<!DOCTYPE html>
<html lang="en" x-data="adminApp()" x-init="$watch('sidebarOpen', value => localStorage.setItem('sidebarOpen', value))" :class="darkMode ? 'dark' : ''" class="h-dvh overflow-hidden">
<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('title', 'Dashboard') — Quickro POS Admin</title>
    <script>
        (() => {
            const stored = localStorage.getItem('adminDark');
            const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
            const enabled = stored === null ? prefersDark : stored === 'true';
            document.documentElement.classList.toggle('dark', enabled);
            document.documentElement.style.colorScheme = enabled ? 'dark' : 'light';
        })();
    </script>
    <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@300;400;500;600;700;800&display=swap" rel="stylesheet">
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    <style>
        [x-cloak] { display: none !important; }
        html, body { height: 100dvh; margin: 0; padding: 0; overflow: hidden; }
        body { font-family: 'Inter', sans-serif; background: inherit; }
    </style>
</head>
<body class="h-full bg-slate-100 text-gray-900 antialiased dark:bg-slate-950 dark:text-gray-100" x-cloak>

<!-- Toast Container -->
<div x-show="toasts.length > 0" class="fixed top-4 right-4 z-50 space-y-2" aria-live="polite">
    <template x-for="toast in toasts" :key="toast.id">
        <div x-show="toast.visible"
             x-transition:enter="transition ease-out duration-300"
             x-transition:enter-start="opacity-0 translate-x-10"
             x-transition:enter-end="opacity-100 translate-x-0"
             x-transition:leave="transition ease-in duration-200"
             x-transition:leave-start="opacity-100"
             x-transition:leave-end="opacity-0"
             :class="toast.type === 'success' ? 'bg-emerald-600' : toast.type === 'error' ? 'bg-red-600' : 'bg-blue-500'"
             class="flex min-w-70 max-w-sm items-center gap-3 rounded-xl px-4 py-3 text-white shadow-xl">
            <span x-show="toast.type === 'success'">✓</span>
            <span x-show="toast.type === 'error'">✗</span>
            <span x-show="toast.type === 'info'">ℹ</span>
            <p class="flex-1 text-sm font-medium" x-text="toast.message"></p>
            <button @click="removeToast(toast.id)" class="text-white/70 hover:text-white">✕</button>
        </div>
    </template>
</div>

