:root {
  color-scheme: light;
  --bg: #f6f3ee;
  --panel: #fffaf2;
  --paper: #ffffff;
  --paper-soft: #fffdf8;
  --ink: #17202f;
  --muted: #626b7a;
  --line: #ddd5c8;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --danger: #b42318;
  --shadow: 0 16px 42px rgba(23, 32, 47, .08);
  --radius: 8px;
  --space: 20px;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow-x: hidden;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
.button {
  min-height: 38px;
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  padding: 8px 12px;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  line-height: 1.2;
  white-space: normal;
}

button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
}

button.danger {
  color: var(--danger);
}

button:disabled {
  opacity: .55;
  cursor: not-allowed;
}

input,
select,
textarea {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  padding: 10px 12px;
  outline: none;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, .12);
}

textarea {
  min-height: 118px;
  resize: vertical;
  line-height: 1.45;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.15;
}

h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

h3 {
  min-width: 0;
  font-size: 16px;
  line-height: 1.25;
  overflow-wrap: anywhere;
}

p,
label,
input,
textarea,
select,
button {
  font-size: 14px;
}

label {
  color: var(--muted);
  font-weight: 700;
}

.muted {
  color: var(--muted);
}

.error {
  color: var(--danger);
}

.shell {
  width: 100%;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: clamp(300px, 24vw, 360px) minmax(0, 1fr);
}

.sidebar-toggle {
  position: fixed;
  top: 16px;
  left: 12px;
  z-index: 20;
  width: 40px;
  min-height: 40px;
  padding: 9px;
  display: grid;
  place-items: center;
  border-color: var(--line);
  background: var(--paper);
  box-shadow: 0 8px 22px rgba(23, 32, 47, .12);
}

.sidebar-toggle .hugeicon {
  display: block;
  width: 100%;
  height: 100%;
}

.sidebar-toggle .sidebar-toggle-open {
  display: none;
}

body.sidebar-collapsed .sidebar-toggle .sidebar-toggle-open {
  display: block;
}

body.sidebar-collapsed .sidebar-toggle .sidebar-toggle-close {
  display: none;
}

.sidebar-toggle .sidebar-toggle-close {
  display: block;
}

body.sidebar-collapsed .shell {
  grid-template-columns: 0 minmax(0, 1fr);
}

body.sidebar-collapsed aside {
  padding-inline: 0;
  border-right: 0;
  overflow: hidden;
}

body.sidebar-collapsed main {
  padding-left: 24px;
}

body.sidebar-collapsed main > .topline:first-child {
  padding-left: clamp(52px, 7vw, 64px);
}

