* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fd0ff; touch-action: none; overscroll-behavior: none;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: #fff; user-select: none; -webkit-user-select: none; }
canvas { display: block; }
body > canvas:first-of-type { position: fixed; inset: 0; width: 100%; height: 100%; }
[hidden] { display: none !important; }
button, .btn { font: inherit; cursor: pointer; border: 0; color: inherit; text-decoration: none; touch-action: manipulation; }

.iconbtn { position: relative; display: inline-grid; place-items: center; width: 60px; height: 60px; font-size: 30px; border-radius: 18px;
  background: rgba(255,255,255,.92); box-shadow: 0 4px 0 rgba(0,0,0,.25); }
.iconbtn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.25); }
#home { position: fixed; left: max(12px, env(safe-area-inset-left)); top: max(12px, env(safe-area-inset-top)); z-index: 30; }
#fsBtn { position: fixed; left: max(12px, env(safe-area-inset-left)); top: calc(max(12px, env(safe-area-inset-top)) + 72px); z-index: 30; font-size: 26px; cursor: pointer; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; text-shadow: 0 2px 0 rgba(0,0,0,.35); }
#hud button { pointer-events: auto; }
#topBtns { position: absolute; left: calc(max(12px, env(safe-area-inset-left)) + 70px); top: max(12px, env(safe-area-inset-top)); display: flex; gap: 10px; }
#posBox { position: absolute; left: 50%; top: 8px; transform: translateX(-50%); font-weight: 900; line-height: 1; display: flex; align-items: baseline;
  background: linear-gradient(#ff5f52, #c62828); padding: 6px 18px 8px; border-radius: 22px; box-shadow: 0 5px 0 rgba(0,0,0,.25); }
#pos { font-size: 54px; } #posSuf { font-size: 26px; margin-right: 6px; } #posBox small { font-size: 22px; opacity: .8; }
#posBox.first { background: linear-gradient(#ffd54f, #f9a825); }
#lapBox { position: absolute; left: calc(50% + 100px); top: 14px; font-size: 34px; font-weight: 900; background: rgba(0,0,0,.35); padding: 4px 14px; border-radius: 16px; }
#lapBox small { font-size: 20px; opacity: .85; }
#timeBox { position: absolute; left: calc(50% - 260px); top: 14px; width: 150px; text-align: right; font-weight: 800; background: rgba(0,0,0,.35); padding: 4px 12px; border-radius: 16px; }
#time { font-size: 26px; font-variant-numeric: tabular-nums; } #best { font-size: 16px; opacity: .9; font-variant-numeric: tabular-nums; }
#mini { position: absolute; right: max(10px, env(safe-area-inset-right)); top: max(10px, env(safe-area-inset-top)); width: 150px; height: 150px;
  background: rgba(20,60,30,.45); border-radius: 50%; border: 4px solid rgba(255,255,255,.7); }
#speedo { position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%); width: 180px; height: 110px; }
#gauge { width: 180px; height: 110px; }
#kmh { position: absolute; left: 0; right: 0; top: 40px; text-align: center; font-size: 40px; font-weight: 900; font-variant-numeric: tabular-nums; }
#unit { position: absolute; left: 0; right: 0; top: 84px; text-align: center; font-size: 14px; font-weight: 700; opacity: .9; }
#gear { position: absolute; right: -6px; bottom: 6px; width: 34px; height: 34px; border-radius: 10px; background: #263238; display: grid; place-items: center; font-weight: 900; font-size: 20px; }
#wrong { position: absolute; left: 50%; top: 30%; transform: translateX(-50%); font-size: 60px; font-weight: 900; color: #fff; background: rgba(229,57,53,.85);
  padding: 8px 26px; border-radius: 26px; animation: blink .6s infinite alternate; white-space: nowrap; }
#wrong span { font-size: 36px; vertical-align: middle; }
@keyframes blink { from { opacity: 1; } to { opacity: .55; } }
#msg { position: absolute; left: 0; right: 0; top: 22%; text-align: center; font-size: 56px; font-weight: 900; opacity: 0; transition: opacity .3s; }
#msg.show { opacity: 1; animation: pop .5s; }
#count { position: fixed; inset: 0; display: grid; place-items: center; pointer-events: none; z-index: 12; font-size: 180px; font-weight: 900; color: #fff;
  text-shadow: 0 8px 0 rgba(0,0,0,.3), 0 0 40px rgba(255,180,0,.8); }
#count.go { color: #76ff03; }
#count .n { animation: pop .45s ease-out; }
@keyframes pop { from { transform: scale(2.2); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ---------- touch controls ---------- */
#touch { position: fixed; inset: 0; pointer-events: none; z-index: 11; }
.tb { pointer-events: auto; touch-action: none; display: grid; place-items: center; border-radius: 28px; font-size: 48px; color: #fff;
  background: rgba(0,0,0,.28); border: 4px solid rgba(255,255,255,.75); box-shadow: 0 6px 0 rgba(0,0,0,.25); }
.tb.on { background: rgba(255,255,255,.55); transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.25); }
.tleft { position: absolute; left: max(14px, env(safe-area-inset-left)); bottom: max(16px, env(safe-area-inset-bottom)); display: flex; gap: 14px; }
.steer { width: 124px; height: 124px; }
.tright { position: absolute; right: max(14px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom)); display: grid;
  grid-template-columns: 96px 140px; grid-template-rows: 80px 140px; gap: 12px; align-items: end; }
