/* Parallax hero — illustrations recreated from the reference card.
   All SVGs are original line-art compositions in cream + gold on deep crimson. */

const { useState: pUseState, useEffect: pUseEffect, useRef: pUseRef, useCallback: pUseCallback } = React;

const C_CREAM   = "#f3e2c4";
const C_CREAM_2 = "#fbf3e1";
const C_GOLD    = "#c9a961";
const C_GOLD_2  = "#e8c97a";
const C_REDDOT  = "#a02323";

/* ============================================================
   HANGING BEADS (top-left)
   Diamond lattice of pearl-strings + small floral clusters,
   with three long tassels of beads hanging down.
============================================================ */
function HangingBeads({ width = 460, height = 460 }) {
  const cols = 8, rows = 7;
  const cellW = width / cols;
  const cellH = 56;
  const dots = [];
  for (let r = 0; r <= rows; r++) {
    for (let c = 0; c <= cols; c++) {
      const x = c * cellW;
      const y = r * cellH;
      dots.push({ x, y, r });
    }
  }
  // floral clusters on alternating intersections
  const flowers = [];
  for (let r = 0; r <= rows; r++) {
    for (let c = 0; c <= cols; c++) {
      if ((r + c) % 2 === 0 && r < rows && c < cols) {
        flowers.push({ x: c * cellW + cellW / 2, y: r * cellH + cellH / 2 });
      }
    }
  }
  // tassels at the bottom of the lattice
  const tassels = [
    { x: cellW * 1.5, drop: 180 },
    { x: cellW * 3.5, drop: 230 },
    { x: cellW * 5.5, drop: 160 },
  ];

  return (
    <svg viewBox={`0 0 ${width} ${height}`} width="100%" height="100%" preserveAspectRatio="xMinYMin meet" style={{ overflow: "visible" }}>
      <defs>
        <radialGradient id="bead">
          <stop offset="0%"  stopColor="#fff3c4" />
          <stop offset="55%" stopColor={C_GOLD_2} />
          <stop offset="100%" stopColor="#8c6b1a" />
        </radialGradient>
      </defs>

      {/* small bead dots arranged on a diamond lattice — no lines, just pearls */}
      {dots.map((d, i) => (
        <circle key={`bd-${i}`} cx={d.x} cy={d.y} r="1.4" fill={C_CREAM_2} opacity="0.7" />
      ))}

      {/* floral clusters at intersections */}
      {flowers.map((f, i) => (
        <g key={`fl-${i}`} transform={`translate(${f.x},${f.y})`} opacity="0.85">
          {Array.from({ length: 5 }).map((_, j) => {
            const a = (j / 5) * Math.PI * 2 - Math.PI / 2;
            return <circle key={j} cx={Math.cos(a) * 3.5} cy={Math.sin(a) * 3.5} r="1.8" fill={C_CREAM_2} />;
          })}
          <circle r="1.8" fill={C_GOLD_2} />
        </g>
      ))}

      {/* tassels */}
      {tassels.map((t, ti) => (
        <g key={`ts-${ti}`} transform={`translate(${t.x},${rows * cellH - 12})`}>
          {/* cap */}
          <path d="M -10,0 Q 0,-6 10,0 L 8,12 Q 0,16 -8,12 Z" fill="url(#bead)" stroke={C_GOLD} strokeWidth="0.6" />
          <circle cx="0" cy="14" r="4" fill="url(#bead)" stroke={C_GOLD} strokeWidth="0.5" />
          {/* strings */}
          {[-6, -2, 2, 6].map((dx, si) => {
            const beads = Math.floor(t.drop / 12);
            return (
              <g key={si} transform={`translate(${dx},18)`}>
                <line x1="0" y1="0" x2="0" y2={t.drop} stroke={C_GOLD} strokeWidth="0.5" opacity="0.8" />
                {Array.from({ length: beads }).map((_, bi) => (
                  <circle key={bi} cx="0" cy={bi * 12 + 6} r={bi === beads - 1 ? 4 : 1.4} fill={bi === beads - 1 ? "url(#bead)" : C_CREAM_2} stroke={bi === beads - 1 ? C_GOLD : "none"} strokeWidth="0.5" />
                ))}
                {/* terminal teardrop */}
                <path
                  d={`M 0,${t.drop + 4} q -5,8 0,16 q 5,-8 0,-16 Z`}
                  fill="url(#bead)" stroke={C_GOLD} strokeWidth="0.5"
                />
              </g>
            );
          })}
        </g>
      ))}
    </svg>
  );
}

