/* =============================================
   MindCare — Main JavaScript
   Shared across all pages
   ============================================= */

/* ── Navbar scroll ── */
(function () {
  const nav = document.getElementById('navbar');
  if (!nav) return;
  function onScroll() { nav.classList.toggle('scrolled', window.scrollY > 50); }
  window.addEventListener('scroll', onScroll, { passive: true });
  onScroll();
})();

/* ── Mobile hamburger ── */
function _applyHamburger(open, btn, spans) {
  if (open) {
    spans[0].style.transform = 'translateY(7.5px) rotate(45deg)';
    spans[1].style.opacity   = '0';
    spans[2].style.transform = 'translateY(-7.5px) rotate(-45deg)';
  } else {
    spans.forEach(s => { s.style.transform = ''; s.style.opacity = ''; });
  }
}

/* Global toggleMenu — called by inline onclick on some pages */
window.toggleMenu = function () {
  const btn  = document.getElementById('hamburger');
  const menu = document.getElementById('mobileMenu');
  if (!btn || !menu) return;
  const open = menu.classList.toggle('open');
  btn.classList.toggle('active', open);
  _applyHamburger(open, btn, btn.querySelectorAll('span'));
};

(function () {
  const btn  = document.getElementById('hamburger');
  const menu = document.getElementById('mobileMenu');
  if (!btn || !menu) return;

  /* Attach click handler — main.js is the single source of truth for all pages */
  btn.addEventListener('click', window.toggleMenu);

  /* Close menu when any nav link is clicked */
  menu.querySelectorAll('a').forEach(a => a.addEventListener('click', () => {
    menu.classList.remove('open');
    btn.classList.remove('active');
    _applyHamburger(false, btn, btn.querySelectorAll('span'));
  }));

  /* Close on outside tap */
  document.addEventListener('click', e => {
    if (menu.classList.contains('open') && !menu.contains(e.target) && !btn.contains(e.target)) {
      menu.classList.remove('open');
      btn.classList.remove('active');
      _applyHamburger(false, btn, btn.querySelectorAll('span'));
    }
  });

  /* Close menu on resize to desktop */
  window.addEventListener('resize', () => {
    if (window.innerWidth > 900 && menu.classList.contains('open')) {
      menu.classList.remove('open');
      btn.classList.remove('active');
      _applyHamburger(false, btn, btn.querySelectorAll('span'));
    }
  });
})();

/* ── Active nav link ── */
(function () {
  const path = window.location.pathname.split('/').pop() || 'index.html';
  document.querySelectorAll('.nav-links a, .mobile-menu a').forEach(a => {
    const href = a.getAttribute('href');
    if (href === path || (path === '' && href === 'index.html')) {
      a.classList.add('active');
    } else {
      a.classList.remove('active');
    }
  });
})();

/* ── Scroll reveal ── */
(function () {
  const io = new IntersectionObserver(entries => {
    entries.forEach(e => { if (e.isIntersecting) { e.target.classList.add('on'); } });
  }, { threshold: 0.10 });
  document.querySelectorAll('.rv, .rv-l, .rv-r').forEach(el => io.observe(el));
})();

/* ── Counter animation ── */
(function () {
  function runCounter(el) {
    if (el.dataset.done) return;
    el.dataset.done = '1';
    const target = parseInt(el.dataset.count);
    const sfx = el.querySelector('span') ? el.querySelector('span').outerHTML : '';
    let c = 0;
    const step = target / (1600 / 16);
    const t = setInterval(() => {
      c = Math.min(c + step, target);
      el.innerHTML = Math.floor(c).toLocaleString() + sfx;
      if (c >= target) clearInterval(t);
    }, 16);
  }
  const co = new IntersectionObserver(entries => {
    entries.forEach(e => { if (e.isIntersecting) runCounter(e.target); });
  }, { threshold: 0.5 });
  document.querySelectorAll('[data-count]').forEach(el => co.observe(el));
})();

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

/* ── Page transition fade-in ── */
(function () {
  document.body.style.opacity = '0';
  document.body.style.transition = 'opacity .45s ease';
  window.addEventListener('load', () => {
    requestAnimationFrame(() => { document.body.style.opacity = '1'; });
  });
  // fade out on navigation
  document.querySelectorAll('a').forEach(a => {
    if (a.href && !a.href.startsWith('#') && a.hostname === location.hostname) {
      a.addEventListener('click', e => {
        if (e.ctrlKey || e.metaKey || e.shiftKey) return;
        e.preventDefault();
        const href = a.href;
        document.body.style.opacity = '0';
        setTimeout(() => { window.location.href = href; }, 300);
      });
    }
  });
})();

/* ── Button ripple effect ── */
document.addEventListener('click', function (e) {
  const btn = e.target.closest('.btn-hp, .btn-wh, .nav-cta, .cs-cta');
  if (!btn) return;
  const ripple = document.createElement('span');
  const rect = btn.getBoundingClientRect();
  const size = Math.max(rect.width, rect.height);
  ripple.style.cssText = `
    position:absolute;width:${size}px;height:${size}px;border-radius:50%;
    background:rgba(255,255,255,.35);transform:scale(0);
    left:${e.clientX - rect.left - size/2}px;top:${e.clientY - rect.top - size/2}px;
    animation:ripple .6s linear;pointer-events:none;
  `;
  btn.style.position = 'relative';
  btn.style.overflow = 'hidden';
  btn.appendChild(ripple);
  setTimeout(() => ripple.remove(), 700);
});

// inject ripple keyframe
const style = document.createElement('style');
style.textContent = `@keyframes ripple{to{transform:scale(4);opacity:0}}`;
document.head.appendChild(style);
