* {
  box-sizing: border-box;
}

html {
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: #07080c;
  color: #b8e8ff;
  font-family: "Segoe UI", system-ui, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
}

body.race-active {
  overflow: hidden;
  touch-action: none;
  align-items: stretch;
  justify-content: flex-start;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* Menus: letterboxed 16:9. Racing: full-viewport column — stage + separate touch bar (no overlap). */
#app.arcade-cab {
  position: relative;
  width: min(100vw - 8px, calc(100dvh * 16 / 9 - 8px));
  height: min(100dvh - 8px, calc(100vw * 9 / 16 - 8px));
  max-width: 100%;
  box-shadow:
    0 0 0 2px #1a5088,
    0 0 24px rgba(0, 200, 255, 0.08),
    0 8px 32px rgba(0, 0, 0, 0.65);
}

#app.arcade-cab.app--racing {
  width: 100%;
  max-width: 100%;
  height: 100dvh;
  height: 100svh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  box-shadow: none;
}

.game-stage {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 0;
  flex: 1 1 auto;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#app.app--racing .game-stage {
  flex: 1 1 auto;
  min-height: 0;
}

/**
 * Logical 960×540 design; JS sets transform: scale(s) with s = min(stageW/960, stageH/540)
 * so portrait/landscape stay 16:9 with letterboxing inside #game-stage — no stretch.
 */
.game-scale-wrap {
  position: relative;
  width: 960px;
  height: 540px;
  transform-origin: center center;
  flex-shrink: 0;
}

.game-scale-wrap::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 0, 0, 0.12) 2px,
    rgba(0, 0, 0, 0.12) 3px
  );
  opacity: 0.35;
}

#game-canvas {
  display: block;
  position: absolute;
  inset: 0;
  width: 960px;
  height: 540px;
  max-width: none;
  max-height: none;
  background: #0d1117;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.rotate-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 24;
  max-width: 88%;
  padding: 0.65rem 0.85rem;
  font-size: 0.78rem;
  line-height: 1.35;
  text-align: center;
  background: rgba(8, 14, 24, 0.92);
  border: 1px solid #3a6a9f;
  border-radius: 8px;
  color: #c8e4ff;
  pointer-events: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.rotate-hint.hidden {
  display: none;
}

.rotate-hint-dismiss {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
}

@media (orientation: landscape), (min-width: 700px) {
  .rotate-hint {
    display: none !important;
  }
}

.screen {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 1.5rem;
  background: rgba(4, 8, 14, 0.94);
  backdrop-filter: blur(4px);
}

.screen.hidden {
  display: none;
}

.arcade-eyebrow {
  margin: 0;
  font-size: 0.7rem;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  opacity: 0.45;
  color: #5de8ff;
}

h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-shadow: 0 0 24px rgba(80, 200, 255, 0.2);
}

h2 {
  margin: 0 0 0.5rem;
  font-size: 1.15rem;
}

.tagline {
  margin: 0 0 0.35rem;
  opacity: 0.7;
  font-size: 0.88rem;
  max-width: 26rem;
  text-align: center;
  line-height: 1.45;
}

.attract-line {
  margin: 0 0 1rem;
  font-size: 0.8rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #ff6ec7;
  opacity: 0.85;
  animation: attract-pulse 2.4s ease-in-out infinite;
}

@keyframes attract-pulse {
  0%,
  100% {
    opacity: 0.55;
  }
  50% {
    opacity: 1;
  }
}

.setup-best {
  margin: 0.25rem 0 0;
  font-size: 0.78rem;
  opacity: 0.75;
  color: #7dffb3;
  font-variant-numeric: tabular-nums;
  max-width: min(280px, 90vw);
  text-align: center;
  line-height: 1.35;
}

/* Audio only while racing — top-right of viewport area above the touch strip. */
#app.app--racing .audio-mute {
  display: block;
  position: absolute;
  top: max(6px, env(safe-area-inset-top));
  right: max(6px, env(safe-area-inset-right));
  bottom: auto;
  left: auto;
  z-index: 35;
  font-size: 0.65rem;
  padding: 0.25rem 0.45rem;
  opacity: 0.9;
  pointer-events: auto;
}

#app:not(.app--racing) .audio-mute {
  display: none;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  width: min(280px, 90vw);
  font-size: 0.85rem;
  opacity: 0.9;
}

select,
input[type="number"] {
  padding: 0.4rem 0.5rem;
  background: #121a28;
  border: 1px solid #2a4a6f;
  color: #e8f0ff;
  border-radius: 4px;
}

.setup-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

button {
  padding: 0.45rem 1rem;
  background: #1a2840;
  border: 1px solid #3a5a80;
  color: #d0e4ff;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.95rem;
}

button:hover {
  background: #243552;
}

button.primary {
  background: #1e4a7a;
  border-color: #4a8ad0;
}

.how-list {
  text-align: left;
  max-width: 420px;
  line-height: 1.6;
  margin: 0 0 1rem;
  padding-left: 1.2rem;
}

kbd {
  display: inline-block;
  padding: 0.1em 0.35em;
  font-size: 0.85em;
  background: #121a28;
  border: 1px solid #3a5a80;
  border-radius: 3px;
}

#results-list {
  text-align: left;
  min-width: 240px;
  margin: 0 0 1rem;
}

.hud {
  position: absolute;
  inset: 0;
  z-index: 8;
  pointer-events: none;
}

.hud.hidden {
  display: none;
}

.start-sequence {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 14;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  pointer-events: none;
}

