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

html, body {
  min-height: 100%;
  background: #000;
  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-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: contain;
}

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

#hud {
  width: var(--board-w, 672px);
  max-width: min(100vw - 16px, var(--board-w, 672px));
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 2px 8px 0;
  flex-shrink: 0;
  background: #000;
}

.hud-block {
  text-align: center;
  min-width: 72px;
}

.hud-block .label {
  color: #ff2020;
  font-size: 10px;
  margin-bottom: 6px;
  letter-spacing: 1px;
}

#score, #high-score, #level {
  color: #ffff40;
  font-size: 13px;
  min-height: 16px;
}

#canvas-wrap {
  position: relative;
  line-height: 0;
  border: 0;
  flex-shrink: 0;
  width: min(100vw - 16px, var(--board-w, 672px));
  background: #000;
}

#game {
  display: block;
  background: #000;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  width: 100%;
  height: auto;
  touch-action: none;
}

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

#overlay.hidden {
  display: none;
}

#overlay-title {
  color: #ffb028;
  font-size: 26px;
  letter-spacing: 4px;
  text-shadow: 3px 3px 0 #d04018;
}

#overlay-sub {
  color: #ff3030;
  font-size: 11px;
  animation: blink 0.8s steps(1) infinite;
}

#overlay-hint {
  color: #fff;
  font-size: 8px;
  line-height: 1.6;
  margin-top: 6px;
}

#overlay-controls {
  color: #58c0ff;
  font-size: 7px;
  line-height: 1.9;
  margin-top: 14px;
  opacity: 0.95;
}

#overlay.ready #overlay-title {
  color: #ffff40;
  font-size: 16px;
  text-shadow: none;
}

#overlay.ready #overlay-sub,
#overlay.ready #overlay-hint,
#overlay.ready #overlay-controls {
  display: none;
}

#overlay.paused #overlay-title {
  color: #fff;
  font-size: 16px;
  text-shadow: none;
}

#overlay.gameover #overlay-title {
  color: #ff3030;
  font-size: 16px;
  text-shadow: none;
}

#footer {
  width: var(--board-w, 672px);
  max-width: min(100vw - 16px, var(--board-w, 672px));
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 28px;
  padding: 2px 8px 0;
  flex-shrink: 0;
  background: #000;
}

#lives {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  min-height: 24px;
}

.life-icon {
  width: 18px;
  height: 20px;
  background: #fcfcfc;
  position: relative;
  border-radius: 9px 9px 4px 4px;
  box-shadow: inset 0 -8px 0 #2038f0;
}

.life-icon::before {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #101010;
  box-shadow: 6px 0 0 #101010;
}

#veg-tray {
  font-size: 0;
  min-height: 22px;
  display: flex;
  gap: 4px;
  align-items: center;
}

#mobile-controls {
  display: none;
  width: min(100vw - 16px, var(--board-w, 672px));
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 2px 2px;
  flex-shrink: 0;
  touch-action: none;
}

