/* global React, INK, CREAM, PULSE, GENS, Sparkle, GradText, CTAButton,
   Reveal, useReveal, useTyping, SectionHead, ControlPill, Shimmer, SectionShell, SB_EASE */
// § 03 Direct a shoot · § 04 Clone any reel

const { useState: srS, useEffect: srE } = React;

// ---------- § 03 — Photoshoot ----------
const SHOOT = [
  { src: 'assets/shoot/set-1.webp', scene: 'Lazy morning · selfie' },
  { src: 'assets/shoot/set-2.webp', scene: 'Lazy morning · reclined' },
  { src: 'assets/shoot/set-3.webp', scene: 'Lazy morning · to camera' },
  { src: 'assets/shoot/set-4.webp', scene: 'Lazy morning · stretch' },
  { src: 'assets/shoot/set-5.webp', scene: 'Lazy morning · propped' },
  { src: 'assets/shoot/set-6.webp', scene: 'Lazy morning · soft light' },
];

const Bracket = ({ c = '#fff' }) => (
  <React.Fragment>
    {[['top','left'],['top','right'],['bottom','left'],['bottom','right']].map(([v,h],i)=>(
      <span key={i} style={{ position:'absolute', [v]:6, [h]:6, width:12, height:12,
        borderTop: v==='top'?`2px solid ${c}`:'none', borderBottom: v==='bottom'?`2px solid ${c}`:'none',
        borderLeft: h==='left'?`2px solid ${c}`:'none', borderRight: h==='right'?`2px solid ${c}`:'none' }} />
    ))}
  </React.Fragment>
);

const SegChip = ({ children, active, locked }) => (
  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, borderRadius: 9999, padding: '8px 14px',
    fontSize: 12, fontWeight: 600, whiteSpace: 'nowrap',
    background: active ? INK : 'rgb(246,243,242)', color: active ? '#fff' : 'rgba(28,27,27,0.7)',
    boxShadow: active ? 'none' : 'inset 0 0 0 1px rgba(28,27,27,0.08)' }}>
    {children}
    {locked && <svg width="11" height="11" viewBox="0 0 16 16" fill="none"><rect x="3.5" y="7" width="9" height="6.5" rx="1.5" stroke="currentColor" strokeWidth="1.5"/><path d="M5.5 7V5.5a2.5 2.5 0 0 1 5 0V7" stroke="currentColor" strokeWidth="1.5"/></svg>}
  </span>
);

