document.addEventListener('DOMContentLoaded', function () {

  var prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

  /* ============================================
     HAMBURGER MORPH
     ============================================ */
  var hamburger = document.querySelector('.hamburger');
  var overlay = document.querySelector('.nav-overlay');
  var overlayLinks = document.querySelectorAll('.nav-overlay a');

  if (hamburger && overlay) {
    hamburger.addEventListener('click', function () {
      var isOpen = this.classList.toggle('active');
      overlay.classList.toggle('open');
      this.setAttribute('aria-expanded', isOpen);
      document.body.style.overflow = isOpen ? 'hidden' : '';
    });

    overlayLinks.forEach(function (link) {
      link.addEventListener('click', function () {
        hamburger.classList.remove('active');
        overlay.classList.remove('open');
        hamburger.setAttribute('aria-expanded', 'false');
        document.body.style.overflow = '';
      });
    });
  }

  /* ============================================
     SCROLL REVEAL — IntersectionObserver
     ============================================ */
  var revealElements = document.querySelectorAll('.reveal');

  if (!prefersReducedMotion && revealElements.length && 'IntersectionObserver' in window) {
    var observer = new IntersectionObserver(function (entries) {
      entries.forEach(function (entry) {
        if (entry.isIntersecting) {
          entry.target.classList.add('visible');
          observer.unobserve(entry.target);
        }
      });
    }, {
      threshold: 0.08,
      rootMargin: '0px 0px -40px 0px'
    });

    revealElements.forEach(function (el) {
      observer.observe(el);
    });
  } else {
    revealElements.forEach(function (el) {
      el.classList.add('visible');
    });
  }

  /* ============================================
     FLUID NAV — Scroll shadow
     ============================================ */
  var navInner = document.querySelector('.fluid-nav-inner');
  if (navInner) {
    var scrollHandler = function () {
      if (window.scrollY > 20) {
        navInner.style.boxShadow = '0 8px 40px rgba(0,0,0,0.06)';
      } else {
        navInner.style.boxShadow = '0 4px 32px rgba(0,0,0,0.04)';
      }
    };
    window.addEventListener('scroll', scrollHandler, { passive: true });
  }

  /* ============================================
     GALLERY LIGHTBOX
     ============================================ */
  var modalOverlay = document.getElementById('galleryModal');
  if (modalOverlay) {
    var modalImg = modalOverlay.querySelector('img');
    var modalTitle = modalOverlay.querySelector('.modal-title');
    var modalClose = modalOverlay.querySelector('.modal-close');

    document.querySelectorAll('.gallery-item').forEach(function (item) {
      item.addEventListener('click', function () {
        var src = this.getAttribute('data-full');
        var title = this.getAttribute('data-title') || '';
        if (modalImg) modalImg.setAttribute('src', src);
        if (modalTitle) modalTitle.textContent = title;
        modalOverlay.classList.add('open');
        document.body.style.overflow = 'hidden';
      });
    });

    function closeModal() {
      modalOverlay.classList.remove('open');
      document.body.style.overflow = '';
    }

    if (modalClose) modalClose.addEventListener('click', closeModal);
    modalOverlay.addEventListener('click', function (e) {
      if (e.target === this) closeModal();
    });
    document.addEventListener('keydown', function (e) {
      if (e.key === 'Escape' && modalOverlay.classList.contains('open')) closeModal();
    });
  }

  /* ============================================
     AUTO-DISMISS ALERTS
     ============================================ */
  document.querySelectorAll('.alert-dismissible').forEach(function (el) {
    setTimeout(function () {
      el.style.transition = 'opacity 500ms ease';
      el.style.opacity = '0';
      setTimeout(function () { el.remove(); }, 500);
    }, 5000);
  });

  /* ============================================
     SMOOTH SCROLL FOR ANCHOR LINKS
     ============================================ */
  document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
    anchor.addEventListener('click', function (e) {
      var targetId = this.getAttribute('href');
      if (targetId === '#') return;
      var target = document.querySelector(targetId);
      if (target) {
        e.preventDefault();
        target.scrollIntoView({
          behavior: prefersReducedMotion ? 'auto' : 'smooth',
          block: 'start'
        });
      }
    });
  });

  /* ============================================
     HERO SLIDER — Fade
     ============================================ */
  (function () {
    var sliderEl = document.getElementById('heroSlider');
    if (!sliderEl) return;

    var slides = sliderEl.querySelectorAll('.slide');
    var dotsContainer = sliderEl.querySelector('.slider-dots');
    var prevBtn = sliderEl.querySelector('.prev');
    var nextBtn = sliderEl.querySelector('.next');
    var current = 0;
    var total = slides.length;
    var timer = null;
    var delay = 6000;
    var reduced = prefersReducedMotion;

    if (total < 2) return;

    for (var i = 0; i < total; i++) {
      var btn = document.createElement('button');
      btn.setAttribute('aria-label', 'Go to slide ' + (i + 1));
      if (i === 0) btn.classList.add('active');
      (function (idx) {
        btn.addEventListener('click', function () {
          stopAutoplay();
          goTo(idx);
          startAutoplay();
        });
      })(i);
      dotsContainer.appendChild(btn);
    }

    var dots = dotsContainer.querySelectorAll('button');

    function goTo(index) {
      if (index < 0) index = total - 1;
      if (index >= total) index = 0;
      if (index === current) return;
      slides[current].classList.remove('active');
      dots[current].classList.remove('active');
      slides[index].classList.add('active');
      dots[index].classList.add('active');
      current = index;
    }

    function next() { goTo(current + 1); }
    function prev() { goTo(current - 1); }

    function startAutoplay() {
      if (reduced) return;
      stopAutoplay();
      timer = setInterval(next, delay);
    }

    function stopAutoplay() {
      if (timer) { clearInterval(timer); timer = null; }
    }

    if (prevBtn) prevBtn.addEventListener('click', function () {
      stopAutoplay(); prev(); startAutoplay();
    });
    if (nextBtn) nextBtn.addEventListener('click', function () {
      stopAutoplay(); next(); startAutoplay();
    });

    sliderEl.addEventListener('mouseenter', stopAutoplay);
    sliderEl.addEventListener('mouseleave', startAutoplay);

    var touchStartX = 0;
    sliderEl.addEventListener('touchstart', function (e) {
      touchStartX = e.changedTouches[0].screenX;
    }, { passive: true });
    sliderEl.addEventListener('touchend', function (e) {
      var diff = e.changedTouches[0].screenX - touchStartX;
      if (Math.abs(diff) > 50) {
        stopAutoplay();
        if (diff < 0) next(); else prev();
        startAutoplay();
      }
    }, { passive: true });

    document.addEventListener('keydown', function (e) {
      if (e.key === 'ArrowLeft') { stopAutoplay(); prev(); startAutoplay(); }
      if (e.key === 'ArrowRight') { stopAutoplay(); next(); startAutoplay(); }
    });

    startAutoplay();
  })();

});
