:root {
  --serif: "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --ivory: #fbf5e6;
  --ivory-2: #ecdfbf;
  --ivory-edge: #d6c393;
  --indigo: #2b1c60;
  --indigo-2: #1a1042;
  --indigo-edge: #140c33;
  --gold: #e9c05a;
  --gold-hi: #fff1b8;
  --ink: #241418;
  --paper: rgba(22, 12, 18, 0.86);
  --good: #6dff9e;
  --bad: #ff2f2f;
  --hud: 100px;
  font-family: var(--serif);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #140d10;
  color: var(--ivory);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

body.playing { touch-action: none; }

[hidden] { display: none !important; }

/* ---------- the table ---------- */

#table {
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at 50% 45%, #5a1420 0%, #2c0a12 55%, #120607 100%);
  background-size: cover;
  background-position: center;
}

/* candle-light vignette over the cloth */
#table::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at 50% 50%, rgba(0, 0, 0, 0) 45%, rgba(0, 0, 0, 0.55) 100%);
  pointer-events: none;
}

#fx {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 5;
}

/* ---------- panels (setup, win) ---------- */

.panel {
  position: relative;
  background: var(--paper);
  border: 2px solid var(--gold);
  outline: 1px solid rgba(233, 192, 90, 0.5);
  outline-offset: 5px;
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.7), inset 0 0 40px rgba(233, 192, 90, 0.08);
  padding: clamp(18px, 4vmin, 36px);
  backdrop-filter: blur(3px);
}

h1, h2 {
  margin: 0 0 0.3em;
  font-weight: 700;
  color: var(--gold);
  text-shadow: 0 0 18px rgba(233, 192, 90, 0.45), 0 2px 0 #5a3a0a;
  letter-spacing: 0.02em;
}

button {
  font: inherit;
  cursor: pointer;
}

#start, #continue {
  display: block;
  width: 100%;
  margin-top: 14px;
  padding: 0.6em 1em;
  font-size: clamp(20px, 3vmin, 28px);
  font-weight: 700;
  color: #2a1606;
  background: linear-gradient(180deg, #ffe39a, #e2ae3c 55%, #b9821c);
  border: 2px solid #fff1b8;
  border-radius: 10px;
  box-shadow: 0 4px 0 #7a4d0c, 0 8px 22px rgba(0, 0, 0, 0.5), 0 0 24px rgba(233, 192, 90, 0.35);
}

#start:active, #continue:active { transform: translateY(3px); box-shadow: 0 1px 0 #7a4d0c, 0 4px 12px rgba(0, 0, 0, 0.5); }

#start:disabled {
  filter: grayscale(0.85) brightness(0.7);
  cursor: not-allowed;
}

/* ---------- setup ---------- */

#setup {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 3;
  user-select: text;
  -webkit-user-select: text;
}

#setup .panel {
  width: min(640px, 100%);
}

#setup h1 { font-size: clamp(34px, 6vmin, 56px); text-align: center; }

.lede {
  margin: 0 0 14px;
  text-align: center;
  color: #e9dcc0;
  font-size: clamp(15px, 2.2vmin, 19px);
  line-height: 1.4;
}

.title-tiles {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-bottom: 10px;
}

.mini {
  width: 44px;
  height: 59px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  font-weight: 700;
  font-size: 34px;
  color: var(--gold);
  background: radial-gradient(circle at 35% 25%, #4b37a0, var(--indigo) 55%, var(--indigo-2));
  box-shadow: inset 0 0 0 2px var(--indigo-2), inset 0 0 0 3px var(--gold), 0 3px 0 var(--indigo-edge), 0 6px 0 var(--ivory-edge), 0 10px 14px rgba(0, 0, 0, 0.6);
  text-shadow: 0 0 10px rgba(233, 192, 90, 0.6);
  animation: bob 3s ease-in-out infinite;
}

.mini:nth-child(2) { animation-delay: -1s; transform: translateY(-4px); }
.mini:nth-child(3) { animation-delay: -2s; }

@keyframes bob {
  50% { transform: translateY(-6px) rotate(2deg); }
}

#words {
  width: 100%;
  font: 18px/1.4 var(--serif);
  padding: 10px 12px;
  color: var(--ink);
  background: var(--ivory);
  border: 2px solid var(--ivory-edge);
  border-radius: 8px;
  resize: vertical;
}

#words:focus { outline: 3px solid var(--gold); outline-offset: 1px; }

.note {
  margin: 6px 0 10px;
  min-height: 1.3em;
  font-size: 15px;
  color: #cfe9c4;
}

