/* ============================================================
   Style DNA Lab — workbench UI
   Design tokens + components. Dark-default with a light option.
   Mobile-first: base styles target phones, media queries scale up.
   ============================================================ */

:root {
  --font-display: "Space Grotesk", system-ui, sans-serif;
  --font-body: "Inter", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;

  --accent: #6a4bff;
  --accent-2: #c9f24d; /* "matched" signal */
  --accent-ink: #ffffff;
  --danger: #ff6b6b;

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-2: 0 18px 50px -24px rgba(0, 0, 0, 0.6);
  --maxw: 1280px;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);

  /* Fluid type scale (clamped for phone → desktop) */
  --fs-title: clamp(24px, 5.2vw, 34px);
  --fs-body: clamp(14px, 2.4vw, 15px);

  /* Touch-comfortable hit target */
  --tap: 44px;

  /* iOS safe-area helpers */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

html[data-theme="dark"] {
  --bg: #0e0f13;
  --bg-grad: radial-gradient(1200px 600px at 80% -10%, rgba(106, 75, 255, 0.14), transparent 60%);
  --surface: #16181f;
  --surface-2: #1c1f29;
  --border: #272b36;
  --border-strong: #353a47;
  --text: #eceef2;
  --muted: #9aa0ad;
  --faint: #6b7280;
}

html[data-theme="light"] {
  --bg: #f5f6f3;
  --bg-grad: radial-gradient(1200px 600px at 80% -10%, rgba(106, 75, 255, 0.1), transparent 60%);
  --surface: #ffffff;
  --surface-2: #f1f2ee;
  --border: #e4e6e0;
  --border-strong: #d2d5cc;
  --text: #15171c;
  --muted: #5b616b;
  --faint: #8a8f99;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-body);
  font-size: var(--fs-body);
  color: var(--text);
  background: var(--bg-grad), var(--bg);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  line-height: 1.5;
  overflow-x: hidden; /* no horizontal scroll, ever */
}

img {
  max-width: 100%;
}

em {
  font-style: normal;
}

/* The `hidden` attribute must always win over component display rules
   (.modal/.lightbox set display, which would otherwise override [hidden]). */
[hidden] {
  display: none !important;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Top bar ─────────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + var(--safe-t));
  padding-left: calc(16px + var(--safe-l));
  padding-right: calc(16px + var(--safe-r));
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  /* On phones the tabs wrap to their own row below the brand. */
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  min-width: 0;
}

.brand__mark {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: conic-gradient(from 210deg, var(--accent), var(--accent-2), var(--accent));
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12) inset, 0 0 18px -4px var(--accent);
}

.brand__name {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand__name em {
  color: var(--muted);
  font-weight: 500;
  font-size: 11px;
  letter-spacing: 0;
}

/* Tabs: a horizontally scrollable pill bar on phones. */
.tabs {
  order: 3; /* below brand + actions on phones */
  display: flex;
  gap: 2px;
  width: 100%;
  margin: 2px 0 0;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  appearance: none;
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  font-size: 14px;
  min-height: 36px;
  padding: 7px 16px;
  border-radius: 999px;
  cursor: pointer;
  scroll-snap-align: center;
  transition: color 0.18s var(--ease), background 0.18s var(--ease);
}

.tab:hover {
  color: var(--text);
}

.tab.is-active {
  color: var(--accent-ink);
  background: var(--accent);
}

.topbar__actions {
  order: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.chip {
  font-size: 11px;
  color: var(--accent-2);
  line-height: 1;
  white-space: nowrap;
}

.chip--model {
  font-family: var(--font-mono);
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 4px 8px;
  border-radius: 999px;
}

.chip--model:empty {
  display: none;
}

.icon-btn {
  appearance: none;
  width: var(--tap);
  height: var(--tap);
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 16px;
  transition: border-color 0.18s var(--ease), transform 0.18s var(--ease);
}

.icon-btn:hover {
  border-color: var(--border-strong);
  transform: translateY(-1px);
}

/* On tablet+ the tabs centre inline between brand and actions. */
@media (min-width: 720px) {
  .topbar {
    gap: 18px;
    padding: 14px 22px;
    flex-wrap: nowrap;
  }
  .tabs {
    order: 0;
    width: auto;
    margin: 0 auto;
    overflow-x: visible;
  }
  .topbar__actions {
    order: 0;
    margin-left: 0;
  }
  .brand__name {
    flex-direction: row;
    align-items: baseline;
    gap: 8px;
  }
  .brand__name em {
    font-size: inherit;
  }
}

/* ── Stage / panels ──────────────────────────────────────── */
.stage {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 22px 16px calc(40px + var(--safe-b));
  padding-left: calc(16px + var(--safe-l));
  padding-right: calc(16px + var(--safe-r));
}

@media (min-width: 720px) {
  .stage {
    padding: 30px 22px 60px;
  }
}

.panel {
  display: none;
  animation: panelIn 0.4s var(--ease);
}

.panel.is-active {
  display: block;
}

@keyframes panelIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
}

/* Workbench: single column on phones; 2-col on wide desktops. */
.workbench {
  display: grid;
  grid-template-columns: 1fr;
  gap: 22px;
  align-items: start;
}

@media (min-width: 1024px) {
  .workbench {
    grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
    gap: 26px;
  }
}

.controls {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 18px;
  box-shadow: var(--shadow-1);
}

@media (min-width: 720px) {
  .controls {
    padding: 26px;
  }
}

.step-eyebrow {
  margin: 0 0 8px;
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent-2);
}

