:root {
  color-scheme: light dark;
  --bg: light-dark(#fbfaf8, #121211);
  --fg: light-dark(#1b1a18, #ecebe7);
  --muted: light-dark(#67645e, #a09d96);
  --edge: light-dark(#8f8b84, #6f6c66);
  --line: light-dark(#e6e3de, #2c2b28);
  --accent: light-dark(#c2410c, #ff8a50);
  --on-accent: light-dark(#fff, #121211);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  line-height: 1.5;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
}

main {
  max-width: 34rem;
  margin: 0 auto;
  padding: 2rem 1rem 4rem;
}

h1 {
  font-size: 1.75rem;
  line-height: 1.15;
  letter-spacing: -0.02em;
  margin: 0;
}

.lede {
  margin: 0.5rem 0 2rem;
  color: var(--muted);
}

fieldset,
.boxes {
  border: 0;
  margin: 0;
  padding: 0;
  min-width: 0;
}

legend,
h2,
caption {
  padding: 0;
  margin: 0 0 0.5rem;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-align: start;
}

label[for] {
  font-weight: 600;
}

.picker {
  display: flex;
  /* Long labels, such as Spanish's Media maratón, go to a second row on narrow screens. */
  flex-wrap: wrap;
  column-gap: 1.5rem;
  margin-bottom: 2rem;
  border-bottom: 1px solid var(--line);
}

/* The radio covers its label, so the whole label is the click and focus target. */
.picker label {
  position: relative;
  display: flex;
  align-items: center;
  min-height: 48px;
  border-bottom: 3px solid transparent;
  margin-bottom: -1px;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}

.picker label:hover,
.picker label:has(:checked) {
  color: var(--fg);
}

.picker label:has(:checked) {
  border-color: var(--accent);
}

.picker input {
  appearance: none;
  position: absolute;
  /* Wider than the label, so the focus ring doesn't touch the text. */
  inset: 0 -0.25rem;
  margin: 0;
  cursor: pointer;
}

/* High-contrast modes repaint borders, so mark the choice with the system's selection colour. */
@media (forced-colors: active) {
  .picker label,
  .picker label:hover,
  .picker label:has(:checked) {
    forced-color-adjust: none;
    border-color: Canvas;
    color: CanvasText;
  }

  .picker label:has(:checked) {
    border-color: Highlight;
  }
}

/* Read by screen readers only: the box's value as a time, since its field holds bare digits. */
.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.box + .box {
  margin-top: 1rem;
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.tag {
  color: var(--muted);
  font-size: 0.875rem;
}

.goal .tag {
  color: var(--accent);
  font-weight: 600;
}

input:focus-visible {
  outline: 2px solid var(--fg);
  outline-offset: 2px;
}

/* The box is drawn under the real input, which is invisible but takes every tap and key. */
.clock {
  position: relative;
  margin-top: 0.25rem;
}

.pairs {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 0.25rem;
  padding: 0.5rem 0.75rem 0.25rem;
  border: 2px solid var(--edge);
  border-radius: 0.75rem;
  font-family: ui-monospace, monospace;
  font-size: 2rem;
  line-height: 1.2;
}

.pair {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.pair small {
  color: var(--muted);
  font-family: system-ui, sans-serif;
  font-size: 0.75rem;
}

.unit {
  align-self: center;
  color: var(--muted);
  font-family: system-ui, sans-serif;
  font-size: 1rem;
}

/* Zeros the user hasn't typed, so 00:00:45 shows which digits are theirs. */
.zeros {
  color: var(--muted);
}

.clock.selected:has(:focus) .zeros + span {
  background: var(--accent);
  color: var(--on-accent);
}

@media (forced-colors: active) {
  .clock.selected:has(:focus) .zeros + span {
    forced-color-adjust: none;
    background: Highlight;
    color: HighlightText;
  }
}

/* Hidden rather than removed, so the digits don't shift when it appears. A border, because
   high-contrast modes drop backgrounds. */
.caret {
  height: 1em;
  margin-top: 0.1em;
  border-left: 2px solid var(--fg);
  visibility: hidden;
}

.clock:not(.selected):has(:focus) .caret {
  visibility: visible;
}

.clock input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  border: 0;
  opacity: 0;
  /* iOS zooms into inputs under 16px when they get focus. */
  font-size: 1rem;
}

.clock:has(:focus-visible) .pairs {
  outline: 2px solid var(--fg);
  outline-offset: 2px;
}

.clock:has([aria-invalid='true']) .pairs {
  border-color: var(--accent);
}

#help,
#message {
  margin: 0.25rem 0 0;
  font-size: 0.875rem;
  color: var(--muted);
}

/* Holds a line even when empty, so an error appearing doesn't push the results down. */
#message {
  min-height: 1lh;
  color: var(--accent);
}

section,
table {
  margin-top: 2.5rem;
}

dl {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin: 0;
  padding-top: 0.75rem;
  border-top: 1px solid var(--line);
}

dl div + div {
  padding-left: 1.25rem;
  border-left: 1px solid var(--line);
}

dt,
dd + dd {
  color: var(--muted);
  font-size: 0.875rem;
}

dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.time {
  font-size: 1.75rem;
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

/* The pace, the number a runner compares with their own. */
.time + dd {
  color: var(--fg);
  font-size: 1rem;
}

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

th {
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 500;
}

th,
td {
  padding: 0.375rem 0;
  text-align: end;
  border-bottom: 1px solid var(--line);
}

th:first-child,
td:first-child {
  text-align: start;
}

td:first-child {
  color: var(--muted);
}

td:last-child {
  font-weight: 600;
}

/* A firmer rule every 5 km, to keep your place down a marathon's 43 rows. */
tr:nth-child(5n) td {
  border-bottom-color: var(--edge);
}

@media (min-width: 48rem) {
  main {
    padding-top: 5rem;
  }

  h1 {
    font-size: 2.25rem;
  }
}

/* Wide screens put the splits beside the goal, so a whole marathon's splits are in view while you
   change it. */
@media (min-width: 64rem) {
  main {
    display: grid;
    grid-template-columns: 32rem minmax(0, 1fr);
    align-items: start;
    column-gap: 4rem;
    max-width: 72rem;
  }

  table {
    margin-top: 0;
  }
}

/* Only when the goal and the records fit the window (they take about 44rem), or their bottom would
   stay hidden until the end of the splits. */
@media (min-width: 64rem) and (min-height: 47rem) {
  .controls {
    position: sticky;
    top: 2rem;
  }
}