/* ============================================================
   GANESH SWIRL (top center)
   Stylised Om / Ganesh trunk inside a red circle.
============================================================ */
function GaneshSwirl({ size = 180 }) {
  return (
    <svg viewBox="0 0 180 200" width={size} height={size * 200 / 180}>
      {/* moon/disc behind */}
      <circle cx="120" cy="40" r="34" fill={C_REDDOT} opacity="0.9" />
      <circle cx="120" cy="40" r="33" fill="none" stroke={C_CREAM} strokeWidth="0.6" opacity="0.6" />
      {/* tilak dot */}
      <circle cx="84" cy="32" r="3" fill={C_CREAM_2} />
      {/* the curling trunk — heavy stroke + comb stripes */}
      <g fill="none" stroke={C_CREAM_2} strokeLinecap="round">
        <path d="M 70,40 C 60,72 90,96 116,90 C 142,84 148,52 124,46 C 110,42 100,58 110,72 C 116,82 130,80 134,68"
              strokeWidth="9" opacity="0.95" />
        <path d="M 70,40 C 60,72 90,96 116,90 C 142,84 148,52 124,46 C 110,42 100,58 110,72 C 116,82 130,80 134,68"
              strokeWidth="2" stroke={C_GOLD} opacity="0.7" strokeDasharray="2 4" />
      </g>
      {/* small comb/teeth strokes along the trunk to suggest Ganesh shading */}
      {[
        [70, 50, -65], [68, 62, -55], [72, 74, -42], [86, 88, -20],
        [104, 92, 0], [120, 90, 18], [134, 80, 38], [142, 64, 60],
        [136, 50, 80], [122, 46, 100], [112, 52, 130], [108, 64, 150],
      ].map(([x, y, rot], i) => (
        <line key={i} x1={x} y1={y} x2={x + 8} y2={y}
              stroke={C_REDDOT} strokeWidth="2" strokeLinecap="round"
              transform={`rotate(${rot} ${x} ${y})`} opacity="0.85" />
      ))}
      {/* tiny dot ornament under */}
      <circle cx="90" cy="110" r="2" fill={C_CREAM_2} />
      <circle cx="98" cy="116" r="1.4" fill={C_GOLD} />
    </svg>
  );
}

/* ============================================================
   LEAF BRANCH (top right) — branch with teardrop leaves on a red sun.
============================================================ */
function LeafBranch({ size = 260 }) {
  return (
    <svg viewBox="0 0 260 260" width={size} height={size}>
      {/* red disc */}
      <circle cx="180" cy="80" r="68" fill={C_REDDOT} opacity="0.9" />
      <circle cx="180" cy="80" r="66" fill="none" stroke={C_CREAM} strokeWidth="0.5" opacity="0.5" />
      {/* main branch curve */}
      <g stroke={C_CREAM_2} fill="none" strokeLinecap="round">
        <path d="M 70,230 C 110,180 120,150 130,110 C 140,70 160,40 200,20" strokeWidth="2.2" />
        {/* twigs */}
        <path d="M 130,110 C 110,108 92,116 80,134" strokeWidth="1.4" />
        <path d="M 130,110 C 154,104 168,90 178,72" strokeWidth="1.4" />
        <path d="M 138,80 C 122,72 110,80 100,96" strokeWidth="1.2" />
        <path d="M 142,72 C 162,68 174,60 184,46" strokeWidth="1.2" />
        <path d="M 152,50 C 138,38 124,40 116,54" strokeWidth="1.2" />
        <path d="M 170,32 C 184,22 198,22 210,30" strokeWidth="1.2" />
      </g>
      {/* leaves */}
      {[
        [72, 232, -55], [82, 200, -38], [88, 175, -32], [108, 154, -22],
        [126, 138, -8], [132, 122, 4], [124, 108, 38], [148, 102, 18],
        [165, 92, 35], [176, 76, 50], [186, 60, 65], [180, 46, 80],
        [108, 134, -90], [82, 138, -110], [100, 104, -80], [120, 96, -45],
        [158, 60, 70], [148, 44, 95], [128, 56, 130], [115, 50, 145],
        [188, 42, 60], [200, 34, 70], [210, 26, 80],
      ].map(([x, y, rot], i) => (
        <g key={i} transform={`translate(${x},${y}) rotate(${rot})`}>
          <path d="M 0,0 Q 5,-7 12,0 Q 5,7 0,0 Z"
                fill="none" stroke={C_CREAM_2} strokeWidth="1.1" />
          <line x1="0" y1="0" x2="12" y2="0" stroke={C_CREAM_2} strokeWidth="0.6" opacity="0.7" />
        </g>
      ))}
      {/* little flying birds (V strokes) */}
      {[[214, 38], [224, 24], [206, 18], [232, 50], [220, 60]].map(([x, y], i) => (
        <path key={`b${i}`} d={`M ${x},${y} q 3,-3 6,0 m 0,0 q 3,-3 6,0`}
              stroke={C_CREAM_2} fill="none" strokeWidth="1" strokeLinecap="round" />
      ))}
    </svg>
  );
}

