// NAV SCROLL
const nav = document.getElementById('mainNav');
window.addEventListener('scroll', () => {
  nav.classList.toggle('scrolled', window.scrollY > 40);
});

// HAMBURGER
const hamburger = document.getElementById('hamburger');
const mobileNav = document.getElementById('mobileNav');
const mobileClose = document.getElementById('mobileClose');
if (hamburger) {
  hamburger.addEventListener('click', () => mobileNav.classList.add('open'));
  mobileClose.addEventListener('click', () => mobileNav.classList.remove('open'));
}
window.closeMobileNav = () => { if(mobileNav) mobileNav.classList.remove('open'); };

// FADE UP ON SCROLL
const fadeEls = document.querySelectorAll('.fade-up');
const io = new IntersectionObserver((entries) => {
  entries.forEach((entry, i) => {
    if (entry.isIntersecting) {
      setTimeout(() => entry.target.classList.add('visible'), i * 80);
      io.unobserve(entry.target);
    }
  });
}, { threshold: 0.1 });
fadeEls.forEach(el => io.observe(el));

// COUNTER ANIMATION
function animateCount(el) {
  const target = parseInt(el.dataset.count);
  const suffix = el.textContent.includes('%') ? '%' : '+';
  const duration = 1800;
  const start = performance.now();
  function update(now) {
    const elapsed = now - start;
    const progress = Math.min(elapsed / duration, 1);
    const ease = 1 - Math.pow(1 - progress, 3);
    el.textContent = Math.round(target * ease) + suffix;
    if (progress < 1) requestAnimationFrame(update);
  }
  requestAnimationFrame(update);
}
const statsSection = document.getElementById('stats');
if (statsSection) {
  const statsObserver = new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting) {
      document.querySelectorAll('.stat-number').forEach(animateCount);
      statsObserver.disconnect();
    }
  }, { threshold: 0.3 });
  statsObserver.observe(statsSection);
}

// PROJECT FILTERS
window.filterProjects = function(cat, btn) {
  document.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active'));
  btn.classList.add('active');
  document.querySelectorAll('.project-card').forEach(card => {
    const show = cat === 'all' || card.dataset.cat === cat;
    card.style.display = show ? 'block' : 'none';
    if (show) card.style.opacity = '1';
  });
};

// FORM SUBMIT
window.submitForm = function() {
  const name = document.getElementById('fname')?.value.trim();
  const email = document.getElementById('femail')?.value.trim();
  const msg = document.getElementById('fmsg')?.value.trim();
  const consent = document.getElementById('fconsent')?.checked;
  const feedback = document.getElementById('form-msg');
  if (!name || !email || !msg || !consent) {
    if(feedback) {
      feedback.style.display = 'block';
      feedback.style.color = '#F09595';
      feedback.textContent = 'Please fill in all required fields and accept the consent checkbox.';
    }
    return;
  }
  if(feedback) {
    feedback.style.display = 'block';
    feedback.style.color = 'var(--gold)';
    feedback.textContent = '✓ Thank you! Your enquiry has been sent. We\'ll be in touch within 24 hours.';
  }
  const fnameField = document.getElementById('fname');
  const femailField = document.getElementById('femail');
  const fphoneField = document.getElementById('fphone');
  const fmsgField = document.getElementById('fmsg');
  const fconsentField = document.getElementById('fconsent');
  if(fnameField) fnameField.value = '';
  if(femailField) femailField.value = '';
  if(fphoneField) fphoneField.value = '';
  if(fmsgField) fmsgField.value = '';
  if(fconsentField) fconsentField.checked = false;
};