// screens-a.jsx — Home, Word Card, Minimal-pair drill
// Each screen takes (theme T, t tweaks). Locally interactive.

// ════════════════════════════════════════════════════════════════════════
// 1 · HOME / DAILY SESSION
// ════════════════════════════════════════════════════════════════════════
function HomeScreen({ T, t }) {
  const head = ppHeadFont(t);
  const label = { fontFamily: PP_UI, fontSize: 12, fontWeight: 600, letterSpacing: 1.4, textTransform: 'uppercase', color: T.faint };
  return (
    <Screen theme={T}>
      {/* header */}
      <div style={{ paddingTop: 70 }}>
        <div style={{ fontFamily: PP_UI, fontSize: 15, fontWeight: 500, color: T.sub }}>Wednesday, 10 June</div>
        <div style={{ fontFamily: head, fontSize: 34, fontWeight: 500, lineHeight: 1.12, marginTop: 6, letterSpacing: -0.2 }}>
          Labrīt, Gabrial.
        </div>
        <div style={{ fontFamily: PP_UI, fontSize: 15.5, color: T.sub, marginTop: 7, lineHeight: 1.4 }}>
          A few new words, and the ones to review.
        </div>
        {t.folk && <div style={{ marginTop: 16 }}><FolkSeam color={T.primary} w={120} h={12} opacity={T.dark ? 0.55 : 0.6} /></div>}
      </div>

      {/* hero session card */}
      <div style={{
        marginTop: 24, background: T.surface, borderRadius: 28, padding: '24px 22px 22px',
        boxShadow: T.shadowCard, border: `0.5px solid ${T.hair}`,
      }}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <span style={label}>Today&rsquo;s session</span>
          <span style={{ fontFamily: PP_UI, fontSize: 13, fontWeight: 600, color: T.primary }}>~10 min</span>
        </div>

        <div style={{ display: 'flex', alignItems: 'baseline', gap: 10, marginTop: 16 }}>
          <span style={{ fontFamily: head, fontSize: 56, fontWeight: 500, lineHeight: 0.9, letterSpacing: -1 }}>12</span>
          <span style={{ fontFamily: PP_UI, fontSize: 16, fontWeight: 500, color: T.sub }}>words</span>
        </div>
        <div style={{ display: 'flex', gap: 16, marginTop: 10, fontFamily: PP_UI, fontSize: 14, color: T.sub }}>
          <span><b style={{ color: T.ink, fontWeight: 600 }}>5</b> new</span>
          <span style={{ color: T.faint }}>·</span>
          <span><b style={{ color: T.ink, fontWeight: 600 }}>7</b> to review</span>
        </div>

        <div style={{ margin: '20px 0 22px' }}>
          <Waveform seed="todaysession" played={0.0} height={50} count={48} theme={T} style={t.wave || 'bars'} />
        </div>

        <button style={{
          width: '100%', height: 56, borderRadius: 18, border: 'none', cursor: 'pointer',
          background: T.primary, color: T.onPrimary, fontFamily: PP_UI, fontSize: 17, fontWeight: 600,
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10,
          boxShadow: `0 8px 20px ${hexA(T.primary, T.dark ? 0.36 : 0.24)}`,
        }}>
          <svg width="18" height="18" viewBox="0 0 24 24" fill={T.onPrimary}><path d="M6 4.5v15a1 1 0 0 0 1.5.87l12-7.5a1 1 0 0 0 0-1.74l-12-7.5A1 1 0 0 0 6 4.5z" /></svg>
          Begin listening
        </button>
      </div>

      {/* spacer pushes secondary + coverage down */}
      <div style={{ flex: 1, minHeight: 14 }} />

      {/* slim secondary: podcast */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 14, background: T.surface, borderRadius: 20,
        padding: '14px 16px', boxShadow: T.shadow, border: `0.5px solid ${T.hair}`,
      }}>
        <div style={{ width: 44, height: 44, borderRadius: 13, background: T.primarySoft, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
          <Icon name="listen" size={22} color={T.primary} />
        </div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontFamily: PP_UI, fontSize: 15.5, fontWeight: 600 }}>Rīta saruna</div>
          <div style={{ fontFamily: PP_UI, fontSize: 13, color: T.sub, marginTop: 1 }}>3 min · only words you know</div>
        </div>
        <Icon name="chevR" size={18} color={T.faint} />
      </div>

      {/* coverage footnote */}
      <div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '16px 2px 0', marginBottom: 96 }}>
        <div style={{ flex: 1, height: 4, borderRadius: 99, background: T.dark ? 'rgba(255,255,255,0.08)' : 'rgba(26,39,51,0.07)', overflow: 'hidden' }}>
          <div style={{ width: '62%', height: '100%', background: T.primary, borderRadius: 99 }} />
        </div>
        <span style={{ fontFamily: PP_UI, fontSize: 12.5, color: T.faint, fontWeight: 500 }}>615 / 1000 words</span>
      </div>

      <TabBar theme={T} active="today" />
    </Screen>
  );
}

