document.addEventListener('DOMContentLoaded', () => {
    // --- MOBILE NAVIGATION TOGGLE ---
    const navToggle = document.querySelector('.nav-toggle');
    const mainNavList = document.getElementById('main-nav-list');

    if (navToggle && mainNavList) {
        navToggle.addEventListener('click', () => {
            const isExpanded = navToggle.getAttribute('aria-expanded') === 'true' || false;
            navToggle.setAttribute('aria-expanded', !isExpanded);
            mainNavList.classList.toggle('nav--open'); // You'll need to define .nav--open in CSS

            // Toggle icon
            const icon = navToggle.querySelector('i');
            if (icon) {
                if (!isExpanded) {
                    icon.classList.remove('fa-bars');
                    icon.classList.add('fa-times');
                    icon.setAttribute('aria-label', 'Chiudi menu');
                } else {
                    icon.classList.remove('fa-times');
                    icon.classList.add('fa-bars');
                    icon.setAttribute('aria-label', 'Apri menu');
                }
            }
        });

        // Close nav when a link is clicked (for single-page sites)
        const navLinks = mainNavList.querySelectorAll('.nav__link');
        navLinks.forEach(link => {
            link.addEventListener('click', () => {
                if (mainNavList.classList.contains('nav--open')) {
                    navToggle.setAttribute('aria-expanded', 'false');
                    mainNavList.classList.remove('nav--open');
                    const icon = navToggle.querySelector('i');
                     if (icon) {
                        icon.classList.remove('fa-times');
                        icon.classList.add('fa-bars');
                        icon.setAttribute('aria-label', 'Apri menu');
                    }
                }
            });
        });
    }

    // --- SWIPER INITIALIZATION FOR TESTIMONIALS ---
    if (typeof Swiper !== 'undefined') {
        const testimonialSwiper = new Swiper('.testimonial-swiper', {
            loop: true,
            slidesPerView: 1,
            spaceBetween: 30,
            autoplay: {
                delay: 5000,
                disableOnInteraction: false,
            },
            pagination: {
                el: '.swiper-pagination',
                clickable: true,
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
            // Responsive breakpoints
            // breakpoints: {
            //     // when window width is >= 768px
            //     768: {
            //         slidesPerView: 2,
            //         spaceBetween: 40
            //     },
            // }
        });
    } else {
        console.warn('Swiper library not loaded.');
    }

    // --- DYNAMIC COPYRIGHT YEAR ---
    const currentYearSpan = document.getElementById('current-year');
    if (currentYearSpan) {
        currentYearSpan.textContent = new Date().getFullYear();
    }

    // --- ACTIVE NAV LINK HIGHLIGHTING ON SCROLL ---
    const sections = document.querySelectorAll('main section[id]');
    const navLi = document.querySelectorAll('.main-nav .nav__list .nav__item .nav__link');

    function changeActiveLink() {
        let current = '';
        sections.forEach(section => {
            const sectionTop = section.offsetTop;
            const sectionHeight = section.clientHeight;
            // Adjust offset if you have a sticky header
            const headerHeight = document.querySelector('.site-header')?.offsetHeight || 0;

            if (pageYOffset >= (sectionTop - headerHeight - sectionHeight / 3) ) { // activate a bit before section top
                current = section.getAttribute('id');
            }
        });

        navLi.forEach(link => {
            link.classList.remove('nav__link--active');
            if (link.getAttribute('href').substring(1) === current) {
                link.classList.add('nav__link--active');
            }
        });
    }

    if (sections.length > 0 && navLi.length > 0) {
        window.addEventListener('scroll', changeActiveLink);
        changeActiveLink(); // Initial call to set active link on page load
    }


    // Smooth scroll for anchor links (if CSS scroll-behavior is not enough or for more control)
    // CSS 'scroll-behavior: smooth;' on <html> should handle this for modern browsers.
    // This is a fallback or an enhancement.
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            const hrefAttribute = this.getAttribute('href');
            if (hrefAttribute && hrefAttribute !== '#' && document.querySelector(hrefAttribute)) {
                e.preventDefault();
                const targetElement = document.querySelector(hrefAttribute);
                if (targetElement) {
                     const headerOffset = document.querySelector('.site-header')?.offsetHeight || 60; // Adjust if you have a sticky header
                     const elementPosition = targetElement.getBoundingClientRect().top + window.pageYOffset;
                     const offsetPosition = elementPosition - headerOffset;

                    window.scrollTo({
                        top: offsetPosition,
                        behavior: 'smooth'
                    });

                    // If it's a nav link, update the URL hash without jumping (optional)
                    // if (this.classList.contains('nav__link')) {
                    //     history.pushState(null, null, hrefAttribute);
                    // }
                }
            }
        });
    });

});

// CSS for .nav--open (add to style.css if not already there for mobile nav)
/*
@media (max-width: 768px) {
  .main-nav .nav__list {
    display: none; // Hidden by default
    flex-direction: column;
    align-items: center;
    width: 100%;
    position: absolute;
    top: 100%; // Below the header part containing logo/toggle
    left: 0;
    background-color: var(--clr-neutral-100);
    box-shadow: 0 2px 5px hsla(0, 0%, 0%, 0.1);
    padding: var(--space-md) 0;
    z-index: 999; // Ensure it's above other content
  }

  .main-nav .nav__list.nav--open {
    display: flex; // Show when .nav--open is added
  }

  .nav-toggle {
    display: block;
  }
}
*/