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

/* Splash screen — shown on page load, removed on first click */
#splash {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: #0a0a0a;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-family: 'Courier New', monospace;
  color: #ffcc00;
}
#splash h1 {
  font-size: 3em;
  letter-spacing: 6px;
  text-transform: uppercase;
  margin-bottom: 24px;
}
#splash .splash-prompt {
  font-size: 1.2em;
  letter-spacing: 3px;
  color: #ccc;
  animation: blink 1s infinite;
}
#splash.hidden { display: none; }

/* Persistent sound toggle — visible on splash + menu, hidden during gameplay */
#sound-toggle {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 310;
  width: 40px;
  height: 40px;
  border-radius: 6px;
  border: 2px solid rgba(255, 204, 0, 0.5);
  background: rgba(20, 20, 30, 0.7);
  color: #ffcc00;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
#sound-toggle:active { background: rgba(255, 204, 0, 0.2); }
#sound-toggle.hidden { display: none; }
#sound-toggle svg.hidden { display: none; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #111;
  font-family: 'Courier New', monospace;
  color: #eee;
}

#game-wrapper {
  display: flex;
  width: 100%;
  height: 100vh;  /* fallback */
  height: 100dvh; /* dynamic viewport — iOS URL-bar collapse handled automatically */
}

#gameCanvas {
  display: block;
  flex: 1;
  height: 100%;
  min-width: 0;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

/* Mobile touch controls */
#mobile-controls {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 60;
  pointer-events: none;
}

#mobile-controls.hidden {
  display: none;
}

#joystick-zone {
  position: absolute;
  left: env(safe-area-inset-left, 0);
  top: 0;
  width: 40%; height: 100%;
  pointer-events: auto;
}

#fire-btn {
  position: absolute;
  right: max(30px, env(safe-area-inset-right, 30px));
  bottom: max(50px, env(safe-area-inset-bottom, 50px));
  width: 80px; height: 80px;
  border-radius: 50%;
  background: rgba(255, 80, 40, 0.4);
  border: 3px solid rgba(255, 100, 60, 0.65);
  color: #fff;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  font-weight: bold;
  letter-spacing: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

#fire-btn.pressed {
  background: rgba(255, 120, 60, 0.7);
  transform: scale(0.92);
}

/* Sidebar HUD — brushed metal panel */
#sidebar {
  position: relative;
  width: 120px;
  background:
    /* diagonal brushed-metal hatching */
    repeating-linear-gradient(
      135deg,
      rgba(255,255,255,0.03) 0px,
      rgba(0,0,0,0.02) 1px,
      rgba(255,255,255,0.03) 2px
    ),
    /* main gradient matching reference image */
    linear-gradient(90deg, #5a5e63 0%, #8a8e93 8%, #b0b4b8 30%, #c8ccd0 60%, #b8bcc0 85%, #a0a4a8 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 10px 20px 22px;
  gap: 0;
  flex-shrink: 0;
  z-index: 50;
  box-shadow:
    inset 2px 0 4px rgba(255,255,255,0.3),
    inset -1px 0 3px rgba(0,0,0,0.15),
    -4px 0 12px rgba(0,0,0,0.4);
}

/* Steel beam left border */
#sidebar::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 14px;
  height: 100%;
  /* beam metallic gradient */
  background:
    repeating-linear-gradient(
      180deg,
      transparent 0px,
      rgba(0,0,0,0.03) 1px,
      transparent 2px
    ),
    linear-gradient(90deg, #3a3e42 0%, #6a6e72 20%, #7e8286 40%, #6a6e72 70%, #4a4e52 100%);
  /* beveled edges */
  box-shadow:
    inset 1px 0 0 rgba(255,255,255,0.2),
    inset -1px 0 0 rgba(0,0,0,0.3),
    2px 0 4px rgba(0,0,0,0.3);
}

