* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden;
  background: #11151c; color: #eef1f6;
  font-family: -apple-system, "SF Pro Text", "Segoe UI", Helvetica, Arial, sans-serif;
}
#gameCanvas { position: fixed; inset: 0; display: block; width: 100%; height: 100%; }

.hidden { display: none !important; }

#loading, #menu {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 20%, #273043, #0e1117 75%);
  z-index: 20;
}
.panel {
  width: min(480px, 92vw); background: rgba(20, 24, 32, 0.82);
  border: 1px solid rgba(255,255,255,0.08); border-radius: 16px;
  padding: 28px 30px 24px; backdrop-filter: blur(6px);
  box-shadow: 0 20px 60px rgba(0,0,0,0.45);
}
.panel h1 { margin: 0 0 4px; font-size: 1.5rem; letter-spacing: -0.01em; }
.subtitle { margin: 0 0 20px; color: #9aa4b6; font-size: 0.92rem; }
#loadingText { color: #9aa4b6; }

.field { display: block; margin-bottom: 16px; }
.field > span { display: block; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: #8089a0; margin-bottom: 6px; }
#nameInput {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.15);
  background: #171b24; color: #fff; font-size: 1rem;
}
#nameInput:focus { outline: 2px solid #4da3ff; }

#colorSwatches { display: flex; gap: 8px; flex-wrap: wrap; }
#colorSwatches button {
  width: 32px; height: 32px; border-radius: 50%; border: 2px solid transparent; cursor: pointer;
  transition: transform 0.1s ease;
}
#colorSwatches button.selected { border-color: #fff; transform: scale(1.12); }

#joinBtn {
  width: 100%; padding: 12px; font-size: 1.05rem; font-weight: 600; border: none; border-radius: 10px;
  background: linear-gradient(135deg, #ff5a5f, #ff7a45); color: #fff; cursor: pointer; margin-top: 4px;
}
#joinBtn:disabled { opacity: 0.5; cursor: default; }
#joinBtn:not(:disabled):hover { filter: brightness(1.08); }

.status { text-align: center; font-size: 0.82rem; color: #8089a0; margin: 10px 0 0; }

.howto { margin-top: 18px; padding-top: 14px; border-top: 1px solid rgba(255,255,255,0.08); font-size: 0.82rem; color: #b7bfcf; }
.howto strong { display: block; margin-bottom: 6px; color: #fff; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; }
kbd {
  display: inline-block; padding: 1px 6px; border-radius: 5px; background: #272d3a; border: 1px solid #3a4151;
  font-size: 0.78rem; font-family: inherit;
}
.share { margin: 14px 0 0; font-size: 0.76rem; color: #6b7384; }

#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#topLeft { position: absolute; top: 18px; left: 18px; }
#lapCounter {
  font-size: 1.3rem; font-weight: 700; background: rgba(10,13,18,0.55); padding: 6px 14px;
  border-radius: 10px; backdrop-filter: blur(4px);
}
#speedReadout {
  margin-top: 6px; font-size: 1.6rem; font-weight: 800; background: rgba(10,13,18,0.55);
  padding: 4px 14px; border-radius: 10px; width: fit-content;
}
#speedReadout span { font-size: 0.8rem; font-weight: 500; color: #9aa4b6; }

#standingsPanel {
  position: absolute; top: 18px; right: 18px; width: 190px; background: rgba(10,13,18,0.55);
  border-radius: 10px; padding: 10px 12px; backdrop-filter: blur(4px);
}
.standingsTitle { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: #8089a0; margin-bottom: 6px; }
#standingsList { list-style: none; margin: 0; padding: 0; font-size: 0.86rem; }
#standingsList li { display: flex; align-items: center; gap: 6px; padding: 2px 0; }
#standingsList .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
#standingsList .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#standingsList .you { color: #ffd166; font-weight: 700; }

#minimap { position: absolute; bottom: 18px; left: 18px; border-radius: 10px; opacity: 0.95; }

/* grand-prix start lights: five columns of two reds, on one by one, then out */
#startLights {
  position: absolute; top: 9%; left: 50%; transform: translateX(-50%);
  display: flex; gap: 14px; padding: 12px 18px; border-radius: 12px;
  background: linear-gradient(#1b1d22, #0c0d10); box-shadow: 0 10px 30px rgba(0,0,0,0.55), inset 0 0 0 2px #2b2e35;
  transition: opacity 0.6s ease 0.5s;
}
#startLights.out { opacity: 0; }
#startLights .pod {
  display: flex; flex-direction: column; gap: 10px; padding: 9px; border-radius: 10px;
  background: #050506; box-shadow: inset 0 0 0 2px #24262c;
}
#startLights .lamp {
  width: 46px; height: 46px; border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #3a1214, #1a0607 70%);
  box-shadow: inset 0 -3px 6px rgba(0,0,0,0.6);
}
#startLights .lamp.on {
  background: radial-gradient(circle at 40% 35%, #ffb3b3, #ff1e1e 45%, #b30000 80%);
  box-shadow: 0 0 22px 6px rgba(255,30,30,0.75), inset 0 -3px 6px rgba(0,0,0,0.3);
}
#startGo {
  position: absolute; top: 34%; left: 50%; transform: translate(-50%, -50%);
  font-size: 5.5rem; font-weight: 900; color: #3dff7a; letter-spacing: 0.04em;
  text-shadow: 0 0 24px rgba(61,255,122,0.6), 0 6px 30px rgba(0,0,0,0.6);
  animation: goPop 1.3s ease-out forwards;
}
@keyframes goPop { 0% { transform: translate(-50%, -50%) scale(0.6); opacity: 1; } 70% { opacity: 1; } 100% { transform: translate(-50%, -50%) scale(1.25); opacity: 0; } }
#finishBanner {
  position: absolute; top: 30%; left: 50%; transform: translate(-50%, -50%); text-align: center;
  background: rgba(10,13,18,0.75); padding: 20px 34px; border-radius: 14px; font-size: 1.6rem; font-weight: 800;
}
#finishSub { font-size: 1rem; font-weight: 500; color: #ffd166; margin-top: 6px; }

