:root {
  --cell: clamp(34px, 10vw, 56px);
  --gap: 4px;
  --board-pad: 10px;
  --tray-cell: clamp(20px, 6vw, 30px);
  --tray-gap: 3px;
  --bg-0: #0e1530;
  --bg-1: #1b2541;
  --bg-2: #2a3760;
  --grid-cell: #232f5e;       /* lighter square */
  --grid-cell-2: #1a2348;     /* darker square */
  --grid-border: rgba(255, 255, 255, 0.06);
  --ghost-ok: rgba(255, 255, 255, 0.22);
  --ghost-ng: rgba(255, 80, 100, 0.28);
  --text: #f4f7ff;
  --text-dim: #a8b3d6;
  --accent: #ffd166;
  --danger: #ff5c7a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  background:
    radial-gradient(1200px 700px at 20% -10%, #2a3a72 0%, transparent 60%),
    radial-gradient(900px 700px at 110% 10%, #4a2a72 0%, transparent 55%),
    linear-gradient(180deg, var(--bg-0) 0%, var(--bg-1) 100%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans",
    "Yu Gothic UI", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: contain;
  touch-action: manipulation;
}

body {
  overflow: hidden;
}

.app {
  min-height: 100dvh;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: max(14px, env(safe-area-inset-top)) 12px
          max(14px, env(safe-area-inset-bottom)) 12px;
}

/* ---------- HUD ---------- */
.hud {
  width: 100%;
  max-width: 520px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  padding: 10px 14px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0.02));
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  backdrop-filter: blur(6px);
}

.hud__score, .hud__best, .hud__streak {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.hud__label {
  font-size: 11px;
  letter-spacing: 0.16em;
  color: var(--text-dim);
}

.hud__level {
  font-size: 10px;
  letter-spacing: 0.1em;
  color: var(--accent);
  font-weight: 800;
}

#level {
  display: inline-block;
  font-variant-numeric: tabular-nums;
}
#level.is-up {
  animation: level-pop 1100ms cubic-bezier(.2, .7, .2, 1);
}
@keyframes level-pop {
  0%   { transform: scale(1); }
  30%  { transform: scale(2.0); color: #fff; text-shadow: 0 0 14px var(--accent); }
  100% { transform: scale(1); }
}

.combo-toast.is-level {
  color: #6fdcff;
  font-size: clamp(26px, 7vw, 36px);
  text-shadow:
    0 3px 0 rgba(0, 0, 0, 0.4),
    0 0 22px rgba(111, 220, 255, 0.85),
    0 0 44px rgba(111, 220, 255, 0.45);
  letter-spacing: 0.06em;
}

.hud__value {
  font-size: clamp(20px, 5.5vw, 28px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
}

#streak.is-hot { color: var(--accent); }

/* ---------- Board ---------- */
.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
}

/* ---------- BoB (motif character, per /projects/ brand rule) ----------
   Blue block-shaped little guy who watches the board from the top-right.
   Reacts to clears: bounces (is-cheer), grins (is-hyped), sleeps (is-sleep on game over).
   Streak tiers swap his skin color; density doesn't touch him (he's the witness, not the risk).
   At full FEVER he stops being just the witness: is-charged telegraphs the 大当たり, then
   is-fever turns him into the detonator that blows the board apart. */
.bro {
  position: absolute;
  top: -14px;
  /* Cap at 4px from viewport edge so the character stays visible on small
     phones (iPhone SE 1st gen 320px would otherwise clip 10px off-screen). */
  right: max(4px, calc(50% - (var(--cell) * 4) - var(--gap) * 3 - 22px));
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(160deg, #6cc8ff 0%, #3a78ff 100%);
  box-shadow:
    inset 0 -4px 0 rgba(0, 0, 0, 0.26),
    inset 0 2px 0 rgba(255, 255, 255, 0.4),
    0 6px 14px rgba(0, 0, 0, 0.4);
  z-index: 3;
  transition: transform 200ms ease, background 200ms ease;
  pointer-events: none;
}

.bro__eye {
  position: absolute;
  top: 13px;
  width: 5px;
  height: 7px;
  background: #0e1530;
  border-radius: 50%;
  animation: bro-blink 5.4s infinite;
}
.bro__eye--l { left: 9px; }
.bro__eye--r { right: 9px; }

.bro__eye span {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: radial-gradient(circle at 35% 30%, #fff 0 1.6px, transparent 2px);
}

.bro__mouth {
  position: absolute;
  bottom: 10px;
  left: 50%;
  width: 8px;
  height: 3px;
  background: #0e1530;
  border-radius: 0 0 5px 5px;
  transform: translateX(-50%);
  transition: transform 200ms ease, width 200ms ease, height 200ms ease, border-radius 200ms ease;
}

@keyframes bro-blink {
  0%, 92%, 100% { transform: scaleY(1); }
  94%, 98% { transform: scaleY(0.1); }
}

.bro.is-cheer { animation: bro-bounce 540ms ease; }
.bro.is-hyped {
  background: linear-gradient(160deg, #ffd166 0%, #ff8a1e 100%);
}
.bro.is-hyped .bro__eye { background: #2b1a00; transform: scaleX(1.3); }
.bro.is-hyped .bro__mouth { width: 14px; height: 6px; border-radius: 0 0 8px 8px; }
.bro.is-sleep .bro__eye { animation: none; transform: scaleY(0.12); }
.bro.is-sleep .bro__mouth { transform: translateX(-50%) rotate(180deg); }

@keyframes bro-bounce {
  0%   { transform: translateY(0) scale(1); }
  35%  { transform: translateY(-10px) scale(1.15, 0.9); }
  60%  { transform: translateY(0) scale(0.94, 1.1); }
  100% { transform: translateY(0) scale(1); }
}

/* ---------- Score pop (+N float-up, labeled stack) ---------- */
.score-pop {
  position: fixed;
  font-weight: 900;
  letter-spacing: 0.04em;
  pointer-events: none;
  z-index: 950;
  transform: translate(-50%, 0);
  animation: score-pop 950ms cubic-bezier(.2,.7,.2,1) forwards;
  white-space: nowrap;
}
.score-pop--piece {
  font-size: clamp(14px, 4vw, 18px);
  color: rgba(245, 247, 255, 0.7);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5);
}
.score-pop--line {
  font-size: clamp(20px, 5.5vw, 28px);
  color: var(--accent);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.4), 0 0 14px rgba(255, 209, 102, 0.65);
}
.score-pop--streak {
  font-size: clamp(18px, 5vw, 24px);
  color: #ff8aa6;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.4), 0 0 14px rgba(255, 138, 166, 0.65);
}
.score-pop--perfect {
  font-size: clamp(26px, 7vw, 36px);
  color: #ffe28a;
  text-shadow:
    0 3px 0 rgba(0, 0, 0, 0.4),
    0 0 22px rgba(255, 209, 102, 0.85),
    0 0 44px rgba(255, 154, 60, 0.55);
}
.score-pop--snug {
  font-size: clamp(20px, 5.5vw, 28px);
  color: #ffd166;
  text-shadow:
    0 2px 0 rgba(0, 0, 0, 0.4),
    0 0 14px rgba(255, 209, 102, 0.75);
}
/* Tier escalates pop size + glow: TIGHT brighter, FLUSH biggest and white-hot. */
.score-pop--tight {
  font-size: clamp(22px, 6vw, 32px);
  color: #ffe28a;
  text-shadow:
    0 2px 0 rgba(0, 0, 0, 0.4),
    0 0 20px rgba(255, 200, 80, 0.9),
    0 0 36px rgba(255, 154, 60, 0.55);
}
.score-pop--flush {
  font-size: clamp(26px, 7vw, 38px);
  color: #fff5b0;
  text-shadow:
    0 3px 0 rgba(0, 0, 0, 0.45),
    0 0 26px rgba(255, 230, 150, 0.95),
    0 0 52px rgba(255, 154, 60, 0.65);
  letter-spacing: 0.05em;
}
@keyframes score-pop {
  0%   { transform: translate(-50%, 8px) scale(0.7); opacity: 0; }
  18%  { transform: translate(-50%, -6px) scale(1.18); opacity: 1; }
  100% { transform: translate(-50%, -62px) scale(1); opacity: 0; }
}

/* Score HUD: 出玉ロールの熱。獲得量 tier で色・グロー・スケールが段階的に上がる。
   inline-block にしないと span に transform が乗らない。 */
#score {
  display: inline-block;
  transform-origin: center bottom;
}
#score.is-tick {
  transition: color 90ms ease, text-shadow 90ms ease, transform 90ms ease;
}
#score.roll-warm {
  color: var(--accent);
  text-shadow:
    0 2px 0 rgba(0, 0, 0, 0.3),
    0 0 12px rgba(255, 209, 102, 0.5);
  transform: scale(1.05);
}
#score.roll-hot {
  color: #ffe27a;
  text-shadow:
    0 2px 0 rgba(0, 0, 0, 0.3),
    0 0 16px rgba(255, 180, 80, 0.75),
    0 0 30px rgba(255, 138, 91, 0.4);
  transform: scale(1.11);
}
#score.roll-blaze {
  color: #fff3c0;
  text-shadow:
    0 2px 0 rgba(0, 0, 0, 0.35),
    0 0 22px rgba(255, 220, 140, 0.95),
    0 0 46px rgba(255, 138, 91, 0.62);
  transform: scale(1.18);
}
/* 着地のカチン — オーバーシュートしてキュッと締める */
#score.roll-land {
  animation: score-land 380ms cubic-bezier(.2, .8, .25, 1);
}
@keyframes score-land {
  0%   { transform: scale(1.22); }
  42%  { transform: scale(0.95); }
  72%  { transform: scale(1.05); }
  100% { transform: scale(1); }
}

