<?php 
$currentTheme = \App\Services\ThemeService::getCurrent(); 
$db = db();
$categories = $db->query("SELECT * FROM categories ORDER BY name ASC")->fetchAll();
$currentLang = \App\Services\LanguageService::getLang();
?>
<!DOCTYPE html>
<html lang="<?php echo $currentLang; ?>" data-theme="<?php echo $currentTheme; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $title ?? htmlspecialchars(get_setting('site_name', 'ApkNusa')); ?></title>
    
    <meta name="description" content="<?php echo $meta_description ?? htmlspecialchars(get_setting('meta_description', __('meta_description_default'))); ?>">
    <meta name="keywords" content="<?php echo $meta_keywords ?? htmlspecialchars(get_setting('meta_keywords', __('meta_keywords_default'))); ?>">
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/dist/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link rel="icon" type="image/x-icon" href="/<?php echo get_setting('site_favicon'); ?>">
    <link rel="stylesheet" href="/assets/css/custom.css?v=<?php echo time(); ?>">
    <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&display=swap" rel="stylesheet">

    <?php echo get_setting('head_js'); ?>

    <script>
        // Early theme initialization to prevent flash
        (function() {
            const savedTheme = localStorage.getItem('theme');
            if (savedTheme) {
                document.documentElement.setAttribute('data-theme', savedTheme);
            }
        })();
    </script>
