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

$json_path = __DIR__ . '/panaci_config.json';
$data = file_exists($json_path) ? json_decode(file_get_contents($json_path), true) : [];

// LÓGICA QUE VOCÊ PEDIU: SE TIVER OFF, CARREGA VAZIO E FECHA EM 1 SEGUNDO
if (!isset($data['webview_status']) || $data['webview_status'] !== 'on') {
    echo '<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body style="background:transparent;">
    <script>
        setTimeout(function() {
            if (window.Android && window.Android.closePopup) {
                window.Android.closePopup();
            }
        }, 100); // 0.1 segundo para ser instantâneo
    </script>
    </body></html>';
    exit();
}

$title_color = isset($data['title_color']) ? $data['title_color'] : '#ffffff';
$desc_color = isset($data['desc_color']) ? $data['desc_color'] : '#94a3b8';
$logo_url = isset($data['logo_url']) ? $data['logo_url'] : '';
?>
<!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">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <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.7) !important; font-family: sans-serif; overflow: hidden; }
        .popup-card { background-color: #0f172a; width: 85%; max-width: 380px; padding: 30px; border-radius: 20px; text-align: center; border: 1px solid #1e293b; animation: zoomIn 0.3s ease-out; }
        @keyframes zoomIn { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
        .logo-img { max-width: 150px; margin-bottom: 20px; display: block; margin: 0 auto 20px; }
        h2 { color: <?= $title_color ?>; margin-bottom: 12px; }
        p { color: <?= $desc_color ?>; line-height: 1.5; }
        .bar { height: 4px; background: #4facfe; width: 100%; animation: cooldown 10s linear forwards; margin-top: 25px; }
        @keyframes cooldown { from { width: 100%; } to { width: 0%; } }
    </style>
</head>
<body>
    <div class="popup-card">
        <?php if (!empty($logo_url)): ?><img src="<?= $logo_url ?>" class="logo-img"><?php endif; ?>
        <h2><?= htmlspecialchars($data['popup_title']) ?></h2>
        <p><?= nl2br(htmlspecialchars($data['popup_description'])) ?></p>
        <div class="bar"></div>
    </div>
    <script>
        setTimeout(function() {
            if (window.Android && window.Android.closePopup) { window.Android.closePopup(); }
        }, 10000);
    </script>
</body>
</html>