.panel-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--fs-title);
  letter-spacing: -0.02em;
  line-height: 1.05;
  margin: 0 0 10px;
}

.panel-lede {
  margin: 0 0 22px;
  color: var(--muted);
  max-width: 52ch;
}

/* ── Fields ──────────────────────────────────────────────── */
.field {
  display: flex;
  flex-direction: column;
  gap: 7px;
  margin-bottom: 16px;
}

.field__label {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.field__sub {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}

.field__hint {
  font-size: 12px;
  color: var(--faint);
  font-family: var(--font-mono);
  align-self: flex-end;
}

.muted {
  color: var(--faint);
  font-weight: 400;
}

.input {
  width: 100%;
  font: inherit;
  font-size: 16px; /* ≥16px stops iOS auto-zoom on focus */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 11px 13px;
  min-height: var(--tap);
  transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}

@media (min-width: 720px) {
  .input {
    font-size: 15px;
  }
}

.input:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
}

.textarea {
  resize: vertical;
  min-height: 64px;
  line-height: 1.55;
}

select.input {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 34px;
}

/* Two-up row collapses to stacked on very small screens. */
.row {
  display: grid;
  grid-template-columns: 1fr 110px;
  gap: 14px;
}

@media (max-width: 400px) {
  .row {
    grid-template-columns: 1fr;
  }
}

.field--narrow {
  margin-bottom: 16px;
}

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  appearance: none;
  font: inherit;
  font-weight: 600;
  border-radius: var(--r-md);
  border: 1px solid transparent;
  padding: 12px 18px;
  min-height: var(--tap);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  transition: transform 0.15s var(--ease), filter 0.15s var(--ease), background 0.15s var(--ease);
}

.btn:disabled {
  opacity: 0.6;
  cursor: progress;
}

.btn--primary {
  width: 100%;
  color: var(--accent-ink);
  background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 92%, white), var(--accent));
  box-shadow: 0 10px 26px -12px var(--accent);
}

.btn--primary:hover:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.06);
}

.btn--ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--muted);
}

.btn--ghost:hover {
  color: var(--text);
  border-color: var(--border-strong);
}

.btn--sm {
  padding: 6px 12px;
  min-height: 36px;
  font-size: 13px;
  border-radius: 9px;
  width: auto;
}

.btn--danger {
  border-color: color-mix(in srgb, var(--danger) 50%, var(--border));
  color: var(--danger);
}

.btn--danger:hover {
  border-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
}

.btn__spinner {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  animation: spin 0.7s linear infinite;
}

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

.form-note {
  margin: 12px 0 0;
  font-size: 12px;
  color: var(--faint);
  text-align: center;
}

/* ── Step blocks (Style Match) ───────────────────────────── */
.stepblock {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 14px;
  margin-bottom: 20px;
}

.stepblock__no {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  border: 1px solid var(--border);
  border-radius: 9px;
  height: 30px;
  display: grid;
  place-items: center;
  background: var(--surface-2);
}

.stepblock__body {
  min-width: 0;
}

.stepblock__body > .field__label {
  display: block;
  margin-bottom: 9px;
}

/* ── Dropzone + thumbs ───────────────────────────────────── */
.dropzone {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--r-md);
  background: var(--surface-2);
  padding: 18px;
  cursor: pointer;
  transition: border-color 0.18s var(--ease), background 0.18s var(--ease);
  text-align: center;
}

.dropzone:hover,
.dropzone:focus-visible,
.dropzone.is-drag {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--surface-2));
  outline: none;
}

.dropzone__hint strong {
  display: block;
  font-weight: 600;
  font-size: 14px;
}

