<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");

function limparMac($valor) {
    $limpo = preg_replace('/[^A-Za-z0-9]/', '', $valor);
    return strtoupper(trim($limpo));
}

$json_mac_path = __DIR__ . '/panaci_mensagens_individuais.json';
$mensagens_mac = file_exists($json_mac_path) ? json_decode(file_get_contents($json_mac_path), true) : [];

$mac_recebido_app = isset($_GET['m']) ? $_GET['m'] : '';
$mac_app_final = limparMac($mac_recebido_app);

$msg_encontrada = null;

foreach ($mensagens_mac as $mac_no_json => $dados) {
    if ($mac_app_final === limparMac($mac_no_json)) {
        if ($dados['status'] === 'on') {
            $msg_encontrada = $dados;
        }
        break; 
    }
}

if (!$msg_encontrada) {
    echo '<!DOCTYPE html><html><body style="background:transparent;"><script>
        setTimeout(function() { if (window.Android && window.Android.closePopup) { window.Android.closePopup(); } }, 100);
    </script></body></html>';
    exit();
}

$titulo = $msg_encontrada['titulo'];
$texto = $msg_encontrada['mensagem'];
$cor_titulo = isset($msg_encontrada['cor_titulo']) ? $msg_encontrada['cor_titulo'] : '#38bdf8';
$cor_desc = isset($msg_encontrada['cor_desc']) ? $msg_encontrada['cor_desc'] : '#cbd5e1';
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <style>
        body, html { 
            margin: 0; padding: 0; width: 100%; height: 100%; 
            display: flex; justify-content: center; align-items: center; 
            background-color: rgba(0, 0, 0, 0.8) !important; 
            font-family: sans-serif; overflow: hidden;
            backface-visibility: hidden;
        }

        .popup-card { 
            /* GRADIENTE EXATO DO XML: START, CENTER, END */
            background: linear-gradient(180deg, #1c254e 0%, #182044 50%, #111731 100%); 
            width: 85%; max-width: 380px; 
            padding: 30px; 
            border-radius: 20px; 
            text-align: center; 
            /* BORDA EXATA DO XML (STROKE) */
            border: 1.5pt solid #2d3a75; 
            animation: zoomIn 0.3s ease-out; 
            box-shadow: 0 20px 50px rgba(0,0,0,0.5);
            transform: translateZ(0); 
        }
        
        /* LOGO AUMENTADA PARA MAIOR DESTAQUE */
        .logo-static { width: 180px; height: auto; margin-bottom: 20px; }

        @keyframes zoomIn { 
            from { transform: scale(0.8); opacity: 0; } 
            to { transform: scale(1); opacity: 1; } 
        }
        
        h2 { color: <?= $cor_titulo ?>; margin-bottom: 12px; font-size: 1.4rem; text-transform: uppercase; font-weight: 800; }
        p { color: <?= $cor_desc ?>; line-height: 1.5; font-size: 1.1rem; margin-bottom: 20px; }
        
        /* ESTILO DO CONTADOR */
        .countdown-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            color: #64748b;
            font-size: 0.9rem;
            margin-top: 10px;
        }

        #timer {
            color: <?= $cor_titulo ?>;
            font-weight: bold;
            font-size: 1.2rem;
            background: rgba(255,255,255,0.1);
            padding: 5px 12px;
            border-radius: 10px;
            min-width: 30px;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="popup-card">
        <img src="https://i.postimg.cc/CM7FKyqJ/6953df2eeefbd.png" class="logo-static" alt="Logo">
        
        <h2><?= htmlspecialchars($titulo) ?></h2>
        <p><?= nl2br(htmlspecialchars($texto)) ?></p>
        
        <div class="countdown-wrapper">
            <span>Esta mensagem fechará em</span>
            <span id="timer">10</span>
        </div>
    </div>

    <script>
        let timeLeft = 10;
        const timerElement = document.getElementById('timer');

        const countdown = setInterval(function() {
            timeLeft -= 1;
            timerElement.innerText = timeLeft;

            if (timeLeft <= 0) {
                clearInterval(countdown);
                if (window.Android && window.Android.closePopup) { 
                    window.Android.closePopup(); 
                }
            }
        }, 1000);
    </script>
</body>
</html>