/* =========================================================
   Контур — design system
   Warm paper in light mode, neutral zinc in dark mode.
   ========================================================= */
:root {
  color-scheme: light;
  --bg: #f7efe4;
  --bg-subtle: #efe5d8;
  --surface: #fbf5ec;
  --surface-2: #eee2d3;
  --surface-3: #e5d6c3;
  --border: #d9cbbb;
  --border-strong: #bcaa94;
  --text: #29231d;
  --text-2: #4b4137;
  --muted: #63574b;
  --faint: #706254;
  --primary: #332b24;
  --primary-fg: #fbf5ec;
  --primary-hover: #483c31;
  --ring: #856d54;
  --code-bg: #eee2d3;
  --code-block: #fbf5ec;
  --ok: #326344; --ok-bg: #e6ecdc; --ok-line: #bbcbb0;
  --warn: #814a15; --warn-bg: #f7e8ce; --warn-line: #dcc097;
  --bad: #963e35; --bad-bg: #f6e2d9; --bad-line: #dcb4a7;
  --shadow-sm: 0 1px 2px rgb(0 0 0 / .04);
  --shadow: 0 1px 2px rgb(0 0 0 / .04), 0 4px 16px rgb(0 0 0 / .05);
  --shadow-lg: 0 16px 48px rgb(0 0 0 / .14), 0 2px 6px rgb(0 0 0 / .06);
  --r-sm: 6px; --r: 10px; --r-lg: 14px; --r-xl: 22px;
  --sidebar: 264px;
  --thread: 768px;
  --ease: cubic-bezier(.2, .7, .2, 1);
  font-family: Inter, "Geist", "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  font-feature-settings: "cv11", "ss01";
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  color: var(--text);
  background: var(--bg);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #09090b; --bg-subtle: #0c0c0e; --surface: #111113; --surface-2: #18181b; --surface-3: #27272a;
    --border: #232326; --border-strong: #34343a;
    --text: #fafafa; --text-2: #d4d4d8; --muted: #a1a1aa; --faint: #71717a;
    --primary: #fafafa; --primary-fg: #09090b; --primary-hover: #e4e4e7; --ring: #71717a;
    --code-bg: #1f1f23; --code-block: #111113;
    --ok: #4ade80; --ok-bg: #052e16; --ok-line: #14532d;
    --warn: #fbbf24; --warn-bg: #2a1a03; --warn-line: #713f12;
    --bad: #f87171; --bad-bg: #2a0a0a; --bad-line: #7f1d1d;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 4px 16px rgb(0 0 0 / .3);
    --shadow-lg: 0 16px 48px rgb(0 0 0 / .6), 0 0 0 1px #27272a;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #09090b; --bg-subtle: #0c0c0e; --surface: #111113; --surface-2: #18181b; --surface-3: #27272a;
  --border: #232326; --border-strong: #34343a;
  --text: #fafafa; --text-2: #d4d4d8; --muted: #a1a1aa; --faint: #71717a;
  --primary: #fafafa; --primary-fg: #09090b; --primary-hover: #e4e4e7; --ring: #71717a;
  --code-bg: #1f1f23; --code-block: #111113;
  --ok: #4ade80; --ok-bg: #052e16; --ok-line: #14532d;
  --warn: #fbbf24; --warn-bg: #2a1a03; --warn-line: #713f12;
  --bad: #f87171; --bad-bg: #2a0a0a; --bad-line: #7f1d1d;
  --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 4px 16px rgb(0 0 0 / .3);
  --shadow-lg: 0 16px 48px rgb(0 0 0 / .6), 0 0 0 1px #27272a;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box }
html, body { height: 100% }
body { margin: 0; background: var(--bg); color: var(--text) }
h1, h2, h3, h4, h5, h6, p { margin: 0 }
a { color: inherit }
button, input, textarea, select { font: inherit; color: inherit }
button { border: 0; background: none; padding: 0; cursor: pointer }
button:disabled { cursor: not-allowed; opacity: .5 }
[hidden] { display: none !important }
:focus { outline: none }
:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; border-radius: var(--r-sm) }
::selection { background: color-mix(in srgb, var(--text) 18%, transparent) }
.sr { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap }
.icon { fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; flex: none }
html[data-theme="dark"] .only-light, html:not([data-theme]) .only-dark { display: none }
html[data-theme="light"] .only-dark { display: none }
@media (prefers-color-scheme: dark) {
  html:not([data-theme]) .only-dark { display: revert }
  html:not([data-theme]) .only-light { display: none }
}
html[data-theme="dark"] .only-dark { display: revert }

