// ZION Story — "Meet the Founder" video page.
// Levi's 2:53 founder film tells the whole story, so the page is built around it:
// a title, the film, then a CTA back into the shop. (Video sourced from
// thefireballguy.com → assets/founder-story.mp4, web-optimized with faststart.)

// Founder film: autoplays muted + looping, baked into the page with a glowing
// orange frame. Click it → "cinema mode": it enlarges to center with sound, and
// the rest of the site dims + blurs behind it. Click outside / ✕ / Esc to exit.
function FounderVideo() {
  const SRC = 'assets/founder-story.mp4';
  const POSTER = 'assets/founder-story-poster.jpg';
  const [focused, setFocused] = React.useState(false);
  const vref = React.useRef(null);

  // Autoplay muted on mount (muted attr set imperatively — most reliable).
  React.useEffect(() => {
    const v = vref.current;
    if (v) { v.muted = true; v.play().catch(() => {}); }
  }, []);

  const open = () => {
    const v = vref.current;
    // Unmute inside the click gesture so the browser allows sound.
    if (v) { v.muted = false; v.loop = false; v.play().catch(() => {}); }
    setFocused(true);
    document.body.style.overflow = 'hidden';
  };
  const close = () => {
    const v = vref.current;
    if (v) { v.muted = true; v.loop = true; }
    setFocused(false);
    document.body.style.overflow = '';
  };

  // Esc closes; keep muted state in sync with focus.
  React.useEffect(() => {
    const v = vref.current;
    if (v) v.muted = !focused;
    if (!focused) return;
    const onKey = (e) => { if (e.key === 'Escape') close(); };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [focused]);

  return (
    <div style={{ maxWidth: 720, margin: '0 auto 56px' }}>
      <div style={{ position: 'relative', aspectRatio: '16 / 9' }}>
      {/* Dim + blur backdrop over the rest of the site */}
      <div onClick={close} style={{
        position: 'fixed', inset: 0, zIndex: 90,
        background: 'rgba(6,4,2,0.72)',
        backdropFilter: 'blur(18px)', WebkitBackdropFilter: 'blur(18px)',
        opacity: focused ? 1 : 0, visibility: focused ? 'visible' : 'hidden',
        pointerEvents: focused ? 'auto' : 'none',
        transition: 'opacity 380ms ease, visibility 380ms', cursor: 'zoom-out',
      }} />

      {/* Video card — inline by default, fixed + centered + enlarged when focused */}
      <div
        onClick={() => { if (!focused) open(); }}
        className="zion-founder-frame"
        style={{
          position: focused ? 'fixed' : 'absolute',
          top: focused ? '50%' : 0,
          left: focused ? '50%' : 0,
          right: focused ? 'auto' : 0,
          bottom: focused ? 'auto' : 0,
          width: focused ? 'min(92vw, 1100px)' : 'auto',
          height: focused ? 'auto' : 'auto',
          maxHeight: focused ? '86vh' : 'none',
          aspectRatio: '16 / 9',
          transform: focused ? 'translate(-50%, -50%)' : 'none',
          zIndex: focused ? 100 : 1,
          overflow: 'hidden', borderRadius: 16,
          cursor: focused ? 'default' : 'zoom-in',
          transition: 'transform 200ms ease',
        }}>
        <video
          ref={vref}
          src={SRC}
          poster={POSTER}
          autoPlay
          muted
          loop
          playsInline
          preload="metadata"
          controls={focused}
          style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block', background: '#000' }}
        />

        {/* "Tap for sound" hint — only on the muted inline preview */}
        {!focused && (
          <>
            <div aria-hidden style={{
              position: 'absolute', inset: 0, pointerEvents: 'none',
              background: 'linear-gradient(180deg, rgba(0,0,0,0) 58%, rgba(0,0,0,0.4) 100%)',
            }} />
            {/* Tap-for-sound hint — bottom-left */}
            <div style={{
              position: 'absolute', bottom: 14, left: 14, display: 'flex', alignItems: 'center', gap: 8,
              fontFamily: 'Poppins', fontWeight: 700, fontSize: 12, letterSpacing: '0.03em', color: '#fff',
              background: 'rgba(0,0,0,0.5)', padding: '8px 14px', borderRadius: 999,
              border: '1px solid rgba(255,255,255,0.25)', pointerEvents: 'none',
            }}>
              <span style={{ fontSize: 13, lineHeight: 1 }}>►</span> Tap for sound
            </div>
          </>
        )}
      </div>

      {/* Close button (cinema mode) */}
      {focused && (
        <button onClick={close} aria-label="Close" style={{
          position: 'fixed', top: 20, right: 24, zIndex: 101,
          width: 46, height: 46, borderRadius: '50%',
          background: 'rgba(0,0,0,0.5)', border: '1px solid rgba(255,255,255,0.3)', color: '#fff',
          fontFamily: 'Poppins', fontWeight: 800, fontSize: 20, cursor: 'pointer',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>✕</button>
      )}
      </div>
    </div>
  );
}

function Story({ onBack, onNavigate }) {
  return (
    <main style={{ background: '#0A0A0A', color: '#fff', overflow: 'hidden', position: 'relative' }}>
      <section style={{
        maxWidth: 980, margin: '0 auto', padding: '64px 24px 110px',
        textAlign: 'center', position: 'relative', zIndex: 1,
      }}>
        <a onClick={onBack} style={{
          cursor: 'pointer', fontFamily: 'Poppins', fontWeight: 700, fontSize: 12,
          letterSpacing: '0.14em', textTransform: 'uppercase', color: '#F7591F',
          display: 'inline-flex', alignItems: 'center', gap: 8, marginBottom: 44,
        }}>← BACK</a>

        {/* ---- Title (relative so the signature can sit beside LEVI ZION) ---- */}
        <div style={{ position: 'relative' }}>
          <div style={{
            fontFamily: 'var(--font-script)', fontStyle: 'italic',
            fontSize: 'clamp(32px, 4.6vw, 60px)', color: '#F7591F', lineHeight: 0.9,
            transform: 'rotate(-3deg)', marginBottom: 4,
          }}>Meet the Founder</div>
          <h1 style={{
            fontFamily: 'Poppins, sans-serif', fontWeight: 900, fontStyle: 'italic',
            textTransform: 'uppercase', letterSpacing: '-0.02em',
            fontSize: 'clamp(54px, 11vw, 134px)', lineHeight: 0.95, margin: '0 0 40px', color: '#fff',
          }}>Levi Zion<span style={{ color: '#F7591F' }}>.</span></h1>

          {/* Cursive brand signature — stacked "The" over "Fireball Guy" */}
          <div style={{
            position: 'absolute', right: 'clamp(-72px, -4.5vw, -30px)', top: 'clamp(-4px, 0.6vw, 12px)',
            pointerEvents: 'none', textAlign: 'center', lineHeight: 0.82,
            fontFamily: 'var(--font-script)', fontStyle: 'italic', color: '#F7591F',
            transform: 'rotate(7deg)', textShadow: '0 0 22px rgba(247,89,31,0.4)',
          }}>
            <div style={{ fontSize: 'clamp(19px, 2.4vw, 33px)' }}>The</div>
            <div style={{ fontSize: 'clamp(26px, 3.6vw, 52px)', marginTop: -9 }}>Fireball Guy</div>
          </div>
        </div>

        {/* ---- Founder film (interactive cinema mode) ---- */}
        <FounderVideo />

        {/* ---- CTA ---- */}
        <div style={{
          fontFamily: 'Poppins, sans-serif', fontWeight: 800, fontStyle: 'italic',
          textTransform: 'uppercase', fontSize: 'clamp(22px, 3.2vw, 38px)', lineHeight: 1,
          color: '#fff', letterSpacing: '-0.01em',
        }}>Ready to ignite your</div>
        <div style={{
          fontFamily: 'var(--font-script)', fontStyle: 'italic',
          fontSize: 'clamp(42px, 6.5vw, 82px)', lineHeight: 1, color: '#F7591F', margin: '2px 0 28px',
        }}>Game?</div>

        <button onClick={() => onNavigate && onNavigate('shop')} style={{
          background: '#F7591F', color: '#fff', border: 'none', cursor: 'pointer',
          fontFamily: 'Poppins', fontWeight: 800, fontSize: 14, letterSpacing: '0.04em',
          padding: '16px 40px', borderRadius: 999,
          boxShadow: '0 12px 30px rgba(247,89,31,0.42)',
        }}>Get your ball today!</button>
      </section>
    </main>
  );
}
window.Story = Story;
