:root {
  --bg: #0d0e12;
  --panel: #171922;
  --panel-2: #20232f;
  --line: #2c3040;
  --text: #eceef5;
  --muted: #9aa0b4;
  --accent: #8cff00;
  --accent-ink: #0b1400;
  --pink: #ff0a84;
  --radius: 10px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

h1, h2 { margin: 0; line-height: 1.15; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 0; }

button {
  font: inherit; color: inherit; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel-2);
  padding: 7px 14px; border-radius: 8px;
}
button:hover { border-color: #4a5070; }
button:focus-visible, input:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
button.primary {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
  font-weight: 700; padding: 12px 34px; font-size: 17px;
}
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; }

/* ---------- setup ---------- */
#setup { max-width: 1100px; margin: 0 auto; padding: 28px 20px 60px; }
.hero { margin-bottom: 26px; }
.hero h1 { font-size: 40px; letter-spacing: -0.02em; }
.hero p { color: var(--muted); margin: 8px 0 0; max-width: 60ch; }

#setup section { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.section-head h2, .music h2 { font-size: 18px; }
.tools { display: flex; gap: 8px; }

.team-grid { display: grid; grid-template-columns: repeat(var(--cols, 6), minmax(0, 1fr)); gap: 8px; }
.team-field {
  display: flex; align-items: center; min-width: 0;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
.team-field:focus-within { border-color: var(--accent); }
/* the colour chip is a swatch with an invisible <select> laid over it, so a click opens the colour list */
.team-field .chip { position: relative; flex: none; width: 30px; align-self: stretch; min-height: 38px; border-right: 1px solid rgba(0,0,0,.4); }
.team-field .chip::after {
  content: "▾"; position: absolute; right: 3px; bottom: 1px; font-size: 11px; line-height: 1; color: #fff;
  text-shadow: 0 0 2px #000, 0 0 3px #000; pointer-events: none;
}
.team-field .chip select { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; border: 0; padding: 0; }
.team-field .chip:focus-within { outline: 2px solid var(--accent); outline-offset: -2px; }
.team-field input {
  flex: 1; min-width: 0; background: transparent; border: 0; color: var(--text);
  font: inherit; padding: 10px 8px; outline-offset: -2px;
}
.team-field input::placeholder { color: #6d7389; }

details { margin-top: 14px; }
summary { cursor: pointer; color: var(--muted); }
summary:hover { color: var(--text); }
.paste textarea {
  width: 100%; margin: 8px 0; padding: 10px; font: inherit; color: var(--text);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; resize: vertical;
}

.music-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 12px; }
#musicName { margin: 0; color: var(--text); }
.credit a { color: var(--accent); }
.credit a:hover { text-decoration: none; }
select {
  font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px;
}
select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.start-row { display: flex; justify-content: center; margin: 26px 0 10px; }

.howto { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 18px; }
.howto ul { margin: 10px 0 0; padding-left: 20px; color: var(--muted); }
.howto li { margin: 5px 0; }
.howto b { color: var(--text); }
.howto b.x2 { color: var(--accent); }
.howto b.r { color: var(--pink); }

/* ---------- game ---------- */
#game { padding: 10px 12px 14px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px 20px; flex-wrap: wrap; margin-bottom: 10px; }
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.brand { font-size: 16px; }
.stat { color: var(--muted); font-variant-numeric: tabular-nums; padding: 2px 10px; border: 1px solid var(--line); border-radius: 999px; }
.stat span { color: var(--text); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button, .seg .seg-file { border: 0; border-radius: 0; background: transparent; padding: 7px 12px; cursor: pointer; display: inline-block; }
.seg button:hover, .seg .seg-file:hover { background: rgba(255,255,255,.06); }
.seg button.on, .seg .seg-file.on { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.seg .seg-file input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg .seg-file:focus-within { outline: 2px solid var(--accent); outline-offset: -2px; }
input[type=range] { width: 90px; accent-color: var(--accent); }

.game-body { display: flex; gap: 12px; align-items: flex-start; }
.stage-wrap { flex: 1 1 0; min-width: 0; display: grid; place-items: center; background: #000; border-radius: var(--radius); }
.stage-wrap:fullscreen { border-radius: 0; height: 100vh; }

.stage {
  position: relative; display: grid; grid-template-columns: 1680fr 320fr;
  aspect-ratio: 2000 / 1120; width: min(100%, calc((100vh - 74px) * 2000 / 1120));
  background: #000; overflow: hidden;
}
.stage-wrap:fullscreen .stage { width: min(100vw, calc(100vh * 2000 / 1120)); }
.stage canvas { display: block; width: 100%; height: 100%; }
.board-wrap { position: relative; }
.board-wrap canvas { position: absolute; inset: 0; }
#pegCanvas { border-left: 4px solid #2a2a2a; }

.toasts { position: absolute; left: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 6px; pointer-events: none; }
.toast {
  padding: 5px 12px; border-radius: 999px; font-weight: 700; font-size: clamp(11px, 1.3vw, 18px);
  background: rgba(0,0,0,.72); color: #fff; border-left: 10px solid var(--c, #fff);
  animation: toast 4s ease forwards;
}
@keyframes toast { 0% { opacity: 0; transform: translateY(8px); } 8% { opacity: 1; transform: none; } 80% { opacity: 1; } 100% { opacity: 0; } }

.pause-badge {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.75); padding: 6px 18px; border-radius: 999px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
}

.win { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.6); backdrop-filter: blur(3px); animation: fade .5s ease; }
@keyframes fade { from { opacity: 0; } }
.win-card {
  text-align: center; padding: clamp(16px, 3vw, 44px) clamp(20px, 5vw, 80px); border-radius: 18px;
  background: var(--tile, #333); color: var(--ink, #fff); border: 4px solid rgba(255,255,255,.85);
  box-shadow: 0 20px 80px rgba(0,0,0,.6); max-width: 90%;
}
.win-kicker { text-transform: uppercase; letter-spacing: .25em; font-weight: 700; font-size: clamp(12px, 1.6vw, 22px); opacity: .85; }
.win-name { font-size: clamp(28px, 6vw, 96px); font-weight: 900; line-height: 1.05; margin: 6px 0; overflow-wrap: anywhere; }
.win-sub { font-size: clamp(12px, 1.5vw, 20px); opacity: .85; }
.win-actions { display: flex; gap: 12px; justify-content: center; margin-top: clamp(12px, 2vw, 28px); }
.win-actions button { background: rgba(0,0,0,.35); color: #fff; border-color: rgba(255,255,255,.6); }
.win-actions button.primary { background: #fff; color: #111; border-color: #fff; padding: 10px 26px; font-size: 16px; }

.leaderboard { flex: none; width: 250px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; max-height: calc(100vh - 74px); overflow: auto; }
.leaderboard h2 { font-size: 15px; margin-bottom: 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.leaderboard ol { list-style: none; margin: 0; padding: 0; }
.lb-row { display: grid; grid-template-columns: 22px 12px 1fr auto auto; align-items: center; gap: 6px; padding: 3px 0; font-size: 13px; font-variant-numeric: tabular-nums; }
.lb-rank { color: var(--muted); text-align: right; }
.lb-sw { width: 12px; height: 12px; border-radius: 3px; }
.lb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-c { color: var(--muted); font-size: 12px; }
.lb-n { color: var(--muted); min-width: 84px; text-align: right; }
.lb-row.out .lb-name { text-decoration: line-through; color: #666; }
.lb-row.out .lb-n { color: #555; }

@media (max-width: 1100px) {
  .game-body { flex-direction: column; }
  .leaderboard { width: 100%; max-height: none; }
  .leaderboard ol { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); column-gap: 16px; }
  .stage { width: 100%; }
}
@media (max-width: 800px) {
  .team-grid[data-cols="6"] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .team-grid[data-cols="4"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 480px) {
  .team-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .hero h1 { font-size: 30px; }
}
@media (prefers-reduced-motion: reduce) {
  .toast, .win { animation: none; }
}