/* ---------- Brand ---------- */
.brand { display: inline-flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; letter-spacing: -.01em; text-decoration: none }
.logo { display: inline-block; flex: none; width: 22px; height: 22px; background: url("/favicon.ico") center / contain no-repeat }
.logo.big { width: 40px; height: 40px }

/* ---------- Generic controls ---------- */
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: var(--r-sm); color: var(--muted); transition: background .15s, color .15s }
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text) }
.icon-btn.small { width: 26px; height: 26px }
.icon-btn.danger:hover:not(:disabled) { color: var(--bad); background: var(--bad-bg) }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 36px; padding: 0 14px; border-radius: var(--r-sm); font-size: 14px; font-weight: 500; white-space: nowrap; transition: background .15s, border-color .15s }
.btn.primary { background: var(--primary); color: var(--primary-fg) }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover) }
.btn.secondary { background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-sm) }
.btn.secondary:hover:not(:disabled) { background: var(--surface-2) }
.btn.wide { width: 100% }
.field { display: grid; gap: 6px; font-size: 13px; font-weight: 500; color: var(--text-2) }
.field input, .admin input:not([type=file]) {
  width: 100%; height: 38px; padding: 0 12px; border: 1px solid var(--border); border-radius: var(--r-sm);
  background: var(--surface); font-size: 14px; font-weight: 400; color: var(--text); transition: border-color .15s, box-shadow .15s
}
.field input:focus, .admin input:focus { border-color: var(--ring); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 22%, transparent) }
input::placeholder, textarea::placeholder { color: var(--faint) }
.badge { display: inline-flex; align-items: center; gap: 6px; height: 22px; padding: 0 8px; border-radius: 999px; font-size: 12px; font-weight: 500; border: 1px solid var(--border); color: var(--muted); background: var(--surface-2); white-space: nowrap }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor }
.badge.ok { color: var(--ok); background: var(--ok-bg); border-color: var(--ok-line) }
.badge.warn { color: var(--warn); background: var(--warn-bg); border-color: var(--warn-line) }
.badge.bad { color: var(--bad); background: var(--bad-bg); border-color: var(--bad-line) }
.kbd { font: 500 11px/1 ui-monospace, "SF Mono", Consolas, monospace; color: var(--muted); border: 1px solid var(--border); border-radius: 5px; padding: 4px 6px; background: var(--surface-2) }

/* =========================================================
   Chat
   ========================================================= */
.chat-body { overflow: hidden }
.app { display: flex; height: 100dvh; background: var(--bg) }

