:root {
  --bg: #ffffff; --side: #f9f9f9; --surface: #ffffff; --bubble: #f1f1f1; --hover: #ececec;
  --text: #0d0d0d; --muted: #6b6b6b; --border: rgba(0, 0, 0, .1); --accent: #0d0d0d; --on-accent: #fff;
  --code-bg: #f7f7f7; --danger: #d92d20; --shadow: 0 4px 24px rgba(0, 0, 0, .08), 0 0 0 1px var(--border);
  --k: #a626a4; --s: #50a14f; --n: #b76b01; --c: #a0a1a7; --f: #4078f2; --t: #c18401;
  --font: ui-sans-serif, -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121212; --side: #0b0b0b; --surface: #222222; --bubble: #2a2a2a; --hover: #262626;
    --text: #ececec; --muted: #9b9b9b; --border: rgba(255, 255, 255, .12); --accent: #ececec; --on-accent: #0d0d0d;
    --code-bg: #1b1b1b; --danger: #f97066; --shadow: 0 4px 24px rgba(0, 0, 0, .5), inset 0 0 0 1px var(--border);
    --k: #c678dd; --s: #98c379; --n: #d19a66; --c: #7f848e; --f: #61afef; --t: #e5c07b;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121212; --side: #0b0b0b; --surface: #222222; --bubble: #2a2a2a; --hover: #262626;
  --text: #ececec; --muted: #9b9b9b; --border: rgba(255, 255, 255, .12); --accent: #ececec; --on-accent: #0d0d0d;
  --code-bg: #1b1b1b; --danger: #f97066; --shadow: 0 4px 24px rgba(0, 0, 0, .5), inset 0 0 0 1px var(--border);
  --k: #c678dd; --s: #98c379; --n: #d19a66; --c: #7f848e; --f: #61afef; --t: #e5c07b;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.5 var(--font); -webkit-font-smoothing: antialiased; overflow: hidden; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
svg.i { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--f); outline-offset: 1px; }

.app { display: flex; height: 100dvh; }
.icon-btn { display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 8px; color: var(--muted); }
.icon-btn:hover { background: var(--hover); color: var(--text); }

/* Sidebar: full panel, or a 52px icon rail when collapsed */
.sidebar { width: 260px; flex: none; display: flex; flex-direction: column; gap: 2px; padding: 8px 6px; background: var(--side); border-right: 1px solid var(--border); transition: width .2s; overflow: hidden; }
.side-head { display: flex; align-items: center; gap: 4px; height: 44px; padding: 0 2px; }
.brand { font-weight: 600; font-size: 17px; white-space: nowrap; }
.side-item { display: flex; align-items: center; gap: 10px; height: 36px; padding: 0 10px; border-radius: 8px; font-size: 14px; white-space: nowrap; text-align: left; }
.side-item:hover { background: var(--hover); }
.side-search { margin: 4px 4px; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); font-size: 14px; }
.side-label { padding: 16px 10px 6px; font-size: 13px; color: var(--muted); }
.chat-list { flex: 1; overflow-y: auto; margin: 0 -2px; padding: 0 2px; }
.chat-item { position: relative; display: flex; align-items: center; border-radius: 8px; font-size: 14px; }
.chat-item a { flex: 1; min-width: 0; padding: 8px 10px; color: inherit; text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item:hover, .chat-item.active { background: var(--hover); }
.chat-item .more { width: 28px; height: 28px; margin-right: 4px; opacity: 0; }
.chat-item:hover .more, .chat-item.active .more, .chat-item .more:focus-visible { opacity: 1; }
.chat-item input { flex: 1; margin: 2px; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--f); background: var(--bg); font-size: 14px; }
.side-foot { margin-top: auto; }
.collapsed .sidebar { width: 52px; }
.collapsed .sidebar .label, .collapsed .side-search { display: none; }
.collapsed .chat-list { display: none; }
.collapsed .side-foot { margin-top: auto; }

/* Main column */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.topbar { display: flex; align-items: center; gap: 4px; height: 52px; padding: 0 8px; flex: none; }
.spacer { flex: 1; }
.model-wrap { position: relative; }
.model-btn { display: flex; align-items: center; gap: 4px; height: 36px; padding: 0 10px; border-radius: 8px; font-size: 17px; font-weight: 500; max-width: 60vw; }
.model-btn span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-btn:hover { background: var(--hover); }
.model-btn svg { color: var(--muted); width: 16px; }

