/* global React */
const { useState, useEffect } = React;

// ─── Icons (Lucide-style, 1.5px stroke) ───
const Icon = ({ name, size = 20 }) => {
  const paths = {
    play: <polygon points="6 4 20 12 6 20 6 4" />,
    mic: <><rect x="9" y="2" width="6" height="12" rx="3" /><path d="M5 11a7 7 0 0 0 14 0" /><line x1="12" y1="18" x2="12" y2="22" /></>,
    lock: <><rect x="4" y="11" width="16" height="10" rx="2" /><path d="M8 11V7a4 4 0 0 1 8 0v4" /></>,
    shield: <path d="M12 2 4 6v6c0 5 3.5 9 8 10 4.5-1 8-5 8-10V6l-8-4z" />,
    calendar: <><rect x="3" y="5" width="18" height="16" rx="2" /><line x1="3" y1="10" x2="21" y2="10" /><line x1="8" y1="3" x2="8" y2="7" /><line x1="16" y1="3" x2="16" y2="7" /></>,
    heart: <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z" />,
    users: <><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" /><circle cx="9" cy="7" r="4" /><path d="M23 21v-2a4 4 0 0 0-3-3.87" /><path d="M16 3.13a4 4 0 0 1 0 7.75" /></>,
    check: <polyline points="4 12 10 18 20 6" />,
    arrowRight: <><line x1="5" y1="12" x2="19" y2="12" /><polyline points="12 5 19 12 12 19" /></>,
    plus: <><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></>,
    bell: <><path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9" /><path d="M13.73 21a2 2 0 0 1-3.46 0" /></>,
    book: <><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20" /><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z" /></>,
    gift: <><polyline points="20 12 20 22 4 22 4 12" /><rect x="2" y="7" width="20" height="5" /><line x1="12" y1="22" x2="12" y2="7" /><path d="M12 7H7.5a2.5 2.5 0 0 1 0-5C11 2 12 7 12 7z" /><path d="M12 7h4.5a2.5 2.5 0 0 0 0-5C13 2 12 7 12 7z" /></>,
    mail: <><rect x="3" y="5" width="18" height="14" rx="2" /><polyline points="3 7 12 13 21 7" /></>,
    sparkle: <path d="M12 3v3M12 18v3M3 12h3M18 12h3M5.6 5.6l2.1 2.1M16.3 16.3l2.1 2.1M5.6 18.4l2.1-2.1M16.3 7.7l2.1-2.1" />,
    apple: <path d="M16.5 2c0 1.5-.5 3-2 4.5-1.4 1.5-3 1.5-3.5 0 0-1.5.5-3 2-4.5C14.4 .5 16 .5 16.5 2zM21 17c-1 2.5-2.5 5-4.5 5-1.5 0-2-1-4-1s-2.5 1-4 1c-2 0-3.5-2.5-4.5-5C2 13 4 9 7 9c1.5 0 3 1 4 1s2.5-1 4-1c3 0 5 4 6 8z" />,
    google: <><path d="M21.6 12.2c0-.7 0-1.3-.2-1.9H12v3.6h5.4c-.2 1.2-1 2.2-2 2.9v2.5h3.3c1.9-1.8 3-4.4 3-7.1z" /><path d="M12 22c2.7 0 5-1 6.7-2.6l-3.3-2.5c-.9.6-2.1 1-3.4 1-2.6 0-4.8-1.8-5.6-4.1H3v2.6A10 10 0 0 0 12 22z" /><path d="M6.4 13.7c-.2-.6-.3-1.2-.3-1.7s.1-1.2.3-1.7V7.7H3a10 10 0 0 0 0 8.6l3.4-2.6z" /><path d="M12 6.2c1.5 0 2.8.5 3.8 1.5l2.9-2.9C16.9 3.2 14.6 2 12 2A10 10 0 0 0 3 7.7l3.4 2.6c.8-2.3 3-4.1 5.6-4.1z" /></>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
         stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"
         aria-hidden="true" focusable="false">
      {paths[name]}
    </svg>
  );
};

