// onnext-screens.jsx — Onnext prototype screens
const { DRIFT, Mark, Wordmark, MiniChart, ScoreRing, BandPill, Readout, Chip,
  PrimaryBtn, GhostBtn, Eyebrow, SourceIcon, BottomNav } = window;

function Screen({ children }) {
  return <div style={{ background:DRIFT.midnight, color:DRIFT.ink, fontFamily:DRIFT.font, height:'100%', display:'flex', flexDirection:'column' }}>{children}</div>;
}
function TopBar({ left, right }) {
  return <div style={{ padding:'58px 20px 14px', display:'flex', alignItems:'center', justifyContent:'space-between', flex:'none' }}>{left}{right}</div>;
}
function Body({ children, style }) { return <div style={{ flex:1, overflow:'auto', padding:'0 20px', ...style }}>{children}</div>; }
function Card({ children, style, onClick }) { return <div onClick={onClick} style={{ background:DRIFT.carbon, border:`1px solid ${DRIFT.line}`, borderRadius:18, padding:18, cursor:onClick?'pointer':'default', ...style }}>{children}</div>; }

// ── ONBOARDING — connect your stack ───────────────────────────
function OnboardingConnect({ onNext }) {
  const [on, setOn] = React.useState(['Strava','Garmin']);
  const t = v => setOn(on.includes(v)?on.filter(x=>x!==v):[...on,v]);
  const SRC = [
    ['Strava','Activity, distance, effort'],
    ['Garmin','Heart rate, sleep, stress'],
    ['WHOOP','Recovery, strain, HRV'],
    ['Oura','Sleep, readiness'],
    ['TrainingPeaks','Planned vs actual load'],
  ];
  return (
    <Screen>
      <div style={{ padding:'58px 20px 6px', flex:'none' }}>
        <div style={{ display:'flex', alignItems:'center', gap:10, marginBottom:24 }}><Mark w={30} sw={5.5}/><Wordmark size={20}/></div>
        <Eyebrow color={DRIFT.signal}>Step 1 / 3 · Connect your stack</Eyebrow>
        <h1 style={{ margin:'12px 0 6px', fontSize:29, fontWeight:600, letterSpacing:'-1.5px', lineHeight:1.08 }}>Point Onnext at your data</h1>
        <p style={{ margin:0, color:DRIFT.muted, fontSize:15 }}>It learns your baseline from what you already track. Read-only. Nothing posts back.</p>
      </div>
      <Body style={{ paddingTop:20 }}>
        <div style={{ display:'flex', flexDirection:'column', gap:10 }}>
          {SRC.map(([n,d])=>{
            const active = on.includes(n);
            return (
              <button key={n} onClick={()=>t(n)} style={{ textAlign:'left', cursor:'pointer', fontFamily:DRIFT.font, padding:'15px 16px', borderRadius:14, display:'flex', alignItems:'center', gap:14,
                border:`1px solid ${active?DRIFT.signal+'66':DRIFT.line}`, background: active?'rgba(201,242,93,0.06)':DRIFT.carbon }}>
                <SourceIcon name={n} on={active}/>
                <div style={{ flex:1 }}>
                  <div style={{ fontSize:16, fontWeight:600, color:DRIFT.ink }}>{n}</div>
                  <div style={{ fontSize:12.5, color:DRIFT.muted, marginTop:1 }}>{d}</div>
                </div>
                <span style={{ fontFamily:DRIFT.mono, fontSize:11, letterSpacing:'0.5px', color: active?DRIFT.signal:DRIFT.faint }}>{active?'LINKED':'LINK'}</span>
              </button>
            );
          })}
        </div>
        <p style={{ fontFamily:DRIFT.mono, fontSize:11.5, color:DRIFT.faint, textAlign:'center', marginTop:22, lineHeight:1.6 }}>You can run Drift on manual check-ins alone.<br/>Wearables make the signal sharper.</p>
      </Body>
      <div style={{ padding:'12px 20px 30px', flex:'none', borderTop:`1px solid ${DRIFT.line}` }}>
        <PrimaryBtn full onClick={onNext}>Continue · {on.length} linked</PrimaryBtn>
      </div>
    </Screen>
  );
}