.combo-toast.is-best {
  color: #ffd166;
  font-size: clamp(28px, 8vw, 40px);
  text-shadow:
    0 4px 0 rgba(0, 0, 0, 0.4),
    0 0 22px rgba(255, 209, 102, 0.9),
    0 0 44px rgba(255, 154, 60, 0.6);
  letter-spacing: 0.08em;
}

/* ---------- Board shake on big clears ---------- */
@keyframes board-shake {
  0%, 100% { transform: translate(0, 0); }
  15%      { transform: translate(-5px, 2px); }
  30%      { transform: translate(4px, -3px); }
  45%      { transform: translate(-4px, 2px); }
  60%      { transform: translate(3px, -1px); }
  80%      { transform: translate(-2px, 1px); }
}
.board.is-shaking { animation: board-shake 360ms ease; }

/* ---------- Streak fire tiers (board edge glow + bro skin) ----------
   streak >=3 → tier 1 (orange) / >=5 → tier 2 (red) / >=7 → tier 3 (purple) */
.board.streak-1 {
  box-shadow:
    0 18px 40px -10px rgba(0, 0, 0, 0.55),
    inset 0 0 0 1px rgba(255, 255, 255, 0.05),
    inset 0 0 0 2px rgba(255, 154, 60, 0.5),
    0 0 40px rgba(255, 154, 60, 0.45);
  animation: tier-pulse 1400ms ease-in-out infinite;
}
.board.streak-2 {
  box-shadow:
    0 18px 40px -10px rgba(0, 0, 0, 0.55),
    inset 0 0 0 1px rgba(255, 255, 255, 0.05),
    inset 0 0 0 3px rgba(255, 59, 111, 0.65),
    0 0 56px rgba(255, 59, 111, 0.55);
  animation: tier-pulse 1000ms ease-in-out infinite;
}
.board.streak-3 {
  box-shadow:
    0 18px 40px -10px rgba(0, 0, 0, 0.55),
    inset 0 0 0 1px rgba(255, 255, 255, 0.05),
    inset 0 0 0 3px rgba(194, 92, 255, 0.78),
    0 0 78px rgba(194, 92, 255, 0.7);
  animation: tier-pulse 700ms ease-in-out infinite;
}
@keyframes tier-pulse {
  0%, 100% { filter: brightness(1); }
  50%      { filter: brightness(1.12); }
}

