/* Login screen — shared password */
(() => {
const { useState } = React;

function Login({ onSuccess }) {
  const [password, setPassword] = useState('');
  const [error, setError] = useState('');
  const [loading, setLoading] = useState(false);

  async function submit(e) {
    e.preventDefault();
    setError(''); setLoading(true);
    try {
      await api.login(password);
      onSuccess();
    } catch (err) {
      setError(err.message || 'Login failed');
    } finally {
      setLoading(false);
    }
  }

  return (
    <div className="min-h-screen flex items-center justify-center p-4">
      <div className="w-full max-w-sm bg-white rounded-2xl shadow-xl p-8">
        <div className="text-center mb-6">
          <div className="text-sm font-bold text-ink">Tov Law</div>
          <div className="text-2xl font-bold mt-1">TCPA IQ <span className="text-brand">2</span></div>
          <div className="text-xs text-ink-4 mt-1">Defendant evaluation · Consumer Lit, Inc.</div>
        </div>
        <form onSubmit={submit}>
          <label className="block text-xs font-semibold text-ink-3 mb-2 uppercase tracking-wider">Shared password</label>
          <input
            type="password"
            value={password}
            onChange={e => setPassword(e.target.value)}
            className="field-input mb-3"
            placeholder="••••••••"
            autoFocus
            disabled={loading}
          />
          {error && <div className="text-xs text-red-700 mb-3 bg-red-50 border border-red-200 rounded p-2">{error}</div>}
          <button
            type="submit"
            disabled={loading || !password}
            className="w-full bg-brand hover:bg-brand-dark text-white font-semibold py-2.5 rounded-lg transition disabled:opacity-50"
          >
            {loading ? 'Signing in…' : 'Sign in'}
          </button>
        </form>
        <div className="mt-6 text-[11px] text-ink-5 text-center">
          For intake team only · &copy; 2026 Tov Law
        </div>
      </div>
    </div>
  );
}

window.Login = Login;
})();
