// Fizasite – mobile nav toggle
document.addEventListener('DOMContentLoaded', function() {
    var toggle = document.querySelector('.nav-toggle');
    var nav = document.querySelector('.main-nav');
    if (toggle && nav) {
        toggle.addEventListener('click', function() {
            var open = nav.classList.toggle('is-open');
            toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
        });
    }

    // Count-up animation (runs when section enters view)
    var countEls = Array.prototype.slice.call(document.querySelectorAll('.js-countup'));
    if (countEls.length && 'IntersectionObserver' in window) {
        var started = false;
        var parseValue = function(txt) {
            txt = (txt || '').toString().trim();
            var m = txt.match(/^([\d,.\s]+)(.*)$/);
            if (!m) return { num: null, suffix: '' };
            var numRaw = (m[1] || '').replace(/[, \t]/g, '');
            var num = parseFloat(numRaw);
            if (!isFinite(num)) return { num: null, suffix: '' };
            return { num: num, suffix: (m[2] || '') };
        };
        var animate = function() {
            if (started) return;
            started = true;
            countEls.forEach(function(el) {
                var original = el.textContent || '';
                var pv = parseValue(original);
                if (pv.num === null) return;
                var end = pv.num;
                var suffix = pv.suffix;
                var start = 0;
                var duration = 1200;
                var startTs = null;
                var isInt = Math.abs(end - Math.round(end)) < 0.00001;
                var step = function(ts) {
                    if (startTs === null) startTs = ts;
                    var p = Math.min(1, (ts - startTs) / duration);
                    // easeOutCubic
                    var eased = 1 - Math.pow(1 - p, 3);
                    var cur = start + (end - start) * eased;
                    el.textContent = (isInt ? Math.round(cur) : cur.toFixed(1)) + suffix;
                    if (p < 1) requestAnimationFrame(step);
                };
                requestAnimationFrame(step);
            });
        };
        var target = document.querySelector('.client-section') || countEls[0];
        var io = new IntersectionObserver(function(entries) {
            entries.forEach(function(entry) {
                if (entry.isIntersecting) animate();
            });
        }, { threshold: 0.35 });
        if (target) io.observe(target);
    }

    var galleryLinks = document.querySelectorAll('.gallery-link[data-lightbox]');
    if (galleryLinks.length && !window.lightbox) {
        galleryLinks.forEach(function(a) {
            a.addEventListener('click', function(e) {
                e.preventDefault();
                window.open(this.href, '_blank', 'noopener');
            });
        });
    }

    // Hero slider (homepage)
    var slider = document.querySelector('.js-hero-slider');
    if (slider) {
        var slides = Array.prototype.slice.call(slider.querySelectorAll('.hero-slide'));
        var prev = document.querySelector('[data-hero-prev]');
        var next = document.querySelector('[data-hero-next]');
        var dotsWrap = document.querySelector('[data-hero-dots]');
        var autoplay = slider.getAttribute('data-autoplay') === '1';
        var interval = parseInt(slider.getAttribute('data-interval') || '5500', 10);
        if (!isFinite(interval) || interval < 2000) interval = 5500;
        var idx = 0;
        var timer = null;

        // Reset initial state so first slide animates on load
        slides.forEach(function(s) {
            s.classList.remove('is-active');
            s.setAttribute('aria-hidden', 'true');
        });

        var setActive = function(n) {
            if (!slides.length) return;
            idx = (n + slides.length) % slides.length;
            slides.forEach(function(s, i) {
                s.classList.toggle('is-active', i === idx);
                s.setAttribute('aria-hidden', i === idx ? 'false' : 'true');
            });
            if (dotsWrap) {
                var dots = Array.prototype.slice.call(dotsWrap.querySelectorAll('button'));
                dots.forEach(function(d, i) { d.classList.toggle('is-active', i === idx); });
            }
        };

        var stop = function() { if (timer) { clearInterval(timer); timer = null; } };
        var start = function() { if (autoplay && slides.length > 1 && !timer) timer = setInterval(function(){ setActive(idx + 1); }, interval); };

        if (dotsWrap) {
            dotsWrap.innerHTML = '';
            slides.forEach(function(_, i) {
                var b = document.createElement('button');
                b.type = 'button';
                b.className = 'hero-dot' + (i === 0 ? ' is-active' : '');
                b.setAttribute('aria-label', 'Go to slide ' + (i + 1));
                b.addEventListener('click', function() { stop(); setActive(i); start(); });
                dotsWrap.appendChild(b);
            });
        }

        if (prev) prev.addEventListener('click', function(){ stop(); setActive(idx - 1); start(); });
        if (next) next.addEventListener('click', function(){ stop(); setActive(idx + 1); start(); });

        slider.addEventListener('mouseenter', stop);
        slider.addEventListener('mouseleave', start);

        requestAnimationFrame(function() {
            setActive(0);
            start();
        });
    }
});