/* Sidebar */
.sidebar {
  width: var(--sidebar); flex: none; display: flex; flex-direction: column; gap: 4px;
  padding: 10px; background: var(--bg-subtle); border-right: 1px solid var(--border);
  transition: margin-left .25s var(--ease), transform .25s var(--ease)
}
.sidebar-closed .sidebar { margin-left: calc(-1 * var(--sidebar)) }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 2px 8px 8px }
.new-chat {
  display: flex; align-items: center; gap: 10px; height: 38px; padding: 0 12px; margin-bottom: 14px;
  border: 1px solid var(--border); border-radius: var(--r-sm); background: var(--surface); box-shadow: var(--shadow-sm);
  font-size: 14px; font-weight: 500; transition: background .15s
}
.new-chat:hover:not(:disabled) { background: var(--surface-2) }
.sidebar-section { flex: 1; min-height: 0; overflow: auto }
.sidebar-label { font-size: 12px; font-weight: 500; color: var(--muted); padding: 6px 10px }
.chat-item { position: relative; display: flex; align-items: center; border-radius: var(--r-sm); font-size: 14px; color: var(--text-2); transition: background .15s }
.chat-item:hover, .chat-item.menu-open { background: var(--surface-2) }
.chat-item[aria-current] { background: var(--surface-3); color: var(--text) }
.chat-open { flex: 1; min-width: 0; padding: 8px 4px 8px 10px; text-align: left; color: inherit }
.chat-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.chat-more { display: grid; place-items: center; width: 28px; height: 28px; margin-right: 4px; border-radius: 6px; color: var(--muted); opacity: 0; transition: opacity .15s, background .15s }
.chat-more .icon { stroke-width: 3 }
.chat-item:hover .chat-more, .chat-item[aria-current] .chat-more, .chat-item.menu-open .chat-more, .chat-more:focus-visible { opacity: 1 }
.chat-more:hover { background: var(--surface-3); color: var(--text) }
.chat-menu { position: absolute; z-index: 20; top: calc(100% + 4px); right: 0; min-width: 180px; padding: 4px; border: 1px solid var(--border); border-radius: var(--r); background: var(--surface); box-shadow: var(--shadow-lg); animation: pop .12s var(--ease) both }
.menu-item { display: flex; align-items: center; gap: 10px; width: 100%; height: 34px; padding: 0 10px; border-radius: var(--r-sm); font-size: 14px; color: var(--text-2); text-align: left }
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-2); color: var(--text) }
.menu-item.danger { color: var(--bad) }
.menu-item.danger:hover { background: var(--bad-bg) }
.chat-rename { width: 100%; height: 34px; padding: 0 10px; border: 1px solid var(--ring); border-radius: var(--r-sm); background: var(--surface); font-size: 14px; color: var(--text) }
@media (hover: none) { .chat-more { opacity: 1 } }
.sidebar-hint { font-size: 12px; color: var(--faint); padding: 10px 10px 0; line-height: 1.45 }
.sidebar-foot { display: grid; gap: 2px; padding-top: 8px; border-top: 1px solid var(--border) }
.side-link { display: flex; align-items: center; gap: 10px; height: 36px; padding: 0 10px; border-radius: var(--r-sm); font-size: 14px; color: var(--text-2); text-decoration: none; text-align: left; transition: background .15s }
.side-link:hover { background: var(--surface-2); color: var(--text) }
.conn { display: flex; align-items: center; gap: 8px; padding: 8px 10px; font-size: 12px; color: var(--muted) }
.conn .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--warn) }
.conn.ready .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 18%, transparent) }
.scrim { position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / .4); backdrop-filter: blur(2px) }

/* Main column */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative }
.chat-header { display: flex; align-items: center; gap: 6px; height: 52px; padding: 0 10px; flex: none; background: var(--bg) }
.model-picker {
  display: inline-flex; align-items: center; gap: 6px; max-width: min(420px, 70vw); height: 34px; padding: 0 10px;
  border-radius: var(--r-sm); font-size: 14px; font-weight: 500; transition: background .15s
}
.model-picker:hover:not(:disabled) { background: var(--surface-2) }
.model-picker span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.model-picker .icon { color: var(--muted) }
.header-new { margin-left: auto; display: none }
.sidebar-closed .header-new { display: inline-grid }

.scroll { flex: 1; min-height: 0; overflow-y: auto; scrollbar-gutter: stable both-edges }
.thread { max-width: var(--thread); margin: 0 auto; padding: 8px 24px 32px; min-height: 100% ; display: flex; flex-direction: column }
#messages { display: flex; flex-direction: column; gap: 28px }

