// Nav.jsx — Fin Fanfa · transparent, centered logo layout
// Premium microinteractions: hover glow halo, dynamic-lit underline,
// subtle vertical displacement.

function Nav({ activeSection, onNavigate }) {
  const [open, setOpen] = React.useState(false);
  const [visible, setVisible] = React.useState(false);
  const { lang } = React.useContext(window.LangContext);
  const t = window.ffT[lang].nav;

  React.useEffect(() => {
    const timer = setTimeout(() => setVisible(true), 900);
    return () => clearTimeout(timer);
  }, []);

  const leftLinks  = [
    { id: 'about',      label: t.about },
    { id: 'experience', label: t.experience },
  ];
  const rightLinks = [
    { id: 'projects', label: t.projects },
    { id: 'stack',    label: t.stack },
  ];

  const goHome = () => {
    if (window.ffScrollTo) window.ffScrollTo(0);
    else window.scrollTo({ top: 0, behavior: 'smooth' });
  };

  const NavLink = ({ id, label }) => {
    const active = activeSection === id;
    const [hov, setHov] = React.useState(false);
    const lit = active || hov;
    return (
      <button
        onClick={() => onNavigate(id)}
        onMouseEnter={() => setHov(true)}
        onMouseLeave={() => setHov(false)}
        style={{
          background: 'none', border: 'none', cursor: 'pointer',
          fontFamily: 'Geist, sans-serif', fontSize: 13, fontWeight: 500,
          letterSpacing: '0.01em',
          color: lit ? 'rgba(206,212,242,0.95)' : 'rgba(206,212,242,0.48)',
          transition: 'color 320ms cubic-bezier(0.4,0,0.2,1), transform 320ms cubic-bezier(0.4,0,0.2,1)',
          padding: '10px 14px',
          position: 'relative',
          textShadow: hov
            ? '0 0 14px rgba(28,96,218,0.4), 0 1px 6px rgba(1,4,10,0.6)'
            : '0 1px 6px rgba(1,4,10,0.55)',
          transform: hov ? 'translateY(-1px)' : 'translateY(0)',
          isolation: 'isolate',
        }}
      >
        {/* Hover glow halo behind text */}
        <span
          aria-hidden="true"
          style={{
            position: 'absolute', inset: '-2px -10px',
            borderRadius: 14,
            background: 'radial-gradient(ellipse 70% 90% at 50% 60%, rgba(28,96,218,0.32) 0%, rgba(28,96,218,0.08) 50%, transparent 75%)',
            opacity: hov ? 1 : 0,
            transform: hov ? 'scale(1)' : 'scale(0.85)',
            transition: 'opacity 360ms cubic-bezier(0.4,0,0.2,1), transform 420ms cubic-bezier(0.2,0.7,0.1,1)',
            pointerEvents: 'none',
            zIndex: -1,
            filter: 'blur(2px)',
          }}
        />
        {label}
        {/* Animated underline — draws on hover, stays solid on active */}
        <span
          aria-hidden="true"
          style={{
            position: 'absolute',
            left: 14, right: 14,
            bottom: 4,
            height: 1,
            background: 'linear-gradient(90deg, transparent 0%, #1C60DA 30%, #6B95FF 70%, transparent 100%)',
            transformOrigin: active ? 'center center' : 'left center',
            transform: lit ? 'scaleX(1)' : 'scaleX(0)',
            opacity: lit ? 1 : 0,
            transition: 'transform 540ms cubic-bezier(0.2,0.7,0.1,1), opacity 320ms ease',
            boxShadow: lit ? '0 0 8px rgba(28,96,218,0.55)' : 'none',
          }}
        />
        {active && (
          <span style={{
            position: 'absolute', left: '50%', bottom: -2,
            transform: 'translateX(-50%)',
            width: 3, height: 3, borderRadius: '50%',
            background: '#1C60DA',
            boxShadow: '0 0 8px rgba(28,96,218,0.95)',
            animation: 'ffPulseAlt 2.6s ease-in-out infinite',
          }} />
        )}
      </button>
    );
  };

  return (
    <>
      <div className="ff-nav-blur-overlay" />
      <nav className="ff-nav-bar" style={{
        position: 'fixed', top: 0, left: 0, right: 0, zIndex: 100,
        height: 'calc(72px + env(safe-area-inset-top, 0px))',
        paddingTop: 'env(safe-area-inset-top, 0px)',
        paddingBottom: 0,
        paddingLeft: 'clamp(20px, 4vw, 48px)',
        paddingRight: 'clamp(20px, 4vw, 48px)',
        display: 'flex', alignItems: 'center',
        background: 'transparent',
        pointerEvents: 'none',
        opacity: visible ? 1 : 0,
        transform: visible ? 'translateY(0)' : 'translateY(-12px)',
        transition: 'opacity 700ms cubic-bezier(0.22,0.61,0.36,1), transform 700ms cubic-bezier(0.22,0.61,0.36,1)',
      }}>

        {/* ── Left: Wordmark + tagline ── */}
        <a
          href="#home"
          onClick={e => { e.preventDefault(); goHome(); }}
          className="ff-nav-brand"
          style={{
            flex: '1 1 0', display: 'flex', flexDirection: 'column',
            justifyContent: 'center', gap: 8,
            textDecoration: 'none', pointerEvents: 'auto',
            paddingTop: 14,
            transition: 'transform 380ms cubic-bezier(0.4,0,0.2,1), filter 380ms ease',
          }}
          onMouseEnter={e => {
            e.currentTarget.style.transform = 'translateX(2px)';
            e.currentTarget.style.filter = 'drop-shadow(0 0 12px rgba(28,96,218,0.35))';
          }}
          onMouseLeave={e => {
            e.currentTarget.style.transform = 'none';
            e.currentTarget.style.filter = 'none';
          }}
        >
          <img
            src="assets/wordmark-white-no-bg.svg"
            alt="Fin Fanfa"
            style={{ width: 156, height: 24, display: 'block' }}
          />

          {/* Tagline */}
          <span style={{
            fontFamily: 'Geist, sans-serif',
            fontSize: 11, fontWeight: 300,
            letterSpacing: '0.28em', textTransform: 'lowercase',
            color: 'rgba(206,212,242,0.72)',
            whiteSpace: 'nowrap',
          }}>
            {t.tagline}
          </span>
        </a>

        {/* ── Center: links + logo ── */}
        <div className="ff-nav-links" style={{
          display: 'flex', alignItems: 'center', gap: 0,
          pointerEvents: 'auto',
        }}>
          {leftLinks.map(l => <NavLink key={l.id} id={l.id} label={l.label} />)}

          <NavLogo onClick={goHome} />

          {rightLinks.map(l => <NavLink key={l.id} id={l.id} label={l.label} />)}
        </div>

        {/* ── Right: CTA group + mobile toggle ── */}
        <div style={{ flex: '1 1 0', display: 'flex', justifyContent: 'flex-end', alignItems: 'center', gap: 12, pointerEvents: 'auto' }}>
          <div className="ff-nav-cta-group">
            <ContactBtn onNavigate={onNavigate} />
            <LangToggle className="ff-nav-lang-toggle" />
          </div>

          <button
            className="ff-nav-mobile-toggle"
            onClick={() => setOpen(!open)}
            style={{
              display: 'none', background: 'transparent',
              border: '1px solid rgba(206,212,242,0.18)',
              borderRadius: 8, padding: 8, cursor: 'pointer', color: '#CED4F2',
              flexShrink: 0,
            }}
          >
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8">
              {open ? <path d="M18 6L6 18M6 6l12 12"/> : <path d="M4 6h16M4 12h16M4 18h16"/>}
            </svg>
          </button>
        </div>
      </nav>

      {/* Mobile drawer — premium glassmorphism */}
      {open && (
        <div style={{
          position: 'fixed',
          top: 'calc(72px + env(safe-area-inset-top, 0px))',
          left: 0, right: 0, bottom: 0,
          zIndex: 97,
          background: 'rgba(1, 4, 10, 0.04)',
          backdropFilter: 'blur(40px)',
          WebkitBackdropFilter: 'blur(40px)',
          borderTop: '1px solid rgba(206,212,242,0.07)',
          display: 'flex', flexDirection: 'column',
          paddingLeft: 28, paddingRight: 28,
          paddingBottom: 'calc(40px + env(safe-area-inset-bottom, 0px))',
          animation: 'ffDrawerIn 300ms cubic-bezier(0.22,0.61,0.36,1) forwards',
          overflowY: 'auto',
        }}>
          {/* Nav links */}
          <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', paddingTop: 24 }}>
            {[...leftLinks, ...rightLinks].map((l, i) => (
              <button
                key={l.id}
                onClick={() => { onNavigate(l.id); setOpen(false); }}
                style={{
                  background: 'none', border: 'none', textAlign: 'left',
                  fontFamily: 'Geist, sans-serif',
                  fontSize: 'clamp(32px, 8.5vw, 48px)',
                  fontWeight: 600,
                  letterSpacing: '-0.028em',
                  color: activeSection === l.id ? '#CED4F2' : 'rgba(206,212,242,0.28)',
                  padding: '14px 0', cursor: 'pointer',
                  borderBottom: '1px solid rgba(206,212,242,0.05)',
                  display: 'flex', alignItems: 'center', justifyContent: 'space-between',
                  animation: `ffDrawerItemIn 420ms cubic-bezier(0.22,0.61,0.36,1) ${80 + i * 55}ms both`,
                }}
              >
                {l.label}
                {activeSection === l.id && (
                  <span style={{
                    width: 7, height: 7, borderRadius: '50%',
                    background: '#1C60DA',
                    boxShadow: '0 0 12px rgba(28,96,218,0.9)',
                    flexShrink: 0,
                  }} />
                )}
              </button>
            ))}
          </div>

          {/* Bottom row — socials + lang toggle + CTA */}
          <div style={{
            display: 'flex', alignItems: 'center', justifyContent: 'space-between',
            paddingTop: 24, marginTop: 16,
            borderTop: '1px solid rgba(206,212,242,0.06)',
            animation: 'ffDrawerItemIn 420ms cubic-bezier(0.22,0.61,0.36,1) 360ms both',
          }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 20 }}>
              {[
                { href: 'https://github.com/felipefinfanfa', label: 'GitHub', icon: <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.477 2 2 6.477 2 12c0 4.418 2.865 8.166 6.839 9.489.5.092.682-.217.682-.482 0-.237-.009-.868-.013-1.703-2.782.604-3.369-1.342-3.369-1.342-.454-1.154-1.11-1.462-1.11-1.462-.908-.62.069-.608.069-.608 1.003.07 1.531 1.03 1.531 1.03.892 1.529 2.341 1.087 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.11-4.555-4.943 0-1.091.39-1.984 1.029-2.683-.103-.253-.446-1.27.098-2.647 0 0 .84-.269 2.75 1.025A9.578 9.578 0 0 1 12 6.836a9.59 9.59 0 0 1 2.504.337c1.909-1.294 2.747-1.025 2.747-1.025.546 1.377.202 2.394.1 2.647.64.699 1.028 1.592 1.028 2.683 0 3.842-2.339 4.687-4.566 4.935.359.309.678.919.678 1.852 0 1.336-.012 2.415-.012 2.743 0 .267.18.578.688.48C19.138 20.163 22 16.418 22 12c0-5.523-4.477-10-10-10z"/></svg> },
                { href: 'https://linkedin.com/in/felipefinfanfa', label: 'LinkedIn', icon: <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 0 1-2.063-2.065 2.064 2.064 0 1 1 2.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg> },
              ].map(({ href, label, icon }) => (
                <a key={label} href={href} target="_blank" rel="noopener noreferrer" aria-label={label}
                  style={{ color: 'rgba(206,212,242,0.38)', display: 'flex', alignItems: 'center', transition: 'color 200ms ease' }}
                  onMouseEnter={e => e.currentTarget.style.color = '#CED4F2'}
                  onMouseLeave={e => e.currentTarget.style.color = 'rgba(206,212,242,0.38)'}
                >{icon}</a>
              ))}
              <LangToggle />
            </div>

            <button
              onClick={() => {
                setOpen(false);
                setTimeout(() => {
                  const el = document.getElementById('contact-form');
                  if (el) {
                    const rect = el.getBoundingClientRect();
                    const centered = rect.top + window.scrollY - Math.max(0, (window.innerHeight - rect.height) / 2);
                    window.scrollTo({ top: centered, left: window.scrollX, behavior: 'smooth' });
                  }
                }, 120);
              }}
              style={{
                fontFamily: 'Geist, sans-serif', fontWeight: 600, fontSize: 13,
                letterSpacing: '0.02em',
                background: '#1C60DA', color: '#fff', border: 'none',
                borderRadius: 9999, padding: '10px 20px',
                cursor: 'pointer',
                display: 'inline-flex', alignItems: 'center', gap: 7,
                boxShadow: '0 0 0 1px rgba(28,96,218,0.35), 0 6px 18px rgba(28,96,218,0.28)',
              }}
            >
              {t.cta}
              <svg width="11" height="11" fill="none" stroke="currentColor" strokeWidth="2.2" viewBox="0 0 24 24"><path d="M5 12h14M12 5l7 7-7 7"/></svg>
            </button>
          </div>
        </div>
      )}
    </>
  );
}

