/* ============================================================
   RAG Document Chat — Professional Theme v3
   Design rules: ONE accent color, neutral surfaces, calm motion.
   No competing gradients, no decorative animation loops.
   ============================================================ */

/* ==================== Design Tokens ==================== */
:root {
    /* Single accent, softened for long sessions. One hue only. */
    --primary: #6d78ff;
    --primary-dark: #5560ef;
    --primary-light: #9aa3ff;
    --primary-soft: rgba(109, 120, 255, 0.14);
    --primary-ring: rgba(109, 120, 255, 0.30);
    --secondary: #6d78ff; /* kept for compatibility; single accent hue */
    --brand-gradient: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
    --success: #22c55e;
    --danger: #f04438;
    --danger-dark: #d92d20;
    --warning: #f79009;

    /* Cooler, deeper neutrals with clearer separation between layers */
    --bg-primary: #0b0d13;
    --bg-secondary: #12151d;
    --bg-tertiary: #1a1f2b;
    --bg-elevated: #171b26;
    --glass-bg: rgba(18, 21, 29, 0.94);

    --text-primary: #eaecf5;
    --text-secondary: #a5adc0;
    --text-muted: #737b8f;

    --border: rgba(148, 163, 184, 0.12);
    --border-strong: rgba(148, 163, 184, 0.22);
    /* Layered shadows read as depth instead of a hard edge */
    --shadow: 0 18px 48px rgba(0, 0, 0, 0.48);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.30), 0 4px 14px rgba(0, 0, 0, 0.20);

    --radius: 16px;
    --radius-sm: 10px;
    --radius-pill: 999px;

    /* Motion: compositor-friendly properties only. Never animates layout. */
    --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
    --transition: background-color 0.18s var(--ease), border-color 0.18s var(--ease),
        color 0.18s var(--ease), box-shadow 0.18s var(--ease),
        transform 0.18s var(--ease), opacity 0.18s var(--ease);
}

[data-theme="light"] {
    --primary: #5b5ef2;
    --primary-dark: #4a4ddb;
    --primary-light: #7b83ff;
    --primary-soft: rgba(91, 94, 242, 0.10);
    --primary-ring: rgba(91, 94, 242, 0.24);
    --secondary: #5b5ef2;
    --brand-gradient: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
    --success: #12805c;
    --danger: #d92d20;
    --danger-dark: #b42318;
    --warning: #b54708;

    --bg-primary: #f6f7fb;
    --bg-secondary: #ffffff;
    --bg-tertiary: #eef0f7;
    --bg-elevated: #ffffff;
    --glass-bg: rgba(255, 255, 255, 0.94);

    --text-primary: #101319;
    --text-secondary: #4a5261;
    --text-muted: #6b7280;

    --border: rgba(16, 19, 25, 0.09);
    --border-strong: rgba(16, 19, 25, 0.16);
    --shadow: 0 18px 44px rgba(16, 19, 25, 0.12);
    --shadow-sm: 0 1px 2px rgba(16, 19, 25, 0.06), 0 4px 12px rgba(16, 19, 25, 0.05);
}

/* ==================== Reset & Base ==================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
        'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow: hidden;
    transition: background 0.2s ease, color 0.2s ease;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body, html {
    height: 100%;
    width: 100%;
    position: fixed;
    overscroll-behavior: none;
}

.chat-messages,
.document-list,
.modal-content,
.about-content,
.document-summary-content {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.text-muted { color: var(--text-muted); }

/* ==================== Scrollbar ==================== */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 20px;
    border: 2px solid transparent;
    background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--text-muted); }

/* ==================== Surfaces ==================== */
.glass {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}

.gradient-text {
    color: var(--primary);
    font-weight: 700;
}

/* ==================== Modal ==================== */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(8, 10, 16, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.18s ease;
}

.modal.active { display: flex; }

.modal-content {
    width: 90%;
    max-width: 460px;
    padding: 36px;
    border-radius: var(--radius);
    animation: slideUp 0.22s ease;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
}

.modal-overlay { position: absolute; inset: 0; }

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
}

.modal-header h2 {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.25em;
    font-weight: 600;
}

.modal-close {
    padding: 8px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.2em;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: var(--transition);
    line-height: 1;
}

