/* ===== Custom Styles for State Farm Charlotte ===== */

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

/* Custom scrollbar */
::-webkit-scrollbar {
    width: 8px;
}
::-webkit-scrollbar-track {
    background: #f6f6f7;
}
::-webkit-scrollbar-thumb {
    background: #c4c6cb;
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
    background: #a1a5ad;
}

/* Selection color */
::selection {
    background: #f06040;
    color: #ffffff;
}

/* ===== Navbar transitions ===== */
#navbar {
    transition: background-color 0.4s ease, box-shadow 0.4s ease, padding 0.4s ease;
}

#navbar.scrolled {
    background-color: rgba(255, 255, 255, 0.97);
    box-shadow: 0 1px 20px rgba(26, 29, 35, 0.08);
    backdrop-filter: blur(12px);
}

#navbar.scrolled .font-serif,
#navbar.scrolled span:not(.block) {
    color: #1a1d23;
}

#navbar.scrolled .mobile-link,
#navbar.scrolled a:not(.bg-coral-500):not(.bg-coral-700):not([href^="tel:"]) {
    color: #4a4f57;
}

#navbar.scrolled .mobile-link:hover {
    color: #f06040;
}

/* ===== Mobile menu ===== */
#mobile-menu {
    animation: slideDown 0.3s ease forwards;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#mobile-menu.hidden {
    display: none;
}

/* ===== Floating card animations ===== */
@keyframes float1 {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-10px); }
}
@keyframes float2 {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-8px); }
}
@keyframes float3 {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-12px); }
}
@keyframes float4 {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-6px); }
}

.floating-card-1 { animation: float1 6s ease-in-out infinite; }
.floating-card-2 { animation: float2 7s ease-in-out infinite 0.5s; }
.floating-card-3 { animation: float3 5.5s ease-in-out infinite 1s; }
.floating-card-4 { animation: float4 6.5s ease-in-out infinite 1.5s; }

/* ===== Scroll reveal (below fold only) ===== */
.reveal {
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.8s cubic-bezier(0.16, 1, 0.3, 1), transform 0.8s cubic-bezier(0.16, 1, 0.3, 1);
}

.reveal.revealed {
    opacity: 1;
    transform: translateY(0);
}

