<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" class="h-full">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('page-title', 'Dashboard') | {{ config('app.name', 'Laravel') }}</title>

    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

    <!-- Scripts & Styles -->
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            darkMode: 'class',
            theme: {
                extend: {
                    colors: {
                        primary: {
                            50: '#f0f9ff',
                            100: '#e0f2fe',
                            200: '#bae6fd',
                            300: '#7dd3fc',
                            400: '#38bdf8',
                            500: '#0ea5e9',
                            600: '#0284c7',
                            700: '#0369a1',
                            800: '#075985',
                            900: '#0c4a6e',
                        },
                        danger: {
                            50: '#fef2f2',
                            500: '#ef4444',
                            600: '#dc2626',
                        },
                        success: {
                            50: '#f0fdf4',
                            500: '#22c55e',
                            600: '#16a34a',
                        },
                        warning: {
                            50: '#fffbeb',
                            500: '#f59e0b',
                            600: '#d97706',
                        },
                    },
                    fontFamily: {
                        sans: ['Inter', 'sans-serif'],
                    },
                }
            }
        };

        // Theme management logic
        if (localStorage.getItem('theme') === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
            document.documentElement.classList.add('dark');
        } else {
            document.documentElement.classList.remove('dark');
        }
    </script>

    <style>
        [x-cloak] { display: none !important; }
        body { font-family: 'Inter', sans-serif; }
        .fi-sidebar { width: 16rem; }
        .fi-main { margin-left: 16rem; }
        @media (max-width: 1024px) {
            .fi-main { margin-left: 0; }
        }
    </style>
