* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: #14181f;
  color: #fff;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  overscroll-behavior: none;
  touch-action: none;
}

#stage {
  position: relative;
  /* Sized by whichever dimension is tighter. On a portrait phone width is
     the constraint (same as before); on a landscape phone/tablet — where
     viewport height can be quite short — the calc(88vh * 4/3) term takes
     over so the 4:3 stage still fits without spilling off the bottom.
     container-type lets every HUD element below scale in cqw units, i.e.
     as a percentage of however wide the stage actually renders, so a phone
     rendering the stage at 340px and a desktop rendering it at 800px both
     get correctly-proportioned (not comically oversized or microscopic) UI. */
  width: min(94vw, calc(88vh * 4 / 3), 800px);
  aspect-ratio: 4 / 3;
  container-type: inline-size;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  border-radius: 12px;
  overflow: hidden;
  background: #000;
  touch-action: none;
}

/* Mobile / touch: fill the entire screen edge-to-edge instead of shrinking
   to fit inside it — the stage is sized to fully COVER the viewport while
   still keeping its internal 4:3 layout (so every canvas draw call and every
   cqw-based HUD size stays correct — only the container's own box is bigger
   than the screen now), and the viewport itself crops whatever spills over.
   That crop lands on the side panels (Hit Map / Swing Read) on very narrow
   portrait phones, trading them away so the batting view itself reads big
   and clear with no letterboxing, which is the point on a phone screen. */
@media (max-width: 768px), (pointer: coarse) {
  #stage {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: max(100vw, 133.334dvh);
    max-width: none;
    border-radius: 0;
    box-shadow: none;
  }

  /* Everything centered (banners, overlays, the pitchInfo/distance pills,
     the continue prompt) stays correctly on-screen through the crop above
     for free, because the stage's horizontal/vertical CENTER always lines
     up with the viewport's center. But #hudTop and #instructions are
     anchored to the *stage's* edges, not centered — and those edges now
     sit off past the visible viewport, so their content would silently
     render out of view. Re-anchor them to the real viewport edges instead
     (the crop is symmetric, so "half the overflow" is the same offset on
     both left/right or top/bottom). */
  #hudTop {
    top: calc((100% - 100dvh) / 2 + 10px);
    left: calc((100% - 100vw) / 2 + 10px);
    right: calc((100% - 100vw) / 2 + 10px);
    gap: 10px;
  }

  #instructions {
    bottom: calc((100% - 100dvh) / 2 + 10px);
  }
}

canvas#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: none;
  touch-action: none;
}

#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#hudTop {
  position: absolute;
  top: 1.5cqw;
  left: 1.5cqw;
  right: 1.5cqw;
  display: flex;
  gap: 2.5cqw;
  align-items: center;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  font-size: clamp(11px, 2cqw, 16px);
}

.stat .label {
  font-size: clamp(7px, 1.25cqw, 10px);
  text-transform: uppercase;
  opacity: 0.75;
  font-weight: 600;
  letter-spacing: 0.06em;
}

#pitchProgress {
  margin-left: auto;
  font-size: clamp(9px, 1.75cqw, 14px);
  align-self: center;
}

#pitchInfo {
  position: absolute;
  top: 7.25cqw;
  left: 50%;
  transform: translate(-50%, -8px);
  background: rgba(0, 0, 0, 0.45);
  padding: 0.375cqw 1.5cqw;
  border-radius: 999px;
  font-weight: 700;
  font-size: clamp(8px, 1.375cqw, 11px);
  letter-spacing: 0.04em;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  white-space: nowrap;
}

#pitchInfo.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

#distanceWrap {
  position: absolute;
  top: 10.75cqw;
  left: 50%;
  transform: translate(-50%, -8px);
  background: rgba(0, 0, 0, 0.45);
  padding: 0.5cqw 1.75cqw;
  border-radius: 999px;
  font-weight: 700;
  font-size: clamp(10px, 1.75cqw, 14px);
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  white-space: nowrap;
}

#distanceWrap.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.banner {
  position: absolute;
  top: 38%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.6);
  font-size: clamp(22px, 5.25cqw, 42px);
  font-weight: 800;
  letter-spacing: 0.02em;
  opacity: 0;
  transition: opacity 0.18s ease-out, transform 0.18s ease-out;
  text-shadow: 0 3px 10px rgba(0, 0, 0, 0.6);
  white-space: nowrap;
}

.banner.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.banner.home_run {
  color: #ffd23f;
  font-size: clamp(28px, 6.75cqw, 54px);
}

.banner.double, .banner.single {
  color: #8ef58e;
}

.banner.groundout, .banner.foul {
  color: #f5d98e;
}

.banner.miss {
  color: #ff6b6b;
}

#continuePrompt {
  position: absolute;
  top: 80%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.5);
  padding: 0.6cqw 2.5cqw;
  border-radius: 999px;
  font-weight: 700;
  font-size: clamp(11px, 2cqw, 15px);
  opacity: 0;
  transition: opacity 0.3s;
  white-space: nowrap;
  animation: continuePulse 1.1s ease-in-out infinite;
}

#continuePrompt.visible {
  opacity: 1;
}

@keyframes continuePulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.06); }
}

#instructions {
  position: absolute;
  bottom: 1.25cqw;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(8px, 1.5cqw, 12px);
  opacity: 0.65;
  white-space: nowrap;
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 14, 20, 0.86);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
  padding: 20px;
  overflow-y: auto;
}

.overlay.hidden {
  display: none;
}

.overlay h1 {
  margin: 0 0 4px;
  font-size: clamp(20px, 4cqw, 32px);
}

.overlay p {
  margin: 0;
  opacity: 0.85;
  max-width: min(380px, 88cqw);
  font-size: clamp(12px, 1.75cqw, 15px);
}

.bestScoreLine {
  font-weight: 700;
  opacity: 1 !important;
  color: #ffd23f;
}

.newBestBadge {
  font-weight: 800;
  opacity: 1 !important;
  color: #ffd23f;
  font-size: clamp(14px, 2.3cqw, 19px) !important;
  animation: continuePulse 1s ease-in-out infinite;
}

.newBestBadge.hidden {
  display: none;
}

.overlay button {
  margin-top: 12px;
  padding: 10px 30px;
  font-size: clamp(13px, 2cqw, 16px);
  font-weight: 700;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  background: #ffd23f;
  color: #1a1a1a;
  transition: transform 0.1s;
  min-height: 40px;
  min-width: 100px;
}

.overlay button:hover {
  transform: scale(1.05);
}

.langSelect {
  margin-top: 14px;
  padding: 6px 10px;
  font-size: clamp(11px, 1.625cqw, 13px);
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  min-height: 32px;
}

.langSelect option {
  color: #1a1a1a;
}