.dropzone__hint span {
  font-size: 12px;
  color: var(--faint);
}

.thumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  margin-top: 14px;
}

.thumbs:empty {
  display: none;
}

.thumb {
  position: relative;
  width: 62px;
  height: 62px;
  border-radius: 9px;
  overflow: hidden;
  border: 1px solid var(--border-strong);
  background: var(--bg);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.thumb__remove {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.66);
  color: #fff;
  cursor: pointer;
  font-size: 11px;
  line-height: 22px;
  padding: 0;
}

/* ── Signature: Style DNA barcode ────────────────────────── */
.dna {
  margin-top: 14px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-2);
  overflow: hidden;
  animation: panelIn 0.4s var(--ease);
}

.dna__bar {
  display: flex;
  height: 30px;
}

.dna__bar > span {
  flex: 1 1 auto;
  min-width: 4px;
  transition: flex-grow 0.3s var(--ease);
}

/* palette legend: swatch + hex + share% */
.dna__legend {
  display: flex;
  flex-wrap: wrap;
  gap: 5px 10px;
  padding: 10px 12px 2px;
}

.dna__legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--muted);
}

.dna__legend i {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  border: 1px solid rgba(128, 128, 128, 0.35);
}

.dna__legend b {
  color: var(--text);
  font-weight: 600;
}

.dna__meta {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
  padding: 11px 12px;
}

.dna__meta li {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 999px;
}

.dna__meta li b {
  color: var(--text);
  font-weight: 600;
  text-transform: none;
}

/* a quiet section divider label inside the DNA panel */
.dna__section {
  font-family: var(--font-mono);
  font-size: 9.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--faint);
  padding: 8px 12px 0;
}

/* ── Preserve toggles ────────────────────────────────────── */
.preserve {
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 14px;
  margin: 0 0 18px;
}

.preserve legend {
  padding: 0 6px;
}

.preserve__grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.toggle {
  cursor: pointer;
  user-select: none;
}

.toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.toggle span {
  display: inline-block;
  font-size: 12.5px;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  color: var(--muted);
  text-transform: capitalize;
  transition: all 0.15s var(--ease);
}

.toggle input:checked + span {
  color: #10240a;
  background: var(--accent-2);
  border-color: var(--accent-2);
  font-weight: 600;
}

.toggle input:focus-visible + span {
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
}

/* ── Memory panel (collapsible, shared across modes) ─────── */
.memory {
  margin: 0 0 18px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-2);
  overflow: hidden;
}

.memory__summary {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 11px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  list-style: none;
}

.memory__summary::-webkit-details-marker {
  display: none;
}

.memory__summary:hover {
  color: var(--text);
}

.memory__summary::before {
  content: "▸";
  font-size: 10px;
  color: var(--accent);
  transition: transform 0.18s var(--ease);
}

.memory[open] .memory__summary::before {
  transform: rotate(90deg);
}

.memory__badge {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--faint);
}

.memory__body {
  padding: 0 14px 14px;
}

.memory__hint {
  margin: 0 0 8px;
  font-size: 11.5px;
  color: var(--faint);
}

.memory__textarea {
  width: 100%;
  min-height: 100px;
  resize: vertical;
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 10px 12px;
}

.memory__textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.memory__footer {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.memory__count {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--faint);
}

/* ── Prompt preview ──────────────────────────────────────── */
.prompt-preview {
  margin: 0 0 18px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-2);
}

.prompt-preview summary {
  cursor: pointer;
  padding: 11px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.prompt-preview summary:hover {
  color: var(--text);
}

.code {
  font-family: var(--font-mono);
  font-size: 12.5px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  color: var(--text);
}

.code--inline {
  padding: 0 14px 14px;
  color: var(--muted);
  max-height: 260px;
  overflow: auto;
}

/* ── Results / gallery ───────────────────────────────────── */
.results {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 18px;
  box-shadow: var(--shadow-1);
  min-height: 220px;
}

@media (min-width: 720px) {
  .results {
    padding: 22px;
  }
}

/* Only sticky on real desktop two-column layouts. */
@media (min-width: 1024px) {
  .results {
    position: sticky;
    top: 86px;
    min-height: 280px;
  }
}

.results__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.results__title {
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 600;
  margin: 0;
}

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

.gallery:empty {
  display: none;
}

.card {
  position: relative;
  border-radius: var(--r-md);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface-2);
  display: flex;
  flex-direction: column;
}

.card__media {
  position: relative;
  aspect-ratio: 1 / 1;
}

.card__media img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  opacity: 0;
  transition: opacity 0.5s var(--ease);
}

.card__media img.is-loaded {
  opacity: 1;
}

