---
interface ServiceCard {
    id: string;
    type: 'wide-light' | 'wide-dark' | 'standard';
    title: string;
    desc: string;
    icon: string;
    linkText?: string;
    btnText?: string;
    href?: string;
}

const {
    sectionSubtitle = "НАШИ УСЛУГИ",
    sectionTitle = "Помощь автовладельцам в Сургуте",
    services = [
        {
            id: 'card1',
            type: 'wide-light',
            title: "Возврат водительских прав",
            desc: "Вернём права после лишения в кратчайшие сроки. Работаем по всей территории РФ.",
            icon: "🚗",
            href: "/services/license-return"
        },
        {
            id: 'card2',
            type: 'standard',
            title: "ОСАГО/КАСКО",
            desc: "Споры со страховыми компаниями. Взыскание полного ущерба.",
            icon: "🛡️",
            href: "/services/osago"
        },
        {
            id: 'card3',
            type: 'standard',
            title: "Разбор ДТП",
            desc: "Полное сопровождение при разборе ДТП. Оспаривание вины.",
            icon: "🚨",
            href: "/services/damages"
        },
        {
            id: 'card4',
            type: 'wide-dark',
            title: "Представительство в суде",
            desc: "Защита ваших интересов в судах всех инстанций.",
            icon: "⚖️",
            href: "/services/court-representation"
        }
    ] as ServiceCard[]
} = Astro.props;
---

<!-- КРИТИЧНО: Инлайн-скрипт скрывает элементы ДО рендера, но только если JS работает -->
<script is:inline>
    (function() {
        if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;

        const style = document.createElement('style');
        style.textContent = '.animate-on-scroll{opacity:0;transform:translateY(30px)}' +
            '[data-animation="scale-up"]{opacity:0;transform:translateY(40px) scale(0.95)}';
        document.head.appendChild(style);
    })();
</script>

<section class="services-section" id="services">
    <div class="site-container">
        <!-- Заголовок секции -->
        <div class="section-header">
            <span class="subtitle animate-on-scroll" data-animation="fade-up">{sectionSubtitle}</span>
            <h2 class="title animate-on-scroll" data-animation="fade-up" data-delay="100">{sectionTitle}</h2>
        </div>

        <!-- Bento Grid -->
        <div class="bento-grid">
            {services.map((service: ServiceCard, index: number) => (
                    <a
                            href={service.href || '/services'}
                            class={`card animate-on-scroll 
                        ${service.type === 'wide-light' ? 'card-wide card-featured' : ''}
                        ${service.type === 'wide-dark' ? 'card-wide card-dark' : ''}
                        ${service.type === 'standard' ? 'card-standard' : ''}
                    `}
                            data-animation="scale-up"
                            data-delay={index * 150 + 200}
                            data-stagger={index}
                    >
                        {service.type === 'wide-light' && <div class="card-glow"></div>}
                        {service.type === 'standard' && <div class="card-glow"></div>}
                        {service.type === 'wide-dark' && <div class="card-glow-dark"></div>}

                        <div class="card-content">
                            <div class={`card-icon 
                            ${service.type === 'wide-dark' ? 'icon-white' : ''}
                            ${service.type === 'standard' || service.type === 'wide-light' ? 'icon-gold' : ''}
                        `}>
                                <span class="icon-emoji">{service.icon}</span>
                            </div>
                            <h3 class="card-title">{service.title}</h3>
                            <p class="card-desc">{service.desc}</p>

                            <span class="card-arrow">
                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M5 12h14M12 5l7 7-7 7"/>
                            </svg>
                        </span>
                        </div>

                        {service.type === 'wide-light' && <div class="card-accent"></div>}
                        {service.type === 'wide-dark' && <div class="card-shine"></div>}
                    </a>
            ))}
        </div>
    </div>
</section>

