:root {
  --cell-size: 60px;
  --gap: 4px;
  --cols: 10;
  --board-pad: 12px;
  --bg1: #1a0a3a;
  --bg2: #4a1568;
  --bg3: #7a1a8a;
  --gold: #ffd860;
  --panel: rgba(255, 255, 255, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: #fff;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  /* Stops iOS Safari from double-tap-zooming */
  touch-action: manipulation;
}
html {
  /* Cover the URL bar/home indicator overscroll on iOS */
  background: linear-gradient(160deg, var(--bg1) 0%, var(--bg2) 50%, var(--bg3) 100%);
  height: 100%;
  min-height: 100dvh;
}
body {
  /* Use dvh so iOS Safari's dynamic UI doesn't expose white bands */
  min-height: 100vh;
  min-height: 100dvh;
  /* Respect notches & gesture bars */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  box-sizing: border-box;
}

body {
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  perspective: 1600px;
  perspective-origin: 50% 45%;
}
/* Bullet-proof full-viewport background. position:fixed + extra inset
   so iOS Safari overscroll, dynamic URL bar and home indicator areas
   are always covered (background-attachment: fixed is unreliable on iOS). */
body::before {
  content: '';
  position: fixed;
  top: -100px;
  bottom: -100px;
  left: -50px;
  right: -50px;
  z-index: -10;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 30% 10%, rgba(255, 80, 200, 0.35) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 90%, rgba(80, 120, 255, 0.35) 0%, transparent 60%),
    linear-gradient(160deg, var(--bg1) 0%, var(--bg2) 50%, var(--bg3) 100%);
}

/* Combo-color tint — two layers crossfaded by JS for smooth color shifts */
.bg-tint {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  opacity: 0;
  transition: opacity 0.45s cubic-bezier(0.22, 1, 0.36, 1);
}
/* Expanding ripple rings */
#bg-ripples {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

/* Animated stars background */
.stars, .stars::before, .stars::after {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(1px 1px at 20% 30%, white, transparent),
    radial-gradient(1px 1px at 60% 70%, white, transparent),
    radial-gradient(1.5px 1.5px at 80% 20%, white, transparent),
    radial-gradient(1px 1px at 40% 80%, white, transparent),
    radial-gradient(1px 1px at 90% 60%, white, transparent),
    radial-gradient(1.5px 1.5px at 10% 50%, white, transparent);
  background-size: 100% 100%;
  opacity: 0.4;
  animation: twinkle 4s ease-in-out infinite alternate;
}
.stars::before { content: ''; opacity: 0.25; transform: scale(1.7) rotate(20deg); animation-duration: 6s; }
.stars::after { content: ''; opacity: 0.18; transform: scale(2.4) rotate(-15deg); animation-duration: 8s; }

@keyframes twinkle { from { opacity: 0.15; } to { opacity: 0.55; } }

#app {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 18px;
  z-index: 1;
  /* Camera shake — JS sets the vars during big specials */
  transform: translate(var(--shake-x, 0px), var(--shake-y, 0px));
  will-change: transform;
}

