<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Local Deep Research</title>

    <!-- Vite HMR for development -->
    {{ vite_hmr() }}

    <!-- SafeLogger must load before app.js which uses it at init -->
    <script src="/static/js/security/safe-logger.js"></script>
    <!-- Load all vendor dependencies and styles through Vite -->
    {{ vite_asset('js/app.js') }}

    <!-- Keep local styles -->
    <link rel="stylesheet" href="/static/css/styles.css">
    <style>
        /* Override main layout for auth pages */
        body {
            min-height: 100vh;
            background: var(--bg-primary);
            background-image:
                radial-gradient(ellipse at top left, rgba(var(--accent-primary-rgb), 0.1) 0%, transparent 50%),
                radial-gradient(ellipse at bottom right, rgba(var(--accent-tertiary-rgb), 0.1) 0%, transparent 50%);
            padding: 0;
            margin: 0;
            overflow-x: hidden;
        }

        .ldr-auth-wrapper {
            min-height: 100vh;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 2rem;
        }

        .ldr-auth-container {
            width: 100%;
            max-width: 450px;
        }

        /* Responsive - adjust for small screens */
        @media (max-width: 600px) {
            .ldr-auth-container {
                max-width: 100%;
                padding: 0 1rem;
            }

            .ldr-card-content {
                padding: 2rem 1.5rem !important;
            }
        }

        .ldr-auth-header {
            text-align: center;
            margin-bottom: 3rem;
        }

        .ldr-auth-header h1 {
            font-size: 2rem;
            font-weight: 600;
            color: var(--text-primary);
            margin-bottom: 0.5rem;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.75rem;
        }

        .ldr-auth-header h1 i {
            color: var(--accent-primary);
            font-size: 1.75rem;
        }

        .ldr-auth-header p {
            color: var(--text-secondary);
            font-size: 1.1rem;
        }

        .ldr-card {
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
            border-radius: 16px;
            box-shadow: var(--card-shadow);
            overflow: hidden;
        }

        .ldr-card-content {
            padding: 2rem 2.5rem;
        }

        .ldr-encryption-banner {
            background: linear-gradient(135deg, rgba(110, 79, 246, 0.1) 0%, rgba(64, 191, 255, 0.1) 100%);
            border: 1px solid rgba(110, 79, 246, 0.3);
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            display: flex;
            align-items: center;
            gap: 1.25rem;
        }

        .ldr-encryption-banner i {
            color: var(--accent-primary);
            font-size: 2rem;
        }

        .ldr-encryption-banner-content {
            flex: 1;
        }

        .ldr-encryption-banner-content strong {
            color: var(--text-primary);
            display: block;
            margin-bottom: 0.5rem;
            font-size: 1.1rem;
        }

        .ldr-encryption-banner-content span {
            color: var(--text-secondary);
            font-size: 1rem;
            line-height: 1.5;
        }

        .ldr-security-warning {
            background: rgba(249, 188, 11, 0.1);
            border: 1px solid rgba(249, 188, 11, 0.3);
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1.5rem;
            color: var(--warning-color);
        }

        .ldr-security-warning i {
            margin-right: 0.5rem;
        }

        .alert {
            background: rgba(var(--error-color-rgb), 0.1);
            border: 1px solid rgba(var(--error-color-rgb), 0.3);
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1rem;
            color: var(--error-color);
            position: relative;
        }

        .alert-success {
            background: rgba(var(--success-color-rgb), 0.1);
            border-color: rgba(var(--success-color-rgb), 0.3);
            color: var(--success-color);
        }

        .alert-info {
            background: rgba(var(--accent-tertiary-rgb), 0.1);
            border-color: rgba(var(--accent-tertiary-rgb), 0.3);
            color: var(--accent-tertiary);
        }

        .alert-dismissible {
            padding-right: 3rem;
        }

        .btn-close {
            position: absolute;
            top: 1rem;
            right: 1rem;
            background: none;
            border: none;
            color: inherit;
            opacity: 0.5;
            cursor: pointer;
            font-size: 1.2rem;
            line-height: 1;
            padding: 0;
            width: auto;
            height: auto;
        }

        .btn-close:hover {
            opacity: 1;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

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

        .ldr-form-control {
            width: 100%;
            padding: 0.75rem 1rem;
            min-height: 44px;
            background: var(--bg-tertiary);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            color: var(--text-primary);
            font-size: 1rem;
            transition: all 0.2s;
        }

        .ldr-form-control:focus {
            outline: none;
            border-color: var(--accent-primary);
            box-shadow: 0 0 0 3px rgba(110, 79, 246, 0.1);
        }

        .ldr-form-control::placeholder {
            color: var(--text-muted);
        }

        .ldr-checkbox-label {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            cursor: pointer;
            color: var(--text-secondary);
            margin-bottom: 1.5rem;
            font-size: 0.95rem;
        }

        .ldr-checkbox-label input[type="checkbox"] {
            width: 1.25rem;
            height: 1.25rem;
            background: var(--bg-tertiary);
            border: 1px solid var(--border-color);
            border-radius: 4px;
            cursor: pointer;
            flex-shrink: 0;
        }

        .ldr-checkbox-label input[type="checkbox"]:checked {
            background: var(--accent-primary);
            border-color: var(--accent-primary);
        }

        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            padding: 0.875rem 2rem;
            background: var(--accent-primary);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            text-decoration: none;
            width: 100%;
        }

        .btn:hover {
            background: var(--accent-secondary);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(110, 79, 246, 0.3);
        }

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

        .ldr-auth-links {
            text-align: center;
            margin-top: 1.5rem;
            padding-top: 1.5rem;
            border-top: 1px solid var(--border-color);
        }

        .ldr-auth-links p {
            color: var(--text-secondary);
            margin-bottom: 0.5rem;
            font-size: 0.95rem;
        }

        .ldr-auth-links a {
            color: var(--accent-tertiary);
            text-decoration: none;
            font-weight: 500;
            font-size: 0.95rem;
        }

        .ldr-auth-links a:hover {
            color: var(--accent-secondary);
        }

        .ldr-browser-hint {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            color: var(--text-muted);
            font-size: 0.875rem;
            margin-top: 1rem;
        }

        .ldr-browser-hint i {
            font-size: 1rem;
        }
    </style>