/* ---------- Density risk tiers (board-wrap vignette) ----------
   fill ≥70% → caution (amber) / ≥80% → danger (red) / ≥90% → red zone (deep red, fast pulse)
   Vignette sits behind the board's box-shadow so it stacks with streak glow. */
.board-wrap::before {
  content: "";
  position: absolute;
  inset: -18px;
  border-radius: 24px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 320ms ease;
  z-index: 0;
}
.board-wrap.density-1::before {
  opacity: 1;
  background: radial-gradient(ellipse at center,
    transparent 55%,
    rgba(255, 180, 60, 0.18) 78%,
    rgba(255, 130, 30, 0.32) 100%);
}
.board-wrap.density-2::before {
  opacity: 1;
  background: radial-gradient(ellipse at center,
    transparent 50%,
    rgba(255, 80, 80, 0.28) 75%,
    rgba(255, 40, 40, 0.5) 100%);
  animation: density-pulse 1100ms ease-in-out infinite;
}
.board-wrap.density-3::before {
  opacity: 1;
  background: radial-gradient(ellipse at center,
    transparent 42%,
    rgba(255, 50, 50, 0.45) 70%,
    rgba(220, 0, 0, 0.75) 100%);
  animation: density-pulse 620ms ease-in-out infinite;
}
@keyframes density-pulse {
  0%, 100% { filter: brightness(1) saturate(1); }
  50%      { filter: brightness(1.25) saturate(1.3); }
}