.start-sequence.hidden {
  display: none;
}

.start-lights {
  display: flex;
  gap: 10px;
  padding: 10px 18px;
  background: rgba(8, 12, 20, 0.88);
  border: 1px solid #2a5080;
  border-radius: 6px;
}

.start-lights .light {
  width: 28px;
  height: 44px;
  border-radius: 4px;
  background: #1a2535;
  border: 1px solid #334;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.35);
}

.start-lights .light.on {
  background: linear-gradient(180deg, #ff4a4a 0%, #b01010 100%);
  border-color: #ff8080;
  box-shadow: 0 0 12px rgba(255, 60, 60, 0.65);
}

.start-go {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  color: #5dff8a;
  text-shadow: 0 0 18px rgba(80, 255, 140, 0.55);
}

.start-go.hidden {
  visibility: hidden;
}

.hud-top {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1rem;
  max-width: 96%;
}

.hud-block {
  background: rgba(8, 14, 24, 0.75);
  border: 1px solid #2a5080;
  padding: 0.35rem 0.65rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

.hud-label {
  opacity: 0.55;
  margin-right: 0.35rem;
}

.hud-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.hud-unit {
  opacity: 0.5;
  font-size: 0.7rem;
  margin-left: 0.2rem;
}

.hud-radar {
  position: absolute;
  bottom: 8px;
  right: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  border: 1px solid #2a5080;
  border-radius: 4px;
  background: rgba(6, 10, 18, 0.8);
  text-align: center;
}

#minimap {
  display: block;
}

.hud-radar-caption {
  font-size: 0.55rem;
  line-height: 1.2;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.45;
  padding: 2px 4px 4px;
  max-width: 100%;
}

.hud-board {
  position: absolute;
  top: 52px;
  right: 8px;
  background: rgba(8, 14, 24, 0.78);
  border: 1px solid #2a5080;
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 0.65rem;
  max-width: 42%;
}

.hud-lb {
  border-collapse: collapse;
  width: 100%;
}

.hud-lb th,
.hud-lb td {
  padding: 2px 6px;
  text-align: left;
}

.hud-lb th {
  opacity: 0.55;
  font-weight: 600;
  border-bottom: 1px solid #2a5080;
}

.hud-lb td:nth-child(3) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.results-table {
  border-collapse: collapse;
  min-width: 280px;
  margin: 0 0 1rem;
  font-size: 0.9rem;
}

.results-table th,
.results-table td {
  padding: 0.35rem 0.65rem;
  border-bottom: 1px solid #2a4a6f;
}

.results-table th {
  text-align: left;
  opacity: 0.75;
}

.results-table td:nth-child(1) {
  font-variant-numeric: tabular-nums;
  width: 2.5rem;
}

.results-table td:nth-child(3) {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.results-record {
  margin: 0 0 0.75rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: #7dffb3;
  text-align: center;
}

.results-record.hidden {
  display: none;
}

.results-table tr.results-you td {
  background: rgba(30, 80, 120, 0.25);
}

.hud-wait {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 0.5rem 1rem;
  background: rgba(12, 20, 36, 0.88);
  border: 1px solid #4a8ad0;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: #a8d4ff;
  pointer-events: none;
  z-index: 4;
}

.hud-wait.hidden {
  display: none;
}

.hud-hint {
  position: absolute;
  bottom: 8px;
  left: 8px;
  font-size: 0.65rem;
  opacity: 0.45;
  max-width: min(70%, 16rem);
}

.hud-hint-touch.hidden,
.hud-hint-keys.hidden {
  display: none;
}

.hud-quit {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 16;
  pointer-events: auto;
  font-size: 0.72rem;
  padding: 0.35rem 0.6rem;
  opacity: 0.88;
}

.touch-controls {
  display: none;
  position: relative;
  z-index: 20;
  flex: 0 0 auto;
  flex-direction: row;
  justify-content: space-between;
  align-items: stretch;
  gap: 10px;
  padding: 6px 10px max(8px, env(safe-area-inset-bottom));
  pointer-events: auto;
  touch-action: none;
  background: #0a1018;
  border-top: 1px solid #2a5080;
}

#app.app--racing .touch-controls.active {
  display: flex;
}

@media (pointer: fine) and (min-width: 900px) {
  #app.app--racing .touch-controls.active {
    display: none;
  }
}

.touch-cluster {
  display: flex;
  flex-direction: row;
  gap: 10px;
  align-items: center;
}

.touch-cluster--steer {
  flex: 1;
  justify-content: flex-start;
}

.touch-cluster--pedals {
  flex: 1;
  justify-content: flex-end;
}

.touch-btn {
  min-width: 3.75rem;
  min-height: 3.25rem;
  font-size: 1.15rem;
  font-weight: 700;
  border-radius: 10px;
  border: 1px solid #4a7aaa;
  background: rgba(20, 36, 56, 0.88);
  color: #e8f4ff;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.touch-btn:active,
.touch-btn.touch-down {
  background: rgba(40, 70, 100, 0.95);
}

.touch-pedal.touch-gas {
  border-color: #4a9f6a;
  color: #b8ffd0;
}

.touch-pedal {
  min-width: 4.25rem;
  min-height: 3.25rem;
}

@media (max-width: 420px) {
  .touch-btn {
    min-width: 3rem;
    min-height: 2.6rem;
  }
  .touch-pedal {
    min-width: 3.4rem;
    min-height: 2.75rem;
  }
}

@media (max-width: 700px) {
  .hud-hint {
    display: none !important;
  }
}
