// game-art.jsx — Procedural SVG game artwork engine for VantageBet.
// Each game carries a `theme` + `palette` [deep, mid, bright] + `glow`/`accent`.
// GameArt renders a distinctive, layered illustrated thumbnail that fills its box.
// viewBox is 4:5 (240×300); composition is vertically centred so square / landscape
// crops still read. No real brand imagery — all art is original geometry.

const _star = (x, y, r, fill, o = 1) => (
  <path key={`s${x}${y}`} d={`M${x} ${y - r} L${x + r * 0.28} ${y - r * 0.28} L${x + r} ${y} L${x + r * 0.28} ${y + r * 0.28} L${x} ${y + r} L${x - r * 0.28} ${y + r * 0.28} L${x - r} ${y} L${x - r * 0.28} ${y - r * 0.28} Z`} fill={fill} opacity={o} />
);
const _dot = (x, y, r, fill, o = 1) => <circle key={`d${x}${y}${r}`} cx={x} cy={y} r={r} fill={fill} opacity={o} />;

function themeMotif(theme, P, G, A, uid) {
  const [c0, c1, c2] = P;
  switch (theme) {

    case 'egyptian': return (
      <g>
        <circle cx="120" cy="118" r="52" fill={`url(#rg${uid})`} />
        <circle cx="120" cy="118" r="40" fill="none" stroke={G} strokeWidth="2" opacity="0.5" />
        {/* dunes */}
        <path d="M0 240 Q120 205 240 240 L240 300 L0 300 Z" fill={c0} opacity="0.85" />
        {/* pyramids */}
        <polygon points="30,242 96,150 162,242" fill={c1} />
        <polygon points="96,150 162,242 118,242" fill={c0} opacity="0.55" />
        <polygon points="132,244 190,172 240,244" fill={c1} opacity="0.85" />
        {/* Eye of Horus mark */}
        <g transform="translate(120 120)" stroke={A} strokeWidth="4" fill="none" strokeLinecap="round">
          <path d="M-26 0 Q0 -18 26 0 Q0 16 -26 0 Z" fill={G} fillOpacity="0.18" />
          <circle cx="0" cy="0" r="7" fill={A} stroke="none" />
          <path d="M-26 2 Q-30 14 -20 18" />
          <path d="M8 10 Q10 22 22 22" />
        </g>
        {_star(52, 70, 4, G, 0.9)}{_star(196, 92, 3, G, 0.8)}{_star(180, 54, 2.5, '#fff', 0.7)}{_star(70, 150, 2.5, G, 0.6)}
      </g>
    );

    case 'candy': return (
      <g>
        <circle cx="120" cy="130" r="60" fill={`url(#rg${uid})`} />
        {/* lollipop swirl */}
        <g transform="translate(120 120)">
          <circle r="34" fill={c1} />
          <path d="M0 0 m0 -30 a30 30 0 1 1 -0.1 0 M0 0 m0 -20 a20 20 0 1 0 0.1 0 M0 0 m0 -10 a10 10 0 1 1 -0.1 0" fill="none" stroke={G} strokeWidth="7" opacity="0.9" />
          <ellipse cx="-10" cy="-12" rx="9" ry="6" fill="#fff" opacity="0.4" />
        </g>
        {/* gumdrops / candies */}
        {[[44,88,15,A],[196,96,13,G],[54,200,17,G],[190,206,14,A],[112,236,12,c2]].map(([x,y,r,f],i)=>(
          <g key={i}><circle cx={x} cy={y} r={r} fill={f} /><ellipse cx={x-r*0.35} cy={y-r*0.4} rx={r*0.4} ry={r*0.28} fill="#fff" opacity="0.45" /></g>
        ))}
        {/* hearts */}
        <path d="M150 60 q6 -10 14 -2 q8 -8 14 2 q0 12 -14 22 q-14 -10 -14 -22 Z" fill={G} opacity="0.85" />
        {_dot(78,52,3,'#fff',0.8)}{_dot(168,150,3,'#fff',0.7)}{_dot(60,150,2.5,'#fff',0.6)}
      </g>
    );

    case 'western': return (
      <g>
        <circle cx="120" cy="122" r="58" fill={`url(#rg${uid})`} />
        <circle cx="120" cy="122" r="42" fill={G} opacity="0.9" />
        {/* horizon mesas */}
        <path d="M0 214 L0 178 L46 178 L46 158 L96 158 L96 200 L240 200 L240 214 Z" fill={c1} opacity="0.9" />
        <path d="M0 300 L0 210 Q120 190 240 210 L240 300 Z" fill={c0} />
        {/* cactus */}
        <g fill={c0} opacity="0.92">
          <rect x="176" y="150" width="14" height="66" rx="7" />
          <rect x="164" y="168" width="12" height="26" rx="6" /><rect x="164" y="168" width="30" height="12" rx="6" />
          <rect x="190" y="158" width="12" height="30" rx="6" /><rect x="184" y="158" width="24" height="12" rx="6" />
        </g>
        {/* sheriff star */}
        <g transform="translate(66 92)">{_star(0,0,17,A,1)}<circle r="6" fill={c0} /></g>
        {_star(40,60,3,'#fff',0.8)}{_star(198,70,2.5,'#fff',0.7)}
      </g>
    );

    case 'mythology': return (
      <g>
        <circle cx="120" cy="120" r="66" fill={`url(#rg${uid})`} />
        {/* clouds */}
        <ellipse cx="70" cy="86" rx="42" ry="18" fill={c2} opacity="0.35" />
        <ellipse cx="170" cy="78" rx="48" ry="20" fill={c2} opacity="0.28" />
        {/* lightning bolt */}
        <path d="M132 44 L92 138 L120 138 L100 214 L166 108 L134 108 L156 44 Z" fill={A} stroke={G} strokeWidth="2" strokeLinejoin="round" />
        <path d="M132 44 L92 138 L120 138 L100 214 L166 108 L134 108 L156 44 Z" fill="none" stroke="#fff" strokeWidth="1" opacity="0.5" />
        {/* columns */}
        {[26,58,182,214].map((x,i)=>(<g key={i}><rect x={x} y="200" width="26" height="70" fill={c1} opacity="0.9" /><rect x={x-3} y="196" width="32" height="8" fill={c2} opacity="0.9" /></g>))}
        {_star(50,60,3.5,G,0.9)}{_star(196,120,3,G,0.8)}{_star(212,58,2.5,'#fff',0.7)}
      </g>
    );

    case 'ocean': return (
      <g>
        <circle cx="120" cy="120" r="66" fill={`url(#rg${uid})`} />
        {/* light shafts */}
        <polygon points="60,0 100,0 150,300 90,300" fill={G} opacity="0.08" />
        <polygon points="150,0 180,0 210,300 165,300" fill={G} opacity="0.06" />
        {/* fish */}
        <g transform="translate(120 130)"><path d="M-40 0 Q-10 -26 34 -8 Q50 0 34 8 Q-10 26 -40 0 Z" fill={c2} /><path d="M34 -8 Q56 -20 60 0 Q56 20 34 8 Z" fill={c2} opacity="0.8" /><circle cx="-24" cy="-4" r="4.5" fill="#fff" /><circle cx="-24" cy="-4" r="2" fill={c0} /><path d="M-6 -6 Q10 -2 -2 8" fill="none" stroke={G} strokeWidth="2" opacity="0.6" /></g>
        {/* bubbles */}
        {[[70,70,7],[86,52,4],[186,96,6],[176,66,3.5],[54,168,5]].map(([x,y,r],i)=>(<circle key={i} cx={x} cy={y} r={r} fill="none" stroke={G} strokeWidth="1.6" opacity="0.6" />))}
        {/* seabed */}
        <path d="M0 300 L0 248 Q120 226 240 250 L240 300 Z" fill={c0} />
        {_star(150,74,3,A,0.9)}
      </g>
    );

    case 'cards': return (
      <g>
        <ellipse cx="120" cy="150" rx="120" ry="130" fill={`url(#rg${uid})`} />
        {/* two cards */}
        <g transform="translate(120 138) rotate(-12)"><rect x="-46" y="-58" width="82" height="116" rx="10" fill="#F4F5FA" /><text x="-36" y="-32" fontFamily="var(--font-display)" fontWeight="800" fontSize="26" fill={c2}>A</text></g>
        <g transform="translate(128 132) rotate(10)"><rect x="-40" y="-56" width="80" height="114" rx="10" fill="#FBFCFF" stroke={c1} strokeWidth="1" />
          <path d="M0 -30 C22 -30 22 2 0 16 C-22 2 -22 -30 0 -30 Z" fill={A} />
          <text x="-30" y="-28" fontFamily="var(--font-display)" fontWeight="800" fontSize="24" fill={A}>K</text>
        </g>
        {/* chips */}
        {[[54,224,'#fff'],[74,232,A],[94,224,c2]].map(([x,y,f],i)=>(<g key={i}><circle cx={x} cy={y} r="17" fill={f} stroke={c0} strokeWidth="3" strokeDasharray="5 5" /><circle cx={x} cy={y} r="9" fill="none" stroke={c0} strokeWidth="2" /></g>))}
        {_star(190,90,4,G,0.9)}{_star(206,140,3,G,0.7)}
      </g>
    );

    case 'fortune': return (
      <g>
        <circle cx="120" cy="120" r="70" fill={`url(#rg${uid})`} />
        {/* radiant burst */}
        <g transform="translate(120 116)" stroke={G} strokeWidth="3" opacity="0.5">{[0,45,90,135,180,225,270,315].map(a=>(<line key={a} x1="0" y1="0" x2={Math.cos(a*Math.PI/180)*84} y2={Math.sin(a*Math.PI/180)*84} />))}</g>
        {/* diamond */}
        <g transform="translate(120 104)"><polygon points="0,-30 26,-8 0,34 -26,-8" fill={A} /><polygon points="0,-30 26,-8 0,-8" fill="#fff" opacity="0.35" /><polygon points="-26,-8 0,34 0,-8" fill="#000" opacity="0.15" /></g>
        {/* coin pile */}
        <g>{[[80,232,20],[120,240,22],[160,232,20],[100,214,18],[140,214,18]].map(([x,y,r],i)=>(<g key={i}><ellipse cx={x} cy={y} rx={r} ry={r*0.6} fill={c2} stroke={G} strokeWidth="2" /><text x={x} y={y+5} textAnchor="middle" fontFamily="var(--font-display)" fontWeight="800" fontSize="15" fill={c0}>$</text></g>))}</g>
        {_star(58,80,4,G)}{_star(190,88,3.5,G,0.8)}{_star(200,150,3,'#fff',0.7)}
      </g>
    );

    case 'cyber': return (
      <g>
        <circle cx="120" cy="110" r="60" fill={`url(#rg${uid})`} />
        {/* neon sun */}
        <circle cx="120" cy="106" r="46" fill="none" stroke={G} strokeWidth="3" />
        <g stroke={c0} strokeWidth="5">{[92,104,116,128].map(y=>(<line key={y} x1="72" y1={y} x2="168" y2={y} />))}</g>
        {/* perspective grid */}
        <g stroke={A} strokeWidth="1.5" opacity="0.55">
          {[0,40,80,120,160,200,240].map(x=>(<line key={x} x1={x} y1="188" x2={(x-120)*3+120} y2="300" />))}
          {[[188,0.9],[212,0.7],[244,0.5],[286,0.35]].map(([y,o],i)=>(<line key={i} x1="0" y1={y} x2="240" y2={y} opacity={o} />))}
        </g>
        {/* floating gems */}
        {[[54,70,A],[190,64,G],[196,150,A]].map(([x,y,f],i)=>(<polygon key={i} points={`${x},${y-9} ${x+8},${y} ${x},${y+9} ${x-8},${y}`} fill={f} opacity="0.9" />))}
        {_star(70,150,3,'#fff',0.8)}
      </g>
    );

    case 'wheel': return (
      <g>
        <circle cx="120" cy="140" r="86" fill={`url(#rg${uid})`} />
        {/* wheel */}
        <g transform="translate(120 140)">
          <circle r="72" fill={c1} stroke={G} strokeWidth="3" />
          {Array.from({length:12}).map((_,i)=>{const a0=i*30*Math.PI/180,a1=(i+1)*30*Math.PI/180;const f=i%2?c0:c2;return <path key={i} d={`M0 0 L${Math.cos(a0)*72} ${Math.sin(a0)*72} A72 72 0 0 1 ${Math.cos(a1)*72} ${Math.sin(a1)*72} Z`} fill={f} opacity={i%3?0.9:1} />;})}
          <circle r="30" fill={c0} stroke={G} strokeWidth="2" />
          <circle r="10" fill={A} />
          <circle cx="0" cy="-58" r="6" fill="#fff" />
        </g>
        {/* pointer */}
        <polygon points="120,50 112,64 128,64" fill={A} />
        {_star(46,80,4,G,0.9)}{_star(198,84,3.5,G,0.8)}
      </g>
    );

    case 'dragon': return (
      <g>
        <circle cx="120" cy="120" r="70" fill={`url(#rg${uid})`} />
        {/* lantern */}
        <g transform="translate(184 78)"><ellipse rx="20" ry="24" fill={A} /><rect x="-20" y="-4" width="40" height="8" fill={c0} opacity="0.3" /><line x1="0" y1="-30" x2="0" y2="-24" stroke={G} strokeWidth="2" /><line x1="0" y1="24" x2="0" y2="34" stroke={G} strokeWidth="2" /></g>
        {/* dragon S-curve */}
        <path d="M28 232 Q52 150 108 158 Q152 164 138 118 Q128 84 168 82" fill="none" stroke={c2} strokeWidth="16" strokeLinecap="round" />
        <path d="M28 232 Q52 150 108 158 Q152 164 138 118 Q128 84 168 82" fill="none" stroke={G} strokeWidth="6" strokeLinecap="round" opacity="0.7" strokeDasharray="2 12" />
        <circle cx="168" cy="82" r="15" fill={c2} /><circle cx="173" cy="78" r="4" fill={G} />
        {/* coins */}
        {[[62,206,15],[92,220,16],[58,240,13]].map(([x,y,r],i)=>(<g key={i}><circle cx={x} cy={y} r={r} fill={A} stroke={G} strokeWidth="2" /><rect x={x-4} y={y-4} width="8" height="8" fill={c0} /></g>))}
        {_star(50,70,4,G)}{_star(120,60,3,'#fff',0.7)}
      </g>
    );

    case 'jungle': return (
      <g>
        <circle cx="120" cy="120" r="66" fill={`url(#rg${uid})`} />
        {/* light shafts */}
        <polygon points="80,0 120,0 150,300 100,300" fill={G} opacity="0.07" />
        {/* temple gem */}
        <g transform="translate(120 128)"><polygon points="0,-26 22,-6 0,30 -22,-6" fill={A} /><polygon points="0,-26 22,-6 0,-6" fill="#fff" opacity="0.3" /></g>
        {/* monstera leaves */}
        <g fill={c2} opacity="0.92">
          <path d="M6 300 Q-8 210 60 176 Q40 214 66 236 Q30 240 44 300 Z" />
          <path d="M234 300 Q248 200 176 172 Q200 214 172 238 Q210 244 194 300 Z" />
        </g>
        <path d="M40 40 Q90 30 96 78 Q60 68 40 40 Z" fill={c2} opacity="0.6" />
        {_star(180,72,4,G,0.9)}{_dot(70,110,3,'#fff',0.7)}
      </g>
    );

    default: return <circle cx="120" cy="150" r="70" fill={`url(#rg${uid})`} />;
  }
}

