document.addEventListener('DOMContentLoaded', function () {
  var navbar = document.getElementById('navbar');
  if (navbar) {
    function onScroll() {
      navbar.classList.toggle('scrolled', window.scrollY > 60);
    }
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
  }
  var ham = document.getElementById('hamburger');
  var mob = document.getElementById('mobile-menu');
  if (ham && mob) {
    ham.addEventListener('click', function () {
      var isOpen = mob.classList.toggle('open');
      ham.classList.toggle('open', isOpen);
      document.body.style.overflow = isOpen ? 'hidden' : '';
    });
    mob.querySelectorAll('a').forEach(function (a) {
      a.addEventListener('click', function () {
        mob.classList.remove('open');
        ham.classList.remove('open');
        document.body.style.overflow = '';
      });
    });
    mob.querySelectorAll('.mob-drop-toggle').forEach(function (toggle) {
      toggle.addEventListener('click', function (e) {
        e.preventDefault();
        e.stopPropagation();
        var grp = toggle.closest('.mob-drop-group');
        var wasOpen = grp.classList.contains('open');
        mob.querySelectorAll('.mob-drop-group').forEach(function (g) {
          g.classList.remove('open');
        });
        if (!wasOpen) grp.classList.add('open');
      });
    });
  }
  var leaveTimers = [];
  document.querySelectorAll('.has-dropdown').forEach(function (item) {
    var menu = item.querySelector('.dropdown-menu');
    if (!menu) return;
    var timer = null;
    item.addEventListener('mouseenter', function () {
      clearTimeout(timer);
      document.querySelectorAll('.has-dropdown.dd-open').forEach(function (other) {
        if (other !== item) other.classList.remove('dd-open');
      });
      item.classList.add('dd-open');
    });
    item.addEventListener('mouseleave', function () {
      timer = setTimeout(function () {
        item.classList.remove('dd-open');
      }, 200);
    });
    menu.addEventListener('mouseenter', function () { clearTimeout(timer); });
    menu.addEventListener('mouseleave', function () {
      timer = setTimeout(function () {
        item.classList.remove('dd-open');
      }, 200);
    });
  });
  var fadeEls = document.querySelectorAll('.fade-up, .fade-in');
  if (window.IntersectionObserver) {
    var io = new IntersectionObserver(function (entries) {
      entries.forEach(function (e) {
        if (e.isIntersecting) { e.target.classList.add('visible'); io.unobserve(e.target); }
      });
    }, { threshold: 0.08, rootMargin: '0px 0px -30px 0px' });
    fadeEls.forEach(function (el) { io.observe(el); });
  } else {
    fadeEls.forEach(function (el) { el.classList.add('visible'); });
  }
  var gfBtns   = document.querySelectorAll('.gf-btn');
  var galItems = document.querySelectorAll('.gallery-item');
  gfBtns.forEach(function (btn) {
    btn.addEventListener('click', function () {
      gfBtns.forEach(function (b) { b.classList.remove('active'); });
      btn.classList.add('active');
      var f = btn.getAttribute('data-f') || 'all';
      galItems.forEach(function (item) {
        item.classList.toggle('hidden', f !== 'all' && item.getAttribute('data-f') !== f);
      });
    });
  });
  var lb    = document.getElementById('lightbox');
  var lbImg = document.getElementById('lbImg');
  if (lb && lbImg) {
    var visible = [], cur = 0;
    function getVisible() {
      return Array.from(galItems).filter(function (i) { return !i.classList.contains('hidden'); });
    }
    function showSlide() {
      var el = visible[cur];
      if (!el) return;
      lbImg.src = el.querySelector('img').src;
      var lbCat  = document.getElementById('lbCat');
      var lbName = document.getElementById('lbName');
      if (lbCat)  lbCat.textContent = el.getAttribute('data-cat')  || '';
      if (lbName) lbName.innerHTML  = el.getAttribute('data-name') || '';
    }
    galItems.forEach(function (el) {
      el.addEventListener('click', function () {
        visible = getVisible();
        cur = Array.from(galItems).indexOf(el);
        showSlide();
        lb.classList.add('open');
      });
    });
    var lbClose = document.getElementById('lbClose');
    var lbPrev  = document.getElementById('lbPrev');
    var lbNext  = document.getElementById('lbNext');
    if (lbClose) lbClose.addEventListener('click', function () { lb.classList.remove('open'); });
    if (lbPrev)  lbPrev.addEventListener('click',  function () { cur=(cur-1+visible.length)%visible.length; showSlide(); });
    if (lbNext)  lbNext.addEventListener('click',  function () { cur=(cur+1)%visible.length; showSlide(); });
    lb.addEventListener('click', function (e) { if (e.target===lb) lb.classList.remove('open'); });
    document.addEventListener('keydown', function (e) {
      if (!lb.classList.contains('open')) return;
      if (e.key==='Escape')     lb.classList.remove('open');
      if (e.key==='ArrowLeft')  { cur=(cur-1+visible.length)%visible.length; showSlide(); }
      if (e.key==='ArrowRight') { cur=(cur+1)%visible.length; showSlide(); }
    });
  }
  var bookForm = document.getElementById('bookingForm');
  if (bookForm) {
    bookForm.addEventListener('submit', function (e) {
      e.preventDefault();
      function val(id) { var el=document.getElementById(id); return el?el.value:''; }
      var msg = 'Hi Toni & Guy Coimbatore!\n' +
                'Name: '    + val('fname')    + '\n' +
                'Phone: '   + val('fphone')   + '\n' +
                'Service: ' + val('fservice') + '\n' +
                'Date: '    + (val('fdate')||'Flexible') + '\n' +
                'Time: '    + (val('ftime')||'Flexible') + '\n' +
                (val('fnotes')?'Notes: '+val('fnotes'):'');
      window.open('https://wa.me/919894333000?text='+encodeURIComponent(msg),'_blank');
    });
  }
  var ticker = document.querySelector('.ticker-inner');
  if (ticker) ticker.innerHTML += ticker.innerHTML;
  document.querySelectorAll('.tab-btn').forEach(function (btn) {
    btn.addEventListener('click', function () {
      var t = btn.getAttribute('data-tab');
      document.querySelectorAll('.tab-btn').forEach(function(b){b.classList.remove('active');});
      document.querySelectorAll('.tab-content').forEach(function(c){c.classList.remove('active');});
      btn.classList.add('active');
      var tc = document.getElementById(t);
      if (tc) tc.classList.add('active');
    });
  });
});