/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --hover: rgba(42, 120, 214, 0.09);
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series: #2a78d6;
  --series-hover: #1c5cab;
  --good: #006300;
  --focus: #2a78d6;

  /* Sequential heat ramp for daily miles (one hue, light -> dark) */
  --h0: #efeeea; --h1: #b7d3f6; --h2: #86b6ef; --h3: #5598e7; --h4: #2a78d6; --h5: #184f95;
  --hi0: #898781; --hi1: #0b0b0b; --hi2: #0b0b0b; --hi3: #0b0b0b; --hi4: #ffffff; --hi5: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --hover: rgba(57, 135, 229, 0.16);
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series: #3987e5;
    --series-hover: #6da7ec;
    --good: #0ca30c;
    --focus: #6da7ec;
    --h0: #262624; --h1: #104281; --h2: #1c5cab; --h3: #2a78d6; --h4: #5598e7; --h5: #9ec5f4;
    --hi0: #898781; --hi1: #ffffff; --hi2: #ffffff; --hi3: #ffffff; --hi4: #0b0b0b; --hi5: #0b0b0b;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232322; --hover: rgba(57, 135, 229, 0.16);
  --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781; --grid: #2c2c2a; --axis: #383835;
  --border: rgba(255, 255, 255, 0.10); --series: #3987e5; --series-hover: #6da7ec; --good: #0ca30c; --focus: #6da7ec;
  --h0: #262624; --h1: #104281; --h2: #1c5cab; --h3: #2a78d6; --h4: #5598e7; --h5: #9ec5f4;
  --hi0: #898781; --hi1: #ffffff; --hi2: #ffffff; --hi3: #ffffff; --hi4: #0b0b0b; --hi5: #0b0b0b;
}

.b0 { --bg: var(--h0); --fg: var(--hi0); }
.b1 { --bg: var(--h1); --fg: var(--hi1); }
.b2 { --bg: var(--h2); --fg: var(--hi2); }
.b3 { --bg: var(--h3); --fg: var(--hi3); }
.b4 { --bg: var(--h4); --fg: var(--hi4); }
.b5 { --bg: var(--h5); --fg: var(--hi5); }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
[hidden] { display: none !important; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.app { display: grid; grid-template-rows: auto minmax(0, 1fr); height: 100vh; height: 100dvh; }

/* ---------- Header ---------- */
.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 20px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--grid);
}
.brand { font-weight: 700; letter-spacing: -0.01em; }
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; flex: 1; min-width: 0; color: var(--ink-2); }
.crumbs a { color: var(--ink-2); text-decoration: none; padding: 3px 6px; border-radius: 6px; }
.crumbs a:hover { background: var(--hover); color: var(--ink); }
.crumbs [aria-current] { color: var(--ink); font-weight: 600; padding: 3px 6px; }
.crumbs .sep { color: var(--muted); }
.controls { display: flex; align-items: center; gap: 12px; }
.levels { display: flex; background: var(--surface-2); border-radius: 9px; padding: 3px; }
.levels button {
  border: 0; background: none; padding: 5px 10px; border-radius: 7px; cursor: pointer; color: var(--ink-2); font-size: 13px;
}
.levels button:hover:not(:disabled) { color: var(--ink); }
.levels button[aria-pressed="true"] { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 0 0 1px var(--border); }
.levels button:disabled { opacity: 0.35; cursor: default; }
.stepper { display: flex; gap: 4px; }
.icon-btn {
  width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface);
  font-size: 20px; line-height: 1; cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--hover); }
.icon-btn:disabled { opacity: 0.3; cursor: default; }

/* ---------- Layout ---------- */
main { display: grid; grid-template-columns: minmax(0, 1fr) 380px; min-height: 0; }
#canvas { position: relative; overflow: hidden; min-height: 0; }
#panel { overflow: auto; background: var(--surface); border-left: 1px solid var(--grid); padding: 20px 20px 32px; }

