/* ===================== Animal Runner — sidebar idle animation ===================== */
/* White line-art animal that auto-runs and jumps over trees, Chrome-dino style.    */
/* Switchable characters: cat / dog / bear / rabbit / turtle / lion / penguin / elephant */
/* ตัวละครเป็น "rig" มีข้อต่อ: ขา 2 ท่อนคำนวณเข่าด้วย IK · ท่าวิ่งแยกตามสัตว์ (ควบ/เหยาะ/เดิน)       */
/* เท้าล็อกกับพื้นตอนแตะ (ความเร็วเท้า = ความเร็วพื้น ไม่ไถล) · หาง/หู/งวงมีแรงเหวี่ยงตามหลัง         */

const RUNNER_ANIMALS = [
  { id: 'cat',      emoji: '🐱', label: 'แมว' },
  { id: 'dog',      emoji: '🐶', label: 'หมา' },
  { id: 'bear',     emoji: '🐻', label: 'หมี' },
  { id: 'rabbit',   emoji: '🐰', label: 'กระต่าย' },
  { id: 'turtle',   emoji: '🐢', label: 'เต่า' },
  { id: 'lion',     emoji: '🦁', label: 'สิงโต' },
  { id: 'penguin',  emoji: '🐧', label: 'เพนกวิน' },
  { id: 'elephant', emoji: '🐘', label: 'ช้าง' },
];

