
    // Scroll to encrypt section
    document.getElementById('tryItBtn').addEventListener('click', () => {
        document.getElementById('encryptSection').scrollIntoView({ behavior: 'smooth' });
    });

    // Header form AJAX submission
    const headerForm = document.getElementById('headerContactForm');
    const headerBtn = document.getElementById('headerSubmitBtn');
    const headerLoader = document.getElementById('headerLoader');
    const headerSuccess = document.getElementById('headerSuccessMsg');
    const headerError = document.getElementById('headerErrorMsg');

    headerForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        headerLoader.style.display = 'block';
        headerSuccess.style.display = 'none';
        headerError.style.display = 'none';
        headerBtn.disabled = true;
        headerBtn.style.opacity = '0.6';
        const formData = new FormData(headerForm);
        try {
            const response = await fetch('/contact', { method: 'POST', body: formData });
            if (!response.ok) throw new Error('Server error');
            headerSuccess.style.display = 'block';
            headerForm.reset();
            setTimeout(() => headerSuccess.style.display = 'none', 5000);
        } catch (err) {
            headerError.style.display = 'block';
            setTimeout(() => headerError.style.display = 'none', 5000);
        } finally {
            headerLoader.style.display = 'none';
            headerBtn.disabled = false;
            headerBtn.style.opacity = '1';
        }
    });

    // Testimonial Slider
    const slider = document.querySelector('.testimonial-slider');
    const slides = document.querySelectorAll('.testimonial-card');
    const prevBtn = document.querySelector('.prev-btn');
    const nextBtn = document.querySelector('.next-btn');
    const dotsContainer = document.querySelector('.dots-container');
    let currentIndex = 0;
    const totalSlides = slides.length;
    let autoSlideInterval;

    slides.forEach((_, i) => {
        const dot = document.createElement('div');
        dot.classList.add('dot');
        if (i === 0) dot.classList.add('active');
        dot.addEventListener('click', () => goToSlide(i));
        dotsContainer.appendChild(dot);
    });
    const dots = document.querySelectorAll('.dot');

    function updateSlider() {
        slider.style.transform = `translateX(-${currentIndex * 100}%)`;
        dots.forEach((dot, i) => dot.classList.toggle('active', i === currentIndex));
    }

    function goToSlide(index) {
        currentIndex = (index + totalSlides) % totalSlides;
        updateSlider();
        resetAutoSlide();
    }

    function nextSlide() { goToSlide(currentIndex + 1); }
    function prevSlide() { goToSlide(currentIndex - 1); }
    function startAutoSlide() { autoSlideInterval = setInterval(nextSlide, 5000); }
    function resetAutoSlide() { clearInterval(autoSlideInterval); startAutoSlide(); }

    prevBtn.addEventListener('click', prevSlide);
    nextBtn.addEventListener('click', nextSlide);
    startAutoSlide();

    // Footer form AJAX submission
    const footerForm = document.getElementById('footerContactForm');
    const footerBtn = document.getElementById('footerSubmitBtn');
    const footerLoader = document.getElementById('footerLoader');
    const footerSuccess = document.getElementById('footerSuccessMsg');
    const footerError = document.getElementById('footerErrorMsg');

    if (footerForm) {
        footerForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            footerLoader.style.display = 'block';
            footerSuccess.style.display = 'none';
            footerError.style.display = 'none';
            footerBtn.disabled = true;
            footerBtn.style.opacity = '0.6';
            const formData = new FormData(footerForm);
            try {
                const response = await fetch('/contact', { method: 'POST', body: formData });
                if (!response.ok) throw new Error('Server error');
                footerSuccess.style.display = 'block';
                footerForm.reset();
                setTimeout(() => footerSuccess.style.display = 'none', 5000);
            } catch (err) {
                footerError.style.display = 'block';
                setTimeout(() => footerError.style.display = 'none', 5000);
            } finally {
                footerLoader.style.display = 'none';
                footerBtn.disabled = false;
                footerBtn.style.opacity = '1';
            }
        });
    }

    // PDF encryption form (updated to handle JSON response and rate limiting)
    const encryptForm = document.getElementById('encryptForm');
    const encryptBtn = document.getElementById('encryptBtn');
    const loader = document.getElementById('loader');
    const successMsg = document.getElementById('successMsg');

    encryptForm.addEventListener('submit', async (e) => {
        e.preventDefault();
        loader.style.display = 'block';
        successMsg.style.display = 'none';
        encryptBtn.disabled = true;
        encryptBtn.style.opacity = '0.6';

        const formData = new FormData(encryptForm);
        try {
            const response = await fetch('/encrypt-pdf', { method: 'POST', body: formData });
            const result = await response.json();

            if (!response.ok) {
                // Handle rate limiting (429) specifically
                if (response.status === 429) {
                    successMsg.style.backgroundColor = '#e74c3c'; // red
                    successMsg.style.color = 'white';
                    successMsg.innerHTML = '<i class="fas fa-exclamation-triangle"></i> ' + (result.message || 'Too many requests. Please try again tomorrow.');
                    successMsg.style.display = 'block';
                    setTimeout(() => {
                        successMsg.style.display = 'none';
                        successMsg.style.backgroundColor = '#10b981'; // reset to green
                    }, 8000);
                    return;
                }
                throw new Error(result.message || 'Encryption failed');
            }

            // Success – show the server's message (e.g., "Check your email...")
            successMsg.style.backgroundColor = '#10b981'; // green
            successMsg.style.color = 'white';
            successMsg.innerHTML = '<i class="fas fa-check-circle"></i> ' + result.message;
            successMsg.style.display = 'block';
            encryptForm.reset();

            setTimeout(() => {
                successMsg.style.display = 'none';
                successMsg.style.backgroundColor = '#10b981';
            }, 6000);
        } catch (err) {
            alert('Error: ' + err.message);
        } finally {
            loader.style.display = 'none';
            encryptBtn.disabled = false;
            encryptBtn.style.opacity = '1';
        }
    });