document.addEventListener('DOMContentLoaded', () => {
  const form = document.getElementById('auth-form');
  const errorEl = document.getElementById('auth-error');
  const isRegister = document.body.dataset.page === 'register';

  form.addEventListener('submit', async (e) => {
    e.preventDefault();
    errorEl.textContent = '';
    const email = document.getElementById('email').value.trim();
    const password = document.getElementById('password').value;
    const endpoint = isRegister ? '/api/auth/register' : '/api/auth/login';

    try {
      await API.request(endpoint, {
        method: 'POST',
        body: JSON.stringify({ email, password }),
      });
      window.location.href = '/';
    } catch (err) {
      errorEl.textContent = err.message;
    }
  });
});
