/* Hero — editorial magazine layout.
   Cream page, an inset looping video panel on the right, and the couple's
   names set large on the left, overlapping the top of the video. Label,
   date, place, and RSVP sit lower-left on the cream. Text/content unchanged;
   only the arrangement is new. */

const { useEffect: useHeroEffect, useRef: useHeroRef } = React;

function Hero() {
  const videoRef = useHeroRef(null);

  useHeroEffect(() => {
    const v = videoRef.current;
    if (!v) return;
    // React's `muted` prop is unreliable; force the DOM property so iOS/Safari
    // will honor autoplay. Also set the attribute for good measure.
    v.muted = true;
    v.defaultMuted = true;
    v.setAttribute('muted', '');
    const tryPlay = () => {
      const p = v.play();
      if (p && p.catch) p.catch(() => {});
    };
    // Force the element to (re)load its sources, then attempt playback.
    try { v.load(); } catch (e) {}
    if (v.readyState >= 2) tryPlay();else
    v.addEventListener('loadeddata', tryPlay, { once: true });
    v.addEventListener('canplay', tryPlay, { once: true });
    // Retry once the page becomes visible / on first touch anywhere / on tap of
    // the video itself (iOS autoplay + Low Power Mode fallbacks).
    const onVisible = () => { if (!document.hidden) tryPlay(); };
    document.addEventListener('visibilitychange', onVisible);
    window.addEventListener('touchstart', tryPlay, { once: true, passive: true });
    v.addEventListener('click', tryPlay);
    return () => {
      v.removeEventListener('loadeddata', tryPlay);
      v.removeEventListener('canplay', tryPlay);
      v.removeEventListener('click', tryPlay);
      document.removeEventListener('visibilitychange', onVisible);
      window.removeEventListener('touchstart', tryPlay);
    };
  }, []);

  return (
    <section id="top" data-screen-label="01 hero" className="hero-ed" style={{ backgroundColor: "rgb(254, 242, 228)" }}>

      {/* Inset looping video — right panel. Wrapper gives the RSVP call-out a
          positioning context so it sits in the video's corner on every size. */}
      <div className="hero-ed__videowrap">
        <video
          ref={videoRef}
          className="hero-ed__video"
          muted
          loop
          playsInline
          webkit-playsinline="true"
          preload="auto"
          autoPlay>
          <source src="assets/casa-fernanda-loop-mobile.mp4" type="video/mp4" />
          <source src="assets/casa-fernanda-loop-mobile.webm" type="video/webm" />
        </video>

        {/* Tilted RSVP call-out, bottom-right corner of the video */}
        <a className="hero-ed__rsvp" href="rsvp.html">
          <span className="hero-ed__rsvp-big">Join Us?</span>
          <span className="hero-ed__rsvp-small">rsvp here</span>
        </a>
      </div>

      {/* Names — large, left, overlapping the video */}
      <img
        className="hero-ed__names"
        src="assets/sydney-andres-names.png"
        alt="Sydney & Andrés" />

      {/* Caption over the video — label, date, place */}
      <div className="hero-ed__meta" style={{ textAlign: "center", height: "132px" }}>
        <div className="hero-ed__label" style={{ textAlign: "center" }}>you&rsquo;re invited</div>
        <div className="hero-ed__date" style={{ color: "rgb(65, 0, 22)", fontSize: "11px", height: "5px" }}>APRIL 30 - MAY 2</div>
        <div className="hero-ed__place" style={{ fontSize: "10px" }}>
          Tepoztlán<span className="hero-ed__sep" style={{ color: "rgb(65, 0, 22)", fontSize: "18px" }}>·</span>Morelos<span className="hero-ed__sep" style={{ color: "rgb(65, 0, 22)", fontSize: "18px" }}>·</span>México
        </div>
      </div>

      {/* Page numbers — 20 / 27 in the bottom corners */}
      <div className="hero-ed__page hero-ed__page--l" style={{ fontFamily: "RandomHandwritten", fontSize: "34px", color: "rgb(194, 172, 0)" }}>20</div>
      <div className="hero-ed__page hero-ed__page--r" style={{ fontFamily: "RandomHandwritten", fontSize: "34px", color: "rgb(194, 172, 0)" }}>27</div>

      <style>{`
        .hero-ed {
          position: relative;
          box-sizing: border-box;
          min-height: 100vh;
          background: var(--parchment);
          padding: 0;
          overflow: hidden;
        }
        .hero-ed__videowrap {
          position: absolute;
          top: clamp(14px, 2vh, 24px);
          right: 0;
          height: calc(100vh - 2 * clamp(14px, 2vh, 24px));
          width: 76%;
          z-index: 1;
        }
        .hero-ed__video {
          position: absolute;
          inset: 0;
          height: 100%;
          width: 100%;
          object-fit: cover;
          object-position: 100% 50%;
          background: var(--night-bordeaux);
          pointer-events: none;
        }
        .hero-ed__video::-webkit-media-controls,
        .hero-ed__video::-webkit-media-controls-enclosure,
        .hero-ed__video::-webkit-media-controls-panel { display: none !important; }
        .hero-ed__rsvp {
          position: absolute;
          right: clamp(28px, 4vw, 76px);
          bottom: clamp(48px, 8vh, 110px);
          z-index: 3;
          display: flex;
          flex-direction: column;
          align-items: center;
          gap: 2px;
          text-decoration: none;
          font-family: "LesEnfantsDuParadis";
          color: #E84817;
          transform: rotate(-22.5deg);
          transform-origin: center;
          pointer-events: auto;
          cursor: pointer;
          transition: transform 320ms var(--ease-walk), filter 320ms var(--ease-walk);
        }
        .hero-ed__rsvp:hover { transform: rotate(-22.5deg) scale(1.06); filter: brightness(1.08); }
        .hero-ed__rsvp-big { font-size: clamp(54px, 5.4vw, 82px); line-height: 0.9; }
        .hero-ed__rsvp-small { font-size: clamp(30px, 2.8vw, 42px); line-height: 1; }
        .hero-ed__names {
          position: absolute;
          left: var(--gutter);
          top: clamp(170px, 22vh, 280px);
          transform: translateY(-14px);
          margin: 0;
          z-index: 2;
          pointer-events: none;
          width: clamp(420px, 62vw, 940px);
          height: auto;
          filter: drop-shadow(0 2px 30px rgba(65, 0, 22, 0.20));
        }
        .hero-ed__meta {
          position: absolute;
          left: 0;
          width: 24%;
          bottom: clamp(42px, 7.5vh, 90px);
          z-index: 3;
          display: flex;
          flex-direction: column;
          align-items: center;
          text-align: center;
        }
        .hero-ed__label {
          font-family: var(--font-body);
          font-weight: 600;
          font-size: 13px;
          letter-spacing: 0.28em;
          text-transform: uppercase;
          color: var(--night-bordeaux);
          margin-bottom: 10px;
        }
        .hero-ed__date {
          font-family: var(--font-body);
          font-weight: 600;
          font-size: 13px;
          letter-spacing: 0.28em;
          text-transform: uppercase;
          line-height: 1.5;
          color: color-mix(in srgb, var(--ink) 78%, transparent);
          margin-bottom: 8px;
        }
        .hero-ed__place {
          font-family: var(--font-body);
          font-weight: 600;
          font-size: 13px;
          letter-spacing: 0.24em;
          text-transform: uppercase;
          color: var(--night-bordeaux);
          margin-bottom: 0;
        }
        .hero-ed__place-line { display: block; line-height: 1.55; }
        .hero-ed__sep { color: var(--golden-glow); margin: 0 0.6em; position: relative; top: 0.12em; }
        .hero-ed__page {
          position: absolute;
          bottom: clamp(-2px, 0.4vh, 4px);
          z-index: 4;
          font-family: var(--font-body);
          font-weight: 600;
          font-size: 16px;
          letter-spacing: 0.12em;
        }
        .hero-ed__page--l {
          left: clamp(12px, 2vw, 28px);
          color: var(--night-bordeaux);
        }
        .hero-ed__page--r {
          right: calc(76% + clamp(12px, 2vw, 28px));
          color: var(--night-bordeaux);
        }

        @media (max-width: 880px) {
          .hero-ed {
            display: flex;
            flex-direction: column;
            min-height: 0;
            padding-top: calc(var(--gutter) + 72px);
          }
          .hero-ed__names {
            position: relative;
            left: 0;
            z-index: 3;
            order: 1;
            width: 100%;
            max-width: 100%;
            height: auto;
            margin-bottom: -34px;
            transform: translateY(-75%);
            pointer-events: none;
          }
          .hero-ed__videowrap {
            position: relative;
            order: 2;
            width: 100%;
            height: 46vh;
            min-height: 280px;
            margin-bottom: 28px;
          }
          .hero-ed__video {
            position: absolute;
            inset: 0;
            object-position: 50% 0%;
          }
          .hero-ed__rsvp {
            right: clamp(14px, 4vw, 28px);
            bottom: clamp(14px, 3vh, 28px);
          }
          .hero-ed__rsvp-big { font-size: clamp(40px, 13vw, 60px); }
          .hero-ed__rsvp-small { font-size: clamp(22px, 7vw, 32px); }
          .hero-ed__meta {
            position: static;
            order: 3;
            width: 100%;
            left: auto;
          }
          .hero-ed__page {
            bottom: clamp(10px, 2.5vh, 22px);
            font-size: 22px;
          }
          .hero-ed__page--l { left: clamp(14px, 4vw, 26px); right: auto; }
          .hero-ed__page--r { right: clamp(14px, 4vw, 26px); left: auto; }
        }
      `}</style>
    </section>);

}

window.Hero = Hero;