function App() {
  const [tweaks, setTweaks] = React.useState({
    variant: 'A',
    theme: 'light',
    accent: '#E74011',
    glass: 'subtle',
  });
  const [active, setActive] = React.useState('foundations');

  // Apply tweaks to <html>
  React.useEffect(() => {
    const root = document.documentElement;
    root.setAttribute('data-variant', tweaks.variant);
    root.setAttribute('data-theme', tweaks.theme);
    root.setAttribute('data-glass', tweaks.glass);
    root.style.setProperty('--accent', tweaks.accent);
  }, [tweaks]);

  const setTweak = (k, v) => setTweaks(t => ({ ...t, [k]: v }));

  // Scrollspy
  React.useEffect(() => {
    const ids = ['foundations', 'components', 'patterns', 'examples', 'changelog'];
    const els = ids.map(id => document.getElementById(id)).filter(Boolean);
    if (!els.length) return;
    const io = new IntersectionObserver(() => {
      const visible = els
        .map(el => ({ id: el.id, rect: el.getBoundingClientRect() }))
        .filter(({ rect }) => rect.bottom > 80 && rect.top < window.innerHeight * 0.5);
      if (visible.length) {
        const sorted = [...visible].sort((a, b) => a.rect.top - b.rect.top);
        const past = sorted.filter(v => v.rect.top <= 140);
        setActive((past.length ? past[past.length - 1] : sorted[0]).id);
      }
    }, { rootMargin: '-72px 0px -50% 0px', threshold: [0, 0.01, 0.5, 1] });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  }, []);

  const navTo = id => {
    const el = document.getElementById(id);
    if (!el) return;
    const y = el.getBoundingClientRect().top + window.scrollY - 8;
    window.scrollTo({ top: y, behavior: 'smooth' });
  };

  return (
    <div className="app">
      <BrTopbar active={active} onNav={navTo} onSearchClick={() => alert('⌘K — Command palette (not wired in kit)')} />

      <main>
        <BrHero variant={tweaks.variant} />

        <span id="foundations" className="anchor" />
        <FoundationsSection />

        <span id="components" className="anchor" />
        <ButtonsSection />
        <FormsSection />
        <CardsSection />

        <span id="patterns" className="anchor" />

        <span id="examples" className="anchor" />
        <ExamplesSection />

        <span id="changelog" className="anchor" />
        <ColophonSection />
      </main>

      <BrTweaksPanel tweaks={tweaks} setTweak={setTweak} />
    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
