:root {
  --void: #02040a;
  --panel: #071018;
  --recess: #0c1e28;
  --gridline: #13485a;
  --cyan: #00f6ff;
  --amber: #ffb000;
  --magenta: #ff2bd6;
  --ice: #e8fbff;
  --gold: #ffb000;
  --fog: #9fd4dc;
  --mist: #5c7d86;
  --hue-teal: #00f6ff;
  --hue-amber: #ffb000;
  --hue-violet: #ff2bd6;
  --hue-coral: #ff6a00;
  --hue-sage: #39ffb0;
  --font-display: "Orbitron", sans-serif;
  --font-sans: "Rajdhani", sans-serif;
}

[data-theme="arcade"] {
  --void: #08010f;
  --panel: #160820;
  --recess: #221033;
  --cyan: #ff3de2;
  --gold: #39ffb0;
  --amber: #4aa3ff;
  --gridline: #ff4ae8;
  --ice: #fff4ff;
  --fog: #f3b8ff;
  --mist: #c07ad8;
  --hue-teal: #00f6ff;
  --hue-amber: #4aa3ff;
  --hue-violet: #ff3de2;
}

[data-theme="sunset"] {
  --void: #0b1020;
  --panel: #141824;
  --recess: #12161f;
  --cyan: #ffd36a;
  --gold: #ffd27a;
  --amber: #ff7a2a;
  --gridline: #3d4a63;
  --ice: #fff3e4;
  --fog: #f0c9a0;
  --mist: #8a7a72;
  --hue-teal: #ffd36a;
  --hue-amber: #ff6b2b;
  --hue-violet: #e86b9a;
  --hue-coral: #ff8a4a;
  --hue-sage: #7ec8ff;
}

