<?php
session_start();
ini_set("display_errors", 0);
ini_set("display_startup_errors", 0);
error_reporting(E_ALL);

if (!isset($_SESSION["ansnscript_admin"])) {
    header("location:login.php");
    exit;
}

$dbans = new SQLite3("./api/.ansdb.db");
$dbans->exec("CREATE TABLE IF NOT EXISTS ibo(id INTEGER PRIMARY KEY NOT NULL,mac_address VARCHAR(100),key VARCHAR(100),username VARCHAR(100),password VARCHAR(100),expire_date VARCHAR(100),dns VARCHAR(100),epg_url VARCHAR(100),title VARCHAR(100),url VARCHAR(100), type VARCHAR(100))");
$dbans->exec("CREATE TABLE IF NOT EXISTS playlist(id INTEGER PRIMARY KEY NOT NULL,mac_address VARCHAR(100),url VARCHAR(100),name VARCHAR(100))");
$dbans->exec("CREATE TABLE IF NOT EXISTS theme(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(100), url VARCHAR(100))");

$mac_count_row = $dbans->query("SELECT COUNT(*) as count FROM ibo")->fetchArray(SQLITE3_ASSOC);
$mac_count = $mac_count_row['count'] ?? 0;

$dbpans = new SQLite3("./api/.anspanel.db");
$resans = $dbpans->query("SELECT * FROM USERS WHERE ID='1'");
$rowans = $resans ? $resans->fetchArray(SQLITE3_ASSOC) : [];
$nameans = $rowans['NAME'] ?? 'Painel UniTV';
$logoans = $rowans['LOGO'] ?? '';
$logoSrc = !empty($logoans) ? $logoans : 'img/logo.png';
$currentPage = basename($_SERVER['PHP_SELF']);

function navItem($href, $icon, $label, $currentPage) {
    $active = $currentPage === basename($href) ? ' active' : '';
    return '<li class="nav-item"><a class="nav-link'.$active.'" href="'.$href.'"><i class="'.$icon.'"></i><span>'.$label.'</span></a></li>';
}

// Configuração do banco para splash_slider
$db = new SQLite3('./api/.ansdb.db');
$db->exec("CREATE TABLE IF NOT EXISTS splash_slider (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    title TEXT DEFAULT '',
    image_url TEXT NOT NULL,
    sort_order INTEGER DEFAULT 0,
    is_active INTEGER DEFAULT 1,
    created_at TEXT DEFAULT CURRENT_TIMESTAMP
)");
@ $db->exec("ALTER TABLE splash_slider ADD COLUMN title TEXT DEFAULT ''");
@ $db->exec("ALTER TABLE splash_slider ADD COLUMN sort_order INTEGER DEFAULT 0");
@ $db->exec("ALTER TABLE splash_slider ADD COLUMN is_active INTEGER DEFAULT 1");
@ $db->exec("ALTER TABLE splash_slider ADD COLUMN created_at TEXT DEFAULT CURRENT_TIMESTAMP");

function getBaseUrl() {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
    $host = $_SERVER['HTTP_HOST'];
    $currentPath = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
    return $protocol . $host . $currentPath;
}

function uploadSplashImage($file) {
    if (!isset($file) || !is_array($file) || (int)$file['error'] !== 0) return null;
    $allowed = ['image/png', 'image/jpeg', 'image/jpg', 'image/webp', 'image/gif'];
    if (!in_array($file['type'], $allowed, true)) return null;
    $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    if ($ext === 'jpeg') $ext = 'jpg';
    $name = uniqid('splash_', true) . '.' . $ext;
    $dir = 'uploads/splash/';
    if (!is_dir($dir)) mkdir($dir, 0755, true);
    $path = $dir . $name;
    if (move_uploaded_file($file['tmp_name'], $path)) {
        return getBaseUrl() . $path;
    }
    return null;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_slider'])) {
    $title = trim($_POST['title'] ?? '');
    $sortOrder = (int)($_POST['sort_order'] ?? 0);
    $imageUrl = trim($_POST['image_url'] ?? '');
    $uploaded = uploadSplashImage($_FILES['image_file'] ?? null);
    if (!empty($uploaded)) $imageUrl = $uploaded;

    if (!empty($imageUrl)) {
        $stmt = $db->prepare('INSERT INTO splash_slider(title, image_url, sort_order, is_active) VALUES (:title, :image_url, :sort_order, 1)');
        $stmt->bindValue(':title', $title, SQLITE3_TEXT);
        $stmt->bindValue(':image_url', $imageUrl, SQLITE3_TEXT);
        $stmt->bindValue(':sort_order', $sortOrder, SQLITE3_INTEGER);
        $stmt->execute();
    }
    header('Location: splash_slider.php');
    exit;
}

