:root {
  color-scheme: light;
  --bg: #f5f5f3;
  --surface: #fcfcfb;
  --surface-2: #f0f0ed;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7973;
  --accent: #d42c2c;
  --accent-ink: #b02020;
  --accent-soft: #fbe8e8;
  --on-accent: #ffffff;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --critical-soft: #fbe9e9;
  --user-bubble: #ecebe7;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .06);
  --radius: 10px;
  --sidebar-w: 272px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #33332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --accent: #d63a3a;
    --accent-ink: #ff8f8f;
    --accent-soft: #3d1d1f;
    --critical-soft: #3a1d1d;
    --user-bubble: #2a2a27;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #33332f;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --text-3: #8f8e86;
  --accent: #d63a3a;
  --accent-ink: #ff8f8f;
  --accent-soft: #3d1d1f;
  --critical-soft: #3a1d1d;
  --user-bubble: #2a2a27;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-ink); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.boot { padding: 40px; color: var(--text-3); }
.muted { color: var(--text-3); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; text-align: right; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---- Controls ---- */
button, .btn {
  font: inherit;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
  line-height: 1.3;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--critical); }
.btn-danger:hover { background: var(--critical-soft); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { padding: 4px 10px; font-size: .85rem; }
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
}
textarea { resize: vertical; }
/* Neutral focus ring: with a red brand, a red outline would read as a validation error. */
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--text-2);
  outline-offset: 1px;
}
input[type="checkbox"] { width: auto; accent-color: var(--accent); }
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-size: .85rem; color: var(--text-2); margin-bottom: 4px; font-weight: 500; }
.hint { font-size: .8rem; color: var(--text-3); margin-top: 4px; }
.check { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 16px; }
.spacer { flex: 1; }
.error-text { color: var(--critical); font-size: .9rem; margin: 8px 0; }

/* ---- Login ---- */
.login-wrap { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.login-card .logo { max-width: 180px; max-height: 64px; object-fit: contain; margin-bottom: 14px; display: block; }
.login-foot { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); font-size: .8rem; color: var(--text-3); display: flex; flex-direction: column; gap: 4px; }
.login-foot a { color: var(--text-3); }
.logo-preview { width: 72px; height: 72px; object-fit: contain; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); padding: 6px; }
.color-row { display: flex; gap: 8px; align-items: center; }
.color-row input[type="color"] { width: 46px; height: 38px; padding: 2px; flex-shrink: 0; cursor: pointer; }
.color-row input[type="text"] { max-width: 130px; font-family: var(--mono); }
.brand-preview { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 12px; border: 1px dashed var(--border); border-radius: 10px; margin-bottom: 14px; }
.brand-preview .swatch-btn { padding: 7px 14px; border-radius: 8px; color: #fff; font-size: .9rem; }
.brand-preview .swatch-soft { padding: 6px 12px; border-radius: 8px; font-size: .9rem; font-weight: 550; }
.seat-pill { font-size: .85rem; color: var(--text-2); }

/* ---- Shell ---- */
.shell { display: flex; height: 100vh; height: 100dvh; }
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head { padding: 14px 14px 8px; display: flex; align-items: center; gap: 10px; }
.brand-logo { width: 32px; height: 32px; object-fit: contain; border-radius: 6px; flex-shrink: 0; }
.brand-logo-sm { width: 24px; height: 24px; }
.sidebar-head .org { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-section { padding: 6px 10px; }
.sidebar-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); padding: 10px 8px 4px; }
.nav a, .conv-list a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: .92rem;
}
.nav a:hover, .conv-list a:hover { background: var(--surface-2); }
.nav a.active, .conv-list a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 550; }
.conv-list { flex: 1; overflow-y: auto; padding: 0 10px 8px; min-height: 0; }
.conv-list a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-foot { border-top: 1px solid var(--border); padding: 10px 14px; display: flex; align-items: center; gap: 8px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-ink); display: grid; place-items: center; font-weight: 650; font-size: .85rem; flex-shrink: 0; }
.sidebar-foot .who { min-width: 0; flex: 1; }
.sidebar-foot .who div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.topbar { display: none; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--surface); }
.page { flex: 1; overflow-y: auto; padding: 28px 32px 48px; }
.page-inner { max-width: 1120px; margin: 0 auto; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { margin: 0; }
.backdrop { display: none; }

@media (max-width: 860px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow); }
  .shell.nav-open .sidebar { transform: none; }
  .shell.nav-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
  .topbar { display: flex; }
  .page { padding: 20px 16px 40px; }
}

