:root {
  --shell: #cdc6da;
  --shell-dark: #aaa2bb;
  --bezel: #3b3649;
  --ink: #1d1a26;
  --accent: #ffcc33;
  --accent-2: #ff4d6d;
  --btn: #b0195e;
  --btn-dark: #7c0f41;
  --ok: #3ddc84;
  --panel: rgba(18, 14, 28, 0.82);
  --panel-line: rgba(255, 255, 255, 0.14);
  --text: #f4f1fa;
  --muted: #b9b2c9;
  --pixel: 'Press Start 2P', ui-monospace, monospace;
  --sans: 'Rubik', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: #1a1524;
  color: var(--text);
  font-family: var(--sans);
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------------- Login ---------------- */
#login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 16px;
  background:
    radial-gradient(circle at 20% 10%, #5b2a86 0, transparent 45%),
    radial-gradient(circle at 90% 90%, #b0195e 0, transparent 40%),
    #1a1524;
  overflow-y: auto;
}
.login-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.logo {
  font-family: var(--pixel);
  font-size: clamp(26px, 8vw, 36px);
  margin: 0;
  text-align: center;
  color: var(--accent);
  text-shadow: 3px 3px 0 var(--btn), 6px 6px 0 rgba(0, 0, 0, 0.4);
  letter-spacing: 1px;
}
.logo span { color: #fff; }
.sub { text-align: center; color: var(--muted); margin: 10px 0 20px; }
.mode-switch { display: flex; gap: 6px; background: rgba(0, 0, 0, 0.3); padding: 4px; border-radius: 12px; margin-bottom: 18px; }
.mode-switch button { flex: 1; border: 0; background: transparent; padding: 10px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.mode-switch button.active { background: #fff; color: var(--ink); }
#login-form { display: flex; flex-direction: column; gap: 10px; }
#login-form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
#login-form input {
  font: inherit; font-size: 16px; padding: 12px 14px; border-radius: 10px;
  border: 2px solid transparent; background: #fff; color: var(--ink); user-select: text;
}
#login-form input:focus { outline: none; border-color: var(--accent); }
.hint { color: var(--muted); margin-top: -4px; }
body.login-tv .player-only { display: none; }
.btn-primary {
  margin-top: 8px; border: 0; padding: 14px; border-radius: 12px;
  background: var(--accent); color: var(--ink); font-family: var(--pixel); font-size: 14px;
  box-shadow: 0 4px 0 #b88a00;
}
.btn-primary:active { transform: translateY(3px); box-shadow: 0 1px 0 #b88a00; }
.error { color: var(--accent-2); min-height: 1.2em; margin: 4px 0 0; font-weight: 600; text-align: center; }

/* ---------------- Console layout (phone portrait = Game Boy) ---------------- */
#console {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(170deg, var(--shell), var(--shell-dark));
  padding: max(8px, env(safe-area-inset-top)) 10px max(6px, env(safe-area-inset-bottom));
}
#screen-bezel {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bezel);
  border-radius: 10px 10px 36px 10px;
  padding: 6px 14px 16px;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.5);
}
.bezel-label {
  font-family: var(--pixel); font-size: 8px; color: #9b95ad; padding: 2px 0 6px; letter-spacing: 1px;
  display: flex; align-items: center; gap: 8px;
}
.bezel-label i { font-style: normal; color: #ff5c8a; }
.led { width: 7px; height: 7px; border-radius: 50%; background: #ff2b4a; box-shadow: 0 0 6px #ff2b4a; }
#screen {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  border-radius: 4px;
  background: #000;
}
#view { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#pad {
  flex: 0 0 auto;
  height: clamp(190px, 34dvh, 300px);
  position: relative;
  touch-action: none;
}
#dpad {
  position: absolute; left: 4%; top: 50%;
  width: clamp(120px, 36vw, 160px); aspect-ratio: 1;
  transform: translateY(-58%);
  touch-action: none;
}
.dp-arm, .dp-center { position: absolute; background: #25222d; }
.dp-up, .dp-down { left: 33.3%; width: 33.4%; height: 36%; }
.dp-left, .dp-right { top: 33.3%; height: 33.4%; width: 36%; }
.dp-up { top: 0; border-radius: 6px 6px 0 0; }
.dp-down { bottom: 0; border-radius: 0 0 6px 6px; }
.dp-left { left: 0; border-radius: 6px 0 0 6px; }
.dp-right { right: 0; border-radius: 0 6px 6px 0; }
.dp-center { left: 33%; top: 33%; width: 34%; height: 34%; }
.dp-center::after { content: ''; position: absolute; inset: 28%; border-radius: 50%; background: #1a1820; }
.dp-arm { box-shadow: 0 4px 0 #111; transition: background 0.05s; }
.dp-arm.on { background: #45404f; }

#ab {
  position: absolute; right: 4%; top: 50%;
  transform: translateY(-62%) rotate(-24deg);
  display: flex; gap: 14px; align-items: flex-end;
}
.ab-btn { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.ab-btn:first-child { transform: translateY(22px); }
.ab-btn small { font-family: var(--pixel); font-size: 7px; color: #6a6280; }
#btn-a, #btn-b {
  width: clamp(58px, 17vw, 76px); aspect-ratio: 1; border-radius: 50%; border: 0;
  background: radial-gradient(circle at 35% 30%, #e0357f, var(--btn) 60%, var(--btn-dark));
  box-shadow: 0 5px 0 #4d0928, 0 8px 14px rgba(0, 0, 0, 0.3);
  font-family: var(--pixel); font-size: 16px; color: rgba(255, 255, 255, 0.75);
  touch-action: none;
}
#btn-a.on, #btn-b.on { transform: translateY(4px); box-shadow: 0 1px 0 #4d0928; }
#meta-btns {
  position: absolute; left: 50%; bottom: 8%;
  transform: translateX(-50%);
  display: flex; gap: 22px;
}
#meta-btns button {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  background: none; border: 0; font-family: var(--pixel); font-size: 7px; color: #6a6280; transform: rotate(-24deg);
  touch-action: none;
}
#meta-btns button span { display: block; width: 44px; height: 13px; border-radius: 8px; background: #7a728c; box-shadow: 0 2px 0 #4f4960; }
#meta-btns button.on span { transform: translateY(2px); box-shadow: none; }
.speaker { position: absolute; right: 7%; bottom: 7%; display: flex; gap: 5px; transform: rotate(-24deg); }
.speaker i { display: block; width: 5px; height: 34px; border-radius: 3px; background: rgba(0, 0, 0, 0.18); }

/* Phone held sideways: fullscreen view, controls on top. */
@media (orientation: landscape) and (pointer: coarse) {
  #console { padding: 0; background: #000; }
  #screen-bezel { padding: 0; border-radius: 0; background: #000; }
  .bezel-label, .speaker { display: none; }
  #screen { border-radius: 0; }
  #pad { position: fixed; inset: 0; height: auto; pointer-events: none; }
  #dpad, #ab, #meta-btns { pointer-events: auto; opacity: 0.85; }
  #dpad { top: auto; bottom: 18px; left: max(18px, env(safe-area-inset-left)); transform: none; width: 140px; }
  #ab { top: auto; bottom: 24px; right: max(24px, env(safe-area-inset-right)); transform: rotate(-20deg); }
  #meta-btns { bottom: 6px; }
  .ab-btn small, #meta-btns button { color: #ddd; }
}

/* Desktop with a keyboard: fullscreen, no on-screen pad. */
@media (hover: hover) and (pointer: fine) {
  #console { padding: 0; background: #000; }
  #screen-bezel { padding: 0; border-radius: 0; background: #000; }
  .bezel-label { display: none; }
  #screen { border-radius: 0; }
  #pad { display: none; }
}

/* TV / observer */
body.mode-tv #console { padding: 0; background: #000; }
body.mode-tv #screen-bezel { padding: 0; border-radius: 0; background: #000; }
body.mode-tv .bezel-label, body.mode-tv #pad { display: none; }
body.mode-tv #screen { border-radius: 0; }

#mute {
  position: fixed; bottom: max(10px, env(safe-area-inset-bottom)); right: 10px; z-index: 50;
  border: 0; background: rgba(0, 0, 0, 0.45); border-radius: 50%; width: 34px; height: 34px; font-size: 16px;
}
@media (pointer: coarse) { body.mode-player #mute { left: 10px; right: auto; opacity: 0.7; } }
@media (orientation: landscape) and (pointer: coarse) { body.mode-player #mute { left: 50%; bottom: auto; top: 6px; transform: translateX(-50%); } }

/* ---------------- In-screen overlays ---------------- */
#tags, #pops { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.tag {
  position: absolute; left: 0; top: 0;
  transform: translate(-50%, -100%);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; font-weight: 800; white-space: nowrap;
  text-shadow: 0 1px 2px #000, 0 0 4px #000;
  will-change: transform;
}
.tag.me .tag-name { color: var(--accent); }
.tag.dead { opacity: 0.4; }
.tag-hp { width: 46px; height: 5px; border-radius: 3px; background: rgba(0, 0, 0, 0.6); overflow: hidden; border: 1px solid rgba(0, 0, 0, 0.7); }
.tag-hp i { display: block; height: 100%; background: var(--ok); transition: width 0.15s; }
.tag-ready { font-size: 10px; padding: 1px 6px; border-radius: 6px; background: #555; }
.tag-ready.on { background: var(--ok); color: #062; text-shadow: none; }
body.mode-tv .tag { font-size: 16px; }
body.mode-tv .tag-hp { width: 76px; height: 8px; }

.pop {
  position: absolute; left: 0; top: 0; font-family: var(--pixel); font-size: 13px; color: #fff;
  text-shadow: 2px 2px 0 #000; animation: pop 0.8s ease-out forwards; white-space: nowrap;
}
.pop.big { font-size: 18px; color: var(--accent-2); }
.pop.burn { font-size: 10px; color: #ffa24d; }
.pop.info { font-size: 9px; color: #9ff; }
body.mode-tv .pop { font-size: 20px; }
body.mode-tv .pop.big { font-size: 28px; }
@keyframes pop {
  0% { transform: translate(-50%, -50%) scale(0.6); opacity: 1; }
  20% { transform: translate(-50%, -90%) scale(1.25); }
  100% { transform: translate(-50%, -220%) scale(1); opacity: 0; }
}

#hud { position: absolute; left: 8px; right: 8px; top: 8px; display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; pointer-events: none; }
.hud-hp { position: relative; width: min(46%, 220px); height: 18px; border-radius: 9px; background: rgba(0, 0, 0, 0.6); border: 2px solid rgba(255, 255, 255, 0.7); overflow: hidden; }
.hud-hp-fill { height: 100%; background: linear-gradient(90deg, #22c55e, #86efac); transition: width 0.15s; }
.hud-hp-text { position: absolute; inset: 0; display: grid; place-items: center; font-family: var(--pixel); font-size: 8px; text-shadow: 1px 1px 0 #000; }
.hud-info { font-family: var(--pixel); font-size: 8px; line-height: 1.6; text-align: right; text-shadow: 1px 1px 0 #000; }
#zone-msg { position: absolute; left: 0; right: 0; top: 34px; text-align: center; font-family: var(--pixel); font-size: 8px; color: #ff8a8a; text-shadow: 1px 1px 0 #000; pointer-events: none; }
body.mode-tv #zone-msg { top: 16px; font-size: 14px; }

#announce {
  position: absolute; left: 0; right: 0; top: 38%; text-align: center; pointer-events: none;
  font-family: var(--pixel); font-size: clamp(22px, 8vw, 44px); color: var(--accent);
  text-shadow: 3px 3px 0 var(--btn), 5px 5px 0 #000;
  opacity: 0;
}
#announce small { display: block; font-size: 0.4em; color: #fff; margin-top: 12px; text-shadow: 2px 2px 0 #000; }
#announce.show { animation: announce 1.6s ease-out forwards; }
#announce.stay { opacity: 1; animation: none; }
@keyframes announce {
  0% { transform: scale(2.2); opacity: 0; }
  12% { transform: scale(1); opacity: 1; }
  80% { opacity: 1; }
  100% { opacity: 0; transform: scale(0.95); }
}
body.mode-tv #announce { font-size: 72px; }

#feed { position: absolute; left: 8px; bottom: 8px; display: flex; flex-direction: column; gap: 3px; pointer-events: none; max-width: 70%; }
.feed-item { background: rgba(0, 0, 0, 0.6); border-radius: 6px; padding: 3px 8px; font-size: 11px; font-weight: 600; animation: feedin 0.25s ease-out; }
.feed-item b { color: var(--accent); }
.feed-item em { font-style: normal; color: var(--accent-2); }
body.mode-tv #feed { left: 24px; bottom: 24px; }
body.mode-tv .feed-item { font-size: 18px; padding: 6px 14px; }
@keyframes feedin { from { transform: translateX(-20px); opacity: 0; } }

#conn { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background: var(--panel); padding: 12px 18px; border-radius: 10px; font-weight: 600; }

/* ---------------- Player lobby panel ---------------- */
#lobby-panel {
  position: absolute; inset: 0; display: flex; flex-direction: column; padding: 8px; gap: 6px;
  font-size: 12px; pointer-events: none;
}
#lobby-panel > * { pointer-events: auto; }
.lp-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.lp-lvl { font-family: var(--pixel); font-size: 8px; line-height: 1.5; text-shadow: 1px 1px 0 #000; }
.lp-lvl b { color: var(--accent); }
.lp-xp { height: 5px; background: rgba(0, 0, 0, 0.5); border-radius: 3px; overflow: hidden; margin-top: 3px; width: 120px; }
.lp-xp i { display: block; height: 100%; background: var(--accent); }
.lp-count { background: var(--panel); border-radius: 8px; padding: 4px 8px; font-weight: 600; }
.lp-char { display: flex; align-items: center; justify-content: space-between; gap: 4px; }
.lp-char-name { text-align: center; }
.lp-char-name strong { display: block; font-family: var(--pixel); font-size: 12px; text-shadow: 2px 2px 0 #000; }
.lp-char-name span { font-size: 11px; color: #eee; text-shadow: 0 1px 2px #000; }
.arrow { border: 0; background: var(--panel); border-radius: 50%; width: 34px; height: 34px; font-size: 14px; flex: none; }
.lp-spacer { flex: 1; }
.lp-atk { background: var(--panel); border-radius: 10px; padding: 6px 8px; display: grid; gap: 4px; }
.lp-atk div { display: grid; grid-template-columns: 22px 1fr; gap: 2px 6px; align-items: center; }
.lp-atk kbd { grid-row: span 2; font-family: var(--pixel); font-size: 9px; width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; background: var(--btn); }
.lp-atk b { font-size: 12px; }
.lp-atk small { color: var(--muted); font-size: 10px; }
.lp-bottom { display: flex; gap: 6px; }
.lp-bottom button { border: 0; border-radius: 10px; padding: 8px 10px; background: var(--panel); font-weight: 600; font-size: 12px; }
.lp-hat { flex: 1; text-align: left; }
.lp-ready { flex: 1.3; background: var(--accent) !important; color: var(--ink); font-family: var(--pixel); font-size: 9px !important; }
.lp-ready.on { background: var(--ok) !important; }
.lp-players { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.chip { background: var(--panel); border-radius: 10px; padding: 2px 8px; font-size: 11px; display: inline-flex; gap: 5px; align-items: center; }
.chip i { width: 7px; height: 7px; border-radius: 50%; background: #777; }
.chip.ready i { background: var(--ok); }
.lp-bots { margin-left: auto; display: inline-flex; gap: 4px; align-items: center; background: var(--panel); border-radius: 10px; padding: 2px 4px 2px 8px; font-size: 11px; }
.lp-bots button { border: 0; background: rgba(255, 255, 255, 0.15); border-radius: 6px; width: 22px; height: 20px; padding: 0; }
.lp-countdown { white-space: pre-line; position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%); font-family: var(--pixel); font-size: 13px; color: var(--accent); text-shadow: 2px 2px 0 #000; text-align: center; pointer-events: none !important; }
.lp-keys { color: var(--muted); font-size: 10px; text-align: center; }
@media (pointer: coarse) { .lp-keys { display: none; } }

/* ---------------- Results ---------------- */
#results {
  position: absolute; inset: 0; background: rgba(10, 6, 20, 0.7); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px; padding: 12px; animation: fadein 0.4s;
}
@keyframes fadein { from { opacity: 0; } }
.res-title { font-family: var(--pixel); font-size: clamp(14px, 5vw, 26px); color: var(--accent); text-shadow: 3px 3px 0 var(--btn); text-align: center; line-height: 1.5; }
.res-table { border-collapse: collapse; font-size: 12px; background: var(--panel); border-radius: 10px; overflow: hidden; }
.res-table th, .res-table td { padding: 5px 8px; text-align: left; }
.res-table th { color: var(--muted); font-weight: 600; font-size: 10px; }
.res-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.res-table tr.me td { color: var(--accent); }
.res-up { color: var(--ok); font-weight: 800; }
.res-unlock { font-size: 12px; color: var(--ok); font-weight: 600; text-align: center; }
body.mode-tv .res-table { font-size: 22px; }
body.mode-tv .res-table th { font-size: 16px; }
body.mode-tv .res-table th, body.mode-tv .res-table td { padding: 8px 18px; }

/* ---------------- TV panel ---------------- */
#tv-panel { position: absolute; inset: 0; pointer-events: none; }
.tv-lobby { position: absolute; inset: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 24px; padding: 40px; align-items: start; }
.tv-hero { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: flex-end; gap: 24px; flex-wrap: wrap; }
.tv-hero .logo { font-size: 56px; text-align: left; }
.tv-join { background: var(--panel); border-radius: 16px; padding: 14px 22px; font-size: 20px; }
.tv-join b { display: block; font-family: var(--pixel); font-size: 20px; color: var(--accent); margin-top: 6px; }
.tv-box { background: var(--panel); border-radius: 16px; padding: 18px 22px; font-size: 20px; align-self: end; }
.tv-box h2 { font-family: var(--pixel); font-size: 14px; margin: 0 0 12px; color: var(--accent); }
.tv-box ol, .tv-box ul { margin: 0; padding-left: 28px; display: grid; gap: 6px; }
.tv-box li small { color: var(--muted); }
.tv-box .ready { color: var(--ok); font-weight: 800; }
.tv-box .muted { color: var(--muted); }
.tv-countdown { white-space: pre-line; font-family: var(--pixel); color: var(--accent); font-size: 22px; text-align: center; grid-column: 1 / -1; text-shadow: 3px 3px 0 #000; }
.tv-side { position: absolute; right: 20px; top: 20px; width: 300px; display: flex; flex-direction: column; gap: 8px; }
.tv-map { position: absolute; left: 24px; top: 20px; font-family: var(--pixel); font-size: 16px; text-shadow: 2px 2px 0 #000; }
.tv-map small { display: block; font-family: var(--sans); font-size: 16px; color: var(--muted); margin-top: 8px; }
.tv-fighter { background: var(--panel); border-radius: 12px; padding: 8px 12px; display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; font-size: 18px; font-weight: 600; }
.tv-fighter.dead { opacity: 0.4; }
.tv-fighter .bar { grid-column: 1 / -1; height: 8px; border-radius: 4px; background: rgba(0, 0, 0, 0.5); overflow: hidden; }
.tv-fighter .bar i { display: block; height: 100%; transition: width 0.15s; }
.tv-fighter small { color: var(--muted); font-weight: 400; }