/* ============================================================
   PALACE SILHOUETTE (bottom left)
   Mughal palace / ghat line art with domes, columns, reflection.
============================================================ */
function PalaceLineArt({ width = 520, height = 320 }) {
  return (
    <svg viewBox="0 0 520 320" width="100%" height="100%" preserveAspectRatio="xMinYMax meet">
      <g fill="none" stroke={C_CREAM_2} strokeWidth="1.2" strokeLinejoin="round" strokeLinecap="round">
        {/* horizon water line */}
        <line x1="0" y1="220" x2="520" y2="220" opacity="0.7" />
        {/* water ripples */}
        <path d="M 20,232 q 8,-2 16,0 t 16,0 t 16,0 t 16,0" opacity="0.45" />
        <path d="M 80,246 q 8,-2 16,0 t 16,0 t 16,0 t 16,0 t 16,0" opacity="0.4" />
        <path d="M 200,258 q 10,-2 20,0 t 20,0 t 20,0 t 20,0" opacity="0.45" />

        {/* main palace body */}
        {/* left tower */}
        <rect x="22" y="118" width="48" height="100" />
        <path d="M 22,118 L 46,80 L 70,118 Z" />
        <circle cx="46" cy="74" r="3" />
        <line x1="46" y1="68" x2="46" y2="62" />
        {/* arches */}
        <path d="M 28,210 q 0,-20 18,-20 q 18,0 18,20" />
        <path d="M 32,190 q 0,-12 14,-12 q 14,0 14,12" />

        {/* central big dome with smaller flanking domes */}
        <rect x="80" y="138" width="180" height="80" />
        {/* big dome */}
        <path d="M 130,138 Q 130,86 170,86 Q 210,86 210,138 Z" />
        <line x1="170" y1="86" x2="170" y2="72" />
        <circle cx="170" cy="68" r="4" />
        <path d="M 170,64 L 170,52 L 174,52 L 170,46 L 166,52 L 170,52" />
        {/* small left dome */}
        <path d="M 90,138 Q 90,116 104,116 Q 118,116 118,138 Z" />
        <line x1="104" y1="116" x2="104" y2="106" />
        <circle cx="104" cy="103" r="2" />
        {/* small right dome */}
        <path d="M 220,138 Q 220,116 234,116 Q 248,116 248,138 Z" />
        <line x1="234" y1="116" x2="234" y2="106" />
        <circle cx="234" cy="103" r="2" />

        {/* main arches row */}
        {[88, 116, 144, 172, 200, 228].map((x, i) => (
          <g key={i}>
            <path d={`M ${x},210 q 0,-22 14,-22 q 14,0 14,22 Z`} />
            <line x1={x} y1="210" x2={x + 28} y2="210" />
          </g>
        ))}
        {/* upper smaller arch row */}
        {[92, 116, 140, 164, 188, 212, 236].map((x, i) => (
          <path key={`u${i}`} d={`M ${x},184 q 0,-12 10,-12 q 10,0 10,12 Z`} />
        ))}

        {/* secondary block right */}
        <rect x="266" y="156" width="120" height="62" />
        {/* small dome cluster */}
        <path d="M 286,156 Q 286,128 302,128 Q 318,128 318,156 Z" />
        <line x1="302" y1="128" x2="302" y2="120" />
        <circle cx="302" cy="117" r="2" />
        <path d="M 340,156 Q 340,134 354,134 Q 368,134 368,156 Z" />
        <line x1="354" y1="134" x2="354" y2="126" />
        <circle cx="354" cy="123" r="2" />
        {/* arches */}
        {[274, 296, 318, 340, 362].map((x, i) => (
          <path key={`a${i}`} d={`M ${x},210 q 0,-18 11,-18 q 11,0 11,18 Z`} />
        ))}

        {/* far right tower */}
        <rect x="406" y="132" width="44" height="88" />
        <path d="M 406,132 L 428,98 L 450,132 Z" />
        <line x1="428" y1="98" x2="428" y2="86" />
        <circle cx="428" cy="82" r="2.5" />
        <path d="M 412,210 q 0,-18 16,-18 q 16,0 16,18" />

        {/* terrace ornament line above main block */}
        <path d="M 80,138 L 260,138" />
        <path d="M 84,134 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 q 6,-4 12,0 opacity=.5" opacity=".5" />

        {/* steps / ghat */}
        <line x1="0" y1="218" x2="450" y2="218" />
        <line x1="0" y1="220" x2="460" y2="220" />

        {/* tiny birds above */}
        {[[120, 60], [140, 50], [200, 40], [260, 56], [320, 36], [360, 48]].map(([x, y], i) => (
          <path key={`b${i}`} d={`M ${x},${y} q 3,-3 6,0 m 0,0 q 3,-3 6,0`} strokeWidth="0.9" />
        ))}

        {/* faint reflection (mirrored, faded) */}
        <g opacity="0.18" transform="translate(0,440) scale(1,-1)">
          <rect x="22" y="118" width="48" height="100" />
          <rect x="80" y="138" width="180" height="80" />
          <path d="M 130,138 Q 130,86 170,86 Q 210,86 210,138 Z" />
          <rect x="266" y="156" width="120" height="62" />
          <rect x="406" y="132" width="44" height="88" />
        </g>
      </g>
    </svg>
  );
}

