// MultiplicationMosaicAnimations.jsx — Cammy's game animation grammar.
// 4 stacked states (Idle / Correct / Wrong / End) all looping live,
// using Multiplication Mosaic as the reference scene. Same motion
// patterns apply uniformly to all 7 of Cammy's games.

const { useMemo } = React;

/* ============================================================
   SCENE ATOMS — artist's studio
   ============================================================ */

const Window = ({ x, y }) => (
  <g transform={`translate(${x} ${y})`}>
    <rect x="0" y="0" width="220" height="160" rx="6" fill="#FBEACA" stroke="#7A5318" strokeWidth="3" />
    <rect x="0" y="0" width="220" height="160" rx="6" fill="url(#mm-window-sky)" />
    {/* mullions */}
    <line x1="110" y1="6" x2="110" y2="154" stroke="#7A5318" strokeWidth="3" />
    <line x1="6" y1="80" x2="214" y2="80" stroke="#7A5318" strokeWidth="3" />
    {/* sill */}
    <rect x="-8" y="156" width="236" height="10" rx="2" fill="#5C3F0E" />
    <rect x="-8" y="156" width="236" height="3" fill="#A07042" opacity="0.7" />
    {/* outside view — soft hill + sky */}
    <g clipPath="url(#mm-window-clip)">
      <rect x="0" y="0" width="220" height="160" fill="#FBEACA" />
      <ellipse cx="170" cy="40" rx="22" ry="22" fill="#FFEACA" opacity="0.85" />
      <path d="M0 110 Q60 88 120 100 Q180 84 220 96 L220 160 L0 160 Z" fill="#C5DCB6" opacity="0.7" />
      <ellipse cx="40" cy="40" rx="22" ry="6" fill="#FFFFFF" opacity="0.7" />
      <ellipse cx="100" cy="60" rx="18" ry="5" fill="#FFFFFF" opacity="0.55" />
    </g>
  </g>
);

const SunRay = ({ x1, y1, x2, y2, opacity = 0.18 }) => (
  <path d={`M${x1} ${y1} L${x2} ${y2} L${x2 + 40} ${y2} L${x1 + 40} ${y1} Z`} fill="#FFEACA" opacity={opacity} />
);

const Bamboo = ({ x, y }) => (
  <g transform={`translate(${x} ${y})`}>
    {/* pot */}
    <path d="M-22 0 L-26 -42 L26 -42 L22 0 Z" fill="#6B3F5E" />
    <path d="M-22 0 L-26 -42 L26 -42 L22 0 Z" fill="url(#mm-pot-light)" />
    <rect x="-28" y="-46" width="56" height="6" rx="1" fill="#4A2A42" />
    {/* dirt + stems */}
    <ellipse cx="0" cy="-42" rx="22" ry="3" fill="#3A2A1E" />
    {/* bamboo stalks */}
    <g className="anim-bamboo-sway">
      <rect x="-14" y="-92" width="6" height="50" fill="#C99B4E" />
      <line x1="-14" y1="-74" x2="-8" y2="-74" stroke="#5C3F0E" strokeWidth="1.4" />
      <line x1="-14" y1="-58" x2="-8" y2="-58" stroke="#5C3F0E" strokeWidth="1.4" />
      {/* leaves */}
      <path d="M-11 -88 Q-20 -98 -28 -90 Q-18 -86 -11 -88 Z" fill="#7AA66F" />
      <path d="M-8 -70 Q4 -76 10 -68 Q-2 -64 -8 -70 Z" fill="#5C8C52" />
    </g>
    <g className="anim-bamboo-sway-slow">
      <rect x="2" y="-110" width="6" height="68" fill="#C99B4E" />
      <line x1="2" y1="-88" x2="8" y2="-88" stroke="#5C3F0E" strokeWidth="1.4" />
      <line x1="2" y1="-66" x2="8" y2="-66" stroke="#5C3F0E" strokeWidth="1.4" />
      <path d="M5 -106 Q15 -116 22 -106 Q12 -100 5 -106 Z" fill="#7AA66F" />
      <path d="M8 -82 Q22 -84 26 -76 Q14 -76 8 -82 Z" fill="#5C8C52" />
      <path d="M5 -56 Q-7 -54 -10 -46 Q3 -48 5 -56 Z" fill="#7AA66F" />
    </g>
    <g className="anim-bamboo-sway" style={{ animationDelay: "-2s" }}>
      <rect x="14" y="-78" width="5" height="36" fill="#D9A441" />
      <line x1="14" y1="-66" x2="19" y2="-66" stroke="#5C3F0E" strokeWidth="1.4" />
      <path d="M17 -76 Q26 -82 30 -74 Q22 -70 17 -76 Z" fill="#7AA66F" />
    </g>
  </g>
);

const PaintJar = ({ x, y, color, accent }) => (
  <g transform={`translate(${x} ${y})`}>
    {/* jar */}
    <ellipse cx="0" cy="14" rx="11" ry="2.5" fill="#2C2B36" opacity="0.25" />
    <path d="M-10 -2 L-10 12 Q-10 14 -8 14 L8 14 Q10 14 10 12 L10 -2 Z" fill="#FFFFFF" opacity="0.4" stroke="#7A5318" strokeWidth="1" />
    <path d="M-9 -2 L-9 12 L9 12 L9 -2 Z" fill={color} opacity="0.75" />
    <rect x="-11" y="-4" width="22" height="3" rx="1" fill="#7A5318" />
    {/* paint surface */}
    <ellipse cx="0" cy="-2" rx="9" ry="2" fill={color} />
    <ellipse cx="-2" cy="-3" rx="3" ry="1" fill={accent} />
    {/* brush in jar */}
    <rect x="3" y="-22" width="2" height="22" fill="#7A5318" />
    <ellipse cx="4" cy="-22" rx="3" ry="4" fill={color} />
  </g>
);

const Easel = ({ children }) => (
  <g>
    {/* shadow */}
    <ellipse cx="380" cy="316" rx="80" ry="6" fill="#2C2B36" opacity="0.22" />
    {/* tripod back leg */}
    <path d="M378 130 L378 314" stroke="#7A5318" strokeWidth="4" />
    {/* front legs */}
    <path d="M380 130 L322 314" stroke="#7A5318" strokeWidth="6" />
    <path d="M380 130 L438 314" stroke="#7A5318" strokeWidth="6" />
    {/* cross brace */}
    <path d="M340 240 L420 240" stroke="#5C3F0E" strokeWidth="3" />
    <path d="M340 240 L420 240" stroke="#A07042" strokeWidth="1" opacity="0.7" />
    {/* canvas frame */}
    <rect x="300" y="124" width="160" height="124" rx="3" fill="#7A5318" />
    <rect x="304" y="128" width="152" height="116" rx="2" fill="#FBEFD4" />
    {/* canvas inset shadow */}
    <rect x="304" y="128" width="152" height="3" fill="#2C2B36" opacity="0.18" />
    {children}
    {/* easel top knob */}
    <circle cx="380" cy="124" r="4" fill="#5C3F0E" />
  </g>
);

