document.addEventListener('DOMContentLoaded', () => {
    // Dark Mode Toggle
    const toggleButton = document.createElement('button');
    toggleButton.id = 'dark-mode-toggle';
    toggleButton.setAttribute('aria-label', 'Toggle Dark Mode');
    toggleButton.innerHTML = '🌙';
    document.body.appendChild(toggleButton);

    const currentTheme = localStorage.getItem('theme');
    if (currentTheme === 'dark') {
        document.body.classList.add('dark-mode');
        toggleButton.innerHTML = '☀️';
    }

    toggleButton.addEventListener('click', () => {
        document.body.classList.toggle('dark-mode');
        let theme = 'light';
        if (document.body.classList.contains('dark-mode')) {
            theme = 'dark';
            toggleButton.innerHTML = '☀️';
        } else {
            toggleButton.innerHTML = '🌙';
        }
        localStorage.setItem('theme', theme);
    });

    // Scroll Progress Bar
    const scrollProgress = document.createElement('div');
    scrollProgress.id = 'scroll-progress';
    document.body.appendChild(scrollProgress);

    // Back to Top Button
    const backToTop = document.createElement('button');
    backToTop.id = 'back-to-top';
    backToTop.setAttribute('aria-label', 'Back to Top');
    backToTop.innerHTML = '↑';
    document.body.appendChild(backToTop);

    // Floating Telegram FAB
    const fabTelegram = document.createElement('a');
    fabTelegram.className = 'fab-telegram';
    fabTelegram.href = 'https://t.me/Bilnett';
    fabTelegram.target = '_blank';
    fabTelegram.innerHTML = '✈️';
    fabTelegram.setAttribute('aria-label', 'Contact on Telegram');
    document.body.appendChild(fabTelegram);

    // Intersection Observer for Reveal Animations
    const observerOptions = {
        threshold: 0.1
    };

    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('active');
            }
        });
    }, observerOptions);

    // Apply reveal to groups and columns
    document.querySelectorAll('.wp-block-group, .wp-block-column').forEach(el => {
        el.classList.add('reveal');
        observer.observe(el);
    });

    window.addEventListener('scroll', () => {
        // Scroll Progress
        const winScroll = document.body.scrollTop || document.documentElement.scrollTop;
        const height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
        const scrolled = (winScroll / height) * 100;
        scrollProgress.style.width = scrolled + "%";

        // Back to Top Visibility
        if (window.scrollY > 400) {
            backToTop.classList.add('visible');
        } else {
            backToTop.classList.remove('visible');
        }
    });

    backToTop.addEventListener('click', () => {
        window.scrollTo({ top: 0, behavior: 'smooth' });
    });

    // Handle Newsletter form submission (Demo)
    const newsletterForm = document.querySelector('.newsletter-section form');
    if (newsletterForm) {
        newsletterForm.addEventListener('submit', (e) => {
            e.preventDefault();
            const email = newsletterForm.querySelector('input[type="email"]').value;
            if (email) {
                alert('Thank you for subscribing, ' + email + '! (Demo only)');
                newsletterForm.reset();
            }
        });
    }
});