:root {
  --bg: #F4F2ED;
  --ink: #141413;
  --ink-2: #34332F;
  --muted: #6E6A63;
  --rule: #DDD9CF;
  --surface: #E9E5DC;
  --bubble: #141413;
  --bubble-ink: #F4F2ED;
  --code: #ECE8E0;
  --accent: #C84B31;
  --accent-ink: #FFFFFF;
  --serif: "Instrument Serif", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --sans: "IBM Plex Sans", ui-sans-serif, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --spring: cubic-bezier(.34, 1.4, .64, 1);
  --pad: clamp(16px, 4vw, 40px);
  --col: 46rem;
  --fs: clamp(15px, .35vw + 14px, 16.5px);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0E0E0D; --ink: #EEEBE4; --ink-2: #CFCBC2; --muted: #8E8A82; --rule: #282724;
  --surface: #1A1917; --bubble: #23221F; --bubble-ink: #EEEBE4; --code: #161614;
  --accent: #E8673F; --accent-ink: #120A07;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0E0E0D; --ink: #EEEBE4; --ink-2: #CFCBC2; --muted: #8E8A82; --rule: #282724;
    --surface: #1A1917; --bubble: #23221F; --bubble-ink: #EEEBE4; --code: #161614;
    --accent: #E8673F; --accent-ink: #120A07;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font: 400 var(--fs)/1.65 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::selection { background: var(--accent); color: var(--accent-ink); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

#vortex { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; pointer-events: none; }

/* ---------- shell: one fluid column that reflows at every width ---------- */
.main {
  position: relative; z-index: 1;
  display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh; height: 100dvh;
}
.main > * { min-width: 0; }
.column { width: 100%; max-width: var(--col); margin: 0 auto; position: relative; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) max(var(--pad), env(safe-area-inset-right)) 10px max(var(--pad), env(safe-area-inset-left));
  animation: drop 500ms var(--ease) both;
}
.brand { display: inline-flex; align-items: center; gap: 10px; font: 500 13px/1 var(--mono); letter-spacing: .02em; }
.brand__dot {
  width: 9px; height: 9px; border-radius: 50%; background: var(--accent);
  transition: transform 300ms var(--spring);
}
body.is-busy .brand__dot { animation: beat 1s var(--ease) infinite; }
.new {
  display: inline-flex; align-items: center; gap: 8px; height: 34px; padding: 0 12px; border-radius: 999px;
  font: 500 12px/1 var(--mono); color: var(--muted); border: 1px solid var(--rule);
  transition: color 160ms var(--ease), border-color 160ms var(--ease), transform 160ms var(--ease), opacity 200ms var(--ease);
}
.actions { display: flex; align-items: center; gap: 8px; }
.theme {
  position: relative; width: 34px; height: 34px; display: grid; place-items: center; border-radius: 50%;
  border: 1px solid var(--rule); color: var(--muted);
  transition: color 160ms var(--ease), border-color 160ms var(--ease), transform 160ms var(--ease);
}
.theme:hover { color: var(--ink); border-color: var(--ink); }
.theme:active { transform: scale(.92); }
.theme svg { position: absolute; width: 15px; height: 15px; transition: transform 300ms var(--spring), opacity 200ms var(--ease); }
.theme .i-moon { opacity: 0; transform: rotate(-90deg) scale(.5); }
:root[data-theme="dark"] .theme .i-sun { opacity: 0; transform: rotate(90deg) scale(.5); }
:root[data-theme="dark"] .theme .i-moon { opacity: 1; transform: none; }
.new[hidden] { display: none; }
.new:not([hidden]) { animation: pop 260ms var(--spring) both; }
.new:hover { color: var(--ink); border-color: var(--ink); }
.new:active { transform: scale(.96); }
.new svg { width: 13px; height: 13px; transition: transform 240ms var(--ease); }
.new:hover svg { transform: rotate(90deg); }

.log { overflow-y: auto; overscroll-behavior: contain; padding: 0 var(--pad); scroll-behavior: smooth; }
.messages { display: flex; flex-direction: column; gap: clamp(18px, 2.4vw, 28px); padding: 12px 0 32px; }

