// MiniPhone screens — real photo wallpaper, full iOS status bar, lock/home with dock.

const APP_BG = 'linear-gradient(135deg, #1A75E0 0%, #0858C0 100%)';
const WALLPAPER = 'assets/wallpaper.jpg';

// ─── iOS status bar ─────────────────────────────────────────────────
function StatusBar({ tint = '#fff' }) {
  const [now, setNow] = React.useState(new Date());
  React.useEffect(() => {
    const id = setInterval(() => setNow(new Date()), 30000);
    return () => clearInterval(id);
  }, []);
  const h = now.getHours().toString().padStart(2, '0');
  const m = now.getMinutes().toString().padStart(2, '0');

  return (
    <div style={{
      position: 'absolute', top: 0, left: 0, right: 0,
      height: 44, zIndex: 10, pointerEvents: 'none',
      display: 'flex', alignItems: 'center',
      justifyContent: 'space-between',
      padding: '0 22px',
      color: tint,
      fontFamily: '-apple-system, "SF Pro Text", system-ui',
    }}>
      {/* Time */}
      <div style={{
        fontSize: 14, fontWeight: 600, letterSpacing: 0.2,
        fontFeatureSettings: '"tnum"',
      }}>{h}:{m}</div>

      {/* Dynamic Island */}
      <div style={{
        position: 'absolute', left: '50%', top: 9,
        transform: 'translateX(-50%)',
        width: 92, height: 27, borderRadius: 16,
        background: '#000',
      }}/>

      {/* Right: signal + wifi + battery */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 5,
      }}>
        {/* signal bars */}
        <svg width="17" height="11" viewBox="0 0 17 11" fill={tint}>
          <rect x="0"  y="7" width="3" height="4" rx="0.6"/>
          <rect x="4"  y="5" width="3" height="6" rx="0.6"/>
          <rect x="8"  y="3" width="3" height="8" rx="0.6"/>
          <rect x="12" y="0" width="3" height="11" rx="0.6"/>
        </svg>
        {/* wifi */}
        <svg width="15" height="11" viewBox="0 0 15 11" fill={tint}>
          <path d="M7.5 0a11 11 0 017.2 2.7l-1 1.1A9.5 9.5 0 007.5 1.5 9.5 9.5 0 001.3 3.8l-1-1.1A11 11 0 017.5 0zm0 3a8 8 0 015.2 1.9l-1 1.1A6.5 6.5 0 007.5 4.5 6.5 6.5 0 003.3 6l-1-1.1A8 8 0 017.5 3zm0 3a5 5 0 013.2 1.2l-1 1.1A3.5 3.5 0 007.5 7.5a3.5 3.5 0 00-2.2.8l-1-1.1A5 5 0 017.5 6zm0 3a2 2 0 011.4.6L7.5 11 6.1 9.6A2 2 0 017.5 9z"/>
        </svg>
        {/* battery */}
        <svg width="25" height="11" viewBox="0 0 25 11" fill="none">
          <rect x="0.5" y="0.5" width="21" height="10" rx="2.4" stroke={tint} strokeOpacity="0.55"/>
          <rect x="2"   y="2"   width="18" height="7"  rx="1.4" fill={tint}/>
          <rect x="22.5" y="3.5" width="1.5" height="4" rx="0.6" fill={tint} fillOpacity="0.55"/>
        </svg>
      </div>
    </div>
  );
}

function HomeIndicator({ tint = '#fff' }) {
  return (
    <div style={{
      position: 'absolute', bottom: 7, left: 0, right: 0,
      display: 'flex', justifyContent: 'center', zIndex: 10,
      pointerEvents: 'none',
    }}>
      <div style={{
        width: 134, height: 5, borderRadius: 3,
        background: tint, opacity: 0.95,
      }}/>
    </div>
  );
}

function Wallpaper() {
  return (
    <>
      <img src={WALLPAPER} alt="" style={{
        position: 'absolute', inset: 0, width: '100%', height: '100%',
        objectFit: 'cover', userSelect: 'none', pointerEvents: 'none',
      }}/>
      {/* darken so white text reads cleanly on any wallpaper */}
      <div style={{
        position: 'absolute', inset: 0,
        background: 'linear-gradient(180deg, rgba(0,0,0,0.45) 0%, rgba(0,0,0,0.18) 35%, rgba(0,0,0,0.25) 70%, rgba(0,0,0,0.55) 100%)',
        pointerEvents: 'none',
      }}/>
    </>
  );
}

