/* ===== Fonts (self-hosted) ===== */
@font-face { font-family: "Inter"; font-style: normal; font-weight: 100 900; font-display: swap; src: url(/static/vendor/inter-latin-wght-normal.woff2) format("woff2-variations"); }
@font-face { font-family: "JetBrains Mono"; font-style: normal; font-weight: 100 800; font-display: swap; src: url(/static/vendor/jetbrains-mono-latin-wght-normal.woff2) format("woff2-variations"); }

/* ===== Tokens ===== */
:root {
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --bg: #f7f7f8; --surface: #ffffff; --surface-2: #f1f1f4; --surface-3: #e7e7ec;
  --border: #e4e4e9; --border-strong: #d4d4db;
  --text: #18181b; --text-2: #55555f; --text-3: #8e8e99;
  --accent: #4f46e5; --accent-hover: #4338ca; --accent-soft: #eef0ff; --accent-text: #ffffff;
  --user-bubble: #e9ebff; --user-text: #1e1b4b;
  --danger: #dc2626; --danger-soft: #fee2e2; --warn: #b45309; --warn-soft: #fef3c7; --ok: #15803d; --ok-soft: #dcfce7;
  --code-bg: #f6f6f9; --code-bar: #ececf1;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 8px 24px -12px rgba(0,0,0,.18);
  --shadow-lg: 0 12px 40px -8px rgba(0,0,0,.25);
  --radius: 14px; --radius-sm: 10px;
  --sb-w: 280px;
  --hl-kw: #7c3aed; --hl-str: #15803d; --hl-cmt: #6b7280; --hl-num: #b45309; --hl-fn: #1d4ed8; --hl-bi: #0e7490; --hl-lit: #be123c; --hl-attr: #b45309; --hl-tag: #1d4ed8;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f0f11; --surface: #18181b; --surface-2: #202024; --surface-3: #2a2a30;
  --border: #27272d; --border-strong: #3a3a42;
  --text: #ececef; --text-2: #a4a4ae; --text-3: #6f6f7a;
  --accent: #818cf8; --accent-hover: #a5b4fc; --accent-soft: #23234a; --accent-text: #0f0f11;
  --user-bubble: #27295a; --user-text: #e6e8ff;
  --danger: #f87171; --danger-soft: #3b1d1d; --warn: #fbbf24; --warn-soft: #3a2d10; --ok: #4ade80; --ok-soft: #14301f;
  --code-bg: #121214; --code-bar: #1c1c20;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.6);
  --shadow-lg: 0 12px 40px -8px rgba(0,0,0,.7);
  --hl-kw: #c084fc; --hl-str: #86efac; --hl-cmt: #8b8b96; --hl-num: #fdba74; --hl-fn: #93c5fd; --hl-bi: #67e8f9; --hl-lit: #fda4af; --hl-attr: #fdba74; --hl-tag: #93c5fd;
  color-scheme: dark;
}

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.55 var(--font); color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; overflow: hidden; }
.hidden { display: none !important; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.sprite { display: none; }
.icon { width: 1.25em; height: 1.25em; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--text-3); font-size: 13px; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; } }
.mobile-only { display: none !important; }

/* ===== Buttons & inputs ===== */
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 40px; padding: 0 16px; border-radius: var(--radius-sm); font-weight: 500; border: 1px solid var(--border); background: var(--surface); color: var(--text); transition: background .15s, border-color .15s, transform .05s; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-text { color: var(--danger); }
.btn.danger-text:hover { background: var(--danger-soft); color: var(--danger); }
.btn.block { width: 100%; }
.btn.tiny { height: 28px; padding: 0 10px; font-size: 12px; }
.btn[disabled] { opacity: .55; cursor: default; transform: none; }
.btn .spinner { display: none; }
.btn.loading .btn-text { visibility: hidden; }
.btn.loading .spinner { display: block; position: absolute; }
.btn { position: relative; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.iconbtn { display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; border-radius: 10px; color: var(--text-2); transition: background .15s, color .15s; flex: none; }
.iconbtn:hover { background: var(--surface-2); color: var(--text); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); padding: 10px 12px; width: 100%; transition: border-color .15s, box-shadow .15s; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
select { width: auto; }
.pill { display: inline-flex; align-items: center; height: 22px; padding: 0 9px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); white-space: nowrap; }
.pill.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.pill.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.pill.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

