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

  // Smooth scroll for nav links
  document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
    anchor.addEventListener('click', function(e) {
      var target = document.querySelector(this.getAttribute('href'));
      if (target) {
        e.preventDefault();
        target.scrollIntoView({ behavior: 'smooth' });
      }
    });
  });

  // Newsletter form submission (placeholder - no backend)
  var newsletterForm = document.querySelector('.newsletter-form');
  if (newsletterForm) {
    newsletterForm.addEventListener('submit', function(e) {
      e.preventDefault();
      var input = this.querySelector('.newsletter-input');
      if (input && input.value) {
        alert('Thank you for subscribing! We\'ll be in touch soon.');
        input.value = '';
      }
    });
  }

  // Topics Carousel
  var topicsTrack = document.querySelector('.topics-track');
  var topicCards = Array.from(document.querySelectorAll('.topic-card'));
  var topicsPrev = document.querySelector('.topics-prev');
  var topicsNext = document.querySelector('.topics-next');
  var topicsCurrent = 0;
  var topicsPerView = 3;

  function getTopicsPerView() {
    return window.innerWidth < 600 ? 1 : window.innerWidth < 900 ? 2 : 3;
  }

  function updateTopicsPerView() {
    topicsPerView = getTopicsPerView();
    topicsCurrent = 0;
    updateTopics();
  }

  function updateTopics() {
    if (!topicCards.length || !topicsTrack) return;
    var max = Math.max(0, topicCards.length - topicsPerView);
    if (topicsCurrent > max) topicsCurrent = max;
    if (topicsCurrent < 0) topicsCurrent = 0;

    var gap = parseFloat(window.getComputedStyle(topicsTrack).columnGap) || 0;
    var cardWidth = topicCards[0].getBoundingClientRect().width;
    var offset = topicsCurrent * (cardWidth + gap);
    topicsTrack.style.transform = 'translateX(-' + offset + 'px)';

    if (topicsPrev) topicsPrev.disabled = topicsCurrent === 0;
    if (topicsNext) topicsNext.disabled = topicsCurrent === max;
  }

  function topicsNextSlide() {
    var max = Math.max(0, topicCards.length - topicsPerView);
    if (topicsCurrent < max) {
      topicsCurrent++;
      updateTopics();
    }
  }

  function topicsPrevSlide() {
    if (topicsCurrent > 0) {
      topicsCurrent--;
      updateTopics();
    }
  }

  if (topicsPrev) topicsPrev.addEventListener('click', topicsPrevSlide);
  if (topicsNext) topicsNext.addEventListener('click', topicsNextSlide);

  window.addEventListener('resize', updateTopicsPerView);
  updateTopicsPerView();

  // Courses Carousel
  var coursesTrack = document.querySelector('.courses-track');
  var courseCards = Array.from(document.querySelectorAll('.course-card'));
  var coursesPrev = document.querySelector('.courses-prev');
  var coursesNext = document.querySelector('.courses-next');
  var coursesDotsContainer = document.querySelector('.courses-dots');
  var coursesCurrent = 0;
  var coursesPerView = 2;

  function getCoursesPerView() {
    return window.innerWidth < 600 ? 1 : 2;
  }

  function renderCoursesDots() {
    if (!coursesDotsContainer || !courseCards.length) return;
    var count = Math.max(0, courseCards.length - coursesPerView + 1);
    coursesDotsContainer.innerHTML = '';
    for (var i = 0; i < count; i++) {
      var dot = document.createElement('button');
      dot.type = 'button';
      dot.className = 'course-dot';
      dot.setAttribute('aria-label', 'Go to slide ' + (i + 1));
      dot.addEventListener('click', function(idx) {
        return function() {
          coursesCurrent = idx;
          updateCourses();
        };
      }(i));
      coursesDotsContainer.appendChild(dot);
    }
    updateCourses();
  }

  function updateCourses() {
    if (!courseCards.length || !coursesTrack) return;
    var max = Math.max(0, courseCards.length - coursesPerView);
    if (coursesCurrent > max) coursesCurrent = max;
    if (coursesCurrent < 0) coursesCurrent = 0;

    var gap = parseFloat(window.getComputedStyle(coursesTrack).columnGap) || 0;
    var cardWidth = courseCards[0].getBoundingClientRect().width;
    var offset = coursesCurrent * (cardWidth + gap);
    coursesTrack.style.transform = 'translateX(-' + offset + 'px)';

    if (coursesPrev) coursesPrev.disabled = coursesCurrent === 0;
    if (coursesNext) coursesNext.disabled = coursesCurrent === max;

    var dots = Array.from(document.querySelectorAll('.course-dot'));
    dots.forEach(function(dot, index) {
      dot.classList.toggle('active', index === coursesCurrent);
    });
  }

  function coursesNextSlide() {
    var max = Math.max(0, courseCards.length - coursesPerView);
    if (coursesCurrent < max) {
      coursesCurrent++;
      updateCourses();
    }
  }

  function coursesPrevSlide() {
    if (coursesCurrent > 0) {
      coursesCurrent--;
      updateCourses();
    }
  }

  if (coursesPrev) coursesPrev.addEventListener('click', coursesPrevSlide);
  if (coursesNext) coursesNext.addEventListener('click', coursesNextSlide);

  window.addEventListener('resize', function() {
    coursesPerView = getCoursesPerView();
    coursesCurrent = 0;
    renderCoursesDots();
    updateCourses();
  });

  renderCoursesDots();

  

});
document.addEventListener("DOMContentLoaded", function() {
    const wellnessTabs = document.querySelectorAll(".slider-tabs-nav .tab-nav-btn");
    const wellnessPanels = document.querySelectorAll(".slider-panels-window .slide-tab-panel");

    wellnessTabs.forEach(tab => {
        tab.addEventListener("click", () => {
            const targetSlide = tab.getAttribute("data-slide");

            // Handle Tab Highlight States
            wellnessTabs.forEach(t => t.classList.remove("active"));
            tab.classList.add("active");

            // Handle Panel Visibility
            wellnessPanels.forEach(panel => {
                panel.classList.remove("active");
            });

            const matchingPanel = document.getElementById(`slide-${targetSlide}`);
            if (matchingPanel) {
                matchingPanel.classList.add("active");
            }
    });
  });
});