.popover { position: absolute; z-index: 30; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); padding: 6px; }
.model-menu { top: 42px; left: 0; width: min(360px, calc(100vw - 24px)); }
.model-menu input { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: transparent; font-size: 14px; margin-bottom: 4px; }
.model-list { max-height: min(420px, 60vh); overflow-y: auto; }
.model-group { padding: 8px 10px 4px; font-size: 12px; color: var(--muted); }
.model-opt { display: flex; width: 100%; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; font-size: 14px; text-align: left; word-break: break-all; }
.model-opt:hover, .model-opt[aria-selected="true"] { background: var(--hover); }
.model-opt[aria-selected="true"]::after { content: "✓"; margin-left: auto; }
.model-empty { padding: 12px 10px; font-size: 14px; color: var(--muted); }
.model-empty button { color: var(--f); text-decoration: underline; }

.chat-menu { position: fixed; min-width: 150px; }
.chat-menu button { display: flex; width: 100%; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; font-size: 14px; }
.chat-menu button:hover { background: var(--hover); }
.chat-menu svg { width: 16px; }
.danger { color: var(--danger); }

.scroll { flex: 1; overflow-y: auto; display: flex; flex-direction: column; scrollbar-gutter: stable both-edges; }
.thread { flex: 1; width: 100%; max-width: 800px; margin: 0 auto; padding: 8px 16px 24px; }
.composer-wrap { position: sticky; bottom: 0; width: 100%; max-width: 800px; margin: 0 auto; padding: 0 16px; background: var(--bg); }
.composer-wrap::before { content: ""; position: absolute; left: 0; right: 0; top: -24px; height: 24px; background: linear-gradient(transparent, var(--bg)); pointer-events: none; }
.empty { display: none; margin: 0 0 28px; text-align: center; font-size: 28px; font-weight: 400; letter-spacing: -.01em; }
.is-empty .scroll { justify-content: center; }
.is-empty .thread { display: none; }
.is-empty .empty { display: block; }
.is-empty .composer-wrap { position: static; padding-bottom: 12vh; }
.is-empty .composer-wrap::before, .is-empty .disclaimer { visibility: hidden; }

.composer { background: var(--surface); border-radius: 28px; box-shadow: var(--shadow); padding: 10px; cursor: text; }
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea { flex: 1; min-height: 36px; max-height: 40vh; padding: 6px 4px; border: 0; outline: 0; resize: none; background: transparent; line-height: 24px; }
.composer textarea::placeholder { color: var(--muted); }
.send-btn { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: var(--on-accent); flex: none; }
.send-btn:disabled { opacity: .3; cursor: default; }
.send-btn svg { stroke-width: 2.2; }
#micBtn.on { color: var(--danger); background: var(--hover); }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; padding: 2px 4px 8px; }
.attachments:empty { display: none; }
.chip { position: relative; display: flex; align-items: center; gap: 8px; height: 56px; padding: 0 12px 0 8px; border-radius: 12px; border: 1px solid var(--border); font-size: 13px; max-width: 220px; }
.chip img { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .x { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; }
.chip .x svg { width: 12px; height: 12px; stroke-width: 3; }
.disclaimer { margin: 0; padding: 8px 0 10px; text-align: center; font-size: 12px; color: var(--muted); }

/* Messages */
.msg { margin: 0 0 28px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.bubble { max-width: 80%; padding: 10px 18px; border-radius: 22px; background: var(--bubble); white-space: pre-wrap; word-wrap: break-word; }
.msg-files { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-bottom: 8px; }
.msg-files img { max-width: 240px; max-height: 240px; border-radius: 14px; display: block; }
.msg-files .chip { height: 44px; }
.actions { display: flex; gap: 2px; margin-top: 6px; min-height: 32px; align-items: center; color: var(--muted); }
.actions .icon-btn { width: 32px; height: 32px; }
.actions svg { width: 18px; height: 18px; }
.msg.user .actions { opacity: 0; transition: opacity .15s; }
.msg.user:hover .actions, .msg.user:focus-within .actions { opacity: 1; }
.meta { font-size: 12px; margin-left: 6px; }
.edit-box { width: 100%; max-width: 80%; background: var(--bubble); border-radius: 22px; padding: 12px 16px; }
.edit-box textarea { width: 100%; border: 0; outline: 0; resize: vertical; background: transparent; min-height: 60px; }
.edit-box .row { justify-content: flex-end; }
.error-text { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); padding: 10px 14px; border-radius: 12px; font-size: 14px; }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md blockquote, .md table, .md .code { margin: 0 0 14px; }
.md h1, .md h2, .md h3, .md h4 { margin: 22px 0 10px; line-height: 1.3; font-weight: 600; }
.md h1 { font-size: 1.5em; } .md h2 { font-size: 1.3em; } .md h3 { font-size: 1.12em; }
.md ul, .md ol { padding-left: 1.6em; }
.md li { margin: 4px 0; }
.md a { color: var(--f); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 24px 0; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 14px; color: var(--muted); }
.md :not(pre) > code { font: .875em var(--mono); background: var(--bubble); padding: 2px 5px; border-radius: 5px; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 14px; }
.md th, .md td { border-bottom: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.md img { max-width: 100%; }
.code { border-radius: 14px; background: var(--code-bg); border: 1px solid var(--border); overflow: hidden; }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 8px 6px 14px; font-size: 12px; color: var(--muted); }
.code-head button { display: flex; align-items: center; gap: 4px; padding: 4px 8px; border-radius: 6px; }
.code-head button:hover { background: var(--hover); color: var(--text); }
.code-head svg { width: 14px; height: 14px; }
.md pre { margin: 0; padding: 4px 16px 16px; overflow-x: auto; font: 13.5px/1.6 var(--mono); }
.thinking { display: inline-block; width: 12px; height: 12px; border-radius: 50%; background: var(--text); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; transform: scale(.8); } }