<div class="fixed inset-0 flex">
    <!-- Sidebar -->
        <aside :class="sidebarOpen ? 'w-64' : 'w-16'"
               class="relative z-30 flex h-full shrink-0 flex-col overflow-hidden bg-slate-950 text-white transition-all duration-200">

        <!-- Logo -->
        <div class="flex items-center h-16 px-4 border-b border-white/10">
            <div class="flex items-center gap-3 min-w-0">
                <div class="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg brand-gradient shadow-lg">
                    <svg class="w-5 h-5 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                    </svg>
                </div>
                <span x-show="sidebarOpen" class="font-bold text-lg text-white truncate">Quickro POS</span>
            </div>
            <button @click="sidebarOpen = !sidebarOpen"
                    class="ml-auto shrink-0 rounded-lg p-1.5 text-white/60 hover:bg-white/10 hover:text-white">
                <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"/>
                </svg>
            </button>
        </div>

        <!-- Navigation -->
        <nav class="flex-1 min-h-0 py-4 space-y-0.5 px-2 overflow-y-auto">

            @php
                $navItems = [
                    ['route' => 'admin.dashboard', 'label' => 'Dashboard', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7h7v7H3V7zm11 0h7v4h-7V7zm0 7h7v3h-7v-3zM3 17h7v3H3v-3z"/>'],
                    ['route' => 'admin.users.index', 'label' => 'Users', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a4 4 0 00-5-3.87M9 20H4v-2a4 4 0 015-3.87m6-4a4 4 0 11-8 0 4 4 0 018 0zm6 0a3 3 0 11-6 0 3 3 0 016 0z"/>'],
                    ['route' => 'admin.subscriptions.index', 'label' => 'Subscriptions', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V7a3 3 0 00-3-3H6a3 3 0 00-3 3v9a3 3 0 003 3z"/>'],
                    ['route' => 'admin.payments.index', 'label' => 'Payments', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-10c1.11 0 2.08.402 2.599 1M12 8V7m0 1v10m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>'],
                    ['route' => 'admin.addons.index', 'label' => 'Add-ons', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6V4m0 2a2 2 0 100 4m0-4a2 2 0 110 4m-6 8a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4m6 6v10m6-2a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4"/>'],
                    ['route' => 'admin.webhooks.index', 'label' => 'Webhooks', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M9 19l3 3m0 0l3-3m-3 3V10"/>'],
                    ['route' => 'admin.api-keys.index', 'label' => 'API Keys', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 114 0 2 2 0 01-4 0zm-8 8l3-3 2 2 6-6 2 2-8 8-5-5z"/>'],
                    ['route' => 'admin.vouchers.index', 'label' => 'Vouchers', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5l6 6v5M7 3a4 4 0 00-4 4v10a2 2 0 002 2h10a2 2 0 002-2V9M7 3l10 6"/>'],
                    ['route' => 'admin.banners.index', 'label' => 'Banners', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>'],
                    ['route' => 'admin.devices.index', 'label' => 'Devices', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 18h.01M7 21h10a2 2 0 002-2V5a2 2 0 00-2-2H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>'],
                    ['route' => 'admin.sync-logs.index', 'label' => 'Sync Logs', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>'],
                    ['route' => 'admin.backups.index', 'label' => 'Backups', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"/>'],
                    ['route' => 'admin.storage.index', 'label' => 'Storage', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582 4 8 4"/>'],
                    ['route' => 'admin.website.index', 'label' => 'Website CMS', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9"/>'],
                    ['route' => 'admin.newsletter.index', 'label' => 'Newsletter', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>'],
                    ['route' => 'admin.notifications.index', 'label' => 'Notifications', 'icon' => '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/>'],
                    ['route' => 'admin.settings.index', 'label' => 'Settings', 'icon' => '<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 stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>'],
                ];

            @endphp

            @foreach($navItems as $item)
                <a href="{{ route($item['route']) }}"
                   class="{{ request()->routeIs(str_replace('.index','',$item['route']).'*') ? 'nav-item-active' : 'nav-item' }} flex items-center gap-3 px-3 py-2.5 rounded-lg transition-all duration-150 whitespace-nowrap">
                    <svg class="h-5 w-5 shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        {!! $item['icon'] !!}
                    </svg>
                    <span x-show="sidebarOpen" class="text-sm font-medium">{{ $item['label'] }}</span>
                </a>
            @endforeach
        </nav>

        <!-- User info at bottom -->
        <div class="border-t border-white/10 p-3">
            <div class="flex items-center gap-3">
                <div class="flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-brand-600 text-sm font-bold text-white">
                    {{ strtoupper(substr(auth()->user()->name, 0, 1)) }}
                </div>
                <div x-show="sidebarOpen" class="min-w-0">
                    <p class="text-sm font-medium text-white truncate">{{ auth()->user()->name }}</p>
                    <p class="text-xs text-white/50 truncate">{{ auth()->user()->email }}</p>
                </div>
                <form x-show="sidebarOpen" method="POST" action="{{ route('admin.logout') }}" class="ml-auto">
                    @csrf
                    <button type="submit" title="Logout"
                            class="p-1.5 rounded-lg text-white/50 hover:text-white hover:bg-white/10">
                        <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <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"/>
                        </svg>
                    </button>
                </form>
            </div>
        </div>
    </aside>

    <!-- Main Content -->
    <div class="flex-1 min-h-0 flex flex-col min-w-0 overflow-hidden">

        <!-- Top Bar -->
        <header class="flex h-16 shrink-0 items-center gap-4 border-b border-slate-200 bg-white/95 px-6 backdrop-blur dark:border-slate-800 dark:bg-slate-900/95">
            <!-- Mobile menu toggle -->
            <button @click="sidebarOpen = !sidebarOpen" class="rounded-lg p-2 text-gray-500 hover:bg-gray-100 dark:hover:bg-slate-800 lg:hidden">
                <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"/>
                </svg>
            </button>

            <h1 class="text-lg font-semibold text-gray-900 dark:text-white">@yield('page-title', 'Dashboard')</h1>

            <div class="ml-auto flex items-center gap-3">
                <!-- Dark mode toggle -->
                <button @click="darkMode = !darkMode; localStorage.setItem('adminDark', darkMode)"
                        class="rounded-lg p-2 text-gray-500 hover:bg-gray-100 dark:text-gray-400 dark:hover:bg-slate-800">
                    <svg x-cloak x-show="!darkMode" class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"/>
                    </svg>
                    <svg x-cloak x-show="darkMode" class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"/>
                    </svg>
                </button>

                <span class="text-sm text-gray-500 dark:text-gray-400">{{ now()->format('D, d M Y') }}</span>
            </div>
        </header>

        <!-- License Warning -->
        @php $licenseVerified = filter_var(env('APP_LICENSE_VERIFIED', 'false'), FILTER_VALIDATE_BOOLEAN); @endphp
        @if(!$licenseVerified)
            <div class="mx-6 mt-4 flex items-center gap-3 rounded-xl border border-red-200 bg-red-50 px-4 py-3 dark:border-red-800 dark:bg-red-900/20">
                <svg class="h-5 w-5 shrink-0 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/>
                </svg>
                <p class="flex-1 text-sm font-medium text-red-800 dark:text-red-200">
                    License not activated. This installation is not licensed.
                    <a href="{{ route('admin.settings.index', ['tab' => 'license']) }}" class="ml-2 underline hover:no-underline">Activate License →</a>
                </p>
            </div>
        @endif

        <!-- Flash Messages -->
        @if(session('success'))
            <div x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 4000)"
                 class="mx-6 mt-4 flex items-center gap-2 bg-emerald-50 dark:bg-emerald-900/20 border border-emerald-200 dark:border-emerald-800 rounded-xl px-4 py-3">
                <svg class="w-5 h-5 text-emerald-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                </svg>
                <p class="text-sm text-emerald-800 dark:text-emerald-200">{{ session('success') }}</p>
            </div>
        @endif
        @if($errors->any())
            <div class="mx-6 mt-4 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-xl px-4 py-3">
                @foreach($errors->all() as $error)
                    <p class="text-sm text-red-800 dark:text-red-200">{{ $error }}</p>
                @endforeach
            </div>
        @endif

        <!-- Page Content -->
        <main class="flex-1 min-h-0 overflow-y-auto p-6">
            @yield('content')
        </main>
    </div>
</div>

<script>
    function adminApp() {
        return {
            darkMode: localStorage.getItem('adminDark') === 'true',
            sidebarOpen: localStorage.getItem('sidebarOpen') !== 'false',
            toasts: [],
            addToast(message, type = 'success') {
                const id = Date.now();
                this.toasts.push({ id, message, type, visible: true });
                setTimeout(() => this.removeToast(id), 4000);
            },
            removeToast(id) {
                const toast = this.toasts.find(t => t.id === id);
                if (toast) { toast.visible = false; setTimeout(() => { this.toasts = this.toasts.filter(t => t.id !== id); }, 300); }
            },
        };
    }
</script>
@stack('scripts')<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</body>
</html>
