// Hero — giant type, real screenshot floating w/ parallax, scroll-out reveal.

function Hero() {
  const shotRef = React.useRef(null);
  const heroRef = React.useRef(null);
  const [scrollY, setScrollY] = React.useState(0);
  const [mouse, setMouse] = React.useState({ x: 0, y: 0 });

  React.useEffect(() => {
    const h = () => setScrollY(window.scrollY);
    window.addEventListener('scroll', h, { passive: true });
    return () => window.removeEventListener('scroll', h);
  }, []);

  // Subtle 3D tilt toward mouse
  React.useEffect(() => {
    const h = (e) => {
      if (!heroRef.current) return;
      const r = heroRef.current.getBoundingClientRect();
      const x = (e.clientX - r.left - r.width / 2) / r.width;
      const y = (e.clientY - r.top - r.height / 2) / r.height;
      setMouse({ x, y });
    };
    window.addEventListener('mousemove', h);
    return () => window.removeEventListener('mousemove', h);
  }, []);

  const parallax = Math.min(scrollY * 0.25, 200);
  const heroFade = Math.max(0, 1 - scrollY / 600);

  return (
    <section ref={heroRef} style={{
      position:'relative',
      padding:'160px 40px 0',
      minHeight: 1100,
      overflow:'hidden',
    }}>
      {/* grain texture */}
      <div style={{
        position:'absolute', inset:0, pointerEvents:'none', zIndex:0,
        background: `radial-gradient(ellipse at 50% 20%, rgba(80,70,229,.08), transparent 60%)`,
      }}/>
      {/* subtle top grid */}
      <div style={{
        position:'absolute', inset:'0 0 auto 0', height:400, pointerEvents:'none', zIndex:0,
        backgroundImage: `linear-gradient(rgba(15,14,20,.04) 1px, transparent 1px), linear-gradient(90deg, rgba(15,14,20,.04) 1px, transparent 1px)`,
        backgroundSize:'64px 64px',
        maskImage:'linear-gradient(180deg, rgba(0,0,0,.5), transparent 80%)',
        WebkitMaskImage:'linear-gradient(180deg, rgba(0,0,0,.5), transparent 80%)',
      }}/>

      <div style={{ position:'relative', zIndex:2, maxWidth:1140, margin:'0 auto', textAlign:'center', opacity: heroFade }}>
        {/* badge */}
        <div style={{
          display:'inline-flex', alignItems:'center', gap:9,
          padding:'5px 14px 5px 7px', borderRadius:999,
          background:'rgba(255,255,255,.65)',
          border:'1px solid rgba(15,14,20,.08)',
          boxShadow:'0 1px 0 rgba(255,255,255,.8) inset, 0 1px 2px rgba(15,14,20,.04)',
          fontSize:12, color:'rgba(15,14,20,.72)',
          fontFamily:"'Geist Mono', monospace", letterSpacing:'-.01em',
          marginBottom:32,
        }}>
          <span style={{
            width:20, height:20, borderRadius:'50%',
            background:`${ACCENT}1a`, color:ACCENT,
            display:'inline-flex', alignItems:'center', justifyContent:'center',
            fontSize:10,
          }}>✦</span>
          v1.0 · now on the Mac App Store
        </div>

        {/* headline */}
        <h1 style={{
          fontSize: 'clamp(28px, 7.5vw, 116px)',
          lineHeight: .95,
          letterSpacing:'-.045em',
          margin:'0 0 32px',
          fontWeight: 500,
        }}>
          Your keys. Your Mac.<br/>
          Zero&nbsp;<span style={{
            fontFamily:"'Instrument Serif', serif",
            fontStyle:'italic',
            fontWeight:400,
            position:'relative',
            paddingRight: '.08em',
            whiteSpace:'nowrap',
          }}>
            compromise
            <svg viewBox="0 0 320 40" width="1.1em" height=".28em" style={{
              position:'absolute', left:0, bottom:'-.08em',
              width:'100%', height:'auto',
            }}>
              <path d="M5 22 C 80 8, 160 8, 315 18" stroke={ACCENT} strokeWidth="3" fill="none" strokeLinecap="round" style={{
                strokeDasharray: 400, strokeDashoffset: 400,
                animation:'drawLine 1.4s .6s cubic-bezier(.2,.7,.3,1) forwards',
              }}/>
            </svg>
          </span>.
        </h1>
        <style>{`
          @keyframes drawLine { to { stroke-dashoffset: 0; } }
          @keyframes floatShot { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
          @keyframes pulseGlow { 0%,100% { opacity:.6; } 50% { opacity:1; } }
          @keyframes marquee { from { transform: translateX(0); } to { transform: translateX(-50%); } }
        `}</style>

        <p style={{
          fontSize:'clamp(16px, 1.5vw, 20px)',
          lineHeight:1.5, color:BONE_DIM,
          maxWidth:880, margin:'0 auto 40px',
          textWrap:'balance',
        }}>
          A native macOS vault built entirely with Swift and Apple's CryptoKit.<br/>
          No Rust, no Electron, no third-party servers. Runs lean and fully sandboxed.
        </p>

        {/* CTAs */}
        <div style={{ display:'flex', gap:10, justifyContent:'center', flexWrap:'wrap', marginBottom:24 }}>
          <MagneticButton primary>
            <DownloadMark size={14} color={BONE}/> Download for macOS
          </MagneticButton>
          <MagneticButton>
            Watch demo <span style={{ opacity:.5 }}>→</span>
          </MagneticButton>
        </div>

        <div style={{
          display:'inline-flex', gap:20, fontSize:12, fontFamily:"'Geist Mono', monospace",
          color:'rgba(15,14,20,.45)', letterSpacing:'-.01em',
        }}>
          <span><b style={{ color:INK, fontWeight:600 }}>$19.99</b> · once</span>
          <span style={{ opacity:.4 }}>·</span>
          <span><b style={{ color:INK, fontWeight:600 }}>Swift 5.9</b> · SwiftUI</span>
          <span style={{ opacity:.4 }}>·</span>
          <span><b style={{ color:INK, fontWeight:600 }}>macOS 14+</b></span>
        </div>
      </div>

      {/* product shot with parallax + mouse tilt */}
      <div style={{
        position:'relative', zIndex:1,
        maxWidth: 1280, margin:'80px auto 0',
        perspective: 2400,
        transform: `translateY(${parallax * -0.5}px)`,
      }}>
        {/* soft bloom under shot */}
        <div style={{
          position:'absolute', inset:'-80px 0 -40px 0',
          background:`radial-gradient(50% 50% at 50% 50%, ${ACCENT}1f, transparent 70%)`,
          filter:'blur(40px)', zIndex:-1,
          animation:'pulseGlow 4s ease-in-out infinite',
        }}/>
        <div ref={shotRef} style={{
          transformStyle:'preserve-3d',
          transform: `rotateX(${mouse.y * -2}deg) rotateY(${mouse.x * 2.5}deg)`,
          transition:'transform .25s cubic-bezier(.2,.7,.3,1)',
        }}>
          <img src="assets/screen-api.png" alt="CypherKeep — API Keys" style={{
            width:'100%', height:'auto', display:'block',
            filter:'drop-shadow(0 60px 80px rgba(15,14,20,.2)) drop-shadow(0 24px 40px rgba(15,14,20,.12))',
            animation:'floatShot 7s ease-in-out infinite',
          }}/>
        </div>

        {/* floating annotations */}
        <Annotation x="-8%" y="22%" side="left" lineLen={80}
          kicker="ENCRYPTION"
          label="AES-256-GCM"
          sub="per-record nonce + auth tag"
          dot={ACCENT}/>
        <Annotation x="108%" y="14%" side="right" lineLen={90}
          kicker="NETWORK"
          label="0 bytes out"
          sub="never leaves your Mac"
          dot="#10b981"/>
        <Annotation x="108%" y="58%" side="right" lineLen={60}
          kicker="STORAGE"
          label="~/Library/…/cypherkeep_clean.db"
          sub="sandboxed. yours."
          dot={ACCENT}/>
      </div>
    </section>
  );
}