/* ===== Toasts / banners ===== */
#toasts { position: fixed; top: max(12px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 420px); pointer-events: none; }
.toast { pointer-events: auto; display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-lg); font-size: 14px; animation: toast-in .2s ease-out; }
.toast.error { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.toast.error .icon { color: var(--danger); }
.toast .icon { color: var(--accent); margin-top: 1px; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }
#offline-banner { position: fixed; bottom: max(12px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); z-index: 100; background: var(--warn-soft); color: var(--warn); padding: 8px 14px; border-radius: 999px; font-size: 13px; font-weight: 500; box-shadow: var(--shadow); }

/* ===== Login ===== */
#login-view { display: grid; place-items: center; min-height: 100dvh; padding: 24px; background: radial-gradient(1200px 600px at 50% -10%, color-mix(in srgb, var(--accent) 14%, transparent), transparent 70%), var(--bg); }
.login-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 32px; box-shadow: var(--shadow-lg); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: -.01em; }
.brand-mark { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; flex: none; }
.brand-mark .icon { width: 18px; height: 18px; }
.brand-mark.big { width: 56px; height: 56px; border-radius: 16px; margin: 0 auto 18px; }
.brand-mark.big .icon { width: 30px; height: 30px; }
.login-card h1 { font-size: 22px; letter-spacing: -.02em; margin: 26px 0 6px; }
.login-card .sub { color: var(--text-2); font-size: 14px; margin: 0 0 20px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; color: var(--text-2); }
.pw { position: relative; display: block; }
.pw input { padding-right: 44px; }
.pw-toggle { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 36px; height: 36px; display: grid; place-items: center; color: var(--text-3); border-radius: 8px; }
.pw-toggle:hover { color: var(--text); }
.form-error { color: var(--danger); font-size: 13px; min-height: 1.2em; margin: 0; }
.hint { color: var(--text-3); font-size: 12px; margin: -6px 0 0; }
.strength { height: 4px; border-radius: 2px; background: var(--surface-3); overflow: hidden; }
.strength i { display: block; height: 100%; width: 0; background: var(--danger); transition: width .2s, background .2s; }
.strength[data-level="2"] i { width: 50%; background: var(--warn); }
.strength[data-level="3"] i { width: 75%; background: var(--ok); }
.strength[data-level="4"] i { width: 100%; background: var(--ok); }
.strength[data-level="1"] i { width: 25%; }

