/* ============================================================
   TAHWISSA v2 — Main JavaScript
   ============================================================ */
(function () {
  'use strict';

  /* --- Nav scroll --- */
  const nav = document.getElementById('mainNav');
  if (nav) {
    function updateNav() { nav.classList.toggle('scrolled', window.scrollY > 40); }
    window.addEventListener('scroll', updateNav, { passive: true });
    updateNav();
  }

  /* --- Mobile menu --- */
  const hamburger  = document.getElementById('hamburger');
  const mobileMenu = document.getElementById('mobileMenu');
  if (hamburger && mobileMenu) {
    hamburger.addEventListener('click', () => {
      const isOpen = hamburger.classList.toggle('open');
      mobileMenu.classList.toggle('open', isOpen);
      document.body.style.overflow = isOpen ? 'hidden' : '';
      hamburger.setAttribute('aria-expanded', isOpen);
    });
    mobileMenu.addEventListener('click', e => {
      if (e.target === mobileMenu) {
        hamburger.classList.remove('open');
        mobileMenu.classList.remove('open');
        document.body.style.overflow = '';
      }
    });
    mobileMenu.querySelectorAll('a[href^="#"]').forEach(a => {
      a.addEventListener('click', () => {
        hamburger.classList.remove('open');
        mobileMenu.classList.remove('open');
        document.body.style.overflow = '';
      });
    });
  }

  /* --- Smooth scroll --- */
  document.querySelectorAll('a[href^="#"]').forEach(a => {
    a.addEventListener('click', e => {
      const id = a.getAttribute('href').slice(1);
      const el = document.getElementById(id);
      if (el) {
        e.preventDefault();
        const top = el.getBoundingClientRect().top + window.scrollY - 70;
        window.scrollTo({ top, behavior: 'smooth' });
      }
    });
  });

  /* --- Reveal on scroll --- */
  const reveals = document.querySelectorAll('.reveal');
  if ('IntersectionObserver' in window && reveals.length) {
    const obs = new IntersectionObserver(entries => {
      entries.forEach(e => { if (e.isIntersecting) { e.target.classList.add('visible'); obs.unobserve(e.target); } });
    }, { threshold: 0.1, rootMargin: '0px 0px -40px 0px' });
    reveals.forEach(el => obs.observe(el));
  } else {
    reveals.forEach(el => el.classList.add('visible'));
  }

  /* --- Contact form --- */
  const form    = document.getElementById('contactForm');
  const success = document.getElementById('formSuccess');
  if (form) {
    form.addEventListener('submit', e => {
      e.preventDefault();
      const btn = form.querySelector('.form-submit');
      btn.disabled = true; btn.style.opacity = '.65';
      fetch('contact.php', { method: 'POST', body: new FormData(form) })
        .then(r => r.json())
        .then(res => {
          btn.disabled = false; btn.style.opacity = '1';
          if (res.ok) {
            form.reset();
            if (success) { success.classList.add('show'); setTimeout(() => success.classList.remove('show'), 6000); }
          } else {
            alert('يرجى ملء جميع الحقول بشكل صحيح.');
          }
        })
        .catch(() => { btn.disabled = false; btn.style.opacity = '1'; alert('خطأ في الاتصال، حاول مجدداً.'); });
    });
  }

})();
