<?php
// includes/header.php - painel TV Express profissional, roxo, responsivo
require_once __DIR__ . '/../auth.php';
require_login();

$user = current_user();
$username = $user['username'] ?? 'Usuário';
$user_id = $user['id'] ?? 0;
$username = user_display_name($user);
$is_admin = user_is_admin($user);
$is_reseller = user_is_reseller($user);

$branding = get_branding();
$logoUrl  = trim((string)($branding['logo_url'] ?? ''));
$bgUrl    = trim((string)($branding['background_url'] ?? ''));

$panelLogo = 'uploads/panel_tvexpress_logo.png';
$panelBackground = 'uploads/panel_tvexpress_bg.jpg';

$theme_config = get_user_theme($user_id);
$theme_mode = $theme_config['mode'] ?? 'purple';
$custom_css = $theme_config['custom_css'] ?? '';
$theme_css = get_theme_css($theme_mode, $custom_css);

$currentPage = basename($_SERVER['PHP_SELF'] ?? 'dashboard.php');
$pageNames = [
    'dashboard.php' => 'Dashboard',
    'theme.php' => 'Tema do painel',
    'devices.php' => 'Dispositivos',
    'background.php' => 'Background do app',
    'logo.php' => 'Logo do app',
    'note.php' => 'Avisos do app',
    'support.php' => 'QR Suporte',
    'dns.php' => 'Ativar Cliente',
    'list_dns.php' => 'Lista de Clientes',
    'teste_auto.php' => 'Teste Auto',
    'user_create.php' => 'Usuários',
    'perfil.php' => 'Meu perfil',
];
$pageNames['user_create.php'] = 'Revendas';
$pageNames['all_clients.php'] = 'Todos clientes';
$pageTitle = $pageNames[$currentPage] ?? 'Painel Administrativo';
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <meta name="theme-color" content="#5f0707">
    <title><?php echo htmlspecialchars($pageTitle); ?> - VODs Express</title>
    <style>
        <?php echo $theme_css; ?>
        :root {
            --primary-bg: linear-gradient(135deg, #ff5252 0%, #ff5252 48%, #bd1414 100%) !important;
            --primary-solid: #ff5252;
            --primary-soft: rgba(255, 82, 82, .20);
            --green: #7CFF6B;
            --panel-bg: #260000;
            --panel-bg-2: #4b0000;
            --card-bg: rgba(67, 8, 8, .78);
            --card-bg-strong: rgba(54, 5, 5, .92);
            --sidebar-bg: linear-gradient(180deg, rgba(70, 4, 4, .96) 0%, rgba(34, 0, 0, .98) 100%);
            --link-bg: rgba(255, 255, 255, .055);
            --link-hover: rgba(255, 92, 92, .22);
            --logout-bg: rgba(255, 77, 77, .18);
            --logout-hover: rgba(255, 77, 77, .30);
            --border-color: rgba(255, 205, 205, .16);
            --border-strong: rgba(255, 205, 205, .32);
            --text-primary: #ffffff;
            --text-secondary: #f6c7c7;
            --text-muted: #d59494;
            --shadow: 0 22px 65px rgba(0, 0, 0, .38);
            --radius-xl: 24px;
            --radius-lg: 18px;
            --radius-md: 14px;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }
        html { min-height: 100%; scroll-behavior: smooth; }
        body {
            min-height: 100vh;
            background:
                radial-gradient(circle at 30% 18%, rgba(255, 64, 64, .45), transparent 35%),
                linear-gradient(135deg, rgba(33, 0, 0, .86), rgba(90, 0, 0, .68)),
                url('<?php echo htmlspecialchars($panelBackground, ENT_QUOTES, 'UTF-8'); ?>') center center / cover fixed no-repeat,
                #260000;
            color: var(--text-primary);
            font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
            overflow-x: hidden;
        }
        body::before {
            content: "";
            position: fixed;
            inset: 0;
            pointer-events: none;
            background:
                linear-gradient(90deg, rgba(128,3,3,.38), transparent 38%, rgba(128,3,3,.22)),
                radial-gradient(circle at 92% 84%, rgba(255,156,107,.09), transparent 22%);
            z-index: -1;
        }
        a { color: inherit; }

        .sidebar-overlay {
            display: none;
            position: fixed;
            inset: 0;
            background: rgba(15, 0, 0, .72);
            backdrop-filter: blur(8px);
            z-index: 998;
            opacity: 0;
            transition: opacity .25s ease;
        }
        .sidebar-overlay.active { display: block; opacity: 1; }

        .sidebar-painel {
            position: fixed;
            top: 0;
            left: 0;
            width: 286px;
            height: 100vh;
            background: var(--sidebar-bg);
            border-right: 1px solid var(--border-color);
            box-shadow: 18px 0 60px rgba(0,0,0,.35);
            color: #fff;
            display: flex;
            flex-direction: column;
            padding: 18px 14px;
            z-index: 999;
            transition: transform .28s ease;
            overflow-y: auto;
        }
        .sidebar-painel::before {
            content: "";
            position: absolute;
            inset: 0;
            background: radial-gradient(circle at 40% 0%, rgba(255, 92, 92, .30), transparent 34%);
            pointer-events: none;
        }
        .sidebar-logo, .sidebar-menu, .sidebar-user { position: relative; z-index: 1; }
        .sidebar-logo {
            min-height: auto;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
            padding: 0;
            border: none;
            border-radius: 0;
            background: transparent;
            box-shadow: none;
        }
        .sidebar-logo a {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            background: transparent;
            border: 0;
        }
        .sidebar-logo img {
            max-height: 70px;
            max-width: 180px;
            width: auto;
            object-fit: contain;
            background: transparent;
            border: none;
            border-radius: 0;
            padding: 0;
            filter: drop-shadow(0 10px 24px rgba(0,0,0,.35));
            transition: transform .2s ease;
        }
        .sidebar-logo img:hover {
            transform: scale(1.05);
        }
        .sidebar-logo strong {
            font-size: 18px;
            color: #fff !important;
            letter-spacing: .02em;
        }
        .brand-subtitle {
            display: inline-flex;
            align-items: center;
            gap: 7px;
            color: var(--text-secondary);
            font-size: 12px;
            letter-spacing: .14em;
            text-transform: uppercase;
        }
        .brand-subtitle::before { content: ""; width: 7px; height: 7px; background: var(--green); border-radius: 99px; box-shadow: 0 0 16px var(--green); }

        .sidebar-menu { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; flex: 1; }
        .sidebar-menu a {
            display: flex;
            align-items: center;
            gap: 10px;
            min-height: 46px;
            padding: 12px 14px;
            border-radius: 15px;
            text-decoration: none;
            color: rgba(255,255,255,.92);
            background: var(--link-bg);
            border: 1px solid transparent;
            font-size: 14px;
            font-weight: 700;
            transition: transform .18s ease, background .18s ease, border-color .18s ease, box-shadow .18s ease;
        }
        .sidebar-menu a:hover {
            background: var(--link-hover);
            border-color: rgba(255,205,205,.20);
            transform: translateX(5px);
            box-shadow: 0 12px 28px rgba(0,0,0,.18);
        }
        .sidebar-menu a.active {
            background: var(--primary-bg);
            border-color: rgba(255,255,255,.32);
            box-shadow: 0 12px 30px rgba(255,44,44,.30);
            color: #fff;
        }
        .sidebar-user {
            padding: 16px;
            border: 1px solid var(--border-color);
            border-radius: 18px;
            background: rgba(0,0,0,.20);
            font-size: 13px;
        }
        .sidebar-user span { color: var(--text-muted); }
        .sidebar-user strong { display: block; margin: 5px 0 12px; color: #fff !important; font-size: 15px; }
        .sidebar-user a {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 8px;
            width: 100%;
            text-align: center;
            padding: 11px 14px;
            border-radius: 14px;
            text-decoration: none;
            color: #fff;
            background: var(--logout-bg);
            border: 1px solid rgba(255, 77, 77, .22);
            font-weight: 800;
            transition: .18s ease;
        }
        .sidebar-user a:hover { background: var(--logout-hover); transform: translateY(-2px); }

        .hamburger-btn {
            display: none;
            position: fixed;
            top: 16px;
            left: 16px;
            z-index: 1000;
            background: var(--primary-bg);
            border: 1px solid rgba(255,255,255,.20);
            width: 48px;
            height: 48px;
            border-radius: 16px;
            cursor: pointer;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            gap: 5px;
            box-shadow: 0 16px 35px rgba(0,0,0,.32);
        }
        .hamburger-btn span { display: block; width: 23px; height: 3px; background: #fff; border-radius: 8px; transition: .25s ease; }
        .hamburger-btn.active span:nth-child(1) { transform: rotate(45deg) translate(6px, 6px); }
        .hamburger-btn.active span:nth-child(2) { opacity: 0; }
        .hamburger-btn.active span:nth-child(3) { transform: rotate(-45deg) translate(5px, -5px); }

        .main-content {
            margin-left: 286px;
            min-height: 100vh;
            padding: 24px 26px 34px;
            transition: margin-left .28s ease;
        }
        .topbar-panel {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            margin-bottom: 24px;
            padding: 16px 18px;
            border: 1px solid var(--border-color);
            border-radius: 22px;
            background: rgba(38, 0, 0, .62);
            box-shadow: var(--shadow);
            backdrop-filter: blur(16px);
        }

        .status-pill::before { content: ""; width: 8px; height: 8px; border-radius: 99px; background: var(--green); box-shadow: 0 0 14px var(--green); }

        .page-header, .dashboard-header {
            margin-bottom: 22px !important;
            padding: 22px 24px !important;
            border: 1px solid var(--border-color) !important;
            border-radius: var(--radius-xl) !important;
            background: rgba(38, 0, 0, .58) !important;
            box-shadow: var(--shadow) !important;
            backdrop-filter: blur(16px) !important;
        }
        .page-header h1, .dashboard-header h1,
        .info-section h2, .card h2, .support-card h2,
        h1, h2, h3, h4, h5, h6 {
            color: #fff !important;
            -webkit-text-fill-color: initial !important;
            background: none !important;
        }
        .page-header h1, .dashboard-header h1 { font-size: clamp(24px, 3vw, 34px) !important; margin-bottom: 7px !important; }
        .page-header p, .dashboard-header p, p, .muted, .text-muted { color: var(--text-secondary) !important; }

        .card, .form-container, .table-container, .info-section, .stat-card,
        .css-editor-container, .theme-card, .support-card, .table-card,
        .preview-section, .upload-section, .info-box {
            background: var(--card-bg) !important;
            border: 1px solid var(--border-color) !important;
            border-radius: var(--radius-xl) !important;
            box-shadow: var(--shadow) !important;
            backdrop-filter: blur(16px) !important;
        }
        .stat-card, .card, .form-container, .table-container, .info-section, .support-card, .table-card {
            padding: 22px !important;
        }
        .stat-card:hover, .theme-card:hover, .action-btn:hover {
            transform: translateY(-4px) !important;
            border-color: rgba(255,92,92,.42) !important;
            box-shadow: 0 26px 70px rgba(0,0,0,.40) !important;
        }
        .stat-card .value, .value {
            background: linear-gradient(135deg, #ffffff 0%, #ffb8b8 100%) !important;
            -webkit-background-clip: text !important;
            -webkit-text-fill-color: transparent !important;
            background-clip: text !important;
        }

        label, strong, th { color: #fff !important; }
        span, div, td, li { border-color: rgba(255,255,255,.08); }
        .info-row, .form-group { border-color: rgba(255,255,255,.08) !important; }
        .info-row label { color: var(--text-muted) !important; }
        .info-row value, value { color: #fff !important; }

        input, select, textarea {
            width: 100%;
            background: rgba(0,0,0,.28) !important;
            border: 1px solid rgba(255,205,205,.22) !important;
            border-radius: 15px !important;
            color: #fff !important;
            outline: none !important;
            min-height: 46px;
            padding: 12px 14px !important;
            transition: border-color .18s ease, box-shadow .18s ease, background .18s ease;
        }
        input::placeholder, textarea::placeholder { color: rgba(246,199,199,.55) !important; }
        input:focus, select:focus, textarea:focus {
            border-color: rgba(255,92,92,.80) !important;
            box-shadow: 0 0 0 4px rgba(255,92,92,.16) !important;
            background: rgba(255,255,255,.055) !important;
        }
        textarea { min-height: 120px; }

        button, .submit-btn, .btn-save, .select-btn, .action-btn, .btn {
            border-radius: 15px !important;
            border: 1px solid rgba(255,255,255,.16) !important;
            cursor: pointer;
            transition: transform .18s ease, box-shadow .18s ease, background .18s ease;
        }
        .submit-btn, .btn-save, button[type="submit"], .select-btn.active-btn {
            background: var(--primary-bg) !important;
            color: #fff !important;
            font-weight: 900 !important;
            box-shadow: 0 14px 35px rgba(255,44,44,.28) !important;
        }
        .submit-btn:hover, .btn-save:hover, button[type="submit"]:hover, .btn:hover { transform: translateY(-2px); }
        .action-btn {
            background: rgba(255,255,255,.07) !important;
            border: 1px solid rgba(255,255,255,.12) !important;
            color: #fff !important;
            text-decoration: none !important;
            font-weight: 800;
        }
        .action-btn:hover { background: var(--primary-bg) !important; }

        .alert, .error-message {
            border-radius: 16px !important;
            border: 1px solid rgba(255,255,255,.16) !important;
            background: rgba(255,255,255,.08) !important;
            color: #fff !important;
            backdrop-filter: blur(12px);
        }
        .alert-success, .alert.ok { background: rgba(46,204,113,.16) !important; border-color: rgba(46,204,113,.32) !important; color: #d8ffd2 !important; }
        .alert-danger, .alert.err { background: rgba(255,75,75,.16) !important; border-color: rgba(255,75,75,.32) !important; color: #ffd5d5 !important; }
        .alert-info { background: rgba(95,174,255,.14) !important; border-color: rgba(95,174,255,.28) !important; color: #d8ecff !important; }

        .table-responsive, .table-container { width: 100%; overflow-x: auto; }
        table { width: 100%; border-collapse: collapse; color: #fff; min-width: 650px; }
        th, td { padding: 14px 13px !important; border-bottom: 1px solid rgba(255,255,255,.08) !important; color: #fff2f2 !important; }
        thead, .table-dark { background: rgba(255,44,44,.24) !important; }
        tr:hover { background: rgba(255,255,255,.045) !important; }
        .key-box { background: rgba(0,0,0,.28) !important; border-color: rgba(255,205,205,.18) !important; color: #ffdcdc !important; }
        .badge { display: inline-flex; padding: 6px 10px; border-radius: 999px; border: 1px solid rgba(255,255,255,.12); background: rgba(255,255,255,.08); color: #fff !important; }
        .text-primary { color: #ff8282 !important; }


        .container-fluid { width: 100%; }
        .row { display: flex; flex-wrap: wrap; margin-left: -8px; margin-right: -8px; }
        .row > * { padding-left: 8px; padding-right: 8px; width: 100%; }
        .col-md-12 { flex: 0 0 100%; max-width: 100%; }
        .d-flex { display: flex !important; }
        .flex-column { flex-direction: column !important; }
        .align-middle { vertical-align: middle !important; }
        .text-end { text-align: right !important; }
        .text-center { text-align: center !important; }
        .fw-bold { font-weight: 800 !important; }
        .small { font-size: 12px !important; }
        .mt-3 { margin-top: 1rem !important; }
        .mt-4 { margin-top: 1.25rem !important; }
        .mb-1 { margin-bottom: .25rem !important; }
        .mb-3 { margin-bottom: 1rem !important; }
        .py-5 { padding-top: 3rem !important; padding-bottom: 3rem !important; }
        .btn-danger { background: linear-gradient(135deg, #ff4d4d, #b51717) !important; color: #fff !important; }
        .btn-sm { padding: 8px 11px !important; min-height: auto !important; font-size: 12px !important; }
        .btn-close { float: right; width: 20px; height: 20px; opacity: .8; border: 0 !important; background: transparent !important; color: #fff !important; box-shadow: none !important; }

        .qr-box { border-radius: 24px !important; padding: 16px !important; background: #fff !important; box-shadow: 0 20px 50px rgba(0,0,0,.30) !important; }
        .url-box { color: var(--text-secondary) !important; }

        .sidebar-painel::-webkit-scrollbar, .table-responsive::-webkit-scrollbar, .table-container::-webkit-scrollbar { width: 7px; height: 7px; }
        .sidebar-painel::-webkit-scrollbar-track, .table-responsive::-webkit-scrollbar-track, .table-container::-webkit-scrollbar-track { background: rgba(255,255,255,.05); }
        .sidebar-painel::-webkit-scrollbar-thumb, .table-responsive::-webkit-scrollbar-thumb, .table-container::-webkit-scrollbar-thumb { background: rgba(255,92,92,.45); border-radius: 99px; }

        @media (max-width: 920px) {
            .hamburger-btn { display: flex; }
            .sidebar-painel { transform: translateX(-105%); width: min(306px, 86vw); }
            .sidebar-painel.active { transform: translateX(0); }
            .main-content { margin-left: 0; padding: 78px 14px 28px; }
            .topbar-panel { padding: 14px; border-radius: 18px; align-items: flex-start; flex-direction: column; }
            .status-pill { width: 100%; justify-content: center; }
            .page-header, .dashboard-header, .stat-card, .card, .form-container, .table-container, .info-section, .support-card, .table-card { padding: 18px !important; border-radius: 18px !important; }
            table { min-width: 700px; }
        }
        @media (max-width: 520px) {
            .main-content { padding-left: 10px; padding-right: 10px; }
            .topbar-title h1 { font-size: 19px; }
            .sidebar-logo { min-height: auto; margin-bottom: 20px; }
            .sidebar-logo img { max-height: 72px; max-width: 190px; }
        }
    </style>
</head>
<body>
    <button class="hamburger-btn" id="hamburgerBtn" aria-label="Abrir menu" type="button">
        <span></span><span></span><span></span>
    </button>

    <div class="sidebar-overlay" id="sidebarOverlay"></div>

    <aside class="sidebar-painel" id="sidebar">
        <div class="sidebar-logo">
            <a href="https://t.me/storeart_digital" target="_blank" rel="noopener noreferrer" title="StoreArt">
                <img src="<?php echo htmlspecialchars($panelLogo, ENT_QUOTES, 'UTF-8'); ?>" alt="VODs Express">
            </a>
        </div>

        <nav class="sidebar-menu">
       
            <a href="devices.php">📺 Dispositivos</a>
            <a href="list_dns.php">🙍🏻‍♂️Clientes</a>
            <?php if ($is_admin): ?>
                <a href="all_clients.php">👨‍👩‍👧 Todos clientes</a>
                <a href="user_create.php">🚻 Revendas</a>
                <a href="teste_auto.php">⏰ Teste Auto</a>
                <a href="logo.php">🎨 Logo</a>
                <a href="background.php">🌄 Fundo</a>
                <a href="support.php">📲 QR Suporte</a>
            <?php endif; ?>
            <a href="perfil.php"><?php echo $is_admin ? '🪪 Admin' : 'Meu painel'; ?></a>
            <a href="logout.php">📤 Sair</a>
        </nav>

    </aside>

    <main class="main-content">


    <script>
        (function(){
            const hamburgerBtn = document.getElementById('hamburgerBtn');
            const sidebar = document.getElementById('sidebar');
            const overlay = document.getElementById('sidebarOverlay');
            function toggleMenu(forceClose) {
                const shouldClose = forceClose === true;
                hamburgerBtn.classList.toggle('active', shouldClose ? false : !hamburgerBtn.classList.contains('active'));
                sidebar.classList.toggle('active', shouldClose ? false : !sidebar.classList.contains('active'));
                overlay.classList.toggle('active', shouldClose ? false : !overlay.classList.contains('active'));
            }
            hamburgerBtn.addEventListener('click', function(){ toggleMenu(false); });
            overlay.addEventListener('click', function(){ toggleMenu(true); });
            document.addEventListener('keydown', function(e){ if (e.key === 'Escape') toggleMenu(true); });
            const currentPage = window.location.pathname.split('/').pop() || 'dashboard.php';
            document.querySelectorAll('.sidebar-menu a').forEach(function(link){
                if (link.getAttribute('href') === currentPage) link.classList.add('active');
                link.addEventListener('click', function(){ if (window.innerWidth <= 920) toggleMenu(true); });
            });
        })();
    </script>
