* { box-sizing: border-box; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0c87d5;
  font-family: "Montserrat", system-ui, sans-serif;
}

.game-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #0c87d5;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  background: #0c87d5;
}

.hud {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-width: min(520px, calc(100vw - 220px));
  pointer-events: none;
}

.hud > div {
  background: rgba(0, 40, 80, 0.72);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.hud strong { font-weight: 800; }

.game-brand {
  width: 100%;
  background: rgba(0, 30, 70, 0.82) !important;
}

.game-brand span {
  opacity: 0.75;
  font-size: 11px;
  margin-left: 6px;
}

.live-leaderboard {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 5;
  width: min(210px, 42vw);
  background: rgba(0, 30, 70, 0.88);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 14px;
  padding: 12px 12px 10px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(6px);
}

.live-leaderboard h3 {
  margin: 0 0 8px;
  text-align: center;
  font-size: 13px;
  letter-spacing: 0.12em;
}

.live-leaderboard ol {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 4px;
}

.live-leaderboard li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  line-height: 1.25;
  padding: 5px 7px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

.live-leaderboard li::before {
  counter-increment: none;
}

.leaderboard-link {
  display: block;
  margin-top: 8px;
  text-align: center;
  color: #ffe27a;
  font-size: 11px;
  font-weight: 800;
  text-decoration: none;
}

.leaderboard-link:hover { text-decoration: underline; }

.settings-button {
  position: absolute;
  top: auto;
  bottom: 14px;
  right: 14px;
  left: auto;
  z-index: 8;
  background: rgba(0, 30, 70, 0.9);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 800;
  font-family: "Montserrat", system-ui, sans-serif;
  cursor: pointer;
}
.settings-button.hidden-in-race {
  display: none !important;
}

.settings-overlay,
.screen-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 70, 120, 0.55);
  overflow: auto;
}

.settings-overlay.visible,
.screen-overlay.visible {
  display: flex;
}

.panel,
.settings-card {
  width: min(420px, 100%);
  background: rgba(0, 28, 64, 0.94);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 18px;
  padding: 16px 16px 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}

.start-panel {
  text-align: center;
}

.start-logo {
  display: block;
  width: min(220px, 55vw);
  height: auto;
  margin: 0 auto 6px;
}

.panel h1 {
  margin: 0 0 4px;
  font-size: clamp(1.2rem, 4vw, 1.65rem);
  line-height: 1.15;
  letter-spacing: 0.01em;
}

.panel h2 {
  margin: 0 0 8px;
  text-align: center;
}

.subtitle {
  margin: 0 0 16px;
  color: #e8f6ff;
  font-size: 1rem;
  font-weight: 600;
font-size: 0.92rem; margin: 0 0 10px; }

.setup-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 0 0 14px;
  text-align: left;
}

.setup-grid label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 800;
}

.setup-grid input,
.settings-grid input {
  width: 100%;
  border: 0;
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 16px;
}

#startGame,
#restartGame,
#changeRider,
.settings-actions button {
  width: 100%;
  border: 0;
  border-radius: 12px;
  padding: 14px 16px;
  margin-top: 8px;
  font-size: 1.05rem;
  font-weight: 900;
  cursor: pointer;
}

#startGame,
#restartGame,
#saveSettings {
  background: #ffd84d;
  color: #10233a;
}

#changeRider,
#resetSettings,
button.secondary {
  background: #e8f3ff;
  color: #123a5c;
}

#resetLeaderboard,
button.danger {
  background: #ff6b5a;
  color: #fff;
}

.controls-block {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  text-align: left;
}

.controls-title {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.controls-hint {
  margin: 0 0 6px;
  font-size: 15px;
  line-height: 1.4;
  color: #f4fbff;
}

.controls-hint:last-child { margin-bottom: 0; }

.controls-hint kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 2px 7px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.35);
  font-family: inherit;
  font-size: 0.92em;
  font-weight: 800;
  text-align: center;
}

.game-key {
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.22);
  text-align: left;
}

.game-key strong {
  display: block;
  margin-bottom: 6px;
  letter-spacing: 0.08em;
  font-size: 13px;
}

.game-key span {
  display: block;
  font-size: 15px;
  line-height: 1.45;
  margin: 4px 0;
  color: #f4fbff;
}

.final-score {
  text-align: center;
  font-size: clamp(2rem, 8vw, 3rem);
  font-weight: 900;
  margin: 8px 0 14px;
}

.leaderboard-panel {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
}

.leaderboard-panel h3 {
  margin: 0 0 8px;
  text-align: center;
}

.leaderboard-panel ol {
  margin: 0;
  padding-left: 1.2rem;
}

.settings-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.settings-close {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: #e8f3ff;
  color: #123a5c;
  font-weight: 900;
  cursor: pointer;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.settings-grid label {
  display: grid;
  gap: 5px;
  font-size: 12px;
  font-weight: 700;
}

.settings-actions {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.touch-controls {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 12px;
  z-index: 30;
  justify-content: center;
  gap: 10px;
  pointer-events: none;
}

.touch-controls button {
  pointer-events: auto;
  min-width: 84px;
  padding: 16px 18px;
  border: 0;
  border-radius: 14px;
  background: rgba(0, 30, 70, 0.8);
  color: #fff;
  font-weight: 900;
}

.rotate-note { display: none !important; }

@media (max-width: 720px) {
  .hud {
    max-width: calc(100vw - 24px);
  }

  .live-leaderboard {
    position: static;
    width: auto;
    margin: 0 12px;
    max-height: none;
  }

  .game-shell {
    display: flex;
    flex-direction: column;
  }

  #game {
    flex: 1 1 auto;
    min-height: 45vh;
  }

  .hud {
    position: absolute;
  }

  .live-leaderboard {
    position: absolute;
    top: auto;
    bottom: 12px;
    right: 12px;
    left: auto;
    width: min(190px, 46vw);
    max-height: 34vh;
    overflow: auto;
  }

  .settings-button {
    right: 12px;
    top: auto;
    bottom: calc(12px + 34vh + 8px);
  }

  .setup-grid,
  .settings-grid {
    grid-template-columns: 1fr;
  }

  .panel,
  .settings-card {
    padding: 16px 14px;
  }

  .controls-hint,
  .game-key span {
    font-size: 14px;
  }

  .start-logo {
    width: min(280px, 72vw);
  }
}

/* portrait play allowed — rotate prompt removed */

/* touch controls: only .in-race (see mobile block) */


.game-key {
  margin-top: 14px;
  text-align: left;
}
.game-key > strong {
  display: block;
  margin-bottom: 8px;
  letter-spacing: 0.08em;
  font-size: 13px;
}
.key-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.key-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(255,255,255,0.1);
}
.key-icon {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  background: rgba(0,40,80,0.35);
  border-radius: 8px;
}
.key-label {
  font-size: 13px;
  line-height: 1.25;
  color: #f4fbff;
}
.key-label b { color: #fff; }
@media (max-width: 520px) {
  .key-grid { grid-template-columns: 1fr; }
}


/* Right-hand KEY panel */
.side-key {
  position: absolute;
  top: 248px;
  right: 12px;
  z-index: 12;
  width: min(200px, 40vw);
  max-height: calc(100vh - 270px);
  overflow: auto;
  pointer-events: none;
}
.side-key .game-key {
  margin: 0;
  padding: 10px;
  border-radius: 14px;
  background: rgba(0, 30, 70, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.35);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.25);
}
.side-key .key-grid {
  grid-template-columns: 1fr;
  gap: 6px;
}
.side-key .key-item {
  padding: 5px 6px;
  background: rgba(255,255,255,0.08);
}
.side-key .key-icon {
  width: 30px;
  height: 30px;
}
.side-key .key-icon svg {
  width: 28px;
  height: 28px;
}
.side-key .key-label {
  font-size: 12px;
}