/* Bro skin per tier — overrides .bro defaults; placed AFTER .is-hyped so streak wins. */
.bro.streak-1 { background: linear-gradient(160deg, #ffd166 0%, #ff8a1e 100%); }
.bro.streak-2 { background: linear-gradient(160deg, #ff9aa2 0%, #ff3b6f 100%); }
.bro.streak-3 { background: linear-gradient(160deg, #d57bff 0%, #6e3eff 100%); }
.bro.streak-1 .bro__eye,
.bro.streak-2 .bro__eye,
.bro.streak-3 .bro__eye { background: #1a0a00; transform: scaleX(1.3); }
.bro.streak-1 .bro__mouth,
.bro.streak-2 .bro__mouth,
.bro.streak-3 .bro__mouth { width: 14px; height: 5px; border-radius: 0 0 8px 8px; }

/* ---------- Perfect Clear ceremony ---------- */
.board.is-perfect::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    radial-gradient(circle at 50% 50%,
      rgba(255, 230, 150, 0.85) 0%,
      rgba(255, 154, 60, 0.5) 35%,
      transparent 75%);
  pointer-events: none;
  animation: perfect-flash 1200ms cubic-bezier(.2,.7,.2,1) forwards;
  z-index: 4;
}
@keyframes perfect-flash {
  0%   { opacity: 0; transform: scale(0.55); }
  18%  { opacity: 1; transform: scale(1.08); }
  55%  { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.15); }
}

.combo-toast.is-perfect {
  color: #fff5b0;
  font-size: clamp(28px, 8vw, 44px);
  text-shadow:
    0 4px 0 rgba(0, 0, 0, 0.4),
    0 0 24px rgba(255, 200, 80, 0.9),
    0 0 48px rgba(255, 154, 60, 0.6);
  letter-spacing: 0.06em;
}

.bro.is-perfect { animation: bro-perfect 1200ms cubic-bezier(.2,.6,.2,1); }
@keyframes bro-perfect {
  0%   { transform: translateY(0) scale(1) rotate(0); }
  18%  { transform: translateY(-28px) scale(1.42) rotate(-10deg); }
  44%  { transform: translateY(-36px) scale(1.38) rotate(12deg); }
  68%  { transform: translateY(-12px) scale(1.14) rotate(-4deg); }
  100% { transform: translateY(0) scale(1) rotate(0); }
}

/* ---------- FEVER ゲージ（パチンコ出玉ラッシュ）----------
   HUD と盤面の間に置く横バー。リスク消去で満タンに溜まり、大当たりで盤面を爆破する。 */
.fever {
  width: 100%;
  max-width: 520px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 14px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.02));
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 14px;
}
.fever__label {
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.18em;
  color: var(--text-dim);
  flex-shrink: 0;
}
.fever__track {
  position: relative;
  flex: 1;
  height: 12px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.34);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06), inset 0 2px 4px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}
/* パチンコ台の玉並びを思わせるセグメント目盛り */
.fever__track::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: repeating-linear-gradient(90deg,
    transparent 0, transparent calc(10% - 1.5px),
    rgba(0, 0, 0, 0.32) calc(10% - 1.5px), rgba(0, 0, 0, 0.32) 10%);
  z-index: 2;
}
.fever__fill {
  width: 0%;
  height: 100%;
  border-radius: 8px;
  background: linear-gradient(90deg, #ff9a3c 0%, #ffd166 55%, #fff3a0 100%);
  box-shadow: 0 0 10px rgba(255, 200, 80, 0.7), 0 0 22px rgba(255, 154, 60, 0.45);
  transition: width 320ms cubic-bezier(.2,.7,.2,1);
}
.fever__ready {
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.06em;
  color: #fff3a0;
  text-shadow: 0 0 10px rgba(255, 200, 80, 0.9);
  flex-shrink: 0;
  opacity: 0;
  transform: scale(0.7);
  transition: opacity 200ms ease, transform 200ms ease;
}
/* リーチ（あと少しで大当たり）— バーが脈打ち「大当たり!」が現れる */
/* リーチ予告 — 派手にしない。ゆっくり静かに脈打つだけ（ここぞが近い合図）。 */
.fever.is-ready { animation: fever-ready 1200ms ease-in-out infinite; }
.fever.is-ready .fever__ready { opacity: 1; transform: scale(1); }
.fever.is-ready .fever__label { color: #ffd166; }
@keyframes fever-ready {
  0%, 100% { box-shadow: inset 0 0 0 1px rgba(255, 209, 102, 0.28); }
  50%      { box-shadow: inset 0 0 0 1px rgba(255, 209, 102, 0.6), 0 0 12px rgba(255, 200, 80, 0.32); }
}

/* BoB がチャージ完了を予告（リーチ）— 青い観察者が静かに金へ滲む。脈動は控えめ。 */
.bro.is-charged {
  background: linear-gradient(160deg, #ffe08a 0%, #ffac3e 100%) !important;
  box-shadow:
    inset 0 -4px 0 rgba(0, 0, 0, 0.26),
    inset 0 2px 0 rgba(255, 255, 255, 0.5),
    0 0 12px rgba(255, 200, 80, 0.55);
  animation: bro-charged 1200ms ease-in-out infinite;
}
@keyframes bro-charged {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.06); }
}

/* ① ため — 盤面が一拍だけ息を吸う（縮んで沈む）。この「間」が着弾を重くする。 */
.board.is-fever-charge {
  animation: fever-charge 420ms cubic-bezier(.5, 0, .5, 1) forwards;
}
@keyframes fever-charge {
  0%   { transform: scale(1);     filter: brightness(1)   saturate(1); }
  100% { transform: scale(0.965); filter: brightness(0.78) saturate(0.65); }
}

/* BoB が起爆装置になる — 観察者が一瞬だけ白熱する */
.bro.is-fever {
  background: radial-gradient(circle at 50% 38%, #fff 0%, #ffe27a 45%, #ff7a1e 100%) !important;
  box-shadow:
    0 0 18px rgba(255, 220, 120, 0.9),
    0 0 40px rgba(255, 160, 50, 0.6) !important;
  animation: bro-fever 300ms cubic-bezier(.3,.6,.3,1) 3;
  z-index: 6;
}
.bro.is-fever .bro__eye { background: #7a2a00 !important; transform: scaleX(1.4); animation: none; }
.bro.is-fever .bro__mouth { width: 16px; height: 8px; border-radius: 0 0 9px 9px; background: #7a2a00; }
@keyframes bro-fever {
  0%   { transform: translateY(0) scale(1) rotate(-3deg); }
  30%  { transform: translateY(-5px) scale(1.22, 0.88) rotate(4deg); }
  60%  { transform: translateY(-2px) scale(0.94, 1.14) rotate(-4deg); }
  100% { transform: translateY(0) scale(1) rotate(-3deg); }
}

/* ② 着弾フラッシュ — 持続させない。一閃だけ。 */
.board.is-fever::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    radial-gradient(circle at 50% 48%,
      rgba(255, 250, 235, 0.95) 0%,
      rgba(255, 200, 110, 0.4) 40%,
      transparent 72%);
  pointer-events: none;
  animation: fever-hit 360ms cubic-bezier(.2,.7,.2,1) forwards;
  z-index: 5;
}
@keyframes fever-hit {
  0%   { opacity: 0; transform: scale(0.7); }
  10%  { opacity: 1; transform: scale(1.04); }
  100% { opacity: 0; transform: scale(1); }
}

/* 単一の衝撃波リング — 一撃の重さ。中心から一度広がって消えるだけ。 */
.shockwave {
  position: fixed;
  transform: translate(-50%, -50%) scale(0.2);
  border-radius: 50%;
  border: 4px solid rgba(255, 236, 175, 0.9);
  box-shadow: 0 0 24px rgba(255, 200, 90, 0.55), inset 0 0 24px rgba(255, 200, 90, 0.35);
  pointer-events: none;
  z-index: 7;
  animation: shockwave 520ms cubic-bezier(.15,.7,.25,1) forwards;
}
@keyframes shockwave {
  0%   { transform: translate(-50%, -50%) scale(0.2); opacity: 0.95; border-width: 4px; }
  100% { transform: translate(-50%, -50%) scale(1.5); opacity: 0;    border-width: 1px; }
}

.combo-toast.is-fever {
  color: #fff6c0;
  font-size: clamp(28px, 8vw, 44px);
  text-shadow:
    0 4px 0 rgba(0, 0, 0, 0.45),
    0 0 22px rgba(255, 210, 90, 0.9),
    0 0 46px rgba(255, 150, 50, 0.6);
  letter-spacing: 0.08em;
}

.particle--fever { box-shadow: 0 0 8px rgba(255, 210, 110, 0.8); }

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, var(--cell));
  grid-template-rows: repeat(8, var(--cell));
  gap: var(--gap);
  padding: var(--board-pad);
  background: linear-gradient(160deg, #16204a 0%, #101733 100%);
  border-radius: 18px;
  box-shadow:
    0 18px 40px -10px rgba(0, 0, 0, 0.55),
    inset 0 0 0 1px rgba(255, 255, 255, 0.05);
  touch-action: none;
}

.cell {
  position: relative;
  width: var(--cell);
  height: var(--cell);
  border-radius: 8px;
  background: var(--grid-cell);
  box-shadow: inset 0 0 0 1px var(--grid-border);
}

/* (r+c) odd squares get the darker tone. Single-class selector so cell--filled
   (also single-class, source-ordered later) wins the background battle when both apply. */
.cell--alt { background: var(--grid-cell-2); }

.cell--filled {
  background: var(--fill, #5b8def);
  box-shadow:
    inset 0 -4px 0 rgba(0, 0, 0, 0.28),
    inset 0 3px 0 rgba(255, 255, 255, 0.42),
    inset 0 0 0 1px rgba(255, 255, 255, 0.18),
    0 3px 6px rgba(0, 0, 0, 0.32);
}

/* SNUG flash: 4+ cell piece slotted with most of its perimeter touching.
   Golden double-pulse on the newly-placed cells. */
.cell--snug { animation: cell-snug 620ms cubic-bezier(.2,.7,.2,1); }
@keyframes cell-snug {
  0%   { filter: brightness(1); }
  25%  { filter: brightness(1.55) drop-shadow(0 0 14px rgba(255, 209, 102, 0.95)); }
  60%  { filter: brightness(1.18) drop-shadow(0 0 6px rgba(255, 209, 102, 0.5)); }
  100% { filter: brightness(1); }
}

.cell--ghost {
  background: var(--ghost-color, var(--ghost-ok));
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.3);
  transition: background 90ms ease;
}

.cell--ghost-bad {
  background: var(--ghost-ng);
}

/* Predictive line-clear preview: gold halo on cells that WILL clear when dropped here.
   Implemented via outline + drop-shadow so ::before/::after stay free for bridges. */
.cell--predict {
  outline: 2px solid rgba(255, 209, 102, 0.85);
  outline-offset: -3px;
  animation: predict-pulse 720ms ease-in-out infinite;
}
.cell--predict.cell--filled {
  filter: drop-shadow(0 0 10px rgba(255, 209, 102, 0.55));
}
@keyframes predict-pulse {
  0%, 100% { opacity: 0.65; }
  50%      { opacity: 1; }
}


.cell--clearing {
  animation: cell-clear 460ms cubic-bezier(.25, .1, .4, 1) forwards;
  z-index: 2;
}

/* 3-stage: bright flash → color hold (saturated) → outward burst.
   Color stays readable until the very end instead of washing into white. */
@keyframes cell-clear {
  0%   { transform: scale(1);    filter: brightness(1)   saturate(1); }
  18%  { transform: scale(1.22); filter: brightness(1.7) saturate(1.4); }
  35%  { transform: scale(1.12); filter: brightness(1.25) saturate(1.6); opacity: 1; }
  70%  { transform: scale(1.05); filter: brightness(1.15) saturate(1.5); opacity: 0.92; }
  100% { transform: scale(0.2);  filter: brightness(1.35) saturate(1.4); opacity: 0; }
}

/* ---------- Tray ---------- */
.tray {
  width: 100%;
  max-width: 520px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 12px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.01));
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  min-height: calc(var(--tray-cell) * 5 + 24px);
}

