/* =============================================================================
   Nyko — design system + components
   Dark-first, quiet surfaces, one accent. 768px content column, 260px sidebar.
   ============================================================================= */

:root {
  --font-sans: "Instrument Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-display: "Instrument Serif", Georgia, "Times New Roman", serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  --accent-h: 250;              /* hue; user-adjustable */
  --accent: hsl(var(--accent-h) 90% 72%);
  --accent-strong: hsl(var(--accent-h) 85% 64%);
  --accent-ink: #fff;
  --accent-soft: hsl(var(--accent-h) 90% 72% / 0.14);
  --accent-soft-2: hsl(var(--accent-h) 90% 72% / 0.26);
  --accent-ring: hsl(var(--accent-h) 90% 72% / 0.45);

  --bg: #0b0b10;
  --bg-elev: #101017;
  --surface: #15151e;
  --surface-2: #1b1b26;
  --surface-3: #22222f;
  --border: rgba(255, 255, 255, 0.075);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text: #ececf1;
  --text-2: #a4a4b6;
  --text-3: #6f6f82;
  --danger: #ff6b6b;
  --success: #4ade80;
  --warning: #fbbf24;
  --code-bg: #0e0e15;
  --shadow-1: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.6);
  --shadow-2: 0 12px 40px -12px rgba(0,0,0,.7), 0 0 0 1px var(--border);
  --scroll: rgba(255,255,255,.14);

  --radius-s: 8px;
  --radius: 12px;
  --radius-l: 16px;
  --radius-xl: 22px;
  --sidebar-w: 268px;
  --content-w: 768px;
  --composer-h: 0px;
  --ease: cubic-bezier(.2,.8,.2,1);
  --dur: 160ms;
  --fs: 16px;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f7f7fa;
  --bg-elev: #f1f1f6;
  --surface: #ffffff;
  --surface-2: #f2f2f7;
  --surface-3: #e9e9f1;
  --border: rgba(20, 20, 40, 0.08);
  --border-strong: rgba(20, 20, 40, 0.16);
  --text: #16161d;
  --text-2: #5b5b6c;
  --text-3: #8c8c9c;
  --accent: hsl(var(--accent-h) 80% 58%);
  --accent-strong: hsl(var(--accent-h) 80% 52%);
  --accent-soft: hsl(var(--accent-h) 80% 58% / 0.12);
  --accent-soft-2: hsl(var(--accent-h) 80% 58% / 0.22);
  --accent-ring: hsl(var(--accent-h) 80% 58% / 0.4);
  --code-bg: #f4f4f8;
  --shadow-1: 0 1px 2px rgba(20,20,40,.06), 0 8px 24px -12px rgba(20,20,40,.18);
  --shadow-2: 0 12px 40px -12px rgba(20,20,40,.25), 0 0 0 1px var(--border);
  --scroll: rgba(20,20,40,.18);
  color-scheme: light;
}
/* System preference, when nothing is stamped yet (before JS runs) */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
  --bg: #f7f7fa;
  --bg-elev: #f1f1f6;
  --surface: #ffffff;
  --surface-2: #f2f2f7;
  --surface-3: #e9e9f1;
  --border: rgba(20, 20, 40, 0.08);
  --border-strong: rgba(20, 20, 40, 0.16);
  --text: #16161d;
  --text-2: #5b5b6c;
  --text-3: #8c8c9c;
  --accent: hsl(var(--accent-h) 80% 58%);
  --accent-strong: hsl(var(--accent-h) 80% 52%);
  --accent-soft: hsl(var(--accent-h) 80% 58% / 0.12);
  --accent-soft-2: hsl(var(--accent-h) 80% 58% / 0.22);
  --accent-ring: hsl(var(--accent-h) 80% 58% / 0.4);
  --code-bg: #f4f4f8;
  --shadow-1: 0 1px 2px rgba(20,20,40,.06), 0 8px 24px -12px rgba(20,20,40,.18);
  --shadow-2: 0 12px 40px -12px rgba(20,20,40,.25), 0 0 0 1px var(--border);
  --scroll: rgba(20,20,40,.18);
  color-scheme: light;
  }
}


