// ESacademy — Variation A: Sanctuary
// Off-white cream bg, deep muted forest green, meditative, spacious.
// Sans: Heebo. Temple-like quiet.

const C = window.ESA_CONTENT;
const BpContext = window.BpContext;

const A_PALETTE = {
  bg: '#f4f1ea',
  bgAlt: '#ebe7dd',
  bgWarm: '#e6e0d2',
  ink: '#1f2a24',
  inkSoft: '#3c4a42',
  muted: '#6b7870',
  rule: '#c8c2b4',
  accent: '#2d4a3e',
  accentSoft: '#5b7a6a',
  paper: '#faf8f3',
  gold: '#7a6a3a'
};

function ANumeral({ children, muted }) {
  return (
    <span style={{
      fontFamily: '"Cormorant Garamond", Georgia, serif',
      fontStyle: 'italic',
      fontWeight: 400,
      color: muted ? A_PALETTE.muted : A_PALETTE.gold,
      letterSpacing: '0.02em'
    }}>{children}</span>
  );
}

function AKicker({ children }) {
  return (
    <div style={{
      fontSize: 13,
      letterSpacing: '0.22em',
      color: A_PALETTE.muted,
      textTransform: 'uppercase',
      fontWeight: 500,
      marginBottom: 18
    }}>{children}</div>
  );
}

function AButton({ children, primary, onClick }) {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const [hover, setHover] = React.useState(false);
  const [magRef, magDelta, magH] = useMagnetic(isMobile ? 0 : 0.28);

  const base = {
    display: 'inline-flex',
    alignItems: 'center',
    justifyContent: 'center',
    gap: 14,
    height: 56,
    padding: '0 30px',
    borderRadius: 2,
    fontSize: 15.5,
    fontWeight: 500,
    letterSpacing: '0.02em',
    cursor: 'pointer',
    transition: 'background 220ms ease, color 220ms ease, border-color 220ms ease, transform 0.5s cubic-bezier(0.22,0.61,0.36,1)',
    fontFamily: 'inherit',
    border: `1px solid ${A_PALETTE.ink}`,
    boxSizing: 'border-box',
    transform: isMobile ? 'none' : `translate(${magDelta.x}px, ${magDelta.y}px)`,
    width: isMobile ? '100%' : 'auto',
  };
  const primaryStyle = primary ? {
    background: hover ? A_PALETTE.accent : `linear-gradient(135deg, ${A_PALETTE.ink} 0%, #253d30 100%)`,
    color: A_PALETTE.bg,
    borderColor: hover ? A_PALETTE.accent : A_PALETTE.ink
  } : {
    background: hover ? A_PALETTE.ink : 'transparent',
    color: hover ? A_PALETTE.bg : A_PALETTE.ink
  };
  return (
    <button
      ref={magRef}
      onClick={onClick}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => { setHover(false); if (!isMobile) magH.onMouseLeave(); }}
      onMouseMove={isMobile ? undefined : magH.onMouseMove}
      style={{ ...base, ...primaryStyle }}>
      <span>{children}</span>
      <span style={{ fontSize: 18, transform: hover ? 'translateX(-4px)' : 'translateX(0)', transition: 'transform 200ms ease' }}>←</span>
    </button>
  );
}

function ASection({ children, bg, id, narrow, tall }) {
  const bp = React.useContext(BpContext);
  const hPad = bp === 'mobile' ? 20 : bp === 'tablet' ? 32 : 64;
  const vPad = bp === 'mobile' ? 56 : bp === 'tablet' ? 80 : (tall ? 160 : 120);
  return (
    <section id={id} style={{
      background: bg || 'transparent',
      padding: `${vPad}px ${hPad}px`,
      position: 'relative', color: "rgb(55, 123, 86)"
    }}>
      <div style={{ maxWidth: narrow ? 860 : 1240, margin: '0 auto' }}>
        {children}
      </div>
    </section>
  );
}

// ──────────────────── NAV ────────────────────
const NAV_SECTION_MAP = {
  'האדם גרסה 2.0': 'adam',
  'תוכנית 369': '369',
  'המצפן': 'compass',
  'האדם גרסה 3.0': 'roots',
};