// ── HOME A — score-forward ────────────────────────────────────
function HomeA() {
  return (
    <Screen>
      <TopBar left={<div style={{ display:'flex', alignItems:'center', gap:9 }}><Mark w={26} sw={5}/><Wordmark size={18}/></div>}
        right={<BandPill level="hold"/>}/>
      <Body>
        <div style={{ display:'flex', flexDirection:'column', alignItems:'center', padding:'14px 0 8px' }}>
          <ScoreRing score={82}/>
          <p style={{ margin:'18px 0 0', textAlign:'center', fontSize:15, color:DRIFT.muted, maxWidth:'28ch' }}>You're holding your baseline. Sleep is dipping — today protect energy, don't chase load.</p>
        </div>
        <Card style={{ marginTop:20, display:'flex', gap:18 }}>
          <Readout label="BASELINE" value="5:24" unit="/km"/>
          <div style={{ width:1, background:DRIFT.line }}/>
          <Readout label="THIS WK" value="3" unit=" sess"/>
          <div style={{ width:1, background:DRIFT.line }}/>
          <Readout label="SLEEP" value="−14" unit="%" color={DRIFT.drift}/>
        </Card>
        <div style={{ marginTop:18 }}>
          <Eyebrow>Today's one action</Eyebrow>
          <Card style={{ marginTop:10, display:'flex', alignItems:'center', gap:14 }}>
            <div style={{ width:44, height:44, borderRadius:11, background:'rgba(201,242,93,0.12)', display:'grid', placeItems:'center', color:DRIFT.signal, flex:'none', fontFamily:DRIFT.mono, fontSize:13 }}>Z2</div>
            <div style={{ flex:1 }}>
              <div style={{ fontSize:15.5, fontWeight:600 }}>Easy 30 min, zone 2</div>
              <div style={{ fontSize:13, color:DRIFT.muted, marginTop:1 }}>Keeps the streak alive without cost</div>
            </div>
            <span style={{ color:DRIFT.signal, fontWeight:700, fontSize:22 }}>›</span>
          </Card>
        </div>
        <div style={{ height:8 }}/>
      </Body>
      <BottomNav active="today"/>
    </Screen>
  );
}

// ── HOME B — chart-forward telemetry ──────────────────────────
function HomeB({ onNext }) {
  return (
    <Screen>
      <TopBar left={<div><div style={{ fontFamily:DRIFT.mono, fontSize:11, color:DRIFT.muted, letterSpacing:'1px' }}>FRI · WK 24</div><h1 style={{ margin:'2px 0 0', fontSize:24, fontWeight:600, letterSpacing:'-1px' }}>Your signal</h1></div>}
        right={<div style={{ fontFamily:DRIFT.mono, fontSize:30, fontWeight:500, letterSpacing:'-1px', color:DRIFT.signal }}>82</div>}/>
      <Body>
        <Card style={{ padding:'18px 18px 14px' }}>
          <div style={{ display:'flex', justifyContent:'space-between', marginBottom:12, fontFamily:DRIFT.mono, fontSize:10.5, color:DRIFT.muted, letterSpacing:'0.8px' }}>
            <span>BASELINE vs SIGNAL · 8 WK</span><span style={{ color:DRIFT.hold }}>● HOLDING</span>
          </div>
          <MiniChart h={92} drift={true} level={0.35}/>
          <div style={{ display:'flex', justifyContent:'space-between', marginTop:10, fontFamily:DRIFT.mono, fontSize:10, color:DRIFT.faint }}>
            <span>MAY</span><span>JUN</span><span>NOW</span>
          </div>
        </Card>
        <div style={{ display:'grid', gridTemplateColumns:'1fr 1fr', gap:12, marginTop:14 }}>
          <Card><Readout label="TRAINING LOAD" value="↘ 12" unit="%" color={DRIFT.drift}/><div style={{ fontSize:12, color:DRIFT.muted, marginTop:8 }}>below your 4-wk avg</div></Card>
          <Card><Readout label="HRV TREND" value="42" unit="ms"/><div style={{ fontSize:12, color:DRIFT.muted, marginTop:8 }}>stable, within range</div></Card>
        </div>
        <Card onClick={onNext} style={{ marginTop:14, display:'flex', gap:13, alignItems:'flex-start', borderColor:DRIFT.drift+'44' }}>
          <Mark w={26} sw={5} sig={DRIFT.drift} base={DRIFT.faint}/>
          <div>
            <div style={{ fontSize:14.5, fontWeight:600, color:DRIFT.ink }}>One factor is starting to drift</div>
            <div style={{ fontSize:13.5, color:DRIFT.muted, marginTop:3 }}>Load down two weeks running. Not alarming — but the trend is. Tap to see the catch.</div>
          </div>
        </Card>
        <div style={{ height:8 }}/>
      </Body>
      <BottomNav active="today"/>
    </Screen>
  );
}

