:root {
  --bg: #0b0b10;
  --bg-elev: #12121a;
  --sidebar: #0e0e15;
  --surface: #171722;
  --surface-2: #1e1e2b;
  --border: #262636;
  --border-strong: #34344a;
  --text: #ececf3;
  --text-2: #a3a3b8;
  --text-3: #6e6e86;
  --accent: #8b7cff;
  --accent-2: #22d3ee;
  --accent-soft: rgba(139, 124, 255, 0.14);
  --user-bubble: #22222f;
  --danger: #f87171;
  --ok: #34d399;
  --code-bg: #0f0f17;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  --grad: linear-gradient(135deg, #8b5cf6 0%, #6366f1 50%, #22d3ee 100%);
  --radius: 14px;
  --sidebar-w: 272px;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #fbfbfd;
  --bg-elev: #ffffff;
  --sidebar: #f4f4f8;
  --surface: #ffffff;
  --surface-2: #f1f1f6;
  --border: #e4e4ec;
  --border-strong: #d2d2de;
  --text: #16161f;
  --text-2: #55556a;
  --text-3: #8a8aa0;
  --accent: #6d5cf6;
  --accent-soft: rgba(109, 92, 246, 0.1);
  --user-bubble: #efeff5;
  --code-bg: #0f0f17;
  --shadow: 0 10px 30px rgba(20, 20, 40, 0.12);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; background: none; border: 0; }