.gas { grid-column: 2; grid-row: 1 / 3; width: 140px; height: 150px; align-self: end; background: rgba(46,204,113,.55); font-size: 64px; }
.brake { grid-column: 1; grid-row: 2; width: 96px; height: 96px; background: rgba(229,57,53,.5); }
.drift { grid-column: 1; grid-row: 1; width: 84px; height: 72px; font-size: 34px; justify-self: center; }
body.easy .drift { visibility: hidden; }

/* ---------- screens ---------- */
.screen { position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 70px 16px 16px;
  overflow-y: auto; touch-action: pan-y; }
#select { background: linear-gradient(180deg, rgba(30,60,140,.15), rgba(10,20,60,.55)); justify-content: flex-start; }
#pause, #finish, #lost { background: rgba(10,20,50,.55); }
.logo { margin: 0; font-size: clamp(40px, 8vw, 76px); font-weight: 900; letter-spacing: -1px; text-shadow: 0 5px 0 #c62828, 0 9px 0 rgba(0,0,0,.25); font-style: italic; }
.logo span { color: #ffd54f; }
.sub { margin: 6px 0 14px; font-size: 26px; font-weight: 800; text-shadow: 0 2px 0 rgba(0,0,0,.35); }
#carGrid { display: flex; flex-wrap: wrap; gap: 14px; justify-content: center; max-width: 1000px; }
.carBtn { width: 170px; padding: 10px 8px 8px; border-radius: 24px; background: #fff; color: #1c2430; font-weight: 900; font-size: 22px;
  box-shadow: 0 7px 0 rgba(0,0,0,.28); border: 5px solid transparent; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.carBtn svg { width: 140px; height: 80px; }
.carBtn:active { transform: translateY(4px); box-shadow: 0 3px 0 rgba(0,0,0,.28); }
.focus, .carBtn.focus { outline: 6px solid #ffd54f !important; outline-offset: 2px; }
.row { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 16px; }
.col { display: flex; flex-direction: column; gap: 12px; align-items: stretch; }
.pill { padding: 12px 20px; border-radius: 999px; background: rgba(255,255,255,.92); color: #1c2430; font-weight: 800; font-size: 20px; box-shadow: 0 4px 0 rgba(0,0,0,.25); min-height: 56px; }
.pill.on { background: #b9f6ca; }
.big { display: inline-flex; align-items: center; justify-content: center; padding: 14px 26px; border-radius: 22px; background: #fff; color: #1c2430;
  font-weight: 900; font-size: 26px; box-shadow: 0 6px 0 rgba(0,0,0,.28); min-height: 70px; }
.big.green { background: #2ecc71; color: #fff; text-shadow: 0 2px 0 rgba(0,0,0,.25); }
.big:active, .pill:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.28); }
.help { margin-top: 14px; font-size: 14px; opacity: .85; text-align: center; max-width: 760px; text-shadow: 0 1px 0 rgba(0,0,0,.4); }
.panel { background: linear-gradient(#3949ab, #283593); border-radius: 30px; padding: 22px 28px 26px; box-shadow: 0 10px 0 rgba(0,0,0,.3); text-align: center; max-width: 94vw; }
.panel h2 { margin: 0 0 14px; font-size: 40px; font-weight: 900; }
#medal { font-size: 96px; line-height: 1; animation: pop .6s; }
#results { margin: 6px auto 4px; display: grid; gap: 6px; min-width: 280px; }
.res { display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,.12); border-radius: 14px; padding: 6px 12px; font-size: 20px; font-weight: 800; }
.res.me { background: rgba(255,213,79,.35); }
.res .dot { width: 22px; height: 22px; border-radius: 50%; border: 3px solid #fff; }
.res .t { margin-left: auto; font-variant-numeric: tabular-nums; opacity: .9; }
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 25; }

@media (max-width: 760px), (max-height: 480px) {
  .steer { width: 100px; height: 100px; }
  .tright { grid-template-columns: 84px 116px; grid-template-rows: 70px 116px; }
  .gas { width: 116px; height: 124px; } .brake { width: 84px; height: 84px; } .drift { width: 74px; height: 62px; }
  #timeBox { left: auto; right: 10px; top: 170px; width: auto; }
  #lapBox { left: auto; right: 170px; top: 14px; }
  #posBox { left: auto; right: 260px; transform: none; }
  #mini { width: 130px; height: 130px; }
  #speedo { transform: translateX(-50%) scale(.85); transform-origin: bottom center; }
  .carBtn { width: 140px; } .carBtn svg { width: 116px; height: 66px; }
}
@media (max-width: 560px) {
  #posBox { right: auto; left: 12px; top: 82px; }
  #lapBox { right: auto; left: 140px; top: 88px; }
}
@media (max-height: 600px) {
  .panel { padding: 14px 22px 18px; }
  .panel h2 { font-size: 30px; margin-bottom: 8px; }
  #medal { font-size: 64px; }
  .res { font-size: 17px; padding: 3px 10px; }
  #results { gap: 4px; }
  .big { min-height: 60px; font-size: 22px; padding: 10px 20px; }
  .row { margin-top: 10px; }
  .col { gap: 8px; }
}
