<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{ $title ?? 'Admin Panel' }}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <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=Manrope:wght@300;400;500;600;700&family=Sora:wght@500;600;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    @vite(['resources/css/app.css', 'resources/js/admin.js'])
</head>
@php
    $user = auth()->user();
    $themePreference = $user?->theme_preference;
    if (! in_array($themePreference, ['light', 'dark', 'auto'], true)) {
        if ($user?->dark_mode_enabled) {
            $themePreference = 'dark';
        } else {
            $themePreference = (string) setting('admin_theme_default', 'light');
        }
    }
    if (! in_array($themePreference, ['light', 'dark', 'auto'], true)) {
        $themePreference = 'light';
    }
@endphp
<body class="theme-admin theme-mode-{{ $themePreference }} {{ $themePreference === 'dark' ? 'dark' : '' }}" data-theme-preference="{{ $themePreference }}" data-theme-endpoint="{{ route('admin.preferences.theme') }}">
    <div class="min-h-screen lg:flex">
        <aside id="adminSidebar" class="fixed inset-y-0 left-0 z-30 w-72 -translate-x-full overflow-y-auto bg-[var(--admin-sidebar)] p-6 text-[var(--admin-sidebar-text)] transition duration-300 lg:relative lg:translate-x-0">
            @include('admin.partials.sidebar')
        </aside>

        <div class="flex min-h-screen flex-1 flex-col lg:pl-72">
            @include('admin.partials.topbar', [
                'heading' => $heading ?? 'Dashboard',
                'subheading' => $subheading ?? null,
                'quickActions' => $quickActions ?? [],
            ])

            <main class="flex-1 p-6">
                @if(session('status'))
                    <div class="mb-4 rounded-2xl border border-emerald-200 bg-emerald-50 p-4 text-emerald-800">
                        {{ session('status') }}
                    </div>
                @endif
                @if($errors->any())
                    <div class="mb-4 rounded-2xl border border-rose-200 bg-rose-50 p-4 text-rose-800">
                        <ul class="list-disc pl-5 text-sm">
                            @foreach($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif
                @yield('content')
                @include('partials.made-by-footer')
            </main>
        </div>
    </div>

    @if(!empty($quickActions ?? []))
        <div class="fixed bottom-6 right-6 z-40 lg:hidden">
            <button type="button" data-quick-toggle class="flex h-12 w-12 items-center justify-center rounded-full bg-emerald-600 text-white shadow-lg">+</button>
            <div data-quick-menu class="mt-3 hidden flex-col gap-2 rounded-2xl border border-[var(--admin-border)] bg-[var(--admin-surface)] p-3 shadow-lg">
                @foreach($quickActions as $action)
                    <a href="{{ $action['href'] }}" class="rounded-lg px-3 py-2 text-sm text-[var(--admin-text)] hover:bg-[var(--admin-bg)]">{{ $action['label'] }}</a>
                @endforeach
            </div>
        </div>
    @endif

    @include('admin.partials.toast')
    @include('admin.partials.modal')
</body>
</html>
