// Sections 5–8: Check-ins, Continuity, Who it's for, CTA + footer

// ─── Section 5 — Daily check-ins ──────────────────────────────────
function CheckIns() {
  const [pain, setPain] = useState(4);
  const [swelling, setSwelling] = useState('mild');
  const [submitted, setSubmitted] = useState(false);

  const submit = () => {
    setSubmitted(true);
    setTimeout(() => setSubmitted(false), 5000);
  };

  // History data — yesterday/today
  const history = [
    { day: 'Day 12', pain: 7, swelling: 'mod' },
    { day: 'Day 13', pain: 6, swelling: 'mod' },
    { day: 'Day 14', pain: 5, swelling: 'mild' },
    { day: 'Day 15', pain: pain, swelling: swelling, today: true },
  ];

  return (
    <section className="section">
      <div className="container">
        <div style={{ maxWidth: 760, marginBottom: 70 }}>
          <Reveal>
            <p className="eyebrow" style={{ margin: 0, marginBottom: 22 }}>Daily check-ins</p>
          </Reveal>
          <h2 className="display-lg" style={{ margin: '0 0 22px' }}>
            <StaggerWords text="A two-tap check-in." step={55} />
            <br />
            <span style={{ color: 'var(--fg-muted)' }}>
              <StaggerWords text="Pain. Swelling. That's it." delay={140} step={55} />
            </span>
          </h2>
          <Reveal delay={200}>
            <p className="body-lg" style={{ margin: 0, maxWidth: 560 }}>
              The streak isn't gamification. It's the smallest thing we can ask for that builds the dataset that makes coaching real.
            </p>
          </Reveal>
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: 'minmax(280px, 380px) 1fr', gap: 60, alignItems: 'flex-start' }} className="checkin-grid">
          {/* Mock UI */}
          <Reveal delay={120}>
            <div className="card" style={{ padding: 0, overflow: 'hidden', background: 'oklch(0.16 0.005 240)' }}>
              <div style={{ padding: '18px 22px', borderBottom: '1px solid var(--line-soft)', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                  <div style={{ width: 8, height: 8, borderRadius: '50%', background: 'var(--accent)', boxShadow: '0 0 10px var(--accent)' }} />
                  <span className="mono" style={{ fontSize: 11, letterSpacing: '0.18em' }}>DAY 15 · STREAK 12</span>
                </div>
                <span className="mono" style={{ fontSize: 11, color: 'var(--fg-muted)' }}>9:14 AM</span>
              </div>

              <div style={{ padding: 24 }}>
                <div style={{ marginBottom: 26 }}>
                  <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 14 }}>
                    <span style={{ fontSize: 14, color: 'var(--fg-muted)' }}>Pain right now</span>
                    <span className="mono" style={{ fontSize: 22, color: 'var(--fg)', fontVariantNumeric: 'tabular-nums' }}>{pain}<span style={{ color: 'var(--fg-faint)', fontSize: 14 }}>/10</span></span>
                  </div>
                  <input
                    type="range" min={0} max={10} value={pain}
                    onChange={(e) => setPain(parseInt(e.target.value))}
                    className="pain-slider"
                  />
                  <div style={{ display: 'flex', justifyContent: 'space-between', marginTop: 6, fontSize: 11, color: 'var(--fg-faint)' }} className="mono">
                    <span>NONE</span><span>WORST</span>
                  </div>
                </div>

                <div style={{ marginBottom: 28 }}>
                  <div style={{ fontSize: 14, color: 'var(--fg-muted)', marginBottom: 14 }}>Swelling</div>
                  <div style={{ display: 'flex', gap: 8 }}>
                    {[['none', 'None'], ['mild', 'Mild'], ['mod', 'Moderate'], ['high', 'High']].map(([k, l]) => (
                      <button
                        key={k}
                        onClick={() => setSwelling(k)}
                        style={{
                          flex: 1,
                          padding: '10px 4px',
                          background: swelling === k ? 'var(--accent)' : 'transparent',
                          color: swelling === k ? '#000' : 'var(--fg-dim)',
                          border: `1px solid ${swelling === k ? 'var(--accent)' : 'var(--line)'}`,
                          borderRadius: 8,
                          fontSize: 12,
                          fontWeight: 500,
                          cursor: 'pointer',
                          transition: 'all 0.3s var(--ease-out)',
                          fontFamily: 'inherit',
                        }}
                      >
                        {l}
                      </button>
                    ))}
                  </div>
                </div>

                <button
                  onClick={submit}
                  className="btn btn-primary"
                  style={{ width: '100%', padding: '14px', cursor: 'pointer', border: 'none', fontFamily: 'inherit', fontSize: 14 }}
                >
                  {submitted ? '✓ Logged' : 'Submit check-in'}
                </button>
              </div>

              {/* Coached response */}
              <div style={{
                padding: '20px 24px',
                borderTop: '1px solid var(--line-soft)',
                background: submitted ? 'rgba(0,255,136,0.04)' : 'transparent',
                transition: 'background 0.6s var(--ease-out)',
                opacity: submitted ? 1 : 0.55,
              }}>
                <div className="mono" style={{ fontSize: 10, letterSpacing: '0.18em', color: 'var(--accent)', marginBottom: 8 }}>
                  PROTOCOL
                </div>
                <div style={{ fontSize: 14, lineHeight: 1.55, color: 'var(--fg-dim)' }}>
                  {submitted ? (
                    <>Pain down from <span style={{ color: 'var(--fg)' }}>7 to {pain}</span> over four days — that's progress. Swelling trending down too. Today's session: light cycling, 12 minutes.</>
                  ) : (
                    <>Submit to see today's coached response.</>
                  )}
                </div>
              </div>
            </div>
          </Reveal>

          {/* Trend visualization */}
          <Reveal delay={240}>
            <div>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 24 }}>
                <span className="mono" style={{ fontSize: 11, letterSpacing: '0.18em', color: 'var(--fg-muted)' }}>PAIN, LAST 4 DAYS</span>
                <span style={{ display: 'flex', alignItems: 'center', gap: 8, fontSize: 13, color: 'var(--accent)' }}>
                  <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
                    <path d="M2 10L7 4L12 10" stroke="currentColor" strokeWidth="1.5" />
                  </svg>
                  Trending down
                </span>
              </div>

              <div style={{ display: 'flex', alignItems: 'flex-end', gap: 14, height: 220, marginBottom: 14 }}>
                {history.map((h, i) => (
                  <div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12 }}>
                    <div style={{
                      fontSize: 13,
                      color: h.today ? 'var(--accent)' : 'var(--fg-muted)',
                      fontFamily: 'inherit',
                    }} className="mono">
                      {h.pain}
                    </div>
                    <div style={{
                      width: '100%',
                      maxWidth: 56,
                      height: `${(h.pain / 10) * 160}px`,
                      background: h.today
                        ? 'linear-gradient(180deg, var(--accent), oklch(0.6 0.18 155))'
                        : 'oklch(0.28 0.005 240)',
                      borderRadius: 4,
                      transition: 'height 0.5s var(--ease-out)',
                    }} />
                    <div style={{ fontSize: 11, color: h.today ? 'var(--fg)' : 'var(--fg-faint)' }} className="mono">
                      {h.day.replace('Day ', 'D')}
                    </div>
                  </div>
                ))}
              </div>

              <div style={{ borderTop: '1px solid var(--line-soft)', paddingTop: 22, marginTop: 14 }}>
                <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 24 }}>
                  <div>
                    <div className="mono" style={{ fontSize: 10, letterSpacing: '0.18em', color: 'var(--fg-faint)', marginBottom: 8 }}>STREAK</div>
                    <div style={{ fontSize: 28, color: 'var(--fg)', letterSpacing: '-0.02em', fontVariantNumeric: 'tabular-nums' }} className="mono">12</div>
                  </div>
                  <div>
                    <div className="mono" style={{ fontSize: 10, letterSpacing: '0.18em', color: 'var(--fg-faint)', marginBottom: 8 }}>AVG PAIN, WK</div>
                    <div style={{ fontSize: 28, color: 'var(--fg)', letterSpacing: '-0.02em', fontVariantNumeric: 'tabular-nums' }} className="mono">5.2</div>
                  </div>
                  <div>
                    <div className="mono" style={{ fontSize: 10, letterSpacing: '0.18em', color: 'var(--fg-faint)', marginBottom: 8 }}>FLARES</div>
                    <div style={{ fontSize: 28, color: 'var(--fg)', letterSpacing: '-0.02em', fontVariantNumeric: 'tabular-nums' }} className="mono">0</div>
                  </div>
                </div>
              </div>
            </div>
          </Reveal>
        </div>
      </div>

      <style>{`
        .pain-slider {
          width: 100%;
          -webkit-appearance: none;
          appearance: none;
          height: 4px;
          background: linear-gradient(90deg, var(--accent) ${pain*10}%, var(--line-soft) ${pain*10}%);
          outline: none;
          border-radius: 2px;
        }
        .pain-slider::-webkit-slider-thumb {
          -webkit-appearance: none;
          appearance: none;
          width: 18px; height: 18px;
          border-radius: 50%;
          background: #fff;
          cursor: pointer;
          box-shadow: 0 0 0 4px rgba(0,255,136,0.18);
          transition: transform 0.2s;
        }
        .pain-slider::-webkit-slider-thumb:hover { transform: scale(1.1); }
        .pain-slider::-moz-range-thumb {
          width: 18px; height: 18px;
          border-radius: 50%;
          background: #fff;
          border: none;
          cursor: pointer;
          box-shadow: 0 0 0 4px rgba(0,255,136,0.18);
        }
        @media (max-width: 860px) {
          .checkin-grid { grid-template-columns: 1fr !important; gap: 40px !important; }
        }
      `}</style>
    </section>
  );
}

