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

    <title>{{ $title ?? 'Booklio Admin' }}</title>

    <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;800&display=swap" rel="stylesheet">
    @if (file_exists(public_path('build/manifest.json')))
        @vite(['resources/css/app.css'])
    @else
        <script src="https://cdn.tailwindcss.com"></script>
        <script>
            tailwind.config = {
                theme: {
                    extend: {
                        fontFamily: {
                            sans: ['Inter', 'ui-sans-serif', 'system-ui', 'sans-serif'],
                        },
                        colors: {
                            booklio: {
                                primary: '#2563EB',
                                secondary: '#0EA5E9',
                                success: '#10B981',
                                warning: '#F59E0B',
                                danger: '#EF4444',
                                dark: '#111827',
                                muted: '#6B7280',
                                background: '#F8FAFC',
                                border: '#E5E7EB',
                            }
                        },
                        boxShadow: {
                            card: '0 8px 24px rgba(15, 23, 42, 0.06)',
                            soft: '0 1px 2px rgba(15, 23, 42, 0.06)',
                            modal: '0 24px 64px rgba(15, 23, 42, 0.18)',
                        }
                    }
                }
            }
        </script>
    @endif
    <style>
        [x-cloak] { display: none !important; }

        .booklio-scrollbar { scrollbar-width: thin; scrollbar-color: #CBD5E1 transparent; }
        .booklio-scrollbar::-webkit-scrollbar { height: 8px; width: 8px; }
        .booklio-scrollbar::-webkit-scrollbar-thumb { background: #CBD5E1; border-radius: 999px; }

        html[dir="rtl"] body { text-align: right; }
        html[dir="rtl"] .text-left { text-align: right !important; }

        html[dir="rtl"] [data-admin-sidebar] {
            left: auto !important;
            right: 0 !important;
            border-right-width: 0;
            border-left: 1px solid #E5E7EB;
        }

        html[dir="rtl"] [data-admin-content] {
            padding-left: 0 !important;
            padding-right: 280px !important;
        }

        html[dir="rtl"] [data-mobile-sidebar] aside {
            margin-left: 0;
            margin-right: auto;
        }

        html[dir="rtl"] [data-booklio-dropdown-menu][data-align="end"],
        html[dir="rtl"] [data-booklio-search-panel] {
            left: 0;
            right: auto;
            text-align: right;
        }

        html[dir="rtl"] [data-booklio-dropdown-menu][data-align="start"] {
            left: auto;
            right: 0;
            text-align: right;
        }

        html[dir="rtl"] [data-booklio-search-root] [data-search-icon] {
            left: 1rem;
            right: auto;
        }

        html[dir="rtl"] [data-booklio-search-root] input {
            padding-left: 3rem;
            padding-right: 1rem;
        }

        @media (max-width: 1023px) {
            html[dir="rtl"] [data-admin-content] {
                padding-right: 0 !important;
            }
        }

        @media (max-width: 640px) {
            header h1 {
                white-space: normal !important;
                overflow: visible !important;
                text-overflow: clip !important;
            }

            .booklio-dropdown-panel {
                position: fixed !important;
                top: 5.75rem !important;
                left: 1rem !important;
                right: 1rem !important;
                width: auto !important;
                max-width: none !important;
                max-height: calc(100vh - 7rem);
                overflow-y: auto;
            }
        }

        main, main * { min-width: 0; }
        main table { width: 100%; table-layout: auto; }
        main thead th {
            vertical-align: top;
            white-space: nowrap;
            overflow-wrap: normal;
            word-break: normal;
        }
        main tbody td {
            vertical-align: top;
            overflow-wrap: normal;
            word-break: normal;
        }
        main a, main button, main input, main select, main textarea { max-width: 100%; }
        main p, main h1, main h2, main h3, main span { overflow-wrap: anywhere; }
        .booklio-filter-grid { display: flex !important; flex-wrap: wrap; align-items: center; gap: .75rem; }
        .booklio-filter-grid > * { flex: 1 1 170px; min-width: min(100%, 170px); }
        .booklio-filter-grid > .relative,
        .booklio-filter-grid > :first-child { flex: 2 1 260px; min-width: min(100%, 240px); }
        .booklio-filter-grid > button,
        .booklio-filter-grid > a { flex: 0 0 auto; min-width: 120px; }
        @media (max-width: 640px) {
            .booklio-filter-grid > * { flex-basis: 100%; min-width: 100%; }
            .booklio-filter-grid > button,
            .booklio-filter-grid > a { width: 100%; }
        }
    </style>
</head>
<body class="min-h-screen overflow-x-hidden bg-booklio-background font-sans text-booklio-dark antialiased">
    <div class="flex min-h-screen">
        <x-admin.sidebar :active="$active ?? 'dashboard'" />

        <div data-admin-content class="min-w-0 flex-1 overflow-x-hidden lg:pl-[280px]">
            <x-admin.header :title="$pageTitle ?? 'Dashboard'" />

            <main class="px-4 py-5 sm:px-6 lg:px-8 lg:py-6">
                {{ $slot ?? '' }}
                @yield('content')
            </main>
        </div>
    </div>

    @stack('modals')
    @php
        $booklioTranslationPayload = $booklioTranslationPayload ?? [
            'defaultLocale' => 'en',
            'currentLocale' => app()->getLocale(),
            'direction' => $layoutDirection ?? 'ltr',
            'languages' => [],
        ];
    @endphp
    <script>
        // Backend integration: hydrate this object from Settings > Language & Localization later.
        window.BooklioTranslations = @json($booklioTranslationPayload);
    </script>
    <script src="{{ asset('js/booklio-dropdowns.js') }}?v={{ filemtime(public_path('js/booklio-dropdowns.js')) }}"></script>
    <script src="{{ asset('js/booklio-ui.js') }}?v={{ filemtime(public_path('js/booklio-ui.js')) }}"></script>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const drawer = document.querySelector('[data-mobile-sidebar]');
            const openButton = document.querySelector('[data-open-sidebar]');
            const closeButtons = document.querySelectorAll('[data-close-sidebar]');

            const openDrawer = () => drawer?.classList.remove('hidden');
            const closeDrawer = () => drawer?.classList.add('hidden');

            openButton?.addEventListener('click', openDrawer);
            closeButtons.forEach((button) => button.addEventListener('click', closeDrawer));

            document.querySelectorAll('[data-modal-open]').forEach((button) => {
                button.addEventListener('click', () => {
                    const modal = document.querySelector(button.dataset.modalOpen);
                    modal?.classList.remove('hidden');
                    modal?.classList.add('flex');
                });
            });

            document.querySelectorAll('[data-modal-close]').forEach((button) => {
                button.addEventListener('click', () => {
                    const modal = button.closest('[data-modal]');
                    modal?.classList.add('hidden');
                    modal?.classList.remove('flex');
                });
            });
        });
    </script>
</body>
</html>
