/* global React, ReactDOM, HeroWordmark, Descent, Identity, Systems, Approach, Methodology, Industries, Reliability, Engagement, Footer, TweaksPanel, useTweaks, TweakSection, TweakSelect, TweakRadio, TweakColor, TweakButton */

const { useEffect, useState } = React;

const PALETTES = [
  { id: 'default',    name: 'Pink Mosque',     swatches: ['#c8345e', '#234ba8', '#e3a02e', '#2a7359', '#6b2876'] },
  { id: 'botanical',  name: 'Botanical Press', swatches: ['#b15639', '#1f3a4f', '#c2902f', '#5e6b34', '#8d4a5c'] },
  { id: 'twilight',   name: 'Persian Twilight',swatches: ['#c4203a', '#1a2780', '#e8b330', '#166953', '#4b1d6e'] },
  { id: 'midnight',   name: 'Midnight Tessera',swatches: ['#e25478', '#4d6dde', '#f1bd55', '#3fa07a', '#a85cba'] },
];

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "palette": "default",
  "density": "max"
}/*EDITMODE-END*/;

function Nav() {
  return (
    <nav className="nav">
      <a href="#top" className="nav-brand" style={{ textDecoration: 'none' }}>
        <span className="stamp"></span>
        eth3rSystems
      </a>
      <div className="nav-links">
        <a href="#systems">Systems</a>
        <a href="#industries">Industries</a>
        <a href="#approach">Approach</a>
        <a href="#methodology">Methodology</a>
      </div>
      <a href="#contact" className="nav-cta">$ Start a project →</a>
    </nav>
  );
}

function Hero() {
  return (
    <section className="hero" id="top" data-screen-label="01 Dawn">
      <div className="wrap">
        <div className="hero-marker">
          <div>01 — composition</div>
          <div>an instrument of assembly</div>
        </div>

        <HeroWordmark />

        <div style={{ textAlign: 'center', margin: '0 auto 6vh', maxWidth: 760 }}>
          <div className="hero-tag">Many pieces · one whole</div>
          <div className="hero-headline">
            A system built one tile at a time. Each piece chosen on purpose.
            <em> Each one belongs.</em>
          </div>
        </div>

        <div className="hero-bottom">
          <div>
            <div className="hero-tag">ETH3R Systems LLC · est. 2024</div>
            <div className="hero-headline" style={{ fontSize: 'clamp(22px, 2.4vw, 36px)' }}>
              Digital infrastructure for businesses that need it to <em>actually work.</em>
            </div>
          </div>
          <div style={{
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'flex-end',
            gap: 18,
          }}>
            <div className="mono" style={{ fontSize: 11, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'var(--ink-mute)' }}>
              v3.1 ↘ scroll to walk
            </div>
            <div style={{ display: 'flex', gap: 8 }}>
              <a href="#systems" style={{
                padding: '12px 18px',
                background: 'var(--ink)',
                color: 'var(--ground)',
                textDecoration: 'none',
                fontFamily: 'var(--mono)',
                fontSize: 12,
                letterSpacing: '0.04em',
              }}>
                Explore Systems →
              </a>
              <a href="#contact" style={{
                padding: '12px 18px',
                background: 'var(--t1)',
                color: 'var(--ground)',
                textDecoration: 'none',
                fontFamily: 'var(--mono)',
                fontSize: 12,
                letterSpacing: '0.04em',
              }}>
                Start a Project
              </a>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  // apply palette + density to <html>
  useEffect(() => {
    const root = document.documentElement;
    PALETTES.forEach(p => root.classList.remove(`palette-${p.id}`));
    root.classList.add(`palette-${t.palette}`);
    root.setAttribute('data-density', t.density);
  }, [t.palette, t.density]);

  return (
    <>
      <Nav />
      <main>
        <Hero />
        <Descent />
        <Identity />
        <Systems />
        <Approach />
        <Methodology />
        <Industries />
        <Reliability />
        <Engagement />
      </main>
      <Footer />

      <div className="side-rail">eth3r · v3.1 · mosaic</div>

      <TweaksPanel title="Tweaks">
        <TweakSection title="Palette">
          <div style={{ display: 'grid', gap: 10 }}>
            {PALETTES.map(p => (
              <button
                key={p.id}
                onClick={() => setTweak('palette', p.id)}
                style={{
                  display: 'flex',
                  alignItems: 'center',
                  gap: 12,
                  padding: '10px 12px',
                  border: t.palette === p.id ? '1px solid var(--ink, #1a1411)' : '1px solid color-mix(in oklab, #1a1411 14%, transparent)',
                  background: t.palette === p.id ? 'color-mix(in oklab, #1a1411 4%, transparent)' : 'transparent',
                  cursor: 'pointer',
                  fontFamily: 'var(--mono)',
                  fontSize: 12,
                  textAlign: 'left',
                  color: 'inherit',
                }}
              >
                <div style={{ display: 'flex', gap: 2 }}>
                  {p.swatches.map((s, i) => (
                    <span key={i} style={{ width: 14, height: 22, background: s, display: 'block' }} />
                  ))}
                </div>
                <span>{p.name}</span>
              </button>
            ))}
          </div>
        </TweakSection>

        <TweakSection title="Density">
          <TweakRadio
            value={t.density}
            onChange={v => setTweak('density', v)}
            options={[
              { value: 'low',  label: 'Low' },
              { value: 'mid',  label: 'Mid' },
              { value: 'high', label: 'High' },
              { value: 'max',  label: 'Max' },
            ]}
          />
        </TweakSection>

        <TweakSection title="Replay Hero">
          <TweakButton
            label="↻ Re-assemble wordmark"
            onClick={() => {
              const el = document.querySelector('.hero-wordmark');
              if (!el) return;
              el.querySelectorAll('.hero-tile, .dust').forEach(tile => {
                tile.style.animation = 'none';
                void tile.offsetWidth;
                tile.style.animation = '';
              });
            }}
          />
        </TweakSection>
      </TweaksPanel>
    </>
  );
}

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