<?php
declare(strict_types=1);

require __DIR__ . '/src/bootstrap.php';

if (is_logged_in()) {
    redirect('vote.php');
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();
    
    $email = normalize_email($_POST['email'] ?? '');
    
    if ($email === '') {
        $error = 'Por favor, insira um e-mail válido.';
    } elseif (email_is_allowed($email)) {
        $_SESSION['user_email'] = $email;
        $_SESSION['is_admin'] = email_is_admin($email);
        
        redirect('vote.php');
    } else {
        $error = 'Acesso não autorizado para este e-mail.';
    }
}
?>
<!DOCTYPE html>
<html lang="pt">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Entrar - Fábrica dos Ovos</title>
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;600;800&display=swap" rel="stylesheet">
    <style>
        html { width: 100vw; overflow-x: hidden; margin: 0; padding: 0; }
        :root {
            --primary-color: #fd8b2b;
            --secondary-color: #ffc273;
            --accent-color: #e56b10;
            --bg-color: #fdfbfb;
            --text-main: #4a4a4a;
            --error-color: #e63946;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Outfit', sans-serif;
        }

        body {
            min-height: 100vh;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #fce0cd 0%, #ffead4 100%);
            overflow-x: hidden;
            overflow-y: auto;
            position: relative;
            margin: 0;
            padding: 0;
        }

        /* Animated Background Elements */
        .bg-shapes {
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            z-index: 0;
            overflow: hidden;
            pointer-events: none;
        }

        .blob {
            position: absolute;
            filter: blur(60px);
            z-index: 0;
            opacity: 0.6;
            animation: float 10s infinite ease-in-out;
        }

        .blob-1 {
            width: 400px;
            height: 400px;
            background: var(--primary-color);
            top: -100px;
            left: -100px;
            border-radius: 50%;
        }

        .blob-2 {
            width: 300px;
            height: 300px;
            background: var(--accent-color);
            bottom: -50px;
            right: -50px;
            border-radius: 50%;
            animation-delay: -3s;
        }
        
        .blob-3 {
            width: 250px;
            height: 250px;
            background: #ffaf55;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            border-radius: 50%;
            animation-delay: -5s;
        }

        @keyframes float {
            0%, 100% { transform: translate(0, 0) scale(1); }
            33% { transform: translate(30px, -50px) scale(1.1); }
            66% { transform: translate(-20px, 20px) scale(0.9); }
        }

        /* Floating Easter Eggs */
        .floating-egg {
            position: absolute;
            z-index: 1;
            animation: float-egg 15s infinite ease-in-out;
            opacity: 0.8;
            filter: drop-shadow(0 10px 15px rgba(0,0,0,0.1));
        }

        @keyframes float-egg {
            0%, 100% { transform: translateY(0) rotate(0deg); }
            50% { transform: translateY(-30px) rotate(10deg); }
        }

        .egg-1 { top: -5%; left: -5%; width: 400px; animation-delay: 0s; }
        .egg-2 { top: 5%; right: -5%; width: 450px; animation-delay: -2s; }
        .egg-3 { bottom: -5%; left: -5%; width: 380px; animation-delay: -5s; }
        .egg-4 { bottom: -2%; right: -2%; width: 350px; animation-delay: -7s; }
        .egg-5 { top: 35%; left: 15%; width: 280px; animation-delay: -3s; opacity: 0.7; }
        .egg-6 { top: 40%; right: 18%; width: 260px; animation-delay: -9s; opacity: 0.7; }
        .egg-7 { top: 12%; left: 40%; width: 220px; animation-delay: -1s; opacity: 0.5; }
        .egg-8 { bottom: 15%; right: 42%; width: 240px; animation-delay: -11s; opacity: 0.5; }
        .egg-9 { top: 65%; left: 25%; width: 200px; animation-delay: -4s; opacity: 0.6; }
        .egg-10 { top: 18%; right: 30%; width: 190px; animation-delay: -13s; opacity: 0.6; }
        .egg-11 { bottom: 35%; left: 45%; width: 170px; animation-delay: -6s; opacity: 0.4; }
        .egg-12 { top: 50%; left: 5%; width: 230px; animation-delay: -15s; opacity: 0.7; }

        @media (max-width: 600px) {
            .egg-1, .egg-2, .egg-3, .egg-4 { width: 120px !important; opacity: 0.6 !important; }
            .egg-5, .egg-6, .egg-7, .egg-8 { width: 80px !important; opacity: 0.5 !important; }
            .egg-9, .egg-10, .egg-11, .egg-12 { display: none !important; }
            
            /* Ajustar posições para não fugirem do ecrã mobile */
            .egg-1 { top: 2%; left: -2%; }
            .egg-2 { top: 10%; right: -2%; }
            .egg-3 { bottom: 2%; left: -2%; }
            .egg-4 { bottom: 10%; right: -2%; }
        }

        /* Login Card */
        .login-wrapper {
            position: relative;
            z-index: 10;
            width: 100%;
            max-width: 450px;
            padding: 20px;
            transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        }

        .login-card {
            background: linear-gradient(135deg, rgba(92, 58, 33, 0.95), rgba(58, 34, 17, 0.95));
            backdrop-filter: blur(20px);
            padding: 3.5rem 2.5rem;
            border-radius: 30px;
            box-shadow: 0 25px 50px rgba(0,0,0,0.3);
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.1);
            position: relative;
            overflow: visible;
            transform: translateY(0);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .login-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 30px 50px rgba(0, 0, 0, 0.3),
                        inset 0 0 0 1px rgba(255, 255, 255, 0.2);
        }

        .logo-container {
            margin-top: -6rem;
            margin-bottom: 0.5rem;
            position: relative;
            display: flex;
            align-items: flex-end;
            justify-content: center;
            z-index: 30;
        }

        @media (max-width: 600px) {
            .logo-container { margin-top: -5rem; margin-bottom: 0px; }
        }

        .logo {
            width: 100%;
            max-width: 550px;
            height: auto;
            position: relative;
            z-index: 1;
            animation: bounce-in 1s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            filter: drop-shadow(0 15px 25px rgba(0,0,0,0.3));
        }

        @media (max-width: 600px) {
            body { 
                overflow-x: hidden !important;
                overflow-y: auto !important; 
                padding: 40px 10px !important;
                display: flex !important;
                align-items: center !important;
            }
            .login-wrapper { padding: 0 !important; max-width: 100% !important; }
            .login-card { padding: 2rem 1.2rem 3rem 1.2rem !important; border-radius: 20px !important; }
            .hanging-pascoa { width: 180px !important; bottom: -50px !important; }
            .logo { max-width: 380px !important; width: 110% !important; margin-left: -5% !important; } 
            .logo-header { height: 100px !important; }
            .title { font-size: 1.3rem !important; margin-bottom: 0.2rem !important; }
            .subtitle { font-size: 0.8rem !important; margin-bottom: 1.2rem !important; }
            .input-field { padding: 0.8rem 1rem !important; font-size: 0.9rem !important; }
            .submit-btn { padding: 0.8rem !important; font-size: 1rem !important; }
        }

        .hanging-pascoa {
            position: absolute;
            bottom: -90px;
            left: 50%;
            transform: translateX(-50%);
            width: 320px;
            z-index: 20;
            filter: drop-shadow(0 10px 15px rgba(0,0,0,0.15));
            animation: bounce-in 1s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            animation-delay: 0.2s;
            animation-fill-mode: both;
            pointer-events: none;
        }

        @keyframes bounce-in {
            0% { transform: scale(0); opacity: 0; }
            50% { transform: scale(1.1); }
            100% { transform: scale(1); opacity: 1; }
        }

        .title {
            color: var(--chocolate-bg);
            font-size: 1.8rem;
            font-weight: 800;
            margin-bottom: 0.5rem;
            letter-spacing: -0.5px;
        }
        
        .subtitle {
            color: #888;
            font-size: 0.95rem;
            margin-bottom: 2rem;
            font-weight: 400;
        }

        .form-group {
            position: relative;
            margin-bottom: 1.2rem;
            text-align: left;
        }

        .input-label {
            display: block;
            margin-bottom: 0.4rem;
            color: var(--chocolate-bg);
            font-weight: 700;
            font-size: 0.9rem;
            margin-left: 0.2rem;
        }

        .input-field {
            width: 100%;
            padding: 1rem 1.2rem;
            border: 2px solid rgba(0,0,0,0.05);
            border-radius: 15px;
            background: rgba(255,255,255,0.9);
            font-size: 1rem;
            color: var(--text-main);
            transition: all 0.3s ease;
            outline: none;
            font-family: inherit;
        }

        .input-field:focus {
            border-color: var(--accent-color);
            background: #fff;
            box-shadow: 0 0 0 4px rgba(229, 107, 16, 0.15);
        }

        .submit-btn {
            width: 100%;
            padding: 1rem;
            border: none;
            border-radius: 15px;
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--accent-color) 100%);
            color: white;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            letter-spacing: 0.5px;
            margin-top: 1rem;
            box-shadow: 0 10px 20px rgba(229, 107, 16, 0.3);
        }

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

        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 15px 25px rgba(229, 107, 16, 0.4);
        }

        .submit-btn:hover::after {
            left: 100%;
        }

        .submit-btn:active {
            transform: translateY(1px);
        }

        .error-message {
            background: rgba(255, 107, 107, 0.1);
            color: var(--error-color);
            padding: 0.8rem;
            border-radius: 12px;
            margin-bottom: 1.5rem;
            font-size: 0.9rem;
            font-weight: 600;
            display: flex;
            align-items: center;
            justify-content: center;
            animation: shake 0.5s ease-in-out;
            border: 1px solid rgba(255, 107, 107, 0.2);
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-5px); }
            75% { transform: translateX(5px); }
        }

        .footer-text {
            margin-top: 2rem;
            font-size: 0.8rem;
            color: #999;
        }
    </style>