function Annotation({ x, y, side, lineLen=70, kicker, label, sub, dot }) {
  const [show, setShow] = React.useState(false);
  React.useEffect(() => {
    const t = setTimeout(() => setShow(true), 900);
    return () => clearTimeout(t);
  }, []);
  const isLeft = side === 'left';
  return (
    <div style={{
      position:'absolute', left:x, top:y,
      display:'flex', alignItems:'center', gap:0,
      flexDirection: isLeft ? 'row' : 'row-reverse',
      transform: `${isLeft ? 'translateX(-100%)' : ''} ${show?'translateX(0)':isLeft?'translateX(8px)':'translateX(-8px)'}`,
      opacity: show ? 1 : 0,
      transition:'opacity .7s, transform .7s cubic-bezier(.2,.7,.3,1)',
      zIndex:3, pointerEvents:'none',
    }}>
      {/* text block */}
      <div style={{
        textAlign: isLeft ? 'right' : 'left',
        padding: isLeft ? '0 14px 0 0' : '0 0 0 14px',
      }}>
        <div style={{
          fontSize:9.5, fontFamily:"'Geist Mono', monospace",
          letterSpacing:'.18em', color:'rgba(15,14,20,.42)',
          marginBottom:4,
        }}>{kicker}</div>
        <div style={{
          fontSize:14.5, fontWeight:500, letterSpacing:'-.01em',
          color:'rgba(15,14,20,.92)', lineHeight:1.15,
          fontFamily: /\./.test(label) ? "'Geist Mono', monospace" : 'inherit',
        }}>{label}</div>
        {sub && (
          <div style={{
            fontSize:11.5, color:'rgba(15,14,20,.52)',
            marginTop:3, lineHeight:1.3,
          }}>{sub}</div>
        )}
      </div>

      {/* connector: line + dot */}
      <div style={{
        display:'flex', alignItems:'center',
        flexDirection: isLeft ? 'row' : 'row-reverse',
      }}>
        <div style={{
          width: lineLen, height: 1,
          background: `linear-gradient(${isLeft?'90deg':'270deg'}, transparent, rgba(15,14,20,.22) 30%, rgba(15,14,20,.22))`,
        }}/>
        <span style={{
          width:9, height:9, borderRadius:'50%', background: dot,
          boxShadow: `0 0 0 4px ${dot}22, 0 0 0 8px ${dot}10`,
          flexShrink:0,
        }}/>
      </div>
    </div>
  );
}