aside {
  min-width: 0;
  min-height: 0;
  max-height: 100dvh;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 24px 24px 24px 58px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

main {
  min-width: 0;
  min-height: 0;
  max-height: 100dvh;
  padding: 24px;
  padding-left: 64px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 20px;
  overflow: hidden;
}

.stack {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.row,
.toolbar,
.topline {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.topline {
  justify-content: space-between;
  gap: 14px;
}

.toolbar {
  align-items: flex-start;
}

.generate-toolbar {
  align-items: center;
}

.generate-button {
  min-height: 52px;
  min-width: 180px;
  padding: 14px 22px;
  font-size: 16px;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  min-height: 28px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--muted);
  line-height: 1.2;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #a3a3a3;
  flex: 0 0 auto;
}

.dot.good {
  background: var(--accent);
}

.dot.bad {
  background: var(--danger);
}

.profile {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: var(--radius);
  padding: 14px;
  display: grid;
  gap: 10px;
  text-align: left;
  cursor: pointer;
  color: inherit;
}

.profile.active {
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}

.profile strong {
  display: block;
  min-width: 0;
  font-size: 15px;
  overflow-wrap: anywhere;
}

.profile .meta {
  min-width: 0;
  display: grid;
  gap: 5px;
  color: var(--muted);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.workbench {
  --prompt-panel-width: 380px;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(280px, var(--prompt-panel-width)) 10px minmax(0, 1fr);
  gap: 14px;
  overflow: hidden;
}

.panel-resizer {
  min-height: 0;
  border-radius: 999px;
  cursor: col-resize;
  background: transparent;
  position: relative;
}

.panel-resizer::before {
  content: "";
  position: absolute;
  inset: 8px 3px;
  border-radius: 999px;
  background: var(--line);
}

.panel-resizer:hover::before,
body.resizing-panel .panel-resizer::before {
  background: var(--accent);
}

body.resizing-panel {
  cursor: col-resize;
  user-select: none;
}

.panel,
.results {
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.panel {
  padding: 18px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.results {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 14px;
  padding: 18px;
  overflow: hidden;
}

.form-grid {
  min-width: 0;
  display: grid;
  gap: 14px;
}

.field {
  min-width: 0;
  display: grid;
  gap: 7px;
}

.compact-grid {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.param-grid {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.switch {
  min-height: 38px;
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-weight: 700;
}

.switch input {
  width: 16px;
  height: 16px;
  padding: 0;
  flex: 0 0 auto;
}

details {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-soft);
  padding: 12px;
}

summary {
  cursor: pointer;
  color: var(--ink);
  font-weight: 800;
  overflow-wrap: anywhere;
}

details .form-grid {
  margin-top: 12px;
}

.file-list {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  color: var(--muted);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.file-pill {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  padding: 8px;
  display: grid;
  gap: 8px;
}

.file-pill img {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 4px;
  object-fit: cover;
  background: #ece7dd;
}

.file-pill strong,
.file-pill span {
  display: block;
  overflow-wrap: anywhere;
}

.file-pill strong {
  color: var(--ink);
  font-size: 12px;
  line-height: 1.25;
}

.file-pill span {
  font-size: 12px;
}

.file-pill button {
  width: 100%;
  min-height: 32px;
  padding: 6px 8px;
}

.dropzone {
  position: relative;
  min-height: 92px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--paper-soft);
  color: var(--muted);
  display: grid;
  place-items: center;
  padding: 16px;
  text-align: center;
  cursor: pointer;
}

.dropzone.dragging {
  border-color: var(--accent);
  background: rgba(15, 118, 110, .08);
  color: var(--accent);
}

.dropzone input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.notice {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper-soft);
  padding: 12px;
  color: var(--muted);
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.image-grid {
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  padding-right: 6px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  grid-auto-rows: 8px;
  gap: 14px;
  align-items: start;
}

.image-item {
  width: 100%;
  padding: 0;
  min-width: 0;
  position: relative;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  overflow: hidden;
  color: inherit;
  text-decoration: none;
  text-align: left;
}

.image-open {
  width: 100%;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  display: block;
  background: transparent;
  color: inherit;
  text-align: left;
}

.image-open img {
  display: block;
  width: 100%;
  object-fit: cover;
  background: #ece7dd;
}

.image-open img {
  aspect-ratio: var(--ratio, 1);
}

.image-open .caption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 58px;
  padding: 8px 10px;
  color: #fff;
  background: linear-gradient(180deg, transparent, rgba(0, 0, 0, .72) 34%, rgba(0, 0, 0, .82));
  font-size: 12px;
  line-height: 1.25;
  overflow: hidden;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .16s ease, transform .16s ease;
}

.image-item:hover .caption,
.image-open:focus-visible .caption {
  opacity: 1;
  transform: translateY(0);
}

.image-ref-button {
  position: absolute;
  top: clamp(6px, 1.8vw, 10px);
  right: clamp(6px, 1.8vw, 10px);
  z-index: 2;
  width: clamp(30px, 7vw, 38px);
  min-height: clamp(30px, 7vw, 38px);
  padding: clamp(6px, 1.5vw, 8px);
  border: 1px solid rgba(255, 255, 255, .72);
  border-radius: 999px;
  background: rgba(24, 33, 47, .72);
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: 0 8px 24px rgba(23, 32, 47, .28);
  opacity: .92;
}

.image-ref-button:hover,
.image-ref-button:focus-visible {
  background: var(--accent);
  opacity: 1;
}

.image-ref-button .hugeicon {
  width: 100%;
  height: 100%;
  display: block;
}

.context-menu {
  position: fixed;
  z-index: 80;
  min-width: 190px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: 0 18px 50px rgba(23, 32, 47, .2);
  padding: 6px;
}

.context-menu[hidden] {
  display: none;
}

.context-menu button {
  width: 100%;
  min-height: 36px;
  border: 0;
  background: transparent;
  text-align: left;
  color: var(--ink);
}

.context-menu button:hover,
.context-menu button:focus-visible {
  background: var(--paper-soft);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 90;
  transform: translateX(-50%);
  max-width: min(420px, calc(100vw - 32px));
  border: 1px solid rgba(23, 32, 47, .16);
  border-radius: 8px;
  background: rgba(24, 33, 47, .95);
  color: #fff;
  padding: 10px 14px;
  box-shadow: 0 18px 50px rgba(23, 32, 47, .24);
  font-weight: 700;
  line-height: 1.35;
  text-align: center;
  overflow-wrap: anywhere;
}

.toast[hidden] {
  display: none;
}

body.modal-open {
  overflow: hidden;
}

.modal[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(23, 32, 47, .48);
}

.modal-panel {
  position: relative;
  z-index: 1;
  width: min(980px, 100%);
  max-height: min(860px, calc(100dvh - 48px));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: 0 24px 80px rgba(23, 32, 47, .22);
  padding: 18px;
  display: grid;
  gap: 16px;
  overflow: auto;
}

.modal-body {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(240px, 420px) minmax(0, 1fr);
  gap: 16px;
}

.modal-image-preview {
  position: relative;
  width: fit-content;
  max-width: 100%;
  align-self: start;
  justify-self: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background-color: #fff;
  background-image:
    linear-gradient(45deg, #d5d5d5 25%, transparent 25%),
    linear-gradient(-45deg, #d5d5d5 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #d5d5d5 75%),
    linear-gradient(-45deg, transparent 75%, #d5d5d5 75%);
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
  overflow: hidden;
}

.modal-image-preview img {
  display: block;
  max-width: min(100%, 420px);
  max-height: min(70dvh, 620px);
  background: transparent;
  object-fit: contain;
}

.modal-ref-action {
  top: clamp(8px, 2vw, 12px);
  right: clamp(8px, 2vw, 12px);
}

.modal-meta {
  min-width: 0;
  display: grid;
  gap: 14px;
  align-content: start;
}

.modal-ref-grid {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}

.modal-ref-item {
  position: relative;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper-soft);
  color: inherit;
  overflow: hidden;
}

.modal-ref-link {
  display: block;
  color: inherit;
  text-decoration: none;
}

.modal-ref-item img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  background: #ece7dd;
}

.modal-ref-add {
  width: clamp(28px, 6vw, 34px);
  min-height: clamp(28px, 6vw, 34px);
  padding: 6px;
}

.modal-ref-item div {
  padding: 8px;
}

.modal-ref-item strong,
.modal-ref-item span {
  display: block;
  overflow-wrap: anywhere;
}

.modal-ref-item strong {
  font-size: 12px;
}

.modal-ref-item span {
  color: var(--muted);
  font-size: 11px;
}

.readonly,
.config-code {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper-soft);
  padding: 10px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.config-code {
  max-height: 360px;
  overflow: auto;
  margin: 0;
  font-size: 12px;
}

@media (max-width: 1180px) {
  .sidebar-toggle {
    position: fixed;
    top: auto;
    right: max(16px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
    left: auto;
    margin: 0;
    z-index: 45;
  }

  .shell {
    grid-template-columns: 1fr;
  }

  body.sidebar-collapsed .shell {
    grid-template-columns: 1fr;
  }

  aside,
  main {
    max-height: none;
    overflow: visible;
  }

  aside {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding-top: 12px;
  }

  body.sidebar-collapsed aside {
    max-height: 0;
    padding: 0;
    border-bottom: 0;
    overflow: hidden;
  }

  main {
    grid-template-rows: auto auto;
  }

  body.sidebar-collapsed main {
    padding-left: 24px;
  }

  body.sidebar-collapsed main > .topline:first-child {
    padding-left: clamp(48px, 12vw, 56px);
  }

  .workbench {
    grid-template-columns: minmax(0, 1fr);
    overflow: visible;
  }

  .panel-resizer {
    display: none;
  }

  .panel,
  .results,
  .image-grid {
    overflow: visible;
  }

  #profileList {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }
}

@media (max-width: 760px) {
  :root {
    --space: 14px;
  }

  aside,
  main {
    padding: 16px;
  }

  body.sidebar-collapsed main {
    padding: 16px;
  }

  body.sidebar-collapsed main > .topline:first-child {
    padding-left: 48px;
  }

  .sidebar-toggle {
    width: 36px;
    min-height: 36px;
    padding: 8px;
    right: max(14px, env(safe-area-inset-right));
    bottom: max(14px, env(safe-area-inset-bottom));
  }

  h1 {
    font-size: 21px;
  }

  .compact-grid,
  .param-grid,
  #profileList {
    grid-template-columns: minmax(0, 1fr);
  }

  .file-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .topline {
    align-items: flex-start;
  }

  .status {
    width: 100%;
    justify-content: flex-start;
  }

  .toolbar button,
  .toolbar .button,
  .generate-button,
  #loginButton,
  #refreshProfiles,
  #loadHistory {
    width: 100%;
  }

  .panel,
  .results {
    padding: 14px;
  }

  textarea {
    min-height: 104px;
  }

  .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }

  .modal {
    padding: 12px;
  }

  .modal-panel {
    max-height: calc(100dvh - 24px);
    padding: 14px;
  }

  .modal-body {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 420px) {
  aside,
  main {
    padding: 12px;
  }

  body.sidebar-collapsed main {
    padding: 12px;
  }

  body.sidebar-collapsed main > .topline:first-child {
    padding-left: 44px;
  }

  .sidebar-toggle {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
  }

  .panel,
  .results,
  details,
  .profile {
    border-radius: 6px;
  }

  .image-grid {
    padding-right: 0;
  }

  .file-list {
    grid-template-columns: minmax(0, 1fr);
  }
}
