<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Interactive Learning - Sign In</title>
    <link rel="icon" type="image/x-icon" href="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRXH01KD_9TMda5AESIluex5Jg2BDv71A07hA&s">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            --secondary-gradient: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            --accent-gradient: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            --light-bg: #f8fafc;
            --card-bg: #ffffff;
            --text-primary: #2d3748;
            --text-secondary: #718096;
            --border-color: #e2e8f0;
            --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
            --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }

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

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: var(--light-bg);
            color: var(--text-primary);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow-x: hidden;
        }

        /* Animated Background */
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: 
                radial-gradient(circle at 20% 50%, rgba(102, 126, 234, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(118, 75, 162, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 40% 20%, rgba(79, 172, 254, 0.1) 0%, transparent 50%);
            animation: float 20s ease-in-out infinite;
            z-index: 0;
        }

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

        /* Premium Header */
        .premium-header {
            background: var(--primary-gradient);
            backdrop-filter: blur(20px);
            padding: 1.5rem 0;
            box-shadow: var(--shadow-lg);
            position: relative;
            z-index: 10;
        }

        .premium-header h1 {
            color: white;
            font-weight: 700;
            font-size: clamp(1.8rem, 4vw, 2.5rem);
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin: 0;
            text-align: center;
        }

        /* Main Container */
        .login-wrapper {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 2rem 1rem;
            position: relative;
            z-index: 1;
        }

        .login-container {
            background: var(--card-bg);
            padding: 3rem 2.5rem;
            border-radius: 24px;
            box-shadow: var(--shadow-xl);
            width: 100%;
            max-width: 480px;
            border: 1px solid var(--border-color);
            animation: fadeInUp 0.6s ease-out;
            position: relative;
            overflow: hidden;
        }

        .login-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 6px;
            background: var(--primary-gradient);
        }

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

        .welcome-badge {
            display: inline-block;
            background: var(--accent-gradient);
            color: white;
            padding: 0.5rem 1.5rem;
            border-radius: 50px;
            font-size: 0.85rem;
            font-weight: 600;
            margin-bottom: 1.5rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .login-container h2 {
            font-size: 2rem;
            margin-bottom: 0.75rem;
            color: var(--text-primary);
            font-weight: 700;
        }

        .login-container p {
            color: var(--text-secondary);
            font-size: 1rem;
            margin-bottom: 2rem;
            line-height: 1.6;
        }

        /* Form Styling */
        .form-group {
            margin-bottom: 1.5rem;
        }

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

        .form-control {
            border: 2px solid var(--border-color);
            border-radius: 12px;
            padding: 0.875rem 1rem;
            font-size: 1rem;
            transition: all 0.3s ease;
            width: 100%;
        }

        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            outline: none;
        }

        .form-control::placeholder {
            color: #a0aec0;
        }

        /* Google Button */
        .google-btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            background: white;
            color: var(--text-primary);
            border: 2px solid var(--border-color);
            border-radius: 12px;
            padding: 1rem 2rem;
            text-decoration: none;
            font-size: 1rem;
            font-weight: 600;
            transition: all 0.3s ease;
            width: 100%;
            margin-top: 1rem;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
            position: relative;
            overflow: hidden;
        }

        .google-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: var(--primary-gradient);
            transition: left 0.3s ease;
            z-index: -1;
        }

        .google-btn:hover::before {
            left: 0;
        }

        .google-btn:hover {
            color: white;
            border-color: transparent;
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
        }

        .google-btn i {
            margin-right: 12px;
            font-size: 1.3em;
            transition: transform 0.3s ease;
        }

        .google-btn:hover i {
            transform: scale(1.1);
        }

        /* Error Message */
        .error-message {
            background: linear-gradient(135deg, #fee, #fdd);
            border: 1px solid #fcc;
            border-radius: 12px;
            padding: 1rem;
            margin-top: 1rem;
            color: #c33;
            font-size: 0.95rem;
            display: none;
            animation: shake 0.5s ease;
        }

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

        .error-message i {
            margin-right: 0.5rem;
        }

        /* Divider */
        .divider {
            display: flex;
            align-items: center;
            text-align: center;
            margin: 2rem 0;
            color: var(--text-secondary);
            font-size: 0.9rem;
        }

        .divider::before,
        .divider::after {
            content: '';
            flex: 1;
            border-bottom: 1px solid var(--border-color);
        }

        .divider span {
            padding: 0 1rem;
            font-weight: 500;
        }

        /* Features List */
        .features-list {
            margin-top: 2rem;
            padding-top: 2rem;
            border-top: 1px solid var(--border-color);
        }

        .feature-item {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            margin-bottom: 1rem;
            color: var(--text-secondary);
            font-size: 0.95rem;
        }

        .feature-item i {
            color: #667eea;
            font-size: 1.1rem;
        }

        /* Footer */
        .login-footer {
            text-align: center;
            padding: 2rem 1rem;
            color: var(--text-secondary);
            font-size: 0.9rem;
            position: relative;
            z-index: 1;
        }

        .login-footer a {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
        }

        .login-footer a:hover {
            text-decoration: underline;
        }

        /* Loading Animation */
        .loading-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.95);
            z-index: 9999;
            justify-content: center;
            align-items: center;
        }

        .loading-spinner {
            width: 50px;
            height: 50px;
            border: 4px solid var(--border-color);
            border-top: 4px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .login-container {
                padding: 2rem 1.5rem;
            }

            .login-container h2 {
                font-size: 1.75rem;
            }

            .features-list {
                display: none;
            }
        }

        @media (max-width: 480px) {
            .premium-header h1 {
                font-size: 1.5rem;
            }

            .login-container {
                padding: 1.5rem 1.25rem;
            }

            .login-container h2 {
                font-size: 1.5rem;
            }

            .google-btn {
                padding: 0.875rem 1.5rem;
                font-size: 0.95rem;
            }
        }
    </style>