// ─── Lock screen ─────────────────────────────────────────────────────
function LockScreen({ onUnlock }) {
  const [now, setNow] = React.useState(new Date());
  React.useEffect(() => {
    const id = setInterval(() => setNow(new Date()), 30000);
    return () => clearInterval(id);
  }, []);
  const h = now.getHours().toString().padStart(2, '0');
  const m = now.getMinutes().toString().padStart(2, '0');
  const weekdays = ['周日','周一','周二','周三','周四','周五','周六'];
  const dateStr = `${weekdays[now.getDay()]}, ${now.getMonth()+1}月${now.getDate()}日`;

  return (
    <div onClick={onUnlock} style={{
      position: 'absolute', inset: 0, cursor: 'pointer', color: '#fff',
      display: 'flex', flexDirection: 'column', alignItems: 'center',
    }}>
      <Wallpaper/>

      {/* Lock icon */}
      <div style={{ marginTop: 52, position: 'relative', zIndex: 2 }}>
        <svg width="14" height="18" viewBox="0 0 18 22" fill="none">
          <rect x="2.5" y="9" width="13" height="11" rx="2.5" fill="#fff" fillOpacity="0.95"/>
          <path d="M5.5 9V6a3.5 3.5 0 017 0v3" stroke="#fff" strokeOpacity="0.95" strokeWidth="1.8" fill="none"/>
        </svg>
      </div>

      {/* Date + time */}
      <div style={{
        marginTop: 6, textAlign: 'center', position: 'relative', zIndex: 2,
        textShadow: '0 1px 3px rgba(0,0,0,0.2)',
      }}>
        <div style={{
          fontFamily: '-apple-system, "SF Pro Display", system-ui',
          fontSize: 15, fontWeight: 500, letterSpacing: 0.2,
        }}>{dateStr}</div>
        <div style={{
          fontFamily: '-apple-system, "SF Pro Display", system-ui',
          fontSize: 84, fontWeight: 200, letterSpacing: -3,
          lineHeight: 1, marginTop: 4,
        }}>{h}:{m}</div>
      </div>

      <div style={{ flex: 1 }} />

      <div style={{
        fontSize: 11, color: 'rgba(255,255,255,0.85)',
        marginBottom: 28, letterSpacing: 0.5, position: 'relative', zIndex: 2,
      }}>点击屏幕解锁</div>

      {/* Flashlight + Camera */}
      <div style={{
        position: 'absolute', bottom: 30, left: 24, right: 24,
        display: 'flex', justifyContent: 'space-between', zIndex: 2,
      }}>
        <QuickActionBtn>
          <svg width="20" height="20" viewBox="0 0 24 24" fill="#fff">
            <path d="M7 3h10v3l-1.5 1.5v4.2l1.5 1.4V15l-3 7h-4l-3-7v-1.9l1.5-1.4V7.5L7 6V3z"/>
          </svg>
        </QuickActionBtn>
        <QuickActionBtn>
          <svg width="22" height="22" viewBox="0 0 24 24" fill="none">
            <path d="M3.5 7.5h2.8l1.5-2.2h8.4l1.5 2.2h2.8a1 1 0 011 1v9.8a1 1 0 01-1 1H3.5a1 1 0 01-1-1V8.5a1 1 0 011-1z"
              fill="#fff"/>
            <circle cx="12" cy="13.3" r="4" fill="none" stroke="#000" strokeOpacity="0.55" strokeWidth="1.4"/>
            <circle cx="17.5" cy="9.5" r="0.8" fill="#000" fillOpacity="0.55"/>
          </svg>
        </QuickActionBtn>
      </div>
    </div>
  );
}

function QuickActionBtn({ children }) {
  return (
    <div style={{
      width: 46, height: 46, borderRadius: 23,
      background: 'rgba(0,0,0,0.42)',
      backdropFilter: 'blur(12px)',
      WebkitBackdropFilter: 'blur(12px)',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
    }}>{children}</div>
  );
}

