* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  background: #0a0a0e;
  color: #f4f4f0;
  font-family: 'Courier New', Courier, monospace;
  overflow: hidden;
  user-select: none;
}

#game-wrapper {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at center, #181822 0%, #050508 100%);
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#hud-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(6, 6, 12, 0.84);
  z-index: 10;
  padding: 20px;
}

.overlay.hidden {
  display: none;
}

.title-card, .status-card {
  max-width: 640px;
  width: 100%;
  background: #12131c;
  border: 4px solid #f5b82e;
  box-shadow: 0 0 0 4px #000, 0 20px 60px rgba(0, 0, 0, 0.85);
  padding: 28px 32px;
  text-align: center;
}

.badge {
  display: inline-block;
  background: #d93829;
  color: #fff;
  font-weight: bold;
  font-size: 12px;
  letter-spacing: 2px;
  padding: 4px 10px;
  margin-bottom: 14px;
}

h1 {
  font-size: 46px;
  line-height: 1.02;
  letter-spacing: 2px;
  color: #fff;
  text-shadow: 4px 4px 0 #d93829, 7px 7px 0 #000;
  margin-bottom: 8px;
}

h1 span {
  color: #f5b82e;
}

.subtitle {
  color: #9ba3b8;
  letter-spacing: 3px;
  font-size: 13px;
  margin-bottom: 22px;
}

.retro-btn {
  background: #f5b82e;
  color: #111;
  border: 3px solid #fff;
  box-shadow: 4px 4px 0 #000;
  font-family: inherit;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 1px;
  padding: 12px 24px;
  cursor: pointer;
  transition: transform 0.08s;
  margin-bottom: 22px;
}

.retro-btn:hover {
  background: #ffd059;
  transform: translate(-1px, -1px);
}

.retro-btn:active {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 #000;
}

.controls-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 16px;
  text-align: left;
  font-size: 13px;
  background: #0b0c12;
  border: 2px solid #2a2d3d;
  padding: 14px 16px;
  margin-bottom: 16px;
}

kbd {
  display: inline-block;
  background: #252836;
  border: 1px solid #555c78;
  border-bottom-width: 2px;
  padding: 1px 6px;
  font-family: inherit;
  font-size: 12px;
  color: #f5b82e;
}

.tip {
  font-size: 12px;
  color: #b7c0d4;
  line-height: 1.5;
}

.tip strong {
  color: #4be37a;
}

.status-card h2 {
  font-size: 48px;
  letter-spacing: 4px;
  margin-bottom: 10px;
  text-shadow: 4px 4px 0 #000;
}

.status-card p {
  color: #b7c0d4;
  margin-bottom: 22px;
  font-size: 15px;
}

.btn-row {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.secondary-btn {
  background: #252836;
  color: #f4f4f0;
  border-color: #9ba3b8;
}

.secondary-btn:hover {
  background: #35394d;
}

.settings-section-title {
  color: #f5b82e;
  font-size: 13px;
  font-weight: bold;
  letter-spacing: 3px;
  border-bottom: 2px solid #2a2d3d;
  padding-bottom: 8px;
  margin-bottom: 16px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: #0b0c12;
  border: 2px solid #2a2d3d;
  padding: 14px 16px;
  margin-bottom: 22px;
  text-align: left;
}

.settings-label strong {
  display: block;
  font-size: 14px;
  color: #fff;
  margin-bottom: 4px;
}

.settings-label span {
  display: block;
  font-size: 12px;
  color: #9ba3b8;
}

.toggle-btn {
  min-width: 84px;
  margin-bottom: 0;
  padding: 8px 16px;
  background: #333748;
  color: #cbd5e1;
  border-color: #64748b;
}

.toggle-btn:hover {
  background: #43485c;
}

.toggle-btn.on {
  background: #4be37a;
  color: #0b1910;
  border-color: #fff;
}

.toggle-btn.on:hover {
  background: #6bf096;
}

.settings-row.disabled {
  opacity: 0.45;
}

.slider-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}

.slider-wrap input[type="range"] {
  width: 110px;
  accent-color: #f5b82e;
  cursor: pointer;
}

.slider-wrap span {
  min-width: 42px;
  text-align: right;
  font-size: 13px;
  font-weight: bold;
  color: #f5b82e;
}


/* Mobile HUD and touch input
   The world and HUD keep receiving frames, but the control overlay only
   intercepts pointer events within actual touch targets. */
.mobile-controls {
  --touch-size: 1;
  --touch-opacity: 0.9;
  display: none;
}
.mobile-controls [hidden] { display: none !important; }
.touch-hint { display: none; }