[data-theme="void"] {
  --cyan: #6ad4dc;
  --gold: #c9a24a;
  --amber: #c9a24a;
  --recess: #02040a;
  --gridline: transparent;
  --panel: #030508;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  width: 100%;
  min-height: 100%;
  height: 100%;
  background: var(--void);
  color: var(--ice);
  font-family: var(--font-sans);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
body {
  display: flex;
  justify-content: center;
  align-items: stretch;
}
.play, .piece-btn, .board-grid { touch-action: none; }

.game-root {
  width: min(480px, 100%);
  max-width: 480px;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: max(8px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  background:
    radial-gradient(80% 50% at 50% -10%, color-mix(in oklab, var(--cyan) 12%, transparent), transparent 60%),
    linear-gradient(180deg, #05080e 0%, var(--void) 55%);
}

[data-theme="void"].game-root { background: #02040a; }
[data-theme="arcade"].game-root {
  background:
    radial-gradient(80% 50% at 50% -8%, rgb(255 61 226 / 0.22), transparent 60%),
    linear-gradient(180deg, #160820 0%, #08010f 55%);
}
[data-theme="arcade"] .board-shell {
  background: linear-gradient(180deg, #220c32, #0e0416);
  box-shadow: 0 0 0 1px #ff4ae8, 0 0 22px rgb(255 61 226 / 0.28);
}
[data-theme="arcade"] .cell-empty {
  background: #140818;
  box-shadow: inset 0 0 0 1.5px #6a2a62, inset 0 0 10px rgb(106 42 98 / 0.28);
}
[data-theme="sunset"].game-root {
  background:
    radial-gradient(70% 42% at 50% -6%, rgb(255 140 50 / 0.38), transparent 58%),
    linear-gradient(180deg, #3a1c18 0%, #1a1428 38%, #0b1020 100%);
}
[data-theme="sunset"] .board-shell {
  background: linear-gradient(180deg, #161820, #0b0d14);
  box-shadow: 0 0 0 1px #ffd36a, 0 0 28px rgb(255 140 50 / 0.22);
}
[data-theme="sunset"] .cell-empty {
  background: #10131b;
  box-shadow: inset 0 0 0 1px #3d4a63, inset 0 2px 4px rgb(0 0 0 / 0.55);
}
[data-theme="sunset"] .cell-ghost {
  border-color: #ffe7c2;
  background: rgb(255 231 194 / 0.12);
}
[data-theme="sunset"] .cell-empty.channel {
  background: #1a2230;
  box-shadow: inset 0 0 0 1px #ffd36a;
}

.wordmark {
  font-family: var(--font-display);
  font-weight: 800;
  letter-spacing: 0.28em;
  text-transform: uppercase;
}
.hud-label {
  font-size: 0.7rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--mist);
  margin: 0;
}
.hud-num {
  font-family: var(--font-sans);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin: 0;
  font-size: 1.15rem;
  color: var(--ice);
  letter-spacing: 0.04em;
}

.screen-title {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.title-motif {
  display: grid;
  grid-template-columns: repeat(3, 32px);
  grid-template-rows: repeat(3, 32px);
  gap: 6px;
  padding: 8px;
  background: var(--panel);
  margin-bottom: 36px;
}
.title-motif .on {
  background: color-mix(in oklab, var(--cyan) 42%, #071018);
  box-shadow: inset 0 0 0 2px var(--cyan), 0 0 8px color-mix(in oklab, var(--cyan) 45%, transparent);
}
.title-motif .off { background: var(--recess); box-shadow: inset 0 0 0 1px var(--gridline); }

.play-btn, .ctrl-btn, .pivot-btn, .theme-chip {
  font-family: var(--font-display);
  letter-spacing: 0.18em;
  border: 1px solid color-mix(in oklab, var(--cyan) 45%, transparent);
  background: #061018;
  color: var(--ice);
  cursor: pointer;
  min-height: 44px;
}
.play-btn { margin-top: 40px; padding: 14px 48px; font-size: 1.1rem; }
.play-btn:hover { box-shadow: 0 0 18px color-mix(in oklab, var(--cyan) 35%, transparent); }
.theme-row { display: flex; gap: 8px; margin-top: 28px; flex-wrap: wrap; justify-content: center; }
.theme-chip {
  padding: 6px 10px;
  font-size: 0.65rem;
  min-width: 64px;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
}
.theme-chip.on { border-color: var(--cyan); color: var(--cyan); }
.theme-dots { display: flex; gap: 3px; }
.theme-dots i { width: 8px; height: 8px; border-radius: 99px; display: block; }

.play {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}
header.hud {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
  align-items: end;
  flex-shrink: 0;
}
header.hud .wordmark { font-size: 0.85rem; color: var(--fog); }
header.hud .stat { text-align: right; }

.combo-rail {
  flex: 0 0 auto;
  min-height: 32px;
  position: relative;
  pointer-events: none;
  margin-top: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.combo-banner {
  position: relative;
  font-family: var(--font-display);
  letter-spacing: 0.18em;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--gold);
  background: rgb(2 6 12 / 0.9);
  border: 1px solid color-mix(in oklab, var(--gold) 50%, transparent);
  padding: 4px 12px;
  white-space: nowrap;
  animation: combo-in 280ms ease-out;
}
.combo-banner.cross { color: var(--ice); border-color: var(--cyan); }
@keyframes combo-in {
  from { transform: scale(0.82); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
.board-area {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  position: relative;
}
.board-wrap {
  width: min(100%, 420px);
  aspect-ratio: 1 / 1;
  height: auto;
  max-height: 100%;
  flex: 0 0 auto;
}

@media (min-width: 600px) and (min-height: 720px) {
  .game-root {
    width: min(560px, 100%);
    max-width: 560px;
  }
  .stage {
    justify-content: center;
  }
  .board-area {
    flex: 0 1 auto;
    align-items: center;
    width: 100%;
  }
  .board-wrap {
    width: min(100%, 520px, 70dvh);
  }
}

@media (min-width: 1024px) and (min-height: 720px) {
  .game-root {
    width: min(640px, 100%);
    max-width: 640px;
  }
  .board-wrap {
    width: min(100%, 560px, 70dvh);
  }
}

.board-shell {
  width: 100%;
  height: 100%;
  padding: 10px;
  background: linear-gradient(180deg, #08141c, #050910);
  box-shadow: 0 0 0 1px color-mix(in oklab, var(--cyan) 35%, transparent);
  position: relative;
  display: flex;
}
.pivot-wrap { width: 100%; height: 100%; min-height: 0; }
[data-theme="void"] .board-shell { background: #02040a; padding: 8px; }
.board-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 3px;
  width: 100%;
  height: 100%;
}
[data-theme="void"] .board-grid { gap: 0; }

.cell { position: relative; min-width: 0; min-height: 0; border-radius: 3px; }
.cell-empty {
  background: var(--recess);
  box-shadow: inset 0 0 0 1px var(--gridline), inset 0 2px 3px rgb(0 0 0 / 0.55);
}
[data-theme="void"] .cell-empty { background: transparent; box-shadow: none; border-radius: 0; }
.cell-stone {
  background: color-mix(in oklab, var(--edge, var(--cyan)) 42%, #071018);
  box-shadow: inset 0 0 0 2px var(--edge, var(--cyan)), 0 0 8px color-mix(in oklab, var(--edge, var(--cyan)) 45%, transparent);
}
.cell-stone[data-hue="teal"] { --edge: var(--hue-teal); }
.cell-stone[data-hue="amber"] { --edge: var(--hue-amber); }
.cell-stone[data-hue="violet"] { --edge: var(--hue-violet); }
.cell-stone[data-hue="coral"] { --edge: var(--hue-coral); }
.cell-stone[data-hue="sage"] { --edge: var(--hue-sage); }
[data-theme="void"] .cell-stone {
  background: color-mix(in oklab, var(--edge, var(--cyan)) 18%, #02040a);
}
.cell-ghost {
  position: absolute;
  inset: 1px;
  pointer-events: none;
  border: 3px dashed #e8fbff;
  background: rgb(232 251 255 / 0.1);
}
.cell-ghost.bad { border-color: var(--magenta); background: rgb(255 43 214 / 0.08); }
[data-theme="void"] .cell-ghost { display: none !important; }
.cell-empty.channel {
  box-shadow: inset 0 0 0 1px var(--ice);
  background: color-mix(in oklab, var(--ice) 8%, var(--recess));
}
[data-theme="void"] .cell-empty.channel { box-shadow: none; background: transparent; }

.play.shake-sm { animation: shake-sm 180ms ease-in-out; }
.play.shake-lg { animation: shake-lg 280ms ease-in-out; }
@keyframes shake-sm {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  75% { transform: translateX(2px); }
}
@keyframes shake-lg {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px) rotate(-0.3deg); }
  40% { transform: translateX(4px) rotate(0.3deg); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}
.play.bloom::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at 50% 42%, color-mix(in oklab, var(--cyan) 28%, transparent), transparent 62%);
  animation: bloom-out 420ms ease-out forwards;
  z-index: 12;
}
@keyframes bloom-out { 0% { opacity: 1; } 100% { opacity: 0; } }
.score-pop { animation: score-pop 420ms ease-out; }
@keyframes score-pop {
  0% { transform: scale(1); color: var(--ice); }
  35% { transform: scale(1.18); color: var(--gold); }
  100% { transform: scale(1); color: var(--ice); }
}
.score-chip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  transform: translate(-50%, 0);
  font-family: var(--font-display);
  letter-spacing: 0.12em;
  font-size: 1.15rem;
  color: #fff;
  text-shadow: 0 0 12px color-mix(in oklab, var(--cyan) 55%, transparent);
  animation: chip-up 900ms ease-out forwards;
}
.score-chip.gold { color: var(--gold); font-size: 1.35rem; }
.score-chip.hot { color: #ff4d4d; font-size: 1.55rem; text-shadow: 0 0 16px rgb(255 70 70 / 0.6); }
@keyframes chip-up {
  0% { opacity: 0; transform: translate(-50%, 8px) scale(0.86); }
  18% { opacity: 1; transform: translate(-50%, 0) scale(1.08); }
  100% { opacity: 0; transform: translate(-50%, -36px) scale(1); }
}
.scan-ribbon {
  position: fixed;
  pointer-events: none;
  z-index: 30;
  background: linear-gradient(90deg, transparent, color-mix(in oklab, var(--cyan) 80%, white), transparent);
  opacity: 0.85;
  animation: ribbon 320ms ease-out forwards;
}
.scan-ribbon.col {
  background: linear-gradient(180deg, transparent, color-mix(in oklab, var(--cyan) 80%, white), transparent);
}
@keyframes ribbon {
  0% { opacity: 0; filter: brightness(2); }
  30% { opacity: 1; }
  100% { opacity: 0; }
}
.cell-clearing { animation: derezz 320ms ease-out forwards; }
@keyframes derezz { to { opacity: 0; transform: scale(0.4); filter: brightness(2); } }
.cell-placed { animation: place-tick 160ms ease-out; }
@keyframes place-tick {
  0% { filter: brightness(1.85); }
  100% { filter: brightness(1); }
}
.pivot-wrap.spinning { animation: spin90 220ms ease-in-out; }
@keyframes spin90 { to { transform: rotate(90deg); } }

.praise {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font-family: var(--font-display);
  letter-spacing: 0.28em;
  font-size: clamp(1.15rem, 5vw, 1.7rem);
  color: var(--cyan);
  text-shadow: 0 0 18px color-mix(in oklab, var(--cyan) 55%, transparent);
  animation: praise-in 700ms ease both;
  text-align: center;
}
@keyframes praise-in {
  0% { opacity: 0; transform: scale(0.84); }
  18% { opacity: 1; transform: scale(1.06); }
  70% { opacity: 1; }
  100% { opacity: 0; }
}

.tray {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
  flex-shrink: 0;
}
.tray-slot {
  min-height: 72px;
  background: var(--panel);
  border: 1px solid color-mix(in oklab, var(--cyan) 18%, transparent);
  display: flex;
  align-items: center;
  justify-content: center;
}
.tray-slot.selected {
  border-color: var(--cyan);
  box-shadow: 0 0 12px color-mix(in oklab, var(--cyan) 35%, transparent);
}
.piece-btn {
  background: transparent;
  border: 0;
  min-width: 44px;
  min-height: 44px;
  cursor: grab;
  color: inherit;
}
.mini-grid { display: grid; gap: 2px; }
.mini-cell {
  width: 12px;
  height: 12px;
  background: color-mix(in oklab, var(--edge, var(--cyan)) 50%, #071018);
  box-shadow: inset 0 0 0 1.5px var(--edge, var(--cyan)), 0 0 6px color-mix(in oklab, var(--edge, var(--cyan)) 35%, transparent);
}
[data-theme="void"] .mini-cell {
  background: color-mix(in oklab, var(--edge, var(--cyan)) 18%, #02040a);
}
.controls {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
  flex-shrink: 0;
}
.ctrl-btn { padding: 8px 12px; min-width: 44px; display: flex; gap: 6px; align-items: center; justify-content: center; }
.ctrl-btn:disabled, .pivot-btn:disabled { opacity: 0.35; cursor: default; }
.pivot-cluster {
  flex: 1;
  max-width: 200px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  min-width: 0;
}
.pivot-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.85rem;
}
.pivot-btn.hungry {
  border-color: var(--cyan);
  animation: hungry-pulse 900ms ease-in-out infinite;
}
@keyframes hungry-pulse {
  0%, 100% { box-shadow: 0 0 0 1px color-mix(in oklab, var(--cyan) 45%, transparent); }
  50% { box-shadow: 0 0 18px color-mix(in oklab, var(--cyan) 70%, transparent); }
}
.pivot-hint {
  margin: 4px 0 0;
  text-align: center;
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--cyan);
  font-family: var(--font-sans);
  font-weight: 600;
}
.pips { display: flex; gap: 4px; }
.pip { width: 6px; height: 6px; border-radius: 99px; background: #133; }
.pip.on { background: var(--cyan); box-shadow: 0 0 6px var(--cyan); }
.overlay {
  position: absolute;
  inset: 0;
  background: rgb(2 6 12 / 0.72);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0 16px 80px;
  z-index: 20;
}
.over-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  padding: 28px 24px 24px;
  text-align: center;
  box-shadow: 0 0 40px rgb(0 246 255 / 0.12);
}
.over-card h2 {
  font-family: var(--font-display);
  letter-spacing: 0.18em;
  font-size: 1.3rem;
}
.over-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 20px 0; }
.over-stats div { background: var(--void); padding: 12px; }
.reset-actions {
  display: flex;
  gap: 12px;
  margin-top: 20px;
}
.reset-actions .ctrl-btn,
.reset-actions .play-btn {
  flex: 1;
  margin: 0;
  min-height: 44px;
}
.ghost-piece {
  position: fixed;
  pointer-events: none;
  z-index: 40;
  transform: translate(-50%, -50%);
}
.ghost-piece.snapped { transform: none; }
.ghost-piece.bad { opacity: 0.55; }
.ghost-piece.bad .mini-cell { box-shadow: inset 0 0 0 1.5px var(--magenta); }
.coach {
  margin: 0;
  width: 100%;
  text-align: center;
  color: var(--fog);
  font-size: 0.8rem;
  letter-spacing: 0.02em;
  line-height: 1.3;
  padding: 2px 0;
}
@media (prefers-reduced-motion: reduce) {
  .pivot-wrap.spinning,
  .cell-clearing,
  .cell-placed,
  .praise,
  .play.shake-sm,
  .play.shake-lg,
  .play.bloom::after,
  .score-chip,
  .scan-ribbon,
  .combo-banner,
  .pivot-btn.hungry { animation: none !important; }
}
