<div>
    <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('roles.compare') }}" wire:navigate
               class="inline-flex items-center gap-2 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">
                <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="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/></svg>
                <span class="hidden sm:inline">{{ __('مقارنة') }}</span>
            </a>
            <a href="{{ route('roles.matrix') }}" wire:navigate
               class="inline-flex items-center gap-2 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">
                <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="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z"/></svg>
                <span class="hidden sm:inline">{{ __('المصفوفة') }}</span>
            </a>
            @can('roles.create')
            <a href="{{ route('roles.create') }}" wire:navigate
               class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium">
                <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>
    </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
    @if(session('error'))
    <div class="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg text-red-700 text-sm">{{ session('error') }}</div>
    @endif

    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3 sm:gap-4">
        @foreach($roles as $role)
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-5">
            <div class="flex items-start justify-between mb-3">
                <div>
                    <h3 class="font-semibold text-gray-800 text-sm sm:text-base">{{ $role->name_ar }}</h3>
                    <p class="text-xs text-gray-500 font-mono">{{ $role->slug }}</p>
                </div>
                <span class="px-2 py-0.5 text-xs rounded-full ms-2 {{ $role->is_system ? 'bg-amber-100 text-amber-700' : 'bg-blue-100 text-blue-700' }}">
                    {{ $role->is_system ? 'نظام' : 'مخصص' }}
                </span>
            </div>
            <div class="flex items-center justify-between text-xs sm:text-sm text-gray-600">
                <span>المستوى: {{ $role->level }}</span>
                <span>{{ $role->users_count }} مستخدم</span>
            </div>
            @if($canEditRole[$role->id] ?? false)
                <div class="mt-3 pt-3 border-t border-gray-100 flex items-center justify-between">
                    @can('roles.update')
                    <a href="{{ route('roles.edit', $role) }}" wire:navigate class="text-blue-600 hover:text-blue-800 text-sm">{{ __('تعديل') }}</a>
                    @endcan

                    @if(!$role->is_system)
                        @can('roles.delete')
                        @if($confirmingDeleteId === $role->id)
                        <div class="flex items-center gap-2">
                            <button wire:click="deleteRole({{ $role->id }})" class="text-red-600 hover:text-red-800 text-xs font-medium">{{ __('تأكيد الحذف') }}</button>
                            <button wire:click="cancelDelete" class="text-gray-500 hover:text-gray-700 text-xs">{{ __('إلغاء') }}</button>
                        </div>
                        @else
                        <button wire:click="confirmDelete({{ $role->id }})" class="text-red-500 hover:text-red-700 text-xs">{{ __('حذف') }}</button>
                        @endif
                        @endcan
                    @endif
                </div>
            @endif
        </div>
        @endforeach
    </div>
</div>
