/* ── Theme ─────────────────────────────────────────────────────────────────── */

:root {
  color-scheme: light;
  --bg: #fbfaf8;
  --surface: #ffffff;
  --ink: #1b1f27;
  --muted: #6d7480;
  --hairline: #e2e0dc;
  --edge: #cfcdc8;           /* outline of an unlived cell */
  --lived: #d7443e;
  --now: #f0a202;
  --now-ring: rgba(240, 162, 2, 0.32);
  --ok: #2f9e5f;
  --focus: #2f6fd0;
  --shadow: 0 1px 2px rgba(20, 20, 25, 0.06), 0 8px 24px rgba(20, 20, 25, 0.06);

  --cell: 11px;
  --gap: 3px;
  --cols: 52;
}

/* Auto mode follows the OS; an explicit data-theme always wins. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #101219;
    --surface: #171a22;
    --ink: #e9ecf3;
    --muted: #98a0b0;
    --hairline: #262b36;
    --edge: #3a4150;
    --lived: #e2564d;
    --now: #f5b342;
    --now-ring: rgba(245, 179, 66, 0.28);
    --focus: #6ea8fe;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101219;
  --surface: #171a22;
  --ink: #e9ecf3;
  --muted: #98a0b0;
  --hairline: #262b36;
  --edge: #3a4150;
  --lived: #e2564d;
  --now: #f5b342;
  --now-ring: rgba(245, 179, 66, 0.28);
  --ok: #4ec97f;
  --focus: #6ea8fe;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
}

/* ── Base ──────────────────────────────────────────────────────────────────── */

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 2.5rem 1.25rem 3rem;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
        Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
  text-decoration-color: var(--edge);
  text-underline-offset: 2px;
}
a:hover { text-decoration-color: var(--lived); }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ── Masthead ──────────────────────────────────────────────────────────────── */

.masthead {
  max-width: 62rem;
  margin: 0 auto 2.25rem;
}

.masthead-row {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
}

.title {
  flex: 1;
  margin: 0;
  font-size: clamp(1.6rem, 1.1rem + 2.2vw, 2.6rem);
  font-weight: 650;
  letter-spacing: -0.02em;
  line-height: 1.15;
}

/* The unit in the headline is the view switcher, as on the original. */
.title-select { position: relative; display: inline-block; }

.title-select select {
  appearance: none;
  -webkit-appearance: none;
  margin: 0;
  padding: 0 1.5em 0 0;
  border: 0;
  border-bottom: 2px dashed var(--edge);
  background: transparent;
  color: var(--lived);
  font: inherit;
  letter-spacing: inherit;
  cursor: pointer;
}
.title-select select:hover { border-bottom-color: var(--lived); }
.title-select select:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
  border-radius: 3px;
}
.title-select::after {
  content: "";
  position: absolute;
  right: 0.35em;
  top: 55%;
  width: 0.42em; height: 0.42em;
  border-right: 2px solid var(--lived);
  border-bottom: 2px solid var(--lived);
  transform: translateY(-50%) rotate(45deg);
  pointer-events: none;
}
.title-select select option { color: var(--ink); background: var(--surface); }

.lede {
  margin: 0.5rem 0 1.5rem;
  color: var(--muted);
  max-width: 44ch;
}

