/*
 * The admin page at /manage (Ervin 2026-10-09). Light and dark, phone (360 px) to desktop.
 * Served under default-src 'self': no inline style anywhere; bar lengths are set through the
 * CSSOM (--w, --at), which the CSP allows.
 */
:root {
  color-scheme: light;
  --page: #f4f3ef;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6b6a65;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --border-strong: rgba(11, 11, 11, 0.22);
  --input-bg: #ffffff;
  /* One series (slot 1, blue); its track is a lighter step of the same ramp. */
  --accent: #2a78d6;
  --accent-text: #1c5cab;
  --track: #cde2fb;
  /* Days known only from the sync log: a de-emphasised gray beside the blue. */
  --untracked: #a8a69e;
  --primary: #256abf;
  --on-primary: #ffffff;
  --focus: #2a78d6;
  /* Status, never used for a series; always with a label. */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --error: #b42323;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #a3a197;
    --grid: #2c2c2a;
    --axis: #45443f;
    --border: rgba(255, 255, 255, 0.1);
    --border-strong: rgba(255, 255, 255, 0.24);
    --input-bg: #121211;
    --accent: #3987e5;
    --accent-text: #86b6ef;
    --track: #1d3352;
    --untracked: #6b6a65;
    --primary: #6da7ec;
    --on-primary: #0b0b0b;
    --focus: #86b6ef;
    --error: #ff8a80;
  }
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font:
    16px/1.5 system-ui,
    -apple-system,
    'Segoe UI',
    sans-serif;
}

[hidden] {
  display: none !important;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.25;
}

p {
  margin: 0;
}

/* ---- Header ------------------------------------------------------------------------------ */

.top {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--page);
  border-bottom: 1px solid var(--border);
}

.top-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
}

.brand h1 {
  font-size: 20px;
  font-weight: 700;
}

.sub {
  color: var(--ink-2);
  font-size: 14px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.updated {
  color: var(--ink-2);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

/* ---- Controls ---------------------------------------------------------------------------- */

.btn {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary {
  background: var(--primary);
  border-color: transparent;
  color: var(--on-primary);
}

.btn-quiet {
  background: transparent;
}

.btn[disabled] {
  opacity: 0.6;
  cursor: default;
}

.btn:focus-visible,
input:focus-visible,
summary:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* ---- Layout ------------------------------------------------------------------------------ */

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
}

.card {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}

.card h2 {
  font-size: 18px;
  font-weight: 650;
  margin-bottom: 12px;
}

.card h3 {
  font-size: 15px;
  font-weight: 650;
  color: var(--ink-2);
  margin: 16px 0 8px;
}

.grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  align-items: start;
  transition: opacity 0.15s;
}

.grid.busy {
  opacity: 0.6;
}

/*
 * Desktop: two balanced columns that read top to bottom in page order (a grid would leave holes
 * beside the long day table and sync list).
 */
@media (min-width: 900px) {
  .grid {
    display: block;
    columns: 2;
    column-gap: 16px;
  }
  .grid > .card {
    break-inside: avoid;
    margin-bottom: 16px;
  }
}

@media (max-width: 420px) {
  main {
    padding: 12px 16px 24px;
  }
  .card {
    padding: 14px 12px;
  }
}

/* Phones: the header scrolls away (sticky, it would take a fifth of the screen). */
@media (max-width: 599px) {
  .top {
    position: static;
  }
  .actions .btn {
    padding: 0 12px;
  }
}

/* ---- Sign in ----------------------------------------------------------------------------- */

.sign-in {
  max-width: 520px;
  margin: 16px auto;
}

.sign-in label {
  display: block;
  font-weight: 600;
}

.key-row {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.key-row input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--ink);
  font: inherit;
}

.hint {
  margin-top: 8px;
  color: var(--ink-2);
  font-size: 14px;
}

.error {
  margin-top: 12px;
  color: var(--error);
  font-weight: 600;
}

.status {
  min-height: 1.5em;
  margin-bottom: 12px;
  color: var(--ink-2);
  font-size: 14px;
}

.status.bad {
  color: var(--error);
  font-weight: 600;
}

/* A section whose call failed: its last numbers stay below this line. */
.section-error {
  margin-bottom: 10px;
  color: var(--error);
  font-size: 14px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

/* The usage store cannot be taken as it stands (unread at boot, writes failing). */
.warning-note {
  margin-bottom: 12px;
  padding: 8px 10px;
  border-left: 4px solid var(--warning);
  background: var(--surface-2);
  border-radius: 4px;
  font-size: 14px;
  overflow-wrap: anywhere;
}

/* ---- Figures ----------------------------------------------------------------------------- */

.hero {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
}

.hero-value {
  font-size: 48px;
  font-weight: 650;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.hero-unit {
  color: var(--ink-2);
  font-size: 18px;
}

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  gap: 8px;
  margin: 0;
}

.tiles + .note,
.hero + .tiles {
  margin-top: 12px;
}

.tile {
  min-width: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface-2);
}

.tile dt {
  color: var(--ink-2);
  font-size: 14px;
}

.tile dd {
  margin: 2px 0 0;
  font-size: 22px;
  font-weight: 650;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.tile .of {
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 400;
}

.note {
  margin-top: 10px;
  color: var(--ink-2);
  font-size: 14px;
}

.empty {
  color: var(--ink-2);
}

/* Meters: used against a limit, with the alert mark where it applies. */
.meters {
  display: grid;
  gap: 14px;
  margin-top: 12px;
}

.meter-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0 12px;
}

