/* Stony Creek Karts. Dark glass panels, white type, logo red (#ed2441). */
:root {
  --red: #ed2441;
  --ink: #f4f5f7;
  --muted: #a7adb6;
  --dim: #6f7680;
  --panel: rgba(12, 14, 18, 0.72);
  --edge: rgba(255, 255, 255, 0.12);
  --good: #3ddc84;
  --bad: #ff5a5f;
  --font: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Inter, Roboto, sans-serif;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  --sl: max(14px, env(safe-area-inset-left));
  --sr: max(14px, env(safe-area-inset-right));
  --st: max(12px, env(safe-area-inset-top));
  --sb: max(14px, env(safe-area-inset-bottom));
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #0d0f12; }
body { font-family: var(--font); color: var(--ink); -webkit-font-smoothing: antialiased; -webkit-user-select: none; user-select: none; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
#game { position: fixed; inset: 0; touch-action: none; }
#game canvas { display: block; width: 100%; height: 100%; }
kbd { font: 600 11px/1 var(--mono); padding: 3px 5px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.25); color: var(--ink); background: rgba(255,255,255,0.06); }
button { font: inherit; color: inherit; cursor: pointer; }

/* ===== HUD ===== */
#hud { position: fixed; inset: 0; pointer-events: none; opacity: 0; transition: opacity 0.5s; }
body.playing #hud { opacity: 1; }
.h-panel { position: absolute; background: var(--panel); border: 1px solid var(--edge); border-radius: 8px; -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
.h-left { position: absolute; left: var(--sl); top: var(--st); display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.h-left .h-panel { position: relative; }
.h-tl { padding: 10px 14px 12px; min-width: 196px; }
.h-eyebrow { font: 600 10px/1 var(--mono); letter-spacing: 0.14em; text-transform: uppercase; color: var(--red); margin-bottom: 6px; }
.h-row { display: flex; align-items: flex-end; gap: 18px; }
.h-big { font-weight: 800; font-size: 40px; line-height: 0.9; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.h-big small { font-size: 16px; color: var(--muted); font-weight: 600; letter-spacing: 0; margin-left: 2px; }
.h-lapbox small { display: block; font: 600 10px/1 var(--mono); letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); margin-bottom: 4px; }
.h-lapbox b { font-size: 24px; font-variant-numeric: tabular-nums; }
.h-lapbox b small { font-size: 14px; color: var(--muted); }
body:not(.is-race) .race-only { display: none; }
.h-times { margin: 10px 0 0; display: grid; gap: 3px; }
.h-times div { display: flex; gap: 10px; align-items: baseline; font: 500 13px/1.25 var(--mono); }
.h-times dt { width: 34px; color: var(--muted); font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; }
.h-times dd { margin: 0; font-variant-numeric: tabular-nums; }
#h-delta { font-weight: 700; }
#h-delta.good { color: var(--good); } #h-delta.bad { color: var(--bad); }

#h-board { margin: 0; padding: 6px 0; list-style: none; min-width: 196px; max-height: calc(100vh - 210px); overflow: hidden; }
#h-board:empty { display: none; }
#h-board li { display: flex; align-items: center; gap: 8px; padding: 3px 12px; font: 500 12px/1.3 var(--font); color: #d9dde3; }
#h-board li b { width: 16px; text-align: right; font: 700 12px/1 var(--mono); color: var(--muted); }
#h-board li i { width: 4px; height: 13px; border-radius: 2px; }
#h-board li span { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#h-board li em { font: 500 11px/1 var(--mono); color: var(--muted); font-style: normal; }
#h-board li.me { background: rgba(237, 36, 65, 0.22); color: #fff; }
#h-board li.me b { color: #fff; }
#h-board li.hint span { color: var(--muted); font-size: 11px; white-space: normal; }

.h-mapwrap { right: var(--sr); top: var(--st); padding: 4px; line-height: 0; }

.h-msgbox { position: absolute; left: 50%; top: 22%; transform: translate(-50%, -8px); text-align: center; opacity: 0; transition: opacity 0.25s, transform 0.25s; }
.h-msgbox.show { opacity: 1; transform: translate(-50%, 0); }
#h-msg { display: inline-block; padding: 10px 22px; border-radius: 8px; background: rgba(12,14,18,0.78); border: 1px solid var(--edge); font-weight: 800; font-size: clamp(20px, 3.4vw, 34px); letter-spacing: -0.01em; }
#h-sub { margin-top: 8px; font: 600 13px/1.3 var(--font); color: #e6e8ec; text-shadow: 0 1px 3px rgba(0,0,0,0.7); }
.h-msgbox.green #h-msg { background: rgba(28, 160, 78, 0.92); border-color: rgba(255,255,255,0.3); }
.h-msgbox.red #h-msg { background: rgba(237, 36, 65, 0.92); border-color: rgba(255,255,255,0.3); }
.h-msgbox.flag #h-msg { background: repeating-conic-gradient(#111 0 25%, #f2f2f2 0 50%) 0 0/18px 18px; color: #fff; text-shadow: 0 0 6px #000, 0 0 2px #000; }
.h-msgbox.best #h-msg { background: rgba(140, 60, 220, 0.92); }

.h-readout { left: 50%; bottom: calc(var(--sb) + 6px); transform: translateX(-50%); padding: 8px 16px; display: none; align-items: baseline; gap: 6px; }
body.readout .h-readout { display: flex; }
.h-readout b { font-size: 28px; font-weight: 800; font-variant-numeric: tabular-nums; min-width: 2.2ch; text-align: right; }
.h-readout small { color: var(--muted); font-size: 11px; margin-right: 12px; }
.h-readout span { font: 600 16px/1 var(--mono); font-variant-numeric: tabular-nums; }
.h-hint { position: absolute; left: 50%; bottom: calc(var(--sb) + 64px); transform: translateX(-50%); font: 600 12px/1.3 var(--font); color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,0.8); opacity: 0; transition: opacity 0.4s; text-align: center; }
.h-hint.show { opacity: 1; }
.h-skip { position: absolute; left: 50%; bottom: calc(var(--sb) + 18px); transform: translateX(-50%); padding: 9px 14px; border-radius: 8px; border: 1px solid var(--edge); background: rgba(12,14,18,0.72); font: 600 13px/1 var(--font); display: none; pointer-events: auto; align-items: center; gap: 10px; }
.h-skip.show { display: flex; }
body.touch .h-skip { bottom: calc(var(--sb) + 150px); }
body.touch .h-hint { bottom: calc(var(--sb) + 200px); }
body.touch #start kbd, body.touch .h-skip kbd { display: none; }

/* ===== Overlays ===== */
.overlay { position: fixed; inset: 0; display: flex; align-items: center; justify-content: flex-start; padding: var(--st) var(--sr) var(--sb) var(--sl); z-index: 10; transition: opacity 0.35s; }
.overlay.hidden { opacity: 0; pointer-events: none; visibility: hidden; }
#title { background: linear-gradient(90deg, rgba(8,10,13,0.82) 0%, rgba(8,10,13,0.55) 38%, rgba(8,10,13,0) 64%); }
#paused, #results { justify-content: center; background: rgba(8, 10, 13, 0.55); }
.t-card { width: min(470px, 100%); padding: clamp(18px, 3vw, 30px); border-radius: 12px; background: rgba(12, 14, 18, 0.74); border: 1px solid var(--edge); -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); max-height: 100%; overflow: auto; margin-left: clamp(0px, 3vw, 40px); }
.t-card.small { width: min(340px, 100%); margin: 0; }
.t-card.wide { width: min(620px, 100%); margin: 0; }
.t-logo { display: block; width: min(330px, 100%); height: auto; margin-bottom: 12px; }
.t-tag { margin: 0 0 18px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.t-eyebrow { margin: 0 0 4px; font: 600 11px/1 var(--mono); letter-spacing: 0.14em; text-transform: uppercase; color: var(--red); }
.t-card h2 { margin: 0 0 16px; font-size: 26px; letter-spacing: -0.02em; }
.t-modes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.t-mode { text-align: left; padding: 12px 13px 13px; border-radius: 9px; border: 1px solid var(--edge); background: rgba(255,255,255,0.04); transition: border-color 0.15s, background 0.15s; }
.t-mode b { display: block; font-size: 15px; margin-bottom: 4px; }
.t-mode span { display: block; font-size: 12px; line-height: 1.35; color: var(--muted); }
.t-mode[aria-checked="true"] { border-color: var(--red); background: rgba(237, 36, 65, 0.14); }
.t-mode:hover { border-color: rgba(255,255,255,0.35); }
.t-opts { display: flex; flex-wrap: wrap; gap: 8px 14px; margin-bottom: 16px; }
.opt { display: flex; flex-direction: column; gap: 5px; font: 600 10px/1 var(--mono); letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.opt select { font: 500 14px/1 var(--font); color: var(--ink); background: rgba(255,255,255,0.07); border: 1px solid var(--edge); border-radius: 6px; padding: 7px 26px 7px 9px; -webkit-appearance: none; appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, #aaa 50%), linear-gradient(135deg, #aaa 50%, transparent 50%); background-position: calc(100% - 13px) 50%, calc(100% - 9px) 50%; background-size: 4px 4px; background-repeat: no-repeat; }
.opt select option { color: #111; }
body.mode-practice .race-opt, body.mode-race .practice-opt { display: none; }
#start { width: 100%; padding: 14px 18px; border: 0; border-radius: 9px; background: var(--red); color: #fff; font-weight: 800; font-size: 17px; letter-spacing: 0.01em; display: flex; justify-content: center; }
#start kbd { margin-left: 10px; border-color: rgba(255,255,255,0.5); background: rgba(0,0,0,0.12); }
#start:disabled { background: #3a3d44; cursor: default; }
#start .loading-label { display: none; }
#start:disabled .ready-label { display: none; }
#start:disabled .loading-label { display: inline; color: #c8ccd2; font-weight: 600; }
#load-error { color: #ff9a9a; font-size: 13px; line-height: 1.4; }
.t-best { margin: 12px 0 0; font: 500 12px/1.4 var(--mono); color: var(--muted); min-height: 1em; }
.t-best b { color: var(--ink); }
.t-keys { list-style: none; padding: 0; margin: 14px 0 0; display: flex; flex-wrap: wrap; gap: 8px 14px; font-size: 12px; color: var(--muted); }
.t-keys li { display: flex; align-items: center; gap: 4px; }
.t-pad { margin: 10px 0 0; font-size: 12px; color: var(--dim); }
.t-foot { margin-top: 16px; display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 14px; font-size: 11px; color: var(--dim); }
.text-btn { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; text-underline-offset: 2px; font-size: 11px; }
.if-touch { display: none; }
body.touch .no-touch { display: none; }
body.touch .if-touch { display: block; }
body.touch .t-pad { display: none; }
.unblkd { position: absolute; top: var(--st); right: var(--sr); display: block; padding: 9px 11px; border-radius: 8px; background: rgba(12,14,18,0.6); border: 1px solid var(--edge); line-height: 0; }
.unblkd svg { width: 104px; height: auto; }
.unblkd .logo-blocks { fill: #f2f2f2; }
.unblkd .logo-word { fill: #ff7a1a; }

.menu-list { display: grid; gap: 8px; }
.menu-list button, .menu-row button { padding: 11px 14px; border-radius: 8px; border: 1px solid var(--edge); background: rgba(255,255,255,0.06); text-align: left; font-size: 15px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.menu-list button:hover, .menu-row button:hover, .menu-list button.focus, .menu-row button.focus { border-color: rgba(255,255,255,0.4); }
.menu-row { display: flex; gap: 10px; margin-top: 18px; }
.menu-row button { flex: 1; justify-content: center; gap: 10px; }
.menu-row button.primary { background: var(--red); border-color: var(--red); color: #fff; }
body:not(.is-practice) .practice-only { display: none; }
.r-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.r-table th { text-align: left; font: 600 10px/1 var(--mono); letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); padding: 0 8px 8px; border-bottom: 1px solid var(--edge); }
.r-table td { padding: 7px 8px; border-bottom: 1px solid rgba(255,255,255,0.06); font-variant-numeric: tabular-nums; }
.r-table td:nth-child(3), .r-table td:nth-child(4) { font-family: var(--mono); font-size: 13px; }
.r-table tr.me td { background: rgba(237, 36, 65, 0.2); font-weight: 700; }
.r-table td i { display: inline-block; width: 4px; height: 12px; border-radius: 2px; margin-right: 8px; vertical-align: -1px; }

/* FPS counter (top centre, clear of the timing panel, map and touch buttons) */
.h-fps { position: absolute; left: 50%; top: var(--st); transform: translateX(-50%); padding: 4px 8px; border-radius: 6px; background: rgba(12,14,18,0.55); border: 1px solid var(--edge); font: 600 11px/1 var(--mono); color: #d9dde3; font-variant-numeric: tabular-nums; display: none; }
body.show-fps .h-fps:not(:empty) { display: block; }

/* ===== Top 25 ===== */
.t-boardbtn { display: inline-flex; gap: 6px; align-items: center; margin-top: 8px; padding: 0; border: 0; background: none; color: var(--ink); font: 600 13px/1.3 var(--font); text-decoration: underline; text-decoration-color: rgba(255,255,255,0.35); text-underline-offset: 3px; }
.t-boardbtn:hover { text-decoration-color: var(--red); }
.overlay.modal { justify-content: center; background: rgba(8,10,13,0.55); z-index: 20; }
.overlay.modal .t-card { margin: 0; }
.p-save { display: none !important; background: rgba(140, 60, 220, 0.85) !important; border-color: rgba(255,255,255,0.3) !important; }
body.lap-to-save .p-save { display: flex !important; }
.s-rank { margin: -8px 0 16px; color: var(--muted); font-size: 14px; }
.s-label { display: block; font: 600 10px/1 var(--mono); letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
#s-name { width: 100%; padding: 12px 14px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.25); background: rgba(255,255,255,0.08); color: var(--ink); font: 700 20px/1.2 var(--font); -webkit-user-select: text; user-select: text; }
#s-name:focus { outline: 2px solid var(--red); outline-offset: 1px; }
.s-note { margin: 8px 0 0; font-size: 12px; color: var(--muted); }
.s-err { margin: 8px 0 0; min-height: 1.3em; font-size: 13px; font-weight: 600; color: var(--bad); }
#save .menu-row { margin-top: 12px; }
#s-ok[disabled] { opacity: 0.6; cursor: wait; }
.b-status { margin: -8px 0 12px; color: var(--muted); font-size: 13px; min-height: 1.2em; }
.b-list { margin: 0; padding: 0; list-style: none; counter-reset: rank; max-height: min(56vh, 520px); overflow: auto; }
.b-list li { counter-increment: rank; display: flex; align-items: baseline; gap: 10px; padding: 7px 8px; border-bottom: 1px solid rgba(255,255,255,0.06); font-size: 15px; }
.b-list li::before { content: counter(rank); width: 2ch; text-align: right; font: 700 12px/1 var(--mono); color: var(--muted); }
.b-list li span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.b-list li b { font: 600 14px/1 var(--mono); font-variant-numeric: tabular-nums; }
.b-list li small { width: 7.5ch; text-align: right; font: 500 11px/1 var(--mono); color: var(--dim); }
.b-list li:nth-child(1)::before { color: #f2c21b; } .b-list li:nth-child(2)::before { color: #c9cdd3; } .b-list li:nth-child(3)::before { color: #d08a4c; }
.b-list li.me { background: rgba(237, 36, 65, 0.22); border-radius: 6px; }

/* ===== Touch controls ===== */
#touch { display: none; }
body.touch.playing #touch { display: block; }
#touch .t-util { position: fixed; top: calc(var(--st) + 230px); right: var(--sr); display: flex; flex-direction: column; gap: 8px; z-index: 6; }
#touch .t-util button { width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--edge); background: rgba(12,14,18,0.6); display: grid; place-items: center; padding: 0; }
#touch .t-util svg { width: 22px; height: 22px; fill: currentColor; }
#touch .t-steer { position: fixed; left: var(--sl); bottom: var(--sb); width: min(42vw, 280px); height: 92px; border-radius: 46px; border: 1px solid var(--edge); background: rgba(12,14,18,0.45); z-index: 6; display: grid; place-items: center; touch-action: none; }
#touch .t-steer.on { background: rgba(12,14,18,0.6); }
#touch .t-knob { width: 70px; height: 70px; border-radius: 50%; background: rgba(255,255,255,0.85); box-shadow: 0 2px 10px rgba(0,0,0,0.4); }
#touch .t-pedals { position: fixed; right: var(--sr); bottom: var(--sb); display: flex; gap: 10px; z-index: 6; touch-action: none; }
#touch .t-pedal { width: 86px; height: 112px; border-radius: 14px; border: 1px solid var(--edge); font-weight: 800; font-size: 14px; letter-spacing: 0.04em; text-transform: uppercase; touch-action: none; }
#touch .t-gas { background: rgba(61, 220, 132, 0.28); height: 132px; }
#touch .t-brake { background: rgba(237, 36, 65, 0.28); align-self: flex-end; }
#touch .t-pedal.on { filter: brightness(1.8); }
body.touch .h-readout { bottom: calc(var(--sb) + 104px); }
body.touch #h-board { display: none; }

@media (max-width: 760px), (max-height: 520px) {
  .h-tl { padding: 8px 10px 9px; min-width: 0; }
  .h-big { font-size: 30px; }
  .h-times div { font-size: 12px; }
  #h-board { display: none; }
  .h-mapwrap canvas { width: 104px !important; height: 152px !important; }
  #title { background: rgba(8,10,13,0.62); justify-content: center; }
  .t-card { margin: 0; }
  .unblkd { display: none; }
}
@media (max-height: 520px) {
  .t-card { padding: 14px 16px; }
  .t-tag, .t-keys, .t-pad { display: none; }
  .t-logo { width: 220px; margin-bottom: 8px; }
  .t-mode span { display: none; }
  #touch .t-util { top: calc(var(--st) + 160px); }
}
