document.addEventListener('DOMContentLoaded', () => {
  const revealItems = document.querySelectorAll('[data-reveal]');
  const observer = new IntersectionObserver((entries) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        entry.target.classList.add('is-visible');
        observer.unobserve(entry.target);
      }
    });
  }, { threshold: 0.16 });
  revealItems.forEach((item) => observer.observe(item));

  document.querySelectorAll('.faq-question').forEach((button) => {
    button.addEventListener('click', () => {
      const item = button.closest('.faq-item');
      item.classList.toggle('active');
      document.querySelectorAll('.faq-item').forEach((other) => {
        if (other !== item) other.classList.remove('active');
      });
    });
  });

  const form = document.querySelector('.lead-form');
  const status = document.querySelector('.form-status');
  if (form) {
    form.addEventListener('submit', async (e) => {
      e.preventDefault();
      const button = form.querySelector('button[type="submit"]');
      const original = button.textContent;
      button.textContent = 'Отправка...';
      button.disabled = true;
      status.textContent = '';
      try {
        const response = await fetch('send.php', { method: 'POST', body: new FormData(form) });
        const result = await response.json();
        status.textContent = result.message || 'Готово';
        status.className = 'form-status ' + (result.success ? 'success' : 'error');
        if (result.success) form.reset();
      } catch (error) {
        status.textContent = 'Ошибка соединения. Попробуйте еще раз.';
        status.className = 'form-status error';
      } finally {
        button.textContent = original;
        button.disabled = false;
      }
    });
  }
});