<style>
    :root {
        --color-primary: #1e3050;
        --color-accent: #d4af37;
        --color-accent-light: #e8c547;
        --color-dark: #0b1426;
        --color-light: #f8fafc;
        --color-gray: #64748b;
        --gradient-gold: linear-gradient(135deg, #d4af37 0%, #f4d03f 50%, #d4af37 100%);
        --gradient-dark: linear-gradient(135deg, #0b1426 0%, #1e3050 100%);
        --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
        --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
        --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
        --mouse-x: 50%;
        --mouse-y: 50%;
    }

    .services-section {
        padding: 6rem 1.5rem;
        background: linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
        font-family: 'Inter', system-ui, -apple-system, sans-serif;
        overflow: hidden;
    }

    /* Заголовок */
    .section-header {
        margin-bottom: 4rem;
        text-align: center;
    }

    .subtitle {
        display: inline-block;
        color: var(--color-accent);
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 3px;
        margin-bottom: 1rem;
        padding: 0.5rem 1rem;
        background: rgba(212, 175, 55, 0.1);
        border-radius: 6px;
    }

    .title {
        color: var(--color-primary);
        font-size: clamp(2rem, 4vw, 3rem);
        font-weight: 800;
        margin: 0;
        line-height: 1.2;
        letter-spacing: -0.02em;
    }

    /* ИСПРАВЛЕНИЕ LCP: opacity: 1 по умолчанию! */
    .animate-on-scroll {
        opacity: 1;
        transform: translateY(0) scale(1);
        will-change: opacity, transform;
    }

    /* Fade Up - для заголовка */
    [data-animation="fade-up"] {
        transform: translateY(0);
        transition: opacity 0.8s cubic-bezier(0.4, 0, 0.2, 1),
        transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
    }

    /* Scale Up - для карточек */
    [data-animation="scale-up"] {
        transform: translateY(0) scale(1);
        transition: opacity 0.7s cubic-bezier(0.4, 0, 0.2, 1),
        transform 0.7s cubic-bezier(0.4, 0, 0.2, 1);
    }

    /* Активное состояние */
    .animate-on-scroll.is-visible {
        opacity: 1;
        transform: translateY(0) scale(1);
    }

    /* Анимация иконки при появлении */
    .icon-emoji {
        font-size: 2rem;
        line-height: 1;
        filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
        transition: transform 0.3s ease;
        display: inline-block;
    }

    .icon-emoji.icon-bounce {
        animation: iconBounce 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    }

    @keyframes iconBounce {
        0% { transform: scale(0) rotate(-45deg); }
        50% { transform: scale(1.2) rotate(5deg); }
        100% { transform: scale(1) rotate(0deg); }
    }

    /* Bento Grid */
    .bento-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        grid-template-rows: auto auto;
        gap: 1.5rem;
    }

    /* Card as Link */
    .card {
        position: relative;
        border-radius: 12px;
        padding: 2.5rem;
        overflow: hidden;
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        isolation: isolate;
        text-decoration: none;
        cursor: pointer;
        display: block;
    }

    .card::before {
        content: '';
        position: absolute;
        inset: 0;
        border-radius: inherit;
        padding: 1px;
        background: linear-gradient(135deg, rgba(255,255,255,0.4) 0%, rgba(255,255,255,0.1) 100%);
        mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
        mask-composite: exclude;
        -webkit-mask-composite: xor;
        pointer-events: none;
    }

    /* Glow Effect */
    .card-glow, .card-glow-dark {
        position: absolute;
        inset: -1px;
        border-radius: inherit;
        opacity: 0;
        transition: opacity 0.4s ease;
        background: radial-gradient(600px circle at var(--mouse-x, 50%) var(--mouse-y, 50%),
        rgba(212, 175, 55, 0.15), transparent 40%);
        z-index: 0;
        pointer-events: none;
    }

    .card-glow-dark {
        background: radial-gradient(600px circle at var(--mouse-x, 50%) var(--mouse-y, 50%),
        rgba(212, 175, 55, 0.2), transparent 40%);
    }

    .card:hover .card-glow,
    .card:hover .card-glow-dark {
        opacity: 1;
    }

    /* Card Sizes */
    .card-wide {
        grid-column: span 2;
        min-height: 320px;
    }

    .card-standard {
        grid-column: span 1;
        min-height: 280px;
        background: rgba(255, 255, 255, 0.8);
        backdrop-filter: blur(12px);
        border: 1px solid rgba(226, 232, 240, 0.8);
        box-shadow: var(--shadow-md), inset 0 1px 0 rgba(255,255,255,0.8);
    }

    /* Featured Card */
    .card-featured {
        background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
        border: 1px solid rgba(226, 232, 240, 0.6);
        box-shadow: var(--shadow-xl);
    }

    .card-featured .card-accent {
        position: absolute;
        top: 0;
        right: 0;
        width: 300px;
        height: 300px;
        background: radial-gradient(circle, rgba(212, 175, 55, 0.1) 0%, transparent 70%);
        pointer-events: none;
    }

    /* Dark Card */
    .card-dark {
        background: var(--gradient-dark);
        border: 1px solid rgba(255, 255, 255, 0.1);
        box-shadow: var(--shadow-xl), 0 0 60px rgba(11, 20, 38, 0.3);
    }

    .card-dark .card-title {
        color: #ffffff;
    }

    .card-dark .card-desc {
        color: rgba(255, 255, 255, 0.7);
    }

    .card-shine {
        position: absolute;
        top: -50%;
        left: -50%;
        width: 200%;
        height: 200%;
        background: linear-gradient(45deg, transparent 30%, rgba(255, 255, 255, 0.03) 50%, transparent 70%);
        transform: rotate(45deg);
        transition: transform 0.6s;
        pointer-events: none;
    }

    .card-dark:hover .card-shine {
        transform: rotate(45deg) translate(20%, 20%);
    }

    /* Card Content */
    .card-content {
        position: relative;
        z-index: 2;
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    /* Icons - Emoji Style */
    .card-icon {
        width: 64px;
        height: 64px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 8px;
        margin-bottom: 1.5rem;
        background: rgba(30, 48, 80, 0.08);
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 0 4px 15px rgba(30, 48, 80, 0.1);
        flex-shrink: 0;
    }

    .card:hover .icon-emoji {
        transform: scale(1.1);
    }

    .card:hover .card-icon {
        transform: translateY(-4px);
        box-shadow: 0 8px 25px rgba(30, 48, 80, 0.15);
    }

    .icon-gold {
        background: linear-gradient(135deg, rgba(212, 175, 55, 0.15) 0%, rgba(244, 208, 63, 0.1) 100%);
        box-shadow: 0 4px 20px rgba(212, 175, 55, 0.2);
    }

    .card:hover .icon-gold {
        box-shadow: 0 8px 30px rgba(212, 175, 55, 0.3);
        background: linear-gradient(135deg, rgba(212, 175, 55, 0.2) 0%, rgba(244, 208, 63, 0.15) 100%);
    }

    .icon-white {
        background: rgba(255, 255, 255, 0.1);
        border: 1px solid rgba(255, 255, 255, 0.2);
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    }

    .card:hover .icon-white {
        background: rgba(255, 255, 255, 0.15);
        box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
    }

    .icon-white .icon-emoji {
        filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
    }

    /* Typography */
    .card-title {
        font-size: 1.5rem;
        font-weight: 700;
        color: var(--color-primary);
        margin: 0 0 0.75rem 0;
        line-height: 1.3;
        letter-spacing: -0.01em;
        transition: color 0.3s ease;
    }

    .card:hover .card-title {
        color: var(--color-accent);
    }

    .card-dark:hover .card-title {
        color: var(--color-accent-light);
    }

    .card-desc {
        font-size: 1rem;
        line-height: 1.7;
        color: var(--color-gray);
        margin: 0;
        flex-grow: 1;
    }

    /* Arrow Indicator */
    .card-arrow {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        background: rgba(30, 48, 80, 0.08);
        color: var(--color-primary);
        margin-top: 1.5rem;
        margin-left: auto;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        opacity: 0.6;
    }

    .card:hover .card-arrow {
        background: var(--color-accent);
        color: white;
        opacity: 1;
        transform: translateX(4px);
    }

    /* Hover Effects */
    .card:hover {
        transform: translateY(-4px);
    }

    .card-featured:hover {
        box-shadow: var(--shadow-xl), 0 20px 40px rgba(30, 48, 80, 0.15);
    }

    .card-standard:hover {
        box-shadow: var(--shadow-xl), 0 20px 40px rgba(0, 0, 0, 0.1);
        border-color: rgba(212, 175, 55, 0.3);
    }

    .card-dark:hover {
        box-shadow: var(--shadow-xl), 0 0 80px rgba(11, 20, 38, 0.5);
        border-color: rgba(212, 175, 55, 0.3);
    }

    /* Responsive */
    @media (max-width: 1024px) {
        .bento-grid {
            grid-template-columns: repeat(2, 1fr);
        }

        .card-wide {
            grid-column: span 2;
        }

        .card-standard {
            grid-column: span 1;
        }
    }

    @media (max-width: 768px) {
        .services-section {
            padding: 4rem 1rem;
        }

        .bento-grid {
            grid-template-columns: 1fr;
        }

        .card-wide,
        .card-standard {
            grid-column: span 1;
            min-height: auto;
        }

        .card {
            padding: 2rem;
            text-align: center;
        }

        .card-content {
            align-items: center;
        }

        .card-icon {
            width: 56px;
            height: 56px;
            margin-left: auto;
            margin-right: auto;
        }

        .icon-emoji {
            font-size: 1.75rem;
        }

        [data-animation="scale-up"] {
            transform: translateY(0) scale(1);
        }
    }

    /* Уважаем prefers-reduced-motion */
    @media (prefers-reduced-motion: reduce) {
        .animate-on-scroll {
            opacity: 1 !important;
            transform: none !important;
            transition: none !important;
        }

        .icon-emoji.icon-bounce {
            animation: none;
        }
    }
</style>

<script>
    // Intersection Observer для анимаций при скроллинге
    const observerOptions = {
        root: null,
        rootMargin: '0px 0px -50px 0px',
        threshold: 0.1
    };

    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                const el = entry.target as HTMLElement;
                const delay = parseInt(el.dataset.delay || '0');

                setTimeout(() => {
                    requestAnimationFrame(() => {
                        el.classList.add('is-visible');

                        const icon = el.querySelector('.icon-emoji');
                        if (icon) {
                            setTimeout(() => {
                                icon.classList.add('icon-bounce');
                            }, 100);
                        }
                    });
                }, delay);

                observer.unobserve(el);
            }
        });
    }, observerOptions);

    document.querySelectorAll('.animate-on-scroll').forEach((el) => {
        observer.observe(el);
    });

    // Glow effect on mouse move
    document.querySelectorAll('.card').forEach((card) => {
        (card as HTMLElement).addEventListener('mousemove', (e: MouseEvent) => {
            const rect = (card as HTMLElement).getBoundingClientRect();
            const x = e.clientX - rect.left;
            const y = e.clientY - rect.top;
            (card as HTMLElement).style.setProperty('--mouse-x', `${x}px`);
            (card as HTMLElement).style.setProperty('--mouse-y', `${y}px`);
        });
    });
</script>