:root {
  color-scheme: light;
  --bg: #f4f3f0;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e0da;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7c7b76;
  --brand: #1f3a5f;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0fc;
  --good: #0a7f0a;
  --good-soft: #e5f4e5;
  --warn: #9a5b00;
  --warn-soft: #fdf2dc;
  --bad: #b42d2d;
  --bad-soft: #fbe7e7;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #33332f;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --brand: #cde2fb;
    --accent: #3987e5;
    --accent-soft: #16273d;
    --good: #4cc94c;
    --good-soft: #17301a;
    --warn: #f0b545;
    --warn-soft: #33270f;
    --bad: #ef7b7b;
    --bad-soft: #3a1b1b;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 26px; letter-spacing: -.01em; }
h2 { font-size: 19px; }
h3 { font-size: 15px; }
p { margin: 0; }
.muted { color: var(--text-2); }
.grow { flex: 1; min-width: 0; }
.small { font-size: 13px; }
.tnum { font-variant-numeric: tabular-nums; }

/* ---------- layout */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 24px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--text); text-decoration: none; }
.brand svg { width: 22px; height: 22px; fill: var(--accent); }
.crumbs { flex: 1; display: flex; gap: 6px; align-items: center; color: var(--text-2); font-size: 14px; min-width: 0; }
.crumbs a { color: var(--text-2); text-decoration: none; }
.crumbs a:hover { color: var(--text); }
.crumbs span.sep { color: var(--text-3); }
.crumbs .current { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.app { max-width: 1180px; margin: 0 auto; padding: 28px 24px 80px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px;
}
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 800px) { .grid2 { grid-template-columns: 1fr; } .app { padding: 20px 16px 60px; } .topbar { padding: 10px 16px; } }

/* ---------- controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 8px 14px; font: inherit; font-weight: 550; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn svg { width: 18px; height: 18px; flex: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible, .chip:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
input[type=text], input[type=password], textarea, select {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px;
}
textarea { resize: vertical; min-height: 44px; }
label.field { display: block; }
label.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border);
  background: var(--surface); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 14px;
  cursor: pointer; color: var(--text);
}
.chip:hover { border-color: var(--accent); }
.chip[aria-pressed=true] { background: var(--accent-soft); border-color: var(--accent); color: var(--text); font-weight: 600; }

/* ---------- login */
.login-wrap { min-height: 80vh; display: grid; place-items: center; }
.login { width: 100%; max-width: 380px; }
.login .logo { width: 44px; height: 44px; margin-bottom: 12px; }
.error-text { color: var(--bad); font-size: 14px; }

/* ---------- home */
.drop {
  border: 2px dashed var(--border); border-radius: var(--radius); padding: 40px 20px; text-align: center;
  background: var(--surface); transition: border-color .15s, background .15s; cursor: pointer;
}
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop svg { width: 40px; height: 40px; stroke: var(--accent); fill: none; stroke-width: 1.6; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 12px; padding: 12px 4px; border-top: 1px solid var(--border); }
.list li:first-child { border-top: 0; }
.list .grow { flex: 1; min-width: 0; }
.list a.title { font-weight: 600; color: var(--text); text-decoration: none; }
.list a.title:hover { color: var(--accent); }