/* Bolts on the steel beam — tiled via background-size */
#sidebar::after {
  content: '';
  position: absolute;
  left: 3px;
  top: 20px;
  width: 8px;
  height: calc(100% - 40px);
  background:
    /* bolt highlight (top-left catch light) */
    radial-gradient(circle 2.5px at 3px 4px, rgba(255,255,255,0.5) 0%, transparent 50%),
    /* bolt shadow rim */
    radial-gradient(circle 4px at 4px 5px, transparent 45%, rgba(0,0,0,0.3) 50%, transparent 55%),
    /* bolt head */
    radial-gradient(circle 4px at 4px 5px, #78797c 0%, #5a5c60 70%, transparent 71%);
  background-size: 8px 70px;
  background-repeat: repeat-y;
  pointer-events: none;
}

#sidebar.hidden {
  display: none;
}

/* 3D inset section boxes */
.sb-section {
  background: rgba(0,0,0,0.15);
  border-radius: 4px;
  padding: 8px;
  border: 1px solid rgba(0,0,0,0.2);
  box-shadow:
    inset 2px 2px 5px rgba(0,0,0,0.35),
    inset -1px -1px 2px rgba(255,255,255,0.12),
    0 1px 0 rgba(255,255,255,0.15);
}

/* ---- Score popups (cartoon burst + number) ---- */
#score-popups {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 45;
}
.score-popup-wrap {
  position: absolute;
  width: 78px;
  height: 78px;
  margin-left: -39px;
  margin-top: -39px;
  pointer-events: none;
  will-change: transform, opacity;
  animation: score-pop 1s ease-out forwards;
  filter: drop-shadow(2px 3px 0 rgba(0,0,0,0.55));
}
/* Cartoon starburst — 12-point jagged burst.
   Outer layer is solid black to form a thick outline around the inner gradient burst. */
.score-popup,
.score-popup-inner {
  position: absolute;
  clip-path: polygon(
    50%   0%,   61%  18%,  82%  12%,
    78%  33%,  100%  38%,  82%  52%,
   100%  67%,  78%  70%,  88%  92%,
    65%  80%,  60% 100%,  50%  82%,
    40% 100%,  35%  80%,  12%  92%,
    22%  70%,   0%  67%,  18%  52%,
     0%  38%,  22%  33%,  18%  12%,
    39%  18%
  );
}
.score-popup {
  inset: 0;
  background: #000;
}
.score-popup-inner {
  inset: 6px;
  background:
    radial-gradient(circle at 42% 38%,
      #fff8a8 0%,
      #ffd94a 22%,
      #ff9e1a 55%,
      #e85500 90%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.score-popup-inner span {
  font-family: 'Courier New', monospace;
  font-weight: 900;
  font-size: 16px;
  letter-spacing: 1px;
  color: #fff;
  -webkit-text-stroke: 2px #000;
  text-shadow:
    1px 1px 0 #000,
    -1px -1px 0 #000,
    0 2px 3px rgba(0,0,0,0.5);
  transform: rotate(-6deg);
}
@keyframes score-pop {
  0%   { transform: scale(0.2) rotate(-12deg); opacity: 0; }
  18%  { transform: scale(1.35) rotate(6deg);  opacity: 1; }
  32%  { transform: scale(1.0)  rotate(0deg);  opacity: 1; }
  70%  { transform: scale(1.0)  rotate(-2deg); opacity: 1; }
  100% { transform: translate(0, -55px) scale(0.9) rotate(0deg); opacity: 0; }
}

/* Score readout (top of sidebar) */
#sb-score {
  width: 100%;
  text-align: center;
  padding: 6px 4px;
  margin-bottom: 6px;
}
.sb-score-label {
  font-size: 9px;
  letter-spacing: 2px;
  color: rgba(0,0,0,0.55);
  text-shadow: 0 1px 0 rgba(255,255,255,0.2);
  margin-bottom: 2px;
}
#sb-score-value {
  font-size: 18px;
  color: #ffcc00;
  text-shadow:
    0 0 6px rgba(255,204,0,0.35),
    1px 1px 0 #000,
    -1px -1px 0 rgba(0,0,0,0.6);
  font-variant-numeric: tabular-nums;
  letter-spacing: 1px;
  transition: transform 0.15s ease-out;
}
#sb-score-value.bump {
  transform: scale(1.18);
}

