<?php
// ── Cabeçalhos de segurança HTTP ─────────────────────────────────────────────
if (!headers_sent()) {
    header('X-Frame-Options: SAMEORIGIN');
    header('X-Content-Type-Options: nosniff');
    header('X-XSS-Protection: 1; mode=block');
    header('Referrer-Policy: strict-origin-when-cross-origin');
    header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net https://unpkg.com https://fonts.googleapis.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://fonts.gstatic.com https://unpkg.com; font-src 'self' https://fonts.gstatic.com https://unpkg.com; img-src 'self' data: https://*.tile.openstreetmap.org https://*.openstreetmap.org; connect-src 'self' https://*.tile.openstreetmap.org; frame-ancestors 'self';");
}

$user = current_user();
$page = $_GET['page'] ?? 'home';
$isAuthenticated = (bool)$user;
$isAdmin = $isAuthenticated && $user['role'] === 'admin';
$plan = $isAuthenticated ? get_user_plan($user['id']) : null;

// Ícones SVG para cada item de menu
$menuIcons = [
    'user_sla'       => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/></svg>',
    'user_tags'      => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"/></svg>',
    'user_maintenance'=> '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>',
    'user_api_tokens'=> '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"/></svg>',
    'admin_saas_metrics' => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>',
    'dashboard'      => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/></svg>',
    'user_ip_form'   => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>',
    'user_ips'       => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h14M5 12a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v4a2 2 0 01-2 2M5 12a2 2 0 00-2 2v4a2 2 0 002 2h14a2 2 0 002-2v-4a2 2 0 00-2-2m-2-4h.01M17 16h.01"/></svg>',
    'user_logs'      => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>',
    'status_page'    => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/></svg>',
    'nms_dashboard'  => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.111 16.404a5.5 5.5 0 017.778 0M12 20h.01m-7.08-7.071c3.904-3.905 10.236-3.905 14.141 0M1.394 9.393c5.857-5.857 15.355-5.857 21.213 0"/></svg>',
    'nms_devices'    => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 3H5a2 2 0 00-2 2v4m6-6h10a2 2 0 012 2v4M9 3v18m0 0h10a2 2 0 002-2v-4M9 21H5a2 2 0 01-2-2v-4m0 0h18"/></svg>',
    'nms_alerts'     => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/></svg>',
    'nms_map'        => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 20l-5.447-2.724A1 1 0 013 16.382V5.618a1 1 0 011.447-.894L9 7m0 13l6-3m-6 3V7m6 10l4.553 2.276A1 1 0 0021 18.382V7.618a1 1 0 00-.553-.894L15 4m0 13V4m0 0L9 7"/></svg>',
    'ubnt_panels'   => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.111 16.404a5.5 5.5 0 017.778 0M12 20h.01m-7.08-7.071c3.904-3.905 10.236-3.905 14.141 0M1.394 9.393c5.857-5.857 15.355-5.857 21.213 0"/></svg>',
    'ubnt_alerts'   => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/></svg>',
    'admin_dashboard'=> '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"/></svg>',
    'admin_users'    => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"/></svg>',
    'admin_plans'    => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"/></svg>',
    'admin_ips'      => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9"/></svg>',
    'admin_logs'     => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 10h16M4 14h16M4 18h16"/></svg>',
    'admin_settings' => '<svg width="18" height="18" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>',
];

$mainMenu = [
    [
        'label' => 'Dashboard',
        'page'  => 'dashboard',
        'href'  => BASE_URL . 'index.php?page=dashboard',
        'match' => ['home', 'dashboard'],
    ],
    [
        'label' => 'Adicionar IP',
        'page'  => 'user_ip_form',
        'href'  => BASE_URL . 'index.php?page=user_ip_form',
        'match' => ['user_ip_form'],
    ],
    [
        'label' => 'Meus IPs',
        'page'  => 'user_ips',
        'href'  => BASE_URL . 'index.php?page=user_ips',
        'match' => ['user_ips'],
    ],
    [
        'label' => 'Logs',
        'page'  => 'user_logs',
        'href'  => BASE_URL . 'index.php?page=user_logs',
        'match' => ['user_logs'],
    ],
    [
        'label' => 'Status Page',
        'page'  => 'status_page',
        'href'  => BASE_URL . 'index.php?page=status_page',
        'match' => ['status_page'],
    ],
    [
        'label' => '🏷️ Tags',
        'page'  => 'user_tags',
        'href'  => BASE_URL . 'index.php?page=user_tags',
        'match' => ['user_tags'],
    ],
    [
        'label' => '🔧 Manutenção',
        'page'  => 'user_maintenance',
        'href'  => BASE_URL . 'index.php?page=user_maintenance',
        'match' => ['user_maintenance'],
    ],
    [
        'label' => '📊 SLA Mensal',
        'page'  => 'user_sla',
        'href'  => BASE_URL . 'index.php?page=user_sla',
        'match' => ['user_sla'],
    ],
    [
        'label' => '🔑 API / Tokens',
        'page'  => 'user_api_tokens',
        'href'  => BASE_URL . 'index.php?page=user_api_tokens',
        'match' => ['user_api_tokens'],
    ],
];