/* ===== App shell ===== */
#app-view { display: flex; height: 100dvh; width: 100%; }
#sidebar { width: var(--sb-w); flex: none; display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--border); transition: margin-left .2s ease, transform .2s ease; }
html.sb-collapsed #sidebar { margin-left: calc(-1 * var(--sb-w)); }
html.sb-collapsed #sb-open { display: inline-flex; }
#sb-open { display: none; }
.sb-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 8px 16px; }
.sb-new { padding: 4px 12px 10px; }
.viewing { margin: 0 12px 8px; padding: 8px 10px; border-radius: 10px; background: var(--warn-soft); color: var(--warn); font-size: 13px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#chat-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; scrollbar-width: thin; }
.group-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); padding: 14px 10px 6px; }
.chat-item { position: relative; display: flex; align-items: center; gap: 8px; padding: 0 6px 0 10px; height: 38px; border-radius: 10px; color: var(--text); cursor: pointer; user-select: none; }
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--surface-3); font-weight: 500; }
.chat-item .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.chat-item .live { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.chat-item .more { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; color: var(--text-3); opacity: 0; flex: none; }
.chat-item:hover .more, .chat-item.active .more, .chat-item .more:focus-visible { opacity: 1; }
.chat-item .more:hover { background: var(--surface-3); color: var(--text); }
.empty-list { padding: 24px 12px; color: var(--text-3); font-size: 13px; text-align: center; }
.sb-foot { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--border); }
.avatar { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; font-weight: 600; font-size: 13px; flex: none; text-transform: uppercase; }
.me { flex: 1; min-width: 0; line-height: 1.2; }
.me-name { font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sb-foot .iconbtn { width: 34px; height: 34px; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
#topbar { display: flex; align-items: center; gap: 6px; height: 56px; padding: 0 12px 0 10px; border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(8px); flex: none; }
.title-wrap { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; padding: 0 6px; }
#chat-title { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; letter-spacing: -.01em; }

/* ===== Chat ===== */
#chat-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; position: relative; }
#scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; scrollbar-width: thin; }
.thread { max-width: 800px; margin: 0 auto; padding: 24px 20px 16px; }
#empty-state { max-width: 560px; margin: 0 auto; padding: clamp(40px, 14vh, 120px) 20px 20px; text-align: center; }
#empty-state h2 { font-size: 26px; letter-spacing: -.02em; margin: 0 0 8px; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 22px; }
.chip-btn { padding: 8px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); font-size: 13px; color: var(--text-2); transition: border-color .15s, color .15s, background .15s; }
.chip-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.msg { margin: 0 0 22px; animation: msg-in .18s ease-out; }
@keyframes msg-in { from { opacity: 0; transform: translateY(4px); } }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { max-width: min(85%, 640px); background: var(--user-bubble); color: var(--user-text); border-radius: 18px 18px 4px 18px; padding: 10px 14px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; }
.msg.assistant { display: grid; grid-template-columns: 28px 1fr; gap: 10px; }
.msg.assistant .av { width: 28px; height: 28px; border-radius: 9px; background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; display: grid; place-items: center; margin-top: 2px; }
.msg.assistant .av .icon { width: 16px; height: 16px; }
.msg .body { min-width: 0; }
.msg .meta { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-3); margin: 4px 0 6px; flex-wrap: wrap; }
.msg .meta .who { font-weight: 600; color: var(--text-2); }
.tools { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px 0 8px; border-radius: 999px; font-size: 12px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); }
.chip .icon { width: 14px; height: 14px; }
.chip.running { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.chip.running .spinner { width: 12px; height: 12px; border-width: 1.5px; }
.chip.error { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); color: var(--danger); }
.chip .ok { display: none; color: var(--ok); }
.chip.done .ok { display: block; }
.msg-error { display: flex; gap: 8px; align-items: center; margin-top: 10px; padding: 8px 12px; border-radius: 10px; background: var(--danger-soft); color: var(--danger); font-size: 13px; }
.cursor::after { content: ""; display: inline-block; width: 8px; height: 1em; vertical-align: text-bottom; margin-left: 2px; background: var(--accent); border-radius: 2px; animation: blink 1s steps(2) infinite; }
.md:empty.cursor::after { margin-left: 0; }
@keyframes blink { 50% { opacity: 0; } }
#jump-bottom { position: absolute; left: 50%; bottom: 112px; transform: translateX(-50%); width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); color: var(--text-2); z-index: 2; }
#jump-bottom:hover { color: var(--text); }

/* ===== Markdown ===== */
.md { font-size: 15px; line-height: 1.65; overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md .table-wrap, .md .codeblock { margin: 0 0 .9em; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.3em 0 .5em; line-height: 1.3; letter-spacing: -.01em; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; } .md h4 { font-size: 1em; }
.md ul, .md ol { padding-left: 1.4em; } .md li { margin: .2em 0; } .md li > p { margin: 0; }
.md li.task { list-style: none; margin-left: -1.4em; } .md li.task input { width: auto; margin: 0 .5em 0 0; vertical-align: middle; accent-color: var(--accent); }
.md code { font-family: var(--mono); font-size: .88em; background: var(--surface-2); border: 1px solid var(--border); padding: .1em .4em; border-radius: 6px; }
.md pre code { background: none; border: 0; padding: 0; font-size: 13px; line-height: 1.55; white-space: pre; }
.md .codeblock { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--code-bg); }
.md .codebar { display: flex; align-items: center; justify-content: space-between; height: 34px; padding: 0 8px 0 14px; background: var(--code-bar); font-size: 12px; color: var(--text-3); font-family: var(--mono); }
.md .codebar .copy { display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 8px; border-radius: 7px; color: var(--text-2); font-family: var(--font); }
.md .codebar .copy:hover { background: var(--surface-3); color: var(--text); }
.md .codebar .copy .icon { width: 14px; height: 14px; }
.md pre { margin: 0; padding: 12px 14px; overflow-x: auto; scrollbar-width: thin; }
.md blockquote { margin-left: 0; padding: 2px 0 2px 14px; border-left: 3px solid var(--border-strong); color: var(--text-2); }
.md .table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; }
.md th { white-space: nowrap; }
.md th, .md td { padding: 8px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; overflow-wrap: normal; word-break: normal; hyphens: none; min-width: 6ch; }
.md th { background: var(--surface-2); font-weight: 600; }
.md tr:last-child td { border-bottom: 0; }
.md img { max-width: 100%; border-radius: 8px; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
.md a { text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent); text-underline-offset: 2px; }
.md a:hover { text-decoration-color: var(--accent); }
.md kbd { font-family: var(--mono); font-size: .85em; padding: .1em .4em; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 5px; background: var(--surface-2); }
/* highlight.js token colours (theme-aware) */
.hljs { color: var(--text); }
.hljs-keyword, .hljs-selector-tag, .hljs-selector-class, .hljs-selector-id, .hljs-doctag, .hljs-template-tag { color: var(--hl-kw); }
.hljs-string, .hljs-regexp, .hljs-link, .hljs-addition { color: var(--hl-str); }
.hljs-comment, .hljs-quote, .hljs-meta { color: var(--hl-cmt); font-style: italic; }
.hljs-number, .hljs-variable, .hljs-template-variable, .hljs-attr, .hljs-attribute, .hljs-params { color: var(--hl-attr); }
.hljs-title, .hljs-title.function_, .hljs-section, .hljs-name, .hljs-tag { color: var(--hl-fn); }
.hljs-built_in, .hljs-type, .hljs-title.class_, .hljs-class .hljs-title { color: var(--hl-bi); }
.hljs-literal, .hljs-symbol, .hljs-bullet, .hljs-deletion { color: var(--hl-lit); }
.hljs-emphasis { font-style: italic; } .hljs-strong { font-weight: 600; }
.hljs-addition { background: var(--ok-soft); } .hljs-deletion { background: var(--danger-soft); }

