<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Safi Hub | B2B Engine</title>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    <!-- Alpine.js for UI interactivity -->
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body class="bg-gray-900 text-gray-100 antialiased" x-data="{ sidebarOpen: true }">

    <div class="flex h-screen overflow-hidden">
        
        <!-- SIDEBAR -->
        <aside :class="sidebarOpen ? 'w-64' : 'w-20'" class="transition-all duration-300 bg-gray-950 border-r border-gray-800 flex flex-col relative">
            <!-- Logo Area -->
            <div class="h-16 flex items-center justify-center border-b border-gray-800">
                <span x-show="sidebarOpen" class="text-xl font-black text-transparent bg-clip-text bg-gradient-to-r from-green-400 to-green-600 tracking-wider">
                    SAFI HUB
                </span>
                <span x-show="!sidebarOpen" class="text-xl font-black text-green-500">SH</span>
            </div>

            <!-- Navigation Links -->
            <nav class="flex-1 px-4 py-6 space-y-2 overflow-y-auto">
                <a href="/dashboard" class="flex items-center gap-3 px-3 py-2 bg-green-500/10 text-green-400 rounded-lg border border-green-500/20">
                    <svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"></path></svg>
                    <span x-show="sidebarOpen" class="font-medium text-sm">Dashboard</span>
                </a>

                <!-- Company Settings -->
                  <a href="{{ route('company.settings') }}" class="flex items-center gap-3 px-3 py-2 text-gray-400 hover:bg-gray-800/50 hover:text-gray-200 rounded-lg transition-colors">
                    <svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"></path></svg>
                    <span x-show="sidebarOpen" class="font-medium text-sm">Organization</span>
                 </a>
                 
                 <!-- Users -->
                 <a href="/users" class="flex items-center gap-3 px-3 py-2 text-gray-400 hover:bg-gray-800/50 hover:text-gray-200 rounded-lg transition-colors">
                    <svg class="w-5 h-5 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                    <span x-show="sidebarOpen" class="font-medium text-sm">Users</span>
                </a>
                
                <!-- Roles & Permissions -->
                <a href="/roles" class="flex items-center gap-3 px-3 py-2 text-gray-400 hover:bg-gray-800/50 hover:text-gray-200 rounded-lg transition-colors">
                    <svg class="w-5 h-5 shrink-0" 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-show="sidebarOpen" class="font-medium text-sm">Roles & Access</span>
                </a>
            </nav>
        </aside>

        <!-- MAIN CONTENT AREA -->
        <div class="flex-1 flex flex-col overflow-hidden relative">
            
            <!-- TOP HEADER -->
            <header class="h-16 border-b border-gray-800 bg-gray-900/80 backdrop-blur flex items-center justify-between px-6 z-10">
                <!-- Sidebar Toggle Button -->
                <button @click="sidebarOpen = !sidebarOpen" class="text-gray-400 hover:text-green-400 transition">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h7"></path></svg>
                </button>

                <!-- NEW: Alpine.js Profile Dropdown -->
                <div x-data="{ profileMenuOpen: false }" class="relative">
                    
                    <!-- Trigger -->
                    <button @click="profileMenuOpen = !profileMenuOpen" class="flex items-center gap-3 focus:outline-none group">
                        <div class="hidden md:block text-right">
                            <div class="text-sm font-bold text-gray-200 group-hover:text-white transition-colors">{{ Auth::user()->name }}</div>
                            <div class="text-xs text-green-500">{{ Auth::user()->role->name }} &bull; {{ Auth::user()->company->name }}</div>
                        </div>
                        
                        <!-- Avatar -->
                        <div class="w-10 h-10 rounded-full bg-gray-800 border-2 border-gray-700 group-hover:border-green-500 flex items-center justify-center overflow-hidden transition-colors shadow-lg">
                            @if(Auth::user()->avatar)
                                <img src="{{ asset('storage/'.Auth::user()->avatar) }}" class="w-full h-full object-cover">
                            @else
                                <span class="text-green-500 font-bold text-sm">{{ substr(Auth::user()->name, 0, 1) }}</span>
                            @endif
                        </div>
                        
                        <!-- Caret -->
                        <svg class="w-4 h-4 text-gray-500 group-hover:text-gray-300 transition-transform duration-200" 
                             :class="{'rotate-180': profileMenuOpen}" 
                             fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
                        </svg>
                    </button>

                    <!-- Dropdown Menu -->
                    <div x-show="profileMenuOpen" 
                         @click.outside="profileMenuOpen = false"
                         x-transition:enter="transition ease-out duration-200"
                         x-transition:enter-start="opacity-0 scale-95 translate-y-2"
                         x-transition:enter-end="opacity-100 scale-100 translate-y-0"
                         x-transition:leave="transition ease-in duration-150"
                         x-transition:leave-start="opacity-100 scale-100 translate-y-0"
                         x-transition:leave-end="opacity-0 scale-95 translate-y-2"
                         class="absolute right-0 mt-3 w-64 bg-gray-900 border border-gray-700 rounded-xl shadow-2xl z-50 overflow-hidden"
                         style="display: none;">
                        
                        <div class="p-4 border-b border-gray-800 bg-gray-950/50">
                            <p class="text-sm text-white font-bold truncate">{{ Auth::user()->name }}</p>
                            <p class="text-xs text-gray-400 truncate mt-0.5">{{ Auth::user()->email }}</p>
                        </div>

                        <div class="p-2">
                            <a href="{{ route('profile.edit') }}" class="flex items-center gap-3 px-3 py-2 text-sm text-gray-300 hover:text-white hover:bg-gray-800 rounded-lg transition-colors group">
                                <svg class="w-4 h-4 text-gray-500 group-hover:text-green-400 transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                                Profile Settings
                            </a>
                        </div>

                        <div class="p-2 border-t border-gray-800">
                            <form method="POST" action="{{ route('logout') }}">
                                @csrf
                                <button type="submit" class="w-full flex items-center gap-3 px-3 py-2 text-sm text-red-400 hover:text-red-300 hover:bg-red-500/10 rounded-lg transition-colors">
                                    <svg class="w-4 h-4 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"></path></svg>
                                    Secure Logout
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
                <!-- END Dropdown -->
                
            </header>

            <!-- PAGE CONTENT INJECTION -->
            <main class="flex-1 overflow-y-auto p-6 md:p-10">
                @yield('content')
            </main>

        </div>
    </div>
</body>
</html>