.modal-close:hover { color: var(--danger); background: var(--bg-tertiary); }

.modal-body { padding: 4px 0 12px; }

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

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

/* ==================== Auth ==================== */
.auth-header { text-align: center; margin-bottom: 26px; }

.auth-header h1 {
    font-size: 1.7em;
    margin-bottom: 8px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.auth-header p { color: var(--text-secondary); font-size: 0.95em; }

.auth-branding {
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid var(--border);
}

.auth-branding small { color: var(--text-muted); font-size: 0.85em; }
.auth-branding strong { color: var(--primary); font-weight: 600; }

.auth-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 26px;
    background: var(--bg-tertiary);
    padding: 4px;
    border-radius: var(--radius-sm);
}

.auth-tab {
    flex: 1;
    padding: 10px;
    background: transparent;
    border: none;
    border-radius: 7px;
    color: var(--text-secondary);
    font-size: 0.95em;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}

.auth-tab:hover { color: var(--text-primary); }

.auth-tab.active {
    background: var(--bg-secondary);
    color: var(--primary);
    box-shadow: var(--shadow-sm);
}

.auth-form { display: none; }
.auth-form.active { display: block; animation: fadeIn 0.18s ease; }

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

.form-group label {
    display: block;
    margin-bottom: 7px;
    color: var(--text-secondary);
    font-size: 0.88em;
    font-weight: 600;
}

.form-group label i { margin-right: 6px; color: var(--primary); }

.form-group input {
    width: 100%;
    padding: 12px 14px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.95em;
    transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.form-group input::placeholder { color: var(--text-muted); }

.form-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
    background: var(--bg-secondary);
}

.error-message {
    margin-top: 12px;
    padding: 11px 13px;
    background: rgba(220, 38, 38, 0.08);
    border: 1px solid rgba(220, 38, 38, 0.3);
    border-radius: var(--radius-sm);
    color: var(--danger);
    font-size: 0.88em;
    display: none;
}

.error-message.active { display: block; }

/* Validation failures can list several problems; keep them on separate lines */
.error-message {
    white-space: pre-line;
    line-height: 1.5;
}

/* ==================== Buttons ==================== */
.btn {
    padding: 12px 20px;
    background: var(--primary);
    color: #ffffff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95em;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 8px;
    justify-content: center;
    width: 100%;
}