.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 650; padding: 3px 9px; border-radius: 999px; white-space: nowrap; }
.badge svg { width: 13px; height: 13px; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.neutral { background: var(--surface-2); color: var(--text-2); }

/* ---------- tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--text-2); text-decoration: none; font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }

/* ---------- tables */
.table-wrap { overflow: auto; max-height: 460px; border: 1px solid var(--border); border-radius: 10px; }
table.data { border-collapse: collapse; width: 100%; font-size: 14px; }
table.data th, table.data td { padding: 7px 12px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
table.data th { position: sticky; top: 0; background: var(--surface-2); font-weight: 650; font-size: 13px; color: var(--text-2); z-index: 1; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:last-child td { border-bottom: 0; }
table.data input[type=text] { padding: 4px 8px; min-width: 140px; }

/* ---------- chat */
.ask-layout { display: grid; grid-template-columns: 240px 1fr; gap: 24px; }
@media (max-width: 900px) { .ask-layout { grid-template-columns: 1fr; } .history { display: none; } }
.history h3 { margin-bottom: 8px; color: var(--text-2); font-size: 13px; text-transform: uppercase; letter-spacing: .04em; }
.history a { display: block; padding: 8px 10px; border-radius: 8px; color: var(--text); text-decoration: none; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history a:hover, .history a.active { background: var(--surface-2); }
.thread > * + * { margin-top: 18px; }
.bubble-user { margin-left: auto; max-width: 80%; width: fit-content; background: var(--accent); color: var(--accent-ink); padding: 10px 14px; border-radius: 14px 14px 4px 14px; white-space: pre-wrap; }
.bubble-small { margin-left: auto; max-width: 80%; width: fit-content; background: var(--surface-2); padding: 8px 12px; border-radius: 12px; font-size: 14px; color: var(--text-2); }
.composer { position: sticky; bottom: 0; padding: 14px 0 6px; background: linear-gradient(transparent, var(--bg) 28%); }
.composer .box { display: flex; gap: 10px; align-items: flex-end; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 8px; box-shadow: var(--shadow); }
.composer textarea { border: 0; min-height: 24px; max-height: 200px; padding: 8px; background: transparent; }
.composer textarea:focus-visible { outline: none; }
.examples { display: flex; flex-wrap: wrap; gap: 8px; }
.working { display: flex; gap: 12px; align-items: center; color: var(--text-2); }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.question { padding: 14px 0; border-top: 1px solid var(--border); }
.question:first-of-type { border-top: 0; }
.question .opts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

/* ---------- result card */
.result-head { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; }
.interp { color: var(--text-2); margin-top: 4px; }
.chart { width: 100%; height: 360px; }
.chart.tall { height: 460px; }
.kpi-hero { padding: 18px 4px 6px; }
.kpi-hero .value { font-size: 44px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi-hero .label { color: var(--text-2); }
.figs { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.fig { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.fig .l { font-size: 12px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fig .v { font-size: 18px; font-weight: 650; font-variant-numeric: tabular-nums; }
ul.insights { margin: 0; padding-left: 20px; }
ul.insights li + li { margin-top: 6px; }
details { border: 1px solid var(--border); border-radius: 10px; padding: 0 14px; background: var(--surface); }
details + details { margin-top: 8px; }
details > summary { cursor: pointer; padding: 10px 0; font-weight: 600; font-size: 14px; list-style: none; display: flex; align-items: center; gap: 8px; }
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "›"; display: inline-block; transition: transform .15s; color: var(--text-3); font-size: 18px; line-height: 1; }
details[open] > summary::before { transform: rotate(90deg); }
details > .body { padding: 0 0 14px; }
pre.sql { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); padding: 12px; border-radius: 8px; overflow: auto; white-space: pre-wrap; margin: 6px 0 0; }
.checks { list-style: none; padding: 0; margin: 0; }
.checks li { display: flex; gap: 10px; padding: 6px 0; align-items: flex-start; }
.checks .ico { flex: none; width: 18px; height: 18px; margin-top: 2px; }
.ico.good { color: var(--good); } .ico.bad { color: var(--bad); } .ico.warn { color: var(--warn); }
.callout { border-radius: 10px; padding: 12px 14px; font-size: 14px; }
.callout.bad { background: var(--bad-soft); color: var(--text); border-left: 3px solid var(--bad); }
.callout.warn { background: var(--warn-soft); border-left: 3px solid var(--warn); }
.callout.info { background: var(--accent-soft); border-left: 3px solid var(--accent); }
.notes { font-size: 12.5px; color: var(--text-3); }

/* ---------- report */
.report-head { border-bottom: 1px solid var(--border); padding-bottom: 18px; }
.report-item { break-inside: avoid; }
.report-item + .report-item { margin-top: 20px; }

/* ---------- modal + toast */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: var(--radius); max-width: 1100px; width: 100%; max-height: 88vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.modal .content { padding: 16px 20px; overflow: auto; }
.modal .table-wrap { max-height: none; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; font-size: 14px; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }

/* ---------- print (Save as PDF) */
@media print {
  :root { color-scheme: light; --bg: #fff; --surface: #fff; --surface-2: #f3f3f1; --border: #ddd; --text: #000; --text-2: #444; --shadow: none; }
  .topbar, .tabs, .no-print, .composer, .history { display: none !important; }
  .app { max-width: none; padding: 0; }
  .card { box-shadow: none; border: 0; padding: 0; }
  .report-item { page-break-inside: avoid; border-top: 1px solid #ddd; padding-top: 16px; }
  details { border: 0; padding: 0; }
}