/* ---------- hero ---------- */
.empty {
  min-height: 100%;
  display: flex; flex-direction: column; justify-content: center;
  padding: clamp(24px, 10vh, 120px) 0 clamp(24px, 6vh, 64px);
  transition: opacity 220ms var(--ease), transform 220ms var(--ease);
}
.empty[hidden] { display: none; }
.empty.is-leaving { opacity: 0; transform: translateY(-16px) scale(.98); }
.empty__title { margin: 0; font: 400 clamp(56px, min(13vw, 19vh), 152px)/.9 var(--serif); letter-spacing: -.03em; }
.empty__title .line { display: block; }
.empty__title em { color: var(--muted); padding-left: .45em; }
.ch { display: inline-block; opacity: 0; transform: translateY(.45em) rotate(4deg); animation: char 600ms var(--ease) forwards; animation-delay: calc(var(--i) * 38ms + 120ms); }
.log:has(.empty:not([hidden])) { display: grid; }
.log:has(.empty:not([hidden])) > .column { display: grid; }

/* ---------- messages ---------- */
.msg { display: flex; gap: 14px; min-width: 0; }
.msg--user { justify-content: flex-end; animation: in-right 280ms var(--ease) both; }
.msg--user .msg__body {
  max-width: min(85%, 34rem);
  padding: 11px 16px; border-radius: 18px 18px 4px 18px;
  background: var(--bubble); color: var(--bubble-ink);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg--assistant { animation: in-left 300ms var(--ease) both; }
.msg--assistant .msg__main { flex: 1; min-width: 0; padding-top: 3px; }
.glyph { width: 26px; height: 26px; flex: none; margin-top: 2px; color: var(--accent); }
.glyph svg { width: 26px; height: 26px; stroke-width: 2; }
.msg.is-live .glyph svg { animation: spin 1.2s linear infinite; }
.msg__meta { margin: 10px 0 0; font: 500 11px/1 var(--mono); letter-spacing: .04em; color: var(--muted); animation: pop 240ms var(--ease) both; }
.msg__error {
  display: inline-flex; margin: 10px 0 0; padding: 6px 12px; border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); font-size: 14px;
  animation: pop 240ms var(--spring) both;
}

/* prose */
.prose { color: var(--ink-2); overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose p, .prose ul, .prose ol, .prose blockquote, .prose .code, .prose .table { margin: 0 0 14px; }
.prose h3, .prose h4, .prose h5, .prose h6 { margin: 22px 0 8px; color: var(--ink); line-height: 1.3; }
.prose h3 { font: 400 1.75em/1.15 var(--serif); }
.prose h4 { font-size: 1.12em; font-weight: 600; }
.prose h5, .prose h6 { font-size: 1em; font-weight: 600; }
.prose strong { color: var(--ink); font-weight: 600; }
.prose a { color: var(--accent); text-underline-offset: 3px; }
.prose ul, .prose ol { padding-left: 22px; }
.prose li { margin: 4px 0; }
.prose li::marker { color: var(--accent); }
.prose blockquote { padding-left: 14px; border-left: 2px solid var(--accent); color: var(--muted); }
.prose hr { border: 0; border-top: 1px solid var(--rule); margin: 22px 0; }
.prose code { font: 400 .87em/1.5 var(--mono); background: var(--code); padding: .12em .38em; border-radius: 5px; }
.code { border: 1px solid var(--rule); border-radius: 10px; background: var(--code); overflow: hidden; }
.code__bar {
  display: flex; justify-content: space-between; align-items: center; height: 36px; padding: 0 6px 0 14px;
  border-bottom: 1px solid var(--rule); font: 500 11px/1 var(--mono); letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
}
.copy { font: inherit; letter-spacing: inherit; text-transform: inherit; color: var(--muted); padding: 7px 9px; border-radius: 6px; transition: color 150ms var(--ease), background-color 150ms var(--ease); }
.copy:hover { color: var(--ink); background: var(--surface); }
.code pre { margin: 0; padding: 14px; overflow-x: auto; }
.code pre code { background: none; padding: 0; font-size: .86em; color: var(--ink); white-space: pre; }
.table { overflow-x: auto; border: 1px solid var(--rule); border-radius: 10px; }
.prose table { border-collapse: collapse; font-size: .92em; min-width: 100%; }
.prose th, .prose td { text-align: left; padding: 8px 14px; border-bottom: 1px solid var(--rule); vertical-align: top; }
.prose tr:last-child td { border-bottom: 0; }
.prose th { color: var(--ink); font-weight: 600; background: var(--surface); }

.caret { display: inline-block; width: .5em; height: 1.05em; margin-left: 2px; vertical-align: -.16em; border-radius: 2px; background: var(--accent); animation: blink 1s steps(1) infinite; }
.thinking { display: inline-flex; gap: 6px; padding: 9px 0; }
.thinking i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: wave 1s var(--ease) infinite; }
.thinking i:nth-child(2) { animation-delay: .12s; }
.thinking i:nth-child(3) { animation-delay: .24s; }

