:root {
  --bg: #0f1218;
  --panel: #171b24;
  --line: #2a3040;
  --text: #e6e9f0;
  --muted: #8a92a6;
  --accent: #3ec9dc;
  --danger: #ef5367;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1200px 600px at 50% -10%, #1d2434 0%, var(--bg) 60%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100%;
}
h1, h2, dt, #counts, #stats, .mini-name, #clear-text, #banner {
  font-family: Consolas, "Courier New", monospace;
  letter-spacing: 0.08em;
}
header {
  width: 100%;
  max-width: 1100px;
  display: flex;
  align-items: baseline;
  gap: 24px;
  padding: 14px 16px 6px;
}
header h1 { margin: 0; font-size: 22px; color: var(--accent); }
#status { color: var(--muted); font-size: 13px; flex: 1; }
#counts { font-size: 13px; color: var(--muted); }
#counts b { color: var(--text); }

main {
  display: flex;
  gap: 24px;
  padding: 8px 16px 24px;
  width: 100%;
  max-width: 1100px;
  flex-wrap: wrap;
  justify-content: center;
}
#arena { display: flex; gap: 12px; align-items: flex-start; }
.side { width: 112px; }
.side h2, #opponents h2 { margin: 0 0 6px; font-size: 12px; color: var(--muted); font-weight: normal; }
canvas { display: block; }
#hold, #next { background: var(--panel); border: 1px solid var(--line); }

#stats { margin: 18px 0 0; text-align: right; }
#stats dt { font-size: 11px; color: var(--muted); margin-top: 10px; }
#stats dd { margin: 0; font-size: 20px; }

.board-wrap { position: relative; display: flex; gap: 6px; align-items: flex-end; }
#board { border: 1px solid var(--line); }
#garbage-meter {
  width: 8px;
  height: 560px;
  background: var(--panel);
  border: 1px solid var(--line);
  position: relative;
}
#garbage-fill {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 0;
  background: var(--danger);
  transition: height 0.15s;
}
#clear-text {
  position: absolute;
  left: 20px; right: 0; top: 40%;
  text-align: center;
  font-size: 18px;
  color: #fff;
  text-shadow: 0 0 8px rgba(0,0,0,0.8);
  opacity: 0;
  transition: opacity 0.6s;
  pointer-events: none;
}
#clear-text.show { opacity: 1; transition: none; }
#banner {
  position: absolute;
  left: 20px; right: 0; top: 50%;
  transform: translateY(-50%);
  background: rgba(10, 12, 18, 0.88);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 16px 8px;
  text-align: center;
}
#banner-title { font-size: 22px; }
#banner-sub { font-size: 12px; color: var(--muted); margin-top: 6px; }
.hidden { display: none !important; }

#sidebar { flex: 1; min-width: 260px; max-width: 420px; display: flex; flex-direction: column; gap: 18px; }
#room { background: var(--panel); border: 1px solid var(--line); padding: 12px; }
.room-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.room-head h2 { flex: 1; margin: 0; font-size: 13px; }
#room-code { color: var(--accent); font-size: 18px; letter-spacing: 0.15em; }
button.small { flex: none; padding: 5px 10px; font-size: 12px; font-weight: normal; }
#room-players { display: flex; flex-wrap: wrap; gap: 6px; font-size: 12px; }
#room-players span { background: var(--bg); border: 1px solid var(--line); padding: 3px 8px; }
#room-players span.host { border-color: var(--accent); color: var(--accent); }
#room-players span.me { font-weight: 600; }
#play-toggle { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 13px; cursor: pointer; }
#play-toggle input { width: 16px; height: 16px; margin: 0; }
body.display #play-toggle { margin: 0; flex: none; }
#start-btn { width: 100%; margin-top: 10px; }
#start-btn:disabled { opacity: 0.4; cursor: default; }
#room-hint { color: var(--muted); font-size: 12px; margin-top: 8px; }
.join-row { margin: 10px 0; }
.join-row input {
  flex: 1; margin: 0; padding: 10px; font-size: 16px;
  background: var(--bg); color: var(--text); border: 1px solid var(--line);
  text-transform: uppercase; letter-spacing: 0.1em; min-width: 0;
}
#join-error { margin-top: 10px; color: var(--danger); font-size: 13px; }
.watch-link { margin: 14px 0 0; font-size: 13px; }
.watch-link a { color: var(--accent); }
#opp-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.mini { width: 50px; }
.mini canvas { border: 1px solid var(--line); }
.mini.dead canvas { opacity: 0.3; }
.mini-name {
  font-size: 9px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 2px;
}
.mini-sub { font-size: 9px; color: var(--danger); font-family: Consolas, monospace; }
#opp-more { color: var(--muted); font-size: 12px; margin-top: 8px; }

