<div>
    <!-- Header -->
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('المشتركين') }}</h1>
        <div class="flex items-center gap-2">
            <a href="{{ route('export.participants', ['status' => $status, 'search' => $search, 'activity' => $activity, 'skill_level' => $skillLevel, 'membership_type' => $membershipType, 'is_free' => $isFree, 'sort_by' => $sortBy, 'sort_dir' => $sortDir]) }}"
               class="inline-flex items-center gap-2 px-3 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 text-xs sm:text-sm font-medium transition-colors">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
                <span class="hidden sm:inline">{{ __('تصدير CSV') }}</span>
            </a>
            @can('participants.create')
            <a href="{{ route('participants.create') }}" wire:navigate
               class="inline-flex items-center gap-2 px-3 sm:px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-xs sm:text-sm font-medium transition-colors">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
                {{ __('تسجيل مشترك') }}
            </a>
            @endcan
        </div>
    </div>

    {{-- Flash Messages --}}
    @if(session('success'))
    <div class="mb-4 p-3 bg-green-50 border border-green-200 rounded-lg text-green-700 text-sm">
        {{ session('success') }}
    </div>
    @endif

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3 sm:p-4 mb-4">
        <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3">
            <div class="col-span-2 md:col-span-3 lg:col-span-4">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="{{ __('بحث بالاسم، الرقم القومي، :membership_id، أو الهاتف...', ['membership_id' => term('membership_id')]) }}"
                       class="w-full px-3 sm:px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm">
            </div>
            <select wire:model.live="status" class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm">
                <option value="">{{ __('كل الحالات') }}</option>
                @foreach($statusOptions as $value => $label)
                <option value="{{ $value }}">{{ __($label) }}</option>
                @endforeach
            </select>
            <select wire:model.live="activity" class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm">
                <option value="">{{ __('كل الأنشطة') }}</option>
                @foreach($activities as $act)
                <option value="{{ $act->id }}">{{ $act->name_ar }}</option>
                @endforeach
            </select>
            <select wire:model.live="membershipType" class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm">
                <option value="">{{ __('كل') }} {{ term('membership_types') }}</option>
                @foreach($membershipTypeOptions as $value => $label)
                <option value="{{ $value }}">{{ $label }}</option>
                @endforeach
            </select>
            <select wire:model.live="isFree" class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm">
                <option value="">{{ __('الكل') }}</option>
                @foreach($isFreeOptions as $value => $label)
                <option value="{{ $value }}">{{ __($label) }}</option>
                @endforeach
            </select>
            <select wire:model.live="skillLevel" class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm">
                <option value="">{{ __('كل المستويات') }}</option>
                @foreach($skillLevelOptions as $value => $label)
                <option value="{{ $value }}">{{ __($label) }}</option>
                @endforeach
            </select>
        </div>
    </div>

    {{-- Desktop Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden hidden md:block">
        <div wire:loading.class="opacity-50 pointer-events-none">
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <x-ui.sort-header column="participant_number" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('رقم المشترك') }}</x-ui.sort-header>
                            <x-ui.sort-header column="name_ar" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('الاسم') }}</x-ui.sort-header>
                            <x-ui.sort-header column="primary_activity_id" :sortBy="$sortBy" :sortDir="$sortDir">{{ __('النشاط') }}</x-ui.sort-header>
                            <x-ui.sort-header column="membership_type" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ term('membership') }}</x-ui.sort-header>
                            <x-ui.sort-header column="status" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('الحالة') }}</x-ui.sort-header>
                            <x-ui.sort-header column="created_at" :sortBy="$sortBy" :sortDir="$sortDir" align="center">{{ __('تاريخ التسجيل') }}</x-ui.sort-header>
                            <th class="px-4 py-3 text-center font-medium text-gray-600">{{ __('إجراءات') }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        @forelse($participants as $participant)
                        <tr class="hover:bg-gray-50 cursor-pointer" onclick="window.location='{{ route('participants.show', $participant) }}'">
                            <td class="px-4 py-3 font-mono text-gray-600" dir="ltr">
                                {{ $participant->participant_number }}
                            </td>
                            <td class="px-4 py-3">
                                <div class="flex items-center gap-2">
                                    <a href="{{ route('participants.show', $participant) }}" wire:navigate class="font-medium text-gray-800 hover:text-blue-600">{{ $participant->person?->name_ar }}</a>
                                    @if($participant->is_free) <x-ui.free-player-badge :small="true" /> @endif
                                </div>
                                @if($participant->person?->name)
                                <p class="text-xs text-gray-500" dir="ltr">{{ $participant->person->name }}</p>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-gray-600">{{ $participant->primaryActivity?->name_ar ?? '—' }}</td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $membershipValue = $participant->membership_type?->value ?? $participant->membership_type;
                                @endphp
                                @if($membershipValue === 'member')
                                    <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">{{ term('member') }}</span>
                                    @if($participant->membership_id)
                                    <p class="text-xs text-gray-500 mt-0.5 font-mono" dir="ltr">{{ $participant->membership_id }}</p>
                                    @endif
                                @elseif($membershipValue === 'non_member')
                                    <span class="px-2 py-0.5 text-xs bg-gray-100 text-gray-600 rounded-full">{{ term('non_member') }}</span>
                                @else
                                    <span class="text-gray-400">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $statusValue = $participant->status->value ?? $participant->status;
                                    $statusColors = [
                                        'registered' => 'blue', 'active' => 'green', 'frozen' => 'cyan',
                                        'suspended' => 'red', 'inactive' => 'gray', 'graduated' => 'purple',
                                        'transferred' => 'amber', 'withdrawn' => 'orange', 'blacklisted' => 'rose',
                                    ];
                                    $color = $statusColors[$statusValue] ?? 'gray';
                                @endphp
                                <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full">
                                    {{ __($statusOptions[$statusValue] ?? $statusValue) }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-center text-gray-600" dir="ltr">{{ $participant->registration_date?->format('Y-m-d') ?? '—' }}</td>
                            <td class="px-4 py-3 text-center">
                                <div class="flex items-center justify-center gap-3">
                                    @can('participants.view')
                                    <a href="{{ route('participants.show', $participant) }}" wire:navigate class="text-blue-600 hover:text-blue-800 text-sm font-medium">{{ __('عرض') }}</a>
                                    @endcan
                                    @can('participants.update')
                                    <a href="{{ route('participants.edit', $participant) }}" wire:navigate class="text-green-600 hover:text-green-800 text-sm font-medium">{{ __('تعديل') }}</a>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="px-4 py-12 text-center">
                                <div class="flex flex-col items-center">
                                    <svg class="w-12 h-12 text-gray-300 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="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"/>
                                    </svg>
                                    <p class="text-gray-500 text-sm">{{ __('لا يوجد مشتركين') }}</p>
                                    @can('participants.create')
                                    <a href="{{ route('participants.create') }}" wire:navigate class="mt-2 text-blue-600 hover:text-blue-800 text-sm font-medium">{{ __('تسجيل أول مشترك') }}</a>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        @if($participants->hasPages())
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $participants->links() }}
        </div>
        @endif
    </div>

    {{-- Mobile Card List --}}
    <div class="md:hidden space-y-3" wire:loading.class="opacity-50 pointer-events-none">
        @forelse($participants as $participant)
        @php
            $statusValue = $participant->status->value ?? $participant->status;
            $statusColors = [
                'registered' => 'blue', 'active' => 'green', 'frozen' => 'cyan',
                'suspended' => 'red', 'inactive' => 'gray', 'graduated' => 'purple',
                'transferred' => 'amber', 'withdrawn' => 'orange', 'blacklisted' => 'rose',
            ];
            $color = $statusColors[$statusValue] ?? 'gray';
        @endphp
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 hover:border-blue-300 transition">
            <div class="flex items-start justify-between gap-3">
                <div class="flex-1 min-w-0">
                    <div class="flex items-center gap-2">
                        <p class="font-semibold text-gray-800 truncate">{{ $participant->person?->name_ar }}</p>
                        @if($participant->is_free) <x-ui.free-player-badge :small="true" /> @endif
                    </div>
                    <p class="text-xs text-gray-500 font-mono mt-0.5" dir="ltr">{{ $participant->participant_number }}</p>
                </div>
                <span class="px-2 py-0.5 text-xs bg-{{ $color }}-100 text-{{ $color }}-700 rounded-full whitespace-nowrap">
                    {{ __($statusOptions[$statusValue] ?? $statusValue) }}
                </span>
            </div>

            <div class="mt-3 flex items-center flex-wrap gap-2 text-xs text-gray-500">
                @if($participant->primaryActivity?->name_ar)
                <span class="truncate">{{ $participant->primaryActivity->name_ar }}</span>
                @endif
                @php $mobileMemType = $participant->membership_type?->value ?? $participant->membership_type; @endphp
                @if($mobileMemType === 'member')
                <span class="px-1.5 py-0.5 bg-green-100 text-green-700 rounded text-[10px]">{{ term('member') }}{{ $participant->membership_id ? ' #'.$participant->membership_id : '' }}</span>
                @endif
                @if($participant->registration_date)
                <span dir="ltr" class="whitespace-nowrap">{{ $participant->registration_date->format('Y-m-d') }}</span>
                @endif
            </div>

            <div class="mt-3 pt-3 border-t border-gray-100 flex items-center gap-4">
                @can('participants.view')
                <a href="{{ route('participants.show', $participant) }}" wire:navigate
                   class="flex-1 text-center py-2 text-sm font-medium text-blue-600 hover:text-blue-800 bg-blue-50 rounded-lg transition-colors">
                    {{ __('عرض') }}
                </a>
                @endcan
                @can('participants.update')
                <a href="{{ route('participants.edit', $participant) }}" wire:navigate
                   class="flex-1 text-center py-2 text-sm font-medium text-green-600 hover:text-green-800 bg-green-50 rounded-lg transition-colors">
                    {{ __('تعديل') }}
                </a>
                @endcan
            </div>
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-8 text-center">
            <svg class="w-12 h-12 text-gray-300 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="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"/>
            </svg>
            <p class="text-gray-500 text-sm">{{ __('لا يوجد مشتركين') }}</p>
            @can('participants.create')
            <a href="{{ route('participants.create') }}" wire:navigate class="mt-2 inline-block text-blue-600 hover:text-blue-800 text-sm font-medium">{{ __('تسجيل أول مشترك') }}</a>
            @endcan
        </div>
        @endforelse

        @if($participants->hasPages())
        <div class="mt-4">
            {{ $participants->links() }}
        </div>
        @endif
    </div>
</div>
