// ===== WebAnchor — Plasma components v3 =====
const { useState, useEffect, useRef, useMemo } = React;

// --- Inline SVG icon set ---
const Icon = {
  Arr: (p) => <svg width="14" height="14" viewBox="0 0 16 16" fill="none" {...p}><path d="M2 8h12m-4-4 4 4-4 4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  ArrUR: (p) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M4 10 10 4m0 0H5m5 0v5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  Check: (p) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="m3 7.5 2.5 2.5L11 4.5" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  Plus: (p) => <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...p}><path d="M7 1.5v11M1.5 7h11" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>,
  X: (p) => <svg width="12" height="12" viewBox="0 0 12 12" fill="none" {...p}><path d="M2 2l8 8M10 2 2 10" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>,
  Code: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><path d="m7.5 6-4 4 4 4m5-8 4 4-4 4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  Cube: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><path d="M10 2.5 3 6v8l7 3.5L17 14V6l-7-3.5Zm0 0v15m0-15L3 6m7-3.5L17 6M3 6l7 3.5M17 6l-7 3.5" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  Layers: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><path d="m10 2.5 7.5 3.75L10 10 2.5 6.25 10 2.5Zm-7.5 7.5L10 13.75 17.5 10m-15 3.75L10 17.5l7.5-3.75" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  Spark: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><path d="M10 1v6m0 6v6M3.5 3.5l4 4m5 5 4 4M1 10h6m6 0h6M3.5 16.5l4-4m5-5 4-4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  Pulse: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><path d="M1.5 10h4l2-6 4 12 2-6h5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  Wand: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><path d="m3 17 9-9m3-3 2 2-3 3-2-2 3-3ZM6 3l1 2 2 1-2 1-1 2-1-2-2-1 2-1 1-2Z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  Globe: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><circle cx="10" cy="10" r="7.5" stroke="currentColor" strokeWidth="1.3"/><path d="M10 2.5c2.5 2.5 2.5 12.5 0 15M10 2.5c-2.5 2.5-2.5 12.5 0 15M2.5 10h15" stroke="currentColor" strokeWidth="1.3"/></svg>,
  Bolt: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><path d="M11 1 3 11h6l-1 8 8-10h-6l1-8Z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  Lock: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><rect x="3.5" y="9" width="13" height="9" rx="1.5" stroke="currentColor" strokeWidth="1.3"/><path d="M6.5 9V6a3.5 3.5 0 1 1 7 0v3" stroke="currentColor" strokeWidth="1.3"/></svg>,
  Server: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><rect x="3" y="3" width="14" height="6" rx="1.5" stroke="currentColor" strokeWidth="1.3"/><rect x="3" y="11" width="14" height="6" rx="1.5" stroke="currentColor" strokeWidth="1.3"/><circle cx="6" cy="6" r="0.8" fill="currentColor"/><circle cx="6" cy="14" r="0.8" fill="currentColor"/></svg>,
  Compass: (p) => <svg width="18" height="18" viewBox="0 0 20 20" fill="none" {...p}><circle cx="10" cy="10" r="7.5" stroke="currentColor" strokeWidth="1.3"/><path d="m7 13 1.5-4.5L13 7l-1.5 4.5L7 13Z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  Mail: (p) => <svg width="16" height="16" viewBox="0 0 20 20" fill="none" {...p}><rect x="2.5" y="4" width="15" height="12" rx="1.5" stroke="currentColor" strokeWidth="1.3"/><path d="m3 5 7 5 7-5" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  Cal: (p) => <svg width="16" height="16" viewBox="0 0 20 20" fill="none" {...p}><rect x="3" y="4" width="14" height="13" rx="1.5" stroke="currentColor" strokeWidth="1.3"/><path d="M3 8h14M7 2.5v3m6-3v3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  Pin: (p) => <svg width="16" height="16" viewBox="0 0 20 20" fill="none" {...p}><path d="M10 2c3 0 6 2.5 6 6 0 4.5-6 10-6 10s-6-5.5-6-10c0-3.5 3-6 6-6Z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/><circle cx="10" cy="8" r="2" stroke="currentColor" strokeWidth="1.3"/></svg>,
  GH: (p) => <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" {...p}><path d="M8 0a8 8 0 0 0-2.5 15.6c.4.1.5-.2.5-.4v-1.4c-2.2.5-2.7-1.1-2.7-1.1-.4-.9-.9-1.2-.9-1.2-.7-.5.1-.5.1-.5.8.1 1.2.8 1.2.8.7 1.2 1.9.9 2.4.7 0-.5.3-.9.5-1.1-1.8-.2-3.6-.9-3.6-3.9 0-.9.3-1.6.8-2.1-.1-.2-.4-1 .1-2.1 0 0 .7-.2 2.2.8a7.7 7.7 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.4 1.1.2 1.9.1 2.1.5.5.8 1.2.8 2.1 0 3-1.8 3.7-3.6 3.9.3.3.6.8.6 1.5v2.2c0 .2.1.5.5.4A8 8 0 0 0 8 0Z"/></svg>,
  Tw: (p) => <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" {...p}><path d="M9.3 6.8 14.7 0h-2L8.3 5.5 4.8 0H0l5.7 8L0 16h2l5-6.2 4 6.2h4.8L9.3 6.8Z"/></svg>,
  LI: (p) => <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor" {...p}><path d="M3 1.5a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3ZM1 6h4v10H1V6Zm7 0h3.8v1.4c.6-.9 1.9-1.7 3.7-1.7 4 0 4.5 2.6 4.5 5.4V16h-4v-4.2c0-1 0-2.3-1.4-2.3-1.4 0-1.6 1.1-1.6 2.2V16H8V6Z" transform="scale(0.78) translate(2 2)"/></svg>,
  Db: (p) => <svg width="14" height="14" viewBox="0 0 16 16" fill="none" {...p}><circle cx="8" cy="8" r="6.5" stroke="currentColor" strokeWidth="1.3"/><path d="M2 5c2 1 10 3 12 7M14 4c-2 2-8 7-12 9M6 1c1 3 4 11 4 14" stroke="currentColor" strokeWidth="1.3"/></svg>,
};