.start-panel .subtitle {
  margin: 0 0 10px;
  font-size: 0.92rem;
}
.start-panel .controls-block {
  margin-top: 10px;
  padding: 8px 10px;
}
.start-panel .controls-hint {
  font-size: 13px;
  margin: 0 0 4px;
}
.start-panel .setup-grid {
  margin: 0 0 10px;
  gap: 10px;
}
#startGame {
  margin-top: 4px;
  padding: 12px 14px;
}

/* Keep TOP 10 above KEY; shrink a touch if needed */
.live-leaderboard {
  max-height: 220px;
  overflow: auto;
}

@media (max-width: 900px) {
  .side-key {
    top: auto;
    bottom: 12px;
    right: 12px;
    width: min(190px, 46vw);
  }
  .live-leaderboard {
    max-height: 160px;
  }
}

@media (max-width: 640px) {
  .side-key {
    position: static;
    width: auto;
    margin: 0 12px 12px;
    pointer-events: auto;
  }
  .key-grid {
    grid-template-columns: 1fr 1fr;
  }
}


/* Compact start centre card */
.start-panel {
  width: min(380px, 92vw) !important;
  padding: 14px 14px 12px !important;
}
.start-panel .start-logo {
  width: min(200px, 48vw) !important;
  margin-bottom: 4px !important;
}
.start-panel h1 {
  display: none; /* logo already carries the title */
}
.start-panel .subtitle {
  margin: 0 0 10px !important;
  font-size: 0.9rem !important;
}
.start-panel .setup-grid {
  gap: 8px !important;
  margin-bottom: 10px !important;
}
.start-panel .controls-block {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(255,255,255,0.08);
  text-align: left;
}
.start-panel .controls-title {
  margin: 0 0 4px;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.9;
}
.start-panel .controls-hint {
  margin: 0 0 3px;
  font-size: 13px;
}
.start-panel #startGame {
  margin-top: 2px;
  padding: 12px 14px;
  font-size: 1.05rem;
}

/* KEY column under TOP 10 on the right */
.side-key {
  position: absolute !important;
  top: 248px !important;
  right: 12px !important;
  left: auto !important;
  transform: none !important;
  z-index: 12 !important;
  width: min(200px, 40vw) !important;
  max-height: calc(100vh - 265px);
  overflow: auto;
}
.side-key .game-key {
  margin: 0;
}
.side-key .key-grid {
  grid-template-columns: 1fr 1fr !important;
}


/* ===== Typography: Montserrat + Font Awesome ===== */
html, body, button, input, select, .panel, .hud, .live-leaderboard, .side-key {
  font-family: "Montserrat", system-ui, sans-serif;
}

/* TOP 10 card */
.live-leaderboard {
  width: min(230px, 44vw);
  padding: 12px 12px 10px;
  background: rgba(0, 24, 58, 0.92);
  border: 1px solid rgba(255,255,255,0.35);
  border-radius: 16px;
}
.live-leaderboard h3 {
  margin: 0 0 8px;
  text-align: center;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: #ffe27a;
}
.live-leaderboard h3 i {
  margin-right: 6px;
  color: #ffd24d;
}
.lb-head {
  display: grid;
  grid-template-columns: 1fr 42px 64px;
  gap: 6px;
  padding: 0 4px 6px;
  margin-bottom: 4px;
  border-bottom: 1px solid rgba(255,255,255,0.2);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.65);
}
.lb-head span:nth-child(2),
.lb-head span:nth-child(3) { text-align: right; }