.card__lineage {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--faint);
  padding: 6px 9px 0;
}

.card__actions {
  position: absolute;
  inset: auto 0 0 0;
  display: flex;
  gap: 6px;
  padding: 9px;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.66), transparent);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.2s var(--ease), transform 0.2s var(--ease);
}

.card:hover .card__actions,
.card:focus-within .card__actions {
  opacity: 1;
  transform: none;
}

/* On touch devices (no hover) keep actions visible. */
@media (hover: none) {
  .card__actions {
    opacity: 1;
    transform: none;
    position: static;
    background: var(--surface-2);
    border-top: 1px solid var(--border);
  }
}

.card__btn {
  flex: 1;
  appearance: none;
  border: 0;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 8px 0;
  min-height: 34px;
  cursor: pointer;
  backdrop-filter: blur(6px);
  transition: background 0.15s var(--ease);
}

.card__btn:hover {
  background: rgba(255, 255, 255, 0.28);
}

@media (hover: none) {
  .card__btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    backdrop-filter: none;
  }
}

/* Inline refine box that drops below a card */
.refine {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 9px;
  display: flex;
  gap: 7px;
  align-items: stretch;
}

.refine input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 8px 10px;
}

.refine input:focus-visible {
  outline: none;
  border-color: var(--accent);
}

.refine button {
  flex: 0 0 auto;
}

/* skeleton */
.skeleton {
  position: relative;
  border-radius: var(--r-md);
  border: 1px solid var(--border);
  background: var(--surface-2);
  aspect-ratio: 1 / 1;
  overflow: hidden;
}

.skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent,
    color-mix(in srgb, var(--text) 9%, transparent),
    transparent
  );
  animation: shimmer 1.3s infinite;
}

@keyframes shimmer {
  100% {
    transform: translateX(100%);
  }
}

.empty {
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--faint);
  min-height: 200px;
  font-size: 14px;
  padding: 20px;
}

.empty[hidden] {
  display: none;
}

/* ── Library ─────────────────────────────────────────────── */
.library {
  max-width: var(--maxw);
  margin: 0 auto;
}

.library__head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 20px;
}

.library__tools {
  display: flex;
  gap: 8px;
}

.admin-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  padding: 14px;
  margin-bottom: 18px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
}

.admin-bar[hidden] {
  display: none;
}

.admin-bar__field {
  flex: 1 1 240px;
  margin: 0;
}

.admin-bar__actions {
  display: flex;
  gap: 8px;
}

.admin-bar__state {
  flex-basis: 100%;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--muted);
}

/* CSS-columns masonry — cheap, no JS layout thrash. */
.grid-masonry {
  column-gap: 14px;
  column-count: 2;
}

@media (min-width: 560px) {
  .grid-masonry {
    column-count: 3;
  }
}

@media (min-width: 900px) {
  .grid-masonry {
    column-count: 4;
  }
}

@media (min-width: 1200px) {
  .grid-masonry {
    column-count: 5;
  }
}

.grid-masonry:empty {
  display: none;
}

.lib-card {
  break-inside: avoid;
  margin: 0 0 14px;
  border-radius: var(--r-md);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s var(--ease), transform 0.15s var(--ease);
  animation: panelIn 0.3s var(--ease);
}

.lib-card:hover {
  border-color: var(--border-strong);
  transform: translateY(-2px);
}

.lib-card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.lib-card__media {
  position: relative;
  display: block;
  background: var(--surface-2);
  min-height: 80px;
}

.lib-card__media img {
  width: 100%;
  display: block;
  opacity: 0;
  transition: opacity 0.4s var(--ease);
}

.lib-card__media img.is-loaded {
  opacity: 1;
}

.lib-card__foot {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
}

.lib-card__tag {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent-2);
}

.lib-card__label {
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lib-card__del {
  margin-left: auto;
  flex: 0 0 auto;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--faint);
  border-radius: 7px;
  width: 26px;
  height: 26px;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
}

.lib-card__del:hover {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 50%, var(--border));
}

/* failed generation card */
.lib-card--error .lib-card__media {
  display: grid;
  place-items: center;
  min-height: 120px;
  background: color-mix(in srgb, var(--danger) 8%, var(--surface-2));
  color: var(--danger);
  font-family: var(--font-mono);
  font-size: 12px;
  text-align: center;
  padding: 16px;
}

.lib-card--error {
  border-color: color-mix(in srgb, var(--danger) 35%, var(--border));
}

/* soft-deleted (admin view) */
.lib-card--deleted {
  opacity: 0.55;
  border-style: dashed;
}