:root[data-fs="s"] { --fs: 15px; }
:root[data-fs="l"] { --fs: 17px; }

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
button:disabled { cursor: not-allowed; opacity: .5; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
[hidden] { display: none !important; }
:focus { outline: none; }
:focus-visible { outline: 2px solid var(--accent-ring); outline-offset: 2px; border-radius: 6px; }
textarea:focus-visible, input:focus-visible, .input:focus-visible, [contenteditable]:focus-visible { outline: none; }
::selection { background: var(--accent-soft-2); }
* { scrollbar-width: thin; scrollbar-color: var(--scroll) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--scroll); border-radius: 8px; }
*::-webkit-scrollbar-track { background: transparent; }
svg.i { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg.i.sm { width: 15px; height: 15px; }
svg.i.xs { width: 13px; height: 13px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; } }

/* ---------- App shell ---------- */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr) 0;
  grid-template-rows: minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
  transition: grid-template-columns var(--dur) var(--ease);
}
.app.sb-collapsed { grid-template-columns: 0 minmax(0, 1fr) 0; }
.app.canvas-open { grid-template-columns: var(--sidebar-w) minmax(0, 1fr) minmax(360px, 44%); }
.app.canvas-open.sb-collapsed { grid-template-columns: 0 minmax(0, 1fr) minmax(360px, 46%); }

/* ---------- Sidebar ---------- */
.sidebar {
  grid-column: 1; grid-row: 1;
  min-height: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  width: var(--sidebar-w);
  transition: transform var(--dur) var(--ease), opacity var(--dur) var(--ease);
}
.sb-collapsed .sidebar { opacity: 0; pointer-events: none; }
.sb-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 16px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; letter-spacing: -.01em; }
.orb {
  width: 26px; height: 26px; border-radius: 50%; flex: none; position: relative;
  background: radial-gradient(circle at 35% 30%, hsl(var(--accent-h) 95% 85%), var(--accent-strong) 55%, hsl(calc(var(--accent-h) + 40) 80% 45%));
  box-shadow: 0 0 0 1px rgba(255,255,255,.08) inset, 0 4px 14px -4px var(--accent-ring);
}
.orb::after { content: ""; position: absolute; inset: 7px; border-radius: 50%; background: var(--bg-elev); opacity: .85; }
.orb.sm { width: 22px; height: 22px; } .orb.sm::after { inset: 6px; }
.orb.lg { width: 44px; height: 44px; } .orb.lg::after { inset: 12px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 8px; color: var(--text-2);
  transition: background var(--dur), color var(--dur);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.active { color: var(--accent); background: var(--accent-soft); }
.sb-actions { padding: 8px 12px 4px; display: grid; gap: 6px; }
.btn-new {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 12px; border-radius: 10px; font-weight: 500; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); transition: all var(--dur);
}
.btn-new:hover { border-color: var(--border-strong); background: var(--surface-2); }
.btn-new kbd { margin-left: auto; display: none; }
.sb-search { display: flex; align-items: center; gap: 8px; padding: 0 10px; height: 34px; border-radius: 10px; color: var(--text-3); background: transparent; border: 1px solid transparent; transition: all var(--dur); }
.sb-search:hover, .sb-search:focus-within { background: var(--surface); border-color: var(--border); }
.sb-search input { flex: 1; background: none; border: 0; min-width: 0; color: var(--text); }
.sb-search input::placeholder { color: var(--text-3); }
kbd { font-family: var(--font-sans); font-size: 11px; color: var(--text-3); border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; line-height: 1.3; background: var(--surface); }
.sb-list { flex: 1; overflow-y: auto; padding: 6px 8px 12px; }
.sb-group { font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3); padding: 14px 10px 6px; }
.conv {
  display: flex; align-items: center; gap: 8px; padding: 7px 8px 7px 10px; border-radius: 9px;
  color: var(--text-2); cursor: pointer; position: relative; user-select: none; transition: background var(--dur), color var(--dur);
}
.conv:hover { background: var(--surface); color: var(--text); }
.conv.active { background: var(--surface-2); color: var(--text); }
.conv .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13.5px; }
.conv .pin { color: var(--accent); }
.conv .more { opacity: 0; width: 26px; height: 26px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; color: var(--text-3); flex: none; }
.conv:hover .more, .conv.active .more, .conv.menu-open .more { opacity: 1; }
.conv .more:hover { background: var(--surface-3); color: var(--text); }
.conv input.rename { flex: 1; background: var(--bg); border: 1px solid var(--accent-ring); border-radius: 6px; padding: 3px 6px; font-size: 13.5px; min-width: 0; }
.sb-empty { color: var(--text-3); font-size: 13px; padding: 24px 12px; text-align: center; }
.sb-foot { border-top: 1px solid var(--border); padding: 10px 10px; display: grid; gap: 4px; }
.sb-foot .row { display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: 9px; color: var(--text-2); width: 100%; transition: background var(--dur), color var(--dur); text-align: left; }
.sb-foot .row:hover { background: var(--surface); color: var(--text); }
.sb-foot .row .sub { margin-left: auto; font-size: 12px; color: var(--text-3); }
.usage { padding: 6px 8px 2px; font-size: 12px; color: var(--text-3); display: flex; justify-content: space-between; }