.live-leaderboard ol,
.leaderboard-panel ol {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
}
.lb-row {
  display: grid !important;
  grid-template-columns: 1fr 42px 64px;
  gap: 6px;
  align-items: center;
  padding: 6px 6px !important;
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  white-space: nowrap;
  overflow: hidden;
}
.lb-name {
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lb-num {
  font-size: 11px;
  font-weight: 600;
  color: #9fd6ff;
  text-align: right;
}
.lb-score {
  font-size: 12px;
  font-weight: 800;
  color: #ffe27a;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.lb-empty {
  text-align: center;
  font-size: 12px;
  opacity: 0.8;
  padding: 8px;
}

/* KEY text white + FA icons */
.side-key .game-key,
.side-key .key-label,
.side-key .game-key > strong {
  color: #fff !important;
}
.side-key .key-label {
  font-size: 12px;
  font-weight: 600;
  line-height: 1.25;
}
.side-key .key-label b {
  color: #fff;
  font-weight: 800;
}
.side-key .key-icon {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: rgba(255,255,255,0.12);
  color: #7dffb3;
  font-size: 15px;
}
.side-key .key-icon.staff { color: #ff9a4d; }
.side-key .key-icon.slider { color: #ffe27a; }
.side-key .key-icon.jump { color: #ff7a5c; }
.side-key .key-icon.ring { color: #ffd24d; }
.side-key .key-icon.water { color: #7ecbff; }
.side-key .key-grid {
  grid-template-columns: 1fr 1fr !important;
}

/* ===== Portrait / mobile responsive (no landscape lock) ===== */
@media (max-width: 900px) {
  .screen-overlay {
    align-items: flex-start;
    overflow: auto;
    padding: 12px;
  }
  .start-panel {
    width: min(420px, 100%) !important;
    margin: 0 auto 12px;
  }
  .live-leaderboard {
    position: absolute;
    top: 10px;
    right: 10px;
    width: min(200px, 42vw);
    max-height: 38vh;
    overflow: auto;
    z-index: 8;
  }
  .side-key {
    position: absolute !important;
    top: auto !important;
    bottom: 12px !important;
    right: 10px !important;
    width: min(200px, 42vw) !important;
    max-height: 42vh;
    overflow: auto;
    z-index: 8;
  }
  .settings-button {
    top: auto;
    bottom: calc(12px + 42vh + 8px);
    right: 10px;
  }
}

@media (max-width: 700px) and (orientation: portrait) {
  .game-shell {
    display: block;
  }
  .screen-overlay.visible {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 10px;
    padding: 10px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
  .start-panel {
    order: 1;
    width: 100% !important;
    max-width: none !important;
  }
  .live-leaderboard {
    position: static !important;
    order: 2;
    width: 100% !important;
    max-height: none;
    margin: 0;
  }
  .side-key {
    position: static !important;
    order: 3;
    width: 100% !important;
    max-height: none;
    transform: none !important;
    margin: 0 0 18px;
    pointer-events: auto;
  }
  .side-key .key-grid {
    grid-template-columns: 1fr 1fr !important;
  }
  .hud {
    max-width: calc(100vw - 20px);
    font-size: 11px;
  }
  .settings-button {
    position: fixed;
    top: auto;
    bottom: 12px;
    right: 12px;
    left: auto;
    z-index: 20;
  }
  .lb-head,
  .lb-row {
    grid-template-columns: 1fr 48px 72px;
  }
  /* touch controls gated by .in-race */
}

@media (max-width: 420px) {
  .start-panel .start-logo {
    width: min(130px, 48vw) !important;
  }
}


/* Force KEY readability */
.side-key, .side-key * { color: #fff; }
.side-key .key-icon { color: #7dffb3; }
.side-key .key-icon.staff { color: #ff9a4d !important; }
.side-key .key-icon.slider { color: #ffe27a !important; }
.side-key .key-icon.jump { color: #ff7a5c !important; }
.side-key .key-icon.ring { color: #ffd24d !important; }
.side-key .key-icon.water { color: #7ecbff !important; }


/* KEY obstacle pictures */
.side-key .key-icon {
  width: 38px !important;
  height: 38px !important;
  padding: 2px;
  background: rgba(0, 20, 50, 0.45) !important;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 10px;
  display: grid;
  place-items: center;
  color: inherit !important;
  font-size: inherit !important;
}
.side-key .key-icon svg {
  width: 34px;
  height: 34px;
  display: block;
}
.side-key .key-label {
  color: #fff !important;
}
.side-key .key-label b {
  color: #fff !important;
}


/* Leaderboard medals */
.lb-head {
  grid-template-columns: 28px 1fr 42px 64px !important;
}
.lb-row {
  grid-template-columns: 28px 1fr 42px 64px !important;
}
.medal {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 14px;
  line-height: 1;
}
.medal.none {
  font-size: 10px;
  font-weight: 800;
  color: rgba(255,255,255,0.7);
  background: rgba(255,255,255,0.08);
}
.medal.gold { filter: drop-shadow(0 0 4px rgba(255, 215, 0, 0.55)); }
.medal.silver { filter: drop-shadow(0 0 4px rgba(200, 200, 210, 0.45)); }
.medal.bronze { filter: drop-shadow(0 0 4px rgba(205, 127, 50, 0.45)); }
.lb-top1 { background: rgba(255, 215, 0, 0.14) !important; }
.lb-top2 { background: rgba(192, 192, 192, 0.12) !important; }
.lb-top3 { background: rgba(205, 127, 50, 0.12) !important; }
.final-label {
  margin: 0;
  text-align: center;
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.8;
}
#gameOverTitle {
  color: #ffe27a;
  letter-spacing: 0.06em;
}


/* Settings stays bottom-right; hidden while racing */
.settings-button {
  top: auto !important;
  bottom: 14px !important;
  right: 14px !important;
  left: auto !important;
}


/* Settings unused for now */
.settings-button,
#settingsButton {
  display: none !important;
}


/* Full rider names on scoreboards */
.live-leaderboard {
  width: min(280px, 48vw) !important;
}
.lb-name,
.live-leaderboard .lb-name,
.leaderboard-panel .lb-name,
#liveLeaderboard .lb-name,
#leaderboard .lb-name {
  overflow: visible !important;
  text-overflow: unset !important;
  white-space: normal !important;
  word-break: break-word;
  overflow-wrap: anywhere;
  line-height: 1.2;
  max-width: none !important;
}
.lb-row {
  align-items: start !important;
  white-space: normal !important;
  overflow: visible !important;
  min-height: 28px;
  padding-top: 7px !important;
  padding-bottom: 7px !important;
}

/* ===== Mobile layout: un-squash start screen + clear bottom controls ===== */
@media (pointer: coarse), (max-width: 900px) {
  /* reserve space so menus never sit under the control bar */
  .screen-overlay.visible {
    padding-bottom: calc(92px + env(safe-area-inset-bottom)) !important;
  }
}

@media (max-width: 700px) and (orientation: portrait) {
  .screen-overlay.visible {
    display: flex !important;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 8px;
    padding: 8px 10px calc(100px + env(safe-area-inset-bottom)) !important;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .start-panel {
    width: 100% !important;
    max-width: none !important;
    padding: 12px 12px 10px !important;
    margin: 0 !important;
  }
  .start-panel .start-logo {
    width: min(150px, 42vw) !important;
    margin-bottom: 2px !important;
  }
  .start-panel h1 { display: none !important; }
  .start-panel .subtitle {
    margin: 0 0 8px !important;
    font-size: 0.85rem !important;
  }
  .start-panel .setup-grid {
    gap: 8px !important;
    margin-bottom: 8px !important;
  }
  .start-panel label { font-size: 12px; }
  .start-panel input {
    padding: 10px !important;
    font-size: 16px !important; /* avoid iOS zoom */
  }
  .start-panel #startGame {
    margin-top: 4px !important;
    padding: 14px 12px !important;
    font-size: 1.05rem !important;
  }

  /* Keyboard help is less useful on phones — keep tiny */
  .start-panel .controls-block {
    margin-top: 6px !important;
    padding: 6px 8px !important;
  }
  .start-panel .controls-hint {
    font-size: 11px !important;
    margin: 0 0 2px !important;
  }

  /* KEY becomes a compact strip under the card */
  .side-key {
    position: static !important;
    width: 100% !important;
    max-height: none !important;
    transform: none !important;
    margin: 0 !important;
    pointer-events: auto;
  }
  .side-key .game-key {
    padding: 8px !important;
  }
  .side-key .game-key > strong {
    margin-bottom: 4px !important;
    font-size: 11px !important;
  }
  .side-key .key-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 4px !important;
  }
  .side-key .key-item {
    padding: 4px 6px !important;
    gap: 6px !important;
  }
  .side-key .key-icon {
    width: 28px !important;
    height: 28px !important;
  }
  .side-key .key-icon svg {
    width: 24px !important;
    height: 24px !important;
  }
  .side-key .key-label {
    font-size: 11px !important;
  }

  /* Top 10: compact, after key */
  .live-leaderboard {
    position: static !important;
    width: 100% !important;
    max-height: 160px;
    overflow: auto;
    margin: 0 !important;
  }

  .hud {
    font-size: 11px;
    max-width: calc(100vw - 16px);
  }
}

@media (max-width: 420px) {
  .touch-controls button {
    min-height: 70px;
    font-size: 15px;
    border-radius: 16px;
  }
  .side-key .key-grid {
    grid-template-columns: 1fr 1fr !important;
  }
}


/* HUD / live board: hide before race on mobile only; desktop always visible */
.hud,
.live-leaderboard {
  transition: opacity 0.15s ease;
}
body.touch-ui .hud:not(.hud-live),
body.touch-ui .live-leaderboard:not(.hud-live) {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}
body.touch-ui .hud.hud-live,
body.touch-ui .live-leaderboard.hud-live,
body.desktop-ui .hud,
body.desktop-ui .live-leaderboard {
  opacity: 1;
  visibility: visible;
}

/* Mobile start: KEY lower + tighter; game over fits phone */
@media (max-width: 700px), (pointer: coarse) {
  .screen-overlay.visible {
    display: flex !important;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 6px;
    padding: 6px 8px max(10px, env(safe-area-inset-bottom)) !important;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .start-panel {
    padding: 10px 10px 8px !important;
    margin: 0 !important;
  }
  .start-panel .start-logo {
    width: min(110px, 36vw) !important;
    margin-bottom: 0 !important;
  }
  .start-panel h1 { display: none !important; }
  .start-panel .subtitle {
    margin: 0 0 6px !important;
    font-size: 0.75rem !important;
  }
  .start-panel .setup-grid { gap: 6px !important; margin-bottom: 6px !important; }
  .start-panel #startGame { padding: 12px 10px !important; margin-top: 2px !important; }

  .start-panel .controls-block .controls-title,
  .start-panel .controls-block .controls-hint:not(.touch-only-hint) {
    display: none !important;
  }
  .start-panel .controls-block {
    margin-top: 4px !important;
    padding: 6px 8px !important;
  }
  .start-panel .controls-hint.touch-only-hint {
    display: block !important;
    font-size: 11px !important;
    margin: 0 !important;
  }

  .live-leaderboard:not(.hud-live) {
    display: none !important;
  }

  /* KEY sits lower / more compact under the start card */
  .side-key {
    position: static !important;
    order: 4;
    width: 100% !important;
    margin: 0 !important; /* sit directly under start card */
    max-height: none !important;
    transform: none !important;
  }
  .side-key .game-key {
    padding: 6px 8px !important;
  }
  .side-key .game-key > strong {
    margin: 0 0 3px !important;
    font-size: 10px !important;
  }
  .side-key .key-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 3px !important;
  }
  .side-key .key-item {
    padding: 3px 5px !important;
    gap: 5px !important;
  }
  .side-key .key-icon {
    width: 20px !important;
    height: 20px !important;
  }
  .side-key .key-icon svg {
    width: 18px !important;
    height: 18px !important;
  }
  .side-key .key-label {
    font-size: 10px !important;
    line-height: 1.15 !important;
  }

  /* Game over modal — fit phone viewport */
  #gameOverScreen.screen-overlay.visible {
    justify-content: flex-start !important;
    padding: 8px 10px max(12px, env(safe-area-inset-bottom)) !important;
  }
  #gameOverScreen .panel {
    width: 100% !important;
    max-width: 100% !important;
    max-height: calc(100dvh - 24px);
    max-height: calc(100svh - 24px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    padding: 14px 12px 16px !important;
    margin: 0 auto;
    box-sizing: border-box;
  }
  #gameOverScreen .panel h2 {
    font-size: 1.35rem !important;
    margin: 0 0 4px !important;
  }
  #gameOverScreen .final-label {
    margin: 0 !important;
    font-size: 12px !important;
  }
  #gameOverScreen .final-score {
    font-size: 2rem !important;
    margin: 4px 0 10px !important;
  }
  #gameOverScreen .leaderboard-panel {
    padding: 8px !important;
    margin-bottom: 8px !important;
    max-height: 32vh;
    overflow: auto;
  }
  #gameOverScreen .leaderboard-panel h3 {
    font-size: 0.95rem !important;
    margin: 0 0 6px !important;
  }
  #gameOverScreen .panel button {
    width: 100%;
    margin: 6px 0 0 !important;
    padding: 12px 10px !important;
  }
}

/* Mobile race controls — thin GO slider on the right */
@media (hover: hover) and (pointer: fine) {
  .touch-controls { display: none !important; }
}
body.desktop-ui .touch-controls { display: none !important; }

body.touch-ui .touch-controls {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: max(8px, env(safe-area-inset-bottom));
  height: 44vh;
  max-height: 300px;
  z-index: 50;
  pointer-events: none;
  box-sizing: border-box;
  padding: 0 10px;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
}
body.touch-ui .touch-controls.in-race {
  display: flex;
}

.touch-steer-col {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 78px;
  flex: 0 0 auto;
}
.touch-steer-btn {
  min-height: 68px;
  border: 2px solid rgba(255,255,255,0.35);
  border-radius: 16px;
  background: rgba(0, 24, 58, 0.88);
  color: #fff;
  font-family: "Montserrat", system-ui, sans-serif;
  font-weight: 900;
  font-size: 14px;
  letter-spacing: 0.04em;
  box-shadow: 0 8px 20px rgba(0,0,0,0.35);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.touch-steer-btn:active {
  background: rgba(12, 135, 213, 0.95);
}

.touch-throttle-col {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 36px;
  flex: 0 0 auto;
  margin-left: auto;
}
.throttle-label-top,
.throttle-label-bot {
  font-family: "Montserrat", system-ui, sans-serif;
  font-weight: 800;
  font-size: 10px;
  letter-spacing: 0.06em;
  color: rgba(255,255,255,0.8);
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}
.throttle-label-top { color: #ffd24d; }

/* Thin vertical track on the right */
.throttle-track {
  position: relative;
  width: 18px !important;
  height: min(40vh, 230px) !important;
  border-radius: 999px;
  background: rgba(0, 20, 48, 0.55);
  border: 2px solid rgba(255,255,255,0.35);
  box-shadow: inset 0 0 12px rgba(0,0,0,0.35), 0 6px 16px rgba(0,0,0,0.3);
  touch-action: none;
  overflow: visible;
}
.throttle-fill {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 0%;
  border-radius: inherit;
  background: linear-gradient(to top, rgba(255,180,40,0.35), rgba(255,210,77,0.85));
  pointer-events: none;
}
.throttle-knob {
  position: absolute;
  left: 50%;
  top: 0;
  width: 28px !important;
  height: 28px !important;
  margin-left: -14px !important;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffe27a, #ffd24d 45%, #e6b800 100%);
  border: 2px solid rgba(255,255,255,0.8);
  box-shadow: 0 3px 10px rgba(0,0,0,0.4);
  z-index: 2;
  touch-action: none;
}

@media (max-height: 700px) and (orientation: portrait) {
  .touch-steer-btn { min-height: 58px; font-size: 13px; }
  .throttle-track { height: min(34vh, 180px) !important; }
}

.touch-only-hint { display: none; }
body.touch-ui .touch-only-hint { display: block; font-size: 11px !important; }


/* fat hit / thin visual throttle */
.touch-throttle-col {
  width: 56px !important;
  align-items: center;
  margin-left: auto !important;
  position: relative;
}
.throttle-track {
  width: 14px !important;
  height: min(42vh, 240px) !important;
  /* expand touch target without looking thick */
  padding: 0;
  box-sizing: content-box;
  border-width: 2px !important;
}
.throttle-track::before {
  content: "";
  position: absolute;
  inset: -8px -22px;
  /* invisible wider grab area */
}
.throttle-knob {
  width: 26px !important;
  height: 26px !important;
  margin-left: -13px !important;
}


/* === Mobile controls layout fix (LEFT+RIGHT row, draggable GO) === */
body.touch-ui .touch-controls.in-race {
  display: flex !important;
  flex-direction: row !important;
  align-items: flex-end !important;
  justify-content: space-between !important;
  pointer-events: none;
}

body.touch-ui .touch-steer-col {
  display: flex !important;
  flex-direction: row !important; /* side by side, not stacked */
  gap: 10px !important;
  width: auto !important;
  pointer-events: auto;
}

body.touch-ui .touch-steer-btn {
  width: 76px;
  min-height: 64px;
  flex: 0 0 auto;
}

body.touch-ui .touch-throttle-col {
  pointer-events: auto;
  width: 64px !important;
  margin-left: auto !important;
}

body.touch-ui .throttle-track {
  position: relative !important;
  width: 16px !important;
  height: min(42vh, 240px) !important;
  overflow: visible !important;
  touch-action: none !important;
  pointer-events: auto !important;
}

/* Fat invisible hit area around thin bar */
body.touch-ui .throttle-track::before {
  content: "";
  position: absolute;
  left: -28px;
  right: -28px;
  top: -12px;
  bottom: -12px;
}

body.touch-ui .throttle-knob {
  position: absolute !important;
  left: 50% !important;
  top: 100%;
  width: 30px !important;
  height: 30px !important;
  margin: 0 !important;
  transform: translate(-50%, -50%) !important;
  touch-action: none !important;
  pointer-events: auto !important;
  z-index: 5;
}

body.touch-ui .throttle-fill {
  pointer-events: none !important;
}

body.dragging-throttle {
  touch-action: none !important;
  user-select: none !important;
}

@media (hover: hover) and (pointer: fine) {
  body.desktop-ui .touch-controls {
    display: none !important;
  }
}


/* Desktop KEY + HUD restore (Inspect device mode ≤700px uses mobile rules instead) */
@media (min-width: 701px) and (pointer: fine) {
  .side-key {
    position: absolute !important;
    top: 120px !important;
    right: 12px !important;
    bottom: auto !important;
    left: auto !important;
    width: min(210px, 28vw) !important;
    max-height: calc(100vh - 140px) !important;
    margin: 0 !important;
    order: initial !important;
    transform: none !important;
    overflow: auto !important;
    pointer-events: none !important;
    z-index: 12 !important;
  }
  .side-key .game-key {
    padding: 10px !important;
  }
  .side-key .game-key > strong {
    font-size: 12px !important;
    margin-bottom: 6px !important;
  }
  .side-key .key-grid {
    grid-template-columns: 1fr !important;
    gap: 6px !important;
  }
  .side-key .key-item {
    padding: 5px 6px !important;
    gap: 8px !important;
  }
  .side-key .key-icon {
    width: 32px !important;
    height: 32px !important;
  }
  .side-key .key-icon svg {
    width: 28px !important;
    height: 28px !important;
  }
  .side-key .key-label {
    font-size: 12px !important;
    line-height: 1.25 !important;
  }

  .hud,
  .live-leaderboard {
    opacity: 1 !important;
    visibility: visible !important;
  }

  .touch-controls {
    display: none !important;
  }
}


/* KEY + logo layout: desktop down, mobile under start card */
@media (min-width: 701px) and (pointer: fine) {
  .side-key {
    position: absolute !important;
    top: 280px !important; /* clear of TOP 10 */
    right: 12px !important;
    bottom: auto !important;
    left: auto !important;
    width: min(210px, 28vw) !important;
    max-height: calc(100vh - 300px) !important;
    margin: 0 !important;
    order: initial !important;
  }
}

@media (max-width: 700px), (pointer: coarse) {
  #startScreen.screen-overlay.visible {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    justify-content: flex-start !important;
    gap: 8px !important;
  }

  #startScreen .start-panel {
    order: 1;
    margin: 0 !important;
  }

  #startScreen .start-logo {
    width: min(200px, 58vw) !important;
    max-width: 220px !important;
    height: auto !important;
    margin: 0 auto 6px !important;
    display: block !important;
  }

  #startScreen .side-key {
    position: static !important;
    order: 2 !important; /* directly under start panel */
    width: 100% !important;
    margin: 0 !important; /* no auto — was pushing KEY to the bottom */
    max-height: none !important;
    transform: none !important;
  }

  /* If live leaderboard is in this overlay, keep it after KEY */
  #startScreen .live-leaderboard {
    order: 3;
  }
}