/* ---- display mode: the room creator's screen shows the players, 6 × 4 ---- */
body.display #arena { display: none; }
body.display main { max-width: none; }
body.display #sidebar { max-width: none; width: 100%; gap: 10px; }
body.display #room { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 8px 12px; }
body.display .room-head { margin: 0; flex: none; }
body.display #room-players { flex: 1; max-height: 44px; overflow: auto; }
body.display #start-btn { flex: none; width: auto; margin: 0; padding: 8px 20px; }
body.display #room-hint { width: 100%; margin: 0; }
/* during a round the grid already shows everyone: keep the panel to one line */
body.display.in-round #room-players,
body.display.in-round #room-hint { display: none; }
body.display #opp-title { font-size: 15px; color: var(--text); }
body.display #opp-grid {
  display: grid;
  /* --cols / --rows come from the player count: up to 8 × 3 */
  grid-template-columns: repeat(var(--cols, 8), minmax(0, 1fr));
  gap: 10px 12px;
  justify-items: center;
}
body.display .mini { width: 100%; display: flex; flex-direction: column; align-items: center; }
body.display .mini canvas {
  /* As big as the column allows, but never taller than a row's share of the
     screen; aspect-ratio keeps the board 10:22 (with the spawn rows) so it is never stretched. The
     bitmap is then redrawn at this exact size (fitBoard), so it stays sharp. */
  width: min(100%, calc((100vh - 160px - var(--rows, 3) * 48px) / var(--rows, 3) / 2.2));
  height: auto;
  aspect-ratio: 10 / 22;
  border: 1px solid var(--line);
}
body.display .mini-name { font-size: 13px; color: var(--text); text-align: center; margin-top: 4px; }
body.display .mini-sub { font-size: 12px; text-align: center; min-height: 16px; } /* same height before and after the rank appears */
body.display #opp-more { display: none; }   /* shown in the title line instead, so the grid height never changes */
body.display .mini.dead canvas { opacity: 0.35; }
body.display .mini { cursor: pointer; }

/* ---- spotlight: big board(s) on the left, the grid moves to the right ---- */
body.display.spot #opponents {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  column-gap: 18px;
  align-items: start;
}
body.display.spot #opp-title { grid-column: 1 / -1; }
body.display.spot #spotlight { grid-column: 1; grid-row: 2; }
body.display.spot #opp-grid { grid-column: 2; grid-row: 2; }
body.display.spot #opp-more { grid-column: 2; grid-row: 3; }

