const PILLARS = [
  { id: 'p1', icon: 'code-xml', title: 'Custom Software & Product Development', desc: 'Web and SaaS apps, internal tools, APIs, modernization.' },
  { id: 'p2', icon: 'phone-call', title: 'AI Agents & Customer Communication', desc: 'Voice and chat agents, omnichannel follow-ups, warm handoffs.' },
  { id: 'p3', icon: 'workflow', title: 'Business Process & Operations Automation', desc: 'CRM sync, back-office tasks, approvals, reporting pipelines.' },
  { id: 'p4', icon: 'megaphone', title: 'Digital Marketing & Content Operations', desc: 'Lead nurturing, campaign ops, human-reviewed drafts and scripts.' },
];

function NavLink({ children, onClick, active, hasMenu }) {
  const [h, setH] = React.useState(false);
  const { Icon } = DS;
  return (
    <button type="button" onClick={onClick} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{ display: 'inline-flex', alignItems: 'center', gap: 4, height: 36, padding: '0 12px', borderRadius: 8, border: 0, background: h || active ? 'var(--yk-light)' : 'transparent', color: 'var(--yk-indigo)', font: '500 14px/1 var(--font-body)', cursor: 'pointer', whiteSpace: 'nowrap', transition: 'background .15s' }}>
      {children}
      {hasMenu && <Icon name="chevron-down" size={15} style={{ transform: active ? 'rotate(180deg)' : 'none', transition: 'transform .2s' }} />}
    </button>
  );
}

function MegaItem({ p, onPick }) {
  const [h, setH] = React.useState(false);
  const { Icon } = DS;
  return (
    <button type="button" onClick={() => onPick(p.id)} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{ display: 'flex', gap: 14, alignItems: 'flex-start', textAlign: 'left', padding: 14, borderRadius: 12, border: `1px solid ${h ? 'var(--border-accent)' : 'transparent'}`, background: h ? 'var(--yk-light)' : 'transparent', cursor: 'pointer', transition: 'all .15s' }}>
      <span style={{ width: 36, height: 36, flex: 'none', borderRadius: 9, display: 'grid', placeItems: 'center', background: '#fff', border: '1px solid var(--border-soft)', color: 'var(--yk-violet)' }}><Icon name={p.icon} size={18} /></span>
      <span>
        <span style={{ display: 'block', font: '600 14px/1.35 var(--font-display)', color: 'var(--yk-indigo)' }}>{p.title}</span>
        <span style={{ display: 'block', font: '400 13px/1.5 var(--font-body)', color: 'var(--text-muted)', marginTop: 3 }}>{p.desc}</span>
      </span>
    </button>
  );
}

function Nav({ onPillar }) {
  const { Button, Logo, IconButton } = DS;
  const [menu, setMenu] = React.useState(false);
  const [mobile, setMobile] = React.useState(false);
  const wide = useMedia('(min-width: 1120px)');
  const go = (id) => { setMenu(false); setMobile(false); kitScroll(id); };
  const pick = (pid) => { onPillar && onPillar(pid); go('services'); };
  const links = [['Solutions', 'solutions'], ['How We Build', 'process'], ['Blueprints', 'blueprints'], ['FAQ', 'faq']];
  return (
    <header style={{ position: 'sticky', top: 0, zIndex: 50, background: 'var(--glass-bg)', backdropFilter: 'var(--glass-blur)', WebkitBackdropFilter: 'var(--glass-blur)', boxShadow: '0 1px 0 rgba(255,255,255,.7) inset', borderBottom: '1px solid var(--yk-lavender)' }} onMouseLeave={() => setMenu(false)}>
      <ScrollProgressBar />
      <Container style={{ height: 'var(--nav-height)', display: 'flex', alignItems: 'center', gap: 24 }}>
        <a href="#" onClick={(e) => { e.preventDefault(); window.scrollTo({ top: 0, behavior: 'smooth' }); }} style={{ display: 'flex' }}><Logo base="../../" height={30} /></a>
        {wide && <nav style={{ display: 'flex', gap: 2, marginLeft: 8 }}>
          <span onMouseEnter={() => setMenu(true)}><NavLink hasMenu active={menu} onClick={() => setMenu(!menu)}>Services</NavLink></span>
          {links.map(([l, id]) => <span key={id} onMouseEnter={() => setMenu(false)}><NavLink onClick={() => go(id)}>{l}</NavLink></span>)}
        </nav>}
        <span style={{ flex: 1 }} />
        {wide ? <>
          <a href="#blueprints" onClick={(e) => { e.preventDefault(); go('blueprints'); }} style={{ font: '500 14px var(--font-body)', color: 'var(--yk-indigo)', whiteSpace: 'nowrap' }}>Explore Blueprints</a>
          <Button arrow onClick={() => go('scope')}>Book a Discovery Call</Button>
        </> : <IconButton icon={mobile ? 'x' : 'menu'} label="Menu" variant="outline" onClick={() => setMobile(!mobile)} />}
      </Container>
      {wide && <div style={{ position: 'absolute', left: 0, right: 0, top: '100%', pointerEvents: menu ? 'auto' : 'none' }}>
        <Container>
          <div onMouseEnter={() => setMenu(true)} style={{ marginLeft: 140, maxWidth: 760, background: 'var(--glass-bg-strong)', backdropFilter: 'var(--glass-blur)', WebkitBackdropFilter: 'var(--glass-blur)', border: '1px solid var(--glass-border)', borderRadius: 16, boxShadow: 'var(--shadow-lg)', padding: 10, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 4, opacity: menu ? 1 : 0, transform: menu ? 'none' : 'translateY(-6px)', transition: 'opacity .2s, transform .25s cubic-bezier(.22,1,.36,1)' }}>
            {PILLARS.map((p) => <MegaItem key={p.id} p={p} onPick={pick} />)}
          </div>
        </Container>
      </div>}
      {!wide && mobile && <div style={{ borderTop: '1px solid var(--yk-lavender)', background: '#fff', padding: '12px var(--container-pad) 20px', display: 'flex', flexDirection: 'column', gap: 4 }}>
        {PILLARS.map((p) => <MegaItem key={p.id} p={p} onPick={pick} />)}
        <div style={{ height: 1, background: 'var(--yk-lavender)', margin: '8px 0' }} />
        {links.map(([l, id]) => <button key={id} onClick={() => go(id)} style={{ textAlign: 'left', padding: '12px 14px', border: 0, background: 'none', font: '500 15px var(--font-body)', color: 'var(--yk-indigo)', cursor: 'pointer' }}>{l}</button>)}
        <Button arrow fullWidth onClick={() => go('scope')} style={{ marginTop: 8 }}>Book a Discovery Call</Button>
      </div>}
    </header>
  );
}

Object.assign(window, { Nav, PILLARS });