// ─── Header ───
function Header({ page, navigate }) {
  const [open, setOpen] = useState(false);
  const navs = [
    { id: 'about',    label: 'About',    href: '/about/' },
    { id: 'features', label: 'Features', href: '/features/' },
    { id: 'pricing',  label: 'Pricing',  href: '/pricing/' },
    { id: 'support',  label: 'Support',  href: '/support/' },
  ];
  const go = (id, e) => { if (e) e.preventDefault(); navigate(id); setOpen(false); };
  return (
    <header className={`header${open ? ' nav-open' : ''}`} data-em-surface="ink">
      <a className="header__brand" href="/" onClick={(e) => go('home', e)}>
        <span className="header__mark">
          <img src="assets/logo.svg" alt="Everlasting Memory — return to home" width="44" height="44" />
        </span>
      </a>
      <nav id="primary-nav" className="header__nav" aria-label="Primary navigation">
        {navs.map(n => (
          <a key={n.id} href={n.href} className={page === n.id ? 'is-active' : ''} onClick={(e) => go(n.id, e)}>{n.label}</a>
        ))}
        <div className="header__nav-signin">
          <button className="btn btn--gold" onClick={(e) => go('signup', e)}>Begin Yours</button>
          <button className="btn btn--ghost-light" onClick={(e) => go('signin', e)}>Sign in</button>
        </div>
      </nav>
      <div className="header__actions">
        <button className="btn btn--ghost-light btn--sm" onClick={() => go('signin')}>Sign in</button>
        <button className="btn btn--gold btn--sm" onClick={() => go('signup')}>Begin Yours</button>
      </div>
      <button
        className="header__burger"
        onClick={() => setOpen(o => !o)}
        aria-label="Toggle menu"
        aria-expanded={open}
        aria-controls="primary-nav"
      >
        <span /><span /><span />
      </button>
    </header>
  );
}

// ─── Footer ───
function Footer({ navigate }) {
  const go = (id, e) => { if (e) e.preventDefault(); navigate(id); };
  return (
    <footer className="footer" data-em-surface="ink">
      <div className="footer__rule" />
      <div className="footer__inner">
        <div className="footer__brand">
          <img src="assets/logo.svg" alt="Everlasting Memory" width="44" height="44" />
        </div>
        <div className="footer__cols">
          <div>
            <div className="em-eyebrow">Product</div>
            <a href="/features/" onClick={(e) => go('features', e)}>Features</a>
            <a href="/pricing/"  onClick={(e) => go('pricing', e)}>Pricing</a>
            <a href="/signup/"   onClick={(e) => go('signup', e)}>Sign up</a>
          </div>
          <div>
            <div className="em-eyebrow">Company</div>
            <a href="/about/"   onClick={(e) => go('about', e)}>About us</a>
            <a href="/support/" onClick={(e) => go('support', e)}>Support</a>
            <span className="footer__placeholder">Press</span>
          </div>
          <div>
            <div className="em-eyebrow">Quiet</div>
            <span className="footer__placeholder">Privacy</span>
            <span className="footer__placeholder">What we keep</span>
            <span className="footer__placeholder">Legacy planning</span>
          </div>
        </div>
      </div>
      <div className="footer__small">
        © 2026 Everlasting Memory · Made with care for the people we love, and the people we'll miss.
      </div>
    </footer>
  );
}

// ─── CTA band (reused) ───
function CtaBand({ navigate, title, body, cta = 'Begin Yours', quiet = "Free for the first hour. No card needed." }) {
  return (
    <section className="cta-band" data-em-surface="ink">
      <div className="cta-band__vignette" />
      <div className="cta-band__inner">
        <h2 dangerouslySetInnerHTML={{ __html: title }} />
        <p>{body}</p>
        <button className="btn btn--gold btn--lg" onClick={() => navigate('signup')}>{cta}</button>
        <div className="cta-band__quietline">{quiet}</div>
      </div>
    </section>
  );
}

// ─── Scroll effects: reveal + parallax ───
function ScrollFX() {
  useEffect(() => {
    // Reveal via IntersectionObserver
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => e.isIntersecting && e.target.classList.add('is-visible'));
    }, { threshold: 0.1, rootMargin: '0px 0px -48px 0px' });
    document.querySelectorAll('[data-reveal]').forEach(el => io.observe(el));

    // Parallax via scroll + rAF
    let ticking = false;
    const update = () => {
      document.querySelectorAll('[data-parallax]').forEach(el => {
        const speed = parseFloat(el.dataset.parallax) || 0.25;
        const rect = el.parentElement.getBoundingClientRect();
        const center = (rect.top + rect.height * 0.5) - window.innerHeight * 0.5;
        const offset = Math.max(-180, Math.min(180, center * speed));
        el.style.transform = `translateY(${offset}px)`;
      });
      ticking = false;
    };
    const onScroll = () => { if (!ticking) { requestAnimationFrame(update); ticking = true; } };
    window.addEventListener('scroll', onScroll, { passive: true });
    update();

    return () => { io.disconnect(); window.removeEventListener('scroll', onScroll); };
  }, []);
  return null;
}

Object.assign(window, { Icon, Header, Footer, CtaBand, ScrollFX });