/* ---------- Main ---------- */
.main { grid-column: 2; grid-row: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; background: var(--bg); }
.topbar {
  display: flex; align-items: center; gap: 6px; padding: 10px 12px; min-height: 52px;
  position: sticky; top: 0; z-index: 5;
  background: linear-gradient(var(--bg) 70%, transparent);
}
.topbar .title { flex: 1; min-width: 0; font-weight: 500; font-size: 14px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 6px; }
.topbar .title[contenteditable="true"] { color: var(--text); outline: 1px solid var(--accent-ring); border-radius: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px; border-radius: 999px;
  font-size: 12.5px; font-weight: 500; color: var(--text-2); background: var(--surface); border: 1px solid var(--border); white-space: nowrap; transition: all var(--dur);
}
.chip:hover { color: var(--text); border-color: var(--border-strong); }
.chip.accent { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.chip .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }

.thread { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; scroll-behavior: smooth; padding-bottom: 8px; }
.thread-inner { max-width: var(--content-w); margin: 0 auto; padding: 8px 20px 24px; display: flex; flex-direction: column; gap: 26px; }

/* Welcome */
.welcome { max-width: var(--content-w); margin: 0 auto; padding: 8vh 24px 24px; display: flex; flex-direction: column; gap: 22px; animation: rise .4s var(--ease) both; }
.welcome h1 { font-family: var(--font-display); font-size: 40px; font-weight: 400; letter-spacing: -.01em; margin: 0; line-height: 1.15; text-wrap: balance; }
.welcome h1 .muted { color: var(--text-3); font-style: italic; }
.tabs { display: flex; gap: 8px; flex-wrap: wrap; }
.tab { display: inline-flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text-2); font-weight: 500; transition: all var(--dur); }
.tab:hover { color: var(--text); border-color: var(--border-strong); }
.tab.active { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.suggest { display: flex; flex-direction: column; }
.suggest button { text-align: left; padding: 12px 6px; border-bottom: 1px solid var(--border); color: var(--text-2); font-size: 15px; display: flex; align-items: center; gap: 10px; transition: color var(--dur), padding-left var(--dur); }
.suggest button:last-child { border-bottom: 0; }
.suggest button:hover { color: var(--text); padding-left: 10px; }
.suggest button .i { color: var(--text-3); }
.feature-row { display: flex; gap: 8px; flex-wrap: wrap; }
.feature-row .chip { height: 28px; font-weight: 500; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* Messages */
.msg { display: flex; gap: 14px; animation: fadeIn .25s var(--ease) both; }
.msg.user { justify-content: flex-end; }
.msg .avatar { flex: none; margin-top: 4px; }
.msg .body { min-width: 0; max-width: 100%; }
.msg.user .body { max-width: 82%; }
.bubble {
  padding: 10px 16px; border-radius: 20px 20px 6px 20px; background: var(--surface-2); border: 1px solid var(--border);
  white-space: pre-wrap; overflow-wrap: anywhere; font-size: var(--fs); line-height: 1.55;
}
.attach-grid { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 8px; }
.attach-grid img { max-width: 220px; max-height: 220px; border-radius: 12px; border: 1px solid var(--border); object-fit: cover; cursor: zoom-in; }
.doc-chip { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 10px; background: var(--surface); border: 1px solid var(--border); font-size: 12.5px; color: var(--text-2); max-width: 260px; }
.doc-chip .i { color: var(--accent); }
.doc-chip .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-chip .meta { color: var(--text-3); font-size: 11px; }

.msg.assistant .body { width: 100%; }
.msg-tools { display: flex; align-items: center; gap: 6px; margin-top: 6px; min-height: 28px; opacity: 0; transition: opacity var(--dur); flex-wrap: wrap; }
.msg:hover .msg-tools, .msg:focus-within .msg-tools, .msg.last .msg-tools { opacity: 1; }
.tool-btn { display: inline-flex; align-items: center; gap: 5px; height: 28px; padding: 0 8px; border-radius: 7px; color: var(--text-3); font-size: 12px; transition: all var(--dur); }
.tool-btn:hover { background: var(--surface-2); color: var(--text); }
.tool-btn.ok { color: var(--success); }
.tool-btn.on { color: var(--accent); }
.meta-txt { font-size: 12px; color: var(--text-3); margin-left: auto; white-space: nowrap; }
.followups { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; animation: fadeIn .3s var(--ease) both; }
.followups button { padding: 7px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text-2); font-size: 13px; display: inline-flex; align-items: center; gap: 6px; transition: all var(--dur); }
.followups button:hover { color: var(--text); border-color: var(--accent-ring); background: var(--accent-soft); }
.followups .i { color: var(--accent); }
.sources { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.source { display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 9px 0 6px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text-2); font-size: 12px; text-decoration: none; transition: all var(--dur); max-width: 220px; }
.source:hover { color: var(--text); border-color: var(--border-strong); text-decoration: none; }
.source img { width: 14px; height: 14px; border-radius: 3px; }
.source span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-line { display: inline-flex; align-items: center; gap: 8px; color: var(--text-3); font-size: 13px; margin: 2px 0 8px; }
.pulse { display: inline-flex; gap: 4px; }
.pulse i { width: 6px; height: 6px; border-radius: 50%; background: var(--text-3); animation: pulse 1.2s infinite ease-in-out; }
.pulse i:nth-child(2) { animation-delay: .15s; } .pulse i:nth-child(3) { animation-delay: .3s; }
@keyframes pulse { 0%, 80%, 100% { transform: scale(.6); opacity: .4; } 40% { transform: scale(1); opacity: 1; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.caret { display: inline-block; width: 8px; height: 1.05em; vertical-align: text-bottom; margin-left: 2px; border-radius: 2px; background: var(--accent); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.thinking { margin: 0 0 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); overflow: hidden; }
.thinking summary { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 8px; padding: 9px 12px; color: var(--text-2); font-size: 13px; font-weight: 500; user-select: none; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary .i { transition: transform var(--dur); color: var(--text-3); }
.thinking[open] summary .i.chev { transform: rotate(90deg); }
.thinking .thought { padding: 0 14px 12px; color: var(--text-3); font-size: 13px; line-height: 1.6; white-space: pre-wrap; max-height: 260px; overflow: auto; border-top: 1px solid var(--border); padding-top: 10px; }
.thinking.live summary .i.spark { color: var(--accent); animation: spin 1.6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-card { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border-radius: 12px; background: color-mix(in srgb, var(--danger) 8%, transparent); border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent); color: var(--text); font-size: 14px; }
.error-card .i { color: var(--danger); margin-top: 1px; }
.error-card .actions { display: flex; gap: 8px; margin-top: 8px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 34px; padding: 0 14px; border-radius: 9px; font-weight: 500; font-size: 13.5px; border: 1px solid var(--border); background: var(--surface); color: var(--text); transition: all var(--dur); }
.btn:hover { border-color: var(--border-strong); background: var(--surface-2); }
.btn.primary { background: var(--accent-strong); border-color: transparent; color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }
.btn.danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.sm { height: 30px; padding: 0 10px; font-size: 12.5px; }

/* ---------- Markdown ("prose") ---------- */
.prose { font-size: var(--fs); line-height: 1.65; overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; } .prose > :last-child { margin-bottom: 0; }
.prose p { margin: 0 0 .9em; }
.prose h1, .prose h2, .prose h3, .prose h4 { line-height: 1.3; letter-spacing: -.01em; margin: 1.3em 0 .5em; font-weight: 600; }
.prose h1 { font-size: 1.45em; } .prose h2 { font-size: 1.25em; } .prose h3 { font-size: 1.1em; } .prose h4 { font-size: 1em; }
.prose ul, .prose ol { padding-left: 1.5em; margin: 0 0 .9em; }
.prose li { margin: .25em 0; } .prose li > p { margin: 0 0 .3em; }
.prose li::marker { color: var(--text-3); }
.prose blockquote { margin: 0 0 .9em; padding: .2em 0 .2em 1em; border-left: 3px solid var(--accent-soft-2); color: var(--text-2); }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
.prose img { max-width: 100%; border-radius: 12px; border: 1px solid var(--border); display: block; margin: .4em 0; }
.prose table { border-collapse: collapse; width: 100%; margin: 0 0 1em; font-size: .93em; display: block; overflow-x: auto; }
.prose th, .prose td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; vertical-align: top; overflow-wrap: normal; hyphens: none; }
.prose th { white-space: nowrap; }
.prose th { background: var(--surface); font-weight: 600; }
.prose code { font-family: var(--font-mono); font-size: .875em; background: var(--surface-2); border: 1px solid var(--border); padding: .12em .4em; border-radius: 6px; }
.prose pre code { background: none; border: 0; padding: 0; font-size: 13px; line-height: 1.6; }
.prose .katex-display { overflow-x: auto; padding: .3em 0; }
.prose input[type="checkbox"] { margin-right: .5em; accent-color: var(--accent); }
.codeblock { margin: .6em 0 1em; border: 1px solid var(--border); border-radius: 12px; background: var(--code-bg); overflow: hidden; }
.codeblock .cb-head { display: flex; align-items: center; gap: 6px; padding: 6px 8px 6px 14px; background: var(--surface); border-bottom: 1px solid var(--border); font-size: 12px; color: var(--text-3); font-family: var(--font-mono); }
.codeblock .cb-head .lang { flex: 1; }
.codeblock pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.codeblock.wrap pre code { white-space: pre-wrap; }
.codeblock .run-out { margin: 0; padding: 10px 16px; border-top: 1px solid var(--border); background: var(--surface); color: var(--text-2); font-family: var(--font-mono); font-size: 12.5px; white-space: pre-wrap; max-height: 260px; overflow: auto; }
.mermaid-box { margin: .6em 0 1em; padding: 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); overflow: auto; text-align: center; }
.mermaid-box svg { max-width: 100%; }
.gen-img { position: relative; }
.gen-img .loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--text-3); font-size: 13px; }