/* Enemy icon grid */
#sb-enemies {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  justify-items: center;
  align-items: center;
  margin-bottom: auto;
}

/* Top-down tank icons (background-image set by JS) */
.enemy-icon {
  width: 16px;
  height: 16px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  filter: drop-shadow(1px 1px 1px rgba(0,0,0,0.5));
}

.enemy-icon.enemy-heavy {
  width: 20px;
  height: 20px;
}

/* Status section — rank + active power-ups */
#sb-status {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin: 10px 0;
}

#sb-rank {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

#sb-rank-icon {
  width: 22px;
  height: 22px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  filter: drop-shadow(1px 1px 2px rgba(0,0,0,0.5));
}

#sb-rank-stars {
  display: flex;
  gap: 1px;
  justify-content: center;
}

.sb-star {
  width: 8px;
  height: 8px;
  background-size: contain;
  background-repeat: no-repeat;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.4));
}

#sb-timers {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 100%;
}

.sb-timer {
  display: flex;
  align-items: center;
  gap: 3px;
  width: 100%;
  justify-content: center;
}

.sb-timer-icon {
  width: 16px;
  height: 16px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  filter: drop-shadow(1px 1px 1px rgba(0,0,0,0.4));
}

.sb-timer-text {
  font-family: 'Courier New', monospace;
  font-size: 10px;
  font-weight: bold;
  color: #2a2a2a;
  text-shadow: 0 1px 0 rgba(255,255,255,0.3);
}

/* Player sections */
#sb-players {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin: 16px 0;
}

.sb-player {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.sb-player-label {
  font-family: 'Courier New', monospace;
  font-size: 9px;
  font-weight: bold;
  color: rgba(0,0,0,0.5);
  letter-spacing: 1px;
  text-shadow: 0 1px 0 rgba(255,255,255,0.25);
}

.sb-player-info {
  display: flex;
  align-items: center;
  gap: 3px;
}

.sb-tank-icon {
  width: 20px;
  height: 20px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  filter: drop-shadow(1px 1px 1px rgba(0,0,0,0.5));
}

.sb-lives-x {
  font-family: 'Courier New', monospace;
  font-size: 10px;
  font-weight: bold;
  color: #2a2a2a;
  text-shadow: 0 1px 0 rgba(255,255,255,0.3);
}

.sb-player-info span {
  font-family: 'Courier New', monospace;
  font-size: 16px;
  font-weight: bold;
  color: #2a2a2a;
  text-shadow: 0 1px 0 rgba(255,255,255,0.4);
}

.sb-player.hidden {
  display: none;
}

/* Stage flag */
#sb-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  margin-top: 16px;
}

.sb-flag {
  width: 36px;
  height: 36px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  filter: drop-shadow(1px 2px 2px rgba(0,0,0,0.4));
}

#sb-stage-num {
  font-family: 'Courier New', monospace;
  font-size: 11px;
  font-weight: bold;
  color: #2a2a2a;
  letter-spacing: 1px;
  text-shadow: 0 1px 0 rgba(255,255,255,0.4);
}

/* Power-up indicators */
#sb-powerup {
  font-family: 'Courier New', monospace;
  font-size: 10px;
  color: #2a2a2a;
  text-align: center;
  margin-top: 8px;
  line-height: 1.4;
  min-height: 14px;
  text-shadow: 0 1px 0 rgba(255,255,255,0.3);
}

/* Mobile: hide sidebar */
@media (max-width: 768px) {
  #sidebar { display: none !important; }
}

/* Overlay screens */
.overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.75);
  z-index: 100;
}

