:root {
  --bg: #05060a;
  --bg-2: #0b0e17;
  --panel: rgba(16, 20, 33, 0.72);
  --line: rgba(120, 160, 255, 0.16);
  --text: #d7e3ff;
  --muted: #6b7896;
  --neon: #21d4fd;
  --neon-2: #ff3b6b;
  --neon-3: #9bff3b;
  --glow: 0 0 18px rgba(33, 212, 253, 0.55);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background:
    radial-gradient(1200px 700px at 70% -10%, rgba(33,212,253,0.10), transparent 60%),
    radial-gradient(900px 600px at 10% 110%, rgba(255,59,107,0.10), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: "Space Mono", ui-monospace, monospace;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* CRT atmosphere -------------------------------------------------------- */
.scanlines {
  position: fixed; inset: 0; z-index: 9999; pointer-events: none;
  background: repeating-linear-gradient(
    to bottom, rgba(0,0,0,0) 0px, rgba(0,0,0,0) 2px,
    rgba(0,0,0,0.18) 3px, rgba(0,0,0,0.18) 4px);
  mix-blend-mode: multiply; opacity: 0.5;
}
.vignette {
  position: fixed; inset: 0; z-index: 9998; pointer-events: none;
  box-shadow: inset 0 0 220px rgba(0,0,0,0.9);
}

/* Screens --------------------------------------------------------------- */
.screen { display: none; height: 100%; }
.screen.active { display: flex; flex-direction: column; }

/* ===================== LOBBY ===================== */
#lobby { align-items: center; justify-content: center; padding: 24px; }

.lobby-head { text-align: center; margin-bottom: 30px; }
.logo {
  font-family: "Monoton", cursive;
  font-size: clamp(48px, 11vw, 120px);
  margin: 0; letter-spacing: 4px; line-height: 1;
  color: #eafaff;
  text-shadow:
    0 0 8px var(--neon), 0 0 26px rgba(33,212,253,0.8),
    0 0 60px rgba(33,212,253,0.5), 0 0 90px rgba(255,59,107,0.35);
  animation: flicker 5s infinite steps(40);
}
@keyframes flicker {
  0%, 92%, 100% { opacity: 1; }
  93% { opacity: 0.72; } 94% { opacity: 1; }
  96% { opacity: 0.85; } 97% { opacity: 1; }
}
.tagline { color: var(--muted); letter-spacing: 2px; margin: 12px 0 0; font-size: 13px; }

.lobby-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 18px;
  width: min(920px, 100%);
}
@media (max-width: 760px) { .lobby-grid { grid-template-columns: 1fr; } }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 22px;
  backdrop-filter: blur(8px);
  box-shadow: 0 10px 40px rgba(0,0,0,0.45), inset 0 1px 0 rgba(255,255,255,0.04);
}

.field-label {
  display: block; text-transform: uppercase; letter-spacing: 3px;
  font-size: 11px; color: var(--neon); margin-bottom: 8px;
}

