<?php
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['usuario'])) {
    header("Location: ../../login.php?redirect=" . urlencode($_SERVER['REQUEST_URI']));
    exit();
}
if (isset($authApp)) {
    // Inline: no se carga helpers.php aquí para evitar redeclaración si la página ya lo carga
    $__lvl = (int)($_SESSION['userLevel'] ?? 0);
    $__ap  = $_SESSION['userApp'] ?? [];
    $__ok  = $__lvl > 0 && (
        empty($__ap) ||
        !isset($__ap[$authApp]) ||
        $__lvl >= (int)($__ap[$authApp]['acceder_aplicacion'] ?? 99)
    );
    unset($__lvl, $__ap);
    if (!$__ok) {
        unset($__ok);
        http_response_code(403);
        die('<!DOCTYPE html><html lang="es"><head><meta charset="UTF-8"><title>Sin permiso</title>'
          . '<style>body{font-family:Arial,sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;background:#f0f2f5}'
          . '.box{background:#fff;border:1px solid #e2e8f0;border-radius:8px;padding:32px 40px;text-align:center;max-width:400px}'
          . 'h2{color:#dc2626;margin-bottom:8px}p{color:#64748b;font-size:14px;margin-bottom:16px}a{color:#2563eb;text-decoration:none;font-size:13px}'
          . '</style></head><body><div class="box"><h2>Sin permiso</h2>'
          . '<p>No tiene acceso a esta secci&oacute;n.</p>'
          . '<a href="../../index.php">&larr; Volver al inicio</a></div></body></html>');
    }
    unset($__ok);
}
setlocale(LC_TIME, 'es_ES.UTF-8', 'spa');
date_default_timezone_set('America/Argentina/Buenos_Aires');