const ShootSection = () => {
  const [gridRef, on] = useReveal(0.3);
  return (
    <SectionShell>
      <CopyHead row k="shoot" />
      <div style={{ display: 'grid', gridTemplateColumns: '0.92fr 1.08fr', gap: 64, alignItems: 'center', marginTop: 52 }}>
        <div>
          <Reveal delay={240}>
            <div style={{ borderRadius: 16, background: '#fff', boxShadow: '0 4px 20px rgba(28,27,27,0.08), inset 0 0 0 1px rgba(28,27,27,0.08)', padding: 18 }}>
              <div style={{ display: 'flex', gap: 14, alignItems: 'center', marginBottom: 14 }}>
                <div style={{ position: 'relative', width: 64, height: 80, borderRadius: 10, overflow: 'hidden', flexShrink: 0 }}>
                  <img loading="lazy" decoding="async" src={SHOOT[0].src} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center 20%' }} />
                  <Bracket />
                </div>
                <div>
                  <div style={{ fontSize: 13, fontWeight: 700, color: INK, marginBottom: 4 }}>The brief</div>
                  <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, fontSize: 10, fontWeight: 700, letterSpacing: '0.08em',
                    textTransform: 'uppercase', color: 'var(--brand-purple-700)', background: 'var(--brand-purple-50)',
                    border: '1px solid var(--brand-purple-200)', borderRadius: 9999, padding: '4px 10px' }}>
                    <span style={{ width: 6, height: 6, borderRadius: '50%', background: 'var(--brand-purple-500)', animation: 'sz-pulse-dot 1.4s ease-in-out infinite' }} />
                    Identity locked
                  </span>
                </div>
              </div>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 7, marginBottom: 14 }}>
                {[
                  { n: 'Social Media', sub: 'All-SFW · feed-ready', b: 'SFW', bc: '#22C55E', on: true, desc: 'Angles, poses, expressions, outfit shifts. Same person, same room.', locks: ['Identity', 'Environment'] },
                  { n: 'Sequenced Set', sub: 'SFW → NSFW · arc', b: 'ARC', bc: 'var(--brand-purple-600)' },
                  { n: 'NSFW Set', sub: 'All-NSFW · same world', b: 'NSFW', bc: INK, lock: true },
                ].map(t => (
                  <div key={t.n} style={{ display: 'flex', gap: 11, padding: '11px 13px', borderRadius: 13, textAlign: 'left',
                    background: t.on ? '#fff' : 'transparent',
                    border: t.on ? '1px solid rgba(28,27,27,0.14)' : '1px solid rgba(28,27,27,0.07)',
                    boxShadow: t.on ? '0 4px 14px rgba(28,27,27,0.06)' : 'none', cursor: 'pointer' }}>
                    <span style={{ width: 16, height: 16, borderRadius: 9999, marginTop: 1, flexShrink: 0, background: '#fff',
                      border: t.on ? '5px solid var(--brand-purple-600)' : '1.5px solid rgba(28,27,27,0.3)', boxSizing: 'border-box' }} />
                    <div style={{ flex: 1, minWidth: 0 }}>
                      <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', gap: 8 }}>
                        <span style={{ fontSize: 13, fontWeight: 700, color: INK }}>{t.n}</span>
                        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, fontSize: 9, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase', color: t.bc }}>
                          {t.b}
                          {t.lock && <svg width="10" height="10" viewBox="0 0 16 16" fill="none"><rect x="3.5" y="7" width="9" height="6.5" rx="1.5" stroke="currentColor" strokeWidth="1.8"/><path d="M5.5 7V5.5a2.5 2.5 0 0 1 5 0V7" stroke="currentColor" strokeWidth="1.8"/></svg>}
                        </span>
                      </div>
                      <div style={{ fontSize: 11, color: 'var(--text-tertiary)', marginTop: 2 }}>{t.sub}</div>
                      {t.on && <div style={{ fontSize: 11.5, color: 'var(--text-secondary)', marginTop: 5, lineHeight: 1.45 }}>{t.desc}</div>}
                      {t.on && (
                        <div style={{ display: 'flex', gap: 4, marginTop: 7 }}>
                          {t.locks.map(l => (
                            <span key={l} style={{ display: 'inline-flex', alignItems: 'center', gap: 4, fontSize: 10, fontWeight: 600, color: 'var(--text-secondary)',
                              background: 'rgb(246,243,242)', borderRadius: 9999, padding: '3px 9px' }}>
                              <svg width="9" height="9" viewBox="0 0 16 16" fill="none"><rect x="3.5" y="7" width="9" height="6.5" rx="1.5" stroke="currentColor" strokeWidth="1.8"/><path d="M5.5 7V5.5a2.5 2.5 0 0 1 5 0V7" stroke="currentColor" strokeWidth="1.8"/></svg>
                              {l}
                            </span>
                          ))}
                        </div>
                      )}
                    </div>
                  </div>
                ))}
              </div>
              <div style={{ display: 'flex', alignItems: 'center', gap: 5, marginBottom: 14 }}>
                <span style={{ fontSize: 10.5, fontWeight: 700, letterSpacing: '0.08em', textTransform: 'uppercase', color: 'var(--text-tertiary)', marginRight: 4 }}>Set size</span>
                {[3, 4, 5, 6, 7, 8].map(n => (
                  <span key={n} style={{ width: 30, height: 30, borderRadius: 9999, display: 'grid', placeItems: 'center', fontSize: 12.5, fontWeight: 700, cursor: 'pointer',
                    background: n === 6 ? INK : 'rgb(246,243,242)', color: n === 6 ? '#fff' : 'rgba(28,27,27,0.65)' }}>{n}</span>
                ))}
                <span style={{ fontSize: 11, color: 'var(--text-tertiary)', marginLeft: 4 }}>… 10</span>
              </div>
            </div>
          </Reveal>
        </div>
        {/* output set — frames develop in sequence */}
        <div ref={gridRef} style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 12 }}>
          {SHOOT.map((g, i) => (
            <div key={i} style={{ position: 'relative', aspectRatio: '4 / 5', borderRadius: 14, overflow: 'hidden', background: 'rgb(235,231,231)',
              opacity: on ? 1 : 0, transform: on ? 'none' : 'translateY(26px) scale(0.96)',
              transition: `opacity 0.65s ${SB_EASE} ${i * 150}ms, transform 0.65s ${SB_EASE} ${i * 150}ms` }}>
              <img loading="lazy" decoding="async" src={g.src} alt={g.scene} style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center 22%',
                filter: on ? 'none' : 'blur(12px)', transition: `filter 0.9s ${SB_EASE} ${200 + i * 150}ms` }} />
              <span style={{ position: 'absolute', top: 8, left: 8, fontSize: 9.5, fontWeight: 700, color: '#fff',
                background: 'rgba(28,27,27,0.5)', backdropFilter: 'blur(6px)', borderRadius: 9999, padding: '3px 8px' }}>{i + 1} / 6</span>
            </div>
          ))}
        </div>
      </div>
    </SectionShell>
  );
};