// ─── Home screen ──────────────────────────────────────────────────────
function HomeScreen({ onAppLaunch }) {
  // onAppLaunch(target?) — target='mirrorAnswer' for 镜像回答, default 'feed' (知乎)
  return (
    <div style={{
      position: 'absolute', inset: 0,
      display: 'flex', flexDirection: 'column',
    }}>
      <Wallpaper/>

      <div style={{ height: 60 }}/>

      <div style={{
        flex: 1, padding: '6px 24px', position: 'relative', zIndex: 2,
        display: 'flex', flexDirection: 'column', gap: 18,
      }}>
        <AppIcon
          label="知乎"
          background={APP_BG}
          onClick={onAppLaunch}
          glyph={<span style={{
            color: '#fff', fontSize: 36, fontWeight: 600,
            fontFamily: '"PingFang SC", "Noto Sans SC", system-ui',
            letterSpacing: -1.5, marginTop: -2,
          }}>知</span>}
        />
      </div>

      <div style={{
        margin: '0 10px 28px',
        padding: '10px 12px',
        borderRadius: 32,
        background: 'rgba(255,255,255,0.22)',
        backdropFilter: 'blur(22px)',
        WebkitBackdropFilter: 'blur(22px)',
        display: 'flex', justifyContent: 'space-around',
        border: '0.5px solid rgba(255,255,255,0.22)',
        position: 'relative', zIndex: 2,
      }}>
        <DockIcon kind="phone"/>
        <DockIcon kind="messages"/>
        <DockIcon kind="safari"/>
        <DockIcon kind="music"/>
      </div>
    </div>
  );
}

function AppIcon({ label, background, glyph, onClick }) {
  return (
    <div onClick={onClick} style={{
      width: 60, display: 'flex', flexDirection: 'column',
      alignItems: 'center', gap: 5, cursor: 'pointer',
    }}>
      <div style={{
        width: 60, height: 60, borderRadius: 14, background,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        boxShadow: '0 4px 10px rgba(0,0,0,0.18)',
      }}>{glyph}</div>
      <div style={{
        color: '#fff', fontSize: 11, fontWeight: 500,
        textShadow: '0 1px 3px rgba(0,0,0,0.5)',
      }}>{label}</div>
    </div>
  );
}

function DockIcon({ kind }) {
  const map = {
    phone: {
      bg: 'linear-gradient(180deg, #4FE36E 0%, #1FA94D 100%)',
      svg: <svg width="28" height="28" viewBox="0 0 24 24" fill="#fff">
        <path d="M5.7 2.5l3.4.6c.5.1.9.6.9 1.1l-.4 3a1.2 1.2 0 01-.6 1l-1.6.9a13.5 13.5 0 007.5 7.5l.9-1.6a1.2 1.2 0 011-.6l3-.4c.5 0 1 .4 1.1.9l.6 3.4c.1.7-.4 1.4-1.1 1.5-.4 0-.7.1-1.1.1A16 16 0 014 5.5c0-.4.1-.8.1-1.1.1-.7.8-1.2 1.6-1.9z"/>
      </svg>,
    },
    messages: {
      bg: 'linear-gradient(180deg, #5BDF55 0%, #1AA84A 100%)',
      svg: <svg width="32" height="32" viewBox="0 0 24 24" fill="#fff">
        <path d="M12 3C6.5 3 2 6.6 2 11c0 2.3 1.2 4.3 3.2 5.7L4 21l4.7-2.3c1 .2 2.2.3 3.3.3 5.5 0 10-3.6 10-8s-4.5-8-10-8z"/>
      </svg>,
    },
    safari: {
      bg: 'radial-gradient(circle, #fff 0%, #fff 40%, #C8D4E2 100%)',
      svg: <svg width="40" height="40" viewBox="0 0 40 40">
        <circle cx="20" cy="20" r="18" fill="none" stroke="#1A8FE3" strokeWidth="2"/>
        <path d="M20 4 v6 M20 36 v-6 M4 20 h6 M36 20 h-6" stroke="#1A8FE3" strokeWidth="1"/>
        <path d="M20 8 L23 18 L31 21 L20 33 L17 22 L9 19 Z" fill="#E74C3C" transform="rotate(45 20 20)"/>
        <path d="M20 8 L23 18 L31 21 L20 14 Z" fill="#fff" transform="rotate(45 20 20)"/>
      </svg>,
    },
    music: {
      bg: 'linear-gradient(180deg, #FF7AAC 0%, #FF2856 100%)',
      svg: <svg width="26" height="26" viewBox="0 0 24 24" fill="#fff">
        <path d="M9 18.5V6.5l11-2.5v12M9 18.5a2.5 2.5 0 11-2.5-2.5A2.5 2.5 0 019 18.5zm11-2.5a2.5 2.5 0 11-2.5-2.5A2.5 2.5 0 0120 16z"/>
      </svg>,
    },
  };
  const it = map[kind];
  return (
    <div style={{
      width: 50, height: 50, borderRadius: 12,
      background: it.bg, display: 'flex',
      alignItems: 'center', justifyContent: 'center',
      boxShadow: '0 2px 6px rgba(0,0,0,0.18)',
      overflow: 'hidden',
    }}>{it.svg}</div>
  );
}