function NavLogo({ onClick }) {
  const [hov, setHov] = React.useState(false);
  return (
    <button
      onClick={onClick}
      onMouseEnter={() => setHov(true)}
      onMouseLeave={() => setHov(false)}
      style={{
        margin: '0 18px', background: 'none', border: 'none',
        cursor: 'pointer', padding: 0, display: 'flex', alignItems: 'center',
        borderRadius: 8,
        transition: 'transform 360ms cubic-bezier(0.4,0,0.2,1), filter 360ms ease',
        transform: hov ? 'scale(1.08)' : 'scale(1)',
        filter: hov ? 'drop-shadow(0 0 14px rgba(28,96,218,0.6))' : 'none',
        position: 'relative',
      }}
    >
      <span
        aria-hidden="true"
        style={{
          position: 'absolute', inset: -8, borderRadius: '50%',
          background: 'radial-gradient(circle, rgba(28,96,218,0.28) 0%, transparent 65%)',
          opacity: hov ? 1 : 0,
          transform: hov ? 'scale(1)' : 'scale(0.6)',
          transition: 'opacity 380ms ease, transform 460ms cubic-bezier(0.2,0.7,0.1,1)',
          pointerEvents: 'none',
          filter: 'blur(2px)',
        }}
      />
      <img
        src="assets/logo-blue-mark.svg"
        alt="Fin Fanfa"
        style={{ width: 34, height: 34, borderRadius: 7, display: 'block', position: 'relative' }}
      />
    </button>
  );
}