// ─── Section 6 — Longitudinal continuity ──────────────────────────
function Continuity() {
  const [ref, progress] = useStickyProgress();
  // Path draws as user scrolls; messages light up at thresholds
  const pathLen = 1200;
  const dashOffset = lerp(pathLen, 0, smoothstep(0.05, 0.85, progress));

  const moments = [
    { day: 3,  side: 'L', text: '"My quad won\'t fire."', tone: 'concern' },
    { day: 11, side: 'R', text: 'Quad sets, 3 × 10. Twice today.', tone: 'coach' },
    { day: 22, side: 'L', text: '"Worried about left calf."', tone: 'concern' },
    { day: 30, side: 'R', text: 'Calf trending fine — but on Day 3 you said your quad wouldn\'t fire. It\'s firing now.', tone: 'callback' },
  ];

  return (
    <section className="section" style={{ paddingTop: 0, paddingBottom: 0 }}>
      <div ref={ref} style={{ height: '280vh', position: 'relative' }}>
        <div className="sticky-stage">
          <div className="container" style={{ width: '100%' }}>
            <div style={{ maxWidth: 720, marginBottom: 50 }}>
              <Reveal>
                <p className="eyebrow" style={{ margin: 0, marginBottom: 22 }}>Longitudinal</p>
              </Reveal>
              <h2 className="display-md" style={{ margin: '0 0 22px' }}>
                Remembers Day 3<br />
                <span style={{ color: 'var(--fg-muted)' }}>when you're on Day 30.</span>
              </h2>
              <Reveal delay={140}>
                <p className="body-lg" style={{ margin: 0, maxWidth: 560 }}>
                  Stateless chatbots forget you between sessions. Protocol carries the thread — early concerns surface again later, when they matter.
                </p>
              </Reveal>
            </div>

            {/* Thread visualization */}
            <div style={{ position: 'relative', maxWidth: 920, margin: '0 auto', minHeight: 380 }}>
              <svg viewBox="0 0 920 380" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', overflow: 'visible' }}>
                <defs>
                  <linearGradient id="threadGrad" x1="0%" y1="0%" x2="100%" y2="0%">
                    <stop offset="0%" stopColor="var(--accent)" stopOpacity="0.2" />
                    <stop offset="100%" stopColor="var(--accent)" stopOpacity="1" />
                  </linearGradient>
                </defs>
                <path
                  d="M 60 80 C 200 80, 280 280, 460 200 S 720 60, 860 280"
                  stroke="url(#threadGrad)"
                  strokeWidth="1.5"
                  fill="none"
                  strokeDasharray={pathLen}
                  strokeDashoffset={dashOffset}
                  style={{ transition: 'stroke-dashoffset 0.1s linear' }}
                />
                {/* dots at moment positions */}
                {[
                  { x: 100, y: 84,  threshold: 0.10 },
                  { x: 300, y: 220, threshold: 0.32 },
                  { x: 540, y: 170, threshold: 0.54 },
                  { x: 800, y: 250, threshold: 0.78 },
                ].map((dot, i) => {
                  const lit = progress > dot.threshold;
                  return (
                    <circle
                      key={i}
                      cx={dot.x} cy={dot.y} r={lit ? 6 : 3}
                      fill={lit ? 'var(--accent)' : 'var(--line)'}
                      style={{
                        transition: 'all 0.5s var(--ease-out)',
                        filter: lit ? 'drop-shadow(0 0 8px var(--accent))' : 'none',
                      }}
                    />
                  );
                })}
              </svg>

              {/* Moment cards */}
              <div style={{ position: 'relative', display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 20, paddingTop: 20 }}>
                {moments.map((m, i) => {
                  const reveal = smoothstep(0.08 + i * 0.18, 0.22 + i * 0.18, progress);
                  const isCallback = m.tone === 'callback';
                  const align = m.side === 'L' ? 'flex-start' : 'flex-end';
                  return (
                    <div key={i} style={{
                      display: 'flex',
                      justifyContent: align,
                      gridColumn: m.side === 'L' ? '1' : '2',
                      transform: `translateY(${lerp(20, 0, reveal)}px)`,
                      opacity: reveal,
                    }}>
                      <div style={{
                        maxWidth: 340,
                        padding: '14px 18px',
                        borderRadius: 14,
                        border: `1px solid ${isCallback ? 'rgba(0,255,136,0.4)' : 'var(--line)'}`,
                        background: isCallback ? 'rgba(0,255,136,0.05)' : 'oklch(0.14 0.005 240)',
                        fontSize: 14,
                        lineHeight: 1.5,
                        color: m.tone === 'concern' ? 'var(--fg-dim)' : 'var(--fg)',
                      }}>
                        <div className="mono" style={{
                          fontSize: 10,
                          letterSpacing: '0.18em',
                          color: isCallback ? 'var(--accent)' : 'var(--fg-faint)',
                          marginBottom: 6,
                        }}>
                          DAY {String(m.day).padStart(2, '0')} {isCallback && '· CALLBACK'}
                        </div>
                        {m.text}
                      </div>
                    </div>
                  );
                })}
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ─── Section 7 — Who it's for ─────────────────────────────────────
function WhoItsFor() {
  const forList = ['ACL reconstruction', 'Fracture', 'Rotator cuff', 'Back injury', 'Sprain', 'Tendon repair', 'Meniscus', 'Achilles', 'Ligament repair'];
  const notFor = ['Acute emergencies', 'Undiagnosed pain', 'Mental-only conditions'];

  return (
    <section className="section">
      <div className="container">
        <div style={{ maxWidth: 800, marginBottom: 60 }}>
          <Reveal>
            <p className="eyebrow" style={{ margin: 0, marginBottom: 22 }}>Who it's for</p>
          </Reveal>
          <h2 className="display-md" style={{ margin: '0 0 22px' }}>
            <StaggerWords text="Honest about scope." step={55} />
            <br />
            <span style={{ color: 'var(--fg-muted)' }}>
              <StaggerWords text="Not for everyone with pain." delay={120} step={55} />
            </span>
          </h2>
          <Reveal delay={200}>
            <p className="body-lg" style={{ margin: 0, maxWidth: 640 }}>
              If you're recovering from a defined musculoskeletal injury where the appointments are weeks apart and the days in between feel uncertain — Protocol is for you.
            </p>
          </Reveal>
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: '1fr', gap: 50 }}>
          <Reveal delay={120}>
            <div>
              <div className="mono" style={{ fontSize: 11, letterSpacing: '0.18em', color: 'var(--fg-muted)', marginBottom: 18 }}>
                BUILT FOR
              </div>
              <div style={{ display: 'flex', flexWrap: 'wrap', gap: 10 }}>
                {forList.map(t => <span key={t} className="chip">{t}</span>)}
              </div>
            </div>
          </Reveal>

          <Reveal delay={240}>
            <div>
              <div className="mono" style={{ fontSize: 11, letterSpacing: '0.18em', color: 'var(--fg-faint)', marginBottom: 18 }}>
                NOT FOR
              </div>
              <div style={{ display: 'flex', flexWrap: 'wrap', gap: 10 }}>
                {notFor.map(t => <span key={t} className="chip chip-x">{t}</span>)}
              </div>
              <p className="body-md" style={{ marginTop: 22, maxWidth: 580, color: 'var(--fg-muted)' }}>
                Protocol is not a replacement for medical care. If you're in acute pain or your symptoms are escalating, call your clinician.
              </p>
            </div>
          </Reveal>
        </div>
      </div>
    </section>
  );
}

