/* ============================================================
   MATH BOXING — Pixel art arcade styling
   Scene-image based rendering (uses real pixel art per state)
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: 'Press Start 2P', monospace;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  color: #fff;
}

html, body {
  /* Ensure the html/body don't bleed under the status bar on iOS */
  background: #000;
  height: 100%;
  overflow: hidden;
}

#app {
  width: 100vw;
  /* Stack of progressively safer height units for iOS Safari.
     svh = small viewport height, assumes URL bar is visible. */
  height: 100vh;
  height: 100dvh;
  height: 100svh;
  position: relative;
  overflow: hidden;
  background: #000;
}

/* ---- Screens ---- */
.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  /* Safe-area insets MUST be on the screen, not #app, because
     absolutely-positioned children ignore parent padding. */
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  box-sizing: border-box;
}
.screen.active { display: flex; }

/* ---- Title screen ---- */
#title-screen {
  align-items: center;
  justify-content: center;
  background: #0d1530;
  padding: 1rem;
  gap: 1.5rem;
  overflow: hidden;
}

#title-image {
  max-width: 100%;
  max-height: 70vh;
  width: auto;
  height: auto;
  object-fit: contain;
  image-rendering: pixelated;
  display: block;
}

#title-screen .big-btn {
  animation: start-pulse 1.5s ease-in-out infinite;
}

/* ---- Game Complete screen ---- */
#game-complete-screen {
  align-items: center;
  justify-content: center;
  background: #0d1530;
  padding: 1rem;
  gap: 1.5rem;
  overflow: hidden;
}

#game-complete-image {
  max-width: 100%;
  max-height: 70vh;
  width: auto;
  height: auto;
  object-fit: contain;
  image-rendering: pixelated;
  display: block;
  opacity: 0;
  transition: opacity 2.5s ease-in;
}

#game-complete-image.fade-in {
  opacity: 1;
}

#game-complete-screen .big-btn {
  /* Fade the START OVER button in alongside the image so it doesn't
     appear floating on a black screen before the art loads. */
  opacity: 0;
  animation: gc-btn-appear 0.6s ease-in 2s forwards, start-pulse 1.5s ease-in-out 2.6s infinite;
}

@keyframes gc-btn-appear {
  to { opacity: 1; }
}

@keyframes start-pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.05); }
}

@keyframes title-bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-6px); }
}

.big-btn {
  font-family: inherit;
  font-size: clamp(0.7rem, 2.5vw, 1rem);
  background: #ffd700;
  color: #000;
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000, inset -2px -2px 0 #c8a020, inset 2px 2px 0 #fff8a0;
  padding: 1rem 2rem;
  cursor: pointer;
  letter-spacing: 2px;
  transition: transform 0.05s;
  font-weight: bold;
}
.big-btn:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 #000, inset -2px -2px 0 #c8a020, inset 2px 2px 0 #fff8a0;
}
.big-btn.secondary {
  background: #2860e0;
  color: #fff;
  border: 3px solid #fff;
  box-shadow:
    inset 2px 2px 0 #5080f0,
    inset -2px -2px 0 #1a3080,
    3px 3px 0 #000;
  letter-spacing: 4px;
  text-shadow: 1px 1px 0 #1a3080;
}
.big-btn.secondary:active {
  transform: translate(2px, 2px);
  box-shadow:
    inset 2px 2px 0 #5080f0,
    inset -2px -2px 0 #1a3080,
    1px 1px 0 #000;
}