/* ===== Composer ===== */
.composer-wrap { flex: none; padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); background: linear-gradient(to top, var(--bg) 70%, transparent); }
#composer { max-width: 800px; margin: 0 auto; display: flex; flex-direction: column; padding: 8px 8px 8px 8px; background: var(--surface); border: 1px solid var(--border-strong); border-radius: 22px; box-shadow: var(--shadow); transition: border-color .15s, box-shadow .15s; }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.attach { width: 36px; height: 36px; color: var(--text-3); }
.attach:hover { color: var(--accent); background: var(--accent-soft); }
.tray { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 4px 8px; }
.att { position: relative; display: flex; align-items: center; gap: 8px; height: 56px; max-width: 240px; padding: 6px 30px 6px 6px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--border); font-size: 13px; }
.att img, .att .fileicon { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; flex: none; }
.att .fileicon { display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.att .fileicon .icon { width: 20px; height: 20px; }
.att .info { display: flex; flex-direction: column; min-width: 0; line-height: 1.3; }
.att .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att .sub { color: var(--text-3); font-size: 12px; }
.att .rm { position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: var(--text-3); }
.att .rm:hover { background: var(--surface-3); color: var(--text); }
.att .rm .icon { width: 13px; height: 13px; }
.att.uploading { opacity: .7; }
.att .spinner { position: absolute; left: 20px; top: 20px; width: 16px; height: 16px; color: var(--accent); }
.att.error { border-color: color-mix(in srgb, var(--danger) 50%, var(--border)); }
.att.error .sub { color: var(--danger); }
#drop-overlay { position: absolute; inset: 0; z-index: 5; display: grid; place-items: center; background: color-mix(in srgb, var(--accent) 10%, var(--bg)); border: 2px dashed var(--accent); margin: 10px; border-radius: 16px; color: var(--accent); font-weight: 600; pointer-events: none; }
#drop-overlay div { display: flex; align-items: center; gap: 10px; font-size: 16px; }
.msg.user { flex-direction: column; align-items: flex-end; gap: 6px; }
.msg-atts { display: flex; flex-wrap: wrap; justify-content: flex-end; align-items: flex-end; gap: 6px; max-width: min(85%, 640px); }
.msg-atts a.img { display: block; border-radius: 12px; overflow: hidden; border: 1px solid var(--border); max-width: 100%; }
.msg-atts a.img img { display: block; max-height: 240px; max-width: 100%; object-fit: cover; }
.msg-atts a.file { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); color: var(--text); text-decoration: none; font-size: 13px; max-width: 100%; }
.msg-atts a.file .icon { color: var(--accent); }
.msg-atts a.file span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent), var(--shadow); }
#prompt { flex: 1; resize: none; border: 0; background: none; padding: 8px 0; max-height: 40vh; line-height: 1.5; font-size: 15px; box-shadow: none; }
#prompt:focus { box-shadow: none; }
#prompt::placeholder { color: var(--text-3); }
.send { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: var(--accent-text); flex: none; transition: background .15s, opacity .15s; }
.send:hover { background: var(--accent-hover); }
.send[disabled] { opacity: .35; cursor: default; background: var(--accent); }
.send.stop { background: var(--text); color: var(--bg); }
.composer-hint { max-width: 800px; margin: 8px auto 0; text-align: center; font-size: 12px; color: var(--text-3); }