// ════════════════════════════════════════════════════════════════════════
// 2 · AUDIO-FIRST WORD CARD  (hear → meaning → spelling → phrase)
// ════════════════════════════════════════════════════════════════════════
function WordCardScreen({ T, t }) {
  const head = ppHeadFont(t);
  const [stage, setStage] = React.useState(0); // 0 listen+choose, 1 answered, 2 spelling, 3 phrase
  const [choice, setChoice] = React.useState(null); // picked meaning
  const [playing, setPlaying] = React.useState(false);
  const [speed, setSpeed] = React.useState(1);
  const [phraseSpeed, setPhraseSpeed] = React.useState(1);
  const next = () => setStage(s => Math.min(3, s + 1));
  const correct = 'good morning';
  const options = ['thank you', 'good morning', 'see you later', 'excuse me'];
  const right = choice === correct;

  const ctaLabel = [null, 'Reveal spelling', 'Hear it in a phrase', 'Got it'][stage];

  return (
    <Screen theme={T}>
      <SessionTop theme={T} step={3} total={12} />

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', paddingBottom: 8 }}>
        <div style={{ fontFamily: PP_UI, fontSize: 12, fontWeight: 600, letterSpacing: 1.4, textTransform: 'uppercase', color: T.faint, marginBottom: 4 }}>
          {stage === 0 ? 'Listen — what does it mean?' : 'New word'}
        </div>

        {/* spelling — hidden until stage 2 */}
        <div style={{ height: 70, display: 'flex', alignItems: 'center', justifyContent: 'center', marginTop: 4 }}>
          {stage >= 2 ? (
            <span style={{ fontFamily: head, fontSize: 58, fontWeight: 500, letterSpacing: -1, lineHeight: 1 }}>labrīt</span>
          ) : (
            <div style={{ display: 'flex', gap: 11 }}>
              {[0,1,2,3,4,5].map(i => <div key={i} style={{ width: 13, height: 13, borderRadius: '50%', background: T.dark ? 'rgba(255,255,255,0.13)' : 'rgba(26,39,51,0.12)' }} />)}
            </div>
          )}
        </div>

        {/* meaning — confirmed after answering */}
        <div style={{ height: 28, marginTop: 12 }}>
          {stage >= 1 && (
            <span style={{ fontFamily: PP_UI, fontSize: 19, fontWeight: 500, color: T.sub }}>good morning</span>
          )}
        </div>

        {/* waveform hero + play */}
        <div style={{ width: '100%', marginTop: stage === 0 ? 18 : 30, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: stage === 0 ? 14 : 22, transition: 'margin .3s' }}>
          <div style={{ width: '78%' }}>
            <Waveform seed="labrit" played={playing ? 0.66 : 0} height={stage === 0 ? 48 : 64} count={42} theme={T} style={t.wave || 'bars'} />
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12 }}>
            <PlayOrb theme={T} size={stage === 0 ? 64 : 80} playing={playing} onClick={() => setPlaying(p => !p)} />
            <SpeedChip theme={T} size={stage === 0 ? 'sm' : 'md'} value={speed} onChange={setSpeed} />
          </div>
          {stage > 0 && <span style={{ fontFamily: PP_UI, fontSize: 13, color: T.faint, fontWeight: 500 }}>Tap to replay · 0:01</span>}
        </div>

        {/* meaning choices — the recall check that feeds scheduling */}
        {stage === 0 && (
          <div style={{ width: '100%', marginTop: 24, display: 'flex', flexDirection: 'column', gap: 10 }}>
            {options.map(opt => {
              const isPicked = choice === opt;
              const isCorrect = opt === correct;
              let bg = T.surface, bd = T.hair, fg = T.ink;
              if (choice) {
                if (isCorrect) { bg = T.goodSoft; bd = hexA(T.good, 0.5); fg = T.good; }
                else if (isPicked) { bg = T.dark ? 'rgba(224,116,138,0.12)' : 'rgba(158,43,58,0.07)'; bd = hexA('#C0485A', 0.45); fg = '#C0485A'; }
                else { fg = T.faint; }
              }
              return (
                <button key={opt} disabled={!!choice} onClick={() => setChoice(opt)} style={{
                  width: '100%', minHeight: 50, borderRadius: 16, cursor: choice ? 'default' : 'pointer',
                  background: bg, border: `1.5px solid ${bd}`, color: fg,
                  fontFamily: PP_UI, fontSize: 16, fontWeight: 600,
                  display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 18px',
                  boxShadow: choice ? 'none' : T.shadow,
                }}>
                  {opt}
                  {choice && isCorrect && <Icon name="check" size={18} color={T.good} sw={2.4} />}
                </button>
              );
            })}
          </div>
        )}

        {/* phrase card — appears at stage 3 */}
        <div style={{
          width: '100%', marginTop: 26, overflow: 'hidden',
          maxHeight: stage >= 3 ? 200 : 0, opacity: stage >= 3 ? 1 : 0,
          transition: 'max-height .35s ease, opacity .3s ease',
        }}>
          <div style={{ background: T.surface, borderRadius: 22, padding: '18px 18px', boxShadow: T.shadow, border: `0.5px solid ${T.hair}` }}>
            <div style={{ fontFamily: PP_UI, fontSize: 11.5, fontWeight: 600, letterSpacing: 1.2, textTransform: 'uppercase', color: T.faint, marginBottom: 12 }}>In a phrase</div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
              <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 7 }}>
                <PlayOrb theme={T} size={46} filled={false} />
                <SpeedChip theme={T} size="sm" value={phraseSpeed} onChange={setPhraseSpeed} />
              </div>
              <div style={{ flex: 1 }}>
                <div style={{ fontFamily: head, fontSize: 21, fontWeight: 500, letterSpacing: -0.2 }}><b style={{ color: T.primary, fontWeight: 500 }}>Labrīt!</b> Kā tev klājas?</div>
                <div style={{ fontFamily: PP_UI, fontSize: 13.5, color: T.sub, marginTop: 3 }}>Good morning! How are you?</div>
              </div>
            </div>
          </div>
        </div>
      </div>

      {/* CTA / scheduling feedback */}
      <div style={{ paddingBottom: 30 }}>
        {stage === 0 ? (
          choice ? (
            <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8, fontFamily: PP_UI, fontSize: 13.5, fontWeight: 500, color: right ? T.good : T.sub }}>
                {right ? <>Recalled — next review in <b style={{ fontWeight: 700 }}>4 days</b>.</> : <>No problem — we&rsquo;ll bring it back <b style={{ fontWeight: 700, color: T.ink }}>sooner</b>.</>}
              </div>
              <button onClick={() => setStage(2)} style={{
                width: '100%', height: 56, borderRadius: 18, border: 'none', cursor: 'pointer',
                background: T.primary, color: T.onPrimary, fontFamily: PP_UI, fontSize: 17, fontWeight: 600,
                display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9,
                boxShadow: `0 8px 20px ${hexA(T.primary, 0.26)}`,
              }}>Reveal spelling</button>
            </div>
          ) : (
            <div style={{ textAlign: 'center', fontFamily: PP_UI, fontSize: 13.5, color: T.faint, fontWeight: 500, padding: '18px 0' }}>
              Your answer tunes when this word returns.
            </div>
          )
        ) : (
          <button onClick={next} style={{
            width: '100%', height: 56, borderRadius: 18, border: 'none', cursor: 'pointer',
            background: stage === 3 ? T.good : T.primary, color: '#fff', fontFamily: PP_UI, fontSize: 17, fontWeight: 600,
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9,
            boxShadow: `0 8px 20px ${hexA(stage === 3 ? T.good : T.primary, 0.26)}`, transition: 'background .2s',
          }}>
            {stage === 3 && <Icon name="check" size={19} color="#fff" />}
            {ctaLabel}
          </button>
        )}
      </div>
    </Screen>
  );
}

