:root {
  --bg: #0f1117;
  --bg2: #1a1d26;
  --bg3: #252834;
  --border: #2e3248;
  --text: #e2e4ef;
  --dim: #7880a0;
  --accent: #6c8eff;
  --accent2: #a78bfa;
  --success: #34d399;
  --warn: #fbbf24;
  --danger: #f87171;
  --tile-bg: #f0c040;
  --tile-text: #1a1000;
  --font: 'Segoe UI', system-ui, sans-serif;
  --mono: 'Menlo', 'Consolas', monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--font); min-height: 100vh; }

/* ── Setup ─────────────────────────────────────────────── */
.screen { width: 100%; }
.hidden { display: none !important; }

#setup-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
}
.setup-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 16px; padding: 40px 48px; width: 420px;
  display: flex; flex-direction: column; gap: 20px;
  box-shadow: 0 8px 40px rgba(0,0,0,.5);
}
.setup-card h1 { font-size: 2.4rem; color: var(--accent); letter-spacing: -.02em; }
.tagline { color: var(--dim); font-size: .95rem; margin-top: -12px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: .85rem; color: var(--dim); }
.form-group input {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 14px; color: var(--text);
  font-size: 1rem; outline: none;
}
.form-group input:focus { border-color: var(--accent); }

.time-opts, .player-opts, .lang-opts { display: flex; gap: 6px; }
.time-opt, .player-opt, .lang-opt {
  flex: 1; padding: 8px 4px; font-size: .82rem; font-weight: 600;
  background: var(--bg3); color: var(--dim);
  border: 1px solid var(--border); border-radius: 8px;
  cursor: pointer; transition: background .12s, color .12s, border-color .12s;
}
.time-opt:hover, .player-opt:hover, .lang-opt:hover { color: var(--text); border-color: var(--accent); }
.time-opt.selected, .player-opt.selected, .lang-opt.selected {
  background: var(--accent); color: #fff; border-color: var(--accent);
}

.stepper {
  display: flex; align-items: center; gap: 0;
  background: var(--bg3); border: 1px solid var(--border); border-radius: 8px;
  width: fit-content;
}
.stepper button {
  background: none; border: none; padding: 7px 14px;
  font-size: 1.1rem; font-weight: 700; color: var(--text);
  border-radius: 8px; line-height: 1;
}
.stepper button:disabled { opacity: .3; cursor: not-allowed; }
.stepper button:not(:disabled):hover { background: var(--bg2); }
#cpu-display {
  min-width: 32px; text-align: center; font-size: 1rem; font-weight: 700;
  color: var(--accent); padding: 0 4px;
}

input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 5px; border-radius: 3px;
  background: var(--bg3); outline: none; cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 18px; height: 18px;
  border-radius: 50%; background: var(--accent); cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: none;
}
.range-labels {
  display: flex; justify-content: space-between;
  font-size: .75rem; color: var(--dim); margin-top: 2px;
}

.bag-count { background: var(--bg3); color: var(--dim); }
.bag-count.bag-empty { background: rgba(248,113,113,.15); color: var(--danger); }

