<?php
// ---------------------------------------------------------------------
//  PHP – keep exactly as you wrote it
// ---------------------------------------------------------------------
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'php_errors.log');

session_start();
require './config/config.php';

$error = '';

if (isset($_POST['login'])) {
    $email    = trim($_POST['email']);
    $password = $_POST['password'];
    $remember = isset($_POST['remember']);

    $stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();
    $user   = $result->fetch_assoc();

    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user_id']    = $user['id'];
        $_SESSION['role']       = $user['role'];
        $_SESSION['company_id']  = $user['company_id'];
        $_SESSION['user_name']  = $user['name'];

        if ($remember) {
            setcookie("email", $email, time() + (86400 * 30), "/");
        }

        if ($_SESSION['role'] == 'admin') {
            header("Location: ./admin/dashboard_admin.php");
            exit();
        }
        header("Location: ./project/dashboard_project_manager.php");
        exit();
    } else {
        $error = "Invalid email or password.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login – Secure Access Portal</title>

    <!-- Inter font -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Material Icons -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

    <style>
        /* --------------------------------------------------------------
           CSS VARIABLES - Modern Color Palette
        -------------------------------------------------------------- */
        :root {
            --primary: #7c3aed;
            --primary-hover: #6d28d9;
            --primary-light: #8b5cf6;
            --bg: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
            --card-bg: rgba(255, 255, 255, 0.08);
            --card-bg-hover: rgba(255, 255, 255, 0.12);
            --text: #f1f5f9;
            --text-light: #cbd5e1;
            --text-lighter: #94a3b8;
            --border: rgba(255, 255, 255, 0.1);
            --border-focus: rgba(124, 58, 237, 0.6);
            --error: #ef4444;
            --error-bg: rgba(239, 68, 68, 0.1);
            --success: #10b981;
            --shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
            --shadow-lg: 0 25px 50px rgba(0, 0, 0, 0.4);
            --radius: 1rem;
            --radius-sm: 0.75rem;
            --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            --transition-slow: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
        }

        /* --------------------------------------------------------------
           BASE STYLES
        -------------------------------------------------------------- */
        *, *::before, *::after {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
           font-family: 'Segoe UI', system-ui, sans-serif;
            min-height: 100vh;
            background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.7)),
                        url('./background.jpeg') center/cover no-repeat fixed;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            color: var(--text);
        }

        /* --------------------------------------------------------------
           ANIMATED BACKGROUND
        -------------------------------------------------------------- */
        .background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            overflow: hidden;
        }

        .gradient-orb {
            position: absolute;
            border-radius: 50%;
            filter: blur(60px);
            opacity: 0.4;
            animation: float-orb 20s ease-in-out infinite;
        }

        .orb-1 {
            width: 400px;
            height: 400px;
            background: linear-gradient(45deg, #fef0a0ff, #ec4899);
            top: -100px;
            left: -100px;
            animation-delay: 0s;
        }

        .orb-2 {
            width: 300px;
            height: 300px;
            background: linear-gradient(45deg, #3b82f6, #3a8963);
            bottom: -50px;
            right: -50px;
            animation-delay: -5s;
        }

        .orb-3 {
            width: 250px;
            height: 250px;
            background: linear-gradient(45deg, #f59e0b, #ef4444);
            top: 40%;
            left: 60%;
            animation-delay: -10s;
        }

        @keyframes float-orb {
            0%, 100% {
                transform: translate(0, 0) scale(1);
            }
            25% {
                transform: translate(50px, 50px) scale(1.1);
            }
            50% {
                transform: translate(0, 100px) scale(0.9);
            }
            75% {
                transform: translate(-50px, 50px) scale(1.05);
            }
        }

        /* --------------------------------------------------------------
           GLOWING PARTICLES
        -------------------------------------------------------------- */
        .particles {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
        }

        .particle {
            position: absolute;
            background: var(--primary-light);
            border-radius: 50%;
            opacity: 0.1;
            animation: float-particle 15s linear infinite;
        }

        @keyframes float-particle {
            0% {
                transform: translateY(100vh) translateX(0) rotate(0deg);
                opacity: 0;
            }
            10% {
                opacity: 0.1;
            }
            90% {
                opacity: 0.1;
            }
            100% {
                transform: translateY(-100px) translateX(100px) rotate(360deg);
                opacity: 0;
            }
        }

        /* --------------------------------------------------------------
           MAIN CONTAINER
        -------------------------------------------------------------- */
        .container {
            display: flex;
            width: 100%;
            max-width: 900px;
            min-height: 500px;
            margin: 2rem;
            border-radius: var(--radius);
            overflow: hidden;
            box-shadow: var(--shadow-lg);
            backdrop-filter: blur(20px);
            background: rgba(15, 23, 42, 0.7);
            border: 1px solid var(--border);
            animation: slide-up 0.8s ease-out;
        }

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

        /* --------------------------------------------------------------
           HERO SECTION
        -------------------------------------------------------------- */
        .hero-section {
            flex: 1;
            background: linear-gradient(135deg, rgba(124, 58, 237, 0.1), rgba(59, 130, 246, 0.1));
            padding: 3rem;
            display: flex;
            flex-direction: column;
            justify-content: center;
            position: relative;
            overflow: hidden;
        }

        .hero-content {
            position: relative;
            z-index: 2;
        }

        .hero-badge {
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            background: rgba(124, 58, 237, 0.2);
            color: var(--primary-light);
            padding: 0.5rem 1rem;
            border-radius: 2rem;
            font-size: 0.875rem;
            font-weight: 500;
            margin-bottom: 2rem;
            border: 1px solid rgba(124, 58, 237, 0.3);
        }

        .hero-title {
            font-size: 2.5rem;
            font-weight: 700;
            line-height: 1.2;
            margin-bottom: 1rem;
            background: linear-gradient(to right, #f1f5f9, #c7d2fe);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .hero-subtitle {
            font-size: 1.125rem;
            color: var(--text-light);
            margin-bottom: 2rem;
            max-width: 400px;
        }

        .hero-features {
            display: flex;
            flex-direction: column;
            gap: 1rem;
            margin-top: 2rem;
        }

        .feature {
            display: flex;
            align-items: center;
            gap: 1rem;
            color: var(--text-light);
        }

        .feature-icon {
            width: 40px;
            height: 40px;
            background: rgba(124, 58, 237, 0.1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--primary-light);
        }

        /* --------------------------------------------------------------
           LOGIN SECTION
        -------------------------------------------------------------- */
        .login-section {
            flex: 0 0 450px;
            background: var(--card-bg);
            padding: 3rem 2.5rem;
            display: flex;
            flex-direction: column;
            justify-content: center;
            transition: var(--transition);
        }

        .login-section:hover {
            background: var(--card-bg-hover);
        }

        .login-header {
            text-align: center;
            margin-bottom: 2.5rem;
        }

        .logo {
            width: 60px;
            height: 60px;
            margin: 0 auto 1.5rem;
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
            font-weight: bold;
            box-shadow: 0 8px 20px rgba(124, 58, 237, 0.3);
        }

        .login-title {
            font-size: 1.75rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
        }

        .login-subtitle {
            color: var(--text-lighter);
            font-size: 0.95rem;
        }

        /* --------------------------------------------------------------
           FORM STYLES
        -------------------------------------------------------------- */
        .form-group {
            margin-bottom: 1.5rem;
            position: relative;
        }

        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: var(--text);
            font-size: 0.95rem;
            transition: var(--transition);
        }

        .input-group {
            position: relative;
            display: flex;
            align-items: center;
        }

        .input-icon {
            position: absolute;
            left: 1rem;
            color: var(--text-lighter);
            transition: var(--transition);
            z-index: 2;
        }

        .form-input {
            width: 100%;
            padding: 1rem 1rem 1rem 3rem;
            background: rgba(255, 255, 255, 0.07);
            border: 1.5px solid var(--border);
            border-radius: var(--radius-sm);
            color: var(--text);
            font-size: 1rem;
            transition: var(--transition);
            outline: none;
        }

        .form-input:focus {
            border-color: var(--border-focus);
            background: rgba(124, 58, 237, 0.1);
            box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.2);
            padding-left: 3.2rem;
        }

        .form-input:focus + .input-icon {
            color: var(--primary-light);
            transform: scale(1.1);
        }

        .form-input::placeholder {
            color: var(--text-lighter);
        }

        /* Password toggle */
        .password-toggle {
            position: absolute;
            right: 1rem;
            background: none;
            border: none;
            color: var(--text-lighter);
            cursor: pointer;
            transition: var(--transition);
            padding: 0.25rem;
            border-radius: 4px;
        }

        .password-toggle:hover {
            color: var(--primary-light);
            background: rgba(255, 255, 255, 0.1);
        }

        /* Remember me & Forgot password */
        .form-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            font-size: 0.9rem;
        }

        .remember-me {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            color: var(--text-lighter);
            cursor: pointer;
        }

        .checkbox {
            width: 18px;
            height: 18px;
            accent-color: var(--primary);
            cursor: pointer;
        }

        .forgot-password {
            color: var(--primary-light);
            text-decoration: none;
            transition: var(--transition);
        }

        .forgot-password:hover {
            color: var(--primary);
            text-decoration: underline;
        }

        /* Submit button */
        .submit-btn {
            width: 100%;
            padding: 1rem;
            background: linear-gradient(135deg, var(--primary), var(--primary-light));
            color: white;
            border: none;
            border-radius: var(--radius-sm);
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            position: relative;
            overflow: hidden;
        }

        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(124, 58, 237, 0.4);
        }

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

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

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

        /* Error message */
        .error-message {
            background: var(--error-bg);
            color: #fca5a5;
            padding: 0.875rem 1rem;
            border-radius: var(--radius-sm);
            font-size: 0.9rem;
            margin-top: 1.5rem;
            border: 1px solid rgba(239, 68, 68, 0.3);
            animation: shake 0.5s ease-in-out;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .error-message::before {
            content: '⚠️';
            font-size: 1rem;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
            20%, 40%, 60%, 80% { transform: translateX(5px); }
        }

        /* --------------------------------------------------------------
           RESPONSIVE DESIGN
        -------------------------------------------------------------- */
        @media (max-width: 968px) {
            .container {
                flex-direction: column;
                margin: 1rem;
                max-width: 500px;
            }

            .hero-section {
                padding: 2rem;
                text-align: center;
            }

            .hero-title {
                font-size: 2rem;
            }

            .login-section {
                flex: 1;
                padding: 2.5rem 2rem;
            }
        }

        @media (max-width: 480px) {
            .container {
                margin: 0.5rem;
                border-radius: 0;
                min-height: 100vh;
            }

            .hero-section {
                padding: 1.5rem;
            }

            .login-section {
                padding: 2rem 1.5rem;
            }

            .hero-title {
                font-size: 1.75rem;
            }

            .form-options {
                flex-direction: column;
                gap: 1rem;
                align-items: flex-start;
            }
        }
    </style>
</head>
<body>

    <!-- Animated Background -->
    <div class="background">
        <div class="gradient-orb orb-1"></div>
        <div class="gradient-orb orb-2"></div>
        <div class="gradient-orb orb-3"></div>
    </div>

    <!-- Floating Particles -->
    <div class="particles" id="particles"></div>

    <!-- Main Container -->
    <div class="container">
        <!-- Hero Section -->
        <div class="hero-section">
            <div class="hero-content">
                <div class="hero-badge">
                    <span class="material-icons">verified</span>
                    Secure Access Portal
                </div>
                <h1 class="hero-title">Welcome Back to Your Dashboard</h1>
                <p class="hero-subtitle">Sign in to access your personalized workspace and continue from where you left off.</p>
                
                <div class="hero-features">
                    <div class="feature">
                        <div class="feature-icon">
                            <span class="material-icons">security</span>
                        </div>
                        <span>Enterprise-grade security</span>
                    </div>
                    <div class="feature">
                        <div class="feature-icon">
                            <span class="material-icons">speed</span>
                        </div>
                        <span>Lightning fast performance</span>
                    </div>
                    <div class="feature">
                        <div class="feature-icon">
                            <span class="material-icons">support</span>
                        </div>
                        <span>24/7 Support available</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Login Section -->
        <div class="login-section">
            <div class="login-header">
                <div class="logo">
                    <span class="material-icons">lock</span>
                </div>
                <h2 class="login-title">Sign In</h2>
                <p class="login-subtitle">Enter your credentials to continue</p>
            </div>

            <form method="POST" novalidate>
                <div class="form-group">
                    <label for="email" class="form-label">Email Address</label>
                    <div class="input-group">
                        <span class="input-icon material-icons">email</span>
                        <input type="email" id="email" name="email" class="form-input" required
                               value="<?=htmlspecialchars($_COOKIE['email']??'')?>"
                               placeholder="you@example.com" autocomplete="email">
                    </div>
                </div>

                <div class="form-group">
                    <label for="password" class="form-label">Password</label>
                    <div class="input-group">
                        <span class="input-icon material-icons">lock</span>
                        <input type="password" id="password" name="password" class="form-input" required
                               placeholder="••••••••" autocomplete="current-password">
                        <button type="button" class="password-toggle" aria-label="Toggle password visibility">
                            <span class="material-icons">visibility</span>
                        </button>
                    </div>
                </div>

                <div class="form-options">
                    <label class="remember-me">
                        <input type="checkbox" id="remember" name="remember" class="checkbox"
                               <?=isset($_COOKIE['email'])?'checked':''?>>
                        Remember me
                    </label>
                    <a href="#" class="forgot-password">Forgot password?</a>
                </div>

                <button type="submit" name="login" class="submit-btn">
                    Sign In to Dashboard
                </button>

                <?php if (!empty($error)): ?>
                    <div class="error-message"><?=htmlspecialchars($error)?></div>
                <?php endif; ?>
            </form>
        </div>
    </div>

    <script>
        // Create floating particles
        function createParticles() {
            const particlesContainer = document.getElementById('particles');
            const particleCount = 20;
            
            for (let i = 0; i < particleCount; i++) {
                const particle = document.createElement('div');
                particle.classList.add('particle');
                
                // Random properties
                const size = Math.random() * 6 + 2;
                const left = Math.random() * 100;
                const animationDuration = Math.random() * 20 + 10;
                const animationDelay = Math.random() * 20;
                
                particle.style.width = `${size}px`;
                particle.style.height = `${size}px`;
                particle.style.left = `${left}vw`;
                particle.style.animationDuration = `${animationDuration}s`;
                particle.style.animationDelay = `${animationDelay}s`;
                
                particlesContainer.appendChild(particle);
            }
        }

        // Password toggle functionality
        document.querySelector('.password-toggle').addEventListener('click', function () {
            const passwordInput = document.getElementById('password');
            const icon = this.querySelector('span');
            
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                icon.textContent = 'visibility_off';
                this.style.color = '#7c3aed';
            } else {
                passwordInput.type = 'password';
                icon.textContent = 'visibility';
                this.style.color = '';
            }
        });

        // Input focus effects
        document.querySelectorAll('.form-input').forEach(input => {
            input.addEventListener('focus', function() {
                this.parentElement.parentElement.style.transform = 'translateY(-2px)';
            });
            
            input.addEventListener('blur', function() {
                this.parentElement.parentElement.style.transform = 'translateY(0)';
            });
        });

        // Form submission animation
        document.querySelector('form').addEventListener('submit', function(e) {
            const btn = this.querySelector('.submit-btn');
            btn.style.transform = 'scale(0.98)';
            setTimeout(() => {
                btn.style.transform = '';
            }, 200);
        });

        // Initialize particles when page loads
        window.addEventListener('load', createParticles);
    </script>
</body>
</html>