<?php
require_once __DIR__ . '/auth.php';

sessionStart();

if (!empty($_SESSION['user_id'])) {
    header('Location: dashboard.php');
    exit;
}

$error   = '';
$expired = !empty($_GET['expired']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username && $password) {
        if (login($username, $password)) {
            header('Location: dashboard.php');
            exit;
        } else {
            $error = 'Usuario o contraseña incorrectos.';
        }
    } else {
        $error = 'Ingrese usuario y contraseña.';
    }
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Acceso – Zero Trust DNS</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <style>
        * { box-sizing: border-box; }

        body {
            margin: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Inter', system-ui, -apple-system, sans-serif;
            background: #070d14;
            overflow: hidden;
        }

        /* Fondo animado con partículas de red */
        .bg-net {
            position: fixed;
            inset: 0;
            background:
                radial-gradient(ellipse 80% 60% at 20% 20%, rgba(220,30,30,0.12) 0%, transparent 60%),
                radial-gradient(ellipse 70% 50% at 80% 80%, rgba(30,100,220,0.15) 0%, transparent 60%),
                radial-gradient(ellipse 100% 80% at 50% 50%, rgba(0,20,50,1) 0%, #070d14 100%);
            z-index: 0;
        }
        .bg-net::before {
            content: '';
            position: absolute;
            inset: 0;
            background-image:
                linear-gradient(rgba(255,255,255,0.025) 1px, transparent 1px),
                linear-gradient(90deg, rgba(255,255,255,0.025) 1px, transparent 1px);
            background-size: 50px 50px;
        }

        /* Card principal */
        .login-wrap {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 460px;
            padding: 1rem;
        }

        .login-card {
            background: rgba(8, 18, 30, 0.85);
            backdrop-filter: blur(24px);
            border: 1px solid rgba(255,255,255,0.08);
            border-radius: 1.5rem;
            padding: 2.5rem 2.25rem;
            box-shadow:
                0 0 0 1px rgba(220,30,30,0.1),
                0 25px 60px rgba(0,0,0,0.6),
                inset 0 1px 0 rgba(255,255,255,0.06);
        }

        /* Logos */
        .logos-row {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 1rem;
            margin-bottom: 2rem;
        }
        .logo-pill {
            background: rgba(255,255,255,0.93);
            border-radius: .875rem;
            padding: .65rem 1.1rem;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 16px rgba(0,0,0,0.35);
        }
        .logo-zt {
            height: 58px;
            width: auto;
        }
        .logo-pcassi {
            height: 36px;
            width: auto;
        }
        .logo-divider {
            width: 1px;
            height: 48px;
            background: linear-gradient(to bottom, transparent, rgba(255,255,255,0.12), transparent);
            flex-shrink: 0;
        }

        /* Títulos */
        .login-title {
            text-align: center;
            margin-bottom: 1.75rem;
        }
        .login-title h1 {
            font-size: 1.5rem;
            font-weight: 700;
            color: #fff;
            margin: 0 0 .3rem;
            letter-spacing: -.01em;
        }
        .login-title p {
            font-size: .82rem;
            color: #4a6080;
            margin: 0;
        }

        /* Divider con texto */
        .divider-label {
            display: flex;
            align-items: center;
            gap: .75rem;
            margin-bottom: 1.25rem;
            color: #2a3a50;
            font-size: .75rem;
            text-transform: uppercase;
            letter-spacing: .08em;
        }
        .divider-label::before, .divider-label::after {
            content: '';
            flex: 1;
            height: 1px;
            background: rgba(255,255,255,0.07);
        }

        /* Inputs */
        .form-label {
            color: #5a7a9a;
            font-size: .8rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: .05em;
            margin-bottom: .4rem;
        }
        .input-group-text {
            background: rgba(255,255,255,0.04) !important;
            border: 1px solid rgba(255,255,255,0.1) !important;
            border-right: none !important;
            color: #3b82f6 !important;
        }
        .form-control {
            background: rgba(255,255,255,0.04) !important;
            border: 1px solid rgba(255,255,255,0.1) !important;
            border-left: none !important;
            color: #e2e8f0 !important;
            padding: .6rem .85rem !important;
        }
        .form-control::placeholder { color: rgba(255,255,255,0.2) !important; }
        .form-control:focus {
            box-shadow: none !important;
            border-color: rgba(59,130,246,0.5) !important;
            background: rgba(59,130,246,0.06) !important;
        }
        .form-control:focus + .input-group-text,
        .input-group:focus-within .input-group-text {
            border-color: rgba(59,130,246,0.5) !important;
        }

        /* Botón */
        .btn-login {
            background: linear-gradient(135deg, #cc1515 0%, #1a3fa0 100%);
            border: none;
            color: #fff;
            font-weight: 700;
            font-size: .95rem;
            letter-spacing: .04em;
            padding: .75rem;
            border-radius: .75rem;
            transition: all .2s;
            position: relative;
            overflow: hidden;
        }
        .btn-login::before {
            content: '';
            position: absolute;
            inset: 0;
            background: linear-gradient(135deg, rgba(255,255,255,0.1), transparent);
            opacity: 0;
            transition: opacity .2s;
        }
        .btn-login:hover { transform: translateY(-1px); box-shadow: 0 8px 24px rgba(220,30,30,0.3); }
        .btn-login:hover::before { opacity: 1; }
        .btn-login:active { transform: translateY(0); }

        /* Footer */
        .login-footer {
            text-align: center;
            margin-top: 1.5rem;
            font-size: .72rem;
            color: #1e2f42;
        }
        .login-footer span { color: #2a4060; }

        /* Alertas */
        .alert { border-radius: .75rem; font-size: .85rem; }
        .alert-danger  { background: rgba(220,53,69,0.15); border-color: rgba(220,53,69,0.3); color: #f87171; }
        .alert-warning { background: rgba(255,193,7,0.12); border-color: rgba(255,193,7,0.25); color: #fcd34d; }

        /* Glow edge en la card */
        .login-card::before {
            content: '';
            position: absolute;
            top: -1px; left: 10%; right: 10%;
            height: 1px;
            background: linear-gradient(90deg, transparent, rgba(220,30,30,0.6), rgba(30,100,220,0.6), transparent);
            border-radius: 50%;
        }
        .login-card { position: relative; }
    </style>
</head>
<body>
<div class="bg-net"></div>

<div class="login-wrap">
    <div class="login-card">

        <!-- Logos -->
        <div class="logos-row">
            <div class="logo-pill">
                <img src="assets/img/zerotrust-logo.png" alt="Zero Trust DNS" class="logo-zt">
            </div>
            <div class="logo-divider"></div>
            <div class="logo-pill">
                <img src="assets/img/pcassi-logo.png" alt="PCASSI Ciberseguridad" class="logo-pcassi">
            </div>
        </div>

        <!-- Título -->
        <div class="login-title">
            <h1>Portal de Clientes</h1>
            <p>Monitoreo de Protección DNS en tiempo real</p>
        </div>

        <?php if ($expired): ?>
            <div class="alert py-2 text-center alert-warning mb-3">
                <i class="bi bi-clock me-1"></i>Sesión expirada. Ingrese nuevamente.
            </div>
        <?php endif; ?>
        <?php if ($error): ?>
            <div class="alert py-2 text-center alert-danger mb-3">
                <i class="bi bi-exclamation-circle me-1"></i><?= htmlspecialchars($error) ?>
            </div>
        <?php endif; ?>

        <div class="divider-label">Acceso seguro</div>

        <form method="post" autocomplete="off">
            <div class="mb-3">
                <label class="form-label">Usuario</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="bi bi-person-fill"></i></span>
                    <input type="text" name="username" class="form-control"
                           placeholder="nombre de usuario" autocomplete="username" required>
                </div>
            </div>
            <div class="mb-4">
                <label class="form-label">Contraseña</label>
                <div class="input-group">
                    <span class="input-group-text"><i class="bi bi-lock-fill"></i></span>
                    <input type="password" name="password" class="form-control"
                           placeholder="••••••••" autocomplete="current-password" required>
                </div>
            </div>
            <button type="submit" class="btn btn-login w-100">
                <i class="bi bi-box-arrow-in-right me-2"></i>INGRESAR
            </button>
        </form>

        <div class="login-footer">
            <i class="bi bi-shield-lock me-1"></i>
            <span>Acceso exclusivo para clientes autorizados de PCASSI</span>
        </div>

    </div>
</div>
</body>
</html>