// ════════════════════════════════════════════════════════════════════════
// 3 · MINIMAL-PAIR PERCEPTION DRILL  (L vs Ļ)
// ════════════════════════════════════════════════════════════════════════
function DrillScreen({ T, t }) {
  const head = ppHeadFont(t);
  const [picked, setPicked] = React.useState(null); // 'L' | 'Ļ'
  const [playing, setPlaying] = React.useState(false);
  const [speed, setSpeed] = React.useState(1);
  const [say, setSay] = React.useState(null); // null | 'idle' | 'rec' | 'done' — saying it back closes the loop
  const correct = 'Ļ';
  const pairs = [
    { key: 'L', glyph: 'L', word: 'lācis', gloss: 'bear', hint: 'as in “love”' },
    { key: 'Ļ', glyph: 'Ļ', word: 'ļoti', gloss: 'very', hint: 'soft, “ly”' },
  ];

  const card = (p) => {
    const isPicked = picked === p.key;
    const isCorrect = p.key === correct;
    let bg = T.surface, bd = T.hair, fg = T.ink, accent = false;
    if (picked) {
      if (isCorrect) { bg = T.goodSoft; bd = hexA(T.good, 0.5); fg = T.good; accent = true; }
      else if (isPicked) { bg = T.dark ? 'rgba(224,116,138,0.12)' : 'rgba(158,43,58,0.07)'; bd = hexA('#C0485A', 0.45); }
    }
    return (
      <button key={p.key} onClick={() => !picked && setPicked(p.key)} disabled={!!picked} style={{
        flex: 1, background: bg, border: `1.5px solid ${bd}`, borderRadius: 24, cursor: picked ? 'default' : 'pointer',
        padding: '26px 14px 20px', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6,
        boxShadow: picked ? 'none' : T.shadow, transition: 'background .2s, border-color .2s, box-shadow .2s', position: 'relative',
      }}>
        {picked && isCorrect && (
          <div style={{ position: 'absolute', top: 12, right: 12, width: 26, height: 26, borderRadius: '50%', background: T.good, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <Icon name="check" size={16} color="#fff" sw={2.4} />
          </div>
        )}
        <span style={{ fontFamily: head, fontSize: 64, fontWeight: 500, lineHeight: 0.95, color: accent ? T.good : fg }}>{p.glyph}</span>
        {picked ? (
          <>
            <span style={{ fontFamily: head, fontSize: 20, fontWeight: 500, color: T.ink, marginTop: 4 }}>{p.word}</span>
            <span style={{ fontFamily: PP_UI, fontSize: 13, color: T.sub }}>{p.gloss}</span>
          </>
        ) : (
          <span style={{ fontFamily: PP_UI, fontSize: 13, color: T.faint, marginTop: 2 }}>{p.hint}</span>
        )}
      </button>
    );
  };

  return (
    <Screen theme={T}>
      <SessionTop theme={T} step={5} total={8} />

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
        <div style={{ textAlign: 'center', marginBottom: 4 }}>
          <div style={{ fontFamily: PP_UI, fontSize: 12, fontWeight: 600, letterSpacing: 1.4, textTransform: 'uppercase', color: T.faint }}>Sound check</div>
          <div style={{ fontFamily: head, fontSize: 27, fontWeight: 500, marginTop: 8, letterSpacing: -0.3 }}>{say ? 'Say it back' : 'Which did you hear?'}</div>
        </div>

        {say ? (
          /* audio out — say the sound you just picked */
          <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', marginTop: 28 }}>
            <span style={{ fontFamily: head, fontSize: 56, fontWeight: 500, lineHeight: 1 }}>ļoti</span>
            <span style={{ fontFamily: PP_UI, fontSize: 14.5, color: T.sub, marginTop: 10 }}>very &middot; <span style={{ color: T.faint }}>LYO-tee</span></span>
            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 11, marginTop: 24 }}>
              <PlayOrb theme={T} size={54} filled={false} playing={playing} onClick={() => setPlaying(p => !p)} />
              <SpeedChip theme={T} size="sm" value={speed} onChange={setSpeed} />
            </div>
            <div style={{ height: 110, marginTop: 20, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'flex-start' }}>
              {say === 'done' ? (
                <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginTop: 16 }}>
                  <div style={{ width: 22, height: 22, borderRadius: '50%', background: T.goodSoft, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                    <Icon name="check" size={13} color={T.good} sw={2.4} />
                  </div>
                  <span style={{ fontFamily: PP_UI, fontSize: 14, fontWeight: 500, color: T.sub }}>Sounded right &mdash; a nice soft <b style={{ fontWeight: 700, color: T.ink }}>Ļ</b>.</span>
                </div>
              ) : (
                <>
                  <MicOrb theme={T} size={72} rec={say === 'rec'} onClick={() => setSay(say === 'rec' ? 'done' : 'rec')} />
                  <div style={{ height: 18, fontFamily: PP_UI, fontSize: 13, color: say === 'rec' ? '#C0485A' : T.faint, marginTop: 12, fontWeight: say === 'rec' ? 600 : 400 }}>
                    {say === 'rec' ? 'Listening… tap to stop' : 'Now say it'}
                  </div>
                </>
              )}
            </div>
          </div>
        ) : (<>
        {/* the played sound */}
        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 18, margin: '30px 0 34px' }}>
          <div style={{ width: '64%' }}>
            <Waveform seed={picked || 'lje'} played={playing ? 0.7 : 0} height={52} count={34} theme={T} style={t.wave || 'bars'} />
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12 }}>
            <PlayOrb theme={T} size={72} playing={playing} onClick={() => setPlaying(p => !p)} />
            <SpeedChip theme={T} size="sm" value={speed} onChange={setSpeed} />
          </div>
        </div>

        {/* the two options */}
        <div style={{ display: 'flex', gap: 14 }}>
          {pairs.map(card)}
        </div>

        {/* feedback line */}
        <div style={{ height: 46, marginTop: 18, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
          {picked && (
            <div style={{ display: 'flex', alignItems: 'center', gap: 8, fontFamily: PP_UI, fontSize: 14.5, fontWeight: 500, color: picked === correct ? T.good : T.sub }}>
              {picked === correct
                ? <>Right — that&rsquo;s the soft <b style={{ fontWeight: 700 }}>Ļ</b>.</>
                : <>Listen again — it was the soft <b style={{ fontWeight: 700, color: T.ink }}>Ļ</b>.</>}
            </div>
          )}
        </div>
        </>)}
      </div>

      {/* bottom action */}
      <div style={{ paddingBottom: 30 }}>
        {say === 'done' ? (
          <button onClick={() => { setPicked(null); setSay(null); setPlaying(false); }} style={{
            width: '100%', height: 54, borderRadius: 18, border: 'none', cursor: 'pointer',
            background: T.primary, color: T.onPrimary, fontFamily: PP_UI, fontSize: 16.5, fontWeight: 600,
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9,
          }}>Next pair <Icon name="chevR" size={17} color={T.onPrimary} /></button>
        ) : say ? (
          <div style={{ textAlign: 'center', fontFamily: PP_UI, fontSize: 13.5, color: T.faint, fontWeight: 500, padding: '17px 0' }}>
            Saying it locks the sound in.
          </div>
        ) : picked ? (
          <button onClick={() => { setSay('idle'); setPlaying(false); }} style={{
            width: '100%', height: 54, borderRadius: 18, border: 'none', cursor: 'pointer',
            background: T.primary, color: T.onPrimary, fontFamily: PP_UI, fontSize: 16.5, fontWeight: 600,
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9,
          }}><Icon name="mic" size={18} color={T.onPrimary} /> Say it back</button>
        ) : (
          <button onClick={() => setPlaying(true)} style={{
            width: '100%', height: 54, borderRadius: 18, cursor: 'pointer',
            background: 'transparent', border: `1.5px solid ${T.hair}`, color: T.sub, fontFamily: PP_UI, fontSize: 16, fontWeight: 600,
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9,
          }}><Icon name="replay" size={18} color={T.sub} /> Play again</button>
        )}
      </div>
    </Screen>
  );
}