// 4×3 mosaic grid on canvas. revealedMask is array of 12 booleans.
// One tile can be "newSnap" to play the tile-snap animation, or "dim" for wrong state.
const MosaicGrid = ({ revealed, newSnapIdx = -1, dimIdx = -1, allRevealed = false }) => {
  // 4 cols × 3 rows = 12, canvas inner area 308-452 × 132-240 → tile ~34x34
  const tileColors = ["#D9A441", "#6B3F5E", "#E07A5F", "#7AA66F", "#C99B4E", "#8A5374", "#E8B651", "#5C8C52", "#D9A441", "#6B3F5E", "#E07A5F", "#7AA66F"];
  const tilePatterns = ["dot", "chev", "leaf", "x", "tri", "dot", "x", "chev", "leaf", "tri", "dot", "chev"];

  const tiles = [];
  for (let i = 0; i < 12; i++) {
    const col = i % 4;
    const row = Math.floor(i / 4);
    const tx = 308 + col * 36;
    const ty = 132 + row * 36;
    const isRevealed = allRevealed || revealed[i] || i === newSnapIdx;
    const isNew = i === newSnapIdx;
    const isDim = i === dimIdx;
    tiles.push(
      <g key={i} transform={`translate(${tx} ${ty})`}>
        {/* tile base */}
        <rect x="0" y="0" width="34" height="34" rx="3" fill={isRevealed ? "#FFFFFF" : "#F2E8D0"} stroke="#7A5318" strokeWidth="0.8" />
        {isRevealed ? (
          <g className={isNew ? "anim-tile-snap" : isDim ? "anim-tile-dim" : ""} style={isNew ? { transformOrigin: "17px 17px" } : undefined}>
            <rect x="2" y="2" width="30" height="30" rx="2" fill={tileColors[i]} />
            <TilePattern shape={tilePatterns[i]} />
            {/* highlight */}
            <rect x="2" y="2" width="30" height="6" rx="2" fill="#FFFFFF" opacity="0.22" />
          </g>
        ) : (
          <text x="17" y="22" textAnchor="middle" fontFamily="Fredoka, sans-serif" fontWeight="700" fontSize="16" fill="#C9B68A">?</text>
        )}
      </g>
    );
  }
  return <g>{tiles}</g>;
};

const TilePattern = ({ shape }) => {
  switch (shape) {
    case "dot":
      return <g fill="#FFFFFF" opacity="0.85"><circle cx="17" cy="17" r="6" /><circle cx="17" cy="17" r="3" fill="#2C2B36" opacity="0.4" /></g>;
    case "chev":
      return <g fill="none" stroke="#FFFFFF" strokeWidth="2.5" opacity="0.85"><path d="M8 12 L17 22 L26 12" /><path d="M8 20 L17 28" /></g>;
    case "leaf":
      return <g fill="#FFFFFF" opacity="0.85"><path d="M9 24 Q17 8 25 24 Q17 28 9 24 Z" /><path d="M9 24 Q17 18 25 24" stroke="#2C2B36" strokeWidth="0.6" opacity="0.4" fill="none" /></g>;
    case "x":
      return <g stroke="#FFFFFF" strokeWidth="3" strokeLinecap="round" opacity="0.85"><path d="M10 10 L24 24" /><path d="M24 10 L10 24" /></g>;
    case "tri":
      return <g fill="#FFFFFF" opacity="0.85"><path d="M17 8 L26 26 L8 26 Z" /></g>;
    default: return null;
  }
};

const PictureFrame = ({ x, y, w = 56, h = 44, label = "" }) => (
  <g transform={`translate(${x} ${y})`}>
    <rect x="0" y="0" width={w} height={h} fill="#7A5318" />
    <rect x="3" y="3" width={w - 6} height={h - 6} fill="#FBEACA" />
    {/* mini multiplication chart hints */}
    <g stroke="#C99B4E" strokeWidth="0.8" opacity="0.7">
      <line x1="6" y1={9} x2={w - 6} y2={9} />
      <line x1="6" y1={15} x2={w - 6} y2={15} />
      <line x1="6" y1={21} x2={w - 6} y2={21} />
      <line x1="6" y1={27} x2={w - 6} y2={27} />
      <line x1="6" y1={33} x2={w - 6} y2={33} />
      <line x1={w / 2} y1="6" x2={w / 2} y2={h - 6} />
    </g>
    <text x={w / 2} y={h - 4} textAnchor="middle" fontFamily="Fredoka, sans-serif" fontWeight="600" fontSize="6" fill="#7A5318">{label}</text>
  </g>
);

const PaintPalette = ({ x, y }) => (
  <g transform={`translate(${x} ${y})`}>
    <ellipse cx="0" cy="2" rx="22" ry="3" fill="#2C2B36" opacity="0.18" />
    <ellipse cx="0" cy="0" rx="22" ry="14" fill="#7A5318" />
    <ellipse cx="0" cy="-1" rx="20" ry="12" fill="#FBEACA" />
    {/* paint blobs */}
    <circle cx="-12" cy="-3" r="4" fill="#D9A441" />
    <circle cx="-2" cy="-5" r="4" fill="#6B3F5E" />
    <circle cx="8" cy="-3" r="4" fill="#E07A5F" />
    <circle cx="14" cy="3" r="3" fill="#7AA66F" />
    {/* thumb hole */}
    <ellipse cx="-16" cy="4" rx="3" ry="2" fill="#FBEACA" stroke="#7A5318" strokeWidth="0.6" />
  </g>
);

/* ============================================================
   CAMMY PANDA — 3 poses (idle / splash / concerned / proud)
   ============================================================ */

const CammyDefs = ({ id }) => (
  <defs>
    <radialGradient id={`${id}-body`} cx="42%" cy="30%" r="82%">
      <stop offset="0%" stopColor="#FFFFFF" />
      <stop offset="60%" stopColor="#F5EAD2" />
      <stop offset="100%" stopColor="#C9B68A" />
    </radialGradient>
    <radialGradient id={`${id}-dark`} cx="38%" cy="28%" r="80%">
      <stop offset="0%" stopColor="#4A4655" />
      <stop offset="55%" stopColor="#2C2B36" />
      <stop offset="100%" stopColor="#13121A" />
    </radialGradient>
    <radialGradient id={`${id}-scarf`} cx="40%" cy="40%">
      <stop offset="0%" stopColor="#8A5374" />
      <stop offset="100%" stopColor="#4A2A42" />
    </radialGradient>
    <radialGradient id={`${id}-cheek`} cx="50%" cy="50%" r="50%">
      <stop offset="0%" stopColor="#E07A5F" stopOpacity="0.55" />
      <stop offset="100%" stopColor="#E07A5F" stopOpacity="0" />
    </radialGradient>
    <linearGradient id={`${id}-rim`} x1="0%" y1="0%" x2="0%" y2="100%">
      <stop offset="0%" stopColor="#FFFFFF" stopOpacity="0.55" />
      <stop offset="100%" stopColor="#FFFFFF" stopOpacity="0" />
    </linearGradient>
    <filter id={`${id}-blur`} x="-30%" y="-30%" width="160%" height="160%">
      <feGaussianBlur stdDeviation="2.6" />
    </filter>
  </defs>
);