// ── ASK A — conversational answer ─────────────────────────────
function AskA() {
  return (
    <Screen>
      <TopBar left={<h1 style={{ margin:0, fontSize:24, fontWeight:600, letterSpacing:'-1px' }}>Ask</h1>}
        right={<div style={{ fontFamily:DRIFT.mono, fontSize:11, color:DRIFT.muted }}>5 sources</div>}/>
      <Body>
        {/* user query */}
        <div style={{ display:'flex', justifyContent:'flex-end', marginBottom:14 }}>
          <div style={{ background:DRIFT.carbon2, borderRadius:'14px 14px 4px 14px', padding:'11px 15px', fontSize:14.5, maxWidth:'80%' }}>Why did my half-marathon PB happen?</div>
        </div>
        {/* answer */}
        <div style={{ display:'flex', gap:10 }}>
          <Mark w={22} sw={5}/>
          <div style={{ flex:1 }}>
            <Card style={{ padding:16 }}>
              <p style={{ margin:0, fontSize:14.5, lineHeight:1.55 }}>Three things lined up in the eight weeks before it:</p>
              <div style={{ margin:'14px 0', display:'flex', flexDirection:'column', gap:10 }}>
                {[['Sleep','7h41 avg · +38 min vs usual',DRIFT.hold],['Long runs','6 of 8 weekends hit',DRIFT.hold],['Easy ratio','81% easy / 19% hard',DRIFT.signal]].map(([a,b,c])=>(
                  <div key={a} style={{ display:'flex', alignItems:'center', gap:11 }}>
                    <span style={{ width:8, height:8, borderRadius:'50%', background:c, flex:'none' }}/>
                    <span style={{ fontSize:13.5, fontWeight:600, width:78 }}>{a}</span>
                    <span style={{ fontFamily:DRIFT.mono, fontSize:12, color:DRIFT.muted }}>{b}</span>
                  </div>
                ))}
              </div>
              <p style={{ margin:0, fontSize:13.5, color:DRIFT.muted, lineHeight:1.55 }}>The PB tracked your sleep more than your mileage. Protecting sleep is your highest-leverage move.</p>
            </Card>
            <div style={{ display:'flex', gap:8, marginTop:12, flexWrap:'wrap' }}>
              <Chip>Show the data</Chip><Chip>What broke it after?</Chip>
            </div>
          </div>
        </div>
        <div style={{ height:10 }}/>
      </Body>
      <div style={{ padding:'12px 20px 30px', flex:'none' }}>
        <div style={{ display:'flex', alignItems:'center', gap:10, background:DRIFT.carbon, border:`1px solid ${DRIFT.line}`, borderRadius:14, padding:'13px 16px' }}>
          <span style={{ flex:1, fontSize:14.5, color:DRIFT.faint }}>Ask your training data anything…</span>
          <div style={{ width:34, height:34, borderRadius:9, background:DRIFT.signal, display:'grid', placeItems:'center', color:DRIFT.midnight, fontWeight:700 }}>↑</div>
        </div>
      </div>
    </Screen>
  );
}

