(function () {
    'use strict';

    /* =========================================================
       Mobile nav toggle
       ========================================================= */
    const toggle = document.querySelector('.nav-toggle');
    const nav    = document.querySelector('.main-nav');

    if (toggle && nav) {
        toggle.addEventListener('click', function () {
            const expanded = this.getAttribute('aria-expanded') === 'true';
            this.setAttribute('aria-expanded', String(!expanded));
            this.setAttribute('aria-label', expanded ? 'Open navigation' : 'Close navigation');
            nav.classList.toggle('is-open', !expanded);
            document.body.style.overflow = expanded ? '' : 'hidden';
        });

        document.addEventListener('keydown', function (e) {
            if (e.key === 'Escape' && nav.classList.contains('is-open')) {
                toggle.setAttribute('aria-expanded', 'false');
                nav.classList.remove('is-open');
                document.body.style.overflow = '';
                toggle.focus();
            }
        });
    }

    /* =========================================================
       Image carousel
       ========================================================= */
    const track    = document.querySelector('.carousel-track');
    const prevBtn  = document.querySelector('.carousel-prev');
    const nextBtn  = document.querySelector('.carousel-next');
    const dotsWrap = document.querySelector('.carousel-dots');

    if (!track) return;

    const slides = Array.from(track.querySelectorAll('.carousel-slide'));
    const dots   = dotsWrap ? Array.from(dotsWrap.querySelectorAll('.carousel-dot')) : [];

    if (slides.length <= 1) return;

    let current   = 0;
    let autoTimer = null;
    const INTERVAL = 5000;

    function goTo(index) {
        slides[current].classList.remove('is-active');
        slides[current].setAttribute('aria-hidden', 'true');
        if (dots[current]) {
            dots[current].classList.remove('is-active');
            dots[current].setAttribute('aria-selected', 'false');
        }

        current = (index + slides.length) % slides.length;

        slides[current].classList.add('is-active');
        slides[current].setAttribute('aria-hidden', 'false');
        if (dots[current]) {
            dots[current].classList.add('is-active');
            dots[current].setAttribute('aria-selected', 'true');
        }
    }

    function startAuto() {
        stopAuto();
        autoTimer = setInterval(function () { goTo(current + 1); }, INTERVAL);
    }

    function stopAuto() {
        if (autoTimer) { clearInterval(autoTimer); autoTimer = null; }
    }

    if (prevBtn) {
        prevBtn.addEventListener('click', function () {
            goTo(current - 1);
            stopAuto();
            startAuto();
        });
    }

    if (nextBtn) {
        nextBtn.addEventListener('click', function () {
            goTo(current + 1);
            stopAuto();
            startAuto();
        });
    }

    dots.forEach(function (dot, i) {
        dot.addEventListener('click', function () {
            goTo(i);
            stopAuto();
            startAuto();
        });
    });

    /* Keyboard navigation */
    track.addEventListener('keydown', function (e) {
        if (e.key === 'ArrowLeft')  { goTo(current - 1); stopAuto(); startAuto(); }
        if (e.key === 'ArrowRight') { goTo(current + 1); stopAuto(); startAuto(); }
    });

    /* Pause on hover / focus */
    track.addEventListener('mouseenter', stopAuto);
    track.addEventListener('mouseleave', startAuto);
    track.addEventListener('focusin',    stopAuto);
    track.addEventListener('focusout',   startAuto);

    /* Respect prefers-reduced-motion */
    if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
        startAuto();
    }

    /* Touch swipe */
    let touchStartX = 0;
    track.addEventListener('touchstart', function (e) {
        touchStartX = e.changedTouches[0].clientX;
    }, { passive: true });

    track.addEventListener('touchend', function (e) {
        const delta = touchStartX - e.changedTouches[0].clientX;
        if (Math.abs(delta) > 50) {
            goTo(delta > 0 ? current + 1 : current - 1);
            stopAuto();
            startAuto();
        }
    }, { passive: true });
})();
