/* global React */
const { useState, useEffect, useRef, useMemo, useCallback } = React;

/* ============== Cat SVG ============== */
function Cat({ size = 280, mood = "happy", outfit = "none", purring = false, blink = true }) {
  const [blinking, setBlinking] = useState(false);
  useEffect(() => {
    if (!blink) return;
    let t;
    const loop = () => {
      const wait = 2400 + Math.random() * 2600;
      t = setTimeout(() => {
        setBlinking(true);
        setTimeout(() => setBlinking(false), 130);
        loop();
      }, wait);
    };
    loop();
    return () => clearTimeout(t);
  }, [blink]);

  // mood: happy | content | sleepy | excited
  const eyeClosed = blinking || mood === "sleepy";

  // Chibi cat: big round head, small body, tiny tail. Soft inky outline.
  const stroke = "#5a4a52";
  const sw = 2.2;
  const fur = "#fdfcfa";
  const furShade = "#ece6e1";
  const earInner = "#f4d4dd";

  return (
    <svg width={size} height={size} viewBox="0 0 260 260" style={{ display: "block" }}>
      <defs>
        <radialGradient id="furGrad" cx="50%" cy="42%" r="65%">
          <stop offset="0%" stopColor="#ffffff" />
          <stop offset="70%" stopColor={fur} />
          <stop offset="100%" stopColor={furShade} />
        </radialGradient>
        <linearGradient id="hoodieGrad" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#7d8a9a" />
          <stop offset="100%" stopColor="#5a6573" />
        </linearGradient>
        <linearGradient id="ribbonGrad" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#ffafcc" />
          <stop offset="100%" stopColor="#ff89b3" />
        </linearGradient>
      </defs>

      {/* Tail — long, thin, curling behind */}
      <path
        d="M 168 198 Q 198 200 208 188 Q 216 178 210 168"
        fill="none"
        stroke={stroke}
        strokeWidth={sw}
        strokeLinecap="round"
      />
      <path
        d="M 168 198 Q 198 200 208 188 Q 216 178 210 168"
        fill="none"
        stroke={fur}
        strokeWidth={sw - 1.4}
        strokeLinecap="round"
      />

      {/* Body — small, oval, sits below the big head */}
      <path
        d="M 96 170 Q 96 145 130 145 Q 164 145 164 170 L 164 210 Q 164 218 156 218 L 104 218 Q 96 218 96 210 Z"
        fill="url(#furGrad)"
        stroke={stroke}
        strokeWidth={sw}
        strokeLinejoin="round"
      />

      {/* Feet */}
      <ellipse cx="112" cy="218" rx="9" ry="4" fill={fur} stroke={stroke} strokeWidth={sw - 0.6} />
      <ellipse cx="148" cy="218" rx="9" ry="4" fill={fur} stroke={stroke} strokeWidth={sw - 0.6} />

      {/* Hoodie overlay — ALWAYS show (matches reference) unless explicitly stripped to none-naked */}
      {outfit !== "naked" && outfit !== "scarf" && (
        <g>
          {/* hoodie body */}
          <path
            d="M 92 168 Q 92 156 104 152 L 156 152 Q 168 156 168 168 L 168 208 Q 168 216 160 216 L 100 216 Q 92 216 92 208 Z"
            fill="url(#hoodieGrad)"
            stroke={stroke}
            strokeWidth={sw}
            strokeLinejoin="round"
          />
          {/* hood at neck */}
          <path
            d="M 96 158 Q 110 148 130 148 Q 150 148 164 158 Q 158 168 130 168 Q 102 168 96 158 Z"
            fill="#48515e"
            stroke={stroke}
            strokeWidth={sw}
            strokeLinejoin="round"
          />
          {/* zipper */}
          <line x1="130" y1="160" x2="130" y2="214" stroke="#3b424d" strokeWidth="1.3" />
          {/* pocket */}
          <path d="M 108 188 L 152 188 L 148 200 L 112 200 Z" fill="none" stroke="#3b424d" strokeWidth="1.2" />
          {/* drawstrings */}
          <line x1="120" y1="160" x2="118" y2="172" stroke={stroke} strokeWidth="1.3" strokeLinecap="round" />
          <line x1="140" y1="160" x2="142" y2="172" stroke={stroke} strokeWidth="1.3" strokeLinecap="round" />
        </g>
      )}

      {/* Head — big and round */}
      <g>
        {/* Ears — triangular with rounded tips */}
        <path
          d="M 78 80 Q 76 38 96 56 Q 110 68 110 82 Z"
          fill="url(#furGrad)"
          stroke={stroke}
          strokeWidth={sw}
          strokeLinejoin="round"
        />
        <path
          d="M 182 80 Q 184 38 164 56 Q 150 68 150 82 Z"
          fill="url(#furGrad)"
          stroke={stroke}
          strokeWidth={sw}
          strokeLinejoin="round"
        />
        {/* inner ears */}
        <path d="M 88 70 Q 90 50 100 62 Q 104 70 102 78 Z" fill={earInner} />
        <path d="M 172 70 Q 170 50 160 62 Q 156 70 158 78 Z" fill={earInner} />

        {/* Head shape — wide round with slight cheek poof */}
        <path
          d="M 60 110 Q 60 70 130 70 Q 200 70 200 110 Q 200 158 130 158 Q 60 158 60 110 Z"
          fill="url(#furGrad)"
          stroke={stroke}
          strokeWidth={sw}
        />

        {/* Eyes — simple inky almonds (closed = curve) */}
        {eyeClosed ? (
          <g stroke={stroke} strokeWidth="2.6" fill="none" strokeLinecap="round">
            <path d="M 100 116 Q 108 122 116 116" />
            <path d="M 144 116 Q 152 122 160 116" />
          </g>
        ) : mood === "excited" ? (
          <g stroke={stroke} strokeWidth="2.6" fill="none" strokeLinecap="round">
            <path d="M 100 120 Q 108 110 116 120" />
            <path d="M 144 120 Q 152 110 160 120" />
          </g>
        ) : (
          <g fill={stroke}>
            <ellipse cx="108" cy="118" rx="3.2" ry="5.2" />
            <ellipse cx="152" cy="118" rx="3.2" ry="5.2" />
            <circle cx="109.2" cy="115.5" r="1.1" fill="#fff" />
            <circle cx="153.2" cy="115.5" r="1.1" fill="#fff" />
          </g>
        )}

        {/* Tiny mouth — small "w" smile */}
        <g fill="none" stroke={stroke} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
          <path d="M 122 132 Q 126 136 130 132 Q 134 136 138 132" />
        </g>

        {/* Whiskers — thin dark lines */}
        <g stroke={stroke} strokeWidth="1.2" strokeLinecap="round" opacity="0.85">
          <line x1="86" y1="128" x2="68" y2="124" />
          <line x1="86" y1="132" x2="66" y2="134" />
          <line x1="174" y1="128" x2="192" y2="124" />
          <line x1="174" y1="132" x2="194" y2="134" />
        </g>

        {/* Outfit-on-head overlays */}
        {outfit === "ribbon" && (
          <g>
            <path d="M 156 60 Q 168 50 178 60 Q 168 70 156 60 Z" fill="url(#ribbonGrad)" stroke="#ff6fa0" strokeWidth="1.5" />
            <path d="M 156 60 Q 144 50 134 60 Q 144 70 156 60 Z" fill="url(#ribbonGrad)" stroke="#ff6fa0" strokeWidth="1.5" />
            <circle cx="156" cy="60" r="3.5" fill="#ff6fa0" />
          </g>
        )}
        {outfit === "headphones" && (
          <g>
            <path d="M 78 78 Q 78 36 130 36 Q 182 36 182 78" fill="none" stroke="#cdb4db" strokeWidth="6" strokeLinecap="round" />
            <ellipse cx="78" cy="80" rx="11" ry="14" fill="#cdb4db" stroke={stroke} strokeWidth="1.4" />
            <ellipse cx="182" cy="80" rx="11" ry="14" fill="#cdb4db" stroke={stroke} strokeWidth="1.4" />
            <ellipse cx="78" cy="80" rx="6" ry="9" fill="#a98ac0" />
            <ellipse cx="182" cy="80" rx="6" ry="9" fill="#a98ac0" />
          </g>
        )}
        {outfit === "crown" && (
          <g>
            <path d="M 100 50 L 110 70 L 120 54 L 130 72 L 140 54 L 150 70 L 160 50 L 160 76 L 100 76 Z"
                  fill="#ffe8a3" stroke="#c79b22" strokeWidth="1.8" strokeLinejoin="round" />
            <circle cx="110" cy="70" r="2" fill="#ff89b3" />
            <circle cx="130" cy="72" r="2" fill="#cdb4db" />
            <circle cx="150" cy="70" r="2" fill="#b8e0d2" />
          </g>
        )}
        {outfit === "scarf" && (
          <g>
            <path d="M 80 150 Q 130 170 180 150 L 180 168 Q 130 188 80 168 Z"
                  fill="#cdb4db" stroke="#7e6493" strokeWidth="1.6" strokeLinejoin="round" />
            <path d="M 170 166 L 184 198 L 168 192 L 162 170 Z" fill="#cdb4db" stroke="#7e6493" strokeWidth="1.6" strokeLinejoin="round" />
          </g>
        )}
        {outfit === "flower" && (
          <g transform="translate(90 70)">
            <circle r="6" cx="-6" cy="0" fill="#ffafcc" />
            <circle r="6" cx="6" cy="0" fill="#ffafcc" />
            <circle r="6" cx="0" cy="-6" fill="#ffafcc" />
            <circle r="6" cx="0" cy="6" fill="#ffafcc" />
            <circle r="5" cx="0" cy="0" fill="#ffe8a3" />
          </g>
        )}
      </g>

      {/* Sleepy z's */}
      {mood === "sleepy" && (
        <g transform="translate(190 56)" fontFamily="Fredoka, sans-serif" fontWeight="600">
          <text fill="#a98ac0" fontSize="22" opacity="0.85">z</text>
          <text fill="#a98ac0" fontSize="16" x="14" y="-14" opacity="0.55">z</text>
        </g>
      )}
    </svg>
  );
}

