/* RaceMe — deliberately single-theme: a night-time handheld screen. */
:root {
  color-scheme: dark;
  --ground: #0d0e1a;
  --panel: #151829;
  --line: #2c3152;
  --rim: #5b6190;
  --text: #e8e6d9;
  --dim: #8a8fb3;
  --duck: #ffd23f;
  --hot: #ff6fae;
  --pixel: "Press Start 2P", "Courier New", monospace;
  --term: "VT323", "Courier New", monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--text);
  font-family: var(--term);
  font-size: 20px;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: none;
  padding: env(safe-area-inset-top, 0px) 16px env(safe-area-inset-bottom, 0px);
}

button { font: inherit; color: inherit; }

.app {
  max-width: 480px;
  margin: 0 auto;
  padding-block: 14px 28px;
  display: grid;
  gap: 14px;
}

/* ---------- header ---------- */
/* Logo and "+ GPX" on one row; the race name gets the full width below. */
.top {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px 12px;
}
.top h1 {
  margin: 0;
  font-family: var(--pixel);
  font-size: 22px;
  letter-spacing: 1px;
  color: var(--text);
}
.top h1 span { color: var(--duck); }
.race-meta {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--dim);
  font-size: 17px;
  letter-spacing: 0.5px;
}
/* Race time either side of the scrub bar, like a video player. */
.timeline { display: flex; align-items: center; gap: 10px; }
.timeline #scrub { flex: 1; min-width: 0; }
.clock, .total {
  font-family: var(--pixel);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  min-width: 7ch;
}
.clock { color: var(--duck); }
.total { color: var(--dim); text-align: right; }

/* Two-line upload button: why (RACE MY RUNS) + what happens (upload GPX). */
.upload-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: 2px solid var(--hot);
  box-shadow: 0 3px 0 #6b2c4a;
  color: var(--hot);
  cursor: pointer;
}
.upload-btn:active { transform: translateY(3px); box-shadow: none; }
.upload-icon { width: 21px; height: 21px; image-rendering: pixelated; flex: none; }
.upload-text { display: grid; gap: 4px; line-height: 1; }
.upload-text b { font-family: var(--pixel); font-size: 10px; font-weight: normal; white-space: nowrap; }
.upload-text small { font-size: 16px; color: var(--dim); }

/* ---------- screen ---------- */
.screen {
  position: relative;
  border: 4px solid var(--rim);
  box-shadow: 0 0 0 4px var(--ground), 0 0 0 8px var(--line);
  background: var(--ground);
  line-height: 0;
  margin: 4px;
}
/* Exact size is set from JS (whole device pixels per game pixel):
   a square map plus the timing panel below it. */
#track {
  width: 100%;
  max-width: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  display: block;
  margin: 0 auto;
}
.ticker {
  position: absolute;
  left: 0;
  right: 0;
  top: 10px;
  text-align: center;
  font-family: var(--pixel);
  font-size: 11px;
  line-height: 1.6;
  color: var(--ground);
  pointer-events: none;
  opacity: 0;
}
.ticker.show {
  opacity: 1;
  animation: blink 0.5s steps(1) 3;
}
.ticker:not(:empty) {
  background: var(--duck);
  margin: 0 auto;
  width: fit-content;
  max-width: calc(100% - 20px);
  padding: 4px 8px;
}
.countdown {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--pixel);
  font-size: clamp(36px, 14vw, 64px);
  line-height: 1;
  color: var(--duck);
  text-shadow: 4px 4px 0 var(--ground);
  pointer-events: none;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------- focus note + picker ---------- */
