:root {
  --bg: #ffffff;
  --bg-subtle: #f7f7f8;
  --bg-elevated: #ffffff;
  --sidebar-bg: #f5f5f6;
  --text: #17181a;
  --text-muted: #6b6f76;
  --text-faint: #9aa0a6;
  --border: #e3e4e8;
  --border-strong: #d0d2d7;
  --accent: #1f6feb;
  --accent-soft: #eaf1fe;
  --danger: #d1383d;
  --danger-soft: #fdeded;
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-w: 280px;
  --shadow: 0 1px 2px rgba(16,18,22,.06), 0 4px 12px rgba(16,18,22,.05);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --bg-subtle: #1c1e22;
    --bg-elevated: #22252a;
    --sidebar-bg: #131417;
    --text: #e9eaec;
    --text-muted: #a0a4ab;
    --text-faint: #71767d;
    --border: #2b2e34;
    --border-strong: #3a3e45;
    --accent: #4c8dff;
    --accent-soft: #1b2942;
    --danger: #ff6b6b;
    --danger-soft: #3a1f21;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 12px rgba(0,0,0,.25);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.55;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; font-size: inherit; }

/* ── App shell ───────────────────────────────────────────────────────────── */
.app { display: flex; height: 100dvh; overflow: hidden; }

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head { padding: 12px 12px 8px; }
.brand {
  display: flex; align-items: center; gap: 9px;
  padding: 6px 8px; margin-bottom: 8px;
  color: var(--text); font-weight: 600; font-size: 15.5px;
  border-radius: var(--radius-sm);
}
.brand:hover { text-decoration: none; background: var(--bg); }
.brand img { width: 24px; height: 24px; border-radius: 6px; }
.brand-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  width: 100%; padding: 10px 12px;
  background: var(--bg-elevated); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; font-weight: 500; text-align: left;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--bg-subtle); border-color: var(--border-strong); }
.btn-primary { background: var(--accent); color: #fff; border-color: transparent; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-icon { width: 34px; height: 34px; padding: 0; justify-content: center; }
.btn-sm { padding: 6px 10px; font-size: 13px; width: auto; }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }

.sidebar-scroll { flex: 1; overflow-y: auto; padding: 4px 8px 12px; min-height: 0; }
.conv-group { margin-top: 14px; }
.conv-group-title {
  font-size: 11.5px; font-weight: 600; color: var(--text-faint);
  text-transform: uppercase; letter-spacing: .05em;
  padding: 6px 8px 4px;
}
.conv-item {
  display: flex; align-items: center; gap: 4px;
  padding: 7px 8px; border-radius: var(--radius-sm);
  color: var(--text); cursor: pointer; position: relative;
}
.conv-item:hover { background: var(--bg); text-decoration: none; }
.conv-item.active { background: var(--bg-elevated); box-shadow: var(--shadow); }
.conv-title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 14px;
}
.conv-actions { display: none; gap: 2px; }
.conv-item:hover .conv-actions, .conv-item.active .conv-actions { display: flex; }
.icon-btn {
  border: 0; background: transparent; color: var(--text-muted);
  cursor: pointer; padding: 4px; border-radius: 6px; line-height: 0;
}
.icon-btn:hover { background: var(--bg-subtle); color: var(--text); }
.empty-note { padding: 16px 8px; color: var(--text-faint); font-size: 13px; }

.sidebar-foot { border-top: 1px solid var(--border); padding: 8px; }
.foot-row { display: flex; align-items: center; gap: 8px; }
.foot-user {
  flex: 1; padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--text); display: flex; align-items: center; gap: 8px; font-size: 14px;
  min-width: 0;
}
.foot-user:hover { background: var(--bg); text-decoration: none; }
.foot-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.usage-line { padding: 2px 10px 6px; font-size: 12px; color: var(--text-faint); }
.usage-bar { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; margin-top: 5px; }
.usage-bar > span { display: block; height: 100%; background: var(--accent); }

/* ── Main ────────────────────────────────────────────────────────────────── */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  min-height: 54px; flex: 0 0 auto;
}
.topbar .conv-label {
  font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-spacer { flex: 1; }
.menu-toggle { display: none; }

.search-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg-elevated);
  color: var(--text-muted); cursor: pointer; font-size: 13px; white-space: nowrap;
}
.search-toggle:hover { border-color: var(--border-strong); color: var(--text); }
.search-toggle.on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