// ── ASK B — suggested-questions launcher ──────────────────────
function AskB() {
  const Q = [
    ['What predicts my best weeks?','PATTERN'],
    ['Am I overtraining right now?','RISK'],
    ['Which sessions drive my biggest gains?','GAINS'],
    ['When am I most likely to drift?','FORECAST'],
    ['Compare this block to my last PB build','COMPARE'],
  ];
  return (
    <Screen>
      <div style={{ padding:'58px 20px 0', flex:'none' }}>
        <Mark w={34} sw={5.5}/>
        <h1 style={{ margin:'18px 0 6px', fontSize:28, fontWeight:600, letterSpacing:'-1.4px', lineHeight:1.1 }}>Ask your training<br/>data anything</h1>
        <p style={{ margin:0, color:DRIFT.muted, fontSize:15 }}>Eight weeks of Strava, Garmin and WHOOP, synthesised. Start here:</p>
      </div>
      <Body style={{ paddingTop:22 }}>
        <div style={{ display:'flex', flexDirection:'column', gap:10 }}>
          {Q.map(([q,tag])=>(
            <button key={q} style={{ textAlign:'left', cursor:'pointer', fontFamily:DRIFT.font, background:DRIFT.carbon, border:`1px solid ${DRIFT.line}`, borderRadius:14, padding:'16px 17px', display:'flex', alignItems:'center', gap:12 }}>
              <span style={{ flex:1, fontSize:15.5, fontWeight:500, color:DRIFT.ink, letterSpacing:'-0.2px' }}>{q}</span>
              <span style={{ fontFamily:DRIFT.mono, fontSize:9.5, letterSpacing:'0.8px', color:DRIFT.muted, border:`1px solid ${DRIFT.line}`, padding:'4px 7px', borderRadius:6 }}>{tag}</span>
            </button>
          ))}
        </div>
        <div style={{ height:10 }}/>
      </Body>
      <div style={{ padding:'12px 20px 30px', flex:'none' }}>
        <div style={{ display:'flex', alignItems:'center', gap:10, background:DRIFT.carbon, border:`1px solid ${DRIFT.signal}55`, borderRadius:14, padding:'13px 16px' }}>
          <span style={{ flex:1, fontSize:14.5, color:DRIFT.faint }}>Type your own…</span>
          <div style={{ width:34, height:34, borderRadius:9, background:DRIFT.signal, display:'grid', placeItems:'center', color:DRIFT.midnight, fontWeight:700 }}>↑</div>
        </div>
      </div>
    </Screen>
  );
}

