<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AniWorld Manager - Login</title>
    <link rel="stylesheet" href="/static/css/styles.css?v={{ static_v }}">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .login-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, var(--color-primary-light) 0%, var(--color-primary) 100%);
            padding: 1rem;
        }
        
        .login-card {
            background: var(--color-surface);
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
            border: 1px solid var(--color-border);
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 2rem;
        }
        
        .login-header .logo {
            font-size: 3rem;
            color: var(--color-primary);
            margin-bottom: 0.5rem;
        }
        
        .login-header h1 {
            margin: 0;
            color: var(--color-text);
            font-size: 1.5rem;
            font-weight: 600;
        }
        
        .login-header p {
            margin: 0.5rem 0 0 0;
            color: var(--color-text-secondary);
            font-size: 0.9rem;
        }
        
        .login-form {
            display: flex;
            flex-direction: column;
            gap: 1.5rem;
        }
        
        .form-group {
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
        }
        
        .form-label {
            font-weight: 500;
            color: var(--color-text);
            font-size: 0.9rem;
        }
        
        .password-input-group {
            position: relative;
        }
        
        .password-input {
            width: 100%;
            padding: 0.75rem 3rem 0.75rem 1rem;
            border: 2px solid var(--color-border);
            border-radius: 8px;
            font-size: 1rem;
            background: var(--color-background);
            color: var(--color-text);
            transition: all 0.2s ease;
            box-sizing: border-box;
        }
        
        .password-input:focus {
            outline: none;
            border-color: var(--color-primary);
            box-shadow: 0 0 0 3px rgba(var(--color-primary-rgb), 0.1);
        }
        
        .password-toggle {
            position: absolute;
            right: 0.75rem;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: var(--color-text-secondary);
            cursor: pointer;
            padding: 0.25rem;
            border-radius: 4px;
            transition: color 0.2s ease;
        }
        
        .password-toggle:hover {
            color: var(--color-primary);
        }
        
        .login-button {
            width: 100%;
            padding: 0.75rem;
            background: var(--color-primary);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
        }
        
        .login-button:hover:not(:disabled) {
            background: var(--color-primary-dark);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(var(--color-primary-rgb), 0.3);
        }
        
        .login-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .error-message {
            background: var(--color-error-light);
            color: var(--color-error);
            padding: 0.75rem;
            border-radius: 8px;
            border: 1px solid var(--color-error);
            font-size: 0.9rem;
            text-align: center;
        }
        
        .success-message {
            background: var(--color-success-light);
            color: var(--color-success);
            padding: 0.75rem;
            border-radius: 8px;
            border: 1px solid var(--color-success);
            font-size: 0.9rem;
            text-align: center;
        }
        
        .theme-toggle {
            position: absolute;
            top: 1rem;
            right: 1rem;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            color: white;
            padding: 0.5rem;
            border-radius: 50%;
            cursor: pointer;
            transition: all 0.2s ease;
            width: 2.5rem;
            height: 2.5rem;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .theme-toggle:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: scale(1.1);
        }
        
        .security-info {
            margin-top: 1.5rem;
            padding: 1rem;
            background: var(--color-info-light);
            border: 1px solid var(--color-info);
            border-radius: 8px;
            font-size: 0.8rem;
            color: var(--color-text-secondary);
            text-align: center;
        }
        
        .loading-spinner {
            width: 1rem;
            height: 1rem;
            border: 2px solid transparent;
            border-top: 2px solid currentColor;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <button class="theme-toggle" id="theme-toggle" title="Toggle theme">
            <i class="fas fa-moon"></i>
        </button>
        
        <div class="login-card">
            <div class="login-header">
                <div class="logo">
                    <i class="fas fa-play-circle"></i>
                </div>
                <h1>AniWorld Manager</h1>
                <p>Please enter your master password to continue</p>
            </div>
            
            <form class="login-form" id="login-form">
                <div class="form-group">
                    <label for="password" class="form-label">Master Password</label>
                    <div class="password-input-group">
                        <input 
                            type="password" 
                            id="password" 
                            name="password" 
                            class="password-input" 
                            placeholder="Enter your password"
                            required
                            autocomplete="current-password"
                            autofocus>
                        <button type="button" class="password-toggle" id="password-toggle" tabindex="-1">
                            <i class="fas fa-eye"></i>
                        </button>
                    </div>
                </div>
                
                <div id="message-container"></div>
                
                <button type="submit" class="login-button" id="login-button">
                    <i class="fas fa-sign-in-alt"></i>
                    <span>Login</span>
                </button>
            </form>
            
            <div class="security-info">
                <i class="fas fa-shield-alt"></i>
                Your session will expire after {{ session_timeout }} hours of inactivity.
                <br>
                After {{ max_attempts }} failed attempts, this IP will be locked for {{ lockout_duration }} minutes.
            </div>
        </div>
    </div>

    <script>
        // Theme toggle functionality
        const themeToggle = document.getElementById('theme-toggle');
        const htmlElement = document.documentElement;
        
        // Load saved theme
        const savedTheme = localStorage.getItem('theme') || 'light';
        htmlElement.setAttribute('data-theme', savedTheme);
        updateThemeIcon(savedTheme);
        
        themeToggle.addEventListener('click', () => {
            const currentTheme = htmlElement.getAttribute('data-theme');
            const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
            
            htmlElement.setAttribute('data-theme', newTheme);
            localStorage.setItem('theme', newTheme);
            updateThemeIcon(newTheme);
        });
        
        function updateThemeIcon(theme) {
            const icon = themeToggle.querySelector('i');
            icon.className = theme === 'dark' ? 'fas fa-sun' : 'fas fa-moon';
        }
        
        // Password visibility toggle
        const passwordToggle = document.getElementById('password-toggle');
        const passwordInput = document.getElementById('password');
        
        passwordToggle.addEventListener('click', () => {
            const type = passwordInput.getAttribute('type');
            const newType = type === 'password' ? 'text' : 'password';
            const icon = passwordToggle.querySelector('i');
            
            passwordInput.setAttribute('type', newType);
            icon.className = newType === 'password' ? 'fas fa-eye' : 'fas fa-eye-slash';
        });
        
        // Form submission
        const loginForm = document.getElementById('login-form');
        const loginButton = document.getElementById('login-button');
        const messageContainer = document.getElementById('message-container');
        
        loginForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const password = passwordInput.value.trim();
            if (!password) {
                showMessage('Please enter your password', 'error');
                return;
            }
            
            setLoading(true);
            
            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ password })
                });
                
                const data = await response.json();
                
                if (response.ok && data.access_token) {
                    // Store JWT token in localStorage
                    localStorage.setItem('access_token', data.access_token);
                    if (data.expires_at) {
                        localStorage.setItem('token_expires_at', data.expires_at);
                    }
                    showMessage('Login successful', 'success');
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 1000);
                } else {
                    const errorMessage = data.detail || data.message || 'Invalid credentials';
                    showMessage(errorMessage, 'error');
                    passwordInput.value = '';
                    passwordInput.focus();
                }
            } catch (error) {
                showMessage('Connection error. Please try again.', 'error');
                console.error('Login error:', error);
            } finally {
                setLoading(false);
            }
        });
        
        function showMessage(message, type) {
            messageContainer.innerHTML = `
                <div class="${type}-message">
                    ${message}
                </div>
            `;
        }
        
        function setLoading(loading) {
            loginButton.disabled = loading;
            const buttonText = loginButton.querySelector('span');
            const buttonIcon = loginButton.querySelector('i');
            
            if (loading) {
                buttonIcon.className = 'loading-spinner';
                buttonText.textContent = 'Logging in...';
            } else {
                buttonIcon.className = 'fas fa-sign-in-alt';
                buttonText.textContent = 'Login';
            }
        }
        
        // Clear message on input
        passwordInput.addEventListener('input', () => {
            messageContainer.innerHTML = '';
        });
        
        // Enter key on password toggle
        passwordToggle.addEventListener('keydown', (e) => {
            if (e.key === 'Enter' || e.key === ' ') {
                e.preventDefault();
                passwordToggle.click();
            }
        });
    </script>
</body>
</html>