.scroll-area { flex: 1; overflow-y: auto; min-height: 0; scroll-behavior: smooth; }
.thread { max-width: 780px; margin: 0 auto; padding: 24px 20px 8px; }

/* ── Welcome ─────────────────────────────────────────────────────────────── */
.welcome { max-width: 780px; margin: 0 auto; padding: 8vh 20px 20px; text-align: center; }
.welcome h1 { font-size: 30px; margin: 0 0 8px; font-weight: 650; letter-spacing: -.01em; }
.welcome .tagline { color: var(--text-muted); font-size: 16px; margin: 0 0 28px; }
.welcome-suggestions {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 10px; margin-top: 30px; text-align: left;
}
.suggestion {
  padding: 13px 15px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg-elevated);
  cursor: pointer; color: var(--text); font-size: 14px;
}
.suggestion:hover { border-color: var(--border-strong); box-shadow: var(--shadow); text-decoration: none; }

/* ── Messages ────────────────────────────────────────────────────────────── */
.msg { display: flex; gap: 14px; padding: 16px 0; }
.msg + .msg { border-top: 1px solid var(--border); }
.avatar {
  flex: 0 0 28px; width: 28px; height: 28px; border-radius: 7px;
  display: grid; place-items: center; font-size: 12px; font-weight: 700;
  margin-top: 2px;
}
.msg-user .avatar { background: var(--bg-subtle); color: var(--text-muted); border: 1px solid var(--border); }
.msg-assistant .avatar { background: var(--accent); color: #fff; }
.msg-body { flex: 1; min-width: 0; }
.msg-role { font-weight: 600; font-size: 13px; margin-bottom: 4px; color: var(--text-muted); }
.msg-content { overflow-wrap: break-word; }
.msg-user .msg-content { white-space: pre-wrap; }
.msg-actions { display: flex; gap: 4px; margin-top: 8px; opacity: 0; transition: opacity .12s; }
.msg:hover .msg-actions { opacity: 1; }
.msg-err {
  margin-top: 8px; padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--danger-soft); color: var(--danger); font-size: 14px;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
/* Neutral disclosure when a requested web search produced no usable sources.
   Uses existing tokens only; intentionally not .msg-err (red implies failure). */
.search-note {
  margin-top: 10px; padding: 8px 11px; border-radius: var(--radius-sm);
  background: var(--bg-elevated); border: 1px solid var(--border);
  color: var(--text-muted); font-size: 13px; line-height: 1.45;
}
.msg-err button {
  background: var(--bg-elevated); border: 1px solid currentColor;
  color: inherit; border-radius: 6px; padding: 4px 10px; cursor: pointer; font-size: 13px;
}
.cursor::after {
  content: ""; display: inline-block; width: 7px; height: 15px;
  background: var(--accent); margin-left: 2px; vertical-align: -2px;
  animation: blink 1.05s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* Markdown */
.msg-content > :first-child { margin-top: 0; }
.msg-content > :last-child { margin-bottom: 0; }
.msg-content h1, .msg-content h2, .msg-content h3 { margin: 1.4em 0 .5em; line-height: 1.3; }
.msg-content h1 { font-size: 1.4em; } .msg-content h2 { font-size: 1.22em; } .msg-content h3 { font-size: 1.08em; }
.msg-content p { margin: .7em 0; }
.msg-content ul, .msg-content ol { margin: .6em 0; padding-left: 1.5em; }
.msg-content li { margin: .25em 0; }
.msg-content blockquote {
  margin: .8em 0; padding: 2px 0 2px 14px;
  border-left: 3px solid var(--border-strong); color: var(--text-muted);
}
.msg-content table { border-collapse: collapse; margin: .9em 0; display: block; overflow-x: auto; max-width: 100%; }
.msg-content th, .msg-content td { border: 1px solid var(--border); padding: 7px 11px; text-align: left; }
.msg-content th { background: var(--bg-subtle); font-weight: 600; }
.msg-content code {
  font-family: var(--mono); font-size: .88em;
  background: var(--bg-subtle); padding: .15em .38em; border-radius: 5px;
  border: 1px solid var(--border);
}
.msg-content pre.code-block {
  position: relative; margin: .9em 0; padding: 13px 15px;
  background: var(--bg-subtle); border: 1px solid var(--border);
  border-radius: var(--radius); overflow-x: auto;
}
.msg-content pre.code-block code {
  background: none; border: 0; padding: 0; font-size: 13px; line-height: 1.5;
}
.copy-btn {
  position: absolute; top: 8px; right: 8px;
  padding: 4px 9px; font-size: 12px; border-radius: 6px;
  background: var(--bg-elevated); color: var(--text-muted);
  border: 1px solid var(--border); cursor: pointer; opacity: 0; transition: opacity .12s;
}
pre.code-block:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text); border-color: var(--border-strong); }