.btn:hover { background: var(--primary-dark); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-small { padding: 9px 14px; font-size: 0.85em; width: auto; }
.btn-icon { padding: 9px; width: auto; }

.btn-secondary {
    background: transparent;
    color: var(--text-primary);
    border: 1px solid var(--border-strong);
    width: auto;
}

.btn-secondary:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.btn-danger { background: var(--danger); width: auto; }
.btn-danger:hover { background: var(--danger-dark); }

/* ==================== App Shell ==================== */
.app-container {
    display: none;
    height: 100vh;
    overflow: hidden;
}

.app-container.active { display: flex; }

/* ==================== Sidebar ==================== */
.sidebar {
    width: 300px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.sidebar-header {
    padding: 18px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h2 { font-size: 1.15em; font-weight: 700; }

.search-box {
    padding: 12px 16px;
    position: relative;
    border-bottom: 1px solid var(--border);
}

.search-box i {
    position: absolute;
    left: 30px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 0.85em;
}

.search-box input {
    width: 100%;
    padding: 9px 12px 9px 32px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.9em;
    transition: border-color 0.18s ease;
}

.search-box input:focus {
    outline: none;
    border-color: var(--primary);
}

.document-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.document-item {
    padding: 12px;
    margin-bottom: 6px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    gap: 12px;
    position: relative;
}

.document-item:hover {
    background: var(--bg-tertiary);
}

.document-item.active {
    background: var(--primary-soft);
    border-color: var(--primary);
}

.document-item .doc-icon { font-size: 1.3em; flex-shrink: 0; }
.document-item .doc-details { flex: 1; min-width: 0; }

.document-item .doc-name {
    font-weight: 600;
    font-size: 0.92em;
    margin-bottom: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.document-item .doc-info { font-size: 0.78em; color: var(--text-muted); }

.doc-actions { display: flex; gap: 5px; margin-left: auto; flex-shrink: 0; }

.doc-view-btn,
.doc-delete-btn {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-muted);
    width: 30px;
    height: 30px;
    border-radius: 7px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    font-size: 13px;
    opacity: 0;
}

.document-item:hover .doc-view-btn,
.document-item:hover .doc-delete-btn { opacity: 1; }

.doc-view-btn:hover { background: var(--primary); color: #fff; }
.doc-delete-btn:hover { background: var(--danger); color: #fff; }

.doc-summary-preview {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 5px;
    padding-top: 5px;
    border-top: 1px solid var(--border);
    max-height: 40px;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
}

.sidebar-footer {
    padding: 14px 18px;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
}

.user-info { flex: 1; display: flex; align-items: center; gap: 11px; min-width: 0; }

.user-info i { font-size: 1.9em; color: var(--primary); }

.user-name {
    font-weight: 600;
    font-size: 0.9em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-email {
    font-size: 0.74em;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-branding {
    padding: 12px 18px;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
}

.branding-info { text-align: center; margin-bottom: 8px; }

.branding-info small {
    display: block;
    color: var(--text-muted);
    font-size: 0.72em;
    margin-bottom: 2px;
}

.branding-info strong { color: var(--primary); font-size: 0.9em; font-weight: 600; }

.branding-links { display: flex; justify-content: center; gap: 14px; }

.branding-links a {
    color: var(--text-muted);
    font-size: 1.05em;
    transition: var(--transition);
    text-decoration: none;
}

.branding-links a:hover { color: var(--primary); }

/* ==================== Main Content ==================== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    min-width: 0;
}

.chat-header {
    padding: 16px 28px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.chat-info { min-width: 0; }

.chat-info h2 {
    font-size: 1.2em;
    margin-bottom: 2px;
    font-weight: 650;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-info p {
    font-size: 0.82em;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.header-actions { display: flex; gap: 8px; flex-shrink: 0; }

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 28px;
    scroll-behavior: smooth;
}

/* ==================== Welcome ==================== */
.welcome-message {
    text-align: center;
    padding: 56px 20px;
    max-width: 620px;
    margin: 0 auto;
}

.welcome-icon {
    width: 72px;
    height: 72px;
    margin: 0 auto 18px;
    border-radius: 20px;
    background: var(--primary-soft);
    color: var(--primary);
    font-size: 2em;
    display: flex;
    align-items: center;
    justify-content: center;
}

.welcome-message h2 { margin-bottom: 12px; font-size: 1.7em; font-weight: 700; letter-spacing: -0.01em; }

.welcome-message p { color: var(--text-secondary); margin-bottom: 22px; font-size: 1.02em; }

.welcome-branding {
    margin: 18px 0;
    padding: 14px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.welcome-branding p { margin: 4px 0; }

.welcome-branding strong { color: var(--primary); font-size: 1.05em; }

.quick-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 18px;
}

.quick-action {
    padding: 12px 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-weight: 600;
    font-size: 0.95em;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.quick-action:hover {
    background: var(--primary);
    border-color: var(--primary);
    color: #ffffff;
}

/* ==================== Messages ==================== */
.message {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
    animation: messageIn 0.22s ease;
}

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

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.95em;
    flex-shrink: 0;
    margin-top: 2px;
}

.message.user .message-avatar {
    background: var(--primary);
    color: #ffffff;
}

.message.assistant .message-avatar {
    background: var(--bg-secondary);
    border: 1px solid var(--border-strong);
    color: var(--primary);
}

.message-content {
    flex: 1;
    min-width: 0;
    max-width: 82%;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    padding: 12px 16px;
    border-radius: var(--radius);
    border-top-left-radius: 6px;
    box-shadow: var(--shadow-sm);
}

.message.user .message-content {
    background: var(--primary);
    border-color: var(--primary);
    color: #ffffff;
    border-top-left-radius: var(--radius);
    border-top-right-radius: 6px;
}

.message-text {
    line-height: 1.62;
    word-wrap: break-word;
    overflow-wrap: break-word;
    font-size: 0.97em;
}

.message-text strong { font-weight: 650; }

.message.user .message-text strong { color: #ffffff; }

.message-text code {
    font-family: 'SF Mono', 'Cascadia Code', Consolas, monospace;
    font-size: 0.88em;
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 5px;
}

.message.user .message-text code { background: rgba(255, 255, 255, 0.18); }

.message-text pre {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px;
    overflow-x: auto;
    margin: 8px 0;
}

.message.user .message-text pre { background: rgba(255, 255, 255, 0.12); border-color: rgba(255, 255, 255, 0.2); }

.message-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
}

.message.user .message-actions { border-top-color: rgba(255, 255, 255, 0.18); }

.speaker-btn {
    padding: 6px 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 7px;
    color: var(--text-secondary);
    font-size: 0.85em;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.speaker-btn:hover { color: var(--primary); border-color: var(--primary); }

.message.user .speaker-btn {
    background: rgba(255, 255, 255, 0.12);
    border-color: rgba(255, 255, 255, 0.22);
    color: #ffffff;
}

.message.user .speaker-btn:hover { border-color: #ffffff; }

.message-sources {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.message.user .message-sources { border-top-color: rgba(255, 255, 255, 0.18); }

.message-sources h4 {
    font-size: 0.8em;
    margin-bottom: 8px;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.source-item {
    padding: 9px 11px;
    background: var(--bg-tertiary);
    border-radius: 7px;
    margin-bottom: 7px;
    font-size: 0.84em;
}

.source-name {
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 3px;
}

.source-text { color: var(--text-secondary); font-size: 0.92em; }

.message-time {
    font-size: 0.72em;
    color: var(--text-muted);
    margin-top: 7px;
}

.message.user .message-time { color: rgba(255, 255, 255, 0.65); }

/* ==================== Typing Indicator ==================== */
.typing-indicator { display: flex; gap: 5px; padding: 6px 4px; }

.typing-dot {
    width: 7px;
    height: 7px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typing 1.2s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* ==================== Composer ==================== */
.chat-input-container {
    padding: 14px 24px 12px;
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
}

.input-wrapper { display: flex; gap: 10px; align-items: flex-end; }

#messageInput {
    flex: 1;
    padding: 12px 16px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--text-primary);
    font-size: 0.95em;
    font-family: inherit;
    resize: none;
    max-height: 150px;
    transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

#messageInput:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

#messageInput::placeholder { color: var(--text-muted); }

.input-actions { display: flex; gap: 8px; align-items: center; }

.input-actions .btn {
    width: 42px;
    height: 42px;
    padding: 0;
    border-radius: 50%;
    font-size: 0.95em;
}

#sendBtn { display: flex; }
#stopBtn { display: none; }

.input-hint {
    margin-top: 8px;
    font-size: 0.74em;
    color: var(--text-muted);
    text-align: center;
}

kbd {
    padding: 2px 6px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-strong);
    border-bottom-width: 2px;
    border-radius: 5px;
    font-family: 'SF Mono', 'Cascadia Code', Consolas, monospace;
    font-size: 0.9em;
}

/* ==================== Upload Modal ==================== */
.upload-area {
    text-align: center;
    padding: 46px 26px;
    border: 2px dashed var(--border-strong);
    border-radius: var(--radius);
    transition: var(--transition);
    cursor: pointer;
}

.upload-area.dragover {
    border-color: var(--primary);
    background: var(--primary-soft);
}

.upload-icon {
    font-size: 2.6em;
    color: var(--primary);
    margin-bottom: 14px;
}

.upload-area h3 { margin-bottom: 8px; font-size: 1.1em; }

.upload-area p { color: var(--text-secondary); margin-bottom: 8px; font-size: 0.92em; }

.upload-hint { font-size: 0.82em !important; color: var(--text-muted) !important; margin-bottom: 20px !important; }

.upload-area .btn { width: auto; padding: 11px 20px; }

.upload-progress { padding: 26px 6px; }

.progress-bar {
    height: 8px;
    background: var(--bg-tertiary);
    border-radius: 20px;
    overflow: hidden;
    margin-bottom: 13px;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: var(--primary);
    border-radius: 20px;
    transition: width 0.25s ease;
}

#uploadStatus { text-align: center; color: var(--text-secondary); font-size: 0.92em; }

#filePreview {
    margin-top: 16px;
    padding: 14px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

#previewList { display: flex; flex-direction: column; gap: 8px; max-height: 280px; overflow-y: auto; }

.file-preview-item {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 10px;
    background: var(--bg-secondary);
    border-radius: 7px;
    border: 1px solid var(--border);
}

.file-preview-item .file-icon { font-size: 1.2em; flex-shrink: 0; }
.file-preview-item .file-details { flex: 1; min-width: 0; }

.file-preview-item .file-name {
    font-weight: 500;
    font-size: 0.9em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-preview-item .file-size { font-size: 0.76em; color: var(--text-muted); margin-top: 2px; }

.remove-file-btn {
    background: transparent;
    color: var(--text-muted);
    border: none;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
}

.remove-file-btn:hover { background: var(--danger); color: #fff; }

/* ==================== Toasts ==================== */
.toast-container {
    position: fixed;
    top: 18px;
    right: 18px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.toast {
    padding: 13px 16px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 11px;
    min-width: 280px;
    max-width: 380px;
    animation: toastIn 0.22s ease;
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(24px); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes slideOutRight {
    to { opacity: 0; transform: translateX(24px); }
}

.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
.toast.warning { border-left: 3px solid var(--warning); }

.toast-icon { font-size: 1.15em; flex-shrink: 0; }
.toast.success .toast-icon { color: var(--success); }
.toast.error .toast-icon { color: var(--danger); }
.toast.warning .toast-icon { color: var(--warning); }

.toast-content { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; font-size: 0.92em; }
.toast-message { font-size: 0.82em; color: var(--text-secondary); }

/* ==================== Loading ==================== */
.loading {
    text-align: center;
    padding: 34px 16px;
    color: var(--text-muted);
    font-size: 0.92em;
}

.loading .btn { width: auto; margin: 0 auto; }

/* ==================== About Modal ==================== */
.about-content { max-height: 68vh; overflow-y: auto; padding-right: 8px; }

.about-section {
    margin-bottom: 24px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--border);
}

.about-section:last-child { border-bottom: none; margin-bottom: 0; }

.about-section h3 { color: var(--primary); margin-bottom: 4px; font-size: 1.15em; }
.about-section h4 { margin-bottom: 12px; font-size: 1.02em; font-weight: 650; }

.about-section p { color: var(--text-secondary); font-size: 0.92em; }

.author-card {
    display: flex;
    gap: 16px;
    padding: 16px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

.author-avatar { font-size: 2.6em; color: var(--primary); line-height: 1; }

.author-info h3 { color: var(--primary); margin-bottom: 6px; }

.author-info p { color: var(--text-secondary); margin-bottom: 12px; line-height: 1.6; }

.author-links { display: flex; flex-wrap: wrap; gap: 8px; }

.author-links a {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 7px 13px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 7px;
    color: var(--text-primary);
    text-decoration: none;
    transition: var(--transition);
    font-size: 0.87em;
}

.author-links a:hover { background: var(--primary); border-color: var(--primary); color: #ffffff; }

.feature-list { list-style: none; padding: 0; }

.feature-list li {
    padding: 7px 0;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.92em;
}

.feature-list i { color: var(--primary); font-size: 0.95em; flex-shrink: 0; }

.tech-badges { display: flex; flex-wrap: wrap; gap: 8px; }

.badge {
    padding: 5px 12px;
    background: var(--primary-soft);
    color: var(--primary);
    border-radius: 20px;
    font-size: 0.82em;
    font-weight: 600;
}

/* ==================== Document Summary Modal ==================== */
.document-summary-content {
    line-height: 1.7;
    font-size: 0.94em;
    max-height: 400px;
    overflow-y: auto;
    padding-right: 8px;
    color: var(--text-secondary);
}

.document-summary-content strong { color: var(--primary); font-weight: 600; }

.document-summary-content ul { margin: 10px 0; padding-left: 22px; }
.document-summary-content li { margin: 6px 0; }
.document-summary-content p { margin: 10px 0; }

.document-stats {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    padding: 13px;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    font-size: 0.88em;
    color: var(--text-secondary);
}

.document-stats strong { color: var(--text-primary); margin-right: 4px; }

/* ==================== Mobile Menu ==================== */
.mobile-menu-btn {
    display: none;
    position: fixed;
    top: 14px;
    left: 14px;
    z-index: 999;
    width: 44px;
    height: 44px;
    background: var(--primary);
    border: none;
    border-radius: 10px;
    color: #ffffff;
    font-size: 1.05em;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    align-items: center;
    justify-content: center;
}

.mobile-menu-btn:active { transform: scale(0.96); }

/* Subtle attention pulse (used to guide the user to the upload button) */
@keyframes pulse {
    0%, 100% { transform: scale(1); box-shadow: var(--shadow-sm); }
    50% { transform: scale(1.06); box-shadow: 0 0 0 6px var(--primary-soft); }
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(8, 10, 16, 0.55);
    z-index: 1000;
}

.sidebar-overlay.active { display: block; }

/* ==================== Responsive ==================== */
@media (max-width: 1024px) {
    .sidebar { width: 260px; }
}

@media (max-width: 768px) {
    .mobile-menu-btn { display: flex; }

    .sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        height: 100vh;
        width: 84%;
        max-width: 320px;
        z-index: 1001;
        transition: left 0.24s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: var(--shadow);
    }

    .sidebar.mobile-open { left: 0; }

    .main-content { width: 100%; height: 100vh; position: relative; }

    .chat-header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 998;
        padding: 12px 16px 12px 68px;
        min-height: 64px;
        border-bottom: 1px solid var(--border);
    }

    .chat-info { flex: 1; min-width: 0; }
    .chat-info h2 { font-size: 1.05em; }
    .chat-info p { font-size: 0.74em; }

    .header-actions { gap: 5px; }
    .header-actions .btn-small { padding: 8px; min-width: 36px; min-height: 36px; }

    #settingsBtn { display: none; }

    .chat-messages {
        padding: 80px 14px 136px;
        height: 100vh;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    .chat-input-container {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 998;
        padding: 10px 12px;
        border-top: 1px solid var(--border);
    }

    #messageInput {
        min-height: 42px;
        max-height: 120px;
        padding: 11px 14px;
        font-size: 16px; /* prevent iOS zoom */
        border-radius: 22px;
    }

    .input-actions .btn { min-width: 42px; min-height: 42px; }

    #attachBtn { display: none; }
    .input-hint { display: none; }

    .message { gap: 9px; margin-bottom: 14px; }
    .message-avatar { width: 32px; height: 32px; font-size: 0.85em; }
    .message-content { max-width: calc(100% - 46px); padding: 11px 13px; }
    .message-text { font-size: 0.94em; }

    .welcome-message { padding: 28px 14px; }
    .welcome-icon { width: 60px; height: 60px; font-size: 1.6em; border-radius: 16px; }
    .welcome-message h2 { font-size: 1.4em; }

    .quick-actions { flex-direction: column; gap: 10px; }
    .quick-action { width: 100%; justify-content: center; }

    .modal { padding: 12px; }
    .modal-content { width: 100%; max-width: 100%; padding: 22px 18px; }

    .modal-header h2 { font-size: 1.15em; }

    .upload-area { padding: 36px 16px; }

    .toast-container { top: 76px; right: 10px; left: 10px; }
    .toast { min-width: 0; width: 100%; max-width: none; }

    .author-card { flex-direction: column; text-align: left; }

    input, textarea, select { font-size: 16px !important; }
}

@media (max-width: 768px) and (orientation: landscape) {
    .chat-header { padding: 8px 12px 8px 60px; min-height: 54px; }
    .chat-info p { display: none; }
    .chat-messages { padding: 64px 14px 96px; }
    .chat-input-container { padding: 8px 12px; }
    #messageInput { max-height: 80px; min-height: 40px; }
    .welcome-message { padding: 18px 12px; }
    .welcome-icon { display: none; }
    .quick-actions { flex-direction: row; flex-wrap: wrap; }
    .quick-action { width: calc(50% - 5px); padding: 9px 12px; font-size: 0.88em; }
}

@media (max-width: 380px) {
    .mobile-menu-btn { width: 40px; height: 40px; top: 12px; left: 12px; }
    .chat-header { padding: 10px 10px 10px 58px; min-height: 58px; }
    .message-avatar { width: 30px; height: 30px; }
    .welcome-message h2 { font-size: 1.25em; }
}

@media (hover: none) and (pointer: coarse) {
    .document-item .doc-view-btn,
    .document-item .doc-delete-btn { opacity: 1; }
    .btn, button { min-height: 40px; }
}

@media (min-width: 1920px) {
    .sidebar { width: 340px; }
    .chat-messages { padding: 36px 44px; }
}

@media print {
    .sidebar, .chat-header, .chat-input-container, .mobile-menu-btn { display: none !important; }
    .main-content { margin: 0; width: 100%; }
    .message { break-inside: avoid; }
}

/* ============================================================
   v4 polish layer

   Appearance only: colours, borders, radii, shadows, transitions.
   No layout properties are set here (no padding / size / display /
   max-width), so the responsive rules above stay authoritative.

   Performance rules honoured:
     - transitions touch only colour / transform / opacity, never layout;
     - no backdrop-filter on full-screen surfaces (it repaints the whole
       page every frame while a modal is open);
     - no decorative animation loops;
     - --transition no longer means "all", so nothing animates by accident.
   ============================================================ */

/* ---------- Page depth (static gradients: painted once) ---------- */
body {
    background-color: var(--bg-primary);
    background-image:
        radial-gradient(1100px 520px at 12% -8%, rgba(109, 120, 255, 0.10), transparent 62%),
        radial-gradient(900px 460px at 100% 2%, rgba(109, 120, 255, 0.05), transparent 58%);
}

body[data-theme="light"] {
    background-image:
        radial-gradient(1000px 480px at 10% -10%, rgba(91, 94, 242, 0.07), transparent 60%),
        radial-gradient(800px 420px at 100% 0%, rgba(91, 94, 242, 0.04), transparent 55%);
}

/* ---------- Surfaces ---------- */
.glass {
    background: var(--bg-secondary);
    border-color: var(--border);
    box-shadow: none; /* panels sit flush; shadows are reserved for floating layers */
}

.sidebar,
.chat-header,
.chat-input-container {
    border-color: var(--border);
}

.sidebar-header,
.sidebar-footer,
.sidebar-branding,
.search-box {
    border-color: var(--border);
}

.modal-content {
    background: var(--bg-elevated);
    border: 1px solid var(--border-strong);
    border-radius: 20px;
    box-shadow: var(--shadow);
}

.modal {
    background: rgba(7, 9, 14, 0.62);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}

[data-theme="light"] .modal {
    background: rgba(16, 19, 25, 0.34);
}

/* ---------- Branding ---------- */
.gradient-text {
    background: var(--brand-gradient);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    -webkit-text-fill-color: transparent;
}

/* ---------- Sidebar documents ---------- */
.document-item:hover {
    background: var(--bg-tertiary);
    border-color: var(--border);
}

.document-item.active {
    background: var(--primary-soft);
    border-color: var(--primary-ring);
}

/* Selected-document accent bar (pseudo-element: no layout impact) */
.document-item.active::before {
    content: "";
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 58%;
    border-radius: 0 3px 3px 0;
    background: var(--primary);
}

.doc-view-btn,
.doc-delete-btn {
    border-radius: 8px;
}

/* ---------- Inputs ---------- */
.search-box input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.form-group input {
    border-color: var(--border);
}

.form-group input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
    background: var(--bg-elevated);
}

.auth-tabs {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
}

.auth-tab.active {
    background: var(--bg-elevated);
    color: var(--primary);
    box-shadow: var(--shadow-sm);
}

/* ---------- Buttons ---------- */
.btn-primary {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
}

.btn-secondary {
    border-color: var(--border-strong);
}

.btn-secondary:hover {
    border-color: var(--primary-ring);
    background: var(--bg-tertiary);
}

/* Composer buttons keep the primary look; the stop button keeps its danger red */
#sendBtn,
#attachBtn {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    box-shadow: 0 2px 10px rgba(109, 120, 255, 0.26);
}

#sendBtn:hover:not(:disabled),
#attachBtn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(109, 120, 255, 0.34);
}

#sendBtn:active:not(:disabled),
#attachBtn:active:not(:disabled) {
    transform: translateY(0);
}

/* ---------- Messages ---------- */
.message-content {
    background: var(--bg-elevated);
    border-color: var(--border);
    box-shadow: var(--shadow-sm);
}

.message.user .message-content {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    border-color: transparent;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.18);
}

.message.assistant .message-avatar {
    background: var(--primary-soft);
    border-color: var(--primary-ring);
    color: var(--primary);
}

.message-text {
    line-height: 1.68;
}

.message-text code {
    border: 1px solid var(--border);
}

/* ---------- Source citations ---------- */
.source-item {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.source-item:hover {
    border-color: var(--primary-ring);
}

.source-name {
    color: var(--primary);
    font-weight: 650;
}

/* ---------- Typing indicator ---------- */
.typing-dot {
    background: var(--primary);
    opacity: 0.65;
}

/* ---------- Composer ---------- */
#messageInput {
    background: var(--bg-tertiary);
    border-color: var(--border);
}

#messageInput:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
    background: var(--bg-secondary);
}

/* ---------- Upload ---------- */
.upload-area {
    border-color: var(--border-strong);
    background: var(--bg-tertiary);
}

.upload-area:hover {
    border-color: var(--primary-ring);
    background: var(--bg-elevated);
}

.upload-area.dragover {
    border-color: var(--primary);
    background: var(--primary-soft);
}

.progress-fill {
    background: linear-gradient(90deg, var(--primary) 0%, var(--primary-light) 100%);
}

#filePreview {
    background: var(--bg-tertiary);
    border-color: var(--border);
}

/* ---------- Toasts ---------- */
.toast {
    background: var(--bg-elevated);
    border-color: var(--border-strong);
    box-shadow: var(--shadow);
    align-items: flex-start;
}

/* Error text can be several lines (message + hint) */
.toast-message {
    white-space: pre-line;
    line-height: 1.45;
    margin-top: 2px;
}

.toast.error .toast-title { color: var(--danger); }
.toast.warning .toast-title { color: var(--warning); }

.toast-close {
    flex-shrink: 0;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.15em;
    line-height: 1;
    padding: 2px 5px;
    border-radius: 6px;
    cursor: pointer;
    transition: var(--transition);
}

.toast-close:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-thumb {
    background-color: var(--border-strong);
    border: 3px solid transparent;
    background-clip: content-box;
    border-radius: 20px;
}

.chat-messages,
.document-list,
.modal-content,
.about-content,
.document-summary-content,
#previewList {
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

/* ---------- Chat scrolling: instant, not animated ----------
   chat.js assigns scrollTop while a response streams in. With smooth
   scrolling the browser animates every one of those updates, which is
   wasted work on exactly the path the user is waiting for. */
.chat-messages {
    scroll-behavior: auto;
}

/* ---------- Keyboard focus ---------- */
:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible {
    outline: none; /* these already show a ring via border + box-shadow */
}

/* ---------- Respect reduced-motion ---------- */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}

/* ============================================================
   Mobile viewport fixes

   Unlike the polish layer above, this block DOES set layout - but only
   inside the mobile breakpoint, on properties no other rule touches.
   The desktop layout is unaffected.
   ============================================================ */

.sidebar-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* The drawer close button only makes sense where the sidebar is a drawer */
.sidebar-close-btn { display: none; }

@media (max-width: 768px) {
    /* Notch / home-indicator safety (viewport-fit=cover is set in the head) */
    .mobile-menu-btn {
        display: flex;
        top: calc(14px + env(safe-area-inset-top, 0px));
        left: calc(14px + env(safe-area-inset-left, 0px));
        width: 44px;
        height: 44px;
        border-radius: 12px;
        box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
    }

    .sidebar-close-btn { display: inline-flex; }

    .sidebar {
        padding-top: env(safe-area-inset-top, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    .chat-header {
        padding-top: calc(12px + env(safe-area-inset-top, 0px));
    }

    .chat-messages {
        padding-bottom: calc(136px + env(safe-area-inset-bottom, 0px));
    }

    .chat-input-container {
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }

    .toast-container {
        top: calc(76px + env(safe-area-inset-top, 0px));
    }

    /* Comfortable tap targets in the drawer */
    .doc-view-btn,
    .doc-delete-btn {
        width: 36px;
        height: 36px;
    }

    /* Header actions become icon-only so the chat title keeps its room */
    .header-actions .btn-label { display: none; }
}
