:root {
  --bg: #0b1026;
  --panel: #151c3d;
  --line: rgba(255, 255, 255, 0.16);
  --text: #f1f5ff;
  --muted: #9aa6d1;
  --accent: #a5cf3f;
  --warm: #facc15;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --app-w: 100vw;
  --app-h: 100vh;
  font-family: ui-rounded, 'Nunito', 'Segoe UI', system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.hidden { display: none !important; }

#app { position: fixed; inset: 0; overflow: hidden; }
/* An upright phone: the game is drawn sideways, ready to be held in landscape.
   The notch and home bar move with it. */
body.rotated #app {
  inset: auto;
  left: 0;
  top: 0;
  width: var(--app-w);
  height: var(--app-h);
  transform-origin: 0 0;
  transform: translateX(var(--app-h)) rotate(90deg);
  --safe-l: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-left, 0px);
}
.muted { color: var(--muted); }

canvas#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 3;
  display: flex;
  overflow: auto;
  padding: 12px calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
  background: rgba(11, 16, 38, 0.82);
}
/* margin: auto centres, and still scrolls from the top on very short screens */
.overlay > .panel, .overlay > .menu-card { margin: auto; }
.overlay.top { z-index: 4; }

#menu { background: radial-gradient(circle at 50% 0%, #24306a, var(--bg) 70%); }

.menu-card, .panel { display: flex; flex-direction: column; align-items: center; text-align: center; gap: clamp(6px, 2vmin, 14px); }
.menu-card h1 { margin: 0; font-size: clamp(34px, 12vmin, 72px); line-height: 1; }
.menu-card p, .panel p { margin: 0; }
.logo { font-size: clamp(30px, 11vmin, 68px); line-height: 1.1; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: clamp(14px, 3vmin, 28px); min-width: min(420px, 100%); }
.panel.wide { width: min(620px, 100%); }
.panel h2 { margin: 0; font-size: clamp(22px, 6vmin, 34px); }
.panel ul { text-align: left; margin: 0; padding-left: 1.1em; line-height: 1.4; font-size: clamp(13px, 3.4vmin, 17px); }
.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

button { font: inherit; color: inherit; border: 0; cursor: pointer; touch-action: manipulation; }
.btn { font-weight: 800; font-size: clamp(14px, 4vmin, 20px); padding: 0.6em 1.2em; border-radius: 14px; background: var(--accent); color: #141414; }
.btn.ghost { background: transparent; color: var(--text); border: 2px solid var(--line); }
.btn.big { font-size: clamp(20px, 7vmin, 32px); padding: 0.45em 2.6em; }
.btn:active { transform: scale(0.96); }
button:focus-visible { outline: 3px solid var(--warm); outline-offset: 2px; }

.setting { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; font-weight: 700; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 12px; background: rgba(255, 255, 255, 0.07); }
.seg button { background: transparent; color: var(--muted); font-weight: 700; padding: 8px 12px; border-radius: 9px; }
.seg button.active { background: var(--accent); color: #141414; }

.credit {
  position: fixed;
  right: calc(12px + var(--safe-r));
  bottom: calc(8px + var(--safe-b));
  font-size: 12px;
  font-weight: 700;
  color: rgba(241, 245, 255, 0.55);
  pointer-events: none;
}

.corner {
  position: fixed;
  z-index: 2;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(20, 20, 20, 0.65);
  border: 2px solid var(--line);
  font-size: 18px;
}
#btn-fullscreen, #btn-back { width: 36px; height: 36px; font-size: 16px; top: calc(6px + var(--safe-t)); }
#btn-fullscreen { left: calc(6px + var(--safe-l)); z-index: 6; display: grid; place-items: center; padding: 0; }
#btn-fullscreen .exit, #btn-fullscreen.on .enter { display: none; }
#btn-fullscreen.on .exit { display: inline; }
#btn-back { left: calc(48px + var(--safe-l)); }
.corner.pause { right: calc(6px + var(--safe-r)); top: calc(6px + var(--safe-t)); width: 36px; height: 36px; font-size: 16px; }

/* ---------- Touch controls ---------- */
#controls { position: fixed; inset: 0; z-index: 2; pointer-events: none; }
#controls > * { pointer-events: auto; }

/* Touch anywhere in this zone: the joystick jumps under the thumb. */
#stick-zone { position: absolute; left: 0; bottom: 0; width: 42%; height: 72%; touch-action: none; }
#stick-base {
  position: absolute;
  left: calc(20px + var(--safe-l));
  bottom: calc(16px + var(--safe-b));
  width: 32vmin;
  height: 32vmin;
  max-width: 170px;
  max-height: 170px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.55);
  background: rgba(20, 20, 20, 0.35);
  display: grid;
  place-items: center;
  opacity: 0.75;
}
#stick-base.active { opacity: 1; }
#stick-knob { width: 44%; height: 44%; border-radius: 50%; background: rgba(255, 255, 255, 0.85); border: 3px solid #141414; }

