/* global React, ART_PALETTE, SUIT_COLOR, Pip, MAJOR_GLYPHS, CardBack */

// Suit-specific court card glyphs (Pages, Knights, Queens, Kings)
const COURT_GLYPHS = {
  wands: {
    // Page of Wands: standing, wand raised, feathered cap
    11: (c) => <g>
      <circle cx="50" cy="34" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 46 26 Q 50 19 62 23" fill="none" stroke={c} strokeWidth="0.55" opacity="0.6"/>
      <path d="M 43 46 L 38 93 L 62 93 L 57 46 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="39" y1="67" x2="61" y2="67" stroke={c} strokeWidth="0.4" opacity="0.45"/>
      <line x1="43" y1="50" x2="36" y2="64" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <line x1="57" y1="50" x2="66" y2="40" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <WandLine x1={66} y1={22} x2={66} y2={56} c={c} sw={0.9} ls={3}/>
      <line x1="20" y1="94" x2="80" y2="94" stroke={c} strokeWidth="0.28" opacity="0.3"/>
    </g>,
    // Knight of Wands: charging, wand raised, leaning forward
    12: (c) => <g>
      <circle cx="52" cy="31" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <line x1="43" y1="35" x2="61" y2="35" stroke={c} strokeWidth="0.4" opacity="0.5"/>
      <path d="M 38 43 L 28 78 L 60 78 L 70 43 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="70" y1="50" x2="80" y2="40" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <WandLine x1={80} y1={22} x2={80} y2={54} c={c} sw={0.9} ls={3}/>
      <path d="M 28 78 Q 20 86 20 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <path d="M 44 78 Q 38 88 36 94" fill="none" stroke={c} strokeWidth="0.5" strokeLinecap="round" opacity="0.55"/>
      <path d="M 60 78 Q 66 86 64 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <line x1="18" y1="95" x2="82" y2="95" stroke={c} strokeWidth="0.28" opacity="0.3"/>
    </g>,
    // Queen of Wands: seated on throne, wand in right hand, sunflower at feet
    13: (c) => <g>
      <line x1="28" y1="22" x2="28" y2="92" stroke={c} strokeWidth="0.65"/>
      <line x1="72" y1="22" x2="72" y2="92" stroke={c} strokeWidth="0.65"/>
      <path d="M 41 24 L 44 18 L 50 25 L 56 18 L 59 24 L 57 31 L 43 31 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 38 52 Q 28 72 28 92 L 72 92 Q 72 72 62 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="38" y1="58" x2="28" y2="70" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <WandLine x1={72} y1={22} x2={72} y2={92} c={c} sw={0.9} ls={3.5}/>
      <circle cx="35" cy="88" r="4" fill="none" stroke={c} strokeWidth="0.5" opacity="0.5"/>
      <circle cx="35" cy="88" r="1.5" fill={c} opacity="0.35"/>
    </g>,
    // King of Wands: seated, wand displayed, commanding
    14: (c) => <g>
      <line x1="26" y1="20" x2="26" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="74" y1="20" x2="74" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="26" y1="20" x2="74" y2="20" stroke={c} strokeWidth="0.45" opacity="0.4"/>
      <path d="M 38 22 L 41 15 L 45 22 L 50 13 L 55 22 L 59 15 L 62 22 L 60 30 L 40 30 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 36 52 Q 26 72 26 94 L 74 94 Q 74 72 64 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="64" y1="60" x2="74" y2="50" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <WandLine x1={74} y1={20} x2={74} y2={94} c={c} sw={1.0} ls={3.5}/>
    </g>,
  },

  cups: {
    // Page of Cups: standing, cup raised, fish emerging
    11: (c) => <g>
      <circle cx="50" cy="34" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 41 27 Q 50 21 59 27" fill="none" stroke={c} strokeWidth="0.55" opacity="0.6"/>
      <path d="M 43 46 L 38 93 L 62 93 L 57 46 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="39" y1="67" x2="61" y2="67" stroke={c} strokeWidth="0.4" opacity="0.45"/>
      <line x1="43" y1="50" x2="36" y2="64" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <line x1="57" y1="50" x2="66" y2="44" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <CupShape cx={70} cy={52} w={13} h={17} c={c} lw={0.85}/>
      <path d="M 70 44 Q 75 39 80 42 Q 75 37 70 40" fill="none" stroke={c} strokeWidth="0.5" opacity="0.6"/>
      <line x1="20" y1="94" x2="80" y2="94" stroke={c} strokeWidth="0.28" opacity="0.3"/>
    </g>,
    // Knight of Cups: calm trot, cup held forward, winged helm
    12: (c) => <g>
      <circle cx="50" cy="31" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 41 29 Q 36 23 38 31" fill="none" stroke={c} strokeWidth="0.45" opacity="0.55"/>
      <path d="M 40 43 L 32 78 L 62 78 L 68 43 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="68" y1="52" x2="78" y2="52" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <CupShape cx={80} cy={58} w={13} h={17} c={c} lw={0.85}/>
      <path d="M 32 78 Q 24 86 22 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <path d="M 48 78 Q 44 88 42 94" fill="none" stroke={c} strokeWidth="0.5" strokeLinecap="round" opacity="0.55"/>
      <path d="M 62 78 Q 68 86 66 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <line x1="18" y1="95" x2="82" y2="95" stroke={c} strokeWidth="0.28" opacity="0.3"/>
    </g>,
    // Queen of Cups: seated, ornate cup cradled in lap, water below
    13: (c) => <g>
      <line x1="28" y1="22" x2="28" y2="92" stroke={c} strokeWidth="0.65"/>
      <line x1="72" y1="22" x2="72" y2="92" stroke={c} strokeWidth="0.65"/>
      <path d="M 41 24 L 44 18 L 50 25 L 56 18 L 59 24 L 57 31 L 43 31 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 38 52 Q 28 72 28 92 L 72 92 Q 72 72 62 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="38" y1="58" x2="44" y2="72" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <line x1="62" y1="58" x2="56" y2="72" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <CupShape cx={50} cy={82} w={15} h={19} c={c} lw={0.9}/>
      <path d="M 28 92 Q 40 88 50 92 Q 60 96 72 92" fill="none" stroke={c} strokeWidth="0.32" opacity="0.4"/>
    </g>,
    // King of Cups: seated, cup raised to side, fish symbol
    14: (c) => <g>
      <line x1="26" y1="20" x2="26" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="74" y1="20" x2="74" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="26" y1="20" x2="74" y2="20" stroke={c} strokeWidth="0.45" opacity="0.4"/>
      <path d="M 38 22 L 41 15 L 45 22 L 50 13 L 55 22 L 59 15 L 62 22 L 60 30 L 40 30 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 36 52 Q 26 72 26 94 L 74 94 Q 74 72 64 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="64" y1="60" x2="74" y2="60" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <CupShape cx={76} cy={68} w={13} h={17} c={c} lw={0.88}/>
      <path d="M 32 84 Q 36 80 40 84 Q 36 88 32 84 Z" fill="none" stroke={c} strokeWidth="0.4" opacity="0.5"/>
    </g>,
  },

  swords: {
    // Page of Swords: standing, sword raised, windswept, alert
    11: (c) => <g>
      <circle cx="50" cy="34" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 43 46 L 38 93 L 62 93 L 57 46 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="39" y1="67" x2="61" y2="67" stroke={c} strokeWidth="0.4" opacity="0.45"/>
      <line x1="43" y1="50" x2="34" y2="62" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <line x1="57" y1="50" x2="70" y2="38" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <SwordLine x1={70} y1={92} x2={70} y2={22} c={c} sw={0.9}/>
      <path d="M 18 52 Q 26 48 34 52" fill="none" stroke={c} strokeWidth="0.32" opacity="0.38"/>
      <path d="M 20 60 Q 30 56 38 60" fill="none" stroke={c} strokeWidth="0.28" opacity="0.3"/>
      <line x1="20" y1="94" x2="80" y2="94" stroke={c} strokeWidth="0.28" opacity="0.3"/>
    </g>,
    // Knight of Swords: charging hard, sword thrust, speed lines
    12: (c) => <g>
      <circle cx="52" cy="30" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <line x1="43" y1="34" x2="61" y2="34" stroke={c} strokeWidth="0.4" opacity="0.5"/>
      <path d="M 36 42 L 22 76 L 56 76 L 72 42 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="72" y1="48" x2="84" y2="38" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <SwordLine x1={84} y1={72} x2={84} y2={22} c={c} sw={0.9}/>
      <path d="M 22 76 Q 14 84 12 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <path d="M 40 76 Q 36 86 34 94" fill="none" stroke={c} strokeWidth="0.5" strokeLinecap="round" opacity="0.55"/>
      <path d="M 56 76 Q 62 86 60 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <line x1="16" y1="50" x2="26" y2="50" stroke={c} strokeWidth="0.3" opacity="0.38"/>
      <line x1="14" y1="56" x2="24" y2="56" stroke={c} strokeWidth="0.28" opacity="0.32"/>
      <line x1="18" y1="95" x2="82" y2="95" stroke={c} strokeWidth="0.28" opacity="0.3"/>
    </g>,
    // Queen of Swords: seated, sword upright, left hand raised
    13: (c) => <g>
      <line x1="28" y1="22" x2="28" y2="92" stroke={c} strokeWidth="0.65"/>
      <line x1="72" y1="22" x2="72" y2="92" stroke={c} strokeWidth="0.65"/>
      <path d="M 41 24 L 44 18 L 50 25 L 56 18 L 59 24 L 57 31 L 43 31 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 38 52 Q 28 72 28 92 L 72 92 Q 72 72 62 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="62" y1="56" x2="72" y2="46" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <SwordLine x1={72} y1={92} x2={72} y2={22} c={c} sw={0.9}/>
      <line x1="38" y1="56" x2="30" y2="48" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <path d="M 18 84 Q 24 80 30 84" fill="none" stroke={c} strokeWidth="0.28" opacity="0.35"/>
    </g>,
    // King of Swords: seated, sword raised high, authoritative
    14: (c) => <g>
      <line x1="26" y1="20" x2="26" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="74" y1="20" x2="74" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="26" y1="20" x2="74" y2="20" stroke={c} strokeWidth="0.45" opacity="0.4"/>
      <path d="M 38 22 L 41 15 L 45 22 L 50 13 L 55 22 L 59 15 L 62 22 L 60 30 L 40 30 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 36 52 Q 26 72 26 94 L 74 94 Q 74 72 64 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="64" y1="56" x2="74" y2="44" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <SwordLine x1={74} y1={94} x2={74} y2={20} c={c} sw={1.0}/>
      <path d="M 28 68 Q 24 64 26 60 Q 30 64 28 68" fill="none" stroke={c} strokeWidth="0.38" opacity="0.4"/>
    </g>,
  },

  pentacles: {
    // Page of Pentacles: standing, pentacle held up in both hands, studying
    11: (c) => <g>
      <circle cx="50" cy="46" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 43 58 L 38 93 L 62 93 L 57 58 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="39" y1="76" x2="61" y2="76" stroke={c} strokeWidth="0.4" opacity="0.45"/>
      <line x1="43" y1="62" x2="38" y2="44" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <line x1="57" y1="62" x2="62" y2="44" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <PentacleDisc cx={50} cy={30} r={10} c={c} lw={0.82}/>
      <line x1="20" y1="94" x2="80" y2="94" stroke={c} strokeWidth="0.28" opacity="0.3"/>
      <circle cx="26" cy="91" r="1.2" fill={c} opacity="0.35"/>
      <circle cx="74" cy="91" r="1.2" fill={c} opacity="0.35"/>
    </g>,
    // Knight of Pentacles: steady, pentacle held carefully, plodding
    12: (c) => <g>
      <circle cx="50" cy="31" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <line x1="41" y1="35" x2="59" y2="35" stroke={c} strokeWidth="0.4" opacity="0.5"/>
      <path d="M 40 43 L 34 80 L 62 80 L 66 43 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="40" y1="54" x2="30" y2="62" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <line x1="66" y1="52" x2="76" y2="56" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <PentacleDisc cx={78} cy={60} r={9} c={c} lw={0.8}/>
      <path d="M 34 80 Q 26 88 24 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <path d="M 50 80 Q 48 88 46 94" fill="none" stroke={c} strokeWidth="0.5" strokeLinecap="round" opacity="0.55"/>
      <path d="M 62 80 Q 68 88 66 94" fill="none" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
      <line x1="18" y1="95" x2="82" y2="95" stroke={c} strokeWidth="0.28" opacity="0.3"/>
    </g>,
    // Queen of Pentacles: seated, pentacle cradled in lap, plant at side
    13: (c) => <g>
      <line x1="28" y1="22" x2="28" y2="92" stroke={c} strokeWidth="0.65"/>
      <line x1="72" y1="22" x2="72" y2="92" stroke={c} strokeWidth="0.65"/>
      <path d="M 41 24 L 44 18 L 50 25 L 56 18 L 59 24 L 57 31 L 43 31 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 38 52 Q 28 72 28 92 L 72 92 Q 72 72 62 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="38" y1="58" x2="44" y2="72" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <line x1="62" y1="58" x2="56" y2="72" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <PentacleDisc cx={50} cy={80} r={10} c={c} lw={0.82}/>
      <path d="M 28 82 Q 24 76 28 70" fill="none" stroke={c} strokeWidth="0.4" opacity="0.5"/>
      <circle cx="27" cy="68" r="2.5" fill="none" stroke={c} strokeWidth="0.4" opacity="0.45"/>
    </g>,
    // King of Pentacles: seated, pentacle on knee, vine at throne
    14: (c) => <g>
      <line x1="26" y1="20" x2="26" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="74" y1="20" x2="74" y2="94" stroke={c} strokeWidth="0.7"/>
      <line x1="26" y1="20" x2="74" y2="20" stroke={c} strokeWidth="0.45" opacity="0.4"/>
      <path d="M 38 22 L 41 15 L 45 22 L 50 13 L 55 22 L 59 15 L 62 22 L 60 30 L 40 30 Z" fill="none" stroke={c} strokeWidth="0.55"/>
      <circle cx="50" cy="41" r="9" fill="none" stroke={c} strokeWidth="0.7"/>
      <path d="M 36 52 Q 26 72 26 94 L 74 94 Q 74 72 64 52 Z" fill="none" stroke={c} strokeWidth="0.65"/>
      <line x1="64" y1="62" x2="74" y2="70" stroke={c} strokeWidth="0.55" strokeLinecap="round"/>
      <PentacleDisc cx={74} cy={76} r={10} c={c} lw={0.82}/>
      <path d="M 26 76 Q 22 70 26 64" fill="none" stroke={c} strokeWidth="0.4" opacity="0.45"/>
      <circle cx="25" cy="62" r="2.5" fill="none" stroke={c} strokeWidth="0.4" opacity="0.4"/>
    </g>,
  },
};