</head>
<body>
    <div class="bg-shapes">
        <div class="blob blob-1"></div>
        <div class="blob blob-2"></div>
        <div class="blob blob-3"></div>
    </div>

    <!-- Floating Background Images -->
    <img src="imagens/Ovo_1-01.png" alt="" class="floating-egg egg-1">
    <img src="imagens/Ovo_2-01.png" alt="" class="floating-egg egg-2">
    <img src="imagens/Ovo_3-01.png" alt="" class="floating-egg egg-3">
    <img src="imagens/Ovo_4-01.png" alt="" class="floating-egg egg-4">
    <img src="imagens/Ovo_1-01.png" alt="" class="floating-egg egg-5">
    <img src="imagens/Ovo_2-01.png" alt="" class="floating-egg egg-6">
    <img src="imagens/Ovo_3-01.png" alt="" class="floating-egg egg-7">
    <img src="imagens/Ovo_4-01.png" alt="" class="floating-egg egg-8">
    <img src="imagens/Ovo_1-01.png" alt="" class="floating-egg egg-9">
    <img src="imagens/Ovo_2-01.png" alt="" class="floating-egg egg-10">
    <img src="imagens/Ovo_3-01.png" alt="" class="floating-egg egg-11">
    <img src="imagens/Ovo_4-01.png" alt="" class="floating-egg egg-12">

    <div class="login-wrapper">
        <div class="login-card">
            <div class="logo-container">
                <img src="imagens/Fabrica_dos_Ovos__Coelho-01.png" alt="Fábrica dos Ovos com Coelho" class="logo">
            </div>
            
            <div style="margin-top: 2rem;"></div>

            <?php if ($error !== ''): ?>
                <div class="error-message">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-right: 8px;">
                        <circle cx="12" cy="12" r="10"></circle>
                        <line x1="12" y1="8" x2="12" y2="12"></line>
                        <line x1="12" y1="16" x2="12.01" y2="16"></line>
                    </svg>
                    <?= escape($error) ?>
                </div>
            <?php endif; ?>

            <form method="post" action="index.php">
                <input type="hidden" name="csrf_token" value="<?= escape(csrf_token()) ?>">
                
                <div class="form-group">
                    <label for="email" class="input-label">E-mail de acesso</label>
                    <input 
                        type="email" 
                        id="email" 
                        name="email" 
                        class="input-field" 
                        placeholder="exemplo@pascoa.local" 
                        required 
                        autofocus
                        value="<?= isset($_POST['email']) ? escape($_POST['email']) : '' ?>"
                    >
                </div>
                
                <button type="submit" class="submit-btn">Entrar na Fábrica</button>
            </form>
        </div>
    </div>
    <?php disable_inspect(); ?>
</body>
</html>
