/* LoginPage.jsx — Guest + Host login with Firebase Auth */

function LoginPage() {
  const [mode, setMode] = React.useState('guest'); // 'guest' | 'host'
  const [view, setView] = React.useState('login'); // 'login' | 'signup' | 'forgot' | 'phone'
  const [loading, setLoading] = React.useState(false);
  const [error, setError] = React.useState('');
  const [form, setForm] = React.useState({ email: '', password: '', name: '', confirmPassword: '' });
  // Phone sign-in state (guest tab only).
  const [phone, setPhone] = React.useState('');
  const [code, setCode] = React.useState('');
  const [confirmation, setConfirmation] = React.useState(null);
  const [showPw, setShowPw] = React.useState(false);
  const [showPw2, setShowPw2] = React.useState(false);

  function update(field, value) {
    setForm(f => ({ ...f, [field]: value }));
    if (error) setError('');
  }

  // Where to land after a successful login. Master accounts can enter either
  // portal — they follow the selected tab; guests/hosts follow their role.
  function destinationFor(session) {
    if (session.role === 'master') {
      return mode === 'host' ? '../admin/host-portal.html' : '../guest-portal/index.html';
    }
    if (session.role === 'host' || session.role === 'admin') return '../admin/host-portal.html';
    return '../guest-portal/index.html';
  }

  async function handleSubmit(e) {
    e.preventDefault();
    setError('');

    if (!window.NovusAuth) return setError('Auth not ready. Please refresh and try again.');

    if (view === 'signup') {
      if (!form.name.trim()) return setError('Please enter your name');
      if (form.password.length < 6) return setError('Password must be at least 6 characters');
      if (form.password !== form.confirmPassword) return setError('Passwords do not match');

      setLoading(true);
      try {
        const session = await window.NovusAuth.signUp(form.email, form.password, form.name);
        window.location.href = destinationFor(session);
      } catch (err) {
        setError(prettyError(err.message));
      } finally {
        setLoading(false);
      }
    } else if (view === 'login') {
      if (!form.email || !form.password) return setError('Please fill in all fields');
      setLoading(true);
      try {
        const session = await window.NovusAuth.login(form.email, form.password);
        window.location.href = destinationFor(session);
      } catch (err) {
        setError(prettyError(err.message));
      } finally {
        setLoading(false);
      }
    }
  }

  function prettyError(msg) {
    msg = msg || '';
    if (msg.includes('popup-closed-by-user') || msg.includes('cancelled-popup-request'))
      return 'Sign-in was cancelled.';
    if (msg.includes('account-exists-with-different-credential'))
      return 'An account already exists with a different sign-in method for this email.';
    if (msg.includes('operation-not-allowed'))
      return "This sign-in method isn't enabled yet.";
    if (msg.includes('invalid-verification-code'))
      return 'That code is incorrect. Please re-check and try again.';
    if (msg.includes('invalid-phone-number'))
      return 'Please enter a valid phone number, including country code.';
    if (msg.includes('user-not-found') || msg.includes('wrong-password') || msg.includes('invalid-credential'))
      return 'Wrong email or password.';
    if (msg.includes('email-already-in-use'))
      return 'An account with this email already exists. Try logging in.';
    if (msg.includes('invalid-email'))
      return 'Please enter a valid email address.';
    if (msg.includes('weak-password'))
      return 'Password is too weak (min. 6 characters).';
    if (msg.includes('network'))
      return 'Network error. Check your connection.';
    return msg;
  }

  // ── social sign-in (guest tab only) ────────────────────────────────────────
  async function handleProvider(kind) {
    setError('');
    if (!window.NovusAuth || !window.NovusAuth.loginWithProvider)
      return setError('Auth not ready. Please refresh and try again.');
    setLoading(true);
    try {
      const session = await window.NovusAuth.loginWithProvider(kind);
      window.location.href = destinationFor(session);
    } catch (err) {
      setError(prettyError(err.code || err.message));
    } finally {
      setLoading(false);
    }
  }

  // ── phone sign-in (guest tab only) ─────────────────────────────────────────
  async function handleStartPhone(e) {
    if (e) e.preventDefault();
    setError('');
    if (!phone.trim()) return setError('Please enter your phone number.');
    if (!window.NovusAuth || !window.NovusAuth.startPhone)
      return setError('Auth not ready. Please refresh and try again.');
    setLoading(true);
    try {
      const conf = await window.NovusAuth.startPhone(phone.trim(), 'novus-recaptcha');
      setConfirmation(conf);
    } catch (err) {
      setError(prettyError(err.code || err.message));
    } finally {
      setLoading(false);
    }
  }

  async function handleConfirmPhone(e) {
    if (e) e.preventDefault();
    setError('');
    if (code.trim().length < 6) return setError('Enter the 6-digit code we sent you.');
    setLoading(true);
    try {
      const session = await window.NovusAuth.confirmPhone(confirmation, code.trim());
      window.location.href = destinationFor(session);
    } catch (err) {
      setError(prettyError(err.code || err.message));
    } finally {
      setLoading(false);
    }
  }

  // Inline branded provider button (no external SDK / asset files).
  function ProviderButton({ kind, label, icon }) {
    return (
      <button
        type="button"
        disabled={loading}
        onClick={() => handleProvider(kind)}
        style={{
          display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10,
          width: '100%', padding: '12px 16px', borderRadius: 10,
          border: '1px solid var(--line)', background: '#fff', color: 'var(--navy-900)',
          fontSize: 14, fontWeight: 500, cursor: loading ? 'default' : 'pointer',
          opacity: loading ? 0.6 : 1, transition: 'all 160ms var(--ease-novus)',
        }}
      >
        {icon}<span>{label}</span>
      </button>
    );
  }

  // Inline brand SVGs (no new asset files).
  const googleIcon = (
    <svg width="18" height="18" viewBox="0 0 48 48" aria-hidden="true">
      <path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"/>
      <path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"/>
      <path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"/>
      <path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"/>
    </svg>
  );
  const facebookIcon = (
    <svg width="18" height="18" viewBox="0 0 24 24" aria-hidden="true">
      <path fill="#1877F2" d="M24 12.07C24 5.4 18.63 0 12 0S0 5.4 0 12.07C0 18.1 4.39 23.1 10.13 24v-8.44H7.08v-3.49h3.05V9.41c0-3.02 1.79-4.69 4.53-4.69 1.31 0 2.68.24 2.68.24v2.97h-1.51c-1.49 0-1.96.93-1.96 1.89v2.25h3.33l-.53 3.49h-2.8V24C19.61 23.1 24 18.1 24 12.07z"/>
    </svg>
  );
  const appleIcon = (
    <svg width="18" height="18" viewBox="0 0 24 24" aria-hidden="true">
      <path fill="#000" d="M16.37 12.78c.02 2.5 2.19 3.33 2.21 3.34-.02.06-.35 1.2-1.15 2.37-.69 1.02-1.41 2.03-2.54 2.05-1.11.02-1.47-.66-2.74-.66-1.27 0-1.66.64-2.72.68-1.09.04-1.92-1.1-2.62-2.12-1.43-2.08-2.52-5.87-1.05-8.43.73-1.27 2.03-2.08 3.45-2.1 1.07-.02 2.08.72 2.74.72.65 0 1.88-.89 3.17-.76.54.02 2.06.22 3.03 1.64-.08.05-1.81 1.06-1.79 3.16zM14.3 4.7c.59-.71.98-1.7.87-2.7-.85.04-1.87.57-2.47 1.28-.54.63-1.01 1.64-.88 2.6.94.08 1.9-.48 2.48-1.18z"/>
    </svg>
  );
  const phoneIcon = (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M6.5 3h3l1.5 4-2 1.5a11 11 0 0 0 4.5 4.5l1.5-2 4 1.5v3a2 2 0 0 1-2 2A16 16 0 0 1 4.5 5a2 2 0 0 1 2-2z" stroke="var(--navy-900)" strokeWidth="1.6" strokeLinejoin="round"/>
    </svg>
  );
  const eyeIcon = (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M1 12s4-7 11-7 11 7 11 7-4 7-11 7S1 12 1 12z" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/>
      <circle cx="12" cy="12" r="3" stroke="currentColor" strokeWidth="1.6"/>
    </svg>
  );
  const eyeOffIcon = (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M3 3l18 18M10.6 10.6a3 3 0 0 0 4.2 4.2M9.9 5.2A10.6 10.6 0 0 1 12 5c7 0 11 7 11 7a18.5 18.5 0 0 1-3.2 4.1M6.1 6.1A18.4 18.4 0 0 0 1 12s4 7 11 7a10.6 10.6 0 0 0 3.2-.5" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/>
    </svg>
  );

  return (
    <div style={{
      minHeight: '100vh',
      display: 'grid',
      gridTemplateColumns: '1fr',
    }} className="login-grid">
      <style>{`
        @media (min-width: 900px) {
          .login-grid { grid-template-columns: 1fr 1fr !important; }
          .login-hero { display: flex !important; }
        }
      `}</style>

      {/* Visual side — hidden on mobile */}
      <div className="login-hero" style={{
        display: 'none',
        position: 'relative',
        background: 'var(--navy-900)',
        overflow: 'hidden',
      }}>
        <img
          src="https://images.unsplash.com/photo-1613977257363-707ba9348227?auto=format&fit=crop&w=1500&q=80"
          alt="Villa"
          style={{ position: 'absolute', inset: 0, width: '100%', height: '100%', objectFit: 'cover', opacity: 0.55 }}
        />
        <div style={{
          position: 'absolute', inset: 0,
          background: 'linear-gradient(135deg, rgba(15,42,66,0.85) 0%, rgba(15,42,66,0.55) 60%, rgba(15,42,66,0.8) 100%)',
        }} />
        <div style={{
          position: 'relative', zIndex: 2,
          padding: 60,
          display: 'flex', flexDirection: 'column', justifyContent: 'space-between',
          height: '100%', color: '#fff',
        }}>
          <a href="index.html" style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
            <img src="../../assets/novus-logo-mark.jpg" style={{ height: 36, borderRadius: 4 }} alt="Novus" />
            <span style={{ fontWeight: 600, letterSpacing: '0.24em', fontSize: 13, color: '#fff' }}>NOVUS RESIDENTIALS</span>
          </a>

          <div>
            <span className="sun-bullet" style={{ boxShadow: '0 0 0 6px rgba(252,172,100,0.18)' }} />
            <h1 style={{
              fontFamily: 'var(--font-display)', fontWeight: 400,
              fontSize: 'clamp(36px, 4vw, 56px)', color: '#fff',
              margin: '20px 0 16px', letterSpacing: '-0.015em', lineHeight: 1.1,
              maxWidth: 480,
            }}>
              The art of <em style={{ fontStyle: 'italic' }}>refined</em> hospitality.
            </h1>
            <p style={{ color: 'rgba(255,255,255,0.7)', fontSize: 16, lineHeight: 1.6, maxWidth: 420 }}>
              A curated collection across Miami, held to a single standard.
              Log in to manage your stay or property.
            </p>
          </div>

          <div style={{ fontSize: 12, color: 'rgba(255,255,255,0.5)' }}>
            <em style={{ fontFamily: 'var(--font-display)', fontStyle: 'italic' }}>Nos vemos pronto.</em>
          </div>
        </div>
      </div>

      {/* Form side */}
      <div style={{
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        padding: '32px 20px',
        background: 'var(--sand-50)',
        minHeight: '100vh',
      }}>
        <div style={{
          width: '100%',
          maxWidth: 440,
        }}>
          {/* Mobile logo */}
          <a href="index.html" style={{
            display: 'flex', alignItems: 'center', gap: 10,
            marginBottom: 32, justifyContent: 'center',
          }} className="login-mobile-logo">
            <img src="../../assets/novus-logo-mark.jpg" style={{ height: 36, borderRadius: 4 }} alt="Novus" />
            <span style={{ fontWeight: 600, letterSpacing: '0.2em', fontSize: 12, color: 'var(--navy-900)' }}>NOVUS RESIDENTIALS</span>
          </a>

          {/* Mode tabs */}
          <div style={{
            display: 'flex',
            background: '#fff',
            border: '1px solid var(--line)',
            borderRadius: 999,
            padding: 4,
            marginBottom: 28,
          }}>
            <button onClick={() => setMode('guest')} style={{
              flex: 1, padding: '10px 16px', borderRadius: 999,
              background: mode === 'guest' ? 'var(--navy-900)' : 'transparent',
              color: mode === 'guest' ? '#fff' : 'var(--ink-700)',
              border: 0, fontSize: 13, fontWeight: 500,
              transition: 'all 200ms var(--ease-novus)',
              cursor: 'pointer',
            }}>Guest</button>
            <button onClick={() => setMode('host')} style={{
              flex: 1, padding: '10px 16px', borderRadius: 999,
              background: mode === 'host' ? 'var(--navy-900)' : 'transparent',
              color: mode === 'host' ? '#fff' : 'var(--ink-700)',
              border: 0, fontSize: 13, fontWeight: 500,
              transition: 'all 200ms var(--ease-novus)',
              cursor: 'pointer',
            }}>Host / Admin</button>
          </div>

          {/* Heading */}
          <div style={{ marginBottom: 28 }}>
            <h2 style={{
              fontFamily: 'var(--font-display)', fontWeight: 400,
              fontSize: 'clamp(28px, 4vw, 36px)', color: 'var(--navy-900)',
              margin: 0, letterSpacing: '-0.015em', lineHeight: 1.15,
            }}>
              {view === 'signup'
                ? (mode === 'host' ? 'Become a host' : 'Create your account')
                : view === 'forgot'
                ? 'Reset your password'
                : mode === 'host' ? 'Host portal access' : 'Welcome back'}
            </h2>
            <p style={{ color: 'var(--ink-500)', fontSize: 14, marginTop: 8, marginBottom: 0 }}>
              {view === 'signup'
                ? `Sign up to ${mode === 'host' ? 'manage your properties' : 'track your stay'}.`
                : view === 'forgot'
                ? 'Enter your email and we\'ll send a reset link.'
                : `Log in to ${mode === 'host' ? 'your host dashboard' : 'manage your reservation'}.`}
            </p>
          </div>

          {/* Phone sign-in sub-view (guest tab only) */}
          {view === 'phone' && (
            <div>
              {!confirmation ? (
                <form onSubmit={handleStartPhone} style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
                  <div className="field">
                    <label htmlFor="login-phone">Mobile number</label>
                    <input
                      id="login-phone"
                      type="tel"
                      value={phone}
                      onChange={e => { setPhone(e.target.value); if (error) setError(''); }}
                      placeholder="+1 305-848-3113"
                      autoComplete="tel"
                      aria-invalid={!!error}
                      required
                    />
                  </div>
                  {error && (
                    <div role="alert" style={{ padding: '12px 14px', background: '#FEF2F0', border: '1px solid #F2D6CF', borderRadius: 10, color: 'var(--danger)', fontSize: 13, lineHeight: 1.5 }}>{error}</div>
                  )}
                  <button type="submit" className="btn btn-primary" disabled={loading} style={{ marginTop: 8, padding: '14px 24px', fontSize: 14, opacity: loading ? 0.6 : 1, justifyContent: 'center' }}>
                    {loading ? 'Sending…' : 'Send code →'}
                  </button>
                  {/* Invisible reCAPTCHA mounts here */}
                  <div id="novus-recaptcha" />
                </form>
              ) : (
                <form onSubmit={handleConfirmPhone} style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
                  <div className="field">
                    <label htmlFor="login-code">6-digit code</label>
                    <input
                      id="login-code"
                      type="text"
                      inputMode="numeric"
                      maxLength={6}
                      value={code}
                      onChange={e => { setCode(e.target.value.replace(/\D/g, '')); if (error) setError(''); }}
                      placeholder="123456"
                      autoComplete="one-time-code"
                      aria-invalid={!!error}
                      required
                    />
                  </div>
                  {error && (
                    <div role="alert" style={{ padding: '12px 14px', background: '#FEF2F0', border: '1px solid #F2D6CF', borderRadius: 10, color: 'var(--danger)', fontSize: 13, lineHeight: 1.5 }}>{error}</div>
                  )}
                  <button type="submit" className="btn btn-primary" disabled={loading} style={{ marginTop: 8, padding: '14px 24px', fontSize: 14, opacity: loading ? 0.6 : 1, justifyContent: 'center' }}>
                    {loading ? 'Verifying…' : 'Verify & continue →'}
                  </button>
                </form>
              )}
              <div style={{ textAlign: 'center', marginTop: 16 }}>
                <button type="button" onClick={() => { setView('login'); setError(''); setConfirmation(null); setCode(''); }} style={{ background: 'none', border: 0, color: 'var(--navy-700)', fontWeight: 500, cursor: 'pointer', fontSize: 14, padding: 4, textDecoration: 'underline' }}>
                  ← Back to login
                </button>
              </div>
            </div>
          )}

          {/* Form */}
          {view !== 'phone' && (
          <form onSubmit={handleSubmit} style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
            {view === 'signup' && (
              <div className="field">
                <label htmlFor="login-name">Full Name</label>
                <input
                  id="login-name"
                  type="text"
                  value={form.name}
                  onChange={e => update('name', e.target.value)}
                  placeholder="Your full name"
                  autoComplete="name"
                />
              </div>
            )}

            <div className="field">
              <label htmlFor="login-email">Email</label>
              <input
                id="login-email"
                type="email"
                value={form.email}
                onChange={e => update('email', e.target.value)}
                placeholder="you@email.com"
                autoComplete="email"
                inputMode="email"
                required
              />
            </div>

            {view !== 'forgot' && (
              <div className="field">
                <label htmlFor="login-password">Password</label>
                <div style={{ position: 'relative' }}>
                  <input
                    id="login-password"
                    type={showPw ? 'text' : 'password'}
                    value={form.password}
                    onChange={e => update('password', e.target.value)}
                    placeholder="Min. 6 characters"
                    autoComplete={view === 'signup' ? 'new-password' : 'current-password'}
                    style={{ width: '100%', paddingRight: 44 }}
                    required
                  />
                  <button type="button" onClick={() => setShowPw(v => !v)}
                    aria-label={showPw ? 'Hide password' : 'Show password'}
                    aria-pressed={showPw}
                    title={showPw ? 'Hide password' : 'Show password'}
                    style={{ position: 'absolute', right: 6, top: '50%', transform: 'translateY(-50%)', background: 'none', border: 0, padding: 6, cursor: 'pointer', color: 'var(--ink-500)', display: 'flex', alignItems: 'center', minWidth: 44, minHeight: 44, justifyContent: 'center' }}>
                    {showPw ? eyeOffIcon : eyeIcon}
                  </button>
                </div>
              </div>
            )}

            {view === 'signup' && (
              <div className="field">
                <label htmlFor="login-confirm">Confirm Password</label>
                <div style={{ position: 'relative' }}>
                  <input
                    id="login-confirm"
                    type={showPw2 ? 'text' : 'password'}
                    value={form.confirmPassword}
                    onChange={e => update('confirmPassword', e.target.value)}
                    placeholder="Re-enter your password"
                    autoComplete="new-password"
                    style={{ width: '100%', paddingRight: 44 }}
                    required
                  />
                  <button type="button" onClick={() => setShowPw2(v => !v)}
                    aria-label={showPw2 ? 'Hide password' : 'Show password'}
                    aria-pressed={showPw2}
                    title={showPw2 ? 'Hide password' : 'Show password'}
                    style={{ position: 'absolute', right: 6, top: '50%', transform: 'translateY(-50%)', background: 'none', border: 0, padding: 6, cursor: 'pointer', color: 'var(--ink-500)', display: 'flex', alignItems: 'center', minWidth: 44, minHeight: 44, justifyContent: 'center' }}>
                    {showPw2 ? eyeOffIcon : eyeIcon}
                  </button>
                </div>
              </div>
            )}

            {error && (
              <div role="alert" style={{
                padding: '12px 14px',
                background: '#FEF2F0',
                border: '1px solid #F2D6CF',
                borderRadius: 10,
                color: 'var(--danger)',
                fontSize: 13,
                lineHeight: 1.5,
              }}>{error}</div>
            )}

            <button
              type="submit"
              className="btn btn-primary"
              disabled={loading}
              style={{
                marginTop: 8, padding: '14px 24px', fontSize: 14,
                opacity: loading ? 0.6 : 1, justifyContent: 'center',
              }}
            >
              {loading ? 'Loading…' : (view === 'signup' ? 'Create account' : view === 'forgot' ? 'Send reset link' : 'Log in')}
              {!loading && ' →'}
            </button>

            {view === 'login' && (
              <div style={{ textAlign: 'center', marginTop: 6 }}>
                <button
                  type="button"
                  onClick={() => { setView('forgot'); setError(''); }}
                  style={{ background: 'none', border: 0, color: 'var(--ink-500)', fontSize: 13, cursor: 'pointer', padding: 6 }}
                >
                  Forgot your password?
                </button>
              </div>
            )}
          </form>
          )}

          {/* Admin Google sign-in — HOST/ADMIN tab, login view */}
          {mode === 'host' && view === 'login' && (
            <div style={{ marginTop: 22 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 12, margin: '0 0 16px' }}>
                <div style={{ flex: 1, height: 1, background: 'var(--line)' }} />
                <span style={{ fontSize: 12, color: 'var(--ink-500)' }}>or</span>
                <div style={{ flex: 1, height: 1, background: 'var(--line)' }} />
              </div>
              <ProviderButton kind="google" label="Continue with Google" icon={googleIcon} />
              <p style={{ fontSize: 12, color: 'var(--ink-500)', textAlign: 'center', marginTop: 10, marginBottom: 0 }}>
                Use your <strong>@novusresidentials.com</strong> Google account for admin access.
              </p>
            </div>
          )}

          {/* Social + phone providers — GUEST tab only, on login/signup views */}
          {mode === 'guest' && (view === 'login' || view === 'signup') && (
            <div style={{ marginTop: 22 }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 12, margin: '0 0 16px' }}>
                <div style={{ flex: 1, height: 1, background: 'var(--line)' }} />
                <span style={{ fontSize: 12, color: 'var(--ink-500)' }}>or continue with</span>
                <div style={{ flex: 1, height: 1, background: 'var(--line)' }} />
              </div>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
                <ProviderButton kind="google" label="Continue with Google" icon={googleIcon} />
                <ProviderButton kind="facebook" label="Continue with Facebook" icon={facebookIcon} />
                <ProviderButton kind="apple" label="Continue with Apple" icon={appleIcon} />
                <button
                  type="button"
                  disabled={loading}
                  onClick={() => { setView('phone'); setError(''); setConfirmation(null); setCode(''); }}
                  style={{
                    display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10,
                    width: '100%', padding: '12px 16px', borderRadius: 10,
                    border: '1px solid var(--line)', background: '#fff', color: 'var(--navy-900)',
                    fontSize: 14, fontWeight: 500, cursor: loading ? 'default' : 'pointer',
                    opacity: loading ? 0.6 : 1, transition: 'all 160ms var(--ease-novus)',
                  }}
                >
                  {phoneIcon}<span>Continue with phone</span>
                </button>
              </div>
            </div>
          )}

          {/* Switch view */}
          {view !== 'phone' && (
          <div style={{
            marginTop: 24,
            paddingTop: 20,
            borderTop: '1px solid var(--line)',
            textAlign: 'center',
            fontSize: 14,
            color: 'var(--ink-500)',
          }}>
            {view === 'login' ? (
              <>Don't have an account?{' '}
                <button onClick={() => { setView('signup'); setError(''); }} style={{
                  background: 'none', border: 0, color: 'var(--navy-700)', fontWeight: 500,
                  cursor: 'pointer', fontSize: 14, padding: 4, textDecoration: 'underline',
                }}>Sign up</button>
              </>
            ) : view === 'signup' ? (
              <>Already have an account?{' '}
                <button onClick={() => { setView('login'); setError(''); }} style={{
                  background: 'none', border: 0, color: 'var(--navy-700)', fontWeight: 500,
                  cursor: 'pointer', fontSize: 14, padding: 4, textDecoration: 'underline',
                }}>Log in</button>
              </>
            ) : (
              <button onClick={() => { setView('login'); setError(''); }} style={{
                background: 'none', border: 0, color: 'var(--navy-700)', fontWeight: 500,
                cursor: 'pointer', fontSize: 14, padding: 4, textDecoration: 'underline',
              }}>← Back to login</button>
            )}
          </div>
          )}

        </div>
      </div>

      <style>{`
        @media (min-width: 900px) {
          .login-mobile-logo { display: none !important; }
        }
      `}</style>
    </div>
  );
}

window.LoginPage = LoginPage;