.slot {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(var(--tray-cell) * 5);
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.18);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

.piece {
  display: grid;
  gap: var(--tray-gap);
  cursor: grab;
  touch-action: none;
  transition: transform 140ms ease;
  will-change: transform;
}

.piece.is-dragging { cursor: grabbing; }

.piece .pcell {
  width: var(--tray-cell);
  height: var(--tray-cell);
  border-radius: 6px;
  background: var(--fill, #5b8def);
  box-shadow:
    inset 0 -2px 0 rgba(0, 0, 0, 0.25),
    inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

.piece.is-empty { visibility: hidden; }
.piece.is-disabled { opacity: 0.4; filter: saturate(0.6); }

/* Floating drag clone, lives in <body> while dragging */
.drag-clone {
  position: fixed;
  display: grid;
  gap: var(--gap);
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 10px 18px rgba(0, 0, 0, 0.5));
}

.drag-clone .pcell {
  width: var(--cell);
  height: var(--cell);
  border-radius: 8px;
  background: var(--fill, #5b8def);
  box-shadow:
    inset 0 -3px 0 rgba(0, 0, 0, 0.25),
    inset 0 2px 0 rgba(255, 255, 255, 0.28);
}

/* ---------- Combo toast ---------- */
.combo-toast {
  position: fixed;
  top: 28%;
  left: 50%;
  transform: translate(-50%, -10px);
  padding: 10px 18px;
  font-size: clamp(22px, 6vw, 34px);
  font-weight: 900;
  letter-spacing: 0.04em;
  color: var(--accent);
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.3);
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  opacity: 0;
  pointer-events: none;
  z-index: 900;
}

.combo-toast.show {
  animation: combo-pop 1100ms cubic-bezier(.2,.7,.2,1) forwards;
}

@keyframes combo-pop {
  0%   { opacity: 0; transform: translate(-50%, 10px) scale(0.7); }
  20%  { opacity: 1; transform: translate(-50%, -8px) scale(1.08); }
  70%  { opacity: 1; transform: translate(-50%, -8px) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -30px) scale(0.95); }
}

