// Fake login screen — pick an identity; persists as session in LAStore.
(function () {
function Login() {
  const [name, setName] = React.useState("");
  const [code, setCode] = React.useState(() => { try { return new URLSearchParams(location.search).get("invite") || ""; } catch { return ""; } });
  const [err, setErr] = React.useState(null);
  const go = (s) => window.LAStore.setSession(s);
  const goStudent = () => {
    if (!name.trim()) return setErr("name");
    const inv = window.LAStore.findInvite(code);
    if (!inv) return setErr("code");
    go({ role: "student", name: name.trim(), invite: inv.code });
  };
  return (
    <div data-screen-label="Login" style={{ minHeight: "100vh", display: "grid", placeItems: "center", background: "var(--bg-page)", padding: 24 }}>
      <div style={{ width: "min(880px, 100%)" }}>
        <div style={{ textAlign: "center", marginBottom: 36 }}>
          <div style={{ display: "flex", justifyContent: "center", alignItems: "center", gap: 10 }} className="brand">
            <span className="brand-mark"><em>R</em></span>
            <span style={{ fontFamily: "var(--serif)", fontSize: 22 }}>Recommend</span>
          </div>
          <p style={{ color: "var(--ink-3)", fontSize: 14.5, marginTop: 10 }}>Evidence-first recommendation letters. Sign in to continue.</p>
        </div>
        <div className="login-cols" style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 20 }}>
          <div className="card" style={{ padding: 28 }}>
            <div className="eyebrow" style={{ marginBottom: 10 }}>Faculty</div>
            <h2 style={{ fontSize: 20, marginBottom: 6 }}>Prof. Amelia Hart</h2>
            <p style={{ fontSize: 13, color: "var(--ink-3)", marginBottom: 18, lineHeight: 1.5 }}>Review request packets, set your submission window, and draft with cited evidence.</p>
            <button className="btn btn-primary" onClick={() => go({ role: "faculty", name: "Prof. Amelia Hart" })}>Sign in as Prof. Hart →</button>
          </div>
          <div className="card" style={{ padding: 28 }}>
            <div className="eyebrow" style={{ marginBottom: 10 }}>Student</div>
            <h2 style={{ fontSize: 20, marginBottom: 6 }}>Request a letter</h2>
            <p style={{ fontSize: 13, color: "var(--ink-3)", marginBottom: 14, lineHeight: 1.5 }}>Build an evidence packet for Prof. Hart, then track its status. You'll need her invite code.</p>
            <input className="input" placeholder="Your full name" value={name} onChange={(e) => { setName(e.target.value); setErr(null); }} onKeyDown={(e) => e.key === "Enter" && goStudent()} style={{ marginBottom: 10, borderColor: err === "name" ? "#DC2626" : undefined }} />
            <input className="input" placeholder="Invite code (e.g. HART26)" value={code} onChange={(e) => { setCode(e.target.value.toUpperCase()); setErr(null); }} onKeyDown={(e) => e.key === "Enter" && goStudent()} style={{ marginBottom: 10, borderColor: err === "code" ? "#DC2626" : undefined, fontFamily: "var(--mono)", letterSpacing: "0.08em" }} />
            <div style={{ display: "flex", gap: 8, alignItems: "center", flexWrap: "wrap" }}>
              <button className="btn btn-accent" onClick={goStudent}>Sign in →</button>
              <button className="btn btn-ghost btn-sm" onClick={() => go({ role: "student", name: "Maya Okonkwo", invite: "HART26" })} style={{ color: "var(--muted)" }}>Demo: Maya Okonkwo</button>
            </div>
            {err === "name" && <div style={{ color: "#DC2626", fontSize: 12, marginTop: 8 }}>Enter your name to continue.</div>}
            {err === "code" && <div style={{ color: "#DC2626", fontSize: 12, marginTop: 8 }}>That invite code isn't valid — ask Prof. Hart for one.</div>}
          </div>
        </div>
        <p style={{ textAlign: "center", color: "var(--subtle)", fontSize: 11.5, marginTop: 22 }}>Prototype — no real authentication.</p>
      </div>
    </div>
  );
}
window.Login = Login;
})();
