// Shared components — Nav, Footer, Logo, etc.
// Exported to window for use across babel scripts.

const { useState, useEffect, useRef, useMemo, useCallback } = React;

// Tiny SVG icons (no external deps)
const Icon = ({ name, size = 16, stroke = 1.5 }) => {
  const props = {
    width: size, height: size, viewBox: '0 0 24 24',
    fill: 'none', stroke: 'currentColor', strokeWidth: stroke,
    strokeLinecap: 'round', strokeLinejoin: 'round',
  };
  switch (name) {
    case 'arrow-right': return <svg {...props}><path d="M5 12h14M13 6l6 6-6 6"/></svg>;
    case 'arrow-up-right': return <svg {...props}><path d="M7 17L17 7M8 7h9v9"/></svg>;
    case 'check': return <svg {...props}><path d="M5 12.5l4.5 4.5L19 7"/></svg>;
    case 'spark': return <svg {...props}><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"/></svg>;
    case 'sun': return <svg {...props}><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></svg>;
    case 'tooth': return <svg {...props}><path d="M7 4c-2 0-3 1.5-3 4 0 2 1 3 1 5 0 4 1 9 3 9 1.5 0 1.5-3 2-5 .2-.8.6-1 1-1s.8.2 1 1c.5 2 .5 5 2 5 2 0 3-5 3-9 0-2 1-3 1-5 0-2.5-1-4-3-4-2 0-2 1-4 1s-2-1-4-1z"/></svg>;
    case 'phone': return <svg {...props}><rect x="6" y="2" width="12" height="20" rx="3"/><path d="M11 18h2"/></svg>;
    case 'share': return <svg {...props}><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><path d="M8.6 10.6l6.8-4.2M8.6 13.4l6.8 4.2"/></svg>;
    case 'chart': return <svg {...props}><path d="M3 3v18h18"/><path d="M7 14l3-3 4 4 5-6"/></svg>;
    case 'shield': return <svg {...props}><path d="M12 2l8 3v7c0 5-4 9-8 10-4-1-8-5-8-10V5l8-3z"/><path d="M9 12l2 2 4-4"/></svg>;
    case 'palette': return <svg {...props}><circle cx="12" cy="12" r="9"/><circle cx="7.5" cy="10.5" r="1"/><circle cx="12" cy="7.5" r="1"/><circle cx="16.5" cy="10.5" r="1"/><path d="M12 21c2 0 3-1 3-2 0-2-3-1-3-3 0-1 1-2 3-2 2 0 4-1 4-3"/></svg>;
    case 'sparkle': return <svg {...props}><path d="M12 3l1.8 5.5L19 10l-5.2 1.5L12 17l-1.8-5.5L5 10l5.2-1.5L12 3z"/></svg>;
    case 'play': return <svg {...props}><path d="M7 4l12 8-12 8V4z" fill="currentColor"/></svg>;
    case 'send': return <svg {...props}><path d="M22 2L11 13M22 2l-7 20-4-9-9-4 20-7z"/></svg>;
    case 'cursor': return <svg {...props}><path d="M5 3l6 16 2-7 7-2L5 3z"/></svg>;
    case 'plus': return <svg {...props}><path d="M12 5v14M5 12h14"/></svg>;
    case 'minus': return <svg {...props}><path d="M5 12h14"/></svg>;
    case 'clock': return <svg {...props}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>;
    case 'sliders': return <svg {...props}><path d="M4 21v-7M4 10V3M12 21v-9M12 8V3M20 21v-5M20 12V3"/><circle cx="4" cy="12" r="2"/><circle cx="12" cy="10" r="2"/><circle cx="20" cy="14" r="2"/></svg>;
    case 'whatsapp': return <svg {...props}><path d="M3 21l1.7-5.1A8 8 0 1 1 8.3 19L3 21z"/><path d="M8.5 10.5c.3 1 1 2 2 3s2 1.7 3 2c.5.1 1 0 1.3-.4l.6-.7c.2-.3.1-.7-.2-.9l-1-.7c-.3-.2-.7-.2-.9.1l-.3.3c-.7-.3-1.3-.8-1.8-1.4-.5-.6-.9-1.2-1.2-1.9l.3-.3c.3-.2.3-.6.1-.9l-.7-1c-.2-.3-.6-.4-.9-.2l-.7.6c-.4.3-.5.8-.4 1.3 0 .3.4.8.8 1.8z" fill="currentColor" stroke="none"/></svg>;
    case 'mail': return <svg {...props}><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/></svg>;
    case 'sms': return <svg {...props}><path d="M21 12a8 8 0 0 1-12 7l-6 2 2-6a8 8 0 1 1 16-3z"/></svg>;
    case 'eye': return <svg {...props}><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="3"/></svg>;
    case 'mouse': return <svg {...props}><rect x="6" y="3" width="12" height="18" rx="6"/><path d="M12 7v4"/></svg>;
    case 'lock': return <svg {...props}><rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/></svg>;
    case 'globe': return <svg {...props}><circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3c2.5 3 4 6 4 9s-1.5 6-4 9c-2.5-3-4-6-4-9s1.5-6 4-9z"/></svg>;
    case 'menu': return <svg {...props}><path d="M3 12h18M3 6h18M3 18h18"/></svg>;
    case 'x': return <svg {...props}><path d="M6 6l12 12M18 6L6 18"/></svg>;
    case 'moon': return <svg {...props}><path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"/></svg>;
    default: return null;
  }
};

