:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --error: #d03b3b;
  --code: #f0efec;
  /* Categorical palette (fixed order, validated for color-vision deficiency). */
  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --series-5: #e87ba4; --series-6: #008300; --series-7: #6250d6; --series-8: #e34948;
  --other: #898781;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #f0efec; --ink-2: #c3c2b7;
    --muted: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --code: #232321;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #f0efec; --ink-2: #c3c2b7;
  --muted: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --code: #232321;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--page); color: var(--ink); font-size: 15px; line-height: 1.5; }
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; border: 1px solid transparent; border-radius: 8px; padding: 8px 14px; background: var(--accent); color: var(--accent-ink); }
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--ink-2); border-color: var(--border); }
button.ghost:hover { background: var(--surface-2); color: var(--ink); }
button.icon { padding: 6px 10px; }
input, textarea { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; width: 100%; }
textarea { resize: vertical; min-height: 48px; max-height: 40vh; }
a { color: var(--accent); }
.muted { color: var(--ink-2); }
.small { font-size: 12px; }
.error { color: var(--error); }
.spacer { flex: 1; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; width: min(380px, 100%); display: grid; gap: 10px; }
.login-card h1 { margin: 0; font-size: 22px; }

/* layout */
.app { display: grid; grid-template-columns: 260px 1fr; height: 100%; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head, .sidebar-foot { display: flex; gap: 8px; align-items: center; justify-content: space-between; padding: 12px; }
.sidebar-foot { border-top: 1px solid var(--border); flex-wrap: wrap; }
.history { flex: 1; overflow: auto; padding: 0 8px; }
.history a { display: block; padding: 8px 10px; border-radius: 8px; color: var(--ink-2); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.history a:hover { background: var(--surface-2); color: var(--ink); }
.history a.active { background: var(--surface-2); color: var(--ink); font-weight: 600; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.convo-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#toggle-sidebar { display: none; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); }
.thread { flex: 1; overflow: auto; padding: 20px 16px 8px; }
.thread > * { max-width: 920px; margin-left: auto; margin-right: auto; }
.empty { flex: 1; overflow: auto; padding: 32px 16px; }
.empty > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.examples { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin-top: 16px; }
.examples button { text-align: left; background: var(--surface); color: var(--ink); border-color: var(--border); }
.examples button:hover { background: var(--surface-2); }
.setup { border: 1px solid var(--border); border-left: 4px solid #fab219; background: var(--surface); padding: 12px 14px; border-radius: 8px; margin-top: 12px; }
.composer { border-top: 1px solid var(--border); background: var(--surface); padding: 12px 16px; }
.composer > * { max-width: 920px; margin-left: auto; margin-right: auto; }
.composer-row { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.toggle { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; }
.toggle input { width: auto; }

/* turns */
.turn { margin-bottom: 28px; }
.question { background: var(--surface-2); border-radius: 12px; padding: 10px 14px; margin-left: auto; width: fit-content; max-width: 85%; white-space: pre-wrap; }
.question .tag { font-size: 11px; font-weight: 700; letter-spacing: .04em; color: var(--ink-2); margin-right: 6px; }
.progress { margin: 10px 0; font-size: 14px; color: var(--ink-2); }
.progress .live::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 8px; animation: pulse 1s infinite alternate; }
@keyframes pulse { from { opacity: .3; } to { opacity: 1; } }
details.steps { margin: 8px 0; font-size: 14px; }
details.steps summary { cursor: pointer; color: var(--ink-2); }
.step { border-left: 2px solid var(--border); padding: 4px 0 4px 12px; margin: 8px 0; }
.step.failed { border-left-color: var(--error); }
.step pre { background: var(--code); border-radius: 8px; padding: 8px 10px; overflow: auto; font-size: 12px; margin: 6px 0; }
.answer { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px 20px; overflow-wrap: anywhere; }
.answer h1 { font-size: 22px; } .answer h2 { font-size: 18px; } .answer h3 { font-size: 16px; }
.answer table:not(.data) { border-collapse: collapse; }
.answer table:not(.data) td, .answer table:not(.data) th { border: 1px solid var(--grid); padding: 4px 8px; }
.answer code { background: var(--code); padding: 1px 4px; border-radius: 4px; }
.turn-actions { display: flex; gap: 8px; margin-top: 8px; }
.turn-actions button { font-size: 13px; padding: 4px 10px; }
.turn-error { color: var(--error); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px; }

/* exhibits */
.exhibit { margin: 16px 0; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); padding: 12px 14px; }
.exhibit-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.exhibit-title { font-weight: 600; font-size: 14px; }
.exhibit-head button { font-size: 12px; padding: 3px 9px; }
.chart-box { position: relative; height: 320px; }
.table-wrap { overflow: auto; max-height: 420px; }
table.data { border-collapse: collapse; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
table.data th { position: sticky; top: 0; background: var(--surface); text-align: left; border-bottom: 1px solid var(--axis); padding: 6px 8px; cursor: pointer; white-space: nowrap; color: var(--ink-2); font-weight: 600; }
table.data td { border-bottom: 1px solid var(--grid); padding: 5px 8px; white-space: nowrap; }
table.data td.num { text-align: right; }
.table-note { font-size: 12px; color: var(--ink-2); margin-top: 6px; }

dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--ink); width: min(760px, calc(100% - 32px)); max-height: 80vh; padding: 16px 20px; }
dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.catalog-item { border-top: 1px solid var(--border); padding: 10px 0; }
.catalog-item ul { margin: 6px 0 0; padding-left: 18px; font-size: 13px; color: var(--ink-2); }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 30% 0 0; z-index: 10; transform: translateX(-100%); transition: transform .15s; }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .3); }
  #toggle-sidebar { display: inline-block; }
  .composer-row .small { display: none; }
}

@media print {
  .sidebar, .topbar, .composer, .turn-actions, details.steps, .exhibit-head button, .question { display: none !important; }
  .app { display: block; }
  .thread { overflow: visible; padding: 0; }
  .answer { border: 0; padding: 0; }
  .exhibit { break-inside: avoid; }
  .turn { break-after: page; }
  .table-wrap { max-height: none; overflow: visible; }
}

.disclaimer { margin: 16px 0 0; padding-top: 10px; border-top: 1px solid var(--grid); font-size: 12px; color: var(--ink-2); }