/* ---- Cards, tiles, tables ---- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile .label { font-size: .82rem; color: var(--text-2); }
.tile .value { font-size: 1.55rem; font-weight: 600; margin-top: 2px; }
.tile .sub { font-size: .8rem; color: var(--text-3); }
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { padding: 9px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: .78rem; font-weight: 600; color: var(--text-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.badge { display: inline-block; font-size: .75rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); white-space: nowrap; }
.badge-accent { background: var(--accent-soft); color: var(--accent-ink); border-color: transparent; }
.badge-critical { background: var(--critical); color: #fff; border-color: transparent; }
.empty { text-align: center; padding: 28px 10px; color: var(--text-3); }

/* Quota meter: fill carries severity (good -> warning -> critical); the label always states the numbers. */
.meter { margin: 6px 0 14px; }
.meter-top { display: flex; justify-content: space-between; gap: 8px; font-size: .88rem; margin-bottom: 5px; }
.meter-track { height: 8px; border-radius: 4px; background: color-mix(in srgb, var(--good) 16%, var(--surface)); overflow: hidden; }
.meter-fill { height: 100%; border-radius: 4px; background: var(--good); }
.meter.warn .meter-track { background: color-mix(in srgb, var(--warning) 22%, var(--surface)); }
.meter.warn .meter-fill { background: var(--warning); }
.meter.over .meter-track { background: color-mix(in srgb, var(--critical) 18%, var(--surface)); }
.meter.over .meter-fill { background: var(--critical); }
.mini-meter { width: 90px; height: 6px; border-radius: 3px; background: color-mix(in srgb, var(--good) 16%, var(--surface)); overflow: hidden; display: inline-block; vertical-align: middle; margin-right: 6px; }
.mini-meter > i { display: block; height: 100%; background: var(--good); border-radius: 3px; }
.mini-meter.warn > i { background: var(--warning); }
.mini-meter.over > i { background: var(--critical); }

/* Bar chart */
.chart { position: relative; }
.chart svg { display: block; overflow: visible; }
.chart .grid line { stroke: var(--border); stroke-width: 1; }
.chart .axis text { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .bar { fill: var(--accent); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .bar.hover { fill: var(--accent-ink); }
.chart-tip { position: absolute; pointer-events: none; background: var(--text); color: var(--surface); font-size: .8rem; padding: 6px 9px; border-radius: 6px; white-space: nowrap; transform: translate(-50%, calc(-100% - 8px)); box-shadow: var(--shadow); }
.chart-tip[hidden] { display: none; }

/* ---- Chat ---- */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--surface); }
.chat-head .title { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.messages { flex: 1; overflow-y: auto; padding: 24px 20px; }
.messages-inner { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }
.msg { display: flex; flex-direction: column; gap: 4px; }
.msg-user { align-items: flex-end; }
.msg-user .bubble { background: var(--user-bubble); border-radius: 14px 14px 4px 14px; padding: 10px 14px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-assistant .bubble { padding: 2px 2px; max-width: 100%; overflow-wrap: anywhere; }
.msg-meta { font-size: .75rem; color: var(--text-3); display: flex; gap: 8px; flex-wrap: wrap; }
.msg-error { background: var(--critical-soft); color: var(--critical); border-radius: 10px; padding: 10px 14px; }
.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 12px 20px 16px; }
.composer-inner { max-width: 820px; margin: 0 auto; }
.composer-box { border: 1px solid var(--border); border-radius: 14px; background: var(--bg); padding: 8px; }
.composer-box:focus-within { border-color: var(--text-3); }
.composer textarea { border: none; background: transparent; resize: none; max-height: 240px; min-height: 44px; padding: 6px 8px; }
.composer textarea:focus-visible { outline: none; }
.composer-bar { display: flex; gap: 8px; align-items: center; padding: 2px 4px 0; }
.composer-bar select { width: auto; max-width: 60%; padding: 5px 8px; font-size: .85rem; }
.composer-note { font-size: .75rem; color: var(--text-3); margin-top: 6px; text-align: center; }
.welcome { max-width: 640px; margin: 10vh auto 0; text-align: center; }
.welcome h1 { font-size: 1.7rem; }
.readonly-banner { background: var(--accent-soft); color: var(--accent-ink); padding: 8px 20px; font-size: .88rem; }

/* Markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md blockquote, .md table { margin: 0 0 .8em; }
.md pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; font-size: .86rem; }
.md code { font-family: var(--mono); font-size: .88em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.md pre code { background: none; padding: 0; }
.md blockquote { border-left: 3px solid var(--border); padding-left: 12px; color: var(--text-2); }
.md table { display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); }
.md img { max-width: 100%; }

/* ---- Modal & toasts ---- */
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--text); padding: 0; width: min(560px, calc(100vw - 32px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
.modal-body { padding: 22px 22px 8px; max-height: 70vh; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 22px 18px; }
.toasts { position: fixed; bottom: 16px; right: 16px; left: 16px; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; z-index: 50; pointer-events: none; }
.toast { background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: .9rem; max-width: 420px; pointer-events: auto; }
.toast.error { background: var(--critical); color: #fff; }
.model-checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 2px 12px; }
.key-status { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.transcript-owner { display: flex; align-items: center; gap: 10px; }
pre.audit-details { margin: 0; font-family: var(--mono); font-size: .78rem; white-space: pre-wrap; word-break: break-word; color: var(--text-2); max-width: 420px; }