.mobile-side {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mobile-btn {
  width: 52px;
  height: 52px;
  border: 2px solid #2038f0;
  border-radius: 4px;
  background: #101018;
  color: #ffff40;
  font-family: inherit;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.mobile-btn.pump-btn {
  width: 60px;
  height: 60px;
  border-color: #ff3030;
  color: #ff3030;
  font-size: 18px;
  font-weight: bold;
}

.mobile-btn:active,
.mobile-btn.active {
  background: #2038f0;
  color: #fff;
  transform: scale(0.96);
}

.mobile-btn.pump-btn:active,
.mobile-btn.pump-btn.active {
  background: #ff3030;
  color: #fff;
}

#dpad {
  display: grid;
  grid-template-columns: 52px 52px 52px;
  grid-template-rows: 52px 52px 52px;
  gap: 4px;
  touch-action: none;
}

.dpad-btn {
  width: 52px;
  height: 52px;
  border: 2px solid #2038f0;
  border-radius: 4px;
  background: #101018;
  color: #ffff40;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.dpad-btn[data-dir="U"] { grid-column: 2; grid-row: 1; }
.dpad-btn[data-dir="L"] { grid-column: 1; grid-row: 2; }
.dpad-btn[data-dir="R"] { grid-column: 3; grid-row: 2; }
.dpad-btn[data-dir="D"] { grid-column: 2; grid-row: 3; }

.dpad-center {
  grid-column: 2;
  grid-row: 2;
  border-color: #222;
  background: #080808;
  pointer-events: none;
  opacity: 0.4;
}

.dpad-btn:not(:disabled):active,
.dpad-btn.active {
  background: #2038f0;
  color: #fff;
  transform: scale(0.96);
}

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

@media (max-width: 820px), (pointer: coarse) {
  html, body {
    overflow: hidden;
    height: 100%;
    touch-action: none;
  }

  #game-wrapper {
    max-height: 100dvh;
  }

  #mobile-controls {
    display: flex;
  }

  #canvas-wrap {
    flex-shrink: 1;
    width: min(
      100vw - 16px,
      var(--board-w, 672px),
      calc((100dvh - 220px) * 672 / 864)
    );
  }

  #game {
    width: 100%;
    max-height: calc(100dvh - 220px);
  }

  #hud, #footer, #mobile-controls {
    width: min(100vw - 16px, var(--board-w, 672px));
  }

  .hud-block .label { font-size: 7px; }
  #score, #high-score, #level { font-size: 11px; }
  #overlay-title { font-size: 18px; }
  #overlay-controls, #overlay-hint { font-size: 6px; }
}

@media (max-height: 700px) and (max-width: 820px),
       (max-height: 700px) and (pointer: coarse) {
  #canvas-wrap {
    width: min(
      100vw - 16px,
      calc((100dvh - 180px) * 672 / 864)
    );
  }
  #game {
    max-height: calc(100dvh - 180px);
  }
  #dpad {
    grid-template-columns: 46px 46px 46px;
    grid-template-rows: 46px 46px 46px;
  }
  .dpad-btn { width: 46px; height: 46px; font-size: 14px; }
  .mobile-btn { width: 44px; height: 44px; font-size: 12px; }
  .mobile-btn.pump-btn { width: 52px; height: 52px; }
}

/* === Dual orientation (portrait + landscape) === */
@media (orientation: portrait) and (max-width: 900px) {
  html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }
  #game-wrapper {
    max-height: 100dvh;
    overflow: hidden;
    justify-content: flex-start;
    gap: 4px;
    padding: 4px;
    padding-bottom: max(4px, env(safe-area-inset-bottom));
  }
  #canvas-wrap, #hud, #footer, #mobile-controls {
    width: min(100vw - 8px, calc((100dvh - 210px) * 0.9)) !important;
    max-width: 100vw;
  }
  #game {
    width: 100% !important;
    height: auto !important;
    max-height: calc(100dvh - 210px) !important;
  }
  #mobile-controls {
    display: flex !important;
    flex-wrap: wrap;
    justify-content: center;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }
  #game-wrapper {
    max-height: 100dvh;
    overflow: hidden;
    gap: 2px;
    padding: 2px 4px;
    padding-left: max(4px, env(safe-area-inset-left));
    padding-right: max(4px, env(safe-area-inset-right));
  }
  #canvas-wrap {
    width: min(70vw, calc((100dvh - 12px) * 1.2), 100vw - 140px) !important;
  }
  #game {
    max-height: calc(100dvh - 12px) !important;
  }
  #hud, #footer {
    font-size: 10px;
  }
  #mobile-controls {
    display: flex !important;
  }
  .mobile-btn, .dpad-btn {
    min-width: 42px !important;
    width: 42px !important;
    height: 42px !important;
    font-size: 12px !important;
  }
}