// Pip arrangement for numbered cards (1–10)
const PipLayout = ({n, suit, color}) => {
  // Geometric, symmetric arrangements
  const layouts = {
    1: [[50,60]],
    2: [[50,40],[50,80]],
    3: [[50,35],[50,60],[50,85]],
    4: [[35,40],[65,40],[35,80],[65,80]],
    5: [[35,40],[65,40],[50,60],[35,80],[65,80]],
    6: [[35,35],[65,35],[35,60],[65,60],[35,85],[65,85]],
    7: [[35,32],[65,32],[35,55],[65,55],[50,68],[35,82],[65,82]],
    8: [[35,30],[65,30],[35,48],[65,48],[35,72],[65,72],[35,90],[65,90]],
    9: [[35,30],[50,30],[65,30],[35,55],[50,55],[65,55],[35,80],[50,80],[65,80]],
    10:[[35,28],[65,28],[35,42],[65,42],[35,58],[50,58],[65,72],[35,72],[35,88],[65,88]]
  };
  const points = layouts[n] || [];
  return <g>
    {points.map(([x,y],i) => <Pip key={i} type={suit} x={x} y={y} size={n>=7?7:9} color={color}/>)}
  </g>;
};

// Helper: draws a chalice centered at (cx, cy) where cy is the base
const CupShape = ({cx, cy, w=11, h=14, c, lw=0.8}) => {
  const hw=w*0.48, nw=w*0.16, bw=w*0.38;
  const top=cy-h*0.6, bowlBot=cy-h*0.25;
  return <g>
    <path d={`M ${cx-hw} ${top} L ${cx-nw} ${bowlBot} L ${cx+nw} ${bowlBot} L ${cx+hw} ${top}`}
      fill="none" stroke={c} strokeWidth={lw} strokeLinejoin="round"/>
    <line x1={cx} y1={bowlBot} x2={cx} y2={cy} stroke={c} strokeWidth={lw*0.75}/>
    <line x1={cx-bw} y1={cy} x2={cx+bw} y2={cy} stroke={c} strokeWidth={lw} strokeLinecap="round"/>
  </g>;
};