.overlay.hidden {
  display: none;
}

.overlay-box {
  text-align: center;
  padding: 40px 60px;
  background: rgba(20, 20, 30, 0.95);
  border: 2px solid #555;
  border-radius: 8px;
  max-width: 640px;
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}

/* Stacked-column menu layout — default for desktop + portrait. Splits into
   two columns only on short screens (phones in landscape, see the
   max-height:480px block below) so the menu fits without scrolling. */
.menu-grid {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: 24px;
}
.menu-grid .menu-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: stretch;
  width: 240px;
  max-width: 100%;
}
.menu-grid .menu-col .menu-btn {
  width: 100%;
  min-width: 0;
}
.menu-grid .option-label {
  margin-bottom: 6px;
  text-align: left;
}
/* Spacer that only reserves vertical space when the grid is in 2-column
   mode — used to push lobby CREATE ROOM down one row to line up with the
   first mode button. Hidden on stacked desktop so it doesn't leave a gap. */
.menu-grid .grid-spacer {
  display: none;
}

/* Tighter padding on short screens (phones in landscape) */
@media (max-height: 480px) {
  .overlay-box {
    padding: 18px 28px;
  }
  .overlay-box h1 {
    font-size: 1.6em;
    margin-bottom: 8px;
  }
  .overlay-box h2 {
    font-size: 1.15em;
    margin-bottom: 6px;
  }
  .overlay-box p {
    font-size: 0.85em;
    margin-bottom: 4px;
  }
  .menu-buttons {
    margin-top: 12px;
    gap: 6px;
  }
  .menu-btn {
    font-size: 0.95em;
    padding: 8px 28px;
    min-width: 180px;
  }
  .menu-grid {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: center;
    margin-top: 10px;
    gap: 14px 18px;
  }
  .menu-grid .menu-col {
    flex: 1 1 220px;
    width: auto;
    max-width: 280px;
    min-width: 0;
    gap: 6px;
  }
  .menu-grid .grid-spacer {
    display: block;
    visibility: hidden;
  }
  .overlay-box .stats {
    margin-top: 8px;
  }
}

.overlay-box h1 {
  font-size: 2.2em;
  margin-bottom: 16px;
  text-transform: uppercase;
  letter-spacing: 3px;
}

.overlay-box h2 {
  font-size: 1.4em;
  margin-bottom: 12px;
  color: #aaa;
}

.overlay-box p {
  font-size: 1em;
  margin-bottom: 8px;
  color: #ccc;
}

.overlay-box .action {
  margin-top: 24px;
  font-size: 1.1em;
  color: #ffcc00;
  animation: blink 1s infinite;
}

@keyframes blink {
  50% { opacity: 0.4; }
}

.overlay-box .stats {
  margin-top: 16px;
  font-size: 0.9em;
  color: #999;
}

/* Controls toggle */
.controls-toggle {
  margin: 20px 0 8px;
}

.controls-toggle p {
  margin-bottom: 10px;
  color: #aaa;
}

.ctrl-btn {
  font-family: 'Courier New', monospace;
  font-size: 0.85em;
  padding: 8px 16px;
  margin: 0 4px;
  background: #222;
  color: #888;
  border: 1px solid #444;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
}

.ctrl-btn:hover {
  border-color: #888;
  color: #ccc;
}

.ctrl-btn.active {
  background: #2a3a1a;
  color: #ffcc00;
  border-color: #ffcc00;
}