@media (any-pointer: coarse), (max-width: 700px) {
  html, body, #game-wrapper { overscroll-behavior: none; }
  #game-wrapper { height: 100dvh; }
  #game-canvas { touch-action: none; }

  .mobile-controls:not([hidden]) {
    display: block;
    position: absolute;
    inset: 0;
    z-index: 5;
    pointer-events: none;
  }

  .touch-btn {
    -webkit-appearance: none;
    appearance: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: none;
    pointer-events: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 48px;
    min-width: 48px;
    padding: 8px 9px;
    border: 2px solid rgba(226, 231, 241, 0.76);
    border-radius: 13px;
    background: rgba(24, 30, 41, 0.86);
    color: #fff;
    opacity: var(--touch-opacity);
    box-shadow: inset 0 2px 0 rgba(255,255,255,0.09), 0 4px 0 rgba(0,0,0,0.74);
    text-shadow: 1px 2px #000;
    font: 800 12px/1.05 'Courier New', Courier, monospace;
    letter-spacing: 0.35px;
    transition: background-color 70ms, box-shadow 70ms;
  }
  .touch-btn.pressed, .touch-btn:active {
    background: #f5b82e;
    color: #131419;
    opacity: 1;
    text-shadow: none;
    box-shadow: inset 0 2px 2px rgba(0,0,0,0.24), 0 1px 0 #000;
  }
  .touch-btn:focus-visible {
    outline: 3px solid #4be37a;
    outline-offset: 2px;
  }
  .touch-primary { background: rgba(192, 54, 41, 0.92); }
  .touch-small {
    min-width: 54px;
    min-height: 43px;
    border-radius: 10px;
    font-size: 10px;
    padding: 6px 8px;
  }

  /* Keep pause close to the status strip, clear of the minimap. */
  .touch-pause {
    position: absolute;
    top: calc(76px + env(safe-area-inset-top, 0px));
    right: calc(12px + env(safe-area-inset-right, 0px));
    width: 48px;
    height: 46px;
    font-size: 18px;
    z-index: 2;
  }

  /* Thumb-following, diagonal-capable walk pad. */
  .touch-dpad {
    position: absolute;
    left: calc(14px + env(safe-area-inset-left, 0px));
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    width: 166px;
    height: 166px;
    display: grid;
    grid-template: repeat(3, 1fr) / repeat(3, 1fr);
    gap: 2px;
    border: 2px solid rgba(181, 195, 216, 0.42);
    border-radius: 50%;
    padding: 5px;
    background: rgba(11, 16, 27, 0.42);
    pointer-events: auto;
    touch-action: none;
    transform: scale(var(--touch-size));
    transform-origin: bottom left;
  }
  .touch-dpad::after {
    content: '';
    grid-area: 2 / 2;
    align-self: center;
    justify-self: center;
    width: 18px;
    height: 18px;
    border: 2px solid rgba(226,231,241,0.4);
    background: rgba(226,231,241,0.13);
    border-radius: 50%;
    pointer-events: none;
  }
  .touch-dpad .touch-btn {
    min-width: 0;
    min-height: 0;
    font-size: 20px;
    border-radius: 12px;
  }
  .touch-dpad .up { grid-area: 1 / 2; }
  .touch-dpad .left { grid-area: 2 / 1; }
  .touch-dpad .right { grid-area: 2 / 3; }
  .touch-dpad .down { grid-area: 3 / 2; }

  /* The right thumb holds FIRE while other actions sit directly above. */
  .touch-foot-actions {
    position: absolute;
    right: calc(14px + env(safe-area-inset-right, 0px));
    bottom: calc(22px + env(safe-area-inset-bottom, 0px));
    width: 135px;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: 46px 88px;
    gap: 10px 8px;
    pointer-events: none;
    transform: scale(var(--touch-size));
    transform-origin: bottom right;
  }
  .touch-foot-actions .touch-primary {
    grid-area: 2 / 1 / 3 / 3;
    justify-self: end;
    min-height: 86px;
    min-width: 88px;
    border-radius: 50%;
    font-size: 17px;
  }

  .touch-drive { position: absolute; inset: 0; pointer-events: none; }
  /* Car steering deliberately remains LEFT / RIGHT, never a joystick. */
  .touch-steering {
    position: absolute;
    left: calc(14px + env(safe-area-inset-left, 0px));
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    display: flex;
    gap: 9px;
    pointer-events: none;
    transform: scale(var(--touch-size));
    transform-origin: bottom left;
  }
  .touch-steering > button:not(.touch-drift) {
    width: 77px;
    height: 88px;
    font-size: 27px;
  }
  .touch-steering .touch-drift {
    position: absolute;
    left: 42px;
    bottom: calc(100% + 10px);
    min-width: 78px;
    min-height: 48px;
    background: rgba(183, 101, 29, 0.94);
    font-size: 11px;
  }

  /* Gas and foot brake are easy to hold together with steering. Reverse
     lives above the brake and is visibly smaller to avoid accidental use. */
  .touch-pedals {
    position: absolute;
    right: calc(14px + env(safe-area-inset-right, 0px));
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    display: grid;
    grid-template-columns: 82px 82px;
    grid-template-rows: 50px 92px;
    gap: 8px;
    pointer-events: none;
    transform: scale(var(--touch-size));
    transform-origin: bottom right;
  }
  .touch-pedals .touch-btn {
    min-width: 0;
    min-height: 0;
    font-size: 11px;
  }
  .touch-pedals [data-hold="KeyW"] {
    grid-column: 1; grid-row: 1 / 3;
    font-size: 16px;
    background: rgba(28, 126, 72, 0.92);
  }
  .touch-pedals [data-hold="KeyS"] { grid-column: 2; grid-row: 1; }
  .touch-pedals [data-hold="KeyB"] {
    grid-column: 2; grid-row: 2;
    background: rgba(164, 55, 41, 0.92);
    font-size: 13px;
  }
  .touch-pedals .touch-btn.pressed { background: #f5b82e; }

  /* Secondary actions stay beside the upper HUD, never over the pedals. */
  .touch-drive-actions {
    position: absolute;
    top: calc(78px + env(safe-area-inset-top, 0px));
    left: calc(12px + env(safe-area-inset-left, 0px));
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
    max-width: min(230px, calc(100vw - 120px));
    pointer-events: none;
    transform: scale(var(--touch-size));
    transform-origin: top left;
  }
  .touch-tank-fire { background: rgba(192, 54, 41, 0.94); }

  .title-card, .status-card {
    max-height: calc(100dvh - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
    padding: 20px 18px;
  }
  .overlay { padding: 16px; }
  .title-card h1 { font-size: clamp(27px, 8vw, 46px); }
  .controls-grid { display: none; }
  .touch-hint { display: block; margin-bottom: 12px; color: #f5b82e; }

  .settings-card .settings-row { flex-wrap: wrap; margin-bottom: 12px; padding: 10px; }
  .settings-card .slider-wrap { width: 100%; justify-content: space-between; }
  .settings-card .slider-wrap input[type="range"] { flex: 1; width: auto; min-width: 80px; }
  .settings-card .settings-section-title { margin-top: 10px; }
}

@media (any-pointer: coarse) and (max-height: 480px),
       (max-width: 700px) and (max-height: 480px) {
  .touch-dpad { width: 148px; height: 148px; bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  .touch-dpad .touch-btn { font-size: 17px; }
  .touch-foot-actions {
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    grid-template-rows: 41px 70px;
    width: 126px;
    gap: 7px;
  }
  .touch-foot-actions .touch-primary { min-height: 70px; min-width: 74px; }
  .touch-steering { bottom: calc(12px + env(safe-area-inset-bottom, 0px)); }
  .touch-steering > button:not(.touch-drift) { width: 70px; height: 72px; }
  .touch-steering .touch-drift { left: 37px; min-height: 40px; bottom: calc(100% + 6px); }
  .touch-pedals {
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    grid-template-columns: 76px 76px;
    grid-template-rows: 42px 76px;
    gap: 6px;
  }
  .touch-pause { top: calc(74px + env(safe-area-inset-top, 0px)); }
  .touch-drive-actions { top: calc(78px + env(safe-area-inset-top, 0px)); }
  .touch-drive-actions .touch-small { min-height: 40px; }
}

@media (max-width: 390px) {
  .touch-dpad, .touch-steering { transform: scale(min(1.04, var(--touch-size))); }
  .touch-foot-actions, .touch-pedals, .touch-drive-actions {
    transform: scale(min(1.04, var(--touch-size)));
  }
}

/* Optional southpaw layout swaps the primary thumb zones, not the button
   meanings. Steering still uses left/right, gas still accelerates. */
@media (any-pointer: coarse), (max-width: 700px) {
  .mobile-controls.left-handed .touch-dpad,
  .mobile-controls.left-handed .touch-steering {
    left: auto;
    right: calc(14px + env(safe-area-inset-right, 0px));
    transform-origin: bottom right;
  }
  .mobile-controls.left-handed .touch-foot-actions,
  .mobile-controls.left-handed .touch-pedals {
    right: auto;
    left: calc(14px + env(safe-area-inset-left, 0px));
    transform-origin: bottom left;
  }
}