// Helper: draws one sword from tip (x1,y1) to pommel (x2,y2) with a perpendicular crossguard
const SwordLine = ({x1,y1,x2,y2,c,sw=1.0,gs=4.5,op=1}) => {
  const dx=x2-x1, dy=y2-y1;
  const len=Math.sqrt(dx*dx+dy*dy);
  const gx=x1+dx*0.73, gy=y1+dy*0.73;
  const px=-dy/len*gs, py=dx/len*gs;
  return <g opacity={op}>
    <line x1={x1} y1={y1} x2={x2} y2={y2} stroke={c} strokeWidth={sw} strokeLinecap="round"/>
    <line x1={gx-px} y1={gy-py} x2={gx+px} y2={gy+py} stroke={c} strokeWidth={sw*0.72} strokeLinecap="round"/>
  </g>;
};

// Swords-specific glyphs — evocative compositions instead of pip arrangements
const SWORDS_GLYPHS = {
  // Ace: single prominent sword, cloud at tip
  1: (c) => <g>
    <line x1="50" y1="22" x2="50" y2="80" stroke={c} strokeWidth="1.5" strokeLinecap="round"/>
    <line x1="37" y1="80" x2="63" y2="80" stroke={c} strokeWidth="1.1" strokeLinecap="round"/>
    <line x1="50" y1="80" x2="50" y2="93" stroke={c} strokeWidth="1.1" strokeLinecap="round"/>
    <circle cx="50" cy="96" r="3" fill="none" stroke={c} strokeWidth="0.9"/>
    <path d="M 38 27 Q 42 22 46 26 Q 48 20 50 24 Q 52 20 54 26 Q 58 22 62 27" fill="none" stroke={c} strokeWidth="0.5" opacity="0.6"/>
  </g>,

  // Two: crossed swords, crescent moon — deliberate blindness
  2: (c) => <g>
    <SwordLine x1={65} y1={25} x2={35} y2={95} c={c} sw={1.2}/>
    <SwordLine x1={35} y1={25} x2={65} y2={95} c={c} sw={1.2}/>
    <path d="M 44 33 A 7 7 0 0 1 56 33 A 5 5 0 0 0 44 33 Z" fill={c} opacity="0.18"/>
    <path d="M 44 33 A 7 7 0 0 1 56 33 A 5 5 0 0 0 44 33 Z" fill="none" stroke={c} strokeWidth="0.5" opacity="0.55"/>
  </g>,

  // Three: three swords converging at a heart, rain
  3: (c) => <g>
    <path d="M 50 76 C 35 63, 30 51, 38 44 C 44 39, 50 43, 50 43 C 50 43, 56 39, 62 44 C 70 51, 65 63, 50 76 Z"
      fill="none" stroke={c} strokeWidth="0.6" opacity="0.4"/>
    <SwordLine x1={50} y1={88} x2={50} y2={22} c={c} sw={1.2}/>
    <SwordLine x1={58} y1={86} x2={30} y2={25} c={c} sw={1.1}/>
    <SwordLine x1={42} y1={86} x2={70} y2={25} c={c} sw={1.1}/>
    <line x1="24" y1="92" x2="22" y2="100" stroke={c} strokeWidth="0.4" opacity="0.35"/>
    <line x1="34" y1="90" x2="32" y2="98" stroke={c} strokeWidth="0.4" opacity="0.35"/>
    <line x1="66" y1="90" x2="64" y2="98" stroke={c} strokeWidth="0.4" opacity="0.35"/>
    <line x1="76" y1="92" x2="74" y2="100" stroke={c} strokeWidth="0.4" opacity="0.35"/>
  </g>,

  // Four: three swords on wall, one lying horizontal — rest
  4: (c) => <g>
    <SwordLine x1={35} y1={60} x2={35} y2={22} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={50} y1={60} x2={50} y2={20} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={65} y1={60} x2={65} y2={22} c={c} sw={0.9} gs={3.5}/>
    <line x1="25" y1="60" x2="75" y2="60" stroke={c} strokeWidth="0.4" opacity="0.45"/>
    <SwordLine x1={22} y1={80} x2={78} y2={80} c={c} sw={1.3} gs={4}/>
  </g>,

  // Five: two upright swords (winner) vs three scattered (defeated)
  5: (c) => <g>
    <SwordLine x1={27} y1={94} x2={27} y2={25} c={c} sw={1.1}/>
    <SwordLine x1={38} y1={94} x2={38} y2={23} c={c} sw={1.1}/>
    <SwordLine x1={82} y1={88} x2={55} y2={46} c={c} sw={0.9} op={0.7}/>
    <SwordLine x1={85} y1={72} x2={64} y2={36} c={c} sw={0.9} op={0.65}/>
    <SwordLine x1={76} y1={95} x2={50} y2={56} c={c} sw={0.9} op={0.6}/>
    <line x1="18" y1="97" x2="88" y2="97" stroke={c} strokeWidth="0.3" opacity="0.4"/>
  </g>,

  // Six: boat with six standing swords, calm water
  6: (c) => <g>
    <path d="M 22 86 Q 50 94 78 86" fill="none" stroke={c} strokeWidth="0.9" strokeLinecap="round"/>
    <line x1="22" y1="86" x2="22" y2="70" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
    <line x1="78" y1="86" x2="78" y2="70" stroke={c} strokeWidth="0.6" strokeLinecap="round"/>
    <line x1="22" y1="70" x2="78" y2="70" stroke={c} strokeWidth="0.4" opacity="0.4"/>
    <SwordLine x1={31} y1={70} x2={31} y2={36} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={40} y1={70} x2={40} y2={33} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={48} y1={70} x2={48} y2={31} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={57} y1={70} x2={57} y2={31} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={65} y1={70} x2={65} y2={33} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={73} y1={70} x2={73} y2={36} c={c} sw={0.9} gs={3.5}/>
    <line x1="15" y1="99" x2="85" y2="99" stroke={c} strokeWidth="0.3" opacity="0.4"/>
    <path d="M 15 103 Q 30 100 46 103 Q 62 106 77 103" fill="none" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Seven: five swords moving (right, angled), two left standing behind
  7: (c) => <g>
    <SwordLine x1={57} y1={90} x2={44} y2={25} c={c} sw={1.0}/>
    <SwordLine x1={62} y1={87} x2={51} y2={23} c={c} sw={1.0}/>
    <SwordLine x1={67} y1={85} x2={57} y2={22} c={c} sw={1.0}/>
    <SwordLine x1={72} y1={85} x2={63} y2={23} c={c} sw={1.0}/>
    <SwordLine x1={78} y1={86} x2={69} y2={25} c={c} sw={1.0}/>
    <SwordLine x1={22} y1={92} x2={22} y2={27} c={c} sw={0.85} op={0.45}/>
    <SwordLine x1={32} y1={92} x2={32} y2={25} c={c} sw={0.85} op={0.45}/>
    <line x1="18" y1="93" x2="82" y2="93" stroke={c} strokeWidth="0.3" opacity="0.38"/>
  </g>,

  // Eight: swords forming a cage, dashed binding in center
  8: (c) => <g>
    <SwordLine x1={22} y1={96} x2={22} y2={24} c={c} sw={0.9}/>
    <SwordLine x1={30} y1={96} x2={30} y2={22} c={c} sw={0.9}/>
    <SwordLine x1={70} y1={96} x2={70} y2={22} c={c} sw={0.9}/>
    <SwordLine x1={78} y1={96} x2={78} y2={24} c={c} sw={0.9}/>
    <SwordLine x1={34} y1={26} x2={66} y2={26} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={34} y1={33} x2={66} y2={33} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={34} y1={89} x2={66} y2={89} c={c} sw={0.9} gs={3.5}/>
    <SwordLine x1={34} y1={96} x2={66} y2={96} c={c} sw={0.9} gs={3.5}/>
    <line x1="50" y1="43" x2="50" y2="79" stroke={c} strokeWidth="0.35" strokeDasharray="2 3" opacity="0.45"/>
  </g>,

  // Nine: nine horizontal swords racked on a wall
  9: (c) => <g>
    {[24,33,42,51,60,69,78,87,96].map((y,i) =>
      <SwordLine key={i} x1={78} y1={y} x2={22} y2={y} c={c} sw={1.0} gs={3.5}/>
    )}
  </g>,

  // Ten: ten swords stabbing downward — tip at bottom, guard near top
  10: (c) => <g>
    {[
      [42,97,20,28],[44,97,27,24],[46,97,34,21],[48,97,41,19],[50,97,48,18],
      [52,97,54,18],[54,97,62,19],[57,97,69,21],[60,97,75,24],[63,97,82,28]
    ].map(([x1,y1,x2,y2],i) =>
      <SwordLine key={i} x1={x1} y1={y1} x2={x2} y2={y2} c={c} sw={0.9} gs={4} op={i<2||i>7?0.75:1}/>
    )}
    <line x1="20" y1="100" x2="80" y2="100" stroke={c} strokeWidth="0.3" opacity="0.4"/>
  </g>,
};