/* Thumb-friendly GO toggle + working steer buttons */
body.touch-ui .touch-controls.in-race {
  display: flex !important;
  z-index: 80 !important;
  pointer-events: none !important;
}
body.touch-ui .touch-steer-col,
body.touch-ui .touch-throttle-col {
  pointer-events: auto !important;
  z-index: 81 !important;
}
body.touch-ui .touch-steer-btn {
  touch-action: none !important;
  -webkit-user-select: none !important;
  user-select: none !important;
  width: 88px !important;
  min-height: 72px !important;
  font-size: 16px !important;
}
body.touch-ui .touch-steer-btn.is-pressed {
  background: rgba(12, 135, 213, 0.98) !important;
  transform: scale(0.97);
}
body.touch-ui .touch-throttle-col {
  width: 72px !important;
}
body.touch-ui .throttle-track {
  width: 28px !important;
  height: min(46vh, 260px) !important;
  touch-action: none !important;
  overflow: visible !important;
  pointer-events: auto !important;
}
body.touch-ui .throttle-track::before {
  content: "";
  position: absolute;
  left: -30px;
  right: -30px;
  top: -16px;
  bottom: -16px;
}
body.touch-ui .throttle-knob {
  width: 44px !important;
  height: 44px !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
  touch-action: none !important;
  pointer-events: auto !important;
  z-index: 5 !important;
}


