* { box-sizing: border-box; }

:root {
  --p1: #e84c3c;
  --p1-dark: #8e1f16;
  --p2: #3a8ee0;
  --p2-dark: #17436e;
  --ink: #f4e9d8;
  --gold: #ffdf6b;
  --panel: #1a1024;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: #0b0713;
  color: var(--ink);
  font-family: "Press Start 2P", "Courier New", monospace;
  overflow: hidden;
  overscroll-behavior: none;
}

#app {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* The stage spans the full browser width; JS sets its height from the
   canvas's live resolution, so the picture never stretches. --s is the
   pixel scale (1 = the original 384x216 look) and drives every HUD size;
   --ctl is the diameter of one round control. */
#stage {
  --s: 1;
  --ctl: 64px;
  position: relative;
  width: 100vw;
  height: 216px;
  overflow: hidden;
  background: #000;
}

canvas#game {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
}

#crt {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background:
    repeating-linear-gradient(
      to bottom,
      rgba(0,0,0,0.18) 0px,
      rgba(0,0,0,0.18) 1px,
      rgba(0,0,0,0) 2px,
      rgba(0,0,0,0) 3px
    );
  mix-blend-mode: multiply;
  box-shadow: inset 0 0 60px rgba(0,0,0,0.65);
}

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: calc(6px * var(--s));
  left: calc(8px * var(--s));
  right: calc(8px * var(--s));
  z-index: 4;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  pointer-events: none;
  font-size: calc(8px * var(--s));
  text-shadow: calc(2px * var(--s)) calc(2px * var(--s)) 0 #000;
}

.bar-wrap { width: 40%; max-width: calc(150px * var(--s)); }
.bar-wrap.right { text-align: right; }

.fighter-name { margin-bottom: calc(3px * var(--s)); letter-spacing: 1px; }

.bar-frame {
  height: calc(12px * var(--s));
  background: #000;
  border: calc(2px * var(--s)) solid var(--ink);
  padding: 1px;
  image-rendering: pixelated;
}
.bar-wrap.right .bar-frame { direction: rtl; }

.bar-fill {
  height: 100%;
  transition: width 0.15s steps(10);
}
.bar-fill.p1 { background: linear-gradient(var(--gold), var(--p1)); width: 100%; }
.bar-fill.p2 { background: linear-gradient(var(--gold), var(--p2)); width: 100%; }

.pips { margin-top: calc(4px * var(--s)); display: flex; gap: calc(4px * var(--s)); }
.bar-wrap.right .pips { justify-content: flex-end; }
.pip {
  width: calc(8px * var(--s));
  height: calc(8px * var(--s));
  border: 1px solid var(--ink);
  background: transparent;
}
.pip.won.p1 { background: var(--p1); }
.pip.won.p2 { background: var(--p2); }

.clock {
  background: #000;
  border: calc(2px * var(--s)) solid var(--ink);
  padding: calc(4px * var(--s)) calc(8px * var(--s));
  font-size: calc(12px * var(--s));
  font-variant-numeric: tabular-nums;
}

#banner {
  position: absolute;
  top: 34%;
  left: 0;
  right: 0;
  z-index: 5;
  text-align: center;
  font-size: calc(16px * var(--s));
  color: #fff;
  text-shadow: calc(3px * var(--s)) calc(3px * var(--s)) 0 #000, 0 0 12px rgba(255,255,255,0.5);
  pointer-events: none;
  opacity: 0;
}
#banner.show { animation: bannerPop 1.4s ease-out; }
@keyframes bannerPop {
  0%   { opacity: 0; transform: scale(0.6); }
  15%  { opacity: 1; transform: scale(1.08); }
  25%  { opacity: 1; transform: scale(1); }
  80%  { opacity: 1; }
  100% { opacity: 0; }
}

/* ---------- On-screen controls (inside the stage) ---------- */
#pad {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
}
#stick, #actions {
  position: absolute;
  bottom: calc(var(--ctl) * 0.3);
  pointer-events: auto;
}
#stick  { left: calc(var(--ctl) * 0.36); }
#actions { right: calc(var(--ctl) * 0.36); }

