:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #16191d;
  --muted: #6b7280;
  --line: #e3e6ea;
  --accent: #2f5d8a;
  --accent-ink: #ffffff;
  --pos: #17643b;
  --neg: #9b2226;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --panel: #1b1f24;
    --ink: #e7eaee;
    --muted: #9aa4b1;
    --line: #2b3138;
    --accent: #6fa8dc;
    --accent-ink: #10161c;
    --pos: #6ed49a;
    --neg: #f08a8a;
  }
}

* { box-sizing: border-box; }

/* MUST come before any rule that sets `display` on an element toggled via the `hidden` attribute.
 * The UA stylesheet's `[hidden] { display: none }` has the same specificity as our own class rules
 * and loses to any later one — so `.drawer { display: grid }` left the full-screen drawer overlay
 * permanently on top of the page, invisible but eating every click, which reads as the whole
 * interface freezing. Same trap for `.login-screen` and `.pager`. */
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2 { font-weight: 600; letter-spacing: -0.01em; }
.muted { color: var(--muted); }
.error { color: var(--neg); }

/* ---- login ---------------------------------------------------------------------------------- */
.login-screen {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
}
.login-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 28px; width: min(380px, 100%); display: grid; gap: 12px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
input, select {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--ink); font: inherit; width: 100%;
}
button {
  padding: 8px 14px; border: 1px solid transparent; border-radius: 6px;
  background: var(--accent); color: var(--accent-ink); font: inherit; cursor: pointer;
}
button:disabled { opacity: .5; cursor: default; }
button.linkish {
  background: none; color: var(--accent); border: none; padding: 0; text-decoration: underline;
}

/* ---- chrome --------------------------------------------------------------------------------- */
/* The nav wraps rather than scrolling: a horizontal scrollbar hides whichever tabs do not fit,
 * and the ones that get hidden are the ones added most recently. */
.topbar {
  display: flex; align-items: center; gap: 12px 18px; padding: 0 20px; min-height: 52px;
  flex-wrap: wrap;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 650; }
.spacer { flex: 1; }
#nav { display: flex; gap: 2px; flex-wrap: wrap; }
#nav button {
  background: none; color: var(--muted); border: none; border-bottom: 2px solid transparent;
  border-radius: 0; padding: 14px 9px; white-space: nowrap; font-size: 13px;
}
#nav button[aria-current="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.user { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; white-space: nowrap; }

main { padding: 20px; max-width: 1500px; margin: 0 auto; }
#view-header h2 { margin: 0 0 2px; font-size: 18px; }
#view-header p { margin: 0 0 14px; }

.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 14px; }
.filters label { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.filters input, .filters select { min-width: 150px; }

/* ---- tables --------------------------------------------------------------------------------- */
.table-wrap {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  overflow-x: auto;
}
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; white-space: nowrap; position: sticky; top: 0; background: var(--panel); }
tr:last-child td { border-bottom: none; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: color-mix(in srgb, var(--accent) 8%, transparent); }
td.mono, th.mono { font-family: var(--mono); font-size: 12px; }
td.num { text-align: right; font-family: var(--mono); font-size: 12px; white-space: nowrap; }
td.pos { color: var(--pos); }
td.neg { color: var(--neg); }
.pill {
  display: inline-block; padding: 1px 7px; border-radius: 99px; font-size: 11px;
  border: 1px solid var(--line); color: var(--muted);
}
.pill.ok { color: var(--pos); border-color: color-mix(in srgb, var(--pos) 40%, transparent); }
.pill.bad { color: var(--neg); border-color: color-mix(in srgb, var(--neg) 40%, transparent); }

/* ---- grouped tables -------------------------------------------------------------------------- */
/* A group header doubles as the period's divider and its total row. The divider is a heavy top
 * border rather than a blank spacer row, so the separation survives scrolling under the sticky
 * header and copy/pasting the table into a spreadsheet. */
table.grouped tbody tr.group th {
  background: color-mix(in srgb, var(--accent) 10%, var(--panel));
  color: var(--ink); font-weight: 650; font-size: 13px;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
  padding: 9px 12px; position: static;
}
/* The gap between periods: a row painted in the PAGE background, so the panel visibly breaks
 * rather than merely drawing a heavier line through one continuous block. */
table.grouped tbody tr.spacer td {
  background: var(--bg); height: 14px; padding: 0; border: none;
}
/* Last row of a group closes its block instead of running into the gap. */
table.grouped tbody tr.spacer + tr.group th { border-top: none; }
table.grouped tbody tr.group th.group-label { white-space: nowrap; }
table.grouped tbody tr.group th.group-total { font-family: var(--mono); font-size: 12px; }
table.grouped tbody tr.group th.group-total.pos { color: var(--pos); }
table.grouped tbody tr.group th.group-total.neg { color: var(--neg); }
.group-count { font-weight: 400; font-size: 11px; }
/* Member rows sit visually inside their group. */
table.grouped tbody tr:not(.group) td:first-child { padding-left: 22px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 12px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px;
}
.card h3 { margin: 0 0 8px; font-size: 14px; }
.card dl { margin: 0; display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 13px; }
.card dt { color: var(--muted); }
.card dd { margin: 0; text-align: right; font-family: var(--mono); font-size: 12px; }

.pager { display: flex; align-items: center; gap: 12px; margin-top: 14px; }

/* ---- detail drawer -------------------------------------------------------------------------- */
.drawer {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; justify-content: end;
  z-index: 10;
}
.drawer-inner {
  background: var(--panel); width: min(760px, 100vw); height: 100%; overflow: auto; padding: 20px;
  border-left: 1px solid var(--line);
}
.drawer pre {
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 12px;
  overflow: auto; font-family: var(--mono); font-size: 12px; max-height: 45vh;
}
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin: 12px 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-family: var(--mono); font-size: 12px; word-break: break-all; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

/* ---- settings -------------------------------------------------------------------------------- */
.settings-card { max-width: 620px; }
.settings-form { display: grid; gap: 12px; }
.settings-form h3 { margin: 0; font-size: 15px; }
.settings-form p { margin: 0; font-size: 13px; }
.settings-form label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.settings-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.settings-actions button[type="button"] { background: none; color: var(--accent); border: 1px solid var(--line); }

/* ---- loading ---------------------------------------------------------------------------------
   The heavy views take tens of seconds. Without a sign of life the page looks broken, and someone
   waiting on an accounting figure reloads — which costs another query. So: say what is running,
   count the seconds honestly, and once it has been slow long enough to worry anyone, say why. */
.loading {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  padding: 28px 22px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
}
.loading-label { margin: 0; font-weight: 600; }
.loading .muted { margin: 8px 0 0; max-width: 62ch; }
.elapsed {
  font-variant-numeric: tabular-nums;
  font-weight: 400;
  color: var(--muted);
}
.spinner {
  width: 18px;
  height: 18px;
  margin-top: 2px;
  flex: 0 0 auto;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
/* Respect a reader who has asked for less motion: keep the cue, drop the spin. */
@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; border-top-color: var(--accent); }
}

/* A busy server is not a broken one, and the difference should be visible at a glance. */
.empty.warn { color: var(--fg); }
.empty.warn p { margin: 0 0 12px; max-width: 62ch; }
button.retry {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--fg);
  cursor: pointer;
}
button.retry:hover { border-color: var(--accent); }
