function Nav({ theme }) {
	const [open, setOpen] = React.useState(false);
	const [scrolled, setScrolled] = React.useState(false);

	React.useEffect(() => {
		const onScroll = () => setScrolled(window.scrollY > 12);
		onScroll();
		window.addEventListener('scroll', onScroll, { passive: true });
		return () => window.removeEventListener('scroll', onScroll);
	}, []);

	const items = [
		{ label: 'Work',    href: '#work' },
		{ label: 'About',   href: '#about' },
		{ label: 'Patreon', href: '#patreon' },
	];

	const barStyle = {
		position: 'fixed', top: 0, left: 0, right: 0, zIndex: 50,
		backdropFilter:       scrolled ? 'saturate(180%) blur(14px)' : 'blur(0px)',
		WebkitBackdropFilter: scrolled ? 'saturate(180%) blur(14px)' : 'blur(0px)',
		background:   scrolled ? 'color-mix(in oklab, var(--bg) 78%, transparent)' : 'transparent',
		borderBottom: scrolled ? '1px solid var(--divider)' : '1px solid transparent',
		transition: 'background .35s ease, border-color .35s ease, backdrop-filter .35s ease',
	};

	return (
		<header style={barStyle}>
			<div className="container" style={{ display: 'flex', alignItems: 'center', gap: 24, height: 72 }}>
				<a href="#top" aria-label="Koelet3D home" style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
					<img src="assets/koelet3d-logo.png" alt="Koelet3D" width="32" height="32" style={{ display: 'block' }}/>
					<span className="display" style={{ fontSize: 20, letterSpacing: '-0.025em', fontWeight: 700, color: 'var(--fg)' }}>
						Koelet3D
					</span>
				</a>

				<div style={{ flex: 1 }} />

				<nav className="nav-desktop" style={{ display: 'flex', gap: 36, alignItems: 'center' }}>
					{items.map(it => (
						<a key={it.href} href={it.href} style={navLinkStyle}>{it.label}</a>
					))}
				</nav>

				<div style={{ display: 'flex', gap: 4, alignItems: 'center' }} className="nav-socials">
					<a href="https://x.com/koelet3d" target="_blank" rel="noopener noreferrer" aria-label="X (Twitter)" style={socialIconStyle}><XIcon/></a>
					<a href="https://bsky.app/profile/koelet3d.bsky.social" target="_blank" rel="noopener noreferrer" aria-label="Bluesky" style={socialIconStyle}><BlueskyIcon/></a>
				</div>

				<a
					href="https://patreon.com/koelet3d"
					target="_blank"
					rel="noopener noreferrer"
					className="cta-pill"
					style={{
						background: 'var(--accent)', color: 'var(--accent-ink)',
						padding: '10px 18px', borderRadius: 999, fontSize: 13.5, fontWeight: 500,
						letterSpacing: 0.01,
						display: 'inline-flex', alignItems: 'center', gap: 8,
						boxShadow: '0 1px 0 rgba(255,255,255,0.12) inset, 0 6px 20px -10px color-mix(in oklab, var(--accent) 70%, transparent)',
					}}
				>
					Join Patreon
					<ArrowUpRight/>
				</a>

				<button
					className="nav-hamburger"
					aria-label="Open menu"
					onClick={() => setOpen(o => !o)}
					style={{
						display: 'none',
						background: 'transparent', border: '1px solid var(--divider)',
						width: 36, height: 36, borderRadius: 999, cursor: 'pointer', color: 'var(--fg)',
						alignItems: 'center', justifyContent: 'center',
					}}
				>
					<MenuIcon open={open}/>
				</button>
			</div>

			<div style={{ display: open ? 'block' : 'none', borderTop: '1px solid var(--divider)', background: 'var(--bg)' }}>
				<div className="container" style={{ padding: '12px 20px 20px' }}>
					{items.map(it => (
						<a key={it.href} href={it.href} onClick={() => setOpen(false)}
							style={{ display: 'block', padding: '14px 0', fontSize: 18, borderBottom: '1px solid var(--divider)' }}>
							{it.label}
						</a>
					))}
				</div>
			</div>

			<style>{`
				.nav-desktop a:hover { color: var(--accent); }
				.cta-pill:hover { transform: translateY(-1px); }
				.cta-pill { transition: transform .2s ease; }
				.nav-socials a:hover { color: var(--fg); background: var(--surface); }
				@media (max-width: 820px) {
					.nav-desktop { display: none !important; }
					.nav-socials { display: none !important; }
					.nav-hamburger { display: inline-flex !important; }
				}
			`}</style>
		</header>
	);
}

const socialIconStyle = {
	display: 'flex', alignItems: 'center', justifyContent: 'center',
	width: 32, height: 32, borderRadius: 8,
	color: 'var(--fg-soft)', transition: 'color .2s ease, background .2s ease',
};

const navLinkStyle = {
	fontSize: 13.5, letterSpacing: 0.02, color: 'var(--fg)', fontWeight: 400, transition: 'color .2s ease',
};

function XIcon() {
	return (
		<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
			<path d="M14.234 10.162 22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299-.929-1.329L3.076 1.56h3.182l5.965 8.532.929 1.329 7.754 11.09h-3.182z"/>
		</svg>
	);
}
function BlueskyIcon() {
	return (
		<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
			<path d="M5.202 2.857C7.954 4.922 10.913 9.11 12 11.358c1.087-2.247 4.046-6.436 6.798-8.501C20.783 1.366 24 .213 24 3.883c0 .732-.42 6.156-.667 7.037-.856 3.061-3.978 3.842-6.755 3.37 4.854.826 6.089 3.562 3.422 6.299-5.065 5.196-7.28-1.304-7.847-2.97-.104-.305-.152-.448-.153-.327 0-.121-.05.022-.153.327-.568 1.666-2.782 8.166-7.847 2.97-2.667-2.737-1.432-5.473 3.422-6.3-2.777.473-5.899-.308-6.755-3.369C.42 10.04 0 4.615 0 3.883c0-3.67 3.217-2.517 5.202-1.026"/>
		</svg>
	);
}
function ArrowUpRight() {
	return (
		<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
			<path d="M3 9L9 3M9 3H4M9 3V8" stroke="currentColor" strokeWidth="1.4" strokeLinecap="square"/>
		</svg>
	);
}
function MenuIcon({ open }) {
	return (
		<svg width="16" height="16" viewBox="0 0 16 16" fill="none">
			<path d={open ? 'M3 3L13 13M13 3L3 13' : 'M2 5H14M2 11H14'} stroke="currentColor" strokeWidth="1.4" strokeLinecap="square"/>
		</svg>
	);
}

window.Nav = Nav;
