{% extends "dashboard/base.html" %}

{% block title %}Access Management{% endblock %}

{% block content %}
<div class="min-h-screen bg-slate-50 dark:bg-slate-900/50" 
     x-data="{ selectedGroup: {{ groups.0.id if groups else 'null' }} }">
    
    <!-- Page Header -->
    <div class="bg-white dark:bg-slate-800 border-b border-slate-200 dark:border-slate-700">
        <div class="container mx-auto px-4 sm:px-6 lg:px-8 py-6">
            <div class="flex items-center justify-between">
                <div>
                    <h1 class="text-2xl font-bold text-slate-900 dark:text-slate-100">Access Management</h1>
                    <p class="mt-1 text-sm text-slate-600 dark:text-slate-400">
                        Manage user groups, feature permissions, and user assignments
                    </p>
                </div>
                <div class="flex items-center space-x-3">
                    <div class="flex items-center px-3 py-1.5 bg-slate-100 dark:bg-slate-700 rounded-full">
                        <div class="w-2 h-2 bg-green-500 rounded-full mr-2"></div>
                        <span class="text-xs font-medium text-slate-700 dark:text-slate-300">
                            {{ groups|length }} Groups
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="container mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <div class="grid grid-cols-1 xl:grid-cols-5 gap-8">
            
            <!-- Left Sidebar: Group Management -->
            <div class="xl:col-span-1 space-y-6">
                
                <!-- Create Group Card -->
                <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 overflow-hidden">
                    <div class="px-6 py-5 border-b border-slate-100 dark:border-slate-700">
                        <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100 flex items-center">
                            <svg class="w-4 h-4 mr-2 text-indigo-600 dark:text-indigo-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                            </svg>
                            Create Group
                        </h3>
                    </div>
                    <form method="post" class="p-6 space-y-4">
                        {% csrf_token %}
                        <input type="hidden" name="action" value="create_group">
                        
                        <div class="space-y-1">
                            <label for="group_name" class="block text-xs font-medium text-slate-700 dark:text-slate-300 uppercase tracking-wide">
                                Group Name
                            </label>
                            <input type="text" id="group_name" name="group_name" required 
                                   placeholder="Enter group name"
                                   class="w-full px-4 py-3 bg-slate-50 dark:bg-slate-900/50 border border-slate-200 dark:border-slate-600 rounded-xl focus:ring-2 focus:ring-indigo-500 focus:border-transparent text-slate-900 dark:text-slate-100 placeholder-slate-400 dark:placeholder-slate-500 transition-all duration-200">
                        </div>
                        
                        <div class="space-y-1">
                            <label for="description" class="block text-xs font-medium text-slate-700 dark:text-slate-300 uppercase tracking-wide">
                                Description
                            </label>
                            <textarea id="description" name="description" rows="3" 
                                      placeholder="Brief description (optional)"
                                      class="w-full px-4 py-3 bg-slate-50 dark:bg-slate-900/50 border border-slate-200 dark:border-slate-600 rounded-xl focus:ring-2 focus:ring-indigo-500 focus:border-transparent text-slate-900 dark:text-slate-100 placeholder-slate-400 dark:placeholder-slate-500 transition-all duration-200 resize-none"></textarea>
                        </div>
                        
                        <button type="submit" 
                                class="w-full flex items-center justify-center px-4 py-3 bg-indigo-600 hover:bg-indigo-700 text-white font-medium rounded-xl focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-slate-800 transition-all duration-200 shadow-sm hover:shadow-md">
                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                            </svg>
                            Create Group
                        </button>
                    </form>
                </div>

                <!-- Groups List -->
                <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 overflow-hidden">
                    <div class="px-6 py-5 border-b border-slate-100 dark:border-slate-700">
                        <h3 class="text-base font-semibold text-slate-900 dark:text-slate-100 flex items-center">
                            <svg class="w-4 h-4 mr-2 text-slate-600 dark:text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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"></path>
                            </svg>
                            Groups ({{ groups|length }})
                        </h3>
                    </div>
                    
                    {% if groups %}
                    <nav class="p-3 space-y-2 max-h-96 overflow-y-auto">
                        {% for group in groups %}
                        <button @click="selectedGroup = {{ group.id }}" 
                                :class="{ 
                                    'bg-indigo-50 dark:bg-indigo-900/30 border-indigo-200 dark:border-indigo-700 text-indigo-900 dark:text-indigo-100': selectedGroup === {{ group.id }}, 
                                    'bg-slate-50 dark:bg-slate-700/30 border-slate-200 dark:border-slate-600 text-slate-700 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700/50': selectedGroup !== {{ group.id }} 
                                }"
                                class="w-full text-left p-4 rounded-xl border transition-all duration-200 group">
                            <div class="flex items-center justify-between">
                                <div class="flex-1 min-w-0">
                                    <h4 class="font-medium truncate">{{ group.group_name }}</h4>
                                    {% if group.description %}
                                    <p class="text-xs text-slate-500 dark:text-slate-400 mt-1 truncate">{{ group.description }}</p>
                                    {% endif %}
                                </div>
                                <div class="flex items-center space-x-2 ml-3">
                                    {% if group.is_active %}
                                    <div class="w-2 h-2 bg-green-500 rounded-full"></div>
                                    {% else %}
                                    <div class="w-2 h-2 bg-slate-400 rounded-full"></div>
                                    {% endif %}
                                    <span class="text-xs px-2 py-1 bg-slate-100 dark:bg-slate-600 text-slate-600 dark:text-slate-300 rounded-full font-medium">
                                        {{ group.users.count() }}
                                    </span>
                                </div>
                            </div>
                        </button>
                        {% endfor %}
                    </nav>
                    {% else %}
                    <div class="p-8 text-center">
                        <div class="w-12 h-12 mx-auto mb-4 bg-slate-100 dark:bg-slate-700 rounded-full flex items-center justify-center">
                            <svg class="w-6 h-6 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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"></path>
                            </svg>
                        </div>
                        <h3 class="text-sm font-medium text-slate-900 dark:text-slate-100 mb-1">No Groups</h3>
                        <p class="text-xs text-slate-500 dark:text-slate-400">Create your first group to get started</p>
                    </div>
                    {% endif %}
                </div>
            </div>

            <!-- Main Content Area -->
            <div class="xl:col-span-4">
                {% if not groups %}
                <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 p-12 text-center">
                    <div class="w-20 h-20 mx-auto mb-6 bg-indigo-100 dark:bg-indigo-900/30 rounded-2xl flex items-center justify-center">
                        <svg class="w-10 h-10 text-indigo-600 dark:text-indigo-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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"></path>
                        </svg>
                    </div>
                    <h2 class="text-xl font-semibold text-slate-900 dark:text-slate-100 mb-2">Welcome to Access Management</h2>
                    <p class="text-slate-600 dark:text-slate-400 mb-6 max-w-md mx-auto">
                        Create your first group to start managing user permissions and feature access across your application.
                    </p>
                </div>
                {% else %}
                
                {% for group in groups %}
                <div x-show="selectedGroup === {{ group.id }}" x-transition class="space-y-8">
                    
                    <!-- Group Header -->
                    <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 p-8">
                        <div class="flex items-start justify-between">
                            <div class="flex-1">
                                <div class="flex items-center space-x-3 mb-3">
                                    <h1 class="text-2xl font-bold text-slate-900 dark:text-slate-100">{{ group.group_name }}</h1>
                                    {% if group.is_active %}
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-800/30 dark:text-green-300">
                                        <div class="w-1.5 h-1.5 bg-green-500 rounded-full mr-1.5"></div>
                                        Active
                                    </span>
                                    {% else %}
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-slate-100 text-slate-600 dark:bg-slate-700 dark:text-slate-400">
                                        <div class="w-1.5 h-1.5 bg-slate-400 rounded-full mr-1.5"></div>
                                        Inactive
                                    </span>
                                    {% endif %}
                                </div>
                                {% if group.description %}
                                <p class="text-slate-600 dark:text-slate-400 leading-relaxed">{{ group.description }}</p>
                                {% endif %}
                                <div class="flex items-center mt-4 text-sm text-slate-500 dark:text-slate-400">
                                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                                    </svg>
                                    {{ group.users.count() }} members assigned
                                </div>
                            </div>
                            <div class="flex items-center space-x-3 ml-6">
                                <form method="post" class="inline">
                                    {% csrf_token %}
                                    <input type="hidden" name="action" value="toggle_group_status">
                                    <input type="hidden" name="group_id" value="{{ group.id }}">
                                    <button type="submit" 
                                            class="inline-flex items-center px-4 py-2 text-sm font-medium rounded-lg border transition-all duration-200
                                            {% if group.is_active %}
                                                border-amber-200 bg-amber-50 text-amber-700 hover:bg-amber-100 dark:border-amber-700 dark:bg-amber-900/20 dark:text-amber-400 dark:hover:bg-amber-900/40
                                            {% else %}
                                                border-green-200 bg-green-50 text-green-700 hover:bg-green-100 dark:border-green-700 dark:bg-green-900/20 dark:text-green-400 dark:hover:bg-green-900/40
                                            {% endif %}">
                                        {% if group.is_active %}
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 9V5a2 2 0 00-2-2H6a2 2 0 00-2 2v4M4 9h1m0 0V5a2 2 0 012-2h2a2 2 0 012 2v4m-6 0h6m0 0v10a1 1 0 001 1h4a1 1 0 001-1V9M9 9h6"></path>
                                            </svg>
                                            Deactivate
                                        {% else %}
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z"></path>
                                            </svg>
                                            Activate
                                        {% endif %}
                                    </button>
                                </form>
                                <form method="post" onsubmit="return confirm('Are you sure you want to delete this group? This action cannot be undone.');">
                                    {% csrf_token %}
                                    <input type="hidden" name="action" value="delete_group">
                                    <input type="hidden" name="group_id" value="{{ group.id }}">
                                    <button type="submit" 
                                            class="inline-flex items-center px-4 py-2 text-sm font-medium border border-red-200 bg-red-50 text-red-700 rounded-lg hover:bg-red-100 dark:border-red-700 dark:bg-red-900/20 dark:text-red-400 dark:hover:bg-red-900/40 transition-all duration-200">
                                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                                        </svg>
                                        Delete
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>

                    <!-- Feature Permissions -->
                    <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 overflow-hidden">
                        <div class="px-8 py-6 border-b border-slate-100 dark:border-slate-700">
                            <div class="flex items-center justify-between">
                                <div>
                                    <h3 class="text-lg font-semibold text-slate-900 dark:text-slate-100 flex items-center">
                                        <svg class="w-5 h-5 mr-3 text-indigo-600 dark:text-indigo-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path>
                                        </svg>
                                        Feature Permissions
                                    </h3>
                                    <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">
                                        Select features this group can access
                                    </p>
                                </div>
                                {% if features %}
                                <div class="text-xs text-slate-500 dark:text-slate-400">
                                    {{ features|length }} features available
                                </div>
                                {% endif %}
                            </div>
                        </div>
                        
                        <form method="post">
                            {% csrf_token %}
                            <input type="hidden" name="action" value="update_group_permissions">
                            <input type="hidden" name="group_id" value="{{ group.id }}">
                            
                            <div class="p-8">
                                {% if features %}
                                <div class="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-4">
                                    {% for feature in features %}
                                    {% if feature.url_name != 'access_management' %}
                                    <label for="feature-{{ group.id }}-{{ feature.id }}" 
                                           class="relative flex items-start p-4 rounded-xl border-2 border-slate-200 dark:border-slate-600 hover:border-indigo-300 dark:hover:border-indigo-500 cursor-pointer transition-all duration-200 group
                                           {% if not feature.is_active %}opacity-60 cursor-not-allowed{% endif %}">
                                        <div class="flex items-center h-5">
                                            <input id="feature-{{ group.id }}-{{ feature.id }}" 
                                                   type="checkbox" 
                                                   name="features" 
                                                   value="{{ feature.id }}"
                                                   {% if feature in group.features.all() and feature.is_active %}checked{% endif %}
                                                   {% if not feature.is_active %}disabled{% endif %}
                                                   class="w-4 h-4 text-indigo-600 border-slate-300 dark:border-slate-500 rounded focus:ring-2 focus:ring-indigo-500 dark:focus:ring-indigo-400 dark:bg-slate-700">
                                        </div>
                                        <div class="ml-3 flex-1 min-w-0">
                                            <div class="text-sm font-medium text-slate-900 dark:text-slate-100 group-hover:text-indigo-900 dark:group-hover:text-indigo-100 transition-colors">
                                                {{ feature.name }}
                                            </div>
                                            {% if feature.description %}
                                            <div class="text-xs text-slate-500 dark:text-slate-400 mt-1 line-clamp-2">
                                                {{ feature.description }}
                                            </div>
                                            {% endif %}
                                            <div class="flex items-center mt-2 space-x-2">
                                                <code class="text-xs px-2 py-1 bg-slate-100 dark:bg-slate-700 text-slate-600 dark:text-slate-400 rounded font-mono">
                                                    {{ feature.url_name }}
                                                </code>
                                                {% if not feature.is_active %}
                                                <span class="text-xs px-2 py-1 bg-red-100 text-red-600 dark:bg-red-900/30 dark:text-red-400 rounded">
                                                    Disabled
                                                </span>
                                                {% endif %}
                                            </div>
                                        </div>
                                    </label>
                                    {% endif %}
                                    {% endfor %}
                                </div>
                                {% else %}
                                <div class="text-center py-12">
                                    <div class="w-16 h-16 mx-auto mb-4 bg-slate-100 dark:bg-slate-700 rounded-2xl flex items-center justify-center">
                                        <svg class="w-8 h-8 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path>
                                        </svg>
                                    </div>
                                    <h3 class="text-base font-medium text-slate-900 dark:text-slate-100 mb-2">No Features Available</h3>
                                    <p class="text-sm text-slate-500 dark:text-slate-400">Create features first to assign permissions to this group.</p>
                                </div>
                                {% endif %}
                            </div>
                            
                            {% if features %}
                            <div class="px-8 py-6 bg-slate-50 dark:bg-slate-800/50 border-t border-slate-100 dark:border-slate-700 flex justify-end">
                                <button type="submit" 
                                        class="inline-flex items-center px-6 py-3 bg-indigo-600 hover:bg-indigo-700 text-white font-medium rounded-xl focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-slate-800 transition-all duration-200 shadow-sm hover:shadow-md">
                                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
                                    </svg>
                                    Update Permissions
                                </button>
                            </div>
                            {% endif %}
                        </form>
                    </div>

                    <!-- User Assignment -->
                    <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-slate-200 dark:border-slate-700 overflow-hidden">
                        <form method="post" x-data="{ 
                            search: '', 
                            get filteredUsers() {
                                if (this.search.trim().length < 2) return [];
                                const searchLower = this.search.trim().toLowerCase();
                                return Array.from(document.querySelectorAll('[data-user-search]')).filter(el => {
                                    const username = el.getAttribute('data-username').toLowerCase();
                                    const email = el.getAttribute('data-email').toLowerCase();
                                    return username.includes(searchLower) || email.includes(searchLower);
                                });
                            },
                            get resultCount() {
                                return this.filteredUsers.length;
                            }
                        }">
                            {% csrf_token %}
                            <input type="hidden" name="action" value="update_group_users">
                            <input type="hidden" name="group_id" value="{{ group.id }}">
                            
                            <!-- Header -->
                            <div class="px-8 py-6 border-b border-slate-100 dark:border-slate-700">
                                <div class="flex items-center justify-between mb-6">
                                    <div>
                                        <h3 class="text-lg font-semibold text-slate-900 dark:text-slate-100 flex items-center">
                                            <svg class="w-5 h-5 mr-3 text-emerald-600 dark:text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                                            </svg>
                                            User Assignment
                                        </h3>
                                        <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">
                                            Search and assign users to this group
                                        </p>
                                    </div>
                                    <div x-show="search.trim().length >= 2" x-transition class="flex items-center">
                                        <div class="inline-flex items-center px-4 py-2 bg-emerald-100 dark:bg-emerald-900/30 text-emerald-800 dark:text-emerald-300 rounded-xl text-sm font-medium">
                                            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                                            </svg>
                                            <span x-text="resultCount"></span> users found
                                        </div>
                                    </div>
                                </div>
                                
                                <!-- Search Input -->
                                <div class="relative">
                                    <div class="absolute inset-y-0 left-0 flex items-center pl-4">
                                        <svg class="w-5 h-5 text-slate-400 dark:text-slate-500" 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"></path>
                                        </svg>
                                    </div>
                                    <input x-model="search" 
                                           type="text" 
                                           placeholder="Type at least 2 characters to search users..."
                                           class="w-full pl-12 pr-12 py-4 bg-slate-50 dark:bg-slate-900/50 border border-slate-200 dark:border-slate-600 rounded-xl focus:ring-2 focus:ring-emerald-500 focus:border-transparent text-slate-900 dark:text-slate-100 placeholder-slate-400 dark:placeholder-slate-500 transition-all duration-200">
                                    <button x-show="search.length > 0" 
                                            @click="search = ''" 
                                            type="button"
                                            class="absolute inset-y-0 right-0 flex items-center pr-4 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 transition-colors">
                                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                                        </svg>
                                    </button>
                                </div>
                            </div>
                            
                            <!-- User Results -->
                            <div class="p-8 min-h-[300px]">
                                <!-- Search Prompt -->
                                <!-- <div x-show="search.trim().length < 2" class="text-center py-16">
                                    <div class="w-20 h-20 mx-auto mb-6 bg-emerald-100 dark:bg-emerald-900/30 rounded-2xl flex items-center justify-center">
                                        <svg class="w-10 h-10 text-emerald-600 dark:text-emerald-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"></path>
                                        </svg>
                                    </div>
                                    <h3 class="text-lg font-semibold text-slate-900 dark:text-slate-100 mb-3">Search for Users</h3>
                                    <p class="text-slate-500 dark:text-slate-400 max-w-md mx-auto leading-relaxed">
                                        Start typing a username or email address (minimum 2 characters) to find and assign users to this group.
                                    </p>
                                    <div class="mt-6 inline-flex items-center px-4 py-2 bg-slate-100 dark:bg-slate-700 rounded-full text-xs text-slate-600 dark:text-slate-400">
                                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                        </svg>
                                        Optimized for large user bases
                                    </div>
                                </div> -->
                                
                                {% if users %}
                                <!-- Hidden User Data -->
                                <div class="hidden">
                                    {% for user in users %}
                                    {% if user.role != 'superadmin' and user.role != 'super_admin' %}
                                    <div data-user-search 
                                         data-username="{{ user.username|lower }}" 
                                         data-email="{{ user.email|lower }}"></div>
                                    {% endif %}
                                    {% endfor %}
                                </div>
                                
                                <!-- User Grid -->
                                <div x-show="search.trim().length >= 2" class="grid grid-cols-1 lg:grid-cols-2 gap-4">
                                    {% for user in users %}
                                    {% if user.role != 'superadmin' and user.role != 'super_admin' %}
                                    <label for="user-{{ group.id }}-{{ user.id }}"
                                           x-show="search.trim().length >= 2 && ('{{ user.username|lower }}'.includes(search.trim().toLowerCase()) || '{{ user.email|lower }}'.includes(search.trim().toLowerCase()))"
                                           x-transition
                                           class="relative flex items-center p-4 border-2 border-slate-200 dark:border-slate-600 rounded-xl hover:border-emerald-300 dark:hover:border-emerald-500 cursor-pointer transition-all duration-200 group">
                                        <input id="user-{{ group.id }}-{{ user.id }}" 
                                               type="checkbox" 
                                               name="users" 
                                               value="{{ user.id }}"
                                               {% if user in group.users.all() %}checked{% endif %}
                                               class="w-4 h-4 text-emerald-600 border-slate-300 dark:border-slate-500 rounded focus:ring-2 focus:ring-emerald-500 dark:focus:ring-emerald-400 dark:bg-slate-700">
                                        <div class="ml-4 flex-1 min-w-0">
                                            <div class="text-sm font-medium text-slate-900 dark:text-slate-100 group-hover:text-emerald-900 dark:group-hover:text-emerald-100 transition-colors">
                                                {{ user.username }}
                                            </div>
                                            <div class="text-xs text-slate-500 dark:text-slate-400 truncate">
                                                {{ user.email }}
                                            </div>
                                            <div class="mt-2">
                                                <span class="inline-flex items-center px-2 py-1 bg-slate-100 dark:bg-slate-700 text-slate-700 dark:text-slate-300 rounded text-xs font-medium">
                                                    {{ user.role|title }}
                                                </span>
                                            </div>
                                        </div>
                                        {% if user in group.users.all() %}
                                        <div class="ml-4 text-emerald-600 dark:text-emerald-400">
                                            <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20">
                                                <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"></path>
                                            </svg>
                                        </div>
                                        {% endif %}
                                    </label>
                                    {% endif %}
                                    {% endfor %}
                                </div>
                                {% endif %}
                            </div>
                            
                            <!-- Footer -->
                            {% if users %}
                            <div x-show="search.trim().length >= 2 && resultCount > 0" 
                                 class="px-8 py-6 bg-slate-50 dark:bg-slate-800/50 border-t border-slate-100 dark:border-slate-700 flex items-center justify-between">
                                <p class="text-sm text-slate-500 dark:text-slate-400">
                                    Select users and save changes to update group membership
                                </p>
                                <button type="submit" 
                                        class="inline-flex items-center px-6 py-3 bg-emerald-600 hover:bg-emerald-700 text-white font-medium rounded-xl focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-emerald-500 dark:focus:ring-offset-slate-800 transition-all duration-200 shadow-sm hover:shadow-md">
                                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
                                    </svg>
                                    Update Users
                                </button>
                            </div>
                            {% endif %}
                        </form>
                    </div>
                </div>
                {% endfor %}
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
{% endblock %}