.hljs-keyword, .hljs-selector-tag, .hljs-built_in, .hljs-meta { color: var(--k); }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-attribute { color: var(--s); }
.hljs-number, .hljs-literal, .hljs-symbol { color: var(--n); }
.hljs-comment, .hljs-quote { color: var(--c); font-style: italic; }
.hljs-title, .hljs-function .hljs-title, .hljs-section { color: var(--f); }
.hljs-type, .hljs-class .hljs-title, .hljs-variable, .hljs-attr, .hljs-params { color: var(--t); }
.hljs-deletion { color: var(--danger); }

/* Settings dialog */
.settings { width: min(640px, calc(100vw - 24px)); max-height: calc(100dvh - 48px); padding: 0; border: 0; border-radius: 18px; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.settings::backdrop { background: rgba(0, 0, 0, .5); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 10px 22px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 1; }
.dialog-head h2 { margin: 0; font-size: 18px; font-weight: 600; }
.dialog-body { padding: 8px 22px 22px; }
.settings h3 { margin: 18px 0 6px; font-size: 15px; font-weight: 600; }
.hint { margin: 0 0 10px; font-size: 13px; color: var(--muted); font-weight: 400; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.btn { height: 36px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--border); font-size: 14px; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn.ghost { border-color: transparent; }
.settings select, .settings input, .settings textarea { width: 100%; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); font-size: 14px; }
.settings textarea { resize: vertical; font-family: var(--font); }
.settings .row select { width: auto; flex: 1; min-width: 160px; height: 36px; }
.provider-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.provider { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; }
.provider div { flex: 1; min-width: 0; }
.provider b { display: block; font-size: 14px; font-weight: 600; }
.provider small { display: block; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.provider-form { margin-top: 14px; padding: 14px; border: 1px solid var(--border); border-radius: 14px; display: flex; flex-direction: column; gap: 10px; }
.provider-form h3 { margin: 0; }
.provider-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.provider-form label .hint { display: inline; margin: 0; }
.form-status { margin: 0; font-size: 13px; color: var(--muted); min-height: 1em; }
.form-status.err { color: var(--danger); }

.scrim { display: none; }
.mobile-only { display: none; }
@media (max-width: 767px) {
  .mobile-only { display: inline-grid; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 40; width: 280px; transform: translateX(-100%); transition: transform .2s; }
  .collapsed .sidebar { width: 280px; }
  .collapsed .sidebar .label { display: revert; }
  .collapsed .chat-list { display: block; }
  .side-open .sidebar { transform: none; }
  .side-open .scrim { display: block; position: fixed; inset: 0; z-index: 35; background: rgba(0, 0, 0, .4); }
  .topbar { justify-content: space-between; }
  .model-wrap { flex: 1; display: flex; justify-content: center; }
  .model-menu { left: 50%; transform: translateX(-50%); }
  .topbar .spacer { display: none; }
  .bubble, .edit-box { max-width: 90%; }
  .empty { font-size: 24px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
.toast { position: fixed; left: 50%; bottom: 110px; transform: translateX(-50%); z-index: 50; width: max-content; max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px; background: var(--accent); color: var(--on-accent); font-size: 14px; box-shadow: var(--shadow); }
