<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" class="h-full scroll-smooth">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ $title ?? 'Dashboard' }} | Aplikasi Percetakan - Smart Profit in a Blink-Ink</title>

    @vite(['resources/css/app.css', 'resources/js/app.js'])

    @stack('styles')

    <script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>

    <script>
        document.addEventListener('alpine:init', () => {
            Alpine.store('theme', {
                theme: 'light',

                init() {
                    const savedTheme = localStorage.getItem('theme');
                    const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
                    this.theme = savedTheme || systemTheme;
                    this.updateTheme();
                },

                toggle() {
                    this.theme = this.theme === 'light' ? 'dark' : 'light';
                    localStorage.setItem('theme', this.theme);
                    this.updateTheme();
                },

                updateTheme() {
                    const html = document.documentElement;
                    const body = document.body;

                    if (this.theme === 'dark') {
                        html.classList.add('dark');
                        if (body) body.classList.add('dark');
                    } else {
                        html.classList.remove('dark');
                        if (body) body.classList.remove('dark');
                    }
                }
            });

            Alpine.store('sidebar', {
                isExpanded: false,
                isMobileOpen: false,
                isHovered: false,
                isDesktopViewport: window.matchMedia('(min-width: 1280px)').matches,
                userId: '{{ auth()->id() }}',

                init() {
                    this.syncViewport();
                    this.applyBodyState();
                },

                syncViewport() {
                    this.isDesktopViewport = window.matchMedia('(min-width: 1280px)').matches;

                    const saved = localStorage.getItem(`sidebar_expanded_${this.userId}`);

                    if (this.isDesktopViewport) {
                        this.isExpanded = saved !== null ? saved === 'true' : true;
                        this.isMobileOpen = false;
                        this.isHovered = false;
                    } else {
                        this.isExpanded = false;
                        this.isHovered = false;
                        this.isMobileOpen = false;
                    }

                    this.applyBodyState();
                },

                applyBodyState() {
                    if (!document.body) return;

                    document.body.classList.toggle(
                        'mobile-sidebar-open',
                        this.isMobileOpen && !this.isDesktopViewport
                    );
                },

                toggleExpanded() {
                    if (!this.isDesktopViewport) return;

                    this.isExpanded = !this.isExpanded;
                    this.isHovered = false;
                    localStorage.setItem(`sidebar_expanded_${this.userId}`, this.isExpanded);
                },

                toggleMobileOpen() {
                    if (this.isDesktopViewport) return;

                    this.isMobileOpen = !this.isMobileOpen;
                    this.applyBodyState();
                },

                setMobileOpen(value) {
                    if (this.isDesktopViewport) {
                        this.isMobileOpen = false;
                    } else {
                        this.isMobileOpen = value;
                    }

                    this.applyBodyState();
                },

                setHovered(value) {
                    if (this.isDesktopViewport && !this.isExpanded) {
                        this.isHovered = value;
                    } else {
                        this.isHovered = false;
                    }
                },

                handleResize() {
                    this.syncViewport();
                }
            });
        });
    </script>

    <script>
        (function () {
            const savedTheme = localStorage.getItem('theme');
            const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
            const theme = savedTheme || systemTheme;

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

    <style>
        [x-cloak] {
            display: none !important;
        }

        :root {
            --page-bg-light:
                radial-gradient(circle at top left, rgba(59, 130, 246, 0.12), transparent 30%),
                radial-gradient(circle at top right, rgba(16, 185, 129, 0.10), transparent 26%),
                radial-gradient(circle at bottom center, rgba(168, 85, 247, 0.08), transparent 28%),
                linear-gradient(180deg, #f8fbff 0%, #eef4ff 55%, #f8fafc 100%);

            --page-bg-dark:
                radial-gradient(circle at top left, rgba(96, 165, 250, 0.16), transparent 28%),
                radial-gradient(circle at top right, rgba(52, 211, 153, 0.12), transparent 24%),
                radial-gradient(circle at bottom center, rgba(129, 140, 248, 0.10), transparent 30%),
                linear-gradient(180deg, #111827 0%, #172033 52%, #1e293b 100%);
        }

        html,
        body {
            min-height: 100%;
        }

        body {
            background: var(--page-bg-light);
            background-attachment: fixed;
        }

        .dark body {
            background: var(--page-bg-dark);
            background-attachment: fixed;
        }

        .app-shell-card {
            background: rgba(255, 255, 255, 0.82);
            backdrop-filter: blur(16px);
            -webkit-backdrop-filter: blur(16px);
            border: 1px solid rgba(148, 163, 184, 0.20);
            box-shadow:
                0 14px 40px rgba(15, 23, 42, 0.07),
                inset 0 1px 0 rgba(255, 255, 255, 0.55);
        }

        .dark .app-shell-card {
            background: rgba(30, 41, 59, 0.74);
            border: 1px solid rgba(148, 163, 184, 0.18);
            box-shadow:
                0 16px 44px rgba(2, 6, 23, 0.28),
                inset 0 1px 0 rgba(255, 255, 255, 0.04);
        }

        .dark #sidebar {
            background: rgba(17, 24, 39, 0.92) !important;
            border-right: 1px solid rgba(148, 163, 184, 0.16) !important;
            box-shadow: 8px 0 30px rgba(2, 6, 23, 0.18);
            backdrop-filter: blur(14px);
            -webkit-backdrop-filter: blur(14px);
        }

        .dark header {
            background: rgba(17, 24, 39, 0.72) !important;
            border-bottom-color: rgba(148, 163, 184, 0.16) !important;
            backdrop-filter: blur(14px);
            -webkit-backdrop-filter: blur(14px);
        }

        .page-inner-soft {
            position: relative;
        }

        .page-inner-soft::before {
            content: "";
            position: absolute;
            inset: 0;
            pointer-events: none;
            border-radius: 28px;
            background: linear-gradient(
                180deg,
                rgba(255, 255, 255, 0.30) 0%,
                rgba(255, 255, 255, 0.06) 100%
            );
        }

        .dark .page-inner-soft::before {
            background: linear-gradient(
                180deg,
                rgba(255, 255, 255, 0.05) 0%,
                rgba(255, 255, 255, 0.01) 100%
            );
        }

        #app-main-shell {
            margin-left: 0 !important;
            transition: margin-left 0.3s ease;
        }

        @media (max-width: 1279.98px) {
            #app-main-shell {
                margin-left: 0 !important;
                width: 100% !important;
            }
        }

        @media (min-width: 1280px) {
            #app-main-shell.sidebar-expanded {
                margin-left: 290px !important;
            }

            #app-main-shell.sidebar-collapsed {
                margin-left: 90px !important;
            }
        }

        .ai-loader-backdrop {
            position: fixed;
            inset: 0;
            z-index: 9999;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 1rem;
            background:
                radial-gradient(circle at top, rgba(37, 99, 235, 0.10), transparent 34%),
                rgba(248, 250, 252, 0.72);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
        }

        .dark .ai-loader-backdrop {
            background:
                radial-gradient(circle at top, rgba(59, 130, 246, 0.12), transparent 34%),
                rgba(2, 6, 23, 0.72);
        }

        .ai-loader-card {
            position: relative;
            width: min(100%, 280px);
            overflow: hidden;
            border-radius: 20px;
            border: 1px solid rgba(148, 163, 184, 0.22);
            background: rgba(255, 255, 255, 0.88);
            padding: 1.15rem;
            box-shadow:
                0 18px 45px rgba(15, 23, 42, 0.10),
                inset 0 1px 0 rgba(255, 255, 255, 0.55);
        }

        .dark .ai-loader-card {
            border-color: rgba(148, 163, 184, 0.16);
            background: rgba(15, 23, 42, 0.86);
            box-shadow:
                0 18px 45px rgba(2, 6, 23, 0.32),
                inset 0 1px 0 rgba(255, 255, 255, 0.04);
        }

        .ai-loader-card::before {
            content: "";
            position: absolute;
            inset: 0;
            opacity: 0.45;
            background-image:
                linear-gradient(rgba(59, 130, 246, 0.08) 1px, transparent 1px),
                linear-gradient(90deg, rgba(59, 130, 246, 0.08) 1px, transparent 1px);
            background-size: 18px 18px;
            mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.9), transparent 75%);
            pointer-events: none;
        }

        .ai-loader-core {
            position: relative;
            margin: 0 auto 0.85rem;
            height: 54px;
            width: 54px;
            border-radius: 18px;
            background: linear-gradient(135deg, #2563eb, #06b6d4);
            box-shadow: 0 12px 28px rgba(37, 99, 235, 0.22);
            animation: tech-float 1.7s ease-in-out infinite;
        }

        .ai-loader-core::before {
            content: "";
            position: absolute;
            inset: 9px;
            border-radius: 13px;
            border: 2px solid rgba(255, 255, 255, 0.78);
        }

        .ai-loader-core::after {
            content: "";
            position: absolute;
            top: 50%;
            left: 50%;
            height: 9px;
            width: 9px;
            border-radius: 9999px;
            background: #ffffff;
            transform: translate(-50%, -50%);
            box-shadow:
                -14px 0 0 rgba(255, 255, 255, 0.65),
                14px 0 0 rgba(255, 255, 255, 0.65),
                0 -14px 0 rgba(255, 255, 255, 0.55),
                0 14px 0 rgba(255, 255, 255, 0.55);
            animation: tech-pulse 1.2s ease-in-out infinite;
        }

        .ai-loader-dot {
            display: none;
        }

        .ai-loader-title {
            position: relative;
            margin-bottom: 0.25rem;
            text-align: center;
            font-size: 0.95rem;
            font-weight: 800;
            letter-spacing: -0.01em;
            color: rgb(15 23 42);
        }

        .dark .ai-loader-title {
            color: rgb(248 250 252);
        }

        .ai-loader-subtitle {
            position: relative;
            margin: 0 auto;
            max-width: 220px;
            text-align: center;
            font-size: 0.78rem;
            line-height: 1.45;
            color: rgb(71 85 105);
        }

        .dark .ai-loader-subtitle {
            color: rgb(148 163 184);
        }

        .ai-loader-status {
            position: relative;
            margin-top: 0.9rem;
        }

        .ai-loader-bar {
            position: relative;
            overflow: hidden;
            height: 6px;
            border-radius: 9999px;
            background: rgba(148, 163, 184, 0.18);
        }

        .ai-loader-bar::before {
            content: "";
            position: absolute;
            inset: 0;
            width: 38%;
            border-radius: inherit;
            background: linear-gradient(90deg, #2563eb, #06b6d4);
            animation: tech-slide 1.25s ease-in-out infinite;
        }

        .ai-loader-meta {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-top: 0.6rem;
            font-size: 0.68rem;
            font-weight: 700;
            letter-spacing: 0.03em;
            color: rgb(100 116 139);
        }

        .dark .ai-loader-meta {
            color: rgb(148 163 184);
        }

        .ai-loader-chip {
            display: inline-flex;
            align-items: center;
            gap: 0.38rem;
            padding: 0.32rem 0.62rem;
            border-radius: 9999px;
            background: rgba(37, 99, 235, 0.08);
            color: rgb(37 99 235);
        }

        .dark .ai-loader-chip {
            background: rgba(96, 165, 250, 0.12);
            color: rgb(147 197 253);
        }

        .ai-loader-chip span {
            display: inline-block;
            height: 6px;
            width: 6px;
            border-radius: 9999px;
            background: currentColor;
            animation: tech-blink 1.1s ease-in-out infinite;
        }

        @keyframes tech-float {
            0%, 100% { transform: translateY(0) scale(0.98); }
            50% { transform: translateY(-3px) scale(1); }
        }

        @keyframes tech-pulse {
            0%, 100% { opacity: 0.72; transform: translate(-50%, -50%) scale(0.9); }
            50% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
        }

        @keyframes tech-slide {
            0% { transform: translateX(-55%); }
            50% { transform: translateX(205%); }
            100% { transform: translateX(-55%); }
        }

        @keyframes tech-blink {
            0%, 100% { opacity: 0.45; transform: scale(0.9); }
            50% { opacity: 1; transform: scale(1.12); }
        }


    </style>
</head>

<body
    x-data="{ cleanupResize: null, aiLoading: true }"
    x-init="
        $store.theme.init();
        $store.sidebar.init();

        const handleResize = () => $store.sidebar.handleResize();
        window.addEventListener('resize', handleResize);
        cleanupResize = () => window.removeEventListener('resize', handleResize);

        requestAnimationFrame(() => {
            setTimeout(() => aiLoading = false, 900);
        });
    "
    x-effect="
        document.body.classList.toggle('overflow-hidden', $store.sidebar.isMobileOpen && !$store.sidebar.isDesktopViewport);
    "
    x-on:beforeunload.window="cleanupResize && cleanupResize()"
    class="min-h-screen text-gray-900 antialiased dark:text-gray-100"
>
    <x-common.preloader />

    <div
        x-show="aiLoading"
        x-transition:enter="transition ease-out duration-500"
        x-transition:enter-start="opacity-0"
        x-transition:enter-end="opacity-100"
        x-transition:leave="transition ease-in duration-400"
        x-transition:leave-start="opacity-100"
        x-transition:leave-end="opacity-0"
        class="ai-loader-backdrop"
        x-cloak
    >
        <div class="ai-loader-card">
            <div class="relative z-10">
                <div class="ai-loader-core"></div>

                <h2 class="ai-loader-title">Menyiapkan Sistem</h2>
                <p class="ai-loader-subtitle">
                    Teknologi berkembang untuk proses kerja yang lebih cepat.
                </p>

                <div class="ai-loader-status">
                    <div class="ai-loader-bar"></div>
                    <div class="ai-loader-meta">
                        <span class="ai-loader-chip"><span></span> Loading</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="relative isolate min-h-screen">
        @include('layouts.sidebar')

        <div
            id="app-main-shell"
            class="relative z-0 min-h-screen"
            :class="($store.sidebar.isExpanded || $store.sidebar.isHovered) ? 'sidebar-expanded' : 'sidebar-collapsed'"
        >
            <div class="relative z-10">
                <div class="relative z-20">
                    @include('layouts.app-header')
                </div>

                <main class="relative z-10 px-3 pb-6 pt-4 sm:px-4 md:px-6 xl:px-8">
                    <div class="mx-auto max-w-[1920px]">
                        <div class="app-shell-card page-inner-soft overflow-hidden rounded-[28px]">
                            <div class="relative z-10 p-4 md:p-6">
                                @yield('content')
                            </div>
                        </div>
                    </div>
                </main>
            </div>
        </div>
    </div>

    <x-ui.toast />

    @includeIf('partials.attendance-popup')

    @stack('modals')
    @stack('scripts')
</body>

</html>