:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-raised: #ffffff;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --wash: rgba(11, 11, 11, 0.04);
  --leg-1: #2a78d6;
  --leg-2: #eb6834;
  --leg-3: #1baf7a;
  --leg-4: #eda100;
  --leg-5: #e87ba4;
  --leg-6: #008300;
  --leg-7: #6250d6;
  --leg-8: #e34948;
  --transfer: #898781;
  --focus: #2a78d6;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.06);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-raised: #222220;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --wash: rgba(255, 255, 255, 0.05);
    --leg-1: #3987e5;
    --leg-2: #d95926;
    --leg-3: #199e70;
    --leg-4: #c98500;
    --leg-5: #d55181;
    --leg-6: #008300;
    --leg-7: #9085e9;
    --leg-8: #e66767;
    --transfer: #898781;
    --focus: #3987e5;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-raised: #222220;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --wash: rgba(255, 255, 255, 0.05);
  --leg-1: #3987e5;
  --leg-2: #d95926;
  --leg-3: #199e70;
  --leg-4: #c98500;
  --leg-5: #d55181;
  --leg-6: #008300;
  --leg-7: #9085e9;
  --leg-8: #e66767;
  --transfer: #898781;
  --focus: #3987e5;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--page); color: var(--ink); font-size: 14px; line-height: 1.4; }
