@php
    $branches = $data['branches'] ?? collect();
    $layout = $section->getSetting('layout', 'grid_3');
    $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-[300px] snap-start shrink-0' : '';
    $listMode = $layout === 'list';
@endphp

<div class="site-section site-section--alt">
    <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($branches->count())
            <div class="{{ $gridClass }}">
                @foreach($branches as $branch)
                    @php
                        $branchImage = \App\Domain\Website\Models\Media::withoutGlobalScope('academy')
                            ->where('academy_id', $academy->id)
                            ->where('collection', 'branch_photo')
                            ->where('mediable_type', 'branch')
                            ->where('mediable_id', $branch->id)
                            ->first();
                    @endphp
                    <div class="site-card {{ $cardExtraClass }} {{ $listMode ? 'flex flex-row' : '' }} reveal reveal--delay-{{ ($loop->index % 3) + 1 }}">
                        @if($branchImage)
                            <div class="relative overflow-hidden {{ $listMode ? 'w-48 shrink-0' : 'aspect-[3/2]' }}">
                                <img src="{{ $branchImage->url }}"
                                     alt="{{ $branch->name_ar }}"
                                     class="site-card__image">
                            </div>
                        @endif

                        <div class="p-5">
                            <div class="flex items-start justify-between">
                                <h3 class="text-lg font-bold text-gray-900">{{ $branch->name_ar }}</h3>
                                @if($branch->is_main)
                                    <span class="px-2 py-0.5 text-xs rounded-full bg-blue-50 text-blue-600 border border-blue-100">
                                        رئيسي
                                    </span>
                                @endif
                            </div>

                            @if($branch->address)
                                <p class="mt-2 text-gray-500 text-sm flex items-start gap-2">
                                    <svg class="w-4 h-4 mt-0.5 shrink-0 text-[var(--site-accent)]" 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"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
                                    {{ $branch->address }}
                                </p>
                            @endif

                            @if($branch->phone)
                                <p class="mt-2 text-gray-500 text-sm flex items-center gap-2">
                                    <svg class="w-4 h-4 shrink-0 text-[var(--site-accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/></svg>
                                    <span dir="ltr">{{ $branch->phone }}</span>
                                </p>
                            @endif
                        </div>
                    </div>
                @endforeach
            </div>
        @else
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                @for($i = 0; $i < 3; $i++)
                    <div class="skeleton-card">
                        <div class="aspect-[3/2] skeleton-block"></div>
                        <div class="p-5 space-y-3">
                            <div class="skeleton-block h-5 w-1/2"></div>
                            <div class="skeleton-block h-3 w-3/4"></div>
                            <div class="skeleton-block h-3 w-1/2"></div>
                        </div>
                    </div>
                @endfor
            </div>
            <div class="empty-state mt-4">
                <p class="text-sm">لم تُضف فروع بعد</p>
            </div>
        @endif
    </div>
</div>