// ─── Phones ──────────────────────────────────────────────────────────
function LeftPhone({ powered, onScreenChange, onMirrorAnswersReady, onLike, onQueryChange, onAskPublish, newRealAnswer, einsteinAnswer, leftBanner, onBannerDismiss, onSynthesisVisibilityChange }) {
  const [screen, setScreen] = React.useState('lock');
  const [qid, setQid] = React.useState('q_w07');
  const [authorId, setAuthorId] = React.useState(null);
  const [mirrorOn, setMirrorOn] = React.useState(false);
  const [askDraft, setAskDraft] = React.useState({ title: '', body: '', topics: [] });
  const [showToast, setShowToast] = React.useState(false);
  const [mirrorQuery, setMirrorQuery] = React.useState('');
  // QuestionCreatedPage 的"是否展开镜像 panel" 状态 — 提升到 LeftPhone, 这样
  // 用户从 mirror 跳到问题详情再返回时, panel 保持展开 (跨屏 mount/unmount 也保留).
  const [mirrorPanelOpen, setMirrorPanelOpen] = React.useState(false);
  // 镜像回答的"已跑完结果 + 滚动位置 + tab" 缓存. 同样提升到 LeftPhone,
  // 用户跳到问题详情再返回时不重跑 pipeline, 也回到上次的滚动位置.
  // shape: { query, scenario, thoughts, activeTab, scrollTop } | null
  const [mirrorCache, setMirrorCache] = React.useState(null);
  // 记录用户进入 'question' 屏前是从哪儿来的 — 返回时回到原处.
  const [questionFrom, setQuestionFrom] = React.useState('feed');
  // 同上, 记录用户进入 'authorProfile' 屏前是从哪儿来的 — 这样从镜像答案点头像跳过去后,
  // 返回时能回到 question-created (而不是默认 'question').
  const [authorFrom, setAuthorFrom] = React.useState('question');
  React.useEffect(() => { if (!powered) setScreen('lock'); }, [powered]);
  // tour 用 — 每次屏跳转后向外广播一下
  React.useEffect(() => { if (onScreenChange) onScreenChange(screen); }, [screen, onScreenChange]);
  // 把当前 askDraft.title 向上广播 (用做 tour 后期 LLM 调用上下文)
  React.useEffect(() => { if (onQueryChange && askDraft.title) onQueryChange(askDraft.title); }, [askDraft.title, onQueryChange]);
  // 镜像 pipeline 跑完 → 把 { count, outOfScope } 报给父级, 由父级决定弹主线 P4
  // ("点赞") 还是离题 P4_OFFTRACK ("数据库没这个, 看看推给了谁").
  React.useEffect(() => {
    if (!onMirrorAnswersReady) return;
    const scenario = mirrorCache && mirrorCache.scenario;
    if (!scenario) return;
    const count = Array.isArray(scenario.qualifying) ? scenario.qualifying.length : 0;
    onMirrorAnswersReady({ count, outOfScope: !!scenario.outOfScope });
  }, [mirrorCache, onMirrorAnswersReady]);
  if (!powered) return null;
  return (
    <>
      {/* Splash appears AFTER tapping the 知乎 app icon, before the feed.
         3-second countdown auto-advances to feed; tap to skip. */}
      {screen === 'splash' && <SplashScreen onContinue={() => setScreen('feed')}/>}
      {screen === 'lock' && <LockScreen onUnlock={() => setScreen('home')}/>}
      {screen === 'home' && <HomeScreen onAppLaunch={() => setScreen('splash')}/>}
      {screen === 'mirrorAnswer' && <MirrorAnswerScreen query={mirrorQuery} onBack={() => setScreen('question-created')}/>}
      {/* "首页" 按钮在首页内点击应当无效(留在首页), 而不是回到 iOS 桌面 */}
      {screen === 'feed' && <ZhihuFeed onBack={() => {/* already on feed — noop */}} onSearchTap={() => setScreen('search')} onCardTap={(id) => { setQid(id || 'q617301106'); setQuestionFrom('feed'); setScreen('question'); }} onDirectTap={() => setScreen('direct')} onMessageTap={() => setScreen('messages')} onProfileTap={() => setScreen('profile')} onPlusTap={() => setScreen('compose')}/>}
      
      {screen === 'compose' && <ComposeSheet onClose={() => setScreen('feed')} onAsk={() => setScreen('ask')}/>}
      {screen === 'ask' && <AskPage onClose={() => setScreen('compose')} onPublish={async (t, b, topics) => {
        setAskDraft({ title: t, body: b, topics: topics || [] });
        setShowToast(true);
        // 改成 await: 等 quickIsInScopeCheck 完成再切屏, 避免 race condition
        // (之前 1500ms 固定超时 + fire-and-forget, LLM 慢的时候 P3_OFFTRACK 来不及生效).
        // handleAskPublish 内部已经有 3s 超时兜底, 不会无限等.
        const start = Date.now();
        try {
          if (onAskPublish) await onAskPublish(t);
        } catch {}
        // 保底 700ms toast 显示时间, 太快显得轻飘
        const elapsed = Date.now() - start;
        if (elapsed < 700) await new Promise(r => setTimeout(r, 700 - elapsed));
        setShowToast(false);
        setScreen('question-created');
      }}/>}
      {screen === 'question-created' && <QuestionCreatedPage
        title={askDraft.title} body={askDraft.body} topics={askDraft.topics}
        mirrorOpen={mirrorPanelOpen}
        setMirrorOpen={setMirrorPanelOpen}
        mirrorCache={mirrorCache}
        setMirrorCache={setMirrorCache}
        onLike={onLike}
        newRealAnswer={newRealAnswer}
        einsteinAnswer={einsteinAnswer}
        leftBanner={leftBanner}
        onBannerDismiss={onBannerDismiss}
        onSynthesisVisibilityChange={onSynthesisVisibilityChange}
        onClose={() => {
          // 完全退出 ask 流程时清空缓存 (这是新一次提问)
          setMirrorPanelOpen(false);
          setMirrorCache(null);
          setScreen('feed');
        }}
        onMirrorAnswer={(q) => { setMirrorQuery(q || askDraft.title || ''); setScreen('mirrorAnswer'); }}
        onSourceTap={(sourceQid) => {
          setQid(sourceQid);
          setQuestionFrom('question-created');
          setScreen('question');
          // 注意: 不 reset mirrorPanelOpen 也不清 mirrorCache, 返回时 panel 还展开, 结果还在
        }}
        onAuthorTap={(authorIdFromMirror) => {
          setAuthorId(authorIdFromMirror);
          setAuthorFrom('question-created');  // 返回时回到镜像答案
          setScreen('authorProfile');
        }}/>}
      {showToast && <PublishToast/>}
      {screen === 'profile' && <ProfilePage onPlusTap={() => setScreen('compose')} onHomeTap={() => setScreen('feed')} onDirectTap={() => setScreen('direct')} onMessageTap={() => setScreen('messages')} onSettingsTap={() => setScreen('settings')}/>}
      {screen === 'settings' && <SettingsPage onBack={() => setScreen('profile')} mirrorOn={mirrorOn} setMirrorOn={setMirrorOn}/>}
      {screen === 'search' && <SearchPage onBack={() => setScreen('feed')}/>}
      {screen === 'question' && <QuestionDetail qid={qid}
        onBack={() => setScreen(questionFrom || 'feed')}
        onAuthorTap={(id) => { setAuthorId(id); setAuthorFrom('question'); setScreen('authorProfile'); }}/>}
      {screen === 'authorProfile' && <AuthorProfile authorId={authorId}
        onBack={() => setScreen(authorFrom || 'question')}
        onQuestionTap={(targetQid) => { setQid(targetQid); setQuestionFrom('authorProfile'); setScreen('question'); }}/>}
      {screen === 'direct' && <DirectAnswer onBack={() => setScreen('feed')} onPlusTap={() => setScreen('compose')} onMessageTap={() => setScreen('messages')} onProfileTap={() => setScreen('profile')}/>}
      {screen === 'messages' && <MessagesPage onBack={() => setScreen('feed')} onPlusTap={() => setScreen('compose')} onDirectTap={() => setScreen('direct')} onProfileTap={() => setScreen('profile')} onSubPage={(id) => setScreen('msg-' + id)}/>}
      {screen.startsWith('msg-') && <MessageSubPage catId={screen.slice(4)} onBack={() => setScreen('messages')}/>}
      <StatusBar tint="#fff"/>
      <HomeIndicator tint="#fff"/>
    </>
  );
}

