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

html, body {
  min-height: 100%;
  background: #000008;
  color: #fff;
  font-family: "Press Start 2P", "Courier New", monospace;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#game-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  max-width: 100vw;
}

/* Desktop: up to 2× logical size, but never larger than the viewport */
#hud {
  width: min(100vw - 16px, var(--board-w, 896px), calc((100vh - 140px) * var(--aspect-w, 448) / var(--aspect-h, 576)));
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.hud-block { text-align: center; min-width: 90px; }
.hud-block .label {
  color: #f44;
  font-size: 9px;
  margin-bottom: 6px;
  letter-spacing: 1px;
}
#score { color: #fff; font-size: 14px; }
#high-score { color: #fff; font-size: 14px; }
#stage { color: #0ff; font-size: 14px; }

#canvas-wrap {
  position: relative;
  line-height: 0;
  border: 3px solid #334;
  box-shadow: 0 0 28px rgba(80, 100, 255, 0.28);
  width: min(100vw - 16px, var(--board-w, 896px), calc((100vh - 140px) * var(--aspect-w, 448) / var(--aspect-h, 576)));
  background: #000;
  flex-shrink: 0;
}

#game {
  display: block;
  width: 100%;
  height: auto;
  max-height: calc(100vh - 140px);
  background: #000;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 12, 0.78);
  text-align: center;
  pointer-events: none;
  gap: 12px;
  padding: 16px;
}
#overlay.hidden { display: none; }

#overlay-title {
  color: #ffd700;
  font-size: clamp(20px, 4vw, 36px);
  letter-spacing: 4px;
  text-shadow: 0 0 12px #f80, 2px 0 0 #f04, -2px 0 0 #08f;
}
#overlay-sub {
  color: #0ff;
  font-size: clamp(10px, 1.6vw, 13px);
  animation: blink 0.85s steps(1) infinite;
}
#overlay-hint { color: #aaf; font-size: clamp(7px, 1.2vw, 9px); line-height: 1.6; }
#overlay-controls {
  color: #8cf;
  font-size: clamp(6px, 1vw, 8px);
  line-height: 1.9;
  margin-top: 10px;
  max-width: 92%;
}

#footer {
  width: min(100vw - 16px, var(--board-w, 896px), calc((100vh - 140px) * var(--aspect-w, 448) / var(--aspect-h, 576)));
  display: flex;
  justify-content: space-between;
  min-height: 22px;
  font-size: 11px;
  color: #fc6;
}
#lives { color: #ff8; letter-spacing: 2px; }
#stage-icons { color: #8f8; font-size: 10px; }

#mobile-controls {
  display: none;
  width: min(100vw - 12px, var(--board-w, 896px));
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  touch-action: none;
  padding: 4px 0 8px;
}
.mobile-col { display: flex; flex-direction: column; gap: 8px; }
.mobile-col.mid { flex: 1; align-items: center; }
.mobile-col.side { gap: 6px; }

.mobile-btn {
  min-width: 64px;
  height: 58px;
  border: 2px solid #668;
  border-radius: 12px;
  background: #12121a;
  color: #eef;
  font-family: inherit;
  font-size: 16px;
  cursor: pointer;
  touch-action: none;
}
.mobile-btn.fire {
  border-color: #fa0;
  color: #fc6;
  min-width: 120px;
  height: 64px;
  font-size: 14px;
}
.mobile-btn:active {
  background: #446;
  transform: scale(0.97);
}
.mobile-btn.fire:active { background: #fa0; color: #000; }

@keyframes blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

/* Phone / touch: fill width, leave room for big touch pads, keep aspect */
@media (max-width: 900px), (pointer: coarse) {
  html, body {
    overflow: hidden;
    height: 100%;
    height: 100dvh;
    touch-action: none;
  }
  #game-wrapper {
    max-height: 100dvh;
    gap: 4px;
    padding: 4px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
  }
  #mobile-controls { display: flex; }
  #canvas-wrap {
    width: min(
      100vw - 8px,
      calc((100dvh - 200px) * var(--aspect-w, 448) / var(--aspect-h, 576))
    );
  }
  #game { max-height: calc(100dvh - 200px); }
  #hud, #footer, #mobile-controls {
    width: min(
      100vw - 8px,
      calc((100dvh - 200px) * var(--aspect-w, 448) / var(--aspect-h, 576))
    );
  }
  .hud-block .label { font-size: 7px; }
  #score, #high-score, #stage { font-size: 11px; }
  #overlay-title { font-size: 18px; }
  #overlay-controls, #overlay-hint { font-size: 6px; }
  .mobile-btn {
    min-width: 70px;
    height: 62px;
    font-size: 18px;
  }
  .mobile-btn.fire {
    min-width: min(140px, 36vw);
    height: 68px;
    font-size: 15px;
  }
}