const PaintBrush = ({ tipColor = "#D9A441", className = "" }) => (
  <g className={className}>
    {/* handle */}
    <rect x="-1.5" y="-26" width="3" height="40" rx="1" fill="#7A5318" />
    <rect x="-1.5" y="-26" width="1" height="40" fill="#A07042" opacity="0.6" />
    {/* ferrule */}
    <rect x="-3" y="-32" width="6" height="6" fill="#C99B4E" />
    <rect x="-3" y="-30" width="6" height="1" fill="#FBEACA" opacity="0.5" />
    {/* bristles */}
    <path d="M-3 -32 L-2.5 -42 L2.5 -42 L3 -32 Z" fill="#FBEACA" stroke="#A07042" strokeWidth="0.4" />
    {/* paint tip */}
    <path d="M-2.5 -42 Q0 -46 2.5 -42 L2 -38 Q0 -36 -2 -38 Z" fill={tipColor} />
  </g>
);

// Shared body + head shell + face base (panda)
const CammyShell = ({ id }) => (
  <>
    {/* ears */}
    <circle cx="60" cy="60" r="18" fill={`url(#${id}-dark)`} />
    <circle cx="140" cy="60" r="18" fill={`url(#${id}-dark)`} />
    <circle cx="60" cy="62" r="10" fill="#3F3D4A" />
    <circle cx="140" cy="62" r="10" fill="#3F3D4A" />
    {/* body */}
    <ellipse cx="100" cy="178" rx="58" ry="40" fill={`url(#${id}-body)`} />
    <ellipse cx="100" cy="186" rx="32" ry="22" fill="#FFFFFF" opacity="0.55" />
    {/* head */}
    <circle cx="100" cy="110" r="56" fill={`url(#${id}-body)`} />
    <ellipse cx="74" cy="78" rx="30" ry="17" fill={`url(#${id}-rim)`} />
    {/* eye patches */}
    <ellipse cx="78" cy="112" rx="15" ry="19" fill={`url(#${id}-dark)`} transform="rotate(-15 78 112)" />
    <ellipse cx="122" cy="112" rx="15" ry="19" fill={`url(#${id}-dark)`} transform="rotate(15 122 112)" />
    {/* cheeks */}
    <circle cx="58" cy="134" r="11" fill={`url(#${id}-cheek)`} />
    <circle cx="142" cy="134" r="11" fill={`url(#${id}-cheek)`} />
    {/* muzzle */}
    <ellipse cx="100" cy="138" rx="22" ry="15" fill="#FFFFFF" />
    {/* nose + Y mouth (mouth varies by pose, drawn separately) */}
  </>
);

const PandaEyesOpen = () => (
  <g>
    <ellipse cx="80" cy="112" rx="5.5" ry="7" fill="#FFFFFF" />
    <ellipse cx="120" cy="112" rx="5.5" ry="7" fill="#FFFFFF" />
    <ellipse cx="81" cy="114" rx="3.5" ry="4.8" fill="#2C2B36" />
    <ellipse cx="119" cy="114" rx="3.5" ry="4.8" fill="#2C2B36" />
    <circle cx="82.5" cy="111.5" r="1.5" fill="#FFFFFF" />
    <circle cx="120.5" cy="111.5" r="1.5" fill="#FFFFFF" />
  </g>
);

const PandaEyesSquint = () => (
  <g stroke="#FFFFFF" strokeWidth="3" strokeLinecap="round" fill="none">
    <path d="M72 113 Q80 105 88 113" />
    <path d="M112 113 Q120 105 128 113" />
  </g>
);

const PandaEyesConcerned = () => (
  <g>
    <ellipse cx="80" cy="114" rx="5" ry="6" fill="#FFFFFF" />
    <ellipse cx="120" cy="114" rx="5" ry="6" fill="#FFFFFF" />
    <ellipse cx="80" cy="116" rx="3" ry="3.8" fill="#2C2B36" />
    <ellipse cx="120" cy="116" rx="3" ry="3.8" fill="#2C2B36" />
    {/* gentle upturned inner brow */}
    <path d="M70 100 Q78 103 86 102" stroke="#FFFFFF" strokeWidth="1.6" fill="none" strokeLinecap="round" opacity="0.85" />
    <path d="M130 100 Q122 103 114 102" stroke="#FFFFFF" strokeWidth="1.6" fill="none" strokeLinecap="round" opacity="0.85" />
  </g>
);

const PandaEyesProud = () => (
  <g>
    <ellipse cx="80" cy="112" rx="5.5" ry="6" fill="#FFFFFF" />
    <ellipse cx="120" cy="112" rx="5.5" ry="6" fill="#FFFFFF" />
    {/* smug curve */}
    <path d="M75 113 Q80 115 85 113" stroke="#2C2B36" strokeWidth="2.4" fill="none" strokeLinecap="round" />
    <path d="M115 113 Q120 115 125 113" stroke="#2C2B36" strokeWidth="2.4" fill="none" strokeLinecap="round" />
    <circle cx="82" cy="110" r="1.2" fill="#FFFFFF" />
    <circle cx="120" cy="110" r="1.2" fill="#FFFFFF" />
  </g>
);

const MouthY = () => (
  <g>
    <path d="M92 130 Q100 138 108 130 Q108 139 100 141 Q92 139 92 130 Z" fill="#1A1922" />
    <path d="M100 141 L100 145" stroke="#1A1922" strokeWidth="1.6" strokeLinecap="round" />
    <path d="M100 145 Q94 151 88 147" stroke="#1A1922" strokeWidth="1.8" fill="none" strokeLinecap="round" />
    <path d="M100 145 Q106 151 112 147" stroke="#1A1922" strokeWidth="1.8" fill="none" strokeLinecap="round" />
  </g>
);

const MouthOpenSmile = () => (
  <g>
    <path d="M92 130 Q100 138 108 130 Q108 139 100 141 Q92 139 92 130 Z" fill="#1A1922" />
    <path d="M100 141 L100 144" stroke="#1A1922" strokeWidth="1.6" strokeLinecap="round" />
    <path d="M86 146 Q100 158 114 146 Q108 150 100 150 Q92 150 86 146 Z" fill="#1A1922" />
    <ellipse cx="100" cy="153" rx="3" ry="2" fill="#E07AA0" />
  </g>
);

const MouthSoft = () => (
  <g>
    <path d="M92 130 Q100 138 108 130 Q108 139 100 141 Q92 139 92 130 Z" fill="#1A1922" />
    <path d="M100 141 L100 145" stroke="#1A1922" strokeWidth="1.6" strokeLinecap="round" />
    <path d="M93 148 Q100 150 107 148" stroke="#1A1922" strokeWidth="1.7" fill="none" strokeLinecap="round" />
  </g>
);