/* Joystick: drag the knob out of the ring to walk, push up to jump. */
#stick {
  width: calc(var(--ctl) * 1.6);
  height: calc(var(--ctl) * 1.6);
  border-radius: 50%;
  border: 3px solid rgba(244, 233, 216, 0.5);
  background: radial-gradient(circle, rgba(26,16,36,0.6) 0%, rgba(26,16,36,0.25) 70%);
  display: grid;
  place-items: center;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
#stick.active { border-color: var(--gold); }

#knob {
  width: 46%;
  height: 46%;
  border-radius: 50%;
  background: rgba(244, 233, 216, 0.8);
  border: 3px solid var(--panel);
  pointer-events: none;
  transition: transform 60ms linear;
}
#stick.active #knob { background: var(--gold); transition: none; }

/* Attack + guard: round icon buttons, thumb cluster bottom-right. */
#actions {
  display: grid;
  grid-template-columns: repeat(2, var(--ctl));
  grid-template-rows: repeat(2, var(--ctl));
  gap: calc(var(--ctl) * 0.2);
  grid-template-areas:
    ".     block"
    "punch kick";
}
.rbtn[data-slot="block"] { grid-area: block; }
.rbtn[data-slot="punch"] { grid-area: punch; }
.rbtn[data-slot="kick"]  { grid-area: kick; }

.rbtn {
  width: var(--ctl);
  height: var(--ctl);
  padding: 0;
  border-radius: 50%;
  border: 3px solid rgba(244, 233, 216, 0.8);
  display: grid;
  place-items: center;
  color: #fff;
  background: rgba(26, 16, 36, 0.6);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.5);
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
.rbtn svg { width: 54%; height: 54%; display: block; }
.rbtn[data-slot="block"] { background: rgba(58, 142, 224, 0.72); }
.rbtn[data-slot="punch"] { background: rgba(232, 76, 60, 0.82); }
.rbtn[data-slot="kick"]  { background: rgba(255, 223, 107, 0.85); color: #3a1d08; border-color: #fff6ea; }
.rbtn.down {
  transform: translateY(3px) scale(0.94);
  box-shadow: 0 0 0 rgba(0, 0, 0, 0.5);
  filter: brightness(1.35);
}
.rbtn:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

/* ---------- Overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  background: rgba(6, 4, 12, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: calc(10px * var(--s));
  text-align: center;
  padding: 4%;
}
.overlay.hidden { display: none; }

#startScreen h1, #endScreen h1 {
  font-size: calc(22px * var(--s));
  color: var(--gold);
  text-shadow: calc(3px * var(--s)) calc(3px * var(--s)) 0 var(--p1-dark), 0 0 20px rgba(255,223,107,0.4);
  margin: 0;
  letter-spacing: 2px;
  text-wrap: balance;
}

.subtitle { font-size: calc(9px * var(--s)); color: var(--ink); opacity: 0.85; margin: 0; }

.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: calc(6px * var(--s)) calc(20px * var(--s));
  font-size: calc(7px * var(--s));
  margin: calc(6px * var(--s)) 0;
  color: var(--ink);
}
.controls b { color: var(--gold); }

.pixel-btn {
  font-family: inherit;
  font-size: calc(11px * var(--s));
  padding: calc(10px * var(--s)) calc(18px * var(--s));
  background: var(--p1);
  color: #fff;
  border: calc(3px * var(--s)) solid #fff;
  box-shadow: calc(4px * var(--s)) calc(4px * var(--s)) 0 #000;
  cursor: pointer;
  letter-spacing: 1px;
}
.pixel-btn:active {
  transform: translate(calc(3px * var(--s)), calc(3px * var(--s)));
  box-shadow: calc(1px * var(--s)) calc(1px * var(--s)) 0 #000;
}
.pixel-btn:focus-visible { outline: calc(3px * var(--s)) solid var(--gold); outline-offset: calc(3px * var(--s)); }

.hint { font-size: calc(6px * var(--s)); opacity: 0.7; max-width: 80%; line-height: 1.8; margin: 0; }

@media (prefers-reduced-motion: reduce) {
  .bar-fill { transition: none; }
  #knob { transition: none; }
  .rbtn.down { transform: none; }
}
