/* ---------- tokens ---------- */
:root {
  --bg: #121211;
  --surface: #1b1b19;
  --surface-2: #242421;
  --surface-3: #2e2e2a;
  --line: #2f2f2b;
  --line-strong: #3d3d38;
  --text: #edece6;
  --text-2: #b4b2a9;
  --muted: #85837b;
  --accent: #c8f55a;
  --accent-ink: #141a06;
  --accent-soft: rgb(200 245 90 / 0.12);
  --danger: #ff8a7a;
  --shadow: 0 1px 0 rgb(255 255 255 / 0.03) inset, 0 12px 40px -12px rgb(0 0 0 / 0.6);
  --radius-lg: 22px;
  --radius: 14px;
  --display: 'Bricolage Grotesque', ui-sans-serif, system-ui, sans-serif;
  --sans: 'Bricolage Grotesque', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
  --topbar: 56px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f6f1;
    --surface: #ffffff;
    --surface-2: #f0efe8;
    --surface-3: #e6e4db;
    --line: #e4e2d9;
    --line-strong: #d3d0c4;
    --text: #1c1c19;
    --text-2: #4a4943;
    --muted: #7c7a71;
    --accent: #4f7d00;
    --accent-ink: #ffffff;
    --accent-soft: rgb(79 125 0 / 0.1);
    --danger: #c0392b;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 12px 32px -16px rgb(0 0 0 / 0.18);
    color-scheme: light;
  }
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
svg { width: 20px; height: 20px; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; border-radius: 10px;
  border: 0; background: transparent; color: var(--text-2);
  transition: background .15s, color .15s;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .4; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed; inset: 0 0 auto; z-index: 5;
  height: var(--topbar);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px;
}
.topbar-left { display: flex; align-items: center; gap: 6px; }
.brand { display: none; align-items: center; gap: 6px; color: var(--text); text-decoration: none; font-weight: 600; letter-spacing: -.01em; }
.brand-mark { width: 24px; height: 24px; color: var(--accent); }
.brand span { font-family: var(--display); font-weight: 500; font-size: 18px; letter-spacing: -.02em; color: var(--text-2); }
body[data-view="chat"] .brand { display: inline-flex; }
body[data-view="home"] #new-chat { visibility: hidden; }
body[data-view="chat"] .topbar { background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }

.status {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 5px 11px; border-radius: 999px;
  border: 1px solid var(--line); color: var(--muted); font-size: 12.5px;
}
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.status[data-mode="live"] .status-dot { background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.status[data-mode="mock"] .status-dot { background: #e8c25a; }
.status[data-mode="off"] .status-dot,
.status[data-mode="no-key"] .status-dot,
.status[data-mode="down"] .status-dot { background: var(--danger); }

/* ---------- background animation (home only) ---------- */
.backdrop {
  position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background: var(--bg) url('/media/bg-poster.jpg') center / cover no-repeat;
  transition: opacity .6s ease;
}
.backdrop-video { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Wide screens: show the whole 2:1 frame instead of cropping it, and feather the
   top/bottom edges into the page so there's no visible letterbox. Phones keep cover. */
@media (min-aspect-ratio: 3/2) {
  .backdrop { background: var(--bg); }
  .backdrop-video {
    object-fit: contain;
    -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 14%, #000 86%, transparent 100%);
    mask-image: linear-gradient(to bottom, transparent 0, #000 14%, #000 86%, transparent 100%);
  }
}
.backdrop-scrim {
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse 55% 48% at 50% 50%, color-mix(in srgb, var(--bg) 80%, transparent), transparent 100%),
    linear-gradient(to bottom,
      color-mix(in srgb, var(--bg) 70%, transparent) 0%,
      color-mix(in srgb, var(--bg) 52%, transparent) 30%,
      color-mix(in srgb, var(--bg) 52%, transparent) 65%,
      color-mix(in srgb, var(--bg) 88%, transparent) 100%);
}
body[data-view="chat"] .backdrop { opacity: 0; }

/* Home controls float over the animation: frosted, not opaque. */
body[data-view="home"] .status { background: color-mix(in srgb, var(--bg) 55%, transparent); backdrop-filter: blur(10px); color: var(--text-2); }
.home .composer { background: color-mix(in srgb, var(--surface) 80%, transparent); backdrop-filter: blur(16px) saturate(1.1); }
.home .suggestion { background: color-mix(in srgb, var(--bg) 50%, transparent); backdrop-filter: blur(10px); color: var(--text); }
.home .suggestion:hover { background: color-mix(in srgb, var(--surface) 85%, transparent); }
.home .fineprint { color: var(--text-2); text-shadow: 0 1px 8px var(--bg); }
.hero h1 { text-shadow: 0 2px 30px color-mix(in srgb, var(--bg) 70%, transparent); }

/* ---------- home ---------- */
.home {
  position: relative; z-index: 1;
  height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: var(--topbar) 16px 40px;
  gap: 28px;
}
.hero { display: flex; align-items: center; gap: 14px; animation: rise .6s cubic-bezier(.2,.7,.2,1) both; }
.hero h1 {
  margin: 0;
  font: 650 clamp(36px, 5.6vw, 56px)/1 var(--display);
  font-variation-settings: 'opsz' 96;
  letter-spacing: -.025em;
}
.home .composer-slot { width: min(720px, 100%); animation: rise .6s .06s cubic-bezier(.2,.7,.2,1) both; }
.suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; max-width: 720px; animation: rise .6s .12s cubic-bezier(.2,.7,.2,1) both; }
.suggestion {
  border: 1px solid var(--line); background: transparent; color: var(--text-2);
  padding: 7px 14px; border-radius: 999px; font-size: 13.5px;
  transition: border-color .15s, color .15s, background .15s;
}
.suggestion:hover { border-color: var(--line-strong); color: var(--text); background: var(--surface); }
.fineprint { margin: 0; color: var(--muted); font-size: 12px; text-align: center; }

@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ---------- composer ---------- */
.composer {
  display: flex; flex-direction: column; gap: 6px;
  padding: 14px 12px 10px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  transition: border-color .15s;
}
.composer:focus-within { border-color: var(--line-strong); }
.composer textarea {
  width: 100%; resize: none; border: 0; outline: 0; background: transparent;
  color: var(--text); font: 16px/1.5 var(--sans);
  min-height: 48px; max-height: 220px; padding: 2px 4px 0 0;
}
.home .composer textarea { min-height: 64px; }
.composer textarea::placeholder { color: var(--muted); }
.composer-bar { display: flex; align-items: center; gap: 4px; margin-left: -8px; }
.spacer { flex: 1; }
.toggle {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 11px; border-radius: 999px;
  border: 1px solid var(--line); background: transparent; color: var(--muted); font-size: 13px;
  transition: all .15s;
}
.toggle svg { width: 16px; height: 16px; }
.toggle[aria-pressed="true"] { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 35%, transparent); background: var(--accent-soft); }
.send {
  display: grid; place-items: center;
  width: 36px; height: 36px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink);
  transition: transform .12s, opacity .15s, background .15s;
}
.send svg { width: 18px; height: 18px; }
.send:hover:not(:disabled) { transform: scale(1.05); }
.send:disabled { background: var(--surface-3); color: var(--muted); }
.send[data-stop] { background: var(--text); color: var(--bg); }

/* ---------- workspace ---------- */
.workspace {
  position: fixed; inset: var(--topbar) 0 0;
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(380px, 1fr);
}
.tabs { display: none; }

/* storyboard pane (left) */
.board-pane {
  overflow-y: auto;
  padding: 28px clamp(20px, 3vw, 40px) 40px;
  border-right: 1px solid var(--line);
  background:
    radial-gradient(1200px 500px at 0% 0%, var(--accent-soft), transparent 60%),
    var(--bg);
}
.board-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.eyebrow { margin: 0 0 4px; font-size: 11.5px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.board-head h2 { margin: 0; font: 650 28px/1.1 var(--display); letter-spacing: -.03em; font-variation-settings: 'opsz' 96; }
.board-meta { color: var(--muted); font-size: 13px; white-space: nowrap; }

.board-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.board-grid[data-count="3"] .panel:first-child,
.board-grid[data-count="5"] .panel:first-child { grid-column: 1 / -1; }
.board-grid[data-count="3"] .panel:first-child .panel-art,
.board-grid[data-count="5"] .panel:first-child .panel-art { aspect-ratio: 21 / 9; }

.panel { margin: 0; display: flex; flex-direction: column; gap: 10px; animation: rise .5s calc(var(--i) * 80ms) cubic-bezier(.2,.7,.2,1) both; }
.panel-art {
  position: relative; aspect-ratio: 4 / 3; border-radius: var(--radius);
  overflow: hidden; border: 1px solid var(--line);
  display: grid; place-items: center; padding: 22px;
  background:
    repeating-linear-gradient(135deg, transparent 0 11px, rgb(127 127 127 / .045) 11px 12px),
    linear-gradient(160deg, var(--surface-2), var(--surface));
}
.panel-num {
  position: absolute; top: 12px; left: 12px;
  font: 500 11px/1 var(--sans); letter-spacing: .08em;
  padding: 5px 7px; border-radius: 6px;
  background: var(--bg); color: var(--text-2); border: 1px solid var(--line);
}
.panel-scene { margin: 0; max-width: 34ch; text-align: center; font: 400 15px/1.45 var(--display); color: var(--text-2); }
.panel-tag { position: absolute; bottom: 10px; right: 12px; font-size: 10.5px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.panel-caption { font-size: 14.5px; font-weight: 500; color: var(--text); padding: 0 2px; }

.panel--empty .panel-art { background: transparent; border-style: dashed; border-color: var(--line-strong); }
.panel--empty { opacity: .55; }
.panel--loading .panel-art { background: var(--surface); }
.panel--loading .panel-art::after,
.panel-caption-skel::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 20%, rgb(255 255 255 / .05) 50%, transparent 80%);
  animation: shimmer 1.4s infinite;
}
.panel-caption-skel { position: relative; overflow: hidden; height: 14px; width: 60%; border-radius: 6px; background: var(--surface-2); }
@keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }

