// Pellex landing page sections. Mobile-first. Each section is a React component.
// Uses window.PELLEX_PALETTES / FONTS / HEADLINES from pellex-tokens.jsx.
// Exposes everything to window at the bottom for cross-script use.

const { useState, useEffect, useRef, useMemo } = React;

// ─────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────

function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    if (!ref.current) return;
    const el = ref.current;
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => { if (e.isIntersecting) el.classList.add('in'); });
    }, { threshold: 0.12, rootMargin: '0px 0px -60px 0px' });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return ref;
}

function Reveal({ children, delay = 0, style }) {
  const ref = useReveal();
  return (
    <div ref={ref} className="reveal" style={{ transitionDelay: `${delay}ms`, ...style }}>
      {children}
    </div>
  );
}

// Tiny icon helpers (minimal stroke icons, no emoji)
const Icon = {
  arrow: (props={}) => (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" {...props}>
      <path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  arrowUpRight: (props={}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...props}>
      <path d="M4 10l6-6M5 4h5v5" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  check: (props={}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...props}>
      <path d="M2.5 7.5l3 3 6-7" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  spark: (props={}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...props}>
      <path d="M7 1v4M7 9v4M1 7h4M9 7h4" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  plus: (props={}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...props}>
      <path d="M7 2v10M2 7h10" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  minus: (props={}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...props}>
      <path d="M2 7h10" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  globe: (props={}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" {...props}>
      <circle cx="7" cy="7" r="5.5" stroke="currentColor" strokeWidth="1.2"/>
      <path d="M1.5 7h11M7 1.5c2 1.6 2 9.4 0 11M7 1.5c-2 1.6-2 9.4 0 11" stroke="currentColor" strokeWidth="1.2"/>
    </svg>
  ),
};

// ─────────────────────────────────────────────────────────────
// Reusable bits
// ─────────────────────────────────────────────────────────────

function Chip({ children, p, accent }) {
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: '5px 10px 5px 8px', borderRadius: 999,
      background: p.bgChip,
      border: `1px solid ${p.border}`,
      color: accent ? p.accent : p.textMute,
      fontSize: 11, letterSpacing: 0.4, textTransform: 'uppercase',
      fontFamily: PELLEX_FONTS.mono, fontWeight: 500,
      whiteSpace: 'nowrap',
    }}>
      <span style={{
        width: 5, height: 5, borderRadius: 99,
        background: accent ? p.accent : p.accent3,
        boxShadow: accent ? `0 0 8px ${p.accent}` : 'none',
      }}/>
      {children}
    </span>
  );
}

