:root {
  --bg: #07080b;
  --panel: rgba(17, 19, 25, 0.84);
  --field: #0a0c11;
  --line: rgba(255, 255, 255, 0.085);
  --line-strong: rgba(255, 255, 255, 0.16);
  --text: #eef0f5;
  --muted: #a2a9ba;
  --dim: #7a8296;
  --accent: #eaa45f;
  --accent-2: #f4ce97;
  --accent-ink: #1b1206;
  --danger: #ff9292;
  --ok: #72d79d;
  --radius: 20px;
  --radius-sm: 13px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --fs: clamp(17px, 0.45vw + 15.5px, 20px);
  --control: 56px;
}

* {
  box-sizing: border-box;
}

html {
  color-scheme: dark;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(52% 42% at 14% -4%, rgba(234, 164, 95, 0.14), transparent 66%),
    radial-gradient(46% 38% at 102% 4%, rgba(103, 133, 255, 0.09), transparent 64%),
    radial-gradient(40% 40% at 50% 112%, rgba(234, 164, 95, 0.05), transparent 70%);
}

::selection {
  background: rgba(234, 164, 95, 0.3);
}

[hidden] {
  display: none !important;
}

.shell {
  position: relative;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 22px clamp(14px, 2.4vw, 30px) 30px;
}

/* ---------------------------------------------------------------- top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.logo-mark {
  width: 52px;
  height: 52px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 16px;
  color: var(--accent);
  background: linear-gradient(145deg, rgba(234, 164, 95, 0.22), rgba(234, 164, 95, 0.03));
  border: 1px solid rgba(234, 164, 95, 0.28);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.logo-mark svg {
  width: 28px;
  height: 28px;
}

.brand-text h1 {
  margin: 0;
  font-size: clamp(1.25rem, 1.5vw, 1.6rem);
  font-weight: 650;
  letter-spacing: 0.01em;
}

.brand-text p {
  margin: 2px 0 0;
  font-size: 0.92rem;
  color: var(--dim);
}

.top-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: none;
  max-width: 46vw;
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.02);
  color: var(--muted);
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-dot {
  width: 9px;
  height: 9px;
  flex: none;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 4px rgba(114, 215, 157, 0.12);
}

/* ----------------------------------------------------------------- layout */
.studio {
  display: grid;
  grid-template-columns: minmax(340px, 460px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(14px);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 22px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.field.grow {
  flex: 1 1 auto;
}

label,
.label-row label {
  font-size: 1rem;
  font-weight: 620;
  letter-spacing: 0.01em;
}

.label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.hint {
  margin: 0;
  font-size: 0.9rem;
  color: var(--dim);
}

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

input[type="number"],
select,
textarea {
  width: 100%;
  min-height: var(--control);
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong);
  background: var(--field);
  font-size: 1.05rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

textarea {
  min-height: 150px;
  resize: vertical;
  line-height: 1.5;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
.dropzone:focus-visible {
  outline: none;
  border-color: rgba(234, 164, 95, 0.7);
  box-shadow: 0 0 0 4px rgba(234, 164, 95, 0.18);
}

select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 24px) 50%, calc(100% - 17px) 50%;
  background-size: 7px 7px, 7px 7px;
  background-repeat: no-repeat;
  padding-right: 44px;
}

#preset {
  width: auto;
  min-height: 46px;
  max-width: 55%;
  padding: 8px 40px 8px 14px;
  font-size: 0.98rem;
}

.pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.mp-row {
  display: flex;
  align-items: center;
  gap: 14px;
}

.input-suffix {
  position: relative;
  flex: 0 0 180px;
}

.input-suffix input {
  padding-right: 58px;
}

.input-suffix span {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--dim);
  font-size: 0.98rem;
  pointer-events: none;
}

.dims {
  flex: 1 1 auto;
  min-height: var(--control);
  display: grid;
  place-items: center;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px dashed var(--line-strong);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 1rem;
  white-space: nowrap;
}

/* --------------------------------------------------------------- dropzone */
.dropzone {
  border: 2px dashed var(--line-strong);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.02);
  padding: 18px;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.dropzone:hover,
.dropzone.drag {
  border-color: rgba(234, 164, 95, 0.7);
  background: rgba(234, 164, 95, 0.07);
}

.dropzone-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 20px 8px;
  text-align: center;
  color: var(--muted);
}

.dropzone-empty svg {
  width: 42px;
  height: 42px;
  color: var(--accent);
}

.dropzone-empty strong {
  font-size: 1.15rem;
  color: var(--text);
}

.dropzone-empty span {
  font-size: 0.95rem;
  color: var(--dim);
}