/* Pygments tokens (works in both themes) */
.hl .k, .hl .kn, .hl .kd, .hl .kc { color: #a626a4; }
.hl .s, .hl .s1, .hl .s2, .hl .sb, .hl .sd { color: #50a14f; }
.hl .c, .hl .c1, .hl .cm { color: #a0a1a7; font-style: italic; }
.hl .m, .hl .mi, .hl .mf, .hl .mh { color: #986801; }
.hl .nf, .hl .fm { color: #4078f2; }
.hl .nc, .hl .nn { color: #c18401; }
.hl .nb { color: #0184bc; }
.hl .o, .hl .ow { color: #383a42; }
.hl .p { color: #383a42; }
@media (prefers-color-scheme: dark) {
  .hl .k, .hl .kn, .hl .kd, .hl .kc { color: #c678dd; }
  .hl .s, .hl .s1, .hl .s2, .hl .sb, .hl .sd { color: #98c379; }
  .hl .c, .hl .c1, .hl .cm { color: #7f848e; }
  .hl .m, .hl .mi, .hl .mf, .hl .mh { color: #d19a66; }
  .hl .nf, .hl .fm { color: #61afef; }
  .hl .nc, .hl .nn { color: #e5c07b; }
  .hl .nb { color: #56b6c2; }
  .hl .o, .hl .ow, .hl .p { color: #abb2bf; }
}

/* Sources */
.sources { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); }
.sources-label { font-size: 12px; font-weight: 600; color: var(--text-faint); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 8px; }
.source-item { display: flex; gap: 9px; padding: 7px 0; align-items: baseline; }
.source-num {
  flex: 0 0 20px; height: 20px; border-radius: 5px; background: var(--bg-subtle);
  border: 1px solid var(--border); color: var(--text-muted);
  font-size: 11px; font-weight: 600; display: grid; place-items: center; margin-top: 1px;
}
.source-text { min-width: 0; }
.source-title { font-size: 13.5px; }
.source-domain { font-size: 12px; color: var(--text-faint); display: block; }

/* ── Composer ────────────────────────────────────────────────────────────── */
.composer-wrap {
  flex: 0 0 auto; padding: 10px 20px max(14px, env(safe-area-inset-bottom));
  background: var(--bg); border-top: 1px solid transparent;
}
.composer-inner { max-width: 780px; margin: 0 auto; }
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--bg-elevated); padding: 8px 8px 8px 14px;
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent;
  color: var(--text); font-family: inherit; font-size: 15px; line-height: 1.5;
  max-height: 220px; min-height: 24px; padding: 4px 0;
}
.composer textarea::placeholder { color: var(--text-faint); }
.send-btn {
  flex: 0 0 34px; width: 34px; height: 34px; border-radius: 9px; border: 0;
  background: var(--accent); color: #fff; cursor: pointer;
  display: grid; place-items: center; transition: opacity .12s;
}
.send-btn:disabled { opacity: .35; cursor: not-allowed; }
.composer-foot {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-top: 7px; font-size: 12px; color: var(--text-faint);
}
.mode-group { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.mode-btn {
  padding: 3px 11px; font-size: 12px; background: transparent; color: var(--text-muted);
  border: 0; cursor: pointer;
}
.mode-btn.active { background: var(--accent); color: #fff; }
.status-line { display: flex; align-items: center; gap: 6px; }
.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); }
.dot.busy { background: var(--accent); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* ── Search menu ─────────────────────────────────────────────────────────── */
.menu-pop {
  position: absolute; top: 100%; right: 0; margin-top: 6px;
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 5px; min-width: 190px; z-index: 40;
}
.menu-pop button {
  display: block; width: 100%; text-align: left; padding: 8px 10px;
  background: transparent; border: 0; border-radius: var(--radius-sm);
  color: var(--text); cursor: pointer; font-size: 14px;
}
.menu-pop button:hover { background: var(--bg-subtle); }
.menu-pop button.active { color: var(--accent); font-weight: 600; }
.menu-pop .hint { font-size: 11.5px; color: var(--text-faint); padding: 3px 10px 6px; }
.search-wrap { position: relative; }

/* ── Banner ──────────────────────────────────────────────────────────────── */
.banner {
  margin: 12px auto 0; max-width: 780px; padding: 11px 14px;
  border-radius: var(--radius-sm); font-size: 14px;
  display: flex; gap: 10px; align-items: center;
}
.banner-warn { background: var(--accent-soft); color: var(--accent); }
.banner-err { background: var(--danger-soft); color: var(--danger); }
.banner a { color: inherit; text-decoration: underline; }

/* ── Guest CTA ───────────────────────────────────────────────────────────── */
.guest-cta {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin: 0 auto 8px; max-width: 780px; padding: 9px 13px;
  background: var(--accent-soft); color: var(--accent);
  border-radius: var(--radius-sm); font-size: 13.5px;
}
.guest-cta a { color: inherit; font-weight: 600; }
.scrim { display: none; }

/* ── Forms / auth pages ──────────────────────────────────────────────────── */
.auth-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--bg-elevated);
  border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0 0 4px; font-size: 21px; }
.auth-card .sub { color: var(--text-muted); margin: 0 0 20px; font-size: 14px; }
.field { margin-bottom: 13px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.field input {
  width: 100%; padding: 10px 12px; font-size: 15px; font-family: inherit;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--bg); color: var(--text); outline: 0;
}
.field input:focus { border-color: var(--accent); }
.auth-alt { margin-top: 16px; font-size: 14px; color: var(--text-muted); text-align: center; }
.err-box {
  background: var(--danger-soft); color: var(--danger); padding: 9px 12px;
  border-radius: var(--radius-sm); font-size: 13.5px; margin-bottom: 14px;
}

/* ── Admin ───────────────────────────────────────────────────────────────── */
.admin-wrap { max-width: 1080px; margin: 0 auto; padding: 24px 20px 60px; }
.admin-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin: 18px 0 26px; }
.stat { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius); padding: 15px; }
.stat .n { font-size: 25px; font-weight: 650; letter-spacing: -.02em; }
.stat .l { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.panel { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius); padding: 17px; margin-bottom: 18px; }
.panel h2 { margin: 0 0 13px; font-size: 16px; }
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data th, table.data td { text-align: left; padding: 7px 9px; border-bottom: 1px solid var(--border); }
table.data th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-faint); font-weight: 600; }
table.data tr:last-child td { border-bottom: 0; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; }
.pill-ok { background: var(--accent-soft); color: var(--accent); }
.pill-bad { background: var(--danger-soft); color: var(--danger); }

/* ── Mobile ──────────────────────────────────────────────────────────────── */
@media (max-width: 820px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 60;
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: 0 0 30px rgba(0,0,0,.2); height: 100dvh;
  }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open .scrim {
    display: block; position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45);
  }
  .menu-toggle { display: grid; }
  .thread { padding: 14px 14px 4px; }
  .composer-wrap { padding: 8px 12px max(10px, env(safe-area-inset-bottom)); }
  .welcome { padding: 5vh 16px 16px; }
  .welcome h1 { font-size: 25px; }
  .welcome-suggestions { grid-template-columns: 1fr; margin-top: 20px; }
  .msg { gap: 10px; }
  .topbar { padding: 8px 12px; }
  .admin-wrap { padding: 16px 12px 50px; }
  table.data { font-size: 12.5px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
