:root {
  color-scheme: light;
  --surface: #fcfcfb;
  --panel: #ffffff;
  --ink: #1a1a19;
  --muted: #52514e;
  --rule: #c3c2b7;
  --grid: #e4e3dc;
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --accent-bg: #eef4fd;
  --warn-bg: #fdf1e9;
}

/* Dark is selected, not an automatic inversion: its own steps from the same
   ramps, validated against the dark surface. The system preference applies
   unless the reader has explicitly chosen light; an explicit choice wins in
   both directions. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
  color-scheme: dark;
  --surface: #17171a;
  --panel: #1f1f23;
  --ink: #f2f1ec;
  --muted: #a8a69e;
  --rule: #3a3a40;
  --grid: #2c2c31;
  --s1: #3987e5;
  --s2: #d95926;
  --s3: #199e70;
  --accent-bg: #1b2536;
  --warn-bg: #2e2119;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface: #17171a;
  --panel: #1f1f23;
  --ink: #f2f1ec;
  --muted: #a8a69e;
  --rule: #3a3a40;
  --grid: #2c2c31;
  --s1: #3987e5;
  --s2: #d95926;
  --s3: #199e70;
  --accent-bg: #1b2536;
  --warn-bg: #2e2119;
}

* { box-sizing: border-box; }
body {
  margin: 0; padding: 0 1.2rem 4rem;
  background: var(--surface); color: var(--ink);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
header, main, footer { max-width: 1080px; margin: 0 auto; }
header { padding: 2.4rem 0 1rem; }
h1 { font-size: 1.9rem; margin: 0 0 .4rem; letter-spacing: -.01em; }
h2 { font-size: 1.25rem; margin: 0 0 .3rem; letter-spacing: -.005em; }
.lede { font-size: 1.06rem; color: var(--muted); margin: 0 0 1rem; max-width: 62ch; }
.caveat {
  background: var(--warn-bg); border-left: 3px solid var(--s2);
  padding: .8rem 1rem; margin: 0; border-radius: 0 8px 8px 0;
  font-size: .93rem; color: var(--ink); max-width: 78ch;
}
section {
  background: var(--panel); border: 1px solid var(--rule); border-radius: 12px;
  padding: 1.3rem 1.4rem 1.5rem; margin: 1.6rem 0;
}
section > p { color: var(--muted); margin: .2rem 0 1rem; max-width: 70ch; }

.controls { display: flex; flex-wrap: wrap; gap: .7rem 1.1rem; align-items: center; margin: .9rem 0; }
.controls label { font-size: .88rem; color: var(--muted); display: inline-flex; gap: .4rem; align-items: center; }
input, select, button {
  font: inherit; font-size: .9rem; color: var(--ink); background: var(--surface);
  border: 1px solid var(--rule); border-radius: 7px; padding: .32rem .5rem;
}
input[type=number] { width: 7.5rem; }
button { cursor: pointer; background: var(--s1); color: #fff; border-color: transparent; padding: .38rem .85rem; }
button:hover { filter: brightness(1.08); }
button.ghost { background: transparent; color: var(--muted); border-color: var(--rule); }
button.x { background: transparent; color: var(--muted); padding: .2rem .45rem; }
.row-actions { display: flex; gap: .6rem; margin: .7rem 0 0; }
.status { font-size: .85rem; color: var(--muted); }

table { border-collapse: collapse; width: 100%; font-size: .89rem; }
.table-scroll, section { overflow-x: auto; }
th, td { text-align: left; padding: .38rem .55rem; border-bottom: 1px solid var(--grid); }
th { color: var(--muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.edit input { width: 100%; min-width: 5rem; }
table.data tr.hot td { background: var(--warn-bg); }
tfoot td { font-weight: 600; border-top: 2px solid var(--rule); border-bottom: none; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .8rem; margin-top: 1.1rem; }
.stat { background: var(--accent-bg); border-radius: 9px; padding: .7rem .85rem; }
.stat .v { font-size: 1.3rem; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat .k { font-size: .78rem; color: var(--muted); margin-top: .15rem; }

figure { margin: 1.2rem 0 0; }
figcaption { font-size: .88rem; color: var(--muted); margin-bottom: .3rem; }
.chart-host { width: 100%; }
.chart-pair { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: 1.2rem; }
.chart-triple { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.2rem; }
.legend { display: flex; flex-wrap: wrap; gap: .3rem 1rem; font-size: .84rem; color: var(--muted); margin-top: .2rem; }
.legend span { display: inline-flex; align-items: center; gap: .35rem; }
.legend i { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }

.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--rule); stroke-width: 1; }
.chart .ref { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 5 4; opacity: .8; }
.chart .tick, .chart .ax-label, .chart .ref-label, .chart .series-label {
  fill: var(--muted); font-size: 11px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.chart .series-label { fill: var(--ink); font-weight: 600; }
.chart .ax-label { font-size: 11.5px; }

.note { font-size: .9rem; color: var(--muted); margin: .9rem 0 0; max-width: 74ch; }
.note strong { color: var(--ink); }
footer { margin-top: 2.5rem; padding-top: 1.2rem; border-top: 1px solid var(--rule); color: var(--muted); font-size: .88rem; }
footer .small { font-size: .82rem; }
a { color: var(--s1); }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }


/* ---------------------------------------------------------- theme switcher */
.theme-switch {
  position: absolute; top: 1.5rem; right: 0;
  display: inline-flex; gap: 2px; padding: 3px;
  background: var(--panel); border: 1px solid var(--rule); border-radius: 999px;
}
.theme-switch button {
  background: transparent; color: var(--muted); border: none;
  border-radius: 999px; padding: .28rem .68rem; font-size: .8rem; line-height: 1.3;
}
.theme-switch button[aria-pressed="true"] { background: var(--s1); color: #fff; }
.theme-switch button:hover:not([aria-pressed="true"]) { color: var(--ink); }
header { position: relative; }
@media (max-width: 640px) {
  .theme-switch { position: static; margin-bottom: 1rem; }
}
footer .copyright { margin-top: .9rem; }
