<?php
require 'config.php';

if (isset($_SESSION['user_id'])) {
    redirect('index.php');
}

$errors = [];

if (is_post()) {
    if(function_exists('verify_csrf')) verify_csrf();
    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';

    if (!$email || !$password) {
        $errors[] = "ইমেইল এবং পাসওয়ার্ড প্রয়োজন।";
    } else {
        $stmt = $pdo->prepare("SELECT * FROM users WHERE email=? LIMIT 1");
        $stmt->execute([$email]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            if (!empty($user['is_blocked'])) {
                $errors[] = "আপনার অ্যাকাউন্ট ব্লক করা হয়েছে। সাপোর্টে যোগাযোগ করুন।";
            } else {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user'] = $user;
                if(function_exists('set_flash')) set_flash("সফলভাবে লগইন হয়েছে!", "success");
                redirect('index.php');
            }
        } else {
            $errors[] = "ভুল ইমেইল অথবা পাসওয়ার্ড।";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>লগইন | ডিজিটাল সেবা পোর্টাল</title>
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet">
    
    <style>
        :root {
            --primary: #2563eb;
            --primary-light: #3b82f6;
            --primary-dark: #1d4ed8;
            --accent: #06b6d4;
            --white: #ffffff;
            --glass-bg: rgba(255, 255, 255, 0.08);
            --glass-border: rgba(255, 255, 255, 0.18);
            --glass-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Hind Siliguri', sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            overflow-x: hidden;
            position: relative;
            background: linear-gradient(135deg, #0f172a 0%, #1e293b 30%, #1e3a5f 60%, #0f172a 100%);
            background-size: 400% 400%;
            animation: bgShift 20s ease infinite;
        }

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

        #liveWallpaper {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 0;
            pointer-events: none;
        }

        .main-container {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 460px;
        }

        .login-card {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(25px);
            -webkit-backdrop-filter: blur(25px);
            border: 1px solid rgba(255, 255, 255, 0.12);
            border-radius: 28px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4), 
                        0 0 0 1px rgba(255, 255, 255, 0.05) inset,
                        0 1px 0 rgba(255, 255, 255, 0.1) inset;
            width: 100%;
            overflow: hidden;
            animation: cardFloat 0.8s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
            transition: all 0.4s ease;
        }

        .login-card:hover {
            box-shadow: 0 30px 70px rgba(37, 99, 235, 0.25), 
                        0 0 0 1px rgba(255, 255, 255, 0.08) inset,
                        0 1px 0 rgba(255, 255, 255, 0.15) inset;
            transform: translateY(-8px);
        }

        @keyframes cardFloat {
            0% {
                opacity: 0;
                transform: translateY(60px) scale(0.85);
                filter: blur(10px);
            }
            60% {
                opacity: 1;
                filter: blur(0);
            }
            100% {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }

        .card-header-custom {
            padding: 45px 30px 30px;
            text-align: center;
            position: relative;
            background: linear-gradient(180deg, rgba(37, 99, 235, 0.15) 0%, transparent 100%);
        }

        .card-header-custom::after {
            content: "";
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 70px;
            height: 2px;
            background: linear-gradient(90deg, transparent, #06b6d4, transparent);
            border-radius: 2px;
        }

        .header-icon-box {
            width: 85px;
            height: 85px;
            background: linear-gradient(135deg, rgba(37, 99, 235, 0.3), rgba(6, 182, 212, 0.15));
            border: 2px solid rgba(6, 182, 212, 0.4);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
            color: #38bdf8;
            box-shadow: 0 0 40px rgba(6, 182, 212, 0.2), 
                        0 0 80px rgba(37, 99, 235, 0.1),
                        inset 0 0 20px rgba(255, 255, 255, 0.05);
            animation: iconGlow 3s ease-in-out infinite;
            transition: all 0.5s ease;
        }

        .login-card:hover .header-icon-box {
            box-shadow: 0 0 60px rgba(6, 182, 212, 0.35), 
                        0 0 100px rgba(37, 99, 235, 0.2);
            border-color: rgba(6, 182, 212, 0.6);
            transform: scale(1.08);
        }

        @keyframes iconGlow {
            0%, 100% { 
                box-shadow: 0 0 40px rgba(6, 182, 212, 0.2), 0 0 80px rgba(37, 99, 235, 0.1);
            }
            50% { 
                box-shadow: 0 0 60px rgba(6, 182, 212, 0.4), 0 0 100px rgba(37, 99, 235, 0.25);
            }
        }

        .portal-title {
            font-size: 2rem;
            font-weight: 700;
            background: linear-gradient(135deg, #ffffff 0%, #93c5fd 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin: 0;
            letter-spacing: 1px;
        }

        .portal-subtitle {
            font-size: 0.95rem;
            color: rgba(148, 163, 184, 0.9);
            margin-top: 10px;
            font-weight: 300;
            letter-spacing: 0.5px;
        }

        .card-body-custom {
            padding: 35px 35px 0;
        }

        .form-label {
            font-weight: 500;
            color: rgba(203, 213, 225, 0.95);
            font-size: 0.9rem;
            margin-bottom: 8px;
            letter-spacing: 0.5px;
        }

        .input-group {
            transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
            margin-bottom: 5px;
        }

        .input-group:focus-within {
            transform: translateY(-3px);
        }

        .input-group-text {
            background: rgba(255, 255, 255, 0.06);
            border: 2px solid rgba(255, 255, 255, 0.1);
            border-right: none;
            color: #38bdf8;
            border-radius: 16px 0 0 16px;
            padding: 14px 16px;
            transition: all 0.4s ease;
        }

        .form-control {
            border-radius: 0 16px 16px 0;
            border: 2px solid rgba(255, 255, 255, 0.1);
            border-left: none;
            padding: 14px 16px;
            font-size: 1rem;
            background: rgba(255, 255, 255, 0.06);
            color: #e2e8f0;
            transition: all 0.4s ease;
            letter-spacing: 0.3px;
        }

        .form-control::placeholder {
            color: rgba(148, 163, 184, 0.5);
        }

        .form-control:focus {
            box-shadow: 0 0 25px rgba(6, 182, 212, 0.15);
            border-color: rgba(6, 182, 212, 0.5);
            background: rgba(255, 255, 255, 0.1);
            outline: none;
            color: #ffffff;
        }

        .input-group:focus-within .input-group-text {
            border-color: rgba(6, 182, 212, 0.5);
            background: rgba(6, 182, 212, 0.15);
            color: #06b6d4;
            box-shadow: 0 0 15px rgba(6, 182, 212, 0.1);
        }

        .btn-login {
            background: linear-gradient(135deg, #2563eb 0%, #06b6d4 100%);
            border: none;
            color: #ffffff;
            font-weight: 700;
            padding: 15px;
            border-radius: 16px;
            width: 100%;
            transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
            box-shadow: 0 10px 30px rgba(37, 99, 235, 0.3);
            font-size: 1.1rem;
            letter-spacing: 1px;
            position: relative;
            overflow: hidden;
            cursor: pointer;
        }

        .btn-login::before {
            content: "";
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255, 255, 255, 0.2) 0%, transparent 60%);
            opacity: 0;
            transition: opacity 0.4s ease;
        }

        .btn-login:hover {
            background: linear-gradient(135deg, #3b82f6 0%, #22d3ee 100%);
            transform: translateY(-4px);
            box-shadow: 0 15px 40px rgba(37, 99, 235, 0.5), 0 0 30px rgba(6, 182, 212, 0.3);
        }

        .btn-login:hover::before {
            opacity: 1;
        }

        .btn-login:active {
            transform: translateY(-1px) scale(0.97);
            box-shadow: 0 5px 20px rgba(37, 99, 235, 0.4);
            transition: all 0.1s ease;
        }

        .forgot-link {
            color: rgba(148, 163, 184, 0.8);
            text-decoration: none;
            font-size: 0.9rem;
            font-weight: 500;
            transition: all 0.3s ease;
            position: relative;
        }

        .forgot-link::after {
            content: "";
            position: absolute;
            bottom: -3px;
            left: 0;
            width: 0;
            height: 1.5px;
            background: linear-gradient(90deg, #f59e0b, #ef4444);
            border-radius: 2px;
            transition: width 0.3s ease;
        }

        .forgot-link:hover {
            color: #f59e0b;
        }

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

        .register-link {
            text-align: center;
            margin-top: 28px;
            margin-bottom: 0;
            padding-bottom: 0;
            font-size: 0.95rem;
            color: rgba(148, 163, 184, 0.8);
        }
        
        .register-link a {
            background: linear-gradient(135deg, #38bdf8, #06b6d4);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            font-weight: 600;
            text-decoration: none;
            transition: all 0.3s ease;
            position: relative;
        }

        .register-link a::after {
            content: "";
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 0;
            height: 2px;
            background: linear-gradient(90deg, #38bdf8, #06b6d4);
            border-radius: 2px;
            transition: width 0.3s ease;
        }

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

        .alert-custom {
            border-radius: 16px;
            font-size: 0.9rem;
            border: none;
            background: rgba(239, 68, 68, 0.12);
            color: #fca5a5;
            border-left: 4px solid #ef4444;
            backdrop-filter: blur(15px);
            -webkit-backdrop-filter: blur(15px);
            animation: shakeError 0.6s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
        }

        @keyframes shakeError {
            10%, 90% { transform: translateX(-1px); }
            20%, 80% { transform: translateX(2px); }
            30%, 50%, 70% { transform: translateX(-4px); }
            40%, 60% { transform: translateX(4px); }
        }

        .form-check-input {
            background-color: rgba(255, 255, 255, 0.08);
            border: 2px solid rgba(255, 255, 255, 0.2);
            cursor: pointer;
            transition: all 0.3s ease;
            width: 18px;
            height: 18px;
        }

        .form-check-input:checked {
            background-color: #06b6d4;
            border-color: #06b6d4;
            box-shadow: 0 0 15px rgba(6, 182, 212, 0.4);
        }

        .form-check-label {
            color: rgba(203, 213, 225, 0.8);
            cursor: pointer;
            transition: color 0.3s ease;
            font-size: 0.9rem;
        }

        /* ===========================================
           WhatsApp বাটন - রেজিস্টার লিঙ্কের ঠিক নিচে
           =========================================== */
        .whatsapp-support {
            text-align: center;
            padding: 8px 35px 30px;
            margin-top: 0;
        }

        .whatsapp-divider {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 14px;
            color: rgba(148, 163, 184, 0.5);
            font-size: 0.8rem;
        }

        .whatsapp-divider::before,
        .whatsapp-divider::after {
            content: "";
            flex: 1;
            height: 1px;
            background: rgba(255, 255, 255, 0.1);
        }

        .whatsapp-btn-inline {
            display: inline-flex;
            align-items: center;
            gap: 10px;
            background: linear-gradient(135deg, #25d366 0%, #128c7e 100%);
            color: white;
            padding: 12px 24px;
            border-radius: 50px;
            text-decoration: none;
            font-weight: 600;
            font-size: 0.95rem;
            transition: all 0.3s ease;
            box-shadow: 0 5px 20px rgba(37, 211, 102, 0.3);
            animation: wpGlow 2s ease-in-out infinite;
        }

        .whatsapp-btn-inline i {
            font-size: 1.3rem;
            animation: wpIconBounce 1.5s infinite;
        }

        @keyframes wpIconBounce {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.2); }
        }

        @keyframes wpGlow {
            0%, 100% { box-shadow: 0 5px 20px rgba(37, 211, 102, 0.3); }
            50% { box-shadow: 0 5px 30px rgba(37, 211, 102, 0.6); }
        }

        .whatsapp-btn-inline:hover {
            background: linear-gradient(135deg, #2bd968 0%, #15a07a 100%);
            transform: translateY(-3px);
            box-shadow: 0 10px 30px rgba(37, 211, 102, 0.5);
            color: white;
        }

        .whatsapp-btn-inline:active {
            transform: translateY(-1px) scale(0.97);
        }

        .footer-text {
            text-align: center;
            font-size: 0.85rem;
            color: rgba(148, 163, 184, 0.6);
            margin-top: 35px;
        }

        .footer-text strong {
            color: #38bdf8;
            font-weight: 600;
        }

        @media (max-width: 576px) {
            .card-body-custom {
                padding: 25px 20px 0;
            }
            .portal-title {
                font-size: 1.6rem;
            }
            .header-icon-box {
                width: 70px;
                height: 70px;
            }
            .whatsapp-support {
                padding: 6px 20px 25px;
            }
            .whatsapp-btn-inline {
                padding: 10px 20px;
                font-size: 0.9rem;
            }
        }
    </style>
</head>
<body>

    <canvas id="liveWallpaper"></canvas>

    <div class="main-container">
        
        <div class="login-card">
            <div class="card-header-custom">
                <div class="header-icon-box">
                    <i class="fas fa-fingerprint fa-2x"></i>
                </div>
                <h2 class="portal-title">BEST SEBA</h2>
                <p class="portal-subtitle">আপনার একাউন্টে লগইন করুন</p>
            </div>

            <div class="card-body-custom">
                
                <?php if (function_exists('show_flash')) show_flash(); ?>

                <?php if (!empty($errors)): ?>
                    <div class="alert alert-danger alert-custom d-flex align-items-center mb-4" role="alert">
                        <i class="fas fa-circle-exclamation me-3"></i>
                        <div><?php echo $errors[0]; ?></div>
                    </div>
                <?php endif; ?>

                <form method="post">
                    <?php if (function_exists('csrf_field')) csrf_field(); ?>

                    <div class="mb-4">
                        <label for="email" class="form-label">
                            <i class="fas fa-envelope me-2"></i>ইমেইল ঠিকানা
                        </label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-at"></i></span>
                            <input type="email" name="email" id="email" class="form-control" 
                                   placeholder="example@email.com" 
                                   value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" required>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label for="password" class="form-label">
                            <i class="fas fa-lock me-2"></i>পাসওয়ার্ড
                        </label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-key"></i></span>
                            <input type="password" name="password" id="password" class="form-control" 
                                   placeholder="••••••••" required>
                        </div>
                    </div>

                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" id="remember">
                            <label class="form-check-label" for="remember">
                                আমাকে মনে রাখুন
                            </label>
                        </div>
                        <a href="#" class="forgot-link">পাসওয়ার্ড ভুলে গেছেন?</a>
                    </div>

                    <button type="submit" class="btn btn-login">
                        <i class="fas fa-arrow-right-to-bracket me-2"></i>লগইন করুন
                    </button>
                </form>

                <div class="register-link">
                    নতুন ব্যবহারকারী? <a href="signup.php">একাউন্ট তৈরি করুন</a>
                </div>

            </div>

            <!-- ✅ WhatsApp বাটন - এখন রেজিস্টার লিঙ্কের ঠিক নিচে, কোনো ফাঁকা জায়গা ছাড়া -->
            <div class="whatsapp-support">
                <div class="whatsapp-divider">অথবা</div>
                <a href="https://wa.me/8801880845221?text=হ্যালো,%20আমার%20লগইন%20করতে%20সমস্যা%20হচ্ছে।%20দয়া%20করে%20সাহায্য%20করুন।" 
                   target="_blank" 
                   class="whatsapp-btn-inline">
                    <i class="fab fa-whatsapp"></i> WhatsApp এ যোগাযোগ করুন
                </a>
            </div>

        </div>

        <div class="footer-text">
            &copy; <?php echo date('Y'); ?> <strong>ডিজিটাল সেবা পোর্টাল</strong> • সর্বস্বত্ব সংরক্ষিত
            <br>
            <small>নিরাপদ • নির্ভরযোগ্য • দ্রুত সেবা</small>
        </div>
    </div>

    <script>
        const canvas = document.getElementById('liveWallpaper');
        const ctx = canvas.getContext('2d');

        let width, height;
        let particles = [];
        let mouseX = -1000;
        let mouseY = -1000;

        function resizeCanvas() {
            width = canvas.width = window.innerWidth;
            height = canvas.height = window.innerHeight;
        }
        window.addEventListener('resize', () => {
            resizeCanvas();
            initParticles();
        });

        document.addEventListener('mousemove', (e) => {
            mouseX = e.clientX;
            mouseY = e.clientY;
        });

        document.addEventListener('mouseleave', () => {
            mouseX = -1000;
            mouseY = -1000;
        });

        class Particle {
            constructor() {
                this.reset();
            }

            reset() {
                this.x = Math.random() * width;
                this.y = Math.random() * height;
                this.baseVx = (Math.random() - 0.5) * 0.4;
                this.baseVy = (Math.random() - 0.5) * 0.4;
                this.vx = this.baseVx;
                this.vy = this.baseVy;
                this.size = Math.random() * 2.5 + 0.8;
                
                const colors = [
                    `rgba(56, 189, 248, OPACITY)`,
                    `rgba(6, 182, 212, OPACITY)`,
                    `rgba(37, 99, 235, OPACITY)`,
                    `rgba(99, 102, 241, OPACITY)`,
                    `rgba(255, 255, 255, OPACITY)`,
                ];
                const colorChoice = colors[Math.floor(Math.random() * colors.length)];
                this.opacity = Math.random() * 0.6 + 0.15;
                this.color = colorChoice.replace('OPACITY', this.opacity);
                this.originalColor = colorChoice;
            }

            update() {
                const dx = this.x - mouseX;
                const dy = this.y - mouseY;
                const dist = Math.sqrt(dx * dx + dy * dy);
                const maxDist = 150;

                if (dist < maxDist && mouseX > 0) {
                    const force = (maxDist - dist) / maxDist;
                    this.vx += (dx / dist) * force * 0.5;
                    this.vy += (dy / dist) * force * 0.5;
                    this.opacity = Math.min(0.8, this.opacity + 0.02);
                } else {
                    this.vx += (this.baseVx - this.vx) * 0.05;
                    this.vy += (this.baseVy - this.vy) * 0.05;
                    if (this.opacity > 0.15) {
                        this.opacity -= 0.005;
                    }
                }

                this.x += this.vx;
                this.y += this.vy;

                if (this.x < 0 || this.x > width) this.vx *= -1;
                if (this.y < 0 || this.y > height) this.vy *= -1;

                this.x = Math.max(0, Math.min(width, this.x));
                this.y = Math.max(0, Math.min(height, this.y));

                this.color = this.originalColor.replace('OPACITY', this.opacity);
            }

            draw(ctx) {
                const gradient = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size * 3);
                gradient.addColorStop(0, this.color);
                gradient.addColorStop(1, 'transparent');
                
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size * 3, 0, Math.PI * 2);
                ctx.fillStyle = gradient;
                ctx.fill();

                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fillStyle = this.color;
                ctx.fill();
            }
        }

        function initParticles() {
            const particleCount = Math.min(Math.floor((width * height) / 12000), 150);
            particles = [];
            for (let i = 0; i < particleCount; i++) {
                particles.push(new Particle());
            }
        }

        function drawConnections() {
            const maxDist = 130;
            for (let i = 0; i < particles.length; i++) {
                for (let j = i + 1; j < particles.length; j++) {
                    const dx = particles[i].x - particles[j].x;
                    const dy = particles[i].y - particles[j].y;
                    const dist = Math.sqrt(dx * dx + dy * dy);

                    if (dist < maxDist) {
                        const opacity = (1 - dist / maxDist) * 0.12;
                        ctx.beginPath();
                        ctx.moveTo(particles[i].x, particles[i].y);
                        ctx.lineTo(particles[j].x, particles[j].y);
                        ctx.strokeStyle = `rgba(56, 189, 248, ${opacity})`;
                        ctx.lineWidth = 0.6;
                        ctx.stroke();
                    }
                }
            }
        }

        function animate() {
            ctx.clearRect(0, 0, width, height);
            particles.forEach(p => {
                p.update();
                p.draw(ctx);
            });
            drawConnections();
            requestAnimationFrame(animate);
        }

        resizeCanvas();
        initParticles();
        animate();
    </script>

</body>
</html>