<?php
require_once __DIR__.'/config.php';
$current = basename($_SERVER['PHP_SELF'] ?? '');
$isLogged = logged();
$role = $_SESSION['role'] ?? 'admin';
$myNotices = function_exists('count_my_panel_notices') ? count_my_panel_notices() : 0;
function nav_active($files){ global $current; return in_array($current, (array)$files, true) ? 'active' : ''; }
?>
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>PopPlay Admin</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
<style>
:root{--bg:#05070b;--bg2:#080c12;--side:#0b1018;--card:#111823;--card2:#151f2c;--line:#263244;--line2:rgba(255,138,0,.26);--orange:#ff8a00;--orange2:#ff4d00;--yellow:#ffba26;--white:#f6f7fb;--muted:#9faabd;--green:#38e07d;--red:#ff5d6c;--shadow:0 18px 50px rgba(0,0,0,.45);--glow:0 0 24px rgba(255,138,0,.35),0 0 70px rgba(255,77,0,.12)}
*{box-sizing:border-box}html,body{min-height:100%;margin:0}body{background:radial-gradient(circle at 14% 8%,rgba(255,138,0,.08),transparent 24%),linear-gradient(135deg,#05070b,#090e16 45%,#04060a);color:var(--white);font-family:Inter,Arial,Helvetica,sans-serif;overflow-x:hidden}a{color:inherit}.muted{color:var(--muted);font-size:14px}.ok{color:var(--green);font-weight:700}.bad,.err{color:var(--red);font-weight:700}.warn{color:var(--yellow);font-weight:700}
input,select,textarea{width:100%;padding:12px 13px;margin:7px 0 14px;border-radius:11px;border:1px solid #334157;background:#0b111a;color:#fff;outline:none}input:focus,select:focus,textarea:focus{border-color:rgba(255,138,0,.8);box-shadow:0 0 0 3px rgba(255,138,0,.12)}label{display:block;color:#dce1ea;font-weight:700;margin-top:6px}.btn,button.btn{display:inline-flex;align-items:center;gap:7px;background:linear-gradient(90deg,var(--orange),#ffb000);color:#080808!important;padding:10px 15px;border-radius:10px;text-decoration:none;border:0;font-weight:900;cursor:pointer;box-shadow:0 0 18px rgba(255,138,0,.22)}.btn.red{background:#dc3545;color:#fff!important}.btn.gray{background:#566070;color:#fff!important}.btn.dark{background:#202b3b;color:#fff!important;border:1px solid #334157}.card{background:linear-gradient(145deg,rgba(17,24,35,.96),rgba(10,15,23,.94));border:1px solid rgba(255,138,0,.18);border-radius:18px;padding:22px;margin-bottom:18px;box-shadow:var(--shadow)}.card h1,.card h2,.card h3{margin-top:0}table{width:100%;border-collapse:collapse;border-radius:14px;overflow:hidden}td,th{border-bottom:1px solid #263244;padding:12px 10px;text-align:left}th{color:#ffb35c;background:#101722;font-size:13px;text-transform:uppercase;letter-spacing:.5px}tr:hover td{background:rgba(255,138,0,.035)}
/* ADMIN LATERAL */
.admin-shell{display:flex;min-height:100vh}.sidebar{position:fixed;left:0;top:0;bottom:0;width:278px;background:linear-gradient(180deg,#0c111a,#070a10);border-right:1px solid rgba(255,138,0,.22);box-shadow:16px 0 50px rgba(0,0,0,.34);padding:22px 17px;z-index:20}.brand{display:flex;align-items:center;gap:13px;padding:9px 10px 20px;border-bottom:1px solid rgba(255,255,255,.08);margin-bottom:18px}.brand-mark{width:46px;height:46px;border-radius:15px;border:2px solid var(--orange);display:grid;place-items:center;color:var(--orange);font-weight:900;box-shadow:var(--glow)}.brand-title{font-size:20px;font-weight:900;letter-spacing:1.5px;line-height:1}.brand-title span{color:var(--orange)}.brand-sub{font-size:11px;color:var(--muted);text-transform:uppercase;letter-spacing:1.3px;margin-top:4px}.nav-section{color:#69758a;text-transform:uppercase;font-size:11px;font-weight:900;letter-spacing:1.2px;margin:19px 12px 8px}.nav a{display:flex;align-items:center;gap:12px;padding:12px 13px;margin:4px 0;border-radius:13px;color:#d9dee8;text-decoration:none;font-weight:800;border:1px solid transparent}.nav a:hover,.nav a.active{background:linear-gradient(90deg,rgba(255,138,0,.2),rgba(255,138,0,.04));border-color:rgba(255,138,0,.34);color:#fff;box-shadow:0 0 18px rgba(255,138,0,.12)}.nav .sub{margin-left:18px;border-left:1px solid rgba(255,255,255,.08);padding-left:9px}.nav .sub a{font-size:13px;padding:9px 12px;color:#b7c0cf}.content{margin-left:278px;width:calc(100% - 278px);padding:24px}.topbar{display:flex;align-items:center;justify-content:space-between;gap:15px;margin-bottom:22px;background:rgba(17,24,35,.72);border:1px solid rgba(255,138,0,.18);border-radius:18px;padding:17px 20px;box-shadow:var(--shadow);backdrop-filter:blur(8px)}.page-title{margin:0;font-size:24px;font-weight:900;letter-spacing:.3px}.page-title span{color:var(--orange)}.top-actions{display:flex;gap:10px;align-items:center}.mobile-menu-btn{display:none}.grid{display:grid;gap:18px}.stats-grid{grid-template-columns:repeat(4,minmax(0,1fr))}.stat-card{position:relative;overflow:hidden;background:linear-gradient(145deg,#131c28,#0b1018);border:1px solid rgba(255,138,0,.18);border-radius:18px;padding:20px;box-shadow:var(--shadow)}.stat-card:after{content:"";position:absolute;right:-25px;top:-25px;width:92px;height:92px;border-radius:50%;background:rgba(255,138,0,.09)}.stat-icon{font-size:25px}.stat-label{color:var(--muted);font-size:13px;font-weight:800;text-transform:uppercase;margin:10px 0 4px}.stat-value{font-size:32px;font-weight:900}.stat-note{color:#c4ccd8;font-size:13px}.dash-grid{grid-template-columns:1.4fr .9fr}.quick-actions{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}.quick-actions a{background:#0c121c;border:1px solid #263244;border-radius:14px;padding:16px;text-decoration:none;font-weight:900}.quick-actions a:hover{border-color:rgba(255,138,0,.55);box-shadow:0 0 18px rgba(255,138,0,.12)}.pill{display:inline-flex;padding:5px 9px;border-radius:999px;background:rgba(255,138,0,.14);color:#ffb35c;font-size:12px;font-weight:900;border:1px solid rgba(255,138,0,.22)}
/* LOGIN CORRIGIDO - RESPONSIVO */
.wrap{max-width:1180px;margin:0 auto;padding:22px}
.login-screen{min-height:100vh;background:radial-gradient(circle at 18% 50%,rgba(255,128,0,.14),transparent 28%),radial-gradient(circle at 69% 39%,rgba(255,97,0,.12),transparent 22%),linear-gradient(130deg,#05070b 0%,#0a0f17 50%,#030509 100%);overflow-x:hidden;overflow-y:auto}
.login-screen:before{content:"";position:fixed;inset:0;background:repeating-linear-gradient(45deg,rgba(255,255,255,.025) 0 1px,transparent 1px 18px);opacity:.55;pointer-events:none}
.login-screen .wrap{width:100%;max-width:none;min-height:100vh;padding:0;display:flex;flex-direction:column}
.login-page{position:relative;z-index:2;flex:1;width:min(1180px,94vw);margin:0 auto;padding:38px 0 26px;display:grid;grid-template-columns:minmax(280px,1fr) minmax(360px,480px);gap:clamp(28px,6vw,74px);align-items:center}
.brand-panel{min-width:0;padding:10px 0}
.play-mark{width:104px;height:104px;margin:0 0 28px clamp(20px,10vw,150px);position:relative;border:5px solid var(--orange);border-radius:28px;transform:rotate(30deg);box-shadow:0 0 22px rgba(255,128,0,.9),inset 0 0 20px rgba(255,128,0,.28)}
.play-mark:after{content:"";position:absolute;left:38px;top:28px;border-left:32px solid var(--orange);border-top:21px solid transparent;border-bottom:21px solid transparent;transform:rotate(-30deg)}
.logo-text{font-size:clamp(40px,5vw,56px);line-height:.92;font-weight:900;letter-spacing:3px;text-transform:uppercase}
.logo-text span:last-child{color:var(--orange)}
.admin-sub{margin:22px 0 0 clamp(0px,3vw,40px);letter-spacing:clamp(3px,1vw,10px);color:#d2d5db;font-size:clamp(14px,2vw,20px);text-transform:uppercase;white-space:normal}
.neon-line{width:min(340px,100%);height:2px;margin:30px 0 30px clamp(0px,3vw,40px);background:linear-gradient(90deg,transparent,var(--orange),transparent);box-shadow:0 0 14px var(--orange)}
.brand-panel>.brand-title,.brand-bullets{margin-left:clamp(0px,3vw,40px)}
.brand-panel>.brand-title{font-size:clamp(18px,2.4vw,26px);font-weight:900;letter-spacing:.8px}
.brand-bullets{font-size:clamp(16px,2.3vw,23px);font-weight:700;color:#ff9400}
.ghost-play{display:none}
.login-card{position:relative;width:100%;background:linear-gradient(145deg,rgba(21,27,35,.94),rgba(7,10,15,.9));border:1px solid rgba(255,128,0,.72);border-radius:24px;padding:clamp(28px,5vw,52px) clamp(22px,4vw,34px) 32px;box-shadow:var(--glow);min-height:auto}
.login-icon{display:flex;justify-content:center;color:var(--orange);margin-bottom:8px}
.login-card h1{text-align:center;margin:0 0 10px;font-size:clamp(26px,4vw,38px)}
.login-card h1 span{color:var(--orange)}
.small-line{width:86px;height:2px;background:linear-gradient(90deg,transparent,var(--orange),transparent);margin:0 auto 14px;box-shadow:0 0 12px var(--orange)}
.login-card p{text-align:center;color:#c4c8d0;margin:0 0 24px}
.field{position:relative;margin-bottom:18px}
.field svg{position:absolute;left:20px;top:50%;transform:translateY(-50%);width:24px;height:24px;stroke:var(--orange);z-index:1}
.field input{height:62px;margin:0;padding-left:62px;padding-right:48px;border-radius:14px;background:#0a1018;border:1px solid rgba(255,138,0,.28);font-size:16px}
.eye{position:absolute;right:20px;top:50%;transform:translateY(-50%);color:#aeb3bd}
.login-options{display:flex;justify-content:space-between;gap:12px;margin:4px 0 22px;color:#c6cad2;font-size:14px;flex-wrap:wrap}.login-options a{text-decoration:none;color:#ffb35c}
.check{display:flex;align-items:center;gap:7px}.box{width:14px;height:14px;border:1px solid rgba(255,138,0,.7);border-radius:4px;display:inline-block}
.login-btn{width:100%;height:62px;border:0;border-radius:12px;background:linear-gradient(90deg,#ff8a00,#ffb000);color:#050505;font-size:19px;font-weight:900;cursor:pointer;box-shadow:0 0 22px rgba(255,138,0,.28)}
.secure-divider,.secure-text{text-align:center;color:#aeb3bd}.secure-divider{margin:18px 0 6px}.secure-text{font-size:13px}
.login-footer{position:relative;z-index:3;width:max-content;max-width:calc(100% - 30px);margin:0 auto 18px;padding:12px 24px;border:1px solid rgba(255,255,255,.14);border-radius:12px;background:rgba(10,14,20,.64);color:#aeb3bd;font-size:14px;text-align:center}
.login-error{color:#ff7676!important;font-weight:700;background:rgba(255,93,108,.09);border:1px solid rgba(255,93,108,.22);padding:10px;border-radius:10px}
@media(max-width:980px){.sidebar{transform:translateX(-100%);transition:.25s}.sidebar.open{transform:translateX(0)}.content{margin-left:0;width:100%;padding:16px}.mobile-menu-btn{display:inline-flex}.stats-grid,.dash-grid{grid-template-columns:1fr}.quick-actions{grid-template-columns:1fr}.topbar{align-items:flex-start}.login-page{grid-template-columns:1fr;gap:24px;padding:34px 0 16px}.brand-panel{text-align:center;padding:0}.play-mark{margin:0 auto 24px}.admin-sub,.brand-panel>.brand-title,.brand-bullets{margin-left:0}.neon-line{margin-left:auto;margin-right:auto}.login-card{max-width:520px;width:100%;margin:0 auto}.login-footer{position:relative;width:auto;text-align:center;margin-bottom:16px}.login-options{justify-content:center}.field input{height:58px}.login-btn{height:58px}}
@media(max-width:520px){.login-page{width:92vw;padding-top:22px}.brand-panel{display:none}.login-card{padding:26px 18px 24px;border-radius:20px}.login-icon svg{width:44px;height:44px}.login-card p{margin-bottom:18px}.login-options{font-size:13px}.login-footer{font-size:12px;padding:10px 14px}}
.profile-menu{position:relative}.profile-btn{display:inline-flex;align-items:center;gap:8px}.profile-drop{display:none;position:absolute;right:0;top:46px;min-width:190px;background:#0b111a;border:1px solid rgba(255,138,0,.35);border-radius:14px;box-shadow:0 18px 45px rgba(0,0,0,.45);padding:8px;z-index:999}.profile-menu:hover .profile-drop{display:block}.profile-drop a{display:block;text-decoration:none;padding:11px 12px;border-radius:10px;color:#fff;font-weight:800}.profile-drop a:hover{background:rgba(255,138,0,.18)}

/* MENU PREMIUM LARANJA - MODELO APROVADO */
.sidebar{
    width:285px!important;
    padding:22px 14px!important;
    background:radial-gradient(circle at 35% 0%,rgba(255,138,0,.10),transparent 34%),linear-gradient(180deg,#080d14,#04070d)!important;
    border-right:2px solid rgba(255,138,0,.95)!important;
    border-top-right-radius:18px!important;
    border-bottom-right-radius:18px!important;
    box-shadow:0 0 28px rgba(255,138,0,.35),18px 0 55px rgba(0,0,0,.45)!important;
    overflow-y:auto!important;
}
.sidebar::-webkit-scrollbar{width:6px}
.sidebar::-webkit-scrollbar-thumb{background:rgba(255,138,0,.45);border-radius:999px}
.brand{
    padding:8px 8px 22px!important;
    margin-bottom:18px!important;
    border-bottom:1px solid rgba(255,255,255,.10)!important;
}
.brand-mark{
    width:48px!important;height:48px!important;
    border-radius:14px!important;
    border:2px solid #ff8a00!important;
    color:#ff8a00!important;
    box-shadow:0 0 18px rgba(255,138,0,.55)!important;
}
.brand-title{font-size:21px!important;font-weight:950!important;letter-spacing:.7px!important}
.brand-sub{font-size:11px!important;letter-spacing:1.5px!important;color:#c9d0dc!important}
.nav-section{
    margin:20px 10px 9px!important;
    color:#9faabd!important;
    font-size:11px!important;
    font-weight:900!important;
    letter-spacing:1.4px!important;
}
.nav a{
    min-height:48px!important;
    margin:8px 0!important;
    padding:13px 14px!important;
    border-radius:11px!important;
    background:linear-gradient(180deg,rgba(22,30,42,.92),rgba(13,19,29,.92))!important;
    border:1px solid rgba(255,255,255,.055)!important;
    color:#f4f7fb!important;
    font-weight:850!important;
    box-shadow:inset 0 1px 0 rgba(255,255,255,.035),0 8px 18px rgba(0,0,0,.22)!important;
}
.nav a:hover{
    transform:translateX(2px);
    border-color:rgba(255,138,0,.55)!important;
    box-shadow:0 0 18px rgba(255,138,0,.25),inset 0 1px 0 rgba(255,255,255,.07)!important;
}
.nav a.active{
    color:#fff!important;
    background:linear-gradient(90deg,#ff8a00,#ff5c00)!important;
    border-color:#ff9d2e!important;
    box-shadow:0 0 22px rgba(255,138,0,.55)!important;
}
.nav a.active::first-letter{color:#fff}
.nav a[href*="dns"]::after,
.nav a[href*="avisos.php"]::after,
.nav a[href*="app_aparencia"]::after,
.nav a[href*="logo_tela"]::after,
.nav a[href*="clientes_online"]::after,
.nav a[href*="revendas"]::after,
.nav a[href*="sub_revendas"]::after,
.nav a[href*="clientes_macs"]::after,
.nav a[href*="link_japones"]::after{
    content:'›';
    margin-left:auto;
    color:#cbd3df;
    font-size:24px;
    line-height:1;
}
.nav .sub{
    margin:2px 0 10px 28px!important;
    padding:4px 0 4px 16px!important;
    border-left:2px solid rgba(255,138,0,.75)!important;
}
.nav .sub a{
    background:transparent!important;
    border:0!important;
    box-shadow:none!important;
    min-height:34px!important;
    margin:2px 0!important;
    padding:7px 10px!important;
    color:#e2e8f1!important;
    font-size:13px!important;
}
.nav .sub a:before{
    content:'●';
    color:#ff8a00;
    margin-right:8px;
}
.content{margin-left:285px!important;width:calc(100% - 285px)!important}
@media(max-width:900px){
    .sidebar{transform:translateX(-105%);transition:.25s}
    .sidebar.open{transform:translateX(0)}
    .content{margin-left:0!important;width:100%!important}
}

</style>
<script>
function toggleSidebar(){var s=document.querySelector('.sidebar'); if(s){s.classList.toggle('open');}}
</script>
</head>
<body class="<?= $isLogged ? 'admin-screen' : (($current==='index.php') ? 'login-screen' : '') ?>">
<?php if($isLogged): ?>
<div class="admin-shell">
    <aside class="sidebar">
        <div class="brand"><div class="brand-mark">▶</div><div><div class="brand-title">POP<span>PLAY</span></div><div class="brand-sub">Painel Admin</div></div></div>
        <nav class="nav">
            <div class="nav-section">Principal</div>
            <a class="<?=nav_active(['dashboard.php'])?>" href="dashboard.php">🏠 Dashboard</a>
            <a class="<?=nav_active(['dns.php','dns_create.php'])?>" href="dns.php">🌐 DNS</a>
            <div class="nav-section">Funções adicionadas</div>
            <?php if(($role ?? 'admin') === 'admin'): ?>
            <a class="<?=nav_active(['avisos.php'])?>" href="avisos.php">📢 Avisos do APK</a>
            <a class="<?=nav_active(['aviso_vencimento.php'])?>" href="aviso_vencimento.php">⏰ Aviso Vencimento</a>
            <div class="sub">
                <a href="avisos.php#novo">➕ Novo Aviso APK</a>
                <a href="avisos.php#historico">📜 Histórico APK</a>
                <a href="avisos.php#limpeza">🗑️ Limpeza Automática</a>
            </div>
            <a class="<?=nav_active(['avisos_painel.php'])?>" href="avisos_painel.php">📨 Avisos Revendas</a>
            <?php else: ?>
            <a class="<?=nav_active(['meus_avisos.php'])?>" href="meus_avisos.php">📨 Meus Avisos <?=($myNotices>0?'('.(int)$myNotices.')':'')?></a>
            <?php endif; ?>
            <a class="<?=nav_active(['logo_tela.php'])?>" href="logo_tela.php">🖼️ Logo na Tela</a>
            <a class="<?=nav_active(['app_aparencia.php'])?>" href="app_aparencia.php">🎨 Aparência APK</a>
            <a class="<?=nav_active(['clientes_online.php'])?>" href="clientes_online.php">📡 Clientes Online</a>
            <a class="<?=nav_active(['revendas.php'])?>" href="revendas.php">🤝 Revendas</a>
            <a class="<?=nav_active(['sub_revendas.php'])?>" href="sub_revendas.php">🤝 Sub Revendas</a>
            <a class="<?=nav_active(['clientes_macs.php'])?>" href="clientes_macs.php">🔐 Dispositivos APK</a>
            <a class="<?=nav_active(['link_japones.php'])?>" href="link_japones.php">㊙️ Link Japonês</a>
            <?php if(is_admin()): ?><div class="nav-section">Sistema</div><?php endif; ?>
            <?php if(is_admin()): ?><a class="<?=nav_active(['user.php'])?>" href="user.php">👤 Usuário</a><?php endif; ?>
        </nav>
    </aside>
    <main class="content">
        <div class="topbar"><div><button class="btn dark mobile-menu-btn" onclick="toggleSidebar()" type="button">☰</button><h1 class="page-title">POPPLAY <span>Admin</span></h1><div class="muted">Painel de controle das funções do aplicativo</div></div><div class="top-actions"><?php if(($role ?? 'admin') === 'admin'): ?><a class="btn dark" href="avisos_painel.php">📨 Avisos Revendas</a><a class="btn" href="logo_tela.php">🖼️ Logo</a><?php else: ?><a class="btn dark" href="meus_avisos.php">📨 Meus Avisos <?=($myNotices>0?'('.(int)$myNotices.')':'')?></a><?php endif; ?><div class="profile-menu"><a class="btn dark profile-btn" href="javascript:void(0)">👤 <?=h($_SESSION['name'] ?? 'Perfil')?> ▼</a><div class="profile-drop"><a href="perfil.php">🔑 Trocar Senha</a><a href="logout.php">🚪 Sair</a></div></div></div></div>
<?php else: ?>
<div class="wrap">
<?php endif; ?>
