@php
    $stats = $data['stats'] ?? [];
    $customStats = $section->getSetting('custom_stats', []);
    $layout = $section->getSetting('layout', 'default');

    $displayStats = [
        'participants' => ['label' => 'طالب نشط', 'icon' => 'M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z'],
        'trainers' => ['label' => 'مدرب محترف', 'icon' => 'M5.121 17.804A13.937 13.937 0 0112 16c2.5 0 4.847.655 6.879 1.804M15 10a3 3 0 11-6 0 3 3 0 016 0zm6 2a9 9 0 11-18 0 9 9 0 0118 0z'],
        'branches' => ['label' => 'فرع', 'icon' => 'M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4'],
        'programs' => ['label' => 'برنامج تدريبي', 'icon' => 'M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253'],
        'activities' => ['label' => 'نشاط رياضي', 'icon' => 'M13 10V3L4 14h7v7l9-11h-7z'],
    ];

    $hasStats = !empty($stats) && array_sum($stats) > 0;
    $hasCustom = !empty($customStats);
@endphp

<div class="stats-band py-16 lg:py-20">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        @if($section->title)
            <div class="text-center mb-12 reveal">
                <h2 class="section-title mx-auto">{{ $section->title ?? 'إنجازاتنا' }}</h2>
                @if($section->subtitle)
                    <p class="mt-4 text-white/60 text-lg">{{ $section->subtitle }}</p>
                @endif
            </div>
        @endif

        @if($hasStats || $hasCustom)
            @php
                $allStats = [];
                foreach ($displayStats as $key => $meta) {
                    if (($stats[$key] ?? 0) > 0) {
                        $allStats[] = ['value' => $stats[$key], 'label' => $meta['label'], 'icon' => $meta['icon'], 'suffix' => '+'];
                    }
                }
                foreach ($customStats as $cs) {
                    if (!empty($cs['value']) && !empty($cs['label'])) {
                        $allStats[] = ['value' => $cs['value'], 'label' => $cs['label'], 'icon' => $cs['icon'] ?? null, 'suffix' => $cs['suffix'] ?? '+'];
                    }
                }
                $colClass = match(count($allStats)) {
                    1, 2 => 'grid-cols-1 sm:grid-cols-2',
                    3 => 'grid-cols-1 sm:grid-cols-3',
                    4 => 'grid-cols-2 lg:grid-cols-4',
                    default => 'grid-cols-2 md:grid-cols-3 lg:grid-cols-' . min(count($allStats), 5),
                };
            @endphp

            @if($layout === 'cards')
                <div class="grid {{ $colClass }} gap-6">
                    @foreach($allStats as $i => $stat)
                        <div class="text-center p-6 rounded-2xl bg-white/5 border border-white/10 hover:bg-white/10 transition-colors reveal reveal--delay-{{ min($i + 1, 4) }}">
                            @if($stat['icon'])
                                <div class="w-12 h-12 mx-auto mb-3 rounded-full bg-[var(--site-accent)]/20 flex items-center justify-center">
                                    <svg class="w-6 h-6 text-[var(--site-accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="{{ $stat['icon'] }}"/>
                                    </svg>
                                </div>
                            @endif
                            <span class="stat-number text-3xl" data-counter="{{ $stat['value'] }}" data-suffix="{{ $stat['suffix'] }}">0</span>
                            <span class="block mt-2 text-white/60 text-sm">{{ $stat['label'] }}</span>
                        </div>
                    @endforeach
                </div>

            @elseif($layout === 'minimal')
                <div class="flex flex-wrap items-center justify-center gap-12 md:gap-16">
                    @foreach($allStats as $i => $stat)
                        <div class="text-center reveal reveal--delay-{{ min($i + 1, 4) }}">
                            <span class="stat-number text-4xl lg:text-5xl" data-counter="{{ $stat['value'] }}" data-suffix="{{ $stat['suffix'] }}">0</span>
                            <span class="block mt-2 text-white/60 text-sm tracking-wide uppercase">{{ $stat['label'] }}</span>
                        </div>
                    @endforeach
                </div>

            @else
                {{-- Default --}}
                <div class="grid {{ $colClass }} gap-8">
                    @foreach($allStats as $i => $stat)
                        <div class="text-center reveal reveal--delay-{{ min($i + 1, 4) }}">
                            @if($stat['icon'])
                                <div class="w-14 h-14 mx-auto mb-4 rounded-xl bg-[var(--site-accent)]/10 flex items-center justify-center">
                                    <svg class="w-7 h-7 text-[var(--site-accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="{{ $stat['icon'] }}"/>
                                    </svg>
                                </div>
                            @endif
                            <span class="stat-number" data-counter="{{ $stat['value'] }}" data-suffix="{{ $stat['suffix'] }}">0</span>
                            <span class="block mt-2 text-white/60 text-sm">{{ $stat['label'] }}</span>
                        </div>
                    @endforeach
                </div>
            @endif
        @else
            <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-8">
                @foreach($displayStats as $key => $meta)
                    <div class="text-center reveal reveal--delay-{{ $loop->index + 1 }}">
                        <div class="w-14 h-14 mx-auto mb-4 rounded-xl bg-white/5 flex items-center justify-center">
                            <svg class="w-7 h-7 text-white/20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="{{ $meta['icon'] }}"/>
                            </svg>
                        </div>
                        <span class="block text-2xl font-bold text-white/20">0+</span>
                        <span class="block mt-2 text-white/30 text-sm">{{ $meta['label'] }}</span>
                    </div>
                @endforeach
            </div>
        @endif
    </div>
</div>
