/* Budget Tree Removal — main.js */

// ── Mobile nav toggle ──────────────────────────────────────────────
const menuToggle = document.getElementById('menuToggle');
const nav = document.getElementById('mainNav');
if (menuToggle && nav) {
  menuToggle.addEventListener('click', () => {
    nav.classList.toggle('open');
    const open = nav.classList.contains('open');
    menuToggle.setAttribute('aria-expanded', open);
    menuToggle.querySelectorAll('span').forEach((s, i) => {
      if (open) {
        if (i === 0) s.style.transform = 'rotate(45deg) translate(5px, 5px)';
        if (i === 1) s.style.opacity = '0';
        if (i === 2) s.style.transform = 'rotate(-45deg) translate(5px, -5px)';
      } else {
        s.style.transform = '';
        s.style.opacity = '';
      }
    });
  });
}

// Mobile dropdown toggles
document.querySelectorAll('.nav-link[data-dropdown]').forEach(link => {
  link.addEventListener('click', (e) => {
    if (window.innerWidth <= 768) {
      e.preventDefault();
      const id = link.getAttribute('data-dropdown');
      const dd = document.getElementById(id);
      if (dd) dd.classList.toggle('open');
    }
  });
});

// Close nav when clicking outside
document.addEventListener('click', (e) => {
  if (nav && !nav.contains(e.target) && menuToggle && !menuToggle.contains(e.target)) {
    nav.classList.remove('open');
  }
});

// ── FAQ accordion ──────────────────────────────────────────────────

document.querySelectorAll('.faq-question').forEach(q => {
  q.addEventListener('click', () => {
    const item = q.closest('.faq-item');
    const answer = item.querySelector('.faq-answer');
    const isOpen = q.classList.contains('open');

    // Close all
    document.querySelectorAll('.faq-question.open').forEach(oq => {
      oq.classList.remove('open');
      oq.closest('.faq-item').querySelector('.faq-answer').classList.remove('open');
    });

    // Open this one if it was closed
    if (!isOpen) {
      q.classList.add('open');
      answer.classList.add('open');
    }
  });
});

// ── Smooth scroll for anchor links ────────────────────────────────
document.querySelectorAll('a[href^="#"]').forEach(a => {
  a.addEventListener('click', e => {
    const id = a.getAttribute('href').slice(1);
    const target = document.getElementById(id);
    if (target) {
      e.preventDefault();
      target.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
  });
});

// ── Sticky header shadow ───────────────────────────────────────────
const header = document.getElementById('header');
if (header) {
  window.addEventListener('scroll', () => {
    header.style.boxShadow = window.scrollY > 10
      ? '0 4px 20px rgba(0,0,0,0.12)'
      : '0 2px 12px rgba(0,0,0,0.09)';
  });
}
