@php
    $faqs = $data['faqs'] ?? collect();
    $showCategories = $section->getSetting('show_categories', true);
    $showSearch = $section->getSetting('show_search', true);
    $expandFirst = $section->getSetting('expand_first', true);
    $categories = $faqs->pluck('category')->filter()->unique()->values();
@endphp

<div class="site-section site-section--alt">
    <div class="max-w-3xl 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($faqs->count())
            <div x-data="{
                open: {{ $expandFirst ? $faqs->first()->id : 'null' }},
                search: '',
                activeCategory: 'all',
                get filteredFaqs() {
                    return Array.from(this.$refs.faqList.children).forEach(el => {
                        const q = el.dataset.question?.toLowerCase() || '';
                        const a = el.dataset.answer?.toLowerCase() || '';
                        const cat = el.dataset.category || '';
                        const matchSearch = !this.search || q.includes(this.search.toLowerCase()) || a.includes(this.search.toLowerCase());
                        const matchCat = this.activeCategory === 'all' || cat === this.activeCategory;
                        el.style.display = (matchSearch && matchCat) ? '' : 'none';
                    });
                }
            }" x-init="$watch('search', () => filteredFaqs); $watch('activeCategory', () => filteredFaqs)">

                {{-- Search --}}
                @if($showSearch)
                    <div class="mb-6 reveal">
                        <div class="relative">
                            <svg class="absolute start-4 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                            </svg>
                            <input type="text" x-model="search"
                                   placeholder="ابحث في الأسئلة..."
                                   class="w-full ps-12 pe-4 py-3 rounded-xl border border-gray-200 bg-white focus:ring-2 focus:ring-[var(--site-accent)]/20 focus:border-[var(--site-accent)] text-sm transition-all">
                        </div>
                    </div>
                @endif

                {{-- Category Tabs --}}
                @if($showCategories && $categories->count() > 1)
                    <div class="flex items-center gap-2 mb-6 overflow-x-auto pb-2 scrollbar-hide reveal">
                        <button @click="activeCategory = 'all'"
                                :class="activeCategory === 'all' ? 'bg-[var(--site-accent)] text-white' : 'bg-gray-100 text-gray-600 hover:bg-gray-200'"
                                class="px-4 py-2 rounded-lg text-sm font-medium transition-colors whitespace-nowrap">
                            الكل
                        </button>
                        @foreach($categories as $cat)
                            <button @click="activeCategory = '{{ $cat }}'"
                                    :class="activeCategory === '{{ $cat }}' ? 'bg-[var(--site-accent)] text-white' : 'bg-gray-100 text-gray-600 hover:bg-gray-200'"
                                    class="px-4 py-2 rounded-lg text-sm font-medium transition-colors whitespace-nowrap">
                                {{ $cat }}
                            </button>
                        @endforeach
                    </div>
                @endif

                {{-- FAQ List --}}
                <div class="space-y-3" x-ref="faqList">
                    @foreach($faqs as $faq)
                        <div class="faq-item reveal reveal--delay-{{ min($loop->index + 1, 4) }}"
                             data-question="{{ mb_strtolower($faq->question) }}"
                             data-answer="{{ mb_strtolower($faq->answer) }}"
                             data-category="{{ $faq->category ?? '' }}"
                             :data-open="open === {{ $faq->id }} ? 'true' : 'false'">
                            <button @click="open = open === {{ $faq->id }} ? null : {{ $faq->id }}"
                                    class="w-full flex items-center justify-between p-5 text-start">
                                <span class="text-gray-900 font-medium pe-4">{{ $faq->question }}</span>
                                <svg class="w-5 h-5 text-[var(--site-accent)] shrink-0 transition-transform duration-300"
                                     :class="open === {{ $faq->id }} ? 'rotate-180' : ''"
                                     fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
                                </svg>
                            </button>
                            <div x-show="open === {{ $faq->id }}"
                                 x-collapse
                                 class="px-5 pb-5">
                                <p class="text-gray-600 leading-relaxed">{{ $faq->answer }}</p>
                            </div>
                        </div>
                    @endforeach
                </div>

                {{-- No Results --}}
                <div x-show="search && !Array.from($refs.faqList.children).some(el => el.style.display !== 'none')"
                     x-cloak
                     class="text-center py-8 text-gray-500 text-sm">
                    لا توجد نتائج لـ "<span x-text="search"></span>"
                </div>
            </div>
        @else
            <div class="space-y-3">
                @for($i = 0; $i < 3; $i++)
                    <div class="skeleton-card p-5 flex items-center justify-between">
                        <div class="skeleton-block h-5 w-2/3"></div>
                        <div class="skeleton-block h-5 w-5 !rounded-full"></div>
                    </div>
                @endfor
            </div>
            <div class="empty-state mt-4">
                <p class="text-sm">لم تُضف أسئلة شائعة بعد</p>
            </div>
        @endif
    </div>
</div>