$nmsMenu = [
    [
        'label' => 'Interfaces Monitoradas',
        'page'  => 'nms_dashboard',
        'href'  => BASE_URL . 'mikrotik/index.php?page=nms_dashboard',
        'match' => ['nms_dashboard', 'nms_interface_dashboard'],
    ],
    [
        'label' => 'NMS Dispositivos',
        'page'  => 'nms_devices',
        'href'  => BASE_URL . 'mikrotik/index.php?page=nms_devices',
        'match' => ['nms_devices', 'nms_device_form', 'nms_device_detail'],
    ],
    [
        'label' => 'Alertas NMS',
        'page'  => 'nms_alerts',
        'href'  => BASE_URL . 'mikrotik/index.php?page=nms_alerts',
        'match' => ['nms_alerts'],
    ],
    [
        'label' => 'Mapa',
        'page'  => 'nms_map',
        'href'  => BASE_URL . 'mikrotik/index.php?page=nms_map',
        'match' => ['nms_map'],
    ],
    [
        'label' => 'Painéis Ubiquiti',
        'page'  => 'ubnt_panels',
        'href'  => BASE_URL . 'ubiquiti/index.php?page=ubnt_panels',
        'match' => ['ubnt_panels', 'ubnt_panel_form', 'ubnt_panel_detail'],
    ],
    [
        'label' => 'Alertas Ubiquiti',
        'page'  => 'ubnt_alerts',
        'href'  => BASE_URL . 'ubiquiti/index.php?page=ubnt_alerts',
        'match' => ['ubnt_alerts'],
    ],
];

$adminMenu = [
    [
        'label' => 'Dashboard Admin',
        'page'  => 'admin_dashboard',
        'href'  => BASE_URL . 'index.php?page=admin_dashboard',
        'match' => ['admin_dashboard'],
    ],
    [
        'label' => 'Usuários',
        'page'  => 'admin_users',
        'href'  => BASE_URL . 'index.php?page=admin_users',
        'match' => ['admin_users'],
    ],
    [
        'label' => 'Planos',
        'page'  => 'admin_plans',
        'href'  => BASE_URL . 'index.php?page=admin_plans',
        'match' => ['admin_plans'],
    ],
    [
        'label' => 'IPs Globais',
        'page'  => 'admin_ips',
        'href'  => BASE_URL . 'index.php?page=admin_ips',
        'match' => ['admin_ips'],
    ],
    [
        'label' => 'Logs Globais',
        'page'  => 'admin_logs',
        'href'  => BASE_URL . 'index.php?page=admin_logs',
        'match' => ['admin_logs'],
    ],
    [
        'label' => 'Configurações',
        'page'  => 'admin_settings',
        'href'  => BASE_URL . 'index.php?page=admin_settings',
        'match' => ['admin_settings'],
    ],
    [
        'label' => '💰 Métricas SaaS',
        'page'  => 'admin_saas_metrics',
        'href'  => BASE_URL . 'index.php?page=admin_saas_metrics',
        'match' => ['admin_saas_metrics'],
    ],
    [
        'label' => '💾 Backup',
        'page'  => 'admin_backup',
        'href'  => BASE_URL . 'index.php?page=admin_backup',
        'match' => ['admin_backup', 'admin_backup_download'],
    ],
];

