*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0a0a0f;
  --surface: rgba(255,255,255,0.04);
  --border: rgba(255,255,255,0.08);
  --accent: #a07fff;
  --accent2: #ff7fa0;
  --text: #ffffff;
  --muted: #555;
  --gold: #ffd700;
  --silver: #aaa;
  --bronze: #cd7f32;
  --tile-colors: #a07fff, #ff7fa0, #7fffc0, #7fd8ff, #ffd87f, #e07fff;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none;
}

/* ── Screens ── */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow: hidden;
}
.hidden { display: none !important; }

/* ── Home ── */
.home-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}
.title {
  font-size: clamp(36px, 10vw, 56px);
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: 6px;
  text-align: center;
  text-shadow: 0 0 40px rgba(160,127,255,0.7);
}
.title-accent { color: var(--accent); }
.tagline {
  font-size: 11px;
  letter-spacing: 3px;
  color: var(--muted);
}
.home-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 260px;
  margin-top: 12px;
}

/* ── Buttons ── */
.btn-primary {
  background: linear-gradient(135deg, #4a2aff, var(--accent));
  border: none;
  border-radius: 12px;
  padding: 16px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 3px;
  color: #fff;
  cursor: pointer;
  width: 100%;
  min-height: 52px;
  box-shadow: 0 4px 20px rgba(160,127,255,0.3);
  transition: transform 0.1s, box-shadow 0.1s;
}
.btn-primary:active { transform: scale(0.97); box-shadow: none; }

.btn-ghost {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 2px;
  color: var(--silver);
  cursor: pointer;
  width: 100%;
  min-height: 48px;
  transition: background 0.1s;
}
.btn-ghost:active { background: rgba(255,255,255,0.09); }

.btn-ghost-sm {
  background: none;
  border: none;
  font-size: 12px;
  letter-spacing: 2px;
  color: var(--muted);
  cursor: pointer;
  padding: 8px;
  margin-top: -4px;
}

/* ── Game HUD ── */
#screen-game {
  justify-content: flex-start;
  padding: 0;
}