// ─── Section 8 — CTA ──────────────────────────────────────────────
function CTA() {
  return (
    <section id="cta" className="section" style={{ paddingTop: '16vh', paddingBottom: '10vh', position: 'relative', overflow: 'hidden' }}>
      <div style={{
        position: 'absolute',
        inset: 0,
        background: 'radial-gradient(ellipse 70% 50% at 50% 100%, rgba(0,255,136,0.14), transparent 70%)',
        pointerEvents: 'none',
      }} />
      <div className="container" style={{ position: 'relative', zIndex: 1 }}>
        <div style={{ maxWidth: 880, margin: '0 auto', textAlign: 'center' }}>
          <Reveal>
            <p className="eyebrow" style={{ margin: 0, marginBottom: 26 }}>Begin</p>
          </Reveal>
          <h2 className="display-lg" style={{ margin: '0 0 30px' }}>
            <StaggerWords text="Day one starts" step={55} />
            <br />
            <span style={{ color: 'var(--accent)' }}>
              <StaggerWords text="when you start." delay={140} step={55} />
            </span>
          </h2>
          <Reveal delay={200}>
            <p className="body-lg" style={{ margin: '0 auto 50px', maxWidth: 560 }}>
              Protocol is in closed beta. Open the app and recover with something on your side.
            </p>
          </Reveal>

          <Reveal delay={300}>
            <div style={{ display: 'flex', gap: 12, justifyContent: 'center', flexWrap: 'wrap' }}>
              <a className="btn btn-primary" href="https://app.protocol-ai.net" target="_blank" rel="noopener noreferrer" style={{ padding: '18px 30px', fontSize: 16 }}>
                Try the beta now <span className="btn-arrow">→</span>
              </a>
            </div>
          </Reveal>

          <Reveal delay={400}>
            <p className="body-sm" style={{ marginTop: 28, color: 'var(--fg-muted)' }}>
              Closed Beta · iOS, Android, Web
            </p>
          </Reveal>
        </div>
      </div>
    </section>
  );
}

