:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --line-2: #c3c2b7;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0fc;
  --bad: #d03b3b;
  --ok: #006300;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(11, 11, 11, .06), 0 4px 16px rgba(11, 11, 11, .04);
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #ffffff; --ink-2: #c3c2b7;
    --muted: #898781; --line: #2c2c2a; --line-2: #383835; --accent: #3987e5; --accent-soft: #16263a;
    --bad: #e66767; --ok: #0ca30c; --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #ffffff; --ink-2: #c3c2b7;
  --muted: #898781; --line: #2c2c2a; --line-2: #383835; --accent: #3987e5; --accent-soft: #16263a;
  --bad: #e66767; --ok: #0ca30c; --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--ink); font: 15px/1.55 var(--font); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; cursor: pointer; }

/* ---- top bar ---- */
.topbar { position: sticky; top: 0; z-index: 1000; display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); font-weight: 650; letter-spacing: -.01em; }
.brand:hover { text-decoration: none; }
.logo { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 7px; background: var(--accent);
  color: #fff; font-weight: 800; font-size: 15px; }
.topbar nav { display: flex; gap: 18px; font-size: 14px; flex-wrap: wrap; }
.topbar nav a { color: var(--ink-2); }

/* ---- chat layout ---- */
.layout { display: grid; grid-template-columns: 260px minmax(0, 1fr); min-height: calc(100vh - 49px); }
.sidebar { border-right: 1px solid var(--line); padding: 16px 12px; background: var(--surface);
  position: sticky; top: 49px; height: calc(100vh - 49px); overflow-y: auto; }
.new-chat { width: 100%; padding: 9px 12px; border-radius: 9px; border: 1px solid var(--line-2); background: var(--surface);
  color: var(--ink); font-weight: 550; text-align: left; }
.new-chat:hover { background: var(--surface-2); }
.history-label { margin: 18px 6px 6px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.history { list-style: none; margin: 0; padding: 0; }
.history li { display: flex; align-items: center; border-radius: 8px; }
.history li.active, .history li:hover { background: var(--surface-2); }
.history a { flex: 1; min-width: 0; padding: 7px 8px; color: var(--ink-2); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history .share { visibility: hidden; border: 0; background: none; color: var(--muted); padding: 4px 8px; }
.history li:hover .share { visibility: visible; }
.history .empty { color: var(--muted); font-size: 13px; padding: 6px 8px; }
.sidebar-note { font-size: 12px; color: var(--muted); margin: 18px 6px; }

.main { display: flex; flex-direction: column; min-width: 0; }
.thread { flex: 1; width: 100%; max-width: 900px; margin: 0 auto; padding: 24px 20px 160px; }

/* ---- welcome ---- */
.welcome { padding: 6vh 0 0; }
.welcome h1 { font-size: clamp(28px, 4vw, 40px); line-height: 1.12; letter-spacing: -.02em; margin: 0 0 12px; }
.lede { font-size: 17px; color: var(--ink-2); max-width: 680px; margin: 0 0 26px; }
.starters { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.starter { text-align: left; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); font-size: 14px; line-height: 1.4; box-shadow: var(--shadow); }
.starter:hover { border-color: var(--accent); }
.more { margin-top: 18px; font-size: 14px; }

/* ---- messages ---- */
.msg { margin: 0 0 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { max-width: 80%; background: var(--accent-soft); color: var(--ink); padding: 10px 14px; border-radius: 14px 14px 4px 14px; white-space: pre-wrap; }
.msg.assistant .text { overflow-wrap: anywhere; }
.msg.assistant .text p { margin: 0 0 10px; }
.msg.assistant .text table { border-collapse: collapse; margin: 10px 0 14px; font-size: 13.5px; display: block; overflow-x: auto; max-width: 100%; }
.msg.assistant .text th, .msg.assistant .text td { border-bottom: 1px solid var(--line); padding: 5px 10px; text-align: left; font-variant-numeric: tabular-nums; }
.msg.assistant .text th { color: var(--ink-2); font-weight: 600; }
.msg.assistant .text code { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.msg.assistant .text h1, .msg.assistant .text h2, .msg.assistant .text h3 { font-size: 16px; margin: 16px 0 6px; }
.meta { font-size: 12px; color: var(--muted); margin-top: 6px; }
.working .meta::before { content: "Working"; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* tool steps */
.steps { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.step { border: 1px solid var(--line); border-radius: 9px; background: var(--surface); font-size: 13px; }
.step summary { list-style: none; cursor: pointer; padding: 6px 10px; color: var(--ink-2); display: flex; gap: 6px; align-items: baseline; }
.step summary::-webkit-details-marker { display: none; }
.step .i { color: var(--accent); width: 14px; text-align: center; }
.step .v { font-weight: 600; color: var(--ink-2); }
.step .l { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.step .t { color: var(--muted); }
.step.pending .i { animation: pulse 1s infinite; }
.step.failed { border-color: color-mix(in srgb, var(--bad) 45%, var(--line)); }
.step.failed .i { color: var(--bad); }
.sql { margin: 0; padding: 10px 12px; border-top: 1px solid var(--line); font: 12px/1.5 var(--mono); white-space: pre-wrap; color: var(--ink-2); background: var(--surface-2); overflow-x: auto; }
.res-note { padding: 6px 12px; color: var(--muted); font-size: 12px; }
.mini-table { overflow-x: auto; border-top: 1px solid var(--line); }
.mini-table table { border-collapse: collapse; font-size: 12px; width: max-content; min-width: 100%; }
.mini-table th, .mini-table td { padding: 4px 10px; border-bottom: 1px solid var(--line); text-align: left; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mini-table th { color: var(--ink-2); }

/* charts & maps */
.visual { margin: 12px 0 18px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.visual figcaption { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); font-weight: 600; font-size: 14px; }
.visual .actions button { border: 0; background: none; color: var(--muted); font-size: 16px; }
.visual .canvas { padding: 8px; }
.visual .canvas.mapbox { padding: 0; height: 460px; }
.visual.big { position: fixed; inset: 60px 20px 20px; z-index: 2000; display: flex; flex-direction: column; margin: 0; }
.visual.big .canvas { flex: 1; }
.visual.big .canvas.mapbox { height: auto; }
.visual .caption:empty { display: none; }
.visual .caption { margin: 0; padding: 8px 14px 12px; color: var(--ink-2); font-size: 13px; }
.vega-embed { width: 100%; }
.vega-embed summary { opacity: .6; }
.map-legend { background: var(--surface); color: var(--ink-2); padding: 8px 10px; border-radius: 8px; font-size: 12px; box-shadow: var(--shadow); max-width: 240px; }
.map-legend > div { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.map-legend .sw { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.map-legend .sw.heat { border-radius: 2px; background: linear-gradient(90deg, #9ec5f4, #3987e5, #eda100, #e34948); width: 22px; }
.map-legend .ramp { display: flex; align-items: stretch; height: 10px; border-radius: 3px; overflow: hidden; }
.map-legend .ramp span { flex: 1; height: 10px; }
.map-legend .ramp-title { font-weight: 600; }
.map-legend .ramp-ends { display: flex; justify-content: space-between; color: var(--muted); }
.leaflet-popup-content { font: 12.5px/1.4 var(--font); }
.pop th { text-align: left; color: #52514e; padding-right: 8px; font-weight: 500; }
.leaflet-container { background: var(--surface-2); }
.err { color: var(--bad); }

/* composer */
.composer { position: sticky; bottom: 0; width: 100%; max-width: 900px; margin: 0 auto; padding: 10px 20px 18px;
  background: linear-gradient(to top, var(--page) 70%, transparent); }
.composer textarea { width: 100%; resize: none; border: 1px solid var(--line-2); border-radius: 14px; padding: 13px 15px;
  font: inherit; background: var(--surface); color: var(--ink); box-shadow: var(--shadow); outline: none; }
.composer textarea:focus { border-color: var(--accent); }
.composer-row { display: flex; align-items: center; gap: 12px; margin-top: 8px; font-size: 13px; color: var(--muted); }
.composer-row .hint { flex: 1; }
.model-pick select { margin-left: 6px; font: inherit; padding: 4px 6px; border-radius: 7px; border: 1px solid var(--line-2); background: var(--surface); color: var(--ink); }
#send { padding: 8px 20px; border: 0; border-radius: 9px; background: var(--accent); color: var(--accent-ink); font-weight: 600; }
#send:disabled { opacity: .5; cursor: wait; }
.shared-bar { position: sticky; bottom: 0; text-align: center; padding: 12px; background: var(--surface); border-top: 1px solid var(--line); font-size: 14px; }

/* ---- content pages ---- */
.page { max-width: 980px; margin: 0 auto; padding: 32px 20px 80px; }
.page h1 { font-size: 30px; letter-spacing: -.02em; margin: 0 0 8px; }
.page .lede { margin-bottom: 28px; }
.cat { margin: 30px 0; }
.cat h2 { font-size: 18px; margin: 0 0 4px; }
.cat .blurb { color: var(--ink-2); margin: 0 0 12px; font-size: 14px; }
.qlist { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.qlist a { display: block; padding: 11px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; color: var(--ink); }
.qlist a:hover { border-color: var(--accent); text-decoration: none; }
.qlist .ds { display: block; margin-top: 4px; font-size: 12px; color: var(--muted); }
.ds-table { width: 100%; border-collapse: collapse; font-size: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.ds-table th, .ds-table td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.ds-table td.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--ink-2); }
.ds-table code { font-family: var(--mono); font-size: 12.5px; }
.stats { display: flex; gap: 12px; flex-wrap: wrap; margin: 0 0 20px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px; min-width: 150px; }
.stat b { display: block; font-size: 24px; letter-spacing: -.01em; }
.stat span { font-size: 13px; color: var(--ink-2); }
pre.describe { white-space: pre-wrap; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 16px; font: 12.5px/1.55 var(--mono); overflow-x: auto; }
.prose { max-width: 720px; }
.prose p, .prose li { color: var(--ink-2); }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .topbar { padding: 10px 16px; }
  .topbar nav { gap: 12px; font-size: 13px; }
  .brand span:last-child { display: none; }
  .thread { padding: 16px 16px 170px; }
  .composer { padding: 8px 16px 14px; }
  .composer-row .hint { display: none; }
  .composer-row { justify-content: space-between; }
  .msg.user .bubble { max-width: 92%; }
  .visual .canvas.mapbox { height: 360px; }
  .visual.big { inset: 56px 8px 8px; }
  .page { padding: 24px 16px 60px; }
}