.game-wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
  flex: 1;
  max-width: 540px;
  min-height: 0;
}
.hud {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 14px 16px 10px;
  background: rgba(255,255,255,0.03);
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.hud-cell { min-width: 48px; }
.hud-right { text-align: right; }
.hud-label { font-size: 9px; letter-spacing: 2px; color: var(--muted); }
.hud-value { font-size: 22px; font-weight: 900; }
.accent { color: var(--accent); }

.hud-timer { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.timer-bar-track {
  width: 100%;
  height: 6px;
  background: rgba(255,255,255,0.08);
  border-radius: 3px;
  overflow: hidden;
}
.timer-bar {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 3px;
  transition: width 0.1s linear, box-shadow 0.1s;
}
.timer-bar.low {
  background: var(--accent2);
  box-shadow: 0 0 10px rgba(255,127,160,0.6);
}
.timer-text { font-size: 20px; font-weight: 900; letter-spacing: 1px; }

/* ── Grid ── */
.grid {
  display: grid;
  gap: 10px;
  padding: 14px;
  width: 100%;
  flex: 1;
  align-content: center;
}

.tile {
  aspect-ratio: 1;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(14px, 4vw, 24px);
  font-weight: 900;
  cursor: pointer;
  transition: transform 0.08s, opacity 0.2s;
  min-height: 44px;
  min-width: 44px;
  position: relative;
  overflow: hidden;
}
.tile:active { transform: scale(0.93); }
.tile.tapped {
  opacity: 0.25;
  cursor: default;
  text-decoration: line-through;
}

/* Neon color cycling — 6 colors mod tile index */
.tile[data-color="0"] { background: rgba(100,80,255,0.18); border: 1.5px solid rgba(100,80,255,0.5); color: #a07fff; box-shadow: inset 0 0 12px rgba(100,80,255,0.1), 0 0 10px rgba(100,80,255,0.15); }
.tile[data-color="1"] { background: rgba(255,80,120,0.18); border: 1.5px solid rgba(255,80,120,0.5); color: #ff7fa0; box-shadow: inset 0 0 12px rgba(255,80,120,0.1), 0 0 10px rgba(255,80,120,0.15); }
.tile[data-color="2"] { background: rgba(80,255,160,0.15); border: 1.5px solid rgba(80,255,160,0.4); color: #7fffc0; box-shadow: inset 0 0 12px rgba(80,255,160,0.08), 0 0 10px rgba(80,255,160,0.12); }
.tile[data-color="3"] { background: rgba(80,200,255,0.15); border: 1.5px solid rgba(80,200,255,0.4); color: #7fd8ff; box-shadow: inset 0 0 12px rgba(80,200,255,0.08), 0 0 10px rgba(80,200,255,0.12); }
.tile[data-color="4"] { background: rgba(255,210,80,0.15); border: 1.5px solid rgba(255,210,80,0.4); color: #ffd87f; box-shadow: inset 0 0 12px rgba(255,210,80,0.08), 0 0 10px rgba(255,210,80,0.12); }
.tile[data-color="5"] { background: rgba(220,80,255,0.18); border: 1.5px solid rgba(220,80,255,0.5); color: #e07fff; box-shadow: inset 0 0 12px rgba(220,80,255,0.1), 0 0 10px rgba(220,80,255,0.15); }

/* ── Game Over ── */
.gameover-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: 100%;
  max-width: 300px;
}
.gameover-header { font-size: 14px; letter-spacing: 4px; color: var(--accent2); font-weight: 700; }
.gameover-score-label { font-size: 10px; letter-spacing: 2px; color: var(--muted); }
.gameover-score {
  font-size: clamp(64px, 20vw, 96px);
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 0 50px rgba(160,127,255,0.8);
}
.gameover-sublabel { font-size: 12px; color: var(--accent); letter-spacing: 2px; }
.nickname-section { width: 100%; margin-top: 8px; }
.nickname-label { font-size: 9px; letter-spacing: 2px; color: var(--muted); text-align: center; margin-bottom: 8px; }
.nickname-input {
  width: 100%;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 10px;
  padding: 14px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 3px;
  color: #fff;
  text-align: center;
  text-transform: uppercase;
  outline: none;
  -webkit-appearance: none;
}
.nickname-input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(160,127,255,0.2); }

/* ── Leaderboard ── */
.leaderboard-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 340px;
  max-height: 100vh;
  overflow-y: auto;
  padding: 20px 0;
}
.leaderboard-title { font-size: 14px; font-weight: 900; letter-spacing: 4px; }
.leaderboard-list { width: 100%; display: flex; flex-direction: column; gap: 6px; }
.lb-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
}
.lb-row.rank-1 { background: rgba(255,215,0,0.07); border-color: rgba(255,215,0,0.25); }
.lb-row.rank-2 { background: rgba(192,192,192,0.05); border-color: rgba(192,192,192,0.18); }
.lb-row.rank-3 { background: rgba(205,127,50,0.06); border-color: rgba(205,127,50,0.18); }
.lb-row.player { border-color: rgba(160,127,255,0.5); background: rgba(160,127,255,0.07); }
.lb-rank { font-size: 15px; font-weight: 900; width: 22px; color: var(--muted); }
.rank-1 .lb-rank { color: var(--gold); }
.rank-2 .lb-rank { color: var(--silver); }
.rank-3 .lb-rank { color: var(--bronze); }
.lb-name { flex: 1; font-size: 13px; letter-spacing: 1px; }
.lb-score { font-size: 15px; font-weight: 900; color: var(--muted); }
.rank-1 .lb-score { color: var(--gold); }
.rank-2 .lb-score { color: var(--silver); }
.rank-3 .lb-score { color: var(--bronze); }
.player .lb-score { color: var(--accent); }

/* +5s bonus toast */
.timer-bonus {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 52px;
  font-weight: 900;
  color: #7fffc0;
  text-shadow: 0 0 30px rgba(80,255,160,0.9);
  pointer-events: none;
  z-index: 100;
  letter-spacing: 2px;
  animation: bonusPop 0.9s ease-out forwards;
}

@keyframes bonusPop {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(0.4); }
  25%  { opacity: 1; transform: translate(-50%, -50%) scale(1.25); }
  55%  { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -65%) scale(1); }
}

/* Dynamic font scaling for large grids */
.grid[data-cols="3"] .tile { font-size: clamp(20px, 7vw, 32px); }
.grid[data-cols="4"] .tile { font-size: clamp(16px, 5.5vw, 26px); }
.grid[data-cols="5"] .tile { font-size: clamp(13px, 4.5vw, 20px); }
.grid[data-cols="6"] .tile { font-size: clamp(11px, 3.5vw, 17px); }
.grid[data-cols="7"] .tile { font-size: clamp(9px, 3vw, 14px); border-radius: 6px; }

@media (max-height: 700px) {
  .hud { padding: 10px 14px 8px; }
  .hud-value { font-size: 18px; }
  .grid { padding: 10px; gap: 7px; }
}
