(function () {
    // If jQuery or CDN scripts fail to load within 4 seconds, force-show everything
    var fallbackTimer = setTimeout(function () {
        // Remove body loading lock
        document.body.classList.remove('page-loading');
        // Hide preloader
        var preloader = document.getElementById('preloader');
        if (preloader) preloader.classList.add('hidden');
        // Reveal all hidden elements
        var reveals = document.querySelectorAll('.reveal');
        reveals.forEach(function (el, i) {
            setTimeout(function () { el.classList.add('visible'); }, i * 30);
        });
    }, 4000);

    // If jQuery loads successfully, it will handle everything — cancel the fallback
    window.addEventListener('load', function () {
        if (typeof window.jQuery !== 'undefined') {
            clearTimeout(fallbackTimer);
        }
    });

    // Plain JS IntersectionObserver as backup (works even without jQuery)
    if ('IntersectionObserver' in window) {
        var observer = new IntersectionObserver(function (entries) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting) {
                    entry.target.classList.add('visible');
                    observer.unobserve(entry.target);
                }
            });
        }, { threshold: 0.08, rootMargin: '0px 0px -20px 0px' });

        // Start observing after a short delay to let jQuery take over if available
        setTimeout(function () {
            document.querySelectorAll('.reveal:not(.visible)').forEach(function (el) {
                observer.observe(el);
            });
        }, 500);
    }
})();