<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Client Manager</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Lucide Icons -->
    <script src="https://unpkg.com/lucide@latest"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
    
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    fontFamily: {
                        sans: ['Inter', 'sans-serif'],
                    },
                    colors: {
                        gray: {
                            50: '#f9fafb',
                            100: '#f3f4f6',
                            200: '#e5e7eb',
                            300: '#d1d5db',
                            400: '#9ca3af',
                            500: '#6b7280',
                            600: '#4b5563',
                            700: '#374151',
                            800: '#1f2937',
                            900: '#111827',
                        },
                        primary: {
                            50: '#eff6ff',
                            100: '#dbeafe',
                            500: '#3b82f6',
                            600: '#2563eb',
                            700: '#1d4ed8',
                        }
                    },
                    boxShadow: {
                        'xs': '0 1px 2px 0 rgba(0, 0, 0, 0.05)',
                        'sm': '0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06)',
                    }
                }
            }
        }
    </script>
    <style>
        body { 
            background-color: #f9fafb;
            -webkit-font-smoothing: antialiased;
        }
        
        /* View Transitions */
        .hidden-view { display: none; }
        .visible-view { display: block; animation: fadeIn 0.3s ease-out; }
        @keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }

        /* Form Elements */
        .form-label {
            @apply block text-xs font-semibold text-gray-500 uppercase tracking-wide mb-1.5;
        }
        
        .form-input, .form-select, .form-textarea {
            @apply block w-full rounded-md border-gray-300 bg-white shadow-xs focus:border-primary-500 focus:ring-primary-500 sm:text-sm px-3 py-2.5 border transition-colors outline-none text-gray-700;
        }
        
        .form-input:hover, .form-select:hover, .form-textarea:hover {
            @apply border-gray-400;
        }

        /* Section Styling */
        .section-container {
            @apply bg-white border border-gray-200 rounded-lg shadow-sm overflow-hidden mb-6;
        }
        
        .section-header {
            @apply px-6 py-4 border-b border-gray-200 bg-white flex items-center justify-between;
        }
        
        .section-title {
            @apply text-base font-semibold text-gray-900;
        }

        /* Toggle Pills Styles */
        .permission-pill {
            @apply flex items-center justify-center px-3 py-1.5 rounded-md text-xs font-medium border transition-all cursor-pointer select-none;
        }
        /* State: Inactive */
        .permission-pill-inactive {
            @apply bg-white border-gray-200 text-gray-500 hover:border-gray-300 hover:bg-gray-50;
        }
        /* State: Active (Checked via Peer) */
        .peer:checked + .permission-pill-content {
            @apply bg-primary-50 border-primary-200 text-primary-700 shadow-sm;
        }
        /* Specific hover for active state handled by logic or simple CSS if needed, 
           but the peer selector handles the main switch. */

    </style>
