<?php
// login.php - Pagina de inicio de sesion
// C.E.I. Federico Froebel - SIN ENCRIPTACION
require_once 'db.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');
    
    if (empty($username) || empty($password)) {
        $error = 'Por favor ingrese usuario y contrasena';
    } else {
        // SIN ENCRIPTACION - comparacion directa
        $stmt = $conn->prepare("SELECT id, username, password, nombre_completo, rol FROM usuarios WHERE username = ? AND password = ? AND activo = 1");
        $stmt->bind_param("ss", $username, $password);
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($result->num_rows === 1) {
            $user = $result->fetch_assoc();
            // Login exitoso - crear sesion
            $_SESSION['usuario_id'] = $user['id'];
            $_SESSION['usuario'] = $user['username'];
            $_SESSION['nombre_completo'] = $user['nombre_completo'];
            $_SESSION['rol'] = $user['rol'];
            
            // Actualizar ultimo acceso
            $conn->query("UPDATE usuarios SET ultimo_acceso = NOW() WHERE id = " . $user['id']);
            
            header("Location: dashboard.php");
            exit();
        } else {
            $error = 'Usuario o contrasena incorrectos';
        }
    }
}

// Si ya esta logueado, redirigir al dashboard
if (isset($_SESSION['usuario'])) {
    header("Location: dashboard.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Iniciar Sesion - C.E.I. Federico Froebel</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            position: relative;
        }

        /* ===== FONDO CON IMAGEN DE LA ESCUELA ===== */
        .bg-school {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: url('fondo.jpg') center center / cover no-repeat;
            z-index: -2;
            animation: kenburns 25s ease-in-out infinite alternate;
        }

        @keyframes kenburns {
            0% { transform: scale(1) translate(0, 0); }
            50% { transform: scale(1.08) translate(-1%, -1%); }
            100% { transform: scale(1) translate(0, 0); }
        }

        /* ===== CAPA OSCURA PARA LEGIBILIDAD ===== */
        .bg-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, rgba(26,35,126,0.88) 0%, rgba(49,27,146,0.85) 40%, rgba(0,0,0,0.75) 100%);
            z-index: -1;
        }

        /* ===== PARTICULAS FLOTANTES ===== */
        .particles {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 0;
            pointer-events: none;
            overflow: hidden;
        }

        .particle {
            position: absolute;
            border-radius: 50%;
            background: rgba(255,255,255,0.12);
            animation: float-particle linear infinite;
        }

        .particle:nth-child(1) { width: 8px; height: 8px; left: 10%; top: 100%; animation-duration: 12s; animation-delay: 0s; }
        .particle:nth-child(2) { width: 12px; height: 12px; left: 20%; top: 100%; animation-duration: 15s; animation-delay: 1s; }
        .particle:nth-child(3) { width: 6px; height: 6px; left: 30%; top: 100%; animation-duration: 10s; animation-delay: 2s; }
        .particle:nth-child(4) { width: 15px; height: 15px; left: 40%; top: 100%; animation-duration: 18s; animation-delay: 0.5s; }
        .particle:nth-child(5) { width: 10px; height: 10px; left: 50%; top: 100%; animation-duration: 14s; animation-delay: 3s; }
        .particle:nth-child(6) { width: 7px; height: 7px; left: 60%; top: 100%; animation-duration: 11s; animation-delay: 1.5s; }
        .particle:nth-child(7) { width: 14px; height: 14px; left: 70%; top: 100%; animation-duration: 16s; animation-delay: 2.5s; }
        .particle:nth-child(8) { width: 9px; height: 9px; left: 80%; top: 100%; animation-duration: 13s; animation-delay: 0s; }
        .particle:nth-child(9) { width: 11px; height: 11px; left: 90%; top: 100%; animation-duration: 17s; animation-delay: 4s; }
        .particle:nth-child(10) { width: 8px; height: 8px; left: 5%; top: 100%; animation-duration: 14s; animation-delay: 3.5s; }
        .particle:nth-child(11) { width: 13px; height: 13px; left: 15%; top: 100%; animation-duration: 12s; animation-delay: 5s; }
        .particle:nth-child(12) { width: 6px; height: 6px; left: 85%; top: 100%; animation-duration: 10s; animation-delay: 1s; }

        @keyframes float-particle {
            0% { transform: translateY(0) rotate(0deg); opacity: 0; }
            10% { opacity: 1; }
            90% { opacity: 1; }
            100% { transform: translateY(-120vh) rotate(720deg); opacity: 0; }
        }

        /* ===== ONDAS ANIMADAS ===== */
        .waves {
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 120px;
            z-index: 0;
            pointer-events: none;
        }

        .wave {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 200%;
            height: 100%;
            background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 320'%3E%3Cpath fill='rgba(255,255,255,0.08)' d='M0,192L48,197.3C96,203,192,213,288,229.3C384,245,480,267,576,250.7C672,235,768,181,864,181.3C960,181,1056,235,1152,234.7C1248,235,1344,181,1392,154.7L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z'%3E%3C/path%3E%3C/svg%3E") repeat-x;
            background-size: 50% 100%;
            animation: wave-move 8s linear infinite;
        }

        .wave:nth-child(2) {
            bottom: 10px;
            opacity: 0.5;
            animation-duration: 12s;
            animation-direction: reverse;
        }

        .wave:nth-child(3) {
            bottom: 20px;
            opacity: 0.3;
            animation-duration: 16s;
        }

        @keyframes wave-move {
            0% { transform: translateX(0); }
            100% { transform: translateX(-50%); }
        }

        /* ===== CONTENEDOR PRINCIPAL ===== */
        .login-wrapper {
            display: flex;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 28px;
            overflow: hidden;
            box-shadow: 0 35px 80px rgba(0,0,0,0.35);
            max-width: 960px;
            width: 92%;
            min-height: 560px;
            position: relative;
            z-index: 1;
            animation: slideUp 0.8s ease-out;
            backdrop-filter: blur(10px);
        }

        @keyframes slideUp {
            from { opacity: 0; transform: translateY(40px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* ===== LADO IZQUIERDO - MEMBRETE ===== */
        .login-left {
            flex: 1;
            background: linear-gradient(160deg, #1a237e 0%, #283593 40%, #3949ab 70%, #5c6bc0 100%);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 40px 30px;
            color: white;
            position: relative;
            overflow: hidden;
        }

        .login-left::before {
            content: '';
            position: absolute;
            top: -60%;
            right: -60%;
            width: 250%;
            height: 250%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 60%);
            animation: pulse-glow 5s ease-in-out infinite;
        }

        .login-left::after {
            content: '';
            position: absolute;
            bottom: -30%;
            left: -30%;
            width: 160%;
            height: 160%;
            background: radial-gradient(circle, rgba(255,255,255,0.05) 0%, transparent 50%);
            animation: pulse-glow 7s ease-in-out infinite reverse;
        }

        @keyframes pulse-glow {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.15); opacity: 0.9; }
        }

        .login-left .logo-container {
            position: relative;
            z-index: 1;
            text-align: center;
            animation: fadeInScale 1s ease-out 0.3s both;
        }

        @keyframes fadeInScale {
            from { opacity: 0; transform: scale(0.8); }
            to { opacity: 1; transform: scale(1); }
        }

        /* ===== MEMBRETE ===== */
        .membrete-img {
            width: 220px;
            height: auto;
            max-height: 100px;
            object-fit: contain;
            margin-bottom: 20px;
            border-radius: 12px;
            box-shadow: 0 8px 30px rgba(0,0,0,0.25);
            animation: shimmer 3s ease-in-out infinite;
        }

        @keyframes shimmer {
            0%, 100% { box-shadow: 0 8px 30px rgba(0,0,0,0.25); }
            50% { box-shadow: 0 8px 40px rgba(255,255,255,0.2); }
        }

        .login-left .logo-container img.escudo {
            width: 140px;
            height: 140px;
            object-fit: contain;
            border-radius: 50%;
            border: 4px solid rgba(255,255,255,0.35);
            background: white;
            padding: 8px;
            margin-bottom: 20px;
            box-shadow: 0 12px 45px rgba(0,0,0,0.3);
            animation: float-logo 4s ease-in-out infinite;
        }

        @keyframes float-logo {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-12px); }
        }

        .login-left h2 {
            font-size: 1.6rem;
            font-weight: 700;
            margin-bottom: 8px;
            position: relative;
            z-index: 1;
            text-shadow: 0 2px 10px rgba(0,0,0,0.2);
        }

        .login-left p {
            font-size: 0.9rem;
            opacity: 0.85;
            position: relative;
            z-index: 1;
            font-weight: 300;
        }

        .login-left .sistema-titulo {
            margin-top: 25px;
            padding: 12px 25px;
            background: rgba(255,255,255,0.15);
            border-radius: 25px;
            font-size: 0.8rem;
            font-weight: 500;
            letter-spacing: 2px;
            text-transform: uppercase;
            position: relative;
            z-index: 1;
            border: 1px solid rgba(255,255,255,0.2);
        }

        /* ===== FORMAS FLOTANTES ===== */
        .floating-shapes {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            pointer-events: none;
            overflow: hidden;
        }

        .shape {
            position: absolute;
            border-radius: 50%;
            border: 2px solid rgba(255,255,255,0.08);
        }

        .shape-1 { width: 100px; height: 100px; top: 15%; left: 10%; animation: orbit 12s linear infinite; }
        .shape-2 { width: 150px; height: 150px; top: 65%; left: 65%; animation: orbit 16s linear infinite reverse; }
        .shape-3 { width: 60px; height: 60px; top: 80%; left: 25%; animation: orbit 10s linear infinite 2s; }
        .shape-4 { width: 80px; height: 80px; top: 35%; left: 75%; animation: orbit 14s linear infinite 1s; }

        @keyframes orbit {
            0% { transform: rotate(0deg) translateX(20px) rotate(0deg); }
            100% { transform: rotate(360deg) translateX(20px) rotate(-360deg); }
        }

        /* ===== LADO DERECHO - FORMULARIO ===== */
        .login-right {
            flex: 1;
            padding: 50px 45px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            position: relative;
        }

        .login-right h3 {
            font-size: 2rem;
            color: #1a237e;
            margin-bottom: 8px;
            font-weight: 700;
            animation: slideInLeft 0.6s ease-out 0.4s both;
        }

        @keyframes slideInLeft {
            from { opacity: 0; transform: translateX(-20px); }
            to { opacity: 1; transform: translateX(0); }
        }

        .login-right .subtitle {
            color: #777;
            font-size: 0.9rem;
            margin-bottom: 30px;
            animation: slideInLeft 0.6s ease-out 0.5s both;
        }

        /* ===== FORMULARIO ===== */
        .form-group {
            margin-bottom: 22px;
            animation: slideInLeft 0.5s ease-out both;
        }

        .form-group:nth-child(1) { animation-delay: 0.6s; }
        .form-group:nth-child(2) { animation-delay: 0.7s; }

        .form-group label {
            display: block;
            font-size: 0.85rem;
            font-weight: 600;
            color: #444;
            margin-bottom: 8px;
        }

        .input-icon {
            position: relative;
        }

        .input-icon input {
            width: 100%;
            padding: 16px 16px 16px 50px;
            border: 2.5px solid #e8e8e8;
            border-radius: 14px;
            font-size: 0.95rem;
            font-family: 'Poppins', sans-serif;
            transition: all 0.35s ease;
            background: #fafafa;
        }

        .input-icon input:focus {
            outline: none;
            border-color: #1a237e;
            background: white;
            box-shadow: 0 0 0 5px rgba(26,35,126,0.1);
            transform: translateY(-2px);
        }

        .input-icon .icon {
            position: absolute;
            left: 18px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 1.1rem;
            opacity: 0.5;
            transition: all 0.3s;
        }

        .input-icon input:focus + .icon,
        .input-icon input:focus ~ .icon {
            opacity: 1;
            color: #1a237e;
        }

        /* ===== BOTON LOGIN ===== */
        .btn-login {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #1a237e 0%, #3949ab 50%, #5c6bc0 100%);
            background-size: 200% 200%;
            color: white;
            border: none;
            border-radius: 14px;
            font-size: 1.05rem;
            font-weight: 700;
            font-family: 'Poppins', sans-serif;
            cursor: pointer;
            transition: all 0.4s ease;
            margin-top: 10px;
            box-shadow: 0 6px 25px rgba(26,35,126,0.35);
            animation: gradientShift 4s ease infinite, slideInLeft 0.5s ease-out 0.8s both;
            position: relative;
            overflow: hidden;
        }

        @keyframes gradientShift {
            0%, 100% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
        }

        .btn-login::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
            transition: left 0.5s;
        }

        .btn-login:hover {
            transform: translateY(-3px);
            box-shadow: 0 12px 35px rgba(26,35,126,0.45);
        }

        .btn-login:hover::before {
            left: 100%;
        }

        .btn-login:active {
            transform: translateY(0);
        }

        /* ===== ENLACE REGISTRO ===== */
        .register-link {
            text-align: center;
            margin-top: 22px;
            font-size: 0.85rem;
            color: #666;
            animation: slideInLeft 0.5s ease-out 0.9s both;
        }

        .register-link a {
            color: #1a237e;
            font-weight: 600;
            text-decoration: none;
            position: relative;
            transition: color 0.3s;
        }

        .register-link a::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 0;
            height: 2px;
            background: linear-gradient(90deg, #1a237e, #3949ab);
            transition: width 0.3s ease;
        }

        .register-link a:hover {
            color: #3949ab;
        }

        .register-link a:hover::after {
            width: 100%;
        }

        /* ===== MENSAJE DE ERROR ===== */
        .error-msg {
            background: linear-gradient(135deg, #ffebee 0%, #fce4ec 100%);
            color: #c62828;
            padding: 14px 18px;
            border-radius: 12px;
            font-size: 0.85rem;
            margin-bottom: 22px;
            display: flex;
            align-items: center;
            gap: 10px;
            border-left: 4px solid #c62828;
            animation: shake 0.5s ease, slideInLeft 0.4s ease-out;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            20% { transform: translateX(-8px); }
            40% { transform: translateX(8px); }
            60% { transform: translateX(-4px); }
            80% { transform: translateX(4px); }
        }

        /* ===== FOOTER ===== */
        .footer-login {
            text-align: center;
            margin-top: 25px;
            color: #aaa;
            font-size: 0.75rem;
            animation: slideInLeft 0.5s ease-out 1s both;
        }

        /* ===== RESPONSIVE ===== */
        @media (max-width: 768px) {
            .login-wrapper {
                flex-direction: column;
                max-width: 400px;
                min-height: auto;
            }
            .login-left {
                padding: 25px 20px;
                min-height: 180px;
            }
            .login-left .logo-container img.escudo {
                width: 90px;
                height: 90px;
            }
            .login-left .logo-container img.escudo {
                width: 100px;
                height: 100px;
            }
            .login-left h2 {
                font-size: 1.2rem;
            }
            .login-right {
                padding: 30px 25px;
            }
            .login-right h3 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>

    <!-- Fondo de la escuela -->
    <div class="bg-school"></div>
    <div class="bg-overlay"></div>

    <!-- Particulas flotantes -->
    <div class="particles">
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
    </div>

    <!-- Ondas animadas -->
    <div class="waves">
        <div class="wave"></div>
        <div class="wave"></div>
        <div class="wave"></div>
    </div>

    <!-- Contenedor principal -->
    <div class="login-wrapper">
        <div class="login-left">
            <div class="floating-shapes">
                <div class="shape shape-1"></div>
                <div class="shape shape-2"></div>
                <div class="shape shape-3"></div>
                <div class="shape shape-4"></div>
            </div>
            <div class="logo-container" style="text-align: center;">
                <img src="logoi.jpg" alt="Logo C.E.I. Federico Froebel" class="escudo" onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%22140%22 height=%22140%22%3E%3Crect width=%22140%22 height=%22140%22 fill=%22%23fff%22 rx=%2270%22/%3E%3Ctext x=%2270%22 y=%2285%22 text-anchor=%22middle%22 fill=%22%231a237e%22 font-size=%2245%22 font-family=%22Arial%22 font-weight=%22bold%22%3EFF%3C/text%3E%3C/svg%3E'">
                <h2>C.E.I. Federico Froebel</h2>
                <p>Sistema de Registro de Estudiantes</p>
                <div class="sistema-titulo">Plataforma Educativa</div>
            </div>
        </div>

        <div class="login-right">
            <h3>Bienvenido</h3>
            <p class="subtitle">Ingrese sus credenciales para acceder al sistema</p>

            <?php if ($error): ?>
            <div class="error-msg">
                <span>&#9888;</span> <?= htmlspecialchars($error) ?>
            </div>
            <?php endif; ?>

            <form method="POST" action="" autocomplete="off">
                <div class="form-group">
                    <label>Usuario</label>
                    <div class="input-icon">
                        <input type="text" name="username" placeholder="Ingrese su usuario" required autofocus>
                        <span class="icon">&#128100;</span>
                    </div>
                </div>

                <div class="form-group">
                    <label>Contrasena</label>
                    <div class="input-icon">
                        <input type="password" name="password" placeholder="Ingrese su contrasena" required>
                        <span class="icon">&#128274;</span>
                    </div>
                </div>

                <button type="submit" class="btn-login">Iniciar Sesion</button>
            </form>

            <div class="register-link">
                No tienes una cuenta? <a href="registro.php">Registrate aqui</a>
            </div>

            <p class="footer-login">&copy; <?= date('Y') ?> C.E.I. Federico Froebel<br>Todos los derechos reservados</p>
        </div>
    </div>

</body>
</html>