// Plum scarf retained, mustard bamboo leaf accent retained
const CammyScarf = ({ id }) => (
  <g>
    <ellipse cx="100" cy="155" rx="50" ry="11" fill={`url(#${id}-scarf)`} />
    <path d="M130 158 Q146 168 142 184 Q138 186 134 180 Q132 170 128 168 Z" fill={`url(#${id}-scarf)`} />
    <path d="M55 152 Q100 162 145 152" stroke="#D9A441" strokeWidth="2.2" fill="none" />
  </g>
);

const BambooLeafAccent = () => (
  <g transform="translate(156 46) rotate(35)">
    <path d="M0 0 Q4 -14 16 -16 Q12 -8 6 -2 Z" fill="#D9A441" />
    <path d="M0 0 Q4 -14 16 -16" stroke="#8E6722" strokeWidth="0.8" fill="none" opacity="0.6" />
  </g>
);

// IDLE Cammy — standing, brush in right paw, head tilt, breathing
const CammyIdle = ({ id }) => (
  <svg viewBox="0 0 200 220" className="w-full h-full block">
    <CammyDefs id={id} />
    <ellipse cx="100" cy="208" rx="56" ry="6" fill="#2C2B36" opacity="0.22" filter={`url(#${id}-blur)`} />
    <g className="anim-breath">
      {/* dark arms */}
      <ellipse cx="56" cy="188" rx="15" ry="22" fill={`url(#${id}-dark)`} transform="rotate(-18 56 188)" />
      <ellipse cx="148" cy="170" rx="14" ry="20" fill={`url(#${id}-dark)`} transform="rotate(22 148 170)" />
      <ellipse cx="50" cy="204" rx="7" ry="5" fill="#1A1922" />
      <CammyScarf id={id} />
      <g transform="rotate(-6 100 110)">
        <CammyShell id={id} />
        <g className="anim-ear-twitch" style={{ transformOrigin: "60px 60px" }}>
          {/* (ear hover override - we just re-render top ear with rotation here is too complex; we let the existing ear stay) */}
        </g>
        <PandaEyesOpen />
        <MouthY />
        <BambooLeafAccent />
      </g>
      {/* paintbrush in right paw */}
      <g transform="translate(160 162)" className="anim-brush-bob">
        <PaintBrush tipColor="#D9A441" />
      </g>
    </g>
  </svg>
);

// SPLASH / CORRECT Cammy — paint joy, eyes squinted, brush raised
const CammySplash = ({ id }) => (
  <svg viewBox="0 0 200 220" className="w-full h-full block">
    <CammyDefs id={id} />
    <ellipse cx="100" cy="208" rx="48" ry="5" fill="#2C2B36" opacity="0.16" filter={`url(#${id}-blur)`} />
    {/* arms — left down, right HIGH with brush */}
    <ellipse cx="56" cy="188" rx="15" ry="22" fill={`url(#${id}-dark)`} transform="rotate(-18 56 188)" />
    <ellipse cx="155" cy="138" rx="13" ry="26" fill={`url(#${id}-dark)`} transform="rotate(34 155 138)" />
    <ellipse cx="50" cy="204" rx="7" ry="5" fill="#1A1922" />
    <CammyScarf id={id} />
    <CammyShell id={id} />
    <PandaEyesSquint />
    <MouthOpenSmile />
    <BambooLeafAccent />
    {/* brush raised */}
    <g transform="translate(176 96) rotate(28)">
      <g className="anim-brush-raise">
        <PaintBrush tipColor="#D9A441" />
      </g>
    </g>
    {/* tiny paint flecks on Cammy */}
    <g>
      <circle cx="120" cy="180" r="2.5" fill="#D9A441" />
      <circle cx="86" cy="174" r="2" fill="#6B3F5E" />
      <circle cx="100" cy="190" r="1.6" fill="#E07A5F" />
    </g>
  </svg>
);

// CONCERNED Cammy — looking down, gentle smile, head shake
const CammyConcerned = ({ id }) => (
  <svg viewBox="0 0 200 220" className="w-full h-full block">
    <CammyDefs id={id} />
    <ellipse cx="100" cy="208" rx="56" ry="6" fill="#2C2B36" opacity="0.22" filter={`url(#${id}-blur)`} />
    <g className="anim-breath">
      <ellipse cx="56" cy="188" rx="15" ry="22" fill={`url(#${id}-dark)`} transform="rotate(-18 56 188)" />
      {/* right arm holding brush in front (looking at it) */}
      <ellipse cx="138" cy="178" rx="14" ry="22" fill={`url(#${id}-dark)`} transform="rotate(28 138 178)" />
      <ellipse cx="50" cy="204" rx="7" ry="5" fill="#1A1922" />
      <CammyScarf id={id} />
      {/* head shake animation on head */}
      <g className="anim-head-shake" style={{ transformOrigin: "100px 150px" }}>
        <CammyShell id={id} />
        <PandaEyesConcerned />
        <MouthSoft />
        <BambooLeafAccent />
      </g>
      {/* paintbrush held forward, viewed */}
      <g transform="translate(150 192) rotate(-50)">
        <PaintBrush tipColor="#6B3F5E" />
      </g>
    </g>
  </svg>
);

// PROUD Cammy (end) — gestures with brush at finished canvas
const CammyProud = ({ id }) => (
  <svg viewBox="0 0 200 220" className="w-full h-full block">
    <CammyDefs id={id} />
    <ellipse cx="100" cy="208" rx="58" ry="6" fill="#2C2B36" opacity="0.22" filter={`url(#${id}-blur)`} />
    <g className="anim-proud-stand">
      <ellipse cx="56" cy="184" rx="15" ry="22" fill={`url(#${id}-dark)`} transform="rotate(-22 56 184)" />
      {/* right arm extended pointing at canvas */}
      <g style={{ transformOrigin: "138px 170px" }}>
        <g className="anim-brush-flourish">
          <ellipse cx="20" cy="40" rx="14" ry="24" fill={`url(#${id}-dark)`} transform="translate(118 130) rotate(-40)" />
          <g transform="translate(80 102)">
            <PaintBrush tipColor="#D9A441" />
          </g>
        </g>
      </g>
      <ellipse cx="50" cy="204" rx="7" ry="5" fill="#1A1922" />
      <CammyScarf id={id} />
      <g transform="rotate(-3 100 110)">
        <CammyShell id={id} />
        <PandaEyesProud />
        <MouthSoft />
        <BambooLeafAccent />
      </g>
    </g>
  </svg>
);

/* ============================================================
   STUDIO SCENE — Multiplication Mosaic reference
   ============================================================ */