.meter-name {
  font-weight: 600;
}

.meter-value {
  font-variant-numeric: tabular-nums;
}

.meter-value .of {
  color: var(--ink-2);
}

.meter-track {
  display: block;
  position: relative;
  height: 12px;
  margin-top: 4px;
  border-radius: 4px;
  background: var(--track);
}

.meter-fill {
  display: block;
  height: 100%;
  width: var(--w, 0%);
  max-width: 100%;
  border-radius: 4px;
  background: var(--accent);
}

.meter-fill.over {
  background: var(--warning);
}

.meter-mark {
  display: block;
  position: absolute;
  top: -4px;
  bottom: -4px;
  left: var(--at, 70%);
  width: 2px;
  margin-left: -1px;
  background: var(--ink-2);
}

.meter-note {
  margin-top: 4px;
  color: var(--ink-2);
  font-size: 14px;
}

/* Status chips: a colored dot and a label, never color alone. */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.6;
}

.chip::before {
  content: '';
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--chip, var(--muted));
}

.chip.good {
  --chip: var(--good);
}

.chip.warning {
  --chip: var(--warning);
}

.chip.serious {
  --chip: var(--serious);
}

.chip.critical {
  --chip: var(--critical);
}

/* ---- Bars -------------------------------------------------------------------------------- */

.track {
  display: block;
  min-width: 0;
}

.bar {
  display: block;
  height: 12px;
  width: var(--w, 0%);
  border-radius: 0 4px 4px 0;
  background: var(--accent);
}

.bar.untracked {
  background: var(--untracked);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  margin: 0 0 10px;
  padding: 0;
  list-style: none;
  color: var(--ink-2);
  font-size: 14px;
}

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

.swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: var(--accent);
}

.swatch.untracked {
  background: var(--untracked);
}

/* A labelled list of counts with a thin bar each (units by method, song book groups). */
.bars {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.bars li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 2px 12px;
}

.bars .name {
  overflow-wrap: anywhere;
}

.bars .count {
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.bars .share {
  color: var(--ink-2);
  font-size: 14px;
  font-weight: 400;
}

.bars .track {
  grid-column: 1 / -1;
}

.bars .bar {
  height: 8px;
}

.groups {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0 24px;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
}

/* ---- Tables ------------------------------------------------------------------------------ */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

th {
  padding: 6px 8px 6px 0;
  border-bottom: 1px solid var(--axis);
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
}

td {
  padding: 7px 8px 7px 0;
  border-bottom: 1px solid var(--grid);
  vertical-align: middle;
}

th:last-child,
td:last-child {
  padding-right: 0;
}

.num {
  text-align: right;
  white-space: nowrap;
}

td.day {
  white-space: nowrap;
}

tr.more {
  display: none;
}

.more-toggle {
  margin-top: 8px;
}

.tag {
  display: block;
  color: var(--ink-2);
  font-size: 12px;
  line-height: 1.2;
  /* Under the date, in as many lines as the column needs (the date itself stays on one). */
  white-space: normal;
}

td.units {
  width: 100%;
}

.units-cell {
  display: flex;
  align-items: center;
  gap: 8px;
}

.units-cell .v {
  flex: none;
  min-width: 3.6em;
  text-align: right;
}

.units-cell .track {
  flex: 1 1 auto;
}

tfoot td {
  border-bottom: 0;
  border-top: 1px solid var(--axis);
  font-weight: 650;
}

tfoot tr + tr td {
  border-top: 0;
}

/* Footer labels wrap, so seven-digit totals keep the table inside its card at 320 px. */
tfoot td.foot-label {
  white-space: normal;
  overflow-wrap: anywhere;
}

/* Never wider than its card: should a column still outgrow it, the table scrolls, not the page. */
.table-wrap {
  max-width: 100%;
  overflow-x: auto;
}

tfoot .sub-row td {
  color: var(--ink-2);
  font-weight: 400;
}

/*
 * Phones: a smaller table font, dates without their weekday, and the bar under its number
 * (beside it, it would get a few pixels), so seven-digit totals fit 320 px. After the base
 * table rules, so it wins.
 */
@media (max-width: 420px) {
  table {
    font-size: 14px;
  }
  .weekday {
    display: none;
  }
  .units-cell {
    flex-direction: column;
    align-items: stretch;
    gap: 3px;
  }
  .units-cell .v {
    min-width: 0;
    text-align: left;
  }
  .units-cell .bar {
    height: 8px;
  }
}

details {
  margin-top: 8px;
}

summary {
  display: flex;
  align-items: center;
  min-height: 44px;
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
}

/* ---- Sync runs --------------------------------------------------------------------------- */

.runs {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.run {
  padding: 10px 12px;
  border: 1px solid var(--grid);
  border-radius: 8px;
}

.run-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 12px;
}

.run-when {
  font-weight: 600;
}

.run-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 6px 12px;
  margin: 8px 0 0;
}

.run-stats dt {
  color: var(--ink-2);
  font-size: 13px;
}

.run-stats dd {
  margin: 0;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.run-note {
  margin-top: 6px;
  color: var(--ink-2);
  font-size: 14px;
  overflow-wrap: anywhere;
}

/* ---- Screen readers only ----------------------------------------------------------------- */

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

@media (prefers-reduced-motion: reduce) {
  .grid {
    transition: none;
  }
}