.focus-note {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 17px;
  line-height: 1.1;
  color: var(--dim);
}
.focus-note .chip { border-color: var(--duck); color: var(--duck); flex: none; }
.picker-card { max-height: calc(100vh - 32px); grid-template-rows: auto 1fr auto; }
.picker-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.pick-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; min-height: 0; display: grid; gap: 2px; }
.pick-list label {
  display: grid;
  grid-template-columns: 24px 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  cursor: pointer;
}
.pick-list input { width: 18px; height: 18px; margin: 0; accent-color: var(--duck); }
.pick-list input:disabled + .name { color: var(--dim); }
.pick-list .name { font-family: var(--pixel); font-size: 10px; }
.pick-list .tag { font-family: var(--pixel); font-size: 8px; color: var(--duck); }
.pick-list .tag.dim { color: var(--dim); }
.pick-list .time { font-size: 20px; font-variant-numeric: tabular-nums; }
.btn:disabled { opacity: 0.4; cursor: default; }
.role { font-family: var(--pixel); font-size: 7px; color: var(--duck); margin-left: 6px; vertical-align: middle; }
.role:empty { display: none; }

/* ---------- controls ---------- */
.deck { display: grid; gap: 14px; }

#scrub {
  width: 100%;
  margin: 0;
  accent-color: var(--duck);
  height: 28px;
}

.controls { display: flex; gap: 10px; }
.btn {
  flex: 1;
  font-family: var(--pixel);
  font-size: 13px;
  padding: 16px 10px;
  background: var(--panel);
  border: 3px solid var(--rim);
  box-shadow: 0 4px 0 var(--line);
  cursor: pointer;
  touch-action: manipulation;
}
.btn:active { transform: translateY(4px); box-shadow: none; }
.btn.primary {
  flex: 1.6;
  background: var(--duck);
  border-color: var(--duck);
  color: var(--ground);
  box-shadow: 0 4px 0 #b08b12;
}
.btn.primary.live { background: var(--text); border-color: var(--text); box-shadow: 0 4px 0 var(--rim); }

.toggles {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 10px;
}
.speed { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-family: var(--pixel);
  font-size: 10px;
  padding: 9px 9px;
  background: transparent;
  border: 2px solid var(--line);
  color: var(--dim);
  cursor: pointer;
  touch-action: manipulation;
}
.chip[aria-pressed="true"] { border-color: var(--duck); color: var(--duck); }

.chip.sound { line-height: 0; padding: 7px 10px; }
.chip.sound img { width: 33px; height: 24px; image-rendering: pixelated; }

.avatars { display: flex; align-items: center; gap: 10px; }
.avatars .label { font-family: var(--pixel); font-size: 10px; letter-spacing: 1px; color: var(--dim); }
.avatar-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip.avatar { padding: 5px 7px; line-height: 0; }
.chip.avatar img { width: 26px; height: 22px; image-rendering: pixelated; }

button:focus-visible, .board li:focus-visible {
  outline: 2px dashed var(--hot);
  outline-offset: 3px;
}

/* ---------- import ---------- */
.source {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 2px dashed var(--line);
}
.source h2 {
  margin: 0;
  font-family: var(--pixel);
  font-size: 11px;
  letter-spacing: 1px;
  color: var(--dim);
}
/* The real file input sits invisibly on top of its button/link, so the tap
   lands on the input itself — label/click() tricks fail in some mobile webviews. */