// RightPhone — tour 阶段 driven 渲染.
//   stage='dark'           → 不渲染 (黑屏)
//   stage='lit'            → LockScreen, 自动 unlock
//   stage='home'           → 主屏 (知乎 app 高亮 + 自动启动)
//   stage='app-loading'    → 知乎 splash
//   stage='messages'       → 消息列表 (主线: "+1 通知"; 离题: "镜像邀请通知")
//   stage='plus-one'       → 主线: 消息详情 "+1" + "现在去更新" 按钮
//   stage='invite-push'    → 离题: 推送通知卡, 无按钮, 自动进 typing
//   stage='typing'         → 全屏 "正在打字中…" pixel 风格
// mode='liked' (主线) | 'invited' (离题). 决定 messages/详情卡的内容.
function RightPhone({ powered, stage, mode = 'liked', likedAuthor, onUpdateAnswerClicked }) {
  if (!powered) return null;
  return (
    <>
      {stage === 'lit'         && <LockScreen onUnlock={() => {}}/>}
      {stage === 'home'        && <HomeScreen onAppLaunch={() => {}}/>}
      {stage === 'app-loading' && <SplashScreen onContinue={() => {}}/>}
      {(stage === 'messages' || stage === 'plus-one' || stage === 'invite-push') && (
        <RightMessagesAutoFlow stage={stage} mode={mode} likedAuthor={likedAuthor} onUpdateAnswerClicked={onUpdateAnswerClicked}/>
      )}
      {stage === 'typing'      && <RightTypingOverlay/>}
      {!stage && <LockScreen onUnlock={() => {}}/>}
      <StatusBar/>
      <HomeIndicator/>
    </>
  );
}