/* Native GO slider — reliable thumb drag, including on start screen */
body.touch-ui .touch-controls.in-race {
  display: flex !important;
  z-index: 90 !important;
}
body.touch-ui .touch-controls.on-start {
  /* Sit above the start card chrome so the GO bar is usable before START RIDE */
  bottom: max(12px, env(safe-area-inset-bottom)) !important;
  z-index: 95 !important;
}
body.touch-ui .touch-controls.on-start .touch-steer-col {
  opacity: 0.35;
  pointer-events: none; /* steering only matters in race */
}
body.touch-ui .throttle-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  height: min(48vh, 280px);
  width: 56px;
  pointer-events: auto;
}
body.touch-ui .throttle-range {
  -webkit-appearance: none;
  appearance: none;
  width: min(48vh, 280px);
  height: 44px;
  margin: 0;
  transform: rotate(-90deg);
  transform-origin: center center;
  background: transparent;
  touch-action: none;
  cursor: pointer;
}
body.touch-ui .throttle-range::-webkit-slider-runnable-track {
  height: 14px;
  border-radius: 999px;
  background: rgba(0, 24, 58, 0.75);
  border: 2px solid rgba(255,255,255,0.4);
}
body.touch-ui .throttle-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 40px;
  height: 40px;
  margin-top: -15px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffe27a, #ffd24d 45%, #e6b800 100%);
  border: 2px solid rgba(255,255,255,0.85);
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}
body.touch-ui .throttle-range::-moz-range-track {
  height: 14px;
  border-radius: 999px;
  background: rgba(0, 24, 58, 0.75);
  border: 2px solid rgba(255,255,255,0.4);
}
body.touch-ui .throttle-range::-moz-range-thumb {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #ffd24d;
  border: 2px solid rgba(255,255,255,0.85);
}