function GameArt({ game, style }) {
  const P = game.palette || ['#1A1D2E', '#22263A', '#5B8AF5'];
  const G = game.glow || P[2];
  const A = game.accent || P[2];
  const uid = 'g' + game.id;
  return (
    <svg viewBox="0 0 240 300" preserveAspectRatio="xMidYMid slice"
      style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', display: 'block', ...style }}>
      <defs>
        <linearGradient id={`bg${uid}`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor={P[1]} /><stop offset="0.55" stopColor={P[0]} /><stop offset="1" stopColor={P[0]} />
        </linearGradient>
        <radialGradient id={`rg${uid}`} cx="0.5" cy="0.5" r="0.5">
          <stop offset="0" stopColor={G} stopOpacity="0.9" /><stop offset="0.45" stopColor={G} stopOpacity="0.35" /><stop offset="1" stopColor={G} stopOpacity="0" />
        </radialGradient>
      </defs>
      <rect width="240" height="300" fill={`url(#bg${uid})`} />
      {themeMotif(game.theme, P, G, A, uid)}
      {/* top sheen + bottom vignette for consistent crop legibility */}
      <rect width="240" height="120" fill="url(#__sheen)" opacity="0.14" />
      <rect y="150" width="240" height="150" fill="#000" opacity="0.28" style={{ mixBlendMode: 'multiply' }} />
    </svg>
  );
}

// shared sheen gradient (defined once, referenced by all instances)
function GameArtDefs() {
  return (
    <svg width="0" height="0" style={{ position: 'absolute' }} aria-hidden="true">
      <defs>
        <linearGradient id="__sheen" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#fff" /><stop offset="1" stopColor="#fff" stopOpacity="0" />
        </linearGradient>
      </defs>
    </svg>
  );
}

Object.assign(window, { GameArt, GameArtDefs });