// ── DRIFT CATCH — the early-warning intervention ──────────────
function DriftCatch({ onNext }) {
  return (
    <Screen>
      <Body style={{ padding:0 }}>
        <div style={{ padding:'66px 20px 24px', borderBottom:`1px solid ${DRIFT.line}` }}>
          <Eyebrow color={DRIFT.drift}>Drift detected · early</Eyebrow>
          <h1 style={{ margin:'14px 0 0', fontSize:27, fontWeight:600, letterSpacing:'-1.4px', lineHeight:1.12 }}>You're starting to<br/>pull off baseline</h1>
          <div style={{ marginTop:20 }}><MiniChart h={86} drift={true} level={0.7}/></div>
          <div style={{ display:'flex', gap:10, marginTop:14 }}>
            <BandPill level="watch"/>
            <span style={{ fontFamily:DRIFT.mono, fontSize:11.5, color:DRIFT.muted, alignSelf:'center' }}>caught 11 days early</span>
          </div>
        </div>
        <div style={{ padding:'22px 20px' }}>
          <Eyebrow>What's moving</Eyebrow>
          <div style={{ margin:'12px 0 20px', display:'flex', flexDirection:'column', gap:9 }}>
            {[['Sessions','5/wk → 3/wk',DRIFT.watch],['Sleep','down 14% this week',DRIFT.watch],['Check-ins','quiet 3 days',DRIFT.drift]].map(([a,b,c])=>(
              <div key={a} style={{ display:'flex', alignItems:'center', gap:11, padding:'12px 14px', background:DRIFT.carbon, borderRadius:12, border:`1px solid ${DRIFT.line}` }}>
                <span style={{ width:8, height:8, borderRadius:'50%', background:c, flex:'none' }}/>
                <span style={{ fontSize:14, fontWeight:600, flex:1 }}>{a}</span>
                <span style={{ fontFamily:DRIFT.mono, fontSize:12, color:DRIFT.muted }}>{b}</span>
              </div>
            ))}
          </div>
          <div style={{ background:'rgba(201,242,93,0.07)', border:`1px solid ${DRIFT.signal}44`, borderRadius:16, padding:'18px 18px' }}>
            <div style={{ fontFamily:DRIFT.mono, fontSize:10.5, letterSpacing:'1px', color:DRIFT.signal, marginBottom:9 }}>THE SMALLEST CORRECTION</div>
            <p style={{ margin:0, fontSize:17, fontWeight:500, letterSpacing:'-0.3px', lineHeight:1.35 }}>Don't chase the missed sessions. Put one easy 20-minute run on the calendar for tomorrow.</p>
          </div>
        </div>
      </Body>
      <div style={{ padding:'12px 20px 30px', flex:'none', display:'flex', gap:12, borderTop:`1px solid ${DRIFT.line}` }}>
        <GhostBtn>Not now</GhostBtn>
        <PrimaryBtn full onClick={onNext}>Schedule it</PrimaryBtn>
      </div>
    </Screen>
  );
}

// ── SCHEDULED — confirmation / back on baseline ───────────────
function Scheduled({ onRestart }) {
  return (
    <Screen>
      <Body style={{ display:'flex', flexDirection:'column', justifyContent:'center', alignItems:'center', textAlign:'center', padding:'0 28px' }}>
        <div style={{ position:'relative', marginBottom:30 }}>
          <Mark w={86} sw={5}/>
        </div>
        <Eyebrow color={DRIFT.signal}>Correction scheduled</Eyebrow>
        <h1 style={{ margin:'14px 0 8px', fontSize:28, fontWeight:600, letterSpacing:'-1.4px', lineHeight:1.12 }}>Tomorrow, 7:00am<br/>Easy 20 minutes</h1>
        <p style={{ margin:0, color:DRIFT.muted, fontSize:15, maxWidth:'30ch' }}>That's the whole job. One small action keeps you on baseline — the drift never compounds.</p>
        <div style={{ display:'flex', gap:10, marginTop:24 }}>
          <BandPill level="hold"/>
          <span style={{ fontFamily:DRIFT.mono, fontSize:11.5, color:DRIFT.muted, alignSelf:'center' }}>signal recovering</span>
        </div>
        <div style={{ width:'100%', marginTop:30 }}><MiniChart h={74} drift={false}/></div>
      </Body>
      <div style={{ padding:'12px 20px 30px', flex:'none', display:'flex', gap:12 }}>
        <GhostBtn full onClick={onRestart}>Back to start</GhostBtn>
        <PrimaryBtn full>Open dashboard</PrimaryBtn>
      </div>
    </Screen>
  );
}

Object.assign(window, { OnboardingConnect, HomeA, HomeB, AskA, AskB, DriftCatch, Scheduled });