const StudioScene = ({ canvasContent, extra }) => (
  <svg viewBox="0 0 800 360" preserveAspectRatio="xMidYMid slice" className="w-full h-full block">
    <defs>
      <linearGradient id="mm-wall" x1="0%" y1="0%" x2="0%" y2="100%">
        <stop offset="0%" stopColor="#FFE9CB" />
        <stop offset="100%" stopColor="#F2D6B0" />
      </linearGradient>
      <linearGradient id="mm-floor" x1="0%" y1="0%" x2="0%" y2="100%">
        <stop offset="0%" stopColor="#A07042" />
        <stop offset="100%" stopColor="#5C3F0E" />
      </linearGradient>
      <linearGradient id="mm-window-sky" x1="0%" y1="0%" x2="0%" y2="100%">
        <stop offset="0%" stopColor="#FFE9CB" />
        <stop offset="100%" stopColor="#FBEACA" />
      </linearGradient>
      <linearGradient id="mm-pot-light" x1="0%" y1="0%" x2="0%" y2="100%">
        <stop offset="0%" stopColor="#FFFFFF" stopOpacity="0.18" />
        <stop offset="100%" stopColor="#FFFFFF" stopOpacity="0" />
      </linearGradient>
      <clipPath id="mm-window-clip">
        <rect x="0" y="0" width="220" height="160" rx="6" />
      </clipPath>
    </defs>

    {/* wall */}
    <rect width="800" height="240" fill="url(#mm-wall)" />
    {/* trim */}
    <rect x="0" y="234" width="800" height="6" fill="#7A5318" />
    <rect x="0" y="232" width="800" height="2" fill="#A07042" opacity="0.7" />
    {/* floor */}
    <rect y="240" width="800" height="120" fill="url(#mm-floor)" />
    {/* plank lines */}
    <g stroke="#5C3F0E" strokeWidth="1" opacity="0.5">
      <line x1="0" y1="282" x2="800" y2="282" />
      <line x1="0" y1="320" x2="800" y2="320" />
    </g>
    <g stroke="#7A5318" strokeWidth="0.8" opacity="0.5">
      <line x1="120" y1="240" x2="120" y2="360" />
      <line x1="260" y1="240" x2="260" y2="360" />
      <line x1="420" y1="240" x2="420" y2="360" />
      <line x1="580" y1="240" x2="580" y2="360" />
      <line x1="720" y1="240" x2="720" y2="360" />
    </g>

    {/* window with afternoon light */}
    <Window x={500} y={36} />
    {/* sun rays angling into scene */}
    <g>
      <SunRay x1={560} y1={170} x2={200} y2={360} opacity={0.22} />
      <SunRay x1={620} y1={180} x2={300} y2={360} opacity={0.16} />
      <SunRay x1={680} y1={190} x2={400} y2={360} opacity={0.12} />
    </g>

    {/* picture frames on wall */}
    <PictureFrame x={110} y={56} w={70} h={50} label="× chart" />
    <PictureFrame x={200} y={84} w={56} h={42} label="6 × n" />

    {/* dust motes drifting through light */}
    {[...Array(14)].map((_, i) => {
      const sx = 200 + (i * 41) % 360;
      const sy = 120 + (i * 67) % 140;
      const dx = -20 - (i * 11) % 30;
      const dy = -60 - (i * 13) % 60;
      const op = 0.35 + ((i * 7) % 20) / 100;
      const dur = 12 + ((i * 5) % 8);
      const delay = -(i * 1.7);
      const size = 2 + (i % 3);
      return (
        <circle
          key={i}
          cx={sx}
          cy={sy}
          r={size}
          fill="#FFEACA"
          className="anim-dust"
          style={{ ["--m-x"]: `${dx}px`, ["--m-y"]: `${dy}px`, ["--m-op"]: op, ["--m-dur"]: `${dur}s`, animationDelay: `${delay}s` }}
        />
      );
    })}

    {/* bamboo plant in left corner */}
    <Bamboo x={70} y={296} />

    {/* easel + canvas */}
    <Easel>{canvasContent}</Easel>

    {/* paint palette on floor */}
    <PaintPalette x={228} y={306} />

    {/* paint jars on floor right */}
    <PaintJar x={580} y={296} color="#D9A441" accent="#FBEACA" />
    <PaintJar x={612} y={300} color="#6B3F5E" accent="#B89DD9" />
    <PaintJar x={644} y={296} color="#E07A5F" accent="#FBD8D0" />

    {/* tile scraps scattered on floor */}
    <g>
      <rect x="240" y="332" width="10" height="10" rx="1" fill="#D9A441" transform="rotate(20 245 337)" />
      <rect x="270" y="338" width="8" height="8" rx="1" fill="#6B3F5E" transform="rotate(-15 274 342)" />
      <rect x="540" y="334" width="10" height="10" rx="1" fill="#E07A5F" transform="rotate(35 545 339)" />
      <rect x="680" y="336" width="9" height="9" rx="1" fill="#7AA66F" transform="rotate(-25 684 340)" />
    </g>

    {extra}
  </svg>
);

/* ============================================================
   PAINT-SPLATTER PARTICLES (correct state)
   ============================================================ */

const usePaintParticles = (count, seed) =>
  useMemo(() => {
    const rnd = (i) => {
      const x = Math.sin((i + seed) * 9301 + 49297) * 233280;
      return x - Math.floor(x);
    };
    const arr = [];
    for (let i = 0; i < count; i++) {
      const r = (k) => rnd(i * 7 + k);
      const angle = (r(1) - 0.5) * Math.PI * 1.6 - Math.PI / 2;
      const dist = 60 + r(2) * 180;
      const cx = Math.cos(angle) * dist + (r(3) - 0.5) * 80;
      const cy = Math.sin(angle) * dist + (r(4) - 0.5) * 60 + 40;
      const size = 5 + r(5) * 9;
      const delay = -r(6) * 5;
      const palette = ["#D9A441", "#6B3F5E", "#E07A5F", "#7AA66F", "#8E6722", "#8A5374", "#E8B651"];
      const color = palette[Math.floor(r(7) * palette.length)];
      const kind = Math.floor(r(8) * 3);
      arr.push({ cx, cy, size, delay, color, kind, i });
    }
    return arr;
  }, [count, seed]);

const PaintSplatter = ({ count = 70, originLeft = "60%", originTop = "60%", seed = 1 }) => {
  const ps = usePaintParticles(count, seed);
  return (
    <div className="absolute inset-0 pointer-events-none overflow-hidden" aria-hidden>
      {ps.map((p) => (
        <div
          key={p.i}
          className="absolute anim-paint-splash"
          style={{
            left: originLeft,
            top: originTop,
            width: p.size,
            height: p.size,
            ["--cx"]: `${p.cx}px`,
            ["--cy"]: `${p.cy}px`,
            animationDelay: `${p.delay}s`,
          }}
        >
          {p.kind === 0 && (
            <svg viewBox="0 0 10 10" className="w-full h-full" fill={p.color}>
              {/* irregular splat blob */}
              <path d="M5 1 Q8 2 9 5 Q8 8 5 9 Q2 8 1 5 Q2 2 5 1 Z" />
              <circle cx="2" cy="6" r="0.8" />
              <circle cx="8" cy="3" r="0.7" />
            </svg>
          )}
          {p.kind === 1 && (
            <div className="w-full h-full rounded-full" style={{ background: p.color }} />
          )}
          {p.kind === 2 && (
            <svg viewBox="0 0 10 10" className="w-full h-full" fill={p.color}>
              {/* small tile chip */}
              <rect x="1" y="1" width="8" height="8" rx="1" />
            </svg>
          )}
        </div>
      ))}
    </div>
  );
};