/* ---------- Syntax highlighting (highlight.js classes, theme-aware) ---------- */
:root { --hl-comment: #768390; --hl-keyword: #f47067; --hl-string: #96d0ff; --hl-title: #dcbdfb; --hl-number: #6cb6ff; --hl-variable: #f69d50; --hl-tag: #8ddb8c; --hl-attr: #6cb6ff; --hl-builtin: #f69d50; --hl-meta: #768390; --hl-text: #adbac7; }
:root[data-theme="light"] { --hl-comment: #6a737d; --hl-keyword: #d73a49; --hl-string: #032f62; --hl-title: #6f42c1; --hl-number: #005cc5; --hl-variable: #e36209; --hl-tag: #22863a; --hl-attr: #005cc5; --hl-builtin: #005cc5; --hl-meta: #6a737d; --hl-text: #24292e; }
.hljs { color: var(--hl-text); }
.hljs-comment, .hljs-quote { color: var(--hl-comment); font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-type, .hljs-doctag, .hljs-operator { color: var(--hl-keyword); }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-meta .hljs-string { color: var(--hl-string); }
.hljs-title, .hljs-title.function_, .hljs-title.class_, .hljs-section, .hljs-name { color: var(--hl-title); }
.hljs-number, .hljs-literal, .hljs-symbol, .hljs-bullet, .hljs-link, .hljs-selector-id, .hljs-selector-class { color: var(--hl-number); }
.hljs-variable, .hljs-template-variable, .hljs-property, .hljs-params { color: var(--hl-variable); }
.hljs-tag, .hljs-selector-pseudo { color: var(--hl-tag); }
.hljs-attr, .hljs-attribute { color: var(--hl-attr); }
.hljs-built_in, .hljs-class .hljs-title { color: var(--hl-builtin); }
.hljs-meta, .hljs-deletion { color: var(--hl-meta); }
.hljs-emphasis { font-style: italic; } .hljs-strong { font-weight: 600; }

/* ---------- Composer ---------- */
.composer-wrap { padding: 4px 16px 10px; }
.composer-inner { max-width: var(--content-w); margin: 0 auto; }
.attach-strip { display: flex; gap: 8px; flex-wrap: wrap; padding: 0 4px 8px; }
.attach-strip .thumb { position: relative; }
.attach-strip .thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); display: block; }
.attach-strip .thumb .doc-chip { height: 44px; }
.attach-strip .rm { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; background: var(--surface-3); color: var(--text); display: grid; place-items: center; border: 1px solid var(--border); }
.attach-strip .rm:hover { background: var(--danger); color: #fff; }
.composer {
  position: relative; border-radius: var(--radius-xl); background: var(--surface); border: 1px solid var(--border);
  box-shadow: var(--shadow-1); transition: border-color var(--dur), box-shadow var(--dur);
}
.composer:focus-within { border-color: var(--accent-ring); box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow-1); }
.composer.drag { border-color: var(--accent); background: var(--accent-soft); }
.composer textarea {
  display: block; width: 100%; resize: none; background: none; border: 0; padding: 14px 16px 6px;
  font-size: 15.5px; line-height: 1.5; max-height: 40vh; color: var(--text);
}
.composer textarea::placeholder { color: var(--text-3); }
.composer .bar { display: flex; align-items: center; gap: 4px; padding: 4px 8px 8px 8px; }
.composer .bar .spacer { flex: 1; }
.picker-btn { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 10px 0 8px; border-radius: 9px; color: var(--text-2); font-weight: 500; font-size: 13px; transition: all var(--dur); }
.picker-btn:hover { background: var(--surface-2); color: var(--text); }
.picker-btn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent-ring); }
.send {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent-strong); color: var(--accent-ink); transition: transform var(--dur), filter var(--dur), background var(--dur);
}
.send:hover { filter: brightness(1.08); transform: translateY(-1px); }
.send:disabled { background: var(--surface-3); color: var(--text-3); opacity: 1; }
.send.stop { background: var(--text); color: var(--bg); }
.icon-btn.rec { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); animation: recpulse 1.2s infinite; }
@keyframes recpulse { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--danger) 18%, transparent); } }
.hint { text-align: center; font-size: 11.5px; color: var(--text-3); padding: 8px 0 0; }
.hint kbd { font-size: 10px; }