.view {
  position: absolute; inset: 0; overflow: auto; padding: 16px 24px 24px;
  display: flex; flex-direction: column; gap: 12px; container-type: size;
}
.view-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; color: var(--muted); font-size: 12px; }
.hint { color: var(--muted); }
.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; background: var(--bg); display: inline-block; }
.legend .lab { margin-right: 6px; white-space: nowrap; }

.zoomable { cursor: zoom-in; }

/* ---------- All time ---------- */
.all-svg { flex: 1; min-height: 0; width: 100%; }
.year-row .row-bg { fill: transparent; }
.year-row:hover .row-bg, .year-row:focus-visible .row-bg { fill: var(--hover); }
.year-row:focus-visible { outline: none; }
.year-row .cell { fill: var(--bg); }
.year-row .cell.today { stroke: var(--ink); stroke-width: 1.5; }
.t-year { font-size: 15px; font-weight: 650; fill: var(--ink); }
.t-mon { font-size: 9px; fill: var(--muted); }
.t-tot { font-size: 16px; font-weight: 650; fill: var(--ink); }
.t-sub { font-size: 11px; fill: var(--muted); }

/* ---------- Year ---------- */
.year-grid {
  --cols: 4; --rows: 3; --g: 3px; --gap: 12px; --pad: 10px;
  --c: max(9px, min(
    (100cqw - 48px - var(--cols) * (2 * var(--pad) + 6 * var(--g)) - (var(--cols) - 1) * var(--gap)) / (var(--cols) * 7),
    (100cqh - 80px - var(--rows) * (2 * var(--pad) + 26px + 5 * var(--g)) - (var(--rows) - 1) * var(--gap)) / (var(--rows) * 6)
  ));
  display: grid; grid-template-columns: repeat(var(--cols), max-content); gap: var(--gap);
  justify-content: center; align-content: start;
}
.mini-month { padding: var(--pad); border-radius: 10px; }
.mini-month:hover, .mini-month.sib { background: var(--hover); }
.mm-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; height: 26px; }
.mm-name { font-weight: 600; }
.mm-season { color: var(--muted); font-size: 11px; margin-left: 4px; font-weight: 400; }
.mm-mi { color: var(--ink-2); font-size: 12px; font-variant-numeric: tabular-nums; }
.mm-grid { display: grid; grid-template-columns: repeat(7, var(--c)); grid-template-rows: repeat(6, var(--c)); gap: var(--g); }
.mm-grid i { display: block; border-radius: 3px; background: var(--bg); }
.mm-grid i.blank { background: none; }

.today { box-shadow: inset 0 0 0 2px var(--ink); }
.future { opacity: 0.35; }
.outside { opacity: 0.35; }

/* ---------- Season ---------- */
.season-grid {
  /* day cells stay roughly square: limited by both the available width and height */
  --ch: max(28px, min(1.4 * (100cqw - 48px - 24px - 72px - 72px) / 21, (100cqh - 176px) / 6));
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; align-items: start;
}
.month-block { display: flex; flex-direction: column; min-height: 0; padding: 12px; border-radius: 12px; }
.month-block:hover { background: var(--hover); }
.mb-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; gap: 8px; }
.mb-head h3 { margin: 0; font-size: 16px; }
.mb-head span { color: var(--ink-2); font-size: 12px; font-variant-numeric: tabular-nums; }
.cal {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: auto repeat(6, var(--ch)); gap: 4px;
}
.wd { font-size: 10px; color: var(--muted); text-align: center; }
.cell {
  position: relative; border-radius: 6px; background: var(--bg); color: var(--fg);
  display: flex; align-items: center; justify-content: center; font-weight: 600; font-size: 13px;
  font-variant-numeric: tabular-nums; min-width: 0;
}
.cell.blank { background: none; }
.cell .dnum { position: absolute; top: 3px; left: 5px; font-size: 9px; font-weight: 400; opacity: 0.75; }
.b0 .dnum { opacity: 1; }