/* ============================================================
   UI ATOMS
   ============================================================ */

const StateLabel = ({ num, title, kind, desc }) => (
  <div className="flex items-baseline gap-3 mb-3 sm:mb-4">
    <div className="font-kid text-[12px] tracking-[0.2em] font-semibold text-walnut-3">{String(num).padStart(2, "0")}</div>
    <div className="flex-1 min-w-0">
      <h2 className="font-display italic text-walnut text-[24px] sm:text-[30px] leading-tight">
        <span className="font-semibold not-italic mr-2">{title}</span>
        <span className="font-display italic text-walnut-2">({kind})</span>
      </h2>
      <p className="font-kid text-walnut-3 text-[12px] sm:text-[13px] mt-1 max-w-[64ch]">{desc}</p>
    </div>
  </div>
);

const AnswerBtn = ({ label, kind = "default", className = "" }) => {
  const base = "min-h-[56px] sm:min-h-[64px] px-4 sm:px-6 rounded-3xl font-kid font-semibold text-[20px] sm:text-[26px] shadow-btn select-none transition-all flex items-center justify-center gap-2";
  if (kind === "correct") return <div className={`${base} bg-[#E8B651] text-walnut ${className}`} style={{ animation: "tile-glow 5s var(--ease-pop) infinite" }}>{label}</div>;
  if (kind === "ghost")   return <div className={`${base} bg-white text-walnut ring-1 ring-walnut/15 ${className}`}>{label}</div>;
  if (kind === "primary") return <div className={`${base} bg-cammy-plum text-cream ${className}`}>{label}</div>;
  return <div className={`${base} bg-white text-walnut ring-1 ring-walnut/10 ${className}`}>{label}</div>;
};

const EquationPanel = ({ children, big = false }) => (
  <div className={`absolute left-[18%] top-[5%] sm:top-[6%] z-20 pointer-events-none`}>
    <div className="bg-white text-walnut rounded-3xl px-5 sm:px-7 py-3 sm:py-4 shadow-lift ring-1 ring-walnut/10">
      <p className={`font-display ${big ? "text-[28px] sm:text-[42px]" : "text-[22px] sm:text-[34px]"} font-semibold leading-none tracking-tight`}>
        {children}
      </p>
    </div>
    <div className="absolute -bottom-2 left-12 w-4 h-4 bg-white rotate-45 ring-1 ring-walnut/10 ring-r-0 ring-b-0" />
  </div>
);

const HintCard = ({ children }) => (
  <div className="absolute right-[6%] top-[10%] max-w-[44%] z-20 pointer-events-none anim-hint-pop">
    <div className="bg-white text-walnut rounded-2xl px-4 sm:px-5 py-3 shadow-lift ring-1 ring-copper/30">
      <div className="flex items-start gap-2.5">
        <div className="mt-0.5 w-5 h-5 rounded-full bg-copper text-cream flex items-center justify-center font-kid font-bold text-[12px] flex-shrink-0">i</div>
        <p className="font-kid text-walnut text-[13px] sm:text-[15px] leading-snug">{children}</p>
      </div>
    </div>
  </div>
);

const PraiseWord = ({ word = "Brilliant!" }) => (
  <div className="absolute inset-x-0 top-[6%] sm:top-[5%] flex justify-center pointer-events-none z-30">
    <div className="anim-praise-pop">
      <h3 className="font-display italic font-bold text-[40px] sm:text-[64px] leading-none tracking-tight"
          style={{ color: "#4A2A42", textShadow: "0 4px 0 #FBEACA, 0 6px 24px rgba(217,164,65,0.55)" }}>
        {word}
      </h3>
    </div>
  </div>
);

const BrushstrokesTag = () => (
  <div className="absolute right-[26%] bottom-[3%] z-30 anim-coin-float pointer-events-none">
    <div className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-full bg-walnut text-cream shadow-lift font-kid font-semibold text-[13px] sm:text-[14px]">
      <svg viewBox="0 0 12 12" className="w-3.5 h-3.5"><path d="M2 10 L8 4 Q10 2 10 4 L4 10 Z" fill="#D9A441"/></svg>
      +8 brushstrokes
    </div>
  </div>
);

const GoldStar = ({ delay = 0, size = 40 }) => (
  <div className="inline-block star-stamp" style={{ animationDelay: `${delay}s`, width: size, height: size }}>
    <svg viewBox="0 0 40 40" className="w-full h-full">
      <defs>
        <radialGradient id={`mm-gs-${delay}`} cx="35%" cy="30%">
          <stop offset="0%" stopColor="#FBEACA" />
          <stop offset="50%" stopColor="#E8B651" />
          <stop offset="100%" stopColor="#8E6722" />
        </radialGradient>
      </defs>
      <path d="M20 3 L25 15 L38 17 L28 26 L31 39 L20 32 L9 39 L12 26 L2 17 L15 15 Z" fill={`url(#mm-gs-${delay})`} stroke="#7A5318" strokeWidth="1.2" strokeLinejoin="round" />
      <ellipse cx="15" cy="12" rx="4" ry="2.4" fill="#FFFFFF" opacity="0.55" />
    </svg>
  </div>
);

const RibbonBanner = ({ text }) => (
  <div className="relative inline-block">
    <svg viewBox="0 0 280 56" className="absolute inset-0 w-full h-full" preserveAspectRatio="none">
      <defs>
        <linearGradient id="mm-ribbon" x1="0%" y1="0%" x2="0%" y2="100%">
          <stop offset="0%" stopColor="#8A5374" />
          <stop offset="100%" stopColor="#4A2A42" />
        </linearGradient>
      </defs>
      <path d="M0 28 L12 8 L268 8 L280 28 L268 48 L12 48 Z" fill="url(#mm-ribbon)" />
      <path d="M0 28 L12 8 L268 8 L280 28" stroke="#B89DD9" strokeWidth="1.5" fill="none" opacity="0.6" />
    </svg>
    <div className="relative px-7 sm:px-9 py-2 sm:py-3 font-kid font-semibold text-cream text-[15px] sm:text-[18px] tracking-wide z-10">{text}</div>
  </div>
);

/* ============================================================
   STATES
   ============================================================ */

