<!DOCTYPE html>
<html lang="id">
<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', 'TUNJUK-i')</title>

    @vite('resources/css/app.css')
    @livewireStyles

    <style>
        /* ============================================================ */
        /* SIDEBAR TRANSISI */
        /* ============================================================ */
        .sidebar {
            transition: width 0.3s ease, transform 0.3s ease;
            overflow: hidden;
            width: 260px;
        }
        .sidebar-collapsed {
            width: 70px !important;
        }
        .sidebar-collapsed .menu-text {
            display: none !important;
        }
        .sidebar-collapsed .menu-item {
            justify-content: center !important;
            padding: 12px 0 !important;
        }
        .sidebar-collapsed .menu-item i {
            margin-right: 0 !important;
            font-size: 20px !important;
        }
        .sidebar-collapsed .section-header span {
            display: none !important;
        }
        .sidebar-collapsed .section-header i {
            margin-right: 0 !important;
            font-size: 18px !important;
        }
        .sidebar-collapsed .section-header {
            justify-content: center !important;
            padding: 12px 0 !important;
            text-align: center !important;
        }
        .sidebar-collapsed .sidebar-brand-text {
            display: none !important;
        }
        .sidebar-collapsed .sidebar-brand {
            justify-content: center !important;
            padding: 12px 0 !important;
        }
        .sidebar-collapsed .badge-soon {
            display: none !important;
        }
        .sidebar-collapsed .border-t {
            display: none !important;
        }

        .main-content {
            transition: margin-left 0.3s ease;
            margin-left: 0;
        }
        .main-content-expanded {
            margin-left: 0;
        }

        .sidebar::-webkit-scrollbar {
            width: 4px;
        }
        .sidebar::-webkit-scrollbar-track {
            background: #f1f1f1;
        }
        .sidebar::-webkit-scrollbar-thumb {
            background: #c9a84c;
            border-radius: 4px;
        }
        .sidebar::-webkit-scrollbar-thumb:hover {
            background: #b8962a;
        }

        @media (max-width: 768px) {
            .sidebar {
                position: fixed;
                top: 64px;
                left: 0;
                height: calc(100vh - 64px);
                z-index: 999;
                transform: translateX(-100%);
                width: 260px !important;
            }
            .sidebar-open {
                transform: translateX(0) !important;
            }
            .sidebar-collapsed {
                width: 70px !important;
                transform: translateX(0) !important;
            }
            .sidebar-backdrop {
                display: none;
                position: fixed;
                top: 64px;
                left: 0;
                width: 100%;
                height: calc(100vh - 64px);
                background: rgba(0,0,0,0.3);
                z-index: 998;
            }
            .sidebar-backdrop.show {
                display: block;
            }
        }

        .menu-item {
            display: flex;
            align-items: center;
            padding: 10px 16px;
            margin: 4px 8px;
            border-radius: 12px;
            color: #4a5568;
            transition: all 0.3s ease;
            text-decoration: none;
            position: relative;
        }
        .menu-item:hover {
            background: linear-gradient(135deg, #fef9e7 0%, #fdebd0 100%);
            color: #0b2b4a;
            transform: translateX(4px);
            box-shadow: 0 4px 12px rgba(201, 168, 76, 0.2);
        }
        .menu-item.active {
            background: linear-gradient(135deg, #0b2b4a 0%, #1a4a6e 100%);
            color: #ffffff !important;
            box-shadow: 0 4px 16px rgba(11, 43, 74, 0.3);
        }
        .menu-item.active i {
            color: #c9a84c !important;
        }
        .menu-item.active .menu-text {
            color: #ffffff !important;
        }
        .menu-item i {
            width: 24px;
            text-align: center;
            font-size: 18px;
            color: #718096;
            transition: all 0.3s ease;
            flex-shrink: 0;
        }
        .menu-item:hover i {
            color: #c9a84c;
            transform: scale(1.1);
        }
        .menu-item .menu-text {
            margin-left: 12px;
            font-size: 14px;
            font-weight: 500;
            white-space: nowrap;
            transition: all 0.3s ease;
            flex: 1;
        }

        .section-header {
            font-size: 11px;
            font-weight: 700;
            letter-spacing: 1.2px;
            text-transform: uppercase;
            color: #a0aec0;
            padding: 16px 16px 8px 16px;
            border-bottom: 2px solid #c9a84c;
            margin: 0 8px 8px 8px;
            display: flex;
            align-items: center;
            transition: all 0.3s ease;
        }
        .section-header i {
            color: #c9a84c;
            font-size: 14px;
            flex-shrink: 0;
        }
        .section-header span {
            margin-left: 8px;
            white-space: nowrap;
        }

        .badge-soon {
            background: #edf2f7;
            color: #a0aec0;
            font-size: 9px;
            padding: 2px 8px;
            border-radius: 20px;
            margin-left: auto;
            font-weight: 600;
            letter-spacing: 0.5px;
            white-space: nowrap;
            transition: all 0.3s ease;
        }
        .menu-item:hover .badge-soon {
            background: #c9a84c;
            color: #ffffff;
        }

        .menu-item-disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        .menu-item-disabled:hover {
            transform: none !important;
            background: transparent !important;
            box-shadow: none !important;
        }
    </style>
</head>
<body class="bg-gray-100 font-sans antialiased">

    <!-- Navbar -->
    <nav class="bg-[#0b2b4a] text-white shadow-lg sticky top-0 z-50">
        <div class="px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center h-16">
                <div class="flex items-center space-x-3">
                    <button onclick="toggleSidebar()" class="text-white hover:text-[#c9a84c] transition p-2 rounded-lg hover:bg-white/10">
                        <i class="fas fa-bars text-xl"></i>
                    </button>
                    @include('partials.logo')
                    <span class="text-xs bg-[#c9a84c] text-[#0b2b4a] px-2 py-0.5 rounded">v1.0</span>
                </div>

                <div class="flex items-center space-x-4">
                    @auth
                        <span class="text-sm text-gray-300 hidden md:inline">
                            <i class="fas fa-user-circle mr-1"></i>
                            {{ Auth::user()->name }}
                        </span>
                        <span class="text-xs px-3 py-1 rounded-full bg-gray-600 text-white hidden md:inline">
                            {{ Auth::user()->role_label ?? Auth::user()->role }}
                        </span>
                        <form method="POST" action="{{ route('logout') }}" class="inline">
                            @csrf
                            <button type="submit" class="text-sm text-gray-300 hover:text-white transition">
                                <i class="fas fa-sign-out-alt mr-1"></i> Logout
                            </button>
                        </form>
                    @else
                        <a href="{{ route('login') }}" class="text-sm hover:text-white transition">
                            <i class="fas fa-sign-in-alt mr-1"></i> Login
                        </a>
                        <a href="{{ route('register') }}" class="text-sm bg-[#c9a84c] hover:bg-yellow-600 text-[#0b2b4a] px-4 py-2 rounded transition">
                            <i class="fas fa-user-plus mr-1"></i> Register
                        </a>
                    @endauth
                </div>
            </div>
        </div>
    </nav>

    <!-- Backdrop (Mobile) -->
    <div id="sidebarBackdrop" class="sidebar-backdrop" onclick="closeSidebarMobile()"></div>

    <!-- Content -->
    <div class="flex">
        @auth
            <!-- Sidebar -->
            <aside id="sidebar" class="sidebar bg-white shadow-lg min-h-screen border-r border-gray-200 flex-shrink-0">
                <div class="p-4 border-b border-gray-200 sidebar-brand flex items-center">
                    <i class="fas fa-{{ 
                        Auth::user()->role === 'super_admin' ? 'crown' : 
                        (Auth::user()->role === 'admin' ? 'shield-alt' : 
                        (Auth::user()->role === 'ppk' ? 'file-contract' : 
                        (Auth::user()->role === 'contractor' ? 'user-tie' : 
                        (Auth::user()->role === 'agent' ? 'robot' : 
                        (Auth::user()->role === 'bank' ? 'university' : 'clipboard-check'))))) 
                    }} text-[#c9a84c] text-xl mr-2 sidebar-brand-icon"></i>
                    <span class="text-sm font-semibold text-gray-700 sidebar-brand-text">Menu</span>
                </div>
                <nav class="p-2 space-y-1 overflow-y-auto" style="max-height: calc(100vh - 140px);">
                    @include('partials.sidebar')
                </nav>
            </aside>

            <!-- Main Content -->
            <main id="mainContent" class="flex-1 px-4 sm:px-6 lg:px-8 py-6 main-content">
                @yield('content')
            </main>
        @else
            <main class="flex-1 max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
                @yield('content')
            </main>
        @endauth
    </div>

    

    @livewireScripts
    @include('partials.footer')
    @vite('resources/js/app.js')
    @stack('scripts')

    <script>
        // ============================================================
        // SIDEBAR TOGGLE
        // ============================================================
        function toggleSidebar() {
            const sidebar = document.getElementById('sidebar');
            if (!sidebar) return;
            
            const isMobile = window.innerWidth <= 768;
            
            if (isMobile) {
                sidebar.classList.toggle('sidebar-open');
                const backdrop = document.getElementById('sidebarBackdrop');
                if (backdrop) backdrop.classList.toggle('show');
            } else {
                sidebar.classList.toggle('sidebar-collapsed');
                localStorage.setItem('sidebarCollapsed', sidebar.classList.contains('sidebar-collapsed'));
            }
        }

        function closeSidebarMobile() {
            const sidebar = document.getElementById('sidebar');
            const backdrop = document.getElementById('sidebarBackdrop');
            if (sidebar) sidebar.classList.remove('sidebar-open');
            if (backdrop) backdrop.classList.remove('show');
        }

        // ============================================================
        // RESTORE SIDEBAR STATE
        // ============================================================
        document.addEventListener('DOMContentLoaded', function() {
            const sidebar = document.getElementById('sidebar');
            if (!sidebar) return;
            
            const isMobile = window.innerWidth <= 768;
            
            if (!isMobile) {
                const isCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
                if (isCollapsed) {
                    sidebar.classList.add('sidebar-collapsed');
                }
            }

            window.addEventListener('resize', function() {
                const isMobileNow = window.innerWidth <= 768;
                if (isMobileNow) {
                    sidebar.classList.remove('sidebar-collapsed');
                } else {
                    const isCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
                    if (isCollapsed) {
                        sidebar.classList.add('sidebar-collapsed');
                    }
                }
            });
        });

        // ============================================================
        // ACTIVE MENU HIGHLIGHT
        // ============================================================
        document.addEventListener('DOMContentLoaded', function() {
            const currentUrl = window.location.pathname;
            document.querySelectorAll('.menu-item').forEach(function(item) {
                const href = item.getAttribute('href');
                if (href && href !== '#' && currentUrl === href) {
                    item.classList.add('active');
                }
            });
        });
    </script>
</body>
</html>