.file-pick { position: relative; overflow: hidden; display: inline-block; }
.gpx-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  font-size: 100px; /* big hit area for the native control inside */
}
.file-pick:has(.gpx-input:focus-visible) { outline: 2px dashed var(--hot); outline-offset: 3px; }
.file-pick.link { color: var(--hot); text-decoration: underline; vertical-align: bottom; }
.file-pick.upload-btn { display: flex; }
.examples { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.examples .label { font-family: var(--pixel); font-size: 10px; letter-spacing: 1px; color: var(--dim); margin-right: 4px; }
.status {
  margin: 0;
  font-size: 18px;
  line-height: 1.15;
  color: var(--duck);
  text-transform: uppercase;
}
.status:empty { display: none; }
.status.error { color: var(--hot); }
.hint { margin: 0; font-size: 16px; line-height: 1.15; color: var(--dim); }
.hint b { color: var(--text); font-weight: normal; }

/* ---------- standings ---------- */
.board-wrap h2 {
  margin: 4px 0 8px;
  font-family: var(--pixel);
  font-size: 11px;
  letter-spacing: 1px;
  color: var(--dim);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.board-wrap h2 small {
  font-family: var(--term);
  font-size: 16px;
  letter-spacing: 0;
}
.board {
  list-style: none;
  margin: 0;
  padding: 0;
  position: relative;
}
.board li {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 30px;
  display: grid;
  grid-template-columns: 26px 36px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  border-left: 3px solid transparent;
  transition: transform 0.35s steps(6);
  cursor: pointer;
}
.board .pos { font-family: var(--pixel); font-size: 11px; color: var(--dim); }
.board img { width: auto; height: 22px; image-rendering: pixelated; }
.board .name { font-family: var(--pixel); font-size: 11px; }
.board .stat { font-size: 22px; font-variant-numeric: tabular-nums; color: var(--dim); }
.board li.lead .pos, .board li.lead .stat { color: var(--duck); }
.board li.done .stat { color: var(--text); }
.board li.sel { border-left-color: var(--hot); background: var(--panel); }

/* ---------- results ---------- */
.results {
  position: fixed;
  inset: 0;
  background: rgba(13, 14, 26, 0.86);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 10;
  animation: fade-in 0.2s ease-out;
}
@keyframes fade-in { from { opacity: 0; } }
.results-card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 4px solid var(--duck);
  padding: 20px 16px;
  display: grid;
  gap: 14px;
  animation: pop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) both; /* overshoots, then settles */
}
@keyframes pop { from { transform: scale(0.85); opacity: 0; } }
.eyebrow {
  margin: 0;
  font-family: var(--pixel);
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--dim);
}
.results-card h2 {
  margin: 0;
  font-family: var(--pixel);
  font-size: 20px;
  line-height: 1.4;
  color: var(--duck);
  text-wrap: balance;
}
.results-card { max-height: calc(100vh - 32px); overflow-y: auto; }
.vs-world { display: grid; gap: 8px; padding-top: 12px; border-top: 2px dashed var(--line); }
#vsList { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
#vsList li {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 2px 6px;
  font-variant-numeric: tabular-nums;
}
.vs-you { margin: 0; font-size: 18px; line-height: 1.15; color: var(--hot); }
#vsList img { width: auto; height: 22px; image-rendering: pixelated; justify-self: center; }
#vsList .name { font-family: var(--pixel); font-size: 9px; }
#vsList .speed { font-size: 20px; }
.avg { margin: -4px 0 0; font-size: 22px; line-height: 1; color: var(--text); }
.avg:empty { display: none; }
.summary { margin: 0; font-size: 20px; line-height: 1.1; color: var(--text); }
.summary:empty { display: none; }
#resultList { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
#resultList li {
  display: grid;
  grid-template-columns: 40px 36px 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 3px 6px;
  font-variant-numeric: tabular-nums;
}
#resultList li.gold { background: var(--ground); outline: 2px solid var(--duck); }
#resultList .pos, #resultList .name { font-family: var(--pixel); font-size: 10px; }
#resultList .pos { color: var(--dim); }
#resultList .gold .pos { color: var(--duck); }
#resultList img { width: auto; height: 22px; image-rendering: pixelated; }
#resultList .time { font-size: 22px; }
#resultList .delta { font-size: 18px; color: var(--dim); min-width: 5ch; text-align: right; }

/* ---------- desktop: screen left, deck right ---------- */
@media (min-width: 900px) {
  .app {
    max-width: 980px;
    grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
    grid-template-areas: "top top" "screen deck";
    column-gap: 32px;
    padding-block: 32px;
  }
  .top { grid-area: top; }
  .screen { grid-area: screen; align-self: start; }
  .deck { grid-area: deck; align-content: start; }
}

/* Phone on its side: map left, controls right, the map as tall as the screen. */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    max-width: none;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-areas: "screen top" "screen deck";
    grid-template-rows: auto 1fr;
    column-gap: 20px;
    padding-inline: max(12px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-right));
  }
  .screen { grid-area: screen; align-self: start; width: calc((100svh - 40px) / 1.34); }
  .deck { grid-area: deck; align-content: start; }
}

@media (prefers-reduced-motion: reduce) {
  .board li { transition: none; }
  .ticker.show, .results, .results-card { animation: none; }
}