/* ---------- Particles ---------- */
.particle {
  position: fixed;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 800;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(8, 12, 28, 0.6);
  backdrop-filter: blur(6px);
  z-index: 1500;
}

.modal[aria-hidden="false"] { display: flex; }

.modal__panel {
  width: min(86vw, 360px);
  padding: 28px 24px;
  background: linear-gradient(180deg, #1d2854, #131a3a);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 20px;
  text-align: center;
  box-shadow: 0 30px 60px -10px rgba(0, 0, 0, 0.6);
  animation: panel-in 220ms ease;
}

@keyframes panel-in {
  from { transform: translateY(20px) scale(0.94); opacity: 0; }
  to   { transform: translateY(0) scale(1); opacity: 1; }
}

.modal__title {
  margin: 0 0 16px 0;
  font-size: 28px;
  letter-spacing: 0.02em;
}

.modal__row {
  display: flex;
  justify-content: space-between;
  padding: 8px 4px;
  font-size: 16px;
  color: var(--text-dim);
}

.modal__row span:last-child {
  color: var(--text);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.modal__btn {
  margin-top: 18px;
  width: 100%;
  padding: 14px 16px;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: #0e1530;
  background: linear-gradient(180deg, #ffe08a, #ffb347);
  border: 0;
  border-radius: 14px;
  cursor: pointer;
  box-shadow: 0 6px 0 #b07520, 0 12px 24px rgba(0, 0, 0, 0.35);
  transition: transform 80ms ease, box-shadow 80ms ease;
}

.modal__btn:active {
  transform: translateY(3px);
  box-shadow: 0 3px 0 #b07520, 0 6px 16px rgba(0, 0, 0, 0.3);
}

/* ---------- Loading overlay (gates play until audio buffers are ready) ---------- */
.loading {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 38%, #1f2c54 0%, #0a0f24 70%, #06091a 100%);
  transition: opacity 0.4s ease;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.loading.is-hidden {
  opacity: 0;
  pointer-events: none;
}

.loading__inner {
  text-align: center;
  color: #f5f7ff;
  padding: 24px;
  width: min(320px, 80vw);
}

.loading__title {
  font: 800 26px/1 system-ui, -apple-system, "Segoe UI", sans-serif;
  letter-spacing: 0.18em;
  color: #ffd166;
  text-shadow: 0 2px 0 #b07520, 0 6px 18px rgba(255, 180, 80, 0.35);
  margin-bottom: 22px;
}

.loading__bar {
  width: 100%;
  height: 10px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.4);
}

.loading__fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #ffd166 0%, #ff8a5b 50%, #ff5cc4 100%);
  border-radius: 6px;
  transition: width 0.25s cubic-bezier(0.2, 0.7, 0.2, 1);
  box-shadow: 0 0 12px rgba(255, 138, 91, 0.6);
}

.loading__status {
  margin-top: 14px;
  color: #c5cce4;
  font: 600 13px/1.4 system-ui, -apple-system, sans-serif;
  letter-spacing: 0.04em;
  min-height: 1.4em;
}

.loading__status.is-ready {
  color: #ffd166;
  font-size: 16px;
  letter-spacing: 0.2em;
  animation: loading-pulse 1.2s ease-in-out infinite;
}

@keyframes loading-pulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.loading__skip {
  margin-top: 18px;
  padding: 10px 18px;
  font-size: 13px;
  font-weight: 700;
  color: #c5cce4;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  cursor: pointer;
}

.loading__skip:active {
  background: rgba(255, 255, 255, 0.15);
}

/* ---------- Mute toggle ---------- */
.mute-toggle {
  position: fixed;
  top: max(env(safe-area-inset-top), 8px);
  right: max(env(safe-area-inset-right), 8px);
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 50%;
  background: rgba(20, 28, 56, 0.7);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  color: #f5f7ff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  z-index: 60;
  display: grid;
  place-items: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  transition: background 120ms ease, transform 80ms ease;
}

.mute-toggle:active {
  transform: scale(0.92);
  background: rgba(20, 28, 56, 0.9);
}

.mute-toggle.is-muted {
  background: rgba(60, 30, 30, 0.7);
  border-color: rgba(255, 120, 120, 0.35);
}

.mute-toggle__icon {
  pointer-events: none;
}

/* ---------- Game Over stats grid ---------- */
.modal__stats {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px 18px;
  margin: 20px 0 4px;
  align-items: baseline;
}

.modal__head {
  font-size: 10px;
  font-weight: 800;
  color: var(--text-dim);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  text-align: right;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  min-width: 64px;
}

.modal__head--label {
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.modal__label {
  font-size: 14px;
  color: var(--text-dim);
  text-align: left;
}

.modal__val {
  font-size: 15px;
  color: var(--text);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  text-align: right;
  min-width: 64px;
}

/* Highlight "this game" column subtly so eye knows where to land */
.modal__stats > .modal__val:nth-of-type(2n+1) {
  color: #ffd166;
}

/* ---------- Score milestone celebration ---------- */
.combo-toast.is-milestone {
  background: linear-gradient(135deg, #ffd166 0%, #ff8a5b 40%, #ff5cc4 75%, #7e5cff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  font-size: clamp(28px, 9vw, 44px);
  font-weight: 900;
  letter-spacing: 0.08em;
  text-shadow: none;
  filter: drop-shadow(0 0 24px rgba(255, 138, 91, 0.55));
}

#score.is-milestone {
  animation: score-milestone 1.5s ease-out;
}

@keyframes score-milestone {
  0%   { transform: scale(1);   color: var(--text); text-shadow: none; }
  18%  { transform: scale(1.45); color: #ffd166; text-shadow: 0 0 18px #ffd166, 0 0 36px #ff8a5b; }
  45%  { transform: scale(1.15); color: #ffe27a; text-shadow: 0 0 10px #ffd166; }
  100% { transform: scale(1);   color: var(--text); text-shadow: none; }
}