.dropzone-loaded {
  display: flex;
  align-items: center;
  gap: 14px;
}

.dropzone-loaded img {
  width: 84px;
  height: 84px;
  flex: none;
  object-fit: cover;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: #000;
}

.source-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1 1 auto;
}

.source-copy strong {
  font-size: 1.02rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.source-copy span {
  font-size: 0.92rem;
  color: var(--dim);
}

.ghost {
  flex: none;
  min-height: 46px;
  padding: 0 18px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  font-size: 0.98rem;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.ghost:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.08);
  border-color: var(--line-strong);
}

.ghost:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.ghost.armed {
  border-color: rgba(255, 146, 146, 0.65);
  background: rgba(255, 146, 146, 0.14);
  color: var(--danger);
}

/* --------------------------------------------------------------- messages */
.notice,
.error {
  margin: 0;
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  font-size: 1rem;
}

.notice {
  background: rgba(234, 164, 95, 0.1);
  border: 1px solid rgba(234, 164, 95, 0.3);
  color: var(--accent-2);
}

.error {
  background: rgba(255, 146, 146, 0.1);
  border: 1px solid rgba(255, 146, 146, 0.32);
  color: var(--danger);
}

.generate {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  min-height: 66px;
  padding: 0 22px;
  border: 0;
  border-radius: 16px;
  background: linear-gradient(180deg, #f1b271, #e2944a);
  color: var(--accent-ink);
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(234, 164, 95, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  transition: transform 0.12s ease, filter 0.15s ease;
}

.generate:hover:not(:disabled) {
  filter: brightness(1.06);
}

.generate:active:not(:disabled) {
  transform: translateY(1px);
}

.generate:disabled {
  filter: grayscale(0.5) brightness(0.8);
  cursor: not-allowed;
}

.generate kbd {
  padding: 4px 9px;
  border-radius: 8px;
  background: rgba(27, 18, 6, 0.16);
  font-family: var(--mono);
  font-size: 0.85rem;
}

/* ------------------------------------------------------------------ stage */
.stage {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 22px;
  min-width: 0;
}

.stage-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.stage-title-wrap {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.stage-title {
  font-size: 1.15rem;
  font-weight: 650;
}

.stage-sub {
  font-size: 0.95rem;
  color: var(--dim);
}

.stage-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.toggle {
  display: inline-flex;
  padding: 4px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: var(--field);
}

.toggle-btn {
  min-height: 46px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 1.02rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.toggle-btn[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
}

.toggle-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.download {
  width: 50px;
  height: 50px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
}

.download svg {
  width: 24px;
  height: 24px;
}

.canvas-wrap {
  width: 100%;
}

.canvas {
  position: relative;
  width: 100%;
  min-height: 320px;
  max-height: min(72vh, 820px);
  aspect-ratio: var(--ratio);
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.025) 25%, transparent 25% 75%, rgba(255, 255, 255, 0.025) 75%),
    linear-gradient(45deg, rgba(255, 255, 255, 0.025) 25%, transparent 25% 75%, rgba(255, 255, 255, 0.025) 75%);
  background-size: 26px 26px;
  background-position: 0 0, 13px 13px;
  overflow: hidden;
  display: grid;
  place-items: center;
}

.canvas img {
  /* Absolute, so the grid's auto row cannot grow past the canvas: a portrait
     image is letterboxed by object-fit instead of being clipped. */
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

.empty-mark {
  width: 66px;
  height: 66px;
  display: grid;
  place-items: center;
  border-radius: 18px;
  border: 1px solid var(--line-strong);
  color: var(--accent);
}

.empty-mark svg {
  width: 34px;
  height: 34px;
}

.empty p {
  margin: 6px 0 0;
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
}

.empty span {
  font-size: 0.98rem;
  color: var(--dim);
  max-width: 34ch;
}

.progress,
.stage-error {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
  background: rgba(7, 8, 11, 0.78);
  backdrop-filter: blur(8px);
}

.progress-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.progress-copy strong {
  font-size: 1.2rem;
}

.progress-copy span {
  font-size: 1rem;
  color: var(--muted);
}

.spinner {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 4px solid rgba(234, 164, 95, 0.22);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.bar {
  width: min(420px, 80%);
  height: 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.25s ease;
}

.error-mark {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(255, 146, 146, 0.16);
  color: var(--danger);
  font-size: 1.6rem;
  font-weight: 700;
}

.stage-error strong {
  font-size: 1.2rem;
}

.stage-error p {
  margin: 0;
  max-width: 46ch;
  color: var(--muted);
  font-size: 1rem;
}

.stage-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.98rem;
}

.foot-hint {
  color: var(--dim);
  font-family: var(--mono);
  font-size: 0.9rem;
}

.result-info {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
}

.result-row {
  display: flex;
  align-items: baseline;
  gap: 14px;
  font-size: 1rem;
}

.result-label {
  flex: 0 0 92px;
  color: var(--dim);
}

.result-value {
  font-family: var(--mono);
  font-size: 0.98rem;
  word-break: break-word;
}

.prompt-details summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 1rem;
  padding: 4px 0;
}

.prompt-details p {
  margin: 8px 0 0;
  font-size: 1rem;
  color: var(--text);
  white-space: pre-wrap;
}

/* ------------------------------------------------------------- queue/tray */
.tray {
  margin-top: 18px;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.tray-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.history-head {
  margin-top: 8px;
}

.tray-title-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
}

.tray-title {
  font-size: 1.15rem;
  font-weight: 650;
}

.tray-count {
  min-width: 34px;
  padding: 3px 11px;
  border-radius: 999px;
  background: rgba(234, 164, 95, 0.18);
  color: var(--accent-2);
  font-size: 0.95rem;
  font-weight: 650;
  text-align: center;
}

.tray-meta {
  display: flex;
  align-items: center;
  gap: 14px;
}

.tray-note {
  color: var(--dim);
  font-size: 0.92rem;
}

.queue-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.queue-empty,
.history-empty {
  padding: 16px;
  border-radius: var(--radius-sm);
  border: 1px dashed var(--line);
  color: var(--dim);
  font-size: 1rem;
}

.queue-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.025);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.queue-item:hover {
  border-color: var(--line-strong);
}

.queue-item.selected {
  border-color: rgba(234, 164, 95, 0.7);
  background: rgba(234, 164, 95, 0.08);
}

.queue-item img {
  width: 68px;
  height: 68px;
  flex: none;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: #000;
}

.queue-copy {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  flex: 1 1 auto;
}

.queue-prompt {
  font-size: 1.02rem;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.queue-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--dim);
  font-size: 0.92rem;
}

