<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
ini_set("display_errors", 0);
error_reporting(0);

if (!isset($_SESSION['id'])) {
    header("Location: login.php");
    exit();
}

$id = $_SESSION['id'];
$isAdmin = $_SESSION['admin'];

$dbans = new SQLite3("./api/.ansdb.db");
$adb = new SQLite3('./api/.adb.db');
$dbpans = new SQLite3("./api/.anspanel.db");

$resans = $dbpans->query("SELECT * FROM USERS WHERE ID='1'");
$rowans = $resans->fetchArray();
$nameans = $rowans["NAME"];
$logoans = $rowans["LOGO"];

$jsondata111 = file_get_contents("./includes/ansibo.json");
$json111 = json_decode($jsondata111, true);
$col2 = $json111["info"]["aa"] ?? 'primary'; 
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title><?= $nameans ?> - Painel Premium</title>
    
    <link rel="shortcut icon" href="./img2/logo.png" type="image/png">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;800&display=swap" rel="stylesheet">
    <link href="css/sb-admin-2.min.css" rel="stylesheet">

    <style>
        :root {
            --bg-nav: #020617; 
            --bg-dark: #020617; 
            --accent: #ffaa00; 
            --text-dim: #94a3b8;
            --border: rgba(255, 255, 255, 0.05);
            --side-width: 280px;
            --top-height: 65px;
            --btn-bg: rgba(255, 255, 255, 0.03);
        }

        /* ANIMAÇÃO DOS ÍCONES */
        @keyframes wiggle {
            0% { transform: rotate(0deg); }
            25% { transform: rotate(-10deg); }
            50% { transform: rotate(10deg); }
            75% { transform: rotate(-10deg); }
            100% { transform: rotate(0deg); }
        }

        body { 
            font-family: 'Plus Jakarta Sans', sans-serif;
            background-color: var(--bg-dark) !important;
            color: #f8fafc;
            margin: 0;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        /* TOPBAR */
        .modern-topbar {
            height: var(--top-height);
            background: var(--bg-nav);
            position: fixed;
            top: 0; width: 100%; z-index: 1200;
            display: flex;
            align-items: center;
            padding: 0 20px;
            border-bottom: 1px solid var(--border);
        }

        .topbar-title {
            font-weight: 800;
            font-size: 1rem;
            color: #fff;
            margin-left: 15px;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        /* SIDEBAR CORRIGIDO */
        .modern-sidebar {
            width: var(--side-width);
            background: var(--bg-nav);
            height: calc(100vh - var(--top-height));
            position: fixed;
            top: var(--top-height);
            left: 0; z-index: 1100;
            border-right: 1px solid var(--border);
            transition: 0.3s ease-in-out;
            display: flex;
            flex-direction: column;
            overflow-y: auto;
            overflow-x: hidden;
        }

        .modern-sidebar::-webkit-scrollbar { width: 4px; }
        .modern-sidebar::-webkit-scrollbar-track { background: var(--bg-nav); }
        .modern-sidebar::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 10px; }

        .sidebar-logo-area {
            padding: 20px 15px;
            text-align: center;
            background: rgba(0,0,0,0.2);
            border-bottom: 1px solid var(--border);
            flex-shrink: 0;
        }

        .sidebar-logo-area img {
            max-width: 140px;
            height: auto;
        }

        .sidebar-content { 
            padding: 15px 12px; 
            flex: 1 0 auto; /* Ajuste para não criar vácuo no fundo */
        }

        .section-header {
            padding: 20px 15px 10px;
            font-size: 0.65rem;
            text-transform: uppercase;
            color: #fff;
            font-weight: 800;
            letter-spacing: 2px;
            opacity: 0.4;
        }

        .nav-link-item {
            display: flex;
            align-items: center;
            padding: 12px 16px;
            color: #e2e8f0;
            text-decoration: none !important;
            font-size: 0.82rem;
            font-weight: 600;
            border-radius: 6px;
            margin-bottom: 6px;
            background: var(--btn-bg); 
            border-left: 4px solid var(--accent); 
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
        }

        /* ATIVAÇÃO DA ANIMAÇÃO NOS ÍCONES */
        .nav-link-item i {
            width: 25px;
            font-size: 1.05rem;
            margin-right: 12px;
            color: var(--accent);
            filter: drop-shadow(0 0 5px rgba(255, 170, 0, 0.5));
            transition: 0.3s;
            text-align: center;
            display: inline-block;
            animation: wiggle 2s ease-in-out infinite;
        }

        .nav-link-item:hover {
            background: rgba(255, 170, 0, 0.12);
            color: #fff;
            transform: translateX(6px);
            border-left: 4px solid #ffffff; 
            box-shadow: 0 4px 15px rgba(0,0,0,0.4);
        }

        .nav-link-item:hover i {
            transform: scale(1.2);
            filter: drop-shadow(0 0 8px rgba(255, 170, 0, 0.9));
        }

        /* BOTÃO SAIR */
        .btn-logout {
            background: rgba(239, 68, 68, 0.08);
            border-left: 4px solid #f87171;
            color: #f87171 !important;
            margin-top: 20px;
            margin-bottom: 50px; /* Margem para parar o scroll corretamente */
        }
        .btn-logout i { 
            color: #f87171; 
            filter: drop-shadow(0 0 5px rgba(239, 68, 68, 0.3)); 
            animation: none; /* Sair não precisa mexer */
        }
        .btn-logout:hover { background: #f87171; color: #fff !important; border-left: 4px solid #fff; }
        .btn-logout:hover i { color: #fff; filter: none; }

        /* MAIN WRAPPER */
        .main-wrapper {
            margin-left: var(--side-width);
            margin-top: var(--top-height);
            display: flex;
            flex-direction: column;
            flex: 1;
        }

        .content-area { flex: 1; padding: 25px; }

        @media (max-width: 991px) {
            .modern-sidebar { left: -100%; height: calc(100vh - var(--top-height)); }
            .modern-sidebar.active { left: 0; box-shadow: 10px 0 30px rgba(0,0,0,0.8); }
            .main-wrapper { margin-left: 0; }
        }

        #sidebarToggle { cursor: pointer; border: none; background: transparent; color: white; font-size: 1.2rem; }
    </style>
</head>
<body id="page-top">

    <header class="modern-topbar">
        <button id="sidebarToggle" onclick="document.querySelector('.modern-sidebar').classList.toggle('active')">
            <i class="fa fa-bars"></i>
        </button>
        <div class="topbar-title"><?= $nameans ?></div>
    </header>

    <aside class="modern-sidebar">
        <div class="sidebar-logo-area">
            <a href="users.php"><img src="img2/logo.png?v=<?= time() ?>" alt="Logo"></a>
        </div>

        <div class="sidebar-content">
            <?php if ($isAdmin) : ?>
                <div class="section-header">Navegação</div>
                <a class="nav-link-item" href="index.php"><i class="fas fa-chart-line"></i><span>DASHBOARD</span></a>
                <a class="nav-link-item" href="edit_texto.php"><i class="fas fa-bullhorn"></i><span>AVISOS DO SISTEMA</span></a>
                <a class="nav-link-item" href="users.php"><i class="fas fa-user-friends"></i><span>MEUS CLIENTES</span></a>
                <a class="nav-link-item" href="all_users.php"><i class="fas fa-users-cog"></i><span>TODOS OS CLIENTES</span></a>
                <a class="nav-link-item" href="gerenciar_planos.php"><i class="fas fa-list-check"></i><span>GERENCIAR PLANOS</span></a>
                <a class="nav-link-item" href="replace.php"><i class="fas fa-network-wired"></i><span>ATUALIZAR DNS</span></a>

                <div class="section-header">Segurança & Auditoria</div>
                <a class="nav-link-item" href="logs.php"><i class="fas fa-eye"></i><span>MONITORAR LOGS</span></a>

                <div class="section-header">Gestão de Revenda</div>
                <a class="nav-link-item" href="stores.php"><i class="fas fa-gem"></i><span>REVENDA PREMIUM</span></a>
                <a class="nav-link-item" href="stores_mac.php"><i class="fas fa-desktop"></i><span>SUB-REVENDA</span></a>

                <div class="section-header">Customização APP</div>
                <a class="nav-link-item" href="layouts.php"><i class="fas fa-palette"></i><span>TEMAS DO APLICATIVO</span></a>
                <a class="nav-link-item" href="logo.php"><i class="fas fa-file-image"></i><span>LOGO PRINCIPAL</span></a>
                <a class="nav-link-item" href="logo2.php"><i class="fas fa-file-image"></i><span>LOGO AVATAR</span></a>
                <a class="nav-link-item" href="Image.php"><i class="fas fa-images"></i><span>FUNDO ABERTURA</span></a>
                <a class="nav-link-item" href="Image2.php"><i class="fas fa-images"></i><span>FUNDO PRINCIPAL</span></a>

                <div class="section-header">Marketing & Banners</div>
                <a class="nav-link-item" href="autoads.php"><i class="fas fa-magic"></i><span>BANNERS AUTOMÁTICOS</span></a>
                <a class="nav-link-item" href="ads.php"><i class="fas fa-tv"></i><span>BANNERS P2P</span></a>
                <a class="nav-link-item" href="ads2.php"><i class="fas fa-play-circle"></i><span>BANNERS PLAYLIST</span></a>

                <div class="section-header">Configurações</div>
                <a class="nav-link-item" href="chatbot.php"><i class="fas fa-robot"></i><span>CHATBOT INTELIGENTE</span></a>
                <a class="nav-link-item" href="profile.php"><i class="fas fa-user-shield"></i><span>MEU PERFIL</span></a>

                <a class="nav-link-item btn-logout" href="logout.php"><i class="fas fa-power-off"></i><span>SAIR DA CONTA</span></a>

            <?php else : ?>
                <div class="section-header">Menu Revendedor</div>
                <a class="nav-link-item" href="users.php"><i class="fas fa-home"></i><span>DASHBOARD</span></a>
                <a class="nav-link-item" href="replace.php"><i class="fas fa-link"></i><span>DOMÍNIO DNS</span></a>
                <a class="nav-link-item" href="logs.php"><i class="fas fa-eye"></i><span>MEUS LOGS</span></a>
                <a class="nav-link-item btn-logout" href="logout.php"><i class="fas fa-power-off"></i><span>SAIR DA CONTA</span></a>
            <?php endif; ?>
        </div>
    </aside>

    <div class="main-wrapper">
        <div class="content-area">