/* ---------- Month ---------- */
.month-cal { flex: 1; min-height: 0; display: grid; grid-template-rows: auto repeat(var(--weeks), minmax(64px, 1fr)); gap: 4px; }
.wk-head, .wk-row { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)) 76px; gap: 6px; }
.wk-head div { font-size: 11px; color: var(--muted); padding: 0 4px; }
.wk-head div:last-child { text-align: right; }
.wk-row { padding: 4px; border-radius: 10px; }
.wk-row:hover { background: var(--hover); }
.mcell {
  border-radius: 8px; padding: 6px 8px; background: var(--bg); color: var(--fg);
  display: flex; flex-direction: column; gap: 1px; min-width: 0; overflow: hidden;
}
.mcell .dnum { font-size: 12px; opacity: 0.8; margin-bottom: auto; }
.mcell .chip { font-size: 15px; font-weight: 650; white-space: nowrap; font-variant-numeric: tabular-nums; }
.mcell .chip small { font-size: 11px; font-weight: 400; opacity: 0.85; }
.wk-total { align-self: center; text-align: right; font-variant-numeric: tabular-nums; padding-right: 4px; }
.wk-total b { display: block; font-size: 15px; }
.wk-total span { color: var(--muted); font-size: 11px; }

/* ---------- Week ---------- */
.week-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 10px; align-items: stretch; }
.day-col { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px; display: flex; flex-direction: column; gap: 8px; min-width: 0; min-height: 240px; container-type: inline-size; }
.day-col.today { box-shadow: inset 0 0 0 2px var(--ink); }
.dc-head { display: flex; justify-content: space-between; align-items: baseline; color: var(--ink-2); font-size: 12px; }
.dc-head b { color: var(--ink); font-size: 18px; }
.rest { color: var(--muted); font-size: 12px; margin-top: 4px; }
.run-card { border-radius: 10px; padding: 10px; background: var(--bg); color: var(--fg); display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.run-card:hover { filter: brightness(0.95); }
.rc-mi { font-size: clamp(15px, 16cqi, 22px); font-weight: 700; line-height: 1.1; white-space: nowrap; }
.rc-mi small { font-size: 12px; font-weight: 500; }
.rc-stats { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 1px 8px; margin: 6px 0 0; font-size: 12px; }
.rc-stats dt { opacity: 0.75; }
.rc-stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rc-stats .u { display: none; opacity: 0.75; }
/* Narrow day columns: drop the labels and let the values carry their units. */
@container (max-width: 125px) {
  .run-card { padding: 8px; }
  .rc-stats { grid-template-columns: minmax(0, 1fr); }
  .rc-stats dt { display: none; }
  .rc-stats dd { text-align: left; }
  .rc-stats .u { display: inline; }
}
.tag { font-size: 10px; text-transform: uppercase; letter-spacing: 0.04em; opacity: 0.8; }

/* ---------- Run ---------- */
.run-view { gap: 20px; }
.run-wrap { width: 100%; max-width: 920px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }
.rh-date { color: var(--ink-2); }
.hero { font-size: 56px; font-weight: 700; line-height: 1; letter-spacing: -0.02em; }
.hero small { font-size: 20px; font-weight: 500; color: var(--ink-2); letter-spacing: 0; }
.rh-sub { color: var(--ink-2); margin-top: 6px; }
.section h4 { margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.section .note { color: var(--muted); font-size: 12px; margin-top: 4px; }
.chart-svg { width: 100%; height: auto; display: block; }

/* ---------- Tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
#panel .tiles { grid-template-columns: 1fr 1fr; }
.tile { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.run-view .tile { background: var(--surface); border: 1px solid var(--border); }
.tile.zoomable { cursor: pointer; }
.tile.zoomable:hover { background: var(--hover); }
.t-label { font-size: 12px; color: var(--ink-2); }
.t-value { font-size: 20px; font-weight: 650; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t-value small { font-size: 12px; font-weight: 500; color: var(--ink-2); }
.t-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.delta { font-size: 12px; color: var(--ink-2); margin-top: 2px; }
.delta.good { color: var(--good); }

/* ---------- Panel ---------- */
.p-eyebrow { font-size: 12px; color: var(--muted); }
#panel h2 { margin: 2px 0 16px; font-size: 22px; letter-spacing: -0.01em; }
.hero-block { margin-bottom: 16px; }
.hero-block .t-label { margin-bottom: 2px; }
.card { margin-top: 20px; }
.card h4 { margin: 0 0 6px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.facts { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.facts li { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }

/* Charts */
.grid-line { stroke: var(--grid); stroke-width: 1; }
.axis-line { stroke: var(--axis); stroke-width: 1; }
.tick { font-size: 10px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.val { font-size: 10px; fill: var(--ink-2); font-weight: 600; }
.bar .b { fill: var(--series); }
.bar:hover .b, .bar:focus-visible .b { fill: var(--series-hover); }
.bar:focus-visible { outline: none; }
.bar.zoomable { cursor: pointer; }
.line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.dot { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
.avg-line { stroke: var(--axis); stroke-width: 1; }
.moving { fill: var(--series); }
.paused { fill: var(--h0); }

/* Runs table */
details.runs { margin-top: 20px; }
details.runs summary { cursor: pointer; font-size: 13px; font-weight: 600; color: var(--ink-2); padding: 4px 0; }
.runs table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 6px; }
.runs th { text-align: left; color: var(--muted); font-weight: 500; padding: 4px 6px; border-bottom: 1px solid var(--grid); }
.runs td { padding: 5px 6px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.runs .num { text-align: right; font-variant-numeric: tabular-nums; }
.runs tr.zoomable { cursor: pointer; }
.runs tbody tr:hover { background: var(--hover); }
.runs tr.cur { background: var(--hover); font-weight: 600; }
.btn-link { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 12px; margin-top: 24px; }

/* ---------- Tooltip & overlays ---------- */
.tip {
  position: fixed; z-index: 10; pointer-events: none; max-width: 280px;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14); border-radius: 8px; padding: 8px 10px; font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.tip b { display: block; margin-bottom: 2px; }
.tip div { color: var(--ink-2); }
.drop-overlay {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center;
  background: rgba(42, 120, 214, 0.12); backdrop-filter: blur(2px);
}
.drop-overlay div { background: var(--surface); padding: 20px 28px; border-radius: 14px; font-weight: 600; border: 2px dashed var(--series); }
.empty { margin: auto; max-width: 420px; text-align: center; color: var(--ink-2); }
.empty h2 { color: var(--ink); }
.empty button { margin-top: 12px; padding: 8px 16px; border-radius: 8px; border: 0; background: var(--series); color: #fff; font-weight: 600; cursor: pointer; }

/* ---------- Small screens ---------- */
@media (max-width: 900px) {
  .app { display: block; height: auto; }
  main { display: block; }
  #canvas { height: 76svh; }
  #panel { border-left: 0; border-top: 1px solid var(--grid); padding: 16px; }
  .view { padding: 12px 16px 16px; }
  .top { padding: 10px 16px; }
  .hint { display: none; }
}
@media (max-width: 640px) {
  .controls { width: 100%; justify-content: space-between; }
  .levels button { padding: 5px 7px; }
  .year-grid { --cols: 3; --c: max(9px, (100cqw - 16px - 3 * (2 * var(--pad) + 6 * var(--g)) - 2 * var(--gap)) / 21); --pad: 6px; --gap: 6px; --g: 2px; }
  .mm-season { display: none; }
  .mm-head { font-size: 12px; }
  .season-grid { grid-template-columns: 1fr; }
  .season-grid { --ch: 40px; }
  .wk-head, .wk-row { grid-template-columns: repeat(7, minmax(0, 1fr)) 50px; gap: 3px; }
  .wk-total { white-space: nowrap; padding-right: 0; }
  .mcell { padding: 4px; }
  .mcell .chip { font-size: 12px; }
  .mcell .chip small { display: none; }
  .wk-total b { font-size: 12px; letter-spacing: -0.02em; }
  .week-grid { grid-template-columns: 1fr; }
  .day-col { flex-direction: row; flex-wrap: wrap; align-items: center; }
  .dc-head { width: 64px; flex-direction: column; }
  .run-card { flex: 1; min-width: 160px; }
  .hero { font-size: 44px; }
}
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