.masthead-tools {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

/* ── Battery ───────────────────────────────────────────────────────────────── */

.battery { display: flex; align-items: center; gap: 0.45rem; }
.battery[hidden] { display: none; }

.battery-svg { width: 44px; height: 22px; overflow: visible; }
.battery-shell { fill: none; stroke: var(--muted); stroke-width: 1.8; }
.battery-cap { fill: var(--muted); }
.battery-fill {
  fill: var(--batt, var(--ok));
  transition: width 0.4s ease, fill 0.4s ease;
}

.battery-pct {
  font-size: 0.95rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

/* Charge levels, read the way a phone reads them. */
.battery[data-level="ok"]   { --batt: var(--ok); }
.battery[data-level="warn"] { --batt: var(--now); }
.battery[data-level="low"]  { --batt: var(--lived); }
.battery[data-level="low"] .battery-pct { color: var(--lived); }

.icon-btn {
  flex: none;
  width: 2.4rem; height: 2.4rem;
  display: grid;
  place-items: center;
  border: 1px solid var(--hairline);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
}
.icon-btn:hover { color: var(--ink); border-color: var(--edge); }
.icon-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.icon-sun { display: none; }
:root[data-theme="dark"] .icon-sun { display: block; }
:root[data-theme="dark"] .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon-sun { display: block; }
  :root:not([data-theme="light"]) .icon-moon { display: none; }
}

/* ── Controls ──────────────────────────────────────────────────────────────── */

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 1.25rem 1.75rem;
  padding: 1rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field label {
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.field-value { color: var(--ink); font-variant-numeric: tabular-nums; }
.field-actions { justify-content: flex-end; margin-left: auto; }

input[type="date"] {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--edge);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
}
input[type="date"]:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

input[type="range"] {
  width: 12rem;
  accent-color: var(--lived);
  cursor: pointer;
}
input[type="range"]:focus-visible { outline: 2px solid var(--focus); outline-offset: 4px; }

.btn {
  padding: 0.5rem 1rem;
  border: 1px solid var(--edge);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.notice {
  margin: 0.9rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  min-height: 1.4em;
}
.notice.is-error { color: var(--lived); }

/* ── Stats ─────────────────────────────────────────────────────────────────── */

.stats[hidden] { display: none; }

.stats {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 2.25rem;
  margin: 1.1rem 0 0;
}
.stats .stat { margin: 0; }
.stats dt {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.stats dd { margin: 0.1rem 0 0; color: var(--muted); }
.stats strong {
  font-size: 1.35rem;
  font-weight: 650;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}
.stat-meter { flex: 1 1 12rem; min-width: 8rem; }
.meter {
  display: block;
  height: 6px;
  border-radius: 999px;
  background: var(--hairline);
  overflow: hidden;
}
.meter-fill {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: var(--lived);
  transition: width 0.35s ease;
}

/* ── Board ─────────────────────────────────────────────────────────────────── */

main { max-width: 62rem; margin: 0 auto; }

.board-scroll {
  overflow-x: auto;
  padding-bottom: 0.5rem;
}

.board {
  display: grid;
  grid-template-columns: auto auto auto;
  grid-template-rows: auto auto auto;
  gap: 0.35rem 0.6rem;
  width: max-content;
  margin: 0;
  padding: 1.25rem 1.5rem 1.5rem;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.board[data-view="months"] { --cell: 22px; --gap: 7px; }
.board[data-view="years"]  { --cell: 26px; --gap: 17px; }

.axis-label {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  white-space: nowrap;
}
.axis-label-x { grid-area: 1 / 3; }
/* Pinned to the top of the grid: the label is only needed on first read, and the
   grid is far taller than the viewport, so centring it would push it off screen. */
.axis-label-y { grid-area: 3 / 1; align-self: start; }
.axis-label-y span {
  display: block;
  writing-mode: vertical-rl;
  line-height: 1;
}

.axis {
  font-size: 0.7rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.axis-x {
  grid-area: 2 / 3;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  gap: 0 var(--gap);
  align-items: end;
  height: 1.1em;
}
.axis-x .tick { position: relative; }
.axis-x .tick span {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
}
.axis-y {
  grid-area: 3 / 2;
  display: grid;
  grid-auto-rows: var(--cell);
  gap: var(--gap) 0;
  justify-items: end;
  align-items: center;
  line-height: 1;
}
/* Age then calendar year. The fixed age width keeps the years in a clean column. */
.axis-y > div {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.45rem;
  white-space: nowrap;
}
.tick-age { min-width: 1.5em; text-align: right; }
.tick-year { opacity: 0.65; }

.grid {
  grid-area: 3 / 3;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  gap: var(--gap);
}

/* Rows exist for ARIA only; display:contents keeps the cells as grid items. */
.grid-row { display: contents; }

/* Hide the horizontal axis when the columns carry no calendar meaning. */
.board[data-view="years"] .axis-label-x,
.board[data-view="years"] .axis-x { visibility: hidden; height: 0; }

/* --tint is set per cell in app.js from the age ramp. Cells ahead of you stay a
   neutral outline rather than a faded tint: pale stops like yellow wash out to
   nothing at low alpha, so a tinted outline would vanish mid-ramp. */
.cell {
  width: var(--cell);
  height: var(--cell);
  border: 1px solid var(--edge);
  border-radius: 2px;
  background: transparent;
  padding: 0;
  cursor: default;
}
.cell.is-lived {
  background: var(--tint, var(--lived));
  border-color: var(--tint, var(--lived));
}
/* The ring, not a fill, marks today — a fill colour could never stay legible
   against every hue in the ramp. Outline so it doesn't disturb the layout. */
.cell.is-current {
  background: var(--tint, var(--now));
  border-color: var(--tint, var(--now));
  outline: 2px solid var(--ink);
  outline-offset: 1px;
}
.cell:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.board[data-view="months"] .cell { border-radius: 50%; }
.board[data-view="years"] .cell { border-radius: 3px; transform: rotate(45deg); }

/* ── Legend ────────────────────────────────────────────────────────────────── */

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.5rem;
  margin: 1.1rem 0 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 0.85rem;
}
.legend li { display: flex; align-items: center; gap: 0.45rem; }
.key {
  width: 11px; height: 11px;
  border: 1px solid var(--edge);
  border-radius: 2px;
}
.key-current {
  background: var(--muted);
  border-color: var(--muted);
  outline: 2px solid var(--ink);
  outline-offset: 1px;
  margin-right: 2px;
}

.legend-ramp { gap: 0.5rem; }
.ramp {
  width: 110px;
  height: 10px;
  border-radius: 999px;
  /* background-image is set from the RAMP stops in app.js */
}

/* ── Tooltip ───────────────────────────────────────────────────────────────── */

.tooltip {
  position: fixed;
  z-index: 10;
  top: 0; left: 0;
  max-width: 15rem;
  padding: 0.45rem 0.65rem;
  border-radius: 8px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.8rem;
  line-height: 1.4;
  box-shadow: var(--shadow);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s ease;
}
.tooltip.is-visible { opacity: 1; }
.tooltip b { display: block; font-weight: 650; }
.tooltip span { opacity: 0.75; }

/* ── Colophon ──────────────────────────────────────────────────────────────── */

.colophon {
  max-width: 62rem;
  margin: 2.75rem auto 0;
  padding-top: 1.25rem;
  border-top: 1px solid var(--hairline);
  color: var(--muted);
  font-size: 0.85rem;
}
.colophon p { margin: 0 0 0.4rem; }
.fineprint { opacity: 0.8; }

/* ── Small screens ─────────────────────────────────────────────────────────── */

@media (max-width: 900px) {
  body { padding: 1.75rem 1rem 2.5rem; }
  .board { --cell: 9px; --gap: 2px; padding: 1rem; }
  .board[data-view="months"] { --cell: 18px; --gap: 6px; }
  .board[data-view="years"]  { --cell: 22px; --gap: 14px; }
  .field-actions { margin-left: 0; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ── Print ─────────────────────────────────────────────────────────────────── */

@media print {
  :root {
    --bg: #fff; --surface: #fff; --ink: #000; --muted: #444;
    --hairline: #ccc; --edge: #999; --lived: #d7443e; --now: #b8860b;
    --shadow: none;
  }
  body { padding: 0; }
  .controls, .icon-btn, .tooltip, .lede, .notice, .fineprint { display: none !important; }
  .board { border: 0; box-shadow: none; padding: 0; break-inside: avoid; }
  .board-scroll { overflow: visible; }
  .colophon { border-top: 0; }
}
