:root {
  --chrome: linear-gradient(160deg, #ffffff 0%, #9aa1a8 35%, #f3f5f7 55%, #6b7178 100%);
  --slate: #26323b;
  --slate-2: #34434e;
  --orange: #ff9a3c;
  --red: #d8342a;
  --cyan: #7fe3ff;
  --cream: #f4ecd8;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #1b2228; color: var(--cream);
  font-family: "Share Tech Mono", ui-monospace, monospace;
  user-select: none;
}
#game, #game canvas { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
.hidden { display: none !important; }

kbd {
  display: inline-block; padding: 1px 6px; margin: 0 2px;
  border-radius: 4px; border: 1px solid #9aa1a8;
  background: #1c252c; color: #fff; font: 12px "Share Tech Mono", monospace;
  box-shadow: 0 2px 0 #0b0f12;
}

/* ---------- title banner ---------- */
#banner {
  position: fixed; top: 0; left: 50%; transform: translateX(-50%);
  width: 300px; padding: 6px 0 12px; z-index: 5;
  display: flex; flex-direction: column; align-items: center; line-height: 0.95;
  font-family: "Bungee", sans-serif; font-size: 30px; color: #f1f3f5;
  text-shadow: 0 2px 0 #10161a, 0 0 10px rgba(255,255,255,0.15);
  background: linear-gradient(180deg, #3c4b56, #222c33);
  clip-path: polygon(0 0, 100% 0, 88% 100%, 12% 100%);
  filter: drop-shadow(0 3px 6px rgba(0,0,0,0.5));
  pointer-events: none;
}
#banner::after {
  content: ""; position: absolute; inset: auto 12% 0 12%; height: 3px;
  background: var(--chrome);
}

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 4; }

#health { position: absolute; top: 18px; left: 22px; width: 380px; }
#health .label {
  font-family: "Bungee", sans-serif; font-size: 24px; color: var(--orange);
  margin-left: 52px; letter-spacing: 1px;
  text-shadow: 0 2px 0 #4a1d05, 0 0 8px rgba(255,140,40,0.5);
}
.bar-row { display: flex; align-items: center; }
.orb {
  width: 54px; height: 54px; border-radius: 50%; flex: none;
  background: var(--chrome); padding: 5px; position: relative; z-index: 2;
  box-shadow: 0 3px 8px rgba(0,0,0,0.5);
}
.orb-core {
  width: 100%; height: 100%; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffd2a8, #e2572c 45%, #6b1408 100%);
  box-shadow: inset 0 -4px 8px rgba(0,0,0,0.5);
}
.bar {
  flex: 1; height: 32px; margin-left: -10px; padding: 5px 6px 5px 14px;
  border-radius: 0 8px 8px 0; background: var(--chrome);
  box-shadow: 0 3px 8px rgba(0,0,0,0.5);
}
.bar-track { height: 100%; background: #1b2328; border-radius: 0 4px 4px 0; overflow: hidden; }
#health-fill {
  height: 100%; width: 100%;
  background: linear-gradient(180deg, #ff6a55 0%, #d0261c 55%, #8e140d 100%);
  box-shadow: inset 0 0 0 2px rgba(0,0,0,0.35);
  transition: width 0.2s ease-out;
}
#health-fill.low { animation: pulse 0.6s infinite alternate; }
@keyframes pulse { to { filter: brightness(1.6); } }

#race-info {
  margin: 10px 0 0 4px; display: inline-flex; gap: 16px;
  padding: 6px 14px; border-radius: 8px;
  background: rgba(20, 28, 34, 0.72); border: 1px solid rgba(255,255,255,0.18);
  font-size: 18px;
}
#race-info b { font-family: "Bungee", sans-serif; font-weight: normal; font-size: 12px; color: var(--orange); margin-right: 6px; }
#race-info i { font-style: normal; color: #fff; }

#minimap-wrap {
  position: absolute; top: 14px; right: 18px; width: 220px; height: 220px;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.55));
}
#minimap { width: 220px; height: 220px; }