if (isset($_GET['toggle'])) {
    $id = (int)$_GET['toggle'];
    $db->exec("UPDATE splash_slider SET is_active = CASE WHEN is_active = 1 THEN 0 ELSE 1 END WHERE id = {$id}");
    header('Location: splash_slider.php');
    exit;
}

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $db->exec("DELETE FROM splash_slider WHERE id = {$id}");
    header('Location: splash_slider.php');
    exit;
}

if (isset($_GET['move']) && isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    $direction = $_GET['move'] === 'up' ? -1 : 1;
    $current = $db->querySingle("SELECT sort_order FROM splash_slider WHERE id = {$id}");
    $current = is_numeric($current) ? (int)$current : 0;
    $newOrder = max(0, $current + $direction);
    $stmt = $db->prepare('UPDATE splash_slider SET sort_order = :sort_order WHERE id = :id');
    $stmt->bindValue(':sort_order', $newOrder, SQLITE3_INTEGER);
    $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
    $stmt->execute();
    header('Location: splash_slider.php');
    exit;
}

$res = $db->query('SELECT * FROM splash_slider ORDER BY is_active DESC, sort_order ASC, id ASC');
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="google" content="notranslate">
    <title>MAXUNITV</title>
    <link rel="shortcut icon" href="./img/logo.png" type="image/png">
    <link rel="icon" href="./img/logo.png" type="image/png">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/jquery.datetimepicker.min.css"/>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        /* Seus estilos existentes aqui (mantidos) */
        :root {
            --bg-0: #020617;
            --bg-1: #030b2a;
            --bg-2: #071238;
            --card-bg: rgba(8, 17, 51, 0.74);
            --border: rgba(255,255,255,0.08);
            --text-main: #eef2ff;
            --text-soft: #a9b3d4;
            --blue: #2563eb;
            --purple: #7c3aed;
            --red: #ef4444;
            --sidebar-width: 286px;
            --topbar-height: 78px;
        }
        * { box-sizing: border-box; }
        html, body { min-height: 100%; }
        body {
            margin: 0;
            font-family: 'Plus Jakarta Sans', sans-serif;
            background:
                radial-gradient(circle at 78% 19%, rgba(239,68,68,0.30) 0, rgba(239,68,68,0.16) 18%, transparent 42%),
                radial-gradient(circle at 28% 77%, rgba(239,68,68,0.24) 0, rgba(239,68,68,0.10) 15%, transparent 32%),
                radial-gradient(circle at 49% 16%, rgba(37,99,235,0.26) 0, rgba(37,99,235,0.12) 14%, transparent 29%),
                linear-gradient(135deg, var(--bg-0) 0%, var(--bg-1) 42%, var(--bg-2) 100%);
            background-attachment: fixed;
            color: var(--text-main);
            overflow-x: hidden;
        }
        body::before, body::after {
            content: "";
            position: fixed;
            left: -12%; right: -12%; height: 18px; z-index: 0; pointer-events: none; border-radius: 999px; filter: blur(16px);
        }
        body::before {
            top: 27%;
            background: linear-gradient(90deg, rgba(255,45,162,0), rgba(112,52,255,0.95), rgba(255,45,162,0));
            box-shadow: 0 0 28px rgba(124,58,237,0.50);
        }
        body::after {
            top: 63%;
            background: linear-gradient(90deg, rgba(255,45,162,0), rgba(59,130,246,0.88), rgba(255,45,162,0));
            box-shadow: 0 0 24px rgba(59,130,246,0.35);
            opacity: 0.85;
        }
        .theme-orb { position: fixed; border-radius: 50%; pointer-events: none; z-index: 0; opacity: 0.72; }
        .theme-orb.orb-a { width: 540px; height: 540px; top: 74px; right: -175px; background: radial-gradient(circle at 32% 36%, rgba(255,255,255,0.10), rgba(239,68,68,0.86) 0, rgba(80,13,56,0.65) 58%, rgba(12,9,37,0.12) 100%); }
        .theme-orb.orb-b { width: 275px; height: 275px; bottom: 34px; left: 170px; background: radial-gradient(circle at 30% 38%, rgba(255,255,255,0.08), rgba(239,68,68,0.78) 0, rgba(57,9,48,0.70) 64%, transparent 100%); }
        .theme-orb.orb-c { width: 208px; height: 208px; top: 40px; left: 35%; background: radial-gradient(circle at 35% 35%, rgba(255,255,255,0.08), rgba(37,99,235,0.68) 0, rgba(7,19,56,0.72) 70%, transparent 100%); }
        .theme-orb.orb-d { width: 54px; height: 54px; top: 40px; left: 15%; background: radial-gradient(circle, rgba(239,68,68,0.95), rgba(87,11,34,0.62)); }
        .theme-orb.orb-e { width: 34px; height: 34px; top: 166px; left: 10%; background: radial-gradient(circle, rgba(239,68,68,0.95), rgba(87,11,34,0.62)); }
        a { color: inherit; }
        #wrapper { display: flex; min-height: 100vh; position: relative; z-index: 1; }
        .sidebar {
            width: var(--sidebar-width); min-height: 100vh; position: fixed; top: 0; left: 0; z-index: 1100; padding-top: 16px;
            background: linear-gradient(180deg, rgba(2,8,35,0.96) 0%, rgba(3,9,38,0.96) 100%);
            border-right: 1px solid var(--border); backdrop-filter: blur(14px); overflow-y: auto; overflow-x: hidden; box-shadow: 16px 0 45px rgba(0,0,0,0.32);
        }
        .sidebar-brand {
            min-height: 132px; margin: 0 18px 18px; border-radius: 28px; background: linear-gradient(160deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
            border: 1px solid rgba(255,255,255,0.07); display: flex; flex-direction: column; gap: 10px; align-items: center; justify-content: center; text-decoration: none;
        }
        .sidebar-brand img { max-width: 78px; max-height: 78px; border-radius: 22px; background: rgba(255,255,255,0.94); padding: 8px; }
        .brand-copy { text-align: center; line-height: 1.2; }
        .brand-copy strong { display: block; font-size: 1rem; font-weight: 800; color: #fff; }
        .brand-copy span { color: var(--text-soft); font-size: 0.79rem; }
        .sidebar .nav-item { margin: 0 14px 9px; }
        .sidebar .nav-link { border-radius: 16px; min-height: 52px; display: flex; align-items: center; font-size: 0.88rem; font-weight: 700; color: var(--text-main); padding: 10px 14px; background: rgba(255,255,255,0.022); border: 1px solid rgba(255,255,255,0.05); transition: all .22s ease; }
        .sidebar .nav-link i { width: 22px; margin-right: 12px; color: #ff8a00; text-align: center; }
        .sidebar .nav-link:hover, .sidebar .nav-link.active { transform: translateX(4px); background: linear-gradient(90deg, rgba(37,99,235,0.18), rgba(124,58,237,0.18)); border-color: rgba(96,165,250,0.40); color: #fff; }
        .sidebar-divider { border-top: 1px solid rgba(255,255,255,0.08); margin: 14px 18px; }
        .collapse-inner { background: rgba(13, 24, 66, 0.95) !important; border: 1px solid rgba(255,255,255,0.07); border-radius: 18px; padding: 8px; }
        .collapse-item { border-radius: 12px; color: var(--text-main) !important; font-weight: 600; }
        .collapse-item:hover { background: rgba(124,58,237,0.14); }
        #content-wrapper { width: 100%; margin-left: var(--sidebar-width); min-height: 100vh; background: transparent !important; }
        .topbar { min-height: var(--topbar-height); background: rgba(5, 12, 38, 0.78) !important; backdrop-filter: blur(14px); border-bottom: 1px solid rgba(255,255,255,0.08); padding-left: 22px; padding-right: 22px; }
        .topbar .title-box { display: flex; align-items: center; gap: 14px; }
        .mini-logo { width: 46px; height: 46px; border-radius: 16px; object-fit: cover; background: rgba(255,255,255,0.94); padding: 5px; }
        .topbar h5 { color: #fff !important; font-size: 0.95rem; font-weight: 800; margin: 0; line-height: 1.2; }
        .topbar small { color: var(--text-soft) !important; }
        .topbar .badge-danger { background: linear-gradient(90deg, #ef4444, #b91c1c); border: 0; color: #fff; padding: 9px 12px; border-radius: 999px; }
        .topbar-divider { border-right: 1px solid rgba(255,255,255,0.08); }
        main[role="main"], .container-fluid { position: relative; z-index: 1; padding: 24px !important; }
        .card, .modal-content, .table-responsive, .bg-white, .input-group-text, .dropdown-menu, .alert, .pagination .page-link {
            background: var(--card-bg) !important; color: var(--text-main) !important; border: 1px solid rgba(255,255,255,0.08) !important; border-radius: 22px !important; box-shadow: 0 24px 50px rgba(0,0,0,0.18); backdrop-filter: blur(12px);
        }
        .modal-header, .modal-footer { border-color: rgba(255,255,255,0.08) !important; }
        .modal { z-index: 3000 !important; }
        .modal-backdrop { z-index: 2990 !important; }
        .modal-dialog { position: relative; z-index: 3001; pointer-events: none; }
        .modal-content, .modal-header, .modal-body, .modal-footer { pointer-events: auto; }
        .modal.show { display: block; }
        body.modal-open { overflow: hidden; padding-right: 0 !important; }
        #wrapper, #content-wrapper, .sidebar, .topbar, main[role="main"] { isolation: auto; }
        .table { color: var(--text-main); margin-bottom: 0; }
        .table thead th { border-top: 0; border-bottom: 1px solid rgba(255,255,255,0.08); color: #cad3f6 !important; font-size: 0.82rem; letter-spacing: 0.05em; text-transform: uppercase; white-space: nowrap; }
        .table td, .table th { border-top-color: rgba(255,255,255,0.06) !important; vertical-align: middle; }
        .table-striped tbody tr:nth-of-type(odd) { background: rgba(255,255,255,0.018); }
        .table tbody tr:hover { background: rgba(124,58,237,0.08); }
        .form-control, .custom-select, textarea, select { background: rgba(255,255,255,0.04) !important; color: #fff !important; border: 1px solid rgba(255,255,255,0.10) !important; border-radius: 14px !important; min-height: 46px; box-shadow: none !important; }
        .form-control::placeholder { color: #9ba8d0; }
        .form-control:focus, .custom-select:focus, textarea:focus, select:focus { border-color: rgba(96,165,250,0.60) !important; box-shadow: 0 0 0 0.2rem rgba(59,130,246,0.15) !important; }
        .btn { border-radius: 14px; font-weight: 700; border: none; box-shadow: none !important; }
        .btn-primary, .btn-success, .badge-primary, .bg-primary, .progress-bar { background: linear-gradient(90deg, var(--blue), var(--purple)) !important; color: #fff !important; }
        .btn-danger, .badge-danger { background: linear-gradient(90deg, #ef4444, #b91c1c) !important; color: #fff !important; }
        .btn-warning { background: linear-gradient(90deg, #f59e0b, #fb7185) !important; color: #fff !important; }
        .btn-secondary, .btn-dark, .badge-secondary { background: rgba(255,255,255,0.08) !important; color: #fff !important; }
        h1, h2, h3, h4, h5, h6, .text-primary, .text-gray-800 { color: #f8fbff !important; }
        .text-muted, .text-secondary, .small, .text-gray-600, .text-gray-500, label { color: var(--text-soft) !important; }
        .sticky-footer { background: transparent !important; color: var(--text-soft); padding: 20px 0 28px; }
        .scroll-to-top { background: linear-gradient(90deg, var(--blue), var(--purple)) !important; }
        .page-top-card { border-radius: 24px; padding: 22px; background: linear-gradient(135deg, rgba(37,99,235,0.18), rgba(124,58,237,0.18)); border: 1px solid rgba(255,255,255,0.08); }
        .icon-bullet { width: 42px; height: 42px; border-radius: 14px; display: inline-flex; align-items: center; justify-content: center; margin-right: 10px; background: linear-gradient(135deg, rgba(37,99,235,0.25), rgba(124,58,237,0.25)); color: #fff; }
        .step-chip { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 999px; background: rgba(255,255,255,0.06); color: #dfe7ff; font-size: 0.88rem; font-weight: 700; }
        .page-top-card h2 { font-size: 2.1rem; }
        .page-top-card p { font-size: 1rem; }
        .card-header h4 { font-size: 1.8rem; }
        .card-header p { font-size: 1rem; }
        .input-group-text { min-width: 52px; justify-content: center; }
        .table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }
        .table td, .table th { font-size: 0.92rem; padding: 0.78rem 0.7rem; }
        .table .btn { font-size: 0.78rem; padding: 0.42rem 0.7rem; white-space: nowrap; }
        .compact-card { overflow: hidden; }
        .compact-search .form-control { font-size: 0.95rem; }
        .compact-search .input-group-text { border-top-right-radius: 0 !important; border-bottom-right-radius: 0 !important; }
        .compact-search .form-control { border-top-left-radius: 0 !important; border-bottom-left-radius: 0 !important; }

        .manage-table { table-layout: auto; width: 100%; }
        .manage-table th:nth-child(6), .manage-table td:nth-child(6) { min-width: 160px; max-width: 220px; }
        .manage-table th:nth-child(2), .manage-table td:nth-child(2) { min-width: 140px; }
        .manage-table th:nth-child(3), .manage-table td:nth-child(3) { min-width: 100px; }
        .manage-table th:nth-child(4), .manage-table td:nth-child(4) { min-width: 100px; }
        .manage-table th:nth-child(5), .manage-table td:nth-child(5) { min-width: 110px; }
        
        .manage-table .actions-col, .manage-table .actions-cell { 
            min-width: 140px; 
            width: 140px; 
            position: sticky; 
            right: 0; 
            z-index: 10; 
            box-shadow: -8px 0 12px -6px rgba(0,0,0,0.5);
        }
        .manage-table thead .actions-col { 
            background: #0a1236 !important; 
            z-index: 11; 
        }
        .manage-table tbody .actions-cell { 
            background: #071030 !important; 
        }
        
        .manage-table td:nth-child(6) { white-space: normal; word-break: break-all; line-height: 1.2; font-size: 0.85rem; }
        .table-actions { display: flex; align-items: center; justify-content: center; gap: 8px; }
        .action-btn { 
            display: inline-flex; 
            align-items: center; 
            justify-content: center; 
            width: 36px; 
            height: 36px; 
            border-radius: 10px !important;
            padding: 0 !important;
        }
        .action-btn i { font-size: 1rem; }
        .action-btn span { display: none; }

        .table-responsive {
            position: relative;
            border-radius: 15px !important;
            overflow-x: auto;
        }
        .table-responsive::-webkit-scrollbar { height: 6px; }
        .table-responsive::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 10px; }

        .cards-grid {
            display: grid;
            grid-template-columns: 1fr;
            gap: 16px;
            width: 100%;
        }
        
        .access-card {
            border-radius: 18px;
            background: var(--card-bg) !important;
            border: 1px solid rgba(255,255,255,0.08) !important;
            overflow: hidden;
            box-shadow: 0 24px 50px rgba(0,0,0,0.18);
            backdrop-filter: blur(12px);
            transition: all 0.3s ease;
        }
        
        .access-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 32px 60px rgba(0,0,0,0.25);
            border-color: rgba(96,165,250,0.40) !important;
        }
        
        .card-header-mobile {
            padding: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, rgba(37,99,235,0.15), rgba(124,58,237,0.15));
            border-bottom: 1px solid rgba(255,255,255,0.08);
        }
        
        .card-logo {
            width: 60px;
            height: 60px;
            border-radius: 14px;
            object-fit: cover;
            background: rgba(255,255,255,0.94);
            padding: 6px;
            box-shadow: 0 8px 20px rgba(0,0,0,0.3);
        }
        
        .card-body-mobile {
            padding: 18px;
            display: flex;
            flex-direction: column;
            gap: 12px;
        }
        
        .card-row {
            display: flex;
            flex-direction: column;
            gap: 4px;
        }
        
        .card-label {
            font-size: 0.8rem;
            font-weight: 700;
            color: #a9b3d4;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }
        
        .card-value {
            font-size: 0.95rem;
            color: #eef2ff;
            word-break: break-word;
            line-height: 1.3;
        }
        
        .card-dns {
            font-size: 0.85rem;
            color: #60a5fa;
            font-family: 'Courier New', monospace;
        }
        
        .card-footer-mobile {
            padding: 14px 18px;
            display: flex;
            gap: 10px;
            border-top: 1px solid rgba(255,255,255,0.08);
            background: rgba(255,255,255,0.02);
        }
        
        .action-btn-card {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.85rem;
            padding: 0.5rem 0.8rem !important;
            border-radius: 12px !important;
        }
        
        .action-btn-card i {
            margin-right: 6px;
        }

        .manage-table .btn i { margin-right: 0; }
        .step-chip { font-size: 0.82rem; padding: 7px 11px; }
        .page-top-actions .btn { font-size: 0.88rem; padding: 0.55rem 0.9rem; }
        
        /* ESTILOS DO SPLASH SLIDER */
        .splash-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        .splash-card {
            background: rgba(255,255,255,.05);
            border: 1px solid rgba(255,255,255,.09);
            border-radius: 18px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0,0,0,.25);
        }
        .splash-card img {
            width: 100%;
            height: 180px;
            object-fit: cover;
            background: #111;
        }
        .splash-card .body {
            padding: 16px;
        }
        .badge-soft {
            display: inline-block;
            padding: 6px 10px;
            border-radius: 999px;
            font-size: 12px;
            font-weight: 700;
        }
        .badge-on {
            background: #123f24;
            color: #7DFFB1;
        }
        .badge-off {
            background: #402222;
            color: #FFAAAA;
        }
        .control-row {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            margin-top: 14px;
        }
        .glass-box {
            background: rgba(255,255,255,.04);
            border: 1px solid rgba(255,255,255,.08);
            border-radius: 18px;
            padding: 20px;
        }
        
        /* BOTÃO DE UPLOAD PROFISSIONAL */
        .upload-wrapper {
            position: relative;
            display: flex;
            align-items: center;
            gap: 12px;
        }
        
        .upload-btn-icon {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 0 18px;
            height: 46px;
            background: linear-gradient(135deg, #2563eb, #1e40af);
            border-radius: 12px;
            cursor: pointer;
            transition: all 0.2s ease;
            font-weight: 600;
            font-size: 14px;
            color: #fff;
            border: none;
        }
        
        .upload-btn-icon i {
            font-size: 16px;
        }
        
        .upload-btn-icon:hover {
            transform: translateY(-1px);
            box-shadow: 0 6px 16px rgba(37, 99, 235, 0.3);
            background: linear-gradient(135deg, #3b82f6, #2563eb);
        }
        
        .upload-btn-icon:active {
            transform: translateY(0);
        }
        
        #image_file {
            display: none;
        }
        
        .file-info-badge {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 0 12px;
            height: 46px;
            background: rgba(255,255,255,0.05);
            border: 1px solid rgba(255,255,255,0.1);
            border-radius: 12px;
            font-size: 13px;
            color: #a9b3d4;
        }
        
        .file-info-badge i {
            color: #10b981;
            font-size: 14px;
        }
        
        .file-info-badge span {
            max-width: 180px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        
       /* --- RESPONSIVIDADE CORRIGIDA (PC & MOBILE) --- */

/* Ajustes para Telas Médias / Notebooks Pequenos */
@media (max-width: 1200px) {
    :root { --sidebar-width: 250px; }
    .page-top-card h2 { font-size: 1.8rem; }
    .card-header h4 { font-size: 1.45rem; }
    .table td, .table th { font-size: 0.86rem; padding: 0.7rem 0.58rem; }
    .table .btn { font-size: 0.74rem; padding: 0.38rem 0.58rem; }
}

/* Ajustes Críticos para Mobile e Tablets (Onde o Menu Esconde) */
@media (max-width: 991px) {
    .sidebar { 
        transform: translateX(-100%); 
        transition: transform .25s ease; 
        position: fixed !important;
        z-index: 2000 !important;
    }
    
    .sidebar.mobile-open { 
        transform: translateX(0); 
        box-shadow: 20px 0 50px rgba(0,0,0,0.8);
    }

    #content-wrapper { 
        margin-left: 0 !important; 
        width: 100% !important;
        min-width: 100% !important;
    }

    main[role="main"], .container-fluid { padding: 16px !important; }
    .topbar { padding-left: 14px; padding-right: 14px; }
    .topbar .title-box small { display: none; }
    
    .page-top-card { padding: 18px; }
    .page-top-card h2 { font-size: 1.55rem; }
    .card-header h4 { font-size: 1.22rem; }
    
    .sidebar-brand { min-height: 112px; }
    .sidebar-brand img { max-width: 64px; max-height: 64px; }
    .sidebar .nav-link { font-size: 0.84rem; min-height: 48px; }
}

/* Ajustes para Celulares Pequenos */
@media (max-width: 575px) {
    .page-top-card { padding: 15px; border-radius: 18px; }
    .page-top-card h2 { font-size: 1.2rem; }
    .card-header h4 { font-size: 1rem; }
    
    /* Empilha botões no mobile */
    .page-top-actions { 
        display: flex;
        flex-direction: column; 
        width: 100%; 
        gap: 8px;
    }
    .page-top-actions .btn { width: 100%; justify-content: center; }
    
    .step-chip { width: 100%; justify-content: flex-start; }
    
    /* Tabelas no Mobile */
    .table td, .table th { font-size: 0.75rem; padding: 10px 5px; }
    .manage-table .actions-col, .manage-table .actions-cell { 
        min-width: 80px; 
        width: 80px; 
    }
    
    /* Cards de Acesso Mobile */
    .card-body-mobile { padding: 12px; gap: 8px; }
    .card-label { font-size: 0.7rem; }
    .card-value { font-size: 0.85rem; }
    .action-btn-card { font-size: 0.75rem; padding: 8px !important; }
}
        
html { scrollbar-width: thin; scrollbar-color: #00a8e1 #1a2a33; }
/* Chrome, Edge e Safari */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: #1a2a33; border-radius: 10px; }
::-webkit-scrollbar-thumb { background: #00a8e1; border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: #0080b3; }
    </style>
</head>
<body id="page-top">
<div class="theme-orb orb-a"></div>
<div class="theme-orb orb-b"></div>
<div class="theme-orb orb-c"></div>
<div class="theme-orb orb-d"></div>
<div class="theme-orb orb-e"></div>
<div id="wrapper">
    <ul class="navbar-nav sidebar sidebar-dark accordion" id="accordionSidebar">
        <a class="sidebar-brand d-flex align-items-center justify-content-center" href="users.php">
            <div class="sidebar-brand-icon text-center">
                <img class="img-profile" src="<?php echo htmlspecialchars($logoSrc); ?>" alt="Logo">
                <div class="brand-copy mt-2">
                    <strong><?php echo htmlspecialchars($nameans); ?></strong>
                    <span>Admin Dashboard</span>
                </div>
            </div>
        </a>
        <hr class="sidebar-divider my-0">
        <?php echo navItem('users.php', 'fas fa-user-cog', 'Gerenciar Acessos ('.$mac_count.')', $currentPage); ?>
        <?php echo navItem('mac_search.php', 'fas fa-rss', 'Gerenciamento MAC', $currentPage); ?>
        <?php echo navItem('app_connection.php', 'fas fa-lock', 'Conexão Aplicativo', $currentPage); ?>
        <?php echo navItem('note.php', 'fas fa-bullhorn', 'Notificações & QRcode', $currentPage); ?>
        <?php echo navItem('theme.php', 'fas fa-paint-brush', 'Logotipo & Background', $currentPage); ?>
        <?php echo navItem('splash_slider.php', 'fas fa-images', 'Tela de Carregamento', $currentPage); ?>
        <?php echo navItem('profile.php', 'fas fa-user', 'Perfil', $currentPage); ?>
        <li class="nav-item mt-2 mb-4">
            <a class="nav-link" href="logout.php">
                <i class="fas fa-sign-out-alt"></i>
                <span>Sair</span>
            </a>
        </li>
    </ul>

    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <nav class="navbar navbar-expand topbar mb-4 static-top">
                <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3 text-light">
                    <i class="fa fa-bars"></i>
                </button>

                <div class="title-box">
                    <img class="mini-logo" src="<?php echo htmlspecialchars($logoSrc); ?>" alt="Logo pequeno">
                    <div>
                        <h5 class="m-0">Painel</small></h5>
                    </div>
                </div>

                <ul class="navbar-nav ml-auto align-items-center">
                    <div class="topbar-divider d-none d-sm-block"></div>
                    <li class="nav-item dropdown no-arrow mx-1">
                        <a class="nav-link dropdown-toggle" href="logout.php">
                            <span class="badge badge-danger">Sair</span>
                            <i class="fas fa-sign-out-alt fa-sm fa-fw ml-2 text-light"></i>
                        </a>
                    </li>
                </ul>
            </nav>

            <div class="container-fluid">
                <div class="glass-box mb-4">
                    <h3><i class="fas fa-images"></i> Slider da Splash</h3>
                    <p class="text-muted mb-0">As imagens cadastradas aqui vão aparecer no fundo da tela de carregamento do app.</p>
                </div>

                <div class="glass-box mb-4">
                    <form method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="add_slider" value="1">
                        <div class="row">
                            <div class="col-md-4 mb-3">
                                <label class="form-label">Título</label>
                                <input type="text" name="title" class="form-control" placeholder="Título da imagem">
                            </div>
                            <div class="col-md-4 mb-3">
                                <label class="form-label">URL da imagem</label>
                                <input type="text" name="image_url" class="form-control" placeholder="https://exemplo.com/imagem.jpg">
                            </div>
                            <div class="col-md-2 mb-3">
                                <label class="form-label">Ordem</label>
                                <input type="number" name="sort_order" class="form-control" value="0">
                            </div>
                            <div class="col-md-2 mb-3">
                                <label class="form-label">Upload</label>
                                <div class="upload-wrapper">
                                    <label for="image_file" class="upload-btn-icon">
                                        <i class="fas fa-upload"></i>
                                        <span>Envie</span>
                                    </label>
                                    <input type="file" name="image_file" id="image_file" accept="image/*">
                                    <div class="file-info-badge" id="fileInfo" style="display: none;">
                                        <i class="fas fa-check-circle"></i>
                                        <span id="fileName"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-12">
                                <button type="submit" class="btn btn-success">
                                    <i class="fas fa-save"></i> Salvar imagem
                                </button>
                            </div>
                        </div>
                    </form>
                </div>

                <div class="splash-grid">
                    <?php while ($row = $res->fetchArray(SQLITE3_ASSOC)): ?>
                        <div class="splash-card">
                            <img src="<?= htmlspecialchars($row['image_url']) ?>" alt="Splash image">
                            <div class="body">
                                <div class="d-flex justify-content-between align-items-center mb-2">
                                    <strong><?= htmlspecialchars($row['title'] ?: ('Imagem #' . $row['id'])) ?></strong>
                                    <?php if ((int)$row['is_active'] === 1): ?>
                                        <span class="badge-soft badge-on"><i class="fas fa-check-circle"></i> ATIVA</span>
                                    <?php else: ?>
                                        <span class="badge-soft badge-off"><i class="fas fa-circle"></i> INATIVA</span>
                                    <?php endif; ?>
                                </div>
                                <div class="text-muted small"><i class="fas fa-sort-amount-down"></i> Ordem: <?= (int)$row['sort_order'] ?></div>
                                <div class="small text-break mt-2"><i class="fas fa-link"></i> <?= htmlspecialchars($row['image_url']) ?></div>
                                <div class="control-row">
                                    <a class="btn btn-sm btn-primary" href="splash_slider.php?toggle=<?= (int)$row['id'] ?>"><i class="fas fa-power-off"></i> Ativar</a>
                                    <a class="btn btn-sm btn-info" href="splash_slider.php?id=<?= (int)$row['id'] ?>&move=up"><i class="fas fa-arrow-up"></i></a>
                                    <a class="btn btn-sm btn-info" href="splash_slider.php?id=<?= (int)$row['id'] ?>&move=down"><i class="fas fa-arrow-down"></i></a>
                                    <a class="btn btn-sm btn-danger" onclick="return confirm('Deseja remover esta imagem?');" href="splash_slider.php?delete=<?= (int)$row['id'] ?>"><i class="fas fa-trash"></i></a>
                                </div>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // Controle do menu mobile
    $("#sidebarToggleTop").on('click', function(e) {
        e.preventDefault();
        $(".sidebar").toggleClass("mobile-open");
    });
    
    $(document).on('click', function(e) {
        if (!$(e.target).closest('.sidebar').length && !$(e.target).closest('#sidebarToggleTop').length) {
            if ($(".sidebar").hasClass("mobile-open")) {
                $(".sidebar").removeClass("mobile-open");
            }
        }
    });

    // Controle do upload de arquivo
    document.getElementById('image_file').addEventListener('change', function(e) {
        const fileInfo = document.getElementById('fileInfo');
        const fileNameSpan = document.getElementById('fileName');
        
        if (this.files && this.files[0]) {
            const file = this.files[0];
            fileNameSpan.textContent = file.name.length > 30 ? file.name.substring(0, 27) + '...' : file.name;
            fileInfo.style.display = 'inline-flex';
        } else {
            fileInfo.style.display = 'none';
            fileNameSpan.textContent = '';
        }
    });
</script>
</body>
</html>