{{> header}}

<style>
  .auth-page {
    min-height: calc(100vh - 60px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 60px 40px;
    background: linear-gradient(135deg, #0f172a 0%, #020617 100%);
    position: relative;
  }
  .auth-page::before {
    content: '';
    position: absolute;
    top: -30%;
    right: -10%;
    width: 60%;
    height: 120%;
    background: radial-gradient(ellipse, rgba(16, 185, 129, 0.08) 0%, transparent 60%);
    pointer-events: none;
  }
  .auth-container { width: 100%; max-width: 460px; position: relative; z-index: 1; }
  .auth-card {
    background: rgba(30, 41, 59, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 24px;
    padding: 3rem;
    backdrop-filter: blur(20px);
    box-shadow: 0 25px 80px rgba(0,0,0,0.4);
  }
  .auth-header { text-align: center; margin-bottom: 2rem; }
  .auth-icon {
    width: 80px; height: 80px;
    background: linear-gradient(135deg, #10b981 0%, #6366f1 100%);
    border-radius: 20px;
    display: flex; align-items: center; justify-content: center;
    font-size: 2.25rem;
    margin: 0 auto 1.5rem;
    box-shadow: 0 10px 30px rgba(16, 185, 129, 0.3);
  }
  .auth-header h1 { color: white; font-size: 1.875rem; font-weight: 700; margin-bottom: 0.5rem; }
  .auth-header p { color: #94a3b8; margin: 0; }
  .alert-error {
    background: rgba(239, 68, 68, 0.15);
    border: 1px solid rgba(239, 68, 68, 0.3);
    color: #fca5a5;
    padding: 14px 18px;
    border-radius: 12px;
    margin-bottom: 1.5rem;
    font-size: 0.9rem;
    display: flex; align-items: center; gap: 0.75rem;
  }
  .form-group { margin-bottom: 1.5rem; }
  .form-label { display: block; color: #e2e8f0; font-weight: 500; margin-bottom: 0.5rem; font-size: 0.9rem; }
  .form-input {
    width: 100%;
    padding: 14px 18px;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    color: white;
    font-size: 1rem;
    transition: all 0.2s ease;
  }
  .form-input:focus {
    outline: none;
    border-color: #10b981;
    background: rgba(15, 23, 42, 0.8);
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1);
  }
  .form-input::placeholder { color: #64748b; }
  .btn-submit {
    width: 100%;
    padding: 16px 24px;
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white; border: none; border-radius: 12px;
    font-size: 1rem; font-weight: 600; cursor: pointer;
    transition: all 0.2s ease;
    display: flex; align-items: center; justify-content: center; gap: 0.5rem;
  }
  .btn-submit:hover { transform: translateY(-2px); box-shadow: 0 10px 30px rgba(16, 185, 129, 0.4); }
  .btn-submit:disabled { opacity: 0.7; cursor: not-allowed; transform: none; }
  .auth-divider { display: flex; align-items: center; margin: 2rem 0; color: #64748b; font-size: 0.85rem; }
  .auth-divider::before, .auth-divider::after { content: ''; flex: 1; height: 1px; background: rgba(255, 255, 255, 0.1); }
  .auth-divider span { padding: 0 1rem; }
  .auth-footer { text-align: center; color: #94a3b8; }
  .auth-footer a { color: #10b981; text-decoration: none; font-weight: 600; }
  .auth-footer a:hover { text-decoration: underline; }
  .back-home {
    display: inline-flex; align-items: center; gap: 0.5rem;
    color: #64748b; text-decoration: none; font-size: 0.9rem;
    margin-bottom: 1.5rem; transition: color 0.2s ease;
  }
  .back-home:hover { color: white; }
  .twofa-section { display: none; }
  .twofa-section.active { display: block; }
  .login-section.hidden { display: none; }
  .twofa-info {
    text-align: center; margin-bottom: 1.5rem; padding: 1rem;
    background: rgba(99, 102, 241, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.2);
    border-radius: 12px; color: #a5b4fc; font-size: 0.9rem;
  }
  .code-input { letter-spacing: 0.5em; text-align: center; font-size: 1.5rem; font-weight: 600; }

  /* Mobile Styles */
  @media (max-width: 600px) {
    .auth-page { padding: 40px 15px; }
    .auth-card { padding: 2rem 1.5rem; border-radius: 16px; }
    .auth-icon { width: 64px; height: 64px; font-size: 1.75rem; }
    .auth-header h1 { font-size: 1.5rem; }
    .form-input { padding: 12px 14px; }
    .btn-submit { padding: 14px 20px; }
  }
</style>

<section class="auth-page">
  <div class="auth-container">
    <a href="/" class="back-home">
      <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
      Back to Home
    </a>
    <div class="auth-card">
      <div class="login-section" id="loginSection">
        <div class="auth-header">
          <div class="auth-icon">🔐</div>
          <h1>Welcome Back</h1>
          <p>Sign in to access your account</p>
        </div>
        {{#if error}}<div class="alert-error"><span>⚠️</span><span>{{error}}</span></div>{{/if}}
        <form id="loginForm">
          <div class="form-group">
            <label class="form-label">Email Address</label>
            <input type="email" name="email" id="email" class="form-input" required placeholder="you@example.com">
          </div>
          <div class="form-group">
            <label class="form-label">Password</label>
            <input type="password" name="password" id="password" class="form-input" required placeholder="••••••••">
          </div>
          <button type="submit" class="btn-submit" id="loginBtn"><span>Sign In</span></button>
        </form>
        <div class="auth-divider"><span>New here?</span></div>
        <div class="auth-footer"><p>Don't have an account? <a href="/register">Create one</a></p></div>
      </div>
      <div class="twofa-section" id="twofaSection">
        <div class="auth-header">
          <div class="auth-icon">🔑</div>
          <h1>Two-Factor Auth</h1>
          <p>Enter your verification code</p>
        </div>
        <div class="twofa-info">Enter the 6-digit code from your authenticator app.</div>
        <div class="alert-error" id="twofaError" style="display:none"><span>⚠️</span><span id="twofaErrorMsg"></span></div>
        <form id="twofaForm">
          <div class="form-group">
            <label class="form-label">Verification Code</label>
            <input type="text" id="twofaCode" class="form-input code-input" required placeholder="000000" maxlength="8">
          </div>
          <button type="submit" class="btn-submit" id="twofaBtn"><span>Verify</span></button>
        </form>
        <div class="auth-divider"><span>or</span></div>
        <div class="auth-footer"><p><a href="#" id="backToLogin">← Back to login</a></p></div>
      </div>
    </div>
  </div>
</section>

<script>
(function() {
  var tempToken = null, redirect = '{{redirect}}' || '/';
  var loginForm = document.getElementById('loginForm'), loginSection = document.getElementById('loginSection');
  var twofaSection = document.getElementById('twofaSection'), twofaForm = document.getElementById('twofaForm');

  loginForm.addEventListener('submit', async function(e) {
    e.preventDefault();
    var btn = document.getElementById('loginBtn');
    btn.disabled = true; btn.innerHTML = '<span>Signing in...</span>';
    try {
      var r = await fetch('/api/auth/login', {
        method: 'POST', headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ email: document.getElementById('email').value, password: document.getElementById('password').value }),
        credentials: 'include'
      });
      var data = await r.json();
      if (!r.ok) throw new Error(data.message || 'Login failed');
      if (data.requires2FA) {
        tempToken = data.tempToken;
        loginSection.classList.add('hidden');
        twofaSection.classList.add('active');
        document.getElementById('twofaCode').focus();
      } else {
        if (data.access_token) {
          localStorage.setItem('token', data.access_token);
          document.cookie = 'access_token=' + data.access_token + '; path=/; max-age=' + (7*24*60*60);
        }
        window.location.href = redirect;
      }
    } catch(err) {
      alert(err.message);
      btn.disabled = false; btn.innerHTML = '<span>Sign In</span>';
    }
  });

  twofaForm.addEventListener('submit', async function(e) {
    e.preventDefault();
    var btn = document.getElementById('twofaBtn');
    btn.disabled = true; btn.innerHTML = '<span>Verifying...</span>';
    try {
      var r = await fetch('/api/auth/verify-2fa', {
        method: 'POST', headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ tempToken: tempToken, code: document.getElementById('twofaCode').value }),
        credentials: 'include'
      });
      var data = await r.json();
      if (!r.ok) throw new Error(data.message || 'Invalid code');
      if (data.access_token) {
        localStorage.setItem('token', data.access_token);
        document.cookie = 'access_token=' + data.access_token + '; path=/; max-age=' + (7*24*60*60);
      }
      window.location.href = redirect;
    } catch(err) {
      document.getElementById('twofaError').style.display = 'flex';
      document.getElementById('twofaErrorMsg').textContent = err.message;
      btn.disabled = false; btn.innerHTML = '<span>Verify</span>';
    }
  });

  document.getElementById('backToLogin').addEventListener('click', function(e) {
    e.preventDefault();
    tempToken = null;
    twofaSection.classList.remove('active');
    loginSection.classList.remove('hidden');
  });
})();
</script>

{{> footer}}