// Badge: contar IPs offline do usuário logado
$offlineBadge = 0;
$nmsOfflineBadge = 0;
if ($isAuthenticated) {
    try {
        $pdo = db();
        $stmt = $pdo->prepare("SELECT COUNT(*) FROM monitored_ips WHERE user_id = ? AND last_status = 'offline' AND active = TRUE");
        $stmt->execute([$user['id']]);
        $offlineBadge = (int)$stmt->fetchColumn();

        $stmtNms = $pdo->prepare("SELECT COUNT(*) FROM nms_devices WHERE user_id = ? AND last_status = 'offline' AND active = TRUE");
        $stmtNms->execute([$user['id']]);
        $nmsOfflineBadge = (int)$stmtNms->fetchColumn();

        try {
            $stmtUbnt = $pdo->prepare("SELECT COUNT(*) FROM ubiquiti_alerts WHERE user_id = ? AND created_at >= NOW() - INTERVAL '24 hours'");
            $stmtUbnt->execute([$user['id']]);
            $ubntAlertBadge = (int)$stmtUbnt->fetchColumn();
        } catch (Throwable $e) { $ubntAlertBadge = 0; }
    } catch (Throwable $e) { $offlineBadge = 0; $nmsOfflineBadge = 0; $ubntAlertBadge = 0; }
}