function MagneticButton({ children, primary }) {
  const ref = React.useRef(null);
  const [off, setOff] = React.useState({ x:0, y:0 });
  const onMove = (e) => {
    const r = ref.current.getBoundingClientRect();
    const x = (e.clientX - r.left - r.width / 2) * 0.2;
    const y = (e.clientY - r.top - r.height / 2) * 0.3;
    setOff({ x, y });
  };
  const onLeave = () => setOff({ x:0, y:0 });
  return (
    <button ref={ref} onMouseMove={onMove} onMouseLeave={onLeave} style={{
      all:'unset', cursor:'pointer',
      padding: primary ? '15px 22px 15px 20px' : '15px 22px',
      borderRadius:18,
      background: primary ? INK : 'rgba(15,14,20,.04)',
      color: primary ? BONE : INK,
      fontSize:14.5, fontWeight:500,
      display:'inline-flex', alignItems:'center', gap:9,
      border: primary ? 'none' : '1px solid rgba(15,14,20,.08)',
      transform:`translate(${off.x}px, ${off.y}px)`,
      transition:'transform .2s cubic-bezier(.2,.7,.3,1), background .2s',
      boxShadow: primary ? '0 12px 30px -10px rgba(15,14,20,.4)' : 'none',
    }}>{children}</button>
  );
}

Object.assign(window, { Hero });