</head>

<body>
    <!-- Loading Overlay -->
    <div class="loading-overlay" id="loadingOverlay">
        <div class="loading-spinner"></div>
    </div>

    <!-- Premium Header -->
    <header class="premium-header">
        <h1><i class="fas fa-graduation-cap me-2"></i>Interactive Learning</h1>
    </header>

    <!-- Main Content -->
    <div class="login-wrapper">
        <div class="login-container">
            <span class="welcome-badge">
                <i class="fas fa-sparkles me-1"></i>Welcome
            </span>
            
            <h2>Sign In to Continue</h2>
            <p>Access your personalized learning experience</p>

            <div class="form-group">
                <label for="inviteCodeInput" class="form-label">
                    <i class="fas fa-ticket-alt me-2"></i>Invitation Code
                </label>
                <input 
                    type="text" 
                    class="form-control" 
                    id="inviteCodeInput" 
                    placeholder="Enter your invitation code"
                >
            </div>

            <div class="divider">
                <span>Continue with</span>
            </div>

            <a id="googleLoginBtn" class="google-btn" href="#">
                <i class="fab fa-google"></i>
                Sign in with Google
            </a>

            <div class="error-message" id="errorMessage">
                <i class="fas fa-exclamation-circle"></i>
                <span id="errorText"></span>
            </div>

            <div class="features-list">
                <div class="feature-item">
                    <i class="fas fa-check-circle"></i>
                    <span>Personalized learning paths</span>
                </div>
                <div class="feature-item">
                    <i class="fas fa-check-circle"></i>
                    <span>Track your progress</span>
                </div>
                <div class="feature-item">
                    <i class="fas fa-check-circle"></i>
                    <span>Multi-language support</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="login-footer">
        <p>Need help? <a href="#">Contact Support</a> | <a href="/policy">Privacy Policy</a></p>
        <p>&copy; 2025 Interactive Learning. All rights reserved.</p>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const errorMessageDiv = document.getElementById('errorMessage');
            const errorTextSpan = document.getElementById('errorText');
            const googleLoginBtn = document.getElementById('googleLoginBtn');
            const inviteCodeInput = document.getElementById('inviteCodeInput');
            const loadingOverlay = document.getElementById('loadingOverlay');

            // Set the initial value of the input box if a code is present in the URL
            const initialInviteCode = "{{ invite_code }}";
            if (initialInviteCode) {
                inviteCodeInput.value = initialInviteCode;
            }

            // Function to update the Google login button's href with the current invite code
            function updateGoogleLoginLink() {
                const code = inviteCodeInput.value.trim() || 'default_code';
                const baseUrl = 'https://lm.warals.codetorise.com/auth/google/login/';
                googleLoginBtn.href = baseUrl + code;
            }

            // Update the link immediately on page load
            updateGoogleLoginLink();

            // Add an event listener to update the link whenever the input changes
            inviteCodeInput.addEventListener('input', updateGoogleLoginLink);

            // Show loading when clicking login button
            googleLoginBtn.addEventListener('click', function() {
                loadingOverlay.style.display = 'flex';
            });

            // Helper: Get query parameters
            function getQueryParams() {
                const params = {};
                window.location.search.substring(1).split("&").forEach(part => {
                    const [key, value] = part.split("=");
                    if (key && value) {
                        params[decodeURIComponent(key)] = decodeURIComponent(value);
                    }
                });
                return params;
            }

            const queryParams = getQueryParams();

            // Show error if FastAPI callback returned an error param
            if (queryParams.error) {
                errorTextSpan.textContent = "Login failed: " + queryParams.error;
                errorMessageDiv.style.display = 'block';
            }

            try {
                // Handle token if returned in URL fragment (#access_token=...)
                const fragmentParams = new URLSearchParams(window.location.hash.substring(1));
                const accessToken = fragmentParams.get('access_token');
                const expiresIn = fragmentParams.get('expires_in');

                if (accessToken && expiresIn) {
                    // Show loading
                    loadingOverlay.style.display = 'flex';
                    
                    // Save token & expiry
                    localStorage.setItem('access_token', accessToken);
                    const expiresAt = Date.now() + parseInt(expiresIn) * 1000;
                    localStorage.setItem('token_expires_at', expiresAt);

                    // Redirect to home
                    setTimeout(() => {
                        window.location.href = '/home';
                    }, 500);
                }

            } catch (e) {
                console.error("Login Error:", e);
                errorTextSpan.textContent = "An error occurred during login. Please try again.";
                errorMessageDiv.style.display = 'block';
                loadingOverlay.style.display = 'none';

                // Clean up fragment
                if (window.location.hash) {
                    window.location.hash = '';
                }
            }

            // Auto-redirect if a token is already stored
            const storedToken = localStorage.getItem('access_token');
            const expiresAt = localStorage.getItem('token_expires_at');

            if (storedToken && expiresAt && Date.now() < parseInt(expiresAt)) {
                loadingOverlay.style.display = 'flex';
                window.location.href = '/home';
            }
        });
    </script>
</body>

</html>