</head>
<body class="h-full bg-gray-50 text-gray-950 antialiased dark:bg-gray-950 dark:text-white">
    @php
        $adminUser = auth()->user();
        $adminInitials = $adminUser
            ? collect(explode(' ', trim($adminUser->name)))->map(fn ($word) => strtoupper(substr($word, 0, 1)))->take(2)->implode('')
            : 'AD';
    @endphp
    <div class="flex h-full min-h-screen">
        <!-- Sidebar -->
        <aside id="sidebar" class="fixed inset-y-0 left-0 z-50 flex w-64 flex-col border-r border-gray-200 bg-white transition duration-300 dark:border-white/10 dark:bg-gray-900 lg:static lg:translate-x-0 -translate-x-full">
            <div class="flex h-16 items-center px-6 border-b border-gray-100 dark:border-white/5">
                <a href="{{ route('admin.dashboard') }}" class="flex items-center gap-2 font-bold text-xl tracking-tight">
                    <span class="text-primary-600">Admin</span>
                    <span>Console</span>
                </a>
            </div>

            <nav class="flex-1 overflow-y-auto p-4 space-y-1">
                <a href="{{ route('admin.dashboard') }}" class="group flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium transition {{ request()->routeIs('admin.dashboard') ? 'bg-primary-50 text-primary-600 dark:bg-primary-500/10 dark:text-primary-400' : 'text-gray-700 hover:bg-gray-50 dark:text-gray-300 dark:hover:bg-white/5' }}">
                    <i data-lucide="layout-dashboard" class="h-5 w-5"></i>
                    <span>Dashboard</span>
                </a>

                <div class="pt-4 pb-1">
                    <p class="px-3 text-[10px] font-bold uppercase tracking-wider text-gray-400 dark:text-gray-500">System</p>
                </div>

                <a href="{{ route('admin.users.index') }}" class="group flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium transition {{ request()->routeIs('admin.users.*') ? 'bg-primary-50 text-primary-600 dark:bg-primary-500/10 dark:text-primary-400' : 'text-gray-700 hover:bg-gray-50 dark:text-gray-300 dark:hover:bg-white/5' }}">
                    <i data-lucide="users" class="h-5 w-5"></i>
                    <span>Manage Users</span>
                </a>

                <a href="#" class="group flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 dark:text-gray-300 dark:hover:bg-white/5">
                    <i data-lucide="settings" class="h-5 w-5"></i>
                    <span>Settings</span>
                </a>
            </nav>

        </aside>

        <!-- Main Wrapper -->
        <div class="flex flex-1 flex-col overflow-hidden">
            <!-- Header -->
            <header class="h-16 flex shrink-0 items-center justify-between border-b border-gray-200 bg-white px-4 dark:border-white/10 dark:bg-gray-900 md:px-8">
                <div class="flex items-center gap-4">
                    <button onclick="document.getElementById('sidebar').classList.toggle('-translate-x-full')" class="p-2 text-gray-500 hover:bg-gray-50 dark:hover:bg-white/5 rounded-lg lg:hidden">
                        <i data-lucide="menu" class="h-5 w-5"></i>
                    </button>
                    <h2 class="text-sm font-semibold text-gray-700 dark:text-gray-200">@yield('page-title', 'Dashboard')</h2>
                </div>

                <div class="flex items-center gap-2">
                    <!-- Theme Switcher -->
                    <div class="flex items-center bg-gray-100 dark:bg-white/5 rounded-lg p-1">
                        <button onclick="setTheme('light')" id="btn-theme-light" class="p-1.5 rounded-md transition-all hover:text-primary-600 dark:text-gray-400">
                            <i data-lucide="sun" class="h-4 w-4"></i>
                        </button>
                        <button onclick="setTheme('dark')" id="btn-theme-dark" class="p-1.5 rounded-md transition-all hover:text-primary-600 dark:text-gray-400">
                            <i data-lucide="moon" class="h-4 w-4"></i>
                        </button>
                        <button onclick="setTheme('system')" id="btn-theme-system" class="p-1.5 rounded-md transition-all hover:text-primary-600 dark:text-gray-400">
                            <i data-lucide="monitor" class="h-4 w-4"></i>
                        </button>
                    </div>

                    <div class="h-6 w-px bg-gray-200 dark:bg-white/10 mx-2 uppercase"></div>

                    <details class="relative">
                        <summary class="list-none cursor-pointer select-none">
                            <div class="flex items-center gap-2 rounded-lg border border-gray-200 px-2 py-1.5 hover:bg-gray-50 dark:border-white/10 dark:hover:bg-white/5">
                                <div class="h-8 w-8 rounded-full bg-primary-600 flex items-center justify-center font-bold text-white text-xs">{{ $adminInitials }}</div>
                                <div class="hidden sm:block text-left">
                                    <p class="text-xs font-semibold text-gray-700 dark:text-gray-200">{{ $adminUser?->name ?? 'Administrator' }}</p>
                                    <p class="text-[10px] text-gray-500 dark:text-gray-400">Admin</p>
                                </div>
                                <i data-lucide="chevron-down" class="h-4 w-4 text-gray-500 dark:text-gray-400"></i>
                            </div>
                        </summary>

                        <div class="absolute right-0 mt-2 w-64 overflow-hidden rounded-xl border border-gray-200 bg-white shadow-lg dark:border-white/10 dark:bg-gray-900 z-50">
                            <div class="px-4 py-3 border-b border-gray-100 dark:border-white/10">
                                <p class="text-sm font-semibold text-gray-800 dark:text-white">{{ $adminUser?->name ?? 'Administrator' }}</p>
                                <p class="text-xs text-gray-500 dark:text-gray-400">{{ $adminUser?->email ?? 'admin@savemart.lk' }}</p>
                            </div>
                            <form action="{{ route('logout') }}" method="POST" class="p-2">
                                @csrf
                                <button type="submit" class="w-full rounded-lg px-3 py-2 text-left text-sm font-medium text-danger-600 hover:bg-danger-50 dark:hover:bg-danger-500/10">
                                    <span class="inline-flex items-center gap-2">
                                        <i data-lucide="log-out" class="h-4 w-4"></i>
                                        Logout
                                    </span>
                                </button>
                            </form>
                        </div>
                    </details>
                </div>
            </header>

            <!-- Main Content -->
            <main class="flex-1 overflow-y-auto bg-gray-50 p-4 dark:bg-gray-950 md:p-8">
                <div class="mx-auto max-w-7xl">
                    @yield('content')
                </div>
            </main>
        </div>
    </div>

    <!-- Scripts -->
    <script src="https://unpkg.com/lucide@latest"></script>
    <script>
        lucide.createIcons();

        function setTheme(theme) {
            if (theme === 'system') {
                localStorage.removeItem('theme');
            } else {
                localStorage.setItem('theme', theme);
            }
            applyTheme();
        }

        function applyTheme() {
            const theme = localStorage.getItem('theme');
            const isDark = theme === 'dark' || (!theme && window.matchMedia('(prefers-color-scheme: dark)').matches);

            document.documentElement.classList.toggle('dark', isDark);

            // Highlight active button
            document.getElementById('btn-theme-light').classList.toggle('bg-white', theme === 'light');
            document.getElementById('btn-theme-light').classList.toggle('shadow-sm', theme === 'light');
            document.getElementById('btn-theme-dark').classList.toggle('bg-gray-800', theme === 'dark');
            document.getElementById('btn-theme-system').classList.toggle('bg-white', !theme);
            document.getElementById('btn-theme-system').classList.toggle('dark:bg-white/10', !theme);
            document.getElementById('btn-theme-system').classList.toggle('shadow-sm', !theme);
        }

        applyTheme();

        // Listen for system theme changes
        window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
            if (!localStorage.getItem('theme')) applyTheme();
        });
    </script>
    @stack('scripts')
</body>
</html>