#chat {
  position: absolute; bottom: 18px; right: 18px; width: 260px; pointer-events: auto;
}
#chatLog {
  max-height: 150px; overflow-y: auto; font-size: 0.8rem; margin-bottom: 6px;
  display: flex; flex-direction: column; gap: 3px;
}
#chatLog div { background: rgba(10,13,18,0.55); padding: 3px 8px; border-radius: 6px; }
#chatLog b { margin-right: 4px; }
#chatInput {
  width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.18);
  background: rgba(10,13,18,0.75); color: #fff; font-size: 0.85rem;
}

#hints {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
  font-size: 0.72rem; color: rgba(255,255,255,0.55); background: rgba(10,13,18,0.4);
  padding: 4px 10px; border-radius: 8px;
}

#nameTags { position: fixed; inset: 0; pointer-events: none; z-index: 9; }
.nameTag {
  position: absolute; transform: translate(-50%, -100%); font-size: 0.72rem; font-weight: 700;
  padding: 2px 7px; border-radius: 6px; background: rgba(10,13,18,0.6); white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
}

/* crash / wobble prompt */
#crashHud { position: absolute; top: 34%; left: 50%; transform: translate(-50%, -50%); text-align: center;
  pointer-events: none; display: none; font-weight: 800; letter-spacing: 0.04em; }
#crashHud.show { display: block; }
#crashHud .msg { font-size: 30px; color: #fff; text-shadow: 0 2px 8px rgba(0,0,0,0.7); }
#crashHud.wobble .msg { color: #ffd166; font-size: 22px; animation: wob 0.12s infinite alternate; }
#crashHud.fallen .msg { color: #ff5a5f; }
#crashHud .bar { display: none; width: 260px; height: 12px; margin: 10px auto 0; border-radius: 6px;
  background: rgba(0,0,0,0.45); overflow: hidden; }
#crashHud.fallen .bar { display: block; }
#crashHud .fill { height: 100%; width: 0; background: linear-gradient(90deg, #ffd166, #2ecc71); transition: width 0.08s; }
@keyframes wob { from { transform: translateX(-4px); } to { transform: translateX(4px); } }

/* KP's name tag + the "you got pulled over" toast */
.nameTag.kpTag { background: rgba(120, 0, 0, 0.75); color: #fff !important; font-weight: 800; padding: 2px 8px; }
#toast { position: absolute; top: 22%; left: 50%; transform: translateX(-50%); background: rgba(120, 10, 10, 0.85);
  color: #fff; font-weight: 700; font-size: 20px; padding: 10px 18px; border-radius: 10px; opacity: 0;
  transition: opacity 0.25s; pointer-events: none; }
#toast.show { opacity: 1; }

/* your own racer's name: top center of the HUD (no label over your head) */
#playerName { position: absolute; top: 14px; left: 50%; transform: translateX(-50%); padding: 6px 16px;
  border-radius: 10px; background: rgba(10, 12, 18, 0.55); color: #fff; font-weight: 700; font-size: 18px;
  letter-spacing: 0.02em; pointer-events: none; }