// Iniciais do usuário para o avatar
$userInitials = '';
foreach (explode(' ', trim($user['name'] ?? '')) as $part) {
    $userInitials .= mb_strtoupper(mb_substr($part, 0, 1));
    if (mb_strlen($userInitials) >= 2) break;
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MonitorPing</title>
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>public/css/styles.css?v=<?php echo filemtime(__DIR__ . '/../public/css/styles.css'); ?>">
    <!-- Favicon -->
    <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><rect width='32' height='32' rx='8' fill='%232563eb'/><path d='M6 16a10 10 0 0020 0' stroke='%2322c55e' stroke-width='2.5' fill='none' stroke-linecap='round'/><circle cx='16' cy='16' r='3' fill='%2322c55e'/><path d='M10 16a6 6 0 0112 0' stroke='white' stroke-width='2' fill='none' stroke-linecap='round'/><path d='M4 16a12 12 0 0124 0' stroke='%2393c5fd' stroke-width='1.5' fill='none' stroke-linecap='round'/></svg>">
    <!-- Dark mode: aplica tema salvo antes do render para evitar flash -->
    <script>
        (function() {
            const t = localStorage.getItem('mp_theme') || 'light';
            document.documentElement.setAttribute('data-theme', t);
        })();
    </script>
</head>
<body>
<div class="layout <?php echo $isAuthenticated ? 'layout-authenticated' : 'layout-public'; ?>">
    <?php if ($isAuthenticated): ?>
        <aside class="sidebar">

            <!-- Logo -->
            <div class="sidebar-logo">
                <div class="sidebar-logo-icon">
                    <svg width="20" height="20" fill="none" stroke="#fff" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.111 16.404a5.5 5.5 0 017.778 0M12 20h.01m-7.08-7.071c3.904-3.905 10.236-3.905 14.141 0M1.394 9.393c5.857-5.857 15.355-5.857 21.213 0"/></svg>
                </div>
                <span>MonitorPing</span>
            </div>

            <!-- Menu principal -->
            <nav class="sidebar-nav">
                <?php foreach ($mainMenu as $item):
                    $active = in_array($page, $item['match'], true) ? 'active' : '';
                    $icon   = $menuIcons[$item['page']] ?? '';
                    $isMeusIps = $item['page'] === 'user_ips';
                ?>
                    <a class="sidebar-link <?php echo $active; ?>" href="<?php echo h($item['href']); ?>">
                        <?php if ($icon): ?><span class="sidebar-link-icon"><?php echo $icon; ?></span><?php endif; ?>
                        <span class="sidebar-link-label"><?php echo h($item['label']); ?></span>
                        <?php if ($isMeusIps && $offlineBadge > 0): ?>
                            <span class="sidebar-badge"><?php echo $offlineBadge; ?></span>
                        <?php endif; ?>
                    </a>
                <?php endforeach; ?>
            </nav>

            <!-- Separador NMS -->
            <div class="sidebar-divider">
                <span>Rede</span>
            </div>
            <nav class="sidebar-nav">
                <?php foreach ($nmsMenu as $item):
                    $active = in_array($page, $item['match'], true) ? 'active' : '';
                    $icon   = $menuIcons[$item['page']] ?? '';
                ?>
                    <a class="sidebar-link <?php echo $active; ?>" href="<?php echo h($item['href']); ?>">
                        <?php if ($icon): ?><span class="sidebar-link-icon"><?php echo $icon; ?></span><?php endif; ?>
                        <span class="sidebar-link-label"><?php echo h($item['label']); ?></span>
                        <?php if ($item['page'] === 'nms_alerts' && $nmsOfflineBadge > 0): ?>
                            <span class="sidebar-badge"><?php echo $nmsOfflineBadge; ?></span>
                        <?php elseif ($item['page'] === 'ubnt_alerts' && ($ubntAlertBadge ?? 0) > 0): ?>
                            <span class="sidebar-badge"><?php echo $ubntAlertBadge; ?></span>
                        <?php endif; ?>
                    </a>
                <?php endforeach; ?>
            </nav>

            <!-- Separador Admin -->
            <?php if ($isAdmin): ?>
                <div class="sidebar-divider sidebar-divider-admin">
                    <span>Administração</span>
                </div>
                <nav class="sidebar-nav">
                    <?php foreach ($adminMenu as $item):
                        $active = in_array($page, $item['match'], true) ? 'active' : '';
                        $icon   = $menuIcons[$item['page']] ?? '';
                    ?>
                        <a class="sidebar-link sidebar-link-admin <?php echo $active; ?>" href="<?php echo h($item['href']); ?>">
                            <?php if ($icon): ?><span class="sidebar-link-icon"><?php echo $icon; ?></span><?php endif; ?>
                            <span class="sidebar-link-label"><?php echo h($item['label']); ?></span>
                        </a>
                    <?php endforeach; ?>
                </nav>
            <?php endif; ?>

            <!-- Footer com avatar -->
            <div class="sidebar-footer">
                <div class="sidebar-user">
                    <div class="sidebar-avatar"><?php echo h($userInitials); ?></div>
                    <div class="sidebar-user-info">
                        <span class="sidebar-user-name"><?php echo h($user['name']); ?></span>
                        <span class="sidebar-user-role"><?php echo $isAdmin ? 'Administrador' : 'Usuário'; ?></span>
                    </div>
                </div>
                <a class="sidebar-link" href="<?php echo BASE_URL; ?>index.php?page=user_profile" style="font-size:0.82rem; opacity:0.8;">
                    <span class="sidebar-link-icon">
                        <svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/></svg>
                    </span>
                    <span class="sidebar-link-label">Meu Perfil</span>
                </a>
                <a class="sidebar-link logout-link" href="<?php echo BASE_URL; ?>index.php?page=logout">
                    <span class="sidebar-link-icon">
                        <svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/></svg>
                    </span>
                    <span class="sidebar-link-label">Sair</span>
                </a>
            </div>

        </aside>
    <?php endif; ?>

    <div class="main">
        <header class="topbar <?php echo $isAuthenticated ? '' : 'topbar-public'; ?>">
            <?php if ($isAuthenticated): ?>
                <div class="topbar-info">
                    <p>Olá, <?php echo h($user['name']); ?> <span class="role-tag"><?php echo h($user['role']); ?></span></p>
                    <?php if ($plan): ?>
                        <small>Plano: <strong><?php echo h($plan['name']); ?></strong> • Até <?php echo (int)$plan['max_ips']; ?> IPs • Intervalo mínimo <?php echo (int)$plan['min_interval_seconds']; ?>s</small>
                    <?php endif; ?>
                </div>
                <div class="topbar-actions">
                    <?php if ($isAdmin): ?>
                        <a class="ghost-button" href="<?php echo BASE_URL; ?>index.php?page=admin_dashboard">Dashboard Admin</a>
                    <?php endif; ?>
                    <span class="chip chip-plan"><?php echo $plan ? h($plan['name']) : 'Sem plano'; ?></span>
                    <span class="chip chip-live">● Live</span>
                    <!-- Dark mode toggle -->
                    <button id="darkToggle" onclick="toggleDark()" title="Alternar tema"
                        style="background:transparent; border:1px solid var(--border); border-radius:8px; padding:6px 10px; cursor:pointer; font-size:1rem; line-height:1; color:var(--text-secondary);">
                        🌙
                    </button>
                </div>
            <?php else: ?>
                <div class="topbar-info">
                    <span class="logo">MonitorPing</span>
                </div>
                <div class="topbar-actions">
                    <a class="ghost-button" href="<?php echo BASE_URL; ?>index.php?page=login">Login</a>
                    <a class="ghost-button" href="<?php echo BASE_URL; ?>index.php?page=register">Registrar</a>
                </div>
            <?php endif; ?>
        </header>
        <main class="container <?php echo $isAuthenticated ? '' : 'container-public'; ?>">
