<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
        <title>Login | Event Photo Delivery</title>
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700;14..32,800&display=swap" rel="stylesheet">
        <!-- Font Awesome 6 -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
        <style>
            * {
                margin: 0;
                padding: 0;
                box-sizing: border-box;
            }

            /* Professional background with overlay */
            body {
                font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
                min-height: 100vh;
                display: flex;
                align-items: center;
                justify-content: center;
                background: url("/admin/static/login background.jfif");
                background-size: cover;
                background-position: center;
                background-repeat: no-repeat;
                position: relative;
                padding: 20px;
                background-color: #f5f7fa;
            }

            /* Light overlay for better background visibility */
            body::before {
                content: '';
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: rgba(255, 255, 255, 0.08);
                z-index: 0;
            }

            /* Login card container */
            .login-card {
                background: #ffffff;
                border-radius: 16px;
                box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
                padding: 48px 40px;
                text-align: center;
                max-width: 500px;
                width: 100%;
                position: relative;
                z-index: 1;
            }

            /* Logo container - SINGLE LOGO */
            .logo-container {
                display: flex;
                align-items: center;
                justify-content: center;
                margin-bottom: 28px;
                background: #f3f4f6;
                border-radius: 12px;
                padding: 24px 20px;
            }

            /* Logo styling - SINGLE LOGO ONLY */
            .logo {
                height: 80px;
                width: auto;
                max-width: 200px;
                object-fit: contain;
                display: block;
                background: transparent;
                padding: 0;
            }

            /* Main heading */
            .login-card h2 {
                font-size: 1.75rem;
                font-weight: 700;
                color: #1a1a1a;
                margin-bottom: 12px;
                letter-spacing: -0.5px;
            }

            /* Subtitle/description */
            .login-card p {
                font-size: 0.95rem;
                color: #6b7280;
                margin-bottom: 32px;
                font-weight: 400;
            }

            /* Form input group */
            .input-group {
                margin-bottom: 20px;
                text-align: left;
            }

            /* Label styling */
            .input-group label {
                display: block;
                margin-bottom: 8px;
                font-weight: 500;
                color: #374151;
                font-size: 0.9rem;
                text-transform: uppercase;
                letter-spacing: 0.3px;
            }

            /* Input field styling */
            .input-group input {
                width: 100%;
                padding: 12px 16px;
                border: 1.5px solid #e5e7eb;
                border-radius: 8px;
                font-size: 0.95rem;
                transition: all 0.3s ease;
                background: #ffffff;
                color: #1f2937;
            }

            .input-group input::placeholder {
                color: #9ca3af;
            }

            .input-group input:focus {
                outline: none;
                border-color: #3b82f6;
                background: #ffffff;
                box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
            }

            /* Password wrapper for toggle button */
            .password-wrapper {
                position: relative;
            }

            /* Password toggle button */
            .toggle-password {
                position: absolute;
                right: 14px;
                top: 50%;
                transform: translateY(-50%);
                background: none;
                border: none;
                color: #6b7280;
                cursor: pointer;
                font-size: 0.8rem;
                font-weight: 600;
                padding: 6px 10px;
                border-radius: 4px;
                transition: all 0.2s ease;
                text-transform: uppercase;
                letter-spacing: 0.2px;
            }

            .toggle-password:hover {
                background: #f3f4f6;
                color: #1f2937;
            }

            .toggle-password:active {
                background: #e5e7eb;
            }

            /* Captcha section */
            .captcha-container {
                margin-bottom: 28px;
                text-align: left;
            }

            .captcha-label {
                display: block;
                margin-bottom: 8px;
                font-weight: 500;
                color: #374151;
                font-size: 0.9rem;
                text-transform: uppercase;
                letter-spacing: 0.3px;
            }

            /* Captcha problem + refresh row */
            .captcha-problem-row {
                display: flex;
                gap: 10px;
                align-items: center;
                margin-bottom: 12px;
            }

            /* Captcha display - MATH PROBLEM */
            .captcha-display {
                background: linear-gradient(135deg, #f0f4ff, #f9fafb);
                border: 1.5px solid #dbeafe;
                border-radius: 8px;
                padding: 14px 18px;
                font-weight: 700;
                font-size: 1.25rem;
                color: #1e40af;
                letter-spacing: 1px;
                flex: 1;
                text-align: center;
                user-select: none;
                font-family: 'Courier New', monospace;
                min-height: 48px;
                display: flex;
                align-items: center;
                justify-content: center;
            }

            /* Refresh button */
            .captcha-refresh {
                background: #f3f4f6;
                border: 1.5px solid #e5e7eb;
                border-radius: 8px;
                padding: 0;
                cursor: pointer;
                transition: all 0.2s ease;
                color: #6b7280;
                font-size: 0.95rem;
                display: flex;
                align-items: center;
                justify-content: center;
                width: 48px;
                height: 48px;
                flex-shrink: 0;
            }

            .captcha-refresh:hover {
                background: #e5e7eb;
                color: #1f2937;
                border-color: #d1d5db;
            }

            .captcha-refresh:active {
                background: #d1d5db;
            }

            /* Captcha input - SECOND ROW */
            .captcha-input {
                width: 100%;
                padding: 12px 16px;
                border: 1.5px solid #e5e7eb;
                border-radius: 8px;
                font-size: 0.95rem;
                transition: all 0.3s ease;
                background: #ffffff;
            }

            .captcha-input::placeholder {
                color: #9ca3af;
            }

            .captcha-input:focus {
                outline: none;
                border-color: #3b82f6;
                box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
            }

            /* Captcha error */
            .captcha-error {
                color: #dc2626;
                font-size: 0.85rem;
                margin-top: 6px;
                display: none;
            }

            .captcha-error.show {
                display: block;
            }

            /* Login button */
            .login-btn {
                width: 100%;
                padding: 13px 16px;
                background: linear-gradient(135deg, #3b82f6, #1e40af);
                color: white;
                border: none;
                border-radius: 8px;
                font-size: 0.95rem;
                font-weight: 600;
                cursor: pointer;
                transition: all 0.3s ease;
                margin-top: 12px;
                text-transform: uppercase;
                letter-spacing: 0.5px;
                box-shadow: 0 4px 12px rgba(59, 130, 246, 0.25);
            }

            .login-btn:hover {
                background: linear-gradient(135deg, #2563eb, #1e3a8a);
                transform: translateY(-1px);
                box-shadow: 0 6px 16px rgba(59, 130, 246, 0.35);
            }

            .login-btn:active {
                transform: translateY(0);
                box-shadow: 0 2px 8px rgba(59, 130, 246, 0.25);
            }

            .login-btn:disabled {
                opacity: 0.6;
                cursor: not-allowed;
                transform: none;
            }

            /* Error message styling */
            .error {
                background: #fef2f2;
                color: #991b1b;
                border: 1px solid #fecaca;
                border-radius: 8px;
                padding: 12px 16px;
                margin-bottom: 24px;
                font-size: 0.9rem;
                font-weight: 500;
                display: flex;
                align-items: center;
                gap: 10px;
            }

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

            /* Success message */
            .success {
                background: #f0fdf4;
                color: #15803d;
                border: 1px solid #bbf7d0;
                border-radius: 8px;
                padding: 12px 16px;
                margin-bottom: 24px;
                font-size: 0.9rem;
                font-weight: 500;
                display: flex;
                align-items: center;
                gap: 10px;
            }

            .success::before {
                content: '✓';
                font-size: 1.1rem;
            }

            /* Ultra small phones */
            @media (max-width: 360px) {
                .login-card {
                    padding: 28px 20px;
                    max-width: 100%;
                }

                .logo-container {
                    padding: 16px 12px;
                    margin-bottom: 14px;
                }

                .logo {
                    height: 50px;
                    max-width: 140px;
                }

                .login-card h2 {
                    font-size: 1.3rem;
                    margin-bottom: 6px;
                }

                .input-group {
                    margin-bottom: 16px;
                }

                .input-group label {
                    font-size: 0.85rem;
                }

                .input-group input {
                    padding: 10px 14px;
                    font-size: 0.9rem;
                }

                .toggle-password {
                    font-size: 0.75rem;
                    padding: 4px 8px;
                }

                .captcha-label {
                    font-size: 0.85rem;
                }

                .captcha-display {
                    font-size: 1.1rem;
                    padding: 12px 14px;
                    min-height: 44px;
                }

                .captcha-refresh {
                    width: 44px;
                    height: 44px;
                    font-size: 0.85rem;
                }

                .login-btn {
                    padding: 11px 12px;
                    font-size: 0.85rem;
                    margin-top: 10px;
                }

                .error {
                    padding: 10px 12px;
                    font-size: 0.85rem;
                }
            }

            /* Small phones (320px - 480px) */
            @media (max-width: 480px) {
                .login-card {
                    padding: 32px 24px;
                    max-width: 100%;
                }

                .logo-container {
                    padding: 18px 14px;
                    margin-bottom: 16px;
                }

                .logo {
                    height: 60px;
                    max-width: 160px;
                }

                .login-card h2 {
                    font-size: 1.5rem;
                    margin-bottom: 10px;
                }

                .input-group {
                    margin-bottom: 18px;
                }

                .input-group label {
                    font-size: 0.88rem;
                }

                .input-group input {
                    padding: 11px 14px;
                    font-size: 0.92rem;
                }

                .toggle-password {
                    font-size: 0.78rem;
                    padding: 5px 8px;
                    right: 12px;
                }

                .captcha-problem-row {
                    gap: 8px;
                    margin-bottom: 10px;
                }

                .captcha-display {
                    font-size: 1.15rem;
                    padding: 12px 14px;
                    min-height: 46px;
                }

                .captcha-refresh {
                    width: 46px;
                    height: 46px;
                    font-size: 0.9rem;
                }

                .captcha-input {
                    padding: 11px 14px;
                    font-size: 0.92rem;
                }

                .login-btn {
                    padding: 12px 14px;
                    font-size: 0.9rem;
                    margin-top: 10px;
                }

                .error {
                    padding: 10px 14px;
                    font-size: 0.88rem;
                }
            }

            /* Medium tablets (480px - 768px) */
            @media (min-width: 481px) and (max-width: 768px) {
                body {
                    padding: 18px;
                }

                .login-card {
                    padding: 40px 36px;
                    max-width: 480px;
                }

                .logo-container {
                    padding: 20px 16px;
                    margin-bottom: 18px;
                }

                .logo {
                    height: 70px;
                    max-width: 180px;
                }

                .login-card h2 {
                    font-size: 1.6rem;
                    margin-bottom: 10px;
                }

                .captcha-display {
                    font-size: 1.2rem;
                    padding: 14px 16px;
                    min-height: 50px;
                }

                .captcha-refresh {
                    width: 50px;
                    height: 50px;
                }
            }

            /* Large tablets and desktops (769px+) */
            @media (min-width: 769px) {
                body {
                    padding: 24px;
                }

                .login-card {
                    padding: 48px 40px;
                    max-width: 500px;
                }

                .logo-container {
                    padding: 24px 20px;
                    margin-bottom: 28px;
                }

                .logo {
                    height: 80px;
                    max-width: 200px;
                }
            }
        </style>
    </head>
    <body>

    <div class="login-card">

        <!-- Logo Container - Single Logo Only -->
        <div class="logo-container">
            <img src="/admin/static/imtma_logo.png" class="logo" alt="IMTMA Logo">
        </div>

        <h2>🔐 Event Photo Delivery</h2>

        <!-- Flash messages (LOGIC UNCHANGED) -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="error">{{ message }}</div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <form method="post" action="{{ url_for('auth.login') }}" id="loginForm" onsubmit="return validateCaptcha()">

            <!-- Hidden user_type (LOGIC UNCHANGED) -->
            <input type="hidden" name="user_type" id="dynamicUserType" value="admin">
            <!-- Hidden correct answer for captcha validation -->
            <input type="hidden" name="captcha_answer" id="captchaAnswer" value="">

            <!-- EMAIL -->
            <div class="input-group">
                <label>📧 Email / Username</label>
                <input type="text" name="username" placeholder="Enter your email" required>
            </div>

            <!-- PASSWORD -->
            <div class="input-group">
                <label>🔑 Password</label>
                <div class="password-wrapper">
                    <input type="password" name="password" id="password"
                           placeholder="Enter password" required>
                    <span class="toggle-password" onclick="togglePassword()" id="toggleBtn">SHOW</span>
                </div>
            </div>

            <!-- MATH CAPTCHA WITH REFRESH -->
            <div class="captcha-container">
                <label class="captcha-label">🔢 Verify You're Human</label>
                <div class="captcha-problem-row">
                    <div class="captcha-display" id="captchaDisplay">5 + 3 = ?</div>
                    <button type="button" class="captcha-refresh" onclick="refreshCaptcha()" title="Generate new captcha">
                        <i class="fas fa-redo"></i>
                    </button>
                </div>
                <input type="text" class="captcha-input" id="captchaInput" placeholder="Enter your answer" inputmode="numeric" maxlength="3" required>
                <div class="captcha-error" id="captchaError">Incorrect answer, please try again</div>
            </div>

            <!-- BUTTON -->
            <button type="submit" class="login-btn" id="loginBtn">
                Login to Dashboard
            </button>

        </form>

    </div>

    <script>
    // Captcha system
    let captchaAnswer = 0;

    // Initialize captcha on page load
    document.addEventListener('DOMContentLoaded', function() {
        refreshCaptcha();
    });

    // Generate random math problem
    function refreshCaptcha() {
        const num1 = Math.floor(Math.random() * 20) + 1;  // 1-20
        const num2 = Math.floor(Math.random() * 20) + 1;  // 1-20
        const operators = ['+', '-'];
        const operator = operators[Math.floor(Math.random() * operators.length)];

        captchaAnswer = operator === '+' ? num1 + num2 : num1 - num2;

        document.getElementById('captchaDisplay').textContent = `${num1} ${operator} ${num2} = ?`;
        document.getElementById('captchaInput').value = '';
        document.getElementById('captchaError').classList.remove('show');
        document.getElementById('captchaAnswer').value = captchaAnswer;
    }

    // Validate captcha before form submission
    function validateCaptcha() {
        const userAnswer = parseInt(document.getElementById('captchaInput').value);
        const errorElement = document.getElementById('captchaError');

        if (userAnswer === captchaAnswer) {
            errorElement.classList.remove('show');
            return true;
        } else {
            errorElement.classList.add('show');
            document.getElementById('captchaInput').focus();
            return false;
        }
    }

    // Toggle password visibility
    function togglePassword() {
        var input = document.getElementById("password");
        var toggleBtn = document.getElementById("toggleBtn");

        if (input.type === "password") {
            input.type = "text";
            toggleBtn.textContent = "HIDE";
        } else {
            input.type = "password";
            toggleBtn.textContent = "SHOW";
        }
    }

    // Allow Enter key to refresh captcha
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Enter' && document.activeElement === document.getElementById('captchaInput')) {
            if (validateCaptcha()) {
                document.getElementById('loginForm').submit();
            }
        }
    });
    </script>

    </body>
    </html>