.note.warn { color: #ffb38a; }

.difficulty {
  border: 0;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

@media (max-width: 560px) { .difficulty { grid-template-columns: repeat(2, 1fr); } }

.difficulty legend {
  margin-bottom: 6px;
  color: var(--gold);
  font-weight: 700;
}

.difficulty label { position: relative; }

.difficulty input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.difficulty label span {
  display: grid;
  gap: 2px;
  text-align: center;
  padding: 10px 6px;
  border-radius: 10px;
  border: 2px solid rgba(233, 192, 90, 0.35);
  background: rgba(255, 255, 255, 0.04);
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.difficulty b { font-size: 19px; }
.difficulty small { font-size: 13px; color: #d8c9a8; }

.difficulty input:checked + span {
  border-color: var(--gold);
  background: rgba(233, 192, 90, 0.18);
  box-shadow: 0 0 16px rgba(233, 192, 90, 0.4);
}

.difficulty input:focus-visible + span { outline: 3px solid var(--gold-hi); }

/* the turn-time slider */
.turn-time {
  display: grid;
  gap: 4px;
  margin: 14px 0 4px;
  color: #f0e3c6;
  font-size: 15px;
}

.turn-time b { color: var(--gold); }

.turn-time input {
  width: 100%;
  height: 32px;
  margin: 0;
  accent-color: var(--gold);
  cursor: pointer;
}

.turn-time .ends {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #d8c9a8;
  margin-top: -4px;
}

.rules {
  margin: 14px 0 0;
  font-size: 14px;
  color: #b9a98c;
  text-align: center;
  line-height: 1.4;
}

/* ---------- the play screen ---------- */

.hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--hud);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 max(12px, 2vmin);
  z-index: 2;
}

.pairs {
  display: grid;
  justify-items: start;
  line-height: 1.05;
  color: var(--gold);
  text-shadow: 0 2px 6px #000, 0 0 14px rgba(233, 192, 90, 0.4);
}

.pairs .label { font-size: calc(var(--hud) * 0.17); color: #e9dcc0; letter-spacing: 0.08em; text-transform: uppercase; }
#pairs { font-size: calc(var(--hud) * 0.34); font-weight: 700; white-space: nowrap; }
@media (max-width: 500px) { #pairs { font-size: calc(var(--hud) * 0.26); } }

.buttons {
  justify-self: end;
  display: flex;
  gap: 10px;
}

.buttons button {
  width: max(48px, calc(var(--hud) * 0.5));
  height: max(48px, calc(var(--hud) * 0.5));
  border-radius: 50%;
  font-size: calc(var(--hud) * 0.22);
  color: var(--gold);
  background: rgba(20, 10, 16, 0.7);
  border: 2px solid rgba(233, 192, 90, 0.6);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

#mute .off, #mute.muted .on { display: none; }
#mute.muted .off { display: inline; }
#music.off { opacity: 0.45; text-decoration: line-through; }

.centre {
  display: grid;
  place-items: center;
  height: 100%;
}

.centre > * { grid-area: 1 / 1; }

#hint {
  margin: 0;
  font-size: calc(var(--hud) * 0.26);
  font-style: italic;
  color: #f3e6c8;
  text-shadow: 0 2px 8px #000, 0 0 16px rgba(233, 192, 90, 0.35);
  opacity: 0;
  transition: opacity 0.4s;
}

#hint.on { opacity: 0.9; }

/* the big turn timer */
#turn-timer {
  --tc: #ffd45a;
  --ts: calc(var(--hud) * 0.9);
  position: relative;
  width: var(--ts);
  height: var(--ts);
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(20, 8, 14, 0.92) 55%, rgba(20, 8, 14, 0.6) 70%, transparent 72%);
  opacity: 0;
  transform: scale(0.5);
  transition: opacity 0.25s, transform 0.3s cubic-bezier(.3, 1.6, .5, 1);
  filter: drop-shadow(0 0 10px var(--tc)) drop-shadow(0 0 22px var(--tc));
}

#turn-timer.on { opacity: 1; transform: scale(1); }

/* wide screens (the classroom TV): the timer sits on the cloth beside the board */
body.wide #turn-timer {
  position: fixed;
  left: var(--tx);
  top: var(--ty);
  --ts: var(--tsize);
}

body.wide #turn-timer circle { stroke-width: 7; }
#turn-timer[data-level="warn"] { --tc: #ff9a2a; }
#turn-timer[data-level="urgent"] { --tc: #ff3434; }

#turn-timer svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

#turn-timer circle { fill: none; stroke-width: 9; }
#turn-timer .track { stroke: rgba(255, 255, 255, 0.14); }

#turn-timer .bar {
  stroke: var(--tc);
  stroke-linecap: round;
  transition: stroke 0.3s;
}

#turn-num {
  position: relative;
  font-size: calc(var(--ts) * 0.49);
  font-weight: 700;
  color: #fff;
  text-shadow: 0 0 12px var(--tc), 0 2px 0 rgba(0, 0, 0, 0.6);
  font-variant-numeric: tabular-nums;
}

#turn-timer.beat { animation: beat 0.5s ease-out; }

@keyframes beat {
  0% { transform: scale(1.22); }
  100% { transform: scale(1); }
}