input, textarea { font: inherit; color: inherit; }
code, pre, kbd { font-family: "JetBrains Mono", ui-monospace, Consolas, monospace; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.hidden { display: none !important; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.error { color: var(--danger); }

.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 14px 12px;
  gap: 10px;
  flex: none;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 8px; }
.brand-wordmark { height: 28px; width: auto; display: block; margin-left: 4px; }
.hero-wordmark { height: 64px; max-width: 80vw; object-fit: contain; width: auto; display: block; margin-bottom: 28px; }
:root[data-theme="light"] .brand-wordmark, :root[data-theme="light"] .hero-wordmark { filter: invert(1); }
.brand-spacer { flex: 1; }
/* Logo mark is white on transparent: always sits on a dark tile. */
.avatar { background: linear-gradient(145deg, #1c1c28, #08080c); padding: 3px; border: 1px solid #2a2a3a; object-fit: contain; }
.avatar.sm { width: 28px; height: 28px; border-radius: 8px; flex: none; }
.new-chat {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 11px;
  background: var(--surface); border: 1px solid var(--border);
  font-weight: 500; transition: border-color .15s, background .15s;
}
.new-chat:hover { border-color: var(--border-strong); background: var(--surface-2); }
.new-chat kbd { margin-left: auto; font-size: 11px; color: var(--text-3); border: 1px solid var(--border); border-radius: 6px; padding: 1px 6px; }
.search { display: flex; align-items: center; gap: 8px; padding: 0 10px; border-radius: 10px; color: var(--text-3); }
.search input { flex: 1; border: 0; outline: 0; background: transparent; padding: 8px 0; font-size: 14px; color: var(--text); }
.search:focus-within { background: var(--surface); }

.conv-list { flex: 1; overflow-y: auto; margin: 0 -4px; padding: 0 4px; scrollbar-width: thin; }
.conv-group { font-size: 12px; font-weight: 600; color: var(--text-3); padding: 14px 10px 6px; }
.conv {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 9px;
  color: var(--text-2); font-size: 14px; cursor: pointer;
  position: relative;
}
.conv:hover { background: var(--surface); color: var(--text); }
.conv.active { background: var(--surface-2); color: var(--text); }
.conv span { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .del { opacity: 0; color: var(--text-3); padding: 2px; border-radius: 6px; }
.conv:hover .del, .conv.active .del { opacity: 1; }
.conv .del:hover { color: var(--danger); }
.conv .del svg { width: 15px; height: 15px; }
.conv-empty { color: var(--text-3); font-size: 13px; padding: 16px 10px; }

.side-footer { border-top: 1px solid var(--border); padding-top: 10px; display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 9px;
  color: var(--text-2); text-decoration: none; font-size: 14px; text-align: left; width: 100%;
}
.nav-link:hover { background: var(--surface); color: var(--text); }
.nav-link.active { color: var(--text); background: var(--accent-soft); }
.nav-link.active svg { color: var(--accent); }

/* ---------- Main ---------- */
.main { flex: 1; min-width: 0; display: flex; position: relative; }
.view { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100%; }
.view[hidden] { display: none; }

.topbar {
  height: 58px; flex: none;
  display: flex; align-items: center; gap: 8px;
  padding: 0 16px;
  position: relative; z-index: 5;
}
.topbar-title { font-size: 16px; font-weight: 600; margin: 0; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 10px; color: var(--text-2); }
.icon-btn:hover { background: var(--surface); color: var(--text); }
.only-mobile { display: none; }

.badge { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; }
.badge.demo { background: rgba(250, 204, 21, .12); color: #facc15; border: 1px solid rgba(250, 204, 21, .25); }

.model-picker { position: relative; }
.model-btn {
  display: flex; align-items: center; gap: 9px;
  padding: 7px 12px; border-radius: 11px;
  font-weight: 600; font-size: 15px;
}
.model-btn:hover { background: var(--surface); }
.model-btn svg { width: 16px; height: 16px; color: var(--text-3); }
.model-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--grad); box-shadow: 0 0 10px rgba(139,124,255,.7); }
.model-menu {
  position: absolute; top: calc(100% + 6px); left: 0;
  min-width: 320px; padding: 6px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow);
  display: none;
}
.model-menu.open { display: block; animation: pop .14s ease-out; }
.model-opt { display: flex; gap: 12px; align-items: flex-start; padding: 10px 12px; border-radius: 10px; width: 100%; text-align: left; }
.model-opt:hover { background: var(--surface-2); }
.model-opt b { display: block; font-weight: 600; font-size: 14px; }
.model-opt small { color: var(--text-3); font-size: 12px; font-family: "JetBrains Mono", monospace; }
.model-opt .check { margin-left: auto; color: var(--accent); opacity: 0; }
.model-opt.sel .check { opacity: 1; }

.settings-pop {
  position: absolute; top: 54px; right: 16px; width: 300px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); padding: 14px; display: none; flex-direction: column; gap: 8px;
}
.settings-pop.open { display: flex; animation: pop .14s ease-out; }
.settings-pop label { font-size: 13px; font-weight: 600; color: var(--text-2); display: flex; justify-content: space-between; }
.settings-pop input[type=range] { accent-color: var(--accent); width: 100%; }
.settings-pop textarea { resize: vertical; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 14px; outline: 0; }
.settings-pop textarea:focus { border-color: var(--accent); }

@keyframes pop { from { opacity: 0; transform: translateY(-4px) scale(.98); } }

/* ---------- Messages ---------- */
.messages { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.thread { max-width: 780px; margin: 0 auto; padding: 8px 24px 40px; }
.empty {
  max-width: 760px; margin: 0 auto; padding: 9vh 24px 24px;
  display: flex; flex-direction: column; align-items: center; text-align: center;
}
.orb { width: 72px; height: 72px; border-radius: 22px; position: relative; margin-bottom: 22px; }
.orb::before { content: ""; position: absolute; inset: -18px; background: var(--grad); filter: blur(32px); opacity: .45; border-radius: 50%; animation: breathe 5s ease-in-out infinite; }
.orb img { position: relative; width: 100%; height: 100%; border-radius: 22px; padding: 8px; object-fit: contain; background: linear-gradient(145deg, #1c1c28, #08080c); border: 1px solid #2a2a3a; }
@keyframes breathe { 50% { opacity: .25; transform: scale(.9); } }
.empty h1 { font-size: 30px; letter-spacing: -0.025em; margin: 0 0 6px; font-weight: 700; }
.empty p { margin: 0 0 32px; }
.suggestions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; width: 100%; }
.suggestions button {
  text-align: left; padding: 14px 16px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface);
  display: flex; flex-direction: column; gap: 2px; transition: border-color .15s, transform .15s;
}
.suggestions button:hover { border-color: var(--border-strong); transform: translateY(-1px); }
.suggestions b { font-size: 14px; font-weight: 600; }
.suggestions span { font-size: 13px; color: var(--text-2); }

.msg { display: flex; gap: 14px; padding: 14px 0; animation: fadeIn .2s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } }
.msg.user { justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 16px; border-radius: 20px 20px 6px 20px;
  max-width: 85%; white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .avatar { width: 30px; height: 30px; border-radius: 9px; flex: none; margin-top: 2px; }
.msg.assistant .body { flex: 1; min-width: 0; }
.msg-actions { display: flex; gap: 2px; margin-top: 6px; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-actions, .msg:last-child .msg-actions { opacity: 1; }
.msg-actions button { padding: 5px; border-radius: 7px; color: var(--text-3); display: grid; place-items: center; }
.msg-actions button:hover { color: var(--text); background: var(--surface); }
.msg-actions svg { width: 16px; height: 16px; }
.msg-meta { font-size: 12px; color: var(--text-3); margin-left: 6px; align-self: center; }

.reasoning { border: 1px solid var(--border); border-radius: 12px; margin-bottom: 12px; background: var(--surface); }
.reasoning summary { cursor: pointer; padding: 8px 12px; font-size: 13px; color: var(--text-2); font-weight: 500; list-style: none; display: flex; align-items: center; gap: 6px; }
.reasoning summary::before { content: "›"; transition: transform .15s; display: inline-block; font-size: 16px; }
.reasoning[open] summary::before { transform: rotate(90deg); }
.reasoning .r-body { padding: 0 14px 12px; font-size: 13.5px; color: var(--text-2); white-space: pre-wrap; border-top: 1px solid var(--border); padding-top: 10px; }

.md { word-wrap: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3 { line-height: 1.3; margin: 22px 0 10px; letter-spacing: -0.01em; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.08em; }
.md ul, .md ol { padding-left: 22px; margin: 0 0 12px; }
.md li { margin: 4px 0; }
.md a { color: var(--accent); }
.md blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--border-strong); color: var(--text-2); }
.md :not(pre) > code { background: var(--surface-2); padding: 2px 6px; border-radius: 6px; font-size: .88em; }
.md table { border-collapse: collapse; margin: 0 0 14px; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 7px 12px; text-align: left; }
.md th { background: var(--surface); font-weight: 600; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }
.code-wrap { margin: 0 0 14px; border-radius: 12px; overflow: hidden; background: var(--code-bg); border: 1px solid var(--border); }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 8px 6px 14px; font-size: 12px; color: #8b8ba3; background: #161621; font-family: "JetBrains Mono", monospace; }
.code-head button { color: #a3a3b8; font-size: 12px; padding: 3px 8px; border-radius: 6px; display: flex; gap: 5px; align-items: center; }
.code-head button:hover { background: #24243a; color: #fff; }
.code-head svg { width: 14px; height: 14px; }
.md pre { margin: 0; padding: 14px 16px; overflow-x: auto; font-size: 13px; line-height: 1.55; color: #e6e6ef; }
.md pre code.hljs { background: transparent; padding: 0; }

.cursor::after { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--text); margin-left: 4px; vertical-align: middle; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }
.typing { display: flex; gap: 5px; padding: 8px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); animation: bounce 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }
.err-box { border: 1px solid rgba(248,113,113,.35); background: rgba(248,113,113,.08); color: var(--danger); border-radius: 12px; padding: 10px 14px; font-size: 14px; }

/* ---------- Composer ---------- */
.composer-wrap { padding: 0 24px 14px; flex: none; }
.composer {
  max-width: 780px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 22px; padding: 10px 10px 10px 18px;
  box-shadow: 0 4px 24px rgba(0,0,0,.18);
  transition: border-color .15s;
}
.composer:focus-within { border-color: var(--border-strong); }
.composer textarea { flex: 1; border: 0; outline: 0; background: transparent; resize: none; max-height: 220px; min-width: 0; padding: 7px 0; line-height: 1.5; }
.composer textarea::placeholder { color: var(--text-3); }
.send {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--text); color: var(--bg); transition: opacity .15s, transform .1s;
}
.send:disabled { opacity: .25; cursor: default; }
.send:not(:disabled):active { transform: scale(.94); }
.send .i-stop { display: none; }
.streaming .send .i-send { display: none; }
.streaming .send .i-stop { display: block; }
.disclaimer { text-align: center; font-size: 12px; color: var(--text-3); margin: 8px 0 0; }

/* ---------- Pages ---------- */
.page .page-body { flex: 1; overflow-y: auto; padding: 8px 24px 60px; }
.page .page-body > * { max-width: 920px; margin-left: auto; margin-right: auto; }
.hero { padding: 28px 0 26px; }
.eyebrow { display: inline-block; font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; margin-bottom: 10px; }
.hero h1 { font-size: 38px; letter-spacing: -0.03em; line-height: 1.1; margin: 0 0 12px; }
.hero p { max-width: 620px; margin: 0 0 20px; }
.hero code, .card p code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; font-size: .9em; }
.copy-field { display: inline-flex; align-items: center; gap: 12px; padding: 6px 6px 6px 14px; border: 1px solid var(--border); background: var(--surface); border-radius: 12px; max-width: 100%; }
.copy-field .label { font-size: 12px; color: var(--text-3); font-weight: 600; }
.copy-field code { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 20px; margin-bottom: 16px; }
.card.narrow { max-width: 480px; margin-top: 40px; }
.card h3 { margin: 0 0 4px; font-size: 16px; }
.card p { margin: 0 0 14px; font-size: 14px; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head p { margin: 0; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row input { flex: 1; min-width: 180px; padding: 10px 12px; border: 1px solid var(--border); background: var(--bg); border-radius: 10px; outline: 0; }
.row input:focus { border-color: var(--accent); }
.row input.w-sm { flex: 0 1 190px; min-width: 140px; }
.primary-btn { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: 14px; }
.primary-btn:hover { opacity: .88; }
.ghost-btn { border: 1px solid var(--border); padding: 6px 12px; border-radius: 9px; font-size: 13px; font-weight: 500; color: var(--text-2); background: var(--bg-elev); }
.ghost-btn:hover { color: var(--text); border-color: var(--border-strong); }
.danger-btn { color: var(--danger); }
.key-reveal { margin-top: 14px; display: flex; align-items: center; gap: 10px; padding: 10px 10px 10px 14px; border-radius: 12px; border: 1px dashed var(--accent); background: var(--accent-soft); }
.key-reveal code { flex: 1; overflow-x: auto; white-space: nowrap; font-size: 13.5px; }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; font-size: 12px; font-weight: 600; color: var(--text-3); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table tr:last-child td { border-bottom: 0; }
.table code { font-size: 12.5px; color: var(--text-2); }
.table-scroll { overflow-x: auto; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); }
.pill.ok { background: rgba(52,211,153,.12); color: var(--ok); }
.pill.off { background: rgba(248,113,113,.12); color: var(--danger); }
.td-actions { display: flex; gap: 6px; justify-content: flex-end; }

.tabs { display: flex; gap: 2px; background: var(--bg); padding: 3px; border-radius: 10px; border: 1px solid var(--border); }
.tabs button { padding: 5px 12px; border-radius: 7px; font-size: 13px; color: var(--text-2); }
.tabs button.active { background: var(--surface-2); color: var(--text); }
.code-block { position: relative; background: var(--code-bg); border-radius: 12px; border: 1px solid var(--border); }
.code-block pre { margin: 0; padding: 16px; overflow-x: auto; font-size: 13px; line-height: 1.6; color: #e6e6ef; }
.code-block .copy-code { position: absolute; top: 10px; right: 10px; background: #1b1b28; border-color: #2a2a3c; color: #b8b8cc; }
.endpoints { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; }
.endpoints li { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; font-size: 14px; }
.method { font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 6px; font-family: "JetBrains Mono", monospace; }
.method.get { background: rgba(52,211,153,.12); color: var(--ok); }
.method.post { background: var(--accent-soft); color: var(--accent); }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 8px auto 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; }
.stat small { display: block; font-size: 12px; color: var(--text-3); font-weight: 600; }
.stat b { font-size: 22px; letter-spacing: -0.02em; }
.stat .sub { font-size: 12px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px;
  font-size: 14px; font-weight: 500; opacity: 0; pointer-events: none; transition: all .2s; z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.scrim { display: none; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .only-mobile { display: grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 50; transform: translateX(-100%);
    transition: transform .22s ease; box-shadow: var(--shadow);
  }
  .app.sidebar-open .sidebar { transform: none; }
  .app.sidebar-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 40; }
  .thread, .empty { padding-left: 16px; padding-right: 16px; }
  .composer-wrap { padding: 0 12px 10px; }
  .page .page-body { padding: 8px 16px 40px; }
  .hero h1 { font-size: 28px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .model-btn #modelName { max-width: 52vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .model-menu { min-width: 280px; }
  .new-chat kbd { display: none; }
}
@media (max-width: 520px) {
  .suggestions { grid-template-columns: 1fr; }
  .suggestions button:nth-child(n+3) { display: none; }
  .empty h1 { font-size: 24px; }
  .settings-pop { right: 8px; left: 8px; width: auto; }
}