// 右屏消息页 — mode='liked' 走 +1 主线, mode='invited' 走镜像邀请离题分支
function RightMessagesAutoFlow({ stage, mode, likedAuthor, onUpdateAnswerClicked }) {
  const leftUserName = (typeof CURRENT_USER !== 'undefined' && CURRENT_USER.name) || '用户';
  const isOffTrack = mode === 'invited';
  return (
    <div style={{
      position: 'absolute', inset: 0,
      background: '#0E0F12', color: '#EDEEF0',
      paddingTop: 38,
      fontFamily: '-apple-system, "PingFang SC", system-ui',
      display: 'flex', flexDirection: 'column',
    }}>
      <div style={{ padding: '6px 12px 4px', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <div style={{ fontSize: 14, fontWeight: 600 }}>消息</div>
        <div style={{ fontSize: 10, color: '#7A8089' }}>99+</div>
      </div>
      <div style={{ height: 1, background: '#1B1D22' }}/>
      {stage === 'messages' ? (
        isOffTrack ? (
          <div style={{ padding: '12px', display: 'flex', alignItems: 'center', gap: 10, animation: 'pop 560ms ease-out' }}>
            <div style={{ width: 32, height: 32, borderRadius: 16, background: '#E8B447', color: '#0E0F12',
              display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 16, fontWeight: 700 }}>📡</div>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 11.5, fontWeight: 600 }}>镜像系统召集</div>
              <div style={{ fontSize: 9.5, color: '#7A8089', marginTop: 2 }}>有一个边界外的问题, 只推给了你</div>
            </div>
            <div style={{ background: '#E8B447', color: '#0E0F12', borderRadius: 9, padding: '2px 7px', fontSize: 9, fontWeight: 700 }}>NEW</div>
          </div>
        ) : (
          <div style={{ padding: '12px', display: 'flex', alignItems: 'center', gap: 10, animation: 'pop 560ms ease-out' }}>
            <div style={{ width: 32, height: 32, borderRadius: 16, background: '#1772F6', color: '#fff',
              display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 14, fontWeight: 700 }}>△</div>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 11.5, fontWeight: 600 }}>赞同 +1</div>
              <div style={{ fontSize: 9.5, color: '#7A8089', marginTop: 2 }}>{leftUserName} 赞同了你的镜像回答</div>
            </div>
            <div style={{ background: '#1772F6', color: '#fff', borderRadius: 9, padding: '2px 7px', fontSize: 9 }}>+1</div>
          </div>
        )
      ) : stage === 'invite-push' ? (
        <RightInvitePushDetail/>
      ) : (
        <RightPlusOneDetail leftUserName={leftUserName} onUpdateAnswerClicked={onUpdateAnswerClicked}/>
      )}
      <style>{`@keyframes pop { 0% { transform: scale(0.6); opacity:0 } 70% { transform: scale(1.08); opacity:1 } 100% { transform: scale(1); } }`}</style>
    </div>
  );
}