input {
  width: 100%; padding: 13px 14px;
  background: #070a12; color: var(--text);
  border: 1px solid var(--line); border-radius: 9px;
  font-family: inherit; font-size: 15px; outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input::placeholder { color: #46506a; }
input:focus { border-color: var(--neon); box-shadow: 0 0 0 3px rgba(33,212,253,0.14); }

.create-row { display: flex; gap: 10px; margin-top: 18px; }
.create-row input { flex: 1; }

.btn {
  font-family: inherit; font-weight: 700; cursor: pointer;
  border-radius: 9px; padding: 13px 18px; font-size: 14px;
  border: 1px solid var(--line); background: #0d1322; color: var(--text);
  transition: transform .08s, box-shadow .15s, background .15s, border-color .15s;
  white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary {
  background: linear-gradient(180deg, #1ec9f0, #0f9fd6);
  color: #03121a; border: none; box-shadow: var(--glow);
}
.btn.ghost { background: transparent; }
.btn.ghost:hover { border-color: var(--neon); color: var(--neon); }
.btn.small { padding: 8px 14px; font-size: 13px; }

.hint { color: var(--muted); font-size: 12px; line-height: 1.6; margin: 18px 0 0; }
.controls-legend { margin-top: 14px; color: var(--muted); font-size: 12px; }

kbd {
  display: inline-block; min-width: 22px; text-align: center;
  padding: 2px 7px; margin: 0 2px;
  background: #0d1322; border: 1px solid var(--line);
  border-bottom-width: 3px; border-radius: 6px;
  font-family: inherit; font-size: 12px; color: var(--text);
}

.arenas-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.arena-list { list-style: none; margin: 10px 0 0; padding: 0; max-height: 320px; overflow-y: auto; }
.arena-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; margin-bottom: 8px;
  background: #090d18; border: 1px solid var(--line); border-radius: 10px;
  transition: border-color .15s, transform .08s;
}
.arena-row:hover { border-color: rgba(33,212,253,0.5); transform: translateX(2px); }
.arena-row .a-name { font-weight: 700; flex: 1; }
.arena-row .a-meta { color: var(--muted); font-size: 12px; }
.a-state {
  font-size: 10px; text-transform: uppercase; letter-spacing: 1.5px;
  padding: 3px 8px; border-radius: 20px; border: 1px solid var(--line);
}
.a-state.running { color: var(--neon-2); border-color: rgba(255,59,107,0.5); }
.a-state.waiting { color: var(--neon-3); border-color: rgba(155,255,59,0.4); }
.a-state.countdown, .a-state.roundover { color: var(--neon); border-color: rgba(33,212,253,0.4); }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 26px 0; }
.error-line { color: var(--neon-2); height: 18px; margin-top: 14px; font-size: 13px; }

/* ===================== ARENA ===================== */
.arena-bar {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 18px; border-bottom: 1px solid var(--line);
  background: rgba(7,10,18,0.7); backdrop-filter: blur(6px);
}
.arena-id { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: 1px; }
.arena-id .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--neon); box-shadow: var(--glow); }
.status-pill {
  margin-left: auto; padding: 6px 14px; border-radius: 20px;
  font-size: 12px; letter-spacing: 1.5px; text-transform: uppercase;
  border: 1px solid var(--line); color: var(--muted);
}
.status-pill.live { color: var(--neon-2); border-color: rgba(255,59,107,0.5); }
.status-pill.spectating { color: var(--neon-3); border-color: rgba(155,255,59,0.45); }

.arena-body { flex: 1; display: flex; min-height: 0; gap: 16px; padding: 16px; }
.stage { flex: 1; display: flex; align-items: center; justify-content: center; min-width: 0; }

.canvas-wrap {
  position: relative;
  aspect-ratio: 1000 / 700;
  max-width: 100%; max-height: 100%;
  border-radius: 12px; overflow: hidden;
  border: 1px solid rgba(33,212,253,0.25);
  box-shadow: 0 0 0 1px rgba(0,0,0,0.6), 0 0 60px rgba(33,212,253,0.10), inset 0 0 80px rgba(0,0,0,0.6);
}
#game {
  display: block; width: 100%; height: 100%;
  background:
    linear-gradient(rgba(120,160,255,0.05) 1px, transparent 1px) 0 0 / 100% 25px,
    linear-gradient(90deg, rgba(120,160,255,0.05) 1px, transparent 1px) 0 0 / 25px 100%,
    #03050b;
  image-rendering: auto;
}