/* Popovers & menus */
.pop {
  position: absolute; z-index: 40; min-width: 240px; padding: 6px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-2);
  animation: popIn .14s var(--ease) both;
}
@keyframes popIn { from { opacity: 0; transform: translateY(4px) scale(.98); } to { opacity: 1; transform: none; } }
.pop .item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; border-radius: 9px; text-align: left; color: var(--text); transition: background var(--dur); }
.pop .item:hover, .pop .item.sel { background: var(--surface-2); }
.pop .item .i { color: var(--text-3); }
.pop .item .txt { flex: 1; min-width: 0; }
.pop .item .name { font-weight: 500; font-size: 13.5px; display: flex; align-items: center; gap: 8px; }
.pop .item .desc { font-size: 12px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pop .item .check { color: var(--accent); }
.pop .item.danger { color: var(--danger); } .pop .item.danger .i { color: var(--danger); }
.pop .sep { height: 1px; background: var(--border); margin: 6px 4px; }
.pop .head { font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3); padding: 8px 10px 4px; }
.badge { font-size: 10.5px; font-weight: 600; padding: 2px 6px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); letter-spacing: .02em; }
.badge.muted { background: var(--surface-3); color: var(--text-3); }
.badge.mono { font-family: var(--font-mono); font-weight: 500; letter-spacing: 0; }