/* Menu buttons */
.menu-buttons {
  margin-top: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.menu-btn {
  font-family: 'Courier New', monospace;
  font-size: 1.1em;
  padding: 12px 40px;
  min-width: 220px;
  background: #222;
  color: #ffcc00;
  border: 2px solid #ffcc00;
  border-radius: 4px;
  cursor: pointer;
  letter-spacing: 2px;
  transition: all 0.15s;
}

.menu-btn:hover {
  background: #2a3a1a;
  color: #fff;
  border-color: #fff;
}

/* Options screen */
.option-group {
  margin: 20px 0;
  text-align: center;
}

.option-label {
  font-size: 0.95em;
  color: #aaa;
  margin-bottom: 10px;
  letter-spacing: 2px;
  text-transform: uppercase;
}

.option-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.volume-row {
  align-items: center;
  gap: 12px;
}
.volume-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 180px;
  height: 6px;
  background: #333;
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}
.volume-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ffcc00;
  border: 2px solid #111;
  cursor: pointer;
}
.volume-slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ffcc00;
  border: 2px solid #111;
  cursor: pointer;
}
.volume-pct {
  font-family: 'Courier New', monospace;
  font-size: 0.9em;
  color: #ffcc00;
  min-width: 40px;
  text-align: right;
}

.game-over h1 { color: #ff4444; }
.base-lost { color: #ff6622; font-size: 1.1em; letter-spacing: 1px; }
.level-complete h1 { color: #44ff44; }

/* How to Play screen */
.overlay-box.help {
  max-width: 560px;
}

#help-body {
  max-height: 58vh;
  overflow-y: auto;
  text-align: left;
  padding: 4px 14px 4px 10px;
  margin-top: 10px;
}

.help-section {
  margin-bottom: 18px;
}

.help-section .option-label {
  text-align: center;
  margin-bottom: 8px;
}

.help-section p {
  font-size: 0.9em;
  line-height: 1.45;
  margin-bottom: 6px;
  color: #ccc;
}

.help-section p b {
  color: #ffcc00;
}

.help-story {
  font-style: italic;
  color: #bbb !important;
}

.powerup-legend {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.pu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.9em;
  color: #ccc;
}

.pu-item b {
  color: #ffcc00;
}

.pu-dot {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.6);
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.25), inset 0 0 4px rgba(255, 255, 255, 0.3);
  flex-shrink: 0;
}
.victory h1 { color: #ffdd00; }

/* Orientation gate — shown only in portrait on touch devices */
#orientation-gate {
  position: fixed;
  inset: 0;
  z-index: 200; /* above overlay (100) and mobile-controls (60) */
  background: #0a0a0a;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffcc00;
  font-family: 'Courier New', monospace;
  text-align: center;
  padding: 24px;
}
#orientation-gate.hidden { display: none; }
.og-inner { max-width: 320px; }
.og-phone {
  width: 60px;
  height: 100px;
  margin: 0 auto 24px;
  border: 4px solid #ffcc00;
  border-radius: 10px;
  animation: og-rotate 2.4s ease-in-out infinite;
  position: relative;
}
.og-phone::before {
  content: '';
  position: absolute;
  inset: 6px 4px;
  background: rgba(255, 204, 0, 0.15);
  border-radius: 3px;
}
.og-title {
  font-size: 1.3em;
  letter-spacing: 3px;
  margin-bottom: 8px;
}
.og-subtitle {
  font-size: 0.9em;
  color: #ccc;
  letter-spacing: 1px;
}
@keyframes og-rotate {
  0%, 25%  { transform: rotate(0deg); }
  50%, 75% { transform: rotate(-90deg); }
  100%     { transform: rotate(0deg); }
}

/* Fullscreen toggle (touch + Fullscreen API only) */
#fullscreen-btn {
  position: fixed;
  top: max(10px, env(safe-area-inset-top, 10px));
  right: max(10px, env(safe-area-inset-right, 10px));
  width: 44px;
  height: 44px;
  border-radius: 6px;
  border: 2px solid rgba(255, 204, 0, 0.7);
  background: rgba(20, 20, 30, 0.7);
  color: #ffcc00;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  z-index: 150; /* above mobile controls, below orientation gate */
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  font-family: 'Courier New', monospace;
}
#fullscreen-btn.hidden { display: none; }
#fullscreen-btn:active { background: rgba(255, 204, 0, 0.25); }
