/* Custom styles for Cabin Almost Heaven */

/* Smooth entrance animations — above-the-fold content uses CSS animation directly */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.animate-fade-in-up {
    animation: fadeInUp 0.8s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

/* Scroll reveal — progressive enhancement, content is visible by default */
.reveal-on-scroll {
    /* Default: visible (no JS needed) */
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.7s cubic-bezier(0.16, 1, 0.3, 1), transform 0.7s cubic-bezier(0.16, 1, 0.3, 1);
}

.reveal-on-scroll.revealed {
    opacity: 0;
    transform: translateY(40px);
}

.reveal-on-scroll.revealed.visible {
    opacity: 1;
    transform: translateY(0);
}

/* Navbar transition */
.nav-scrolled {
    background: rgba(254, 253, 248, 0.95) !important;
    backdrop-filter: blur(12px);
    box-shadow: 0 1px 20px rgba(20, 83, 45, 0.08);
}

.nav-scrolled .font-serif {
    color: rgb(22, 101, 52) !important;
}

/* Mobile menu transitions */
.mobile-menu-open {
    opacity: 1 !important;
    pointer-events: auto !important;
}

/* Custom scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: #fefdf8;
}

::-webkit-scrollbar-thumb {
    background: #166534;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #15803d;
}

/* Selection color */
::selection {
    background: #166534;
    color: #fefce8;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .reveal-on-scroll {
        opacity: 1;
        transform: none;
        transition: none;
    }
    .reveal-on-scroll.revealed {
        opacity: 1;
        transform: none;
    }
    .reveal-on-scroll.revealed.visible {
        opacity: 1;
        transform: none;
    }
    html {
        scroll-behavior: auto;
    }
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
</style>

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

    // ——— NAVBAR SCROLL EFFECT ———
    const nav = document.getElementById('nav');
    let lastScroll = 0;

    function handleNavScroll() {
        const currentScroll = window.pageYOffset;
        if (currentScroll > 80) {
            nav.classList.add('nav-scrolled');
        } else {
            nav.classList.remove('nav-scrolled');
        }
        lastScroll = currentScroll;
    }

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

    // ——— MOBILE MENU ———
    const menuBtn = document.getElementById('menuBtn');
    const mobileMenu = document.getElementById('mobileMenu');
    const menuIconOpen = document.getElementById('menuIconOpen');
    const menuIconClose = document.getElementById('menuIconClose');
    let menuOpen = false;

    function toggleMenu() {
        menuOpen = !menuOpen;
        if (menuOpen) {
            mobileMenu.classList.add('mobile-menu-open');
            menuIconOpen.classList.add('hidden');
            menuIconClose.classList.remove('hidden');
            document.body.style.overflow = 'hidden';
        } else {
            mobileMenu.classList.remove('mobile-menu-open');
            menuIconOpen.classList.remove('hidden');
            menuIconClose.classList.add('hidden');
            document.body.style.overflow = '';
        }
    }

    menuBtn.addEventListener('click', toggleMenu);

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

    // ——— SCROLL REVEAL (Progressive Enhancement) ———
    // Check if user prefers reduced motion
    const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

    if (!prefersReducedMotion) {
        // Add hidden state via JS — content is visible by default for no-JS / reduced-motion
        const revealElements = document.querySelectorAll('.reveal-on-scroll');
        revealElements.forEach(function (el) {
            el.classList.add('revealed');
        });

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

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

    // ——— CONTACT FORM ———
    const contactForm = document.getElementById('contactForm');
    const successMessage = document.getElementById('successMessage');

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

            // Simple validation
            const formData = new FormData(contactForm);
            let isValid = true;

            contactForm.querySelectorAll('[required]').forEach(function (field) {
                if (!field.value.trim()) {
                    isValid = false;
                    field.classList.add('border-red-400');
                } else {
                    field.classList.remove('border-red-400');
                }
            });

            if (!isValid) return;

            // Show success
            contactForm.style.display = 'none';
            successMessage.classList.remove('hidden');
        });
    }

    // ——— SMOOTH SCROLL FOR ANCHOR LINKS ———
    document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
        anchor.addEventListener('click', function (e) {
            const targetId = this.getAttribute('href');
            if (targetId === '#top') {
                window.scrollTo({ top: 0, behavior: 'smooth' });
                return;
            }
            const target = document.querySelector(targetId);
            if (target) {
                e.preventDefault();
                const navHeight = nav.offsetHeight;
                const targetPosition = target.getBoundingClientRect().top + window.pageYOffset - navHeight - 20;
                window.scrollTo({
                    top: targetPosition,
                    behavior: 'smooth'
                });
            }
        });
    });
})();