/* ---------- composer ---------- */
.composer { padding: 6px var(--pad) max(16px, env(safe-area-inset-bottom)); animation: rise-in 520ms var(--ease) 180ms both; }
.composer__box {
  position: relative; display: flex; flex-direction: column; gap: 2px;
  padding: 8px 8px 8px 18px;
  background: var(--bg); border: 1px solid var(--rule); border-radius: 22px;
  transition: border-color 200ms var(--ease), transform 200ms var(--ease);
}
.composer__box:focus-within { border-color: color-mix(in srgb, var(--ink) 45%, var(--rule)); }
.composer__line {
  position: absolute; left: 18px; right: 18px; bottom: -1px; height: 2px; border-radius: 2px; background: var(--accent);
  transform: scaleX(0); transform-origin: left; opacity: 0;
}
body.is-busy .composer__line { opacity: 1; animation: sweep 1.4s var(--ease) infinite; }
.composer textarea {
  width: 100%; min-width: 0; resize: none; border: 0; outline: 0; background: transparent;
  padding: 9px 0; max-height: min(200px, 30dvh); line-height: 1.5; font-size: 16px;
}
.composer textarea::placeholder { color: var(--muted); }
.composer__bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-left: -8px; }
.picker { position: relative; display: inline-flex; align-items: center; min-width: 0; max-width: 70%; }
.picker select {
  appearance: none; -webkit-appearance: none; width: 15rem; min-width: 0; max-width: 100%;
  height: 34px; padding: 0 30px 0 12px; border-radius: 999px;
  font: 500 12.5px/1 var(--mono); color: var(--ink-2);
  background: var(--surface); border: 1px solid transparent; cursor: pointer;
  text-overflow: ellipsis; white-space: nowrap; overflow: hidden;
  transition: border-color 160ms var(--ease), color 160ms var(--ease);
}
.picker select:hover { border-color: var(--rule); color: var(--ink); }
.picker select option, .picker select optgroup { background: var(--bg); color: var(--ink); }
.picker svg { position: absolute; right: 10px; width: 13px; height: 13px; pointer-events: none; color: var(--muted); }
.send {
  width: 40px; height: 40px; display: grid; place-items: center; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink);
  transition: opacity 200ms var(--ease), transform 220ms var(--spring), background-color 200ms var(--ease);
}
.send:not(:disabled):hover { transform: scale(1.06); }
.send:active { transform: scale(.92); }
.send:disabled { opacity: .3; cursor: default; transform: scale(.92); }
.send svg { transition: transform 220ms var(--spring), opacity 160ms var(--ease); }
.send .i-stop { position: absolute; fill: currentColor; stroke: none; opacity: 0; transform: scale(.4) rotate(-90deg); }
.send .i-send { opacity: 1; }
.send.is-stop { background: var(--ink); color: var(--bg); }
.send.is-stop .i-stop { opacity: 1; transform: none; }
.send.is-stop .i-send { opacity: 0; transform: translateY(-8px); }
.send:not(:disabled):not(.is-stop):hover .i-send { transform: translateY(-2px); }