function LangToggle({ className }) {
  const { lang, setLang } = React.useContext(window.LangContext);
  const [hov, setHov] = React.useState(false);
  return (
    <button
      className={className}
      onClick={() => setLang(lang === 'en' ? 'pt' : 'en')}
      onMouseEnter={() => setHov(true)}
      onMouseLeave={() => setHov(false)}
      style={{
        fontFamily: 'Geist Mono, monospace',
        fontSize: 11, fontWeight: 600, letterSpacing: '0.10em',
        color: hov ? 'rgba(206,212,242,0.90)' : 'rgba(206,212,242,0.45)',
        background: hov ? 'rgba(206,212,242,0.07)' : 'transparent',
        border: '1px solid',
        borderColor: hov ? 'rgba(206,212,242,0.28)' : 'rgba(206,212,242,0.14)',
        borderRadius: 9999, padding: '5px 9px',
        cursor: 'pointer', transition: 'all 220ms ease',
      }}
    >
      {lang === 'en' ? 'PT' : 'EN'}
    </button>
  );
}

function ContactBtn({ onNavigate }) {
  const { lang } = React.useContext(window.LangContext);
  const t = window.ffT[lang].nav;
  const [hov, setHov] = React.useState(false);
  const handleClick = () => {
    if (window.matchMedia('(pointer: coarse)').matches) {
      const el = document.getElementById('contact-form');
      if (el) {
        const rect = el.getBoundingClientRect();
        const centered = rect.top + window.scrollY - Math.max(0, (window.innerHeight - rect.height) / 2);
        window.scrollTo({ top: centered, left: window.scrollX, behavior: 'smooth' });
      }
    } else {
      onNavigate('contact');
    }
  };
  return (
    <button
      onClick={handleClick}
      onMouseEnter={() => setHov(true)}
      onMouseLeave={() => setHov(false)}
      style={{
        position: 'relative',
        fontFamily: 'Geist, sans-serif', fontSize: 12.5, fontWeight: 600,
        letterSpacing: '0.03em',
        background: hov
          ? 'linear-gradient(135deg, #2B6FE8 0%, #1C60DA 100%)'
          : 'linear-gradient(135deg, #1C60DA 0%, #1550C8 100%)',
        color: '#fff',
        border: 'none',
        borderRadius: 9999,
        padding: '9px 22px',
        cursor: 'pointer',
        transition: 'all 320ms cubic-bezier(0.4,0,0.2,1)',
        whiteSpace: 'nowrap',
        display: 'inline-flex', alignItems: 'center', gap: 8,
        boxShadow: hov
          ? '0 0 0 1px rgba(110,150,255,0.6), 0 0 24px rgba(28,96,218,0.45), 0 10px 28px rgba(28,96,218,0.5)'
          : '0 0 0 1px rgba(28,96,218,0.3), 0 4px 14px rgba(28,96,218,0.25)',
        transform: hov ? 'translateY(-1.5px)' : 'none',
        isolation: 'isolate',
      }}
    >
      {/* Inner sheen on hover */}
      <span
        aria-hidden="true"
        style={{
          position: 'absolute', inset: 0, borderRadius: 9999,
          background: 'linear-gradient(135deg, rgba(255,255,255,0.18) 0%, transparent 50%)',
          opacity: hov ? 1 : 0,
          transition: 'opacity 320ms ease',
          pointerEvents: 'none',
        }}
      />
      {t.cta}
      <svg width="11" height="11" fill="none" stroke="currentColor" strokeWidth="2.2" viewBox="0 0 24 24"
        style={{
          transition: 'transform 360ms cubic-bezier(0.2,0.7,0.1,1)',
          transform: hov ? 'translateX(2px)' : 'translateX(0)',
        }}>
        <path d="M5 12h14M12 5l7 7-7 7"/>
      </svg>
    </button>
  );
}

Object.assign(window, { Nav });