/* Empty state */
.welcome { margin: auto 0; padding: 0 0 12vh; animation: rise .4s var(--ease) both }
.welcome h1 { font-size: clamp(26px, 3.4vw, 34px); font-weight: 600; letter-spacing: -.025em; line-height: 1.15 }
.welcome p { margin-top: 8px; font-size: clamp(17px, 2vw, 21px); color: var(--muted); letter-spacing: -.01em }

/* Messages */
.msg { animation: rise .25s var(--ease) both }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; padding-top: 4px }
.bubble {
  max-width: min(85%, 620px); padding: 10px 16px; border-radius: 18px; background: var(--surface-2);
  white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.55
}
.file-chip { display: inline-flex; align-items: center; gap: 8px; max-width: 80%; padding: 6px 10px; border: 1px solid var(--border); border-radius: var(--r); background: var(--surface); font-size: 13px; color: var(--text-2) }
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.msg.assistant { display: grid; grid-template-columns: 32px minmax(0, 1fr); gap: 14px }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; border: 1px solid var(--border); background: var(--surface); color: var(--text-2) }
.content { min-width: 0; padding-top: 3px }
.msg-actions { display: flex; gap: 2px; margin-top: 8px; margin-left: -8px; opacity: 0; transition: opacity .15s }
.msg.assistant:hover .msg-actions, .msg-actions:focus-within { opacity: 1 }
.ghost-btn { display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 8px; border-radius: var(--r-sm); font-size: 12px; color: var(--muted); transition: background .15s, color .15s }
.ghost-btn:hover { background: var(--surface-2); color: var(--text) }
.ghost-btn.copied, .code-copy.copied { color: var(--ok) }

/* Typing & tool status */
.typing { display: flex; gap: 5px; height: 26px; align-items: center }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); animation: blink 1.2s infinite ease-in-out }
.typing span:nth-child(2) { animation-delay: .15s }
.typing span:nth-child(3) { animation-delay: .3s }
.tool-status { display: inline-flex; align-items: center; gap: 8px; margin-bottom: 6px; padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface-2); font-size: 12px; color: var(--muted) }
.tool-status::before { content: ""; width: 10px; height: 10px; border: 1.5px solid var(--muted); border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite }
.streaming .prose > :last-child::after { content: ""; display: inline-block; width: 7px; height: 15px; margin-left: 3px; vertical-align: -2px; border-radius: 2px; background: var(--text); animation: cursor 1s steps(2, start) infinite }
@keyframes cursor { 50% { opacity: 0 } }