/* === Mobile controls v2: must receive touches === */
body.touch-ui .touch-controls {
  display: none;
  position: fixed !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  top: auto !important;
  height: 46vh !important;
  max-height: none !important;
  z-index: 9999 !important;
  pointer-events: none !important;
  padding: 8px 12px max(12px, env(safe-area-inset-bottom)) !important;
  box-sizing: border-box !important;
  justify-content: space-between !important;
  align-items: flex-end !important;
  background: linear-gradient(to top, rgba(0,20,50,0.35), transparent) !important;
}
body.touch-ui .touch-controls.in-race {
  display: flex !important;
}
body.touch-ui .touch-steer-col,
body.touch-ui .touch-throttle-col {
  pointer-events: auto !important;
  position: relative !important;
  z-index: 10000 !important;
}
body.touch-ui .touch-steer-col {
  display: flex !important;
  flex-direction: row !important;
  gap: 12px !important;
}
body.touch-ui .touch-steer-btn {
  width: 92px !important;
  min-height: 76px !important;
  border: 0 !important;
  border-radius: 18px !important;
  background: rgba(0, 40, 90, 0.92) !important;
  color: #fff !important;
  font-weight: 900 !important;
  font-size: 16px !important;
  touch-action: none !important;
  -webkit-user-select: none !important;
  user-select: none !important;
}
body.touch-ui .touch-steer-btn.is-pressed {
  background: #0b8ad8 !important;
}
body.touch-ui .touch-throttle-col {
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  gap: 6px !important;
  width: 70px !important;
}
body.touch-ui .throttle-track {
  position: relative !important;
  width: 36px !important;
  height: min(42vh, 250px) !important;
  border-radius: 20px !important;
  background: rgba(0, 24, 58, 0.85) !important;
  border: 2px solid rgba(255,255,255,0.45) !important;
  touch-action: none !important;
  overflow: visible !important;
  pointer-events: auto !important;
}
body.touch-ui .throttle-track::before {
  content: "" !important;
  position: absolute !important;
  inset: -20px -28px !important; /* fat thumb target */
}
body.touch-ui .throttle-fill {
  position: absolute !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  height: 85% !important;
  border-radius: inherit !important;
  background: linear-gradient(to top, rgba(255,180,40,0.4), rgba(255,210,77,0.9)) !important;
  pointer-events: none !important;
}
body.touch-ui .throttle-knob {
  position: absolute !important;
  left: 50% !important;
  top: 15% !important;
  width: 48px !important;
  height: 48px !important;
  margin: 0 !important;
  transform: translate(-50%, -50%) !important;
  border-radius: 50% !important;
  background: radial-gradient(circle at 35% 30%, #ffe27a, #ffd24d 50%, #e6b800) !important;
  border: 3px solid #fff !important;
  box-shadow: 0 4px 14px rgba(0,0,0,0.45) !important;
  touch-action: none !important;
  pointer-events: auto !important;
  z-index: 2 !important;
}
body.touch-ui .throttle-label-top,
body.touch-ui .throttle-label-bot {
  color: #fff !important;
  font-weight: 800 !important;
  font-size: 12px !important;
  pointer-events: none !important;
}
body.desktop-ui .touch-controls {
  display: none !important;
}


/* Android Chrome: no long-press callout / selection on controls */
body.touch-ui .touch-controls,
body.touch-ui .touch-controls * {
  -webkit-user-select: none !important;
  user-select: none !important;
  -webkit-touch-callout: none !important;
  -webkit-tap-highlight-color: transparent !important;
  touch-action: none !important;
}

body.touch-ui .touch-controls {
  display: none;
  position: fixed !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  height: 48vh !important;
  z-index: 2147483000 !important;
  pointer-events: none !important;
  padding: 10px 14px max(14px, env(safe-area-inset-bottom)) !important;
  box-sizing: border-box !important;
  justify-content: space-between !important;
  align-items: flex-end !important;
  background: linear-gradient(to top, rgba(0,15,40,0.45), transparent 70%) !important;
}
body.touch-ui .touch-controls.in-race {
  display: flex !important;
}
body.touch-ui .touch-steer-col,
body.touch-ui .touch-throttle-col {
  pointer-events: auto !important;
  z-index: 2147483001 !important;
}
body.touch-ui .touch-steer-col {
  display: flex !important;
  flex-direction: row !important;
  gap: 12px !important;
}
body.touch-ui .touch-steer-btn {
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  width: 96px !important;
  height: 80px !important;
  border-radius: 18px !important;
  background: rgba(0, 40, 90, 0.95) !important;
  color: #fff !important;
  font-weight: 900 !important;
  font-size: 17px !important;
  font-family: Montserrat, system-ui, sans-serif !important;
  box-shadow: 0 6px 18px rgba(0,0,0,0.35) !important;
}
body.touch-ui .touch-steer-btn.is-pressed {
  background: #0b8ad8 !important;
  transform: scale(0.96);
}
body.touch-ui .touch-throttle-col {
  width: 72px !important;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  gap: 6px !important;
}
body.touch-ui .throttle-track {
  position: relative !important;
  width: 40px !important;
  height: min(44vh, 260px) !important;
  border-radius: 22px !important;
  background: rgba(0, 24, 58, 0.9) !important;
  border: 2px solid rgba(255,255,255,0.5) !important;
  overflow: visible !important;
}
body.touch-ui .throttle-track::before {
  content: "" !important;
  position: absolute !important;
  inset: -24px -32px !important;
}
body.touch-ui .throttle-fill {
  position: absolute !important;
  left: 0; right: 0; bottom: 0;
  height: 85%;
  border-radius: inherit;
  background: linear-gradient(to top, rgba(255,180,40,0.45), #ffd24d);
  pointer-events: none !important;
}
body.touch-ui .throttle-knob {
  position: absolute !important;
  left: 50% !important;
  top: 15% !important;
  width: 52px !important;
  height: 52px !important;
  transform: translate(-50%, -50%) !important;
  border-radius: 50% !important;
  background: radial-gradient(circle at 35% 30%, #ffe27a, #ffd24d 50%, #e6b800) !important;
  border: 3px solid #fff !important;
  box-shadow: 0 4px 16px rgba(0,0,0,0.45) !important;
  pointer-events: none !important; /* drag handled by track */
}
body.touch-ui .throttle-label-top,
body.touch-ui .throttle-label-bot {
  color: #fff !important;
  font-weight: 800 !important;
  font-size: 12px !important;
  pointer-events: none !important;
}


/* === GAMEBOY FIX v4 === */
body.desktop-ui #touchControls { display: none !important; }
/* live board shown on start; hidden in-race below */
body.touch-ui.in-race-controls { --gb-h: 120px; }
body.touch-ui.in-race-controls #game,
body.touch-ui.in-race-controls canvas#game {
  height: calc(100dvh - var(--gb-h)) !important;
  max-height: calc(100dvh - var(--gb-h)) !important;
  width: 100% !important;
}
body.touch-ui #touchControls {
  display: none !important;
  position: fixed !important;
  left: 0 !important; right: 0 !important; bottom: 0 !important; top: auto !important;
  width: 100% !important; height: var(--gb-h, 120px) !important;
  z-index: 2147483000 !important;
  pointer-events: auto !important;
  background: #000 !important;
  border-top: 2px solid #222 !important;
  padding: 0 !important; margin: 0 !important; box-sizing: border-box !important;
}
body.touch-ui #touchControls.in-race { display: block !important; }
body.touch-ui #touchControls .gb-bar {
  width: 100% !important; height: 100% !important;
  display: flex !important; flex-direction: row !important;
  align-items: center !important; justify-content: space-between !important;
  padding: 8px 16px max(10px, env(safe-area-inset-bottom)) !important;
  box-sizing: border-box !important; background: #000 !important; pointer-events: auto !important;
}
body.touch-ui #touchControls .gb-pad {
  display: flex !important; flex-direction: row !important; gap: 12px !important; pointer-events: auto !important;
}
body.touch-ui #touchControls .gb-btn {
  pointer-events: auto !important; touch-action: none !important;
  -webkit-user-select: none !important; user-select: none !important; -webkit-touch-callout: none !important;
  display: flex !important; align-items: center !important; justify-content: center !important;
  color: #fff !important; font-weight: 900 !important; font-family: system-ui, sans-serif !important;
  background: #222 !important; border: 3px solid #555 !important; box-shadow: 0 4px 0 #000 !important;
  margin: 0 !important; padding: 0 !important;
}
body.touch-ui #touchControls .gb-left,
body.touch-ui #touchControls .gb-right {
  width: 76px !important; height: 76px !important; border-radius: 14px !important; font-size: 28px !important;
}
body.touch-ui #touchControls .gb-go {
  width: 92px !important; height: 92px !important; border-radius: 50% !important; font-size: 32px !important;
  background: #333 !important; border: 4px solid #777 !important;
}
body.touch-ui #touchControls .gb-btn.is-pressed {
  transform: translateY(3px) !important; background: #0a7ad9 !important; box-shadow: 0 1px 0 #000 !important;
}
body.touch-ui #touchControls .gb-go.is-pressed { background: #c62828 !important; }

/* === TOP 10 visible before race (above start overlay) === */
.live-leaderboard {
  z-index: 25 !important;
  opacity: 1 !important;
  visibility: visible !important;
  color: #fff !important;
  background: rgba(0, 24, 58, 0.94) !important;
}
.live-leaderboard.hud-live,
body.desktop-ui .live-leaderboard,
body.touch-ui .live-leaderboard.hud-live {
  display: block !important;
  opacity: 1 !important;
  visibility: visible !important;
  pointer-events: auto !important;
  filter: none !important;
}
.live-leaderboard h3,
.live-leaderboard .lb-name,
.live-leaderboard .lb-num,
.live-leaderboard .lb-score,
.live-leaderboard li,
.live-leaderboard ol {
  opacity: 1 !important;
  color: #fff !important;
}
/* Keep it out of the way of the Game Boy bar while racing on phones */
body.touch-ui.in-race-controls .live-leaderboard {
  display: none !important;
}
/* === VIEW LEADERBOARD BTN + KEY ICONS === */
.btn-view-leaderboard {
  display: block;
  width: 100%;
  box-sizing: border-box;
  margin-top: 10px;
  padding: 11px 12px;
  text-align: center;
  text-decoration: none !important;
  font-weight: 900;
  font-size: 13px;
  letter-spacing: 0.04em;
  color: #0b1f3a !important;
  background: #ffd400;
  border: 0;
  border-radius: 12px;
  box-shadow: 0 3px 0 rgba(0,0,0,.25);
}
.btn-view-leaderboard:hover,
.btn-view-leaderboard:active {
  filter: brightness(1.05);
  color: #0b1f3a !important;
}
.live-leaderboard .btn-view-leaderboard {
  margin-top: 12px;
}
.side-key .btn-view-leaderboard--key {
  margin-top: 12px;
}

/* Bigger KEY icons */
.side-key .key-icon {
  width: 44px !important;
  height: 44px !important;
  border-radius: 10px !important;
  font-size: 20px !important;
}
.side-key .key-icon svg,
.key-icon svg {
  width: 34px !important;
  height: 34px !important;
}
.side-key .key-label {
  font-size: 13px !important;
}
.side-key .key-item {
  gap: 8px !important;
  align-items: center !important;
}