// === Course Sales Carousels ===
function initCarousel(containerSel, getPerView) {
  var container = document.querySelector(containerSel);
  if (!container) return;

  var track = container.querySelector('.carousel-track');
  var cards = Array.from(track.querySelectorAll('.carousel-card'));
  var prevBtn = container.querySelector('.carousel-prev');
  var nextBtn = container.querySelector('.carousel-next');
  var dotsContainer = container.querySelector('.carousel-dots');
  var current = 0;
  var perView = 1;

  function calcPerView() {
    perView = typeof getPerView === 'function' ? getPerView() : (getPerView || 3);
  }

  function renderDots() {
    if (!dotsContainer || !cards.length) return;
    var count = Math.max(1, cards.length - perView + 1);
    dotsContainer.innerHTML = '';
    for (var i = 0; i < count; i++) {
      (function(idx) {
        var dot = document.createElement('button');
        dot.type = 'button';
        dot.className = 'carousel-dot';
        dot.setAttribute('aria-label', 'Go to slide ' + (idx + 1));
        dot.addEventListener('click', function() { goTo(idx); });
        dotsContainer.appendChild(dot);
      })(i);
    }
    update();
  }

  function update() {
    if (!cards.length) return;
    var max = Math.max(0, cards.length - perView);
    if (current > max) current = max;
    if (current < 0) current = 0;

    var gap = parseFloat(window.getComputedStyle(track).columnGap) || 0;
    var cardWidth = cards[0].getBoundingClientRect().width;
    var offset = current * (cardWidth + gap);
    track.style.transform = 'translateX(-' + offset + 'px)';

    if (prevBtn) prevBtn.disabled = current === 0;
    if (nextBtn) nextBtn.disabled = current >= max;

    if (dotsContainer) {
      var dots = Array.from(dotsContainer.querySelectorAll('.carousel-dot'));
      dots.forEach(function(dot, i) { dot.classList.toggle('active', i === current); });
    }
  }

  function goTo(index) {
    current = index;
    update();
  }

  function next() {
    var max = Math.max(0, cards.length - perView);
    if (current < max) { current++; update(); }
  }

  function prev() {
    if (current > 0) { current--; update(); }
  }

  function onResize() {
    calcPerView();
    current = 0;
    renderDots();
    update();
  }

  if (prevBtn) prevBtn.addEventListener('click', prev);
  if (nextBtn) nextBtn.addEventListener('click', next);
  window.addEventListener('resize', onResize);
  calcPerView();
  renderDots();
}

document.addEventListener('DOMContentLoaded', function() {
  initCarousel('.glance-grid.carousel', function() {
    return window.innerWidth < 600 ? 2 : window.innerWidth < 900 ? 3 : 4;
  });
  initCarousel('.transformation-stories.carousel', function() {
    return window.innerWidth < 768 ? 1 : 2;
  });
  initCarousel('.course-grid.carousel', function() {
    return window.innerWidth < 600 ? 1 : window.innerWidth < 768 ? 2 : 3;
  });
});