/* Prose (rendered markdown) */
.prose { line-height: 1.7; overflow-wrap: anywhere; color: var(--text) }
.prose > * + * { margin-top: 14px }
.prose h2, .prose h3, .prose h4, .prose h5, .prose h6 { font-weight: 600; letter-spacing: -.015em; line-height: 1.3; margin-top: 24px }
.prose h2 { font-size: 20px } .prose h3 { font-size: 17px } .prose h4, .prose h5, .prose h6 { font-size: 15px }
.prose > :first-child { margin-top: 0 }
.prose ul, .prose ol { padding-left: 22px; margin-bottom: 0 }
.prose li + li { margin-top: 6px }
.prose li > ul, .prose li > ol { margin-top: 6px }
.prose li::marker { color: var(--muted) }
.prose strong { font-weight: 600 }
.prose a { text-decoration: underline; text-decoration-color: var(--border-strong); text-underline-offset: 3px }
.prose a:hover { text-decoration-color: currentColor }
.prose code { font: 13px/1.5 ui-monospace, "Cascadia Code", "SF Mono", Consolas, monospace; background: var(--code-bg); padding: 2px 6px; border-radius: 5px }
.prose blockquote { margin: 14px 0 0; padding-left: 14px; border-left: 3px solid var(--border-strong); color: var(--text-2) }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 22px 0 }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--r) }
.prose table { width: 100%; border-collapse: collapse; font-size: 14px }
.prose th, .prose td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border) }
.prose th { background: var(--surface-2); font-weight: 600 }
.prose tr:last-child td { border-bottom: 0 }
.code-block { border: 1px solid var(--border); border-radius: var(--r); overflow: hidden; background: var(--code-block) }
.code-head { display: flex; align-items: center; justify-content: space-between; height: 36px; padding: 0 6px 0 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); font: 12px ui-monospace, Consolas, monospace; color: var(--muted) }
.code-copy { display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 8px; border-radius: 5px; font: 500 12px Inter, system-ui, sans-serif; color: var(--muted) }
.code-copy:hover { background: var(--surface-3); color: var(--text) }
/* The effective color-scheme follows the user's system or the local theme override. */
.code-block {
  --tok-comment: light-dark(#596579, #9aa8bb);
  --tok-keyword: light-dark(#773da0, #d6a5ff);
  --tok-string: light-dark(#136247, #85dab1);
  --tok-key: light-dark(#095988, #8acff5);
  --tok-value: light-dark(#94431e, #f8b683);
  --tok-call: light-dark(#294c9e, #a5baff);
}
.code-block .tok-comment { color: var(--tok-comment) }
.code-block .tok-keyword { color: var(--tok-keyword) }
.code-block .tok-string { color: var(--tok-string) }
.code-block .tok-key { color: var(--tok-key) }
.code-block .tok-value { color: var(--tok-value) }
.code-block .tok-call { color: var(--tok-call) }
.code-block pre { margin: 0; padding: 14px 16px; overflow-x: auto }
.code-block pre code { background: none; padding: 0; font-size: 13px; line-height: 1.65; white-space: pre }

/* Sources */
.sources { margin-top: 18px }
.sources-label { font-size: 12px; font-weight: 500; color: var(--muted); margin-bottom: 8px }
.source-chips { display: flex; flex-wrap: wrap; gap: 6px }
.source { position: relative; max-width: 100% }
.source[open] { flex-basis: 100% }
.source summary {
  display: inline-flex; align-items: center; gap: 8px; max-width: 280px; height: 30px; padding: 0 10px 0 4px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface); font-size: 13px; color: var(--text-2);
  cursor: pointer; list-style: none; transition: background .15s, border-color .15s
}
.source summary::-webkit-details-marker { display: none }
.source summary:hover, .source[open] summary { background: var(--surface-2); border-color: var(--border-strong) }
.source-num { display: grid; place-items: center; min-width: 22px; height: 22px; border-radius: 50%; background: var(--surface-3); font-size: 11px; font-weight: 600; color: var(--text-2) }
.source-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.source-body { margin-top: 8px; padding: 14px 16px; border: 1px solid var(--border); border-radius: var(--r); background: var(--bg-subtle); font-size: 13px; line-height: 1.6; color: var(--text-2) }
.source-body p { white-space: pre-wrap; max-height: 220px; overflow: auto }
.source-body a { display: inline-flex; align-items: center; gap: 6px; margin-top: 10px; font-weight: 500; color: var(--text); text-decoration: none }
.source-body a:hover span { text-decoration: underline }

/* Composer */
.composer-wrap { flex: none; width: 100%; max-width: var(--thread); margin: 0 auto; padding: 0 24px max(14px, env(safe-area-inset-bottom)); background: linear-gradient(to bottom, transparent, var(--bg) 18px) }
.status { margin: 0 4px 8px; padding: 8px 12px; border-radius: var(--r); background: var(--warn-bg); border: 1px solid var(--warn-line); color: var(--warn); font-size: 13px }
.status:empty { display: none }
.composer {
  position: relative; display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: var(--r-xl);
  background: var(--surface); box-shadow: var(--shadow); padding: 8px; transition: border-color .15s, box-shadow .15s
}
.composer:focus-within { border-color: var(--border-strong); box-shadow: var(--shadow), 0 0 0 3px color-mix(in srgb, var(--ring) 14%, transparent) }
.composer textarea {
  width: 100%; min-height: 44px; max-height: 220px; resize: none; border: 0; background: none; padding: 10px 10px 6px;
  font-size: 15px; line-height: 1.5; color: var(--text); overflow-y: auto
}
.composer textarea:focus-visible { outline: none }
.attachment { display: flex; align-items: center; gap: 10px; width: fit-content; max-width: 100%; margin: 4px 4px 2px; padding: 6px 6px 6px 8px; border: 1px solid var(--border); border-radius: var(--r); background: var(--bg-subtle) }
.attachment-icon { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 7px; background: var(--primary); color: var(--primary-fg) }
.attachment-meta { display: grid; min-width: 0 }
.attachment-meta strong { font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px }
.attachment-meta small { font-size: 12px; color: var(--muted) }
.search-group { display: flex; align-items: center; gap: 8px; margin: 0 6px 4px; padding: 0 10px; height: 34px; border-radius: var(--r-sm); background: var(--surface-2); color: var(--muted) }
.search-group input { flex: 1; border: 0; background: none; font-size: 13px; color: var(--text); height: 100% }
.search-group input:focus-visible { outline: none }
.composer-actions { display: flex; align-items: center; gap: 4px }
.upload { cursor: pointer }
.pill { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; border: 1px solid var(--border); border-radius: 999px; font-size: 13px; font-weight: 500; color: var(--muted); transition: all .15s }
.pill:hover:not(:disabled) { color: var(--text); background: var(--surface-2) }
.pill[aria-pressed="true"] { color: var(--text); background: var(--surface-2); border-color: var(--border-strong) }
.pill.unavailable { opacity: .55 }
.send { margin-left: auto; display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; background: var(--primary); color: var(--primary-fg); transition: background .15s, opacity .15s, transform .1s }
.send:hover:not(:disabled) { background: var(--primary-hover) }
.send:active:not(:disabled) { transform: scale(.94) }
.send:disabled { opacity: .25 }
.send .spinner { display: none; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite }
.composer.busy .send { opacity: 1 }
.composer.busy .send .arrow { display: none }
.composer.busy .send .spinner { display: block }
.disclaimer { margin-top: 8px; text-align: center; font-size: 12px; color: var(--faint) }

/* Model palette */
.palette {
  width: min(560px, calc(100vw - 24px)); max-height: min(520px, 76dvh); margin: 12vh auto auto; padding: 0;
  border: 1px solid var(--border); border-radius: var(--r-lg); background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg); overflow: hidden
}
.palette[open] { display: flex; flex-direction: column; animation: pop .16s var(--ease) both }
.palette::backdrop { background: rgb(0 0 0 / .35); backdrop-filter: blur(3px) }
.palette-search { display: flex; align-items: center; gap: 10px; padding: 0 12px 0 16px; height: 52px; border-bottom: 1px solid var(--border); color: var(--muted) }
.palette-search input { flex: 1; height: 100%; border: 0; background: none; font-size: 15px; color: var(--text) }
.palette-search input:focus-visible { outline: none }
.palette-search input::-webkit-search-cancel-button { display: none }
.count { font-size: 12px; color: var(--faint); font-variant-numeric: tabular-nums }
.palette-list { overflow-y: auto; padding: 6px; min-height: 0 }
.palette-group { padding: 10px 10px 4px; font-size: 12px; font-weight: 500; color: var(--muted) }
.palette-empty { padding: 32px; text-align: center; color: var(--muted); font-size: 14px }
.model-row { width: 100%; display: flex; align-items: center; gap: 12px; padding: 8px 10px; border-radius: var(--r-sm); text-align: left }
.model-row.active { background: var(--surface-2) }
.model-text { display: grid; min-width: 0; flex: 1 }
.model-text strong { font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.model-text small { font: 12px ui-monospace, Consolas, monospace; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.model-row .icon { color: var(--text) }

/* =========================================================
   Admin
   ========================================================= */
.admin-body { background: var(--bg-subtle); background: light-dark(var(--bg), var(--bg-subtle)); min-height: 100% }
.admin-top { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 28px; height: 56px; padding: 0 clamp(16px, 3vw, 32px); background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: saturate(1.6) blur(10px); border-bottom: 1px solid var(--border) }
.tabs { display: flex; gap: 4px; height: 100% }
.tabs a { display: flex; align-items: center; padding: 0 10px; font-size: 14px; color: var(--muted); text-decoration: none; position: relative; transition: color .15s }
.tabs a:hover { color: var(--text) }
.tabs a[aria-current] { color: var(--text); font-weight: 500 }
.tabs a[aria-current]::after { content: ""; position: absolute; left: 10px; right: 10px; bottom: -1px; height: 2px; background: var(--text); border-radius: 2px }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px }
.admin { max-width: 1200px; margin: 0 auto; padding: clamp(24px, 4vw, 40px) clamp(16px, 3vw, 32px) 64px }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 24px }
.page-head h1 { font-size: 28px; font-weight: 600; letter-spacing: -.025em; line-height: 1.2 }
.page-head p { margin-top: 4px; color: var(--muted); font-size: 14px }
.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 50; max-width: 360px; padding: 12px 16px; border-radius: var(--r);
  background: var(--primary); color: var(--primary-fg); font-size: 14px; box-shadow: var(--shadow-lg);
  opacity: 0; transform: translateY(8px); pointer-events: none; transition: opacity .2s, transform .2s var(--ease)
}
.toast.show { opacity: 1; transform: none }
.toast[data-kind="error"] { background: var(--bad); color: #fff }

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border: 1px solid var(--border); border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow-sm); margin-bottom: 16px; overflow: hidden }
.stat { padding: 18px 20px; display: grid; gap: 4px }
.stat + .stat { border-left: 1px solid var(--border) }
.stat span { font-size: 13px; color: var(--muted); font-weight: 500 }
.stat strong { font-size: 28px; font-weight: 600; letter-spacing: -.03em; line-height: 1.2; font-variant-numeric: tabular-nums }
.stat small { font-size: 12px; color: var(--faint) }

.grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; align-items: start }
.span-2 { grid-column: 1 / -1 }
.stack { display: grid; gap: 16px }
.card { border: 1px solid var(--border); border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow-sm); padding: 20px }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px }
.card-head h2 { font-size: 16px; font-weight: 600; letter-spacing: -.01em }
.card-head p { margin-top: 4px; font-size: 13px; color: var(--muted); line-height: 1.5 }
.card .table-wrap { margin: 0 -20px; border: 0; border-radius: 0; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border) }
table.data { width: 100%; border-collapse: collapse; font-size: 14px }
table.data th { padding: 10px 12px; text-align: left; font-size: 12px; font-weight: 500; color: var(--muted); background: var(--bg-subtle); border-bottom: 1px solid var(--border); white-space: nowrap }
table.data td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: middle }
table.data tr:last-child td { border-bottom: 0 }
table.data tbody tr:hover td { background: var(--bg-subtle) }
table.data th:first-child, table.data td:first-child { padding-left: 20px }
table.data th:last-child, table.data td:last-child { padding-right: 16px }
.num { text-align: right !important; font-variant-numeric: tabular-nums; white-space: nowrap }
.muted { color: var(--muted); white-space: nowrap }
td.empty { padding: 28px 20px !important; text-align: center; color: var(--muted) }
.key-name, .file-name { display: inline-flex; align-items: center; gap: 10px; min-width: 0 }
.key-name .icon, .file-name .icon { color: var(--muted) }
.key-name strong { font-weight: 500 }
.actions-col { width: 1%; white-space: nowrap }
.row-actions { display: flex; justify-content: flex-end; gap: 2px }
.inline-form { display: flex; align-items: flex-end; gap: 12px; margin-top: 16px }
.inline-form .field { width: 220px }
.inline-form .grow { flex: 1; width: auto }
.file-list { list-style: none; margin: 0 0 16px; padding: 0; border: 1px solid var(--border); border-radius: var(--r) }
.file-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 6px 6px 12px; font-size: 14px; border-bottom: 1px solid var(--border) }
.file-list li:last-child { border-bottom: 0 }
.file-list li.empty { justify-content: center; padding: 20px; color: var(--muted) }
.file-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
.upload-form { display: grid; gap: 10px }
.dropzone { display: grid; justify-items: center; gap: 4px; padding: 20px; border: 1px dashed var(--border-strong); border-radius: var(--r); text-align: center; cursor: pointer; color: var(--muted); transition: background .15s, border-color .15s }
.dropzone:hover, .dropzone:focus-within { background: var(--bg-subtle); border-color: var(--ring) }
.dropzone span { font-size: 14px; font-weight: 500; color: var(--text); overflow-wrap: anywhere }
.dropzone small { font-size: 12px }
#settings { display: grid; gap: 12px }
.btn-row { display: flex; gap: 8px }