.board-note {
  margin-top: 22px; display: flex; flex-direction: column; gap: 4px;
  color: var(--muted); font-size: 14px; max-width: 46ch;
}
.board-note strong { color: var(--text-2); font-weight: 500; }

/* chat pane (right) */
.chat-pane { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.messages { flex: 1; overflow-y: auto; padding: 26px clamp(16px, 2.4vw, 32px) 12px; display: flex; flex-direction: column; gap: 22px; }
.chat-pane .composer-slot { padding: 8px clamp(12px, 2vw, 24px) 18px; }

.msg { display: flex; gap: 12px; animation: rise .3s ease both; }
.msg.is-update { animation: none; }
.msg--user { justify-content: flex-end; }
.bubble {
  max-width: 85%; padding: 10px 15px; border-radius: 18px 18px 6px 18px;
  background: var(--surface-2); white-space: pre-wrap; overflow-wrap: anywhere;
}
.avatar {
  flex: none; width: 28px; height: 28px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
}
.avatar svg { width: 18px; height: 18px; }
.msg-body { min-width: 0; flex: 1; padding-top: 3px; }
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md h4 { margin: 0 0 .75em; }
.md ul, .md ol { padding-left: 1.25em; }
.md li + li { margin-top: .25em; }
.md strong { font-weight: 600; color: var(--text); }
.md h4 { font-size: 15px; font-weight: 600; }
.md blockquote { padding: 10px 14px; border-left: 2px solid var(--accent); background: var(--surface); border-radius: 0 10px 10px 0; color: var(--text); }
.md code { font: 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace; padding: 2px 5px; border-radius: 5px; background: var(--surface-2); }
.md.is-streaming > :last-child::after,
.md.is-streaming:empty::after {
  content: ''; display: inline-block; width: 8px; height: 8px; margin-left: 4px; border-radius: 50%;
  background: var(--accent); vertical-align: middle; animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: .25; transform: scale(.7); } }

.board-pill {
  margin-top: 12px; display: inline-flex; align-items: center; gap: 7px;
  padding: 6px 11px; border-radius: 999px; font-size: 12.5px;
  border: 1px solid var(--line); background: transparent; color: var(--text-2);
}
.board-pill svg { width: 15px; height: 15px; }
.board-pill[aria-current="true"] { border-color: color-mix(in srgb, var(--accent) 40%, transparent); color: var(--accent); background: var(--accent-soft); }
.msg-error { margin-top: 8px; color: var(--danger); font-size: 13.5px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
body[data-streaming] [data-retry] { display: none; }
.msg-error button { border: 1px solid currentColor; background: transparent; color: inherit; border-radius: 999px; padding: 3px 10px; font-size: 12.5px; }

/* ---------- mobile ---------- */
@media (max-width: 860px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto minmax(0, 1fr); }
  .tabs {
    display: flex; gap: 4px; margin: 10px 16px 0; padding: 4px;
    border-radius: 12px; background: var(--surface);
    border: 1px solid var(--line);
  }
  .tab {
    flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    height: 34px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-size: 14px;
  }
  .tab svg { width: 16px; height: 16px; }
  .tab[aria-selected="true"] { background: var(--surface-3); color: var(--text); }
  .board-pane, .chat-pane { grid-row: 2; grid-column: 1; border-right: 0; }
  .workspace[data-tab="chat"] .board-pane,
  .workspace[data-tab="board"] .chat-pane { display: none; }
  .board-pane { padding: 20px 16px 32px; }
  .board-grid { grid-template-columns: 1fr; gap: 16px; }
  .messages { padding: 18px 16px 8px; }
  .chat-pane .composer-slot { padding: 6px 12px max(12px, env(safe-area-inset-bottom)); }
  .hero { flex-direction: column; gap: 10px; text-align: center; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