/* replaced by DESKTOP LB/KEY ALIGN */
/* === DESKTOP LB/KEY ALIGN === */
body.desktop-ui .live-leaderboard,
body.desktop-ui .side-key {
  width: 240px !important;
  max-width: 240px !important;
  box-sizing: border-box !important;
}
body.desktop-ui .live-leaderboard {
  display: block !important;
  opacity: 1 !important;
  visibility: visible !important;
  filter: none !important;
  z-index: 30 !important;
  right: 12px !important;
  top: 12px !important;
  background: rgba(0, 24, 58, 0.96) !important;
}
body.desktop-ui .side-key {
  right: 12px !important;
  width: 240px !important;
  max-width: 240px !important;
}
/* Desktop: button under TOP 10 only */
body.desktop-ui .live-leaderboard .btn-view-leaderboard {
  display: block !important;
  margin-top: 12px !important;
}
body.desktop-ui .side-key .btn-view-leaderboard--key {
  display: none !important;
}
/* Mobile/touch: button under KEY only */
body.touch-ui .live-leaderboard .btn-view-leaderboard {
  display: none !important;
}
body.touch-ui .side-key .btn-view-leaderboard--key {
  display: block !important;
}
/* === DESKTOP RIGHT SIDEBAR === */
body.desktop-ui {
  --sidebar-w: 260px;
}
body.desktop-ui .desktop-sidebar {
  position: fixed !important;
  top: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  width: var(--sidebar-w) !important;
  z-index: 40 !important;
  display: flex !important;
  flex-direction: column !important;
  gap: 12px !important;
  padding: 12px 12px max(12px, env(safe-area-inset-bottom)) !important;
  box-sizing: border-box !important;
  background: rgba(4, 18, 40, 0.92) !important;
  border-left: 2px solid rgba(255,255,255,0.18) !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  pointer-events: auto !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard {
  position: static !important;
  width: 100% !important;
  max-width: none !important;
  max-height: none !important;
  overflow: visible !important;
  opacity: 1 !important;
  visibility: visible !important;
  filter: none !important;
  background: rgba(0, 30, 70, 0.95) !important;
  margin: 0 !important;
  flex: 0 0 auto !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard ol {
  max-height: none !important;
  overflow: visible !important;
  margin: 0 !important;
  padding: 0 !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard li {
  padding: 5px 6px !important;
  font-size: 12px !important;
}
body.desktop-ui .btn-view-leaderboard--sidebar {
  display: block !important;
  width: 100% !important;
  flex: 0 0 auto !important;
  margin: 0 !important;
  padding: 12px 10px !important;
  text-align: center !important;
  text-decoration: none !important;
  font-weight: 900 !important;
  font-size: 13px !important;
  color: #0b1f3a !important;
  background: #ffd400 !important;
  border-radius: 12px !important;
  box-shadow: 0 3px 0 rgba(0,0,0,.3) !important;
}
body.desktop-ui .desktop-sidebar .side-key {
  position: static !important;
  width: 100% !important;
  max-width: none !important;
  max-height: none !important;
  overflow: visible !important;
  margin: 0 !important;
  flex: 1 1 auto !important;
}
body.desktop-ui .btn-view-leaderboard--key {
  display: none !important;
}

/* Push game + HUD left of sidebar */
body.desktop-ui .game-shell /*ok*/ {
  width: calc(100vw - var(--sidebar-w)) !important;
  max-width: calc(100vw - var(--sidebar-w)) !important;
  margin-right: var(--sidebar-w) !important;
}
body.desktop-ui #game,
body.desktop-ui canvas#game {
  width: calc(100vw - var(--sidebar-w)) !important;
  max-width: calc(100vw - var(--sidebar-w)) !important;
}
body.desktop-ui .hud {
  max-width: calc(100vw - var(--sidebar-w) - 24px) !important;
}
body.desktop-ui .screen-overlay, body.desktop-ui #startScreen, body.desktop-ui #gameOverScreen {
  width: calc(100vw - var(--sidebar-w)) !important;
  right: var(--sidebar-w) !important;
  left: 0 !important;
}

/* Mobile: sidebar is not a fixed column */
/* replaced: hide sidebar on touch */
body.touch-ui .btn-view-leaderboard--sidebar {
  display: none !important;
}
body.touch-ui .btn-view-leaderboard--key {
  display: block !important;
}
/* === DESKTOP SIDEBAR + START MODAL FIX === */
body.desktop-ui {
  --sidebar-w: 270px;
}
body.desktop-ui .desktop-sidebar {
  position: fixed !important;
  top: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  width: var(--sidebar-w) !important;
  z-index: 50 !important;
  display: flex !important;
  flex-direction: column !important;
  gap: 10px !important;
  padding: 12px !important;
  box-sizing: border-box !important;
  background: #071a33 !important;
  border-left: 2px solid rgba(255,255,255,.2) !important;
  overflow-x: hidden !important;
  overflow-y: auto !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard {
  position: static !important;
  width: 100% !important;
  max-height: none !important;
  overflow: visible !important;
  opacity: 1 !important;
  visibility: visible !important;
  margin: 0 !important;
  flex: 0 0 auto !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard ol {
  max-height: none !important;
  overflow: visible !important;
}
body.desktop-ui .btn-view-leaderboard--sidebar {
  display: block !important;
  width: 100% !important;
  flex: 0 0 auto !important;
  text-align: center !important;
  padding: 12px 10px !important;
  font-weight: 900 !important;
  color: #0b1f3a !important;
  background: #ffd400 !important;
  border-radius: 12px !important;
  text-decoration: none !important;
}
body.desktop-ui .desktop-sidebar .side-key {
  position: static !important;
  width: 100% !important;
  max-height: none !important;
  overflow: visible !important;
  margin: 0 !important;
  flex: 0 0 auto !important;
}
body.desktop-ui .btn-view-leaderboard--key { display: none !important; }

/* Game + start modal use left area only */
body.desktop-ui #game,
body.desktop-ui canvas#game {
  width: calc(100vw - var(--sidebar-w)) !important;
  max-width: calc(100vw - var(--sidebar-w)) !important;
}
body.desktop-ui #startScreen.screen-overlay,
body.desktop-ui #gameOverScreen.screen-overlay,
body.desktop-ui #settingsPanel {
  left: 0 !important;
  right: var(--sidebar-w) !important;
  width: auto !important;
  max-width: none !important;
}
body.desktop-ui #startScreen .start-panel,
body.desktop-ui #startScreen .panel {
  position: relative !important;
  z-index: 60 !important;
  opacity: 1 !important;
  visibility: visible !important;
  display: block !important;
  margin: 4vh auto 0 !important;
  max-width: min(420px, calc(100vw - var(--sidebar-w) - 48px)) !important;
}