// Cups-specific glyphs
const CUPS_GLYPHS = {
  // Ace: single large overflowing chalice, water streams, lotus above
  1: (c) => <g>
    <CupShape cx={50} cy={82} w={28} h={36} c={c} lw={1.2}/>
    <line x1="38" y1="61" x2="35" y2="69" stroke={c} strokeWidth="0.5" opacity="0.5"/>
    <line x1="50" y1="60" x2="50" y2="68" stroke={c} strokeWidth="0.5" opacity="0.5"/>
    <line x1="62" y1="61" x2="65" y2="69" stroke={c} strokeWidth="0.5" opacity="0.5"/>
    <path d="M 44 44 Q 50 37 56 44 L 50 41 Z" fill="none" stroke={c} strokeWidth="0.65" opacity="0.55"/>
    <path d="M 30 90 Q 40 87 50 90 Q 60 93 70 90" fill="none" stroke={c} strokeWidth="0.35" opacity="0.4"/>
    <path d="M 27 97 Q 40 94 50 97 Q 60 100 73 97" fill="none" stroke={c} strokeWidth="0.28" opacity="0.3"/>
  </g>,

  // Two: two cups facing each other, arc above
  2: (c) => <g>
    <CupShape cx={32} cy={82} w={20} h={26} c={c} lw={0.95}/>
    <CupShape cx={68} cy={82} w={20} h={26} c={c} lw={0.95}/>
    <path d="M 32 66 Q 50 50 68 66" fill="none" stroke={c} strokeWidth="0.7" opacity="0.65"/>
    <path d="M 36 70 Q 50 56 64 70" fill="none" stroke={c} strokeWidth="0.35" opacity="0.35"/>
    <circle cx="50" cy="40" r="4" fill="none" stroke={c} strokeWidth="0.6" opacity="0.5"/>
    <line x1="22" y1="90" x2="78" y2="90" stroke={c} strokeWidth="0.28" opacity="0.3"/>
  </g>,

  // Three: three cups at different heights, celebration dots
  3: (c) => <g>
    <CupShape cx={27} cy={78} w={18} h={23} c={c} lw={0.9}/>
    <CupShape cx={50} cy={67} w={18} h={23} c={c} lw={0.9}/>
    <CupShape cx={73} cy={78} w={18} h={23} c={c} lw={0.9}/>
    <circle cx="37" cy="50" r="1.2" fill={c} opacity="0.5"/>
    <circle cx="50" cy="43" r="1.3" fill={c} opacity="0.55"/>
    <circle cx="63" cy="50" r="1.2" fill={c} opacity="0.5"/>
    <circle cx="43" cy="57" r="0.8" fill={c} opacity="0.35"/>
    <circle cx="57" cy="57" r="0.8" fill={c} opacity="0.35"/>
    <line x1="18" y1="89" x2="82" y2="89" stroke={c} strokeWidth="0.28" opacity="0.28"/>
  </g>,

  // Four: three cups on ground, one offered from cloud above
  4: (c) => <g>
    <CupShape cx={28} cy={90} w={15} h={20} c={c} lw={0.85}/>
    <CupShape cx={50} cy={90} w={15} h={20} c={c} lw={0.85}/>
    <CupShape cx={72} cy={90} w={15} h={20} c={c} lw={0.85}/>
    <line x1="18" y1="92" x2="82" y2="92" stroke={c} strokeWidth="0.28" opacity="0.28"/>
    <CupShape cx={75} cy={42} w={13} h={17} c={c} lw={0.8}/>
    <path d="M 62 37 Q 69 29 75 32 Q 82 28 88 34 Q 90 41 85 45 Q 79 48 75 46 Q 68 49 62 43 Q 58 39 62 37 Z"
      fill="none" stroke={c} strokeWidth="0.45" opacity="0.28"/>
  </g>,

  // Five: three spilled cups (left), two upright cups (right)
  5: (c) => <g>
    <g transform="rotate(-90 26 88)"><CupShape cx={26} cy={88} w={12} h={16} c={c} lw={0.85}/></g>
    <g transform="rotate(-90 42 90)"><CupShape cx={42} cy={90} w={12} h={16} c={c} lw={0.85}/></g>
    <g transform="rotate(90 58 88)"><CupShape cx={58} cy={88} w={12} h={16} c={c} lw={0.85}/></g>
    <path d="M 18 94 Q 36 91 56 94" fill="none" stroke={c} strokeWidth="0.38" opacity="0.4"/>
    <CupShape cx={70} cy={75} w={13} h={17} c={c} lw={0.8}/>
    <CupShape cx={83} cy={75} w={13} h={17} c={c} lw={0.8}/>
    <line x1="18" y1="97" x2="82" y2="97" stroke={c} strokeWidth="0.25" opacity="0.28"/>
  </g>,

  // Six: six cups in two rows, small star in each bowl
  6: (c) => <g>
    {[[28,40],[50,40],[72,40],[28,75],[50,75],[72,75]].map(([cx,cy],i) => <g key={i}>
      <CupShape cx={cx} cy={cy} w={15} h={20} c={c} lw={0.8}/>
      <circle cx={cx} cy={cy-20*0.6+4} r="1.4" fill={c} opacity="0.35"/>
    </g>)}
    <line x1="18" y1="85" x2="82" y2="85" stroke={c} strokeWidth="0.25" opacity="0.25"/>
  </g>,

  // Seven: seven cups drifting in cloud-like scatter
  7: (c) => <g>
    {[
      [50, 30, 13, 17],
      [26, 50, 11, 14],
      [74, 50, 11, 14],
      [38, 68, 11, 14],
      [62, 68, 11, 14],
      [24, 86, 10, 13],
      [76, 86, 10, 13],
    ].map(([cx,cy,w,h],i) => <CupShape key={i} cx={cx} cy={cy} w={w} h={h} c={c} lw={0.75}/>)}
    <path d="M 22 45 Q 50 28 78 45" fill="none" stroke={c} strokeWidth="0.28" opacity="0.2"/>
    <path d="M 20 65 Q 50 48 80 65" fill="none" stroke={c} strokeWidth="0.22" opacity="0.18"/>
  </g>,

  // Eight: eight cups arranged in arc, crescent moon (departure)
  8: (c) => <g>
    {[
      [22,93],[34,89],[46,86],[58,84],[70,86],
      [26,70],[40,67],[54,64],
    ].map(([cx,cy],i) => <CupShape key={i} cx={cx} cy={cy} w={13} h={17} c={c} lw={0.8}/>)}
    <path d="M 77 28 Q 89 38 89 51 Q 89 63 77 69 Q 86 61 84 51 Q 82 39 77 28 Z"
      fill="none" stroke={c} strokeWidth="0.65" opacity="0.55"/>
    <line x1="18" y1="98" x2="82" y2="98" stroke={c} strokeWidth="0.28" opacity="0.28"/>
  </g>,

  // Nine: nine cups displayed in two rows (5+4)
  9: (c) => <g>
    {[22,34,46,58,70].map((cx,i) => <CupShape key={`a${i}`} cx={cx} cy={90} w={12} h={16} c={c} lw={0.8}/>)}
    {[28,42,56,70].map((cx,i) => <CupShape key={`b${i}`} cx={cx} cy={66} w={12} h={16} c={c} lw={0.8}/>)}
    <line x1="16" y1="94" x2="78" y2="94" stroke={c} strokeWidth="0.35" opacity="0.35"/>
    <line x1="18" y1="70" x2="80" y2="70" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Ten: ten cups in rainbow arc
  10: (c) => <g>
    {Array.from({length:10}, (_,i) => {
      const cx = 14 + i*7.8;
      const cy = 55 + (i-4.5)*(i-4.5)*0.9;
      return <CupShape key={i} cx={cx} cy={cy} w={7} h={9} c={c} lw={0.65}/>;
    })}
    <path d="M 16 74 Q 50 52 84 74" fill="none" stroke={c} strokeWidth="0.3" opacity="0.28"/>
    <path d="M 30 90 Q 50 83 70 90" fill="none" stroke={c} strokeWidth="0.5" opacity="0.4"/>
    <line x1="18" y1="100" x2="82" y2="100" stroke={c} strokeWidth="0.25" opacity="0.25"/>
  </g>,
};

