/* Layout: full-bleed game canvas; HUD pinned to corners; menus float as a
   timing-screen panel over the live attract-mode race. One dark look. */
:root {
  --asphalt: #12141c;
  --panel: #171a26;
  --panel-edge: #2b3044;
  --ink: #f2efe6;
  --dim: #9096ab;
  --kerb: #e0353a;
  --amber: #ffb627;
  --purple: #b45cff;
  --green: #3fcf6e;
  --shadow: #07080c;
  --display: "Press Start 2P", "Courier New", monospace;
  --body: "VT323", "Courier New", monospace;
  color-scheme: dark;
}

[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--asphalt);
  color: var(--ink);
  font-family: var(--body);
  font-size: 22px;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}
#game { position: fixed; inset: 0; }
#game canvas { display: block; image-rendering: pixelated; }

button { font: inherit; color: inherit; }
button:focus-visible, input:focus-visible { outline: 3px solid var(--amber); outline-offset: 2px; }

/* ---------- Menus ---------- */
#menus { position: fixed; inset: 0; pointer-events: none; }
.screen {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) 16px max(16px, env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  pointer-events: auto;
}
.panel {
  width: min(460px, 100%);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 3px solid var(--panel-edge);
  box-shadow: 6px 6px 0 var(--shadow);
  padding: 24px 20px 20px;
  display: grid; gap: 14px;
  text-align: center;
}
.logo { margin: 0; font-family: var(--display); font-weight: 400; line-height: 1.1; display: grid; gap: 6px; }
.logo-a { font-size: clamp(26px, 7vw, 40px); color: var(--ink); letter-spacing: 2px; }
.logo-b {
  font-size: clamp(26px, 7vw, 40px); color: var(--kerb); letter-spacing: 2px;
  padding-bottom: 10px;
  background: repeating-linear-gradient(90deg, var(--kerb) 0 12px, var(--ink) 12px 24px) bottom / 100% 6px no-repeat;
}
h2 { margin: 0; font-family: var(--display); font-weight: 400; font-size: 16px; letter-spacing: 2px; color: var(--amber); }
.tag, .note { margin: 0; color: var(--dim); font-size: 20px; line-height: 1.15; }
.error { margin: 0; color: var(--kerb); font-size: 20px; }
.field { display: grid; gap: 6px; font-family: var(--display); font-size: 10px; letter-spacing: 2px; color: var(--dim); text-align: left; }
input {
  font-family: var(--display); font-size: 16px; letter-spacing: 2px;
  background: var(--asphalt); color: var(--ink);
  border: 3px solid var(--panel-edge); padding: 10px 12px; text-transform: uppercase;
  width: 100%; box-sizing: border-box;
  -webkit-user-select: text; user-select: text;
}
.code-input { font-size: 32px; text-align: center; letter-spacing: 12px; }
.menu-buttons { display: grid; gap: 10px; }
.btn {
  font-family: var(--display); font-size: 13px; letter-spacing: 1px;
  padding: 14px 12px; cursor: pointer;
  background: var(--asphalt); border: 3px solid var(--panel-edge);
  box-shadow: 0 4px 0 var(--shadow);
  transition: transform .06s, border-color .1s;
}
.btn:hover { border-color: var(--dim); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--shadow); }
.btn.primary { background: var(--kerb); border-color: var(--kerb); color: var(--ink); }
.btn.primary:hover { border-color: var(--ink); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.keys { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 14px; color: var(--dim); font-size: 20px; }
kbd { font-family: var(--display); font-size: 9px; border: 2px solid var(--panel-edge); padding: 3px 5px; margin-right: 3px; color: var(--ink); }
@media (pointer: coarse) { .keys { display: none; } }

.room-code { font-family: var(--display); font-size: clamp(36px, 12vw, 56px); letter-spacing: 10px; color: var(--ink); padding: 6px 0; }
.players { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; text-align: left; }
.players li { display: flex; align-items: center; gap: 10px; font-size: 24px; padding: 6px 10px; background: var(--asphalt); border: 2px solid var(--panel-edge); }
.players .swatch { width: 18px; height: 18px; border: 2px solid var(--shadow); flex: none; }
.players .tagline { margin-left: auto; color: var(--dim); font-size: 18px; }

.table-wrap { overflow-x: auto; }
.board { width: 100%; border-collapse: collapse; font-size: 22px; font-variant-numeric: tabular-nums; }
.board th { font-family: var(--display); font-size: 9px; letter-spacing: 1px; color: var(--dim); font-weight: 400; text-align: left; padding: 4px 6px; border-bottom: 2px solid var(--panel-edge); }
.board td { padding: 4px 6px; text-align: left; border-bottom: 1px solid var(--panel-edge); }
.board td:last-child, .board th:last-child { text-align: right; }
.board tr.me td { color: var(--amber); }
.board .t-purple { color: var(--purple); }
.board .t-green { color: var(--green); }
.board .empty td { color: var(--dim); text-align: center; padding: 14px; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
.hud-left, .hud-right, .hud-center {
  position: absolute;
  top: calc(10px + env(safe-area-inset-top, 0px));
}
.hud-left {
  left: calc(10px + env(safe-area-inset-left, 0px));
  display: grid; gap: 1px;
  padding: 8px 10px;
  background: color-mix(in srgb, var(--panel) 80%, transparent);
  border: 2px solid var(--panel-edge);
  min-width: 112px;
}
.hud-label { font-family: var(--display); font-size: 8px; letter-spacing: 1px; color: var(--dim); }
.hud-big { font-family: var(--display); font-size: 16px; margin-bottom: 4px; }
.hud-num { font-size: 26px; line-height: .95; font-variant-numeric: tabular-nums; margin-bottom: 4px; }
#hud-best.t-purple { color: var(--purple); }
.hud-center { left: 50%; transform: translateX(-50%); }
.gearbox {
  display: flex; gap: 10px; align-items: stretch;
  padding: 8px 10px;
  background: color-mix(in srgb, var(--panel) 80%, transparent);
  border: 2px solid var(--panel-edge);
}
.gear-wrap { display: grid; justify-items: center; align-content: start; gap: 2px; }
#hud-gear { font-family: var(--display); font-size: 30px; line-height: 1; color: var(--amber); }
.meter-wrap { display: grid; gap: 3px; width: clamp(130px, 26vw, 240px); position: relative; }
#meter {
  height: 20px; background: var(--asphalt); border: 2px solid var(--panel-edge);
  position: relative; overflow: hidden;
}
#meter::after { /* segment ticks */
  content: ""; position: absolute; inset: 0;
  background: repeating-linear-gradient(90deg, transparent 0 calc(10% - 2px), var(--asphalt) calc(10% - 2px) 10%);
}
#meter-fill { position: absolute; inset: 0 auto 0 0; width: 0%; background: var(--amber); }
#meter.full #meter-fill { background: var(--green); animation: blink .3s steps(2) infinite; }
#meter.drain #meter-fill { background: var(--kerb); }
#meter.maxed #meter-fill { background: var(--purple); }
#shift-cue { font-family: var(--display); font-size: 9px; letter-spacing: 2px; color: var(--dim); }
#shift-cue.on { color: var(--green); animation: blink .3s steps(2) infinite; }
@keyframes blink { 50% { opacity: .35; } }

