:root {
  color-scheme: dark;
  --bg: #111214;
  --panel: #191a1d;
  --line: #333438;
  --text: #f5f3ee;
  --muted: #b3b2b1;
  --accent: #ed343a;
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.65 "Segoe UI", Arial, sans-serif; -webkit-font-smoothing: antialiased; }
::selection { color: white; background: #ae2026; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid #ff777b; outline-offset: 5px; }
a { color: inherit; text-decoration: none; }
button, select { font: inherit; }
button { cursor: pointer; touch-action: manipulation; }
h1, h2, p { margin: 0; }
h1, h2 { line-height: 1.12; font-weight: 800; letter-spacing: -.045em; text-wrap: balance; }
.shell { width: min(1240px, calc(100% - 96px)); margin-inline: auto; }
.site-header { position: relative; z-index: 2; border-bottom: 1px solid var(--line); }
.navigation { position: relative; display: flex; align-items: center; justify-content: space-between; min-height: 100px; gap: 12px; }
.brand { display: inline-flex; align-items: center; gap: 13px; font: italic 900 32px/1 "Arial Black", "Segoe UI", sans-serif; letter-spacing: -.065em; }
.brand img { width: 60px; height: 60px; object-fit: contain; }
.brand small { display: block; margin-top: 7px; color: #c3c2c1; font: normal 700 8px/1.2 "Segoe UI", sans-serif; letter-spacing: .16em; }
.text-accent { color: var(--accent); }
.language-selector { display: flex; align-items: center; gap: 3px; flex-shrink: 0; color: var(--muted); font-size: 12px; font-weight: 700; }
.language-selector button { min-width: 36px; min-height: 44px; padding: 0; color: inherit; border: 0; background: transparent; }
.language-selector button[aria-pressed="true"], .language-selector button:hover { color: white; text-decoration: underline; text-decoration-color: var(--accent); text-underline-offset: 7px; }
.return-link, .nav-current, .site-menu a { font-size: 13px; font-weight: 600; }
.return-link, .nav-current { white-space: nowrap; }
.return-link { display: inline-flex; align-items: center; gap: 5px; min-height: 36px; }
.return-link > span:first-child, .nav-contact span { color: #ff777b; }
.return-link:hover, .nav-current, .site-menu a:hover {
  text-decoration: underline 2px var(--accent);
  text-underline-offset: 7px;
}
.nav-current { color: white; }
.site-menu { display: flex; align-items: center; gap: clamp(8px, .9vw, 14px); }
.site-menu a { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; white-space: nowrap; }
.site-menu .nav-contact { border: 1px solid #535359; padding-inline: 10px; }
.menu-toggle { display: none; min-height: 40px; padding: 5px 10px; gap: 8px; align-items: center; color: var(--text); background: transparent; border: 1px solid var(--line); font-size: 13px; }
@media (max-width: 1200px) {
  .navigation {
    display: grid;
    grid-template-areas: "brand language toggle" "return current current";
    grid-template-columns: minmax(0, 1fr) auto auto;
    gap: 3px 9px;
    padding-block: 6px;
  }
  .navigation .brand { grid-area: brand; justify-self: start; }
  .return-link { grid-area: return; justify-self: start; }
  .nav-current { grid-area: current; justify-self: end; }
  .navigation .language-selector { grid-area: language; }
  .menu-toggle { grid-area: toggle; display: inline-flex; }
  .site-menu {
    display: none;
    position: absolute;
    top: 100%;
    inset-inline: 0;
    padding: 8px 12px;
    border: 1px solid var(--line);
    background: var(--panel);
    box-shadow: 0 8px 20px rgb(0 0 0 / .45);
    max-height: min(65svh, 430px);
    overflow: auto;
  }
  .menu-toggle[aria-expanded="true"] + .site-menu {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  }
}
main { padding-block: 16px; }
.game-stage {
  position: relative;
  container: game / inline-size;
  aspect-ratio: 25 / 14;
  width: 100%;
  max-width: max(280px, calc((100svh - 133px) * 25 / 14));
  margin-inline: auto;
  overflow: hidden;
  background: var(--bg);
  outline: 1px solid var(--line);
}
.game-stage :focus-visible { outline-offset: 2px; }
#setup, #race { position: absolute; inset: 0; }
#setup {
  --menu-gap: clamp(8px, 2cqw, 24px);
  display: grid;
  grid-template-rows: auto auto auto;
  align-content: center;
  justify-items: start;
  gap: var(--menu-gap);
  padding: clamp(10px, 3.8cqw, 48px);
  overflow: auto;
  background: linear-gradient(150deg, var(--panel), var(--bg) 64%);
}
#title { font-size: clamp(23px, 5.2cqw, 60px); }
.setup-grid { display: grid; width: 100%; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: var(--menu-gap); }
fieldset { min-width: 0; border: 1px solid var(--line); padding: clamp(6px, 1.5cqw, 18px); margin: 0; background: var(--panel); }
legend { color: #ff969b; padding-inline: 5px; font-size: clamp(10px, 1.2cqw, 12px); font-weight: 700; text-transform: uppercase; letter-spacing: .13em; }
.players { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: clamp(4px, 1cqw, 10px); }
.player-card { position: relative; min-width: 0; display: grid; align-content: start; gap: 4px; padding: clamp(5px, 1.2cqw, 12px); border: 1px solid var(--line); background: var(--bg); cursor: pointer; }
.player-card:has(input:checked) { border-color: var(--accent); background: #24191c; }
.player-card:hover { border-color: #ff777b; }
.player-card input { position: absolute; top: 6px; right: 6px; accent-color: var(--accent); margin: 0; }
canvas { display: block; width: 100%; height: auto; }
.player-card canvas { max-width: 160px; margin-inline: auto; }
.player-card strong { font-size: clamp(12px, 1.8cqw, 18px); line-height: 1.2; }
.player-card span { color: var(--muted); font-size: clamp(10px, 1.2cqw, 12px); line-height: 1.25; }
#trackPreview { background: var(--bg); border: 1px solid var(--line); margin-bottom: clamp(6px, 1.2cqw, 12px); }
#trackChoice { width: 100%; }
.button { display: inline-flex; justify-content: space-between; align-items: center; gap: clamp(8px, 2.7cqw, 27px); min-height: clamp(34px, 4.9cqw, 49px); padding: 5px clamp(8px, 2.1cqw, 21px); font-size: 12px; font-weight: 750; line-height: 1.4; border: 1px solid transparent; transition: background .2s, border-color .2s; }
.button span { font-size: clamp(13px, 1.9cqw, 19px); line-height: 1; }
.button-primary { background: #d5272e; color: white; }
.button-primary:hover { background: #ec343c; }

#race { --inset: clamp(4px, 1.2cqw, 14px); --hud-background: rgb(17 18 20 / .91); }
.race-view, .race-screen { position: absolute; inset: 0; }
.race-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.race-hud, .race-controls { position: absolute; inset-inline: var(--inset); pointer-events: none; }
.race-hud { top: var(--inset); display: flex; justify-content: space-between; align-items: start; gap: 4px; }
#raceTitle {
  padding: clamp(3px, .7cqw, 8px);
  background: var(--hud-background);
  font-size: clamp(11px, 2cqw, 22px);
}
.timing { display: flex; flex-shrink: 0; background: var(--hud-background); border: 1px solid var(--line); }
.timing div { min-width: clamp(55px, 10.4cqw, 104px); display: grid; gap: 2px; padding: clamp(2px, .5cqw, 6px) clamp(4px, .9cqw, 10px); }
.timing div + div { border-left: 1px solid var(--line); color: #ff969b; }
.timing span { color: var(--muted); font-size: clamp(8px, 1.1cqw, 11px); font-weight: 700; letter-spacing: .09em; line-height: 1.3; }
.timing strong { font: 700 clamp(10px, 1.8cqw, 18px)/1.2 Consolas, monospace; font-variant-numeric: tabular-nums; }
.race-controls { bottom: var(--inset); display: grid; gap: clamp(4px, .6cqw, 8px); }
.toolbar { display: flex; justify-content: space-between; align-items: end; gap: 4px; flex-wrap: wrap; }
.toolbar p { padding: 3px 6px; color: var(--muted); background: var(--hud-background); font-size: clamp(10px, 1.2cqw, 13px); line-height: 1.25; }
.race-actions, .race-select { display: flex; align-items: center; gap: clamp(3px, .8cqw, 8px); }
.race-actions { margin-left: auto; }
.race-select { color: var(--text); background: var(--hud-background); }
.race-select span { padding-left: 6px; color: var(--muted); font-size: 12px; }
select, #reset, #back, .touch button {
  pointer-events: auto;
  min-height: clamp(30px, 4.6cqw, 46px);
  max-width: 100%;
  border: 1px solid #535359;
  border-radius: 0;
  background: #1b1b20;
  color: var(--text);
  padding: 3px clamp(5px, 1cqw, 10px);
  font-size: clamp(11px, 1.3cqw, 13px);
}
#reset:hover, #back:hover, .touch button:hover { border-color: white; }
.touch { display: none; justify-content: space-between; gap: 6px; }
.touch, .touch * {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.touch > div { display: flex; gap: 6px; }
.touch button {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 5px;
  min-width: 48px;
  min-height: 62px;
  padding: 4px;
  line-height: 1;
  background: #24252a;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.touch button > * { pointer-events: none; }
.touch button span { font-size: 18px; }
.touch button small { font-size: 10px; font-weight: 700; }
.touch button[data-drive="up"] { border-color: #a7474c; background: #461f25; }
.touch button[data-held] { color: white; background: #d5272e; }

@media (any-pointer: coarse) {
  body { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
  .game-stage { --available-height: calc(100svh - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  .touch { display: flex; touch-action: none; }
  .touch > div { pointer-events: auto; }
  #back, #reset, #camera { min-height: 40px; }
  #status:not([data-result]) { display: none; }
}
@media (max-width: 1000px) {
  .shell { width: calc(100% - 64px); }
  .brand { font-size: 28px; }
}
@media (max-width: 700px) {
  .shell { width: calc(100% - 40px); }
  main.shell { width: calc(100% - 16px); padding-block: 8px; }
  .navigation { min-height: 78px; }
  .brand { font-size: 26px; gap: 9px; }
  .brand img { width: 44px; height: 44px; }
  .menu-toggle #siteMenuLabel { display: none; }
}
@media (max-width: 380px) {
  .brand small { font-size: 7px; }
  .language-selector button { min-width: 28px; }
}
@media (orientation: landscape) and (max-height: 540px) {
  .navigation { min-height: 56px; }
  .brand { font-size: 23px; gap: 8px; }
  .brand img { width: 37px; height: 37px; }
  main { padding-block: 8px; }
  .game-stage { max-width: max(280px, calc((100svh - 112px) * 25 / 14)); }
}
@container game (max-width: 520px) {
  #setup {
    --menu-gap: clamp(4px, 1.4cqw, 8px);
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto auto;
    align-content: space-between;
    align-items: end;
  }
  #title, .player-options { grid-column: 1 / -1; width: 100%; }
  .setup-grid { display: contents; }
  .track-options { width: 100%; padding: 0; border: 0; background: transparent; }
  .track-options legend { padding-inline: 0; }
  .player-card { padding-block: 4px; }
  .player-card canvas { width: auto; height: clamp(26px, 9cqw, 46px); max-width: 100%; object-fit: contain; }
  #trackPreview { display: none; }
  #start, #trackChoice { min-height: 34px; }
  #status:not([data-result]) { display: none; }
  .race-select span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
}
@container game (max-width: 400px) {
  #setup { padding: 7px; }
  #title { font-size: 20px; }
  .player-options { padding: 0; border: 0; background: transparent; }
  .player-options legend { padding-inline: 0; }
  .player-card canvas { height: clamp(24px, 8cqw, 32px); }
  .player-card span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
}

@media (any-pointer: coarse) and (orientation: portrait), (any-pointer: coarse) and (max-width: 539px) {
  .game-stage {
    max-width: 560px;
    height: clamp(350px, calc(var(--available-height) - 121px), 720px);
    min-height: calc(min(56vw, 314px) + 160px);
    aspect-ratio: auto;
  }
  #setup { align-content: center; gap: 14px; padding: 12px; }
  #start, #trackChoice { min-height: 44px; }
  #race { display: flex; flex-direction: column; }
  .race-view { position: static; display: flex; flex: 1; flex-direction: column; justify-content: center; min-height: 0; }
  .race-hud { position: static; order: -1; align-items: center; padding: 8px; }
  #raceTitle { font-size: clamp(13px, 3.8cqw, 19px); }
  .timing strong { font-size: 14px; }
  .race-screen { position: relative; flex: none; width: 100%; aspect-ratio: 25 / 14; }
  .race-controls { position: static; padding: 8px; gap: 8px; }
  .race-actions { justify-content: space-between; width: 100%; margin: 0; }
  .touch > div { width: 48%; }
  .touch button { flex: 1; min-height: clamp(62px, 19cqw, 84px); }
}

@media (any-pointer: coarse) and (orientation: landscape) and (min-width: 540px) {
  main.shell { width: calc(100% - 16px); padding-block: 5px; }
  .game-stage {
    --thumb-width: clamp(106px, 16cqw, 160px);
    max-width: none;
    height: max(200px, calc(var(--available-height) - 68px));
    aspect-ratio: auto;
  }
  .race-screen { inset-inline: var(--thumb-width); container-type: size; }
  .race-canvas {
    inset: 50% auto auto 50%;
    width: min(100cqw, calc(100cqh * 25 / 14));
    height: min(100cqh, calc(100cqw * 14 / 25));
    transform: translate(-50%, -50%);
  }
  .race-controls { display: contents; }
  .toolbar { position: absolute; inset: auto var(--thumb-width) 4px; justify-content: center; pointer-events: none; }
  .race-actions { margin: 0; }
  .race-select span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .touch { position: absolute; inset: 0; align-items: end; padding-bottom: 12px; pointer-events: none; }
  .touch > div { justify-content: center; width: var(--thumb-width); }
  .touch button { width: clamp(48px, 7cqw, 66px); min-height: 70px; }
}

@media (any-pointer: coarse) and (orientation: landscape) and (min-width: 540px) and (max-width: 1200px) {
  .navigation {
    grid-template-areas: "brand return current language toggle";
    grid-template-columns: minmax(0, 1fr) auto auto auto auto;
    min-height: 54px;
    gap: 8px;
    padding-block: 4px;
  }
  .navigation .brand { font-size: 22px; gap: 7px; }
  .navigation .brand img { width: 34px; height: 34px; }
  .navigation .brand small { font-size: 7px; }
}
