:root {
  color-scheme: light;
  --bg: #fcfcfb;
  --text: #1d1d1b;
  --muted: #64635e;
  --border: #e3e2dd;
  --grid: #ecebe7;
  --row-hover: #f5f4f0;
  --row-selected: #eaf2fc;
  --tip-bg: #ffffff;
  --adj: #2a78d6;
  --ctrl: #eb6834;
  --error: #b42318;
  --logo-ink: #2d3a48;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #1a1a19;
    --text: #f1f0eb;
    --muted: #a8a79e;
    --border: #383834;
    --grid: #2c2c29;
    --row-hover: #232321;
    --row-selected: #1f2a38;
    --tip-bg: #262624;
    --adj: #3987e5;
    --ctrl: #d95926;
    --error: #f97066;
    --logo-ink: #f1f0eb;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1a1a19;
  --text: #f1f0eb;
  --muted: #a8a79e;
  --border: #383834;
  --grid: #2c2c29;
  --row-hover: #232321;
  --row-selected: #1f2a38;
  --tip-bg: #262624;
  --adj: #3987e5;
  --ctrl: #d95926;
  --error: #f97066;
  --logo-ink: #f1f0eb;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

.logo { display: block; height: 22px; width: auto; margin-bottom: 20px; }
.logo-ink { fill: var(--logo-ink); }

h1 {
  font-size: 20px;
  font-weight: 600;
  line-height: 1.3;
  margin: 0;
}

h2 {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
}

p { margin: 0; }

.muted { color: var(--muted); }

.error { color: var(--error); margin-top: 16px; }

[hidden] { display: none !important; }

header { margin-bottom: 20px; }

header .muted { margin-top: 2px; }

.description { margin-top: 10px; max-width: 720px; }

.title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
}

section { margin-top: 36px; }

select, input {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  min-height: 32px;
  max-width: 100%;
}

select:focus-visible, input:focus-visible, button:focus-visible, tr:focus-visible {
  outline: 2px solid var(--adj);
  outline-offset: 1px;
}

label { color: var(--muted); white-space: nowrap; }

label select { margin-left: 6px; }

.controls { display: flex; flex-wrap: wrap; gap: 8px 16px; }

#filter { min-width: 0; }

.result { font-size: 15px; max-width: 760px; }

.note { color: var(--muted); margin-top: 6px; max-width: 760px; }

.result + .note { margin-top: 8px; }

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  margin-bottom: 10px;
}

/* Tables */

.scroll-x { overflow-x: auto; margin-top: 14px; }

.table-gap { margin-top: 20px; }

table { border-collapse: collapse; width: 100%; }

th, td {
  padding: 6px 10px;
  text-align: left;
  white-space: nowrap;
}

th {
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

td { border-bottom: 1px solid var(--grid); }

th:first-child, td:first-child { padding-left: 0; }

.num { text-align: right; }

th.adj { box-shadow: inset 0 -2px 0 var(--adj); }
th.ctrl { box-shadow: inset 0 -2px 0 var(--ctrl); }

.summary { max-width: 640px; }
.summary td:first-child { color: var(--muted); white-space: normal; }
.summary th:last-child, .summary td:last-child { padding-right: 0; }

.weekly tbody tr:hover, .breakdown tbody tr:hover { background: var(--row-hover); }
.weekly tr.settling td { color: var(--muted); }
.weekly tr.settling td:first-child::after { content: " · settling"; font-size: 12px; }
.weekly tr.early td { color: var(--muted); }
.weekly tr.early td:first-child::after { content: " · too early"; font-size: 12px; }

.breakdown tbody tr { cursor: pointer; }
.breakdown tr.small td { color: var(--muted); }
.breakdown tr.selected, .breakdown tr.selected:hover { background: var(--row-selected); }
.breakdown td:first-child { white-space: normal; min-width: 14em; }

.breakdown th { position: sticky; top: 0; background: var(--bg); z-index: 1; }

@media (min-width: 720px) {
  .breakdown-wrap { max-height: 560px; overflow-y: auto; }
}

button.sort {
  font: inherit;
  font-weight: 600;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  white-space: nowrap;
}

/* Chart */

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  margin: 0 0 0 auto;
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 13px;
}

.legend li { display: flex; align-items: center; gap: 6px; }

.key { display: inline-block; width: 18px; height: 0; border-top: 2px solid; }
.key-adj { border-color: var(--adj); }
.key-ctrl { border-color: var(--ctrl); }
.key-lag { border-top-style: dashed; border-color: var(--muted); }
.key-base { border-top: 2px dotted var(--muted); }

.chart-box { position: relative; height: 320px; }

@media (max-width: 600px) {
  main { padding-top: 16px; }
  .legend { margin-left: 0; }
  .chart-box { height: 260px; }
  th, td { padding: 6px 6px; }
  .summary { font-size: 13px; }
  .summary td:first-child { min-width: 7em; }
}

footer { margin-top: 36px; font-size: 13px; max-width: 760px; }

/* Sign-in page (served by server.js) */
main.login { max-width: 320px; padding-top: 72px; }
main.login .logo { margin-bottom: 28px; }
.login h1 { font-size: 17px; }
.login form { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.login input { width: 100%; }
.login button {
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--adj);
  border: 0;
  border-radius: 6px;
  min-height: 34px;
  margin-top: 4px;
  cursor: pointer;
}
.login-error { color: var(--error); margin-top: 12px; }