/* ---------- the tiles ---------- */

#board {
  position: absolute;
  --tw: 100px;
  --th: 75px;
  --r: calc(var(--tw) * 0.11);
  --edge1: calc(var(--tw) * 0.035);
  --edge2: calc(var(--tw) * 0.085);
}

.tile {
  position: absolute;
  width: var(--tw);
  height: var(--th);
  perspective: calc(var(--tw) * 6);
  cursor: pointer;
  touch-action: none;
}

.flip {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.42s cubic-bezier(.35, .1, .25, 1.25);
}

.tile.up .flip { transform: rotateY(180deg); }

.face {
  position: absolute;
  inset: 0;
  border-radius: var(--r);
  display: grid;
  place-items: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}

/* face down: an indigo back with a gold "?" in a double gold border, ivory layer beneath */
.back {
  background:
    radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.16), transparent 45%),
    radial-gradient(circle at 50% 50%, #4a36a3, var(--indigo) 55%, var(--indigo-2));
  box-shadow:
    inset 0 calc(var(--tw) * -0.02) calc(var(--tw) * 0.05) rgba(0, 0, 0, 0.45),
    inset 0 calc(var(--tw) * 0.015) 0 rgba(255, 255, 255, 0.18),
    0 var(--edge1) 0 var(--indigo-edge),
    0 var(--edge2) 0 var(--ivory-edge),
    0 calc(var(--edge2) + 2px) 0 #a8946a,
    0 calc(var(--tw) * 0.14) calc(var(--tw) * 0.14) rgba(0, 0, 0, 0.65);
  overflow: visible;
}

.back::before, .back::after {
  content: "";
  position: absolute;
  border-radius: calc(var(--r) * 0.7);
  pointer-events: none;
}

.back::before { inset: calc(var(--th) * 0.07); border: max(2px, calc(var(--tw) * 0.022)) solid var(--gold); box-shadow: 0 0 8px rgba(233, 192, 90, 0.35); }
.back::after { inset: calc(var(--th) * 0.11); border: max(1px, calc(var(--tw) * 0.01)) solid rgba(233, 192, 90, 0.7); }

.q {
  font-size: calc(var(--th) * 0.62);
  font-weight: 700;
  line-height: 1;
  margin-top: calc(var(--tw) * -0.02);
  color: var(--gold);
  background: linear-gradient(180deg, #fff3c2 0%, #f1c95a 45%, #b07a18 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 calc(var(--tw) * 0.012) 0 #3a2400) drop-shadow(0 0 calc(var(--tw) * 0.06) rgba(233, 192, 90, 0.55));
}

/* face up: carved ivory, indigo layer beneath */
.front {
  transform: rotateY(180deg);
  background:
    radial-gradient(circle at 30% 18%, rgba(255, 255, 255, 0.9), transparent 50%),
    linear-gradient(165deg, var(--ivory) 0%, #f3e9cf 55%, var(--ivory-2) 100%);
  box-shadow:
    inset 0 0 0 max(2px, calc(var(--tw) * 0.02)) rgba(255, 255, 255, 0.7),
    inset 0 calc(var(--tw) * -0.03) calc(var(--tw) * 0.06) rgba(140, 105, 40, 0.28),
    0 var(--edge1) 0 var(--ivory-edge),
    0 var(--edge2) 0 var(--indigo),
    0 calc(var(--edge2) + 2px) 0 var(--indigo-edge),
    0 calc(var(--tw) * 0.14) calc(var(--tw) * 0.14) rgba(0, 0, 0, 0.65);
  overflow: visible;
}

/* an engraved border, like the frame cut into a mahjong tile's face */
.front::before {
  content: "";
  position: absolute;
  inset: calc(var(--th) * 0.07);
  border-radius: calc(var(--r) * 0.6);
  border: max(1px, calc(var(--tw) * 0.012)) solid rgba(43, 28, 96, 0.28);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.8);
  pointer-events: none;
}

.word {
  display: block;
  max-width: 92%;
  white-space: pre;   /* a phrase may be set on two lines (board.js setWord) */
  text-align: center;
  font-weight: 700;
  line-height: 1.08;
  color: var(--ink);
  /* carved: a light edge below, a dark shadow above */
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.85), 0 -1px 0 rgba(80, 40, 20, 0.35);
}

@media (hover: hover) {
  .tile:not(.up):hover .flip { transform: translateY(-3px); }
}

.tile:not(.up):active .flip { transform: translateY(1px) scale(0.98); }

/* the row numbers and column letters round the board */
.cell-label {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: calc(var(--gutter) * 0.8);
  font-weight: 700;
  line-height: 1;
  color: var(--gold-hi);
  text-shadow: 0 2px 4px #000, 0 0 10px rgba(0, 0, 0, 0.9), 0 0 16px rgba(233, 192, 90, 0.5);
  pointer-events: none;
}