#spotlight { display: flex; gap: 14px; cursor: pointer; }
.spot-view { display: flex; flex-direction: column; align-items: center; gap: 4px; }
/* board heights leave room for the HOLD/NEXT strip below (about a tenth of the board height) */
.spot-board {
  height: calc((100vh - 290px) / 1.11);
  width: auto;
  aspect-ratio: 10 / 22;
  border: 2px solid var(--accent);
  box-shadow: 0 0 24px rgba(62, 201, 220, 0.25);
  background: #0b0d12;
}
#spotlight[data-count="2"] .spot-board { height: calc((100vh - 330px) / 1.11); }
/* three side by side would push the grid off the screen: keep them a bit smaller */
#spotlight[data-count="3"] .spot-board { height: min(calc((100vh - 330px) / 1.11), 26vw); }
.spot-view.dead .spot-board { opacity: 0.45; border-color: var(--danger); box-shadow: none; }
.spot-strip { display: block; height: auto; }
.spot-name { font-family: Consolas, "Courier New", monospace; font-size: 24px; letter-spacing: 0.06em; }
.spot-stats { font-family: Consolas, monospace; font-size: 14px; color: var(--muted); }
.spot-badge {
  min-height: 22px;
  font-family: Consolas, monospace; font-size: 13px; letter-spacing: 0.08em;
  color: var(--muted);
}
.spot-badge.hot { color: #e9cf3a; }
.spot-bar { height: 3px; background: var(--accent); align-self: flex-start; transition: width 0.25s linear; }

/* with the spotlight up, tiles stay at most half its height so the big board stands out */
body.display.spot .mini canvas { width: min(100%, calc((100vh - 160px - max(var(--rows, 3), 2) * 48px) / max(var(--rows, 3), 2) / 2.2)); }

/* the spotlit player's tile in the grid */
body.display .mini.spot canvas { border-color: var(--accent); box-shadow: 0 0 10px rgba(62, 201, 220, 0.5); }
body.display .mini.spot .mini-name { color: var(--accent); }

#overlay {
  position: fixed; inset: 0;
  background: rgba(8, 10, 14, 0.9);
  display: flex; align-items: center; justify-content: center;
  z-index: 10;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 28px 32px;
  width: min(520px, 92vw);
}
.card h1 { margin: 0 0 8px; color: var(--accent); }
.card p { color: var(--muted); font-size: 14px; line-height: 1.5; }
.card label { display: block; margin: 16px 0; font-size: 14px; }
.card input {
  display: block; width: 100%; margin-top: 6px;
  padding: 10px; font-size: 16px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line);
}
.row { display: flex; gap: 10px; }
button {
  flex: 1; padding: 12px; font-size: 15px; cursor: pointer;
  background: var(--accent); color: #06121a; border: 0; font-weight: 600;
}
button.secondary { background: transparent; color: var(--text); border: 1px solid var(--line); }
.keys { margin-top: 20px; width: 100%; font-size: 12px; color: var(--muted); border-collapse: collapse; }
.keys td { padding: 4px 6px; }
.keys td:nth-child(odd) { color: var(--text); font-family: Consolas, monospace; white-space: nowrap; }

/* ---- touch controls: hidden unless the device has a touch screen ------- */
#touch {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  gap: 6px;
  background: rgba(15, 18, 24, 0.95);
  border-top: 1px solid var(--line);
  z-index: 5;
}
body.touch #touch { display: flex; }
#touch button {
  flex: 1;
  padding: 0;
  height: 56px;
  font-size: 22px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#touch button[data-action="hold"] { font-size: 12px; letter-spacing: 0.08em; }
#touch button[data-action="hard"] { background: #1f2a3a; }
#touch button.down { background: var(--accent); color: #06121a; }
.touch-help { display: none; }
body.touch .touch-help { display: block; }
#board { touch-action: none; user-select: none; -webkit-user-select: none; }
body.touch main { padding-bottom: 90px; }

/* ---- portrait / narrow screens ---------------------------------------- */
@media (max-width: 720px) {
  header { gap: 12px; padding: 10px 12px 4px; flex-wrap: wrap; }
  header h1 { font-size: 16px; }
  main { gap: 12px; padding: 4px 8px 16px; }
  #arena { width: 100%; gap: 6px; }
  .side { width: 56px; flex: none; }
  .side h2 { font-size: 10px; }
  #hold, #next { width: 100%; height: auto; }
  #stats { margin-top: 8px; }
  #stats dt { font-size: 9px; margin-top: 6px; }
  #stats dd { font-size: 13px; }
  #st-time { font-size: 10px; }
  .board-wrap { flex: 1; min-width: 0; align-items: stretch; }
  #board { width: 100%; height: auto; }
  #garbage-meter { height: auto; width: 6px; }
  #banner-title { font-size: 16px; }
  #clear-text { font-size: 14px; }
  #sidebar { max-width: none; width: 100%; }
  .mini { width: 40px; }
  .mini canvas { width: 40px; height: 88px; }
  .card { padding: 20px 18px; }
  .keys { display: none; }
}
