*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Light is the default, deliberately regardless of the OS preference: there
   is no prefers-color-scheme rule below, so a dark-mode desktop still gets
   the light page. The dark palette is complete and kept ready — setting
   data-theme="dark" on <html> switches everything over, which is all a
   future theme button has to do. `color-scheme` rides along in each palette
   so native scrollbars, spinners and form controls follow suit. */
:root {
  color-scheme: light;
  --bg: #f6f8fc;
  --panel: #eaeef7;
  --surface: #ffffff;
  --focus: #5b83c9;
  --accent: #d0234a;
  --text: #182033;
  --muted: #5c6780;
  --border: #ccd6e6;
  --shadow: #1b233326;
  --tile-filter: none;

  --c-warmup: #7c8aa0;
  --c-work:   #e03131;
  --c-work2:  #9c1f1f;
  --c-rest:   #1c7ed6;
  --c-cool:   #2f9e44;
}

/* Not applied until something sets data-theme="dark" on <html>. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1a1a2e;
  --panel: #16213e;
  --surface: #142138;
  --focus: #0f3460;
  --accent: #e94560;
  --text: #e0e0e0;
  --muted: #8892a4;
  --border: #2a3a5c;
  --shadow: #0008;
  --tile-filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.7);

  --c-warmup: #9aa8bd;
  --c-work:   #ff6b6b;
  --c-work2:  #c92a2a;
  --c-rest:   #4dabf7;
  --c-cool:   #51cf66;
}

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex: none;
}
header h1 { font-size: 17px; font-weight: 650; letter-spacing: .2px; }
header .sub { color: var(--muted); font-size: 13px; }
header .spacer { flex: 1; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 340px 1fr;
  min-height: 0;
}

aside {
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.main {
  display: grid;
  grid-template-rows: 1fr auto;
  min-height: 0;
  min-width: 0;
}

#map { width: 100%; height: 100%; background: var(--surface); }
.leaflet-tile-pane { filter: var(--tile-filter); }
.leaflet-container { font: inherit; background: var(--surface); }

section.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
}
section.card > h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--muted);
  font-weight: 650;
  margin-bottom: 10px;
}

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }

input[type="number"], input[type="text"] {
  width: 100%;
  padding: 6px 8px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
input[type="number"]:focus, input[type="text"]:focus {
  outline: 2px solid var(--focus);
  outline-offset: -1px;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  white-space: nowrap;   /* three buttons share the Track row; none may wrap */
}
button:hover { border-color: var(--focus); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; }
button:disabled { opacity: .45; cursor: not-allowed; }

.row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.row > * { min-width: 0; }

/* --- interval sets --- */
/* One template for the header and the rows so the columns cannot drift.
   The reps column has to hold two digits *and* the number spinner Chromium
   reveals on hover, which is wider than it looks. */