#buttons {
  position: absolute;
  right: calc(14px + var(--safe-r));
  bottom: calc(14px + var(--safe-b));
  --u: min(1vmin, 5.2px);
  width: calc(46 * var(--u));
  height: calc(44 * var(--u));
}
.pad {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.7);
  background: rgba(20, 20, 20, 0.5);
  font-weight: 900;
  font-size: calc(2.7 * var(--u));
  line-height: 1.15;
  touch-action: none;
}
.pad .ic { font-size: calc(6.5 * var(--u)); }
.pad.down { background: rgba(165, 207, 63, 0.85); transform: scale(0.93); }
.pad.kick { right: 0; bottom: 0; width: calc(24 * var(--u)); height: calc(24 * var(--u)); }
.pad.punch { right: calc(26 * var(--u)); bottom: calc(2 * var(--u)); width: calc(20 * var(--u)); height: calc(20 * var(--u)); }
.pad.ult { right: calc(4 * var(--u)); bottom: calc(26 * var(--u)); width: calc(18 * var(--u)); height: calc(18 * var(--u)); }
.pad.ult .ic { font-size: calc(5 * var(--u)); }
.pad.ult:disabled { opacity: 0.55; }
.pad.ult:not(:disabled) { background: #f59e0b; border-color: #fde047; animation: pulse 0.6s infinite alternate; }
.meter { width: 68%; height: calc(1.3 * var(--u)); border-radius: 6px; background: rgba(255, 255, 255, 0.25); overflow: hidden; }
.meter i { display: block; height: 100%; width: 0; background: #38bdf8; }
.pad.ult:not(:disabled) .meter { visibility: hidden; }
@keyframes pulse { to { transform: scale(1.1); } }

.logo-img { width: clamp(56px, 22vmin, 120px); height: auto; }
.gloves-vs { height: 1.15em; width: auto; vertical-align: -0.22em; }
.menu-card .row .btn.ghost { font-size: clamp(13px, 3.4vmin, 18px); padding: 0.55em 0.9em; }

/* ---------- Adventure ---------- */
#stage-face { width: clamp(56px, 20vmin, 96px); height: auto; }
.big-score { font-size: clamp(30px, 10vmin, 52px); font-weight: 900; color: var(--warm); line-height: 1; }
#score-list { width: 100%; margin: 0; padding: 0; list-style: none; max-height: 52vmin; overflow: auto; }
#score-list li { display: grid; grid-template-columns: 2.2em 1fr auto auto; gap: 10px; align-items: center; padding: 5px 8px; border-radius: 10px; text-align: left; font-weight: 700; font-size: clamp(13px, 3.6vmin, 18px); }
#score-list li:nth-child(odd) { background: rgba(255, 255, 255, 0.06); }
#score-list li.mine { background: rgba(250, 204, 21, 0.25); }
#score-list small { color: var(--muted); font-weight: 600; }
#score-list b { color: var(--warm); text-align: right; min-width: 5em; }

/* ---------- Account ---------- */
#account .panel, .panel.profile { gap: clamp(5px, 1.6vmin, 10px); }
#account h3, .profile h3 { margin: 2px 0 0; font-size: clamp(12px, 3.4vmin, 17px); color: var(--muted); }
#player-name, .profile .name { font-size: clamp(22px, 7vmin, 34px); line-height: 1.1; }
#account-profile { width: 100%; }
.records { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; width: 100%; }
.records section { flex: 1 1 220px; max-width: 280px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.adventure-line { font-weight: 800; color: var(--muted); }
.adventure-line b { color: var(--warm); font-size: 1.5em; margin-left: 6px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 0; width: 100%; }
.stats div { background: rgba(255, 255, 255, 0.07); border-radius: 10px; padding: 4px; }
.stats dt { font-size: clamp(11px, 3vmin, 14px); color: var(--muted); font-weight: 700; }
.stats dd { margin: 0; font-size: clamp(18px, 5.6vmin, 28px); font-weight: 900; }
#name-input {
  width: 12em;
  font: inherit;
  font-weight: 800;
  text-align: center;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 0.4em;
  user-select: text;
  -webkit-user-select: text;
}
button:disabled { opacity: 0.55; }

/* ---------- Levels and leaderboard ---------- */
.level { font-weight: 900; color: var(--warm); font-size: clamp(15px, 4.6vmin, 22px); }
.small { font-size: clamp(11px, 3vmin, 14px); }
#board .panel { gap: 6px; }
#board-head, #board-list button { display: grid; grid-template-columns: 2.2em 1fr 7em; gap: 8px; align-items: center; width: 100%; text-align: left; padding: 4px 8px; }
#board-head { font-size: 12px; font-weight: 800; color: var(--muted); }
#board-head span:nth-child(n + 3), #board-list button > :nth-child(n + 3) { text-align: right; }
#board-list { width: 100%; margin: 0; padding: 0; list-style: none; max-height: 52vmin; overflow: auto; }
#board-list button { background: transparent; border-radius: 10px; font-weight: 800; font-size: clamp(13px, 3.6vmin, 18px); }
#board-list li:nth-child(odd) button { background: rgba(255, 255, 255, 0.06); }
#board-list li button.mine { background: rgba(250, 204, 21, 0.25); }
#board-list button:active { background: rgba(165, 207, 63, 0.35); }
#board-list .who { display: flex; flex-direction: column; min-width: 0; line-height: 1.15; overflow: hidden; white-space: nowrap; }
#board-list small { color: var(--muted); font-weight: 600; font-size: 0.75em; }
#board-list b { color: var(--warm); }

/* ---------- Two phones ---------- */
.btn.alt { background: #38bdf8; }
.stack { display: flex; flex-direction: column; align-items: center; gap: 8px; }
#join-code {
  width: 6.5em;
  font: inherit;
  font-size: 22px;
  font-weight: 900;
  letter-spacing: 0.2em;
  text-align: center;
  text-transform: uppercase;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid var(--line);
  border-radius: 12px;
  user-select: text;
  -webkit-user-select: text;
}
#room-code { font-size: clamp(34px, 12vmin, 60px); letter-spacing: 0.2em; line-height: 1; }
#lobby-msg { min-height: 1.3em; color: var(--warm); font-weight: 700; max-width: 360px; }
.wait { color: var(--warm); font-weight: 700; }
.wait:empty { display: none; }
#notice {
  position: fixed;
  z-index: 4;
  left: 50%;
  top: 40%;
  transform: translateX(-50%);
  padding: 12px 18px;
  border-radius: 14px;
  background: rgba(11, 16, 38, 0.92);
  font-weight: 800;
  text-align: center;
  white-space: nowrap;
}

/* ---------- Letter keypad (touch screens) ---------- */
#keypad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 4px; width: min(380px, 100%); }
#keypad button { padding: 0; height: clamp(26px, 8.5vmin, 38px); border-radius: 8px; background: rgba(255, 255, 255, 0.12); font-weight: 800; }
#keypad button:active { background: var(--accent); color: #141414; }
#keypad .del { grid-column: span 3; }
/* With the keypad open the room screen has to fit a short landscape phone. */
#lobby.typing .panel { gap: 5px; padding: 10px; }
#lobby.typing h2, #lobby.typing .muted { display: none; }
#lobby.typing #lobby-msg:empty { display: none; }