/* ===== Menus & dialogs ===== */
.menu { position: fixed; z-index: 60; min-width: 160px; padding: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-lg); animation: toast-in .12s ease-out; }
.menu button { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; border-radius: 8px; font-size: 14px; text-align: left; }
.menu button:hover { background: var(--surface-2); }
.menu button.danger { color: var(--danger); }
.menu .icon { width: 16px; height: 16px; }
dialog { border: 1px solid var(--border); border-radius: 16px; padding: 0; background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg); width: min(92vw, 400px); }
dialog::backdrop { background: rgba(0,0,0,.45); backdrop-filter: blur(2px); }
dialog form { padding: 22px; display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; font-size: 17px; letter-spacing: -.01em; }
dialog p { margin: 0; color: var(--text-2); font-size: 14px; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* ===== Admin ===== */
#admin-pane { flex: 1; min-height: 0; overflow-y: auto; }
.admin-inner { max-width: 800px; margin: 0 auto; padding: 28px 20px 40px; }
.admin-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 18px; }
.admin-head h2 { margin: 0 0 4px; font-size: 22px; letter-spacing: -.02em; }
.admin-head p { margin: 0; }
.users { display: flex; flex-direction: column; gap: 8px; }
.user-row { display: grid; grid-template-columns: 36px 1fr auto auto; align-items: center; gap: 12px; padding: 10px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.user-row .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-row .sub { font-size: 12px; color: var(--text-3); }
.user-row .badges { display: flex; gap: 6px; flex-wrap: wrap; }
.user-row .acts { display: flex; gap: 4px; flex-wrap: wrap; }
h3 { font-size: 15px; margin: 28px 0 10px; }
.add-user { display: flex; gap: 8px; }
.add-user input { flex: 1; min-width: 0; }

/* ===== Responsive ===== */
@media (max-width: 820px) {
  .mobile-only { display: inline-flex !important; }
  .desktop-only { display: none !important; }
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 50; width: min(86vw, 320px); transform: translateX(-100%); box-shadow: none; margin-left: 0 !important; padding-top: env(safe-area-inset-top); }
  html.sb-open #sidebar { transform: none; box-shadow: var(--shadow-lg); }
  #backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.4); opacity: 0; pointer-events: none; transition: opacity .2s; }
  html.sb-open #backdrop { opacity: 1; pointer-events: auto; }
  .chat-item { height: 44px; } .chat-item .more { opacity: 1; }
  #topbar { height: 52px; padding-top: env(safe-area-inset-top); height: calc(52px + env(safe-area-inset-top)); }
  .thread { padding: 16px 14px 8px; }
  .msg.user .bubble { max-width: 92%; }
  .msg.assistant { grid-template-columns: 1fr; gap: 0; } .msg.assistant .av { display: none; }
  .composer-wrap { padding: 6px 10px calc(8px + env(safe-area-inset-bottom)); }
  #composer { border-radius: 20px; }
  .att { max-width: 100%; }
  .msg-atts a.img img { max-height: 200px; }
  #prompt, input, select, textarea { font-size: 16px; } /* prevents iOS zoom */
  #prompt { max-height: 30dvh; }
  .composer-hint { display: none; } #readonly-hint { display: block !important; }
  #readonly-hint.hidden { display: none !important; }
  #jump-bottom { bottom: 84px; }
  .user-row { grid-template-columns: 36px 1fr; }
  .user-row .badges, .user-row .acts { grid-column: 1 / -1; }
  .admin-head { flex-direction: column-reverse; align-items: flex-start; }
  .md pre code { font-size: 12.5px; }
}