/* ============================================================
   FLOATING DIYA (oil lamp) — cream line art with gold flame
============================================================ */
function LineDiya({ size = 56, lit = true }) {
  return (
    <svg viewBox="0 0 80 70" width={size} height={size * 70 / 80}>
      {/* reflection on water */}
      <ellipse cx="40" cy="58" rx="22" ry="3" fill={C_GOLD} opacity="0.25" />
      {/* bowl */}
      <path d="M 12,42 Q 40,58 68,42 L 64,50 Q 40,62 16,50 Z"
            fill="none" stroke={C_CREAM_2} strokeWidth="1.2" strokeLinejoin="round" />
      <path d="M 12,42 Q 40,30 68,42" fill="none" stroke={C_CREAM_2} strokeWidth="1.2" />
      {/* wick base */}
      <path d="M 36,38 L 44,38 L 42,32 L 38,32 Z" fill="none" stroke={C_CREAM_2} strokeWidth="1" />
      {lit && (
        <g>
          {/* flame outer */}
          <path d="M 40,30 C 34,22 34,12 40,2 C 46,12 46,22 40,30 Z"
                fill={C_GOLD_2} stroke={C_CREAM_2} strokeWidth="0.8" />
          {/* flame inner */}
          <path d="M 40,26 C 37,20 37,14 40,8 C 43,14 43,20 40,26 Z"
                fill="#fff3b0" opacity="0.85" />
        </g>
      )}
    </svg>
  );
}