// 离题分支右屏: "镜像系统给你推送了一个边界外的问题" 详情卡, 无按钮,
// 父级 timeout 自动切到 typing.
function RightInvitePushDetail() {
  return (
    <div style={{ padding: '14px 14px 0', flex: 1, fontSize: 11, lineHeight: 1.55, animation: 'fadeIn 640ms ease-out' }}>
      <div style={{
        background: '#16181D', borderRadius: 8, padding: '12px 12px 10px',
        border: '1px solid #5B4F23',
      }}>
        <div style={{ fontSize: 10, color: '#E8B447', marginBottom: 6, display: 'flex', alignItems: 'center', gap: 4 }}>
          <span style={{ fontSize: 12 }}>📡</span>
          <span>知乎 · 镜像系统召集</span>
        </div>
        <div style={{ fontSize: 12, fontWeight: 600, marginBottom: 8 }}>
          镜像系统给你推送了一个问题
        </div>
        <div style={{ fontSize: 10.5, color: '#9AA0A8', lineHeight: 1.55 }}>
          这是一道从来没有人答过的题, 有点超纲. 但只有像你这样的全能选手才接得住 — 要不要来露一手?
        </div>
      </div>
      <div style={{ marginTop: 14, padding: '10px 0', textAlign: 'center', fontSize: 10, color: '#5A5F66', fontStyle: 'italic' }}>
        正在跳转回答页…
      </div>
      <style>{`@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }`}</style>
    </div>
  );
}

function RightPlusOneDetail({ leftUserName, onUpdateAnswerClicked }) {
  return (
    <div style={{ padding: '14px 14px 0', flex: 1, fontSize: 11, lineHeight: 1.55, animation: 'fadeIn 640ms ease-out' }}>
      <div style={{ background: '#16181D', borderRadius: 8, padding: '12px 12px 10px', border: '1px solid #1B1D22' }}>
        <div style={{ fontSize: 10, color: '#7A8089', marginBottom: 6 }}>知乎 · 镜像回答</div>
        <div style={{ fontSize: 12, fontWeight: 600, marginBottom: 8 }}>
          {leftUserName} 给你的镜像回答点了赞
        </div>
        <div style={{ fontSize: 10.5, color: '#9AA0A8', lineHeight: 1.55 }}>
          你过去关于该话题的一条长答, 在镜像回答里作为推荐内容展示给了新用户. 看起来这个话题还在被讨论.
        </div>
      </div>
      <div style={{ marginTop: 14 }}>
        <div onClick={onUpdateAnswerClicked} style={{
          width: '100%', padding: '11px 0', borderRadius: 8, textAlign: 'center',
          background: '#1772F6', color: '#fff', fontSize: 12, fontWeight: 600,
          cursor: 'pointer',
          boxShadow: '0 4px 14px rgba(23, 114, 246, 0.35)',
        }}>现在去更新</div>
      </div>
      <style>{`@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }`}</style>
    </div>
  );
}

function RightTypingOverlay() {
  const [dots, setDots] = React.useState('');
  React.useEffect(() => {
    const t = setInterval(() => setDots(d => d.length >= 3 ? '' : d + '·'), 380);
    return () => clearInterval(t);
  }, []);
  return (
    <div style={{
      position: 'absolute', inset: 0, background: '#000',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      flexDirection: 'column', gap: 16,
      fontFamily: '"JetBrains Mono", "Silkscreen", monospace',
      color: '#1772F6',
    }}>
      <div style={{ fontSize: 14, letterSpacing: 4 }}>正在打字中</div>
      <div style={{ fontSize: 28, letterSpacing: 6 }}>{dots || ' '}</div>
    </div>
  );
}

Object.assign(window, { LeftPhone, RightPhone });