.hud-right { right: calc(10px + env(safe-area-inset-right, 0px)); display: grid; gap: 6px; justify-items: end; }
#minimap {
  width: 160px; height: 110px; image-rendering: pixelated;
  background: color-mix(in srgb, var(--panel) 80%, transparent);
  border: 2px solid var(--panel-edge);
}
#standings { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; min-width: 150px; }
#standings li {
  display: flex; gap: 8px; align-items: center; font-size: 20px;
  background: color-mix(in srgb, var(--panel) 80%, transparent); padding: 2px 8px; border: 2px solid var(--panel-edge);
}
#standings .pos { font-family: var(--display); font-size: 10px; width: 16px; }
#standings .swatch { width: 10px; height: 10px; flex: none; }
#standings .lap { margin-left: auto; color: var(--dim); }
#standings li.me { border-color: var(--amber); }

.icon-btn {
  pointer-events: auto; cursor: pointer;
  font-family: var(--display); font-size: 10px;
  background: color-mix(in srgb, var(--panel) 85%, transparent);
  border: 2px solid var(--panel-edge); padding: 8px 10px; color: var(--ink);
}
#btn-quit { position: absolute; left: calc(10px + env(safe-area-inset-left, 0px)); bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
#btn-mute { position: fixed; right: calc(10px + env(safe-area-inset-right, 0px)); bottom: calc(10px + env(safe-area-inset-bottom, 0px)); z-index: 5; }
body.racing #btn-mute { display: none; }