.login-card { max-width: 380px; margin: 10vh auto 0; display: grid; gap: 14px; padding: 32px; border: 1px solid var(--border); border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow) }
.login-card h1 { font-size: 22px; font-weight: 600; letter-spacing: -.02em; margin-top: 6px }
.login-card > p { color: var(--muted); font-size: 14px; margin-top: -8px }

/* ---------- Motion ---------- */
@keyframes rise { from { opacity: 0; transform: translateY(6px) } to { opacity: 1; transform: none } }
@keyframes pop { from { opacity: 0; transform: scale(.97) } to { opacity: 1; transform: none } }
@keyframes blink { 0%, 80%, 100% { opacity: .25; transform: scale(.85) } 40% { opacity: 1; transform: none } }
@keyframes spin { to { transform: rotate(360deg) } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; scroll-behavior: auto !important }
}

/* ---------- Responsive ---------- */
@media (max-width: 1024px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)) }
  .stat:nth-child(3) { border-left: 0 }
  .stat:nth-child(n+3) { border-top: 1px solid var(--border) }
  .grid { grid-template-columns: 1fr }
}
@media (max-width: 860px) {
  .sidebar { position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; margin-left: 0 !important; transform: translateX(-100%); visibility: hidden; transition: transform .25s var(--ease), visibility 0s .25s }
  .sidebar-open .sidebar { transform: none; visibility: visible; box-shadow: var(--shadow-lg); transition: transform .25s var(--ease) }
  .header-new { display: inline-grid }
}
@media (max-width: 640px) {
  :root { font-size: 15px }
  .thread { padding: 4px 16px 24px }
  .composer-wrap { padding: 0 10px max(10px, env(safe-area-inset-bottom)) }
  .composer { border-radius: 18px }
  .msg.assistant { grid-template-columns: minmax(0, 1fr); gap: 8px }
  .msg.assistant .avatar { width: 26px; height: 26px }
  .bubble { max-width: 90% }
  .msg-actions { opacity: 1 }
  .disclaimer { font-size: 11px }
  .admin-top { gap: 12px }
  .admin-top .brand { font-size: 0; gap: 0 }
  .page-head h1 { font-size: 24px }
  .stat strong { font-size: 22px }
  .inline-form { flex-direction: column; align-items: stretch }
  .inline-form .field { width: auto }
  table.data th:nth-child(5), table.data td:nth-child(5) { display: none }
}
.answer-limit { display:flex; align-items:center; flex-wrap:wrap; gap:.65rem; margin:.8rem 0; font-size:.9rem; }
.source-kind { font-weight:600; }