#speedo-wrap { position: absolute; left: 22px; bottom: 18px; width: 220px; }
#speedo { width: 220px; height: 220px; filter: drop-shadow(0 4px 10px rgba(0,0,0,0.6)); }
#boost-label {
  margin-top: 2px; text-align: center; font-family: "Bungee", sans-serif;
  font-size: 11px; color: var(--cyan); text-shadow: 0 1px 2px #000;
}

#shield-btn {
  position: absolute; right: 26px; bottom: 26px; width: 120px; height: 120px;
  border-radius: 50%; border: none; padding: 9px; cursor: pointer;
  background: var(--chrome); pointer-events: auto;
  box-shadow: 0 5px 14px rgba(0,0,0,0.6);
}
#shield-btn::before {
  content: ""; position: absolute; inset: 9px; border-radius: 50%;
  background: radial-gradient(circle at 50% 40%, #2d5a6e, #12252f 75%);
  box-shadow: inset 0 0 14px rgba(0,0,0,0.8);
}
#shield-btn svg { position: relative; width: 66px; height: 66px; margin-top: 4px; filter: drop-shadow(0 0 6px rgba(127,227,255,0.6)); }
#shield-btn.active svg { animation: glow 0.3s infinite alternate; }
@keyframes glow { to { filter: drop-shadow(0 0 16px #bff4ff) brightness(1.3); } }
#shield-cd { position: absolute; inset: 9px; border-radius: 50%; pointer-events: none; }
#shield-btn kbd { position: absolute; bottom: -4px; left: 50%; transform: translateX(-50%); }

#msg, #sub-msg {
  position: absolute; left: 0; right: 0; text-align: center;
  font-family: "Bungee", sans-serif; color: #fff; pointer-events: none;
}
#msg { top: 30%; font-size: 84px; text-shadow: 0 4px 0 #8a2a12, 0 0 30px rgba(255,140,60,0.7); }
#sub-msg { top: calc(30% + 100px); font-size: 22px; color: var(--orange); text-shadow: 0 2px 0 #000; }
#msg.pop { animation: pop 0.35s ease-out; }
@keyframes pop { from { transform: scale(1.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }

#vignette {
  position: fixed; inset: 0; pointer-events: none; z-index: 3; opacity: 0;
  background: radial-gradient(ellipse at center, transparent 45%, rgba(220,30,20,0.75) 100%);
}
#shield-tint {
  position: fixed; inset: 0; pointer-events: none; z-index: 3; opacity: 0;
  background: radial-gradient(ellipse at center, transparent 55%, rgba(110,220,255,0.35) 100%);
  transition: opacity 0.2s;
}

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at center, rgba(20,26,32,0.35), rgba(10,14,18,0.85));
  padding: 16px;
}
.panel {
  width: min(760px, 100%); max-height: 100%; overflow: auto;
  padding: 28px 32px; border-radius: 18px;
  background: linear-gradient(180deg, rgba(48,62,72,0.95), rgba(26,34,40,0.96));
  border: 3px solid #c9ced3; box-shadow: 0 20px 60px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,255,255,0.3);
  text-align: center;
}
.panel h1 {
  margin: 0; font-family: "Bungee", sans-serif; font-weight: normal; line-height: 0.9;
  font-size: clamp(44px, 9vw, 86px); display: flex; flex-direction: column;
  color: #fff; text-shadow: 0 5px 0 #8a2a12, 0 0 40px rgba(255,140,60,0.5);
}
.panel h1 span:first-child { color: var(--orange); }
.panel h2 { font-family: "Bungee", sans-serif; font-weight: normal; font-size: 48px; margin: 0 0 10px; color: var(--orange); text-shadow: 0 3px 0 #000; }
.panel h3 { font-family: "Bungee", sans-serif; font-weight: normal; font-size: 15px; color: var(--orange); margin: 0 0 10px; }
.tag { font-size: 17px; color: #d8dde2; margin: 14px 0 20px; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; margin-top: 24px; text-align: left; }
@media (max-width: 640px) { .cols { grid-template-columns: 1fr; } }
.controls p { margin: 7px 0; font-size: 15px; }
.hint { color: #b9c3cb; font-size: 13px !important; line-height: 1.4; }
.g { color: #6dff9a; } .o { color: #ffb15c; }

button { font-family: "Bungee", sans-serif; cursor: pointer; }
button.big {
  font-size: 24px; padding: 12px 38px; border-radius: 40px; border: 3px solid #f2f4f6;
  color: #fff; background: linear-gradient(180deg, #e8513a, #a3200f);
  box-shadow: 0 5px 0 #5c1208, 0 10px 24px rgba(0,0,0,0.5);
}
button.big:hover { filter: brightness(1.12); }
button.big:active { transform: translateY(3px); box-shadow: 0 2px 0 #5c1208; }

ol { margin: 0; padding-left: 26px; font-size: 15px; }
ol li { margin: 4px 0; }
ol li .t { float: right; color: #fff; }
ol li.me { color: var(--cyan); }
ol li.empty { list-style: none; margin-left: -26px; color: #9aa5ae; }

#res-stats { font-size: 20px; line-height: 1.6; margin-bottom: 14px; }
#res-stats b { color: var(--orange); font-weight: normal; }
#score-form { display: flex; gap: 8px; justify-content: center; margin: 10px 0 16px; flex-wrap: wrap; }
#score-form input {
  font: 18px "Share Tech Mono", monospace; padding: 8px 12px; border-radius: 8px;
  border: 2px solid #9aa1a8; background: #141b20; color: #fff; width: 240px;
}
#score-form button { font-size: 15px; padding: 8px 16px; border-radius: 8px; border: 2px solid #ddd; background: #2e7d9a; color: #fff; }
#res-board { text-align: left; max-width: 420px; margin: 0 auto 18px; }

.loader { font-family: "Bungee", sans-serif; font-size: 26px; color: var(--orange); animation: pulse 0.7s infinite alternate; }

@media (max-width: 760px) {
  #banner { width: 200px; font-size: 20px; }
  #health { width: 240px; left: 12px; }
  #health .label { font-size: 16px; }
  #race-info { font-size: 13px; gap: 8px; }
  #minimap-wrap, #minimap { width: 130px; height: 130px; }
  #speedo-wrap { width: 140px; left: 10px; }
  #speedo { width: 140px; height: 140px; }
  #shield-btn { width: 86px; height: 86px; right: 14px; bottom: 14px; }
  #shield-btn svg { width: 46px; height: 46px; }
  #msg { font-size: 54px; }
}

/* ---------- title screen (key art background) ---------- */
#menu {
  position: fixed; inset: 0; z-index: 10;
  background: #1b2228 url("../img/title.jpg") left center / cover no-repeat;
  display: flex; align-items: flex-end; justify-content: flex-start;
  padding: 0 0 4vh 3vw;
}
#menu::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(90deg, rgba(12,16,20,0.55) 0%, rgba(12,16,20,0.15) 45%, transparent 60%),
              linear-gradient(0deg, rgba(12,16,20,0.6) 0%, transparent 45%);
}
.menu-side {
  position: relative; width: min(560px, 94vw); text-align: left;
  padding: 20px 24px; border-radius: 16px;
  background: rgba(22, 30, 37, 0.78); backdrop-filter: blur(6px);
  border: 2px solid rgba(220, 226, 232, 0.7);
  box-shadow: 0 16px 40px rgba(0,0,0,0.55);
}
.menu-side .tag { margin: 0 0 14px; }
.promo {
  display: inline-flex; align-items: center; gap: 10px; margin: 14px 0 4px; padding: 7px 14px 7px 9px;
  color: #f2f4f6; text-decoration: none; font-family: "Bungee", sans-serif; font-size: 14px; line-height: 1.1;
  background: rgba(20, 28, 34, 0.75); border: 2px solid rgba(240, 244, 248, 0.6); border-radius: 12px;
  pointer-events: auto; -webkit-tap-highlight-color: transparent;
}
.promo:hover, .promo:active { border-color: #ff9a3c; filter: brightness(1.15); }
.promo svg { width: 34px; height: 34px; flex: none; }
.promo svg rect { fill: #d8321f; }
.promo svg path { fill: #fff; }
.promo small { display: block; font-family: "Share Tech Mono", monospace; font-size: 12px; color: #b9c3cb; letter-spacing: 0; }
.start-row { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
.start-row .promo { margin: 0; }
#results .promo { display: flex; width: fit-content; margin: 12px auto 0; }
body.touch .promo { font-size: 13px; }
body.touch #results .promo { margin-top: 10px; }
.menu-side .cols { margin-top: 18px; }
body.on-menu #banner { display: none; }
@media (max-height: 700px) {
  .menu-side .cols { display: none; }
}

/* ---------- touch controls (phones / tablets) ---------- */
#touch, #rotate { display: none; }
body.touch { touch-action: none; -webkit-touch-callout: none; -webkit-user-select: none; }
body.touch #touch { display: block; }
.tc {
  pointer-events: auto; touch-action: none; -webkit-tap-highlight-color: transparent;
  width: 76px; height: 76px; border-radius: 50%; padding: 0;
  font-family: "Bungee", sans-serif; font-size: 13px; color: #fff;
  border: 3px solid rgba(240, 244, 248, 0.85);
  background: radial-gradient(circle at 50% 35%, rgba(70, 90, 104, 0.85), rgba(20, 28, 34, 0.85));
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
.tc.on { transform: scale(0.93); filter: brightness(1.5); }
.tc.gas { width: 92px; height: 92px; font-size: 18px; background: radial-gradient(circle at 50% 35%, #3fbf6a, #1c6b36); }
.tc.boost { background: radial-gradient(circle at 50% 35%, #ff9a3c, #a3480f); }
.tc.small { font-size: 11px; }
/* floating joystick: the base jumps to wherever the thumb lands in the zone */
#stick-zone {
  position: absolute; left: 0; bottom: 0; width: 40%; height: 68%;
  pointer-events: auto; touch-action: none; -webkit-tap-highlight-color: transparent;
}
#stick-base {
  position: absolute; left: calc(max(14px, env(safe-area-inset-left)) + 90px); top: calc(100% - 110px);
  width: 160px; height: 160px; margin: -80px 0 0 -80px; border-radius: 50%;
  border: 3px solid rgba(240, 244, 248, 0.55);
  background: radial-gradient(circle, rgba(20, 28, 34, 0.25) 40%, rgba(20, 28, 34, 0.55));
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35); opacity: 0.6; transition: opacity 0.15s;
}
#stick-base.on { opacity: 1; transition: none; }
#stick-base::before, #stick-base::after {
  position: absolute; top: 50%; margin-top: -10px; font-size: 16px; line-height: 20px; color: rgba(255, 255, 255, 0.5);
}
#stick-base::before { content: "\25C0"; left: 8px; }
#stick-base::after { content: "\25B6"; right: 8px; }
#stick-knob {
  position: absolute; left: 50%; top: 50%; width: 62px; height: 62px; margin: -31px 0 0 -31px; border-radius: 50%;
  border: 3px solid rgba(240, 244, 248, 0.9);
  background: radial-gradient(circle at 50% 35%, #ff6a4a, #a3200f);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
}
.tc-right {
  position: absolute; right: max(14px, env(safe-area-inset-right)); bottom: max(14px, env(safe-area-inset-bottom));
  display: grid; grid-template-columns: auto auto; gap: 10px; align-items: center; justify-items: center;
}

body.touch #speedo-wrap { left: 50%; transform: translateX(-50%); bottom: 6px; width: 120px; }
body.touch #speedo { width: 120px; height: 120px; }
body.touch #boost-label { display: none; }
body.touch #shield-btn {
  width: 72px; height: 72px; padding: 6px;
  right: calc(max(14px, env(safe-area-inset-right)) + 196px); bottom: 64px;
}
body.touch #shield-btn::before, body.touch #shield-cd { inset: 6px; }
body.touch #shield-btn svg { width: 38px; height: 38px; margin-top: 2px; }
body.touch #shield-btn kbd { display: none; }
body.touch #health { width: 230px; top: 8px; left: max(12px, env(safe-area-inset-left)); }
body.touch #health .label { font-size: 14px; margin-left: 36px; }
body.touch .orb { width: 36px; height: 36px; padding: 4px; }
body.touch .bar { height: 22px; padding: 4px 5px 4px 12px; }
body.touch #race-info { font-size: 12px; gap: 8px; padding: 4px 8px; margin-top: 6px; }
body.touch #race-info b { font-size: 9px; }
body.touch #minimap-wrap, body.touch #minimap { width: 118px; height: 118px; }
body.touch #minimap-wrap { top: 8px; right: max(10px, env(safe-area-inset-right)); }
body.touch #banner { width: 170px; font-size: 16px; padding: 4px 0 8px; }
body.touch #msg { font-size: 56px; top: 24%; }
body.touch #sub-msg { top: calc(24% + 66px); font-size: 16px; }
body.touch .panel { padding: 16px 18px; }
body.touch .panel h2 { font-size: 30px; }
body.touch #res-stats { font-size: 16px; }
body.touch button.big { font-size: 18px; padding: 10px 28px; }
body.touch .menu-side { padding: 14px 18px; }
body.touch .menu-side .cols { display: none; }

#speed-chip, #a2hs { display: none; }
#boost-fill.locked { background: #7d8a92; }

/* full-screen toggle: just left of the minimap (top-right corner on the title screen) */
#fs-btn {
  display: none; position: fixed; z-index: 30; top: 14px; right: 250px; width: 42px; height: 42px; padding: 7px;
  border-radius: 10px; border: 2px solid rgba(240, 244, 248, 0.6); background: rgba(14, 20, 25, 0.65);
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
body.can-fs #fs-btn { display: block; }
body.on-menu #fs-btn { right: max(14px, env(safe-area-inset-right)); }
#fs-btn svg { width: 100%; height: 100%; fill: none; stroke: #f2f4f6; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
#fs-btn .fs-out, body.is-fs #fs-btn .fs-in { display: none; }
body.is-fs #fs-btn .fs-out { display: inline; }
#fs-btn:hover { border-color: #ff9a3c; }
@media (max-width: 760px) { #fs-btn { right: 160px; } }
body.touch #fs-btn { top: 8px; right: calc(max(10px, env(safe-area-inset-right)) + 128px); }
@media (max-height: 500px) {
  body.touch #fs-btn { top: 6px; right: calc(max(8px, env(safe-area-inset-right)) + 100px); width: 36px; height: 36px; padding: 6px; }
}
body.touch.on-menu #fs-btn { right: max(10px, env(safe-area-inset-right)); }

/* ---------- short landscape phones (iPhone in Safari is ~305pt tall) ---------- */
@media (max-height: 500px) {
  body.touch #banner, body.touch #speedo-wrap { display: none; }
  body.touch.ios-browser #a2hs {
    display: block; margin: 10px 0 0; font-size: 12px; color: #b9c3cb;
  }
  body.touch #a2hs b { color: #ff9a3c; font-weight: normal; }

  body.touch #health { width: 210px; top: 6px; left: max(10px, env(safe-area-inset-left)); }
  body.touch #health .label { display: none; }
  body.touch .orb { width: 26px; height: 26px; padding: 3px; }
  body.touch .bar { height: 16px; padding: 3px 4px 3px 10px; }
  body.touch #race-info { font-size: 11px; gap: 7px; padding: 3px 7px; margin-top: 4px; }
  body.touch #race-info b { font-size: 8px; }

  body.touch #minimap-wrap, body.touch #minimap { width: 92px; height: 92px; }
  body.touch #minimap-wrap { top: 6px; right: max(8px, env(safe-area-inset-right)); }

  body.touch #speed-chip {
    display: block; position: absolute; top: 6px; left: 50%; transform: translateX(-50%);
    min-width: 96px; padding: 3px 10px 5px; text-align: center; border-radius: 10px;
    background: rgba(14, 20, 25, 0.72); border: 2px solid rgba(240, 244, 248, 0.55);
    font-family: "Bungee", sans-serif; color: #fff; line-height: 1;
  }
  #speed-chip i { font-style: normal; font-size: 22px; }
  #speed-chip b { font-weight: normal; font-size: 9px; color: #b9c3cb; margin-left: 4px; }
  #speed-chip .boost-bar { height: 4px; margin-top: 4px; border-radius: 2px; background: rgba(255, 255, 255, 0.15); overflow: hidden; }
  #boost-fill { height: 100%; width: 100%; background: linear-gradient(90deg, #ff6a2a, #ffc04a); }

  /* thumb cluster: GAS in the corner, BOOST above, BRAKE beside, DRIFT on the diagonal */
  body.touch .tc-right {
    display: block; width: 128px; height: 128px;
    right: max(10px, env(safe-area-inset-right)); bottom: max(10px, env(safe-area-inset-bottom));
  }
  body.touch .tc-right .tc { position: absolute; width: 50px; height: 50px; font-size: 9px; border-width: 2px; }
  body.touch .tc-right .tc.gas { right: 0; bottom: 0; width: 66px; height: 66px; font-size: 14px; }
  body.touch .tc-right .tc.boost { right: 8px; bottom: 76px; }
  body.touch .tc-right .tc:nth-child(3) { right: 76px; bottom: 8px; }
  body.touch .tc-right .tc:nth-child(1) { right: 70px; bottom: 70px; }
  body.touch #shield-btn {
    width: 52px; height: 52px; padding: 4px;
    right: calc(max(10px, env(safe-area-inset-right)) + 146px); bottom: 12px;
  }
  body.touch #shield-btn::before, body.touch #shield-cd { inset: 4px; }
  body.touch #shield-btn svg { width: 28px; height: 28px; margin-top: 1px; }

  body.touch #stick-base { left: calc(max(10px, env(safe-area-inset-left)) + 80px); top: calc(100% - 82px); }
  body.touch #stick-zone { height: 72%; }

  body.touch #msg { font-size: 40px; top: 28%; }
  body.touch #sub-msg { top: calc(28% + 48px); font-size: 13px; }
  body.touch .menu-side { padding: 10px 14px; }
  body.touch .menu-side .tag { display: none; }
  body.touch #menu { background-position: left top; }
  /* results: stats and buttons on the left, leaderboard on the right, no scrolling */
  body.touch #results .panel {
    display: grid; grid-template-columns: auto minmax(200px, 1fr); column-gap: 22px; row-gap: 6px;
    grid-template-areas: "title board" "stats board" "form board" "again board" "promo board";
    align-items: center; justify-items: center; max-height: calc(100vh - 16px); padding: 12px 18px;
  }
  body.touch #res-title { grid-area: title; font-size: 26px; margin: 0; }
  body.touch #res-stats { grid-area: stats; font-size: 13px; }
  body.touch #score-form { grid-area: form; margin: 2px 0; flex-wrap: nowrap; }
  body.touch #score-form input { width: 150px; font-size: 14px; }
  body.touch #score-form button { font-size: 12px; padding: 6px 10px; }
  body.touch #res-board { grid-area: board; align-self: stretch; margin: 0; max-height: calc(100vh - 44px); overflow: auto; font-size: 13px; }
  body.touch #again-btn { grid-area: again; font-size: 16px; padding: 8px 24px; }
  body.touch #results .promo { grid-area: promo; margin: 4px 0 0; }
}

@media (orientation: portrait) {
  body.touch #rotate {
    display: flex; position: fixed; inset: 0; z-index: 50;
    flex-direction: column; align-items: center; justify-content: center; gap: 10px;
    background: #1b2228 url("../img/title.jpg") 30% center / cover no-repeat;
    font-family: "Bungee", sans-serif; font-size: 26px; color: #fff; text-align: center;
    text-shadow: 0 3px 0 #000, 0 0 20px #000;
  }
  body.touch #rotate small { font-family: "Share Tech Mono", monospace; font-size: 15px; }
}
