<?php
require_once __DIR__ . '/auth.php';
requireAdmin();
$admin = getAdminUser();
$currentPage = $_GET['page'] ?? 'dashboard';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>VocabPanda Admin</title>
    <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.4.0/css/all.min.css">
    <style>
        :root {
            --sidebar-width: 260px;
            --sidebar-bg: #1e293b;             /* slate-800 */
            --sidebar-hover: rgba(255,255,255,0.08);
            --sidebar-active: rgba(255,255,255,0.15);
            --accent: #f59e0b;                 /* amber-500 */
            --topbar-bg: #ffffff;
            --body-bg: #f1f5f9;               /* slate-100 */
            --text-light: #cbd5e1;
            --text-white: #f8fafc;
            --transition: 0.3s ease;
        }

        body {
            background: var(--body-bg);
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
            overflow-x: hidden;
        }

        /* ---------- Sidebar ---------- */
        .sidebar {
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            width: var(--sidebar-width);
            background: var(--sidebar-bg);
            color: var(--text-light);
            z-index: 1040;
            transform: translateX(-100%);
            transition: transform var(--transition);
            box-shadow: 2px 0 12px rgba(0,0,0,0.25);
        }

        .sidebar.show {
            transform: translateX(0);
        }

        @media (min-width: 992px) {
            .sidebar {
                transform: translateX(0);
                position: fixed;
                height: 100vh;
            }
        }

        .sidebar .brand {
            padding: 1.5rem 1rem;
            font-size: 1.3rem;
            font-weight: 700;
            letter-spacing: 0.5px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            background: rgba(0,0,0,0.2);
            color: var(--text-white);
        }

        .sidebar .nav-link {
            color: var(--text-light);
            padding: 0.8rem 1.2rem;
            font-weight: 500;
            border-left: 3px solid transparent;
            display: flex;
            align-items: center;
            text-decoration: none;
            transition: background 0.2s, border-color 0.2s;
        }

        .sidebar .nav-link:hover {
            background: var(--sidebar-hover);
            color: #fff;
            border-left-color: var(--accent);
        }

        .sidebar .nav-link.active {
            background: var(--sidebar-active);
            color: #fff;
            font-weight: 600;
            border-left-color: var(--accent);
        }

        .sidebar .nav-link i {
            width: 24px;
            text-align: center;
            margin-right: 0.75rem;
            font-size: 1rem;
        }

        /* ---------- Overlay (mobile) ---------- */
        .sidebar-overlay {
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,0.5);
            z-index: 1030;
            opacity: 0;
            visibility: hidden;
            transition: opacity var(--transition);
        }

        .sidebar-overlay.active {
            opacity: 1;
            visibility: visible;
        }

        @media (min-width: 992px) {
            .sidebar-overlay {
                display: none !important;
            }
        }

        /* ---------- Main content area ---------- */
        .main-content {
            margin-left: 0;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            background: var(--body-bg);
            transition: margin-left var(--transition);
        }

        @media (min-width: 992px) {
            .main-content {
                margin-left: var(--sidebar-width);
            }
        }

        /* ---------- Topbar ---------- */
        .topbar {
            background: var(--topbar-bg);
            padding: 0.75rem 1.5rem;
            box-shadow: 0 1px 4px rgba(0,0,0,0.05);
            display: flex;
            justify-content: space-between;
            align-items: center;
            z-index: 1020;
        }

        .toggle-btn {
            font-size: 1.5rem;
            color: #334155;
            cursor: pointer;
            display: block;
        }

        @media (min-width: 992px) {
            .toggle-btn {
                display: none;
            }
        }

        .topbar h2 {
            font-size: 1.2rem;
            font-weight: 600;
            color: #0f172a;
            margin: 0;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 1rem;
            font-size: 0.9rem;
        }

        .user-info i {
            color: #64748b;
            font-size: 1.2rem;
        }

        .logout-link {
            color: #dc2626;
            text-decoration: none;
            font-weight: 500;
        }

        .logout-link:hover {
            color: #b91c1c;
        }

        /* ---------- Content wrapper ---------- */
        .content-wrap {
            padding: 1.5rem;
            flex: 1;
        }

        /* ---------- Responsive tables already handled by .table-responsive ---------- */
        /* ---------- Minor mobile adjustments ---------- */
        @media (max-width: 576px) {
            .user-info .fw-semibold {
                display: none;
            }
            .content-wrap {
                padding: 1rem;
            }
        }
    </style>
</head>
<body>
<div id="wrapper">
    <!-- Overlay for mobile sidebar -->
    <div class="sidebar-overlay" id="sidebarOverlay" onclick="closeSidebar()"></div>

    <!-- Sidebar -->
    <aside class="sidebar" id="adminSidebar">
        <div class="brand">
            <i class="fa fa-paw me-2"></i> VocabPanda
        </div>
        <nav class="flex-grow-1 mt-2">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a href="index.php?page=dashboard" class="nav-link <?= $currentPage === 'dashboard' ? 'active' : '' ?>">
                        <i class="fa fa-th-large"></i> Dashboard
                    </a>
                </li>
                <li class="nav-item">
                    <a href="index.php?page=courses" class="nav-link <?= $currentPage === 'courses' ? 'active' : '' ?>">
                        <i class="fa fa-book"></i> Courses
                    </a>
                </li>
                <li class="nav-item">
                    <a href="index.php?page=users" class="nav-link <?= $currentPage === 'users' ? 'active' : '' ?>">
                        <i class="fa fa-users"></i> Users
                    </a>
                </li>
                <li class="nav-item">
                    <a href="index.php?page=subscriptions" class="nav-link <?= $currentPage === 'subscriptions' ? 'active' : '' ?>">
                        <i class="fa fa-credit-card"></i> Subscriptions
                    </a>
                </li>
                <li class="nav-item">
                    <a href="index.php?page=transactions" class="nav-link <?= $currentPage === 'transactions' ? 'active' : '' ?>">
                        <i class="fa fa-exchange-alt"></i> Transactions
                    </a>
                </li>
                <li class="nav-item">
                    <a href="index.php?page=leaderboard" class="nav-link <?= $currentPage === 'leaderboard' ? 'active' : '' ?>">
                        <i class="fa fa-trophy"></i> Leaderboard
                    </a>
                </li>
            </ul>
        </nav>
        <div class="p-3 text-center small opacity-75">© <?= date('Y') ?> VocabPanda</div>
    </aside>

    <!-- Main Content -->
    <div class="main-content">
        <header class="topbar">
            <div class="d-flex align-items-center">
                <span class="toggle-btn" onclick="toggleSidebar()">
                    <i class="fa fa-bars"></i>
                </span>
                <h2 class="ms-2">Admin Panel</h2>
            </div>
            <div class="user-info">
                <i class="fa fa-user-circle"></i>
                <span class="fw-semibold"><?= htmlspecialchars($admin['name'] ?? 'Admin') ?></span>
                <a href="logout.php" class="logout-link"><i class="fa fa-sign-out-alt me-1"></i>Logout</a>
            </div>
        </header>
        <main class="content-wrap">