@media (prefers-reduced-motion: no-preference) {
    .reveal {
        opacity: 0;
        transform: translateY(30px);
    }
    .reveal.revealed {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Stagger delays */
.reveal-delay-1 { transition-delay: 0.1s; }
.reveal-delay-2 { transition-delay: 0.2s; }
.reveal-delay-3 { transition-delay: 0.3s; }
.reveal-delay-4 { transition-delay: 0.4s; }
.reveal-delay-5 { transition-delay: 0.5s; }
.reveal-delay-6 { transition-delay: 0.6s; }

/* ===== Service card hover glow ===== */
.group:hover .group-hover\:bg-coral-500 {
    background-color: #f06040;
}

/* ===== Form focus states ===== */
input:focus,
select:focus,
textarea:focus {
    outline: none;
}

/* ===== Testimonial quote mark ===== */
svg[viewBox="0 0 24 24"].absolute {
    opacity: 0.6;
}

/* ===== Map hover ===== */
.rounded-2xl.overflow-hidden:hover iframe {
    filter: brightness(0.95);
    transition: filter 0.3s ease;
}

/* ===== Counter animation ===== */
.counter-value {
    display: inline-block;
}

/* ===== Accessibility ===== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    html {
        scroll-behavior: auto;
    }
    .reveal {
        opacity: 1;
        transform: none;
    }
}

/* ===== Tablet adjustments ===== */
@media (max-width: 768px) {
    .font-serif {
        line-height: 1.15;
    }
}

/* ===== Print styles ===== */
@media print {
    nav, #mobile-menu, button, form, iframe {
        display: none !important;
    }
    body {
        color: #000;
        background: #fff;
    }
}
</style>

<sm-file path="script.js">
(function () {
    'use strict';

    // ===== Navbar scroll behavior =====
    const navbar = document.getElementById('navbar');
    let lastScroll = 0;

    function handleNavbarScroll() {
        const currentScroll = window.pageYOffset;
        if (currentScroll > 60) {
            navbar.classList.add('scrolled');
        } else {
            navbar.classList.remove('scrolled');
        }
        lastScroll = currentScroll;
    }

    window.addEventListener('scroll', handleNavbarScroll, { passive: true });
    handleNavbarScroll();

    // ===== Mobile menu =====
    const mobileMenuBtn = document.getElementById('mobile-menu-btn');
    const mobileMenu = document.getElementById('mobile-menu');
    const menuIconOpen = document.getElementById('menu-icon-open');
    const menuIconClose = document.getElementById('menu-icon-close');
    let menuOpen = false;

    mobileMenuBtn.addEventListener('click', function () {
        menuOpen = !menuOpen;
        if (menuOpen) {
            mobileMenu.classList.remove('hidden');
            menuIconOpen.classList.add('hidden');
            menuIconClose.classList.remove('hidden');
        } else {
            mobileMenu.classList.add('hidden');
            menuIconOpen.classList.remove('hidden');
            menuIconClose.classList.add('hidden');
        }
    });

    // Close mobile menu on link click
    document.querySelectorAll('.mobile-link').forEach(function (link) {
        link.addEventListener('click', function () {
            menuOpen = false;
            mobileMenu.classList.add('hidden');
            menuIconOpen.classList.remove('hidden');
            menuIconClose.classList.add('hidden');
        });
    });

    // ===== Scroll reveal (progressive enhancement) =====
    // Only enable if user hasn't requested reduced motion
    const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

    if (!prefersReducedMotion && 'IntersectionObserver' in window) {
        const revealElements = document.querySelectorAll('.reveal');

        const revealObserver = new IntersectionObserver(function (entries) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting) {
                    entry.target.classList.add('revealed');
                    revealObserver.unobserve(entry.target);
                }
            });
        }, {
            threshold: 0.12,
            rootMargin: '0px 0px -40px 0px'
        });

        revealElements.forEach(function (el) {
            revealObserver.observe(el);
        });
    }

    // ===== Contact form =====
    const contactForm = document.getElementById('contact-form');
    const formSuccess = document.getElementById('form-success');

    if (contactForm) {
        contactForm.addEventListener('submit', function (e) {
            e.preventDefault();

            // Basic validation
            const firstName = document.getElementById('first-name').value.trim();
            const email = document.getElementById('email').value.trim();

            if (!firstName || !email) {
                return;
            }

            // Simulate submission
            contactForm.style.opacity = '0.5';
            contactForm.style.pointerEvents = 'none';

            setTimeout(function () {
                contactForm.classList.add('hidden');
                formSuccess.classList.remove('hidden');
                formSuccess.classList.add('reveal', 'revealed');
            }, 800);
        });
    }

    // ===== Smooth scroll for anchor links =====
    document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
        anchor.addEventListener('click', function (e) {
            const targetId = this.getAttribute('href');
            if (targetId === '#') return;

            const target = document.querySelector(targetId);
            if (target) {
                e.preventDefault();
                const navHeight = navbar.offsetHeight;
                const targetPosition = target.getBoundingClientRect().top + window.pageYOffset - navHeight - 20;

                window.scrollTo({
                    top: targetPosition,
                    behavior: 'smooth'
                });
            }
        });
    });

    // ===== Active nav link highlighting =====
    const sections = document.querySelectorAll('section[id]');
    const navLinks = document.querySelectorAll('nav a[href^="#"]');

    function highlightNav() {
        const scrollPos = window.pageYOffset + 150;

        sections.forEach(function (section) {
            const sectionTop = section.offsetTop;
            const sectionHeight = section.offsetHeight;
            const sectionId = section.getAttribute('id');

            if (scrollPos >= sectionTop && scrollPos < sectionTop + sectionHeight) {
                navLinks.forEach(function (link) {
                    link.classList.remove('text-coral-600');
                    if (link.getAttribute('href') === '#' + sectionId) {
                        link.classList.add('text-coral-600');
                    }
                });
            }
        });
    }

    window.addEventListener('scroll', highlightNav, { passive: true });

    // ===== Counter animation =====
    const counters = document.querySelectorAll('.counter-value');
    let countersAnimated = false;

    if (counters.length > 0 && !prefersReducedMotion && 'IntersectionObserver' in window) {
        const counterObserver = new IntersectionObserver(function (entries) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting && !countersAnimated) {
                    countersAnimated = true;
                    animateCounters();
                    counterObserver.unobserve(entry.target);
                }
            });
        }, { threshold: 0.5 });

        counters.forEach(function (counter) {
            counterObserver.observe(counter);
        });
    }

    function animateCounters() {
        counters.forEach(function (counter) {
            const target = parseInt(counter.getAttribute('data-target'), 10);
            const suffix = counter.getAttribute('data-suffix') || '';
            const duration = 2000;
            const startTime = performance.now();

            function updateCounter(currentTime) {
                const elapsed = currentTime - startTime;
                const progress = Math.min(elapsed / duration, 1);
                // Ease out cubic
                const eased = 1 - Math.pow(1 - progress, 3);
                const current = Math.round(eased * target);
                counter.textContent = current.toLocaleString() + suffix;

                if (progress < 1) {
                    requestAnimationFrame(updateCounter);
                }
            }

            requestAnimationFrame(updateCounter);
        });
    }

})();