/* ---- lobby / waiting room / results ---- */
.panel.wide { width: min(860px, 94vw); max-height: 92vh; overflow-y: auto; }
.panel h2 { font-size: 1rem; margin: 14px 0 8px; color: #cfd6e4; }
.panel h2 small { color: #8a93a6; font-weight: 400; }
.cols { display: flex; gap: 22px; flex-wrap: wrap; }
.col { flex: 1 1 320px; min-width: 0; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.list li { display: flex; align-items: center; gap: 8px; background: rgba(255,255,255,0.06); border-radius: 8px; padding: 8px 10px; }
.list li.empty { color: #8a93a6; justify-content: center; }
.list li .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li .meta { color: #8a93a6; font-size: 0.85em; }
.list li .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.list li .ready { color: #2ecc71; font-weight: 700; }
.list li .notready { color: #8a93a6; }
.list li button { padding: 6px 12px; font-size: 0.9em; }
#menu input, #menu select { width: 100%; box-sizing: border-box; padding: 8px 10px; border-radius: 8px; border: 1px solid #39425a;
  background: #141926; color: #fff; font-size: 0.95rem; }
#menu button { padding: 10px 16px; border: 0; border-radius: 10px; background: #4da3ff; color: #fff; font-weight: 700;
  font-size: 1rem; cursor: pointer; margin-top: 8px; }
#menu button:disabled { opacity: 0.45; cursor: default; }
#menu button.secondary { background: #39425a; }
#menu button.primary { background: #2ecc71; width: 100%; }
#readyBtn { width: 100%; }
#menu input[type="checkbox"] { width: 18px; height: 18px; flex: none; margin: 1px 0 0; accent-color: #4da3ff; }
.checkRow { display: flex !important; flex-direction: row !important; align-items: flex-start; gap: 8px; cursor: pointer; color: #cfd6e4; font-size: 0.9rem; }
.checkRow small { color: #8a93a6; }
.checkRow.disabled { opacity: 0.5; cursor: default; }
.opts .checkRow.full { grid-column: 1 / -1; }
#readyBtn.isReady { background: #2ecc71; }
.seg { display: flex; gap: 6px; }
.seg button { flex: 1; background: #39425a !important; margin-top: 0 !important; font-size: 0.9rem !important; }
.seg button.on { background: #4da3ff !important; }
.seg small { display: block; font-weight: 400; opacity: 0.8; font-size: 0.75em; }
.opts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.opts label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85em; color: #aab3c5; }
.results { margin: 0; padding-left: 0; list-style: none; counter-reset: place; }
.results li { counter-increment: place; display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px;
  background: rgba(255,255,255,0.05); margin-bottom: 5px; }
.results li::before { content: counter(place); width: 1.6em; font-weight: 800; color: #ffd166; }
.results li .dot { width: 12px; height: 12px; border-radius: 50%; }
.results li .grow { flex: 1; }
.results li .rtime { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 5.2em; text-align: right; }
.results li .rtime.dnf { color: #8a93a6; }
.results li .gap { font-variant-numeric: tabular-nums; min-width: 4.6em; text-align: right; }
#screenResults { width: min(460px, 92vw); }
#cancelRaceBtn { position: absolute; top: auto; bottom: 150px; right: 18px; padding: 8px 14px; border: 0; border-radius: 10px;
  background: #c0392b; color: #fff; font-weight: 700; cursor: pointer; z-index: 5;
  pointer-events: auto; /* the HUD layer ignores the mouse; this button mustn't */ }
#cancelRaceBtn.confirm { background: #ff3b2f; box-shadow: 0 0 0 3px rgba(255,255,255,0.7); }

/* ---- phones & tablets (touch.js) ---- */
body.touch { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
body.touch #menu input { font-size: 16px; } /* < 16px makes iOS zoom in on focus */
#touchSteer { position: absolute; inset: 0; pointer-events: auto; touch-action: none; }
#touchKick {
  position: absolute; right: max(18px, env(safe-area-inset-right)); bottom: max(18px, env(safe-area-inset-bottom));
  width: clamp(112px, 26vmin, 170px); aspect-ratio: 1; border-radius: 50%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 40%, rgba(77,163,255,0.55), rgba(30,80,160,0.45));
  border: 3px solid rgba(255,255,255,0.55); box-shadow: 0 6px 24px rgba(0,0,0,0.45);
  pointer-events: auto; touch-action: none; color: #fff;
}
#touchKick span { font-size: clamp(20px, 5vmin, 28px); font-weight: 900; letter-spacing: 0.08em; }
#touchKick small { font-size: 11px; opacity: 0.75; margin-top: 2px; }
#touchKick.hit { animation: kickHit 0.16s ease-out; }
@keyframes kickHit { 0% { transform: scale(0.88); background: rgba(120,200,255,0.85); } 100% { transform: scale(1); } }
#touchHorn, #touchChat {
  position: absolute; width: 46px; height: 46px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.4);
  background: rgba(10,13,18,0.6); font-size: 20px; pointer-events: auto; touch-action: none; padding: 0;
  right: calc(max(18px, env(safe-area-inset-right)) + clamp(112px, 26vmin, 170px) + 12px);
}
#touchHorn { bottom: max(18px, env(safe-area-inset-bottom)); }
#touchChat { bottom: calc(max(18px, env(safe-area-inset-bottom)) + 56px); }
#touchGuide {
  position: absolute; left: 50%; top: 58%; transform: translateX(-50%); display: flex; flex-direction: column;
  align-items: center; gap: 4px; font-weight: 700; color: rgba(255,255,255,0.8); text-shadow: 0 2px 6px rgba(0,0,0,0.7);
  transition: opacity 1s ease 2s; pointer-events: none;
}
#touchGuide.used { opacity: 0; }
body.touch #hints { display: none; }
body.touch #minimap { width: clamp(170px, 30vw, 260px); height: auto; left: max(18px, env(safe-area-inset-left)); bottom: max(18px, env(safe-area-inset-bottom)); }
body.touch #chat { left: 50%; right: auto; transform: translateX(-50%); bottom: 12px; width: min(260px, 34vw); }
body.touch #chatLog { max-height: 72px; font-size: 0.72rem; }
body.touch #cancelRaceBtn { bottom: auto; right: auto; top: 160px; left: max(18px, env(safe-area-inset-left)); }
@media (orientation: portrait) {
  body.touch #hud::after {
    content: "↻ Turn your device sideways for a wider view"; position: absolute; top: 44%; left: 50%;
    transform: translateX(-50%); white-space: nowrap; font-size: 0.8rem; color: rgba(255,255,255,0.75);
    background: rgba(10,13,18,0.55); padding: 4px 10px; border-radius: 8px; pointer-events: none;
  }
  body.touch #chat { top: 120px; bottom: auto; }
}
/* small screens (phones): shrink the HUD so the view isn't covered */
@media (max-width: 760px), (max-height: 480px) {
  #topLeft { top: 10px; left: 10px; }
  #lapCounter { font-size: 1rem; padding: 4px 10px; }
  #speedReadout { font-size: 1.15rem; padding: 2px 10px; }
  #playerName { top: 10px; font-size: 14px; padding: 4px 10px; }
  #standingsPanel { top: 10px; right: 10px; width: 140px; padding: 6px 8px; }
  #standingsList { font-size: 0.72rem; }
  #startLights { top: 46px; gap: 8px; padding: 8px 10px; }
  #startLights .pod { gap: 6px; padding: 6px; }
  #startLights .lamp { width: 28px; height: 28px; }
  #startGo { font-size: 3.5rem; }
  #crashHud .msg { font-size: 22px; }
  #crashHud .bar { width: 200px; }
  #finishBanner { font-size: 1.2rem; padding: 14px 22px; }
  body.touch #cancelRaceBtn { top: 118px; left: 10px; }
  .panel { padding: 18px 16px 16px; }
}

/* ---- your place in the race + podium celebration (placing.js) ---- */
#placeReadout {
  margin-top: 6px; width: fit-content; padding: 2px 14px 4px; border-radius: 10px; background: rgba(10,13,18,0.55);
  font-size: 2.1rem; font-weight: 900; line-height: 1.1; font-variant-numeric: tabular-nums;
}
#placeReadout:empty { display: none; }
#placeReadout sup { font-size: 0.5em; margin-left: 1px; }
#placeReadout span { font-size: 0.42em; font-weight: 600; color: #9aa4b6; margin-left: 6px; }
#placeReadout.p1 { color: #ffd166; text-shadow: 0 0 14px rgba(255,209,102,0.55); }
#placeReadout.p2 { color: #e3e7ef; text-shadow: 0 0 12px rgba(227,231,239,0.45); }
#placeReadout.p3 { color: #e0a370; text-shadow: 0 0 12px rgba(224,163,112,0.45); }
#placeReadout.pop { animation: placePop 0.7s ease-out; }
@keyframes placePop { 0% { transform: scale(1); } 25% { transform: scale(1.35); } 100% { transform: scale(1); } }

#celebrate { position: fixed; inset: 0; z-index: 40; pointer-events: none; }
#celebrate canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#celebrate .card {
  position: absolute; top: 16%; left: 50%; transform: translateX(-50%); text-align: center;
  padding: 18px 40px 20px; border-radius: 18px; background: rgba(10,13,18,0.78);
  box-shadow: 0 16px 50px rgba(0,0,0,0.5); animation: celebIn 0.6s cubic-bezier(.2,1.6,.4,1) both;
  transition: opacity 1s;
}
#celebrate.fade .card { opacity: 0; }
#celebrate .medal { font-size: 5rem; line-height: 1; animation: medalSpin 1.2s ease-out; }
#celebrate .word { font-size: 2.6rem; font-weight: 900; letter-spacing: 0.04em; margin-top: 6px; }
#celebrate .sub { font-size: 1rem; color: #cfd6e4; margin-top: 2px; }
#celebrate.p1 .word { color: #ffd166; text-shadow: 0 0 22px rgba(255,209,102,0.6); }
#celebrate.p2 .word { color: #e3e7ef; text-shadow: 0 0 18px rgba(227,231,239,0.5); }
#celebrate.p3 .word { color: #e0a370; text-shadow: 0 0 18px rgba(224,163,112,0.5); }
@keyframes celebIn { from { transform: translateX(-50%) scale(0.4); opacity: 0; } to { transform: translateX(-50%) scale(1); opacity: 1; } }
@keyframes medalSpin { from { transform: rotateY(0deg) scale(0.6); } to { transform: rotateY(720deg) scale(1); } }
@media (max-width: 760px), (max-height: 480px) {
  #placeReadout { font-size: 1.5rem; padding: 1px 10px 3px; }
  #celebrate .medal { font-size: 3.4rem; }
  #celebrate .word { font-size: 1.7rem; }
  #celebrate .card { top: 10%; padding: 12px 26px 14px; }
}

/* passing / being passed mid-race */
#placeBanner {
  position: absolute; top: 62px; left: 50%; transform: translate(-50%, -14px); text-align: center;
  padding: 8px 22px 9px; border-radius: 12px; background: rgba(10,13,18,0.78); opacity: 0; pointer-events: none;
  transition: opacity 0.25s, transform 0.25s; white-space: nowrap; box-shadow: 0 8px 26px rgba(0,0,0,0.45);
}
#placeBanner.show { opacity: 1; transform: translate(-50%, 0); }
#placeBanner .big { font-size: 1.25rem; font-weight: 800; }
#placeBanner .small { font-size: 0.85rem; color: #cfd6e4; margin-top: 1px; }
#placeBanner.up { border: 2px solid rgba(46,204,113,0.8); }
#placeBanner.up .big { color: #5ff09a; }
#placeBanner.down { border: 2px solid rgba(255,90,95,0.7); }
#placeBanner.down .big { color: #ff8a8d; }
#placeBanner.podium { padding: 12px 30px 13px; }
#placeBanner.podium.show { animation: podiumIn 0.5s cubic-bezier(.2,1.6,.4,1); }
#placeBanner.podium .big { font-size: 1.9rem; font-weight: 900; letter-spacing: 0.02em; }
#placeBanner.p1 { border-color: #ffd166; } #placeBanner.p1 .big { color: #ffd166; text-shadow: 0 0 18px rgba(255,209,102,0.6); }
#placeBanner.p2 { border-color: #e3e7ef; } #placeBanner.p2 .big { color: #eef1f6; text-shadow: 0 0 14px rgba(227,231,239,0.5); }
#placeBanner.p3 { border-color: #e0a370; } #placeBanner.p3 .big { color: #f0b080; text-shadow: 0 0 14px rgba(224,163,112,0.5); }
@keyframes podiumIn { from { transform: translate(-50%, 0) scale(0.5); } to { transform: translate(-50%, 0) scale(1); } }
.placeBit {
  position: absolute; top: 96px; left: 50%; width: 9px; height: 6px; border-radius: 1px; pointer-events: none;
  animation: placeBit 1.5s cubic-bezier(.15,.6,.4,1) forwards;
}
@keyframes placeBit {
  from { transform: translate(-50%, 0) rotate(0); opacity: 1; }
  to { transform: translate(calc(-50% + var(--dx)), var(--dy)) rotate(var(--rot)); opacity: 0; }
}
@media (max-width: 760px), (max-height: 480px) {
  #placeBanner { top: 44px; padding: 6px 14px 7px; }
  #placeBanner .big { font-size: 1rem; }
  #placeBanner.podium .big { font-size: 1.35rem; }
}