// ─── Footer ───────────────────────────────────────────────────────
function Footer() {
  return (
    <footer className="footer">
      <div className="container">
        <div style={{ display: 'grid', gridTemplateColumns: '1fr auto', gap: 40, alignItems: 'flex-start' }} className="footer-grid">
          <div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 14 }}>
              <div className="nav-dot" />
              <span style={{ fontWeight: 600, color: 'var(--fg)', fontSize: 16 }}>Protocol</span>
            </div>
            <p style={{ margin: 0, maxWidth: 460, lineHeight: 1.55, color: 'var(--fg-muted)' }}>
              Protocol is not a replacement for medical advice, diagnosis or treatment. Always consult a qualified clinician about your injury.
            </p>
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 10, fontSize: 13, color: 'var(--fg-muted)', textAlign: 'right' }} className="footer-meta">
            <a href="mailto:naman.sharma@protocol-ai.net" style={{ color: 'var(--fg)', textDecoration: 'none', borderBottom: '1px solid var(--line)', paddingBottom: 2, transition: 'border-color 0.3s, color 0.3s' }} onMouseEnter={(e) => { e.currentTarget.style.borderColor = 'var(--accent)'; e.currentTarget.style.color = 'var(--accent)'; }} onMouseLeave={(e) => { e.currentTarget.style.borderColor = 'var(--line)'; e.currentTarget.style.color = 'var(--fg)'; }}>naman.sharma@protocol-ai.net</a>
            <span>Built in Sydney · 2026</span>
            <span>namansharma14@github</span>
          </div>
        </div>
        <div style={{ marginTop: 50, paddingTop: 24, borderTop: '1px solid var(--line-soft)', display: 'flex', justifyContent: 'space-between', fontSize: 12, color: 'var(--fg-faint)', flexWrap: 'wrap', gap: 12 }}>
          <span>© 2026 Protocol</span>
          <span className="mono" style={{ letterSpacing: '0.16em' }}>RECOVERY · ANCHORED</span>
        </div>
      </div>
      <style>{`
        @media (max-width: 720px) {
          .footer-grid { grid-template-columns: 1fr !important; }
          .footer-meta { text-align: left !important; }
        }
      `}</style>
    </footer>
  );
}

// ─── Nav ──────────────────────────────────────────────────────────
function Nav() {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 40);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return (
    <nav className="nav" style={{ background: scrolled ? 'rgba(0,0,0,0.7)' : 'linear-gradient(180deg, rgba(0,0,0,0.5), rgba(0,0,0,0))' }}>
      <a href="#" className="nav-logo" style={{ color: 'var(--fg)', textDecoration: 'none' }}>
        <span className="nav-dot" />
        Protocol
      </a>
      <div className="nav-links">
<a className="btn btn-primary" href="https://app.protocol-ai.net" target="_blank" rel="noopener noreferrer" style={{ padding: '10px 18px', fontSize: 13 }}>
          Try beta <span className="btn-arrow">→</span>
        </a>
      </div>
    </nav>
  );
}

Object.assign(window, { CheckIns, Continuity, WhoItsFor, CTA, Footer, Nav });
