<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" class="dark">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">

        <title>{{ config('app.name', 'Aquila-System') }}</title>

        <script>
            if (localStorage.getItem('theme') === 'light') {
                document.documentElement.classList.remove('dark');
            } else {
                document.documentElement.classList.add('dark');
            }
        </script>

        <link rel="preconnect" href="https://fonts.bunny.net">
        <link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" />
        <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">

        @livewireStyles
        @vite(['resources/css/app.css', 'resources/js/app.js'])
    </head>
    <body class="font-sans antialiased" x-data="{ ...portalTheme(), sidebarOpen: false }" x-init="init()" x-on:keydown.escape.window="sidebarOpen = false">
        <div class="flex min-h-screen bg-slate-100 dark:bg-slate-950">
            <aside class="fixed inset-y-0 left-0 z-40 hidden w-72 border-r border-slate-200 bg-white/95 px-4 py-5 shadow-soft backdrop-blur lg:flex lg:flex-col dark:border-slate-800 dark:bg-slate-950/95">
                <a href="{{ route('dashboard') }}" class="block shrink-0 px-2">
                    <x-brand-logo class="h-20 w-full" imageClass="h-full w-full object-contain" />
                    <span class="mt-3 block text-center text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">Portal Corporativo</span>
                </a>

                <nav class="sidebar-scroll mt-8 min-h-0 flex-1 overflow-y-auto pr-2">
                    <x-system-sidebar-nav />
                </nav>

                <div class="mt-5 shrink-0 rounded-lg border border-slate-200 bg-slate-50 p-3 dark:border-slate-800 dark:bg-slate-900">
                    <div class="flex items-center gap-3">
                        <x-user-avatar :user="auth()->user()" size="md" />
                        <div class="min-w-0">
                            <p class="truncate text-sm font-semibold text-slate-900 dark:text-white">{{ auth()->user()->name ?? '' }}</p>
                            <p class="truncate text-xs text-slate-500 dark:text-slate-400">{{ auth()->user()->departamento->nome ?? 'Sem departamento' }}</p>
                        </div>
                    </div>
                </div>
            </aside>

            <div x-cloak x-show="sidebarOpen" class="fixed inset-0 z-50 lg:hidden" aria-modal="true" role="dialog">
                <div x-show="sidebarOpen" x-transition.opacity class="absolute inset-0 bg-slate-950/70" x-on:click="sidebarOpen = false"></div>

                <aside x-show="sidebarOpen"
                       x-transition:enter="transition duration-200 ease-out"
                       x-transition:enter-start="-translate-x-full"
                       x-transition:enter-end="translate-x-0"
                       x-transition:leave="transition duration-150 ease-in"
                       x-transition:leave-start="translate-x-0"
                       x-transition:leave-end="-translate-x-full"
                       class="relative flex h-full w-[min(88vw,20rem)] flex-col border-r border-slate-200 bg-white px-4 py-5 shadow-2xl dark:border-slate-800 dark:bg-slate-950">
                    <div class="flex shrink-0 items-center justify-between gap-3 px-2">
                        <a href="{{ route('dashboard') }}" class="min-w-0 flex-1" x-on:click="sidebarOpen = false">
                            <x-brand-logo class="h-16 w-full" imageClass="h-full w-full object-contain" />
                            <span class="mt-2 block text-center text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400">Portal Corporativo</span>
                        </a>

                        <button type="button" x-on:click="sidebarOpen = false" class="rounded-lg p-2 text-slate-500 transition hover:bg-slate-100 dark:text-slate-300 dark:hover:bg-slate-900" aria-label="Fechar menu">
                            <x-heroicon name="x-mark" class="h-5 w-5" />
                        </button>
                    </div>

                    <nav class="sidebar-scroll mt-7 min-h-0 flex-1 overflow-y-auto pr-2">
                        <x-system-sidebar-nav mobile />
                    </nav>
                </aside>
            </div>

            <div class="flex min-h-screen w-full flex-col lg:pl-72">
                <header class="sticky top-0 z-30 border-b border-slate-200 bg-white/85 backdrop-blur dark:border-slate-800 dark:bg-slate-950/85">
                    <div class="flex h-16 items-center gap-4 px-4 sm:px-6 lg:px-8">
                        <button type="button" x-on:click="sidebarOpen = true" class="rounded-lg border border-slate-200 p-2 text-slate-600 transition hover:bg-slate-100 lg:hidden dark:border-slate-800 dark:text-slate-300 dark:hover:bg-slate-900" aria-label="Abrir menu">
                            <x-heroicon name="bars-3" class="h-5 w-5" />
                        </button>

                        <a href="{{ route('dashboard') }}" class="lg:hidden">
                            <x-brand-logo class="h-10 w-32 px-2 py-1" imageClass="h-full w-full object-contain" />
                        </a>

                        <div class="hidden flex-1 items-center lg:flex">
                            <label class="relative w-full max-w-xl">
                                <x-heroicon name="magnifying-glass" class="pointer-events-none absolute left-3 top-1/2 h-5 w-5 -translate-y-1/2 text-slate-400" />
                                <input type="search" placeholder="Buscar no portal..." class="form-control pl-10" disabled>
                            </label>
                        </div>

                        <div class="ml-auto flex items-center gap-2">
                            <button type="button" x-on:click="dark = !dark" class="rounded-lg border border-slate-200 p-2 text-slate-600 transition hover:bg-slate-100 dark:border-slate-800 dark:text-slate-300 dark:hover:bg-slate-900" title="Alternar tema" aria-label="Alternar tema">
                                <x-heroicon name="moon" class="h-5 w-5 dark:hidden" />
                                <x-heroicon name="sun" class="hidden h-5 w-5 dark:block" />
                            </button>
                            <button type="button" class="rounded-lg border border-slate-200 p-2 text-slate-600 transition hover:bg-slate-100 dark:border-slate-800 dark:text-slate-300 dark:hover:bg-slate-900" title="Notificações" aria-label="Notificações">
                                <x-heroicon name="bell" class="h-5 w-5" />
                            </button>
                            <form method="POST" action="{{ route('logout') }}">
                                @csrf
                                <button type="submit" class="btn-secondary px-3" aria-label="Sair">
                                    <x-heroicon name="arrow-left-on-rectangle" class="h-5 w-5" />
                                    <span class="hidden sm:inline">Sair</span>
                                </button>
                            </form>
                        </div>
                    </div>
                </header>

                @if (isset($header))
                    <section class="border-b border-slate-200 bg-white dark:border-slate-800 dark:bg-slate-950">
                        <div class="px-4 py-6 sm:px-6 lg:px-8">
                            {{ $header }}
                        </div>
                    </section>
                @endif

                <main class="flex-1 px-4 py-6 sm:px-6 lg:px-8">
                    {{ $slot }}
                </main>
            </div>
        </div>

        <div x-data="{ show: false, message: '' }"
             x-on:notify.window="message = $event.detail.message; show = true; setTimeout(() => show = false, 2800)"
             x-show="show"
             x-transition
             class="fixed bottom-5 right-5 z-50 rounded-lg bg-slate-950 px-4 py-3 text-sm font-semibold text-white shadow-lg dark:bg-white dark:text-slate-950"
             style="display: none;">
            <span x-text="message"></span>
        </div>

        <script src="https://cdn.quilljs.com/1.3.7/quill.min.js"></script>
        @livewireScripts
        @stack('scripts')
    </body>
</html>