/* ============================================================
   LOTUS BOUQUET (bottom right) — open lotus + buds + pads
============================================================ */
function LotusBouquet({ size = 360 }) {
  return (
    <svg viewBox="0 0 360 460" width={size} height={size * 460 / 360}>
      <g fill="none" stroke={C_CREAM_2} strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round">
        {/* stems */}
        <path d="M 200,460 C 190,360 200,260 220,160" strokeWidth="1.4" />
        <path d="M 140,460 C 138,400 130,330 110,290" />
        <path d="M 260,460 C 280,420 290,380 300,340" />
        <path d="M 240,460 C 232,420 228,380 230,340" />
        <path d="M 90,460 C 82,420 70,380 50,360" />

        {/* main open lotus — top right */}
        <g transform="translate(220,130)">
          {/* outer petals */}
          {[-80, -45, 0, 45, 80, -100, 100].map((a, i) => (
            <path key={i}
                  d="M 0,0 Q -16,-46 0,-86 Q 16,-46 0,0 Z"
                  transform={`rotate(${a})`}
                  strokeWidth="1.2" />
          ))}
          {/* inner petals */}
          {[-30, 0, 30].map((a, i) => (
            <path key={`i${i}`}
                  d="M 0,0 Q -10,-30 0,-58 Q 10,-30 0,0 Z"
                  transform={`rotate(${a})`}
                  strokeWidth="1" />
          ))}
          {/* center seedpod */}
          <ellipse cx="0" cy="-8" rx="9" ry="6" />
          <circle cx="-4" cy="-9" r="1.2" fill={C_GOLD} stroke="none" />
          <circle cx="2"  cy="-10" r="1.2" fill={C_GOLD} stroke="none" />
          <circle cx="6"  cy="-7" r="1.2" fill={C_GOLD} stroke="none" />
          <circle cx="-2" cy="-6" r="1.2" fill={C_GOLD} stroke="none" />
        </g>

        {/* bud — closed lotus left of main */}
        <g transform="translate(108,288)">
          <path d="M 0,0 Q -10,-26 0,-46 Q 10,-26 0,0 Z" strokeWidth="1.2" />
          <path d="M -2,-2 Q -7,-18 0,-32 Q 7,-18 2,-2" strokeWidth="0.9" />
        </g>
        <g transform="translate(300,338) rotate(20)">
          <path d="M 0,0 Q -8,-22 0,-38 Q 8,-22 0,0 Z" strokeWidth="1.2" />
        </g>

        {/* small lotus medium */}
        <g transform="translate(230,335) scale(0.7)">
          {[-60, -20, 20, 60].map((a, i) => (
            <path key={i}
                  d="M 0,0 Q -12,-30 0,-58 Q 12,-30 0,0 Z"
                  transform={`rotate(${a})`}
                  strokeWidth="1.1" />
          ))}
          <ellipse cx="0" cy="-6" rx="6" ry="4" />
        </g>

        {/* lily pads — big disc with radial lines */}
        {[
          { x: 110, y: 408, r: 56 },
          { x: 240, y: 426, r: 64 },
          { x: 320, y: 396, r: 46 },
          { x: 60,  y: 432, r: 40 },
        ].map((p, i) => (
          <g key={`pad${i}`} transform={`translate(${p.x},${p.y})`}>
            <circle r={p.r} strokeWidth="1.4" />
            {Array.from({ length: 14 }).map((_, k) => {
              const a = (k / 14) * Math.PI * 2 - Math.PI / 2;
              return <line key={k} x1="0" y1="0"
                           x2={Math.cos(a) * p.r * 0.9}
                           y2={Math.sin(a) * p.r * 0.9}
                           strokeWidth="0.7" opacity="0.7" />;
            })}
            {/* notch */}
            <path d={`M 0,0 L 0,${p.r}`} strokeWidth="1.4" />
            <circle r="2" fill={C_CREAM_2} stroke="none" />
          </g>
        ))}
      </g>
    </svg>
  );
}