</head>
<body class="flex flex-col min-h-screen">

    <!-- Navbar -->
    <nav class="bg-white border-b border-gray-200 sticky top-0 z-30">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <div class="flex items-center gap-6">
                    <div class="flex items-center gap-2 cursor-pointer" onclick="showList()">
                        <div class="w-8 h-8 bg-gray-900 rounded-lg flex items-center justify-center text-white font-bold text-lg">
                            N
                        </div>
                        <span class="font-bold text-xl text-gray-900 tracking-tight">Nexus<span class="text-primary-600">CRM</span></span>
                    </div>
                    <div class="hidden md:flex h-full space-x-8">
                        <a href="#" onclick="showList()" class="border-primary-600 text-gray-900 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium">Clients</a>
                        <a href="#" class="border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium">Dashboard</a>
                    </div>
                </div>
                <div class="flex items-center gap-4">
                    <button class="text-gray-400 hover:text-gray-500"><i data-lucide="bell" class="w-5 h-5"></i></button>
                    <div class="h-9 w-9 rounded-full bg-gray-100 border border-gray-200 flex items-center justify-center text-sm font-semibold text-gray-600">
                        AD
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <main class="flex-grow py-8 px-4 sm:px-6 lg:px-8">
        
        <!-- ================= LIST VIEW ================= -->
        <div id="listView" class="max-w-7xl mx-auto visible-view">
            <div class="sm:flex sm:items-center sm:justify-between mb-8">
                <div>
                    <h1 class="text-2xl font-bold text-gray-900">Clients</h1>
                    <p class="mt-1 text-sm text-gray-500">Manage client profiles and access rights.</p>
                </div>
                <div class="mt-4 sm:mt-0">
                    <button type="button" onclick="showCreate()" class="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-lg text-white bg-gray-900 hover:bg-gray-800 focus:outline-none">
                        <i data-lucide="plus" class="w-4 h-4 mr-2"></i> New Client
                    </button>
                </div>
            </div>
            
            <!-- Placeholder List Table -->
            <div class="bg-white border border-gray-200 rounded-lg shadow-sm overflow-hidden p-12 text-center text-gray-500">
                <p>Client list view hidden for brevity. Click "New Client" to see the form design.</p>
            </div>
        </div>

        <!-- ================= CREATE VIEW ================= -->
        <div id="createView" class="max-w-5xl mx-auto hidden-view">
            <form id="update_client" action="#" method="post">
                
                <!-- Form Header -->
                <div class="flex items-center justify-between mb-8 pb-5 border-b border-gray-200">
                    <div class="flex items-center gap-4">
                        <button type="button" onclick="showList()" class="p-2 -ml-2 rounded-full hover:bg-gray-100 text-gray-500 transition-colors">
                            <i data-lucide="arrow-left" class="w-5 h-5"></i>
                        </button>
                        <div>
                            <h1 class="text-2xl font-bold text-gray-900">Add New Client</h1>
                            <p class="text-sm text-gray-500">Configure client details and access permissions.</p>
                        </div>
                    </div>
                    <div class="flex gap-3">
                        <button type="button" onclick="showList()" class="px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-lg text-gray-700 bg-white hover:bg-gray-50 focus:outline-none">
                            Cancel
                        </button>
                        <button type="submit" class="px-5 py-2 border border-transparent shadow-sm text-sm font-medium rounded-lg text-white bg-primary-600 hover:bg-primary-700 focus:outline-none">
                            Save Client
                        </button>
                    </div>
                </div>

                <div class="grid grid-cols-1 lg:grid-cols-12 gap-8">
                    
                    <!-- Left Column (Inputs) -->
                    <div class="lg:col-span-8 space-y-6">
                        
                        <!-- General Info -->
                        <div class="section-container">
                            <div class="section-header">
                                <h3 class="section-title">General Information</h3>
                            </div>
                            <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                                <div class="col-span-2 md:col-span-1">
                                    <label class="form-label">Company Name <span class="text-red-500">*</span></label>
                                    <input type="text" name="name" class="form-input" required placeholder="e.g. Acme Corp">
                                </div>
                                <div class="col-span-2 md:col-span-1">
                                    <label class="form-label">Email Address <span class="text-red-500">*</span></label>
                                    <input type="email" name="email" class="form-input" required placeholder="contact@acme.com">
                                </div>
                                <div class="col-span-2">
                                    <label class="form-label">Short Note</label>
                                    <textarea name="short_note" rows="2" class="form-textarea" placeholder="Internal notes..."></textarea>
                                </div>
                            </div>
                        </div>

                        <!-- Access Control / Permissions -->
                        <div class="section-container border-primary-100 ring-4 ring-primary-50/50">
                            <div class="section-header bg-gray-50/50">
                                <h3 class="section-title flex items-center gap-2">
                                    <i data-lucide="shield" class="w-4 h-4 text-primary-600"></i> Access Control
                                </h3>
                            </div>
                            <div class="p-6">
                                
                                <!-- Permission Toggle Cards -->
                                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
                                    <!-- Option 1: Everyone -->
                                    <label class="relative flex cursor-pointer rounded-xl border border-gray-200 bg-white p-4 shadow-sm focus:outline-none hover:border-primary-400 hover:bg-primary-50/10 transition-all peer-checked:border-primary-600 peer-checked:ring-1 peer-checked:ring-primary-600">
                                        <input type="radio" name="permission_type" value="everyone" class="sr-only" checked onchange="togglePermissions(this)">
                                        <span class="flex flex-1">
                                            <span class="flex flex-col">
                                                <span class="block text-sm font-medium text-gray-900 mb-1">Everyone</span>
                                                <span class="block text-xs text-gray-500">Visible to all admins and staff with client access.</span>
                                            </span>
                                        </span>
                                        <div class="check-icon hidden text-primary-600">
                                            <i data-lucide="check-circle-2" class="w-5 h-5"></i>
                                        </div>
                                        <div class="uncheck-icon block text-gray-300">
                                            <i data-lucide="circle" class="w-5 h-5"></i>
                                        </div>
                                    </label>

                                    <!-- Option 2: Custom -->
                                    <label class="relative flex cursor-pointer rounded-xl border border-gray-200 bg-white p-4 shadow-sm focus:outline-none hover:border-primary-400 hover:bg-primary-50/10 transition-all">
                                        <input type="radio" name="permission_type" value="custom" class="sr-only" onchange="togglePermissions(this)">
                                        <span class="flex flex-1">
                                            <span class="flex flex-col">
                                                <span class="block text-sm font-medium text-gray-900 mb-1">Restricted Access</span>
                                                <span class="block text-xs text-gray-500">Only specific users can view or manage this client.</span>
                                            </span>
                                        </span>
                                        <div class="check-icon hidden text-primary-600">
                                            <i data-lucide="check-circle-2" class="w-5 h-5"></i>
                                        </div>
                                        <div class="uncheck-icon block text-gray-300">
                                            <i data-lucide="circle" class="w-5 h-5"></i>
                                        </div>
                                    </label>
                                </div>

                                <!-- Custom Permission Logic Area -->
                                <div id="custom_permissions_area" class="hidden animate-fade-in space-y-4">
                                    
                                    <!-- Modern User Selector Bar -->
                                    <div class="flex gap-2">
                                        <div class="relative flex-grow">
                                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none text-gray-400">
                                                <i data-lucide="user-plus" class="w-4 h-4"></i>
                                            </div>
                                            <select id="user_select" class="block w-full pl-10 pr-10 py-2.5 text-sm border-gray-300 focus:ring-primary-500 focus:border-primary-500 rounded-lg shadow-sm bg-white cursor-pointer appearance-none">
                                                <option value="">Select a user to grant access...</option>
                                                <option value="1" data-name="Admin User" data-role="Administrator">Admin User (Administrator)</option>
                                                <option value="2" data-name="Sarah Sales" data-role="Sales Lead">Sarah Sales (Sales Lead)</option>
                                                <option value="3" data-name="Mike Tech" data-role="Support">Mike Tech (Support)</option>
                                                <option value="4" data-name="Jenny Finance" data-role="Accountant">Jenny Finance (Accountant)</option>
                                            </select>
                                            <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none text-gray-400">
                                                <i data-lucide="chevron-down" class="w-4 h-4"></i>
                                            </div>
                                        </div>
                                        <button type="button" onclick="addUserRow()" class="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-lg text-white bg-gray-900 hover:bg-gray-800 focus:outline-none transition-colors">
                                            Add
                                        </button>
                                    </div>

                                    <!-- Permission Cards Container -->
                                    <div id="permission_list_body" class="space-y-3 mt-4">
                                        <!-- Empty State -->
                                        <div id="empty_state_row" class="text-center py-10 border-2 border-dashed border-gray-200 rounded-xl bg-gray-50/50">
                                            <div class="inline-flex items-center justify-center w-12 h-12 rounded-full bg-white border border-gray-100 mb-3 shadow-sm">
                                                <i data-lucide="users" class="w-5 h-5 text-gray-400"></i>
                                            </div>
                                            <h3 class="text-sm font-medium text-gray-900">No users assigned</h3>
                                            <p class="text-xs text-gray-500 mt-1">Select a user above to grant custom permissions.</p>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>

                    </div>

                    <!-- Right Column (Settings) -->
                    <div class="lg:col-span-4 space-y-6">
                        
                        <!-- Settings -->
                        <div class="section-container">
                            <div class="section-header">
                                <h3 class="section-title">Settings</h3>
                            </div>
                            <div class="p-6 space-y-5">
                                <div>
                                    <label class="form-label">Customer Group</label>
                                    <select name="group" class="form-select">
                                        <option value="1">Standard</option>
                                        <option value="2">VIP</option>
                                        <option value="3">Wholesale</option>
                                    </select>
                                </div>
                                <div>
                                    <label class="form-label">Language</label>
                                    <select name="language" class="form-select">
                                        <option value="en">English</option>
                                        <option value="es">Spanish</option>
                                    </select>
                                </div>
                                <div>
                                    <label class="form-label">Currency</label>
                                    <select name="currency" class="form-select">
                                        <option value="USD">USD ($)</option>
                                        <option value="EUR">EUR (€)</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>

                <div class="h-20"></div>
            </form>
        </div>

    </main>

    <script>
        // Initialize Lucide icons
        document.addEventListener('DOMContentLoaded', () => {
            lucide.createIcons();
            
            // Initial toggle state check
            const checkedRadio = document.querySelector('input[name="permission_type"]:checked');
            if(checkedRadio) togglePermissions(checkedRadio);
        });

        // Navigation View Switching
        function showList() {
            document.getElementById('createView').classList.add('hidden-view');
            document.getElementById('createView').classList.remove('visible-view');
            
            document.getElementById('listView').classList.remove('hidden-view');
            document.getElementById('listView').classList.add('visible-view');
            window.scrollTo({ top: 0, behavior: 'smooth' });
        }

        function showCreate() {
            document.getElementById('listView').classList.add('hidden-view');
            document.getElementById('listView').classList.remove('visible-view');
            
            document.getElementById('createView').classList.remove('hidden-view');
            document.getElementById('createView').classList.add('visible-view');
            window.scrollTo({ top: 0, behavior: 'smooth' });
        }

        // --- Permission Logic ---
        
        function togglePermissions(radio) {
            // Visual Active State for Radios
            document.querySelectorAll('input[name="permission_type"]').forEach(r => {
                const label = r.parentElement;
                const check = label.querySelector('.check-icon');
                const uncheck = label.querySelector('.uncheck-icon');
                
                if (r.checked) {
                    label.classList.add('border-primary-600', 'ring-1', 'ring-primary-600', 'bg-primary-50', 'bg-opacity-10');
                    check.classList.remove('hidden');
                    uncheck.classList.add('hidden');
                } else {
                    label.classList.remove('border-primary-600', 'ring-1', 'ring-primary-600', 'bg-primary-50', 'bg-opacity-10');
                    check.classList.add('hidden');
                    uncheck.classList.remove('hidden');
                }
            });

            // Show/Hide Custom Area
            const customArea = document.getElementById('custom_permissions_area');
            if (radio.value === 'custom') {
                customArea.classList.remove('hidden');
            } else {
                customArea.classList.add('hidden');
            }
        }

        function addUserRow() {
            const select = document.getElementById('user_select');
            const userId = select.value;
            const userName = select.options[select.selectedIndex]?.dataset.name;
            const userRole = select.options[select.selectedIndex]?.dataset.role;
            const container = document.getElementById('permission_list_body');
            const emptyState = document.getElementById('empty_state_row');

            if (!userId) return;

            // Hide empty state if visible
            if (emptyState) emptyState.style.display = 'none';

            // Check if user already exists
            if (document.getElementById(`perm_card_${userId}`)) {
                alert('User already added to permissions.');
                return;
            }

            // Create Modern Card Row
            const rowDiv = document.createElement('div');
            rowDiv.id = `perm_card_${userId}`;
            rowDiv.className = "flex flex-col sm:flex-row sm:items-center justify-between p-4 bg-white border border-gray-200 rounded-xl shadow-sm hover:shadow-md hover:border-gray-300 transition-all animate-fade-in gap-4";
            
            rowDiv.innerHTML = `
                <!-- User Info -->
                <div class="flex items-center min-w-0 flex-1">
                    <div class="flex-shrink-0 h-10 w-10 rounded-full bg-gray-100 flex items-center justify-center text-sm font-bold text-gray-600 border border-gray-200">
                        ${userName.charAt(0)}
                    </div>
                    <div class="ml-3 truncate">
                        <div class="text-sm font-semibold text-gray-900">${userName}</div>
                        <div class="text-xs text-gray-500">${userRole}</div>
                    </div>
                    <input type="hidden" name="assigned_user[]" value="${userId}">
                </div>
                
                <!-- Pill Toggles -->
                <div class="flex items-center gap-2">
                    <!-- View Pill -->
                    <label class="cursor-pointer group">
                        <input type="checkbox" name="perm_view_${userId}" value="1" checked class="peer sr-only">
                        <div class="permission-pill-content flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-xs font-medium text-gray-500 hover:bg-gray-50 transition-all peer-checked:bg-blue-50 peer-checked:border-blue-200 peer-checked:text-blue-700">
                            <i data-lucide="eye" class="w-3.5 h-3.5"></i>
                            <span>View</span>
                        </div>
                    </label>

                    <!-- Edit Pill -->
                    <label class="cursor-pointer group">
                        <input type="checkbox" name="perm_edit_${userId}" value="1" class="peer sr-only">
                        <div class="permission-pill-content flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-xs font-medium text-gray-500 hover:bg-gray-50 transition-all peer-checked:bg-blue-50 peer-checked:border-blue-200 peer-checked:text-blue-700">
                            <i data-lucide="edit-2" class="w-3.5 h-3.5"></i>
                            <span>Edit</span>
                        </div>
                    </label>

                    <!-- Delete Pill -->
                    <label class="cursor-pointer group">
                        <input type="checkbox" name="perm_delete_${userId}" value="1" class="peer sr-only">
                        <div class="permission-pill-content flex items-center gap-1.5 px-3 py-1.5 rounded-lg border border-gray-200 bg-white text-xs font-medium text-gray-500 hover:bg-gray-50 transition-all peer-checked:bg-red-50 peer-checked:border-red-200 peer-checked:text-red-700">
                            <i data-lucide="trash" class="w-3.5 h-3.5"></i>
                            <span>Delete</span>
                        </div>
                    </label>

                    <div class="h-4 w-px bg-gray-200 mx-2"></div>

                    <!-- Remove Button -->
                    <button type="button" onclick="removeUserRow('${userId}')" class="p-2 rounded-lg text-gray-400 hover:text-red-600 hover:bg-red-50 transition-colors" title="Remove User">
                        <i data-lucide="x" class="w-4 h-4"></i>
                    </button>
                </div>
            `;

            container.appendChild(rowDiv);

            // Re-run Lucide
            lucide.createIcons();

            // Reset Select
            select.value = "";
        }

        function removeUserRow(userId) {
            const row = document.getElementById(`perm_card_${userId}`);
            if (row) {
                row.style.opacity = '0';
                row.style.transform = 'scale(0.95)';
                setTimeout(() => {
                    row.remove();
                    const container = document.getElementById('permission_list_body');
                    const rows = container.querySelectorAll('[id^="perm_card_"]');
                    if (rows.length === 0) {
                         const emptyState = document.getElementById('empty_state_row');
                         if (emptyState) emptyState.style.display = 'block';
                         lucide.createIcons();
                    }
                }, 200);
            }
        }
    </script>
</body>
</html>
