// Navigation bar — utilisée sur toutes les pages.
// Sur une sous-page, définir window.CPL_BASE = 'index.html' avant ce script
// pour que les ancres pointent vers la page principale.
const Nav = () => {
  const base = window.CPL_BASE || '';
  const [theme, setTheme] = React.useState(() => document.body.dataset.theme || 'dark');
  const [badgeStyle, setBadgeStyle] = React.useState(() => document.body.dataset.badge || 'navy');
  const [resOpen, setResOpen] = React.useState(false);
  const resRef = React.useRef(null);

  React.useEffect(() => {
    const sync = () => {
      setTheme(document.body.dataset.theme || 'dark');
      setBadgeStyle(document.body.dataset.badge || 'navy');
    };
    sync();
    const mo = new MutationObserver(sync);
    mo.observe(document.body, { attributes: true, attributeFilter: ['data-theme', 'data-badge'] });
    return () => mo.disconnect();
  }, []);

  // Ferme le menu Ressources au clic extérieur
  React.useEffect(() => {
    if (!resOpen) return;
    const onClick = (e) => {
      if (resRef.current && !resRef.current.contains(e.target)) setResOpen(false);
    };
    document.addEventListener('click', onClick);
    return () => document.removeEventListener('click', onClick);
  }, [resOpen]);

  const wordmarkColor = theme === 'light' ? '#0a0e17' : '#e8ecf5';

  return (
    <nav className="nav">
      <div className="wrap nav-inner">
        <a href={base || '#'} className="nav-logo" aria-label="Créations PL" style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
          <img
            src={`assets/badge-${badgeStyle}.png`}
            alt=""
            style={{ height: 38, width: 38, borderRadius: 9, display: 'block' }}
          />
          <span style={{ fontWeight: 500, fontSize: 17, letterSpacing: '-0.01em', color: wordmarkColor }}>
            Créations <span style={{ color: 'var(--cyan)' }}>PL</span>
          </span>
        </a>
        <div className="nav-links" style={{ alignItems: 'center' }}>
          <a href={base + '#services'}>Services</a>
          <a href={base + '#process'}>Méthode</a>
          <a href={base + '#about'}>À propos</a>
          <div ref={resRef} style={{ position: 'relative' }}>
            <button
              onClick={() => setResOpen(o => !o)}
              aria-expanded={resOpen}
              aria-haspopup="true"
              style={{
                display: 'flex', alignItems: 'center', gap: 6,
                fontSize: 14, color: resOpen ? 'var(--fg)' : 'inherit',
                padding: 0
              }}
            >
              Ressources
              <svg width="10" height="10" viewBox="0 0 10 10" fill="none" style={{ transform: resOpen ? 'rotate(180deg)' : 'none', transition: 'transform .2s ease' }}>
                <path d="M2 3.5l3 3 3-3" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round"/>
              </svg>
            </button>
            {resOpen && (
              <div style={{
                position: 'absolute', top: 'calc(100% + 14px)', left: '50%', transform: 'translateX(-50%)',
                minWidth: 220,
                background: 'var(--bg-2)',
                border: '1px solid var(--line-strong)',
                borderRadius: 12,
                padding: 8,
                boxShadow: '0 16px 48px rgba(0,0,0,0.45)',
                display: 'flex', flexDirection: 'column', gap: 2,
                zIndex: 60
              }}>
                <a href="etudes-de-cas.html" className="nav-dd-item">Études de cas</a>
                <a href="playbook-ia-pme.html" className="nav-dd-item">Playbook IA PME</a>
                <a href="#" className="nav-dd-item" onClick={(e) => { e.preventDefault(); setResOpen(false); window.cplFaq && window.cplFaq.open(); }}>FAQ</a>
                <span className="nav-dd-item" style={{ color: 'var(--fg-mute)', cursor: 'default' }}>
                  Blog <span className="mono" style={{ marginLeft: 6 }}>bientôt</span>
                </span>
              </div>
            )}
          </div>
          <a href={base + '#contact'}>Contact</a>
        </div>
        <div className="nav-right">
          <span className="mono" style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span className="live-dot"></span>
            Disponible
          </span>
          <a href={base + '#contact'} className="btn btn-primary" style={{ padding: '10px 18px', fontSize: 14 }}>
            Planifier un appel →
          </a>
        </div>
      </div>
      <style>{`
        .nav-dd-item {
          display: block;
          padding: 10px 14px;
          border-radius: 8px;
          font-size: 14px;
          color: var(--fg-dim);
          transition: background .15s ease, color .15s ease;
        }
        a.nav-dd-item:hover { background: var(--surface); color: var(--fg); }
      `}</style>
    </nav>
  );
};

Object.assign(window, { Nav });