/* ============================================================
   BOTTOM FRIEZE — decorative border band, central lotus icon.
============================================================ */
function BottomFrieze({ width = 1200 }) {
  return (
    <svg viewBox={`0 0 ${width} 96`} width="100%" height="96" preserveAspectRatio="none">
      <g fill="none" stroke={C_GOLD_2} strokeWidth="1.1" strokeLinecap="round">
        {/* top wavy line */}
        <path d={`M 0,8 ${Array.from({ length: Math.ceil(width / 40) }).map((_, i) => `q 10,-8 20,0 q 10,8 20,0`).join(" ")}`} />
        {/* zigzag main */}
        <path d={`M 0,38 ${Array.from({ length: Math.ceil(width / 28) }).map((_, i) => `l 14,-14 l 14,14`).join(" ")}`} strokeWidth="1.4" />
        {/* dot row */}
        {Array.from({ length: Math.ceil(width / 18) }).map((_, i) => (
          <circle key={i} cx={i * 18 + 9} cy="58" r="1.6" fill={C_GOLD_2} stroke="none" />
        ))}
        {/* small triangles */}
        {Array.from({ length: Math.ceil(width / 28) }).map((_, i) => (
          <path key={`t${i}`} d={`M ${i * 28 + 6},80 L ${i * 28 + 14},70 L ${i * 28 + 22},80 Z`} />
        ))}
      </g>
      {/* central lotus icon (only if room) */}
      <g transform={`translate(${width / 2},48)`}>
        <path d="M 0,8 Q -18,-4 -28,12 Q -10,18 0,8 Z" fill="none" stroke={C_GOLD_2} strokeWidth="1" />
        <path d="M 0,8 Q 18,-4 28,12 Q 10,18 0,8 Z" fill="none" stroke={C_GOLD_2} strokeWidth="1" />
        <path d="M 0,8 Q -8,-12 0,-20 Q 8,-12 0,8 Z" fill="none" stroke={C_GOLD_2} strokeWidth="1" />
        <circle r="1.6" fill={C_GOLD_2} />
      </g>
    </svg>
  );
}

/* ============================================================
   FLYING BIRDS — tiny V strokes scattered.
============================================================ */
function BirdSwarm({ count = 6, area = { w: 200, h: 80 } }) {
  const birds = Array.from({ length: count }).map((_, i) => ({
    x: (i / count) * area.w + Math.random() * 20,
    y: Math.random() * area.h,
    s: 0.8 + Math.random() * 0.6,
  }));
  return (
    <svg viewBox={`0 0 ${area.w} ${area.h}`} width="100%" height="100%">
      {birds.map((b, i) => (
        <path key={i} d={`M ${b.x},${b.y} q 4,-3 8,0 m 0,0 q 4,-3 8,0`}
              stroke={C_CREAM_2} fill="none" strokeWidth={b.s} strokeLinecap="round" />
      ))}
    </svg>
  );
}