/* Mobile: don't use fixed sidebar */
/* replaced: hide sidebar on touch */
body.touch-ui .btn-view-leaderboard--sidebar { display: none !important; }
body.touch-ui .btn-view-leaderboard--key { display: block !important; }
/* === KEY FIT + MOBILE START EXTRAS === */
/* Desktop: compress TOP10 + KEY so all fit without clipping */
body.desktop-ui .desktop-sidebar {
  gap: 8px !important;
  padding: 10px 10px 8px !important;
  overflow-y: auto !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard {
  padding: 8px 8px 6px !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard h3 {
  font-size: 13px !important;
  margin: 0 0 4px !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard .lb-head {
  font-size: 10px !important;
  margin-bottom: 2px !important;
}
body.desktop-ui .desktop-sidebar .live-leaderboard li {
  padding: 2px 4px !important;
  font-size: 11px !important;
  line-height: 1.2 !important;
  min-height: 0 !important;
}
body.desktop-ui .btn-view-leaderboard--sidebar {
  padding: 10px 8px !important;
  font-size: 12px !important;
  margin: 0 !important;
}
body.desktop-ui .desktop-sidebar .side-key,
body.desktop-ui .desktop-sidebar .game-key {
  padding: 8px !important;
  max-height: none !important;
  overflow: visible !important;
}
body.desktop-ui .desktop-sidebar .game-key > strong {
  font-size: 12px !important;
  margin: 0 0 6px !important;
}
body.desktop-ui .desktop-sidebar .key-grid {
  display: grid !important;
  grid-template-columns: 1fr 1fr !important;
  gap: 6px !important;
}
body.desktop-ui .desktop-sidebar .key-item {
  gap: 6px !important;
  align-items: center !important;
}
body.desktop-ui .desktop-sidebar .key-icon {
  width: 28px !important;
  height: 28px !important;
  flex: 0 0 28px !important;
}
body.desktop-ui .desktop-sidebar .key-icon svg {
  width: 22px !important;
  height: 22px !important;
}
body.desktop-ui .desktop-sidebar .key-label {
  font-size: 10px !important;
  line-height: 1.15 !important;
}

/* Mobile start: show KEY + button under the start card */
.mobile-start-extras {
  display: none;
}
body.touch-ui #startScreen.visible .mobile-start-extras,
body.touch-ui #startScreen.screen-overlay.visible .mobile-start-extras {
  display: block !important;
  width: min(420px, calc(100vw - 24px)) !important;
  margin: 10px auto 16px !important;
  position: relative !important;
  z-index: 70 !important;
}
body.touch-ui .mobile-start-extras .side-key {
  display: block !important;
  position: static !important;
  width: 100% !important;
  max-width: none !important;
  margin: 0 !important;
  opacity: 1 !important;
  visibility: visible !important;
  background: rgba(0, 24, 58, 0.94) !important;
  border-radius: 14px !important;
  padding: 12px !important;
}
body.touch-ui .mobile-start-extras .btn-view-leaderboard--key {
  display: block !important;
  width: 100% !important;
  margin-top: 10px !important;
  padding: 14px 12px !important;
  text-align: center !important;
  font-weight: 900 !important;
  font-size: 15px !important;
  color: #0b1f3a !important;
  background: #ffd400 !important;
  border-radius: 12px !important;
  text-decoration: none !important;
}
body.touch-ui #startScreen.visible {
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  align-items: flex-start !important;
  padding-bottom: max(20px, env(safe-area-inset-bottom)) !important;
}
/* Hide the desktop sidebar copies of KEY/TOP10 on phones (keep TOP10 for race CSS as before) */
body.touch-ui .desktop-sidebar > .live-leaderboard,
body.touch-ui .desktop-sidebar > .btn-view-leaderboard--sidebar,
body.touch-ui .desktop-sidebar > .side-key {
  display: none !important;
}
body.desktop-ui .mobile-start-extras {
  display: none !important;
}
/* === MOBILE START ORDER FIX === */
/* Never let the desktop sidebar leak into the mobile start layout */
body.touch-ui .desktop-sidebar {
  display: none !important;
}

body.touch-ui #startScreen.visible {
  display: flex !important;
  flex-direction: column !important;
  justify-content: flex-start !important;
  align-items: center !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch !important;
  padding: 12px 12px max(20px, env(safe-area-inset-bottom)) !important;
}

body.touch-ui #startScreen .panel,
body.touch-ui #startScreen .start-panel {
  order: 1 !important;
  position: relative !important;
  width: min(420px, 100%) !important;
  margin: 0 auto 12px !important;
  flex: 0 0 auto !important;
}

body.touch-ui #startScreen .mobile-start-extras {
  order: 2 !important;
  display: block !important;
  width: min(420px, 100%) !important;
  margin: 0 auto 16px !important;
  position: relative !important;
  z-index: 5 !important;
}

body.touch-ui #startScreen .mobile-start-extras .side-key {
  display: block !important;
  position: static !important;
  width: 100% !important;
  margin: 0 !important;
  opacity: 1 !important;
  visibility: visible !important;
}

body.touch-ui #startScreen .mobile-start-extras .btn-view-leaderboard--key {
  display: block !important;
  width: 100% !important;
  margin-top: 10px !important;
}

body.desktop-ui .mobile-start-extras {
  display: none !important;
}
/* === MOBILE START COMPACT FIT === */
body.touch-ui #startScreen.visible {
  padding: 6px 10px max(8px, env(safe-area-inset-bottom)) !important;
  gap: 8px !important;
  justify-content: flex-start !important;
}
body.touch-ui #startScreen .panel,
body.touch-ui #startScreen .start-panel {
  margin: 0 auto !important;
  padding: 10px 12px 8px !important;
  width: min(420px, 100%) !important;
  gap: 6px !important;
}
body.touch-ui #startScreen .start-logo {
  width: min(160px, 42vw) !important;
  max-height: 72px !important;
  object-fit: contain !important;
  margin: 0 auto 2px !important;
}
body.touch-ui #startScreen h1 {
  display: none !important; /* logo already carries the title */
}
body.touch-ui #startScreen .subtitle {
  margin: 0 0 6px !important;
  font-size: 12px !important;
  line-height: 1.2 !important;
}
body.touch-ui #startScreen .setup-grid,
body.touch-ui #startScreen .setup-row {
  gap: 6px !important;
  margin: 0 0 6px !important;
}
body.touch-ui #startScreen label {
  font-size: 11px !important;
  margin: 0 !important;
  gap: 2px !important;
}
body.touch-ui #startScreen input {
  padding: 8px 10px !important;
  font-size: 14px !important;
  min-height: 36px !important;
}
body.touch-ui #startScreen #startGame,
body.touch-ui #startScreen button#startGame {
  margin: 4px 0 6px !important;
  padding: 11px 12px !important;
  font-size: 15px !important;
}
body.touch-ui #startScreen .controls-block {
  margin: 0 !important;
  padding: 6px 8px !important;
}
body.touch-ui #startScreen .controls-title {
  display: none !important;
}
body.touch-ui #startScreen .controls-hint {
  margin: 0 !important;
  font-size: 11px !important;
  line-height: 1.25 !important;
}
body.touch-ui #startScreen .controls-hint:not(.touch-only-hint) {
  display: none !important; /* desktop key hints off on phone */
}
body.touch-ui #startScreen .mobile-start-extras {
  margin: 0 auto !important;
  width: min(420px, 100%) !important;
}
body.touch-ui #startScreen .mobile-start-extras .side-key,
body.touch-ui #startScreen .mobile-start-extras .game-key {
  padding: 8px 10px !important;
  margin: 0 !important;
}
body.touch-ui #startScreen .mobile-start-extras .game-key > strong {
  font-size: 11px !important;
  margin: 0 0 4px !important;
}
body.touch-ui #startScreen .mobile-start-extras .key-grid {
  grid-template-columns: 1fr 1fr !important;
  gap: 4px 8px !important;
}
body.touch-ui #startScreen .mobile-start-extras .key-item {
  gap: 6px !important;
  min-height: 0 !important;
}
body.touch-ui #startScreen .mobile-start-extras .key-icon {
  width: 26px !important;
  height: 26px !important;
}
body.touch-ui #startScreen .mobile-start-extras .key-icon svg {
  width: 20px !important;
  height: 20px !important;
}
body.touch-ui #startScreen .mobile-start-extras .key-label {
  font-size: 10px !important;
  line-height: 1.15 !important;
}
body.touch-ui #startScreen .mobile-start-extras .btn-view-leaderboard--key {
  margin-top: 8px !important;
  padding: 10px 12px !important;
  font-size: 13px !important;
}
/* === MOBILE START HIDE ON PLAY === */
body.touch-ui #startScreen:not(.visible),
body.touch-ui #startScreen:not(.visible) .mobile-start-extras,
body.touch-ui #startScreen:not(.visible) .side-key,
body.touch-ui #startScreen:not(.visible) .panel {
  display: none !important;
  visibility: hidden !important;
  opacity: 0 !important;
  pointer-events: none !important;
  z-index: -1 !important;
}
body.touch-ui #startScreen.visible {
  display: flex !important;
  visibility: visible !important;
  opacity: 1 !important;
  pointer-events: auto !important;
  z-index: 40 !important;
}
body.touch-ui #startScreen:not(.visible) {
  display: none !important;
}