// ---------- § 04 — Clone any reel ----------
const REEL_URL = 'instagram.com/reel/xK92hf4…';

const MiniPhone = ({ children, label, w = 210 }) => (
  <div style={{ width: w }}>
    <div style={{ position: 'relative', width: '100%', aspectRatio: '9 / 16', borderRadius: 22, overflow: 'hidden',
      background: '#0d0a12', boxShadow: '0 24px 48px -10px rgba(0,0,0,0.55), inset 0 0 0 1px rgba(255,255,255,0.12)' }}>
      {children}
    </div>
    <div style={{ textAlign: 'center', marginTop: 12, fontSize: 11, fontWeight: 700, letterSpacing: '0.1em', textTransform: 'uppercase', color: 'rgba(255,255,255,0.55)' }}>{label}</div>
  </div>
);

const ReelSection = () => {
  const [ref, on] = useReveal(0.35);
  const [muted, setMuted] = srS(true);
  const [cMuted, setCMuted] = srS(true);
  const cloneRef = React.useRef(null);
  const toggleCMute = () => { const v = cloneRef.current; if (v) v.muted = cMuted ? false : true; setCMuted(m => !m); };
  const vidRef = React.useRef(null);
  const toggleMute = () => { const v = vidRef.current; if (v) v.muted = muted ? false : true; setMuted(m => !m); };
  // pause both videos while offscreen — keeps decode off the main thread during scroll
  srE(() => {
    const vids = [vidRef.current, cloneRef.current].filter(Boolean);
    vids.forEach(v => { v.muted = true; v.volume = 1; });
    if (!vids.length) return;
    const io = new IntersectionObserver((es) => es.forEach(e => {
      const v = e.target;
      if (e.isIntersecting) { const p = v.play(); if (p && p.catch) p.catch(() => {}); } else v.pause();
    }), { threshold: 0.05 });
    vids.forEach(v => io.observe(v));
    return () => io.disconnect();
  }, []);
  const [typed, doneTyping] = useTyping(REEL_URL, on, 26, 500);
  const [phase, setPhase] = srS('idle'); // idle → cloning → done
  srE(() => {
    if (doneTyping && phase === 'idle') { const t = setTimeout(() => setPhase('cloning'), 500); return () => clearTimeout(t); }
    if (phase === 'cloning') { const t = setTimeout(() => setPhase('done'), 1800); return () => clearTimeout(t); }
  }, [doneTyping, phase]);
  return (
    <SectionShell dark>
      <div ref={ref}>
        <CopyHead dark row k="reel" />
        {/* link bar */}
        <Reveal delay={160}>
          <div style={{ maxWidth: 560, margin: '44px auto 0', display: 'flex', gap: 12 }}>
            <div style={{ flex: 1, borderRadius: 14, background: 'rgba(255,255,255,0.07)', border: '1px solid rgba(255,255,255,0.16)',
              display: 'flex', alignItems: 'center', gap: 10, padding: '0 16px', height: 52 }}>
              <svg width="15" height="15" viewBox="0 0 16 16" fill="none"><path d="M6.5 9.5a3 3 0 0 0 4.2 0l2.4-2.4a3 3 0 1 0-4.2-4.2l-1 1M9.5 6.5a3 3 0 0 0-4.2 0L2.9 8.9a3 3 0 1 0 4.2 4.2l1-1" stroke="rgba(255,255,255,0.6)" strokeWidth="1.5" strokeLinecap="round"/></svg>
              <span style={{ fontSize: 13.5, color: typed ? '#fff' : 'rgba(255,255,255,0.45)', fontWeight: 500 }}>
                {typed || 'Paste a reel link'}
                {on && !doneTyping && <span style={{ display: 'inline-block', width: 2, height: 15, background: '#A675FF', marginLeft: 2, verticalAlign: 'middle', animation: 'sz-blink 1s step-end infinite' }} />}
              </span>
            </div>
            <span style={{ display: 'inline-flex', alignItems: 'center', gap: 8, borderRadius: 14, padding: '0 20px', height: 52,
              background: doneTyping ? PULSE : 'rgba(255,255,255,0.1)', transition: `background 0.4s ${SB_EASE}`,
              boxShadow: doneTyping ? '0 8px 24px -6px rgba(129,60,237,0.6)' : 'none',
              fontSize: 13, fontWeight: 700, letterSpacing: '0.05em', textTransform: 'uppercase', color: '#fff', cursor: 'pointer' }}>
              {phase === 'cloning' ? 'Cloning…' : 'Clone it'}
            </span>
          </div>
        </Reveal>
        {/* phones */}
        <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', gap: 44, marginTop: 52 }}>
          <Reveal delay={220}>
            <MiniPhone label="The reference">
              <video ref={vidRef} src="assets/reel-reference.mp4" autoPlay muted loop playsInline preload="metadata"
                style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover' }} />
              <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, height: 60, background: 'linear-gradient(to top, rgba(13,10,18,0.75), transparent)' }} />
              <span style={{ position: 'absolute', bottom: 12, left: 12, right: 34, fontSize: 10, color: 'rgba(255,255,255,0.75)', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{REEL_URL}</span>
              <span onClick={toggleMute} style={{ position: 'absolute', right: 8, bottom: 8, width: 26, height: 26, borderRadius: '50%', cursor: 'pointer',
                background: 'rgba(20,16,23,0.6)', backdropFilter: 'blur(6px)', display: 'grid', placeItems: 'center', border: '1px solid rgba(255,255,255,0.25)' }}>
                {muted
                  ? <svg width="12" height="12" viewBox="0 0 16 16" fill="none"><path d="M2.5 6v4h2.5L8.5 13V3L5 6H2.5z" fill="#fff"/><path d="M10.5 6l4 4M14.5 6l-4 4" stroke="#fff" strokeWidth="1.5" strokeLinecap="round"/></svg>
                  : <svg width="12" height="12" viewBox="0 0 16 16" fill="none"><path d="M2.5 6v4h2.5L8.5 13V3L5 6H2.5z" fill="#fff"/><path d="M10.5 5.5a3.5 3.5 0 0 1 0 5M12 3.5a6 6 0 0 1 0 9" stroke="#fff" strokeWidth="1.4" strokeLinecap="round"/></svg>}
              </span>
            </MiniPhone>
          </Reveal>
          {/* connector */}
          <Reveal delay={320}>
            <svg width="86" height="40" viewBox="0 0 86 40" style={{ overflow: 'visible' }}>
              <line x1="4" y1="20" x2="74" y2="20" stroke="#A675FF" strokeWidth="2" strokeDasharray="3 7" strokeLinecap="round" style={{ animation: 'sz-dash 0.7s linear infinite' }} />
              <path d="M72 13l9 7-9 7" fill="none" stroke="#A675FF" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
            </svg>
          </Reveal>
          <Reveal delay={380}>
            <MiniPhone label="Your clone">
              {/* plays from the start, in step with the reference — no cloning shimmer */}
              <video ref={cloneRef} src="assets/reel-clone.mp4" autoPlay muted loop playsInline preload="metadata"
                style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover' }} />
              <React.Fragment>
                  <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(to top, rgba(0,0,0,0.55), transparent 40%)' }} />
                  <span style={{ position: 'absolute', top: 10, left: 10, display: 'inline-flex', alignItems: 'center', gap: 5,
                    background: 'rgba(18,15,24,0.55)', backdropFilter: 'blur(6px)', borderRadius: 9999, padding: '4px 9px',
                    animation: 'sz-like-pop 0.4s var(--ease-out)' }}>
                    <span style={{ width: 6, height: 6, borderRadius: '50%', background: '#A675FF', animation: 'sz-pulse-dot 1.3s ease-in-out infinite' }} />
                    <span style={{ fontSize: 9, fontWeight: 700, letterSpacing: '0.06em', textTransform: 'uppercase', color: '#fff' }}>Your likeness</span>
                  </span>
                  <div style={{ position: 'absolute', bottom: 14, left: 12, fontSize: 10.5, fontWeight: 700, color: '#fff' }}>Same motion. New star.</div>
                  <span onClick={toggleCMute} style={{ position: 'absolute', right: 8, bottom: 8, width: 26, height: 26, borderRadius: '50%', cursor: 'pointer',
                    background: 'rgba(20,16,23,0.6)', backdropFilter: 'blur(6px)', display: 'grid', placeItems: 'center', border: '1px solid rgba(255,255,255,0.25)' }}>
                    {cMuted
                      ? <svg width="12" height="12" viewBox="0 0 16 16" fill="none"><path d="M2.5 6v4h2.5L8.5 13V3L5 6H2.5z" fill="#fff"/><path d="M10.5 6l4 4M14.5 6l-4 4" stroke="#fff" strokeWidth="1.5" strokeLinecap="round"/></svg>
                      : <svg width="12" height="12" viewBox="0 0 16 16" fill="none"><path d="M2.5 6v4h2.5L8.5 13V3L5 6H2.5z" fill="#fff"/><path d="M10.5 5.5a3.5 3.5 0 0 1 0 5M12 3.5a6 6 0 0 1 0 9" stroke="#fff" strokeWidth="1.4" strokeLinecap="round"/></svg>}
                  </span>
                </React.Fragment>
            </MiniPhone>
          </Reveal>
        </div>
      </div>
    </SectionShell>
  );
};

Object.assign(window, { ShootSection, ReelSection });
