<?php
session_start();

if (!isset($_SESSION["user"]) || empty($_SESSION["user"]["is_admin"])) {
    header("Location: ../Index.php");
    exit;
}

$username = $_SESSION["user"]["username"] ?? "Admin";

$avatar = "../Assets/Logo.png";

if (!empty($_SESSION["user"]["avatar"]) && !empty($_SESSION["user"]["discord_id"])) {
    $avatar = "https://cdn.discordapp.com/avatars/" .
        $_SESSION["user"]["discord_id"] . "/" .
        $_SESSION["user"]["avatar"] . ".png";
}

require "../Auth/db.php";

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if (isset($_GET["delete_movie"])) {

    $deleteMovieId = (int)$_GET["delete_movie"];

    try {

        // REMOVE HERO REFERENCES FIRST
        $stmt = $pdo->prepare("
            DELETE FROM hero_movies
            WHERE movie_id = ?
        ");

        $stmt->execute([$deleteMovieId]);

        // DELETE MOVIE
        $stmt = $pdo->prepare("
            DELETE FROM media_movies
            WHERE id = ?
        ");

        $stmt->execute([$deleteMovieId]);

    } catch (PDOException $e) {

        die($e->getMessage());

    }

    header("Location: Dashboard.php?tab=db_movies");
    exit;
}

$movieDbStmt = $pdo->query("
    SELECT *
    FROM media_movies
    ORDER BY id DESC
");

$movieDbList = $movieDbStmt->fetchAll(PDO::FETCH_ASSOC);

if (isset($_POST["save_hero_movies"])) {
    $selectedMovies = $_POST["hero_movies"] ?? [];

    $pdo->query("DELETE FROM hero_movies");

    $slot = 1;

    foreach ($selectedMovies as $movieId) {
        if ($slot > 3) break;

        $stmt = $pdo->prepare("
            INSERT INTO hero_movies (movie_id, slot)
            VALUES (?, ?)
        ");
        $stmt->execute([(int)$movieId, $slot]);

        $slot++;
    }

    header("Location: Dashboard.php?tab=hero");
    exit;
}

$heroStmt = $pdo->query("SELECT movie_id FROM hero_movies ORDER BY slot ASC");
$selectedHeroMovies = $heroStmt->fetchAll(PDO::FETCH_COLUMN);

if (isset($_POST["add_movie"])) {
    $title = trim($_POST["title"] ?? "");
    $tmdbId = trim($_POST["tmdb_id"] ?? "");
    $description = trim($_POST["description"] ?? "");
    $category = trim($_POST["category"] ?? "");
    $youtubeTrailerUrl = trim($_POST["youtube_trailer_url"] ?? "");
    $rating = trim($_POST["rating"] ?? "");
    $releaseYear = trim($_POST["release_year"] ?? "");
    $duration = trim($_POST["duration"] ?? "");

    $bannerPath = null;
    $logoPath = null;

    if (!empty($_FILES["banner"]["name"])) {
        $uploadDir = "../Uploads/MovieBanners/";

        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $fileName = time() . "_" . basename($_FILES["banner"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["banner"]["tmp_name"], $targetPath)) {
            $bannerPath = "Uploads/MovieBanners/" . $fileName;
        }
    }

    if (!empty($_FILES["logo"]["name"])) {
        $uploadDir = "../Uploads/MovieLogos/";

        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $fileName = time() . "_" . basename($_FILES["logo"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["logo"]["tmp_name"], $targetPath)) {
            $logoPath = "Uploads/MovieLogos/" . $fileName;
        }
    }

    $playerEmbedUrl = "";

    if (!empty($tmdbId)) {
        $playerEmbedUrl = "https://www.vidking.net/embed/movie/" . $tmdbId;
    }

    if (!empty($title)) {
        $stmt = $pdo->prepare("
            INSERT INTO media_movies 
            (title, tmdb_id, description, logo, banner, category, youtube_trailer_url, rating, release_year, duration, player_embed_url)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        ");

        $stmt->execute([
            $title,
            $tmdbId,
            $description,
            $logoPath,
            $bannerPath,
            $category,
            $youtubeTrailerUrl,
            $rating,
            $releaseYear,
            $duration,
            $playerEmbedUrl
        ]);
    }

    header("Location: Dashboard.php?tab=movies");
    exit;
}

/* TOP 10 SAVE */
if (isset($_POST["save_top10"])) {

    $positions = $_POST["top10_positions"] ?? [];

    /* RESET MOVIES */
    $pdo->query("
        UPDATE media_movies
        SET top10_position = NULL
    ");

    /* RESET TV SHOWS */
    $pdo->query("
        UPDATE media_series
        SET top10_position = NULL
    ");

    foreach ($positions as $key => $position) {

        /* SKIP EMPTY */
        if (
            $position === "" ||
            $position === null
        ) {
            continue;
        }
        
        $position = (int)$position;
        
        if ($position < 1 || $position > 10) {
            continue;
        }

        $position = (int)$position;

        /* MOVIES */
        if (strpos($key, "movie_") === 0) {

            $movieId = (int)str_replace(
                "movie_",
                "",
                $key
            );

            $stmt = $pdo->prepare("
                UPDATE media_movies
                SET top10_position = ?
                WHERE id = ?
            ");

            $stmt->execute([
                $position,
                $movieId
            ]);
        }

        /* TV SHOWS */
        if (strpos($key, "series_") === 0) {

            $seriesId = (int)str_replace(
                "series_",
                "",
                $key
            );

            $stmt = $pdo->prepare("
                UPDATE media_series
                SET top10_position = ?
                WHERE id = ?
            ");

            $stmt->execute([
                $position,
                $seriesId
            ]);
        }
    }
    header("Location: Dashboard.php?tab=top10");
    exit;
}

$stmt = $pdo->query("
    SELECT id, username, email, discord_id, avatar, provider, created_at
    FROM users
    ORDER BY id DESC
");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);

$totalUsers = count($users);
$discordUsers = count(array_filter($users, fn($u) => !empty($u["discord_id"])));
$emailUsers = count(array_filter($users, fn($u) => $u["provider"] === "email"));
$totalUsers = count($users);
$discordUsers = count(array_filter($users, fn($u) => !empty($u["discord_id"])));
$emailUsers = count(array_filter($users, fn($u) => $u["provider"] === "email"));

/* CHAT MODERATION - ADD WORD */
if (isset($_POST["add_blacklist"])) {
    $word = trim($_POST["word"] ?? "");

    if (!empty($word)) {
        $stmt = $pdo->prepare("
            INSERT IGNORE INTO stored_chat_moderation (word)
            VALUES (?)
        ");
        $stmt->execute([$word]);
    }

    header("Location: Dashboard.php?tab=chat");
    exit;
}

/* CHAT MODERATION - DELETE WORD */
if (isset($_GET["delete_word"])) {
    $deleteId = (int)$_GET["delete_word"];

    $stmt = $pdo->prepare("
        DELETE FROM stored_chat_moderation
        WHERE id = ?
    ");
    $stmt->execute([$deleteId]);

    header("Location: Dashboard.php?tab=chat");
    exit;
}

/* CHAT MODERATION - EDIT WORD */
if (isset($_POST["edit_word"])) {
    $id = (int)$_POST["word_id"];
    $newWord = trim($_POST["new_word"] ?? "");

    if (!empty($newWord)) {
        $stmt = $pdo->prepare("
            UPDATE stored_chat_moderation
            SET word = ?
            WHERE id = ?
        ");
        $stmt->execute([$newWord, $id]);
    }

    header("Location: Dashboard.php?tab=chat");
    exit;
}

$wordsPerPage = 5;
$currentWordPage = isset($_GET["word_page"]) ? max(1, (int)$_GET["word_page"]) : 1;
$wordOffset = ($currentWordPage - 1) * $wordsPerPage;

$totalWordStmt = $pdo->query("SELECT COUNT(*) FROM stored_chat_moderation");
$totalWords = (int)$totalWordStmt->fetchColumn();
$totalWordPages = max(1, ceil($totalWords / $wordsPerPage));

$stmt = $pdo->prepare("
    SELECT *
    FROM stored_chat_moderation
    ORDER BY word ASC
    LIMIT ? OFFSET ?
");
$stmt->bindValue(1, $wordsPerPage, PDO::PARAM_INT);
$stmt->bindValue(2, $wordOffset, PDO::PARAM_INT);
$stmt->execute();

$blacklistWords = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (isset($_POST["add_series"])) {
    $title = trim($_POST["title"] ?? "");
    $tmdbId = trim($_POST["tmdb_id"] ?? "");
    $description = trim($_POST["description"] ?? "");
    $category = trim($_POST["category"] ?? "");
    $youtubeTrailerUrl = trim($_POST["youtube_trailer_url"] ?? "");
    $rating = trim($_POST["rating"] ?? "");
    $releaseYear = trim($_POST["release_year"] ?? "");
    $duration = trim($_POST["duration"] ?? "");

    $bannerPath = null;
    $logoPath = null;

    if (!empty($_FILES["banner"]["name"])) {
        $uploadDir = "../Uploads/SeriesBanners/";
        if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);

        $fileName = time() . "_" . basename($_FILES["banner"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["banner"]["tmp_name"], $targetPath)) {
            $bannerPath = "Uploads/SeriesBanners/" . $fileName;
        }
    }

    if (!empty($_FILES["logo"]["name"])) {
        $uploadDir = "../Uploads/SeriesLogos/";
        if (!is_dir($uploadDir)) mkdir($uploadDir, 0755, true);

        $fileName = time() . "_" . basename($_FILES["logo"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["logo"]["tmp_name"], $targetPath)) {
            $logoPath = "Uploads/SeriesLogos/" . $fileName;
        }
    }

    if (!empty($title) && !empty($tmdbId)) {
        $stmt = $pdo->prepare("
            INSERT INTO media_series
            (title, tmdb_id, description, logo, banner, category, youtube_trailer_url, rating, release_year, duration)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        ");

        $stmt->execute([
            $title,
            $tmdbId,
            $description,
            $logoPath,
            $bannerPath,
            $category,
            $youtubeTrailerUrl,
            $rating,
            $releaseYear,
            $duration
        ]);

        $seriesId = $pdo->lastInsertId();

        if (!empty($_POST["season_number"])) {
            foreach ($_POST["season_number"] as $index => $seasonNumber) {
                $seasonNumber = (int)$seasonNumber;
                $episodeNumber = (int)($_POST["episode_number"][$index] ?? 0);
                $episodeTitle = trim($_POST["episode_title"][$index] ?? "");
                $episodeBackground = null;
        
                if (!empty($_FILES["episode_background"]["name"][$index])) {
                    $uploadDir = "../Uploads/SeriesEpisodes/";
        
                    if (!is_dir($uploadDir)) {
                        mkdir($uploadDir, 0755, true);
                    }
        
                    $fileName = time() . "_" . $index . "_" . basename($_FILES["episode_background"]["name"][$index]);
                    $targetPath = $uploadDir . $fileName;
        
                    if (move_uploaded_file($_FILES["episode_background"]["tmp_name"][$index], $targetPath)) {
                        $episodeBackground = "Uploads/SeriesEpisodes/" . $fileName;
                    }
                }
        
                if ($seasonNumber > 0 && $episodeNumber > 0) {
                    $epStmt = $pdo->prepare("
                        INSERT INTO media_series_episodes
                        (series_id, season_number, episode_number, episode_title, episode_background)
                        VALUES (?, ?, ?, ?, ?)
                    ");
        
                    $epStmt->execute([
                        $seriesId,
                        $seasonNumber,
                        $episodeNumber,
                        $episodeTitle,
                        $episodeBackground
                    ]);
                }
            }
        }
    }

    header("Location: Dashboard.php?tab=series");
    exit;
}

if (isset($_GET["delete_series"])) {
    $deleteSeriesId = (int)$_GET["delete_series"];

    $stmt = $pdo->prepare("DELETE FROM media_series_episodes WHERE series_id = ?");
    $stmt->execute([$deleteSeriesId]);

    $stmt = $pdo->prepare("DELETE FROM media_series WHERE id = ?");
    $stmt->execute([$deleteSeriesId]);

    header("Location: Dashboard.php?tab=db_series");
    exit;
}

$seriesDbStmt = $pdo->query("
    SELECT *
    FROM media_series
    ORDER BY id DESC
");

$seriesDbList = $seriesDbStmt->fetchAll(PDO::FETCH_ASSOC);

if (isset($_POST["create_badge"])) {

    $badgeName = trim($_POST["badge_name"] ?? "");
    $badgeImage = null;

    if (!empty($_FILES["badge_image"]["name"])) {

        $uploadDir = "../Uploads/Badges/";

        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $fileName = time() . "_" . basename($_FILES["badge_image"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["badge_image"]["tmp_name"], $targetPath)) {
            $badgeImage = "Uploads/Badges/" . $fileName;
        }
    }

    if (!empty($badgeName) && !empty($badgeImage)) {

        $stmt = $pdo->prepare("
            INSERT INTO user_badges
            (badge_name, badge_image)
            VALUES (?, ?)
        ");

        $stmt->execute([
            $badgeName,
            $badgeImage
        ]);
    }

    header("Location: Dashboard.php?tab=badge");
    exit;
}

if (isset($_GET["delete_badge"])) {

    $deleteBadgeId = (int)$_GET["delete_badge"];

    $stmt = $pdo->prepare("
        SELECT badge_image
        FROM user_badges
        WHERE id = ?
    ");

    $stmt->execute([$deleteBadgeId]);

    $badge = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($badge) {

        if (!empty($badge["badge_image"])) {

            $filePath = "../" . $badge["badge_image"];

            if (file_exists($filePath)) {
                unlink($filePath);
            }
        }

        $stmt = $pdo->prepare("
            DELETE FROM user_badges
            WHERE id = ?
        ");

        $stmt->execute([$deleteBadgeId]);
    }

    header("Location: Dashboard.php?tab=badge");
    exit;
}

$badgeStmt = $pdo->query("
    SELECT *
    FROM user_badges
    ORDER BY id DESC
");

$badgeList = $badgeStmt->fetchAll(PDO::FETCH_ASSOC);

if (isset($_POST["toggle_maintenance"])) {

    $maintenance = isset($_POST["maintenance_mode"]) ? 1 : 0;

    $stmt = $pdo->prepare("
        UPDATE site_settings
        SET maintenance_mode = ?
        WHERE id = 1
    ");

    $stmt->execute([$maintenance]);

    header("Location: Dashboard.php?tab=settings");
    exit;
}

$settingsStmt = $pdo->query("
    SELECT maintenance_mode
    FROM site_settings
    WHERE id = 1
");

$siteSettings = $settingsStmt->fetch(PDO::FETCH_ASSOC);

$maintenanceMode = (int)($siteSettings["maintenance_mode"] ?? 0);

if (isset($_POST["add_ppv"])) {

    $title = trim($_POST["title"] ?? "");
    $startTime = trim($_POST["start_time"] ?? "");
    $playerSlug = trim($_POST["player_slug"] ?? "");
    $description = trim($_POST["description"] ?? "");
    $category = trim($_POST["category"] ?? "");

    $bannerPath = null;
    $logoPath = null;

    /* LOGO */
    if (!empty($_FILES["logo"]["name"])) {

        $uploadDir = "../Uploads/PPVLogos/";

        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $fileName = time() . "_" . basename($_FILES["logo"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["logo"]["tmp_name"], $targetPath)) {
            $logoPath = "Uploads/PPVLogos/" . $fileName;
        }
    }

    /* BANNER */
    if (!empty($_FILES["banner"]["name"])) {

        $uploadDir = "../Uploads/PPVBanners/";

        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0755, true);
        }

        $fileName = time() . "_" . basename($_FILES["banner"]["name"]);
        $targetPath = $uploadDir . $fileName;

        if (move_uploaded_file($_FILES["banner"]["tmp_name"], $targetPath)) {
            $bannerPath = "Uploads/PPVBanners/" . $fileName;
        }
    }

    $streamUrl = "";
    
    if (!empty($playerSlug)) {
        $streamUrl = "https://pooembed.eu/embed/" . $playerSlug;
    }

    if (!empty($title)) {

        $stmt = $pdo->prepare("
            INSERT INTO media_ppv
            (title, start_time_bst, player_slug, description, logo, banner, category, stream_url)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        ");

        $stmt->execute([
            $title,
            $startTime,
            $playerSlug,
            $description,
            $logoPath,
            $bannerPath,
            $category,
            $streamUrl
        ]);
    }

    header("Location: Dashboard.php?tab=ppv");
    exit;
}

if (isset($_GET["delete_ppv"])) {

    $deletePpvId = (int)$_GET["delete_ppv"];

    $stmt = $pdo->prepare("
        DELETE FROM media_ppv
        WHERE id = ?
    ");

    $stmt->execute([$deletePpvId]);

    header("Location: Dashboard.php?tab=db_ppv");
    exit;
}

$ppvDbStmt = $pdo->query("
    SELECT *
    FROM media_ppv
    ORDER BY id DESC
");

$ppvDbList = $ppvDbStmt->fetchAll(PDO::FETCH_ASSOC);

if (isset($_POST["update_status"])) {

    $apiStatus = trim($_POST["api_status"] ?? "");
    $databaseStatus = trim($_POST["database_status"] ?? "");
    $websiteStatus = trim($_POST["website_status"] ?? "");

    $stmt = $pdo->prepare("
        UPDATE site_settings
        SET
            api_status = ?,
            database_status = ?,
            website_status = ?
        WHERE id = 1
    ");

    $stmt->execute([
        $apiStatus,
        $databaseStatus,
        $websiteStatus
    ]);

    header("Location: Dashboard.php?tab=settings");
    exit;
}

if (isset($_POST["create_maintenance"])) {

    $title = trim($_POST["title"] ?? "");
    $maintenanceStatus = trim($_POST["maintenance_status"] ?? "");
    $startsAt = trim($_POST["starts_at"] ?? "");
    $maintenanceNote = trim($_POST["maintenance_note"] ?? "");

    if (!empty($title)) {

        $stmt = $pdo->prepare("
            INSERT INTO maintenance_events
            (title, maintenance_status, starts_at, maintenance_note)
            VALUES (?, ?, ?, ?)
        ");

        $stmt->execute([
            $title,
            $maintenanceStatus,
            $startsAt,
            $maintenanceNote
        ]);
    }

    header("Location: Dashboard.php?tab=settings");
    exit;
}

if (isset($_GET["delete_maintenance"])) {

    $deleteMaintenanceId = (int)$_GET["delete_maintenance"];

    $stmt = $pdo->prepare("
        DELETE FROM maintenance_events
        WHERE id = ?
    ");

    $stmt->execute([$deleteMaintenanceId]);

    header("Location: Dashboard.php?tab=settings");
    exit;
}

try {

    $maintenanceStmt = $pdo->query("
        SELECT *
        FROM maintenance_events
        ORDER BY id DESC
    ");

    $maintenanceEvents = $maintenanceStmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {

    $maintenanceEvents = [];

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Administrator Panel - Rewire</title>
<link rel="icon" type="image/png" href="../Assets/Logo.png">
<script src="https://unpkg.com/lucide@latest"></script>

<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, sans-serif;
}

body {
    background: #050505;
    color: white;
}

.admin-layout {
    display: flex;
    min-height: 100vh;
}

/* SIDEBAR */
.sidebar {
    width: 280px;
    background: #0b0f12;
    border-right: 1px solid rgba(255,255,255,0.07);
    padding: 22px 14px;
    transition: 0.3s ease;
}

.admin-layout.sidebar-hidden .sidebar {
    width: 0;
    padding: 0;
    overflow: hidden;
    border-right: none;
}

.logo {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px 28px;
}

.logo img {
    height: 86px;
    max-width: 180px;
    object-fit: contain;
}

.sidebar-title {
    color: #7d8894;
    font-size: 0.72rem;
    font-weight: 800;
    letter-spacing: 1px;
    margin: 18px 14px 10px;
}

.side-link {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px 16px;
    color: #c8d0d8;
    border-radius: 8px;
    margin-bottom: 6px;
    transition: 0.25s ease;
    font-weight: 700;
    background: transparent;
    border: none;
    cursor: pointer;
    text-align: left;
    font-size: 1rem;
}

.side-link:hover,
.side-link.active {
    background: rgba(0,194,255,0.10);
    color: white;
}

.side-link i {
    width: 21px;
    height: 21px;
    transition: 0.25s ease;
}

.side-link:hover i,
.side-link.active i {
    color: #00c2ff;
    filter: drop-shadow(0 0 6px rgba(0,194,255,0.6));
}

/* MAIN */
.main {
    flex: 1;
    background: #050505;
    transition: 0.3s ease;
}

/* TOP BAR */
.topbar {
    height: 74px;
    background: #0b0f12;
    border-bottom: 1px solid rgba(255,255,255,0.07);
    display: flex;
    align-items: center;
    padding: 0 28px;
    gap: 25px;
}

/* HAMBURGER */
.menu-btn {
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.hamburger {
    width: 26px;
    height: 18px;
    position: relative;
    display: inline-block;
}

.hamburger span {
    position: absolute;
    width: 100%;
    height: 2px;
    background: #c8d0d8;
    border-radius: 2px;
    transition: all 0.35s ease;
}

.hamburger span:nth-child(1) { top: 0; }
.hamburger span:nth-child(2) { top: 8px; }
.hamburger span:nth-child(3) { bottom: 0; }

.menu-btn.active .hamburger span:nth-child(1) {
    transform: rotate(45deg);
    top: 8px;
}

.menu-btn.active .hamburger span:nth-child(2) {
    opacity: 0;
    transform: scale(0);
}

.menu-btn.active .hamburger span:nth-child(3) {
    transform: rotate(-45deg);
    bottom: 8px;
}

/* SEARCH */
.search {
    flex: 1;
    max-width: 900px;
    height: 42px;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 22px;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 18px;
    color: #8f9aa5;
    background: rgba(255,255,255,0.02);
}

.search input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: white;
    font-size: 0.95rem;
}

/* AVATAR DROPDOWN */
.profile-dropdown {
    position: relative;
    cursor: pointer;
    margin-left: auto;
}

.profile-avatar {
    width: 40px;
    height: 40px;
    border-radius: 11px;
    object-fit: cover;
    border: 1px solid rgba(255,255,255,0.25);
    transition: 0.25s ease;
}

.profile-avatar:hover {
    transform: scale(1.08);
    border-color: #00c2ff;
}

.profile-menu {
    position: absolute;
    top: 52px;
    right: 0;
    width: 180px;
    background: rgba(8,10,13,0.97);
    border: 1px solid rgba(255,255,255,0.14);
    border-radius: 12px;
    padding: 10px;
    display: none;
    box-shadow: 0 12px 35px rgba(0,0,0,0.55);
    z-index: 99;
}

.profile-menu.active {
    display: block;
}

.profile-menu-name {
    color: white;
    font-size: 0.85rem;
    font-weight: bold;
    padding: 8px 10px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    margin-bottom: 8px;
}

.profile-menu a {
    display: block;
    padding: 9px 10px;
    border-radius: 8px;
    color: #00c2ff;
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: bold;
}

.profile-menu a:hover {
    background: rgba(0,194,255,0.12);
}

/* CONTENT */
.content {
    padding: 34px 28px;
}

.page-title {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 30px;
}

.page-title h1 {
    font-size: 1.6rem;
}

.blank-panel {
    min-height: 620px;
    background: #0b0f12;
    border-radius: 18px;
    border: 1px solid rgba(255,255,255,0.06);
    padding: 35px;
    color: #c8d0d8;
}

.panel-title {
    font-size: 1.8rem;
    margin-bottom: 10px;
}

.panel-text {
    color: #77818c;
}

/* USER PANEL */
.user-panel {
    display: none;
}

.user-panel.active {
    display: block;
}

.user-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 18px;
    margin-bottom: 20px;
}

.stat-card {
    background: linear-gradient(
        135deg,
        rgba(0,194,255,0.08),
        rgba(255,255,255,0.02)
    ), #050505;
    border: 1px solid rgba(0,194,255,0.15);
    border-radius: 14px;
    padding: 22px;
    box-shadow: 0 0 20px rgba(0,194,255,0.08);
}

.stat-card h3 {
    color: #7f8a96;
    font-size: 0.75rem;
    letter-spacing: 1px;
    margin-bottom: 12px;
}

.stat-card strong {
    font-size: 2.2rem;
    color: white;
}

.users-table {
    background: #071014;
    border: 1px solid rgba(0,194,255,0.12);
    border-radius: 14px;
    overflow: hidden;
}

.users-table-header,
.user-row {
    display: grid;
    grid-template-columns: 2fr 2fr 1fr 1.2fr;
    align-items: center;
    padding: 16px 18px;
}

.users-table-header {
    background: #071014;
    color: #7f8a96;
    font-size: 0.75rem;
    font-weight: 800;
    letter-spacing: 1px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}

.user-row {
    background: #071014;
    min-height: 74px;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    transition: 0.25s ease;
}

.user-row:hover {
    background: #0a171d;
}

.user-cell {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-avatar {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    object-fit: cover;
    background: #111;
    border: 1px solid rgba(0,194,255,0.25);
    box-shadow: 0 0 10px rgba(0,194,255,0.2);
}

.user-name {
    font-weight: 800;
    color: white;
}

.user-sub {
    color: #6f7b87;
    font-size: 0.75rem;
    margin-top: 4px;
}

.status-pill {
    width: fit-content;
    background: rgba(0,194,255,0.15);
    color: #00c2ff;
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 800;
    box-shadow: 0 0 10px rgba(0,194,255,0.3);
}

.action-btn {
    background: linear-gradient(
        135deg,
        rgba(0,194,255,0.15),
        rgba(255,255,255,0.05)
    );
    border: 1px solid rgba(0,194,255,0.35);
    color: white;
    border-radius: 10px;
    padding: 9px 15px;
    font-weight: 800;
    cursor: pointer;
    transition: 0.25s ease;
}

.action-btn:hover {
    background: rgba(0,194,255,0.25);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0,194,255,0.35);
}

.action-btn {
    display: inline-block;
    text-decoration: none;
}

.chat-panel{
    width:100%;
}

.chat-header{
    display:flex;
    align-items:center;
    justify-content:space-between;
    margin-bottom:24px;
    gap:20px;
}

.chat-header h2{
    font-size:1.7rem;
    font-weight:800;
    color:white;
}

.blacklist-form{
    display:flex;
    gap:12px;
}

.blacklist-form input{
    width:280px;
    height:48px;
    background:#071014;
    border:1px solid rgba(0,194,255,0.15);
    border-radius:12px;
    padding:0 16px;
    color:white;
    outline:none;
    transition:0.25s ease;
}

.blacklist-form input:focus{
    border-color:#00c2ff;
    box-shadow:0 0 16px rgba(0,194,255,0.2);
}

.blacklist-form button{
    height:48px;
    padding:0 20px;
    border:none;
    border-radius:12px;
    background:linear-gradient(135deg,#00c2ff,#0090c7);
    color:white;
    font-weight:800;
    cursor:pointer;
    transition:0.25s ease;
}

.blacklist-form button:hover{
    transform:translateY(-2px);
    box-shadow:0 10px 24px rgba(0,194,255,0.25);
}

.blacklist-table{
    background: #071014;
    border: 1px solid rgba(0,194,255,0.12);
    border-radius: 14px;
    overflow: hidden;
}

.blacklist-header{
    display:grid;
    grid-template-columns:1fr 220px;
    padding:18px 24px;
    background:#071014;
    color:#7f8a96;
    font-size:0.76rem;
    font-weight:800;
    letter-spacing:1px;
    border-bottom:1px solid rgba(255,255,255,0.08);
}

.blacklist-row{
    background:#071014;
    border-bottom:1px solid rgba(255,255,255,0.05);
    padding:18px 24px;
    transition:0.25s ease;
}

.blacklist-row:hover{
    background:#0a171d;
}

.edit-word-form{
    display:grid;
    grid-template-columns:1fr 220px;
    gap:18px;
    align-items:center;
}

.word-input{
    width:100%;
    height:52px;
    background:#050505;
    border:1px solid rgba(0,194,255,0.12);
    border-radius:12px;
    padding:0 16px;
    color:white;
    font-size:0.95rem;
    outline:none;
}

.word-input:focus{
    border-color:#00c2ff;
    box-shadow:0 0 16px rgba(0,194,255,0.16);
}

.blacklist-actions{
    display:flex;
    gap:12px;
}

.save-word-btn,
.delete-word-btn{
    flex:1;
    height:50px;
    border:none;
    border-radius:12px;
    font-weight:800;
    text-decoration:none;
    display:flex;
    align-items:center;
    justify-content:center;
    transition:0.25s ease;
}

.save-word-btn{
    background:linear-gradient(135deg,#00c2ff,#0090c7);
    color:white;
    cursor:pointer;
}

.save-word-btn:hover{
    transform:translateY(-2px);
    box-shadow:0 10px 20px rgba(0,194,255,0.22);
}

.delete-word-btn{
    background:rgba(255,59,59,0.14);
    border:1px solid rgba(255,59,59,0.25);
    color:#ff5c5c;
}

.delete-word-btn:hover{
    background:rgba(255,59,59,0.2);
}

.chat-panel {
    display: none;
    width: 100%;
}

.chat-panel.active {
    display: block;
}

.pagination {
    display: flex;
    gap: 8px;
    margin-top: 18px;
    justify-content: flex-end;
}

.pagination a {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: #071014;
    border: 1px solid rgba(0,194,255,0.15);
    color: #c8d0d8;
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    font-weight: 800;
    transition: 0.25s ease;
}

.pagination a:hover,
.pagination a.active {
    background: rgba(0,194,255,0.18);
    color: white;
    border-color: #00c2ff;
    box-shadow: 0 0 14px rgba(0,194,255,0.22);
}

.movie-creator-panel {
    display: none;
}

.movie-creator-panel.active {
    display: block;
}

.media-form {
    width: 100%;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 18px;
    margin-bottom: 25px;
}

.form-group.full {
    grid-column: span 2;
}

.form-group label {
    display: block;
    color: #8f9aa5;
    font-size: 0.8rem;
    font-weight: 800;
    letter-spacing: 1px;
    margin-bottom: 9px;
}

.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    background: #071014;
    border: 1px solid rgba(0,194,255,0.15);
    border-radius: 12px;
    color: white;
    padding: 14px 16px;
    outline: none;
}

.form-group textarea {
    min-height: 130px;
    resize: vertical;
}

.cast-box {
    background: #071014;
    border: 1px solid rgba(0,194,255,0.12);
    border-radius: 16px;
    padding: 20px;
    margin-bottom: 24px;
}

.cast-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 18px;
}

.cast-header button,
.submit-media-btn {
    background: linear-gradient(135deg, #00c2ff, #0090c7);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 12px 18px;
    font-weight: 800;
    cursor: pointer;
}

.cast-row {
    display: grid;
    grid-template-columns: 1fr 1fr 110px;
    gap: 12px;
    margin-bottom: 12px;
}

.cast-row input {
    background: #050505;
    border: 1px solid rgba(0,194,255,0.12);
    border-radius: 12px;
    color: white;
    padding: 13px 15px;
}

.cast-row button {
    background: rgba(255,59,59,0.14);
    color: #ff5c5c;
    border: 1px solid rgba(255,59,59,0.25);
    border-radius: 12px;
    font-weight: 800;
    cursor: pointer;
}

/* FILE INPUT */

.form-group input[type="file"] {
    background: #071014;
    color: #c8d0d8;
    padding: 12px;
    cursor: pointer;
}

.form-group input[type="file"]::file-selector-button {
    background: linear-gradient(135deg,#00c2ff,#0090c7);
    border: none;
    color: white;
    padding: 10px 16px;
    border-radius: 10px;
    margin-right: 14px;
    font-weight: 800;
    cursor: pointer;
    transition: 0.25s ease;
}

.form-group input[type="file"]::file-selector-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 16px rgba(0,194,255,0.25);
}

/* SELECT DROPDOWN */

.form-group select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;

    background:
        #071014
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' fill='white' viewBox='0 0 24 24'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E")
        no-repeat right 16px center;

    padding-right: 50px;
    color: white;
    cursor: pointer;
}

.form-group select option {
    background: #071014;
    color: white;
}

.movie-db-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.movie-edit-btn,
.movie-delete-btn {
    width: 92px;
    height: 44px;
    border-radius: 10px;
    font-weight: 900;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 0.25s ease;
}

.movie-edit-btn {
    background: rgba(0,194,255,0.12);
    border: 1px solid rgba(0,194,255,0.45);
    color: white;
}

.movie-edit-btn:hover {
    background: rgba(0,194,255,0.24);
    box-shadow: 0 0 18px rgba(0,194,255,0.25);
}

.movie-delete-btn {
    background: rgba(255,59,59,0.16);
    border: 1px solid rgba(255,59,59,0.35);
    color: #ff6b6b;
}

.movie-delete-btn:hover {
    background: rgba(255,59,59,0.25);
    box-shadow: 0 0 18px rgba(255,59,59,0.18);
}

.movie-db-panel {
    display: none;
    width: 100%;
}

.movie-db-panel.active {
    display: block;
}

.movie-db-panel {
    display: none;
}

.hero-panel {
    display: none;
    width: 100%;
}

.hero-panel.active {
    display: block;
}

.hero-checkbox {
    width: 24px;
    height: 24px;
    accent-color: #00c2ff;
    cursor: pointer;
}

.series-creator-panel {
    display: none;
}

.series-creator-panel.active {
    display: block;
}

/* EPISODES BOX */
.episodes-box {
    margin-top: 25px;
    padding: 24px;
    border: 1px solid rgba(0,194,255,0.18);
    border-radius: 18px;
    background: rgba(5,12,15,0.82);
}

.episodes-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.episodes-header h3 {
    font-size: 1.15rem;
    font-weight: 800;
    color: white;
}

.episodes-header button {
    height: 46px;
    padding: 0 18px;

    border: none;
    border-radius: 12px;

    background: linear-gradient(135deg,#00c2ff,#0090c7);
    color: white;

    font-weight: 800;
    cursor: pointer;

    transition: 0.25s ease;
}

.episodes-header button:hover {
    transform: translateY(-1px);
    box-shadow: 0 8px 18px rgba(0,194,255,0.25);
}

/* ROW */
.episode-row {
    display: grid;
    grid-template-columns: 100px 100px 1fr 260px 120px;
    gap: 14px;
    align-items: center;
    margin-bottom: 14px;
}

/* INPUTS */
.episode-row input {
    width: 100%;
    height: 48px;

    background: #081117;
    border: 1px solid rgba(0,194,255,0.16);
    border-radius: 12px;

    padding: 0 14px;

    color: white;
    font-size: 0.92rem;

    outline: none;
    transition: 0.25s ease;
}

.episode-row input:focus {
    border-color: #00c2ff;
    box-shadow: 0 0 14px rgba(0,194,255,0.16);
}

/* FILE INPUT */
.episode-row input[type="file"] {
    padding: 7px 10px;
    overflow: hidden;

    color: #c8d0d8;
    font-size: 0.82rem;
}

.episode-row input[type="file"]::file-selector-button {
    height: 32px;

    border: none;
    border-radius: 8px;

    padding: 0 14px;
    margin-right: 12px;

    background: linear-gradient(135deg,#00c2ff,#0090c7);
    color: white;

    font-size: 0.8rem;
    font-weight: 800;

    cursor: pointer;
    transition: 0.25s ease;
}

.episode-row input[type="file"]::file-selector-button:hover {
    filter: brightness(1.08);
}

/* DELETE BUTTON */
.episode-row button {
    width: 100%;
    height: 48px;

    border-radius: 12px;
    border: 1px solid rgba(255,80,80,0.24);

    background: rgba(255,60,60,0.10);
    color: #ff6b6b;

    font-weight: 800;
    font-size: 0.9rem;

    cursor: pointer;
    transition: 0.25s ease;
}

.episode-row button:hover {
    background: rgba(255,60,60,0.18);
    border-color: rgba(255,80,80,0.40);

    transform: translateY(-1px);
}

.submit-media-btn {
    margin-top: 18px;
}

.badge-panel{
    display:none;
    width:100%;
}

.badge-panel.active{
    display:block;
}

/* BADGE SYSTEM */

.badge-top-section{
    margin-bottom:30px;
}

.badge-form{
    background:#071014;
    border:1px solid rgba(0,194,255,0.12);
    border-radius:18px;
    padding:24px;
}

.badge-list-wrapper{
    margin-top:10px;
}

.badge-list-header{
    display:flex;
    align-items:center;
    justify-content:space-between;
    margin-bottom:22px;
}

.badge-list-header h2{
    font-size:1.3rem;
    color:white;
    font-weight:800;
}

.badge-list-header span{
    color:#7f8a96;
    font-size:0.9rem;
    font-weight:700;
}

.badge-grid{
    display:grid;
    grid-template-columns:repeat(auto-fill,minmax(240px,1fr));
    gap:22px;
}

.badge-card{
    background:linear-gradient(
        180deg,
        rgba(10,20,28,0.96),
        rgba(5,10,14,0.98)
    );

    border:1px solid rgba(0,194,255,0.12);
    border-radius:18px;

    overflow:hidden;

    transition:0.25s ease;

    position:relative;
}

.badge-card:hover{
    transform:translateY(-4px);

    border-color:rgba(0,194,255,0.35);

    box-shadow:
        0 10px 30px rgba(0,194,255,0.14),
        0 0 18px rgba(0,194,255,0.10);
}

.badge-image-wrap{
    height:170px;

    background:
        radial-gradient(circle at top, rgba(0,194,255,0.18), transparent),
        #081117;

    display:flex;
    align-items:center;
    justify-content:center;

    padding:20px;
}

.badge-image{
    max-width:100%;
    max-height:120px;
    object-fit:contain;

    filter:drop-shadow(0 0 18px rgba(0,194,255,0.18));
}

.badge-info{
    padding:18px 18px 10px;
}

.badge-name{
    font-size:1rem;
    font-weight:800;
    color:white;
    margin-bottom:8px;
}

.badge-id{
    color:#74808c;
    font-size:0.8rem;
}

.badge-actions{
    padding:0 18px 18px;
}

.badge-delete-btn{
    width:100%;
    height:46px;

    display:flex;
    align-items:center;
    justify-content:center;

    text-decoration:none;

    border-radius:12px;

    background:rgba(255,59,59,0.10);
    border:1px solid rgba(255,59,59,0.25);

    color:#ff6b6b;

    font-weight:800;

    transition:0.25s ease;
}

.badge-delete-btn:hover{
    background:rgba(255,59,59,0.18);

    border-color:rgba(255,59,59,0.45);

    box-shadow:0 8px 20px rgba(255,59,59,0.12);
}

.empty-badge-state{
    grid-column:1/-1;

    height:220px;

    border-radius:18px;

    border:1px dashed rgba(255,255,255,0.08);

    display:flex;
    align-items:center;
    justify-content:center;

    color:#70808d;

    background:#071014;
}

.settings-panel{
    display:none;
    width:100%;
}

.settings-panel.active{
    display:block;
}

.settings-panel{
    display:none;
    width:100%;
}

.settings-panel.active{
    display:block;
}

.settings-card{
    background:#071014;
    border:1px solid rgba(0,194,255,0.12);
    border-radius:18px;
    padding:24px;
}

.settings-row{
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:20px;
}

.settings-name{
    font-size:1rem;
    font-weight:800;
    color:white;
    margin-bottom:6px;
}

.settings-desc{
    color:#7f8a96;
    font-size:0.9rem;
}

/* SWITCH */

.switch{
    position:relative;
    width:70px;
    height:38px;
    display:inline-block;
}

.switch input{
    opacity:0;
    width:0;
    height:0;
}

.slider{
    position:absolute;
    inset:0;
    background:#1b242b;
    border:1px solid rgba(255,255,255,0.08);
    border-radius:999px;
    cursor:pointer;
    transition:0.25s ease;
}

.slider::before{
    content:"";
    position:absolute;
    width:30px;
    height:30px;
    left:4px;
    top:3px;
    background:white;
    border-radius:50%;
    transition:0.25s ease;
}

.switch input:checked + .slider{
    background:#00c2ff;
    box-shadow:0 0 18px rgba(0,194,255,0.35);
}

.switch input:checked + .slider::before{
    transform:translateX(31px);
}

.settings-maintenance-table .user-row,
.settings-maintenance-table .users-table-header {
    grid-template-columns: 2fr 1fr 1.5fr 140px;
}

.top10-panel{
    display:none;
    width:100%;
}

.top10-panel.active{
    display:block;
}

.top10-select{
    width:120px;
    height:42px;

    background:#071014;
    border:1px solid rgba(0,194,255,0.18);
    border-radius:10px;

    color:white;
    font-weight:700;

    padding:0 12px;

    outline:none;

    transition:0.25s ease;
}

.top10-select:focus{
    border-color:#00c2ff;
    box-shadow:0 0 12px rgba(0,194,255,0.25);
}

.top10-select option{
    background:#071014;
    color:white;
}
</style>
</head>

<body>
<?php include "../Preloader.php"; ?>

<div class="admin-layout">

    <aside class="sidebar">
        <div class="logo">
            <img src="../Assets/Logo.png" alt="Rewire">
        </div>

        <button class="side-link active" onclick="changeTab('Dashboard', this)">
            <i data-lucide="layout-dashboard"></i>
            Dashboard
        </button>
        
        <div class="sidebar-title">MANAGEMENT</div>
        
        <button class="side-link" onclick="changeTab('Users', this)">
            <i data-lucide="users"></i>
            Users
        </button>
        
        <button class="side-link" onclick="changeTab('Chat Moderation', this)">
            <i data-lucide="shield-alert"></i>
            Chat Moderation
        </button>
        
        <button class="side-link" onclick="changeTab('Settings', this)">
            <i data-lucide="settings"></i>
            Settings
        </button>        
        
        <div class="sidebar-title">MEDIA CREATOR</div>
        
        <button class="side-link" onclick="changeTab('Movies', this)">
            <i data-lucide="film"></i>
            Movies
        </button>
        
        <button class="side-link" onclick="changeTab('TV Shows', this)">
            <i data-lucide="tv"></i>
            TV Shows
        </button>
        
        <button class="side-link" onclick="changeTab('PPV', this)">
            <i data-lucide="ticket"></i>
            PPV
        </button>
        
        <button class="side-link" onclick="changeTab('Hero', this)">
            <i data-lucide="image"></i>
            Hero
        </button>
        
        <button class="side-link" onclick="changeTab('Badge Creator', this)">
            <i data-lucide="shield"></i>
            Badge
        </button>
        
        <div class="sidebar-title">DATABASE</div>
        <button class="side-link" onclick="changeTab('DB Movies', this)">
            <i data-lucide="film"></i>
            Movies
        </button>
        
        <button class="side-link" onclick="changeTab('DB TV Shows', this)">
            <i data-lucide="tv"></i>
            TV Shows
        </button>
        
        <button class="side-link" onclick="changeTab('DB PPV', this)">
            <i data-lucide="ticket"></i>
            PPV
        </button>
        
        <button class="side-link" onclick="changeTab('Top 10', this)">
            <i data-lucide="trending-up"></i>
            Top 10
        </button>        
    </aside>

    <main class="main">

        <div class="topbar">
            <button class="menu-btn" onclick="toggleSidebar(this)">
                <span class="hamburger">
                    <span></span>
                    <span></span>
                    <span></span>
                </span>
            </button>

            <div class="search">
                <i data-lucide="search"></i>
                <input type="text" placeholder="Search">
            </div>

            <div class="profile-dropdown" onclick="toggleProfileMenu()">
                <img src="<?php echo htmlspecialchars($avatar); ?>" class="profile-avatar">

                <div class="profile-menu" id="profileMenu">
                    <div class="profile-menu-name">
                        <?php echo htmlspecialchars($username); ?>
                    </div>

                    <a href="../Index.php">Back To Site</a>
                    <a href="../Auth/logout.php">Logout</a>
                </div>
            </div>
        </div>

        <div class="content">
            <div class="page-title">
                <h1 id="pageTitle">Dashboard</h1>
                <span>› Admin Interface</span>
            </div>
            <div class="blank-panel">
                <div id="defaultPanel">
                    <div class="panel-title" id="panelTitle">Dashboard</div>
                    <div class="panel-text" id="panelText">Select a section from the sidebar.</div>
                </div>
            
                <div class="chat-panel" id="chatPanel">
                    <div class="chat-header">
                        <h2>Chat Moderation</h2>
                        <form method="POST" class="blacklist-form">
                            <input type="text" name="word" placeholder="Enter blacklisted word..." required>
                            <button type="submit" name="add_blacklist">Add Word</button>
                        </form>
                    </div>
            
                    <div class="blacklist-table">
                        <div class="blacklist-header">
                            <div>BLACKLISTED WORD</div>
                            <div>ACTIONS</div>
                        </div>
            
                        <?php foreach ($blacklistWords as $row): ?>
                            <div class="blacklist-row">
                                <form method="POST" class="edit-word-form">
                                    <input type="hidden" name="word_id" value="<?php echo $row["id"]; ?>">
                                    <input type="text" name="new_word" value="<?php echo htmlspecialchars($row["word"]); ?>" class="word-input">
                                    <div class="blacklist-actions">
                                        <button type="submit" name="edit_word" class="save-word-btn">Save</button>
                                        <a href="Dashboard.php?tab=chat&word_page=<?php echo $currentWordPage; ?>&delete_word=<?php echo $row["id"]; ?>" class="delete-word-btn">Delete</a>
                                    </div>
                                </form>
                            </div>
                        <?php endforeach; ?>
                    </div>
            
                    <?php if ($totalWordPages > 1): ?>
                        <div class="pagination">
                            <?php for ($i = 1; $i <= $totalWordPages; $i++): ?>
                                <a href="Dashboard.php?tab=chat&word_page=<?php echo $i; ?>" class="<?php echo $i === $currentWordPage ? 'active' : ''; ?>"><?php echo $i; ?></a>
                            <?php endfor; ?>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="settings-panel" id="settingsPanel">
                    <div class="panel-title">
                        Settings
                    </div>
                
                    <div class="panel-text" style="margin-bottom:25px;">
                        Configure your platform settings here.
                    </div>
                
                    <form method="POST" class="settings-card">
                
                        <div class="settings-row">
                            <div>
                                <div class="settings-name">
                                    Maintenance Mode
                                </div>
                
                                <div class="settings-desc">
                                    Disable Access To The Entire Website, For Maintance Use Only!
                                </div>
                            </div>
                
                            <label class="switch">
                                <input
                                    type="checkbox"
                                    name="maintenance_mode"
                                    onchange="this.form.submit()"
                                    <?php echo $maintenanceMode ? "checked" : ""; ?>
                                >
                
                                <span class="slider"></span>
                            </label>
                        </div>
                    <input type="hidden" name="toggle_maintenance">
                    </form>
                    
                    <div class="settings-card" style="margin-top:24px;">
                    
                        <div class="settings-name" style="margin-bottom:20px;">
                            Status Page Manager
                        </div>
                    
                        <form method="POST">
                    
                            <div class="form-grid">
                    
                                <div class="form-group">
                                    <label>API Status</label>
                    
                                    <select name="api_status">
                                        <option>Fully Operational</option>
                                        <option>Partially Operational</option>
                                        <option>Full Outage</option>
                                    </select>
                                </div>
                    
                                <div class="form-group">
                                    <label>Database Status</label>
                    
                                    <select name="database_status">
                                        <option>Fully Operational</option>
                                        <option>Partially Operational</option>
                                        <option>Full Outage</option>
                                    </select>
                                </div>
                    
                                <div class="form-group">
                                    <label>Website Status</label>
                    
                                    <select name="website_status">
                                        <option>Fully Operational</option>
                                        <option>Partially Operational</option>
                                        <option>Full Outage</option>
                                    </select>
                                </div>
                    
                                <div class="form-group">
                                    <label>Maintenance Title</label>
                    
                                    <input
                                        type="text"
                                        name="title"
                                        placeholder="Scheduled Backend Upgrade"
                                    >
                                </div>
                    
                                <div class="form-group">
                                    <label>Maintenance Status</label>
                    
                                    <select name="maintenance_status">
                                        <option>Scheduled</option>
                                        <option>In Progress</option>
                                        <option>Completed</option>
                                    </select>
                                </div>
                    
                                <div class="form-group">
                                    <label>Maintenance Start Time</label>
                                    <input
                                        type="datetime-local"
                                        name="starts_at"
                                    >
                                </div>
                    
                                <div class="form-group full">
                                    <label>Maintenance Note</label>
                    
                                    <textarea
                                        name="maintenance_note"
                                        placeholder="Servers may briefly restart during deployment..."
                                    ></textarea>
                                </div>
                            </div>
                            <div style="display:flex;gap:16px;flex-wrap:wrap;">
                    
                                <button
                                    type="submit"
                                    name="update_status"
                                    class="submit-media-btn"
                                >
                                    Update System Status
                                </button>
                    
                                <button
                                    type="submit"
                                    name="create_maintenance"
                                    class="submit-media-btn"
                                >
                                    Create Maintenance Event
                                </button>
                                <a
                                    href="../Status.php"
                                    target="_blank"
                                    class="submit-media-btn"
                                    style="
                                        text-decoration:none;
                                        display:flex;
                                        align-items:center;
                                        justify-content:center;
                                    "
                                >
                                    Open Status Page
                                </a>
                            </div>
                        </form>
                    </div>
                    <div class="settings-card" style="margin-top:24px;">
                        <div class="settings-name" style="margin-bottom:20px;">
                            Maintenance Events
                        </div>
                        <?php if (empty($maintenanceEvents)): ?>
                    
                            <div class="panel-text">
                                No Maintenance Events Created.
                            </div>
                        <?php else: ?>
                            <div class="users-table settings-maintenance-table">
                                <div class="users-table-header">
                                    <div>TITLE</div>
                                    <div>STATUS</div>
                                    <div>START TIME</div>
                                    <div>ACTIONS</div>
                                </div>
                                
                                <?php if (!empty($maintenanceEvents)): ?>
                                    <?php foreach ($maintenanceEvents as $event): ?>
                                        <div class="user-row">
                                            <div>
                                                <?php echo htmlspecialchars($event["title"] ?? "No Title"); ?>
                                            </div>
                        
                                            <div>
                                                <span class="status-pill">
                                                    <?php echo htmlspecialchars($event["maintenance_status"] ?? "Unknown"); ?>
                                                </span>
                                            </div>
                        
                                            <div>
                                                <?php
                                                echo !empty($event["starts_at"] ?? null)
                                                    ? date("M j, Y g:i A", strtotime($event["starts_at"]))
                                                    : "No Date";
                                                ?>
                                            </div>
                        
                                            <div class="movie-db-actions">
                                                <a
                                                    class="movie-delete-btn"
                                                    href="Dashboard.php?tab=settings&delete_maintenance=<?php echo $event["id"]; ?>"
                                                    onclick="return confirm('Delete this maintenance event?');"
                                                >
                                                    Delete
                                                </a>
                                            </div>
                                        </div>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>                    
                </div>
            
                <div class="user-panel" id="usersPanel">
                    <div class="user-stats">
                        <div class="stat-card">
                            <h3>TOTAL USERS</h3>
                            <strong><?php echo $totalUsers; ?></strong>
                        </div>
                        <div class="stat-card">
                            <h3>DISCORD USERS</h3>
                            <strong><?php echo $discordUsers; ?></strong>
                        </div>
                        <div class="stat-card">
                            <h3>EMAIL USERS</h3>
                            <strong><?php echo $emailUsers; ?></strong>
                        </div>
                    </div>
            
                    <div class="users-table">
                        <div class="users-table-header">
                            <div>USER</div>
                            <div>DISCORD ID</div>
                            <div>STATUS</div>
                            <div>ACTIONS</div>
                        </div>
            
                        <?php foreach ($users as $u): ?>
                            <?php
                                $userAvatar = "../Assets/Logo.png";
                                if (!empty($u["avatar"]) && !empty($u["discord_id"])) {
                                    $userAvatar = "https://cdn.discordapp.com/avatars/" . $u["discord_id"] . "/" . $u["avatar"] . ".png";
                                }
                            ?>
                            <div class="user-row">
                                <div class="user-cell">
                                    <img src="<?php echo htmlspecialchars($userAvatar); ?>" class="user-avatar">
                                    <div>
                                        <div class="user-name"><?php echo htmlspecialchars($u["username"]); ?></div>
                                        <div class="user-sub"><?php echo htmlspecialchars($u["email"] ?? "No email"); ?></div>
                                    </div>
                                </div>
                                <div><?php echo htmlspecialchars($u["discord_id"] ?? "Email User"); ?></div>
                                <div><span class="status-pill">Active</span></div>
                                <div><a class="action-btn" href="UserProfile.php?id=<?php echo $u["id"]; ?>">View Profile</a></div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            
                <div class="movie-db-panel" id="movieDbPanel">
                    <div class="users-table">
                        <div class="users-table-header">
                            <div>MOVIE</div>
                            <div>TMDB ID</div>
                            <div>CATEGORY</div>
                            <div>ACTIONS</div>
                        </div>
            
                        <?php foreach ($movieDbList as $movie): ?>
                            <div class="user-row">
                                <div class="user-cell">
                                    <?php
                                        $movieThumb = !empty($movie["banner"]) ? "../" . $movie["banner"] : (!empty($movie["logo"]) ? "../" . $movie["logo"] : "../Assets/Logo.png");
                                    ?>
                                    <img src="<?php echo htmlspecialchars($movieThumb); ?>" class="user-avatar">
                                    <div>
                                        <div class="user-name"><?php echo htmlspecialchars($movie["title"]); ?></div>
                                        <div class="user-sub">
                                            <?php echo htmlspecialchars($movie["release_year"] ?? "Unknown"); ?> · ⭐ <?php echo htmlspecialchars($movie["rating"] ?? "N/A"); ?>
                                        </div>
                                    </div>
                                </div>
                                <div><?php echo htmlspecialchars($movie["tmdb_id"] ?? "No TMDB ID"); ?></div>
                                <div><span class="status-pill"><?php echo htmlspecialchars($movie["category"] ?? "None"); ?></span></div>
                                <div class="movie-db-actions">
                                    <a class="movie-edit-btn" href="EditMovie.php?id=<?php echo $movie["id"]; ?>">Edit</a>
                                    <a class="movie-delete-btn" href="Dashboard.php?tab=db_movies&delete_movie=<?php echo $movie["id"]; ?>" onclick="return confirm('Delete this movie?');">Delete</a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                
                <div class="movie-db-panel" id="seriesDbPanel">
                    <div class="users-table">
                        <div class="users-table-header">
                            <div>TV SHOW</div>
                            <div>TMDB ID</div>
                            <div>CATEGORY</div>
                            <div>ACTIONS</div>
                        </div>
                
                        <?php foreach ($seriesDbList as $show): ?>
                            <div class="user-row">
                                <div class="user-cell">
                                    <?php
                                        $showThumb = !empty($show["banner"])
                                            ? "../" . $show["banner"]
                                            : (!empty($show["logo"])
                                                ? "../" . $show["logo"]
                                                : "../Assets/Logo.png");
                                    ?>
                
                                    <img
                                        src="<?php echo htmlspecialchars($showThumb); ?>"
                                        class="user-avatar"
                                    >
                
                                    <div>
                                        <div class="user-name">
                                            <?php echo htmlspecialchars($show["title"]); ?>
                                        </div>
                
                                        <div class="user-sub">
                                            <?php echo htmlspecialchars($show["release_year"] ?? "Unknown"); ?>
                                            · ⭐ <?php echo htmlspecialchars($show["rating"] ?? "N/A"); ?>
                                        </div>
                                    </div>
                                </div>
                
                                <div>
                                    <?php echo htmlspecialchars($show["tmdb_id"] ?? "No TMDB ID"); ?>
                                </div>
                
                                <div>
                                    <span class="status-pill">
                                        <?php echo htmlspecialchars($show["category"] ?? "None"); ?>
                                    </span>
                                </div>
                
                                <div class="movie-db-actions">
                                    <a
                                        class="movie-edit-btn"
                                        href="EditSeries.php?id=<?php echo $show["id"]; ?>"
                                    >
                                        Edit
                                    </a>
                
                                    <a
                                        class="movie-delete-btn"
                                        href="Dashboard.php?tab=db_series&delete_series=<?php echo $show["id"]; ?>"
                                        onclick="return confirm('Delete this TV show?');"
                                    >
                                        Delete
                                    </a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                
                <div class="movie-db-panel" id="ppvDbPanel">
                    <div class="users-table">
                        <div class="users-table-header">
                            <div>PPV EVENT</div>
                            <div>CATEGORY</div>
                            <div>START TIME</div>
                            <div>ACTIONS</div>
                        </div>
                        <?php foreach ($ppvDbList as $ppv): ?>
                            <div class="user-row">
                                <div class="user-cell">
                                    <?php
                                        $ppvThumb = !empty($ppv["banner"])
                                            ? "../" . $ppv["banner"]
                                            : "../Assets/Logo.png";
                                    ?>
                                    <img
                                        src="<?php echo htmlspecialchars($ppvThumb); ?>"
                                        class="user-avatar"
                                    >
                                    <div>
                                        <div class="user-name">
                                            <?php echo htmlspecialchars($ppv["title"]); ?>
                                        </div>
                                        <div class="user-sub">
                                            ID #<?php echo $ppv["id"]; ?>
                                        </div>
                                    </div>
                                </div>
                                <div>
                                    <span class="status-pill">
                                        <?php echo htmlspecialchars($ppv["category"] ?? "None"); ?>
                                    </span>
                                </div>
                                <div>
                                    <?php
                                    echo !empty($ppv["start_time_bst"])
                                        ? date("M j, Y g:i A", strtotime($ppv["start_time_bst"]))
                                        : "No Time";
                                    ?>
                                </div>
                                <div class="movie-db-actions">
                
                                    <a
                                        class="movie-edit-btn"
                                        href="EditPPV.php?id=<?php echo $ppv["id"]; ?>"
                                    >
                                        Edit
                                    </a>
                
                                    <a
                                        class="movie-delete-btn"
                                        href="Dashboard.php?tab=db_ppv&delete_ppv=<?php echo $ppv["id"]; ?>"
                                        onclick="return confirm('Delete this PPV event?');"
                                    >
                                        Delete
                                    </a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>                
            
                <div class="hero-panel" id="heroPanel">
                    <form method="POST" class="media-form">
                        <div class="users-table">
                            <div class="users-table-header">
                                <div>MOVIE</div>
                                <div>TMDB ID</div>
                                <div>CATEGORY</div>
                                <div>SELECT</div>
                            </div>
            
                            <?php foreach ($movieDbList as $movie): ?>
                                <?php
                                    $movieThumb = !empty($movie["banner"]) ? "../" . $movie["banner"] : (!empty($movie["logo"]) ? "../" . $movie["logo"] : "../Assets/Logo.png");
                                    $checked = in_array($movie["id"], $selectedHeroMovies) ? "checked" : "";
                                ?>
                                <div class="user-row">
                                    <div class="user-cell">
                                        <img src="<?php echo htmlspecialchars($movieThumb); ?>" class="user-avatar">
                                        <div>
                                            <div class="user-name"><?php echo htmlspecialchars($movie["title"]); ?></div>
                                            <div class="user-sub">
                                                <?php echo htmlspecialchars($movie["release_year"] ?? "Unknown"); ?> · ⭐ <?php echo htmlspecialchars($movie["rating"] ?? "N/A"); ?>
                                            </div>
                                        </div>
                                    </div>
                                    <div><?php echo htmlspecialchars($movie["tmdb_id"] ?? "No TMDB ID"); ?></div>
                                    <div><span class="status-pill"><?php echo htmlspecialchars($movie["category"] ?? "None"); ?></span></div>
                                    <div>
                                        <input type="checkbox" name="hero_movies[]" value="<?php echo $movie["id"]; ?>" <?php echo $checked; ?> class="hero-checkbox">
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <button type="submit" name="save_hero_movies" class="submit-media-btn" style="margin-top:20px;">Save Hero Movies</button>
                    </form>
                </div>
                
                <div class="badge-panel" id="badgePanel">
                
                    <div class="badge-top-section">
                        <form method="POST" enctype="multipart/form-data" class="media-form badge-form">
                
                            <div class="form-grid">
                                <div class="form-group">
                                    <label>Badge Name</label>
                                    <input type="text" name="badge_name" placeholder="Premium Member" required>
                                </div>
                
                                <div class="form-group">
                                    <label>Badge Image</label>
                                    <input type="file" name="badge_image" accept="image/*" required>
                                </div>
                            </div>
                
                            <button type="submit" name="create_badge" class="submit-media-btn">
                                Create Badge
                            </button>
                
                        </form>
                    </div>
                
                    <div class="badge-list-wrapper">
                
                        <div class="badge-list-header">
                            <h2>Created Badges</h2>
                            <span><?php echo count($badgeList); ?> Total</span>
                        </div>
                
                        <div class="badge-grid">
                
                            <?php if (empty($badgeList)): ?>
                
                                <div class="empty-badge-state">
                                    No Badges Created Yet.
                                </div>
                
                            <?php endif; ?>
                
                            <?php foreach ($badgeList as $badge): ?>
                
                                <div class="badge-card">
                
                                    <div class="badge-image-wrap">
                                        <img
                                            src="../<?php echo htmlspecialchars($badge["badge_image"]); ?>"
                                            alt="<?php echo htmlspecialchars($badge["badge_name"]); ?>"
                                            class="badge-image"
                                        >
                                    </div>
                
                                    <div class="badge-info">
                                        <div class="badge-name">
                                            <?php echo htmlspecialchars($badge["badge_name"]); ?>
                                        </div>
                
                                        <div class="badge-id">
                                            Badge #<?php echo $badge["id"]; ?>
                                        </div>
                                    </div>
                
                                    <div class="badge-actions">
                                        <a
                                            href="Dashboard.php?tab=badge&delete_badge=<?php echo $badge["id"]; ?>"
                                            class="badge-delete-btn"
                                            onclick="return confirm('Delete this badge?');"
                                        >
                                            Delete
                                        </a>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>             
        
                <div class="movie-creator-panel" id="movieCreatorPanel">
                    <form method="POST" enctype="multipart/form-data" class="media-form">
                        <div class="form-grid">
                            <div class="form-group">
                                <label>Title</label>
                                <input type="text" name="title" placeholder="Movie title..." required>
                            </div>
                            <div class="form-group">
                                <label>TMDB ID</label>
                                <input type="text" name="tmdb_id" placeholder="TMDB ID...">
                            </div>
                            <div class="form-group">
                                <label>YouTube Trailer Link</label>
                                <input type="text" name="youtube_trailer_url" placeholder="https://www.youtube.com/watch?v=...">
                            </div>
                            <div class="form-group">
                                <label>Rating</label>
                                <input type="text" name="rating" placeholder="6.0">
                            </div>
                            <div class="form-group">
                                <label>Release Year</label>
                                <input type="text" name="release_year" placeholder="2026">
                            </div>
                            <div class="form-group">
                                <label>Duration</label>
                                <input type="text" name="duration" placeholder="1h 38m">
                            </div>
                            <div class="form-group full">
                                <label>Player Embed URL</label>
                                <input type="text" name="player_embed_url" placeholder="Leave empty to auto-use TMDB ID">
                            </div>
                            <div class="form-group full">
                                <label>Description</label>
                                <textarea name="description" placeholder="Movie description..."></textarea>
                            </div>
                            <div class="form-group">
                                <label>Logo</label>
                                <input type="file" name="logo" accept="image/*">
                            </div>
                            <div class="form-group">
                                <label>Banner</label>
                                <input type="file" name="banner" accept="image/*">
                            </div>
                            <div class="form-group">
                                <label>Category</label>
                                <select name="category">
                                    <option value="">Select Category</option>
                                    <option>Action</option>
                                    <option>Adventure</option>
                                    <option>Animation</option>
                                    <option>Comedy</option>
                                    <option>Crime</option>
                                    <option>Documentary</option>
                                    <option>Drama</option>
                                    <option>Family</option>
                                    <option>Fantasy</option>
                                    <option>History</option>
                                    <option>Horror</option>
                                    <option>Music</option>
                                    <option>Mystery</option>
                                    <option>Romance</option>
                                    <option>Sci-Fi</option>
                                    <option>Thriller</option>
                                    <option>War</option>
                                </select>
                            </div>
                        </div>
                        <button type="submit" name="add_movie" class="submit-media-btn">Create Movie</button>
                    </form>
                </div>
            
                <div class="series-creator-panel" id="seriesCreatorPanel">
                    <form method="POST" enctype="multipart/form-data" class="media-form">
                        <div class="form-grid">
                            <div class="form-group">
                                <label>Title</label>
                                <input type="text" name="title" placeholder="Series title..." required>
                            </div>
                            <div class="form-group">
                                <label>TMDB ID</label>
                                <input type="text" name="tmdb_id" placeholder="TMDB ID..." required>
                            </div>
                            <div class="form-group">
                                <label>YouTube Trailer Link</label>
                                <input type="text" name="youtube_trailer_url" placeholder="https://www.youtube.com/watch?v=...">
                            </div>
                            <div class="form-group">
                                <label>Rating</label>
                                <input type="text" name="rating" placeholder="8.5">
                            </div>
                            <div class="form-group">
                                <label>Release Year</label>
                                <input type="text" name="release_year" placeholder="2026">
                            </div>
                            <div class="form-group">
                                <label>Duration</label>
                                <input type="text" name="duration" placeholder="45m">
                            </div>
                            <div class="form-group full">
                                <label>Description</label>
                                <textarea name="description" placeholder="Series description..."></textarea>
                            </div>
                            <div class="form-group">
                                <label>Logo</label>
                                <input type="file" name="logo" accept="image/*">
                            </div>
                            <div class="form-group">
                                <label>Banner</label>
                                <input type="file" name="banner" accept="image/*">
                            </div>
                            <div class="form-group">
                                <label>Category</label>
                                <select name="category">
                                    <option value="">Select Category</option>
                                    <option>Action</option>
                                    <option>Adventure</option>
                                    <option>Animation</option>
                                    <option>Comedy</option>
                                    <option>Crime</option>
                                    <option>Documentary</option>
                                    <option>Drama</option>
                                    <option>Family</option>
                                    <option>Fantasy</option>
                                    <option>History</option>
                                    <option>Horror</option>
                                    <option>Mystery</option>
                                    <option>Romance</option>
                                    <option>Sci-Fi</option>
                                    <option>Thriller</option>
                                    <option>War</option>
                                </select>
                            </div>
                        </div>
            
                        <div class="episodes-box">
                            <div class="episodes-header">
                                <h3>Episodes</h3>
                                <button type="button" onclick="addEpisodeRow()">Add Episode</button>
                            </div>
                            <div id="episodeRows">
                                <div class="episode-row">
                                    <input type="number" name="season_number[]" placeholder="Season" min="1" required>
                                    <input type="number" name="episode_number[]" placeholder="Episode" min="1" required>
                                    <input type="text" name="episode_title[]" placeholder="Episode title">
                                    <input type="file" name="episode_background[]" accept="image/*">
                                    <button type="button" onclick="removeEpisodeRow(this)">Delete</button>
                                </div>
                            </div>
                        </div>
            
                        <button type="submit" name="add_series" class="submit-media-btn">Create TV Show</button>
                    </form>
                </div>
                
                <div class="movie-creator-panel" id="ppvCreatorPanel">

                    <form method="POST" enctype="multipart/form-data" class="media-form">
                
                        <div class="form-grid">
                
                            <div class="form-group">
                                <label>Title</label>
                                <input type="text" name="title" placeholder="PPV title..." required>
                            </div>
                
                            <div class="form-group">
                                <label>Time Start In BST</label>
                                <input type="datetime-local" name="start_time" required>
                            </div>
                
                            <div class="form-group">
                                <label>Player Slug</label>
                                <input type="text" name="player_slug" placeholder="example-player" required>
                            </div>
                
                            <div class="form-group">
                                <label>Category</label>
                
                                <select name="category" required>
                                    <option value="">Select Category</option>
                
                                    <option>All Sports</option>
                                    <option>Football</option>
                                    <option>Basketball</option>
                                    <option>American Football</option>
                                    <option>Hockey</option>
                                    <option>Combat Sports</option>
                                    <option>Motorsports</option>
                                    <option>24/7</option>
                                </select>
                            </div>
                
                            <div class="form-group">
                                <label>Banner</label>
                                <input type="file" name="banner" accept="image/*">
                            </div>
                        </div>
                        <button
                            type="submit"
                            name="add_ppv"
                            class="submit-media-btn"
                        >
                            Create PPV
                        </button>
                    </form>
                </div>
                <div class="top10-panel" id="top10Panel">
                
                    <div class="panel-title">
                        Top 10 Manager
                    </div>
                
                    <div class="panel-text" style="margin-bottom:25px;">
                        Select movies and TV shows for the homepage Top 10 section.
                    </div>
                
                    <form method="POST" class="media-form">
                
                        <div class="users-table">
                
                            <div class="users-table-header">
                                <div>TITLE</div>
                                <div>TYPE</div>
                                <div>RATING</div>
                                <div>POSITION</div>
                            </div>
                
                            <?php foreach ($movieDbList as $movie): ?>
                
                                <div class="user-row">
                
                                    <div class="user-cell">
                
                                        <?php
                                            $movieThumb =
                                                !empty($movie["banner"])
                                                ? "../" . $movie["banner"]
                                                : "../Assets/Logo.png";
                                        ?>
                
                                        <img
                                            src="<?php echo htmlspecialchars($movieThumb); ?>"
                                            class="user-avatar"
                                        >
                
                                        <div>
                                            <div class="user-name">
                                                <?php echo htmlspecialchars($movie["title"]); ?>
                                            </div>
                
                                            <div class="user-sub">
                                                <?php echo htmlspecialchars($movie["release_year"] ?? "Unknown"); ?>
                                            </div>
                                        </div>
                
                                    </div>
                
                                    <div>
                                        <span class="status-pill">
                                            Movie
                                        </span>
                                    </div>
                
                                    <div>
                                        ⭐ <?php echo htmlspecialchars($movie["rating"] ?? "N/A"); ?>
                                    </div>
                
                                    <div>
                
                                    <select
                                        name="top10_positions[movie_<?php echo $movie["id"]; ?>]"
                                        class="top10-select"
                                    >
                                    
                                        <option value="">
                                            None
                                        </option>
                                    
                                        <?php for ($i = 1; $i <= 10; $i++): ?>
                                    
                                            <option
                                                value="<?php echo $i; ?>"
                                    
                                                <?php
                                                    if (
                                                        isset($movie["top10_position"]) &&
                                                        (int)$movie["top10_position"] === $i
                                                    ) {
                                                        echo "selected";
                                                    }
                                                ?>
                                            >
                                                Top <?php echo $i; ?>
                                            </option>
                                    
                                        <?php endfor; ?>
                                    
                                    </select>
                
                                    </div>
                
                                </div>
                
                            <?php endforeach; ?>
                
                            <?php foreach ($seriesDbList as $show): ?>
                
                                <div class="user-row">
                
                                    <div class="user-cell">
                
                                        <?php
                                            $showThumb =
                                                !empty($show["banner"])
                                                ? "../" . $show["banner"]
                                                : "../Assets/Logo.png";
                                        ?>
                
                                        <img
                                            src="<?php echo htmlspecialchars($showThumb); ?>"
                                            class="user-avatar"
                                        >
                
                                        <div>
                                            <div class="user-name">
                                                <?php echo htmlspecialchars($show["title"]); ?>
                                            </div>
                
                                            <div class="user-sub">
                                                <?php echo htmlspecialchars($show["release_year"] ?? "Unknown"); ?>
                                            </div>
                                        </div>
                
                                    </div>
                
                                    <div>
                                        <span class="status-pill">
                                            TV Show
                                        </span>
                                    </div>
                
                                    <div>
                                        ⭐ <?php echo htmlspecialchars($show["rating"] ?? "N/A"); ?>
                                    </div>
                
                                    <div>
                
                                    <select
                                        name="top10_positions[series_<?php echo $show["id"]; ?>]"
                                        class="top10-select"
                                    >
                                    
                                        <option value="">
                                            None
                                        </option>
                                    
                                        <?php for ($i = 1; $i <= 10; $i++): ?>
                                    
                                            <option
                                                value="<?php echo $i; ?>"
                                    
                                                <?php
                                                    if (
                                                        isset($show["top10_position"]) &&
                                                        (int)$show["top10_position"] === $i
                                                    ) {
                                                        echo "selected";
                                                    }
                                                ?>
                                            >
                                                Top <?php echo $i; ?>
                                            </option>
                                    
                                        <?php endfor; ?>
                                    
                                    </select>
                
                                    </div>
                
                                </div>
                
                            <?php endforeach; ?>
                
                        </div>
                
                        <button
                            type="submit"
                            name="save_top10"
                            class="submit-media-btn"
                            style="margin-top:20px;"
                        >
                            Save Top 10
                        </button>
                    </form>
                </div>          
            </div>
        </div>
    </main>
</div>

<script>
lucide.createIcons();

function changeTab(tabName, button) {

    document.querySelectorAll(".side-link").forEach(link => {
        link.classList.remove("active");
    });

    button.classList.add("active");

    document.getElementById("pageTitle").textContent = tabName;

    const defaultPanel = document.getElementById("defaultPanel");

    const usersPanel = document.getElementById("usersPanel");
    const chatPanel = document.getElementById("chatPanel");

    const movieCreatorPanel = document.getElementById("movieCreatorPanel");
    const movieDbPanel = document.getElementById("movieDbPanel");

    const seriesCreatorPanel = document.getElementById("seriesCreatorPanel");
    const seriesDbPanel = document.getElementById("seriesDbPanel");

    const heroPanel = document.getElementById("heroPanel");
    const badgePanel = document.getElementById("badgePanel");

    const settingsPanel = document.getElementById("settingsPanel");

    const ppvCreatorPanel = document.getElementById("ppvCreatorPanel");
    const ppvDbPanel = document.getElementById("ppvDbPanel");

    const top10Panel = document.getElementById("top10Panel");

    // REMOVE ACTIVE FROM EVERYTHING
    usersPanel.classList.remove("active");
    chatPanel.classList.remove("active");

    movieCreatorPanel.classList.remove("active");
    movieDbPanel.classList.remove("active");

    seriesCreatorPanel.classList.remove("active");
    seriesDbPanel.classList.remove("active");

    heroPanel.classList.remove("active");
    badgePanel.classList.remove("active");

    settingsPanel.classList.remove("active");

    ppvCreatorPanel.classList.remove("active");
    ppvDbPanel.classList.remove("active");

    top10Panel.classList.remove("active");

    defaultPanel.style.display = "none";

    // OPEN SELECTED TAB
    if (tabName === "Users") {

        usersPanel.classList.add("active");

    } else if (tabName === "Chat Moderation") {

        chatPanel.classList.add("active");

    } else if (tabName === "Movies") {

        movieCreatorPanel.classList.add("active");

    } else if (tabName === "DB Movies") {

        movieDbPanel.classList.add("active");

    } else if (tabName === "TV Shows") {

        seriesCreatorPanel.classList.add("active");

    } else if (tabName === "DB TV Shows") {

        seriesDbPanel.classList.add("active");

    } else if (tabName === "Hero") {

        heroPanel.classList.add("active");

    } else if (tabName === "Badge Creator") {

        badgePanel.classList.add("active");

    } else if (tabName === "Settings") {

        settingsPanel.classList.add("active");

    } else if (tabName === "PPV") {

        ppvCreatorPanel.classList.add("active");

    } else if (tabName === "DB PPV") {

        ppvDbPanel.classList.add("active");

    } else if (tabName === "Top 10") {

        top10Panel.classList.add("active");

    } else {

        defaultPanel.style.display = "block";

        document.getElementById("panelTitle").textContent = tabName;

        document.getElementById("panelText").textContent =
            tabName + " section is ready to build.";
    }
}

function toggleProfileMenu() {
    const menu = document.getElementById("profileMenu");
    if (menu) menu.classList.toggle("active");
}

document.addEventListener("click", function(e) {
    const dropdown = document.querySelector(".profile-dropdown");
    const menu = document.getElementById("profileMenu");

    if (dropdown && menu && !dropdown.contains(e.target)) {
        menu.classList.remove("active");
    }
});

function toggleSidebar(btn) {
    document.querySelector(".admin-layout").classList.toggle("sidebar-hidden");

    btn.classList.toggle("active");
}

const urlParams = new URLSearchParams(window.location.search);

const tab = urlParams.get("tab");

const tabMap = {
    chat: "Chat Moderation",
    db_movies: "DB Movies",
    db_series: "DB TV Shows",
    db_ppv: "DB PPV",
    badge: "Badge Creator",
    settings: "Settings",
    ppv: "PPV"
};

if (tabMap[tab]) {

    const btn = [...document.querySelectorAll(".side-link")]
        .find(btn => btn.textContent.trim() === tabMap[tab]);

    if (btn) {
        changeTab(tabMap[tab], btn);
    }
}

document.addEventListener("change", function(e) {
    if (e.target.classList.contains("hero-checkbox")) {
        const checked = document.querySelectorAll(".hero-checkbox:checked");

        if (checked.length > 3) {
            e.target.checked = false;
            alert("You can only select up to 3 hero movies.");
        }
    }
});

function addEpisodeRow() {
    const episodeRows = document.getElementById("episodeRows");

    const row = document.createElement("div");
    row.className = "episode-row";

    row.innerHTML = `
        <input type="number" name="season_number[]" placeholder="Season" min="1" required>

        <input type="number" name="episode_number[]" placeholder="Episode" min="1" required>

        <input type="text" name="episode_title[]" placeholder="Episode title">

        <input type="file" name="episode_background[]" accept="image/*">

        <button type="button" onclick="removeEpisodeRow(this)">
            Delete
        </button>
    `;

    episodeRows.appendChild(row);
}

function removeEpisodeRow(button) {
    button.closest(".episode-row").remove();
}
</script>
<script src="../Security.js"></script>
</body>
</html>