// ════════════════════════════════════════════════════════════════════════
// 2b · INVERSE WORD CARD — meaning out → audio out: pick the word, then say it
// ════════════════════════════════════════════════════════════════════════
function WordSayScreen({ T, t }) {
  const head = ppHeadFont(t);
  // inverse of the recognition card: the meaning is the cue —
  // meaning out (pick the word) → audio out (say it) → compare
  const [stage, setStage] = React.useState('choose'); // choose → speak → rec → result
  const [choice, setChoice] = React.useState(null);   // picked Latvian word
  const [nativePlay, setNativePlay] = React.useState(false);
  const [playWho, setPlayWho] = React.useState(null); // 'native' | 'you'
  const [speed, setSpeed] = React.useState(1);

  const correct = 'labrīt';
  const options = [
    { lv: 'paldies', en: 'thank you' },
    { lv: 'labrīt', en: 'good morning' },
    { lv: 'uz redzēšanos', en: 'see you later' },
    { lv: 'atvainojiet', en: 'excuse me' },
  ];
  const right = choice === correct;
  const pickedEn = choice ? options.find(o => o.lv === choice).en : '';

  const playOne = (who, ms) => { setPlayWho(who); setTimeout(() => setPlayWho(w => (w === who ? null : w)), ms); };
  const playBoth = () => {
    const nativeMs = 1100 / speed; // slowed native model takes longer
    setPlayWho('native');
    setTimeout(() => setPlayWho('you'), nativeMs);
    setTimeout(() => setPlayWho(null), nativeMs + 1100);
  };

  const CompareRow = ({ icon, label, who, seed }) => (
    <div onClick={() => playOne(who, who === 'native' ? 1100 / speed : 1100)} style={{ display: 'flex', alignItems: 'center', gap: 14, background: T.surface, borderRadius: 18, padding: '13px 18px', border: `0.5px solid ${playWho === who ? hexA(T.primary, 0.4) : T.hair}`, boxShadow: T.shadow, cursor: 'pointer' }}>
      <Icon name={icon} size={18} color={playWho === who ? T.primary : T.faint} />
      <span style={{ fontFamily: PP_UI, fontSize: 13, fontWeight: 600, color: playWho === who ? T.ink : T.sub, width: 48 }}>{label}</span>
      <div style={{ flex: 1 }}>
        <Waveform seed={seed} played={playWho === who ? 0.7 : 0} height={26} count={32} theme={T} style={t.wave || 'bars'} />
      </div>
    </div>
  );

  return (
    <Screen theme={T}>
      <SessionTop theme={T} step={9} total={12} />

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', paddingBottom: 8 }}>
        <div style={{ fontFamily: PP_UI, fontSize: 12, fontWeight: 600, letterSpacing: 1.4, textTransform: 'uppercase', color: T.faint }}>Review &middot; say it</div>

        {stage === 'choose' && (
          <>
            {/* the meaning is the cue — inverse of the recognition card */}
            <div style={{ fontFamily: PP_UI, fontSize: 27, fontWeight: 500, color: T.ink, marginTop: 22, letterSpacing: -0.3 }}>good morning</div>
            <div style={{ fontFamily: PP_UI, fontSize: 14.5, color: T.sub, marginTop: 10 }}>Which word says it?</div>

            <div style={{ width: '100%', marginTop: 26, display: 'flex', flexDirection: 'column', gap: 10 }}>
              {options.map(o => {
                const isPicked = choice === o.lv;
                const isCorrect = o.lv === correct;
                let bg = T.surface, bd = T.hair, fg = T.ink;
                if (choice) {
                  if (isCorrect) { bg = T.goodSoft; bd = hexA(T.good, 0.5); fg = T.good; }
                  else if (isPicked) { bg = T.dark ? 'rgba(224,116,138,0.12)' : 'rgba(158,43,58,0.07)'; bd = hexA('#C0485A', 0.45); fg = '#C0485A'; }
                  else { fg = T.faint; }
                }
                return (
                  <button key={o.lv} disabled={!!choice} onClick={() => setChoice(o.lv)} style={{
                    width: '100%', minHeight: 52, borderRadius: 16, cursor: choice ? 'default' : 'pointer',
                    background: bg, border: `1.5px solid ${bd}`, color: fg,
                    fontFamily: head, fontSize: 19, fontWeight: 500,
                    display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 18px',
                    boxShadow: choice ? 'none' : T.shadow,
                  }}>
                    {o.lv}
                    {choice && isCorrect && <Icon name="check" size={18} color={T.good} sw={2.4} />}
                  </button>
                );
              })}
            </div>

            <div style={{ height: 22, marginTop: 16, fontFamily: PP_UI, fontSize: 13.5, fontWeight: 500, color: right ? T.good : T.sub, textAlign: 'center' }}>
              {choice && (right
                ? <>That&rsquo;s it &mdash; now say it out loud.</>
                : <>It&rsquo;s <b style={{ fontFamily: head, fontWeight: 600, color: T.ink }}>labrīt</b> &mdash; <b style={{ fontFamily: head, fontWeight: 600 }}>{choice}</b> means &ldquo;{pickedEn}&rdquo;.</>)}
            </div>
          </>
        )}

        {(stage === 'speak' || stage === 'rec') && (
          <>
            {/* cue stays quiet; the recalled word becomes the hero */}
            <div style={{ fontFamily: PP_UI, fontSize: 15, color: T.sub, marginTop: 20 }}>good morning</div>
            <div style={{ marginTop: 12 }}>
              <span style={{ fontFamily: head, fontSize: 52, fontWeight: 500, letterSpacing: -0.8, lineHeight: 1 }}>labrīt</span>
            </div>
            <div style={{ fontFamily: PP_UI, fontSize: 13.5, color: T.faint, marginTop: 10 }}>LAH-breet</div>

            {/* native model — hear it again before speaking */}
            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 11, marginTop: 28 }}>
              <PlayOrb theme={T} size={58} filled={false} playing={nativePlay} onClick={() => setNativePlay(p => !p)} />
              <SpeedChip theme={T} size="sm" value={speed} onChange={setSpeed} />
            </div>

            {/* audio out — say it back */}
            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', marginTop: 26 }}>
              <MicOrb theme={T} rec={stage === 'rec'} onClick={() => setStage(stage === 'rec' ? 'result' : 'rec')} />
              <div style={{ height: 18, fontFamily: PP_UI, fontSize: 13, color: stage === 'rec' ? '#C0485A' : T.faint, marginTop: 12, fontWeight: stage === 'rec' ? 600 : 400 }}>
                {stage === 'rec' ? 'Listening… tap to stop' : 'Now say it'}
              </div>
            </div>
          </>
        )}

        {stage === 'result' && (
          <>
            {/* revealed word */}
            <div style={{ marginTop: 22 }}>
              <span style={{ fontFamily: head, fontSize: 52, fontWeight: 500, letterSpacing: -0.8, lineHeight: 1 }}>labrīt</span>
            </div>
            <div style={{ fontFamily: PP_UI, fontSize: 16, color: T.sub, marginTop: 10 }}>good morning &middot; <span style={{ color: T.faint }}>LAH-breet</span></div>

            {/* compare: native model vs your attempt */}
            <div style={{ width: '100%', marginTop: 26, display: 'flex', flexDirection: 'column', gap: 10 }}>
              <CompareRow icon="speaker" label="Native" who="native" seed="labrit-native" />
              <CompareRow icon="mic" label="You" who="you" seed="labrit-you" />
            </div>
            <button onClick={playBoth} style={{
              display: 'flex', alignItems: 'center', gap: 8, padding: '11px 20px', borderRadius: 99, cursor: 'pointer', marginTop: 16,
              background: 'transparent', border: `1.5px solid ${hexA(T.primary, 0.45)}`, color: T.primary,
              fontFamily: PP_UI, fontSize: 14, fontWeight: 600,
            }}>
              <svg width="14" height="14" viewBox="0 0 24 24" fill={T.primary}><path d="M6 4.5v15a1 1 0 0 0 1.5.87l12-7.5a1 1 0 0 0 0-1.74l-12-7.5A1 1 0 0 0 6 4.5z" /></svg>
              Play back-to-back
            </button>
            <div style={{ display: 'flex', justifyContent: 'center', marginTop: 12 }}>
              <SpeedChip theme={T} size="sm" value={speed} onChange={setSpeed} />
            </div>

            {/* the pick and the speaking both feed scheduling */}
            <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginTop: 18 }}>
              <div style={{ width: 22, height: 22, borderRadius: '50%', background: T.goodSoft, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                <Icon name="check" size={13} color={T.good} sw={2.4} />
              </div>
              <span style={{ fontFamily: PP_UI, fontSize: 14, fontWeight: 500, color: T.sub }}>
                {right
                  ? <>Sounded right &mdash; next review in <b style={{ color: T.ink, fontWeight: 700 }}>6 days</b>.</>
                  : <>Sounded right &mdash; the missed pick brings it back in <b style={{ color: T.ink, fontWeight: 700 }}>2 days</b>.</>}
              </span>
            </div>
          </>
        )}
      </div>

      {/* bottom */}
      <div style={{ paddingBottom: 30 }}>
        {stage === 'choose' ? (
          choice ? (
            <button onClick={() => setStage('speak')} style={{
              width: '100%', height: 56, borderRadius: 18, border: 'none', cursor: 'pointer',
              background: T.primary, color: T.onPrimary, fontFamily: PP_UI, fontSize: 17, fontWeight: 600,
              display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9,
              boxShadow: `0 8px 20px ${hexA(T.primary, 0.26)}`,
            }}><Icon name="mic" size={19} color={T.onPrimary} /> Now say it</button>
          ) : (
            <div style={{ textAlign: 'center', fontFamily: PP_UI, fontSize: 13.5, color: T.faint, fontWeight: 500, padding: '18px 0' }}>
              Recalling the word from its meaning is the strongest review.
            </div>
          )
        ) : stage === 'result' ? (
          <button style={{
            width: '100%', height: 56, borderRadius: 18, border: 'none', cursor: 'pointer',
            background: T.good, color: '#fff', fontFamily: PP_UI, fontSize: 17, fontWeight: 600,
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 9,
            boxShadow: `0 8px 20px ${hexA(T.good, 0.26)}`,
          }}>
            <Icon name="check" size={19} color="#fff" />
            Continue
          </button>
        ) : (
          <div style={{ textAlign: 'center', fontFamily: PP_UI, fontSize: 13.5, color: T.faint, fontWeight: 500, padding: '18px 0' }}>
            Speaking it closes the loop.
          </div>
        )}
      </div>
    </Screen>
  );
}

// router for the two word-card directions
function WordFlowScreen({ T, t, state = 'hear' }) {
  if (state === 'pic-review') return <WordPicReviewScreen T={T} t={t} />;
  if (state && state.indexOf('learn-') === 0) return <WordLearnScreen T={T} t={t} kind={state.slice(6)} />;
  if (state === 'say') return <WordSayScreen T={T} t={t} />;
  return <WordCardScreen T={T} t={t} />;
}

Object.assign(window, { HomeScreen, WordCardScreen, WordSayScreen, WordFlowScreen, DrillScreen });