.overlay {
  position: absolute; inset: 0; display: flex;
  flex-direction: column; align-items: center; justify-content: center;
  text-align: center; pointer-events: none;
  background: radial-gradient(circle, rgba(3,5,11,0.35), rgba(3,5,11,0.78));
  opacity: 0; transition: opacity .2s; gap: 8px;
}
.overlay.show { opacity: 1; }
.overlay .big {
  font-family: "Monoton", cursive; font-size: clamp(40px, 9vw, 96px);
  color: #eafaff; text-shadow: 0 0 20px var(--neon), 0 0 50px rgba(33,212,253,0.6);
}
.overlay .sub { color: var(--text); font-size: 16px; letter-spacing: 1px; }
.overlay .sub2 { color: var(--muted); font-size: 13px; margin-top: 4px; }

.scoreboard {
  width: 240px; flex-shrink: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 18px; display: flex; flex-direction: column;
}
.scoreboard h2 {
  margin: 0 0 14px; font-size: 13px; letter-spacing: 4px; color: var(--neon);
  font-weight: 700;
}
#scoreList { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; }
.score-row {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; margin-bottom: 6px; border-radius: 9px;
  background: #090d18; border: 1px solid transparent;
}
.score-row.you { border-color: rgba(33,212,253,0.45); }
.score-row.dead { opacity: 0.42; }
.score-row .swatch { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; box-shadow: 0 0 8px currentColor; }
.score-row .s-nick { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-row .s-tag { font-size: 9px; color: var(--neon-3); letter-spacing: 1px; }
.score-row .s-pts { font-weight: 700; font-size: 15px; }
.board-foot { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.ctrl-hint { color: var(--muted); font-size: 11px; line-height: 1.8; }

@media (max-width: 720px) {
  .arena-body { flex-direction: column; }
  .scoreboard { width: 100%; max-height: 150px; }
}

/* ---- global leaderboard (optional, Supabase) ---- */
.leaderboard { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.leaderboard.hidden { display: none; }
.lb-list { list-style: none; margin: 8px 0 0; padding: 0; }
.lb-row {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 2px; font-size: 13px;
}
.lb-row .lb-rank { width: 22px; color: var(--neon-3); font-weight: 700; }
.lb-row .lb-nick { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-row .lb-wins { color: var(--muted); }
.lb-row:first-child .lb-rank { color: var(--neon); }
.conn-state {
  position: fixed; bottom: 12px; right: 14px; z-index: 50;
  font-size: 11px; color: var(--muted); letter-spacing: 1px;
}
.conn-state .ok { color: var(--neon-3); }
.conn-state .bad { color: var(--neon-2); }

/* ---- mobile touch controls ---- */
.touch-controls { position: absolute; inset: 0; z-index: 6; display: none; }
body.touch .touch-controls { display: flex; }
.touch-zone {
  flex: 1; pointer-events: auto; touch-action: none;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
  display: flex; align-items: flex-end; justify-content: center; padding-bottom: 22px;
}
.touch-zone.left { border-right: 1px dashed rgba(215,227,255,0.10); }
.touch-zone span { font-size: 32px; color: rgba(215,227,255,0.16); transition: color .1s, transform .1s; }
.touch-zone.active span { color: var(--neon); transform: scale(1.3); text-shadow: var(--glow); }

.rotate-hint { display: none; }
@media (orientation: portrait) and (pointer: coarse) {
  body.touch .rotate-hint {
    display: block; position: absolute; left: 50%; bottom: 64px; transform: translateX(-50%);
    z-index: 7; white-space: nowrap; font-size: 11px; letter-spacing: 1px;
    color: var(--muted); background: rgba(7,10,18,0.7); padding: 6px 12px; border-radius: 20px;
    border: 1px solid var(--line); pointer-events: none;
  }
}

/* hide keyboard hint on touch devices */
body.touch .ctrl-hint { display: none; }

@media (max-width: 720px) {
  .arena-body { padding: 8px; gap: 8px; }
  .scoreboard { max-height: 116px; padding: 12px; }
  .scoreboard h2 { margin-bottom: 8px; }
  .conn-state { bottom: 6px; right: 8px; }
  .arena-bar { padding: 9px 12px; gap: 10px; }
}