/* ── Buttons ───────────────────────────────────────────── */
button {
  border: none; border-radius: 8px; padding: 10px 18px;
  font-size: .9rem; font-weight: 600; cursor: pointer;
  transition: opacity .15s, transform .1s;
}
button:disabled { opacity: .35; cursor: not-allowed; }
button:not(:disabled):hover { opacity: .85; }
button:not(:disabled):active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }
.btn-success { background: var(--success); color: #0a1a10; }
.btn-return { background: rgba(251,191,36,.15); color: var(--warn); border: 1px solid rgba(251,191,36,.35); }
.grid-cell.board-selected { outline: 2px solid var(--warn); outline-offset: -2px; box-shadow: 0 0 10px rgba(251,191,36,.45); }

/* ── Drag and drop ──────────────────────────────────────── */
.tile-ghost {
  position: fixed; z-index: 9999; pointer-events: none;
  width: 36px; height: 36px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 1.05rem; color: var(--tile-text);
  background: linear-gradient(145deg,#ffe05a 0%,#f0c040 45%,#c89820 100%);
  box-shadow: 0 8px 22px rgba(0,0,0,.55), 0 4px 0 #9a7010;
  transform: scale(1.15) rotate(3deg);
  user-select: none;
}
.tile-ghost .tile-val {
  font-size: .42rem; position: absolute; bottom: 2px; right: 4px;
  font-weight: 700; color: var(--tile-text);
}
[data-dragging='1'] { opacity: 0.25; }
.is-dragging * { cursor: grabbing !important; }
.tile-rack .tile, .grid-cell.draft, .grid-cell.occupied { cursor: grab; }
.grid-cell.drag-over {
  background: rgba(108,142,255,.22) !important;
  box-shadow: inset 0 0 0 2px var(--accent);
}
.tile-rack.drag-over {
  box-shadow: 0 0 0 2px var(--accent), inset 0 0 14px rgba(108,142,255,.12);
  border-radius: 8px;
}
.btn-danger { background: var(--danger); color: #fff; }

/* ── Game layout ───────────────────────────────────────── */
#game-screen { display: flex; flex-direction: column; height: 100vh; }

.game-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px; background: var(--bg2); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 12px; }
.header-left h2 { font-size: 1.3rem; color: var(--accent); }
.header-right { display: flex; gap: 12px; }

.badge {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 6px; padding: 2px 10px; font-size: .78rem; color: var(--dim);
}
.turn-badge {
  background: var(--accent); color: #fff;
  border-radius: 6px; padding: 3px 10px; font-size: .78rem; font-weight: 700;
  animation: pulse 1.2s infinite;
}
@keyframes pulse { 0%,100% { opacity:1 } 50% { opacity:.6 } }

.condition-box {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px 12px; font-size: .82rem;
  display: flex; align-items: center; gap: 6px;
}
.condition-box .label { color: var(--dim); font-size: .75rem; }

/* ── Turn timer ────────────────────────────────────────── */
.turn-timer {
  font-family: var(--mono); font-size: .9rem; font-weight: 700;
  padding: 4px 14px; border-radius: 8px; min-width: 62px; text-align: center;
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
}
.turn-timer.warn {
  color: var(--warn); border-color: rgba(251,191,36,.4);
  animation: timer-warn 1.4s ease-in-out infinite;
}
.turn-timer.danger {
  color: var(--danger); border-color: rgba(248,113,113,.5);
  animation: timer-danger 0.45s ease-in-out infinite;
}
@keyframes timer-warn {
  0%, 100% { box-shadow: none; }
  50%       { box-shadow: 0 0 12px rgba(251,191,36,.4); }
}
@keyframes timer-danger {
  0%, 100% { box-shadow: 0 0 8px rgba(248,113,113,.4); transform: scale(1); }
  50%      { box-shadow: 0 0 22px rgba(248,113,113,.75); transform: scale(1.08); }
}

.game-body {
  display: grid;
  grid-template-columns: 220px 1fr 240px;
  gap: 0;
  flex: 1;
  overflow: hidden;
}

.panel {
  padding: 14px; overflow-y: auto;
  border-right: 1px solid var(--border);
}
.panel:last-child { border-right: none; }
.panel h3 { font-size: .85rem; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); margin-bottom: 10px; }

/* ── Players panel ─────────────────────────────────────── */
.player-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
}
.player-card.active { border-color: var(--accent); }
.player-card.winner { border-color: var(--success); }
.player-name { font-weight: 700; font-size: .9rem; margin-bottom: 4px; }
.player-meta { font-size: .78rem; color: var(--dim); margin-bottom: 8px; }
.player-score { font-size: 1.2rem; font-weight: 700; color: var(--accent); }
.player-words { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px; }
.word-chip {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 4px; padding: 1px 6px; font-size: .75rem; font-family: var(--mono);
}

/* ── Main panel: grid ──────────────────────────────────── */
.panel-main { display: flex; flex-direction: column; gap: 12px; }

