:root {
  --ink: #14213d;
  --ink-2: #1d2d52;
  --ink-3: #2a3d68;
  --paper: #fdf6e3;
  --paper-2: #efe4c8;
  --coral: #ff6b4a;
  --teal: #2ec4b6;
  --sun: #ffbe0b;
  --muted: #9fb0d0;
  --good: #3ddc84;
  --bad: #ff4d6d;
  --radius: 14px;
  font-family: 'Bricolage Grotesque', system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--ink);
  background-image: radial-gradient(circle at 1px 1px, rgba(255,255,255,0.06) 1px, transparent 0);
  background-size: 22px 22px;
  color: var(--paper);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
#app { min-height: 100%; display: flex; flex-direction: column; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.1; }

.wrap {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
}

/* ---------- Brand ---------- */
.logo { display: flex; gap: 4px; justify-content: center; margin: 24px 0 4px; }
.logo span {
  width: 52px; height: 52px; border-radius: 12px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 30px;
  background: var(--paper); color: var(--ink);
  box-shadow: 0 4px 0 var(--paper-2);
  transform: rotate(var(--r, 0deg));
}
.logo span:nth-child(odd) { --r: -4deg; }
.logo span:nth-child(even) { --r: 3deg; }
.logo span:nth-child(3) { background: var(--coral); color: white; box-shadow: 0 4px 0 #c94a2e; }
.logo span:nth-child(5) { background: var(--teal); color: var(--ink); box-shadow: 0 4px 0 #1e8f85; }
.tagline { text-align: center; color: var(--muted); margin: 0 0 12px; }

/* ---------- Controls ---------- */
.btn {
  border: 0; border-radius: var(--radius);
  padding: 16px 18px;
  font-weight: 800; font-size: 18px;
  background: var(--ink-3); color: var(--paper);
  box-shadow: 0 4px 0 rgba(0,0,0,0.35);
  transition: transform 0.08s;
  width: 100%;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,0.35); }
.btn.primary { background: var(--coral); color: white; box-shadow: 0 4px 0 #c94a2e; }
.btn.teal { background: var(--teal); color: var(--ink); box-shadow: 0 4px 0 #1e8f85; }
.btn.small { padding: 10px 14px; font-size: 15px; width: auto; }
.btn.ghost { background: transparent; box-shadow: none; color: var(--muted); font-weight: 600; }
.btn:disabled { opacity: 0.5; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }

.card {
  background: var(--ink-2);
  border-radius: 18px;
  padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  border: 1px solid rgba(255,255,255,0.06);
}
.label { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; }
.input {
  width: 100%; border: 2px solid var(--ink-3); background: var(--ink);
  border-radius: 12px; padding: 14px; font-size: 20px; font-weight: 600;
}
.input:focus { outline: none; border-color: var(--teal); }
.input.code { text-transform: uppercase; letter-spacing: 0.3em; text-align: center; font-weight: 800; font-size: 28px; }

.seg { display: flex; background: var(--ink); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button {
  flex: 1; border: 0; background: transparent; padding: 10px 4px; border-radius: 9px;
  font-weight: 700; color: var(--muted);
}
.seg button.on { background: var(--paper); color: var(--ink); }

/* ---------- Lobby ---------- */
.room-code {
  display: flex; align-items: baseline; justify-content: space-between;
}
.room-code b { font-size: 34px; letter-spacing: 0.18em; color: var(--sun); }
.players { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 12px 8px 8px; border-radius: 999px; background: var(--ink);
  font-weight: 700;
}
.chip .dot { width: 22px; height: 22px; border-radius: 50%; background: var(--c); display: grid; place-items: center; font-size: 12px; color: var(--ink); }
.chip.off { opacity: 0.45; }
.chip .pts { color: var(--muted); font-weight: 600; }
.hint { color: var(--muted); font-size: 14px; margin: 0; line-height: 1.4; }
.hint b { color: var(--paper); }

/* ---------- Board ---------- */
.board-wrap { position: relative; width: 100%; aspect-ratio: 1; touch-action: none; user-select: none; -webkit-user-select: none; }
.board {
  position: absolute; inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: var(--gap, 8px);
  padding: var(--gap, 8px);
  background: var(--ink-2);
  border-radius: 20px;
}
.tile {
  background: var(--paper); color: var(--ink);
  border-radius: 14%;
  display: grid; place-items: center;
  font-weight: 800;
  font-size: calc(var(--size) * 0.5);
  box-shadow: 0 calc(var(--size) * 0.06) 0 var(--paper-2);
  transition: transform 0.1s, background 0.15s;
  position: relative;
}
.tile.qu { font-size: calc(var(--size) * 0.38); }
.tile.n::after, .tile.m::after, .tile.w::after, .tile.z::after {
  /* underline letters that look alike when the board rotates */
  content: ''; position: absolute; bottom: 16%; width: 26%; height: 5%; border-radius: 4px; background: currentColor; opacity: 0.35;
}
.tile.sel { background: var(--sun); box-shadow: 0 calc(var(--size) * 0.06) 0 #c99400; transform: scale(0.94); }
.tile.sel.first { background: var(--coral); color: white; box-shadow: 0 calc(var(--size) * 0.06) 0 #c94a2e; }
.board.flash-good .tile.sel { background: var(--good); box-shadow: 0 calc(var(--size) * 0.06) 0 #24a35f; }
.board.flash-dupe .tile.sel { background: var(--muted); box-shadow: none; }
.board.flash-bad .tile.sel { background: var(--bad); color: white; box-shadow: none; }
.board.flash-bad { animation: shake 0.3s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
.trail { position: absolute; inset: 0; pointer-events: none; }
.trail polyline { fill: none; stroke: rgba(20, 33, 61, 0.35); stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Play (phone) ---------- */
.hud { display: flex; align-items: center; gap: 12px; }
.timer { flex: 1; height: 14px; background: var(--ink-2); border-radius: 999px; overflow: hidden; }
.timer i { display: block; height: 100%; background: var(--teal); border-radius: 999px; transition: width 0.25s linear; }
.timer.low i { background: var(--coral); }
.clock { font-weight: 800; font-size: 22px; min-width: 56px; text-align: right; font-variant-numeric: tabular-nums; }
.score-pill { background: var(--ink-2); border-radius: 999px; padding: 6px 12px; font-weight: 800; }
.current {
  height: 56px; display: grid; place-items: center;
  font-size: 34px; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase;
}
.current.good { color: var(--good); }
.current.bad { color: var(--bad); }
.current.dupe { color: var(--muted); }
.found { display: flex; flex-wrap: wrap; gap: 6px; align-content: flex-start; min-height: 40px; }
.found span { background: var(--ink-2); padding: 4px 10px; border-radius: 8px; font-weight: 600; text-transform: uppercase; font-size: 14px; }
.found span.new { background: var(--good); color: var(--ink); }
.play-actions { display: flex; gap: 8px; justify-content: space-between; }

.countdown {
  position: fixed; inset: 0; display: grid; place-items: center; z-index: 20;
  background: rgba(20, 33, 61, 0.85);
  font-size: 30vmin; font-weight: 800; color: var(--sun);
}
.countdown span { animation: pop 0.9s ease-out; }
@keyframes pop { from { transform: scale(1.8); opacity: 0; } 30% { opacity: 1; } to { transform: scale(1); } }

/* ---------- Results ---------- */
.results-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.cols { display: grid; grid-template-columns: repeat(var(--cols), minmax(0, 1fr)); gap: 10px; }
.col { background: var(--ink-2); border-radius: 16px; padding: 12px; display: flex; flex-direction: column; gap: 8px; border-top: 6px solid var(--c); min-width: 0; }
.col-head { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.col-head h3 { font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.col-head .total { font-size: 30px; font-weight: 800; color: var(--c); font-variant-numeric: tabular-nums; }
.col-words { display: flex; flex-direction: column; gap: 4px; }
.w {
  display: flex; justify-content: space-between; gap: 6px;
  padding: 4px 8px; border-radius: 8px; background: var(--ink);
  text-transform: uppercase; font-weight: 700; font-size: 15px;
  animation: drop 0.35s ease-out;
}
.w em { font-style: normal; color: var(--sun); }
.w.shared { opacity: 0.55; }
.w.shared span { text-decoration: line-through; text-decoration-thickness: 2px; }
.w.shared em { color: var(--muted); }
@keyframes drop { from { transform: translateY(-10px) scale(1.06); opacity: 0; } }
.spotlight {
  min-height: 72px; display: grid; place-items: center; text-align: center;
  font-size: 40px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.05em;
}
.spotlight small { display: block; font-size: 15px; letter-spacing: 0; text-transform: none; color: var(--muted); font-weight: 600; }
.winner { text-align: center; font-size: 30px; font-weight: 800; color: var(--sun); animation: pop 0.6s; }
.missed { display: flex; flex-wrap: wrap; gap: 6px; }
.missed span { background: var(--ink); border-radius: 8px; padding: 4px 10px; text-transform: uppercase; font-weight: 700; font-size: 14px; color: var(--muted); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }

/* ---------- Big screen ---------- */
.screen { width: 100%; height: 100vh; padding: 3vmin 4vmin; display: flex; flex-direction: column; gap: 3vmin; overflow: hidden; }
.screen .logo span { width: 7vmin; height: 7vmin; font-size: 4.4vmin; border-radius: 1.6vmin; }
.screen-lobby { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 5vmin; align-items: center; }
.join-box { display: flex; flex-direction: column; gap: 2vmin; align-items: flex-start; }
.join-box .big-code { font-size: 16vmin; font-weight: 800; letter-spacing: 0.12em; color: var(--sun); line-height: 1; }
.join-box p { font-size: 3vmin; color: var(--muted); margin: 0; }
.join-box p b { color: var(--paper); }
.join-box img { width: 24vmin; height: 24vmin; border-radius: 2vmin; background: white; padding: 1vmin; }
.screen .players { gap: 1.5vmin; }
.screen .chip { font-size: 3vmin; padding: 1vmin 2vmin 1vmin 1vmin; }
.screen .chip .dot { width: 4vmin; height: 4vmin; font-size: 2vmin; }
.screen-play { flex: 1; display: grid; grid-template-columns: auto 1fr; gap: 5vmin; align-items: center; min-height: 0; }
.screen-play .board-wrap { height: 84vh; width: 84vh; }
.screen-side { display: flex; flex-direction: column; gap: 3vmin; }
.screen-side .clock { font-size: 14vmin; text-align: left; }
.screen-side .timer { height: 2vmin; }
.screen .stat { display: flex; justify-content: space-between; font-size: 3.6vmin; font-weight: 700; padding: 1.2vmin 2vmin; background: var(--ink-2); border-radius: 1.6vmin; border-left: 1vmin solid var(--c); }
.screen .settings-line { font-size: 2.6vmin; color: var(--muted); }
.screen .cols { flex: 1; min-height: 0; }
.screen .col { overflow: hidden; }
.screen .col-words { display: grid; grid-template-columns: repeat(auto-fill, minmax(18vmin, 1fr)); gap: 0.8vmin; align-content: start; }
.screen .w { font-size: 2.4vmin; padding: 0.6vmin 1vmin; }
.screen .col-head h3 { font-size: 3.4vmin; }
.screen .col-head .total { font-size: 6vmin; }
.screen .spotlight { font-size: 7vmin; min-height: 10vmin; }
.screen .spotlight small { font-size: 2.6vmin; }
.screen .winner { font-size: 6vmin; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--paper); color: var(--ink); padding: 12px 18px; border-radius: 12px; font-weight: 700; z-index: 30;
  box-shadow: 0 8px 30px rgba(0,0,0,0.4); max-width: calc(100% - 32px);
}
.conn { position: fixed; top: 8px; right: 8px; background: var(--bad); color: white; font-size: 12px; font-weight: 700; padding: 4px 8px; border-radius: 6px; z-index: 40; }