const StateIdle = () => (
  <section>
    <StateLabel
      num={1}
      title="Idle"
      kind="the equation"
      desc="Scene breathes. Bamboo sways (6s), dust motes drift through window light (12–20s, opacity 0.35–0.55). Cammy breathes (4s) by the easel, brush gently bobbing. Equation panel floats in. Tiles wait calmly."
    />
    <article className="rounded-[24px] bg-cream paper-grain ring-1 ring-walnut/15 shadow-lift overflow-hidden">
      <div className="relative aspect-[16/7.4] sm:aspect-[16/7.2] overflow-hidden">
        <StudioScene
          canvasContent={<MosaicGrid revealed={[true, false, true, false, false, true, false, true, true, false, false, true]} />}
        />
        <EquationPanel>7 × 8 = <span className="text-cammy-deep">?</span></EquationPanel>
        <div className="absolute" style={{ left: "57%", bottom: "8%", width: "20%", maxWidth: 200 }}>
          <CammyIdle id="cammy-idle" />
        </div>
      </div>
      <div className="bg-cream px-4 sm:px-6 py-4 sm:py-5 border-t border-walnut/10">
        <div className="grid grid-cols-4 gap-2 sm:gap-3 max-w-[640px] mx-auto">
          <AnswerBtn label="48" />
          <AnswerBtn label="54" />
          <AnswerBtn label="56" />
          <AnswerBtn label="64" />
        </div>
      </div>
    </article>
  </section>
);

const StateCorrect = () => (
  <section>
    <StateLabel
      num={2}
      title="Correct"
      kind="the splash!"
      desc="Camera zooms 1.04×. Mosaic tile snaps in (scale 0→1.25→1, rotate -10°→0°). Paint splatter bursts (~70 mustard/plum/copper particles). Cammy raises her brush triumphantly. Italic 'Brilliant!' praise word. Selected button glows gold. +8 brushstrokes floats up."
    />
    <article className="rounded-[24px] bg-cream paper-grain ring-1 ring-walnut/15 shadow-lift overflow-hidden">
      <div className="relative aspect-[16/7.4] sm:aspect-[16/7.2] overflow-hidden">
        <div className="absolute inset-0 anim-zoom-cycle scene-stage">
          <StudioScene
            canvasContent={
              <MosaicGrid
                revealed={[true, false, true, false, false, true, false, true, true, false, false, true]}
                newSnapIdx={4}
              />
            }
          />
        </div>
        <PraiseWord word="Brilliant!" />
        <PaintSplatter count={84} originLeft="46%" originTop="48%" seed={3} />
        <div className="absolute" style={{ left: "57%", bottom: "8%", width: "20%", maxWidth: 200 }}>
          <CammySplash id="cammy-splash" />
        </div>
      </div>
      <div className="bg-cream px-4 sm:px-6 py-4 sm:py-5 border-t border-walnut/10 relative">
        <div className="grid grid-cols-4 gap-2 sm:gap-3 max-w-[640px] mx-auto relative">
          <AnswerBtn label="48" />
          <AnswerBtn label="54" />
          <div className="relative">
            <AnswerBtn label="56" kind="correct" />
            <BrushstrokesTag />
          </div>
          <AnswerBtn label="64" />
        </div>
      </div>
    </article>
  </section>
);

const StateWrong = () => (
  <section>
    <StateLabel
      num={3}
      title="Wrong"
      kind="let's try painting that again"
      desc="Cammy looks down at the brush with a small concerned smile and gently shakes her head. Warm cream vignette pulses around the scene. The wrong tile dims softly (opacity 0.45, desaturated). Hint card slides in from right. No harsh alarm or red."
    />
    <article className="rounded-[24px] bg-cream paper-grain ring-1 ring-walnut/15 shadow-lift overflow-hidden">
      <div className="relative aspect-[16/7.4] sm:aspect-[16/7.2] overflow-hidden">
        <StudioScene
          canvasContent={
            <MosaicGrid
              revealed={[true, false, true, false, false, true, false, true, true, false, false, true]}
              dimIdx={6}
            />
          }
        />
        {/* warm cream vignette */}
        <div className="absolute inset-0 anim-vignette-pulse pointer-events-none" style={{ background: "radial-gradient(ellipse at center, transparent 40%, rgba(224,122,95,0.32) 100%)" }} />
        <div className="absolute inset-0 anim-vignette-pulse pointer-events-none" style={{ background: "rgba(245,222,200,0.16)" }} />
        <HintCard>
          Let&rsquo;s try painting that one <span className="font-semibold text-cammy-deep">again</span>.
        </HintCard>
        <div className="absolute" style={{ left: "57%", bottom: "8%", width: "20%", maxWidth: 200 }}>
          <CammyConcerned id="cammy-concerned" />
        </div>
      </div>
      <div className="bg-cream px-4 sm:px-6 py-4 sm:py-5 border-t border-walnut/10">
        <div className="grid grid-cols-4 gap-2 sm:gap-3 max-w-[640px] mx-auto">
          <AnswerBtn label="48" />
          <AnswerBtn label="54" className="anim-tile-dim" />
          <AnswerBtn label="56" />
          <AnswerBtn label="64" />
        </div>
      </div>
    </article>
  </section>
);

const StateEnd = () => (
  <section>
    <StateLabel
      num={4}
      title="End-of-round"
      kind="completed mosaic"
      desc="ALL 12 mosaic tiles revealed — full painting glows. Cammy gestures proudly with her brush, slight up-bob (4s loop). Banner pops with 'Beautiful mosaic!' headline + 'mosaic complete' ribbon. 5 gold stars stamp in sequence. A second tiny panda peeks from behind the easel cheering. CTAs below."
    />
    <article className="rounded-[24px] bg-cream paper-grain ring-1 ring-walnut/15 shadow-lift overflow-hidden">
      <div className="relative aspect-[16/7.4] sm:aspect-[16/7.2] overflow-hidden">
        <StudioScene
          canvasContent={<MosaicGrid revealed={Array(12).fill(true)} allRevealed />}
          extra={
            <g transform="translate(444 200)" className="anim-peek">
              {/* tiny peeking panda behind easel */}
              <circle cx="0" cy="0" r="16" fill="#FFFFFF" />
              <circle cx="-8" cy="-12" r="6" fill="#2C2B36" />
              <circle cx="8" cy="-12" r="6" fill="#2C2B36" />
              <ellipse cx="-6" cy="-2" rx="4" ry="5" fill="#2C2B36" />
              <ellipse cx="6" cy="-2" rx="4" ry="5" fill="#2C2B36" />
              <ellipse cx="-5" cy="-2" rx="2" ry="2.6" fill="#FFFFFF" />
              <ellipse cx="5" cy="-2" rx="2" ry="2.6" fill="#FFFFFF" />
              <ellipse cx="-5" cy="-1" rx="1.2" ry="1.6" fill="#2C2B36" />
              <ellipse cx="5" cy="-1" rx="1.2" ry="1.6" fill="#2C2B36" />
              <ellipse cx="0" cy="4" rx="6" ry="4" fill="#FFFFFF" />
              <ellipse cx="0" cy="2.5" rx="1.5" ry="1" fill="#2C2B36" />
              {/* tiny waving paw */}
              <ellipse cx="-14" cy="-4" rx="4" ry="6" fill="#2C2B36" transform="rotate(-30 -14 -4)" />
            </g>
          }
        />
        <PaintSplatter count={50} originLeft="50%" originTop="18%" seed={5} />
        <div className="absolute inset-x-0 top-[5%] flex flex-col items-center gap-2 z-20 pointer-events-none anim-banner-pop">
          <h3 className="font-display italic font-bold text-walnut text-[26px] sm:text-[38px] leading-none">Beautiful mosaic!</h3>
          <RibbonBanner text="12 / 12 tiles painted" />
          <div className="flex gap-1.5 sm:gap-2 mt-1">
            <GoldStar delay={0} />
            <GoldStar delay={0.2} />
            <GoldStar delay={0.4} />
            <GoldStar delay={0.6} />
            <GoldStar delay={0.8} />
          </div>
        </div>
        <div className="absolute" style={{ left: "57%", bottom: "8%", width: "20%", maxWidth: 200 }}>
          <CammyProud id="cammy-proud" />
        </div>
      </div>
      <div className="bg-cream px-4 sm:px-6 py-4 sm:py-5 border-t border-walnut/10">
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-3 sm:gap-4 max-w-[640px] mx-auto">
          <AnswerBtn label="Play again ↻" kind="primary" />
          <AnswerBtn label="Pick another game" kind="ghost" />
        </div>
      </div>
    </article>
  </section>
);

