:root {
  --bg: #0b0d12;
  --panel: #161a22;
  --line: #262c38;
  --text: #f4f6fa;
  --muted: #8b93a5;
  --accent: #ffd23f;   /* lap number */
  --count: #4fc3ff;    /* countdown */
  --go: #2ee66b;
  --go-ink: #04210f;
  --stop: #ff4d4d;
  --stop-ink: #2a0606;
  --finish: #ff9f1c;
  --pause: #8b93a5;
  --on: #2ee66b;
  --sel-bg: #f4f6fa;
  --sel-ink: #0b0d12;
}
/* High-contrast for direct sun: black on yellow. */
[data-theme="sun"] {
  --bg: #ffd400;
  --panel: #fff3a3;
  --line: #000;
  --text: #000;
  --muted: #2b2400;
  --accent: #000;
  --count: #002f87;
  --go: #006b25;
  --go-ink: #fff;
  --stop: #b30000;
  --stop-ink: #fff;
  --finish: #8a1c00;
  --pause: #000;
  --on: #006b25;
  --sel-bg: #000;
  --sel-ink: #ffd400;
}
[data-theme="sun"] .seg, [data-theme="sun"] .toggle, [data-theme="sun"] .icon-btn,
[data-theme="sun"] .second-btn, [data-theme="sun"] .lapbar { border-width: 2px; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow-x: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
button:disabled { cursor: default; }
[hidden] { display: none !important; }

.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
  /* Extra room below the status bar / Dynamic Island: iOS blurs content right at that edge. */
  padding: calc(env(safe-area-inset-top, 0px) + 12px) max(16px, env(safe-area-inset-right))
           max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  max-width: 1100px;
  margin: 0 auto;
}
/* Installed (home-screen) app: the page runs right up under the status bar and
 * iOS's soft blur at that edge reaches further down than in Safari, so give the
 * race tabs more clearance there. */
@media (display-mode: standalone) {
  .app { padding-top: calc(env(safe-area-inset-top, 0px) + 26px); }
  body .audio-warn { top: calc(env(safe-area-inset-top, 0px) + 26px); }
}

/* ---------- controls ---------- */
.controls { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: minmax(76px, 1fr);
  overflow-x: auto; scrollbar-width: none;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 4px; gap: 4px;
}
.seg::-webkit-scrollbar { display: none; }
.seg button {
  border: 0; background: transparent; border-radius: 10px; padding: 9px 4px;
  font-weight: 700; font-size: 14px; line-height: 1.1; color: var(--muted); white-space: nowrap;
}
.seg button small { display: block; font-weight: 500; font-size: 11px; opacity: .8; margin-top: 2px; }
.seg button[aria-checked="true"] { background: var(--sel-bg); color: var(--sel-ink); }
.seg button:disabled:not([aria-checked="true"]) { opacity: .35; }

.toolbar { display: flex; gap: 8px; align-items: stretch; }
.spacer { flex: 1; }
.toggle, .icon-btn {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px;
  font-weight: 600; font-size: 14px;
}
.icon-btn { padding: 8px 10px; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg.gear { stroke-width: 1.8; }
@media (max-width: 480px) {
  .icon-btn span { display: none; }
  .toolbar { gap: 6px; }
  .toggle { padding: 8px 10px; gap: 6px; }
  .icon-btn { padding: 8px 7px; }
}
.toggle > span:last-child { white-space: nowrap; }
.toggle-dot {
  width: 38px; height: 22px; border-radius: 11px; background: var(--line); position: relative; transition: background .15s; flex: none;
}
.toggle-dot::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: transform .15s;
}
.toggle[aria-checked="true"] .toggle-dot { background: var(--on); }
.toggle[aria-checked="true"] .toggle-dot::after { transform: translateX(16px); }

/* ---------- display ---------- */
.display {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 0; text-align: center;
}
.heat-banner {
  font-size: 14px; font-weight: 700; padding: 6px 12px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line); margin-bottom: 8px;
}
.status {
  font-size: clamp(18px, 4.5vmin, 34px); font-weight: 800; letter-spacing: .14em; text-transform: uppercase;
  color: var(--muted);
}
.big {
  font-size: clamp(120px, 50vmin, 440px);
  font-weight: 900; line-height: .95;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  letter-spacing: -0.04em;
  white-space: nowrap;
}
.big.wide { font-size: clamp(80px, 26vmin, 260px); line-height: 1.3; }
.sub { font-size: clamp(15px, 3.4vmin, 26px); color: var(--muted); font-weight: 600; }