.to-bottom {
  position: absolute; left: 50%; top: -48px; width: 36px; height: 36px; margin-left: -18px;
  display: grid; place-items: center; border-radius: 50%;
  background: var(--bg); border: 1px solid var(--rule); color: var(--ink);
  opacity: 0; transform: translateY(8px) scale(.9); pointer-events: none;
  transition: opacity 200ms var(--ease), transform 200ms var(--ease);
}
.to-bottom.is-on { opacity: 1; transform: none; pointer-events: auto; }

/* ---------- saved chats ---------- */
.scrim { position: fixed; inset: 0; z-index: 30; background: #000; opacity: 0; transition: opacity 220ms var(--ease); }
.scrim.is-on { opacity: .28; }
.scrim[hidden], .history[hidden] { display: none; }
.history {
  position: fixed; z-index: 31; top: 60px; right: max(var(--pad), env(safe-area-inset-right));
  width: min(360px, calc(100vw - 32px)); max-height: min(560px, calc(100dvh - 80px));
  display: flex; flex-direction: column;
  background: var(--bg); border: 1px solid var(--rule); border-radius: 16px;
  box-shadow: 0 18px 50px -20px rgba(0, 0, 0, .35);
  opacity: 0; transform: translateY(-8px) scale(.98); transform-origin: top right;
  transition: opacity 200ms var(--ease), transform 220ms var(--ease);
}
.history.is-open { opacity: 1; transform: none; }
.history__head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 0 18px; }
.history__head h2 { margin: 0; font: 400 24px/1 var(--serif); }
.history__note { margin: 4px 18px 8px; font: 400 11.5px/1.4 var(--mono); color: var(--muted); }
.history__list { list-style: none; margin: 0; padding: 0 8px 8px; overflow-y: auto; overscroll-behavior: contain; }
.history__item { position: relative; display: flex; align-items: center; border-radius: 10px; transition: background-color 150ms var(--ease); }
.history__item:hover, .history__item:focus-within { background: var(--surface); }
.history__open { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; padding: 10px 6px 10px 12px; text-align: left; border-radius: 10px; }
.history__open[aria-current="true"] .history__title { color: var(--accent); }
.history__title { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history__sub { font: 400 11px/1 var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history__del { width: 32px; height: 32px; margin-right: 4px; flex: none; display: grid; place-items: center; border-radius: 8px; color: var(--muted); opacity: .55; transition: opacity 150ms var(--ease), color 150ms var(--ease); }
.history__item:hover .history__del, .history__del:focus-visible { opacity: 1; }
.history__del:hover { color: var(--accent); }
.history__empty { padding: 18px 12px; color: var(--muted); font-size: 14px; }

/* ---------- motion ---------- */
@keyframes char { to { opacity: 1; transform: none; } }
@keyframes in-right { from { opacity: 0; transform: translateX(14px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes in-left { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes drop { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
@keyframes rise-in { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
@keyframes pop { from { opacity: 0; transform: scale(.9); } to { opacity: 1; transform: none; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes beat { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.5); } }
@keyframes wave { 0%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-5px); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes sweep { 0% { transform: scaleX(0); transform-origin: left; } 50% { transform: scaleX(1); transform-origin: left; } 50.01% { transform-origin: right; } 100% { transform: scaleX(0); transform-origin: right; } }

/* ---------- small screens ---------- */
@media (max-width: 600px) {
  .history { top: auto; bottom: 0; right: 0; left: 0; width: 100%; max-height: 75dvh; border-radius: 18px 18px 0 0; transform: translateY(24px); transform-origin: bottom center; padding-bottom: env(safe-area-inset-bottom); }
  .picker { max-width: 75%; }
  .glyph { display: none; }
  .msg--user .msg__body { max-width: 88%; }
  .new span { display: none; }
  .new { width: 34px; padding: 0; justify-content: center; }
  .empty__title em { padding-left: .3em; }
}
@media (max-height: 520px) {
  .empty { padding: 16px 0; }
  .empty__title { font-size: clamp(44px, 16vh, 96px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-delay: 0ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
  .log { scroll-behavior: auto; }
  body.is-busy .composer__line { transform: scaleX(1); }
}
