// ────────────────────────────────────────────────────────────────────────
//  rsvp.jsx — RSVP call-to-action section. NOT a form: an elegant invitation
//  to respond, with a real button that links out to the external RSVP site.
//
//  • deep-maroon premium card on warm ivory, antique-gold frame & type
//  • staggered reveal on scroll-in (emblem+names → heading → body → button)
//    built on the project's IntersectionObserver pattern (window.useInView)
//  • faint VR watermark + faint temple line-art + subtle falling petals
//  • a subtle secondary "IMPORTANT" info card below the main card
// ────────────────────────────────────────────────────────────────────────
(function () {
  const { useRef } = React;

  // ── EASY-TO-EDIT CONFIG ───────────────────────────────────────────────
  // Clean /rsvp route inside this one merged project. On a static host
  // (Vercel/Netlify/GitHub Pages) the folder rsvp/index.html serves at /rsvp.
  const RSVP_URL = '/rsvp/';     // ← the merged RSVP page (Vercel clean URL; cleanUrls in vercel.json)
  const RSVP_DEADLINE = '[Date TBD]'; // ← e.g. '15th June 2025'
  // ──────────────────────────────────────────────────────────────────────

  // palette
  const GOLD = '#c9a24b';
  const GOLD_LT = '#e3c172';
  const GOLD_HI = '#f3da97';
  const GOLD_DK = '#9c7a32';
  const IVORY = '#f3e7cd';
  const CREAM = '#efe0bf';
  const MAROON = '#6b1d1d';

  const A = (q) => window.asset ? window.asset(q) : q;

  // small gold calendar glyph
  function CalendarIcon({ size = 20, color = GOLD_HI }) {
    return (
      <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true"
      style={{ display: 'block', flex: 'none' }}>
        <rect x="3.5" y="5" width="17" height="15.5" rx="2.4" stroke={color} strokeWidth="1.5" />
        <path d="M3.5 9.5 H20.5" stroke={color} strokeWidth="1.5" />
        <path d="M8 3 V6.5 M16 3 V6.5" stroke={color} strokeWidth="1.5" strokeLinecap="round" />
        <rect x="6.7" y="12" width="2.4" height="2.4" rx="0.5" fill={color} />
        <rect x="10.8" y="12" width="2.4" height="2.4" rx="0.5" fill={color} />
        <rect x="14.9" y="12" width="2.4" height="2.4" rx="0.5" fill={color} />
      </svg>);

  }

  // gold corner flourish for the maroon card
  function GoldCorner({ pos }) {
    const flip = { tl: 'none', tr: 'scaleX(-1)', bl: 'scaleY(-1)', br: 'scale(-1,-1)' }[pos];
    const place = {
      tl: { top: 14, left: 14 }, tr: { top: 14, right: 14 },
      bl: { bottom: 14, left: 14 }, br: { bottom: 14, right: 14 }
    }[pos];
    return (
      <svg width="52" height="52" viewBox="0 0 52 52" aria-hidden="true"
      style={{ position: 'absolute', ...place, transform: flip, pointerEvents: 'none', zIndex: 3 }}>
        <g fill="none" stroke={GOLD_LT} strokeWidth="1.1" strokeLinecap="round">
          <path d="M5 21 C5 11 11 5 21 5" strokeOpacity="0.85" />
          <path d="M5 31 C5 16 16 5 31 5" strokeOpacity="0.5" />
          <path d="M12 12 q9 0 9 9" strokeOpacity="0.6" />
          <path d="M5 5 q12 -1 13 4 M5 5 q-1 12 4 13" strokeOpacity="0.7" />
          <circle cx="5" cy="5" r="1.8" fill={GOLD_LT} stroke="none" />
        </g>
      </svg>);

  }

  // beaded gold divider (line · diamond beads · lotus · beads · line)
  function BeadDivider({ width = 230, reveal = 1, color = GOLD_LT }) {
    const len = 70 * reveal;
    return (
      <svg width={width} height="18" viewBox="0 0 230 18" style={{ display: 'block', overflow: 'visible' }}>
        <g stroke={color} strokeWidth="1" fill="none" strokeOpacity="0.8">
          <line x1={92 - len} y1="9" x2="92" y2="9" />
          <line x1="138" y1="9" x2={138 + len} y2="9" />
        </g>
        <g fill={color} style={{ opacity: reveal }}>
          <circle cx="100" cy="9" r="1.5" /><circle cx="130" cy="9" r="1.5" />
          <path d="M108 9 l4 -3 l4 3 l-4 3 Z" fillOpacity="0.9" />
          <path d="M122 9 l-4 -3 l-4 3 l4 3 Z" fillOpacity="0.9" />
          <circle cx="115" cy="9" r="2" />
        </g>
      </svg>);

  }

  // tiny gold heart divider used above the button
  function HeartDivider({ width = 200, reveal = 1, color = GOLD_LT }) {
    const len = 64 * reveal;
    return (
      <svg width={width} height="18" viewBox="0 0 200 18" style={{ display: 'block', overflow: 'visible' }}>
        <g stroke={color} strokeWidth="1" strokeOpacity="0.7">
          <line x1={84 - len} y1="9" x2="84" y2="9" />
          <line x1="116" y1="9" x2={116 + len} y2="9" />
        </g>
        <g fill={color} style={{ opacity: reveal }}>
          <circle cx="90" cy="9" r="1.4" /><circle cx="110" cy="9" r="1.4" />
          <path d="M100 13.5 C 96 10 96.2 5.6 99 5.6 C 100 5.6 100 6.6 100 6.9 C 100 6.6 100 5.6 101 5.6 C 103.8 5.6 104 10 100 13.5 Z" />
        </g>
      </svg>);

  }

  // faint temple skyline (simple stepped-gopuram silhouettes) near the card base
  function TempleSkyline() {
    const gop = (x, w, h) => {
      const steps = 4,sw = w,parts = [];
      for (let i = 0; i < steps; i++) {
        const iw = sw * (1 - i * 0.16),ih = h / steps;
        const ix = x + (sw - iw) / 2,iy = h - (i + 1) * ih;
        parts.push(<rect key={i} x={ix} y={iy} width={iw} height={ih * 0.92} rx="1.5" />);
      }
      parts.push(<polygon key="top" points={`${x + sw / 2},${0} ${x + sw * 0.34},${h / steps} ${x + sw * 0.66},${h / steps}`} />);
      return parts;
    };
    return (
      <svg viewBox="0 0 400 120" preserveAspectRatio="xMidYEnd meet" aria-hidden="true"
      style={{ position: 'absolute', left: 0, right: 0, bottom: 0, width: '100%', height: 'clamp(78px,20vw,116px)',
        pointerEvents: 'none', opacity: 0.16 }}>
        <g fill="none" stroke={GOLD_LT} strokeWidth="1.2" strokeLinejoin="round">
          {gop(34, 70, 92)}
          {gop(150, 100, 116)}
          {gop(300, 66, 86)}
          <line x1="0" y1="118" x2="400" y2="118" strokeOpacity="0.7" />
        </g>
      </svg>);

  }

  function RSVPSection() {
    const [ref, inView] = window.useInView(0.22);
    const OD = window.OrnamentalDivider;
    const secRef = useRef(null);
    // scroll-linked drift so the card travels with the scroll, not in a jump
    const p = window.useScrollProgress ? window.useScrollProgress(secRef) : 0;

    // staggered transition helper
    const rise = (delay, dist = 22) => ({
      opacity: inView ? 1 : 0,
      transform: inView ? 'translateY(0)' : `translateY(${dist}px)`,
      transition: `opacity .85s cubic-bezier(.22,.61,.36,1) ${delay}s, transform .85s cubic-bezier(.22,.61,.36,1) ${delay}s`
    });

    return (
      <section
        ref={secRef}
        id="rsvp"
        data-screen-label="RSVP"
        style={{
          position: 'relative',
          minHeight: '100vh',
          display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
          padding: 'clamp(54px,10vh,110px) 5vw clamp(60px,12vh,120px)',
          background: 'transparent',
          overflow: 'hidden'
        }}>
        
        {/* soft vignette */}
        <div aria-hidden="true" style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
          background: 'radial-gradient(120% 86% at 50% 40%, transparent 70%, rgba(110,62,24,0.08) 100%)' }} />

        {/* (ambient petals now drift page-wide via window.FlowAmbience) */}

        {/* ── MAIN RSVP CARD ─────────────────────────────────────────── */}
        <div
          ref={ref}
          style={{
            position: 'relative', zIndex: 2,
            width: 'min(92vw, 470px)',
            padding: 'clamp(30px,7vw,52px) clamp(22px,6vw,44px) clamp(34px,8vw,56px)',
            textAlign: 'center',
            borderRadius: 22,
            background: 'linear-gradient(168deg, #7a2222 0%, #631a1b 30%, #4d1314 66%, #380d0e 100%)',
            border: `1.5px solid ${GOLD}`,
            boxShadow: inView ?
            '0 40px 80px -34px rgba(50,12,10,0.66), 0 0 0 6px rgba(247,237,213,0.55), 0 0 0 7px rgba(201,162,75,0.45), inset 0 1px 0 rgba(255,214,160,0.18)' :
            '0 30px 60px -34px rgba(50,12,10,0.5), 0 0 0 6px rgba(247,237,213,0.4), 0 0 0 7px rgba(201,162,75,0.3)',
            opacity: inView ? 1 : 0,
            transform: inView ?
            `scale(1) translate3d(0,${(-p * 30).toFixed(2)}px,0)` :
            'scale(0.965) translate3d(0,40px,0)',
            transition: inView ?
            'opacity 1s cubic-bezier(.22,.61,.36,1), transform .35s linear, box-shadow 1.2s ease' :
            'opacity 1s cubic-bezier(.22,.61,.36,1), transform 1.05s cubic-bezier(.22,.61,.36,1), box-shadow 1.2s ease',
            overflow: 'hidden'
          }}>
          
          {/* faint VR watermark */}
          <img src={A('assets/emblem2.png')} alt="" aria-hidden="true" draggable="false"
          style={{ position: 'absolute', left: '50%', top: '52%', width: '128%', maxWidth: 'none',
            transform: 'translate(-50%,-50%)', opacity: 0.05, pointerEvents: 'none',
            filter: 'brightness(2.4) sepia(1) saturate(2) hue-rotate(-2deg)' }} />

          {/* inner gold keyline */}
          <div aria-hidden="true" style={{ position: 'absolute', inset: 11,
            border: `1px solid rgba(201,162,75,0.42)`, borderRadius: 15, pointerEvents: 'none' }} />
          <GoldCorner pos="tl" /><GoldCorner pos="tr" />
          <GoldCorner pos="bl" /><GoldCorner pos="br" />

          <div style={{ position: 'relative', zIndex: 2, display: 'flex', flexDirection: 'column', alignItems: 'center' }}>

            {/* emblem */}
            <div style={{ ...rise(0.05, 14), width: 'clamp(96px,26vw,128px)', marginBottom: 'clamp(8px,2vw,12px)' }}>
              <img src={A('assets/emblem2.png')} alt="Vyshnavan & Rathusaigini emblem" draggable="false"
              style={{ display: 'block', width: '100%', height: 'auto',
                filter: 'drop-shadow(0 3px 12px rgba(0,0,0,0.32)) drop-shadow(0 0 18px rgba(247,206,120,0.4))' }} />
            </div>

            {/* names — kept on a single line across all widths */}
            <h2 style={{ ...rise(0.12), margin: '0 0 clamp(10px,2.6vw,16px)',
              fontFamily: 'Marcellus, serif', color: GOLD_HI,
              fontSize: 'clamp(13px,3.6vw,23px)', letterSpacing: '0.05em', lineHeight: 1.1,
              whiteSpace: 'nowrap',
              textShadow: '0 1px 10px rgba(0,0,0,0.3)' }}>
              VYSHNAVAN <span style={{ color: GOLD, fontStyle: 'normal' }}>&amp;</span> RATHUSAIGINI
            </h2>

            <div style={{ ...rise(0.16), marginBottom: 'clamp(16px,4vw,26px)' }}>
              <BeadDivider width={232} reveal={inView ? 1 : 0} />
            </div>

            {/* heading */}
            <h3 style={{ ...rise(0.22), margin: 0, fontFamily: '"Cormorant Garamond", serif', fontWeight: 700,
              color: GOLD_HI, fontSize: 'clamp(34px,10vw,58px)', letterSpacing: '0.06em', lineHeight: 1.0,
              textTransform: 'uppercase',
              textShadow: '0 2px 16px rgba(0,0,0,0.34), 0 0 26px rgba(243,218,151,0.18)' }}>
              Your Presence
            </h3>

            {/* script subheading */}
            <p style={{ ...rise(0.3), margin: 'clamp(2px,1vw,6px) 0 0',
              fontFamily: '"Great Vibes", cursive', color: GOLD_LT,
              fontSize: 'clamp(30px,8.6vw,50px)', lineHeight: 1.05, letterSpacing: '0.01em',
              textShadow: '0 2px 14px rgba(0,0,0,0.34)' }}>
              means the world to us
            </p>

            <div style={{ ...rise(0.34), margin: 'clamp(16px,4vw,24px) 0 clamp(18px,4.4vw,26px)' }}>
              {OD ? <OD width={210} color={GOLD_LT} reveal={inView ? 1 : 0} /> : null}
            </div>

            {/* body */}
            <p style={{ ...rise(0.4), margin: 0, fontFamily: '"Cormorant Garamond", serif',
              color: IVORY, fontSize: 'clamp(17px,4.6vw,22px)', lineHeight: 1.5, letterSpacing: '0.01em',
              maxWidth: '26ch' }}>
              We would be truly blessed to celebrate our special day with you and your family.
            </p>

            <div style={{ ...rise(0.46), margin: 'clamp(20px,5vw,30px) 0 clamp(18px,4.4vw,26px)' }}>
              <HeartDivider width={196} reveal={inView ? 1 : 0} />
            </div>

            {/* ── RSVP CTA BUTTON → opens the merged /rsvp page (same tab) ── */}
            <a
              href={RSVP_URL}
              className="rsvp-cta"
              style={{ ...rise(0.54, 16), display: 'block', width: '100%', maxWidth: 360, textDecoration: 'none' }}>
              
              <span className="rsvp-cta__inner">
                <span className="rsvp-cta__shimmer" aria-hidden="true" />
                <CalendarIcon size={21} />
                <span className="rsvp-cta__label">RSVP&nbsp;NOW</span>
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"
                className="rsvp-cta__chev" style={{ flex: 'none' }}>
                  <path d="M9 5 L16 12 L9 19" stroke={GOLD_HI} strokeWidth="1.8"
                  strokeLinecap="round" strokeLinejoin="round" />
                </svg>
              </span>
            </a>

            {/* kindly line */}
            <p style={{ ...rise(0.6), margin: 'clamp(20px,5vw,28px) 0 0',
              fontFamily: '"Cormorant Garamond", serif', fontStyle: 'italic', color: CREAM,
              fontSize: 'clamp(15px,4vw,19px)', letterSpacing: '0.01em' }}>
              Kindly let us know if you can join us.
            </p>

            {/* deadline chip */}
            <div style={{ ...rise(0.66), marginTop: 'clamp(12px,3vw,16px)',
              display: 'inline-flex', alignItems: 'center', gap: 9,
              padding: 'clamp(8px,2.2vw,11px) clamp(16px,4vw,22px)',
              border: `1px solid rgba(201,162,75,0.55)`, borderRadius: 999,
              background: 'rgba(255,240,210,0.05)' }}>
              <CalendarIcon size={16} color={GOLD_LT} />
              <span style={{ fontFamily: 'Jost, sans-serif', color: GOLD_HI,
                fontSize: 'clamp(12px,3.2vw,14px)', letterSpacing: '0.04em' }}>
                Please RSVP by {RSVP_DEADLINE}
              </span>
            </div>
          </div>
        </div>

        {/* ── SECONDARY "IMPORTANT" INFO CARD ─────────────────────────── */}
        <div style={{ ...rise(0.74), position: 'relative', zIndex: 2,
          width: 'min(92vw, 470px)', marginTop: 'clamp(16px,4vw,22px)',
          display: 'flex', alignItems: 'center', gap: 'clamp(14px,3.6vw,18px)',
          padding: 'clamp(16px,4vw,20px) clamp(18px,4.6vw,24px)',
          background: 'linear-gradient(165deg, rgba(255,251,243,0.92), rgba(247,237,213,0.86))',
          border: `1px solid rgba(169,128,47,0.42)`, borderRadius: 14,
          boxShadow: '0 16px 34px -22px rgba(70,34,10,0.4)' }}>
          <div style={{ flex: 'none', width: 'clamp(42px,11vw,50px)', height: 'clamp(42px,11vw,50px)',
            borderRadius: '50%', display: 'flex', alignItems: 'center', justifyContent: 'center',
            background: 'radial-gradient(circle at 38% 30%, #fbf1d8, #f2e3c0)',
            border: `1px solid rgba(169,128,47,0.5)` }}>
            <svg width="26" height="26" viewBox="0 0 32 32" fill="none" aria-hidden="true">
              <g stroke={MAROON} strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
                <circle cx="11" cy="11" r="3.4" /><circle cx="21" cy="11" r="3.4" />
                <path d="M5 25 q0 -6 6 -6 q6 0 6 6 M15 25 q0 -6 6 -6 q6 0 6 6" />
              </g>
              <path d="M16 27.5 C 12.6 24.6 12.8 20.6 15.4 20.6 C 16 20.6 16 21.3 16 21.6 C 16 21.3 16 20.6 16.6 20.6 C 19.2 20.6 19.4 24.6 16 27.5 Z"
              fill={MAROON} opacity="0.9" />
            </svg>
          </div>
          <div style={{ textAlign: 'left' }}>
            <p style={{ margin: '0 0 3px', fontFamily: 'Marcellus, serif', color: MAROON,
              fontSize: 'clamp(13px,3.4vw,15px)', letterSpacing: '0.14em', textTransform: 'uppercase' }}>
              Important
            </p>
            <p style={{ margin: 0, fontFamily: '"Cormorant Garamond", serif', color: '#4a2a14',
              fontSize: 'clamp(14px,3.7vw,17px)', lineHeight: 1.42 }}>
              Your response helps us plan better and ensure a wonderful celebration for everyone.
            </p>
          </div>
        </div>

        {/* component-scoped button styles */}
        <style>{`
          .rsvp-cta { -webkit-tap-highlight-color: transparent; outline: none; }
          .rsvp-cta__inner {
            position: relative; display: flex; align-items: center; justify-content: center;
            gap: 12px; min-height: 60px;
            padding: 16px 22px; border-radius: 13px;
            background: linear-gradient(170deg, #8d2a2a 0%, #6e1f20 52%, #531616 100%);
            border: 1.6px solid ${GOLD};
            box-shadow: 0 0 0 4px rgba(255,247,228,0.10) inset,
                        0 10px 24px -12px rgba(20,4,4,0.7),
                        0 0 0 1px rgba(201,162,75,0.4);
            overflow: hidden;
            transition: transform .32s cubic-bezier(.22,.61,.36,1), box-shadow .32s ease, filter .32s ease;
          }
          .rsvp-cta__inner::after {
            content: ''; position: absolute; inset: 5px; border-radius: 9px;
            border: 1px solid rgba(227,193,114,0.5); pointer-events: none;
          }
          .rsvp-cta__label {
            font-family: "Cormorant Garamond", serif; font-weight: 700;
            color: ${GOLD_HI}; font-size: clamp(22px,6vw,30px);
            letter-spacing: 0.12em; text-transform: uppercase; line-height: 1;
            text-shadow: 0 1px 8px rgba(0,0,0,0.4);
          }
          .rsvp-cta__chev { transition: transform .32s ease; }
          .rsvp-cta__shimmer {
            position: absolute; top: -30%; left: -40%; width: 32%; height: 160%;
            background: linear-gradient(100deg, transparent, rgba(255,246,214,0.55), transparent);
            filter: blur(6px); transform: skewX(-12deg) translateX(-60px);
            opacity: 0; pointer-events: none;
          }
          .rsvp-cta:hover .rsvp-cta__inner,
          .rsvp-cta:focus-visible .rsvp-cta__inner {
            transform: translateY(-3px);
            filter: brightness(1.06);
            box-shadow: 0 0 0 4px rgba(255,247,228,0.14) inset,
                        0 18px 34px -12px rgba(20,4,4,0.75),
                        0 0 0 1px rgba(227,193,114,0.7),
                        0 0 28px rgba(227,193,114,0.5);
          }
          .rsvp-cta:hover .rsvp-cta__chev,
          .rsvp-cta:focus-visible .rsvp-cta__chev { transform: translateX(4px); }
          .rsvp-cta:hover .rsvp-cta__shimmer { animation: rsvpSweep 1s ease-out; }
          .rsvp-cta:active .rsvp-cta__inner { transform: translateY(-1px) scale(0.992); }
          @keyframes rsvpSweep {
            0% { opacity: 0; transform: skewX(-12deg) translateX(-60px); }
            18% { opacity: 1; }
            100% { opacity: 0; transform: skewX(-12deg) translateX(380px); }
          }
          @media (prefers-reduced-motion: reduce) {
            .rsvp-cta__inner, .rsvp-cta__chev { transition: none; }
            .rsvp-cta:hover .rsvp-cta__shimmer { animation: none; }
          }
        `}</style>
      </section>);

  }

  window.RSVPSection = RSVPSection;
})();