/* ============== Tiny SVG icons ============== */
const Icon = {
  paw: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <ellipse cx="6" cy="9" rx="2" ry="2.6" />
      <ellipse cx="12" cy="6.5" rx="2.2" ry="2.8" />
      <ellipse cx="18" cy="9" rx="2" ry="2.6" />
      <ellipse cx="9" cy="13.5" rx="1.8" ry="2.4" />
      <ellipse cx="15" cy="13.5" rx="1.8" ry="2.4" />
      <path d="M 7 18 Q 7 14 12 14 Q 17 14 17 18 Q 17 21 12 21 Q 7 21 7 18 Z" />
    </svg>
  ),
  fish: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 3 12 Q 8 5 14 6 Q 20 7 21 12 Q 20 17 14 18 Q 8 19 3 12 Z" />
      <circle cx="16" cy="11" r="1" fill="#fff" />
      <path d="M 3 12 L -1 8 L -1 16 Z" transform="translate(2 0)" />
    </svg>
  ),
  star: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 12 2 L 14.5 9 L 22 9.5 L 16 14 L 18 21.5 L 12 17 L 6 21.5 L 8 14 L 2 9.5 L 9.5 9 Z" />
    </svg>
  ),
  heart: (s = 18, c = "#ff89b3") => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill={c}>
      <path d="M 12 21 C -4 11 5 1 12 8 C 19 1 28 11 12 21 Z" />
    </svg>
  ),
  sparkle: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 12 2 L 13.5 10.5 L 22 12 L 13.5 13.5 L 12 22 L 10.5 13.5 L 2 12 L 10.5 10.5 Z" />
    </svg>
  ),
  cloud: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 6 18 Q 2 18 2 14 Q 2 11 5 10 Q 5 6 10 6 Q 14 6 15 9 Q 19 9 20 12 Q 22 13 22 16 Q 22 18 19 18 Z" />
    </svg>
  ),
  music: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 9 17 Q 6 17 6 19 Q 6 21 9 21 Q 11 21 11 19 L 11 7 L 18 5 L 18 15 Q 15 15 15 17 Q 15 19 18 19 Q 20 19 20 17 L 20 3 L 9 6 Z" />
    </svg>
  ),
  yarn: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <circle cx="12" cy="12" r="9" fill="currentColor" fillOpacity="0.2" />
      <path d="M 4 10 Q 12 6 20 10" />
      <path d="M 4 14 Q 12 10 20 14" />
      <path d="M 8 4 Q 14 12 18 20" />
      <path d="M 6 6 Q 12 14 16 22" />
    </svg>
  ),
  bowl: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 3 11 L 21 11 Q 20 19 12 19 Q 4 19 3 11 Z" />
      <ellipse cx="12" cy="11" rx="9" ry="2" opacity="0.4" />
    </svg>
  ),
  leaf: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 4 20 Q 4 8 14 4 Q 22 4 20 14 Q 16 22 4 20 Z" />
    </svg>
  ),
  moon: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 18 14 Q 12 16 8 12 Q 4 8 8 2 Q 2 4 2 12 Q 2 22 12 22 Q 18 22 20 16 Q 19 14 18 14 Z" />
    </svg>
  ),
  trophy: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 7 4 L 17 4 L 17 10 Q 17 14 12 14 Q 7 14 7 10 Z" />
      <path d="M 4 5 L 7 5 L 7 9 Q 4 9 4 6 Z M 17 5 L 20 5 L 20 6 Q 20 9 17 9 Z" />
      <rect x="9" y="14" width="6" height="3" />
      <rect x="6" y="17" width="12" height="3" rx="1" />
    </svg>
  ),
  clover: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <circle cx="12" cy="6" r="3.5" />
      <circle cx="6" cy="12" r="3.5" />
      <circle cx="18" cy="12" r="3.5" />
      <circle cx="12" cy="18" r="3.5" />
      <circle cx="12" cy="12" r="1.5" fill="#fff" />
    </svg>
  ),
  gear: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <path d="M 12 2 L 13 5 L 16 4 L 17 7 L 20 8 L 19 11 L 22 13 L 19 15 L 20 18 L 17 19 L 16 22 L 13 21 L 12 24 L 11 21 L 8 22 L 7 19 L 4 18 L 5 15 L 2 13 L 5 11 L 4 8 L 7 7 L 8 4 L 11 5 Z" />
      <circle cx="12" cy="13" r="3.5" fill="#fff" />
    </svg>
  ),
  sun: (s = 18) => (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor">
      <circle cx="12" cy="12" r="4" />
      <g stroke="currentColor" strokeWidth="2" strokeLinecap="round">
        <line x1="12" y1="2" x2="12" y2="5" />
        <line x1="12" y1="19" x2="12" y2="22" />
        <line x1="2" y1="12" x2="5" y2="12" />
        <line x1="19" y1="12" x2="22" y2="12" />
        <line x1="5" y1="5" x2="7" y2="7" />
        <line x1="17" y1="17" x2="19" y2="19" />
        <line x1="5" y1="19" x2="7" y2="17" />
        <line x1="17" y1="7" x2="19" y2="5" />
      </g>
    </svg>
  ),
};

window.Cat = Cat;
window.Icon = Icon;
