:root {
  color-scheme: dark;
  --page-background: #0f1216;
  --page-text: #e8eaed;
  --page-muted: #aab2bd;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--page-background);
  color: var(--page-text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
}

/*
 * On a touch screen the throttle is held with a finger for long stretches. A
 * touch that lands on the text or on the track (a finger that slides off the
 * button, a second finger) would start a text selection, and iOS would cover
 * the game with its Copy / Translate / Look Up menu. So nothing is selectable
 * on a touch screen; with a mouse the text stays selectable.
 */
@media (pointer: coarse) {
  body {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
  }
}

/* Opening the lap telemetry must not move the track when a scroll bar appears. */
html {
  scrollbar-gutter: stable;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px;
}

h1 {
  margin: 0 0 12px;
  /* Leave room for the fixed display button when the title is near the edge. */
  padding-inline-start: clamp(0px, calc((1212px - 100vw) / 2), 56px);
  font-size: 1.5rem;
  font-weight: 600;
}

#track {
  display: block;
  width: 100%;
  aspect-ratio: 5 / 2;
  border-radius: 8px;
  background: var(--page-background);
}

noscript {
  display: block;
  margin-top: 12px;
  color: var(--page-muted);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 12px;
}

button {
  min-height: 48px;
  padding: 8px 20px;
  border: 2px solid #aab2bd;
  border-radius: 8px;
  background: #1c222a;
  color: var(--page-text);
  font: inherit;
  cursor: pointer;
}

button:hover {
  background: #252d38;
}

button:focus-visible {
  outline: 3px solid #ffd400;
  outline-offset: 2px;
}

button[aria-pressed="true"] {
  border-color: #ffd400;
  background: #3a3300;
}

.display-menu {
  position: fixed;
  z-index: 1;
  top: 12px;
  left: 12px;
}

#display-toggle {
  display: grid;
  width: 48px;
  height: 48px;
  padding: 0;
  place-items: center;
  font-size: 1.375rem;
  line-height: 1;
}

.display-options {
  max-width: calc(100vw - 24px);
  max-height: calc(100dvh - 80px);
  margin-top: 8px;
  padding: 4px;
  overflow: auto;
  border: 1px solid #aab2bd;
  border-radius: 8px;
  background: rgb(22 27 33 / 94%);
}

.display-options label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 8px 10px;
  cursor: pointer;
}

.display-options label:focus-within {
  outline: 2px solid #ffd400;
  outline-offset: -2px;
  border-radius: 4px;
}

.display-options input {
  width: 18px;
  height: 18px;
  margin: 0;
  flex: none;
}

.display-parameter {
  margin: 4px 10px 8px;
  padding-top: 12px;
  border-top: 1px solid #aab2bd;
}

/* Fixed and out of the flow: showing it does not move the track. */
.frame-meter {
  position: fixed;
  top: 4px;
  right: 4px;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgb(22 27 33 / 85%);
  color: var(--page-muted);
  font-size: 0.65rem;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: pre;
  pointer-events: none;
  display: flex;
  gap: 6px;
}

/* `display` above would otherwise win over the `hidden` attribute. */
.frame-meter[hidden] {
  display: none;
}

@media (max-width: 340px) {
  .frame-meter {
    font-size: 0.58rem;
  }
}

.circuit-choice {
  display: flex;
  align-items: center;
  gap: 8px;
}

select {
  min-height: 48px;
  padding: 8px 12px;
  border: 2px solid #aab2bd;
  border-radius: 8px;
  background: #1c222a;
  color: var(--page-text);
  font: inherit;
}

select:hover {
  background: #252d38;
}

select:focus-visible {
  outline: 3px solid #ffd400;
  outline-offset: 2px;
}

#throttle {
  flex: 1 1 240px;
  min-height: 72px;
  font-size: 1.25rem;
  font-weight: 600;
  /* The button is held down: no scrolling, zooming or text selection. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.help {
  margin: 12px 0 0;
  color: var(--page-muted);
}

kbd {
  padding: 1px 6px;
  border: 1px solid #aab2bd;
  border-radius: 4px;
  background: #1c222a;
  color: var(--page-text);
  font: inherit;
  font-size: 0.9em;
}

.telemetry {
  margin-top: 12px;
}

.telemetry-open {
  display: block;
  margin-top: 12px;
}

/* No display button in this window: nothing to leave room for. */
.telemetry-window h1 {
  padding-inline-start: 0;
}

.lap-note,
.lap-empty {
  margin: 12px 0 0;
  color: var(--page-muted);
}

