// app.jsx — Root component for Arkwright Systems homepage.

const { useState, useEffect } = React;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "heroVariant": "beforeafter",
  "dark": false
}/*EDITMODE-END*/;

// ─────────────────────────────────────────────────────────────────────────────
// Top chrome: wordmark + nav pill + CTA. Tracks scroll progress.
// ─────────────────────────────────────────────────────────────────────────────

function TopChrome({ onToggleMenu, onToggleTheme, dark, scrollPct, menuOpen }) {
  return (
    <div className="top-chrome">
      <a className="wordmark" href="/">
        <span className="glyph">A</span>
        Arkwright Systems<sup>®</sup>
      </a>

      <nav className="nav-pill" aria-label="Primary">
        <button className="nav-segment" type="button" onClick={onToggleMenu}>
          {menuOpen ? (
            <>
              <svg className="nav-icon" viewBox="0 0 24 24">
                <line x1="6" y1="6" x2="18" y2="18"></line>
                <line x1="18" y1="6" x2="6" y2="18"></line>
              </svg>
              Close
            </>
          ) : (
            <>
              <svg className="nav-icon" viewBox="0 0 24 24">
                <line x1="3" y1="6" x2="21" y2="6"></line>
                <line x1="3" y1="12" x2="21" y2="12"></line>
                <line x1="3" y1="18" x2="21" y2="18"></line>
              </svg>
              Menu
            </>
          )}
        </button>
        <button className="nav-segment" type="button" onClick={onToggleTheme} aria-label="Toggle theme">
          {dark ? (
            <svg className="nav-icon" viewBox="0 0 24 24">
              <circle cx="12" cy="12" r="4"></circle>
              <line x1="12" y1="2" x2="12" y2="5"></line>
              <line x1="12" y1="19" x2="12" y2="22"></line>
              <line x1="2" y1="12" x2="5" y2="12"></line>
              <line x1="19" y1="12" x2="22" y2="12"></line>
              <line x1="4.6" y1="4.6" x2="6.7" y2="6.7"></line>
              <line x1="17.3" y1="17.3" x2="19.4" y2="19.4"></line>
              <line x1="4.6" y1="19.4" x2="6.7" y2="17.3"></line>
              <line x1="17.3" y1="6.7" x2="19.4" y2="4.6"></line>
            </svg>
          ) : (
            <svg className="nav-icon" viewBox="0 0 24 24">
              <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
            </svg>
          )}
        </button>
        <div className="nav-segment">
          <span className="nav-percent">{scrollPct}%</span>
        </div>
      </nav>

      <a className="cta-pill" href="#build"><SplitText>Build a system →</SplitText></a>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// App
// ─────────────────────────────────────────────────────────────────────────────

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [menuOpen, setMenuOpen] = useState(false);
  const [scrollPct, setScrollPct] = useState(0);

  // Apply theme to <html> so it covers everything (including drawer scroll bg)
  useEffect(() => {
    const root = document.documentElement;
    if (t.dark) root.setAttribute('data-theme', 'dark');
    else root.removeAttribute('data-theme');
  }, [t.dark]);

  // Scroll progress
  useEffect(() => {
    const update = () => {
      const max = document.documentElement.scrollHeight - window.innerHeight;
      const next = max <= 0 ? 0 : Math.round((window.scrollY / max) * 100);
      setScrollPct(Math.max(0, Math.min(100, next)));
    };
    update();
    window.addEventListener('scroll', update, { passive: true });
    window.addEventListener('resize', update);
    return () => {
      window.removeEventListener('scroll', update);
      window.removeEventListener('resize', update);
    };
  }, []);

  // Menu is a centered dropdown — no scroll lock needed.

  const toggleTheme = () => setTweak('dark', !t.dark);

  return (
    <>
      <TopChrome
        onToggleMenu={() => setMenuOpen((v) => !v)}
        onToggleTheme={toggleTheme}
        dark={t.dark}
        scrollPct={scrollPct}
        menuOpen={menuOpen}
      />
      <MenuDrawer
        open={menuOpen}
        onClose={() => setMenuOpen(false)}
      />
      <main>
        <Hero variant={t.heroVariant} />
        <CategoryStatement />
        <ComponentsSection />
        <EnvironmentsSection />
        <StagesSection />
        <RolesSection />
        <ProofSection />
        <PromiseSection />
        <VisionClose />
      </main>
      <SiteFooter />

      <TweaksPanel title="Arkwright tweaks">
        <TweakSection label="Theme" />
        <TweakToggle
          label="Dark mode"
          value={t.dark}
          onChange={(v) => setTweak('dark', v)}
        />
        <TweakSection label="Hero" />
        <TweakRadio
          label="Variant"
          value={t.heroVariant}
          options={[
            { value: 'beforeafter', label: 'Before / after' },
            { value: 'constellation', label: 'Constellation' },
            { value: 'single', label: 'Single shot' },
          ]}
          onChange={(v) => setTweak('heroVariant', v)}
        />
      </TweaksPanel>
    </>
  );
}

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