
  :root {
    --pink: #ff5d8f;
    --purple: #8a4cff;
    --gold:  #ffb547;
    --bg-1:  #1a0a40;
    --bg-2:  #4d1f7a;
    --tile:  #2a1655;
  }
  * { box-sizing: border-box; }
  html, body { margin: 0; padding: 0; height: 100%; }
  body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
    background: var(--bg-1);
    color: #fff;
    min-height: 100vh;
    min-height: 100svh;
    user-select: none;
    -webkit-user-select: none;
    overflow-x: hidden;
  }
  /* Painted crystal-cavern backdrop (Gemini gen, 2026-05-17) on a fixed
     layer — background-attachment:fixed is broken on iOS Safari, this
     isn't. Purple radial-gradient is the fallback if the WebP fails. */
  body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: -1;
    background:
      url('./assets/bg.webp') center/cover no-repeat,
      radial-gradient(circle at 30% 20%, var(--bg-2), var(--bg-1) 70%);
  }
  body.shake { animation: bodyShake 320ms ease-in-out; }
  @keyframes bodyShake {
    0%,100% { transform: translate(0,0); }
    20% { transform: translate(-6px, 4px); }
    40% { transform: translate(7px, -3px); }
    60% { transform: translate(-5px, -2px); }
    80% { transform: translate(4px, 5px); }
  }
  header {
    text-align: center;
    padding: 14px 12px 6px;
  }
  header h1 {
    font-size: 30px; margin: 0;
    color: var(--gold);
    text-shadow: 0 0 12px rgba(255, 181, 71, 0.7), 2px 2px 0 #1a0a40;
    letter-spacing: 2px;
  }
  header .nav { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 8px; }
  header .nav a, header .nav button {
    padding: 5px 10px; background: rgba(255,255,255,0.1); color: #fff;
    border: 1px solid rgba(255,255,255,0.3); border-radius: 999px;
    text-decoration: none; font-size: 12px; font-weight: 600;
    cursor: pointer; font-family: inherit;
  }
  header .nav button.muted { opacity: 0.55; text-decoration: line-through; }

  .hud {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    max-width: 480px;
    margin: 8px auto;
    padding: 0 12px;
  }
  .hud .stat {
    background: rgba(255,255,255,0.1);
    border: 2px solid rgba(255, 181, 71, 0.4);
    border-radius: 12px;
    padding: 8px 6px;
    text-align: center;
  }
  .hud .stat .label { font-size: 11px; opacity: 0.7; text-transform: uppercase; letter-spacing: 1px; }
  .hud .stat .value { font-size: 22px; font-weight: 800; color: var(--gold); }
  .hud .stat.warn .value { color: #ff7caf; }

  .progress-wrap {
    max-width: 480px;
    margin: 6px auto 4px;
    padding: 0 12px;
  }
  .progress {
    height: 14px;
    background: rgba(255,255,255,0.08);
    border: 2px solid rgba(255, 181, 71, 0.5);
    border-radius: 999px;
    overflow: hidden;
    position: relative;
  }
  .progress .bar {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #ff7eb9 0%, #ffb547 60%, #ffe46b 100%);
    border-radius: 999px;
    transition: width 360ms cubic-bezier(.3,1.4,.5,1);
    box-shadow: 0 0 8px rgba(255, 181, 71, 0.7);
  }
  .progress .bar.full {
    animation: barFlash 800ms ease-in-out infinite;
  }
  @keyframes barFlash {
    0%,100% { filter: brightness(1); }
    50%     { filter: brightness(1.4); }
  }
  .progress-label {
    text-align: center;
    font-size: 12px;
    font-weight: 700;
    opacity: 0.85;
    margin-top: 4px;
    letter-spacing: 0.5px;
  }

  .combo-banner {
    text-align: center;
    height: 38px;
    font-size: 24px;
    font-weight: 900;
    color: var(--gold);
    text-shadow: 0 0 16px rgba(255, 181, 71, 0.9);
    pointer-events: none;
    transition: opacity 200ms, transform 200ms;
    opacity: 0;
    transform: scale(0.7);
    margin-top: 4px;
  }
  .combo-banner.show {
    opacity: 1;
    transform: scale(1.1);
  }

  .field-wrap {
    display: flex;
    justify-content: center;
    padding: 4px 12px 24px;
  }
  .field {
    --gem: 56px;
    width: calc(var(--gem) * 8 + 16px);
    height: calc(var(--gem) * 8 + 16px);
    max-width: 96vw;
    max-height: 96vw;
    background:
      linear-gradient(135deg, rgba(255,255,255,0.06), rgba(255,255,255,0.02)),
      var(--tile);
    border: 4px solid var(--gold);
    border-radius: 18px;
    box-shadow: 0 0 30px rgba(255, 181, 71, 0.4), inset 0 0 20px rgba(0,0,0,0.4);
    position: relative;
    padding: 8px;
    touch-action: none;
  }
  /* responsive gem size */
  @media (max-width: 520px) {
    .field { --gem: calc((96vw - 16px) / 8); }
  }

  .cell {
    position: absolute;
    width: var(--gem);
    height: var(--gem);
    display: flex; align-items: center; justify-content: center;
  }
  .cell::before {
    content: "";
    position: absolute;
    inset: 4px;
    border-radius: 8px;
    background: rgba(255,255,255,0.04);
    z-index: 0;
    transition: background 180ms, box-shadow 180ms;
  }
  .cell img {
    position: relative; z-index: 1;
    width: 80%; height: 80%;
    object-fit: contain;
    pointer-events: none;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
    transition: transform 220ms cubic-bezier(.5,1.6,.5,1);
  }
  .cell.selected::before {
    background: radial-gradient(circle, rgba(255, 230, 120, 0.5) 0%, rgba(255, 181, 71, 0.2) 80%);
    box-shadow: 0 0 14px gold inset, 0 0 14px gold;
    animation: pulse 0.6s ease-in-out infinite alternate;
  }
  .cell.selected img {
    transform: scale(1.18);
  }
  @keyframes pulse {
    from { transform: scale(1); }
    to   { transform: scale(1.06); }
  }
  .cell.bump img {
    animation: bump 240ms ease-in-out;
  }
  @keyframes bump {
    0%   { transform: scale(1); }
    50%  { transform: scale(1.25) rotate(8deg); }
    100% { transform: scale(1); }
  }
  .cell.poof img {
    animation: poof 380ms ease-out forwards;
  }
  @keyframes poof {
    0%   { transform: scale(1) rotate(0); opacity: 1; filter: brightness(1.5); }
    50%  { transform: scale(1.4) rotate(40deg); opacity: 0.9; filter: brightness(2); }
    100% { transform: scale(0.1) rotate(180deg); opacity: 0; }
  }
  /* Idle hint — a gem pair that makes a match wiggles gently. */
  .cell.hint::before {
    background: rgba(255,255,255,0.14);
    box-shadow: 0 0 12px rgba(255, 230, 120, 0.8);
  }
  .cell.hint img {
    animation: hintNudge 800ms ease-in-out infinite;
  }
  @keyframes hintNudge {
    0%, 100% { transform: scale(1); }
    50%      { transform: scale(1.16); }
  }

  /* ===== SPECIAL GEMS =====
     Striped: 4-in-a-row spawn. Clears whole row (horizontal) or
              whole column (vertical) when matched.
     Wrapped: 5-in-a-row spawn. (Wrapped gems are not implemented in
              this build — reserved.)
     Bomb:    rainbow color bomb. Clears all gems of the swapped color.
              Spawned from a 5+ in a row match.
     The visual treatment is an overlay on top of the existing gem
     sprite so any of the 6 colors can be "special". */
  .cell.special::before {
    background: radial-gradient(circle, rgba(255, 240, 150, 0.7) 0%, rgba(255, 181, 71, 0.3) 70%, transparent 100%);
    box-shadow: 0 0 16px rgba(255, 230, 120, 0.85);
    animation: specialPulse 1.2s ease-in-out infinite alternate;
  }
  @keyframes specialPulse {
    from { box-shadow: 0 0 10px rgba(255, 230, 120, 0.6); }
    to   { box-shadow: 0 0 22px rgba(255, 230, 120, 1); }
  }
  /* Striped overlay — stripes painted via repeating-linear-gradient
     on a pseudo-element so the gem PNG underneath stays visible. */
  .cell.striped-h::after,
  .cell.striped-v::after {
    content: "";
    position: absolute;
    inset: 4px;
    border-radius: 8px;
    z-index: 2;
    pointer-events: none;
    mix-blend-mode: screen;
    opacity: 0.85;
  }
  .cell.striped-h::after {
    background: repeating-linear-gradient(
      0deg,
      rgba(255,255,255,0.9) 0px,
      rgba(255,255,255,0.9) 3px,
      transparent 3px,
      transparent 9px
    );
  }
  .cell.striped-v::after {
    background: repeating-linear-gradient(
      90deg,
      rgba(255,255,255,0.9) 0px,
      rgba(255,255,255,0.9) 3px,
      transparent 3px,
      transparent 9px
    );
  }
  /* Color bomb — sprite is replaced with a rotating rainbow disc. */
  .cell.bomb img {
    visibility: hidden;
  }
  .cell.bomb::after {
    content: "";
    position: absolute;
    inset: 6px;
    border-radius: 50%;
    background:
      conic-gradient(
        #ff5d8f, #ffb547, #ffe46b, #8eff8e,
        #6ee9ff, #b88dff, #ff5d8f
      );
    z-index: 2;
    pointer-events: none;
    animation: bombSpin 2s linear infinite;
    box-shadow: 0 0 14px rgba(255, 255, 255, 0.85), inset 0 0 8px rgba(0,0,0,0.4);
  }
  @keyframes bombSpin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
  }
  /* Activation flash — when a special gem fires off its effect. */
  .cell.activate::before {
    background: radial-gradient(circle, rgba(255,255,255,0.95) 0%, rgba(255,230,120,0.6) 50%, transparent 100%);
    box-shadow: 0 0 30px white, 0 0 60px gold;
    animation: activateFlash 280ms ease-out;
  }
  @keyframes activateFlash {
    from { transform: scale(1.4); opacity: 1; }
    to   { transform: scale(2.4); opacity: 0; }
  }

  /* ===== Sprite particles — mini gem images + glow dots (no emoji). ===== */
  .pspark, .pburst, .pglow {
    position: absolute;
    pointer-events: none;
    z-index: 10;
  }
  .pspark {
    width: 18px; height: 18px;
    object-fit: contain;
    animation: spark 520ms ease-out forwards;
  }
  .pglow {
    width: 12px; height: 12px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255,255,255,0.95), rgba(255,255,255,0));
    animation: spark 480ms ease-out forwards;
  }
  .pglow.big {
    width: 20px; height: 20px;
    animation: burst 640ms ease-out forwards;
  }
  @keyframes spark {
    0%   { opacity: 1; transform: scale(0.5) translate(0, 0); }
    100% { opacity: 0; transform: scale(1.6) translate(var(--dx), var(--dy)); }
  }

  /* Bigger particle burst for special gem activations. */
  .pburst {
    width: 26px; height: 26px;
    object-fit: contain;
    animation: burst 720ms ease-out forwards;
    z-index: 12;
  }
  @keyframes burst {
    0%   { opacity: 1; transform: scale(0.4) translate(0, 0) rotate(0); }
    100% { opacity: 0; transform: scale(2.2) translate(var(--dx), var(--dy)) rotate(540deg); }
  }

  .score-pop {
    position: absolute;
    pointer-events: none;
    color: var(--gold);
    font-weight: 900;
    font-size: 22px;
    text-shadow: 2px 2px 0 #1a0a40, 0 0 8px rgba(255,181,71,0.8);
    animation: scorePop 900ms ease-out forwards;
    z-index: 11;
  }
  @keyframes scorePop {
    0%   { opacity: 1; transform: translateY(0) scale(0.8); }
    20%  { opacity: 1; transform: translateY(-6px) scale(1.2); }
    100% { opacity: 0; transform: translateY(-50px) scale(1); }
  }
  .score-pop.big {
    font-size: 32px;
    z-index: 13;
  }

  .footer-controls {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
    padding: 0 12px 12px;
  }
  .footer-controls button {
    padding: 10px 18px;
    font-size: 14px;
    font-weight: 800;
    border: none;
    border-radius: 999px;
    cursor: pointer;
    font-family: inherit;
  }
  .footer-controls .reset {
    background: #fff; color: var(--purple);
    border: 2px solid var(--gold);
  }

  .help {
    font-size: 13px;
    text-align: center;
    color: rgba(255,255,255,0.7);
    padding: 0 12px 24px;
    max-width: 480px;
    margin: 0 auto;
  }
  .help b { color: var(--gold); }

  .toast {
    position: fixed; left: 50%; bottom: 28px;
    transform: translateX(-50%);
    background: var(--purple); color: #fff;
    padding: 12px 20px; border-radius: 999px;
    font-size: 14px; font-weight: 700;
    box-shadow: 0 6px 18px rgba(0,0,0,0.4);
    opacity: 0; pointer-events: none;
    transition: opacity 200ms;
    z-index: 100; text-align: center; max-width: 88%;
  }
  .toast.show { opacity: 1; }

  /* ===== Level-complete + game-over overlay =====
     Reused for both "you cleared it!" and "out of moves" states. */
  .gameover {
    position: fixed; inset: 0;
    background: rgba(20, 5, 50, 0.85);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 50;
  }
  .gameover.show { display: flex; }
  .gameover .panel {
    background: linear-gradient(135deg, var(--bg-2), var(--bg-1));
    border: 3px solid var(--gold);
    border-radius: 18px;
    padding: 24px 28px;
    text-align: center;
    box-shadow: 0 0 40px rgba(255, 181, 71, 0.7);
    max-width: 90%;
    min-width: 280px;
  }
  .gameover h2 { color: var(--gold); margin: 0 0 8px; font-size: 28px; }
  .gameover .final { font-size: 36px; font-weight: 900; color: #fff; margin: 8px 0 6px; }
  .gameover .sub { font-size: 14px; opacity: 0.85; margin: 0 0 14px; }
  .gameover .stars {
    font-size: 56px;
    margin: 8px 0 14px;
    letter-spacing: 6px;
    min-height: 64px;
  }
  .gameover .stars .star {
    display: inline-block;
    opacity: 0.25;
    transform: scale(0.7);
    transition: opacity 280ms, transform 360ms cubic-bezier(.4,1.7,.5,1);
  }
  .gameover .stars .star.lit {
    opacity: 1;
    transform: scale(1.15) rotate(8deg);
    filter: drop-shadow(0 0 12px rgba(255, 230, 120, 0.95));
  }
  .gameover button {
    padding: 12px 24px; font-size: 16px; font-weight: 800;
    background: var(--pink); color: #fff;
    border: none; border-radius: 999px;
    cursor: pointer; font-family: inherit;
    box-shadow: 0 4px 0 #c2436d;
    margin: 4px;
  }
  .gameover button.ghost {
    background: rgba(255,255,255,0.1);
    color: #fff;
    box-shadow: none;
    border: 1px solid rgba(255,255,255,0.3);
  }

  /* Princess-Run-style title screen — full-bleed painted hero art that
     doubles as the asset-loading curtain. */
  #title-screen {
    position: fixed; inset: 0;
    z-index: 9000;
    display: grid; place-items: center;
    transition: opacity 0.5s ease;
    background: #0a1a3a;
    overflow: hidden;
  }
  #title-screen.hide { opacity: 0; pointer-events: none; }
  #title-screen #title-art {
    position: absolute; inset: 0;
    width: 100%; height: 100%;
    object-fit: cover; object-position: center;
    z-index: 0; pointer-events: none;
  }
  #title-screen #title-text {
    position: absolute; z-index: 1;
    left: 50%; top: 6vh;
    transform: translateX(-50%);
    text-align: center;
    pointer-events: none; user-select: none;
    line-height: 0.9;
    font-family: "Fredoka", "Comic Sans MS", system-ui, sans-serif;
    font-weight: 800;
    width: max-content;
  }
  #title-screen #title-text .row1,
  #title-screen #title-text .row2 {
    display: block;
    color: #fff7d6;
    letter-spacing: 2px;
    text-shadow:
      -3px 0 0 #1a2440, 3px 0 0 #1a2440,
      0 -3px 0 #1a2440, 0 3px 0 #1a2440,
      -3px -3px 0 #1a2440, 3px 3px 0 #1a2440,
      -3px 3px 0 #1a2440, 3px -3px 0 #1a2440,
      0 6px 0 rgba(0,0,0,0.35),
      0 12px 22px rgba(0,0,0,0.55);
    animation: title-bob 2.4s ease-in-out infinite alternate;
  }
  #title-screen #title-text .row1 {
    font-size: clamp(24px, 6.5vw, 76px);
    color: #ffe18f;
    margin-bottom: 4px;
  }
  #title-screen #title-text .row2 {
    font-size: clamp(40px, 11vw, 132px);
    color: #ffdf63;
    letter-spacing: 4px;
    animation-delay: 0.2s;
  }
  @keyframes title-bob {
    from { transform: translateY(0)    rotate(-1deg); }
    to   { transform: translateY(-6px) rotate(1deg); }
  }
  #title-screen .title-actions {
    position: absolute; z-index: 1;
    left: 50%; bottom: 10vh;
    transform: translateX(-50%);
    display: flex; flex-direction: column;
    align-items: center; gap: 14px;
  }
  #title-screen .wood-btn {
    appearance: none; border: none; cursor: pointer;
    font-family: "Georgia", "Times New Roman", serif;
    font-weight: 900;
    font-size: clamp(20px, 3.4vw, 34px);
    letter-spacing: 0.06em;
    color: #4a2a14;
    text-shadow: 0 1px 0 rgba(255, 240, 210, 0.7);
    padding: 0.5em 1.6em;
    border-radius: 18px;
    background: linear-gradient(180deg, #fbe6b1 0%, #f1c878 45%, #d99a3f 100%);
    box-shadow:
      0 0 0 4px #4a2a14,
      0 0 0 7px #b6803a,
      inset 0 4px 0 rgba(255, 250, 220, 0.65),
      inset 0 -6px 8px rgba(120, 60, 20, 0.45),
      0 8px 14px rgba(0, 0, 0, 0.45);
    transition: transform 0.12s ease;
  }
  #title-screen .wood-btn:hover  { transform: translateY(-2px); }
  #title-screen .wood-btn:active { transform: translateY(2px); }
  #title-screen .wood-btn:disabled { cursor: wait; filter: saturate(0.5) brightness(0.85); }