#sets, .set-head { --set-cols: 66px 1fr 1fr 26px; }
.set {
  display: grid;
  grid-template-columns: var(--set-cols);
  gap: 6px;
  align-items: end;
  margin-bottom: 8px;
}
.set .del {
  padding: 6px 0;
  color: var(--muted);
  border-color: transparent;
  background: transparent;
  line-height: 1;
}
.set .del:hover { color: var(--accent); border-color: var(--border); }
.set-head {
  display: grid;
  grid-template-columns: var(--set-cols);
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .6px;
  margin-bottom: 4px;
}
.presets { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.presets button { padding: 3px 7px; font-size: 12px; color: var(--muted); }
/* A saved preset is one pill split in two: the name applies it, the ✕
   deletes it. Nested buttons are not allowed, hence the wrapper. */
.preset-saved { display: inline-flex; }
.preset-saved button:first-child { border-radius: 6px 0 0 6px; }
.preset-saved .del-preset {
  border-radius: 0 6px 6px 0;
  border-left: none;
  padding: 3px 5px;
  color: var(--muted);
}
.preset-saved .del-preset:hover { color: var(--accent); }
/* Dashed, and in full-strength text, so it reads as the one button in the
   row that makes a pill rather than applying one. */
.presets .save-preset { border-style: dashed; color: var(--text); }

/* --- entry mode + pace --- */
/* Both rows are flex rather than a two-column grid so that hiding one pace
   field lets the other take the whole width instead of leaving a hole. The
   explicit display:none is needed because `hidden` loses to `display:flex`. */
.modes, .paces { display: flex; gap: 8px; margin-bottom: 10px; }
.modes > div, .paces > div { flex: 1 1 0; min-width: 0; }
.paces[hidden], .paces > div[hidden] { display: none; }

.seg { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button {
  flex: 1 1 0;
  border: none;
  border-radius: 0;
  padding: 5px 4px;
  font-size: 12px;
  color: var(--muted);
  background: var(--bg);
}
.seg button + button { border-left: 1px solid var(--border); }
/* Inverting text/surface rather than tinting keeps the selected chip well
   clear of the AA contrast floor in both palettes. */
.seg button[aria-pressed="true"] { background: var(--text); color: var(--surface); font-weight: 650; }

/* A half-typed time is not an error worth shouting about, but the field has
   to say that nothing is being applied yet. */
input.bad, input.bad:focus { border-color: var(--accent); outline-color: var(--accent); }

.check { display: flex; align-items: center; gap: 7px; margin-bottom: 10px; cursor: pointer; }
.check input { flex: none; width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }

/* --- summary --- */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: right; padding: 4px 5px; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
td { font-variant-numeric: tabular-nums; }
tbody tr { cursor: pointer; }
tbody tr:hover, tbody tr.hi { background: color-mix(in srgb, var(--focus) 18%, transparent); }
tfoot td { font-weight: 650; border-bottom: none; padding-top: 8px; }
.swatch {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 2px;
  margin-right: 6px;
  vertical-align: baseline;
}

.totals { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; font-size: 13px; }
.totals dt { color: var(--muted); }
.totals dd { text-align: right; font-variant-numeric: tabular-nums; }

.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; }

.warn {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  border: 1px solid var(--accent);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
}
.hint { font-size: 12px; color: var(--muted); }
.hint.alert { color: var(--accent); }
.hint:empty { display: none; }

/* --- elevation strip --- */
.elev {
  flex: none;
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 6px 10px 2px;
  position: relative;
}
.elev svg { display: block; width: 100%; height: 110px; touch-action: none; }
.elev .cursor-line { stroke: var(--text); stroke-width: 1; stroke-dasharray: 3 3; opacity: .7; }
.elev-readout {
  position: absolute;
  top: 6px; right: 12px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  background: var(--panel);
  padding: 1px 4px;
  pointer-events: none;
}
.elev-empty { font-size: 12px; color: var(--muted); padding: 8px 2px; }

/* --- rep number badges on the map --- */
.rep-badge {
  background: var(--c-work);
  color: #fff;
  border: 1.5px solid #fff;
  border-radius: 50%;
  width: 20px; height: 20px;
  font-size: 11px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
  box-shadow: 0 1px 3px #0006;
}
/* The two ends of the interval block. Green where the reps begin, accent where
   they finish; both drag, and both set the warm-up. */
.start-badge, .end-badge {
  background: var(--c-cool);
  border: 2px solid #fff;
  border-radius: 50%;
  width: 16px; height: 16px;
  box-shadow: 0 1px 3px #0006;
  cursor: grab;
}
.end-badge { background: var(--accent); }

/* --- route library --- */
dialog#library {
  width: min(520px, 92vw);
  max-height: min(560px, 86vh);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 40px var(--shadow);
  overflow: hidden;
}
dialog#library[open] { display: flex; flex-direction: column; }
dialog#library::backdrop { background: #0007; }

.lib-head, .lib-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--panel);
  flex: none;
}
.lib-head { border-bottom: 1px solid var(--border); }
.lib-foot { border-top: 1px solid var(--border); }
.lib-head h2 {
  flex: 1;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--muted);
  font-weight: 650;
}
.lib-foot .hint { flex: 1; }
.lib-body { overflow-y: auto; padding: 8px; min-height: 0; }
.lib-body .hint { padding: 6px 4px; }

.lib-row { display: flex; gap: 4px; margin-bottom: 2px; }
.lib-open {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 8px 10px;
  text-align: left;
  background: transparent;
  border-color: transparent;
}
.lib-open .name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 600;
}
.lib-open .meta { flex: none; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.lib-open:hover { background: color-mix(in srgb, var(--focus) 16%, transparent); border-color: transparent; }
.lib-row.current .lib-open { background: color-mix(in srgb, var(--focus) 26%, transparent); }
.lib-del { flex: none; background: transparent; border-color: transparent; color: var(--muted); }
.lib-del:hover { color: var(--accent); border-color: var(--border); }

/* --- drop zone --- */
#drop {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--focus) 35%, transparent);
  border: 3px dashed var(--focus);
  display: none;
  place-items: center;
  font-size: 20px;
  font-weight: 600;
  z-index: 9999;
  pointer-events: none;
}
body.dragging #drop { display: grid; }

.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 10px;
  text-align: center;
  color: var(--muted);
  pointer-events: none;
  z-index: 500;
}
.empty[hidden] { display: none; }
.empty strong { color: var(--text); font-size: 18px; }
.maparea { position: relative; min-height: 0; }

@media (max-width: 860px) {
  body { overflow: auto; height: auto; }
  .layout { grid-template-columns: 1fr; }
  aside { border-right: none; border-bottom: 1px solid var(--border); overflow: visible; }
  .maparea { height: 60vh; }
  .main { grid-template-rows: auto auto; }
}
