<?php
// 1. FIXED: Accurately determine active page even if URL is just "/admin/"
$current_page = basename($_SERVER['PHP_SELF']);
if ($current_page === '' || $current_page === 'index.php' || $current_page === 'admin') {
    $current_page = 'dashboard.php';
}

// Helper function to assign premium dual-theme active classes
function getNavClass($page, $current_page) {
    if ($current_page === $page) {
        return 'bg-emerald-50 text-emerald-600 border-emerald-200 shadow-sm dark:bg-emerald-500/10 dark:text-emerald-400 dark:border-emerald-500/20';
    }
    return 'text-slate-500 hover:bg-slate-100 hover:text-slate-900 border-transparent dark:text-gray-400 dark:hover:bg-slate-800/50 dark:hover:text-white';
}
?>
<aside class="w-full md:w-64 bg-white border-b md:border-b-0 md:border-r border-slate-200 dark:bg-slate-900 dark:border-slate-800 flex flex-col shrink-0 md:min-h-screen md:sticky md:top-0 z-40 transition-colors duration-300">
    
    <!-- Header -->
    <div class="flex items-center justify-between p-5 border-b border-slate-200 dark:border-slate-800">
        <div class="flex items-center space-x-3">
            <div class="w-8 h-8 bg-gradient-to-br from-emerald-500 to-teal-600 rounded-lg flex items-center justify-center font-black text-white shadow-lg shadow-emerald-500/25 text-sm">A</div>
            <span class="font-extrabold tracking-tight text-slate-900 dark:text-white text-sm">ADMIN</span>
        </div>
        
        <button id="admin-mobile-btn" onclick="toggleMobileMenu()" class="md:hidden text-slate-500 hover:text-slate-900 dark:text-gray-400 dark:hover:text-white focus:outline-none bg-slate-50 dark:bg-slate-800 p-2 rounded-lg border border-slate-200 dark:border-slate-700 transition">
            <svg id="menu-icon" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg>
            <svg id="close-icon" class="w-5 h-5 hidden" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
        </button>
    </div>

    <!-- Navigation -->
    <nav id="admin-nav-menu" class="hidden md:flex flex-col px-3 py-4 md:py-3 space-y-0.5 text-xs font-semibold tracking-wider flex-1 bg-white dark:bg-slate-900 md:bg-transparent absolute md:relative w-full left-0 top-[73px] md:top-0 border-b md:border-b-0 border-slate-200 dark:border-slate-800 z-50 overflow-y-auto pb-6" style="max-height: calc(100vh - 73px);">
        
        <!-- Dashboard -->
        <a href="<?php echo SITE_URL; ?>/admin/dashboard.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 mt-1 <?php echo getNavClass('dashboard.php', $current_page); ?>">
            <span class="text-base opacity-80">📊</span>
            <span>Dashboard</span>
        </a>
        
        <a href="<?php echo SITE_URL; ?>/admin/users.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('users.php', $current_page); ?>">
            <span class="text-base opacity-80">👥</span>
            <span>Users & VIPs</span>
        </a>

        <!-- Engine Control -->
        <div class="px-3 pt-4 pb-1">
            <span class="text-[9px] font-black tracking-[0.15em] text-slate-400 uppercase">Engine Control</span>
        </div>
        <a href="<?php echo SITE_URL; ?>/admin/bankers-control.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('bankers-control.php', $current_page); ?>">
            <span class="text-base opacity-80">🎯</span>
            <span>Bankers Control</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/ai-engine.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('ai-engine.php', $current_page); ?>">
            <span class="text-base opacity-80">🧠</span>
            <span>AI Engine</span>
        </a>

        <!-- Revenue & Billing -->
        <div class="px-3 pt-4 pb-1">
            <span class="text-[9px] font-black tracking-[0.15em] text-slate-400 uppercase">Revenue & Billing</span>
        </div>
        <a href="<?php echo SITE_URL; ?>/admin/plan-management.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('plan-management.php', $current_page); ?>">
            <span class="text-base opacity-80">📦</span>
            <span>Pricing Plans</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/payment-integration.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('payment-integration.php', $current_page); ?>">
            <span class="text-base opacity-80">💳</span>
            <span>Payment Gateways</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/transaction-approval.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('transaction-approval.php', $current_page); ?>">
            <span class="text-base opacity-80">✅</span>
            <span>Manual Approvals</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/monetization.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('monetization.php', $current_page); ?>">
            <span class="text-base opacity-80">📈</span>
            <span>Monetization</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/referral-management.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('referral-management.php', $current_page); ?>">
            <span class="text-base opacity-80">🤝</span>
            <span>Referral System</span>
        </a>

        <!-- Content CMS -->
        <div class="px-3 pt-4 pb-1">
            <span class="text-[9px] font-black tracking-[0.15em] text-slate-400 uppercase">Content CMS</span>
        </div>
        <a href="<?php echo SITE_URL; ?>/admin/news-cms.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('news-cms.php', $current_page); ?>">
            <span class="text-base opacity-80">📰</span>
            <span>Blog & News</span>
        </a>
        
        <!-- Frontend Pages (Dropdown) -->
        <div class="space-y-0.5">
            <button onclick="document.getElementById('page-management-menu').classList.toggle('hidden')" class="w-full flex items-center justify-between px-3 py-2.5 rounded-xl text-slate-500 hover:bg-slate-100 hover:text-slate-900 dark:text-gray-400 dark:hover:bg-slate-800/50 dark:hover:text-white transition font-semibold text-xs tracking-wider focus:outline-none">
                <div class="flex items-center space-x-3">
                    <span class="text-base opacity-80">🖥️</span>
                    <span>Frontend Pages</span>
                </div>
                <svg class="w-3.5 h-3.5 opacity-50 transition-transform duration-200" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
            </button>
            
            <div id="page-management-menu" class="hidden flex flex-col space-y-0.5 pl-10 pr-3 py-1">
                <a href="<?php echo SITE_URL; ?>/admin/cont/home-management.php" class="<?php echo $current_page == 'home-management.php' ? 'text-emerald-500 font-bold' : 'text-slate-500 dark:text-gray-400 hover:text-emerald-600 dark:hover:text-emerald-400'; ?> text-[11px] uppercase tracking-widest py-1.5 transition">🏠 Home Page</a>
                <a href="<?php echo SITE_URL; ?>/admin/cont/header-management.php" class="<?php echo $current_page == 'header-management.php' ? 'text-emerald-500 font-bold' : 'text-slate-500 dark:text-gray-400 hover:text-emerald-600 dark:hover:text-emerald-400'; ?> text-[11px] uppercase tracking-widest py-1.5 transition">🔝 Header & SEO</a>
                <a href="<?php echo SITE_URL; ?>/admin/cont/footer-management.php" class="<?php echo $current_page == 'footer-management.php' ? 'text-emerald-500 font-bold' : 'text-slate-500 dark:text-gray-400 hover:text-emerald-600 dark:hover:text-emerald-400'; ?> text-[11px] uppercase tracking-widest py-1.5 transition">🌍 Footer Section</a>
            </div>
        </div>

        <!-- System -->
        <div class="px-3 pt-4 pb-1">
            <span class="text-[9px] font-black tracking-[0.15em] text-slate-400 uppercase">System</span>
        </div>
        <a href="<?php echo SITE_URL; ?>/admin/api-integration.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('api-integration.php', $current_page); ?>">
            <span class="text-base opacity-80">🔌</span>
            <span>Data API</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/email-management.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('email-management.php', $current_page); ?>">
            <span class="text-base opacity-80">✉️</span>
            <span>System Emails</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/settings.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('settings.php', $current_page); ?>">
            <span class="text-base opacity-80">⚙️</span>
            <span>Settings</span>
        </a>
        <a href="<?php echo SITE_URL; ?>/admin/send-system-notification.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('send-system-notification.php', $current_page); ?>">
    <span class="text-base">📢</span>
    <span>System Broadcast</span>
