/* ============================================================
   THEME-MODERN.JS — scroll reveal, counters, hero slider,
   card tilt, parallax blobs. No dependencies.
   ============================================================ */
(function () {
    'use strict';

    var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

    /* ── Scroll reveal ──────────────────────────────────────── */
    function initReveal() {
        var els = document.querySelectorAll('.reveal');
        if (!els.length) return;
        if (reduceMotion || !('IntersectionObserver' in window)) {
            els.forEach(function (el) { el.classList.add('rv-in'); });
            return;
        }
        var io = new IntersectionObserver(function (entries) {
            entries.forEach(function (e) {
                if (e.isIntersecting) {
                    e.target.classList.add('rv-in');
                    io.unobserve(e.target);
                }
            });
        }, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });
        els.forEach(function (el) { io.observe(el); });
    }

    /* ── Animated counters: <span data-count="22">0</span> ──── */
    function initCounters() {
        var els = document.querySelectorAll('[data-count]');
        if (!els.length) return;
        function animate(el) {
            var target = parseFloat(el.getAttribute('data-count')) || 0;
            if (reduceMotion) { el.textContent = target; return; }
            var dur = 1800, start = null;
            function step(ts) {
                if (!start) start = ts;
                var p = Math.min((ts - start) / dur, 1);
                var eased = 1 - Math.pow(1 - p, 3);
                el.textContent = Math.round(target * eased).toLocaleString('en-IN');
                if (p < 1) requestAnimationFrame(step);
            }
            requestAnimationFrame(step);
        }
        if (!('IntersectionObserver' in window)) {
            els.forEach(animate);
            return;
        }
        var io = new IntersectionObserver(function (entries) {
            entries.forEach(function (e) {
                if (e.isIntersecting) { animate(e.target); io.unobserve(e.target); }
            });
        }, { threshold: 0.4 });
        els.forEach(function (el) { io.observe(el); });
    }

    /* ── Hero slider (dynamic banners) ──────────────────────── */
    function initHeroSlider() {
        var hero = document.querySelector('.hero-modern[data-slider]');
        if (!hero) return;
        var slides = hero.querySelectorAll('.hero-slide');
        var bgs    = hero.querySelectorAll('.h-bg');
        var dotsBox = hero.querySelector('.hero-dots');
        if (slides.length <= 1) return;

        var idx = 0, timer = null;
        function show(n) {
            idx = (n + slides.length) % slides.length;
            slides.forEach(function (s, i) { s.classList.toggle('on', i === idx); });
            bgs.forEach(function (b, i) { b.style.opacity = (i === idx) ? '.38' : '0'; });
            if (dotsBox) {
                dotsBox.querySelectorAll('button').forEach(function (d, i) {
                    d.classList.toggle('on', i === idx);
                });
            }
        }
        function play() {
            stop();
            if (!reduceMotion) timer = setInterval(function () { show(idx + 1); }, 6000);
        }
        function stop() { if (timer) { clearInterval(timer); timer = null; } }

        if (dotsBox) {
            slides.forEach(function (_, i) {
                var b = document.createElement('button');
                b.setAttribute('aria-label', 'Slide ' + (i + 1));
                b.addEventListener('click', function () { show(i); play(); });
                dotsBox.appendChild(b);
            });
        }
        hero.addEventListener('mouseenter', stop);
        hero.addEventListener('mouseleave', play);
        show(0);
        play();
    }

    /* ── Testimonial slider: [data-t-slider] ────────────────── */
    function initTestimonialSlider() {
        document.querySelectorAll('[data-t-slider]').forEach(function (box) {
            var slides = box.querySelectorAll('.t-slide');
            if (slides.length <= 1) return;
            var dotsBox = box.querySelector('.t-dots');
            var idx = 0, timer = null;

            function show(n) {
                idx = (n + slides.length) % slides.length;
                slides.forEach(function (s, i) { s.classList.toggle('on', i === idx); });
                if (dotsBox) {
                    dotsBox.querySelectorAll('button').forEach(function (d, i) {
                        d.classList.toggle('on', i === idx);
                    });
                }
            }
            function play() {
                stop();
                if (!reduceMotion) timer = setInterval(function () { show(idx + 1); }, 5500);
            }
            function stop() { if (timer) { clearInterval(timer); timer = null; } }

            if (dotsBox) {
                slides.forEach(function (_, i) {
                    var b = document.createElement('button');
                    b.setAttribute('aria-label', 'Testimonial ' + (i + 1));
                    b.addEventListener('click', function () { show(i); play(); });
                    dotsBox.appendChild(b);
                });
            }
            box.addEventListener('mouseenter', stop);
            box.addEventListener('mouseleave', play);
            show(0);
            play();
        });
    }

    /* ── Gentle 3D tilt on cards: [data-tilt] ───────────────── */
    function initTilt() {
        if (reduceMotion || !window.matchMedia('(hover: hover)').matches) return;
        document.querySelectorAll('[data-tilt]').forEach(function (card) {
            var raf = null;
            card.addEventListener('mousemove', function (e) {
                if (raf) return;
                raf = requestAnimationFrame(function () {
                    var r = card.getBoundingClientRect();
                    var x = (e.clientX - r.left) / r.width - 0.5;
                    var y = (e.clientY - r.top) / r.height - 0.5;
                    card.style.transform =
                        'perspective(800px) rotateY(' + (x * 6) + 'deg) rotateX(' + (-y * 6) + 'deg) translateY(-8px)';
                    raf = null;
                });
            });
            card.addEventListener('mouseleave', function () {
                card.style.transform = '';
            });
        });
    }

    /* ── Parallax drift for decorative blobs ────────────────── */
    function initParallax() {
        if (reduceMotion) return;
        var els = document.querySelectorAll('[data-parallax]');
        if (!els.length) return;
        var ticking = false;
        window.addEventListener('scroll', function () {
            if (ticking) return;
            ticking = true;
            requestAnimationFrame(function () {
                var sy = window.scrollY;
                els.forEach(function (el) {
                    var speed = parseFloat(el.getAttribute('data-parallax')) || 0.15;
                    var box = el.getBoundingClientRect();
                    if (box.bottom > 0 && box.top < window.innerHeight) {
                        el.style.transform = 'translateY(' + ((sy * speed) % 120 - 60) + 'px)';
                    }
                });
                ticking = false;
            });
        }, { passive: true });
    }

    /* ── Boot ───────────────────────────────────────────────── */
    function boot() {
        initReveal();
        initCounters();
        initHeroSlider();
        initTestimonialSlider();
        initTilt();
        initParallax();
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', boot);
    } else {
        boot();
    }
})();