</head>
<body class="animated-bg">
    <!-- Dynamic background blobs -->
    <div class="bg-blob" style="position: fixed; top: -10%; left: -10%; width: 45%; height: 45%; border-radius: 50%; filter: blur(100px); z-index: -1; opacity: 0.15; animation: float-blob 25s infinite alternate, color-cycle 30s infinite;"></div>
    <div class="bg-blob" style="position: fixed; bottom: -10%; right: -10%; width: 50%; height: 50%; border-radius: 50%; filter: blur(100px); z-index: -1; opacity: 0.15; animation: float-blob 30s infinite alternate-reverse, color-cycle 35s infinite reverse;"></div>
    <div class="bg-blob" style="position: fixed; top: 40%; left: 30%; width: 35%; height: 35%; border-radius: 50%; filter: blur(100px); z-index: -1; opacity: 0.1; animation: float-blob 20s infinite alternate, color-cycle 25s infinite 5s;"></div>

    <style>
        @keyframes float-blob {
            0% { transform: translate(0, 0) scale(1); }
            100% { transform: translate(15%, 15%) scale(1.2); }
        }
        @keyframes color-cycle {
            0% { background-color: #10B981; }
            25% { background-color: #3B82F6; }
            50% { background-color: #F59E0B; }
            75% { background-color: #EC4899; }
            100% { background-color: #10B981; }
        }
        
        @media (max-width: 991.98px) {
            .mobile-theme-row {
                display: flex;
                align-items: center;
                justify-content: space-between;
                background: var(--subtle-bg);
                padding: 0.65rem 1rem;
                border-radius: 12px;
                border: 1px solid var(--border-color);
                margin-bottom: 0.75rem;
            }
            .mobile-lang-row {
                display: flex;
                align-items: center;
                justify-content: space-between;
                background: var(--subtle-bg);
                padding: 0.65rem 1rem;
                border-radius: 12px;
                border: 1px solid var(--border-color);
            }
        }
    </style>

    <nav class="navbar navbar-expand-lg navbar-light border-bottom py-3 sticky-top shadow-sm">
        <div class="container">
            <a class="navbar-brand fw-bold text-success d-flex align-items-center" href="/">
                <?php if (get_setting('site_icon')): ?>
                    <img src="/<?php echo get_setting('site_icon'); ?>" alt="Logo" class="me-2" style="height: 30px;">
                <?php else: ?>
                    <i class="fas fa-robot me-2"></i>
                <?php endif; ?>
                <?php echo htmlspecialchars(get_setting('site_name', 'ApkNusa')); ?>
            </a>
            
            <button class="navbar-toggler border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto align-items-lg-center">
                    <li class="nav-item">
                        <a class="nav-link px-3" href="/"><?php echo __('home'); ?></a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link px-3" href="/blog">Blog</a>
                    </li>

                    <li class="nav-item dropdown px-3">
                        <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">
                            <?php echo __('categories'); ?>
                        </a>
                        <ul class="dropdown-menu shadow border-0">
                            <li><a class="dropdown-item category-filter" href="/" data-category=""><?php echo __('all_categories'); ?></a></li>
                            <?php foreach ($categories as $cat): ?>
                                <li><a class="dropdown-item category-filter" href="/?category=<?php echo $cat['slug']; ?>" data-category="<?php echo htmlspecialchars($cat['slug']); ?>"><?php echo htmlspecialchars($cat['name']); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    </li>
                    
                    <li class="nav-item dropdown d-none d-lg-block px-2">
                        <a class="nav-link dropdown-toggle lang-selector-btn" href="#" role="button" data-bs-toggle="dropdown">
                            <i class="fas fa-globe"></i> <?php echo $currentLang == 'id' ? 'Indonesia' : 'English'; ?>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end shadow border-0">
                            <li><a class="dropdown-item d-flex align-items-center" href="/lang/id"><span class="me-2">🇮🇩</span> Indonesia</a></li>
                            <li><a class="dropdown-item d-flex align-items-center" href="/lang/en"><span class="me-2">🇺🇸</span> English</a></li>
                        </ul>
                    </li>

                    <!-- Theme Toggle Desktop -->
                    <li class="nav-item d-none d-lg-block px-2">
                        <button id="theme-toggle" class="theme-toggle-btn" aria-label="Toggle theme">
                            <i class="fa-solid <?php echo $currentTheme === 'dark' ? 'fa-sun' : 'fa-moon'; ?>"></i>
                        </button>
                    </li>

                    <?php if (isset($_SESSION['user_id'])): ?>
                        <li class="nav-item ms-lg-3">
                            <a class="btn btn-success rounded-pill px-4 d-flex align-items-center w-100 w-lg-auto" href="/profile">
                                <i class="fas fa-user-circle me-2"></i> <?php echo __('profile'); ?>
                            </a>
                        </li>
                    <?php else: ?>
                        <li class="nav-item ms-lg-3">
                            <a class="btn btn-outline-secondary border-0 px-3 w-100 w-lg-auto text-start text-lg-center mb-2 mb-lg-0" href="/login"><?php echo __('login'); ?></a>
                        </li>
                        <li class="nav-item ms-lg-2">
                            <a class="btn btn-success rounded-pill px-4 w-100 w-lg-auto mb-2 mb-lg-0" href="/register"><?php echo __('register'); ?></a>
                        </li>
                    <?php endif; ?>

                    <!-- Mobile Controls -->
                    <li class="d-lg-none mt-3">
                        <div class="mobile-theme-row">
                            <span class="theme-status-text small fw-bold"><?php echo $currentTheme === 'dark' ? 'Dark Mode' : 'Light Mode'; ?></span>
                            <button id="theme-toggle-mobile" class="theme-toggle-btn" aria-label="Toggle theme">
                                <i class="fa-solid <?php echo $currentTheme === 'dark' ? 'fa-sun' : 'fa-moon'; ?>"></i>
                            </button>
                        </div>
                        
                        <div class="mobile-lang-row">
                            <div class="d-flex align-items-center gap-2">
                                <i class="fas fa-globe text-success"></i>
                                <span class="small fw-bold">Language</span>
                            </div>
                            <div class="dropdown">
                                <button class="btn btn-link text-decoration-none p-0 dropdown-toggle small fw-bold" type="button" data-bs-toggle="dropdown" style="color: inherit;">
                                    <?php echo $currentLang == 'id' ? 'Indonesia' : 'English'; ?>
                                </button>
                                <ul class="dropdown-menu dropdown-menu-end shadow border-0">
                                    <li><a class="dropdown-item" href="/lang/id">🇮🇩 Indonesia</a></li>
                                    <li><a class="dropdown-item" href="/lang/en">🇺🇸 English</a></li>
                                </ul>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <main class="min-vh-100">