/* ============================================================
   RULES CARD
   ============================================================ */

const RULES = [
  ["Idle breath",       "4s ease-in-out",                  "scale 1 → 1.015 → 1"],
  ["Bamboo sway",       "6s cubic-bezier(.45,.05,.55,.95)", "rotate -2° → 3°"],
  ["Dust motes",        "12–20s linear · drift through light", "translate-y + opacity 0.35–0.55"],
  ["Brush bob",         "4s ease-in-out",                  "translate-y -1.5px, rotate -3° → -1°"],
  ["Ear twitch",        "7s ease-in-out · random phase",   "rotate -5° → 2°"],
  ["Paint splatter",    "1.2s burst · 70–100 particles",   "translate + rotate 720°, mustard / plum / copper"],
  ["Tile snap-in",      "0.4s pop",                         "scale 0 → 1.25 → 1, rotate -10° → 0°"],
  ["Praise word",       "0.65s cubic-bezier(.34,1.56,.64,1)", "scale 0.4 → 1.12 → 1, rotate -8° → 0°"],
  ["Camera zoom",       "0.4s ease-out",                    "scale(1.04)"],
  ["Wrong vignette",    "1.5s × 2 cycles",                  "warm cream radial pulse"],
  ["Head shake",        "0.4s once",                        "rotate -4° → 4° → -2° → 0°"],
  ["Brush flourish",    "0.5s pop",                         "rotate -3° → -40° → -10° → -30°"],
  ["Star stamp",        "0.4s pop · 0.2s stagger",          "scale 0 → 1.25 → 1, rotate -30° → 0°"],
];

const RulesCard = () => (
  <article className="rounded-[24px] bg-cammy-deep text-cream p-5 sm:p-7 shadow-lift">
    <div className="flex items-baseline justify-between mb-4">
      <h3 className="font-display italic font-semibold text-[22px] sm:text-[26px]">Animation grammar</h3>
      <span className="font-kid text-[11px] uppercase tracking-[0.22em] text-cream/60">applies to all 7 Cammy games</span>
    </div>
    <div className="grid grid-cols-1 sm:grid-cols-2 gap-y-3 gap-x-6">
      {RULES.map(([name, timing, transform]) => (
        <div key={name} className="flex flex-col gap-0.5 border-l-2 border-cammy-mustard/70 pl-3">
          <div className="font-kid font-semibold text-cream text-[14px] leading-tight">{name}</div>
          <div className="font-mono text-[11px] text-cream/70 leading-tight">{timing}</div>
          <div className="font-mono text-[11px] text-cammy-mustard leading-tight">{transform}</div>
        </div>
      ))}
    </div>
    <div className="mt-5 pt-4 border-t border-cream/15">
      <p className="font-kid text-[12px] text-cream/70">
        Default pop easing: <span className="font-mono text-cammy-mustard">cubic-bezier(0.34, 1.56, 0.64, 1)</span> &middot;
        Flutter easing: <span className="font-mono text-cammy-mustard">cubic-bezier(0.45, 0.05, 0.55, 0.95)</span>
      </p>
    </div>
  </article>
);

/* ============================================================
   MAIN COMPONENT
   ============================================================ */

function MultiplicationMosaicAnimations() {
  return (
    <div className="min-h-screen px-4 sm:px-8 lg:px-12 py-8 sm:py-12 max-w-[1100px] mx-auto">
      <header className="mb-8 sm:mb-12">
        <div className="flex items-start gap-2 text-[11px] uppercase tracking-[0.22em] font-semibold text-copper">
          <span className="inline-block w-6 h-px bg-copper mt-2" />
          <span>Game animation system &middot; Cammy</span>
        </div>
        <h1 className="font-display text-walnut mt-3 leading-[0.95] text-[40px] sm:text-[60px] lg:text-[68px] tracking-tight max-w-[20ch]">
          <span className="italic font-medium">Multiplication Mosaic,</span>{" "}
          <span className="font-semibold">in motion.</span>
        </h1>
        <p className="font-display italic text-walnut-2 text-[15px] sm:text-[18px] mt-4 max-w-[64ch] leading-snug">
          The visual + motion grammar for all seven of Cammy&rsquo;s games &mdash; Spelling Studio,
          Grammar Gallery, Vocab Brushwork, Reading Comprehension, Multiplication Mosaic, Division Dash,
          Fraction Workshop. Cozy artist&rsquo;s studio + paint-splatter joy. Every state below loops live.
        </p>
        <div className="flex flex-wrap gap-2 mt-5">
          <span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full bg-cammy-bg ring-1 ring-cammy-mustard/40 text-[11px] font-semibold text-cammy-deep">
            <span className="w-1.5 h-1.5 rounded-full bg-cammy-mustard" />
            3rd Grade · 4 states
          </span>
          <span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full bg-cream ring-1 ring-walnut/15 text-[11px] font-semibold text-walnut-2">
            Reference scene: Multiplication Mosaic
          </span>
        </div>
      </header>

      <div className="flex flex-col gap-8 sm:gap-12">
        <StateIdle />
        <StateCorrect />
        <StateWrong />
        <StateEnd />
        <RulesCard />
      </div>

      <footer className="mt-12 pt-6 border-t border-walnut/10 flex flex-wrap items-baseline justify-between gap-4 text-[12px] font-medium text-walnut-3">
        <p className="font-display italic text-walnut-2 text-[14px]">
          &ldquo;Every tile is a brave try.&rdquo;
        </p>
        <span>Cammy &amp; Caris Learning Club &middot; Cammy animation system</span>
      </footer>
    </div>
  );
}

window.MultiplicationMosaicAnimations = MultiplicationMosaicAnimations;

window.MMAStates = { StateIdle, StateCorrect, StateWrong, StateEnd };
