// Landing — bold typographic
const Landing = ({ go }) => {
  const [hovered, setHovered] = React.useState(null);
  const [about] = useAboutContent();
  const events = useActiveEvents();
  const [hero] = useHeroVideo();
  const eventsRef = React.useRef(null);
  const nextEvent = events[0];
  const scrollToEvents = () => {
    const target = eventsRef.current;
    if (!target) return;
    // Walk up to find the actual scroll container — body has overflow:hidden,
    // so the Landing div (overflow:auto) is the scroller. The container has
    // CSS `scroll-behavior: smooth`, so the scrollTop assignment animates.
    let parent = target.parentElement;
    while (parent && parent !== document.body) {
      const oy = getComputedStyle(parent).overflowY;
      if ((oy === 'auto' || oy === 'scroll') && parent.scrollHeight > parent.clientHeight) break;
      parent = parent.parentElement;
    }
    if (!parent) { target.scrollIntoView({ block: 'start' }); return; }
    parent.scrollTop = target.getBoundingClientRect().top - parent.getBoundingClientRect().top + parent.scrollTop - 4;
  };
  return (
    <div style={{ height: '100%', overflowY: 'auto', background: T.bg, color: T.bone, scrollBehavior: 'smooth', position: 'relative' }}>
      {/* Fixed-viewport patch — follows the user as they scroll, biased
          downward (62% from top) so the hero video at the top of the
          viewport doesn't crop it on first paint. Bigger size fills the
          horizontal space the user marked off. */}
      <Watermark
        size={1280}
        opacity={0.12}
        style={{ backgroundPosition: 'center 78%' }}
      />
      {/* Top nav */}
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '20px 48px', borderBottom: `1px solid ${T.rule}`, position: 'sticky', top: 0, background: T.bg, zIndex: 5 }}>
        <SWLockup size={1} />
      </div>

      {/* Hero — full bleed terrain w/ centered overlaid type. */}
      <div style={{ position: 'relative' }}>
        <TerrainSlot
          h={340}
          src={hero.src}
          mediaType={hero.meta?.type || 'video/mp4'}
          objectPosition="center"
          style={{ background: T.bg, height: 'clamp(280px, 38vh, 420px)' }}
        >
          <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(180deg, rgba(14,14,16,0.55) 0%, rgba(14,14,16,0.15) 45%, rgba(14,14,16,0.95) 100%)' }} />
          <div style={{ position: 'absolute', inset: 0, padding: 'clamp(24px, 4vh, 36px) clamp(28px, 4vw, 48px)', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'flex-end', textAlign: 'center' }}>
            <Mono size={11} color={T.gold}>// 2026 COUNTRY EVENT SERIES</Mono>
            <Bebas size={88} style={{ marginTop: 8, lineHeight: 0.86, fontSize: 'clamp(44px, 6.5vw, 92px)', letterSpacing: '-0.005em' }}>SPECIAL WARFARE<br/>TRAINING DAY.</Bebas>
            <Body size={17} color={T.bone} style={{ marginTop: 14, maxWidth: 640 }}>
              Six cities. One standard. Show up, see what the pipeline asks of you.
            </Body>
            {events.length > 0 && (
              <div style={{ marginTop: 22 }}>
                <PrimaryCTA onClick={scrollToEvents} style={{ fontSize: 14, padding: '13px 20px' }}>
                  ↓ Click a location to sign up
                </PrimaryCTA>
              </div>
            )}
          </div>
        </TerrainSlot>
      </div>

      {/* Tour stops */}
      <div ref={eventsRef} style={{ padding: '32px 48px 48px' }}>
        <Mono size={11} color={T.gold}>// 01 UPCOMING EVENTS</Mono>
        <Bebas size={44} as="h2" style={{ marginTop: 8, marginBottom: 20, lineHeight: 0.92, display: 'block' }}>UPCOMING EVENTS.</Bebas>
        {events.length === 0 && (
          <div style={{ padding: '40px 24px', border: `1px solid ${T.rule}`, textAlign: 'center', background: T.bg2 }}>
            <Mono size={11} color={T.mute}>// NO UPCOMING EVENTS</Mono>
            <Body size={14} color={T.mute} style={{ display: 'block', marginTop: 10 }}>
              Check back soon — new tour stops are posted as venues confirm.
            </Body>
          </div>
        )}
        {events.length > 0 && (
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 16 }}>
          {events.map(e => {
            const isHover = hovered === e.code;
            return (
              <button
                key={e.code}
                onClick={() => go('signup', { eventCode: e.code })}
                onMouseEnter={() => setHovered(e.code)}
                onMouseLeave={() => setHovered(null)}
                style={{
                  textAlign: 'left',
                  background: isHover ? T.bg2 : 'transparent',
                  border: `1px solid ${isHover ? T.gold : T.rule}`,
                  color: T.bone,
                  padding: '22px 22px 18px',
                  position: 'relative',
                  cursor: 'pointer',
                  transition: 'all .15s',
                }}
              >
                <CornerStamp time="0600" place={e.state} style={{ top: 14, right: 14 }} />
                <div style={{ marginTop: 18 }}>
                  <Bebas size={42}>{e.city}</Bebas>
                  <Mono size={10} color={T.mute} style={{ display: 'block', marginTop: 4 }}>{e.state} · {e.date}</Mono>
                  <Mono size={10} color={T.mute} style={{ display: 'block', marginTop: 2 }}>{e.venue}</Mono>
                </div>
                <div style={{ marginTop: 22, paddingTop: 16, borderTop: `1px solid ${T.rule}`, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                  <Mono size={10} color={T.mute}>FREE · CANDIDATE PORTAL</Mono>
                  <Bebas size={16} color={isHover ? T.gold : T.bone}>Sign up →</Bebas>
                </div>
              </button>
            );
          })}
        </div>
        )}
      </div>

      {/* About — what is Training Day */}
      <div style={{ background: T.bg2, borderTop: `1px solid ${T.rule}`, padding: '72px 48px 64px' }}>
        <Mono size={11} color={T.gold}>// 02 ABOUT THE EVENT</Mono>
        <Bebas size={64} as="h2" style={{ marginTop: 14, lineHeight: 0.92, display: 'block' }}>{about.headline}</Bebas>
        {about.intro.map((p, i) => (
          <Body key={i} size={15} color={T.bone} style={{ marginTop: i === 0 ? 22 : 14, maxWidth: 680, display: 'block' }}>{p}</Body>
        ))}

        {/* What to Expect — 2×2 grid */}
        <div style={{ marginTop: 44 }}>
          <Mono size={11} color={T.mute}>// WHAT TO EXPECT</Mono>
          <div style={{ marginTop: 16, display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gap: 14 }}>
            {about.expect.map(b => (
              <div key={b.num} style={{ border: `1px solid ${T.rule}`, padding: '22px 22px 20px', background: T.bg, position: 'relative' }}>
                <Mono size={10} color={T.mute}>EVENT · {b.num}</Mono>
                <Bebas size={32} style={{ marginTop: 8, display: 'block', lineHeight: 1 }}>{b.title}</Bebas>
                <Body size={14} color={T.bone} style={{ marginTop: 12, display: 'block' }}>{b.body}</Body>
              </div>
            ))}
          </div>
        </div>

        {/* Who Should Come — vertical bordered rows */}
        <div style={{ marginTop: 48 }}>
          <Mono size={11} color={T.mute}>// WHO SHOULD COME</Mono>
          <div style={{ marginTop: 14, borderTop: `1px solid ${T.rule}` }}>
            {about.who.map((w, i) => (
              <div key={i} style={{ display: 'grid', gridTemplateColumns: '160px 1fr', gap: 24, padding: '18px 0', borderBottom: `1px solid ${T.rule}`, alignItems: 'baseline' }}>
                <Mono size={11} color={T.gold}>{w.label}</Mono>
                <Body size={14} color={T.bone} style={{ maxWidth: 680, display: 'block' }}>{w.text}</Body>
              </div>
            ))}
          </div>
        </div>

        {/* Bottom Line — callout */}
        <div style={{ marginTop: 44, borderLeft: `3px solid ${T.gold}`, padding: '14px 22px', background: T.bg }}>
          <Mono size={10} color={T.gold}>// THE BOTTOM LINE</Mono>
          <Body size={17} color={T.bone} style={{ marginTop: 8, maxWidth: 680, display: 'block' }}>{about.bottomLine}</Body>
        </div>
      </div>

      {/* Final CTA — for visitors who scrolled all the way through. Points
          back at the events grid above so they can pick a city. */}
      {events.length > 0 && (
        <div style={{ padding: '64px 48px', borderTop: `1px solid ${T.rule}`, textAlign: 'center' }}>
          <Mono size={11} color={T.gold} style={{ display: 'block' }}>// READY TO SHOW UP?</Mono>
          <Bebas size={56} as="h2" style={{ marginTop: 12, lineHeight: 0.95, display: 'block' }}>PICK A LOCATION.</Bebas>
          <Body size={15} color={T.mute} style={{ marginTop: 14, maxWidth: 540, marginLeft: 'auto', marginRight: 'auto', display: 'block' }}>
            Six cities, one standard. Free to attend. Sign up takes about three minutes.
          </Body>
          <div style={{ marginTop: 24, display: 'flex', justifyContent: 'center' }}>
            <PrimaryCTA onClick={scrollToEvents} style={{ fontSize: 15, padding: '14px 26px' }}>
              ↑ See events
            </PrimaryCTA>
          </div>
        </div>
      )}

      {/* Footer — brand left, parent service right. Admin entry is hidden
          (recruiters navigate to /#admin directly). */}
      <div style={{ padding: '32px 48px', borderTop: `1px solid ${T.rule}`, display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 24 }}>
        <SWLockup size={0.85} />
        <Mono size={10} color={T.mute}>SW TRAINING DAY · 2026 SERIES</Mono>
        <div style={{ display: 'flex', alignItems: 'center', gap: 18 }}>
          <div style={{ background: T.bone, padding: '6px 12px', display: 'flex', alignItems: 'center' }}>
            <img
              src="media/slideshow/01-special-warfare.png"
              alt="U.S. Air Force Special Warfare"
              onError={e => { e.currentTarget.parentElement.style.display = 'none'; }}
              style={{ height: 30, width: 'auto', display: 'block' }}
            />
          </div>
        </div>
      </div>
    </div>
  );
};

window.Landing = Landing;
