<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>
        @can('users.create')
        <a href="{{ route('users.create') }}" wire:navigate
           class="inline-flex items-center justify-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium min-h-[44px]">
            <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>
            <span class="hidden sm:inline">إضافة مستخدم</span>
        </a>
        @endcan
    </div>

    <!-- 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-4 gap-3">
            <div class="col-span-2">
                <input type="text" wire:model.live.debounce.300ms="search"
                       placeholder="بحث بالاسم أو البريد..."
                       class="w-full px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
            </div>
            <select wire:model.live="role" class="px-3 py-2 text-sm border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                <option value="">كل الأدوار</option>
                @foreach($roles as $r)
                    <option value="{{ $r->id }}">{{ $r->name_ar }}</option>
                @endforeach
            </select>
            <label class="flex items-center gap-2 cursor-pointer">
                <input type="checkbox" wire:model.live="showInactive" class="w-4 h-4 rounded border-gray-300 text-blue-600">
                <span class="text-sm text-gray-600">عرض غير النشطين</span>
            </label>
        </div>
    </div>

    <!-- Desktop Table -->
    <div class="hidden md:block bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-sm" wire:loading.class="opacity-50 pointer-events-none">
                <thead class="bg-gray-50 border-b border-gray-200">
                    <tr>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">المستخدم</th>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">البريد</th>
                        <th class="px-4 py-3 text-start font-medium text-gray-600">الدور</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">الحالة</th>
                        <th class="px-4 py-3 text-center font-medium text-gray-600">إجراءات</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($users as $u)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <div class="flex items-center gap-3">
                                <div class="w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center">
                                    <span class="text-xs font-medium text-blue-700">{{ mb_substr($u->name_ar ?? $u->name, 0, 1) }}</span>
                                </div>
                                <div>
                                    <p class="font-medium text-gray-800">{{ $u->name_ar ?? $u->name }}</p>
                                    @if($u->is_super_admin)
                                        <span class="text-xs text-red-600 font-medium">Super Admin</span>
                                    @endif
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3 text-gray-600" dir="ltr">{{ $u->email }}</td>
                        <td class="px-4 py-3 text-gray-600">{{ $u->primaryRole?->name_ar ?? '—' }}</td>
                        <td class="px-4 py-3 text-center">
                            @if($u->status === 'active')
                                <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">نشط</span>
                            @elseif($u->status === 'suspended')
                                <span class="px-2 py-0.5 text-xs bg-amber-100 text-amber-700 rounded-full">موقوف</span>
                            @else
                                <span class="px-2 py-0.5 text-xs bg-red-100 text-red-700 rounded-full">غير نشط</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center">
                            @can('users.update')
                            <a href="{{ route('users.edit', $u) }}" wire:navigate
                               class="inline-flex items-center justify-center text-blue-600 hover:text-blue-800 text-sm py-2 min-h-[44px]">تعديل</a>
                            @endcan
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="5" class="px-4 py-8 text-center text-gray-500">لا يوجد مستخدمين</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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

    <!-- Mobile Cards -->
    <div class="md:hidden space-y-3" wire:loading.class="opacity-50 pointer-events-none">
        @forelse($users as $u)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3">
            <div class="flex items-center justify-between">
                <div class="flex items-center gap-3">
                    <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center">
                        <span class="text-sm font-medium text-blue-700">{{ mb_substr($u->name_ar ?? $u->name, 0, 1) }}</span>
                    </div>
                    <div>
                        <p class="font-medium text-gray-800 text-sm">{{ $u->name_ar ?? $u->name }}</p>
                        <p class="text-xs text-gray-500">{{ $u->primaryRole?->name_ar ?? '—' }}</p>
                        @if($u->is_super_admin)
                            <span class="text-xs text-red-600 font-medium">Super Admin</span>
                        @endif
                    </div>
                </div>
                <div class="flex items-center gap-3">
                    @if($u->status === 'active')
                        <span class="px-2 py-0.5 text-xs bg-green-100 text-green-700 rounded-full">نشط</span>
                    @elseif($u->status === 'suspended')
                        <span class="px-2 py-0.5 text-xs bg-amber-100 text-amber-700 rounded-full">موقوف</span>
                    @else
                        <span class="px-2 py-0.5 text-xs bg-red-100 text-red-700 rounded-full">غير نشط</span>
                    @endif
                    @can('users.update')
                    <a href="{{ route('users.edit', $u) }}" wire:navigate
                       class="inline-flex items-center justify-center text-blue-600 hover:text-blue-800 text-sm py-2 min-h-[44px]">تعديل</a>
                    @endcan
                </div>
            </div>
        </div>
        @empty
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 text-center text-gray-500">
            لا يوجد مستخدمين
        </div>
        @endforelse

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