/* ---------- glows ---------- */

.glow {
  position: absolute;
  inset: 0;
  border-radius: var(--r);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.tile.waiting, .tile.miss, .tile.expire { z-index: 3; }
.tile.matched { z-index: 2; }

/* the tile waiting for its partner */
.tile.waiting .glow {
  opacity: 1;
  box-shadow: 0 0 0 2px var(--gold-hi), 0 0 calc(var(--tw) * 0.18) calc(var(--tw) * 0.04) rgba(255, 210, 90, 0.7);
}

.tile.matched .glow {
  opacity: 1;
  box-shadow:
    0 0 0 3px #f4ffd6,
    0 0 calc(var(--tw) * 0.08) calc(var(--tw) * 0.02) rgba(255, 222, 90, 0.8),
    0 0 calc(var(--tw) * 0.18) calc(var(--tw) * 0.04) rgba(70, 255, 140, 0.4);
  animation: match-burst 1.4s ease-out;
}

@keyframes match-burst {
  0% {
    box-shadow:
      0 0 0 5px #ffffff,
      0 0 calc(var(--tw) * 0.2) calc(var(--tw) * 0.06) #ffe066,
      0 0 calc(var(--tw) * 0.4) calc(var(--tw) * 0.12) rgba(70, 255, 140, 0.8);
  }
  40% {
    box-shadow:
      0 0 0 4px #fbffe8,
      0 0 calc(var(--tw) * 0.14) calc(var(--tw) * 0.04) #ffd84a,
      0 0 calc(var(--tw) * 0.3) calc(var(--tw) * 0.08) rgba(70, 255, 140, 0.6);
  }
}

.tile.matched .flip { animation: match-hop 0.6s cubic-bezier(.3, 1.6, .5, 1); transform: rotateY(180deg); }

@keyframes match-hop {
  0% { transform: rotateY(180deg) scale(1); }
  40% { transform: rotateY(180deg) scale(1.12) translateY(-6%); }
  100% { transform: rotateY(180deg) scale(1); }
}

.tile.matched .front {
  box-shadow:
    inset 0 0 calc(var(--tw) * 0.12) rgba(80, 210, 120, 0.55),
    inset 0 0 0 max(2px, calc(var(--tw) * 0.02)) rgba(255, 255, 255, 0.7),
    0 var(--edge1) 0 var(--ivory-edge),
    0 var(--edge2) 0 var(--indigo),
    0 calc(var(--edge2) + 2px) 0 var(--indigo-edge),
    0 calc(var(--tw) * 0.14) calc(var(--tw) * 0.14) rgba(0, 0, 0, 0.65);
  background:
    radial-gradient(circle at 30% 18%, rgba(255, 255, 255, 0.95), transparent 50%),
    linear-gradient(165deg, #fffbe8 0%, #f6edc8 55%, #ebdca6 100%);
}

.tile.miss .glow {
  opacity: 1;
  box-shadow:
    0 0 0 4px #ffd4d4,
    0 0 calc(var(--tw) * 0.2) calc(var(--tw) * 0.09) var(--bad),
    0 0 calc(var(--tw) * 0.6) calc(var(--tw) * 0.24) rgba(255, 30, 30, 0.75);
  animation: miss-throb 0.55s ease-in-out 2 alternate;
}

@keyframes miss-throb {
  from { filter: brightness(1.4); }
  to { filter: brightness(0.85); }
}

.tile.miss { animation: shake 0.5s cubic-bezier(.36, .07, .19, .97); }

.tile.miss .front {
  box-shadow:
    inset 0 0 calc(var(--tw) * 0.12) rgba(230, 40, 40, 0.55),
    0 var(--edge1) 0 var(--ivory-edge),
    0 var(--edge2) 0 var(--indigo),
    0 calc(var(--edge2) + 2px) 0 var(--indigo-edge),
    0 calc(var(--tw) * 0.14) calc(var(--tw) * 0.14) rgba(0, 0, 0, 0.65);
  background: linear-gradient(165deg, #fff1ec 0%, #f6d9cf 60%, #ecc3b4 100%);
}

@keyframes shake {
  10%, 90% { transform: translateX(-2%); }
  20%, 80% { transform: translateX(4%); }
  30%, 50%, 70% { transform: translateX(-6%); }
  40%, 60% { transform: translateX(6%); }
}

/* the turn ran out */
.tile.expire .glow {
  opacity: 0;
  animation: expire-flash 0.7s ease-out;
}

@keyframes expire-flash {
  0% { opacity: 1; box-shadow: 0 0 0 4px #ffe0c0, 0 0 calc(var(--tw) * 0.25) calc(var(--tw) * 0.1) #ff7a1a, 0 0 calc(var(--tw) * 0.6) calc(var(--tw) * 0.2) rgba(255, 60, 20, 0.7); }
  100% { opacity: 0; box-shadow: 0 0 0 4px #ffe0c0, 0 0 calc(var(--tw) * 0.25) calc(var(--tw) * 0.1) #ff7a1a, 0 0 calc(var(--tw) * 0.6) calc(var(--tw) * 0.2) rgba(255, 60, 20, 0.7); }
}

/* the ring that drains round the waiting tile */
.ring {
  --rc: #ffd45a;
  position: absolute;
  left: calc(var(--ring-pad, 7px) * -1);
  top: calc(var(--ring-pad, 7px) * -1);
  width: calc(100% + var(--ring-pad, 7px) * 2);
  height: calc(100% + var(--ring-pad, 7px) * 2);
  overflow: visible;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}

.tile.waiting .ring { opacity: 1; }
.tile[data-level="warn"] .ring { --rc: #ff9a2a; }
.tile[data-level="urgent"] .ring { --rc: #ff3434; }

.ring rect {
  fill: none;
  stroke: var(--rc);
  stroke-width: max(4px, calc(var(--tw) * 0.045));
  stroke-linecap: round;
  filter: drop-shadow(0 0 4px var(--rc)) drop-shadow(0 0 10px var(--rc));
}

/* everything matched: the tiles glow together */
#board.cleared .tile .glow { animation: cleared 1.2s ease-in-out infinite alternate; }

@keyframes cleared {
  to { filter: brightness(1.5) saturate(1.3); }
}

/* ---------- the special tiles ---------- */

/* special faces are dark, so they never look like a word */
.tile:not([data-kind="word"]) .front::before { border-color: rgba(233, 192, 90, 0.35); box-shadow: none; }

.tile[data-kind="ghost"] .front {
  background:
    radial-gradient(circle at 50% 40%, rgba(150, 170, 255, 0.35), transparent 60%),
    radial-gradient(circle at 50% 50%, #2c2f63 0%, #15173a 65%, #0b0c22 100%);
}

.tile[data-kind="cat"] .front {
  background:
    radial-gradient(circle at 50% 38%, rgba(217, 242, 74, 0.22), transparent 55%),
    radial-gradient(circle at 50% 50%, #4b3a5c 0%, #2a1f36 65%, #150f1c 100%);
}

.tile[data-kind="crystal"] .front {
  background:
    radial-gradient(circle at 50% 45%, rgba(190, 150, 255, 0.4), transparent 60%),
    radial-gradient(circle at 50% 50%, #3a2463 0%, #1f1240 65%, #100823 100%);
}

.tile[data-kind="key"] .front {
  background:
    radial-gradient(circle at 50% 45%, rgba(255, 210, 110, 0.3), transparent 60%),
    radial-gradient(circle at 50% 50%, #5a2a26 0%, #33150f 65%, #1c0b08 100%);
}

.tile[data-kind="hourglass"] .front {
  background:
    radial-gradient(circle at 50% 45%, rgba(130, 220, 230, 0.28), transparent 60%),
    radial-gradient(circle at 50% 50%, #1f4a52 0%, #112b31 65%, #08171a 100%);
}

.art {
  position: absolute;
  inset: 8%;
  width: 84%;
  height: 84%;
  overflow: visible;
}

.tile.active { z-index: 3; }
.tile.active .glow { opacity: 1; animation: miss-throb 0.6s ease-in-out infinite alternate; }

/* the cat arches, shakes and its eyes blaze */
.tile.active[data-kind="cat"] .glow { box-shadow: 0 0 0 4px #f4ffb0, 0 0 calc(var(--tw) * 0.3) calc(var(--tw) * 0.1) #d9f24a, 0 0 calc(var(--tw) * 0.8) calc(var(--tw) * 0.3) rgba(160, 220, 40, 0.7); }
.tile.active .cat-art { animation: cat-hiss 0.16s linear infinite; transform-origin: 50% 90%; }
.tile.active .cat-art .eyes { filter: drop-shadow(0 0 3px #eaff6a) drop-shadow(0 0 7px #d9f24a); }
.tile.active .cat-art .tail { animation: cat-tail 0.4s ease-in-out infinite alternate; transform-box: fill-box; transform-origin: 0% 100%; }

@keyframes cat-hiss {
  0%, 100% { transform: scale(1.12, 1.06) translateX(0); }
  25% { transform: scale(1.14, 1.04) translateX(-2%); }
  75% { transform: scale(1.1, 1.08) translateX(2%); }
}

@keyframes cat-tail {
  to { transform: rotate(-14deg); }
}

/* the crystal ball glows and its mist swirls */
.tile.active[data-kind="crystal"] .glow { box-shadow: 0 0 0 4px #efe4ff, 0 0 calc(var(--tw) * 0.3) calc(var(--tw) * 0.1) #b28dff, 0 0 calc(var(--tw) * 0.9) calc(var(--tw) * 0.35) rgba(140, 90, 255, 0.8); }
.tile.active .crystal-art { animation: orb-pulse 0.8s ease-in-out infinite alternate; filter: drop-shadow(0 0 calc(var(--tw) * 0.08) #c9b0ff); }
.tile.active .crystal-art .swirl { animation: swirl 1.2s linear infinite; transform-box: view-box; transform-origin: 50px 45px; }

@keyframes orb-pulse { to { transform: scale(1.1); } }
@keyframes swirl { to { transform: rotate(360deg); } }

/* the tiles the crystal ball shows glow violet */
.tile.peek { z-index: 2; }
.tile.peek .glow { opacity: 1; box-shadow: 0 0 0 3px #e6dcff, 0 0 calc(var(--tw) * 0.25) calc(var(--tw) * 0.06) rgba(160, 120, 255, 0.85); }

/* the key turns in its lock */
.tile.active[data-kind="key"] .glow { box-shadow: 0 0 0 4px var(--gold-hi), 0 0 calc(var(--tw) * 0.3) calc(var(--tw) * 0.1) var(--gold), 0 0 calc(var(--tw) * 0.8) calc(var(--tw) * 0.3) rgba(255, 190, 60, 0.75); }
.tile.active .key-art { animation: key-turn 0.7s cubic-bezier(.5, 0, .3, 1.4) forwards; filter: drop-shadow(0 0 calc(var(--tw) * 0.06) #ffe28a); }

@keyframes key-turn {
  0% { transform: scale(1) rotate(0); }
  40% { transform: scale(1.15) rotate(0); }
  100% { transform: scale(1.15) rotate(90deg); }
}

/* the hourglass turns over */
.tile.active[data-kind="hourglass"] .glow { box-shadow: 0 0 0 4px #d8fbff, 0 0 calc(var(--tw) * 0.3) calc(var(--tw) * 0.1) #7ee8fa, 0 0 calc(var(--tw) * 0.8) calc(var(--tw) * 0.3) rgba(60, 200, 230, 0.7); }
.tile.active .hourglass-art .glass { animation: hourglass-turn 0.9s cubic-bezier(.6, 0, .3, 1.2) forwards; transform-box: view-box; transform-origin: 50px 50px; }

@keyframes hourglass-turn { to { transform: rotate(180deg); } }

/* used up: faded */
.tile.spent .art { opacity: 0.4; filter: grayscale(0.7); animation: none; }
.tile.spent .art * { animation: none !important; }

/* each tile in the chain of blasts flares as it goes */
.tile.blasted { z-index: 3; }
.tile.blasted .glow { opacity: 0; animation: blast-glow 1.1s ease-out; }

@keyframes blast-glow {
  0% { opacity: 1; box-shadow: 0 0 0 8px #ffffff, 0 0 calc(var(--tw) * 0.6) calc(var(--tw) * 0.3) #ffb23a, 0 0 calc(var(--tw) * 1.6) calc(var(--tw) * 0.7) rgba(255, 60, 10, 0.9); }
  100% { opacity: 0; box-shadow: 0 0 0 0 transparent, 0 0 calc(var(--tw) * 0.2) 0 #ff8a1f, 0 0 calc(var(--tw) * 0.4) 0 rgba(255, 60, 10, 0); }
}

/* the ghost bobs gently once it is showing, and swells while it wails */
.tile[data-kind="ghost"].up .ghost-art {
  animation: ghost-float 2.4s ease-in-out infinite;
  filter: drop-shadow(0 0 calc(var(--tw) * 0.05) rgba(190, 205, 255, 0.9));
}

@keyframes ghost-float {
  50% { transform: translateY(-5%); }
}

.tile.haunting { z-index: 3; }

.tile.haunting .ghost-art {
  animation: ghost-wail 0.65s ease-in-out infinite alternate;
  filter: drop-shadow(0 0 calc(var(--tw) * 0.08) #d8e0ff) drop-shadow(0 0 calc(var(--tw) * 0.2) #8f7dff);
}

.tile.haunting .mouth { animation: ghost-mouth 0.65s ease-in-out infinite alternate; transform-box: fill-box; transform-origin: center; }

@keyframes ghost-wail {
  from { transform: scale(1.05) rotate(-4deg) translateY(-3%); }
  to { transform: scale(1.22) rotate(4deg) translateY(-8%); }
}

@keyframes ghost-mouth {
  to { transform: scaleY(1.5); }
}

.tile.haunting .glow {
  opacity: 1;
  box-shadow: 0 0 0 4px #e6e9ff, 0 0 calc(var(--tw) * 0.3) calc(var(--tw) * 0.1) #a99bff, 0 0 calc(var(--tw) * 0.8) calc(var(--tw) * 0.3) rgba(120, 90, 255, 0.75);
  animation: miss-throb 0.65s ease-in-out infinite alternate;
}

.tile.spent .ghost-art { opacity: 0.4; filter: grayscale(0.7); animation: none; }

/* an eerie mist over the table while the ghost is about */
#mist {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  opacity: 0;
  transition: opacity 0.8s;
  background:
    radial-gradient(ellipse at 30% 60%, rgba(150, 130, 255, 0.28), transparent 55%),
    radial-gradient(ellipse at 70% 40%, rgba(120, 200, 255, 0.22), transparent 55%),
    rgba(20, 10, 40, 0.35);
}

body.haunted #mist { opacity: 1; animation: mist-drift 3s ease-in-out infinite alternate; }

@keyframes mist-drift {
  to { background-position: 40px -20px, -40px 20px, 0 0; }
}

/* the blast: a white-hot flash and the whole table shaking */
#flash {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 7;
  opacity: 0;
  background: radial-gradient(circle, rgba(255, 250, 220, 0.95), rgba(255, 160, 60, 0.6) 40%, rgba(120, 20, 0, 0.3) 100%);
}

#flash.on { animation: flash 0.7s ease-out; }

@keyframes flash {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

body.quake #table, body.quake #play { animation: quake 0.7s cubic-bezier(.36, .07, .19, .97); }

@keyframes quake {
  10% { transform: translate(-12px, 6px); }
  20% { transform: translate(10px, -8px); }
  30% { transform: translate(-8px, -6px); }
  40% { transform: translate(8px, 8px); }
  55% { transform: translate(-5px, 3px); }
  70% { transform: translate(4px, -3px); }
  85% { transform: translate(-2px, 1px); }
}

/* ---------- the bony hand, the candles and the big red button ---------- */

#doom {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  --hs: 100px;
  --bs: 120px;
  --cs: 40px;
}

#doom .button, #doom .candle {
  position: absolute;
  transform: translate(-50%, -50%);
}

#doom .button { width: var(--bs); height: var(--bs); }
#doom .button svg { width: 100%; height: 100%; display: block; overflow: visible; }
#doom .button .dome { transform-box: view-box; transform-origin: 50px 50px; transition: transform 0.08s, filter 0.08s; }
#doom .button.pressed .dome { transform: scale(0.8); filter: brightness(0.7); }

/* one step to go: the button throbs red */
#doom .button.danger { animation: button-throb 0.7s ease-in-out infinite alternate; }

@keyframes button-throb {
  from { filter: drop-shadow(0 0 4px rgba(255, 40, 20, 0.6)); }
  to { filter: drop-shadow(0 0 calc(var(--bs) * 0.18) #ff2a1a) drop-shadow(0 0 calc(var(--bs) * 0.4) rgba(255, 30, 10, 0.7)); }
}

/* a candle seen from above: a round of wax, a wick and a flickering flame */
#doom .candle { width: var(--cs); height: var(--cs); }

#doom .wax {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 42% 38%, #fffaf0 0%, #f0e2c4 45%, #c9b48a 85%, #8a7550 100%);
  box-shadow: 0 calc(var(--cs) * 0.08) calc(var(--cs) * 0.12) rgba(0, 0, 0, 0.6);
}

#doom .wax::after {
  content: "";
  position: absolute;
  left: 44%;
  top: 44%;
  width: 12%;
  height: 12%;
  border-radius: 50%;
  background: #1a120c;
}

#doom .flame {
  position: absolute;
  inset: -55%;
  border-radius: 50%;
  background: radial-gradient(circle, #ffffff 0%, #fff3b0 9%, #ffc23a 18%, rgba(255, 140, 30, 0.45) 32%, rgba(255, 110, 20, 0.12) 52%, transparent 70%);
  mix-blend-mode: screen;
  animation: flicker 0.18s steps(2) infinite alternate;
  transition: opacity 0.25s;
}

@keyframes flicker {
  from { transform: scale(1) translate(0, 0); opacity: 1; }
  to { transform: scale(0.92) translate(2%, -2%); opacity: 0.85; }
}

#doom .candle.out .flame { opacity: 0; animation: none; }

#doom .smoke {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(200, 200, 210, 0.7), transparent 65%);
  opacity: 0;
}

#doom .candle.puff .smoke { animation: smoke 1.4s ease-out; }

@keyframes smoke {
  0% { opacity: 0.9; transform: scale(0.6); }
  100% { opacity: 0; transform: scale(2.6) translateY(-30%); }
}

/* the hand */
#doom .hand {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--hs);
  height: var(--hs);
  z-index: 2;
}

#doom .jump { width: 100%; height: 100%; filter: drop-shadow(0 4px 3px rgba(0, 0, 0, 0.6)); }
#doom .bones { width: 118%; height: 118%; margin: -9%; display: block; overflow: visible; }   /* the drawing has room round it */
#doom .finger { transform-box: view-box; }

/* scuttling: fingers patter and the hand bobs */
#doom .hand.walking .finger { animation: finger-walk 0.2s ease-in-out infinite alternate; }
#doom .hand.walking .f-middle, #doom .hand.walking .f-pinky { animation-delay: -0.1s; }
#doom .hand.walking .f-thumb { animation-duration: 0.26s; }
#doom .hand.walking .bones { animation: hand-bob 0.2s ease-in-out infinite alternate; }
#doom .hand.backwards .finger, #doom .hand.backwards .bones { animation-duration: 0.12s; }

@keyframes finger-walk {
  from { transform: rotate(-11deg) scaleY(1); }
  to { transform: rotate(9deg) scaleY(0.8); }
}

@keyframes hand-bob {
  from { transform: translateY(0) rotate(-3deg); }
  to { transform: translateY(-4%) rotate(3deg); }
}

/* waiting: drumming its fingertips on the table */
#doom .hand.idle .finger { animation: finger-tap 1.5s ease-in-out infinite; }
#doom .hand.idle .f-pinky { animation-delay: 0s; }
#doom .hand.idle .f-ring { animation-delay: 0.1s; }
#doom .hand.idle .f-middle { animation-delay: 0.2s; }
#doom .hand.idle .f-index { animation-delay: 0.3s; }
#doom .hand.idle .f-thumb { animation: none; }

@keyframes finger-tap {
  0%, 30%, 100% { transform: scaleY(1); }
  12% { transform: scaleY(0.74); }
}

/* one step from the button: it drums faster and twitches */
#doom .hand.idle.danger .finger { animation-duration: 0.75s; }
#doom .hand.danger .bones { animation: twitch 1.6s ease-in-out infinite; }

@keyframes twitch {
  0%, 60%, 100% { transform: none; }
  64% { transform: translate(-3%, 2%) rotate(-6deg); }
  68% { transform: translate(3%, -2%) rotate(5deg); }
  72% { transform: translate(-2%, 1%) rotate(-3deg); }
  76% { transform: none; }
}

/* rearing up over the button: fingers curl back */
#doom .hand.rearing .finger { transform: scaleY(0.72); transition: transform 0.4s ease-out; }
#doom .hand.rearing .f-thumb { transform: rotate(18deg) scaleY(0.8); }

/* the slam: flat, fingers spread wide */
#doom .hand.slam .finger { transition: transform 0.08s ease-in; }
#doom .hand.slam .f-thumb { transform: rotate(-22deg) scaleY(1.05); }
#doom .hand.slam .f-index { transform: rotate(-12deg) scaleY(1.06); }
#doom .hand.slam .f-middle { transform: rotate(-2deg) scaleY(1.06); }
#doom .hand.slam .f-ring { transform: rotate(9deg) scaleY(1.06); }
#doom .hand.slam .f-pinky { transform: rotate(20deg) scaleY(1.06); }
#doom .hand.slam { z-index: 7; }
#doom .hand.rearing { z-index: 7; }

/* after the blast: shaking the dust off */
#doom .hand.dusting .bones { animation: dust 0.35s ease-in-out 2; }

@keyframes dust {
  25% { transform: rotate(-16deg) scale(0.95); }
  75% { transform: rotate(16deg) scale(0.95); }
}

/* ---------- the alarm and the chain of blasts ---------- */

#alarm {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  opacity: 0;
  transition: opacity 0.4s;
  background: radial-gradient(ellipse at center, rgba(255, 0, 0, 0) 35%, rgba(255, 20, 10, 0.55) 100%);
}

body.alarm #alarm { opacity: 1; animation: alarm 0.7s ease-in-out infinite alternate; }
body.alarm #table { filter: brightness(0.6) saturate(1.3) sepia(0.4) hue-rotate(-20deg); transition: filter 0.4s; }

@keyframes alarm {
  from { background-color: rgba(255, 0, 0, 0); }
  to { background-color: rgba(255, 0, 0, 0.16); }
}

body.rumble #table, body.rumble #play { animation: rumble 0.12s linear infinite; }

@keyframes rumble {
  0% { transform: translate(0, 0); }
  25% { transform: translate(-4px, 3px); }
  50% { transform: translate(3px, -3px); }
  75% { transform: translate(-3px, -2px); }
  100% { transform: translate(2px, 3px); }
}

/* ---------- win ---------- */

#win {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 6;
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.25), rgba(0, 0, 0, 0.55));
  animation: fade-in 0.6s ease-out;
}

#win .panel {
  text-align: center;
  width: min(520px, 100%);
  animation: rise 0.7s cubic-bezier(.3, 1.4, .5, 1);
}

#win h2 { font-size: clamp(38px, 8vmin, 72px); }

#win-detail {
  margin: 0 0 8px;
  font-size: clamp(17px, 2.8vmin, 24px);
  color: #f0e3c6;
}

@keyframes fade-in { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(30px) scale(0.9); } }

@media (prefers-reduced-motion: reduce) {
  .mini { animation: none; }
  .tile.miss { animation: none; }
  body.quake #table, body.quake #play, body.rumble #table, body.rumble #play { animation: none; }
}
