:root, .viz-root {
  color-scheme: light;
  --bg: #f5f6f8;
  --surface-1: #ffffff;
  --surface-2: #f9fafb;
  --line: #e3e6eb;
  --line-strong: #cfd4dc;
  --text-primary: #191919;
  --text-secondary: #5f6672;
  --text-tertiary: #8a909b;
  --accent: #257de0;
  --accent-ink: #1b64be;
  --pos: #257de0;          /* profit bars: diverging blue pole */
  --neg: #ec4647;          /* loss bars: diverging red pole */
  --good-ink: #006c10;
  --bad-ink: #c42b2c;
  --warn-bg: #fff6e0;
  --warn-line: #f3d48a;
  --warn-ink: #7a5200;
  --err-bg: #fdecec;
  --err-line: #f2b8b8;
  --grid: #eceef2;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}
@media (prefers-color-scheme: dark) {
  :root, .viz-root {
    color-scheme: dark;
    --bg: #111214;
    --surface-1: #191919;
    --surface-2: #1f2023;
    --line: #2b2d31;
    --line-strong: #3a3d42;
    --text-primary: #efefef;
    --text-secondary: #a3a8b1;
    --text-tertiary: #7c818a;
    --accent: #338ced;
    --accent-ink: #6aabf1;
    --pos: #338ced;
    --neg: #eb6263;
    --good-ink: #4ad15e;
    --bad-ink: #f07b7c;
    --warn-bg: #2a2210;
    --warn-line: #5a4716;
    --warn-ink: #f2c75c;
    --err-bg: #2c1616;
    --err-line: #5c2626;
    --grid: #26282c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text-primary);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-ink); }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--text-secondary); }
.small { font-size: 12.5px; }
.loading { padding: 60px; text-align: center; color: var(--text-secondary); }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login {
  width: 100%; max-width: 360px; background: var(--surface-1); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login p { margin: 0 0 20px; color: var(--text-secondary); }
.login label { display: block; font-size: 13px; font-weight: 500; margin: 12px 0 6px; }
.login .btn { width: 100%; margin-top: 18px; justify-content: center; }

/* ---------- Shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1180px; margin: 0 auto; padding: 10px 20px; display: flex; align-items: center; gap: 18px;
}
.brand { font-weight: 700; letter-spacing: -0.01em; white-space: nowrap; }
.nav { display: flex; gap: 2px; flex: 1; overflow-x: auto; }
.nav a {
  text-decoration: none; color: var(--text-secondary); padding: 7px 11px; border-radius: 8px; font-weight: 500;
  white-space: nowrap;
}
.nav a:hover { color: var(--text-primary); background: var(--surface-2); }
.nav a.active { color: var(--text-primary); background: var(--surface-1); box-shadow: inset 0 0 0 1px var(--line); }
.top-actions { display: flex; align-items: center; gap: 10px; }
.sync-note { font-size: 12.5px; color: var(--text-secondary); white-space: nowrap; }
.sync-note.bad { color: var(--bad-ink); }
main { max-width: 1180px; margin: 0 auto; padding: 22px 20px 60px; }
@media (max-width: 760px) {
  .topbar-inner { flex-wrap: wrap; gap: 8px; }
  .nav { order: 3; flex-basis: 100%; }
  .sync-note { display: none; }
}

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line-strong);
  background: var(--surface-1); padding: 7px 12px; border-radius: 8px; cursor: pointer; font-weight: 500;
}
.btn:hover { border-color: var(--text-tertiary); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-secondary); }
.btn.ghost:hover { color: var(--text-primary); background: var(--surface-2); }
.btn.danger { color: var(--bad-ink); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn .spin { width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], select, textarea {
  background: var(--surface-1); border: 1px solid var(--line-strong); border-radius: 8px; padding: 7px 10px; outline: none;
  width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
textarea { resize: vertical; min-height: 110px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 16px; }
.seg { display: inline-flex; background: var(--surface-1); border: 1px solid var(--line); border-radius: 9px; padding: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; padding: 6px 10px; border-radius: 7px; cursor: pointer; color: var(--text-secondary); font-weight: 500; }
.seg button.on { background: var(--surface-2); color: var(--text-primary); box-shadow: inset 0 0 0 1px var(--line); }
.date-pair { display: inline-flex; gap: 6px; align-items: center; }
.date-pair input { width: 150px; }

/* ---------- Cards ---------- */
.card { background: var(--surface-1); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card + .card, .stack > * + * { margin-top: 16px; }
.card h2 { font-size: 15px; margin: 0 0 4px; }
.card .sub { color: var(--text-secondary); margin: 0 0 14px; font-size: 13px; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; margin-top: 16px; }
.grid2:first-child, .notices + .grid2 { margin-top: 0; }
.grid2 > .card { margin-top: 0; }
.grid2 + .card { margin-top: 16px; }
@media (max-width: 900px) { .grid2 { grid-template-columns: minmax(0, 1fr); } }
.grid2 > * { min-width: 0; }
.page-title { font-size: 20px; margin: 0 0 4px; letter-spacing: -0.01em; }
.page-sub { margin: 0 0 18px; color: var(--text-secondary); max-width: 760px; }

.kpis { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 16px; }
@media (max-width: 1100px) { .kpis { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 560px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
.kpi { background: var(--surface-1); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); min-width: 0; }
.kpi .l { font-size: 12.5px; color: var(--text-secondary); }
.kpi .v { font-size: 24px; font-weight: 700; letter-spacing: -0.02em; margin-top: 2px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .n { font-size: 12px; color: var(--text-tertiary); margin-top: 2px; }
.kpi.hero { grid-column: span 1; }
.kpi .v.pos { color: var(--good-ink); }
.kpi .v.neg { color: var(--bad-ink); }

.notices { display: grid; gap: 8px; margin-bottom: 16px; }
.notice { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--warn-line); background: var(--warn-bg); color: var(--warn-ink); border-radius: 10px; padding: 9px 12px; font-size: 13px; }
.notice.err { border-color: var(--err-line); background: var(--err-bg); color: var(--bad-ink); }
.notice.info { border-color: var(--line); background: var(--surface-1); color: var(--text-secondary); }
.notice b { font-weight: 600; }
.notice a { color: inherit; font-weight: 600; }
.notice .ico { flex: none; font-weight: 700; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { font-size: 12px; font-weight: 600; color: var(--text-secondary); background: var(--surface-1); position: sticky; top: 0; }
tbody tr:hover td { background: var(--surface-2); }
tr.total td { font-weight: 700; border-bottom: 1px solid var(--line-strong); }
tr.sub td:first-child { padding-left: 22px; color: var(--text-secondary); }
td.pos { color: var(--good-ink); } td.neg { color: var(--bad-ink); }
td.left, th.left { text-align: left; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 999px; font-size: 11.5px; font-weight: 600; border: 1px solid var(--line-strong); color: var(--text-secondary); }
.badge.warn { border-color: var(--warn-line); background: var(--warn-bg); color: var(--warn-ink); }
.badge.err { border-color: var(--err-line); background: var(--err-bg); color: var(--bad-ink); }
.badge.ok { border-color: color-mix(in srgb, var(--good-ink) 35%, transparent); color: var(--good-ink); }
.scroll { max-height: 520px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.scroll table th { border-bottom: 1px solid var(--line-strong); }

/* ---------- Chart ---------- */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .axis text { fill: var(--text-tertiary); font-size: 11px; }
.chart .grid line { stroke: var(--grid); }
.chart .zero { stroke: var(--line-strong); }
.chart .bar.pos { fill: var(--pos); }
.chart .bar.neg { fill: var(--neg); }
.chart .bar.dim { opacity: 0.35; }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .hl, .chart .hl.on { fill: color-mix(in srgb, var(--text-primary) 6%, transparent); }
.tooltip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--surface-1); border: 1px solid var(--line-strong);
  border-radius: 10px; padding: 8px 10px; font-size: 12.5px; box-shadow: 0 6px 24px rgba(0,0,0,0.12); min-width: 170px;
}
.tooltip .t { font-weight: 600; margin-bottom: 4px; }
.tooltip .r { display: flex; justify-content: space-between; gap: 14px; font-variant-numeric: tabular-nums; }
.tooltip .r span:first-child { color: var(--text-secondary); }

/* ---------- Forms ---------- */
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; align-items: end; }
.field label { display: block; font-size: 12.5px; color: var(--text-secondary); margin-bottom: 4px; font-weight: 500; }
.inline { display: flex; gap: 8px; align-items: center; }
.cell-input { width: 120px !important; text-align: right; padding: 5px 8px !important; }
.saved { color: var(--good-ink); font-size: 12px; }
.err-text { color: var(--bad-ink); font-size: 13px; margin-top: 8px; }
pre.code { background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; max-height: 320px; overflow: auto; font-size: 12px; white-space: pre-wrap; word-break: break-all; }
details summary { cursor: pointer; font-weight: 500; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 13.5px; }
.kv div:nth-child(odd) { color: var(--text-secondary); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60; background: var(--text-primary); color: var(--surface-1);
  padding: 9px 14px; border-radius: 10px; font-size: 13px; box-shadow: 0 6px 24px rgba(0,0,0,0.2); max-width: 90vw;
}
.toast.err { background: var(--bad-ink); color: #fff; }
