@php
    $events = $data['events'] ?? collect();
    $layout = $section->getSetting('layout', 'grid_3');
    $maxItems = $section->getSetting('max_items', 6);
    $events = $events->take($maxItems);

    $gridClass = match($layout) {
        'grid_2' => 'grid grid-cols-1 sm:grid-cols-2 gap-[var(--card-gap,1.5rem)]',
        'grid_4' => 'grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-[var(--card-gap,1.5rem)]',
        'list' => 'flex flex-col gap-[var(--card-gap,1.5rem)]',
        'carousel' => 'flex overflow-x-auto snap-x snap-mandatory gap-[var(--card-gap,1.5rem)] pb-4 -mx-4 px-4 scrollbar-hide',
        default => 'grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-[var(--card-gap,1.5rem)]',
    };
    $cardExtraClass = $layout === 'carousel' ? 'min-w-[320px] snap-start shrink-0' : '';
    $listMode = $layout === 'list';

    $typeLabels = [
        'tournament' => 'بطولة',
        'camp' => 'معسكر',
        'competition' => 'مسابقة',
        'open_day' => 'يوم مفتوح',
        'workshop' => 'ورشة عمل',
        'friendly' => 'مباراة ودية',
        'exhibition' => 'عرض',
        'other' => 'إيفنت',
    ];
    $typeColors = [
        'tournament' => 'bg-amber-100 text-amber-800',
        'camp' => 'bg-green-100 text-green-800',
        'competition' => 'bg-red-100 text-red-800',
        'open_day' => 'bg-blue-100 text-blue-800',
        'workshop' => 'bg-purple-100 text-purple-800',
        'friendly' => 'bg-cyan-100 text-cyan-800',
        'exhibition' => 'bg-pink-100 text-pink-800',
        'other' => 'bg-gray-100 text-gray-800',
    ];
@endphp