.lib-card__badge {
  font-family: var(--font-mono);
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--border));
  border-radius: 999px;
  padding: 1px 6px;
}

.lib-sentinel {
  height: 1px;
}

.lib-status {
  text-align: center;
  color: var(--faint);
  font-size: 12.5px;
  padding: 16px 0;
  min-height: 20px;
}

/* ── Docs ────────────────────────────────────────────────── */
.docs {
  max-width: 960px;
  margin: 0 auto;
}

.docs__head {
  margin-bottom: 28px;
}

.docs__head code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  color: var(--accent-2);
  word-break: break-all;
}

.docs__grid {
  display: grid;
  gap: 18px;
}

.doc-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
}

.doc-card__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.doc-card__title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 15px;
  margin: 0;
}

.doc-card__tag {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.doc-card__desc {
  color: var(--muted);
  font-size: 13.5px;
  margin: 0;
  padding: 14px 18px 0;
}

.codeblock {
  position: relative;
  margin: 14px 18px 18px;
}

.codeblock pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 14px 15px;
  overflow: auto;
  max-height: 340px;
}

.codeblock__copy {
  position: absolute;
  top: 9px;
  right: 9px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--muted);
  border-radius: 8px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  cursor: pointer;
}

.codeblock__copy:hover {
  color: var(--text);
}

.codeblock__copy.is-copied {
  color: #10240a;
  background: var(--accent-2);
  border-color: var(--accent-2);
}

/* ── Lightbox / detail view ──────────────────────────────── */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.9);
  display: grid;
  place-items: center;
  padding: calc(20px + var(--safe-t)) calc(16px + var(--safe-r)) calc(20px + var(--safe-b)) calc(16px + var(--safe-l));
  animation: panelIn 0.25s var(--ease);
  overflow: auto;
}

.lightbox__inner {
  display: grid;
  gap: 16px;
  width: 100%;
  max-width: 1100px;
  grid-template-columns: 1fr;
  align-items: start;
}

@media (min-width: 860px) {
  .lightbox__inner {
    grid-template-columns: minmax(0, 1.4fr) minmax(0, 0.8fr);
  }
}

.lightbox__img {
  justify-self: center;
  max-width: 100%;
  max-height: 80vh;
  border-radius: var(--r-md);
  box-shadow: var(--shadow-2);
}

.lightbox__meta {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 16px;
  font-size: 13px;
  max-height: 80vh;
  overflow: auto;
}

.lightbox__meta[hidden] {
  display: none;
}

.lightbox__meta dl {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
}

.lightbox__meta dt {
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--faint);
}

.lightbox__meta dd {
  margin: 0;
  color: var(--text);
  word-break: break-word;
}

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

.lightbox__meta h3 {
  font-family: var(--font-display);
  font-size: 15px;
  margin: 0 0 12px;
}

.lightbox__close {
  position: fixed;
  top: calc(14px + var(--safe-t));
  right: calc(16px + var(--safe-r));
  z-index: 61;
  width: var(--tap);
  height: var(--tap);
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
}

/* ── Modal ───────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(0, 0, 0, 0.7);
  display: grid;
  place-items: center;
  padding: calc(20px + var(--safe-t)) 20px calc(20px + var(--safe-b));
  backdrop-filter: blur(4px);
}

.modal__card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 24px;
  box-shadow: var(--shadow-2);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal__title {
  font-family: var(--font-display);
  margin: 0;
  font-size: 20px;
}

.modal__lede {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.modal__err {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

/* ── Toasts ──────────────────────────────────────────────── */
.toasts {
  position: fixed;
  top: calc(70px + var(--safe-t));
  right: calc(14px + var(--safe-r));
  left: calc(14px + var(--safe-l));
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

@media (min-width: 560px) {
  .toasts {
    left: auto;
    max-width: 360px;
  }
}

.toast {
  pointer-events: auto;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--accent);
  border-radius: var(--r-md);
  padding: 12px 15px;
  box-shadow: var(--shadow-2);
  font-size: 13.5px;
  animation: toastIn 0.28s var(--ease);
}

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

.toast--ok {
  border-left-color: var(--accent-2);
}

.toast strong {
  display: block;
  margin-bottom: 2px;
  font-family: var(--font-display);
}

.toast span {
  display: block;
  color: var(--muted);
}

.toast__retry {
  margin-top: 9px;
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  border-radius: 8px;
  padding: 6px 12px;
  min-height: 34px;
  cursor: pointer;
}

.toast__retry:hover {
  border-color: var(--accent);
  color: var(--accent);
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(16px);
  }
}

/* ── Reduced motion ──────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
