:root {
  --bg: #12142b; --board-bg: #171a38; --empty: #2a2e5a;
  --text: #eef0ff; --muted: #8d92c4; --accent: #ffd93d;
  --c1: #ff5a5f; --c2: #ff9f43; --c3: #ffd93d; --c4: #3ddc84;
  --c5: #2fd4e8; --c6: #4a7dff; --c7: #a55eea;
  --board: min(calc(100vw - 44px), calc((100vh - 184px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) / 1.36), 560px);
  --cell: calc(var(--board) / 8);
  /* 0.55 cell, but a 1x5 piece must still fit a third of the tray */
  --tc: min(calc(var(--cell) * 0.55), calc((var(--board) - 16px) / 15));
}
@supports (height: 100dvh) {
  :root { --board: min(calc(100vw - 44px), calc((100dvh - 184px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) / 1.36), 560px); }
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden; position: fixed; inset: 0; width: 100%;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none; touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
body {
  display: flex; flex-direction: column; align-items: center;
  height: 100vh; height: 100dvh;
  padding: max(env(safe-area-inset-top), 8px) max(env(safe-area-inset-right), 8px)
           max(env(safe-area-inset-bottom), 8px) max(env(safe-area-inset-left), 8px);
}

.hud { width: calc(var(--board) + 12px); display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; padding: 4px 2px; }
.stat { display: flex; flex-direction: column; min-width: 0; }
.stat.best { align-items: flex-start; }
.stat.main { align-items: center; }
.hud-btns { justify-self: end; display: flex; gap: 8px; }
.label { font-size: 12px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.value { font-size: clamp(34px, 6vh, 48px); font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.1; }
.best .value { color: var(--accent); font-size: 20px; }
.value.pop { animation: pop .25s ease-out; }

.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0; font-size: 20px; cursor: pointer;
  background: var(--board-bg); color: var(--text); touch-action: manipulation;
}
.i { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn .i { width: 22px; height: 22px; pointer-events: none; }
.label .crown { width: 14px; height: 14px; vertical-align: -2px; }
.icon-btn:active { transform: scale(.94); }
.icon-btn.armed { background: #ff5a5f; color: #12142b; }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.play { flex: 1; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; min-height: 0; }
.board-wrap { padding: 6px; background: var(--board-bg); border-radius: 16px; box-shadow: 0 8px 30px rgba(0,0,0,.4); }
#board { width: var(--board); height: var(--board); display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); }
.status { height: 24px; line-height: 24px; font-size: 14px; font-weight: 600; color: var(--text); text-align: center; white-space: nowrap; }
.status .racha { color: #ff7ad9; font-weight: 800; }
.pip { display: inline-block; width: 6px; height: 6px; border-radius: 50%; margin-left: 4px; background: #3a3f73; vertical-align: middle; }
.pip.on { background: #ff7ad9; }

.cell { position: relative; padding: 2px; }
.cell .b { width: 100%; height: 100%; border-radius: 22%; background: var(--empty); box-shadow: inset 0 2px 0 rgba(0,0,0,.22); }

[data-c="1"] { --col: var(--c1); } [data-c="2"] { --col: var(--c2); }
[data-c="3"] { --col: var(--c3); } [data-c="4"] { --col: var(--c4); }
[data-c="5"] { --col: var(--c5); } [data-c="6"] { --col: var(--c6); }
[data-c="7"] { --col: var(--c7); }
[data-g="1"] { --gcol: var(--c1); } [data-g="2"] { --gcol: var(--c2); }
[data-g="3"] { --gcol: var(--c3); } [data-g="4"] { --gcol: var(--c4); }
[data-g="5"] { --gcol: var(--c5); } [data-g="6"] { --gcol: var(--c6); }
[data-g="7"] { --gcol: var(--c7); }

.cell[data-c] .b, .piece .b {
  background: var(--col);
  box-shadow: inset 0 3px 0 rgba(255,255,255,.38), inset 0 -4px 0 rgba(0,0,0,.28), inset 0 0 0 1px rgba(255,255,255,.12);
}
.cell[data-g] .b { background: var(--gcol); opacity: .55; }
.cell[data-g] .b { background: color-mix(in srgb, var(--gcol) 45%, var(--empty)); box-shadow: inset 0 0 0 2px var(--gcol); opacity: 1; }
.cell.hint .b { filter: brightness(1.35); outline: 2px solid #fff; outline-offset: -2px; animation: hintPulse 500ms ease-in-out infinite alternate; }
.cell.placed .b { animation: settle 180ms ease-out; }
.cell.clearing .b { animation: out 250ms ease-in var(--d, 0ms) forwards; }
.cell.clearing::after { content: ''; position: absolute; inset: 2px; border-radius: 22%; border: 2px solid #fff; opacity: 0; animation: ring 360ms ease-out var(--d, 0ms); }
.board-wrap.shake { animation: shake 220ms cubic-bezier(.36,.07,.19,.97); }
#board.over .cell[data-c] .b { filter: grayscale(.85) brightness(.65); transition: filter 500ms ease-out; }

.tray { width: calc(var(--board) + 12px); padding: 6px; border-radius: 18px; background: #171a36; box-shadow: inset 0 0 0 1px rgba(255,255,255,.05); display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.slot { min-width: 44px; min-height: 44px; touch-action: none; height: calc(var(--tc) * 5 + 12px); display: flex; align-items: center; justify-content: center; background: none; }
.piece { display: grid; grid-template-columns: repeat(var(--w), var(--tc)); grid-template-rows: repeat(var(--h), var(--tc)); cursor: grab; touch-action: none; transition: opacity .2s; }
.piece .b { border-radius: 22%; margin: 1px; }
.piece.dim { opacity: .5; filter: grayscale(.85) brightness(.8); cursor: default; }
.piece.nope { animation: nope 220ms ease-in-out; }
.piece.nudge { animation: nudge 900ms ease-in-out infinite alternate; }
.piece.enter { animation: enter .35s ease-out backwards; }
.piece.dragging { opacity: .2; }
.piece.float {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none; cursor: grabbing;
  transform-origin: 0 0; will-change: transform; filter: drop-shadow(0 10px 12px rgba(0,0,0,.45));
}
.piece.float .b { margin: 0; border: 2px solid transparent; background-clip: padding-box; animation: lift 120ms cubic-bezier(.2,.9,.3,1.3); }

.fx { position: fixed; inset: 0; pointer-events: none; z-index: 60; overflow: hidden; }
.ftext { position: absolute; transform: translate(-50%, 0); font-weight: 900; font-size: clamp(28px, 6vh, 44px); color: var(--accent); text-shadow: 0 2px 0 rgba(0,0,0,.5), 0 0 18px rgba(255,217,61,.6); white-space: nowrap; animation: rise .95s ease-out forwards; }
.ftext.combo { color: #ff7ad9; font-size: clamp(20px, 4vh, 30px); text-shadow: 0 2px 0 rgba(0,0,0,.5), 0 0 16px rgba(255,122,217,.6); animation-delay: .1s; }
.ftext.small { font-size: 15px; font-weight: 700; color: rgba(238,240,255,.8); text-shadow: none; animation: riseS 500ms ease-out forwards; }

.overlay { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; background: rgba(10,11,28,.72); animation: fade .3s ease-out; }
.overlay[hidden] { display: none; }
.card { width: min(86vw, 340px); padding: 26px 22px; border-radius: 22px; text-align: center; background: var(--board-bg); box-shadow: 0 20px 60px rgba(0,0,0,.6), inset 0 0 0 1px rgba(255,255,255,.08); animation: enter .4s ease-out; }
.card h2 { margin: 0 0 6px; font-size: 26px; }
.new { margin: 0 0 8px; color: var(--accent); font-weight: 800; }
.result { display: flex; flex-direction: column; margin: 12px 0; }
.big { font-size: 52px; font-weight: 900; line-height: 1.1; }
.mid { font-size: 28px; font-weight: 800; color: var(--accent); }
.btn { margin-top: 12px; min-height: 56px; width: 100%; padding: 16px 26px; border: 0; border-radius: 14px; font-size: 18px; font-weight: 800; cursor: pointer; color: #12142b; background: var(--c4); box-shadow: inset 0 3px 0 rgba(255,255,255,.4), inset 0 -4px 0 rgba(0,0,0,.25); touch-action: manipulation; }
.btn:active { transform: scale(.97); }

@keyframes pop { 0% { transform: scale(1.18); } 100% { transform: scale(1); } }
@keyframes settle { 0% { transform: scale(.82); } 60% { transform: scale(1.06); } 100% { transform: scale(1); } }
@keyframes lift { from { transform: scale(.55); } }
@keyframes out { 0% { filter: brightness(2); } 30% { transform: scale(1.1); } 100% { transform: scale(.1); opacity: 0; } }
@keyframes ring { 0% { opacity: .9; transform: scale(.7); } 100% { opacity: 0; transform: scale(1.35); } }
@keyframes shake { 20% { transform: translate(-3px, 1px); } 50% { transform: translate(3px, -1px); } 80% { transform: translate(-2px, 0); } }
@keyframes hintPulse { to { filter: brightness(1.6); transform: scale(.92); } }
@keyframes nope { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
@keyframes nudge { to { transform: translateY(-8px); } }
@keyframes enter { from { opacity: 0; transform: translateY(24px) scale(.9); } to { opacity: 1; transform: none; } }
@keyframes rise { from { opacity: 1; transform: translate(-50%, 0); } to { opacity: 0; transform: translate(-50%, -70px); } }
@keyframes riseS { to { opacity: 0; transform: translate(-50%, -36px); } }
@keyframes fadeOut { to { opacity: 0; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-delay: 0s !important; transition-duration: .01ms !important; }
  .cell.hint .b, .piece.nudge { animation: none !important; }
  .ftext { animation-name: fadeOut !important; animation-duration: 900ms !important; }
}