.pill {
  padding: 2px 10px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  font-size: 0.88rem;
}

.pill.running {
  border-color: rgba(234, 164, 95, 0.6);
  color: var(--accent-2);
}

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

.pill.error {
  border-color: rgba(255, 146, 146, 0.5);
  color: var(--danger);
}

.queue-cancel {
  flex: none;
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: rgba(255, 255, 255, 0.03);
  color: var(--muted);
  font-size: 1.2rem;
  cursor: pointer;
}

.queue-cancel:hover {
  color: var(--danger);
  border-color: rgba(255, 146, 146, 0.5);
}

.history-strip {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 6px;
  scrollbar-width: thin;
}

.history-item {
  flex: 0 0 auto;
  width: 132px;
  padding: 0;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.02);
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s ease, transform 0.12s ease;
}

.history-item:hover {
  border-color: var(--line-strong);
  transform: translateY(-1px);
}

.history-item.selected {
  border-color: rgba(234, 164, 95, 0.75);
}

.history-item img {
  display: block;
  width: 100%;
  height: 132px;
  object-fit: cover;
  background: #000;
}

.history-item span {
  display: block;
  padding: 8px 10px;
  font-size: 0.88rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ----------------------------------------------------------------- footer */
.footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-top: 18px;
  padding: 6px 4px;
  color: var(--dim);
  font-size: 0.92rem;
}

.footer-sep {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--line-strong);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  max-width: min(92vw, 560px);
  padding: 14px 22px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: rgba(12, 14, 19, 0.96);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.5);
  font-size: 1.02rem;
  z-index: 40;
}

/* ------------------------------------------------------------ responsive */
@media (max-width: 1024px) {
  .studio {
    grid-template-columns: 1fr;
  }

  .stage {
    order: -1;
  }
}

@media (max-width: 640px) {
  :root {
    --control: 54px;
  }

  .shell {
    padding: 14px 12px 24px;
  }

  .controls,
  .stage,
  .tray {
    padding: 16px;
    gap: 16px;
  }

  .top-meta {
    display: none;
  }

  .pair {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .mp-row {
    flex-wrap: wrap;
  }

  .input-suffix {
    flex: 1 1 140px;
  }

  .generate {
    position: sticky;
    bottom: 12px;
    z-index: 20;
  }

  .generate kbd {
    display: none;
  }

  .stage-actions {
    width: 100%;
    justify-content: space-between;
  }

  .toggle-btn {
    min-height: 48px;
    padding: 0 18px;
  }

  .queue-item img {
    width: 56px;
    height: 56px;
  }

  .history-item,
  .history-item img {
    width: 108px;
  }

  .history-item img {
    height: 108px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