const Logo = ({ size = 'md' }) => {
  const big = size === 'lg';
  return (
    <div className="logo" style={{ fontSize: big ? 26 : 19 }}>
      <span className="logo-mark" style={{ width: big ? 36 : 28, height: big ? 36 : 28, fontSize: big ? 22 : 18 }}>日</span>
      <span>Hinata</span>
    </div>
  );
};

const Nav = ({ dark, onToggleDark }) => {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return (
    <nav className={`nav ${scrolled ? 'scrolled' : ''}`}>
      <div className="container nav-inner">
        <a href="#top"><Logo /></a>
        <div className="nav-links">
          <a href="#how">How it works</a>
          <a href="#patient">Patient view</a>
          <a href="#roi">ROI</a>
          <a href="#features">Features</a>
          <a href="#pricing">Pricing</a>
        </div>
        <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
          <button className="btn btn-ghost" onClick={onToggleDark} aria-label="Toggle dark mode" style={{ width: 36, height: 36, padding: 0, justifyContent: 'center' }}>
            <Icon name={dark ? 'sun' : 'moon'} size={16} />
          </button>
          <a className="btn btn-primary" href="#trial">Start free trial <Icon name="arrow-right" size={14} /></a>
        </div>
      </div>
    </nav>
  );
};

// Reveal hook
const useReveal = () => {
  useEffect(() => {
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) {
          e.target.classList.add('in');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12 });
    document.querySelectorAll('.reveal:not(.in)').forEach((el) => io.observe(el));
    return () => io.disconnect();
  });
};

// Scroll progress hook — returns 0..1 across an element's bounds
const useScrollProgress = (ref, opts = {}) => {
  const { start = 0.9, end = 0.1 } = opts; // fractions of viewport height
  const [p, setP] = useState(0);
  useEffect(() => {
    const onScroll = () => {
      const el = ref.current;
      if (!el) return;
      const rect = el.getBoundingClientRect();
      const vh = window.innerHeight;
      // 0 when top of el at start*vh, 1 when bottom of el at end*vh
      const total = rect.height + (start - end) * vh;
      const traveled = (start * vh) - rect.top;
      const v = Math.max(0, Math.min(1, traveled / total));
      setP(v);
    };
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', onScroll);
    return () => {
      window.removeEventListener('scroll', onScroll);
      window.removeEventListener('resize', onScroll);
    };
  }, [ref, start, end]);
  return p;
};

// Easings
const ease = {
  out: (t) => 1 - Math.pow(1 - t, 3),
  inOut: (t) => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2,
};
const lerp = (a, b, t) => a + (b - a) * t;
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));

Object.assign(window, { Icon, Logo, Nav, useReveal, useScrollProgress, ease, lerp, clamp });