a { color: inherit; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.app {
  display: grid;
  grid-template-columns: 420px minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas: "side map" "side charts";
  height: 100vh;
}

/* ------------------------------------------------------------------ sidebar */
.sidebar {
  grid-area: side;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 20px 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.header-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.eyebrow { margin: 0; color: var(--ink-2); font-size: 12px; letter-spacing: 0.02em; }
h1 { margin: 6px 0 2px; font-size: 26px; line-height: 1.15; font-weight: 650; letter-spacing: -0.01em; }
.subtitle { margin: 0; color: var(--ink-2); }
.unit-toggle { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; padding: 2px; background: var(--page); }
.unit-toggle button { border: 0; background: none; padding: 3px 10px; border-radius: 6px; cursor: pointer; color: var(--ink-2); font-size: 12px; }
.unit-toggle button[aria-pressed="true"] { background: var(--surface-raised); color: var(--ink); box-shadow: 0 0 0 1px var(--border); font-weight: 600; }

.figures { display: flex; flex-direction: column; gap: 12px; }
.hero { display: flex; align-items: baseline; gap: 8px; }
.hero-value { font-size: 52px; font-weight: 650; line-height: 1; letter-spacing: -0.02em; }
.hero-label { color: var(--ink-2); font-size: 15px; }
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile { background: var(--page); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; gap: 2px; }
.tile-label { color: var(--ink-2); font-size: 12px; }
.tile-value { font-size: 17px; font-weight: 600; }

.section-title { margin: 0; font-size: 13px; font-weight: 600; color: var(--ink); }
.section-head { display: flex; align-items: baseline; justify-content: space-between; }
.hint { color: var(--muted); font-size: 12px; }

.leg-list { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.leg-row {
  display: grid; grid-template-columns: 22px 1fr auto; align-items: center; gap: 8px;
  border: 0; background: none; text-align: left; padding: 6px 8px; border-radius: 8px; cursor: pointer;
}
.leg-row:hover { background: var(--wash); }
.leg-row .leg-name { font-weight: 600; }
.leg-row .leg-meta { color: var(--ink-2); font-size: 12px; text-align: right; }
.swatch-line { display: block; height: 4px; border-radius: 2px; width: 22px; }

.day-table { width: 100%; border-collapse: collapse; margin-top: 6px; font-variant-numeric: tabular-nums; }
.day-table th { color: var(--muted); font-weight: 500; font-size: 12px; text-align: left; padding: 4px; border-bottom: 1px solid var(--grid); }
.day-table td { padding: 6px 4px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.day-table .num { text-align: right; }
.day-table tbody tr { cursor: pointer; }
.day-table tbody tr:hover td { background: var(--wash); }
.day-table tbody tr[aria-selected="true"] td { background: var(--wash); font-weight: 600; }
.day-table tbody tr.gap-row td { cursor: default; color: var(--muted); font-size: 12px; padding: 4px 6px; background: none; font-weight: 400; }
.day-cell { display: flex; align-items: center; gap: 8px; }
.day-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.day-name { color: var(--ink-2); font-size: 12px; overflow: hidden; text-overflow: ellipsis; max-width: 62px; }
.day-table tr[aria-selected="true"] .day-name { color: var(--ink); }

.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 11.5px; }

/* ------------------------------------------------------------------ map */
.map-wrap { grid-area: map; position: relative; min-height: 0; }
#map { position: absolute; inset: 0; background: var(--page); }
.map-card {
  position: absolute; top: 12px; left: 12px; max-width: min(340px, calc(100% - 24px));
  background: var(--surface-raised); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  padding: 10px 12px; display: none;
}
.map-card.visible { display: block; }
.map-card .card-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.map-card .card-eyebrow { color: var(--ink-2); font-size: 12px; }
.map-card h3 { margin: 2px 0 6px; font-size: 17px; }
.map-card dl { margin: 0; display: grid; grid-template-columns: repeat(4, auto); gap: 2px 14px; }
.map-card dt { color: var(--ink-2); font-size: 11.5px; }
.map-card dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.map-card .card-links { margin-top: 8px; display: flex; gap: 12px; font-size: 12px; }
.icon-button { border: 0; background: none; cursor: pointer; color: var(--ink-2); font-size: 18px; line-height: 1; padding: 2px 4px; border-radius: 6px; }
.icon-button:hover { background: var(--wash); color: var(--ink); }
.link-button { border: 0; background: none; padding: 0; cursor: pointer; color: var(--ink-2); text-decoration: underline; text-underline-offset: 2px; font-size: 12px; }
.link-button:hover { color: var(--ink); }

.map-key {
  position: absolute; right: 12px; bottom: 28px; background: var(--surface-raised); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 10px; box-shadow: var(--shadow); font-size: 12px; display: flex; flex-direction: column; gap: 4px;
}
.key-row { display: flex; align-items: center; gap: 8px; color: var(--ink); }
.key-line { width: 22px; height: 0; border-top: 3px solid; border-radius: 2px; }
.key-line.transfer { border-top: 2px dashed var(--transfer); }
.map-status { position: absolute; inset: auto 12px 12px 12px; margin: 0; background: var(--surface-raised); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; color: var(--ink-2); }

.day-badge {
  width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center;
  background: var(--surface-raised); color: var(--ink); font-size: 11px; font-weight: 700;
  border: 2.5px solid; box-shadow: 0 0 0 2px var(--surface-raised); cursor: pointer; font-variant-numeric: tabular-nums;
}
.day-badge.dim { opacity: 0.35; }
.day-badge.selected { transform: scale(1.2); }
.finish-badge { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; background: var(--ink); color: var(--surface); font-size: 11px; box-shadow: 0 0 0 2px var(--surface-raised); }
.transfer-label {
  background: var(--surface-raised); border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px;
  font-size: 11.5px; color: var(--ink-2); white-space: nowrap; box-shadow: var(--shadow);
}
.hover-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--ink); box-shadow: 0 0 0 2px var(--surface-raised); pointer-events: none; }
.maplibregl-ctrl-attrib { font-size: 11px; }

/* ------------------------------------------------------------------ charts */
.charts {
  grid-area: charts; background: var(--surface); border-top: 1px solid var(--border);
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); grid-template-rows: auto auto; gap: 4px 28px; padding: 12px 20px 10px;
}
.chart-block-profile { grid-column: 2; grid-row: 1 / span 2; display: flex; flex-direction: column; }
.chart-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.chart-title { margin: 0; font-size: 13px; font-weight: 600; }
.chart-sub { color: var(--muted); font-size: 12px; }
.chart { position: relative; }
.chart-columns { height: 96px; }
.chart-profile { height: 100%; min-height: 200px; flex: 1; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .rest-tick { stroke: var(--axis); stroke-width: 2; stroke-linecap: round; }
.chart .transfer-bracket { stroke: var(--transfer); stroke-width: 1; fill: none; }
.chart .crosshair { stroke: var(--ink-2); stroke-width: 1; }
.chart .day-boundary { stroke: var(--grid); stroke-width: 1; }
.chart .dimmed { opacity: 0.35; }
.chart .label-strong { fill: var(--ink); font-weight: 600; }

.tooltip {
  position: fixed; z-index: 10; pointer-events: none; background: var(--surface-raised); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); padding: 7px 9px; font-size: 12px; min-width: 140px;
}
.tooltip .tt-title { display: flex; align-items: center; gap: 6px; font-weight: 600; margin-bottom: 3px; }
.tooltip .tt-row { display: flex; justify-content: space-between; gap: 14px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.tooltip .tt-row b { color: var(--ink); font-weight: 600; }

/* ------------------------------------------------------------------ narrow */
@media (max-width: 1100px) {
  .charts { grid-template-columns: minmax(0, 1fr); }
  .chart-block-profile { grid-column: 1; grid-row: auto; }
  .chart-profile { height: 200px; flex: none; }
}
@media (max-width: 820px) {
  /* One column: title and totals, then map, charts, legs, the day table. */
  .app { display: flex; flex-direction: column; height: auto; }
  .sidebar { display: contents; }
  .sidebar > * { padding-left: 16px; padding-right: 16px; background: var(--surface); }
  .trip-header { order: 0; padding-top: 16px; }
  .figures { order: 1; padding-top: 12px; padding-bottom: 16px; }
  .map-wrap { order: 2; height: 58vh; min-height: 320px; }
  .charts { order: 3; padding: 12px 16px; }
  .legs { order: 4; padding-top: 16px; }
  .days { order: 5; padding-top: 16px; }
  .sidebar-footer { order: 6; padding-top: 16px; padding-bottom: 16px; }
  .map-key { display: none; }
  .map-card dl { grid-template-columns: repeat(2, auto); }
  .hero-value { font-size: 44px; }
}
@media (max-width: 480px) {
  .day-table .day-name { display: none; }  /* names stay in the day card and tooltips */
}
@media (forced-colors: active) {
  .day-badge, .swatch-line, .day-dot { forced-color-adjust: none; }
}