function PrimaryCTA({ p, children, full, onClick, sub }) {
  return (
    <button onClick={onClick} style={{
      appearance: 'none', border: 'none', cursor: 'pointer',
      display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 10,
      width: full ? '100%' : 'auto',
      padding: '15px 14px 15px 22px',
      borderRadius: 14,
      background: p.accent,
      color: p.accentInk,
      fontFamily: PELLEX_FONTS.body, fontWeight: 700,
      fontSize: 16, letterSpacing: -0.1,
      boxShadow: `0 8px 24px -8px ${p.accent}66, inset 0 1px 0 rgba(255,255,255,0.4), inset 0 -2px 0 rgba(0,0,0,0.12)`,
      transition: 'transform 120ms ease',
      textAlign: 'left',
    }}
      onMouseDown={e => e.currentTarget.style.transform = 'scale(0.98)'}
      onMouseUp={e => e.currentTarget.style.transform = ''}
      onMouseLeave={e => e.currentTarget.style.transform = ''}
    >
      <span style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', minWidth: 0, flex: 1 }}>
        <span style={{ whiteSpace: 'nowrap' }}>{children}</span>
        {sub && <span style={{ fontSize: 11, opacity: 0.7, fontWeight: 600, letterSpacing: 0.1, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', maxWidth: '100%' }}>{sub}</span>}
      </span>
      <span style={{
        width: 30, height: 30, borderRadius: 99,
        background: p.accentInk, color: p.accent,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        flexShrink: 0,
      }}><Icon.arrow /></span>
    </button>
  );
}

function GhostCTA({ p, children, onClick, full }) {
  return (
    <button onClick={onClick} style={{
      appearance: 'none', cursor: 'pointer',
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      width: full ? '100%' : 'auto',
      padding: '14px 18px',
      borderRadius: 14,
      background: 'transparent',
      border: `1px solid ${p.borderStrong}`,
      color: p.text,
      fontFamily: PELLEX_FONTS.body, fontWeight: 600,
      fontSize: 15, letterSpacing: -0.1,
      whiteSpace: 'nowrap',
    }}>
      {children}
      <Icon.arrowUpRight />
    </button>
  );
}

// ─────────────────────────────────────────────────────────────
// Section: Header (sticky)
// ─────────────────────────────────────────────────────────────
function Header({ p, lang, setLang }) {
  const [languageMenuOpen, setLanguageMenuOpen] = useState(false);
  const languageOptions = [
    { code: 'en', label: 'English', short: 'EN' },
    { code: 'tc', label: '繁體中文', short: '繁中' },
    { code: 'sc', label: '简体中文', short: '简中' },
  ];
  return (
    <div className="pellex-header" style={{
      position: 'sticky', top: 0, zIndex: 30,
      padding: '12px 18px',
      background: `${p.bg}cc`,
      backdropFilter: 'blur(14px) saturate(140%)',
      WebkitBackdropFilter: 'blur(14px) saturate(140%)',
      borderBottom: `1px solid ${p.border}`,
    }}>
      <div className="pellex-header-inner" style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <div style={{
            width: 26, height: 26, borderRadius: 8,
            background: p.accent, color: p.accentInk,
            display: 'grid', placeItems: 'center',
            fontFamily: PELLEX_FONTS.display, fontSize: 20, fontWeight: 600,
             lineHeight: 1,
            boxShadow: `inset 0 -2px 0 ${p.accentInk}22`,
          }}>P</div>
          <span style={{ color: p.text, fontFamily: PELLEX_FONTS.display, fontSize: 20, letterSpacing: -0.3, lineHeight: 1 }}>
            Pellex
          </span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <div data-no-i18n="true" style={{ position: 'relative' }}>
            <button
              aria-label="Change language"
              aria-haspopup="listbox"
              aria-expanded={languageMenuOpen}
              onClick={() => setLanguageMenuOpen(!languageMenuOpen)}
              style={{
                background: 'transparent', border: `1px solid ${p.border}`,
                padding: '5px 10px', borderRadius: 99,
                color: p.textMute, fontSize: 11, fontFamily: PELLEX_FONTS.mono,
                letterSpacing: 0.4, cursor: 'pointer', display: 'flex', gap: 5, alignItems: 'center',
              }}>
              <Icon.globe />
              {PELLEX_LANG_LABEL[lang] || 'EN'}
              <span aria-hidden="true" style={{ fontSize: 9, transform: languageMenuOpen ? 'rotate(180deg)' : 'none', transition: 'transform 160ms ease' }}>⌄</span>
            </button>
            {languageMenuOpen && (
              <div
                role="listbox"
                aria-label="Language options"
                style={{
                  position: 'absolute', right: 0, top: 'calc(100% + 8px)', minWidth: 140,
                  padding: 6, borderRadius: 14, background: p.bgElev,
                  border: `1px solid ${p.borderStrong || p.border}`,
                  boxShadow: '0 18px 40px rgba(0,0,0,0.35)', zIndex: 60,
                }}>
                {languageOptions.map((option) => {
                  const selected = lang === option.code;
                  return (
                    <button
                      key={option.code}
                      role="option"
                      aria-selected={selected}
                      onClick={() => { setLang(option.code); setLanguageMenuOpen(false); }}
                      style={{
                        width: '100%', border: 0, borderRadius: 10, padding: '9px 10px',
                        background: selected ? p.bgChip : 'transparent',
                        color: selected ? p.text : p.textMute, cursor: 'pointer',
                        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
                        fontFamily: PELLEX_FONTS.body, fontSize: 13, fontWeight: selected ? 700 : 500,
                        textAlign: 'left',
                      }}>
                      <span>{option.label}</span>
                      <span style={{ fontFamily: PELLEX_FONTS.mono, fontSize: 10, color: selected ? p.accent : p.textDim }}>
                        {selected ? '✓' : option.short}
                      </span>
                    </button>
                  );
                })}
              </div>
            )}
          </div>
          <a href="#kit" onClick={(e) => { e.preventDefault(); window.pellexNav('kit'); }} style={{
            padding: '6px 12px', borderRadius: 99,
            background: p.accent, color: p.accentInk,
            fontSize: 12, fontWeight: 700, textDecoration: 'none',
            fontFamily: PELLEX_FONTS.body, letterSpacing: -0.1,
            whiteSpace: 'nowrap',
          }}>Get the kit</a>
        </div>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Section: Hero
// ─────────────────────────────────────────────────────────────
function Hero({ p, headlineKey, density, lang }) {
  const h = PELLEX_HEADLINES[headlineKey] || PELLEX_HEADLINES.asset;
  const translatedH1 = lang === 'tc'
    ? '建立一個小型 AI 資產，讓它在你睡覺時也能運作。'
    : lang === 'sc'
      ? '建立一个小型 AI 资产，让它在你睡觉时也能运作。'
      : h.h1;
  const padY = density === 'compact' ? 28 : 44;
  return (
    <section className="site-hero" style={{
      padding: `${padY}px 22px 28px`,
      position: 'relative', overflow: 'hidden',
    }}>
      {/* glow */}
      <div style={{
        position: 'absolute', inset: '-40% -20% auto -20%', height: 380, zIndex: 0,
        background: `radial-gradient(60% 60% at 50% 30%, ${p.accent}1f 0%, transparent 70%)`,
        pointerEvents: 'none',
      }}/>
      <div className="site-hero-copy" style={{ position: 'relative', zIndex: 1 }}>
        <Reveal>
          <Chip p={p} accent>{h.eyebrow}</Chip>
        </Reveal>

        <Reveal delay={80}>
          <h1 style={{
            margin: '18px 0 0',
            fontFamily: PELLEX_FONTS.display,
            color: p.text,
            fontSize: 44, lineHeight: 1.02,
            letterSpacing: -0.8,
            fontWeight: 600,
            textWrap: 'balance',
          }}>
            <span style={{ color: p.textMute, fontSize: 32, lineHeight: 1.1, display: 'block', marginBottom: 6 }}>
              {h.h1pre}
            </span>
            {lang === 'en' ? h.h1.split(' ').map((w, i) => {
              const highlight = ['AI', 'asset', 'shovels', 'income', 'system,', 'sleep.', 'Sunday.'].includes(w);
              return (
                <span key={i} style={{
                  fontWeight: highlight ? 700 : 600,
                  color: highlight ? p.accent : p.text,
                  background: highlight ? `linear-gradient(180deg, transparent 62%, ${p.accent}25 62%)` : 'none',
                }}>{w}{i < h.h1.split(' ').length - 1 ? ' ' : ''}</span>
              );
            }) : <span data-no-i18n="true" style={{ color: p.text }}>{translatedH1}</span>}
          </h1>
        </Reveal>

        <Reveal delay={160}>
          <p style={{
            margin: '20px 0 28px',
            color: p.textMute, fontSize: 16, lineHeight: 1.5,
            fontFamily: PELLEX_FONTS.body, fontWeight: 400,
            maxWidth: 360, textWrap: 'pretty',
          }}>
            {h.sub}
          </p>
        </Reveal>

        <Reveal delay={240}>
          <div className="site-hero-actions" style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
            <PrimaryCTA p={p} full sub="Free · No credit card · ~5 min read"
              onClick={() => window.pellexNav('map')}>
              Show me the route
            </PrimaryCTA>
            <GhostCTA p={p} full onClick={() => window.pellexNav('kit')}>See the $9 Starter Kit</GhostCTA>
          </div>
        </Reveal>

        <Reveal delay={320}>
          <div className="site-proof-row" style={{
            marginTop: 22, display: 'flex', gap: 12, alignItems: 'center',
            color: p.textDim, fontSize: 12, fontFamily: PELLEX_FONTS.mono, letterSpacing: 0.4,
            flexWrap: 'nowrap', whiteSpace: 'nowrap',
          }}>
            <span style={{ display: 'inline-flex', gap: 6, alignItems: 'center' }}>
              <span style={{ color: p.accent }}><Icon.check/></span> Beginner-safe
            </span>
            <span style={{ width: 3, height: 3, borderRadius: 99, background: p.textDim }}/>
            <span style={{ display: 'inline-flex', gap: 6, alignItems: 'center' }}>
              <span style={{ color: p.accent }}><Icon.check/></span> No code
            </span>
            <span style={{ width: 3, height: 3, borderRadius: 99, background: p.textDim }}/>
            <span style={{ display: 'inline-flex', gap: 6, alignItems: 'center' }}>
              <span style={{ color: p.accent }}><Icon.check/></span> 1 weekend
            </span>
          </div>
        </Reveal>
      </div>
    </section>
  );
}

// ─────────────────────────────────────────────────────────────
// Section: Asset-machine visual (the "what you'll build" mock)
// ─────────────────────────────────────────────────────────────
function AssetMachine({ p }) {
  return (
    <Reveal>
      <section className="asset-machine" style={{ padding: '8px 22px 36px' }}>
        <div className="asset-machine-card" style={{
          position: 'relative',
          borderRadius: 22,
          border: `1px solid ${p.border}`,
          background: `linear-gradient(180deg, ${p.bgElev} 0%, ${p.bg} 100%)`,
          overflow: 'hidden',
          padding: '18px 16px 16px',
        }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 14 }}>
            <span style={{ color: p.textMute, fontSize: 11, fontFamily: PELLEX_FONTS.mono, letterSpacing: 0.6, textTransform: 'uppercase' }}>
              Your tiny machine
            </span>
            <span style={{ color: p.accent, fontSize: 11, fontFamily: PELLEX_FONTS.mono }}>● live</span>
          </div>

          {/* Free page → product → automation */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
            <MachineRow p={p} step="01" label="Free traffic asset"
              detail="ai-follow-up-for-cleaning-quotes.html"
              meta="312 visits / day" />
            <Connector p={p} />
            <MachineRow p={p} step="02" label="$9 prompt pack" accent
              detail="Cleaning Quote Follow-up Kit"
              meta="14 sold this week" sparkline />
            <Connector p={p} />
            <MachineRow p={p} step="03" label="Automated delivery"
              detail="Stripe → Email → Notion"
              meta="0 min / sale" />
          </div>

          <div style={{
            marginTop: 14, padding: 12, borderRadius: 12,
            background: p.bgChip, border: `1px dashed ${p.border}`,
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
          }}>
            <div style={{ fontSize: 11, color: p.textMute, fontFamily: PELLEX_FONTS.mono, letterSpacing: 0.4 }}>
              MONTH 1 RESULT
            </div>
            <div style={{ display: 'flex', alignItems: 'baseline', gap: 8 }}>
              <span style={{ color: p.accent, fontFamily: PELLEX_FONTS.display,  fontSize: 28, lineHeight: 1 }}>$487</span>
              <span style={{ color: p.textDim, fontSize: 11, fontFamily: PELLEX_FONTS.mono }}>example</span>
            </div>
          </div>
        </div>
      </section>
    </Reveal>
  );
}

function MachineRow({ p, step, label, detail, meta, accent, sparkline }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 12,
      padding: '12px 12px',
      borderRadius: 12,
      background: accent ? `${p.accent}10` : p.bgCard,
      border: `1px solid ${accent ? p.accent + '33' : p.border}`,
    }}>
      <div style={{
        width: 32, height: 32, borderRadius: 8,
        background: accent ? p.accent : 'rgba(255,255,255,0.04)',
        color: accent ? p.accentInk : p.textMute,
        display: 'grid', placeItems: 'center',
        fontFamily: PELLEX_FONTS.mono, fontSize: 11, fontWeight: 600, letterSpacing: 0.4,
        flexShrink: 0,
      }}>{step}</div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ color: p.text, fontSize: 13, fontWeight: 500, marginBottom: 2 }}>{label}</div>
        <div style={{
          color: p.textDim, fontSize: 11, fontFamily: PELLEX_FONTS.mono,
          whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
        }}>{detail}</div>
      </div>
      <div style={{ textAlign: 'right', flexShrink: 0 }}>
        {sparkline && <Spark p={p} />}
        <div style={{ color: accent ? p.accent : p.textMute, fontSize: 11, fontFamily: PELLEX_FONTS.mono, marginTop: 2 }}>
          {meta}
        </div>
      </div>
    </div>
  );
}