function Logo({ size = 22 }) {
  return <img src="assets/logo.png" alt="WebAnchor" style={{ width: size, height: size, objectFit: 'contain' }} />;
}

// --- Router ---
const ROUTES = [
  { path: '/', label: 'Home' },
  { path: '/services', label: 'Services' },
  { path: '/portfolio', label: 'Portfolio' },
  { path: '/pricing', label: 'Pricing' },
  { path: '/about', label: 'About' },
  { path: '/faq', label: 'FAQ' },
  { path: '/contact', label: 'Contact' },
];

function useRoute() {
  const get = () => (window.location.hash.replace(/^#/, '') || '/');
  const [route, setRoute] = useState(get());
  useEffect(() => {
    const onChange = () => {
      setRoute(get());
      window.scrollTo({ top: 0, behavior: 'instant' });
    };
    window.addEventListener('hashchange', onChange);
    return () => window.removeEventListener('hashchange', onChange);
  }, []);
  return route;
}

function Link({ to, children, className = '', style, ...rest }) {
  const onClick = (e) => {
    e.preventDefault();
    if (window.location.hash !== '#' + to) window.location.hash = to;
    else window.scrollTo({ top: 0, behavior: 'smooth' });
  };
  return <a href={'#' + to} onClick={onClick} className={className} style={style} {...rest}>{children}</a>;
}

// --- Nav: floating pill, glass blur, gradient CTA ---
function Nav() {
  const route = useRoute();
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 12);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  useEffect(() => { setOpen(false); }, [route]);

  return (
    <>
      <nav className={'nav ' + (scrolled ? 'scrolled' : '')}>
        <div className="nav-inner">
          <Link to="/" className="nav-brand">
            <Logo size={22} />
            <span>WebAnchor</span>
          </Link>
          <div className="nav-links">
            {ROUTES.slice(1, 6).map(r => (
              <Link key={r.path} to={r.path} className={route === r.path ? 'active' : ''}>{r.label}</Link>
            ))}
          </div>
          <div className="nav-side">
            <Link to="/contact" className="btn btn-ghost btn-sm nav-secondary">Book a call</Link>
            <Link to="/contact" className="btn btn-grad btn-sm">Get a quote <span className="arr"><Icon.Arr /></span></Link>
            <button className="nav-toggle" onClick={() => setOpen(o => !o)} aria-label="Menu">
              {open ? <Icon.X /> : <svg width="16" height="16" viewBox="0 0 16 16"><path d="M2 5h12M2 11h12" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>}
            </button>
          </div>
        </div>
      </nav>
      {open && (
        <div className="mobile-menu">
          {ROUTES.map(r => (
            <Link key={r.path} to={r.path} className={route === r.path ? 'active' : ''}>{r.label}</Link>
          ))}
          <Link to="/contact" className="btn btn-grad btn-lg" style={{ marginTop: 28, alignSelf: 'flex-start' }}>Get a quote <span className="arr"><Icon.Arr /></span></Link>
        </div>
      )}
    </>
  );
}

// --- Section head ---
function SectionHead({ eyebrow, title, sub, center = false, action }) {
  return (
    <div className={'section-head ' + (center ? 'center' : '')}>
      {eyebrow && <span className="eyebrow">{eyebrow}</span>}
      <h2 className="grad-text-soft">{title}</h2>
      {sub && <p className="lead">{sub}</p>}
      {action && <div style={{ marginTop: 8 }}>{action}</div>}
    </div>
  );
}

// --- Aurora background helper (subtle iridescent glows) ---
function Aurora({ variant = 'default' }) {
  if (variant === 'hero') {
    return (
      <>
        <div className="aurora aurora-violet" style={{ width: 600, height: 600, top: -100, left: -200, opacity: 0.35 }} />
        <div className="aurora aurora-cyan" style={{ width: 540, height: 540, top: -150, right: -150, opacity: 0.3 }} />
        <div className="aurora aurora-pink" style={{ width: 400, height: 400, top: 200, left: '40%', opacity: 0.18 }} />
      </>
    );
  }
  if (variant === 'minimal') {
    return (
      <>
        <div className="aurora aurora-violet" style={{ width: 500, height: 500, top: '50%', left: -200, opacity: 0.18 }} />
        <div className="aurora aurora-cyan" style={{ width: 500, height: 500, top: 100, right: -200, opacity: 0.15 }} />
      </>
    );
  }
  return null;
}

// --- Page wrapper ---
function Page({ children }) { return <div className="page-enter">{children}</div>; }

// --- Page intro (for subpages) ---
function PageIntro({ eyebrow, title, sub, rightCol }) {
  return (
    <section style={{ padding: '60px 0 80px', position: 'relative' }}>
      <Aurora variant="minimal" />
      <div className="wrap" style={{ position: 'relative' }}>
        <div style={{ display: 'grid', gridTemplateColumns: rightCol ? '1.2fr 1fr' : '1fr', gap: 60, alignItems: 'end' }}>
          <div className="col" style={{ gap: 24 }}>
            <span className="eyebrow reveal r-1">{eyebrow}</span>
            <h1 className="grad-text-soft reveal r-2" style={{ maxWidth: '16ch' }}>{title}</h1>
            {sub && <p className="lead reveal r-3" style={{ maxWidth: '60ch' }}>{sub}</p>}
          </div>
          {rightCol && <div className="reveal r-4">{rightCol}</div>}
        </div>
      </div>
    </section>
  );
}

// --- CTA tail block ---
function CtaTail() {
  return (
    <section className="section">
      <div className="wrap-tight">
        <div className="cta-block">
          <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none', overflow: 'hidden', borderRadius: 'inherit' }}>
            <div className="aurora aurora-violet" style={{ width: 500, height: 500, bottom: -200, left: -100, opacity: 0.3 }} />
            <div className="aurora aurora-cyan" style={{ width: 500, height: 500, top: -200, right: -100, opacity: 0.25 }} />
          </div>
          <div style={{ position: 'relative', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 22 }}>
            <span className="eyebrow">Let's talk</span>
            <h2 className="grad-text-soft" style={{ maxWidth: '20ch' }}>Ready to anchor your business online?</h2>
            <p className="lead" style={{ maxWidth: '52ch' }}>
              A 20-minute call, no charge, no pressure. We'll talk about your project, answer your questions,
              and see if we're a good fit. No sales pitch — promise.
            </p>
            <div className="row" style={{ gap: 10, marginTop: 8 }}>
              <Link to="/contact" className="btn btn-grad btn-lg">Get a quote <span className="arr"><Icon.Arr /></span></Link>
              <Link to="/contact" className="btn btn-ghost btn-lg">Book a call</Link>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// --- Footer (water, with a sandy sea floor at the very bottom) ---
function Footer() {
  return (
    <footer className="footer">
      <div className="wrap">
        <div className="footer-mega grad-text">
          Anchor your business online.
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 60, alignItems: 'end', marginBottom: 64 }}>
          <p style={{ fontSize: 18, color: 'var(--text-dim)', maxWidth: '52ch' }}>
            Quick form, no pressure. We'll get back to you within one business day — usually a lot sooner.
          </p>
          <div className="row" style={{ gap: 12, justifyContent: 'flex-end' }}>
            <Link to="/contact" className="btn btn-ghost">Book a call</Link>
            <Link to="/contact" className="btn btn-grad btn-lg">Get a quote <span className="arr"><Icon.Arr /></span></Link>
          </div>
        </div>

        <div className="footer-grid">
          <div className="footer-col">
            <Link to="/" className="nav-brand" style={{ fontSize: 18 }}>
              <Logo size={26} /><span>WebAnchor</span>
            </Link>
            <p style={{ marginTop: 16, color: 'var(--text-muted)', fontSize: 14, maxWidth: '34ch' }}>
              Custom websites and web applications. Based in Cape Town, working with clients worldwide.
            </p>
            <div className="socials" style={{ marginTop: 24 }}>
              <a href="https://instagram.com/webanchor.studio" aria-label="Instagram"><Icon.Tw /></a>
              <a href="https://linkedin.com/company/webanchor" aria-label="LinkedIn"><Icon.LI /></a>
            </div>
          </div>
          <div className="footer-col">
            <h5>Quick links</h5>
            <Link to="/">Home</Link>
            <Link to="/services">Services</Link>
            <Link to="/portfolio">Portfolio</Link>
            <Link to="/pricing">Pricing</Link>
          </div>
          <div className="footer-col">
            <h5>Studio</h5>
            <Link to="/about">About</Link>
            <Link to="/faq">FAQ</Link>
            <Link to="/contact">Contact</Link>
          </div>
          <div className="footer-col">
            <h5>Get in touch</h5>
            <a href="mailto:info@web-anchor.com">info@web-anchor.com</a>
            <a href="tel:+27789909431">078 990 9431 · call</a>
            <a href="https://wa.me/27789909431">078 990 9431 · WhatsApp</a>
            <div style={{ marginTop: 12, fontSize: 13, color: 'var(--text-muted)' }}>Cape Town, South Africa<br/>Serving clients worldwide</div>
          </div>
        </div>
      </div>

      <div className="seabed-band">
        <div className="seabed-surf" aria-hidden="true">
          <svg viewBox="0 0 1440 90" preserveAspectRatio="none"><path d="M0,90 L0,52 C160,30 300,68 480,58 C660,48 820,18 1020,34 C1200,48 1320,72 1440,54 L1440,90 Z" /></svg>
        </div>
        <div className="wrap">
          <div className="footer-bottom">
            <span className="pebble-text">© 2026 WebAnchor · Privacy Policy · Terms of Service</span>
            <span className="pebble-text">Created by WebAnchor · washed up in Cape Town</span>
          </div>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, {
  Icon, Logo, ROUTES, useRoute, Link, Nav, Footer,
  SectionHead, Aurora, Page, PageIntro, CtaTail,
});