/* ---- Level select ---- */
#level-select-screen {
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at center, #142048 0%, #050818 90%);
  padding: 1rem;
  gap: 2rem;
}
.select-title {
  font-size: clamp(1rem, 5vw, 1.8rem);
  color: #ffd700;
  text-shadow: 2px 2px 0 #000;
  letter-spacing: 4px;
}
#level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.7rem;
  max-width: 95vw;
}
.level-btn {
  font-family: inherit;
  width: clamp(48px, 14vw, 84px);
  height: clamp(48px, 14vw, 84px);
  font-size: clamp(0.9rem, 3.5vw, 1.4rem);
  font-weight: bold;
  background: #2a2d35;
  color: #fff;
  border: 3px solid #000;
  /* Subtle 3D bevel - lighter top-left, darker bottom-right */
  box-shadow:
    inset 2px 2px 0 #4a4d57,
    inset -2px -2px 0 #14161c,
    3px 3px 0 #000;
  cursor: pointer;
  text-shadow: 1px 1px 0 #000;
}
.level-btn:not(.locked):not(.current):hover {
  background: #34373f;
}
.level-btn.locked {
  background: #1a1c22;
  color: #555;
  cursor: not-allowed;
  box-shadow:
    inset 2px 2px 0 #2a2d35,
    inset -2px -2px 0 #0a0c10,
    3px 3px 0 #000;
}
/* Highlight the next-to-play level (the unlocked one) in red */
.level-btn.current {
  background: #c92020;
  color: #fff;
  box-shadow:
    inset 2px 2px 0 #ff5050,
    inset -2px -2px 0 #6a0a0a,
    3px 3px 0 #000;
}
.level-btn:not(.locked):active {
  transform: translate(2px, 2px);
  box-shadow:
    inset 2px 2px 0 #4a4d57,
    inset -2px -2px 0 #14161c,
    1px 1px 0 #000;
}
.level-btn.current:active {
  box-shadow:
    inset 2px 2px 0 #ff5050,
    inset -2px -2px 0 #6a0a0a,
    1px 1px 0 #000;
}

/* ---- Match screen ---- */
#match-screen {
  background: #000;
  flex-direction: column;
}

/* HUD */
.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.3rem;
  padding: 0.3rem 0.5rem;
  background: #000;
  border-bottom: 3px solid #d72020;
  font-size: clamp(0.4rem, 1.6vw, 0.7rem);
  flex-shrink: 0;
}