<div class="site-section">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="text-center mb-12 reveal">
            <h2 class="section-title mx-auto">{{ $section->title ?? 'الإيفنتات والبطولات' }}</h2>
            @if($section->subtitle)
                <p class="mt-4 text-gray-500 text-lg">{{ $section->subtitle }}</p>
            @endif
        </div>

        @if($events->count())
            <div class="{{ $gridClass }}">
                @foreach($events as $event)
                    <a href="{{ route('website.events.show', ['slug' => $academy->slug ?? 'default', 'eventSlug' => $event->slug]) }}"
                       class="site-card {{ $cardExtraClass }} {{ $listMode ? 'flex flex-row' : '' }} overflow-hidden group reveal reveal--delay-{{ ($loop->index % 3) + 1 }} hover:shadow-xl transition-shadow">
                        {{-- Cover --}}
                        @if($event->cover)
                            <div class="relative overflow-hidden {{ $listMode ? 'w-48 shrink-0' : 'aspect-[16/9]' }}">
                                <img src="{{ $event->cover->url }}"
                                     alt="{{ $event->title }}"
                                     class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500"
                                     loading="lazy">
                                <span class="absolute top-3 start-3 px-2 py-0.5 rounded-full text-xs font-medium {{ $typeColors[$event->type->value] ?? 'bg-gray-100 text-gray-800' }}">
                                    {{ $typeLabels[$event->type->value] ?? 'إيفنت' }}
                                </span>
                            </div>
                        @else
                            <div class="relative {{ $listMode ? 'w-48 shrink-0' : 'aspect-[16/9]' }} bg-gradient-to-br from-[var(--site-accent)]/10 to-[var(--site-accent)]/5 flex items-center justify-center">
                                <svg class="w-12 h-12 text-[var(--site-accent)]/30" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M16 4v12l-4-2-4 2V4M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                                <span class="absolute top-3 start-3 px-2 py-0.5 rounded-full text-xs font-medium {{ $typeColors[$event->type->value] ?? 'bg-gray-100 text-gray-800' }}">
                                    {{ $typeLabels[$event->type->value] ?? 'إيفنت' }}
                                </span>
                            </div>
                        @endif

                        <div class="p-5 flex flex-col flex-1">
                            {{-- Date Badge --}}
                            <div class="flex items-center gap-2 mb-2">
                                <div class="flex items-center gap-1.5 text-xs text-[var(--site-accent)] font-medium">
                                    <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                                    {{ $event->starts_at->translatedFormat('j F Y') }}
                                </div>
                                @if($event->starts_at->isFuture())
                                    @php $daysLeft = now()->diffInDays($event->starts_at); @endphp
                                    @if($daysLeft <= 7)
                                        <span class="px-1.5 py-0.5 bg-red-50 text-red-600 text-[10px] font-bold rounded">
                                            باقي {{ $daysLeft }} {{ $daysLeft === 1 ? 'يوم' : 'أيام' }}
                                        </span>
                                    @endif
                                @endif
                            </div>

                            <h3 class="text-lg font-bold text-gray-900 line-clamp-2 group-hover:text-[var(--site-accent)] transition-colors">
                                {{ $event->title }}
                            </h3>

                            {{-- Location --}}
                            @if($event->location_name || $event->facility)
                                <p class="text-gray-400 text-xs mt-1.5 flex items-center gap-1">
                                    <svg class="w-3.5 h-3.5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/></svg>
                                    {{ $event->location_name ?? $event->facility?->name_ar ?? '' }}
                                </p>
                            @endif

                            {{-- Capacity --}}
                            <div class="mt-auto pt-3">
                                @if($event->max_capacity)
                                    @php
                                        $spotsLeft = max(0, $event->max_capacity - $event->registrations_count);
                                        $pct = min(100, round(($event->registrations_count / $event->max_capacity) * 100));
                                    @endphp
                                    <div class="flex items-center justify-between text-xs text-gray-500 mb-1">
                                        <span>{{ $event->registrations_count }}/{{ $event->max_capacity }} مسجل</span>
                                        @if($spotsLeft <= 5 && $spotsLeft > 0)
                                            <span class="text-red-500 font-medium">{{ $spotsLeft }} أماكن متبقية</span>
                                        @endif
                                    </div>
                                    <div class="w-full h-1.5 bg-gray-100 rounded-full overflow-hidden">
                                        <div class="h-full rounded-full transition-all {{ $pct >= 90 ? 'bg-red-500' : ($pct >= 70 ? 'bg-amber-500' : 'bg-[var(--site-accent)]') }}"
                                             style="width: {{ $pct }}%"></div>
                                    </div>
                                @else
                                    <span class="text-xs text-gray-400">{{ $event->registrations_count }} مسجل</span>
                                @endif
                            </div>
                        </div>
                    </a>
                @endforeach
            </div>

            {{-- View All --}}
            @if($events->count() >= 3)
                <div class="text-center mt-8 reveal">
                    <a href="{{ route('website.events.index', ['slug' => $academy->slug ?? 'default']) }}"
                       class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full border border-[var(--site-accent)] text-[var(--site-accent)] text-sm font-medium hover:bg-[var(--site-accent)] hover:text-white transition-colors">
                        عرض كل الإيفنتات
                        <svg class="w-4 h-4 rtl:rotate-180" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 8l4 4m0 0l-4 4m4-4H3"/></svg>
                    </a>
                </div>
            @endif
        @else
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                @for($i = 0; $i < 3; $i++)
                    <div class="skeleton-card">
                        <div class="aspect-[16/9] skeleton-block"></div>
                        <div class="p-5 space-y-3">
                            <div class="skeleton-block h-3 w-20"></div>
                            <div class="skeleton-block h-5 w-3/4"></div>
                            <div class="skeleton-block h-3 w-full"></div>
                        </div>
                    </div>
                @endfor
            </div>
            <div class="empty-state mt-4">
                <p class="text-sm">لا توجد إيفنتات قادمة حالياً</p>
            </div>
        @endif
    </div>
</div>
