/* eslint-disable */
// Scene library for card art: self-contained SVG illustrations built from
// primitive shapes, parametrized by a palette.

function Stars({ seed = 1, count = 5, color = "#fff" }) {
  // Disabled
  return null;
}

function Sparkle({ x, y, size = 6, color = "#fff" }) {
  // Disabled
  return null;
}

const SCENES = {
  // bird
  bird({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={11} count={6} color={p.starlight || "#fff"} />
        <ellipse cx="40" cy="160" rx="80" ry="6" fill={p.shadow} opacity="0.3" />
        {/* body */}
        <circle cx="100" cy="105" r="50" fill={p.body} />
        {/* belly */}
        <ellipse cx="100" cy="120" rx="30" ry="34" fill={p.belly} />
        {/* wing */}
        <path d={`M 130 100 q 28 -8 28 18 q 0 16 -16 16 q -20 0 -22 -18 z`} fill={p.wing} />
        {/* eye */}
        <circle cx="116" cy="90" r="10" fill="white" />
        <circle cx="118" cy="90" r="5" fill={p.dark || "#1a1a2e"} />
        <circle cx="120" cy="88" r="1.6" fill="white" />
        {/* beak */}
        <path d="M 144 100 l 12 4 l -10 6 z" fill={p.accent} />
        {/* foot */}
        <rect x="92" y="150" width="6" height="10" rx="2" fill={p.accent} />
        <rect x="108" y="150" width="6" height="10" rx="2" fill={p.accent} />
      </svg>
    );
  },

  // Planet with ring and moon
  planet({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={23} count={8} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="100" rx="78" ry="22" fill={p.ring} transform="rotate(-18 100 100)" />
        <circle cx="100" cy="100" r="50" fill={p.body} />
        <ellipse cx="84" cy="86" rx="18" ry="10" fill={p.highlight} opacity="0.85" />
        <ellipse cx="100" cy="100" rx="78" ry="22" fill="none" stroke={p.ringEdge} strokeWidth="3" transform="rotate(-18 100 100)" strokeDasharray="0 999 60" />
        {/* small moon */}
        <circle cx="36" cy="42" r="11" fill={p.moon} />
        <circle cx="32" cy="40" r="3" fill={p.moonShadow} opacity="0.6" />
      </svg>
    );
  },

  // Crescent moon with stars
  moon({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={37} count={10} color={p.starlight || "#fff"} />
        <circle cx="110" cy="90" r="58" fill={p.body} />
        <circle cx="138" cy="78" r="50" fill={p.shadow} />
        <circle cx="68" cy="60" r="4" fill={p.accent} />
        <Sparkle x={42} y={120} size={7} color={p.sparkle || p.accent} />
        <Sparkle x={170} y={140} size={5} color={p.sparkle || p.accent} />
      </svg>
    );
  },

  // Sleeping cat
  cat({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={41} count={7} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="156" rx="80" ry="6" fill={p.shadow} opacity="0.3" />
        {/* body */}
        <ellipse cx="100" cy="120" rx="64" ry="30" fill={p.body} />
        {/* tail */}
        <path d={`M 36 118 q -16 -4 -14 18 q 2 14 14 12`} stroke={p.body} strokeWidth="14" fill="none" strokeLinecap="round" />
        {/* head */}
        <circle cx="148" cy="100" r="22" fill={p.body} />
        {/* ears */}
        <path d={`M 132 84 l 4 -14 l 12 8 z`} fill={p.body} />
        <path d={`M 164 86 l 0 -14 l 10 6 z`} fill={p.body} />
        {/* closed eye */}
        <path d="M 142 100 q 4 -4 8 0" stroke={p.dark} strokeWidth="2" fill="none" strokeLinecap="round" />
        <path d="M 154 100 q 4 -4 8 0" stroke={p.dark} strokeWidth="2" fill="none" strokeLinecap="round" />
        {/* whiskers */}
        <path d="M 144 110 l -8 -2" stroke={p.dark} strokeWidth="1.4" />
        <path d="M 144 113 l -8 2" stroke={p.dark} strokeWidth="1.4" />
        {/* "z z" */}
        <text x="166" y="62" fontFamily="Nunito" fontWeight="900" fontSize="20" fill={p.accent}>z</text>
        <text x="178" y="48" fontFamily="Nunito" fontWeight="900" fontSize="14" fill={p.accent}>z</text>
      </svg>
    );
  },

  // Coffee mug
  cup({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={5} count={5} color={p.starlight || "#fff"} />
        {/* steam */}
        <path d="M 80 32 q -8 16 0 32" stroke={p.steam} strokeWidth="5" fill="none" strokeLinecap="round" opacity="0.8" />
        <path d="M 100 28 q 8 16 0 32" stroke={p.steam} strokeWidth="5" fill="none" strokeLinecap="round" opacity="0.9" />
        <path d="M 120 32 q -8 16 0 32" stroke={p.steam} strokeWidth="5" fill="none" strokeLinecap="round" opacity="0.8" />
        {/* handle */}
        <path d="M 148 110 q 22 0 22 -22 q 0 -20 -22 -20" stroke={p.body} strokeWidth="10" fill="none" />
        {/* mug body */}
        <rect x="60" y="68" width="88" height="78" rx="14" fill={p.body} />
        {/* coffee top */}
        <ellipse cx="104" cy="78" rx="38" ry="8" fill={p.coffee} />
        {/* heart on mug */}
        <path d="M 104 110 q -6 -10 -14 -2 q -6 6 14 22 q 20 -16 14 -22 q -8 -8 -14 2 z" fill={p.accent} />
      </svg>
    );
  },

  // Heart with sparkles
  heart({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={7} count={6} color={p.starlight || "#fff"} />
        <path d="M 100 156 C 36 116 28 78 60 60 C 84 46 100 70 100 80 C 100 70 116 46 140 60 C 172 78 164 116 100 156 Z" fill={p.body} />
        <circle cx="78" cy="78" r="8" fill={p.highlight} opacity="0.85" />
        <Sparkle x={36} y={42} size={8} color={p.accent} />
        <Sparkle x={170} y={64} size={7} color={p.accent} />
        <Sparkle x={150} y={140} size={5} color={p.accent} />
      </svg>
    );
  },

  // Star burst
  star({ p }) {
    const pts = [];
    for (let i = 0; i < 10; i++) {
      const a = (Math.PI * 2 * i) / 10 - Math.PI / 2;
      const r = i % 2 ? 22 : 52;
      pts.push(`${100 + Math.cos(a) * r},${90 + Math.sin(a) * r}`);
    }
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={13} count={9} color={p.starlight || "#fff"} />
        <circle cx="100" cy="90" r="56" fill={p.glow} opacity="0.35" />
        <polygon points={pts.join(" ")} fill={p.body} />
        <circle cx="100" cy="90" r="14" fill={p.highlight} />
        <Sparkle x={30} y={140} size={6} color={p.accent} />
        <Sparkle x={172} y={36} size={8} color={p.accent} />
        <Sparkle x={172} y={148} size={5} color={p.accent} />
      </svg>
    );
  },

  // Flame
  flame({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={29} count={7} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="160" rx="48" ry="8" fill={p.shadow} opacity="0.4" />
        <path d="M 100 24 C 70 60 60 88 60 110 C 60 138 78 156 100 156 C 122 156 140 138 140 110 C 140 88 130 60 100 24 Z" fill={p.body} />
        <path d="M 100 60 C 84 80 78 100 78 116 C 78 134 88 146 100 146 C 112 146 122 134 122 116 C 122 100 116 80 100 60 Z" fill={p.inner} />
        <ellipse cx="100" cy="126" rx="10" ry="14" fill={p.highlight} />
      </svg>
    );
  },

  // Diamond / gem
  diamond({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={61} count={8} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="158" rx="46" ry="6" fill={p.shadow} opacity="0.4" />
        <polygon points="60,78 100,40 140,78 100,156" fill={p.body} />
        <polygon points="60,78 100,40 100,78" fill={p.facet1} />
        <polygon points="100,40 140,78 100,78" fill={p.facet2} />
        <polygon points="60,78 100,78 100,156" fill={p.facet3} />
        <Sparkle x={38} y={40} size={8} color={p.accent} />
        <Sparkle x={170} y={50} size={6} color={p.accent} />
        <Sparkle x={166} y={130} size={5} color={p.accent} />
      </svg>
    );
  },

  // Cassette tape
  cassette({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={47} count={5} color={p.starlight || "#fff"} />
        <rect x="34" y="50" width="132" height="84" rx="10" fill={p.body} />
        <rect x="44" y="62" width="112" height="32" rx="4" fill={p.label} />
        <circle cx="74" cy="112" r="14" fill={p.dark} />
        <circle cx="74" cy="112" r="4" fill={p.label} />
        <circle cx="126" cy="112" r="14" fill={p.dark} />
        <circle cx="126" cy="112" r="4" fill={p.label} />
        <path d="M 96 70 q 4 -6 8 0 m 0 0 q -4 6 -8 0" stroke={p.accent} strokeWidth="2" fill="none" />
        <text x="100" y="84" textAnchor="middle" fontFamily="Nunito" fontSize="10" fontWeight="900" fill={p.dark}>LO-FI</text>
      </svg>
    );
  },

  // Lamp
  lamp({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <circle cx="100" cy="76" r="80" fill={p.glow} opacity="0.45" />
        <Stars seed={71} count={4} color={p.starlight || "#fff"} />
        {/* shade */}
        <path d="M 60 84 L 78 38 L 122 38 L 140 84 Z" fill={p.shade} />
        <rect x="92" y="84" width="16" height="6" fill={p.dark} />
        {/* stand */}
        <rect x="96" y="90" width="8" height="50" fill={p.stand} />
        <rect x="72" y="138" width="56" height="10" rx="3" fill={p.stand} />
        <circle cx="100" cy="84" r="10" fill={p.bulb} />
      </svg>
    );
  },

  // Monitor
  monitor({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={83} count={5} color={p.starlight || "#fff"} />
        <rect x="30" y="40" width="140" height="80" rx="10" fill={p.body} />
        <rect x="40" y="50" width="120" height="60" rx="4" fill={p.screen} />
        <rect x="86" y="120" width="28" height="14" fill={p.body} />
        <rect x="60" y="132" width="80" height="6" rx="3" fill={p.dark} />
        {/* on screen */}
        <circle cx="100" cy="80" r="14" fill={p.accent} />
        <rect x="56" y="98" width="50" height="4" rx="2" fill={p.accent} opacity="0.8" />
        <rect x="56" y="106" width="36" height="4" rx="2" fill={p.accent} opacity="0.5" />
      </svg>
    );
  },

  // Speech bubble
  speech({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={101} count={5} color={p.starlight || "#fff"} />
        <path d="M 36 40 h 128 a 12 12 0 0 1 12 12 v 60 a 12 12 0 0 1 -12 12 h -42 l -16 18 l -4 -18 h -66 a 12 12 0 0 1 -12 -12 v -60 a 12 12 0 0 1 12 -12 z" fill={p.body} />
        <circle cx="76" cy="82" r="6" fill={p.accent} />
        <circle cx="100" cy="82" r="6" fill={p.accent} />
        <circle cx="124" cy="82" r="6" fill={p.accent} />
      </svg>
    );
  },

  // Snowflake
  snow({ p }) {
    const arms = [];
    for (let i = 0; i < 6; i++) {
      arms.push(<g key={i} transform={`rotate(${i * 60} 100 90)`}>
        <line x1="100" y1="90" x2="100" y2="34" stroke={p.body} strokeWidth="6" strokeLinecap="round" />
        <line x1="100" y1="50" x2="86" y2="40" stroke={p.body} strokeWidth="4" strokeLinecap="round" />
        <line x1="100" y1="50" x2="114" y2="40" stroke={p.body} strokeWidth="4" strokeLinecap="round" />
      </g>);
    }
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={113} count={7} color={p.starlight || "#fff"} />
        <circle cx="100" cy="90" r="56" fill={p.glow} opacity="0.3" />
        {arms}
        <circle cx="100" cy="90" r="8" fill={p.body} />
      </svg>
    );
  },

  // Polaroid
  polaroid({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={131} count={6} color={p.starlight || "#fff"} />
        <g transform="rotate(-8 100 90)">
          <rect x="50" y="36" width="100" height="116" rx="6" fill="white" />
          <rect x="58" y="44" width="84" height="76" fill={p.photo} />
          <circle cx="100" cy="82" r="22" fill={p.accent} />
          <rect x="68" y="124" width="64" height="4" rx="2" fill={p.dark} opacity="0.4" />
          <rect x="68" y="132" width="40" height="4" rx="2" fill={p.dark} opacity="0.3" />
        </g>
      </svg>
    );
  },

  // Microphone
  mic({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={151} count={7} color={p.starlight || "#fff"} />
        <rect x="88" y="32" width="24" height="60" rx="12" fill={p.body} />
        <rect x="93" y="50" width="14" height="3" fill={p.dark} opacity="0.5" />
        <rect x="93" y="60" width="14" height="3" fill={p.dark} opacity="0.5" />
        <rect x="93" y="70" width="14" height="3" fill={p.dark} opacity="0.5" />
        <path d="M 70 84 q 0 34 30 34 q 30 0 30 -34" stroke={p.accent} strokeWidth="5" fill="none" strokeLinecap="round" />
        <rect x="96" y="118" width="8" height="22" fill={p.accent} />
        <rect x="80" y="138" width="40" height="6" rx="3" fill={p.accent} />
        <Sparkle x={40} y={56} size={7} color={p.starlight} />
        <Sparkle x={160} y={108} size={6} color={p.starlight} />
      </svg>
    );
  },

  // Galaxy
  galaxy({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <defs>
          <radialGradient id="g-gal" cx="50%" cy="50%">
            <stop offset="0%" stopColor={p.center} />
            <stop offset="60%" stopColor={p.body} />
            <stop offset="100%" stopColor={p.dark} stopOpacity="0" />
          </radialGradient>
        </defs>
        <Stars seed={181} count={12} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="90" rx="80" ry="32" fill="url(#g-gal)" transform="rotate(-15 100 90)" />
        <circle cx="100" cy="90" r="14" fill={p.center} />
        <Sparkle x={166} y={36} size={6} color={p.accent} />
        <Sparkle x={36} y={134} size={7} color={p.accent} />
      </svg>
    );
  },

  // Blob
  blob({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={199} count={6} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="158" rx="56" ry="6" fill={p.shadow} opacity="0.4" />
        <path d="M 60 110 C 60 60 100 50 100 50 C 100 50 140 60 140 110 C 140 144 122 156 100 156 C 78 156 60 144 60 110 Z" fill={p.body} />
        <circle cx="84" cy="100" r="6" fill={p.dark} />
        <circle cx="116" cy="100" r="6" fill={p.dark} />
        <path d="M 88 122 q 12 10 24 0" stroke={p.dark} strokeWidth="3" fill="none" strokeLinecap="round" />
        <circle cx="72" cy="118" r="6" fill={p.blush} opacity="0.7" />
        <circle cx="128" cy="118" r="6" fill={p.blush} opacity="0.7" />
      </svg>
    );
  },

  // Owl
  owl({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={211} count={8} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="160" rx="56" ry="6" fill={p.shadow} opacity="0.3" />
        <ellipse cx="100" cy="110" rx="48" ry="50" fill={p.body} />
        <ellipse cx="100" cy="124" rx="28" ry="32" fill={p.belly} />
        {/* eye disks */}
        <circle cx="82" cy="92" r="16" fill={p.eyeDisk} />
        <circle cx="118" cy="92" r="16" fill={p.eyeDisk} />
        <circle cx="82" cy="92" r="6" fill={p.dark} />
        <circle cx="118" cy="92" r="6" fill={p.dark} />
        <circle cx="84" cy="90" r="1.6" fill="white" />
        <circle cx="120" cy="90" r="1.6" fill="white" />
        {/* beak */}
        <path d="M 96 102 l 4 8 l 4 -8 z" fill={p.accent} />
        {/* ear tufts */}
        <path d="M 72 64 l 4 -12 l 8 8 z" fill={p.body} />
        <path d="M 128 64 l -4 -12 l -8 8 z" fill={p.body} />
      </svg>
    );
  },

  // Controller
  controller({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={223} count={5} color={p.starlight || "#fff"} />
        <path d="M 50 70 C 30 70 24 90 30 110 C 36 130 60 130 76 120 L 124 120 C 140 130 164 130 170 110 C 176 90 170 70 150 70 C 132 70 124 80 100 80 C 76 80 68 70 50 70 Z" fill={p.body} />
        <circle cx="64" cy="100" r="14" fill={p.dark} />
        <rect x="56" y="98" width="16" height="4" fill={p.accent} />
        <rect x="62" y="92" width="4" height="16" fill={p.accent} />
        <circle cx="132" cy="92" r="4" fill={p.button1} />
        <circle cx="146" cy="100" r="4" fill={p.button2} />
        <circle cx="132" cy="108" r="4" fill={p.button3} />
        <circle cx="118" cy="100" r="4" fill={p.button4} />
      </svg>
    );
  },

  // Cookie
  cookie({ p }) {
    return (
      <svg viewBox="0 0 200 180" preserveAspectRatio="xMidYMid slice">
        <Stars seed={239} count={5} color={p.starlight || "#fff"} />
        <ellipse cx="100" cy="158" rx="50" ry="6" fill={p.shadow} opacity="0.4" />
        <circle cx="100" cy="90" r="56" fill={p.body} />
        <circle cx="80" cy="74" r="5" fill={p.chip} />
        <circle cx="118" cy="68" r="6" fill={p.chip} />
        <circle cx="124" cy="106" r="5" fill={p.chip} />
        <circle cx="78" cy="108" r="6" fill={p.chip} />
        <circle cx="100" cy="92" r="4" fill={p.chip} />
      </svg>
    );
  },
};

// Rarity gradient backgrounds
const RARITY_BG = {
  common: { from: "#3B6EE5", to: "#2D4FB8" },     // blue
  rare:   { from: "#1FB3D6", to: "#3257B9" },     // cyan→blue
  epic:   { from: "#7B3FE5", to: "#C13FB8" },     // purple→magenta
  legend: { from: "#FFB347", to: "#FF6BAA" },     // sunset
};

function CardScene({ kind, palette, rarity }) {
  const Renderer = SCENES[kind] || SCENES.star;
  return <Renderer p={palette} />;
}

Object.assign(window, { SCENES, CardScene, RARITY_BG });