.lapbar {
  width: min(560px, 90%); height: 10px; border-radius: 5px; background: var(--panel); border: 0 solid var(--line);
  margin: 16px 0 12px; overflow: hidden;
}
.lapbar-fill { height: 100%; width: 0%; background: var(--accent); }

.clock { display: flex; gap: clamp(18px, 6vw, 64px); }
.clock > div { display: flex; flex-direction: column; }
.clock-label { font-size: 11px; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); }
.clock-val { font-size: clamp(22px, 5.5vmin, 46px); font-weight: 700; font-variant-numeric: tabular-nums; }

.app[data-phase="countdown"] .big, .app[data-phase="countdown"] .status { color: var(--count); }
.app[data-phase="countdown"] .lapbar-fill { background: var(--count); }
.app[data-phase="racing"] .status { color: var(--go); }
.app[data-phase="finished"] .status, .app[data-phase="finished"] .big { color: var(--finish); }
.app[data-phase="finished"] .lapbar-fill { background: var(--finish); }
.app[data-phase="paused"] .big { color: var(--pause); animation: blink 1.2s steps(2, start) infinite; }
.app[data-phase="paused"] .status { color: var(--text); }
@keyframes blink { 50% { opacity: .35; } }

.big.pulse { animation: pulse .35s ease-out; }
@keyframes pulse { from { transform: scale(1.12); } to { transform: scale(1); } }

/* ---------- actions ---------- */
.actions { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.action-row { display: flex; gap: 10px; width: min(560px, 100%); }
.main-btn, .second-btn {
  border: 0; border-radius: 20px;
  padding: clamp(16px, 4.2vmin, 28px) 12px;
  font-size: clamp(24px, 5.6vmin, 42px); font-weight: 900; letter-spacing: .06em; text-transform: uppercase;
  box-shadow: 0 6px 0 rgba(0,0,0,.35);
}
.main-btn {
  flex: 1; position: relative; overflow: hidden;
  background: var(--go); color: var(--go-ink);
  touch-action: none;
}
.second-btn { flex: 0 0 38%; background: var(--panel); border: 1px solid var(--line); }
.main-btn:active, .second-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 rgba(0,0,0,.35); }
.main-label { position: relative; z-index: 1; }
.main-btn.hold { background: var(--stop); color: var(--stop-ink); }
/* Hold-to-stop progress fill */
.main-btn.hold::before {
  content: ""; position: absolute; inset: 0; background: rgba(0,0,0,.35);
  transform: scaleX(0); transform-origin: left;
}
.main-btn.holding::before { transform: scaleX(1); transition: transform .8s linear; }
.app[data-phase="finished"] .main-btn { background: var(--sel-bg); color: var(--sel-ink); }

.hint { font-size: 13px; color: var(--muted); min-height: 18px; }
.hint.flash { animation: flash .9s; }
@keyframes flash { 0%, 40% { color: var(--stop); transform: scale(1.08); } }
.link-btn {
  border: 0; background: none; color: var(--muted); font-size: 14px; padding: 4px 10px; text-decoration: underline;
}
.app:not([data-phase="idle"]) .link-btn { visibility: hidden; }

/* Landscape phones: put controls in a side column */
@media (orientation: landscape) and (max-height: 600px) {
  .app { grid-template-columns: 1fr 320px; grid-template-rows: 1fr auto; }
  .controls { grid-column: 2; grid-row: 1; }
  .seg { grid-auto-flow: row; grid-template-columns: 1fr 1fr; overflow: visible; }
  .display { grid-column: 1; grid-row: 1 / span 2; }
  .actions { grid-column: 2; grid-row: 2; }
  .big { font-size: clamp(100px, 56vh, 400px); }
  .big.wide { font-size: clamp(70px, 30vh, 240px); }
  .link-btn { display: none; }
  .icon-btn span { display: none; }
  .toggle { flex: 1; }
}