.title-wrap {
  position: relative;
  display: inline-block;
  padding: 30px 80px 38px;
  margin: -10px 0 -4px;
  perspective: 900px;
  transform-style: preserve-3d;
  animation: title-float 5.5s ease-in-out infinite;
  pointer-events: none;
}
/* Pulsing halo behind the text — softer blur for cheaper paint */
.title-wrap::before {
  content: '';
  position: absolute;
  inset: 18px 50px;
  background:
    radial-gradient(ellipse at center, rgba(255, 220, 120, 0.5) 0%, rgba(255, 100, 200, 0.2) 35%, transparent 70%);
  filter: blur(14px);
  z-index: -1;
  animation: halo-pulse 3.2s ease-in-out infinite;
  pointer-events: none;
}
.title-particles {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

h1.title {
  position: relative;
  z-index: 2;
  margin: 0;
  font-size: 68px;
  font-weight: 900;
  letter-spacing: 3px;
  text-transform: uppercase;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  /* Stacked text-shadows fake real 3D depth (extruded base + soft cast shadow) */
  background: linear-gradient(180deg, #fff8d6 0%, #ffe27d 30%, var(--gold) 60%, #d88500 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow:
    0 1px 0 #c8a20c,
    0 2px 0 #b89200,
    0 3px 0 #a88500,
    0 4px 0 #987500,
    0 5px 0 #886500,
    0 6px 0 #785500,
    0 7px 0 #684500,
    0 8px 1px rgba(0, 0, 0, 0.4),
    0 12px 18px rgba(0, 0, 0, 0.55),
    0 18px 38px rgba(255, 200, 60, 0.55);
  filter: drop-shadow(0 0 22px rgba(255, 200, 60, 0.55));
  transform-style: preserve-3d;
  -webkit-text-stroke: 1px rgba(120, 60, 0, 0.4);
}

@keyframes title-float {
  0%, 100% { transform: translateY(0) rotateY(-5deg) rotateX(3deg); }
  25%      { transform: translateY(-7px) rotateY(0deg)  rotateX(-1deg); }
  50%      { transform: translateY(-3px) rotateY(5deg)  rotateX(-3deg); }
  75%      { transform: translateY(-7px) rotateY(0deg)  rotateX(1deg); }
}

@keyframes halo-pulse {
  0%, 100% { opacity: 0.55; transform: scale(1); }
  50%      { opacity: 1;    transform: scale(1.18); }
}

/* React class fires on combos — shake + bright flash + scale punch */
h1.title.react {
  animation:
    title-shake 0.6s cubic-bezier(0.36, 0.07, 0.19, 0.97),
    title-flash 0.6s ease-out;
}
@keyframes title-shake {
  0%, 100% { transform: translate(0, 0) rotate(0) scale(1); }
  10%      { transform: translate(-6px, -3px) rotate(-2deg) scale(1.08); }
  22%      { transform: translate(7px, 4px)   rotate(2.5deg) scale(1.12); }
  35%      { transform: translate(-5px, 2px)  rotate(-2deg) scale(1.09); }
  48%      { transform: translate(4px, -2px)  rotate(1.5deg) scale(1.06); }
  62%      { transform: translate(-3px, 1px)  rotate(-1deg) scale(1.04); }
  78%      { transform: translate(2px, -1px)  rotate(0.5deg) scale(1.02); }
}
@keyframes title-flash {
  0%   { filter: drop-shadow(0 0 32px rgba(255, 200, 60, 0.6)); }
  20%  { filter: drop-shadow(0 0 60px white) drop-shadow(0 0 100px rgba(255, 220, 100, 1)) brightness(1.7) saturate(1.4); }
  50%  { filter: drop-shadow(0 0 50px rgba(255, 240, 180, 0.95)) brightness(1.35); }
  100% { filter: drop-shadow(0 0 32px rgba(255, 200, 60, 0.6)); }
}
h1.title.react-hard {
  animation:
    title-shake-hard 0.85s cubic-bezier(0.36, 0.07, 0.19, 0.97),
    title-flash-hard 0.85s ease-out;
}
@keyframes title-shake-hard {
  0%, 100% { transform: translate(0, 0) rotate(0) scale(1); }
  8%       { transform: translate(-10px, -5px) rotate(-4deg) scale(1.12); }
  18%      { transform: translate(12px, 6px)   rotate(5deg)  scale(1.18); }
  30%      { transform: translate(-9px, 3px)   rotate(-3deg) scale(1.14); }
  44%      { transform: translate(7px, -4px)   rotate(2.5deg) scale(1.1); }
  60%      { transform: translate(-5px, 2px)   rotate(-1.5deg) scale(1.06); }
  80%      { transform: translate(2px, -1px)   rotate(0.5deg) scale(1.02); }
}
@keyframes title-flash-hard {
  0%   { filter: drop-shadow(0 0 32px rgba(255, 200, 60, 0.6)); }
  18%  { filter: drop-shadow(0 0 80px white) drop-shadow(0 0 140px #ff66cc) brightness(2.2) saturate(1.6); }
  45%  { filter: drop-shadow(0 0 60px white) brightness(1.5); }
  100% { filter: drop-shadow(0 0 32px rgba(255, 200, 60, 0.6)); }
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  /* Span board + gap + sidebar so HUD centers over the whole play area. */
  width: calc(var(--cols) * (var(--cell-size) + var(--gap)) + 2 * var(--board-pad) + 18px + 230px);
}

.hud .stat {
  background: var(--panel);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: 8px 10px;
  text-align: center;
  box-shadow:
    0 4px 12px rgba(0, 0, 0, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
}
.hud .label {
  font-size: 10px;
  letter-spacing: 2px;
  text-transform: uppercase;
  opacity: 0.7;
}
.hud .value {
  font-size: 22px;
  font-weight: 800;
  color: var(--gold);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  font-variant-numeric: tabular-nums;
}
.hud .best {
  font-size: 9px;
  letter-spacing: 1px;
  text-transform: uppercase;
  opacity: 0.65;
  margin-top: 2px;
}
.hud .best span { color: var(--gold); font-weight: 700; }

.play-area {
  display: flex;
  gap: 18px;
  align-items: flex-start;
  transform-style: preserve-3d;
  transform:
    rotateX(var(--para-x, 0deg))
    rotateY(var(--para-y, 0deg));
  transition: transform 0.18s linear;
  will-change: transform;
}

#board-wrap {
  position: relative;
  padding: var(--board-pad);
  border-radius: 18px;
  background:
    linear-gradient(160deg, rgba(255, 255, 255, 0.08), rgba(0, 0, 0, 0.25));
  border: 2px solid rgba(255, 255, 255, 0.12);
  box-shadow:
    0 20px 60px rgba(0, 0, 0, 0.5),
    inset 0 2px 0 rgba(255, 255, 255, 0.15),
    inset 0 -2px 0 rgba(0, 0, 0, 0.2);
  transform-style: preserve-3d;
  transform:
    rotateX(var(--rot-x, 0deg))
    rotateY(var(--rot-y, 0deg));
  transition: transform 0.55s cubic-bezier(0.34, 1.56, 0.64, 1);
  will-change: transform;
}

#board {
  position: relative;
  width: calc(var(--cols) * (var(--cell-size) + var(--gap)) - var(--gap));
  height: calc(var(--cols) * (var(--cell-size) + var(--gap)) - var(--gap));
  overflow: hidden;
  border-radius: 6px;
  /* Critical on mobile: stops the page from scrolling/zooming when swiping a gem */
  touch-action: none;
  -webkit-touch-callout: none;
}

.cell-bg {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.18);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

.gem {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  cursor: pointer;
  transition:
    transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1),
    left 0.22s cubic-bezier(0.34, 1.56, 0.64, 1),
    top 0.32s cubic-bezier(0.45, 0.05, 0.55, 0.95),
    opacity 0.18s;
  /* Single drop-shadow only — composing 3 was killing perf during cascades */
  filter: drop-shadow(0 5px 6px rgba(0, 0, 0, 0.5));
}
/* Ground shadow under each gem — sells the "floating above the cell" feel */
.gem::after {
  content: '';
  position: absolute;
  left: 18%;
  right: 18%;
  bottom: 4%;
  height: 7px;
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.55) 0%, rgba(0, 0, 0, 0.2) 50%, transparent 80%);
  z-index: -1;
  pointer-events: none;
}
@media (hover: hover) {
  .gem:hover {
    transform: scale(1.1) translateY(-3px);
    filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.55)) brightness(1.12);
    z-index: 5;
  }
}
/* Slightly smaller visual content inside the cell so gems breathe */
.gem .gem-svg, .gem .gem-overlay {
  transform: scale(0.88);
  transform-origin: center center;
}
.gem.selected {
  animation: selected-pulse 0.6s ease-in-out infinite alternate;
  z-index: 10;
}
@keyframes selected-pulse {
  from { transform: scale(1.05); filter: drop-shadow(0 0 6px #fff) drop-shadow(0 4px 6px rgba(0, 0, 0, 0.45)); }
  to   { transform: scale(1.18); filter: drop-shadow(0 0 18px #fff) drop-shadow(0 8px 14px rgba(0, 0, 0, 0.5)) brightness(1.2); }
}
.gem.matching {
  animation: gem-pop 0.42s ease-out forwards;
  pointer-events: none;
  z-index: 20;
}
@keyframes gem-pop {
  0%   { transform: scale(1); opacity: 1; }
  35%  { transform: scale(1.4) rotate(15deg); opacity: 1; filter: drop-shadow(0 0 30px white) brightness(1.6); }
  100% { transform: scale(0) rotate(180deg); opacity: 0; }
}
.gem.invalid-swap {
  animation: shake 0.35s ease-in-out;
}
.gem.hint {
  animation: hint-pulse 0.7s ease-in-out infinite alternate;
  z-index: 8;
}
@keyframes hint-pulse {
  from { transform: scale(1); filter: drop-shadow(0 5px 6px rgba(0, 0, 0, 0.5)); }
  to   { transform: scale(1.18); filter: drop-shadow(0 0 18px white) drop-shadow(0 0 30px #ffd860) brightness(1.4); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px) rotate(-8deg); }
  50% { transform: translateX(6px) rotate(8deg); }
  75% { transform: translateX(-3px) rotate(-4deg); }
}
.gem.spawning {
  animation: spawn-bounce 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes spawn-bounce {
  from { transform: scale(0); }
  to   { transform: scale(1); }
}

.gem-svg, .gem-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.gem-overlay { z-index: 2; }

.gem-overlay.rainbow {
  animation: rainbow-spin 2.5s linear infinite;
}
@keyframes rainbow-spin {
  0%   { filter: hue-rotate(0deg) drop-shadow(0 0 6px #fff); }
  100% { filter: hue-rotate(360deg) drop-shadow(0 0 6px #fff); }
}

.gem.special {
  animation: special-glow 1.4s ease-in-out infinite alternate;
}
@keyframes special-glow {
  from { filter: drop-shadow(0 0 4px rgba(255, 255, 255, 0.5)) drop-shadow(0 4px 6px rgba(0, 0, 0, 0.45)); }
  to   { filter: drop-shadow(0 0 14px rgba(255, 255, 255, 0.95)) drop-shadow(0 4px 6px rgba(0, 0, 0, 0.45)); }
}

#particles {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}

.controls {
  display: flex;
  gap: 10px;
  align-items: center;
}
button.btn {
  background: linear-gradient(180deg, #ffe27d 0%, var(--gold) 50%, #d88500 100%);
  color: #4a2200;
  font-weight: 900;
  border: 2px solid rgba(0, 0, 0, 0.2);
  border-radius: 24px;
  padding: 8px 18px;
  font-size: 14px;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow:
    0 4px 0 #8a4d00,
    0 8px 14px rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
  transition: transform 0.1s, box-shadow 0.1s;
}
@media (hover: hover) {
  button.btn:hover { filter: brightness(1.07); }
}
button.btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #8a4d00, 0 2px 4px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.toast {
  position: absolute;
  left: 50%;
  top: 30%;
  transform: translate(-50%, -50%) scale(0.5);
  font-size: 38px;
  font-weight: 900;
  color: #fff;
  text-shadow:
    0 0 18px rgba(255, 200, 60, 1),
    0 4px 0 rgba(0, 0, 0, 0.5);
  background: linear-gradient(180deg, #fff7c8, var(--gold), #d88500);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 0 12px rgba(255, 200, 60, 0.9));
  pointer-events: none;
  opacity: 0;
  z-index: 200;
  letter-spacing: 2px;
}
.toast.show {
  animation: toast-in 1.2s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
@keyframes toast-in {
  0%   { transform: translate(-50%, -50%) scale(0.3) rotate(-15deg); opacity: 0; }
  20%  { transform: translate(-50%, -50%) scale(1.3) rotate(8deg); opacity: 1; }
  35%  { transform: translate(-50%, -50%) scale(1) rotate(0); opacity: 1; }
  80%  { transform: translate(-50%, -50%) scale(1) rotate(0); opacity: 1; }
  100% { transform: translate(-50%, -80%) scale(0.9); opacity: 0; }
}

/* Big combo multiplier banner */
.combo-mult {
  position: absolute;
  top: 18%;
  left: 50%;
  font-size: 64px;
  font-weight: 900;
  letter-spacing: 2px;
  color: #fff;
  background: linear-gradient(180deg, #fff7c8, var(--gold), #d88500);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 0 16px rgba(255, 220, 100, 1)) drop-shadow(0 4px 6px rgba(0, 0, 0, 0.6));
  pointer-events: none;
  z-index: 250;
  animation: combo-mult 0.85s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.combo-mult.mega {
  font-size: 88px;
  filter: drop-shadow(0 0 28px white) drop-shadow(0 0 60px #ff66cc) drop-shadow(0 4px 6px rgba(0, 0, 0, 0.6));
}
@keyframes combo-mult {
  0%   { transform: translate(-50%, -50%) scale(0.2) rotate(-12deg); opacity: 0; }
  18%  { transform: translate(-50%, -50%) scale(1.4) rotate(8deg); opacity: 1; }
  50%  { transform: translate(-50%, -50%) scale(1) rotate(-2deg); opacity: 1; }
  100% { transform: translate(-50%, -120%) scale(1.4) rotate(0); opacity: 0; }
}

/* Achievement popup (bottom right) */
.achievement-toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 12px 18px 12px 14px;
  background: linear-gradient(160deg, #2a1854 0%, #4a1568 100%);
  border: 2px solid var(--gold);
  border-radius: 14px;
  box-shadow: 0 0 30px rgba(255, 220, 100, 0.7), 0 8px 24px rgba(0, 0, 0, 0.6);
  z-index: 500;
  transform: translateX(120%);
  opacity: 0;
  transition: transform 0.55s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.4s;
  min-width: 240px;
}
.achievement-toast.show { transform: translateX(0); opacity: 1; }
.achievement-toast .ach-icon {
  font-size: 38px;
  filter: drop-shadow(0 0 6px rgba(255, 220, 100, 0.9));
}
.achievement-toast .ach-label {
  font-size: 10px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--gold);
  opacity: 0.85;
}
.achievement-toast .ach-name {
  font-size: 16px;
  font-weight: 800;
  color: white;
  margin-top: 2px;
}

.score-popup {
  position: absolute;
  pointer-events: none;
  font-size: 22px;
  font-weight: 900;
  color: var(--gold);
  text-shadow: 0 0 8px #fff, 0 2px 0 rgba(0, 0, 0, 0.8);
  z-index: 150;
  animation: score-rise 0.9s ease-out forwards;
}
@keyframes score-rise {
  0%   { transform: translateY(0) scale(0.5); opacity: 0; }
  20%  { transform: translateY(-10px) scale(1.2); opacity: 1; }
  100% { transform: translateY(-50px) scale(1); opacity: 0; }
}

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 0, 30, 0.85);
  backdrop-filter: blur(8px);
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 20px;
  z-index: 1000;
}
#overlay.show { display: flex; }
#overlay h2 {
  font-size: 56px;
  margin: 0;
  background: linear-gradient(180deg, #fff7c8, var(--gold), #d88500);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 0 20px var(--gold));
}
#overlay .final-score {
  font-size: 36px;
  font-weight: 900;
  color: #fff;
}

/* ===== Specials sidebar ===== */
#specials-panel {
  width: 230px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  border-radius: 16px;
  background: linear-gradient(160deg, rgba(255, 255, 255, 0.08), rgba(0, 0, 0, 0.25));
  border: 2px solid rgba(255, 255, 255, 0.12);
  box-shadow:
    0 14px 36px rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.15);
  transform-style: preserve-3d;
}
#specials-panel .panel-title {
  font-size: 11px;
  letter-spacing: 2px;
  text-transform: uppercase;
  text-align: center;
  opacity: 0.7;
  padding: 4px 0 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  margin-bottom: 4px;
}
.spec-entry {
  position: relative;
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.08);
  transition: transform 0.2s, box-shadow 0.2s, background 0.4s, filter 0.5s, opacity 0.5s;
  overflow: hidden;
}
.spec-entry .spec-lock {
  position: absolute;
  top: 6px;
  right: 8px;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
}
.spec-entry.locked {
  filter: grayscale(1) brightness(0.55);
  opacity: 0.5;
}
.spec-entry.locked .spec-lock { opacity: 0.85; }
.spec-entry.locked .spec-name { color: rgba(255, 255, 255, 0.6); }
/* Newly unlocked: gold burst expanding ring */
.spec-entry.unlocked {
  animation: spec-unlock 1.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes spec-unlock {
  0%   { transform: scale(0.85); filter: brightness(2.2); box-shadow: 0 0 0 rgba(255, 220, 100, 0); background: rgba(255, 220, 100, 0.6); }
  30%  { transform: scale(1.15); filter: brightness(1.6); box-shadow: 0 0 40px rgba(255, 220, 100, 1), 0 0 80px rgba(255, 220, 100, 0.6); background: rgba(255, 220, 100, 0.4); }
  100% { transform: scale(1); filter: brightness(1); box-shadow: 0 0 0 rgba(255, 220, 100, 0); background: rgba(0, 0, 0, 0.2); }
}
.spec-entry .spec-icon {
  position: relative;
  width: 56px;
  height: 56px;
  flex: 0 0 56px;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.5));
}
.spec-entry .spec-icon svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.spec-entry .spec-info { min-width: 0; }
.spec-entry .spec-name {
  font-weight: 800;
  color: var(--gold);
  font-size: 14px;
  letter-spacing: 1px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}
.spec-entry .spec-desc {
  font-size: 11px;
  opacity: 0.85;
  line-height: 1.35;
  margin-top: 2px;
}
.spec-entry::before {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: 12px;
  pointer-events: none;
  background: radial-gradient(circle at 50% 50%, rgba(255, 240, 180, 0.7) 0%, transparent 70%);
  opacity: 0;
  transition: opacity 0.4s;
}
.spec-entry.fire {
  animation: spec-fire 0.85s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.spec-entry.fire::before { animation: spec-fire-glow 0.85s ease-out; }
.spec-entry.fire .spec-icon { animation: spec-icon-pop 0.85s cubic-bezier(0.34, 1.56, 0.64, 1); }

@keyframes spec-fire {
  0%   { transform: scale(1); background: rgba(0, 0, 0, 0.2); box-shadow: 0 0 0 rgba(255, 220, 100, 0); }
  18%  { transform: scale(1.08); background: rgba(255, 220, 100, 0.4); box-shadow: 0 0 24px rgba(255, 220, 100, 0.9); }
  100% { transform: scale(1); background: rgba(0, 0, 0, 0.2); box-shadow: 0 0 0 rgba(255, 220, 100, 0); }
}
@keyframes spec-fire-glow {
  0%   { opacity: 0; }
  20%  { opacity: 1; }
  100% { opacity: 0; }
}
@keyframes spec-icon-pop {
  0%   { transform: scale(1) rotate(0); }
  25%  { transform: scale(1.4) rotate(-15deg); filter: drop-shadow(0 0 16px white) brightness(1.5); }
  60%  { transform: scale(1) rotate(8deg); }
  100% { transform: scale(1) rotate(0); }
}

/* ===== Responsive: tablet & landscape ===== */
@media (max-width: 900px) {
  .play-area { flex-direction: column; align-items: center; }
  #specials-panel {
    width: calc(var(--cols) * (var(--cell-size) + var(--gap)) + 2 * var(--board-pad));
    max-width: 100%;
  }
  .hud {
    width: calc(var(--cols) * (var(--cell-size) + var(--gap)) + 2 * var(--board-pad));
  }
}

@media (max-width: 800px) {
  h1.title { font-size: 46px; letter-spacing: 1px; }
  .title-wrap { padding: 20px 40px 26px; }
}

/* ===== Touch devices: kill heavy filters ===== */
@media (pointer: coarse) {
  /* drop-shadow on every gem destroys iOS Safari perf — use a baked rect shadow */
  .gem { filter: none; }
  .gem::after { display: none; }
  /* Title text-shadow stack is brutal on iOS — cut to 2 layers */
  h1.title {
    text-shadow:
      0 2px 0 #b89200,
      0 4px 0 #886500,
      0 6px 8px rgba(0, 0, 0, 0.55);
    filter: none;
    -webkit-text-stroke: 0;
  }
  .title-wrap { animation: none; transform: none; }
  .title-wrap::before { filter: blur(6px); animation: halo-pulse 4s ease-in-out infinite; }
  /* Combo tilt is over-the-top on small screens — disabled via JS too, kill the transition */
  #board-wrap { transition: none; }
  /* Camera shake also costs — keep on combos but lighter; just keep the var */
}

/* ===== Mobile portrait ===== */
@media (max-width: 600px) {
  #app { gap: 8px; padding: 8px; }
  h1.title { font-size: 28px; letter-spacing: 1px; }
  .title-wrap { padding: 10px 18px 14px; margin: 0; }

  /* HUD becomes a tighter 2x2 grid */
  .hud {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }
  .hud .stat { padding: 4px 6px; }
  .hud .label { font-size: 9px; letter-spacing: 1px; }
  .hud .value { font-size: 16px; }
  .hud .best { font-size: 8px; }

  /* Specials panel: 5 cards splitting the full width equally, no scroll */
  #specials-panel {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
    padding: 10px 12px;
    box-sizing: border-box;
  }
  #specials-panel .panel-title { display: none; }
  .spec-entry {
    flex-direction: column;
    align-items: center;
    text-align: center;
    width: auto;
    min-width: 0; /* lets grid shrink the cell */
    padding: 6px 2px;
    gap: 3px;
  }
  .spec-entry .spec-icon { width: 42px; height: 42px; flex-basis: 42px; }
  .spec-entry .spec-info { width: 100%; min-width: 0; }
  .spec-entry .spec-name {
    font-size: 10px;
    letter-spacing: 0.5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .spec-entry .spec-desc { display: none; } /* too long for cards */
  .spec-entry .spec-lock { top: 2px; right: 4px; font-size: 10px; }

  /* Controls wrap nicely */
  .controls { flex-wrap: wrap; justify-content: center; }
  button.btn { padding: 7px 12px; font-size: 12px; letter-spacing: 0.5px; }

  /* Trim heavy combo banner */
  .combo-mult { font-size: 44px; }
  .combo-mult.mega { font-size: 60px; }

  /* Smaller toast */
  .toast { font-size: 26px; }

  /* Achievement toast bigger touch margin */
  .achievement-toast { left: 10px; right: 10px; bottom: 10px; min-width: 0; }
}

/* ===== Tutorial overlay ===== */
#tutorial {
  position: fixed;
  inset: 0;
  background: rgba(10, 0, 30, 0.88);
  backdrop-filter: blur(12px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 16px;
  box-sizing: border-box;
}
#tutorial.show { display: flex; }
.tut-card {
  background: linear-gradient(160deg, #2a1854 0%, #4a1568 100%);
  border: 2px solid var(--gold);
  border-radius: 18px;
  padding: 28px 26px;
  max-width: 460px;
  width: 100%;
  box-sizing: border-box;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6), 0 0 40px rgba(255, 220, 100, 0.3);
}
.tut-card h2 {
  margin: 0 0 16px;
  background: linear-gradient(180deg, #fff7c8, var(--gold), #d88500);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  font-size: 28px;
}
.tut-card p {
  font-size: 14px;
  line-height: 1.55;
  margin: 10px 0;
  opacity: 0.92;
}
.tut-card .btn { margin-top: 18px; }
