:root {
  --bg: #0e0e10;
  --surface: #17171b;
  --surface-2: #1f1f25;
  --border: #2a2a32;
  --text: #e8e8ea;
  --muted: #8a8a93;
  --accent: #ff5d3a;
  --success: #3ecf8e;
  --warn: #ffb454;
  --danger: #ff5252;
  --radius: 10px;
  --maxw: 1100px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: inherit; }
.muted { color: var(--muted); font-size: 0.9em; }

.topbar {
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
}
.brand {
  font-weight: 600;
  text-decoration: none;
  letter-spacing: -0.01em;
}

.container {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 32px 24px;
}

.hero h1 { font-size: 2.2em; margin-bottom: 0.4em; }
.hero .lead { font-size: 1.1em; max-width: 640px; }

.session-header { margin-bottom: 24px; }
.session-header h1 { margin-bottom: 4px; }
.status {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 0.85em;
}
.status-generating { background: var(--warn); color: #1a1a1f; }
.status-ready { background: var(--surface-2); }
.status-all_approved { background: var(--success); color: #0a1a14; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}

.banner {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 20px;
}
.banner-success {
  background: rgba(62, 207, 142, 0.12);
  border: 1px solid var(--success);
  color: var(--success);
}

.upload-form { display: grid; gap: 14px; max-width: 520px; }
.upload-form label { display: grid; gap: 4px; font-size: 0.9em; color: var(--muted); }
.upload-form input[type="file"] {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px;
  border-radius: 6px;
}
button {
  background: var(--accent);
  color: white;
  border: 0;
  padding: 10px 18px;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.95em;
}
button:hover { filter: brightness(1.1); }

.ref-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin: 16px 0;
}
.ref-grid figure { margin: 0; }
.ref-grid img {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 6px;
  background: var(--surface-2);
}
.ref-grid figcaption { color: var(--muted); font-size: 0.8em; margin-top: 4px; }

.references summary { cursor: pointer; padding: 8px 0; color: var(--muted); }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}
.digital-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.digital-card.approval-approved { border-color: var(--success); }
.digital-card.approval-needs_work { border-color: var(--warn); }
.digital-card header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.digital-card h3 { margin: 0; font-size: 1em; }
.badge {
  font-size: 0.7em;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.badge.approved { background: var(--success); color: #0a1a14; }
.badge.needs-work { background: var(--warn); color: #1a1a1f; }

.image-slot {
  background: var(--surface-2);
  aspect-ratio: 9 / 16;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.image-slot img { width: 100%; height: 100%; object-fit: cover; }
.image-slot.empty, .image-slot.generating {
  flex-direction: column;
  gap: 12px;
}

.score-details summary { cursor: pointer; font-size: 0.9em; }
.score-list {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  font-size: 0.85em;
}
.score-list li {
  display: grid;
  grid-template-columns: 1.5fr auto 1fr;
  gap: 6px;
  padding: 2px 0;
  align-items: baseline;
}
.score-list .dim { color: var(--muted); }
.score-list .val { font-variant-numeric: tabular-nums; }
.score-list .note { font-size: 0.85em; }

.last-tweak { font-size: 0.85em; }

.approval-row { display: flex; gap: 8px; }
.approval-row form { flex: 1; }
.approval-row button { width: 100%; }
.btn-approve { background: var(--success); color: #0a1a14; }
.btn-needs-work { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }

.tweak-form { display: grid; gap: 6px; }
.tweak-form label { display: grid; gap: 4px; font-size: 0.85em; color: var(--muted); }
.tweak-form textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 8px;
  font-family: inherit;
  resize: vertical;
}
.btn-tweak { background: var(--accent); }

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  margin: 16px auto;
}
.spinner.small { width: 24px; height: 24px; border-width: 2px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Variation gallery — 4-up grid for the headshot iteration loop. */
.variation-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
  margin: 16px 0;
}
.variation-tile {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color 0.15s;
}
.variation-tile.selected { border-color: var(--accent); }
.variation-tile .image-slot { aspect-ratio: 3 / 4; }
.variation-tile .score { margin: 0; font-size: 0.75em; text-align: center; }
.variation-actions { display: flex; flex-direction: column; gap: 4px; }
.variation-actions form { margin: 0; }
.variation-actions button {
  width: 100%;
  font-size: 0.8em;
  padding: 6px 8px;
}
.btn-toggle {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-toggle.active { background: var(--accent); color: #0a1a14; border-color: var(--accent); }

.variation-controls { display: grid; gap: 12px; margin-top: 16px; }
.variation-controls form { margin: 0; }
.variation-controls > form:first-child { display: flex; gap: 12px; align-items: center; }
.variation-pending { text-align: center; padding: 24px; }

/* Reference-pool strip — shows what's actually fed into the next round. */
.ref-pool {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin: 12px 0;
}
.ref-pool h3 { margin: 0 0 8px; font-size: 0.9em; color: var(--muted); font-weight: 500; }
.ref-pool .ref-grid {
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 8px;
  margin: 0;
}
.ref-pool .ref-grid img { aspect-ratio: 3 / 4; }
.ref-pool .ref-grid figcaption { font-size: 0.7em; }

/* Per-thumbnail (x) remove button on ref pool entries. */
.ref-pool-item { position: relative; margin: 0; }
.ref-remove-form { position: absolute; top: 4px; right: 4px; margin: 0; }
.btn-remove-ref {
  width: 22px;
  height: 22px;
  padding: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  border: none;
  font-size: 0.9em;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-remove-ref:hover { background: rgba(220, 50, 50, 0.85); }

/* Round labels + previous-rounds collapsed history. */
.round-label {
  margin: 16px 0 8px;
  font-size: 0.85em;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.previous-rounds {
  margin-top: 8px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.previous-rounds summary {
  cursor: pointer;
  padding: 6px 0;
  color: var(--muted);
  font-size: 0.9em;
}
.previous-rounds .variation-grid { margin: 8px 0 16px; }

/* Horizontal lineage tree — root (refs) on the left, rounds flowing right.
   Position-relative anchors the SVG edge layer over the scrollable area. */
.lineage-tree {
  position: relative;
  display: flex;
  flex-direction: row;
  gap: 16px;
  overflow-x: auto;
  padding: 12px 4px 16px;
  margin: 16px 0;
  scroll-behavior: smooth;
}
.lineage-edges {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 0;
  overflow: visible;
}
.lineage-edges .edge {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linecap: round;
  opacity: 0.55;
}
/* Ensure columns sit above the edge layer so images aren't visually
   tinted by the strokes. */
.lineage-column { position: relative; z-index: 1; }
.lineage-column {
  flex: 0 0 auto;
  width: 170px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.lineage-column.ref-column {
  width: 160px;
  border-right: 1px dashed var(--border);
  padding-right: 16px;
}
.lineage-column .round-label {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 4px;
}
.round-current {
  background: var(--accent);
  color: #0a1a14;
  font-size: 0.65em;
  padding: 2px 6px;
  border-radius: 999px;
  letter-spacing: 0.04em;
  text-transform: lowercase;
}
.ref-stack { display: flex; flex-direction: column; gap: 8px; }
.ref-stack .ref-pool-item img {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 6px;
  background: var(--surface-2);
}
.ref-stack figcaption {
  color: var(--muted);
  font-size: 0.7em;
  margin-top: 4px;
  text-align: center;
}

/* Vertical stack of compact variation tiles — one tile per row inside
   each round column so all 4 variations align in a single line beneath
   the round header. */
.variation-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.variation-stack .variation-tile {
  padding: 4px;
  gap: 4px;
}
.variation-stack .image-slot { aspect-ratio: 3 / 4; }
.variation-stack .score { font-size: 0.7em; margin: 0; }
.variation-stack .variation-actions button {
  font-size: 0.7em;
  padding: 4px 6px;
}

.round-pending { opacity: 0.7; }
.round-pending .variation-pending { padding: 24px 8px; text-align: center; }

/* Tldraw canvas mount point (variation gallery).
   Tldraw needs an explicit-height parent or it collapses to 0. */
.tldraw-canvas {
  position: relative;
  width: 100%;
  height: 700px;
  margin: 16px 0;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.tldraw-canvas .tl-container {
  background: var(--bg);
  /* Used by our custom LineGrid component (see frontend/canvas.tsx).
     Default dark-mode grid in tldraw is hsl(0,0%,40%) — too dim
     against our near-black background. */
  --color-grid: hsl(0, 0%, 28%);
}

/* Pending-variation ghost boxes get a live, generating-now look:
     - marching dashes (stroke-dashoffset animation)
     - pulsing stroke opacity (breathing effect)
   Ghost shape IDs are deterministically prefixed "ghost-" — see
   `pushGhosts` in frontend/canvas.tsx — so we can target them by
   prefix on the `data-shape-id` attribute tldraw stamps on each
   shape wrapper. */
@keyframes ghost-march {
  to { stroke-dashoffset: -32; }
}
@keyframes ghost-pulse {
  0%, 100% { stroke-opacity: 0.4; }
  50% { stroke-opacity: 1; }
}
@keyframes ghost-fill-pulse {
  0%, 100% { fill-opacity: 0.08; }
  50% { fill-opacity: 0.32; }
}
.tldraw-canvas [data-shape-id^="shape:ghost-"] svg path,
.tldraw-canvas [data-shape-id^="shape:ghost-"] svg rect {
  animation:
    ghost-march 0.9s linear infinite,
    ghost-pulse 1.4s ease-in-out infinite,
    ghost-fill-pulse 1.4s ease-in-out infinite;
}

/* Overlay scaffolding — the host div is pointer-events: none so it
   doesn't intercept Tldraw drags. Individual button surfaces opt back
   into pointer events via `pointer-events: auto`. */
.canvas-shape-menu,
.canvas-step-prompt,
.canvas-toolbar {
  pointer-events: auto;
}

/* On-canvas Step 2 upload prompt — anchored to a dashed placeholder
   shape positioned right of the locked headshot. */
.canvas-step-prompt {
  position: absolute;
  z-index: 10;
  width: 200px;
  padding: 14px 14px 12px;
  background: rgba(20, 20, 22, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  color: var(--text);
}
.canvas-step-prompt h3 {
  margin: 0 0 6px;
  font-size: 0.95em;
  font-weight: 600;
}
.canvas-step-prompt p {
  margin: 0 0 12px;
  font-size: 0.8em;
  color: rgba(255, 255, 255, 0.65);
  line-height: 1.35;
}

/* Measurements form — wider than the file-upload prompt because it
   has several numeric rows. Anchored to the dashed "measurements-
   prompt" shape via pageToViewport in canvas.tsx. */
.canvas-measurements-prompt { width: 260px; }
.canvas-headshot-prompt { width: 280px; }
.canvas-closeup-prompt { width: 320px; }
.canvas-closeup-prompt details {
  margin-bottom: 10px;
}
.canvas-closeup-prompt details summary {
  cursor: pointer;
  font-size: 0.78em;
  color: rgba(255, 255, 255, 0.55);
  padding: 4px 0;
}
.canvas-closeup-prompt details ul {
  margin: 6px 0 0;
  padding-left: 16px;
  font-size: 0.78em;
  line-height: 1.45;
  color: rgba(255, 255, 255, 0.7);
}
.canvas-closeup-prompt details li { margin-bottom: 4px; }
.canvas-closeup-prompt form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.canvas-closeup-prompt .canvas-file-input {
  font-size: 0.8em;
  color: rgba(255, 255, 255, 0.85);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 4px;
  padding: 6px 8px;
}
.canvas-measurements-prompt form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.canvas-measurements-prompt .measurements-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.8em;
  color: rgba(255, 255, 255, 0.85);
}
.canvas-measurements-prompt .measurements-row input {
  width: 90px;
  padding: 4px 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 4px;
  color: #f0f0f0;
  font-family: inherit;
  font-size: 0.9em;
}
.canvas-measurements-prompt details {
  margin-top: 2px;
}
.canvas-measurements-prompt details summary {
  cursor: pointer;
  font-size: 0.78em;
  color: rgba(255, 255, 255, 0.55);
  padding: 4px 0;
}
.canvas-measurements-prompt details[open] {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.canvas-btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.canvas-btn-primary:hover {
  background: var(--accent);
  filter: brightness(1.08);
}

/* Contextual menu — floats next to the selected variation's right edge.
   The (left, top) inline styles come from editor.pageToViewport so this
   tracks pan/zoom/drag automatically via the rAF loop. */
.canvas-shape-menu {
  position: absolute;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px;
  background: rgba(20, 20, 22, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  min-width: 140px;
}

/* Fixed-top-right toolbar — global actions that are always available. */
.canvas-toolbar {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: rgba(20, 20, 22, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
}
.canvas-toolbar-count {
  font-size: 0.75em;
  color: rgba(255, 255, 255, 0.6);
  font-variant-numeric: tabular-nums;
  padding-right: 4px;
  border-right: 1px solid rgba(255, 255, 255, 0.14);
  margin-right: 2px;
}

/* Monochrome button — black surface, white text, white border on hover. */
.canvas-btn {
  background: rgba(255, 255, 255, 0.06);
  color: #f0f0f0;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 0.8em;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
  font-family: inherit;
}
.canvas-btn:hover {
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.35);
}
.canvas-btn-on {
  background: #fff;
  color: #0d0d10;
  border-color: #fff;
}
.canvas-btn-on:hover {
  background: rgba(255, 255, 255, 0.92);
  border-color: rgba(255, 255, 255, 0.92);
}

/* Destructive variant — muted by default, red on hover. */
.canvas-btn-remove {
  color: rgba(255, 255, 255, 0.55);
}
.canvas-btn-remove:hover {
  background: rgba(255, 82, 82, 0.18);
  border-color: rgba(255, 82, 82, 0.55);
  color: #fff;
}

/* Upload control uses the same look as a button. */
.canvas-btn-upload {
  display: inline-flex;
  align-items: center;
}
.canvas-btn-upload input[type="file"] { display: none; }