/* ---------- sheets (settings / heats / log) ---------- */
dialog.sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px; max-height: 92dvh;
  background: var(--bg); color: var(--text); border-radius: 20px 20px 0 0;
  display: none; flex-direction: column;
  -webkit-user-select: text; user-select: text;
}
dialog.sheet[open] { display: flex; animation: up .2s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@media (min-width: 700px) { dialog.sheet { margin: auto; border-radius: 20px; } }
dialog.sheet::backdrop { background: rgba(0,0,0,.55); }
[data-theme="sun"] dialog.sheet { border: 2px solid #000; }
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--line); margin: 0;
}
.sheet-head h2 { margin: 0; font-size: 20px; }
.close { border: 0; background: none; color: var(--count); font-weight: 700; font-size: 17px; padding: 6px; }
.sheet-body { overflow-y: auto; padding: 4px 18px max(24px, env(safe-area-inset-bottom)); }
.sheet-body section { padding: 10px 0 14px; border-bottom: 1px solid var(--line); }
.sheet-body section:last-of-type { border-bottom: 0; }
.sheet-body h3 { margin: 6px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); }
.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  min-height: 46px; font-size: 16px; font-weight: 500;
}
.row small { flex-basis: 100%; order: 3; margin-top: -10px; color: var(--muted); font-size: 12px; }
.row input[type="range"] { flex: 1; max-width: 220px; accent-color: var(--on); }
select, .add-race input {
  font: inherit; font-size: 16px; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; max-width: 100%;
}
select { max-width: 62%; }
input.switch {
  appearance: none; -webkit-appearance: none; margin: 0;
  width: 46px; height: 28px; border-radius: 14px; background: var(--line); position: relative; transition: background .15s; flex: none;
}
input.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s;
}
input.switch:checked { background: var(--on); }
input.switch:checked::after { transform: translateX(18px); }
.mini-seg { display: flex; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 3px; }
.mini-seg button { border: 0; background: none; padding: 6px 14px; border-radius: 8px; font-weight: 600; color: var(--text); opacity: .75; }
.mini-seg button[aria-pressed="true"] { opacity: 1; }
.mini-seg button[aria-pressed="true"] { background: var(--sel-bg); color: var(--sel-ink); }
.pill {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 9px 16px; font-weight: 600; font-size: 15px;
}
.pill.ghost { background: none; }
.gh-link { display: inline-flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; }
.about a, .help a { color: inherit; }
.pill:disabled, .x:disabled, .wide-btn:disabled { opacity: .4; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.wide-btn {
  width: 100%; margin-top: 14px; border: 0; border-radius: 14px; padding: 16px; font-size: 18px; font-weight: 800;
  background: var(--go); color: var(--go-ink);
}
.wide-btn.danger { background: var(--stop); color: var(--stop-ink); }
.help ol, .help ul { margin: 4px 0 0; padding-left: 20px; }
.help li, .help p { font-size: 15px; line-height: 1.45; margin: 0 0 8px; }
.help b { color: var(--accent); }
[data-theme="sun"] .help b { color: inherit; text-decoration: underline; text-decoration-thickness: 2px; }
.note { color: var(--muted); font-size: 14px; line-height: 1.4; }
.about { color: var(--muted); font-size: 12px; text-align: center; margin-top: 18px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li {
  display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 15px;
}
.list li > span:first-child { flex: 1; }
.list li.empty { color: var(--muted); justify-content: center; border: 0; }
.x {
  border: 1px solid var(--line); background: var(--panel); width: 36px; height: 36px; border-radius: 10px; flex: none;
  font-size: 15px; font-weight: 700;
}
.heats li .heat-n { flex: 0 0 64px; font-weight: 700; }
.heats li select { flex: 1; max-width: none; min-width: 0; }
.heats li.current { background: var(--panel); border-radius: 10px; padding-left: 8px; }
.log li { align-items: flex-start; }
.log .when { flex: 0 0 104px; color: var(--muted); font-size: 13px; padding-top: 2px; }
.log li > span:last-child { flex: 1; }
.log li.stopped b { color: var(--stop); }
.add-race { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px 8px; margin-top: 10px; align-items: end; }
.add-race label { display: flex; flex-direction: column; gap: 4px; min-width: 0; font-size: 13px; color: var(--muted); }
.add-race .cr-name { grid-column: 1 / -1; }
.add-race input { min-width: 0; width: 100%; }
.add-race .pill { min-height: 40px; }

/* ---------- audio interrupted bar ---------- */
.audio-warn {
  position: fixed; z-index: 60; left: 12px; right: 12px; top: calc(env(safe-area-inset-top, 0px) + 12px);
  max-width: 520px; margin: 0 auto; padding: 14px 16px; border: 0; border-radius: 16px;
  background: var(--finish); color: #1a0e00; font-size: 15px; line-height: 1.35; text-align: left;
  box-shadow: 0 12px 40px rgba(0,0,0,.45);
  animation: card-up .3s ease-out;
}
[data-theme="sun"] .audio-warn { color: #fff; }

/* ---------- install card ---------- */
.install-card {
  position: fixed; z-index: 50; left: 12px; right: 12px; top: calc(env(safe-area-inset-top, 0px) + 12px);
  max-width: 520px; margin: 0 auto;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-radius: 18px;
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  box-shadow: 0 12px 40px rgba(0,0,0,.45);
  animation: card-up .35s ease-out;
}
[data-theme="sun"] .install-card { border-width: 2px; }
.install-card img { border-radius: 10px; flex: none; }
.install-text { flex: 1; display: flex; flex-direction: column; gap: 2px; font-size: 14px; line-height: 1.3; }
.install-text span { color: var(--muted); }
.install-actions { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.install-actions .pill { background: var(--go); color: var(--go-ink); border: 0; }
.install-actions .link-btn { visibility: visible !important; font-size: 13px; padding: 2px 6px; }
.ios-share { display: inline-block; width: 14px; height: 14px; vertical-align: -2px; }
@keyframes card-up { from { transform: translateY(-120%); opacity: 0; } }

/* ---------- loading screen (splash.js) ---------- */
.splash {
  position: fixed; inset: 0; z-index: 100; overflow: hidden;
  background: #0b0d12;
  display: grid; place-items: center;
  transition: opacity .75s ease;
}
.splash canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
/* No WebGL: a static checkerboard instead of the waving flag. */
.splash.no-gl #splashFlag { display: none; }
.splash.no-gl {
  background: repeating-conic-gradient(#0d0f14 0 25%, #f0f0ee 0 50%) 0 0 / calc(min(100vw, 100vh) / 3) calc(min(100vw, 100vh) / 3);
}
#splashFx { z-index: 2; transition: transform .75s ease, filter .75s ease; }

.splash-badge {
  position: relative; z-index: 1;
  padding: clamp(18px, 4.5vmin, 30px) clamp(26px, 7vmin, 48px) clamp(16px, 4vmin, 26px);
  border-radius: 22px;
  background: linear-gradient(160deg, rgba(22,26,36,.94), rgba(8,10,14,.94));
  box-shadow: 0 0 0 2px rgba(255,210,63,.85), 0 0 0 6px rgba(8,10,14,.6), 0 24px 60px rgba(0,0,0,.55);
  text-align: center; color: #f4f6fa;
  animation: badge-in .8s cubic-bezier(.2,1.4,.4,1) .15s both;
}
.splash-stripe {
  height: 8px; margin: 0 auto 12px; width: 70%; border-radius: 2px;
  background: repeating-conic-gradient(#f4f6fa 0 25%, #0b0d12 0 50%) 0 0 / 8px 8px;
  opacity: .9;
}
.splash-brand {
  font-size: clamp(54px, 17vmin, 120px); font-weight: 900; line-height: .9; letter-spacing: .06em;
  color: #ffd23f; text-shadow: 0 3px 0 #a87a00, 0 10px 24px rgba(255,210,63,.25);
  font-style: italic;
}
.splash-name {
  font-size: clamp(14px, 4.2vmin, 24px); font-weight: 800; letter-spacing: .45em; text-transform: uppercase;
  margin: 10px 0 0 .45em;
}
.splash-sub {
  display: inline-block; margin-top: 10px; padding: 3px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 800; letter-spacing: .25em; text-transform: uppercase;
  background: #e8382c; color: #fff;
}
@keyframes badge-in { from { opacity: 0; transform: scale(.82) rotate(-3deg); } }

/* Reveal: flag and badge are already hidden by the smoke; the smoke then lifts away. */
.splash.reveal { opacity: 0; background: transparent; pointer-events: none; }
.splash.reveal #splashFlag, .splash.reveal .splash-badge { visibility: hidden; }
.splash.reveal #splashFx { transform: scale(1.12); filter: blur(8px); }
@media (prefers-reduced-motion: reduce) { .splash-badge { animation: none; } }