/* ============================================================
   PARALLAX HERO
============================================================ */
function ParallaxHero({ groomName, brideName, dateDisplay, location }) {
  const stageRef = pUseRef(null);
  const [scrollY, setScrollY] = pUseState(0);
  const [tilt, setTilt] = pUseState({ x: 0, y: 0 });
  const [sparkles, setSparkles] = pUseState([]);
  const [tappedKey, setTappedKey] = pUseState(null);

  // scroll-driven parallax
  pUseEffect(() => {
    let raf = 0;
    const onScroll = () => {
      cancelAnimationFrame(raf);
      raf = requestAnimationFrame(() => {
        if (!stageRef.current) return;
        const r = stageRef.current.getBoundingClientRect();
        const seen = Math.max(0, Math.min(1, -r.top / Math.max(1, r.height)));
        setScrollY(seen);
      });
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => { window.removeEventListener("scroll", onScroll); cancelAnimationFrame(raf); };
  }, []);

  // mouse 3D tilt + touch + gyroscope
  pUseEffect(() => {
    const el = stageRef.current;
    if (!el) return;
    const onMove = (e) => {
      const r = el.getBoundingClientRect();
      const t = e.touches ? e.touches[0] : e;
      const dx = (t.clientX - r.left - r.width / 2) / r.width;
      const dy = (t.clientY - r.top  - r.height / 2) / r.height;
      setTilt({ x: dx, y: dy });
    };
    const onOrient = (e) => {
      if (e.gamma == null) return;
      setTilt({ x: Math.max(-0.5, Math.min(0.5, e.gamma / 60)), y: Math.max(-0.5, Math.min(0.5, e.beta / 90)) });
    };
    el.addEventListener("mousemove", onMove);
    el.addEventListener("touchmove", onMove, { passive: true });
    window.addEventListener("deviceorientation", onOrient);
    return () => {
      el.removeEventListener("mousemove", onMove);
      el.removeEventListener("touchmove", onMove);
      window.removeEventListener("deviceorientation", onOrient);
    };
  }, []);

  // sparkle on tap of any illustration
  const sparkleAt = pUseCallback((clientX, clientY, key) => {
    if (!stageRef.current) return;
    const r = stageRef.current.getBoundingClientRect();
    const x = clientX - r.left, y = clientY - r.top;
    const id = Math.random();
    const newOnes = Array.from({ length: 9 }).map((_, i) => {
      const a = (i / 9) * Math.PI * 2 + Math.random() * 0.6;
      return {
        id: id + i, x, y,
        dx: Math.cos(a) * (40 + Math.random() * 60),
        dy: Math.sin(a) * (40 + Math.random() * 60),
        rot: Math.random() * 360,
        size: 4 + Math.random() * 6,
        kind: i % 3,
      };
    });
    setSparkles((s) => [...s, ...newOnes]);
    setTappedKey(key);
    setTimeout(() => setTappedKey(null), 600);
    setTimeout(() => setSparkles((s) => s.filter((p) => !newOnes.find((n) => n.id === p.id))), 1500);
  }, []);

  const onElemTap = (e, key) => {
    e.stopPropagation();
    const t = e.touches ? e.touches[0] : (e.changedTouches ? e.changedTouches[0] : e);
    sparkleAt(t.clientX, t.clientY, key);
  };

  // background taps do nothing — only direct illustration taps sparkle

  // helper for layer transform combining scroll + tilt
  const layerStyle = (depth, opts = {}) => {
    // depth: 0 = closest, 1 = farthest. Higher depth -> slower scroll move, larger background offset
    const scrollAmt = (1 - depth) * 280 * scrollY;  // foreground moves more
    const farAmt    = depth * 80 * scrollY;          // background moves slightly opposite/down
    const tx = tilt.x * (1 - depth) * 28;
    const ty = tilt.y * (1 - depth) * 18;
    const rZ = tilt.x * (1 - depth) * 2;
    return {
      transform: `translate3d(${(opts.tx || 0) + tx}px, ${(opts.ty || 0) + farAmt - scrollAmt}px, 0) rotate(${rZ}deg) scale(${1 - depth * 0.04})`,
      willChange: "transform",
      ...(opts.style || {}),
    };
  };

  return (
    <section id="hero" className="parallax-hero" data-screen-label="01 Welcome" ref={stageRef}>
      {/* texture overlay */}
      <div className="ph-texture" />
      <div className="ph-vignette" />

      {/* === FAR BACK LAYER === */}
      <div className="ph-layer ph-layer-far" style={layerStyle(0.85)}>
        <div className="ph-bg-disc" style={{
          left: "8%", top: "-12%",
          width: "60vw", height: "60vw",
        }} />
        <div className="ph-bg-disc soft" style={{
          right: "-8%", bottom: "-18%",
          width: "70vw", height: "70vw",
        }} />
      </div>

      {/* === BACK: PALACE === */}
      <div
        className={`ph-layer ph-palace ${tappedKey === "palace" ? "is-tapped" : ""}`}
        style={layerStyle(0.6)}
        onClick={(e) => onElemTap(e, "palace")}
        onTouchEnd={(e) => onElemTap(e, "palace")}
        role="button" tabIndex={0} aria-label="Palace illustration"
      >
        <PalaceLineArt />
        <div className="ph-birds-left">
          <BirdSwarm count={5} area={{ w: 240, h: 120 }} />
        </div>
      </div>

      {/* === MID: LEAF BRANCH + SUN (top-right) === */}
      <div
        className={`ph-layer ph-branch ${tappedKey === "branch" ? "is-tapped" : ""}`}
        style={layerStyle(0.4)}
        onClick={(e) => onElemTap(e, "branch")}
        onTouchEnd={(e) => onElemTap(e, "branch")}
        role="button" tabIndex={0} aria-label="Leaf branch with rising sun"
      >
        <LeafBranch size={300} />
      </div>

      {/* === MID-FRONT: GANESH SWIRL === */}
      <div
        className={`ph-layer ph-ganesh ${tappedKey === "ganesh" ? "is-tapped" : ""}`}
        style={layerStyle(0.25)}
        onClick={(e) => onElemTap(e, "ganesh")}
        onTouchEnd={(e) => onElemTap(e, "ganesh")}
        role="button" tabIndex={0} aria-label="Ganesh blessing"
      >
        <GaneshSwirl size={200} />
      </div>

      {/* === FRONT: LOTUS BOUQUET === */}
      <div
        className={`ph-layer ph-lotus ${tappedKey === "lotus" ? "is-tapped" : ""}`}
        style={layerStyle(0.05)}
        onClick={(e) => onElemTap(e, "lotus")}
        onTouchEnd={(e) => onElemTap(e, "lotus")}
        role="button" tabIndex={0} aria-label="Lotus pond"
      >
        <LotusBouquet size={400} />
      </div>

      {/* === FRONT: DIYAS row === */}
      <div className="ph-diyas" style={layerStyle(0.0, { ty: 0 })}>
        {[0, 1, 2, 3, 4].map((i) => (
          <button
            key={i}
            className={`ph-diya ${tappedKey === `diya-${i}` ? "is-tapped" : ""}`}
            style={{ animationDelay: `${i * 0.4}s` }}
            onClick={(e) => onElemTap(e, `diya-${i}`)}
            onTouchEnd={(e) => onElemTap(e, `diya-${i}`)}
            aria-label="Diya"
          >
            <LineDiya size={64 - i * 4} />
          </button>
        ))}
      </div>

      {/* === COPY === */}
      <div className="ph-content" style={{
        transform: `translate3d(0, ${-scrollY * 80}px, 0)`,
        willChange: "transform",
      }}>
        <div className="ph-eyebrow">
          <span className="ph-divider-mini" />
          Save <em>the</em> Date
          <span className="ph-divider-mini" />
        </div>

        <h1 className="ph-names">
          <span className="ph-name">{groomName}</span>
          <span className="ph-amp">&amp;</span>
          <span className="ph-name">{brideName}</span>
        </h1>

        <div className="ph-leaf-divider" aria-hidden="true">
          <svg viewBox="0 0 200 24" width="200" height="24">
            <path d="M 0,12 L 70,12" stroke={C_GOLD} strokeWidth="0.8" />
            <path d="M 130,12 L 200,12" stroke={C_GOLD} strokeWidth="0.8" />
            <path d="M 100,12 m -10,0 q 4,-8 10,-6 q -2,8 -10,6 z M 100,12 m 10,0 q -4,-8 -10,-6 q 2,8 10,6 z" fill={C_GOLD} />
            <circle cx="100" cy="12" r="1.6" fill={C_GOLD_2} />
          </svg>
        </div>

        <div className="ph-date">{dateDisplay}</div>
        <div className="ph-place">{location}</div>

        <div className="ph-cta">
          <a href="#celebration" className="ph-btn">
            <span>Open the Story</span>
            <svg viewBox="0 0 24 24" width="16" height="16"><path d="M 4 12 L 20 12 M 14 6 L 20 12 L 14 18" stroke="currentColor" strokeWidth="1.6" fill="none" strokeLinecap="round" /></svg>
          </a>
          <a href="#venue" className="ph-btn ghost">
            <svg viewBox="0 0 24 24" width="16" height="16"><path d="M 12 2 C 7 2 4 5 4 10 C 4 17 12 22 12 22 C 12 22 20 17 20 10 C 20 5 17 2 12 2 Z M 12 12 m -3 0 a 3 3 0 1 0 6 0 a 3 3 0 1 0 -6 0" stroke="currentColor" strokeWidth="1.4" fill="none" /></svg>
            <span>Where</span>
          </a>
        </div>


      </div>

      {/* === BOTTOM FRIEZE === */}
      <div className="ph-frieze" style={{
        transform: `translate3d(0, ${(1 - scrollY) * 0}px, 0)`,
      }}>
        <BottomFrieze width={1400} />
      </div>

      {/* sparkles */}
      {sparkles.map((s) => (
        <span
          key={s.id}
          className={`ph-spark ph-spark-${s.kind}`}
          style={{
            left: s.x, top: s.y,
            "--dx": `${s.dx}px`, "--dy": `${s.dy}px`,
            "--rot": `${s.rot}deg`, "--size": `${s.size}px`,
          }}
        />
      ))}
    </section>
  );
}

Object.assign(window, {
  HangingBeads, GaneshSwirl, LeafBranch, PalaceLineArt, LineDiya,
  LotusBouquet, BottomFrieze, BirdSwarm, ParallaxHero,
});