function CatRunner() {
  const canvasRef = useRef(null);
  const wrapRef = useRef(null);

  const [animalId, setAnimalId] = useState(() => {
    try { return localStorage.getItem('sb_runner_animal') || 'cat'; } catch (e) { return 'cat'; }
  });
  const animalRef = useRef(animalId);

  const pick = (id) => {
    animalRef.current = id;
    setAnimalId(id);
    try { localStorage.setItem('sb_runner_animal', id); } catch (e) {}
  };
  const cycle = () => {
    const i = RUNNER_ANIMALS.findIndex(a => a.id === animalRef.current);
    pick(RUNNER_ANIMALS[(i + 1) % RUNNER_ANIMALS.length].id);
  };

  useEffect(() => {
    const canvas = canvasRef.current, wrap = wrapRef.current;
    if (!canvas || !wrap) return;
    const ctx = canvas.getContext('2d');

    // ‼️ dpr สูงสุด 3 → เส้นคมบนจอ retina (ตัวละครมีรายละเอียดเล็ก ๆ เยอะ)
    let W = 220, H = 92, dpr = Math.min(window.devicePixelRatio || 1, 3);
    const resize = () => {
      W = wrap.clientWidth || 220;
      canvas.width = Math.round(W * dpr); canvas.height = Math.round(H * dpr);
      canvas.style.width = W + 'px'; canvas.style.height = H + 'px';
      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    };
    resize();
    const ro = new ResizeObserver(resize); ro.observe(wrap);

    const groundY = H - 20;
    const STROKE = 'rgba(255,255,255,0.94)';
    const SOFT   = 'rgba(255,255,255,0.45)';   // ขาฝั่งไกล (ให้มีมิติ)
    const FAINT  = 'rgba(255,255,255,0.30)';
    const FILL   = 'rgba(255,255,255,0.07)';
    const BODYF  = 'rgba(15,23,42,0.55)';      // พื้นในตัว — บังขาฝั่งไกล/ภูเขาด้านหลัง
    const ORANGE = 'rgba(255,183,94,0.95)';
    const TAU = Math.PI * 2;
    const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
    const lerp = (a, b, t) => a + (b - a) * t;
    const smooth = (t) => t * t * (3 - 2 * t);

    // ================= RIG HELPERS =================
    const dot = (x, y, r, c) => { ctx.fillStyle = c || STROKE; ctx.beginPath(); ctx.arc(x, y, r, 0, TAU); ctx.fill(); };
    const shape = (fill) => { if (fill) { ctx.fillStyle = fill; ctx.fill(); } ctx.stroke(); };

    // IK ขา 2 ท่อน: สะโพก (hx,hy) → เท้า (fx,fy) ยาว a,b · bend>0 = เข่าชี้หน้า, <0 = ชี้หลัง
    function ik(hx, hy, fx, fy, a, b, bend) {
      let dx = fx - hx, dy = fy - hy, d = Math.hypot(dx, dy) || 0.001;
      const dd = clamp(d, Math.abs(a - b) + 0.05, a + b - 0.05);
      fx = hx + dx / d * dd; fy = hy + dy / d * dd;
      const base = Math.atan2(dy, dx);
      const al = Math.acos(clamp((a * a + dd * dd - b * b) / (2 * a * dd), -1, 1));
      const ang = base + (bend > 0 ? -al : al);
      return { kx: hx + a * Math.cos(ang), ky: hy + a * Math.sin(ang), fx, fy };
    }
    // วงจรเท้า: ช่วงแตะพื้น (duty) เท้าเลื่อนถอยหลังเท่าความเร็วพื้น · ช่วงยก เท้าโค้งไปข้างหน้า
    function footCycle(p, duty, stride, lift) {
      p = ((p % 1) + 1) % 1;
      if (p < duty) return [stride * (0.5 - p / duty), 0];
      const t = (p - duty) / (1 - duty);
      return [stride * (-0.5 + smooth(t)), -lift * Math.sin(Math.PI * t)];
    }
    // หางเป็นโซ่หลายข้อ — ข้อปลายแกว่งตามหลังข้อโคน (ดูมีน้ำหนัก) · คืนจุดปลาย
    function chain(x, y, n, seg, base, curl, sway, t, lag, w0, w1) {
      let a = base, px = x, py = y;
      for (let i = 0; i < n; i++) {
        a += curl + sway * Math.sin(t - i * lag) / n;
        const nx = px + Math.cos(a) * seg, ny = py + Math.sin(a) * seg;
        ctx.lineWidth = lerp(w0, w1, i / Math.max(1, n - 1));
        ctx.beginPath(); ctx.moveTo(px, py); ctx.lineTo(nx, ny); ctx.stroke();
        px = nx; py = ny;
      }
      return [px, py, a];
    }

    // ================= QUADRUPED (แมว/หมา/หมี/สิงโต/ช้าง) =================
    //  พิกัด: (0,0) = พื้นใต้สะโพก · +x = หน้า · y ลบ = ขึ้น
    //  off = เฟสขา [หลังใกล้, หลังไกล, หน้าใกล้, หน้าไกล] — ควบ (แมว/สิงโต) · เหยาะทแยง (หมา) · เดินทีละขา (หมี/ช้าง)
    const QUAD = {
      cat: {
        hip: [0, -11], sh: [16, -11.4], hind: [6.3, 6.4], fore: [5.7, 6.2], legW: 1.5,
        off: [0, 0.08, 0.5, 0.58], duty: 0.42, stride: 10, lift: 3.6, bob: 0.55, pitch: 1.3,
        torso: { rear: 3.6, chest: 3.4, back: 7.4, sag: 0.8, withers: 0.4, belly: 1.4, rearLow: 1, chestLow: 2.4, tuck: 0.5 },
        neck: [1.2, -5.2], head: 'cat',
        tail: { n: 8, seg: 2.2, base: -2.75, curl: 0.13, sway: 1.6, freq: 0.09, lag: 0.55, w0: 1.7, w1: 1.1 },
      },
      dog: {
        hip: [0, -12], sh: [17, -12.4], hind: [6.6, 6.8], fore: [6.2, 6.6], legW: 1.6,
        off: [0, 0.5, 0.5, 0], duty: 0.5, stride: 10, lift: 3.2, bob: 0.7, pitch: 0.5,
        torso: { rear: 3.4, chest: 4, back: 7.4, sag: 0.5, withers: 0.8, belly: 1.4, rearLow: 1, chestLow: 3, tuck: 0.9 },
        neck: [1.6, -5.4], head: 'dog',
        tail: { n: 5, seg: 2.1, base: -2.1, curl: -0.32, sway: 2.4, freq: 0.42, lag: 0.4, w0: 1.8, w1: 1.2 },
      },
      bear: {
        hip: [0, -12], sh: [18, -12], hind: [6, 6.6], fore: [6.2, 6.4], legW: 2.5,
        off: [0, 0.5, 0.25, 0.75], duty: 0.62, stride: 9, lift: 2.6, bob: 0.9, pitch: 0.8,
        torso: { rear: 4.4, chest: 3.6, back: 8.4, sag: -1.8, withers: 1.6, belly: 2.4, rearLow: 2.2, chestLow: 2.8, tuck: -0.6 },
        neck: [2.2, -4.6], head: 'bear', tail: null,
      },
      lion: {
        hip: [0, -12.4], sh: [18, -12.8], hind: [6.6, 6.9], fore: [6.3, 7], legW: 1.75,
        off: [0, 0.1, 0.5, 0.6], duty: 0.42, stride: 11, lift: 3.8, bob: 0.5, pitch: 1.4,
        torso: { rear: 3.6, chest: 3.8, back: 7.8, sag: 0.8, withers: 0.4, belly: 1.6, rearLow: 1, chestLow: 3, tuck: 0.7 },
        neck: [1.6, -5.4], head: 'lion',
        tail: { n: 9, seg: 2.2, base: 2.55, curl: -0.12, sway: 1.3, freq: 0.08, lag: 0.5, w0: 1.5, w1: 1.1, tuft: true },
      },
      elephant: {
        hip: [0, -14], sh: [21, -14.2], hind: [7, 7.4], fore: [7.2, 7.4], legW: 3.4,
        off: [0, 0.5, 0.25, 0.75], duty: 0.68, stride: 12, lift: 2.4, bob: 0.8, pitch: 0.4,
        torso: { rear: 5.4, chest: 4, back: 10.5, sag: -0.8, withers: -0.6, belly: 3.4, rearLow: 2.6, chestLow: 3.2, tuck: -1.2 },
        neck: [2.4, -6], head: 'elephant',
        tail: { n: 5, seg: 1.8, base: 2.3, curl: 0.04, sway: 0.9, freq: 0.06, lag: 0.5, w0: 1.2, w1: 0.9, tuft: true },
      },
    };

    // ลำตัวแบบสัตว์จริง: สะโพกโค้งมน → หลัง (แอ่น/โหนก) → ไหล่ → อกลึก → เอวคอด (tuck) → กลับสะโพก
    function torso(hip, sh, o) {
      const midX = (hip.x + sh.x) / 2, topY = Math.min(hip.y, sh.y) - o.back;
      const r0x = hip.x - o.rear * 0.35, r0y = hip.y + o.rearLow;
      const waistX = hip.x + (sh.x - hip.x) * 0.32, waistY = hip.y + o.rearLow - (o.tuck || 0);
      ctx.beginPath();
      ctx.moveTo(r0x, r0y);
      ctx.bezierCurveTo(hip.x - o.rear - 0.8, hip.y - o.back * 0.2, hip.x - o.rear * 0.9, hip.y - o.back, hip.x + 0.6, hip.y - o.back);
      ctx.quadraticCurveTo(midX, topY + o.sag, sh.x - 1.2, sh.y - o.back - o.withers);
      ctx.bezierCurveTo(sh.x + o.chest * 0.8, sh.y - o.back - o.withers * 0.6, sh.x + o.chest + 0.8, sh.y - o.back * 0.35, sh.x + o.chest * 0.7, sh.y + o.chestLow * 0.4);
      ctx.quadraticCurveTo(sh.x + o.chest * 0.2, sh.y + o.chestLow + 0.6, sh.x - 2, sh.y + o.chestLow);
      ctx.quadraticCurveTo(midX + 1, sh.y + o.chestLow + o.belly * 0.4, waistX, waistY);
      ctx.quadraticCurveTo(hip.x + 0.8, hip.y + o.rearLow + 0.4, r0x, r0y);
      ctx.closePath();
      shape(BODYF); ctx.fillStyle = FILL; ctx.fill();
      // กล้ามต้นขาหลัง/สะบัก (เส้นจาง) — ให้ตัวมีปริมาตร
      if (o.muscle) {
        ctx.save(); ctx.strokeStyle = FAINT; ctx.lineWidth = 0.9;
        ctx.beginPath(); ctx.moveTo(hip.x - 1.6, hip.y - o.back * 0.85); ctx.quadraticCurveTo(hip.x + 2.6, hip.y - o.back * 0.45, hip.x + 1.2, hip.y + 0.6); ctx.stroke();
        ctx.beginPath(); ctx.moveTo(sh.x - 2.2, sh.y - o.back * 0.8); ctx.quadraticCurveTo(sh.x + 0.6, sh.y - o.back * 0.25, sh.x - 0.6, sh.y + 0.8); ctx.stroke();
        ctx.restore();
      }
    }

    function quadLeg(D, ax, ay, ph, L, hind, far, P) {
      const len = L[0] + L[1];
      let fx, fy;
      if (P.air) {
        // ท่ากลางอากาศ: ขึ้น (u→1) ขาหลังเหยียดถีบ ขาหน้าพับ · ลง (u→0) ขาหน้ายื่นรับพื้น ขาหลังพับเก็บ
        const u = clamp((P.k + 1) / 2, 0, 1);
        const rise = hind ? [-D.stride * 0.75, len * 0.8] : [D.stride * 0.45, len * 0.55];
        const fall = hind ? [D.stride * 0.25, len * 0.62] : [D.stride * 0.65, len * 0.92];
        fx = ax + lerp(fall[0], rise[0], u) + (far ? -0.8 : 0); fy = ay + lerp(fall[1], rise[1], u);
      } else {
        const c = footCycle(P.p + ph, D.duty, D.stride, D.lift);
        fx = ax + c[0]; fy = c[1];
      }
      const j = ik(ax, ay, fx, fy, L[0], L[1], hind ? -1 : 1);
      // ขาเรียว: ท่อนบน (ต้นขา) หนากว่าท่อนล่าง
      ctx.strokeStyle = far ? SOFT : STROKE;
      ctx.lineWidth = D.legW * 1.3; ctx.beginPath(); ctx.moveTo(ax, ay); ctx.lineTo(j.kx, j.ky); ctx.stroke();
      ctx.lineWidth = D.legW * 0.9; ctx.beginPath(); ctx.moveTo(j.kx, j.ky); ctx.lineTo(j.fx, j.fy); ctx.stroke();
      // อุ้งเท้า/เท้า — ชี้ไปหน้าเล็กน้อย
      ctx.lineWidth = Math.max(1.1, D.legW * 0.8);
      ctx.beginPath(); ctx.moveTo(j.fx - 0.4, j.fy); ctx.lineTo(j.fx + D.legW * 0.55 + 0.9, j.fy); ctx.stroke();
    }

    function drawQuad(D, P) {
      let bob = 0, pitch = 0;
      if (!P.air) { bob = -D.bob * Math.abs(Math.sin(P.p * TAU)); pitch = D.pitch * Math.sin(P.p * TAU + 0.7); }
      else pitch = clamp(P.k * 2.4, -2.4, 2.4);     // ขึ้น = หน้าเชิด
      const hip = { x: D.hip[0], y: D.hip[1] + bob + pitch * 0.5 };
      const sh  = { x: D.sh[0],  y: D.sh[1]  + bob - pitch * 0.5 };
      // ขาฝั่งไกลก่อน (จาง) → ตัว → ขาฝั่งใกล้
      quadLeg(D, hip.x - 0.8, hip.y + 1, D.off[1], D.hind, true, true, P);
      quadLeg(D, sh.x - 0.8,  sh.y + 1,  D.off[3], D.fore, false, true, P);
      // หาง (หลังตัว)
      if (D.tail) {
        const T = D.tail, tx = hip.x - D.torso.rear + 0.5, ty = hip.y - D.torso.back * 0.75;
        ctx.strokeStyle = STROKE;
        const swing = T.sway * (0.6 + 0.4 * Math.sin(P.t * 0.013));
        const lift = P.air ? -P.k * 0.35 : 0;
        const end = chain(tx, ty, T.n, T.seg, T.base + lift + P.tailLag * 0.5, T.curl, swing, P.t * T.freq, T.lag, T.w0, T.w1);
        if (T.tuft) {
          ctx.fillStyle = STROKE; ctx.beginPath();
          ctx.ellipse(end[0], end[1], 1.9, 1.1, end[2], 0, TAU); ctx.fill();
        }
      } else {  // หางกุด (หมี)
        dot(hip.x - D.torso.rear - 0.6, hip.y - D.torso.back * 0.6, 1.5, FAINT);
      }
      ctx.strokeStyle = STROKE; ctx.lineWidth = 1.6;
      torso(hip, sh, D.torso);
      quadLeg(D, hip.x, hip.y + 1, D.off[0], D.hind, true, false, P);
      quadLeg(D, sh.x, sh.y + 1, D.off[2], D.fore, false, false, P);
      // หัว: ส่ายตามหลังจังหวะตัวนิดหน่อย
      const hb = P.air ? 0 : 0.55 * Math.sin(P.p * TAU * 2 - 1.1);
      ctx.save();
      ctx.translate(sh.x + D.neck[0], sh.y + D.neck[1] + hb);
      ctx.rotate(P.air ? -P.k * 0.08 : 0);
      ctx.strokeStyle = STROKE; ctx.lineWidth = 1.5;
      HEADS[D.head](P, sh);
      ctx.restore();
    }

    // ---- หัวสัตว์ (พิกัดหัว: (0,0) = โคนคอ หันขวา) ----
    const HEADS = {
      cat(P) {
        ctx.beginPath(); ctx.moveTo(-2.4, 4.6); ctx.quadraticCurveTo(-0.4, 0, 1.8, -2.2); ctx.stroke();  // ท้ายทอย-คอ
        ctx.beginPath(); ctx.ellipse(5.2, -3.4, 4.9, 4.1, -0.08, 0, TAU); shape(BODYF);
        ctx.beginPath(); ctx.ellipse(9.2, -1.9, 1.9, 1.5, 0, 0, TAU); shape(BODYF);  // ปากกระบอก
        const tw = Math.sin(P.t * 0.05) > 0.97 ? 0.35 : 0;   // หูกระดิกเป็นพัก ๆ
        ctx.save(); ctx.translate(3, -6.6); ctx.rotate(-0.15 - tw);
        ctx.beginPath(); ctx.moveTo(-1.6, 0.3); ctx.lineTo(-0.6, -5); ctx.lineTo(2, -0.4); shape(BODYF);
        ctx.strokeStyle = FAINT; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.moveTo(-0.6, -0.4); ctx.lineTo(-0.4, -3.2); ctx.stroke();
        ctx.restore();
        ctx.save(); ctx.translate(6.8, -7); ctx.rotate(0.12);
        ctx.beginPath(); ctx.moveTo(-1.6, 0.4); ctx.lineTo(0.4, -5.1); ctx.lineTo(2.2, 0.6); shape(BODYF);
        ctx.strokeStyle = FAINT; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.moveTo(0.3, -0.4); ctx.lineTo(0.4, -3.3); ctx.stroke();
        ctx.restore();
        const blink = (P.t % 220) < 6;
        if (blink) { ctx.lineWidth = 0.9; ctx.beginPath(); ctx.moveTo(6.4, -4.2); ctx.lineTo(8, -4.2); ctx.stroke(); }
        else { dot(7.2, -4.3, 0.95); }
        dot(10.9, -2.6, 0.6, ORANGE);
        ctx.strokeStyle = FAINT; ctx.lineWidth = 0.6;
        ctx.beginPath(); ctx.moveTo(9.8, -1.6); ctx.lineTo(14.2, -2.6); ctx.moveTo(9.8, -1.2); ctx.lineTo(14.3, -0.6); ctx.stroke();
      },
      dog(P) {
        ctx.beginPath(); ctx.moveTo(-2.6, 5); ctx.quadraticCurveTo(-0.6, 0, 1.6, -2.4); ctx.stroke();
        ctx.beginPath(); ctx.ellipse(4.6, -3.8, 4.6, 4, 0, 0, TAU); shape(BODYF);
        ctx.beginPath(); ctx.moveTo(7.4, -6); ctx.quadraticCurveTo(10.6, -5.8, 13.2, -4.6);
        ctx.quadraticCurveTo(14.2, -2.6, 12.6, -1.5); ctx.quadraticCurveTo(10, -0.6, 7, -1); shape(BODYF);
        dot(13.3, -4.4, 1, STROKE); dot(7, -5, 0.95);
        // ลิ้นห้อยตอนวิ่ง
        if (!P.air) { ctx.fillStyle = 'rgba(255,140,160,0.85)'; ctx.beginPath(); ctx.ellipse(11.2, -0.4 + Math.abs(Math.sin(P.p * TAU)) * 0.6, 1, 1.5, 0.3, 0, TAU); ctx.fill(); }
        // หูตกแกว่งตามแรงเหวี่ยง
        const ew = Math.sin(P.p * TAU * 2 - 1.6) * 0.25 + (P.air ? -P.k * 0.4 : 0);
        ctx.save(); ctx.translate(2.6, -6.8); ctx.rotate(0.2 + ew);
        ctx.beginPath(); ctx.moveTo(0, 0); ctx.quadraticCurveTo(-2.8, 1.6, -2, 6.6); ctx.quadraticCurveTo(0.4, 6.4, 1.6, 1); shape(BODYF);
        ctx.restore();
      },
      bear(P) {
        ctx.beginPath(); ctx.arc(5, -3, 5.2, 0, TAU); shape(BODYF);
        ctx.beginPath(); ctx.arc(1.6, -7.6, 1.9, 0, TAU); shape(BODYF);
        ctx.beginPath(); ctx.arc(7.6, -8, 1.9, 0, TAU); shape(BODYF);
        ctx.beginPath(); ctx.ellipse(9.8, -1.6, 3, 2.2, 0, 0, TAU); shape(BODYF);
        dot(12.3, -2.4, 1.05); dot(7, -4.6, 1);
        dot(1.6, -7.6, 0.8, FAINT); dot(7.6, -8, 0.8, FAINT);   // ในหู
      },
      lion(P) {
        // แผงคอ: ก้อนขนฟูรอบหน้า ขอบเป็นปอยหยัก ปลิวไปด้านหลังตามแรงวิ่ง
        const cx = 3.6, cy = -3.6, N = 22;
        ctx.beginPath();
        for (let i = 0; i <= N; i++) {
          const a = i / N * TAU, back = Math.cos(a) < 0 ? 1.6 : 0;       // ด้านหลังหัวฟูกว่า
          const r = 7.4 + back + (i % 2 ? 1.1 : -0.2) + Math.sin(P.t * 0.11 + i * 1.7) * 0.35;
          const x = cx + Math.cos(a) * r - (P.air ? 0 : 0.5) * (Math.cos(a) < 0 ? 1 : 0), y = cy + Math.sin(a) * r * 0.92;
          if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);
        }
        ctx.closePath(); ctx.lineWidth = 1.3; shape('rgba(255,214,150,0.10)');
        ctx.strokeStyle = FAINT; ctx.lineWidth = 0.8;
        for (let i = 0; i < 8; i++) {
          const a = 1.9 + i * 0.36, r0 = 5, r1 = 8.2;
          ctx.beginPath(); ctx.moveTo(cx + Math.cos(a) * r0, cy + Math.sin(a) * r0); ctx.lineTo(cx + Math.cos(a) * r1, cy + Math.sin(a) * r1); ctx.stroke();
        }
        ctx.strokeStyle = STROKE; ctx.lineWidth = 1.5;
        ctx.beginPath(); ctx.arc(5.6, -3.6, 4.4, 0, TAU); shape('rgba(15,23,42,0.9)');
        ctx.beginPath(); ctx.ellipse(9, -1.9, 2.4, 1.8, 0, 0, TAU); shape(BODYF);
        dot(10.8, -2.6, 0.75, ORANGE); dot(7.2, -4.8, 0.95);
      },
      elephant(P) {
        // หูใหญ่โบกช้า ๆ อยู่หลังหัว
        const flap = Math.sin(P.t * 0.06) * 0.18 + (P.air ? -P.k * 0.2 : 0);
        ctx.save(); ctx.translate(2.2, -4); ctx.rotate(flap);
        ctx.beginPath(); ctx.ellipse(-1.4, 1.6, 4.4, 6.2, 0.15, 0, TAU); shape(BODYF);
        ctx.restore();
        ctx.beginPath(); ctx.moveTo(-1, 2); ctx.quadraticCurveTo(0, -7.6, 6.6, -7.6);
        ctx.quadraticCurveTo(11, -7.4, 11.4, -2.8); ctx.quadraticCurveTo(11.4, 1, 8.6, 2.8); shape(BODYF);
        ctx.save(); ctx.translate(2.2, -4); ctx.rotate(flap);
        ctx.beginPath(); ctx.ellipse(-1.4, 1.6, 4.4, 6.2, 0.15, -1.2, 1.9); ctx.stroke();
        ctx.restore();
        dot(7.6, -4.2, 1);
        ctx.strokeStyle = 'rgba(255,255,255,0.8)'; ctx.lineWidth = 1.3;   // งา
        ctx.beginPath(); ctx.moveTo(9.4, 1.6); ctx.quadraticCurveTo(11.6, 3.4, 13.6, 2.4); ctx.stroke();
        // งวง: โซ่ 6 ข้อ แกว่งตามจังหวะเดิน
        ctx.strokeStyle = STROKE;
        chain(10.6, -0.6, 6, 1.9, 1.15, 0.12, 1.1, P.t * 0.07 + P.p * TAU, 0.6, 2.6, 1.3);
      },
    };

    // ================= RABBIT — กระโดดเป็นจังหวะ (bound) =================
    function drawRabbit(P) {
      const p = ((P.p % 1) + 1) % 1;
      const hop = P.air ? 0 : Math.max(0, Math.sin(p * TAU)) * 4.2;     // ครึ่งรอบแรกลอย ครึ่งหลังแตะพื้น
      const vyl = P.air ? P.k : Math.cos(p * TAU);                         // + ขึ้น / − ลง
      const pitch = clamp(-vyl * 0.18, -0.2, 0.2);
      ctx.save(); ctx.translate(0, -hop);
      // ขาหลังไกล/ใกล้: ต้นขากลม + เท้ายาว (เหยียดตอนถีบ พับตอนลง)
      const ext = P.air ? (P.k > 0 ? 1 : 0.2) : (p < 0.5 ? 1 - p * 2 : 0.15);
      // ขาหลัง: ต้นขา → ข้อเท้า (hock) → เท้ายาวแนบพื้น · ext=1 ถีบเหยียดไปข้างหลัง
      const hindFoot = (far) => {
        ctx.strokeStyle = far ? SOFT : STROKE; ctx.lineWidth = 1.6;
        const o = far ? -0.8 : 0, hx = 4 + o, hy = -4.4;
        const kx = lerp(1.8, -1.6, ext) + o, ky = lerp(-1.6, -3, ext);
        const tx = lerp(7.4, -5.4, ext) + o, ty = lerp(0, -1.2, ext);
        ctx.beginPath(); ctx.moveTo(hx, hy); ctx.lineTo(kx, ky); ctx.lineTo(tx, ty); ctx.stroke();
      };
      hindFoot(true);
      ctx.save(); ctx.rotate(pitch);
      // ตัว
      ctx.strokeStyle = STROKE; ctx.lineWidth = 1.6;
      ctx.beginPath(); ctx.ellipse(8, -8.6, 8.2, 5.4, -0.1, 0, TAU); shape(BODYF); ctx.fillStyle = FILL; ctx.fill();
      ctx.beginPath(); ctx.ellipse(4.4, -7, 3.8, 3.9, 0, 0, TAU); shape(BODYF);   // ต้นขาหลัง
      dot(-0.4, -10.2, 2.1, 'rgba(255,255,255,0.75)');                            // หางปุย
      // ขาหน้า: ยื่นไปข้างหน้าตอนลอย แตะพื้นตอนลง
      const fr = P.air ? (P.k < 0 ? 1 : 0.4) : (p < 0.5 ? 0.3 + p : 1 - (p - 0.5));
      ctx.lineWidth = 1.4;
      [[13.4, SOFT, 0.6], [14.4, STROKE, 0]].forEach(([fx, c, o]) => {
        ctx.strokeStyle = c;
        ctx.beginPath(); ctx.moveTo(fx - 1, -6.2); ctx.lineTo(fx + fr * 2.4 - o, -2.8); ctx.lineTo(fx + fr * 3.4 - o, 0 + hop * 0 - (P.air ? 1.2 : 0)); ctx.stroke();
      });
      // หัว + หู (หูลู่ไปหลังตามแรงลม/ตอนขึ้น)
      ctx.strokeStyle = STROKE; ctx.lineWidth = 1.5;
      ctx.beginPath(); ctx.ellipse(17, -13.2, 4.2, 3.6, -0.2, 0, TAU); shape(BODYF);
      const ear = -0.5 - (vyl > 0 ? vyl * 0.35 : 0) + Math.sin(P.t * 0.09) * 0.06;
      [[15.4, ear - 0.12, SOFT], [17, ear, STROKE]].forEach(([ex, a, c]) => {
        ctx.save(); ctx.translate(ex, -16); ctx.rotate(a);
        ctx.strokeStyle = c; ctx.beginPath(); ctx.ellipse(0, -5, 1.6, 5.2, 0, 0, TAU); shape(BODYF);
        if (c === STROKE) { ctx.strokeStyle = FAINT; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.moveTo(0, -1.6); ctx.lineTo(0, -8.2); ctx.stroke(); }
        ctx.restore();
      });
      dot(18.6, -14, 0.95); dot(21, -12.3, 0.6, ORANGE);
      ctx.restore();
      hindFoot(false);
      ctx.restore();
    }

    // ================= TURTLE — ขาสั้นเดินทแยง กระดองโยกเบา ๆ =================
    function drawTurtle(P) {
      const D = { duty: 0.6, stride: 4, lift: 1.4 };
      const legs = (far) => [[3, 0], [19, 0.5]].forEach(([x, ph], i) => {
        const q = P.p + ph + (far ? 0.5 : 0);
        let fx, fy;
        if (P.air) { fx = x + (i ? 2.5 : -2.5); fy = -2.5; }
        else { const c = footCycle(q, D.duty, D.stride, D.lift); fx = x + c[0]; fy = c[1]; }
        const j = ik(x + (far ? -0.6 : 0), -5.4, fx, fy, 2.6, 3, i ? 1 : -1);
        ctx.strokeStyle = far ? SOFT : STROKE; ctx.lineWidth = 2.6;
        ctx.beginPath(); ctx.moveTo(x, -5.4); ctx.lineTo(j.kx, j.ky); ctx.lineTo(j.fx, j.fy); ctx.stroke();
      });
      legs(true);
      const rock = P.air ? -P.k * 0.06 : Math.sin(P.p * TAU * 2) * 0.03;
      ctx.save(); ctx.translate(11, -5); ctx.rotate(rock); ctx.translate(-11, 5);
      // หัว + คอ ยืด-หดตามก้าว
      const nk = P.air ? 1.2 : Math.sin(P.p * TAU) * 0.9;
      ctx.strokeStyle = STROKE; ctx.lineWidth = 1.6;
      ctx.beginPath(); ctx.moveTo(22, -7.6); ctx.quadraticCurveTo(25 + nk, -10, 28 + nk, -10.4);
      ctx.quadraticCurveTo(32 + nk, -10.6, 32 + nk, -8); ctx.quadraticCurveTo(31.6 + nk, -5.8, 27.6 + nk, -6.2);
      ctx.quadraticCurveTo(25, -6, 22.6, -5); shape(BODYF);
      dot(29.6 + nk, -9, 0.85);
      ctx.beginPath(); ctx.moveTo(-1.6, -6.4); ctx.lineTo(-4.2, -5); ctx.stroke();   // หาง
      // กระดอง + ลายเกล็ด
      ctx.lineWidth = 1.8;
      ctx.beginPath(); ctx.moveTo(-2.4, -5.2);
      ctx.quadraticCurveTo(-1.6, -18.4, 11, -18.4); ctx.quadraticCurveTo(23.6, -18.4, 24.4, -5.2); ctx.closePath();
      shape(BODYF); ctx.fillStyle = FILL; ctx.fill();
      ctx.strokeStyle = FAINT; ctx.lineWidth = 0.9;
      ctx.beginPath();
      ctx.moveTo(6, -16.6); ctx.lineTo(8.4, -11.6); ctx.lineTo(13.6, -11.6); ctx.lineTo(16, -16.6);
      ctx.moveTo(8.4, -11.6); ctx.lineTo(6.4, -5.4); ctx.moveTo(13.6, -11.6); ctx.lineTo(15.6, -5.4);
      ctx.moveTo(1.2, -10.4); ctx.lineTo(8.4, -11.6); ctx.moveTo(13.6, -11.6); ctx.lineTo(21, -10.4);
      ctx.stroke();
      ctx.strokeStyle = STROKE; ctx.lineWidth = 1.4; ctx.beginPath(); ctx.moveTo(-2.4, -5.2); ctx.lineTo(24.4, -5.2); ctx.stroke();
      ctx.restore();
      legs(false);
    }

    // ================= PENGUIN — เดินเตาะแตะ โยกซ้ายขวา =================
    function drawPenguin(P) {
      const p = ((P.p % 1) + 1) % 1, s = Math.sin(p * TAU);
      const rot = P.air ? -P.k * 0.1 : s * 0.13;
      const hop = P.air ? 0 : Math.abs(Math.cos(p * TAU)) * 1.1;
      // เท้า: ก้าวสลับ (เท้าไหนแตะพื้น อีกเท้ายก)
      const foot = (fx, lift, c) => { ctx.fillStyle = c; ctx.beginPath(); ctx.moveTo(fx - 2.4, -lift); ctx.lineTo(fx + 4, -lift); ctx.lineTo(fx + 0.6, -3 - lift); ctx.closePath(); ctx.fill(); };
      foot(7.6 + (P.air ? 1 : s * 1.6), P.air ? 2 : Math.max(0, s) * 2, 'rgba(255,183,94,0.55)');
      ctx.save(); ctx.translate(11, -hop); ctx.rotate(rot); ctx.translate(-11, 0);
      ctx.strokeStyle = STROKE; ctx.lineWidth = 1.6;
      ctx.beginPath(); ctx.ellipse(11, -15, 8.6, 13.6, 0, 0, TAU); shape('rgba(15,23,42,0.75)');
      ctx.fillStyle = 'rgba(255,255,255,0.18)'; ctx.beginPath(); ctx.ellipse(13, -13, 5.2, 10.2, 0, 0, TAU); ctx.fill();  // พุงขาว (ไม่มีเส้นขอบ)
      dot(15.2, -24.2, 1);
      ctx.fillStyle = ORANGE; ctx.beginPath(); ctx.moveTo(18.6, -24); ctx.lineTo(24.4, -22.6); ctx.lineTo(18.6, -21.2); ctx.closePath(); ctx.fill();
      // ครีบ: กางตอนโยก/กระพือตอนลอย
      const fl = P.air ? Math.sin(P.t * 0.6) * 0.6 : -s * 0.35;
      ctx.save(); ctx.translate(6.4, -18); ctx.rotate(0.35 + fl);
      ctx.beginPath(); ctx.ellipse(0, 5, 1.8, 6, 0, 0, TAU); shape('rgba(15,23,42,0.9)');
      ctx.restore();
      ctx.restore();
      foot(13.4 + (P.air ? 1 : -s * 1.6), P.air ? 2 : Math.max(0, -s) * 2, ORANGE);
    }

    const CUSTOM = { rabbit: drawRabbit, turtle: drawTurtle, penguin: drawPenguin };

    // ---- per-animal tuning: speed (px/เฟรม) · apex (ความสูงกระโดด) · len (ความยาวตัวใช้คิดระยะข้าม) ----
    //  inc (เฟสต่อเฟรม) ของสัตว์ 4 ขา = duty×speed/stride → เท้าแตะพื้นเลื่อนเท่าพื้นพอดี ไม่ไถล
    const CFG = {
      cat:      { speed: 1.9, apex: 24, len: 34, scale: 1.0 },
      dog:      { speed: 2.1, apex: 24, len: 36, scale: 1.0 },
      bear:     { speed: 1.5, apex: 20, len: 36, scale: 1.0 },
      rabbit:   { speed: 2.2, apex: 28, len: 26, scale: 1.05, inc: 0.05 },
      turtle:   { speed: 0.7, apex: 16, len: 34, scale: 1.0, inc: 0.105 },
      lion:     { speed: 2.0, apex: 24, len: 38, scale: 1.0 },
      penguin:  { speed: 1.2, apex: 18, len: 20, scale: 1.0, inc: 0.045 },
      elephant: { speed: 1.3, apex: 18, len: 44, scale: 0.98 },
    };
    const incOf = (id, cfg) => cfg.inc || (QUAD[id] ? QUAD[id].duty * cfg.speed / QUAD[id].stride : 0.08);

    // ---- state ----
    let catY = 0, vy = 0, v0 = 1, onGround = true, runPhase = 0, frame = 0, raf = 0;
    let dist = 0, curSpeed = 1.9, airSpeed = 1.9;
    let squash = 0;        // ยุบ-ยืดตอนลงพื้น/กระโดด (+ = ยุบ, − = ยืด) ค่อย ๆ คืนเป็น 0
    let tailLag = 0, tailVel = 0;   // สปริงหาง: สะบัดตามการเปลี่ยนความสูง
    const G = 0.32;
    let obstacles = [];
    let nextSpawnAt = 120;
    let dust = [];
    let meteor = null;
    const stars = Array.from({ length: 12 }, () => ({ x: Math.random() * 400, y: 5 + Math.random() * 34, r: Math.random() * 0.8 + 0.35, p: Math.random() * 6 }));

    const spawn = () => {
      const kind = Math.random() < 0.6 ? 'pine' : 'bush';
      const h = kind === 'pine' ? 14 + Math.floor(Math.random() * 3) * 4 : 10 + Math.floor(Math.random() * 2) * 3;
      obstacles.push({ x: W + 12, h, kind, twin: kind === 'pine' && Math.random() < 0.25 });
    };
    const puff = (n, x, spread, up) => {
      for (let i = 0; i < n; i++) dust.push({ x: x + (Math.random() - 0.5) * spread, y: groundY - 1,
        vx: -0.4 - Math.random() * 0.9, vy: -(Math.random() * up), r: 0.7 + Math.random() * 1.2, life: 1 });
    };

    function drawAnimal(id, x, baseY, P) {
      const cfg = CFG[id] || CFG.cat;
      ctx.save();
      ctx.translate(x, baseY);
      if (cfg.scale !== 1) ctx.scale(cfg.scale, cfg.scale);
      if (squash) { ctx.translate(12, 0); ctx.scale(1 + squash * 0.1, 1 - squash * 0.1); ctx.translate(-12, 0); }
      ctx.lineJoin = 'round'; ctx.lineCap = 'round';
      if (QUAD[id]) drawQuad(QUAD[id], P);
      else (CUSTOM[id] || drawPenguin)(P);
      ctx.restore();
    }

    function drawTree(x, h) {
      ctx.save();
      ctx.strokeStyle = STROKE; ctx.fillStyle = FILL; ctx.lineWidth = 1.6; ctx.lineJoin = 'round'; ctx.lineCap = 'round';
      const baseY = groundY;
      ctx.beginPath();
      ctx.moveTo(x, baseY - h);
      ctx.lineTo(x - 5, baseY - h * 0.5); ctx.lineTo(x - 2.5, baseY - h * 0.5);
      ctx.lineTo(x - 6.5, baseY - 3); ctx.lineTo(x + 6.5, baseY - 3);
      ctx.lineTo(x + 2.5, baseY - h * 0.5); ctx.lineTo(x + 5, baseY - h * 0.5);
      ctx.closePath(); ctx.fill(); ctx.stroke();
      ctx.beginPath(); ctx.moveTo(x, baseY - 3); ctx.lineTo(x, baseY); ctx.stroke();
      ctx.restore();
    }
    function drawBush(x, h) {
      ctx.save();
      ctx.strokeStyle = STROKE; ctx.fillStyle = FILL; ctx.lineWidth = 1.6; ctx.lineJoin = 'round'; ctx.lineCap = 'round';
      const r = h * 0.42, baseY = groundY;
      ctx.beginPath();
      ctx.moveTo(x - r * 1.6, baseY);
      ctx.arc(x - r * 0.7, baseY - r * 0.9, r * 0.9, Math.PI * 0.95, Math.PI * 1.75);
      ctx.arc(x + r * 0.2, baseY - r * 1.25, r, Math.PI * 1.15, Math.PI * 1.95);
      ctx.arc(x + r * 0.95, baseY - r * 0.75, r * 0.75, Math.PI * 1.4, Math.PI * 0.05);
      ctx.lineTo(x + r * 1.6, baseY);
      ctx.closePath(); ctx.fill(); ctx.stroke();
      ctx.restore();
    }

    // ภูเขา 2 ชั้น (parallax) — ฟังก์ชันคลื่นต่อเนื่อง ไม่ต้องสุ่ม/เก็บ state
    function drawHills(off, base, amp, alpha) {
      ctx.save();
      ctx.beginPath(); ctx.moveTo(0, groundY);
      for (let x = 0; x <= W + 4; x += 4) {
        const t = (x + off) * 0.018;
        ctx.lineTo(x, base - amp * (0.55 + 0.3 * Math.sin(t) + 0.15 * Math.sin(t * 2.3 + 1.7)));
      }
      ctx.lineTo(W, groundY); ctx.closePath();
      ctx.fillStyle = `rgba(148,170,210,${alpha})`; ctx.fill();
      ctx.restore();
    }

    const catX = 30;
    let lastId = animalRef.current;
    function loop() {
      ctx.clearRect(0, 0, W, H);
      const id = animalRef.current;
      const cfg = CFG[id] || CFG.cat;
      if (id !== lastId) { lastId = id; catY = 0; vy = 0; onGround = true; }   // เปลี่ยนตัวกลางอากาศ → เริ่มบนพื้น
      frame++;
      curSpeed = onGround ? cfg.speed : airSpeed;
      dist += curSpeed;

      // ดาว (กระพริบ) + ดวงจันทร์เสี้ยว
      ctx.fillStyle = '#fff';
      stars.forEach((s) => {
        const sx = ((s.x - dist * 0.03) % (W + 10) + W + 10) % (W + 10) - 5;
        ctx.globalAlpha = 0.15 + 0.35 * (0.5 + 0.5 * Math.sin(frame * 0.03 + s.p));
        ctx.beginPath(); ctx.arc(sx, s.y, s.r, 0, TAU); ctx.fill();
      });
      ctx.globalAlpha = 0.55;
      ctx.beginPath(); ctx.arc(W - 22, 15, 6, 0, TAU); ctx.fill();
      ctx.globalCompositeOperation = 'destination-out'; ctx.globalAlpha = 1;
      ctx.beginPath(); ctx.arc(W - 19.5, 13, 5.4, 0, TAU); ctx.fill();
      ctx.globalCompositeOperation = 'source-over';

      // ดาวตก (นาน ๆ ที)
      if (!meteor && Math.random() < 0.0025) meteor = { x: W * (0.3 + Math.random() * 0.5), y: 4, life: 1 };
      if (meteor) {
        const g = ctx.createLinearGradient(meteor.x, meteor.y, meteor.x + 16, meteor.y - 7);
        g.addColorStop(0, `rgba(255,255,255,${0.7 * meteor.life})`); g.addColorStop(1, 'rgba(255,255,255,0)');
        ctx.strokeStyle = g; ctx.lineWidth = 1.2;
        ctx.beginPath(); ctx.moveTo(meteor.x, meteor.y); ctx.lineTo(meteor.x + 16, meteor.y - 7); ctx.stroke();
        meteor.x -= 2.6; meteor.y += 1.1; meteor.life -= 0.03;
        if (meteor.life <= 0) meteor = null;
      }
      ctx.globalAlpha = 1;

      // ภูเขาไกล/ใกล้ — เลื่อนช้ากว่าพื้น
      drawHills(dist * 0.15, groundY - 6, 26, 0.06);
      drawHills(dist * 0.35 + 300, groundY - 2, 15, 0.08);

      // พื้น: เส้นทึบจาง + ขีดกรวดเลื่อนตามความเร็ว (เห็นว่ากำลังวิ่งจริง)
      ctx.strokeStyle = FAINT; ctx.lineWidth = 1.2;
      ctx.beginPath(); ctx.moveTo(0, groundY + 0.5); ctx.lineTo(W, groundY + 0.5); ctx.stroke();
      ctx.lineWidth = 1;
      for (let i = 0; i < W / 9 + 2; i++) {
        const gx = i * 9 - (dist % 9), k = (i + Math.floor(dist / 9)) % 5;
        ctx.globalAlpha = k === 0 ? 0.35 : 0.16;
        ctx.beginPath(); ctx.moveTo(gx, groundY + 4 + (k % 2) * 3); ctx.lineTo(gx + (k === 0 ? 4 : 2), groundY + 4 + (k % 2) * 3); ctx.stroke();
      }
      ctx.globalAlpha = 1;

      // physics
      const len = cfg.len * cfg.scale;
      if (!onGround) {
        catY += vy; vy -= G;
        if (catY <= 0) { catY = 0; vy = 0; onGround = true; squash = 1; puff(6, catX + len * 0.5, 18, 1.1); }
      }
      if (onGround) runPhase += incOf(id, cfg);
      squash *= 0.8; if (Math.abs(squash) < 0.02) squash = 0;
      if (onGround && frame % 16 === 0) puff(1, catX, 4, 0.5);
      // สปริงหาง: ตามแรงเร่งแนวตั้ง แล้วแกว่งกลับ
      tailVel += (-(onGround ? 0 : vy * 0.12) - tailLag) * 0.12; tailVel *= 0.86; tailLag += tailVel;

      // obstacles — เกิดตามระยะทาง (สัตว์ช้า/เร็ว ระยะห่างต้นไม้เท่ากัน)
      if (dist >= nextSpawnAt) { spawn(); nextSpawnAt = dist + 150 + Math.random() * 120; }
      obstacles.forEach((o) => o.x -= curSpeed);
      obstacles = obstacles.filter((o) => o.x > -20);

      // กระโดด: เวลาลอย = 2v₀/G · ต้องลอยพ้นทั้งตัว (len + ต้นไม้) → สัตว์ช้าพุ่งไปข้างหน้าตอนลอย
      if (onGround) {
        v0 = Math.sqrt(2 * G * cfg.apex);
        const airT = 2 * v0 / G;
        airSpeed = Math.max(cfg.speed, (len + 22) / airT);
        const cx = catX + len * 0.5;
        for (const o of obstacles) {
          const d = o.x - cx;
          if (d > 0 && d <= airSpeed * airT * 0.5 + 2) { vy = v0; onGround = false; squash = -0.7; puff(3, catX, 8, 0.6); break; }
        }
      }

      obstacles.forEach((o) => {
        if (o.kind === 'bush') drawBush(o.x, o.h);
        else { drawTree(o.x, o.h); if (o.twin) drawTree(o.x + 9, o.h - 4); }
      });

      // เงาใต้ตัว — เล็ก/จางลงเมื่อลอยสูง
      const shd = Math.max(0.35, 1 - catY / 36);
      ctx.fillStyle = `rgba(0,0,0,${0.38 * shd})`;
      ctx.beginPath(); ctx.ellipse(catX + len * 0.45, groundY + 1, len * 0.42 * shd, 2.2 * shd, 0, 0, TAU); ctx.fill();

      // ฝุ่น
      dust.forEach((p) => { p.x += p.vx - (curSpeed - 1) * 0.3; p.y += p.vy; p.vy += 0.05; p.life -= 0.035; });
      dust = dust.filter((p) => p.life > 0 && p.y < groundY + 2);
      dust.forEach((p) => { ctx.fillStyle = `rgba(255,255,255,${0.35 * p.life})`; ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, TAU); ctx.fill(); });

      const P = { p: runPhase, air: !onGround, k: onGround ? 0 : clamp(vy / (v0 || 1), -1, 1), t: frame, tailLag };
      drawAnimal(id, catX, groundY - catY, P);

      // ขอบซ้าย-ขวาค่อย ๆ จาง (ต้นไม้/ภูเขาไหลเข้า-ออกนุ่ม ๆ ไม่ตัดกึก)
      ctx.globalCompositeOperation = 'destination-out';
      [[0, 14, 1, 0], [W - 18, W, 0, 1]].forEach(([a, b, s, e]) => {
        const g = ctx.createLinearGradient(a, 0, b, 0);
        g.addColorStop(0, `rgba(0,0,0,${s})`); g.addColorStop(1, `rgba(0,0,0,${e})`);
        ctx.fillStyle = g; ctx.fillRect(a, 0, b - a, H);
      });
      ctx.globalCompositeOperation = 'source-over';

      raf = requestAnimationFrame(loop);
    }
    raf = requestAnimationFrame(loop);

    return () => { cancelAnimationFrame(raf); ro.disconnect(); };
  }, []);

  return (
    <div className="cat-runner">
      <div ref={wrapRef} className="cat-runner-stage" title="คลิกเพื่อเปลี่ยนตัวละคร" onClick={cycle}>
        <canvas ref={canvasRef}></canvas>
      </div>
      <div className="cat-runner-cap">WORKING<span className="cat-runner-dots"><i>.</i><i>.</i><i>.</i></span></div>
      <div className="runner-pick">
        {RUNNER_ANIMALS.map(a => (
          <button key={a.id}
            className={'runner-emoji' + (a.id === animalId ? ' on' : '')}
            title={a.label} onClick={() => pick(a.id)}>{a.emoji}</button>
        ))}
      </div>
    </div>);
}
window.CatRunner = CatRunner;
