// PAGE — composes the full scrollable homepage inside a scroll container
// Plus: fixed bottom CTA that appears after the hero

function KBPage() {
  const K = window.KB;
  const { Phone, Line, Sparkle } = window.KBIcon;
  const scrollRef = React.useRef(null);
  const [showFixedCta, setShowFixedCta] = React.useState(false);

  React.useEffect(() => {
    const el = scrollRef.current;
    if (!el) return;

    const onScroll = () => setShowFixedCta(el.scrollTop > 640 || window.scrollY > 640);
    el.addEventListener('scroll', onScroll);
    window.addEventListener('scroll', onScroll, { passive: true });

    // Section reveal — viewport-based (works for both window and inner-div scroll)
    const sections = Array.from(el.querySelectorAll('section')).slice(1);
    sections.forEach(s => s.classList.add('kb-reveal'));
    const revealObs = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          entry.target.classList.add('kb-visible');
          revealObs.unobserve(entry.target);
        }
      });
    }, { root: null, threshold: 0.06, rootMargin: '0px 0px -24px 0px' });
    sections.forEach(s => revealObs.observe(s));

    return () => {
      el.removeEventListener('scroll', onScroll);
      window.removeEventListener('scroll', onScroll);
      revealObs.disconnect();
    };
  }, []);

  return (
    <div style={{ position: 'relative', width: '100%', height: '100%' }}>
      <div
        ref={scrollRef}
        style={{
          width: '100%', height: '100%',
          overflowY: 'auto',
          overflowX: 'hidden',
          background: K.navy900,
        }}
        data-screen-label="01 Mobile Homepage"
      >
        <KBHero />
        <KBMethods />
        <KBCategories />
        <KBTrust />
        <KBStats />
        <KBRecruitBanner />
        <KBForm />
        <KBFooter />
      </div>

      <KBModals />

      {/* Fixed bottom CTA */}
      <div style={{
        position: 'absolute', left: 0, right: 0, bottom: 0,
        padding: '12px 14px 36px',
        background: `linear-gradient(180deg, transparent 0%, ${K.navy900}ee 30%, ${K.navy900} 60%)`,
        pointerEvents: 'none',
        zIndex: 20,
        opacity: showFixedCta ? 1 : 0,
        transform: `translateY(${showFixedCta ? 0 : 30}px)`,
        transition: `all 0.35s ${K.ease}`,
      }}>
        <div style={{
          pointerEvents: 'auto',
          display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 6,
          background: K.navy800,
          border: `1px solid ${K.gold}40`,
          borderRadius: 10,
          padding: 6,
          boxShadow: '0 20px 40px rgba(0,0,0,0.5)',
        }}>
          <a href="tel:0644008382" style={{
            height: 48, border: 'none', borderRadius: 6,
            background: 'rgba(255,255,255,0.04)',
            color: K.goldLight, textDecoration: 'none',
            fontFamily: K.fontSans, fontWeight: 800, fontSize: 11.5,
            display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 2,
          }}>
            <Phone size={16} color={K.goldLight} />
            <span style={{ fontSize: 9.5, letterSpacing: '0.08em' }}>電話</span>
          </a>
          <a href="https://line.me/ti/p/xWmu96gEhw" target="_blank" rel="noopener noreferrer" style={{
            height: 48, border: 'none', borderRadius: 6,
            background: '#06C755',
            color: '#fff',
            fontFamily: K.fontSans, fontWeight: 800, fontSize: 11.5,
            display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 2,
            textDecoration: 'none',
          }}>
            <Line size={16} color="#fff" />
            <span style={{ fontSize: 9.5, letterSpacing: '0.08em' }}>LINE査定</span>
          </a>
          <a href="#form" onClick={() => {
            const s = document.querySelector('#form');
            if (s) s.scrollIntoView({ behavior: 'smooth', block: 'start' });
          }} style={{
            height: 48, border: 'none', borderRadius: 6,
            background: `linear-gradient(180deg, ${K.goldLight}, ${K.gold} 50%, ${K.goldDeep})`,
            color: K.navy900, textDecoration: 'none',
            fontFamily: K.fontSans, fontWeight: 900, fontSize: 11.5,
            display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 2,
            boxShadow: '0 6px 16px rgba(201,169,97,0.3)',
          }}>
            <Sparkle size={14} color={K.navy900} />
            <span style={{ fontSize: 9.5, letterSpacing: '0.08em' }}>無料査定</span>
          </a>
        </div>
      </div>
    </div>
  );
}

function KBRecruitBanner() {
  const K = window.KB;
  return (
    <section style={{ background: K.navy900, padding: '0 20px 40px' }}>
      <a href="/legacy/recruit.html" style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '20px 20px',
        border: `1px solid ${K.gold}35`,
        background: 'rgba(201,169,97,0.04)',
        textDecoration: 'none',
        borderRadius: 2,
      }}>
        <div>
          <div style={{
            fontFamily: K.fontMono, fontSize: 9,
            letterSpacing: '0.28em', color: K.goldLight,
            marginBottom: 6, textTransform: 'uppercase',
          }}>Careers</div>
          <div style={{
            fontFamily: K.fontSerif, fontSize: 16, fontWeight: 700,
            color: K.ink, letterSpacing: '0.04em',
          }}>採用情報</div>
          <div style={{
            fontFamily: K.fontSans, fontSize: 12,
            color: K.inkDim, marginTop: 4, lineHeight: 1.6,
          }}>買取査定員、募集中</div>
        </div>
        <div style={{
          fontFamily: K.fontMono, fontSize: 18,
          color: K.gold, opacity: 0.7,
        }}>→</div>
      </a>
    </section>
  );
}

window.KBPage = KBPage;