.hud-side {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.hud-side.right { align-items: flex-end; }

.hud-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.hud-side.right .hud-row { flex-direction: row-reverse; }

.label-1p { color: #d72020; }
.label-2p { color: #2850c8; }
.score-val { color: #ffd700; letter-spacing: 1px; }

.health-bar {
  width: clamp(80px, 28vw, 180px);
  height: 12px;
  background: #d72020;
  border: 2px solid #000;
  position: relative;
  overflow: hidden;
}
.health-bar::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to right, #ffd700 0%, #ffd700 var(--health-pct, 100%), transparent var(--health-pct, 100%));
  transition: background 0.3s;
}
.hud-side.right .health-bar::before {
  background: linear-gradient(to left, #ffd700 0%, #ffd700 var(--health-pct, 100%), transparent var(--health-pct, 100%));
}

.fighter-info {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.hud-side.right .fighter-info { flex-direction: row-reverse; }

.fighter-name {
  color: #fff;
  font-size: clamp(0.5rem, 2vw, 0.8rem);
  letter-spacing: 1px;
}

.stars {
  display: flex;
  gap: 2px;
}
.star {
  font-size: clamp(0.7rem, 2.5vw, 1rem);
  text-shadow: 1px 1px 0 #000;
}
.star.lit { color: #ffd700; }
.star.unlit { color: #444; }

.hud-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
}
.time-box {
  background: #d72020;
  border: 3px solid #ffd700;
  padding: 0.3rem 0.8rem;
  text-align: center;
  min-width: 80px;
}
.time-label {
  color: #ffd700;
  font-size: clamp(0.5rem, 1.8vw, 0.75rem);
  letter-spacing: 2px;
}
.time-val {
  color: #fff;
  font-size: clamp(1.4rem, 5.5vw, 2rem);
  text-shadow: 2px 2px 0 #000;
  font-weight: bold;
  line-height: 1.1;
}

.q-time {
  font-size: clamp(0.6rem, 2.4vw, 0.95rem);
  color: #fff;
  background: #2850c8;
  padding: 4px 10px;
  border: 2px solid #000;
  transition: background 0.2s;
  font-weight: bold;
}
.q-time.urgent {
  background: #d72020;
  animation: urgent-pulse 0.5s infinite;
}
@keyframes urgent-pulse {
  0%, 100% { background: #d72020; }
  50%      { background: #ff8080; }
}

/* Arena = scene image container */
#arena {
  flex: 0 0 auto;
  position: relative;
  width: 100%;
  /* Aspect ratio matches our scene images (~1:1, slightly wider than tall) */
  aspect-ratio: 1.1 / 1;
  max-height: 38vh;
  overflow: hidden;
  background: #000;
}

#scene-image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* Shift image down so we crop more of the source's top headroom padding,
     revealing more of the characters' legs and the paw circle below.
     Values 0-100%: 0%=top of image at top, 100%=bottom of image at bottom.
     ~75% biases toward showing legs while keeping heads in frame. */
  object-position: center 75%;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  display: block;
}
/* Hide broken-image icon when src is empty (before first level starts) */
#scene-image:not([src]),
#scene-image[src=""] {
  visibility: hidden;
}

#level-banner, #sudden-death-banner {
  position: absolute;
  top: 20%;
  left: 50%;
  transform: translateX(-50%) scale(0);
  font-size: clamp(1rem, 5vw, 2rem);
  color: #ffd700;
  text-shadow: 3px 3px 0 #d72020, 6px 6px 0 #000;
  z-index: 10;
  pointer-events: none;
  letter-spacing: 3px;
  transition: transform 0.3s ease-out;
}
#level-banner.show, #sudden-death-banner.show {
  transform: translateX(-50%) scale(1);
  animation: banner-pulse 0.4s ease-in-out 3;
}
#sudden-death-banner { color: #ff4040; }

@keyframes banner-pulse {
  0%, 100% { transform: translateX(-50%) scale(1); }
  50%      { transform: translateX(-50%) scale(1.15); }
}

.turn-indicator {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(0.5rem, 2vw, 0.8rem);
  padding: 4px 10px;
  border: 2px solid #000;
  z-index: 5;
  letter-spacing: 2px;
}
.turn-indicator.attack {
  background: #d72020;
  color: #fff;
}
.turn-indicator.defend {
  background: #2850c8;
  color: #fff;
}
.turn-indicator.sudden-death {
  background: #ff4040;
  color: #ffd700;
  animation: pulse-bg 0.5s infinite;
}
@keyframes pulse-bg {
  0%, 100% { background: #ff4040; }
  50%      { background: #ffd700; color: #ff4040; }
}

#streak-indicator {
  position: absolute;
  top: 40px;
  left: 50%;
  transform: translateX(-50%) scale(0);
  background: #ffd700;
  color: #d72020;
  padding: 4px 10px;
  border: 2px solid #000;
  font-size: clamp(0.5rem, 2vw, 0.8rem);
  z-index: 5;
  letter-spacing: 1px;
  transition: transform 0.2s;
}
#streak-indicator.show {
  transform: translateX(-50%) scale(1);
}

/* Scene flash overlay - subtle yellow/blue tint on action */
.scene-flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  animation: scene-flash 0.35s ease-out forwards;
}
.scene-flash.hit {
  background: radial-gradient(ellipse at center, rgba(255, 215, 0, 0.4) 0%, transparent 60%);
}
.scene-flash.block {
  background: radial-gradient(ellipse at center, rgba(120, 180, 255, 0.35) 0%, transparent 60%);
}
@keyframes scene-flash {
  0%   { opacity: 1; }
  100% { opacity: 0; }
}

.shake { animation: shake 0.25s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%      { transform: translateX(-6px); }
  40%      { transform: translateX(6px); }
  60%      { transform: translateX(-4px); }
  80%      { transform: translateX(4px); }
}

.floating-points {
  position: absolute;
  font-size: clamp(0.6rem, 2.4vw, 1rem);
  z-index: 7;
  pointer-events: none;
  animation: float-up 1.2s ease-out forwards;
  text-shadow: 2px 2px 0 #000;
  letter-spacing: 1px;
  white-space: nowrap;
}
.floating-points.player {
  color: #ffd700;
  left: 18%;
  top: 18%;
}
.floating-points.enemy {
  color: #ff6060;
  right: 18%;
  top: 18%;
}
@keyframes float-up {
  0%   { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(-50px); opacity: 0; }
}

/* Question + Pad area */
.play-area {
  flex: 1;
  background: #000;
  border-top: 3px solid #d72020;
  padding: 0.3rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  min-height: 0;
}

#question-display {
  flex: 1;
  background: #1a1a2a;
  border: 3px solid #ffd700;
  padding: 0.4rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  /* Match answer-display font size so vertical subtraction columns align cleanly */
  font-size: clamp(1.1rem, 4.5vw, 1.8rem);
  min-height: 50px;
  position: relative;
  letter-spacing: 3px;
}

/* Invisible spacer that mirrors the QUIT button's footprint, so the
   question-display ends up the same width as the answer-display below it. */
.row-spacer {
  min-width: 60px;
  flex-shrink: 0;
  visibility: hidden;
  /* Match button border so total width is identical */
  border: 3px solid transparent;
  padding: 0 0.8rem;
}

.vert-stack {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
.vert-line {
  display: flex;
  font-family: 'Press Start 2P', monospace;
}
.vert-line span {
  display: inline-block;
  min-width: 0.7em;
  text-align: center;
  color: #fff;
}

.horiz-eq {
  color: #fff;
  display: flex;
  gap: 0.4em;
  align-items: center;
}
.horiz-eq .op,
.horiz-eq .eq { color: #ffd700; }

.timeout-flash {
  animation: flash-red 0.4s;
}
@keyframes flash-red {
  0%, 100% { border-color: #ffd700; }
  50%      { border-color: #ff4040; background: #4a1010; }
}

.answer-row {
  display: flex;
  align-items: stretch;
  gap: 0.4rem;
  flex-shrink: 0;
}

#answer-display {
  flex: 1;
  background: #ffd700;
  color: #d72020;
  border: 3px solid #000;
  text-align: center;
  /* Identical font-size to #question-display so digits align vertically */
  font-size: clamp(1.1rem, 4.5vw, 1.8rem);
  padding: 0.3rem;
  letter-spacing: 3px;
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Use monospace to match question's vertical-stack column widths */
  font-family: 'Press Start 2P', monospace;
}

#quit-match-btn {
  background: #d72020;
  color: #fff;
  border: 3px solid #000;
  font-family: inherit;
  font-size: clamp(0.55rem, 2vw, 0.8rem);
  font-weight: bold;
  padding: 0 0.8rem;
  cursor: pointer;
  letter-spacing: 1px;
  box-shadow: inset 2px 2px 0 #ff6060, inset -2px -2px 0 #8a0a0a;
  min-width: 60px;
  flex-shrink: 0;
}
#quit-match-btn:active {
  transform: translate(1px, 1px);
}

#number-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.25rem;
  flex: 1;
  min-height: 0;
}

.pad-btn {
  font-family: inherit;
  font-size: clamp(0.85rem, 3.8vw, 1.3rem);
  background: #2850c8;
  color: #fff;
  border: 3px solid #000;
  box-shadow: 3px 3px 0 #000, inset -2px -2px 0 #1a3080, inset 2px 2px 0 #6080e0;
  padding: 0.15rem 0;
  cursor: pointer;
  min-height: 28px;
}
.pad-btn:active {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 #000, inset -2px -2px 0 #1a3080, inset 2px 2px 0 #6080e0;
}
.pad-btn.submit {
  background: #20a020;
  box-shadow: 3px 3px 0 #000, inset -2px -2px 0 #105010, inset 2px 2px 0 #60d060;
}
.pad-btn.back {
  background: #d72020;
  box-shadow: 3px 3px 0 #000, inset -2px -2px 0 #8a0a0a, inset 2px 2px 0 #ff6060;
}

/* Result screen */
#result-screen {
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, #1a2050 0%, #000 80%);
  padding: 1rem;
  gap: 1.5rem;
}
.result-title {
  font-size: clamp(1.5rem, 7vw, 3rem);
  letter-spacing: 3px;
  text-shadow: 3px 3px 0 #000;
  text-align: center;
}
.result-title.win {
  color: #ffd700;
  animation: title-bob 1s ease-in-out infinite;
}
.result-title.lose {
  color: #d72020;
}
#result-summary {
  font-size: clamp(0.7rem, 2.5vw, 1rem);
  text-align: center;
  line-height: 2;
  color: #fff;
}
#result-actions {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

/* Tablet+ tweaks: cap arena width so big screens don't blow up the scene */
@media (min-width: 768px) {
  #arena { max-width: 600px; margin: 0 auto; max-height: 40vh; }
  .play-area { max-width: 600px; margin: 0 auto; width: 100%; }
  .hud { max-width: 600px; margin: 0 auto; width: 100%; }
}
