// Source: index.html script block (extracted). Loaded via <script type="text/babel"> in index.html.
function Login() {
  const { t: tr, lang, setLang } = useLang();

  const [authError, setAuthError] = React.useState(() => {
    try {
      const code = new URL(window.location.href).searchParams.get("auth_error");
      return code || null;
    } catch { return null; }
  });
  React.useEffect(() => {
    if (!authError) return;
    try {
      const url = new URL(window.location.href);
      url.searchParams.delete("auth_error");
      window.history.replaceState({}, "", url.pathname + (url.search ? "?" + url.searchParams.toString() : "") + url.hash);
    } catch { /* ignore */ }
  }, [authError]);

  const L = {
    hero_eyebrow:   {en:"for licensed clinicians",                 zh:"面向执业临床医师"},
    hero_h1:        {en:"Review, correct, and score agent trajectories for clinical RL.", zh:"审核、修正并为临床强化学习中的智能体轨迹评分。"},
    hero_p:         {en:"You'll see generated tasks before they enter training — fix diagnoses, tune tools and evaluation criteria, run a live simulation against the model, and score every action the agent takes. Your judgment becomes the reward.",
                     zh:"在任务进入训练之前你会先看到生成的任务——修正诊断、调整工具与评估标准、对模型运行一次实时模拟，并为智能体的每一个动作打分。你的判断就是奖励信号。"},
    foot_copy:      {en:"© 2026 Clinical Agent Project",           zh:"© 2026 临床智能体项目"},
    foot_audit:     {en:"HIPAA-grade audit · all edits signed",    zh:"HIPAA 级审计 · 所有修改均签名"},
    foot_build:     {en:"Build a3e2f1",                            zh:"构建号 a3e2f1"},
    card_h2:        {en:"Sign in to continue",                     zh:"登录以继续"},
    card_sub:       {en:"Use your Google or GitHub account. We only read your email, name, and avatar.",
                     zh:"使用 Google 或 GitHub 账户登录。我们仅读取你的邮箱、姓名和头像。"},
    sso_google:     {en:"Continue with Google",                    zh:"使用 Google 继续"},
    sso_github:     {en:"Continue with GitHub",                    zh:"使用 GitHub 继续"},
    note:           {en:"By continuing you agree your activity is logged for audit purposes.",
                     zh:"继续即表示你同意你的操作将被记录以供审计。"},
    err_state_invalid:         {en:"Sign-in expired. Please try again.",
                                zh:"登录会话已过期，请重试。"},
    err_oauth_exchange_failed: {en:"The provider rejected the sign-in. Please try again.",
                                zh:"第三方登录失败，请重试。"},
    err_profile_fetch_failed:  {en:"Could not read your profile from the provider.",
                                zh:"无法从第三方读取你的个人资料。"},
    err_email_missing:         {en:"Your account has no verified email — add one with the provider and retry.",
                                zh:"你的账号没有经过验证的邮箱，请在第三方添加后重试。"},
    err_provider_unknown:      {en:"Unknown sign-in provider.",
                                zh:"未知的登录方式。"},
    err_default:               {en:"Sign-in failed. Please try again.",
                                zh:"登录失败，请重试。"},
  };
  const s = (k) => (L[k] && L[k][lang]) || (L[k] && L[k].en) || "";

  const currentPath = (() => {
    try {
      const u = new URL(window.location.href);
      return (u.pathname || "/") + (u.hash || "");
    } catch { return "/"; }
  })();
  const href = (provider) =>
    `/api/auth/login?provider=${encodeURIComponent(provider)}&redirect=${encodeURIComponent(currentPath)}`;

  const errorMessage = (() => {
    if (!authError) return null;
    const key = `err_${authError}`;
    return (L[key] && (L[key][lang] || L[key].en)) || s("err_default");
  })();

  return (
    <div className="login-wrap">
      <div className="login-left">
        <div className="login-h">
          <div className="brand-mark">CA</div>
          <div>
            <div className="brand-name">{tr("brand_name")}</div>
            <div style={{fontSize:11.5, color:"var(--ink-mute)", fontFamily:"var(--font-mono)"}}>v0.4 · tau2 / primekg</div>
          </div>
          <div className="lang-toggle" style={{marginLeft:"auto"}} role="group" title={tr("lang_toggle_title")}>
            <button data-active={lang==="en"} onClick={()=>setLang("en")}>EN</button>
            <button data-active={lang==="zh"} onClick={()=>setLang("zh")}>中文</button>
          </div>
        </div>
        <div className="login-hero">
          <div className="eyebrow">{s("hero_eyebrow")}</div>
          <h1>{s("hero_h1")}</h1>
          <p>{s("hero_p")}</p>
        </div>
        <div className="login-foot">
          <span>{s("foot_copy")}</span>
          <span>{s("foot_audit")}</span>
          <span>{s("foot_build")}</span>
        </div>
      </div>
      <div className="login-right">
        <div className="login-bg-stripes">
          <svg viewBox="0 0 400 600" preserveAspectRatio="xMidYMid slice">
            {Array.from({length:28}).map((_,i)=>(
              <line key={i} x1={i*30-50} y1="0" x2={i*30+250} y2="600" stroke="var(--line)" strokeWidth="1"/>
            ))}
          </svg>
        </div>
        <div className="login-card">
          <h2>{s("card_h2")}</h2>
          <p className="sub">{s("card_sub")}</p>
          {errorMessage && (
            <div
              role="alert"
              style={{
                marginBottom: 14,
                padding: "9px 11px",
                background: "oklch(94% 0.04 25)",
                border: "1px solid oklch(72% 0.14 25)",
                color: "oklch(38% 0.12 25)",
                borderRadius: "var(--r-sm)",
                fontSize: 12.5,
              }}
            >
              {errorMessage}
            </div>
          )}
          <div className="login-alt">
            <a className="btn-sso" href={href("google")}>{s("sso_google")}</a>
            <a className="btn-sso" href={href("github")}>{s("sso_github")}</a>
          </div>
          <div className="credential-hint" style={{marginTop: 20}}>
            {s("note")}
          </div>
        </div>
      </div>
    </div>
  );
}

window.Login = Login;