function ANav() {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const isCompact = isMobile || bp === 'tablet';
  const [scrolled, setScrolled] = React.useState(false);
  const [activeId, setActiveId] = React.useState('');
  const [menuOpen, setMenuOpen] = React.useState(false);

  React.useEffect(() => {
    if (!isCompact && menuOpen) setMenuOpen(false);
  }, [isCompact]);

  React.useEffect(() => {
    const root = document.getElementById('esa-a-root');
    if (!root) return;
    if (menuOpen) {
      root.style.overflow = 'hidden';
    } else {
      root.style.overflow = 'auto';
    }
    return () => { root.style.overflow = 'auto'; };
  }, [menuOpen]);

  React.useEffect(() => {
    const el = document.getElementById('esa-a-root');
    if (!el) return;
    const onScroll = () => setScrolled(el.scrollTop > 40);
    el.addEventListener('scroll', onScroll);
    return () => el.removeEventListener('scroll', onScroll);
  }, []);

  React.useEffect(() => {
    const ids = Object.values(NAV_SECTION_MAP);
    const io = new IntersectionObserver(
      (entries) => {
        entries.forEach(e => { if (e.isIntersecting) setActiveId(e.target.id); });
      },
      { threshold: 0.35 }
    );
    ids.forEach(id => { const el = document.getElementById(id); if (el) io.observe(el); });
    return () => io.disconnect();
  }, []);

  const navItems = ['האדם גרסה 2.0', 'תוכנית 369', 'המצפן', 'האדם גרסה 3.0', 'אודות'];

  const navBase = {
    position: 'sticky',
    top: 0,
    zIndex: 50,
    background: scrolled || menuOpen ? 'rgba(244,241,234,0.96)' : 'transparent',
    backdropFilter: scrolled || menuOpen ? 'blur(12px)' : 'none',
    borderBottom: scrolled && !menuOpen ? `1px solid ${A_PALETTE.rule}` : '1px solid transparent',
    transition: 'background 300ms ease, backdrop-filter 300ms ease, border-color 300ms ease',
  };

  if (isCompact) {
    const hPad = isMobile ? 20 : 32;
    return (
      <nav style={navBase}>
        <div style={{
          padding: `16px ${hPad}px`,
          display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
            <div style={{
              width: 24, height: 24, borderRadius: '50%',
              border: `1.5px solid ${A_PALETTE.ink}`, position: 'relative', flexShrink: 0,
            }}>
              <div style={{ position: 'absolute', inset: 5, borderRadius: '50%', background: A_PALETTE.accent }} />
            </div>
            <div style={{ fontSize: isMobile ? 14 : 16, fontWeight: 600, letterSpacing: '0.04em', color: A_PALETTE.ink }}>
              הארכיטקטורה של התודעה
            </div>
          </div>

          <button
            onClick={() => setMenuOpen(o => !o)}
            style={{
              background: 'none', border: 'none', cursor: 'pointer',
              padding: '6px 8px', color: A_PALETTE.ink,
              display: 'flex', flexDirection: 'column', gap: 5, alignItems: 'flex-end',
            }}
            aria-label="תפריט"
          >
            <span style={{
              display: 'block', height: 1.5, background: A_PALETTE.ink,
              width: 24,
              transform: menuOpen ? 'rotate(45deg) translate(4px, 6px)' : 'none',
              transition: 'all 250ms ease',
              transformOrigin: 'center',
            }} />
            <span style={{
              display: 'block', height: 1.5, background: A_PALETTE.ink,
              width: 20,
              opacity: menuOpen ? 0 : 1,
              transition: 'opacity 250ms ease',
            }} />
            <span style={{
              display: 'block', height: 1.5, background: A_PALETTE.ink,
              width: menuOpen ? 24 : 16,
              transform: menuOpen ? 'rotate(-45deg) translate(4px, -6px)' : 'none',
              transition: 'all 250ms ease',
              transformOrigin: 'center',
            }} />
          </button>
        </div>

        {menuOpen && (
          <div style={{
            background: 'rgba(244,241,234,0.98)',
            padding: `0 ${hPad}px 24px`,
            borderTop: `1px solid ${A_PALETTE.rule}`,
          }}>
            {navItems.map((x) => {
              const sectionId = NAV_SECTION_MAP[x];
              const isActive = sectionId && activeId === sectionId;
              return (
                <a
                  key={x}
                  href={x === 'האדם גרסה 2.0' ? '/adam-2.0.html' : x === 'תוכנית 369' ? '/369' : undefined}
                  onClick={() => setMenuOpen(false)}
                  style={{
                    color: isActive ? A_PALETTE.accent : A_PALETTE.inkSoft,
                    textDecoration: 'none', cursor: 'pointer',
                    padding: '14px 0',
                    fontSize: 18, fontWeight: isActive ? 500 : 400,
                    borderBottom: `1px solid ${A_PALETTE.rule}`,
                    display: 'block',
                    transition: 'color 250ms ease',
                  }}
                >{x}</a>
              );
            })}
            <div style={{
              marginTop: 20,
              padding: '14px 20px',
              background: A_PALETTE.ink,
              color: A_PALETTE.bg,
              textAlign: 'center',
              fontSize: 15, fontWeight: 500, cursor: 'pointer',
              borderRadius: 2,
            }}>התחל כאן ←</div>
          </div>
        )}
      </nav>
    );
  }

  return (
    <nav style={navBase}>
      <div style={{
        maxWidth: 1240,
        margin: '0 auto',
        padding: '22px 64px',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'space-between',
        gap: 40
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
          <div style={{
            width: 28, height: 28, borderRadius: '50%',
            border: `1.5px solid ${A_PALETTE.ink}`, position: 'relative'
          }}>
            <div style={{ position: 'absolute', inset: 6, borderRadius: '50%', background: A_PALETTE.accent }} />
          </div>
          <div style={{ fontSize: 18, fontWeight: 600, letterSpacing: '0.04em', color: A_PALETTE.ink }}>
            הארכיטקטורה של התודעה
          </div>
        </div>

        <div style={{ display: 'flex', gap: 34, fontSize: 14, color: A_PALETTE.inkSoft }}>
          {navItems.map((x) => {
            const sectionId = NAV_SECTION_MAP[x];
            const isActive = sectionId && activeId === sectionId;
            return (
              <a
                key={x}
                href={x === 'האדם גרסה 2.0' ? '/adam-2.0.html' : x === 'תוכנית 369' ? '/369' : undefined}
                style={{
                  color: isActive ? A_PALETTE.accent : 'inherit',
                  textDecoration: 'none',
                  cursor: 'pointer',
                  position: 'relative',
                  paddingBottom: 3,
                  fontWeight: isActive ? 500 : 400,
                  transition: 'color 250ms ease',
                }}>
                {x}
                <span style={{
                  position: 'absolute', bottom: 0, right: 0, left: 0,
                  height: 1,
                  background: A_PALETTE.accent,
                  transform: `scaleX(${isActive ? 1 : 0})`,
                  transition: 'transform 300ms cubic-bezier(0.22,0.61,0.36,1)',
                  transformOrigin: 'right',
                }} />
              </a>
            );
          })}
        </div>

        <div style={{
          padding: '10px 20px',
          border: `1px solid ${A_PALETTE.ink}`,
          borderRadius: 2,
          fontSize: 14,
          fontWeight: 500,
          cursor: 'pointer',
          color: A_PALETTE.ink
        }}>התחל כאן ←</div>
      </div>
    </nav>
  );
}

// ──────────────────── HERO ────────────────────
function AHero() {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const isTablet = bp === 'tablet';
  const isDesktop = bp === 'desktop';
  const h = C.hero;
  const parallax = useParallax(12);
  const hPad = isMobile ? 20 : isTablet ? 32 : 64;

  if (isDesktop) {
    return (
      <section style={{
        minHeight: 'calc(100vh - 72px)',
        display: 'grid',
        gridTemplateColumns: '1fr 1fr',
        position: 'relative',
        overflow: 'hidden',
      }}>
        {/* עמודת טקסט — ימין ב-RTL */}
        <div style={{
          padding: '80px 64px 120px',
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'center',
          position: 'relative',
        }}>
          <div className="esa-page-enter">
            <AKicker>{h.kicker}</AKicker>
          </div>
          <h1 className="esa-page-enter-delay" style={{
            fontSize: 'clamp(52px, 6vw, 96px)',
            lineHeight: 1.05, fontWeight: 500,
            letterSpacing: '-0.025em',
            color: A_PALETTE.ink,
            margin: '0 0 48px',
          }}>
            {h.title[0]}<br />
            {h.title[1]}<br />
            <span style={{
              fontFamily: '"Cormorant Garamond", Georgia, serif',
              fontStyle: 'italic', fontWeight: 500,
              fontSize: '0.85em', color: A_PALETTE.accent,
              letterSpacing: '-0.01em',
            }}>{h.title[2]}</span>
          </h1>
          <div className="esa-page-enter-delay" style={{ animationDelay: '0.32s' }}>
            <p style={{
              fontSize: 21, lineHeight: 1.6,
              color: A_PALETTE.inkSoft,
              maxWidth: 560, margin: '0 0 52px',
              fontWeight: 400, whiteSpace: 'pre-line',
            }}>{h.sub}</p>
            <div style={{ display: 'flex', gap: 12, flexWrap: 'wrap' }}>
              <a href="#start" style={{textDecoration:'none'}}><AButton primary>{h.primary}</AButton></a>
              <a href="#start" style={{textDecoration:'none'}}><AButton>{h.secondary}</AButton></a>
            </div>
          </div>
          <div style={{
            position: 'absolute', bottom: 32, right: 64, left: 64,
            display: 'flex', justifyContent: 'space-between',
            fontSize: 11, letterSpacing: '0.24em',
            color: A_PALETTE.inkSoft, opacity: 0.6,
            textTransform: 'uppercase', fontWeight: 500,
          }}>
            <div>מערכת הפעלה פנימית &nbsp;·&nbsp; מסלול 369 &nbsp;·&nbsp; המצפן</div>
            <div>גלול ↓</div>
          </div>
        </div>

        {/* עמודת תמונה — שמאל ב-RTL */}
        <div style={{
          position: 'relative',
          overflow: 'hidden',
          transform: `translateY(${parallax.y * 0.35}px)`,
          transition: 'transform 0.1s linear',
        }}>
          <div style={{
            position: 'absolute', inset: 0,
            animation: 'esa-float 7s ease-in-out infinite',
          }}>
            <img
              src="hero-visual.png?v=4"
              alt="" aria-hidden="true"
              style={{
                position: 'absolute', inset: 0,
                width: '100%', height: '100%',
                objectFit: 'cover', objectPosition: 'center 28%',
                mixBlendMode: 'multiply', opacity: 0.94,
                filter: 'contrast(1.06) saturate(1.05)',
                userSelect: 'none', pointerEvents: 'none',
              }}
            />
          </div>
          {/* gradient: נמוג לכיוון הטקסט (קצה ימני של עמודת התמונה) */}
          <div aria-hidden="true" style={{
            position: 'absolute', inset: 0,
            background: `linear-gradient(to right, transparent 52%, ${A_PALETTE.bg} 90%)`,
            pointerEvents: 'none',
          }} />
          {/* gradient: נמוג בתחתית */}
          <div aria-hidden="true" style={{
            position: 'absolute', inset: 0,
            background: `linear-gradient(to top, ${A_PALETTE.bg} 0%, transparent 22%)`,
            pointerEvents: 'none',
          }} />
        </div>
      </section>
    );
  }

  // מובייל / טאבלט — layout מקורי
  return (
    <section style={{
      minHeight: isMobile ? '100svh' : 'calc(100vh - 72px)',
      padding: `48px ${hPad}px ${isMobile ? 80 : 120}px`,
      display: 'flex',
      alignItems: 'center',
      position: 'relative'
    }}>
      {!isMobile && (
        <div style={{
          position: 'absolute', inset: 0,
          overflow: 'hidden', pointerEvents: 'none',
        }}>
          <div style={{
            position: 'absolute', top: '46%', left: '6%',
            transform: `translateY(-50%) translate(${parallax.x}px, ${parallax.y}px)`,
            width: 'min(400px, 42vw)', aspectRatio: '1 / 1',
            WebkitMaskImage: 'radial-gradient(ellipse 60% 58% at 50% 44%, #000 44%, rgba(0,0,0,0.8) 64%, rgba(0,0,0,0.32) 82%, transparent 100%)',
            maskImage: 'radial-gradient(ellipse 60% 58% at 50% 44%, #000 44%, rgba(0,0,0,0.8) 64%, rgba(0,0,0,0.32) 82%, transparent 100%)',
            filter: 'contrast(1.08) saturate(1.04)',
            transition: 'transform 0.1s linear',
          }}>
            <div style={{ animation: 'esa-float 7s ease-in-out infinite', width: '100%', height: '100%', position: 'relative' }}>
              <img
                src="hero-visual.png?v=4" alt="" aria-hidden="true"
                style={{
                  width: '100%', height: '100%',
                  objectFit: 'cover', objectPosition: 'center 36%',
                  mixBlendMode: 'multiply', opacity: 0.88,
                  userSelect: 'none', pointerEvents: 'none',
                }}
              />
              <div aria-hidden="true" style={{
                position: 'absolute', left: 0, right: 0, bottom: 0, height: '68%',
                background: `linear-gradient(to top, ${A_PALETTE.bg} 0%, ${A_PALETTE.bg}e6 18%, ${A_PALETTE.bg}99 40%, ${A_PALETTE.bg}44 65%, transparent 100%)`,
                mixBlendMode: 'normal', opacity: 0.62, pointerEvents: 'none',
              }} />
            </div>
          </div>
        </div>
      )}
      <div style={{ maxWidth: 1240, width: '100%', margin: '0 auto', position: 'relative' }}>
        <div style={{ maxWidth: isMobile ? '100%' : 820 }}>
          <div className="esa-page-enter">
            <AKicker>{h.kicker}</AKicker>
          </div>
          <h1 className="esa-page-enter-delay" style={{
            fontSize: isMobile ? 'clamp(38px, 10vw, 52px)' : 'clamp(56px, 7vw, 104px)',
            lineHeight: 1.05, fontWeight: 500,
            letterSpacing: '-0.025em', color: A_PALETTE.ink,
            margin: `0 0 ${isMobile ? 28 : 48}px`,
          }}>
            {h.title[0]}<br />
            {h.title[1]}<br />
            <span style={{
              fontFamily: '"Cormorant Garamond", Georgia, serif',
              fontStyle: 'italic', fontWeight: 500,
              fontSize: '0.85em', color: A_PALETTE.accent,
              letterSpacing: '-0.01em',
            }}>{h.title[2]}</span>
          </h1>
          <div className="esa-page-enter-delay" style={{ animationDelay: '0.32s' }}>
            <p style={{
              fontSize: isMobile ? 17 : 22, lineHeight: 1.55,
              color: A_PALETTE.inkSoft, maxWidth: 680,
              margin: `0 0 ${isMobile ? 36 : 56}px`,
              fontWeight: 400, whiteSpace: 'pre-line',
            }}>{h.sub}</p>
            <div style={{
              display: 'flex', gap: 12, flexWrap: 'wrap',
              flexDirection: isMobile ? 'column' : 'row',
            }}>
              <a href="#start" style={{textDecoration:'none'}}><AButton primary>{h.primary}</AButton></a>
              <a href="#start" style={{textDecoration:'none'}}><AButton>{h.secondary}</AButton></a>
            </div>
          </div>
        </div>
        {!isMobile && (
          <div style={{
            position: 'absolute', bottom: -80, right: 0, left: 0,
            display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end',
            fontSize: 11, letterSpacing: '0.24em',
            color: A_PALETTE.inkSoft, opacity: 0.78,
            textTransform: 'uppercase', fontWeight: 500,
          }}>
            <div>מערכת הפעלה פנימית &nbsp;·&nbsp; מסלול 369 &nbsp;·&nbsp; המצפן &nbsp;·&nbsp; חיים על קו ההתפתחות</div>
            <div>גלול ↓</div>
          </div>
        )}
      </div>
    </section>
  );
}

// ──────────────────── RESONANCE ────────────────────
function AResonance() {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const r = C.resonance;
  const gridCols = isMobile ? 'repeat(1, 1fr)' : bp === 'tablet' ? 'repeat(2, 1fr)' : 'repeat(4, 1fr)';

  return (
    <ASection bg={A_PALETTE.bgAlt}>
      <Reveal>
        <AKicker>{r.kicker}</AKicker>
        <h2 style={{
          fontSize: isMobile ? 36 : 56, fontWeight: 500, color: A_PALETTE.ink,
          margin: '0 0 32px', letterSpacing: '-0.025em',
          lineHeight: 1.05, maxWidth: 900
        }}>{r.title}</h2>
      </Reveal>

      {r.sub && (
        <Reveal delay={0.1}>
          <p style={{
            fontSize: isMobile ? 17 : 20, lineHeight: 1.65, color: A_PALETTE.inkSoft,
            maxWidth: 780, margin: '0 0 48px', fontWeight: 400, whiteSpace: 'pre-line'
          }}>{r.sub}</p>
        </Reveal>
      )}

      <div style={{
        display: 'grid', gridTemplateColumns: gridCols,
        gap: 1, background: A_PALETTE.rule, border: `1px solid ${A_PALETTE.rule}`
      }}>
        {r.items.map((item, i) => (
          <Reveal key={i} delay={i * 0.08}>
            <div style={{
              padding: isMobile ? '28px 20px' : '40px 32px', background: A_PALETTE.bgAlt,
              display: 'flex', flexDirection: 'column', gap: 20, height: '100%'
            }}>
              <ANumeral>0{i + 1}</ANumeral>
              <div style={{ fontSize: 18, lineHeight: 1.4, color: A_PALETTE.ink, fontWeight: 400 }}>{item}</div>
            </div>
          </Reveal>
        ))}
      </div>
    </ASection>
  );
}

// ──────────────────── FIT ────────────────────
function AFitCard({ c }) {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const [cardRef, tilt, tiltH] = useCardTilt(4);
  return (
    <div
      ref={cardRef}
      {...(isMobile ? {} : tiltH)}
      style={{
        background: A_PALETTE.paper,
        padding: isMobile ? '28px 20px' : '44px 32px',
        display: 'flex', flexDirection: 'column', gap: 18, minHeight: isMobile ? 'auto' : 280,
        transform: isMobile ? 'none' : `perspective(800px) rotateX(${tilt.rx}deg) rotateY(${tilt.ry}deg)`,
        transition: 'transform 0.4s cubic-bezier(0.22,0.61,0.36,1)',
        willChange: 'transform',
        borderTop: `3px solid ${A_PALETTE.gold}`,
      }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <ANumeral>{c.n}</ANumeral>
        <div style={{ width: 24, height: 1, background: A_PALETTE.muted }} />
      </div>
      <div style={{ fontSize: 18, fontWeight: 500, color: A_PALETTE.ink, lineHeight: 1.3 }}>{c.title}</div>
      <div style={{ fontSize: 15, color: A_PALETTE.inkSoft, lineHeight: 1.6 }}>{c.body}</div>
    </div>
  );
}

function AFit() {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const f = C.fit;
  const gridCols = isMobile ? 'repeat(1, 1fr)' : bp === 'tablet' ? 'repeat(2, 1fr)' : 'repeat(4, 1fr)';

  return (
    <ASection>
      <Reveal style={{ marginBottom: 32, maxWidth: 900 }}>
        <AKicker>{f.kicker}</AKicker>
        <h2 style={{
          fontSize: isMobile ? 36 : 56, fontWeight: 500, color: A_PALETTE.ink,
          margin: '0 0 32px', letterSpacing: '-0.025em', lineHeight: 1.05
        }}>{f.title}</h2>
        <p style={{
          fontSize: isMobile ? 17 : 20, lineHeight: 1.65, color: A_PALETTE.inkSoft,
          margin: 0, maxWidth: 780, whiteSpace: 'pre-line', fontWeight: 400
        }}>{f.sub}</p>
      </Reveal>

      <div style={{
        display: 'grid', gridTemplateColumns: gridCols,
        gap: 1, background: A_PALETTE.rule, border: `1px solid ${A_PALETTE.rule}`
      }}>
        {f.cards.map((c, i) => (
          <Reveal key={c.n} delay={i * 0.07}>
            <AFitCard c={c} />
          </Reveal>
        ))}
      </div>
    </ASection>
  );
}

// ──────────────────── JOURNEY ────────────────────
function AJourney() {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const isCompact = isMobile || bp === 'tablet';
  const j = C.journey;
  const sectionRef = React.useRef(null);
  const [activeStep, setActiveStep] = React.useState(0);

  React.useEffect(() => {
    const root = document.getElementById('esa-a-root');
    const target = root || window;

    const update = () => {
      const section = sectionRef.current;
      if (!section) return;
      const rect = section.getBoundingClientRect();
      const totalScroll = section.offsetHeight - window.innerHeight;
      const scrolled = -rect.top;
      const progress = Math.min(1, Math.max(0, scrolled / totalScroll));
      setActiveStep(Math.min(3, Math.floor(progress * 4)));
    };

    target.addEventListener('scroll', update, { passive: true });
    update();
    return () => target.removeEventListener('scroll', update);
  }, []);

  const hPad = isMobile ? 20 : 32;

  return (
    <section ref={sectionRef} id="esa-journey" style={{ background: A_PALETTE.ink, height: isCompact ? '350vh' : '500vh', position: 'relative' }}>
      <div style={{
        position: 'sticky', top: 0, height: '100vh',
        display: 'flex', flexDirection: 'column', justifyContent: 'center',
        padding: `48px ${isCompact ? hPad : 64}px`, overflow: 'hidden',
      }}>
        <div style={{ textAlign: 'center', marginBottom: isCompact ? 40 : 72 }}>
          <div style={{
            fontSize: 13, letterSpacing: '0.22em', color: 'rgba(244,241,234,0.5)',
            textTransform: 'uppercase', marginBottom: 18
          }}>{j.kicker}</div>
          <h2 style={{
            fontSize: isCompact ? 34 : 56, fontWeight: 500, color: A_PALETTE.bg,
            margin: `0 0 ${isMobile ? 12 : 24}px`, letterSpacing: '-0.025em'
          }}>{j.title}</h2>
          {!isMobile && (
            <div style={{
              fontSize: 18, color: 'rgba(244,241,234,0.65)',
              maxWidth: 620, margin: '0 auto', lineHeight: 1.6
            }}>{j.sub}</div>
          )}
        </div>

        {isCompact ? (
          <div style={{ position: 'relative', width: '100%' }}>
            {/* Progress dots */}
            <div style={{ display: 'flex', gap: 8, justifyContent: 'center', marginBottom: 36 }}>
              {j.steps.map((_, i) => (
                <div key={i} style={{
                  height: 6, borderRadius: 3,
                  width: i === activeStep ? 28 : 8,
                  background: i === activeStep ? A_PALETTE.accentSoft : 'rgba(244,241,234,0.25)',
                  transition: 'all 0.4s ease',
                }} />
              ))}
            </div>

            {j.steps.map((s, i) => (
              <div key={s.n} style={{
                textAlign: 'center',
                padding: `0 ${hPad}px`,
                opacity: i === activeStep ? 1 : 0,
                transform: `translateY(${i === activeStep ? 0 : 28}px)`,
                transition: 'opacity 0.5s ease, transform 0.5s ease',
                position: i === activeStep ? 'relative' : 'absolute',
                top: i === activeStep ? 'auto' : 0,
                left: i === activeStep ? 'auto' : 0,
                right: i === activeStep ? 'auto' : 0,
                pointerEvents: i === activeStep ? 'auto' : 'none',
              }}>
                <div style={{
                  width: 72, height: 72, borderRadius: '50%',
                  background: A_PALETTE.accent,
                  border: `1px solid ${A_PALETTE.accent}`,
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                  margin: '0 auto 24px',
                  boxShadow: `0 0 0 6px rgba(90,107,79,0.18), 0 0 32px rgba(90,107,79,0.35)`,
                }}>
                  <div style={{ width: 12, height: 12, borderRadius: '50%', background: A_PALETTE.bg }} />
                </div>
                <div style={{
                  fontFamily: '"Cormorant Garamond", Georgia, serif', fontStyle: 'italic',
                  fontSize: 16, color: A_PALETTE.accentSoft, marginBottom: 10,
                }}>{s.n}</div>
                <div style={{
                  fontSize: 28, fontWeight: 600, color: A_PALETTE.bg,
                  marginBottom: 12, letterSpacing: '-0.01em',
                }}>{s.name}</div>
                <div style={{
                  fontSize: 12, letterSpacing: '0.15em', textTransform: 'uppercase',
                  color: A_PALETTE.bg, marginBottom: 16, fontWeight: 600,
                }}>{s.tag}</div>
                <div style={{
                  fontSize: 15, color: 'rgba(244,241,234,0.85)', lineHeight: 1.65,
                  maxWidth: 320, margin: '0 auto',
                }}>{s.body}</div>
              </div>
            ))}
          </div>
        ) : (
          <div style={{ position: 'relative', maxWidth: 1240, margin: '0 auto', width: '100%', padding: '40px 0' }}>
            <div style={{
              position: 'absolute', top: 'calc(40px + 40px)', right: '12.5%', left: '12.5%',
              height: 1,
              background: 'linear-gradient(90deg, transparent 0%, rgba(244,241,234,0.2) 15%, rgba(244,241,234,0.2) 85%, transparent 100%)'
            }} />
            <div style={{
              position: 'absolute', top: 'calc(40px + 40px)', right: '12.5%',
              width: `${activeStep * 25}%`,
              height: 1,
              background: A_PALETTE.accent,
              opacity: 0.9,
              transition: 'width 0.7s cubic-bezier(0.22,0.61,0.36,1)',
            }} />

            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 20 }}>
              {j.steps.map((s, i) => {
                const isFeatured = activeStep === i;
                const isPast = i < activeStep;
                return (
                  <div key={s.n} style={{
                    display: 'flex', flexDirection: 'column',
                    alignItems: 'center', textAlign: 'center', position: 'relative',
                    opacity: i > activeStep ? 0.38 : 1,
                    transition: 'opacity 0.5s ease',
                  }}>
                    <div style={{ position: 'relative', marginBottom: 24, zIndex: 2 }}>
                      {isFeatured && (
                        <div style={{
                          position: 'absolute', inset: -8, borderRadius: '50%',
                          border: `1px solid ${A_PALETTE.accent}`,
                          animation: 'esa-pulse-ring 2.4s cubic-bezier(0.4,0,0.6,1) infinite',
                        }} />
                      )}
                      <div style={{
                        width: 80, height: 80, borderRadius: '50%',
                        background: isFeatured ? A_PALETTE.accent : isPast ? A_PALETTE.accentSoft : A_PALETTE.ink,
                        border: isFeatured ? `1px solid ${A_PALETTE.accent}` : `1px solid rgba(244,241,234,0.3)`,
                        display: 'flex', alignItems: 'center', justifyContent: 'center',
                        boxShadow: isFeatured ? `0 0 0 6px rgba(90,107,79,0.18), 0 0 32px rgba(90,107,79,0.35)` : 'none',
                        transition: 'all 0.6s cubic-bezier(0.22,0.61,0.36,1)',
                      }}>
                        <div style={{ width: 12, height: 12, borderRadius: '50%', background: A_PALETTE.bg }} />
                      </div>
                    </div>

                    <div style={{
                      fontFamily: '"Cormorant Garamond", Georgia, serif', fontStyle: 'italic',
                      fontSize: 16, color: isFeatured ? A_PALETTE.accentSoft : 'rgba(244,241,234,0.5)',
                      marginBottom: 10, transition: 'color 0.4s ease',
                    }}>{s.n}</div>
                    <div style={{
                      fontSize: isFeatured ? 24 : 22, fontWeight: isFeatured ? 600 : 500,
                      color: A_PALETTE.bg, marginBottom: 10, letterSpacing: '-0.01em',
                      minHeight: '2.4em', display: 'flex', alignItems: 'flex-end', justifyContent: 'center',
                      transition: 'font-size 0.4s ease',
                    }}>{s.name}</div>
                    <div style={{
                      fontSize: 12, letterSpacing: '0.15em', textTransform: 'uppercase',
                      color: isFeatured ? A_PALETTE.bg : A_PALETTE.accentSoft,
                      opacity: isFeatured ? 1 : 0.85, marginBottom: 16,
                      fontWeight: isFeatured ? 600 : 400, transition: 'all 0.4s ease',
                    }}>{s.tag}</div>
                    <div style={{
                      fontSize: 14,
                      color: isFeatured ? 'rgba(244,241,234,0.85)' : 'rgba(244,241,234,0.65)',
                      lineHeight: 1.6, maxWidth: 220, transition: 'color 0.4s ease',
                    }}>{s.body}</div>
                  </div>
                );
              })}
            </div>
          </div>
        )}
      </div>
    </section>
  );
}

// ──────────────────── START PATHS ────────────────────
function AStart() {
  const bp = React.useContext(BpContext);
  const isMobile = bp === 'mobile';
  const s = C.start;
  const [active, setActive] = React.useState(0);
  const gridCols = isMobile ? '1fr' : bp === 'tablet' ? 'repeat(2, 1fr)' : 'repeat(3, 1fr)';

  return (
    <ASection id="start">
      <Reveal style={{ maxWidth: 820, marginBottom: 48 }}>
        <AKicker>{s.kicker}</AKicker>
        <h2 style={{
          fontSize: isMobile ? 36 : 56, fontWeight: 500, color: A_PALETTE.ink,
          margin: 0, letterSpacing: '-0.025em', lineHeight: 1.05
        }}>{s.title}</h2>
      </Reveal>

      <div style={{ display: 'grid', gridTemplateColumns: gridCols, gap: isMobile ? 16 : 24 }}>
        {s.paths.map((p, i) => {
          const isActive = isMobile ? true : (active === i);
          return (
            <Reveal key={p.letter} delay={i * 0.09}>
              <div
                onMouseEnter={() => setActive(i)}
                onMouseLeave={() => setActive(0)}
                onClick={() => { if (i === 0) window.location.href = '/adam-2.0.html'; else if (i === 2) window.location.href = '/369'; }}
                style={{
                  padding: isMobile ? '32px 24px' : '48px 36px',
                  background: isActive ? `linear-gradient(135deg, ${A_PALETTE.ink} 0%, #253d30 100%)` : A_PALETTE.paper,
                  color: isActive ? A_PALETTE.bg : A_PALETTE.ink,
                  border: `1px solid ${isActive ? A_PALETTE.ink : A_PALETTE.rule}`,
                  boxShadow: isActive ? '0 12px 40px rgba(31,42,36,0.22)' : '0 2px 16px rgba(31,42,36,0.07)',
                  transform: isActive ? 'translateY(-3px)' : 'none',
                  transition: 'all 300ms ease',
                  cursor: 'pointer', minHeight: isMobile ? 'auto' : 360,
                  display: 'flex', flexDirection: 'column', gap: 20
                }}>
                <div style={{
                  width: 52, height: 52, borderRadius: '50%',
                  border: `1px solid ${isActive ? 'rgba(244,241,234,0.4)' : A_PALETTE.rule}`,
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                  fontSize: 20, fontWeight: 500,
                  fontFamily: '"Cormorant Garamond", Georgia, serif',
                  fontStyle: 'italic',
                  color: isActive ? A_PALETTE.bg : A_PALETTE.gold
                }}>{p.letter}</div>
                <div style={{ fontSize: 22, fontWeight: 500, lineHeight: 1.3, letterSpacing: '-0.01em' }}>{p.who}</div>
                <div style={{ fontSize: 15, lineHeight: 1.65, opacity: 0.8, flex: 1 }}>{p.body}</div>
                <div style={{
                  display: 'flex', alignItems: 'center', gap: 12,
                  paddingTop: 20,
                  borderTop: `1px solid ${isActive ? 'rgba(244,241,234,0.2)' : A_PALETTE.rule}`,
                  fontSize: 14, fontWeight: 500
                }}>
                  <span>{p.cta}</span>
                  <span style={{ marginRight: 'auto' }}>←</span>
                </div>
              </div>
            </Reveal>
          );
        })}
      </div>

      <Reveal delay={0.2}>
        <div style={{
          marginTop: 32, padding: '18px 24px',
          background: A_PALETTE.bgAlt,
          fontSize: 14, color: A_PALETTE.inkSoft,
          borderRight: `2px solid ${A_PALETTE.accent}`
        }}>{s.note}</div>
      </Reveal>
    </ASection>
  );
}

// Export
window.EsaVariationA = { ANav, AHero, AResonance, AFit, AJourney, AStart, A_PALETTE, AKicker, AButton, ASection, ANumeral };