#lights {
  position: fixed; top: 36%; left: 50%; transform: translate(-50%, -50%);
  display: flex; gap: 10px; padding: 12px 14px;
  background: var(--shadow); border: 3px solid var(--panel-edge); pointer-events: none;
}
.light { width: 30px; height: 30px; border-radius: 50%; background: #2a0c0e; box-shadow: inset 0 -4px 0 rgba(0,0,0,.4); }
.light.on { background: var(--kerb); box-shadow: 0 0 14px var(--kerb), inset 0 -4px 0 rgba(0,0,0,.25); }
#lights.go .light { background: var(--green); box-shadow: 0 0 14px var(--green); }

#toast {
  position: fixed; top: 38%; left: 50%; transform: translate(-50%, -50%);
  font-family: var(--display); font-size: clamp(14px, 3.4vw, 22px); letter-spacing: 2px; text-align: center;
  padding: 12px 18px; background: color-mix(in srgb, var(--shadow) 85%, transparent);
  border: 3px solid var(--panel-edge); pointer-events: none; line-height: 1.6;
}
#toast .t-purple { color: var(--purple); }
#toast .t-green { color: var(--green); }
#toast .t-amber { color: var(--amber); }
#toast .t-red { color: var(--kerb); }

/* ---------- Touch controls ---------- */
#touch { position: fixed; inset: auto 0 0 0; height: 0; }
#slider {
  position: fixed;
  left: calc(14px + env(safe-area-inset-left, 0px));
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  width: min(46vw, 340px); height: 76px;
  touch-action: none;
}
.slider-rail {
  position: absolute; left: 0; right: 0; top: 50%; height: 40px; transform: translateY(-50%);
  background: color-mix(in srgb, var(--panel) 75%, transparent);
  border: 3px solid var(--panel-edge);
  border-radius: 40px 40px 40px 40px / 30px 30px 50px 50px; /* gentle wheel curve */
  display: flex; justify-content: space-between; align-items: center; padding: 0 14px;
  color: var(--dim); font-size: 18px;
}
#knob {
  position: absolute; top: 50%; left: 50%; width: 60px; height: 60px;
  transform: translate(-50%, -50%);
  background: var(--ink); border: 4px solid var(--shadow); border-radius: 50%;
  box-shadow: inset 0 -6px 0 #b9b5aa;
}
#knob::after { content: ""; position: absolute; left: 50%; top: 8px; width: 6px; height: 16px; margin-left: -3px; background: var(--kerb); }
.touch-right {
  position: fixed;
  right: calc(14px + env(safe-area-inset-right, 0px));
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  display: flex; align-items: flex-end; gap: 12px;
}
.pad-btn {
  touch-action: none; border-radius: 50%;
  font-family: var(--display); font-size: 10px;
  border: 4px solid var(--shadow);
}
#btn-brake { width: 72px; height: 72px; background: color-mix(in srgb, var(--panel) 85%, transparent); border-color: var(--panel-edge); }
#btn-shift { width: 104px; height: 104px; font-size: 13px; background: var(--panel-edge); color: var(--dim); }
#btn-shift.ready { background: var(--green); color: var(--shadow); animation: blink .3s steps(2) infinite; }
.pad-btn.down { transform: scale(.94); filter: brightness(1.3); }
body.touch #btn-quit { bottom: auto; top: calc(10px + env(safe-area-inset-top, 0px)); left: auto; right: calc(176px + env(safe-area-inset-right, 0px)); }

#rotate-hint {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(320px, calc(100vw - 32px)); box-sizing: border-box;
  display: flex; gap: 10px; align-items: center;
  background: var(--shadow); border: 2px solid var(--panel-edge); padding: 8px 8px 8px 12px; font-size: 20px; line-height: 1.1; z-index: 6;
}
#rotate-hint .icon-btn { flex: none; margin-left: auto; }

/* Small landscape phones: tighten the HUD */
@media (max-height: 480px) {
  .hud-left { min-width: 92px; padding: 6px 8px; }
  .hud-num { font-size: 22px; }
  #hud-gear { font-size: 22px; }
  #minimap { width: 120px; height: 82px; }
  .meter-wrap { width: clamp(110px, 22vw, 200px); }
  #slider { height: 64px; }
  #knob { width: 52px; height: 52px; }
  #btn-shift { width: 88px; height: 88px; }
  #btn-brake { width: 60px; height: 60px; }
  .panel { padding: 16px; gap: 10px; }
}
/* Portrait phones: stack the HUD corners so they fit */
@media (max-width: 560px) {
  .hud-left { min-width: 0; padding: 6px 8px; }
  .hud-num { font-size: 22px; }
  .hud-big { font-size: 13px; }
  .hud-center { top: calc(150px + env(safe-area-inset-top, 0px)); }
  #minimap { width: 110px; height: 76px; }
  #standings li { font-size: 18px; }
  /* thumb controls: slider along the bottom-left, buttons stacked on the right */
  .touch-right { flex-direction: column; align-items: center; }
  #slider { width: calc(100vw - 104px - 3 * 14px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)); }
}
@media (prefers-reduced-motion: reduce) {
  #meter.full #meter-fill, #shift-cue.on, #btn-shift.ready { animation: none; }
}