// Helper: draws a wand (staff) with alternating leaf-sprouts along the shaft
const WandLine = ({x1,y1,x2,y2,c,sw=1.0,ls=3.5,op=1}) => {
  const dx=x2-x1, dy=y2-y1;
  const len=Math.sqrt(dx*dx+dy*dy);
  const px=-dy/len, py=dx/len;
  return <g opacity={op}>
    <line x1={x1} y1={y1} x2={x2} y2={y2} stroke={c} strokeWidth={sw} strokeLinecap="round"/>
    {[0.28,0.50,0.70].map((t,i) => {
      const lx=x1+dx*t, ly=y1+dy*t, s=i%2===0?1:-1;
      return <line key={i} x1={lx} y1={ly} x2={lx+px*ls*s} y2={ly+py*ls*s}
        stroke={c} strokeWidth={sw*0.65} strokeLinecap="round"/>;
    })}
  </g>;
};

// Wands-specific glyphs
const WANDS_GLYPHS = {
  // Ace: single large wand, bold sprouts, energy at tip
  1: (c) => <g>
    <WandLine x1={50} y1={22} x2={50} y2={92} c={c} sw={1.4} ls={6}/>
    <line x1="50" y1="92" x2="43" y2="85" stroke={c} strokeWidth="0.7" strokeLinecap="round"/>
    <line x1="50" y1="92" x2="57" y2="85" stroke={c} strokeWidth="0.7" strokeLinecap="round"/>
    <circle cx="50" cy="22" r="3" fill="none" stroke={c} strokeWidth="0.6" opacity="0.5"/>
  </g>,

  // Two: two wands planted, globe surveyed between them
  2: (c) => <g>
    <WandLine x1={28} y1={22} x2={28} y2={92} c={c} sw={1.1} ls={4}/>
    <WandLine x1={72} y1={22} x2={72} y2={92} c={c} sw={1.1} ls={4}/>
    <circle cx="50" cy="57" r="11" fill="none" stroke={c} strokeWidth="0.75"/>
    <line x1="39" y1="57" x2="61" y2="57" stroke={c} strokeWidth="0.4" opacity="0.4"/>
    <path d="M 50 46 Q 54 57 50 68" fill="none" stroke={c} strokeWidth="0.4" opacity="0.4"/>
    <line x1="18" y1="93" x2="82" y2="93" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Three: three wands, horizon, ships in distance
  3: (c) => <g>
    <WandLine x1={27} y1={22} x2={27} y2={92} c={c} sw={1.0} ls={4}/>
    <WandLine x1={50} y1={20} x2={50} y2={92} c={c} sw={1.0} ls={4}/>
    <WandLine x1={73} y1={22} x2={73} y2={92} c={c} sw={1.0} ls={4}/>
    <line x1="18" y1="72" x2="82" y2="72" stroke={c} strokeWidth="0.35" opacity="0.4"/>
    <rect x="30" y="69" width="10" height="3" fill="none" stroke={c} strokeWidth="0.35" opacity="0.4" rx="0.5"/>
    <line x1="35" y1="66" x2="35" y2="69" stroke={c} strokeWidth="0.35" opacity="0.4"/>
    <rect x="58" y="68" width="10" height="3" fill="none" stroke={c} strokeWidth="0.35" opacity="0.4" rx="0.5"/>
    <line x1="63" y1="65" x2="63" y2="68" stroke={c} strokeWidth="0.35" opacity="0.4"/>
    <line x1="18" y1="93" x2="82" y2="93" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Four: four wands, garland swags between them
  4: (c) => <g>
    <WandLine x1={22} y1={30} x2={22} y2={92} c={c} sw={1.0} ls={4}/>
    <WandLine x1={40} y1={26} x2={40} y2={92} c={c} sw={1.0} ls={4}/>
    <WandLine x1={60} y1={26} x2={60} y2={92} c={c} sw={1.0} ls={4}/>
    <WandLine x1={78} y1={30} x2={78} y2={92} c={c} sw={1.0} ls={4}/>
    <path d="M 22 36 Q 31 47 40 36" fill="none" stroke={c} strokeWidth="0.7" opacity="0.6"/>
    <path d="M 40 36 Q 50 47 60 36" fill="none" stroke={c} strokeWidth="0.7" opacity="0.6"/>
    <path d="M 60 36 Q 69 47 78 36" fill="none" stroke={c} strokeWidth="0.7" opacity="0.6"/>
    <circle cx="31" cy="44" r="1.2" fill={c} opacity="0.4"/>
    <circle cx="50" cy="44" r="1.2" fill={c} opacity="0.4"/>
    <circle cx="69" cy="44" r="1.2" fill={c} opacity="0.4"/>
    <line x1="18" y1="93" x2="82" y2="93" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Five: five wands clashing at competing angles
  5: (c) => <g>
    <WandLine x1={30} y1={25} x2={60} y2={90} c={c} sw={1.0} ls={3.5}/>
    <WandLine x1={70} y1={25} x2={40} y2={90} c={c} sw={1.0} ls={3.5}/>
    <WandLine x1={50} y1={22} x2={50} y2={90} c={c} sw={1.0} ls={3.5}/>
    <WandLine x1={22} y1={38} x2={78} y2={62} c={c} sw={0.9} ls={3}/>
    <WandLine x1={78} y1={38} x2={22} y2={62} c={c} sw={0.9} ls={3}/>
    <line x1="18" y1="92" x2="82" y2="92" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Six: five dim wands, one victorious center wand with laurel wreath
  6: (c) => <g>
    <WandLine x1={22} y1={26} x2={22} y2={90} c={c} sw={0.85} ls={3.5} op={0.45}/>
    <WandLine x1={34} y1={24} x2={34} y2={90} c={c} sw={0.85} ls={3.5} op={0.45}/>
    <WandLine x1={58} y1={24} x2={58} y2={90} c={c} sw={0.85} ls={3.5} op={0.45}/>
    <WandLine x1={70} y1={24} x2={70} y2={90} c={c} sw={0.85} ls={3.5} op={0.45}/>
    <WandLine x1={80} y1={26} x2={80} y2={90} c={c} sw={0.85} ls={3.5} op={0.45}/>
    <WandLine x1={46} y1={22} x2={46} y2={90} c={c} sw={1.2} ls={4.5}/>
    <circle cx="46" cy="24" r="5" fill="none" stroke={c} strokeWidth="0.7" opacity="0.7"/>
    <line x1="18" y1="92" x2="82" y2="92" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Seven: six wands rising from below, one defender's wand angled against them
  7: (c) => <g>
    <WandLine x1={20} y1={52} x2={32} y2={92} c={c} sw={0.9} ls={3.5} op={0.55}/>
    <WandLine x1={32} y1={48} x2={42} y2={92} c={c} sw={0.9} ls={3.5} op={0.55}/>
    <WandLine x1={44} y1={46} x2={52} y2={92} c={c} sw={0.9} ls={3.5} op={0.55}/>
    <WandLine x1={56} y1={46} x2={62} y2={92} c={c} sw={0.9} ls={3.5} op={0.55}/>
    <WandLine x1={68} y1={48} x2={74} y2={92} c={c} sw={0.9} ls={3.5} op={0.55}/>
    <WandLine x1={80} y1={52} x2={86} y2={92} c={c} sw={0.9} ls={3.5} op={0.55}/>
    <WandLine x1={50} y1={22} x2={38} y2={72} c={c} sw={1.2} ls={4.5}/>
    <line x1="18" y1="92" x2="82" y2="92" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Eight: eight wands flying in parallel diagonal formation
  8: (c) => <g>
    {Array.from({length:8}, (_,i) =>
      <WandLine key={i} x1={24+i*6} y1={22} x2={42+i*6} y2={92} c={c} sw={1.0} ls={3.5}/>
    )}
  </g>,

  // Nine: eight wands planted behind a wall, one leaning forward in foreground
  9: (c) => <g>
    {[18,27,36,45,54,63,72,81].map((x,i) =>
      <WandLine key={i} x1={x} y1={24} x2={x} y2={72} c={c} sw={0.85} ls={3} op={0.45}/>
    )}
    <line x1="14" y1="73" x2="86" y2="73" stroke={c} strokeWidth="0.4" opacity="0.4"/>
    <WandLine x1={46} y1={22} x2={52} y2={95} c={c} sw={1.2} ls={4.5}/>
    <line x1="18" y1="96" x2="82" y2="96" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Ten: ten wands leaning forward in a dense bundle (burden)
  10: (c) => <g>
    {[28,33,38,43,48,53,58,63,68,73].map((x,i) =>
      <WandLine key={i} x1={x} y1={24} x2={x+11} y2={92} c={c} sw={0.9} ls={3}/>
    )}
    <path d="M 32 34 Q 56 28 79 36" fill="none" stroke={c} strokeWidth="0.5" opacity="0.45"/>
    <line x1="18" y1="93" x2="82" y2="93" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,
};

