/* Smooth-scroll helper that accounts for the fixed nav height */
function scrollToSection(id) {
  const el = document.getElementById(id);
  if (!el) return;
  const isMobile = window.matchMedia('(max-width: 767px)').matches;
  const navOffset = isMobile ? 84 : 110;
  const top = el.getBoundingClientRect().top + window.scrollY - navOffset;
  window.scrollTo({ top, behavior: 'smooth' });
}

/* Sticky nav with Apple "liquid glass" effect when scrolled */
function Nav() {
  const [scrolled, setScrolled] = React.useState(false);
  const isMobile = useIsMobile();
  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 80);
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  return (
    <>
      <style>{`
        @keyframes navSheen {
          0%   { transform: translateX(-120%); }
          100% { transform: translateX(120%); }
        }
      `}</style>

      <header style={{
        position:'fixed', top: scrolled ? (isMobile?10:14) : 0, left: scrolled ? (isMobile?10:'max(16px, calc(50vw - 420px))') : 0, right: scrolled ? (isMobile?10:'max(16px, calc(50vw - 420px))') : 0,
        zIndex:100,
        height: isMobile ? (scrolled ? 52 : 80) : (scrolled ? 64 : 160),
        transition:'top 500ms cubic-bezier(0.22,1,0.36,1), left 500ms cubic-bezier(0.22,1,0.36,1), right 500ms cubic-bezier(0.22,1,0.36,1), border-radius 500ms cubic-bezier(0.22,1,0.36,1), height 500ms cubic-bezier(0.22,1,0.36,1)',
        borderRadius: scrolled ? 500 : 0,
        overflow:'hidden',
      }}>
        {/* Glass base - heavy blur + saturation boost (Apple-style) */}
        <div style={{
          position:'absolute', inset:0,
          background: scrolled
            ? 'linear-gradient(180deg, rgba(28,24,20,0.55) 0%, rgba(18,15,12,0.45) 100%)'
            : 'transparent',
          backdropFilter: scrolled ? 'blur(28px) saturate(200%)' : 'none',
          WebkitBackdropFilter: scrolled ? 'blur(28px) saturate(200%)' : 'none',
          transition:'background 400ms',
        }} />

        {/* Inner highlight - top edge glow that liquid glass has */}
        <div style={{
          position:'absolute', inset:0, borderRadius:'inherit', pointerEvents:'none',
          boxShadow: scrolled
            ? 'inset 0 1px 0 0 rgba(255,230,180,0.22), inset 0 0 0 1px rgba(238,179,50,0.18), inset 0 -1px 0 0 rgba(0,0,0,0.3), 0 18px 40px -12px rgba(0,0,0,0.55), 0 0 32px -8px rgba(238,179,50,0.20)'
            : 'none',
          transition:'box-shadow 400ms',
        }} />

        {/* Specular highlight sheen - soft moving light */}
        <div style={{
          position:'absolute', inset:0, borderRadius:'inherit', overflow:'hidden', pointerEvents:'none',
          opacity: scrolled ? 1 : 0, transition:'opacity 600ms',
        }}>
          <div style={{
            position:'absolute', top:'-50%', left:0, width:'40%', height:'200%',
            background: 'linear-gradient(105deg, transparent 0%, rgba(255,230,180,0.10) 45%, rgba(255,230,180,0.22) 50%, rgba(255,230,180,0.10) 55%, transparent 100%)',
            filter:'blur(2px)',
            animation: scrolled ? 'navSheen 7s ease-in-out infinite' : 'none',
          }} />
        </div>

        {/* Subtle top gradient - liquid surface curvature */}
        <div style={{
          position:'absolute', inset:0, borderRadius:'inherit', pointerEvents:'none',
          background: scrolled ? 'linear-gradient(180deg, rgba(255,255,255,0.08) 0%, rgba(255,255,255,0) 50%)' : 'none',
        }} />

        {/* Content */}
        <div style={{position:'relative', height:'100%', maxWidth:1280, margin:'0 auto', width:'100%', padding: isMobile ? '0 12px' : '0 24px', display:'flex', flexDirection:'row-reverse', alignItems:'center', justifyContent:'space-between', gap: isMobile ? 8 : 24}}>
          <div style={{display:'flex', alignItems:'center', gap: isMobile ? 8 : 14}}>
            <img
              src="logo.png"
              alt="Yakir Abadi"
              style={{
                height: isMobile ? (scrolled ? 38 : 60) : (scrolled ? 48 : 138), width:'auto', display:'block',
                filter: scrolled ? 'drop-shadow(0 0 12px rgba(238,179,50,0.45))' : 'drop-shadow(0 0 14px rgba(238,179,50,0.35))',
                transition:'filter 400ms, height 500ms cubic-bezier(0.22,1,0.36,1)',
              }}
            />
            {!isMobile && (
              <span style={{
                fontFamily:'Open Sans, sans-serif',
                fontWeight:700,
                fontSize:20,
                color:'#F5F2ED',
                letterSpacing:'-0.01em',
                display:'inline-flex',
                alignItems:'baseline',
                gap:'0.3em',
                whiteSpace:'nowrap',
              }}>
                <span style={{color:'rgba(238,179,50,0.45)', fontSize:'0.7em', fontWeight:400}}>·</span>
                קורס AI
                <span style={{
                  background:'linear-gradient(180deg, #FFD777 0%, #EEB332 50%, #B27418 100%)',
                  WebkitBackgroundClip:'text',
                  WebkitTextFillColor:'transparent',
                  backgroundClip:'text',
                  fontWeight:900,
                  filter:'drop-shadow(0 0 10px rgba(238,179,50,0.45))',
                  letterSpacing:'-0.015em',
                }}>חדשני</span>
              </span>
            )}
          </div>
          <nav style={{display:'flex', gap: isMobile ? 12 : 28, alignItems:'center'}}>
            {[
              { label: 'סילבוס',  id: 'syllabus' },
              { label: 'מה תבנו', id: 'builds'   },
              { label: 'מחיר',    id: 'pricing'  },
            ].map(l => (
              <a
                key={l.id}
                href={`#${l.id}`}
                onClick={(e) => { e.preventDefault(); scrollToSection(l.id); }}
                style={{color:'rgba(245,242,237,0.78)', fontSize: isMobile ? 12 : 14, textDecoration:'none', cursor:'pointer', transition:'color 200ms', whiteSpace:'nowrap'}}
                onMouseEnter={(e) => { e.currentTarget.style.color = '#EEB332'; }}
                onMouseLeave={(e) => { e.currentTarget.style.color = 'rgba(245,242,237,0.78)'; }}
              >{l.label}</a>
            ))}
            <button onClick={() => scrollToSection('pricing')} style={{
              background: scrolled ? 'rgba(238,179,50,0.10)' : 'transparent',
              color:'#EEB332',
              border:'1.5px solid rgba(238,179,50,0.55)',
              borderRadius:500, padding: isMobile ? '5px 12px' : '8px 20px',
              fontSize: isMobile ? 11 : 13, fontFamily:'Open Sans, sans-serif', fontWeight:600, cursor:'pointer',
              backdropFilter: scrolled ? 'blur(8px)' : 'none',
              WebkitBackdropFilter: scrolled ? 'blur(8px)' : 'none',
              boxShadow:'0 0 16px 0 rgba(238,179,50,0.3), inset 0 1px 0 rgba(255,230,180,0.2)',
              transition:'background 400ms',
              whiteSpace:'nowrap',
            }}>הירשם</button>
          </nav>
        </div>
      </header>
    </>
  );
}
window.Nav = Nav;