</head>
<body>
    <div class="ldr-auth-wrapper">
        <div class="ldr-auth-container">
            <div class="ldr-auth-header">
                <h1><i aria-hidden="true" class="fas fa-flask"></i> Local Deep Research</h1>
                <p>Secure Research Platform</p>
            </div>

            <div class="ldr-card">
                <div class="ldr-card-content">
                    <div class="ldr-encryption-banner">
                        <i aria-hidden="true" class="fas fa-shield-alt"></i>
                        <div class="ldr-encryption-banner-content">
                            <strong>Your data is encrypted</strong>
                            <span>Each user has their own encrypted database protected by their password</span>
                        </div>
                    </div>

                {% if not has_encryption %}
                    <div class="ldr-security-warning">
                        <i aria-hidden="true" class="fas fa-exclamation-triangle"></i>
                        <strong>Security Warning:</strong>
                        Database encryption is not available. Your data will be stored unencrypted.
                        <br><small>Install SQLCipher for secure storage.</small>
                    </div>
                {% endif %}

                {% with messages = get_flashed_messages(with_categories=true) %}
                    {% if messages %}
                        {% for category, message in messages %}
                            {% if category == 'success' %}
                            <div class="alert alert-success alert-dismissible">
                            {% elif category == 'info' %}
                            <div class="alert alert-info alert-dismissible">
                            {% else %}
                            <div class="alert alert-warning alert-dismissible">
                            {% endif %}
                                {{ message }}
                                <button type="button" class="btn-close" onclick="this.parentElement.style.display='none'">×</button>
                            </div>
                        {% endfor %}
                    {% endif %}
                {% endwith %}

                <form method="post" action="{{ url_for('auth.login', next=next_page) if next_page else url_for('auth.login') }}" autocomplete="on">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>

                    <div class="form-group">
                        <label for="username">Username</label>
                        <input type="text"
                               class="ldr-form-control"
                               id="username"
                               name="username"
                               placeholder="Enter your username"
                               autocomplete="username"
                               required
                               aria-required="true"
                               autofocus>
                    </div>

                    <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password"
                               class="ldr-form-control"
                               id="password"
                               name="password"
                               placeholder="Enter your password"
                               autocomplete="current-password"
                               required
                               aria-required="true">
                    </div>

                    <label class="ldr-checkbox-label">
                        <input type="checkbox"
                               id="remember"
                               name="remember"
                               value="true">
                        <span>Remember me for 30 days</span>
                    </label>

                    <button type="submit" class="btn">
                        <i aria-hidden="true" class="fas fa-sign-in-alt"></i>
                        Login
                    </button>
                </form>

                <div class="ldr-auth-links">
                    {% if allow_registrations %}
                    <p>Don't have an account? <a href="{{ url_for('auth.register') }}">Register here</a></p>
                    {% endif %}
                    <div class="ldr-browser-hint">
                        <i aria-hidden="true" class="fas fa-info-circle"></i>
                        <span>Your browser can save your password for easy login</span>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Clear theme localStorage on logout to prevent cross-user theme bleeding -->
    <script>
        (function() {
            // Check if user was just logged out (via flash message or URL parameter)
            var wasLoggedOut = {{ 'true' if session.get('just_logged_out') else 'false' }};
            var urlParams = new URLSearchParams(window.location.search);
            if (wasLoggedOut || urlParams.has('logged_out')) {
                // Clear all theme keys from localStorage
                var keysToRemove = [];
                for (var i = 0; i < localStorage.length; i++) {
                    var key = localStorage.key(i);
                    if (key && key.startsWith('ldr-theme')) {
                        keysToRemove.push(key);
                    }
                }
                keysToRemove.forEach(function(key) {
                    localStorage.removeItem(key);
                });
                if (keysToRemove.length > 0) {
                    console.log('Theme localStorage cleared on logout');
                }
            }
        })();
    </script>
</body>
</html>