// Helper: draws a pentacle coin (circle + five-pointed star)
const PentacleDisc = ({cx, cy, r=8, c, lw=0.8}) => {
  const star = Array.from({length:10}, (_,i) => {
    const a = (i*36 - 90) * Math.PI / 180;
    const rad = i%2===0 ? r*0.62 : r*0.25;
    return `${i===0?'M':'L'}${(cx+Math.cos(a)*rad).toFixed(1)},${(cy+Math.sin(a)*rad).toFixed(1)}`;
  }).join(' ') + ' Z';
  return <g>
    <circle cx={cx} cy={cy} r={r} fill="none" stroke={c} strokeWidth={lw}/>
    <path d={star} fill="none" stroke={c} strokeWidth={lw*0.6} strokeLinejoin="round"/>
  </g>;
};

// Pentacles-specific glyphs
const PENTACLES_GLYPHS = {
  // Ace: single large pentacle, garden arch below
  1: (c) => <g>
    <PentacleDisc cx={50} cy={52} r={20} c={c} lw={1.0}/>
    <path d="M 33 90 Q 33 72 50 72 Q 67 72 67 90" fill="none" stroke={c} strokeWidth="0.6" opacity="0.5"/>
    <line x1="22" y1="90" x2="78" y2="90" stroke={c} strokeWidth="0.35" opacity="0.4"/>
    <circle cx="28" cy="88" r="1.2" fill={c} opacity="0.4"/>
    <circle cx="40" cy="85" r="1.0" fill={c} opacity="0.35"/>
    <circle cx="60" cy="85" r="1.0" fill={c} opacity="0.35"/>
    <circle cx="72" cy="88" r="1.2" fill={c} opacity="0.4"/>
  </g>,

  // Two: two pentacles connected by S-ribbon, waves below
  2: (c) => <g>
    <PentacleDisc cx={36} cy={42} r={13} c={c} lw={0.9}/>
    <PentacleDisc cx={64} cy={74} r={13} c={c} lw={0.9}/>
    <path d="M 36 29 C 22 29 22 55 36 55 C 50 55 50 61 64 61 C 78 61 78 87 64 87"
      fill="none" stroke={c} strokeWidth="0.65" opacity="0.55"/>
    <path d="M 18 92 Q 30 88 42 92 Q 54 96 66 92 Q 78 88 82 92" fill="none" stroke={c} strokeWidth="0.28" opacity="0.3"/>
  </g>,

  // Three: stone arch with three pentacles set into it (craftsmanship)
  3: (c) => <g>
    <path d="M 26 92 L 26 58 Q 26 28 50 28 Q 74 28 74 58 L 74 92"
      fill="none" stroke={c} strokeWidth="0.7" opacity="0.55"/>
    <path d="M 20 92 L 20 58 Q 20 22 50 22 Q 80 22 80 58 L 80 92"
      fill="none" stroke={c} strokeWidth="0.35" opacity="0.3"/>
    <PentacleDisc cx={50} cy={32} r={9} c={c} lw={0.8}/>
    <PentacleDisc cx={28} cy={62} r={9} c={c} lw={0.8}/>
    <PentacleDisc cx={72} cy={62} r={9} c={c} lw={0.8}/>
    <line x1="18" y1="93" x2="82" y2="93" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Four: one on head, one each side, one underfoot — possession
  4: (c) => <g>
    <PentacleDisc cx={50} cy={28} r={10} c={c} lw={0.85}/>
    <PentacleDisc cx={22} cy={60} r={10} c={c} lw={0.85}/>
    <PentacleDisc cx={78} cy={60} r={10} c={c} lw={0.85}/>
    <PentacleDisc cx={50} cy={94} r={10} c={c} lw={0.85}/>
    <circle cx="50" cy="44" r="5" fill="none" stroke={c} strokeWidth="0.5" opacity="0.3"/>
    <path d="M 36 76 Q 36 62 50 58 Q 64 62 64 76 Q 64 84 50 84 Q 36 84 36 76 Z"
      fill="none" stroke={c} strokeWidth="0.45" opacity="0.28"/>
  </g>,

  // Five: five pentacles in stained-glass window, cold ground below
  5: (c) => <g>
    <path d="M 30 74 L 30 44 Q 30 24 50 24 Q 70 24 70 44 L 70 74"
      fill="none" stroke={c} strokeWidth="0.6" opacity="0.45"/>
    <PentacleDisc cx={50} cy={33} r={8} c={c} lw={0.75}/>
    <PentacleDisc cx={36} cy={52} r={8} c={c} lw={0.75}/>
    <PentacleDisc cx={64} cy={52} r={8} c={c} lw={0.75}/>
    <PentacleDisc cx={36} cy={68} r={8} c={c} lw={0.75}/>
    <PentacleDisc cx={64} cy={68} r={8} c={c} lw={0.75}/>
    <line x1="18" y1="82" x2="82" y2="82" stroke={c} strokeWidth="0.35" opacity="0.35"/>
    <circle cx="28" cy="88" r="0.9" fill={c} opacity="0.3"/>
    <circle cx="44" cy="86" r="0.9" fill={c} opacity="0.3"/>
    <circle cx="60" cy="88" r="0.9" fill={c} opacity="0.3"/>
    <circle cx="74" cy="86" r="0.9" fill={c} opacity="0.3"/>
  </g>,

  // Six: balance scales above, six pentacles below (giving/receiving)
  6: (c) => <g>
    <line x1="50" y1="26" x2="50" y2="46" stroke={c} strokeWidth="0.7"/>
    <line x1="28" y1="46" x2="72" y2="46" stroke={c} strokeWidth="0.7"/>
    <line x1="28" y1="46" x2="28" y2="58" stroke={c} strokeWidth="0.5"/>
    <line x1="72" y1="46" x2="72" y2="58" stroke={c} strokeWidth="0.5"/>
    <path d="M 22 58 Q 28 55 34 58" fill="none" stroke={c} strokeWidth="0.5"/>
    <path d="M 66 58 Q 72 55 78 58" fill="none" stroke={c} strokeWidth="0.5"/>
    <circle cx="50" cy="24" r="2" fill={c} opacity="0.5"/>
    {[[28,72],[50,72],[72,72],[28,92],[50,92],[72,92]].map(([cx,cy],i) =>
      <PentacleDisc key={i} cx={cx} cy={cy} r={9} c={c} lw={0.78}/>
    )}
  </g>,

  // Seven: seven pentacles on a vine (patient harvest)
  7: (c) => <g>
    <path d="M 50 86 Q 44 72 42 56 Q 40 40 50 26" fill="none" stroke={c} strokeWidth="0.55" opacity="0.45"/>
    {[[50,26],[36,40],[64,40],[28,56],[50,54],[72,56],[38,72]].map(([cx,cy],i) =>
      <PentacleDisc key={i} cx={cx} cy={cy} r={8} c={c} lw={0.8}/>
    )}
    <line x1="18" y1="88" x2="82" y2="88" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Eight: eight pentacles in two columns (craftsman's work)
  8: (c) => <g>
    {[[32,24],[32,44],[32,64],[32,84],[64,24],[64,44],[64,64],[64,84]].map(([cx,cy],i) =>
      <PentacleDisc key={i} cx={cx} cy={cy} r={9} c={c} lw={0.8}/>
    )}
    <line x1="48" y1="18" x2="48" y2="94" stroke={c} strokeWidth="0.28" opacity="0.22"/>
  </g>,

  // Nine: nine pentacles among garden vines
  9: (c) => <g>
    <path d="M 28 86 Q 35 66 32 46 Q 30 30 42 26" fill="none" stroke={c} strokeWidth="0.5" opacity="0.4"/>
    <path d="M 72 86 Q 65 66 68 46 Q 70 30 58 26" fill="none" stroke={c} strokeWidth="0.5" opacity="0.4"/>
    {[[50,26],[30,36],[70,36],[26,54],[50,52],[74,54],[30,72],[50,74],[70,72]].map(([cx,cy],i) =>
      <PentacleDisc key={i} cx={cx} cy={cy} r={9} c={c} lw={0.8}/>
    )}
    <line x1="18" y1="86" x2="82" y2="86" stroke={c} strokeWidth="0.3" opacity="0.3"/>
  </g>,

  // Ten: ten pentacles in Tree of Life formation
  10: (c) => <g>
    {[
      [50,22],
      [68,34],[32,34],
      [68,52],[32,52],[50,52],
      [68,70],[32,70],
      [50,80],
      [50,95],
    ].map(([cx,cy],i) => <PentacleDisc key={i} cx={cx} cy={cy} r={6} c={c} lw={0.72}/>)}
    {[
      [[50,22],[68,34]],[[50,22],[32,34]],
      [[68,34],[32,34]],[[68,34],[68,52]],[[32,34],[32,52]],
      [[68,52],[50,52]],[[32,52],[50,52]],
      [[50,52],[50,80]],
      [[68,52],[68,70]],[[32,52],[32,70]],
      [[68,70],[50,80]],[[32,70],[50,80]],
      [[50,80],[50,95]],
    ].map(([[x1,y1],[x2,y2]],i) =>
      <line key={i} x1={x1} y1={y1} x2={x2} y2={y2} stroke={c} strokeWidth="0.25" opacity="0.3"/>
    )}
  </g>,
};

// Major arcana image map
const MAJOR_IMAGES = {
  'fool':             '/major-arcana/00-fool.webp',
  'magician':         '/major-arcana/01-magician.webp',
  'high-priestess':   '/major-arcana/02-high-priestess.webp',
  'empress':          '/major-arcana/03-empress.webp',
  'emperor':          '/major-arcana/04-emperor.webp',
  'hierophant':       '/major-arcana/05-hierophant.webp',
  'lovers':           '/major-arcana/06-lovers.webp',
  'chariot':          '/major-arcana/07-chariot.webp',
  'strength':         '/major-arcana/08-strength.webp',
  'hermit':           '/major-arcana/09-hermit.webp',
  'wheel-of-fortune': '/major-arcana/10-wheel-of-fortune.webp',
  'justice':          '/major-arcana/11-justice.webp',
  'hanged-man':       '/major-arcana/12-hanged-man.webp',
  'death':            '/major-arcana/13-death.webp',
  'temperance':       '/major-arcana/14-temperance.webp',
  'devil':            '/major-arcana/15-devil.webp',
  'tower':            '/major-arcana/16-tower.webp',
  'star':             '/major-arcana/17-star.webp',
  'moon':             '/major-arcana/18-moon.webp',
  'sun':              '/major-arcana/19-sun.webp',
  'judgement':        '/major-arcana/20-judgement.webp',
  'world':            '/major-arcana/21-world.webp',
};

// Main face component
const CardFace = ({card, reversed = false}) => {
  const c = SUIT_COLOR[card.suit] || ART_PALETTE.ink;
  const isMajor = card.suit === 'major';
  const num = card.number;

  // 大阿卡纳：有真实图片时直接渲染 PNG
  if (isMajor && MAJOR_IMAGES[card.id]) {
    return (
      <img
        src={MAJOR_IMAGES[card.id]}
        alt={card.nameZh}
        loading="lazy"
        decoding="async"
        style={{
          width: '100%',
          height: '100%',
          display: 'block',
          objectFit: 'cover',
          objectPosition: 'center',
          transform: reversed ? 'rotate(180deg)' : 'none',
          borderRadius: 'inherit',
        }}
      />
    );
  }

  let centerArt;
  if (isMajor) {
    const fn = MAJOR_GLYPHS[card.id];
    centerArt = fn ? fn(c) : <text x="50" y="65" textAnchor="middle" fontFamily="serif" fontSize="20" fill={c}>{card.roman}</text>;
  } else if (card.suit === 'swords' && num <= 10 && SWORDS_GLYPHS[num]) {
    centerArt = SWORDS_GLYPHS[num](c);
  } else if (card.suit === 'cups' && num <= 10 && CUPS_GLYPHS[num]) {
    centerArt = CUPS_GLYPHS[num](c);
  } else if (card.suit === 'wands' && num <= 10 && WANDS_GLYPHS[num]) {
    centerArt = WANDS_GLYPHS[num](c);
  } else if (card.suit === 'pentacles' && num <= 10 && PENTACLES_GLYPHS[num]) {
    centerArt = PENTACLES_GLYPHS[num](c);
  } else if (num >= 11) {
    const courtFn = COURT_GLYPHS[card.suit]?.[num];
    centerArt = courtFn ? courtFn(c) : null;
  } else {
    centerArt = <PipLayout n={num} suit={card.suit} color={c}/>;
  }

  const cornerLabel = isMajor ? card.roman : null;

  return (
    <svg viewBox="0 0 100 150" xmlns="http://www.w3.org/2000/svg"
      style={{width:'100%', height:'100%', display:'block', transform: reversed ? 'rotate(180deg)' : 'none'}}>
      <defs>
        <linearGradient id={`paper-${card.id}`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#ffffff"/>
          <stop offset="1" stopColor="#f7f5f0"/>
        </linearGradient>
      </defs>
      <rect x="0" y="0" width="100" height="150" fill={`url(#paper-${card.id})`}/>
      {/* outer border */}
      <rect x="3" y="3" width="94" height="144" fill="none" stroke={c} strokeWidth="0.6"/>
      <rect x="5" y="5" width="90" height="140" fill="none" stroke={c} strokeWidth="0.2" opacity="0.5"/>
      {/* corner numerals — major arcana only */}
      {cornerLabel && <>
        <text x="9" y="14" fontFamily="'EB Garamond', serif" fontSize="6" fill={c} fontStyle="italic">{cornerLabel}</text>
        <text x="91" y="143" fontFamily="'EB Garamond', serif" fontSize="6" fill={c} fontStyle="italic" textAnchor="end" transform="rotate(180 91 141)">{cornerLabel}</text>
      </>}

      {/* main illustration zone */}
      <g transform={isMajor ? undefined : 'scale(1.1) translate(-4.5, 2)'}>
        {centerArt}
      </g>

      {/* divider */}
      <line x1="15" y1="124" x2="85" y2="124" stroke={c} strokeWidth="0.3" opacity="0.6"/>
      <circle cx="50" cy="124" r="1" fill={c} opacity="0.7"/>

      {/* name plate — Chinese only */}
      <text x="50" y="139" textAnchor="middle" fontFamily="'Source Han Serif SC','Noto Serif SC',serif" fontSize="6.5" fill={ART_PALETTE.ink} letterSpacing="2">
        {card.nameZh}
      </text>
    </svg>
  );
};

window.CardFace = CardFace;