function Spark({ p }) {
  return (
    <svg width="50" height="14" viewBox="0 0 50 14" fill="none">
      <path d="M2 11 L10 9 L18 10 L26 6 L34 7 L42 3 L48 4" stroke={p.accent} strokeWidth="1.4" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
      <circle cx="48" cy="4" r="2" fill={p.accent}/>
    </svg>
  );
}

function Connector({ p }) {
  return (
    <div style={{ display: 'flex', justifyContent: 'center' }}>
      <div style={{ width: 1, height: 12, background: p.border }}/>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Section: 3 Rules
// ─────────────────────────────────────────────────────────────
function Rules({ p }) {
  const rules = [
    { n: '01', title: 'Content first, product second.', body: 'Publish a genuinely useful page before you ask for money. Help comes first — money follows the help.' },
    { n: '02', title: 'Sell the shovel, not the mine.', body: 'Don\'t promise income. Sell the templates, prompts and checklists that help someone move 10× faster today.' },
    { n: '03', title: 'Niche down hard.', body: '"AI business" is too broad. "AI follow-up kit for cleaning quotes" is buyable. Pick a tiny painful problem.' },
  ];
  return (
    <section className="rules-section desktop-section" style={{ padding: '36px 22px' }}>
      <Reveal>
        <Chip p={p}>What we learned</Chip>
        <h2 style={{
          margin: '14px 0 6px',
          fontFamily: PELLEX_FONTS.display,
          color: p.text, fontSize: 30, lineHeight: 1.05, letterSpacing: -0.5,
          fontWeight: 600, textWrap: 'balance',
        }}>
          Three rules we live by — copied from the people doing it well.
        </h2>
        <p style={{ color: p.textMute, fontSize: 14, lineHeight: 1.5, margin: '0 0 22px' }}>
          The pattern is boring on purpose. Boring is what actually works.
        </p>
      </Reveal>
      <div className="rules-grid" style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        {rules.map((r, i) => (
          <Reveal key={r.n} delay={i * 80}>
            <div style={{
              padding: 18,
              borderRadius: 16,
              background: p.bgElev,
              border: `1px solid ${p.border}`,
            }}>
              <div style={{
                display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 8,
              }}>
                <span style={{ color: p.accent, fontFamily: PELLEX_FONTS.mono, fontSize: 12, letterSpacing: 0.4 }}>{r.n}</span>
                <h3 style={{ margin: 0, color: p.text, fontSize: 17, fontWeight: 500, letterSpacing: -0.2, fontFamily: PELLEX_FONTS.body }}>
                  {r.title}
                </h3>
              </div>
              <p style={{ margin: 0, color: p.textMute, fontSize: 14, lineHeight: 1.5 }}>
                {r.body}
              </p>
            </div>
          </Reveal>
        ))}
      </div>
    </section>
  );
}

// ─────────────────────────────────────────────────────────────
// Section: 4-step playbook
// ─────────────────────────────────────────────────────────────
function Playbook({ p }) {
  const [active, setActive] = useState(0);
  const steps = [
    {
      tag: 'PICK',
      title: 'Pick a tiny painful niche.',
      body: 'Tutors stuck on weekend enquiries. Cleaners chasing late quotes. Salons forgetting follow-ups. Look for boring, repeated pain.',
      example: 'e.g. "AI follow-up for cleaning quotes"',
      time: '~20 min',
    },
    {
      tag: 'PUBLISH',
      title: 'Publish a free useful page.',
      body: 'Make one page someone can search, share, or copy from. A template, checklist, calculator, or teardown. Help first.',
      example: 'e.g. cleaning-follow-up-template.html',
      time: '~2 hrs',
    },
    {
      tag: 'SELL',
      title: 'Sell the shovel.',
      body: 'Package a prompt pack, mini guide, spreadsheet, or audit. Price small ($9-49). Help them move faster, not get rich.',
      example: 'e.g. $9 prompt pack + setup guide',
      time: '~1 day',
    },
    {
      tag: 'AUTOMATE',
      title: 'Automate fulfilment.',
      body: 'Stripe link + auto-email delivery + Notion drop. Use no-code tools you already know. Custom software is the last resort.',
      example: 'Stripe → Resend → Notion',
      time: '~3 hrs',
    },
  ];

  return (
    <section className="playbook-section desktop-section" style={{ padding: '36px 22px', background: p.bgElev, borderTop: `1px solid ${p.border}`, borderBottom: `1px solid ${p.border}` }}>
      <Reveal>
        <Chip p={p}>The playbook</Chip>
        <h2 style={{
          margin: '14px 0 6px',
          fontFamily: PELLEX_FONTS.display,
          color: p.text, fontSize: 32, lineHeight: 1.05, letterSpacing: -0.6,
          fontWeight: 600, textWrap: 'balance',
        }}>
          Four steps. <span style={{ color: p.accent, fontWeight: 700 }}>One weekend.</span>
        </h2>
        <p style={{ color: p.textMute, fontSize: 14, lineHeight: 1.5, margin: '0 0 22px' }}>
          A tiny repeatable machine: content attracts attention, a small product converts, automation fulfils, feedback tells you what to make next.
        </p>
      </Reveal>

      {/* Step tabs */}
      <div className="playbook-tabs" style={{
        display: 'flex', gap: 6, marginBottom: 14,
        background: p.bgCard, padding: 4, borderRadius: 12,
        border: `1px solid ${p.border}`,
      }}>
        {steps.map((s, i) => (
          <button key={s.tag} onClick={() => setActive(i)} style={{
            flex: 1, padding: '8px 4px', borderRadius: 8,
            background: active === i ? p.accent : 'transparent',
            color: active === i ? p.accentInk : p.textMute,
            border: 'none', cursor: 'pointer',
            fontFamily: PELLEX_FONTS.mono, fontSize: 10, fontWeight: 600, letterSpacing: 0.4,
            transition: 'all 200ms ease',
          }}>{s.tag}</button>
        ))}
      </div>

      <Reveal key={active}>
        <div style={{
          padding: 22,
          borderRadius: 16,
          background: p.bgCard,
          border: `1px solid ${p.border}`,
          minHeight: 200,
        }}>
          <div style={{
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            marginBottom: 16,
          }}>
            <div style={{
              color: p.accent, fontFamily: PELLEX_FONTS.mono, fontSize: 11, letterSpacing: 0.6,
              display: 'flex', alignItems: 'center', gap: 8,
            }}>
              <span style={{
                width: 22, height: 22, borderRadius: 6,
                background: `${p.accent}1f`, color: p.accent,
                display: 'grid', placeItems: 'center', fontSize: 10, fontWeight: 600,
              }}>0{active+1}</span>
              STEP {active+1} / 4
            </div>
            <div style={{ color: p.textDim, fontSize: 11, fontFamily: PELLEX_FONTS.mono }}>{steps[active].time}</div>
          </div>

          <h3 style={{
            margin: '0 0 10px',
            fontFamily: PELLEX_FONTS.display, fontSize: 26, lineHeight: 1.1,
            color: p.text, letterSpacing: -0.4, fontWeight: 600,
          }}>
            {steps[active].title}
          </h3>
          <p style={{ margin: '0 0 16px', color: p.textMute, fontSize: 14, lineHeight: 1.55 }}>
            {steps[active].body}
          </p>
          <div style={{
            padding: '10px 12px', borderRadius: 10,
            background: p.bgChip,
            border: `1px solid ${p.border}`,
            fontFamily: PELLEX_FONTS.mono, fontSize: 12, color: p.accent,
            display: 'inline-flex', alignItems: 'center', gap: 8,
          }}>
            <Icon.spark/>
            {steps[active].example}
          </div>
        </div>
      </Reveal>

      <div style={{ marginTop: 14, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
        <button onClick={() => setActive(Math.max(0, active - 1))} disabled={active === 0} style={{
          background: 'transparent', border: 'none', cursor: 'pointer',
          color: active === 0 ? p.textDim : p.text, fontSize: 13, fontFamily: PELLEX_FONTS.body,
          display: 'flex', alignItems: 'center', gap: 6,
        }}><span style={{ transform: 'rotate(180deg)', display: 'inline-flex' }}><Icon.arrow/></span> Back</button>
        <div style={{ display: 'flex', gap: 5 }}>
          {steps.map((_, i) => (
            <div key={i} style={{
              width: i === active ? 18 : 5, height: 5, borderRadius: 99,
              background: i === active ? p.accent : p.border,
              transition: 'all 240ms ease',
            }}/>
          ))}
        </div>
        <button onClick={() => setActive(Math.min(steps.length - 1, active + 1))} disabled={active === steps.length-1} style={{
          background: 'transparent', border: 'none', cursor: 'pointer',
          color: active === steps.length-1 ? p.textDim : p.text, fontSize: 13, fontFamily: PELLEX_FONTS.body,
          display: 'flex', alignItems: 'center', gap: 6,
        }}>Next <Icon.arrow/></button>
      </div>
    </section>
  );
}

// ─────────────────────────────────────────────────────────────
// Section: Before / After
// ─────────────────────────────────────────────────────────────
function BeforeAfter({ p }) {
  return (
    <section className="compare-section desktop-section" style={{ padding: '40px 22px' }}>
      <Reveal>
        <Chip p={p}>The shift</Chip>
        <h2 style={{
          margin: '14px 0 22px',
          fontFamily: PELLEX_FONTS.display,
          color: p.text, fontSize: 30, lineHeight: 1.05, letterSpacing: -0.5,
          fontWeight: 600, textWrap: 'balance',
        }}>
          Stop selling your hours. <span style={{ color: p.accent2, fontWeight: 700 }}>Start owning a tiny asset.</span>
        </h2>
      </Reveal>

      <div className="compare-grid" style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
        <Reveal>
          <CompareCard p={p} label="Before" tone="dim"
            title="Trading time for dollars"
            rows={[
              ['Money in', 'Only when you work'],
              ['Sleeping', 'Income stops'],
              ['Sick day', 'Lose a client'],
              ['Holiday', 'Anxiety attack'],
            ]} />
        </Reveal>
        <div className="compare-arrow" style={{ display: 'flex', justifyContent: 'center', color: p.textMute }}>
          <div style={{
            width: 32, height: 32, borderRadius: 99,
            background: p.bgElev, border: `1px solid ${p.border}`,
            display: 'grid', placeItems: 'center',
          }}>
            <Icon.arrow style={{ transform: 'rotate(90deg)' }}/>
          </div>
        </div>
        <Reveal delay={120}>
          <CompareCard p={p} label="After" tone="bright"
            title="Owning a tiny machine"
            rows={[
              ['Money in', 'While you sleep'],
              ['Sleeping', 'Stripe keeps working'],
              ['Sick day', 'Auto-delivery still ships'],
              ['Holiday', 'Email still sells the kit'],
            ]} />
        </Reveal>
      </div>
    </section>
  );
}

function CompareCard({ p, label, title, rows, tone }) {
  const bright = tone === 'bright';
  return (
    <div style={{
      padding: 20, borderRadius: 18,
      background: bright ? `linear-gradient(135deg, ${p.accent}18 0%, ${p.bgCard} 100%)` : p.bgElev,
      border: `1px solid ${bright ? p.accent + '40' : p.border}`,
      position: 'relative', overflow: 'hidden',
    }}>
      <div style={{
        position: 'absolute', top: 12, right: 14,
        color: bright ? p.accent : p.textDim,
        fontFamily: PELLEX_FONTS.mono, fontSize: 10, letterSpacing: 0.6, textTransform: 'uppercase',
      }}>{label}</div>
      <h3 style={{
        margin: '0 0 14px',
        fontFamily: PELLEX_FONTS.display, fontSize: 22, lineHeight: 1.1,
        color: p.text, letterSpacing: -0.3, fontWeight: 600,
      }}>{title}</h3>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
        {rows.map(([k, v], i) => (
          <div key={i} style={{
            display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            paddingBottom: i < rows.length - 1 ? 8 : 0,
            borderBottom: i < rows.length - 1 ? `1px dashed ${p.border}` : 'none',
          }}>
            <span style={{ color: p.textMute, fontSize: 13 }}>{k}</span>
            <span style={{
              color: bright ? p.accent : p.text,
              fontSize: 13, fontWeight: bright ? 600 : 400,
              fontFamily: bright ? PELLEX_FONTS.mono : PELLEX_FONTS.body,
              letterSpacing: bright ? 0.2 : -0.1,
            }}>{v}</span>
          </div>
        ))}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Section: Free resources
// ─────────────────────────────────────────────────────────────
function Resources({ p }) {
  const items = [
    { tag: 'TEMPLATE', title: 'Weekend tutoring reply', desc: 'Drop-in template for parents asking about lessons.', kind: 'free' },
    { tag: 'KIT', title: 'Cleaning quote follow-up', desc: 'Niche lead-magnet you can copy in an hour.', kind: 'free' },
    { tag: 'CALCULATOR', title: 'Cleaning ROI calc', desc: 'Live calculator that captures emails while running.', kind: 'free' },
  ];
  return (
    <section className="resources-section desktop-section" style={{ padding: '36px 22px' }}>
      <Reveal>
        <Chip p={p}>Resource hub</Chip>
        <h2 style={{
          margin: '14px 0 6px',
          fontFamily: PELLEX_FONTS.display,
          color: p.text, fontSize: 30, lineHeight: 1.05, letterSpacing: -0.5,
          fontWeight: 600, textWrap: 'balance',
        }}>
          Steal a working example before you build your own.
        </h2>
        <p style={{ color: p.textMute, fontSize: 14, lineHeight: 1.5, margin: '0 0 22px' }}>
          Each free page below is a real "traffic asset" — see the shape, then copy it for your niche.
        </p>
      </Reveal>

      <div className="resources-grid" style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        {items.map((it, i) => (
          <Reveal key={i} delay={i * 60}>
            <a href="#resource" onClick={(e) => { e.preventDefault(); window.pellexNav('resource'); }} style={{
              display: 'flex', alignItems: 'center', gap: 14,
              padding: 16, borderRadius: 14,
              background: p.bgElev,
              border: `1px solid ${p.border}`,
              textDecoration: 'none',
              transition: 'border-color 180ms ease, transform 180ms ease',
            }}
              onMouseEnter={e => { e.currentTarget.style.borderColor = p.accent + '55'; }}
              onMouseLeave={e => { e.currentTarget.style.borderColor = p.border; }}
            >
              <ThumbBlock p={p} kind={it.tag}/>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{
                  color: p.accent, fontFamily: PELLEX_FONTS.mono, fontSize: 10, letterSpacing: 0.6,
                  marginBottom: 4,
                }}>{it.tag} · FREE</div>
                <div style={{ color: p.text, fontSize: 15, fontWeight: 500, marginBottom: 2 }}>{it.title}</div>
                <div style={{ color: p.textMute, fontSize: 12, lineHeight: 1.4 }}>{it.desc}</div>
              </div>
              <div style={{ color: p.textMute }}><Icon.arrowUpRight/></div>
            </a>
          </Reveal>
        ))}
      </div>
    </section>
  );
}

function ThumbBlock({ p, kind }) {
  // Tiny iconographic block per resource type
  const common = {
    width: 52, height: 52, borderRadius: 12,
    background: p.bgCard, border: `1px solid ${p.border}`,
    display: 'grid', placeItems: 'center', flexShrink: 0,
  };
  if (kind === 'TEMPLATE') return (
    <div style={common}>
      <svg width="22" height="26" viewBox="0 0 22 26" fill="none">
        <rect x="2" y="2" width="18" height="22" rx="2" stroke={p.accent} strokeWidth="1.4"/>
        <path d="M6 9h10M6 13h10M6 17h6" stroke={p.accent} strokeWidth="1.4" strokeLinecap="round"/>
      </svg>
    </div>
  );
  if (kind === 'KIT') return (
    <div style={common}>
      <svg width="26" height="22" viewBox="0 0 26 22" fill="none">
        <rect x="2" y="5" width="22" height="15" rx="2" stroke={p.accent} strokeWidth="1.4"/>
        <path d="M9 5V3a2 2 0 012-2h4a2 2 0 012 2v2M13 11v4M11 13h4" stroke={p.accent} strokeWidth="1.4" strokeLinecap="round"/>
      </svg>
    </div>
  );
  return (
    <div style={common}>
      <svg width="22" height="22" viewBox="0 0 22 22" fill="none">
        <rect x="2" y="2" width="18" height="18" rx="2" stroke={p.accent} strokeWidth="1.4"/>
        <path d="M2 8h18M7 14h2M11 14h4M7 17h8" stroke={p.accent} strokeWidth="1.4" strokeLinecap="round"/>
      </svg>
    </div>
  );
}

Object.assign(window, {
  useReveal, Reveal, Icon, Chip, PrimaryCTA, GhostCTA,
  Header, Hero, AssetMachine, Rules, Playbook, BeforeAfter, Resources,
});