/* ---------- Canvas (artifacts) ---------- */
.canvas { grid-column: 3; grid-row: 1; display: none; flex-direction: column; min-width: 0; min-height: 0; border-left: 1px solid var(--border); background: var(--bg-elev); }
.canvas-open .canvas { display: flex; }
.canvas-head { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--border); min-height: 52px; }
.canvas-head .title { flex: 1; font-weight: 500; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-2); }
.seg { display: inline-flex; padding: 3px; border-radius: 9px; background: var(--surface); border: 1px solid var(--border); }
.seg button { height: 26px; padding: 0 10px; border-radius: 6px; font-size: 12.5px; font-weight: 500; color: var(--text-3); }
.seg button.active { background: var(--surface-3); color: var(--text); }
.canvas-body { flex: 1; min-height: 0; position: relative; background: #fff; }
.canvas-body iframe { width: 100%; height: 100%; border: 0; display: block; background: #fff; }
.canvas-body pre { margin: 0; height: 100%; overflow: auto; padding: 16px; font-family: var(--font-mono); font-size: 12.5px; line-height: 1.6; background: var(--code-bg); color: var(--text); }
.canvas-foot { display: flex; gap: 6px; flex-wrap: wrap; padding: 10px; border-top: 1px solid var(--border); }
.canvas-foot .chip { height: 28px; }

/* ---------- Modals ---------- */
.overlay { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.55); display: grid; place-items: center; padding: 20px; animation: fadeIn .15s both; backdrop-filter: blur(6px); }
.modal { width: min(760px, 100%); max-height: min(86vh, 820px); display: flex; flex-direction: column; border-radius: 18px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-2); overflow: hidden; animation: popIn .18s var(--ease) both; }
.modal.sm { width: min(440px, 100%); }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 16px 18px 12px; }
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 600; flex: 1; letter-spacing: -.01em; }
.modal-body { display: flex; min-height: 0; flex: 1; }
.modal-nav { width: 180px; flex: none; border-right: 1px solid var(--border); padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.modal-nav button { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 9px; color: var(--text-2); font-weight: 500; text-align: left; }
.modal-nav button:hover { background: var(--surface-2); color: var(--text); }
.modal-nav button.active { background: var(--accent-soft); color: var(--accent); }
.modal-pane { flex: 1; overflow-y: auto; padding: 18px 22px 26px; min-width: 0; }
.modal-pane h3 { margin: 0 0 4px; font-size: 14px; font-weight: 600; }
.modal-pane .lead { color: var(--text-3); font-size: 13px; margin: 0 0 18px; }
.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field label { font-size: 13px; font-weight: 500; color: var(--text); }
.field .help { font-size: 12px; color: var(--text-3); }
.input, .textarea, select.input { width: 100%; padding: 9px 12px; border-radius: 10px; background: var(--bg); border: 1px solid var(--border); color: var(--text); transition: border-color var(--dur), box-shadow var(--dur); }
.input:focus, .textarea:focus { border-color: var(--accent-ring); box-shadow: 0 0 0 3px var(--accent-soft); }
.textarea { resize: vertical; min-height: 90px; line-height: 1.5; }
.row { display: flex; align-items: center; gap: 12px; }
.row.between { justify-content: space-between; }
.switch { position: relative; width: 40px; height: 22px; border-radius: 999px; background: var(--surface-3); transition: background var(--dur); flex: none; border: 1px solid var(--border); }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--text-2); transition: transform var(--dur), background var(--dur); }
.switch[aria-checked="true"] { background: var(--accent-strong); border-color: transparent; }
.switch[aria-checked="true"]::after { transform: translateX(18px); background: #fff; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.setting:last-child { border-bottom: 0; }
.setting .l { min-width: 0; } .setting .l b { display: block; font-weight: 500; font-size: 13.5px; } .setting .l span { display: block; color: var(--text-3); font-size: 12px; margin-top: 2px; }
input[type="range"] { accent-color: var(--accent); width: 160px; }
.swatches { display: flex; gap: 8px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; transition: transform var(--dur); }
.swatch:hover { transform: scale(1.08); }
.swatch.active { border-color: var(--text); }
.list-card { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.list-card .li { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.list-card .li:last-child { border-bottom: 0; }
.list-card .li .t { flex: 1; min-width: 0; } .list-card .li .t b { display: block; font-weight: 500; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .list-card .li .t span { display: block; color: var(--text-3); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar-sq { width: 34px; height: 34px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 600; font-size: 12px; flex: none; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 16px; border-top: 1px solid var(--border); }
.modal p { margin: 0 0 12px; color: var(--text-2); }

/* Command palette */
.palette { width: min(620px, 100%); align-self: start; margin-top: 10vh; }
.palette .p-in { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.palette .p-in input { flex: 1; background: none; border: 0; font-size: 16px; }
.palette .p-list { max-height: 50vh; overflow-y: auto; padding: 6px; }

/* Toasts */
.toasts { position: fixed; top: 14px; left: 50%; transform: translateX(-50%); z-index: 80; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { pointer-events: auto; display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 12px; background: var(--surface-3); border: 1px solid var(--border); box-shadow: var(--shadow-2); font-size: 13.5px; animation: popIn .18s var(--ease) both; }
.toast.ok .i { color: var(--success); } .toast.err .i { color: var(--danger); }
.toast button { color: var(--accent); font-weight: 500; }

/* Lightbox */
.lightbox { position: fixed; inset: 0; z-index: 90; background: rgba(0,0,0,.85); display: grid; place-items: center; cursor: zoom-out; animation: fadeIn .15s; }
.lightbox img { max-width: 92vw; max-height: 92vh; border-radius: 8px; }

/* Shared (read-only) banner */
.shared-bar { display: flex; align-items: center; gap: 10px; justify-content: center; padding: 8px; font-size: 13px; color: var(--text-2); background: var(--accent-soft); border-bottom: 1px solid var(--border); }

/* Scroll-to-bottom */
.to-bottom { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(var(--composer-h) + 16px); z-index: 6; width: 34px; height: 34px; border-radius: 50%; background: var(--surface-3); border: 1px solid var(--border); color: var(--text-2); display: grid; place-items: center; box-shadow: var(--shadow-1); animation: popIn .15s both; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .app, .app.sb-collapsed, .app.canvas-open, .app.canvas-open.sb-collapsed { grid-template-columns: 0 minmax(0, 1fr) 0; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 50; width: min(86vw, 320px); transform: translateX(-100%); opacity: 1; box-shadow: var(--shadow-2); }
  .app.sb-open .sidebar { transform: none; pointer-events: auto; }
  .sb-collapsed .sidebar { pointer-events: none; }
  .app.sb-open .sidebar { pointer-events: auto; }
  .scrim { position: fixed; inset: 0; z-index: 45; background: rgba(0,0,0,.5); animation: fadeIn .15s; }
  .canvas { position: fixed; inset: 0; z-index: 55; border: 0; }
  .canvas-open .canvas { display: flex; }
  .welcome { padding-top: 6vh; } .welcome h1 { font-size: 30px; }
  .thread-inner { padding: 8px 14px 16px; gap: 22px; }
  .msg.user .body { max-width: 92%; }
  .composer-wrap { padding: 4px 10px calc(8px + env(safe-area-inset-bottom)); }
  .hint { display: none; }
  .modal { max-height: 92vh; border-radius: 16px; }
  .modal-body { flex-direction: column; }
  .modal-nav { width: auto; flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .modal-nav button { white-space: nowrap; }
  .topbar .title { font-size: 13px; }
  #sb-open-btn { display: inline-flex !important; }
}
@media (min-width: 901px) { .mobile-only { display: none !important; } }