</a>
        <a href="<?php echo SITE_URL; ?>/admin/cron-management.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl transition-all duration-200 <?php echo getNavClass('cron-management.php', $current_page); ?>">
    <span class="text-base">⏰</span>
    <span>Cron Manager</span>
</a>

        <div class="h-px bg-slate-200 dark:bg-slate-700/50 my-3 mx-3"></div>

        <!-- Logout & Theme -->
        <a href="<?php echo SITE_URL; ?>/admin/logout.php" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl text-rose-500 hover:bg-rose-50 border border-transparent dark:hover:bg-rose-500/10 dark:hover:border-rose-500/20 transition-all duration-200">
            <span class="text-base opacity-80">🚪</span>
            <span>Sign Out</span>
        </a>

        <!-- <button id="theme-toggle-btn" onclick="toggleTheme()" class="mt-2 flex items-center justify-between px-4 py-3 rounded-xl bg-slate-100 hover:bg-slate-200 dark:bg-slate-800 dark:hover:bg-slate-700 border border-slate-200 dark:border-slate-700 transition-all text-left w-full group cursor-pointer">
            <span class="text-xs font-bold text-slate-600 dark:text-gray-300 tracking-wider">APPEARANCE</span>
            <div class="bg-white dark:bg-slate-900 shadow-sm border border-slate-200 dark:border-slate-700 rounded-md p-1.5 flex items-center justify-center group-hover:scale-105 transition-transform">
                <span id="theme-icon" class="text-base">🌙</span>
            </div>
        </button> -->
    </nav>
</aside>

<script>
    // --- Mobile Menu Logic ---
    function toggleMobileMenu() {
        const menu = document.getElementById('admin-nav-menu');
        const menuIcon = document.getElementById('menu-icon');
        const closeIcon = document.getElementById('close-icon');
        
        menu.classList.toggle('hidden');
        menu.classList.toggle('flex');
        menuIcon.classList.toggle('hidden');
        closeIcon.classList.toggle('hidden');
    }

    // --- Theme Toggle Logic ---
    const htmlEl = document.documentElement;
    const themeIcon = document.getElementById('theme-icon');

    // Instantly check storage on page load
    if (localStorage.getItem('fpp_theme') === 'dark') {
        htmlEl.classList.add('dark');
        if(themeIcon) themeIcon.textContent = '☀️';
    } else {
        htmlEl.classList.remove('dark');
        if(themeIcon) themeIcon.textContent = '🌙';
    }

    // Button Click Function
    function toggleTheme() {
        htmlEl.classList.toggle('dark');
        
        if (htmlEl.classList.contains('dark')) {
            localStorage.setItem('fpp_theme', 'dark');
            themeIcon.textContent = '☀️';
        } else {
            localStorage.setItem('fpp_theme', 'light');
            themeIcon.textContent = '🌙';
        }
    }
    
    // Auto-expand the "Page Management" dropdown if the user is currently on one of its pages
    document.addEventListener("DOMContentLoaded", function() {
        const current = "<?php echo $current_page; ?>";
        if (current === 'home-management.php' || current === 'header-management.php' || current === 'footer-management.php') {
            document.getElementById('page-management-menu').classList.remove('hidden');
        }
    });
</script>