.section-header { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.section-header h3 { margin-bottom: 0; }

.my-grid-section { flex-shrink: 0; }

.grid-canvas {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px;
  min-height: 200px; overflow: auto;
  position: relative;
}
.grid-inner { display: inline-grid; gap: 2px; }
.grid-cell {
  width: 36px; height: 36px; border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem; font-weight: 700; font-family: var(--mono);
  cursor: pointer; transition: background .15s;
}
.grid-cell.occupied {
  background: linear-gradient(145deg, #fce068 0%, #f0c030 50%, #d4a018 100%);
  color: var(--tile-text);
  border: none;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.38),
    inset 0 -1px 0 rgba(0,0,0,.18),
    0 2px 0 #8a6010,
    0 3px 5px rgba(0,0,0,.35);
}
.grid-cell.draft {
  background: linear-gradient(145deg, #a8c8ff 0%, #88b0ff 50%, #6090e0 100%);
  color: #0a1533;
  border: none;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.3),
    inset 0 -1px 0 rgba(0,0,0,.15),
    0 2px 0 #2040a0,
    0 3px 5px rgba(0,0,0,.35);
}
.grid-cell.empty {
  background: var(--bg3); border: 1px dashed var(--border);
}
.grid-cell.empty:hover { background: #2a2e40; border-color: var(--accent); }

/* ── Active rules — felt playmat ───────────────────────── */
.active-rules-section {
  flex-shrink: 0;
  background: radial-gradient(ellipse at 40% 35%, #163d20 0%, #0f2c17 55%, #0a2012 100%);
  border-radius: 14px;
  padding: 14px;
  border: 1px solid #081608;
  box-shadow:
    inset 0 0 40px rgba(0,0,0,.35),
    inset 0 1px 0 rgba(255,255,255,.03),
    0 3px 10px rgba(0,0,0,.4);
}
.active-rules-section h3 { color: rgba(180,220,190,.55); }

#active-rules {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
}

.active-rule-card {
  background: linear-gradient(160deg, #22263a 0%, #1a1d2e 60%, #161924 100%);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 12px; overflow: hidden;
  box-shadow:
    0 2px 0 rgba(0,0,0,.6),
    0 6px 18px rgba(0,0,0,.6),
    inset 0 1px 0 rgba(255,255,255,.06);
  display: flex; flex-direction: column;
}
.active-rule-card.unset { opacity: .5; }

.active-rule-header {
  padding: 7px 12px;
  font-size: .65rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .09em;
}
.active-rule-card.win_condition .active-rule-header {
  background: rgba(52,211,153,.15); color: var(--success);
  border-bottom: 1px solid rgba(52,211,153,.25);
}
.active-rule-card.scoring .active-rule-header {
  background: rgba(251,191,36,.12); color: var(--warn);
  border-bottom: 1px solid rgba(251,191,36,.2);
}
.active-rule-card.unset .active-rule-header {
  background: transparent; color: var(--dim);
  border-bottom: 1px solid var(--border);
}

.active-rule-body { padding: 10px 12px; flex: 1; }
.active-rule-name { font-weight: 700; font-size: .88rem; margin-bottom: 4px; }
.active-rule-desc { font-size: .76rem; color: var(--dim); line-height: 1.35; }
.active-rule-meta { font-size: .7rem; color: var(--dim); font-style: italic; margin-top: 6px; }

/* ── Tile rack — wooden groove ─────────────────────────── */
.tile-rack-section { flex-shrink: 0; }
.tile-rack {
  display: flex; flex-wrap: wrap; gap: 7px;
  min-height: 66px;
  background: linear-gradient(180deg,
    #1a0d03 0%,
    #2c1808 6%,
    #3e2610 18%,
    #4a2e14 38%,
    #3e2610 62%,
    #2c1808 82%,
    #1a0d03 100%);
  border-radius: 9px;
  padding: 14px 13px 11px;
  border: 1.5px solid #110800;
  box-shadow:
    inset 0 5px 14px rgba(0,0,0,.75),
    inset 0 -2px 6px rgba(255,180,80,.05),
    0 2px 0 rgba(0,0,0,.7),
    0 4px 10px rgba(0,0,0,.45);
}

/* ── Tiles — 3-D bevel ─────────────────────────────────── */
.tile {
  width: 42px; height: 42px;
  background: linear-gradient(145deg, #fce068 0%, #f0c030 48%, #d4a018 100%);
  color: var(--tile-text);
  border-radius: 5px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem; font-weight: 900; font-family: var(--mono);
  cursor: pointer;
  transition: transform .12s, box-shadow .12s;
  user-select: none;
  position: relative;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.55),
    inset 1px 0 0 rgba(255,255,255,.22),
    inset 0 -2px 0 rgba(0,0,0,.22),
    inset -1px 0 0 rgba(0,0,0,.12),
    0 4px 0 #9a7010,
    0 5px 8px rgba(0,0,0,.55);
}
.tile:hover {
  transform: translateY(-4px);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.55),
    inset 1px 0 0 rgba(255,255,255,.22),
    inset 0 -2px 0 rgba(0,0,0,.22),
    inset -1px 0 0 rgba(0,0,0,.12),
    0 6px 0 #9a7010,
    0 8px 14px rgba(0,0,0,.65);
}
.tile.selected {
  background: linear-gradient(145deg, #8ab0ff 0%, #6c8eff 48%, #4a6edf 100%);
  color: #fff;
  transform: translateY(-4px);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,.4),
    inset 0 -2px 0 rgba(0,0,0,.2),
    0 5px 0 #2a4aaf,
    0 7px 14px rgba(108,142,255,.5);
}
.tile .tile-val {
  position: absolute; bottom: 2px; right: 4px;
  font-size: .5rem; font-weight: 400; opacity: .65;
}
.tile.used { opacity: .3; cursor: default; }

/* ── Action bar ────────────────────────────────────────── */
.action-bar { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── Hand cards — felt playmat ─────────────────────────── */
.hand-cards-section {
  flex-shrink: 0;
  background: radial-gradient(ellipse at 60% 40%, #16391f 0%, #0f2916 55%, #0a1f10 100%);
  border-radius: 14px;
  padding: 14px;
  border: 1px solid #071208;
  box-shadow:
    inset 0 0 40px rgba(0,0,0,.35),
    inset 0 1px 0 rgba(255,255,255,.03),
    0 3px 10px rgba(0,0,0,.4);
}
.hand-cards-section h3 { color: rgba(180,220,190,.55); }

.card-hand-row {
  display: flex; flex-direction: row; flex-wrap: nowrap;
  gap: 8px; overflow-x: auto; padding-bottom: 6px;
  min-height: 52px;
}
.card-hand-row::-webkit-scrollbar { height: 4px; }
.card-hand-row::-webkit-scrollbar-track { background: transparent; }
.card-hand-row::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.card-item {
  flex-shrink: 0; width: 118px; min-height: 152px;
  background: linear-gradient(160deg, #22263a 0%, #1a1d2e 60%, #161924 100%);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 12px; overflow: hidden;
  cursor: pointer;
  transition: transform .15s, box-shadow .15s;
  box-shadow:
    0 2px 0 rgba(0,0,0,.6),
    0 5px 16px rgba(0,0,0,.55),
    inset 0 1px 0 rgba(255,255,255,.07);
  display: flex; flex-direction: column;
}
.card-item:not(.locked):hover {
  transform: translateY(-6px) rotate(-1deg);
  box-shadow:
    0 4px 0 rgba(0,0,0,.5),
    0 14px 30px rgba(0,0,0,.7),
    inset 0 1px 0 rgba(255,255,255,.1);
}

.card-header {
  padding: 7px 9px;
  font-size: .62rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .08em;
  flex-shrink: 0;
}
.card-header.win_condition {
  background: rgba(52,211,153,.15); color: var(--success);
  border-bottom: 1px solid rgba(52,211,153,.2);
}
.card-header.scoring {
  background: rgba(251,191,36,.12); color: var(--warn);
  border-bottom: 1px solid rgba(251,191,36,.18);
}
.card-header.chaos {
  background: rgba(248,113,113,.12); color: var(--danger);
  border-bottom: 1px solid rgba(248,113,113,.18);
}

.card-body { flex: 1; padding: 8px 9px; display: flex; flex-direction: column; gap: 4px; }
.card-name { font-weight: 700; font-size: .82rem; line-height: 1.2; }
.card-desc { font-size: .7rem; color: var(--dim); line-height: 1.3; }
.card-item.locked { cursor: default; }
.cards-locked-msg { font-size: .8rem; font-style: italic; }

/* ── Event log ─────────────────────────────────────────── */
.panel-log {
  display: flex; flex-direction: column;
  overflow: hidden !important; /* override .panel's overflow-y: auto */
}
.panel-log h3 { flex-shrink: 0; }
#event-log {
  flex: 1; overflow-y: auto; background: var(--bg2);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px; display: flex; flex-direction: column; gap: 3px;
}
.log-entry {
  font-size: .78rem; color: var(--dim); padding: 3px 0;
  border-bottom: 1px solid rgba(46,50,72,.6);
  line-height: 1.35;
}
.log-entry:first-child { color: var(--text); border-bottom: none; }
.log-entry:last-child { border-bottom: none; }

.log-entry.ev-rule  { color: var(--success); }
.log-entry.ev-score { color: var(--warn); }
.log-entry.ev-peel  { color: var(--accent); font-weight: 600; }
.log-entry.ev-card  { color: var(--accent2); }
.log-entry.ev-chaos { color: var(--danger); }
.log-entry.ev-win     { color: var(--success); font-weight: 700; }
.log-entry.ev-timeout { color: #fb923c; font-style: italic; }

/* ── Modal ─────────────────────────────────────────────── */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-box {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; min-width: 300px;
  display: flex; flex-direction: column; gap: 14px;
}
.target-btn {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 14px; text-align: left;
  font-size: .9rem; font-weight: 600;
}
.target-btn:hover { border-color: var(--accent); }

/* ── Overlay ───────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.8);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.overlay-box {
  background: var(--bg2); border: 1px solid var(--success);
  border-radius: 16px; padding: 48px; text-align: center;
  display: flex; flex-direction: column; gap: 20px;
}
.overlay-box h2 { font-size: 1.8rem; color: var(--success); }

/* ── Misc ──────────────────────────────────────────────── */
.error { color: var(--danger); font-size: .82rem; margin-top: 4px; }
.dim { color: var(--dim); font-size: .82rem; }

/* Mini grid for opponent display */
.mini-grid { display: inline-grid; gap: 1px; margin-top: 6px; }
.mini-cell {
  width: 16px; height: 16px; border-radius: 2px;
  background: var(--tile-bg); color: var(--tile-text);
  font-size: .55rem; font-weight: 700; display: flex;
  align-items: center; justify-content: center;
}
.mini-empty { background: var(--bg3); }

/* ── Score Rankings (end-game overlay) ────────────────── */
.score-rankings {
  display: flex; flex-direction: column; gap: 6px;
  min-width: 260px; text-align: left;
}
.rank-row {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 14px; border-radius: 8px;
  background: var(--bg3); border: 1px solid var(--border);
}
.rank-row.rank-winner { border-color: var(--warn); background: rgba(251,191,36,.08); }
.rank-row.rank-you    { border-color: var(--accent); }
.rank-pos  { font-size: 1.2rem; line-height: 1; min-width: 28px; }
.rank-name { flex: 1; font-weight: 600; font-size: .95rem; }
.rank-name em { font-style: normal; color: var(--dim); font-weight: 400; font-size: .82rem; }
.rank-score { font-family: var(--mono); font-size: .95rem; font-weight: 700; color: var(--accent2); }

/* ── Fun Stats (end-game overlay) ─────────────────────── */
.fun-stats { text-align: left; }
.fun-stats h3 { color: var(--accent); margin-bottom: 8px; }
.fun-stats-headlines {
  list-style: none; padding: 0; margin: 0 0 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.fun-stats-headlines li {
  font-size: .88rem; color: var(--fg);
  padding: 6px 10px; background: var(--bg3);
  border-radius: 6px;
}
.stats-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
}
.stat-item {
  background: var(--bg3); border-radius: 8px;
  padding: 10px 14px;
}
.stat-label { font-size: .75rem; color: var(--dim); margin-bottom: 2px; }
.stat-value { font-size: 1rem; font-weight: 700; color: var(--fg); }

/* ── Home screen ───────────────────────────────────────── */
#home-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: radial-gradient(ellipse at 50% 30%, rgba(108,142,255,.07) 0%, transparent 65%);
}
.home-layout {
  display: flex; flex-direction: column; align-items: center; gap: 52px;
}
.home-hero { text-align: center; }
.home-tiles-row {
  display: flex; gap: 6px; justify-content: center; margin-bottom: 20px;
}
.home-tile {
  width: 34px; height: 34px;
  background: linear-gradient(145deg, #fce068 0%, #f0c030 48%, #c89820 100%);
  color: var(--tile-text);
  border-radius: 5px;
  display: flex; align-items: center; justify-content: center;
  font-size: .95rem; font-weight: 900; font-family: var(--mono);
  box-shadow: 0 3px 0 #8a6010, 0 4px 8px rgba(0,0,0,.45);
}
.home-title {
  font-size: 4rem; font-weight: 900; color: var(--accent);
  letter-spacing: -.04em; line-height: 1;
}
.home-tagline {
  color: var(--dim); font-size: 1rem; margin-top: 10px;
  letter-spacing: .01em;
}
.home-nav { display: flex; flex-direction: column; gap: 10px; width: 220px; }
.home-btn { width: 100%; padding: 13px; font-size: 1rem; }

/* ── Options & setup screen shared layout ───────────────── */
#options-screen, #setup-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
}

/* nav-header: back button + title row at the top of setup/options cards */
.nav-header {
  display: flex; align-items: center; gap: 14px;
  margin-bottom: 4px;
}
.nav-header h2 { font-size: 1.4rem; }

.btn-back {
  background: none; border: none; padding: 0;
  color: var(--dim); font-size: .9rem; cursor: pointer;
  display: flex; align-items: center; gap: 5px;
  font-family: var(--font); font-weight: 500;
  flex-shrink: 0;
}
.btn-back:hover { color: var(--text); }

.btn-link {
  background: none; border: none; padding: 0;
  color: var(--accent); font-size: .82rem;
  cursor: pointer; font-family: var(--font); font-weight: 600;
  white-space: nowrap;
}
.btn-link:hover { opacity: .7; }

.options-summary-row {
  display: flex; align-items: center; gap: 8px;
}
.options-summary-row .dim { flex: 1; font-size: .82rem; }
.small { font-size: .82rem; }

/* ── Help screen ───────────────────────────────────────── */
#help-screen {
  min-height: 100vh; overflow-y: auto;
  display: flex; justify-content: center; align-items: flex-start;
  padding: 40px 20px 80px;
}
.help-layout { width: 100%; max-width: 660px; }
.help-card {
  display: flex; flex-direction: column; gap: 8px;
}
.help-card > .btn-back { margin-bottom: 6px; }
.help-card > h1 {
  font-size: 2rem; color: var(--accent);
  letter-spacing: -.02em; margin-bottom: 12px;
}

.help-section {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 12px; padding: 18px 20px;
  display: flex; flex-direction: column; gap: 8px;
}
.help-section h2 {
  font-size: 1rem; color: var(--text);
  font-weight: 700; margin-bottom: 2px;
  padding-bottom: 6px; border-bottom: 1px solid var(--border);
}
.help-section p, .help-section li {
  color: var(--dim); line-height: 1.65; font-size: .88rem;
}
.help-section strong { color: var(--text); }
.help-section em { color: var(--accent2); font-style: normal; }
.help-section ul, .help-section ol {
  padding-left: 20px; display: flex; flex-direction: column; gap: 5px;
}

/* ── Stats screen ──────────────────────────────────────── */
.stats-section {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 12px; padding: 18px 20px;
  display: flex; flex-direction: column; gap: 12px;
}
.stats-section-title {
  font-size: 1.05rem; font-weight: 700; color: var(--text);
}
.stats-section-sub {
  font-size: .82rem; color: var(--dim); margin-top: -6px;
}

/* Leaderboard + top-scores rows */
.lb-row {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  grid-template-rows: auto auto;
  column-gap: 10px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.lb-row:last-child { border-bottom: none; }
.lb-pos  { font-size: 1.1rem; text-align: center; }
.lb-name { font-weight: 700; font-size: .95rem; }
.lb-wins { font-family: var(--mono); font-weight: 700; font-size: .95rem;
           color: var(--accent); white-space: nowrap; }
.score-num { color: var(--warn); }
.lb-meta {
  grid-column: 2 / -1;
  font-size: .77rem; color: var(--dim);
  margin-top: 2px;
}

/* All-time fun stats grid */
.alltime-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}
.alltime-card {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 16px;
  display: flex; flex-direction: column; gap: 4px;
}
.alltime-label {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--dim); font-weight: 600;
}
.alltime-value {
  font-size: 1rem; font-weight: 700; color: var(--accent2);
  line-height: 1.2;
}
.alltime-sub {
  font-size: .74rem; color: var(--dim);
}
.game-ender-note {
  font-size: .88rem; color: var(--dim); margin: -4px 0 12px;
  text-align: center; font-style: italic;
}

/* ── Lobby screens ────────────────────────────────────────────────────────── */
.invite-block {
  text-align: center;
  padding: 20px 0 24px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}
.invite-label {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--dim); font-weight: 600; margin-bottom: 8px;
}
.invite-code {
  font-size: 2.8rem; font-family: monospace; font-weight: 700;
  letter-spacing: 0.3em; color: var(--accent2);
  margin: 4px 0 14px;
}
.btn-copy {
  background: var(--bg3); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 7px 16px;
  cursor: pointer; font-size: .9rem;
  transition: background .15s;
}
.btn-copy:hover { background: var(--bg4, #333); }
.copy-confirm {
  font-size: .82rem; color: var(--accent); margin-top: 6px;
  font-weight: 600;
}
.waiting-players-list {
  list-style: none; padding: 0; margin: 8px 0 4px;
}
.waiting-players-list li {
  padding: 6px 0; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
}
#waiting-cpu-info {
  margin: 8px 0 0; color: var(--dim);
}

/* ── Tab bar (base — hidden on desktop) ─────────────────── */
.tab-bar {
  display: none;
  height: 56px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  align-items: stretch;
  flex-shrink: 0;
}
.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  border-radius: 0;
  color: var(--dim);
  font-size: .65rem;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  padding: 0;
  cursor: pointer;
  position: relative;
  transition: color .15s;
}
.tab-btn:not(:disabled):hover { opacity: 1; color: var(--text); }
.tab-btn.tab-active { color: var(--accent); }
.tab-btn.tab-active::before {
  content: '';
  position: absolute;
  top: 0; left: 20%; right: 20%;
  height: 2px;
  background: var(--accent);
  border-radius: 0 0 2px 2px;
}
.tab-icon { font-size: 1.3rem; line-height: 1; }
.tab-dot {
  position: absolute;
  top: 8px;
  right: calc(50% - 20px);
  width: 7px; height: 7px;
  background: var(--danger);
  border-radius: 50%;
}

/* ── Responsive: ≤900px ─────────────────────────────────── */
@media (max-width: 900px) {

  /* Setup / lobby / options cards */
  .setup-card {
    width: calc(100vw - 32px);
    max-width: 420px;
    padding: 28px 24px;
  }

  /* Help and stats screens */
  #help-screen,
  #stats-screen {
    padding: 20px 16px 80px;
    align-items: flex-start;
  }

  /* Home screen title */
  .home-title { font-size: 2.6rem; }
  .home-nav { width: 100%; max-width: 260px; }

  /* Game header — tighter, allow wrapping */
  .game-header {
    padding: 8px 12px;
    flex-wrap: wrap;
    gap: 6px;
  }

  /* Switch game-body from 3-col grid to a panel stack */
  .game-body {
    display: block;
    position: relative;
  }

  /* All panels: absolutely fill game-body; hidden by default */
  .panel {
    position: absolute;
    inset: 0;
    visibility: hidden;
    pointer-events: none;
    border-right: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .panel.tab-active {
    visibility: visible;
    pointer-events: auto;
  }

  /* Show the tab bar */
  .tab-bar { display: flex; }

  /* Larger touch targets for tiles and cells */
  .tile { width: 46px; height: 46px; font-size: 1.15rem; }
  .grid-cell { width: 40px; height: 40px; font-size: 1rem; }
  .tile-ghost { width: 42px; height: 42px; }

  /* Action bar: 2×2 grid */
  .action-bar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  /* Active rules: single column */
  #active-rules { grid-template-columns: 1fr; }

  /* Win overlay: fit on mobile */
  .overlay-box {
    padding: 28px 20px;
    margin: 16px;
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    border-radius: 12px;
  }

  /* Card modal: fit on mobile */
  .modal-box {
    margin: 16px;
    min-width: 0;
    width: calc(100vw - 32px);
  }

  /* All-time stats */
  .alltime-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ── Very narrow phones ──────────────────────────────────── */
@media (max-width: 420px) {
  .home-title { font-size: 2rem; }
  .home-tile { width: 28px; height: 28px; font-size: .8rem; }
  .alltime-grid { grid-template-columns: 1fr; }
  .header-left h2 { font-size: 1.1rem; }
}