.lap-table {
  width: 100%;
  margin-top: 12px;
  border-collapse: collapse;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* `display` in the narrow layout would otherwise win over `hidden`. */
.lap-table[hidden] {
  display: none;
}

.lap-table caption {
  font-weight: 600;
  text-align: left;
}

.lap-table th,
.lap-table td {
  padding: 4px;
  font-weight: 400;
}

.lap-table th {
  color: var(--page-muted);
}

.lap-table tr {
  border-bottom: 1px solid #3a4250;
}

.lap-table tbody tr {
  cursor: pointer;
}

.lap-table tbody tr:hover {
  background: #1c222a;
}

.lap-pick {
  min-width: 100%;
  min-height: 32px;
  padding: 2px 4px;
  border-width: 1px;
}

@media (pointer: coarse) {
  .lap-pick {
    min-height: 44px;
  }
}

.lap-chart {
  margin: 16px 0 0;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.lap-chart figcaption {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 24px;
}

/*
 * Both lines take the colors of the sectors: they differ in the dashes, and the
 * reference in lighter shades.
 */
.line-key {
  display: inline-block;
  width: 32px;
  margin-right: 8px;
  vertical-align: middle;
}

.line-key[hidden] {
  display: none;
}

.line-key-chosen {
  border-top: 1px solid var(--page-text);
}

.line-key-reference {
  border-top: 1px dashed var(--page-text);
}

.plot-name {
  margin: 12px 0 6px;
  color: var(--page-muted);
}

.plot {
  display: grid;
  /* The right column is for the second scale; every box leaves it free, so
     that the three stay aligned. */
  grid-template-columns: 48px minmax(0, 1fr) 40px;
}

.plot svg {
  display: block;
  width: 100%;
  height: 330px;
  overflow: visible;
  fill: none;
}

/* The box is stretched to the page: the strokes must keep their width. */
.plot svg * {
  vector-effect: non-scaling-stroke;
}

.plot-scale {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 330px;
  padding-right: 6px;
  color: var(--page-muted);
  font-size: 0.75rem;
  line-height: 1;
  text-align: right;
}

.plot-scale-right {
  padding: 0 0 0 6px;
  text-align: left;
}

.plot-gap {
  stroke: var(--page-text);
  stroke-width: 1;
}

.line-key-gap {
  border-top: 1px solid var(--page-text);
}

.plot-grid {
  stroke: #3a4250;
  stroke-width: 1;
}

/* Their strokes are the gradients, set in the page. */
.plot-chosen {
  stroke-width: 1;
}

.plot-reference {
  stroke-width: 1;
  stroke-dasharray: 6 4;
}

.chart-colors {
  position: absolute;
}

#chart-chosen-colors [data-color="purple"] {
  stop-color: #c060ff;
}

#chart-chosen-colors [data-color="green"] {
  stop-color: #00ff66;
}

#chart-chosen-colors [data-color="yellow"] {
  stop-color: #ffd400;
}

#chart-reference-colors [data-color="purple"] {
  stop-color: #d99bff;
}

#chart-reference-colors [data-color="green"] {
  stop-color: #6dffab;
}

#chart-reference-colors [data-color="yellow"] {
  stop-color: #ffe866;
}

/* Under the boxes, as wide as they are. */
.plot-sectors,
.plot-distance {
  display: flex;
  margin: 0 40px 0 48px;
  color: var(--page-muted);
}

.plot-sectors {
  margin-top: 4px;
  text-align: center;
}

.plot-sectors span {
  flex: 1;
}

.plot-distance {
  justify-content: space-between;
}

/* On a narrow screen the four sectors take a second line of each row. */
@media (max-width: 600px) {
  .lap-table,
  .lap-table caption,
  .lap-table thead,
  .lap-table tbody {
    display: block;
  }

  .lap-table tr {
    display: grid;
    grid-template-columns: repeat(12, minmax(0, 1fr));
    align-items: baseline;
  }

  .lap-table th,
  .lap-table td {
    padding: 2px;
  }

  /* Lap, time, gap and date on the first line, the sectors below them. */
  .lap-table tr > :nth-child(1) {
    grid-area: 1 / 1 / 2 / 3;
  }

  .lap-table tr > :nth-child(2) {
    grid-area: 1 / 3 / 2 / 6;
  }

  .lap-table tr > :nth-child(3) {
    grid-area: 1 / 6 / 2 / 9;
  }

  .lap-table tr > :nth-child(4) {
    grid-area: 2 / 1 / 3 / 4;
  }

  .lap-table tr > :nth-child(5) {
    grid-area: 2 / 4 / 3 / 7;
  }

  .lap-table tr > :nth-child(6) {
    grid-area: 2 / 7 / 3 / 10;
  }

  .lap-table tr > :nth-child(7) {
    grid-area: 2 / 10 / 3 / 13;
  }

  .lap-table tr > :nth-child(8) {
    grid-area: 1 / 9 / 2 / 13;
  }
}

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin: 0 0 8px;
  padding: 0;
}

.tile {
  padding: 8px 12px;
  border: 1px solid #3a4250;
  border-radius: 8px;
  background: #161b21;
}

.tile dt {
  color: var(--page-muted);
  font-size: 0.85rem;
}

.tile dd {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 600;
  /* Digits of equal width, so the running time does not jitter. */
  font-variant-numeric: tabular-nums;
}

#best-sector-laps {
  margin-top: 8px;
  font-size: 0.75rem;
  color: var(--page-muted);
}

.tile .sector-marks {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.sector-marks span {
  flex: 1;
  height: 6px;
  border-radius: 2px;
  background: #ffffff;
}

.sector-marks [data-color="green"] {
  background: #00ff66;
}

.sector-marks [data-color="purple"] {
  background: #c060ff;
}

.sector-marks [data-color="yellow"] {
  background: #ffd400;
}

/*
 * The current lap can read "1:04.213 s (off track)", which wraps onto a second
 * line in a narrow tile. Two lines are always reserved, so that the tile keeps
 * its height and the track below does not move when the car leaves the track.
 */
#current-lap {
  min-height: 3em;
}

.site-footer {
  max-width: 1100px;
  margin: 0 auto;
  padding: 8px 16px 24px;
  text-align: center;
}

.site-footer a {
  color: #8ab4f8;
}

.site-footer a:focus-visible {
  outline: 3px solid #ffd400;
  outline-offset: 2px;
}

.announcer {
  /*
   * The height is reserved for the longest announcements, so that the track
   * does not move down when a message appears or changes. A message wraps onto
   * more lines on a narrow screen, hence the steps. Measured in a browser with
   * the longest sentence and with up to three sentences at once: one line
   * suffices from about 900px, two from about 520px, three below that.
   */
  min-height: 1.5em;
  margin: 0 0 12px;
  color: var(--page-text);
}

@media (max-width: 900px) {
  .announcer {
    min-height: 3em;
  }
}

@media (max-width: 520px) {
  .announcer {
    min-height: 4.5em;
  }
}
