/* =====================================================
   ArloPlus — Professional Chat UI
   Dark/Light theme with CSS custom properties
   ===================================================== */

/* --- CSS Variables --- */
:root {
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --accent: #6366f1;
    --accent-hover: #4f46e5;
    --accent-light: rgba(99, 102, 241, 0.1);
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #22c55e;
    --warning: #f59e0b;
    --radius: 12px;
    --radius-sm: 8px;
    --radius-xs: 6px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);
    --transition: 0.2s ease;
}

/* Dark theme (default) */
[data-theme="dark"] {
    --bg-primary: #0f0f0f;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #252525;
    --bg-hover: #2a2a2a;
    --bg-input: #1e1e1e;
    --border: #2e2e2e;
    --border-light: #383838;
    --text-primary: #f5f5f5;
    --text-secondary: #a0a0a0;
    --text-muted: #666;
    --msg-user: #2563eb;
    --msg-user-text: #fff;
    --msg-assistant: #252525;
    --msg-assistant-text: #f5f5f5;
    --scrollbar-bg: #1a1a1a;
    --scrollbar-thumb: #444;
}

/* Light theme */
[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #f8f9fa;
    --bg-tertiary: #f0f0f0;
    --bg-hover: #e8e8e8;
    --bg-input: #ffffff;
    --border: #e0e0e0;
    --border-light: #eaeaea;
    --text-primary: #111111;
    --text-secondary: #555555;
    --text-muted: #999;
    --msg-user: #2563eb;
    --msg-user-text: #fff;
    --msg-assistant: #f0f0f0;
    --msg-assistant-text: #111111;
    --scrollbar-bg: #f0f0f0;
    --scrollbar-thumb: #ccc;
}

/* --- Reset & Base --- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.6;
    color: var(--text-primary);
    background: var(--bg-primary);
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.hidden { display: none !important; }

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--scrollbar-bg); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }

/* =====================================================
   BUTTONS
   ===================================================== */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    border: none;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--bg-hover); }

.btn-danger {
    background: var(--danger);
    color: #fff;
}
.btn-danger:hover { background: var(--danger-hover); }

.btn-full { width: 100%; justify-content: center; }

.btn-icon {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius-xs);
    display: flex;
    align-items: center;
    transition: all var(--transition);
}
.btn-icon:hover { background: var(--bg-hover); color: var(--text-primary); }

.btn-sm {
    padding: 5px 12px;
    font-size: 13px;
    border: none;
    border-radius: var(--radius-xs);
    cursor: pointer;
    font-family: var(--font);
    transition: all var(--transition);
}
.btn-edit {
    background: var(--accent-light);
    color: var(--accent);
}
.btn-edit:hover { background: var(--accent); color: #fff; }

.btn-sm.btn-info {
    background: rgba(59, 130, 246, 0.1);
    color: #3b82f6;
}
.btn-sm.btn-info:hover {
    background: #3b82f6;
    color: #fff;
}

.btn-sm.btn-danger {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
}
.btn-sm.btn-danger:hover {
    background: var(--danger);
    color: #fff;
}

/* =====================================================
   LOGIN PAGE
   ===================================================== */

.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-primary);
    padding: 20px;
}

.login-card {
    width: 100%;
    max-width: 420px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 48px 40px;
    box-shadow: var(--shadow-lg);
}

.login-header {
    text-align: center;
    margin-bottom: 32px;
}

.login-logo {
    margin-bottom: 16px;
}

.login-header h1 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 4px;
}

.login-subtitle {
    color: var(--text-secondary);
    font-size: 14px;
}

.login-form .form-group {
    margin-bottom: 20px;
}

.login-form label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.login-form input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 15px;
    font-family: var(--font);
    transition: border-color var(--transition);
}

.login-form input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

.login-footer {
    text-align: center;
    margin-top: 32px;
    font-size: 12px;
    color: var(--text-muted);
}

.flash-messages {
    margin-bottom: 20px;
}

.flash {
    padding: 10px 14px;
    border-radius: var(--radius-xs);
    font-size: 14px;
    margin-bottom: 8px;
}

.flash-error {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.flash-info {
    background: var(--accent-light);
    color: var(--accent);
    border: 1px solid rgba(99, 102, 241, 0.2);
}

/* =====================================================
   APP LAYOUT
   ===================================================== */

.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* --- Sidebar --- */

.sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: margin-left 0.3s ease;
    overflow: hidden;
    z-index: 100;
}

.sidebar.collapsed {
    margin-left: -280px;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 16px 12px;
    border-bottom: 1px solid var(--border);
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo-text {
    font-size: 18px;
    font-weight: 700;
}

.btn-new-chat {
    margin: 12px 12px 8px;
    background: var(--accent);
    color: #fff;
    justify-content: center;
}
.btn-new-chat:hover { background: var(--accent-hover); }

.sidebar-section {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

.sidebar-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    padding: 12px 8px 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.doc-count {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 10px;
}

/* Session items */

.session-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
    font-size: 14px;
    color: var(--text-secondary);
    position: relative;
}

.session-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.session-item.active {
    background: var(--accent-light);
    color: var(--accent);
}

.session-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.session-actions {
    display: none;
    gap: 2px;
}

.session-item:hover .session-actions {
    display: flex;
}

.session-action-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 3px;
    border-radius: 4px;
    display: flex;
    transition: all var(--transition);
}

.session-action-btn:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.session-action-btn.delete:hover { color: var(--danger); }

/* Document items */

.doc-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    color: var(--text-secondary);
    transition: all var(--transition);
}

.doc-item:hover { background: var(--bg-hover); }

.doc-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.doc-chunks {
    font-size: 11px;
    color: var(--text-muted);
}

.doc-delete {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 3px;
    border-radius: 4px;
    display: none;
    transition: all var(--transition);
}

.doc-item:hover .doc-delete { display: flex; }
.doc-delete:hover { color: var(--danger); }

/* Sidebar footer */

.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 12px;
    position: relative;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 14px;
    transition: all var(--transition);
    text-decoration: none;
    margin-bottom: 8px;
}

.sidebar-link:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    text-decoration: none;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px;
    border-radius: var(--radius-sm);
}

.user-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 14px;
    flex-shrink: 0;
}

.user-details {
    flex: 1;
    min-width: 0;
}

.user-name {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
}

.user-role {
    display: block;
    font-size: 11px;
    color: var(--text-muted);
    text-transform: capitalize;
}

.user-menu {
    position: absolute;
    bottom: 72px;
    left: 12px;
    right: 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    z-index: 200;
}

.menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 14px;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-size: 14px;
    font-family: var(--font);
    cursor: pointer;
    transition: all var(--transition);
    text-align: left;
}

.menu-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.menu-item-danger:hover { color: var(--danger); }

/* --- Main Content --- */

.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-primary);
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-primary);
    min-height: 56px;
}

.chat-title {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.header-actions {
    display: flex;
    gap: 8px;
}

.btn-upload {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    cursor: pointer;
    font-size: 13px;
    padding: 8px 14px;
}
.btn-upload:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* Upload progress */

.upload-bar {
    padding: 12px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
}

.upload-info {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    margin-bottom: 6px;
}

.upload-filename { color: var(--text-primary); font-weight: 500; }
.upload-status { color: var(--text-secondary); }

.progress-bar {
    height: 4px;
    background: var(--bg-tertiary);
    border-radius: 2px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 2px;
    width: 0%;
    transition: width 0.3s ease;
}

/* =====================================================
   CHAT MESSAGES
   ===================================================== */

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* Welcome screen */

.welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px;
    max-width: 600px;
    margin: 0 auto;
}

.welcome-icon { margin-bottom: 24px; }

.welcome-screen h2 {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 12px;
}

.welcome-screen p {
    color: var(--text-secondary);
    font-size: 15px;
    line-height: 1.6;
    margin-bottom: 32px;
}

.welcome-features {
    display: flex;
    gap: 24px;
    flex-wrap: wrap;
    justify-content: center;
}

.feature {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--text-secondary);
}

/* Message bubbles */

.message {
    display: flex;
    gap: 12px;
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    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;
    flex-shrink: 0;
    font-size: 14px;
    font-weight: 600;
}

.message.user .message-avatar {
    background: var(--msg-user);
    color: #fff;
}

.message.assistant .message-avatar {
    background: var(--accent);
    color: #fff;
}

.message-content {
    flex: 1;
    min-width: 0;
}

.message-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.message-author {
    font-size: 14px;
    font-weight: 600;
}

.message-time {
    font-size: 11px;
    color: var(--text-muted);
}

.message-text {
    font-size: 15px;
    line-height: 1.7;
    color: var(--text-primary);
    word-wrap: break-word;
}

.message-text p { margin-bottom: 8px; }
.message-text p:last-child { margin-bottom: 0; }
.message-text code {
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
}
.message-text pre {
    background: var(--bg-tertiary);
    padding: 14px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 8px 0;
}
.message-text pre code {
    background: none;
    padding: 0;
}
.message-text ul, .message-text ol {
    padding-left: 20px;
    margin: 8px 0;
}
.message-text li { margin-bottom: 4px; }
.message-text blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    color: var(--text-secondary);
    margin: 8px 0;
}
.message-text strong { font-weight: 600; }
.message-text h1, .message-text h2, .message-text h3, .message-text h4 {
    margin: 16px 0 8px;
    font-weight: 600;
}
.message-text h1 { font-size: 20px; }
.message-text h2 { font-size: 18px; }
.message-text h3 { font-size: 16px; }
.message-text h4 { font-size: 15px; }
.message-text hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 16px 0;
}
.message-text del {
    text-decoration: line-through;
    opacity: 0.7;
}
.message-text a {
    color: var(--accent);
    text-decoration: none;
}
.message-text a:hover {
    text-decoration: underline;
}
.message-text ol {
    list-style-type: decimal;
}

/* Code block wrapper */
.code-block-wrapper {
    position: relative;
    margin: 12px 0;
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: var(--bg-tertiary);
}
.code-block-wrapper pre {
    margin: 0;
    border-radius: 0;
    padding: 16px;
    padding-top: 12px;
    overflow-x: auto;
}
.code-block-wrapper pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
}
.code-lang {
    position: absolute;
    top: 6px;
    left: 12px;
    font-size: 11px;
    font-weight: 500;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    pointer-events: none;
}
.btn-copy-code {
    position: absolute;
    top: 6px;
    right: 8px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px 6px;
    display: flex;
    align-items: center;
    transition: all 0.15s;
    opacity: 0;
}
.code-block-wrapper:hover .btn-copy-code {
    opacity: 1;
}
.btn-copy-code:hover {
    color: var(--text-primary);
    background: var(--bg-secondary);
}
.btn-copy-code.copied {
    color: #22c55e;
    border-color: #22c55e;
}

/* Copy message button */
.btn-copy-msg {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-left: auto;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 4px;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 8px;
    font-size: 12px;
    transition: all 0.15s;
    opacity: 0;
}
.message:hover .btn-copy-msg {
    opacity: 1;
}
.btn-copy-msg:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
    border-color: var(--border);
}
.btn-copy-msg.copied {
    color: #22c55e;
    opacity: 1;
}
.btn-copy-msg .copy-label {
    font-family: var(--font);
}

/* Tables */
.table-wrapper {
    overflow-x: auto;
    margin: 12px 0;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}
.message-text table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}
.message-text th,
.message-text td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
}
.message-text th {
    background: var(--bg-tertiary);
    font-weight: 600;
    font-size: 13px;
    white-space: nowrap;
}
.message-text tr:last-child td {
    border-bottom: none;
}
.message-text tr:hover td {
    background: var(--bg-secondary);
}

/* Source tags */

.message-sources {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
}

.source-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

/* Typing indicator */

.typing-indicator {
    display: flex;
    gap: 5px;
    padding: 0 20px 12px;
    max-width: 800px;
    margin: 0 auto;
    width: 100%;
    padding-left: 68px;
}

.typing-dot {
    width: 8px;
    height: 8px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typingBounce 1.4s ease-in-out infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-8px); opacity: 1; }
}

/* =====================================================
   CHAT INPUT
   ===================================================== */

.chat-input-container {
    padding: 12px 20px 16px;
    border-top: 1px solid var(--border);
    background: var(--bg-primary);
}

.chat-input-wrapper {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 12px;
    transition: border-color var(--transition);
}

.chat-input-wrapper:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

.chat-input {
    flex: 1;
    border: none;
    background: none;
    color: var(--text-primary);
    font-size: 15px;
    font-family: var(--font);
    resize: none;
    min-height: 24px;
    max-height: 200px;
    line-height: 1.5;
    outline: none;
}

.chat-input::placeholder { color: var(--text-muted); }

.btn-send {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    padding: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
    flex-shrink: 0;
}

.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* Web Search Toggle */
.btn-web-search {
    background: none;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    padding: 7px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
    flex-shrink: 0;
}

.btn-web-search:hover {
    border-color: var(--text-secondary);
    color: var(--text-secondary);
}

.btn-web-search.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    box-shadow: 0 0 8px var(--accent-light);
}

.btn-web-search.active:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

/* Web Citations */
.web-citations {
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.web-citation-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: var(--radius-sm);
    font-size: 12px;
    text-decoration: none;
    transition: all var(--transition);
    max-width: 250px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.web-citation-link:hover {
    background: var(--accent);
    color: #fff;
}

.web-citation-link svg {
    flex-shrink: 0;
}

/* Cite buttons on source tags & web citations */
.btn-cite-source,
.btn-cite-web {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: inherit;
    cursor: pointer;
    padding: 1px 3px;
    border-radius: 3px;
    opacity: 0.5;
    transition: all 0.15s;
    flex-shrink: 0;
}
.btn-cite-source:hover,
.btn-cite-web:hover {
    opacity: 1;
    background: rgba(255,255,255,0.15);
}
.btn-cite-web.cited {
    color: #22c55e;
    opacity: 1;
}

.web-citation-item {
    display: inline-flex;
    align-items: center;
    gap: 2px;
}

/* =====================================================
   MODE TABS (Chat / Writer toggle in header)
   ===================================================== */

.mode-tabs {
    display: flex;
    gap: 4px;
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    padding: 3px;
}

.mode-tab {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 5px 12px;
    border: none;
    border-radius: calc(var(--radius-sm) - 1px);
    background: transparent;
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 500;
    font-family: var(--font);
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.mode-tab:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.mode-tab.active {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.mode-tab svg {
    flex-shrink: 0;
}

/* =====================================================
   WRITER VIEW
   ===================================================== */

.writer-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.writer-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-wrap: wrap;
}

.writer-mode-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.writer-mode-selector label {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    white-space: nowrap;
}

.writer-mode-selector select {
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 13px;
    font-family: var(--font);
    cursor: pointer;
}

.writer-stats {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--text-muted);
    margin-left: auto;
}

.stat-divider {
    color: var(--border);
}

.btn-analyze {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 16px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    font-family: var(--font);
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn-analyze:hover {
    background: var(--accent-hover);
}

.btn-analyze:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.writer-content {
    flex: 1;
    display: flex;
    overflow: hidden;
}

.writer-editor-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    min-width: 0;
}

.writer-textarea {
    flex: 1;
    width: 100%;
    border: none;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 15px;
    font-family: var(--font);
    line-height: 1.8;
    padding: 24px;
    resize: none;
    outline: none;
}

.writer-textarea::placeholder {
    color: var(--text-muted);
    line-height: 1.6;
}

.writer-feedback-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    min-width: 0;
    background: var(--bg-primary);
}

.writer-feedback-placeholder {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px 24px;
    color: var(--text-muted);
}

.writer-feedback-placeholder h3 {
    font-size: 18px;
    font-weight: 600;
    margin: 16px 0 8px;
    color: var(--text-secondary);
}

.writer-feedback-placeholder p {
    font-size: 14px;
    line-height: 1.5;
    margin-bottom: 24px;
    max-width: 360px;
}

.writer-mode-hints {
    text-align: left;
    width: 100%;
    max-width: 340px;
}

.mode-hint {
    font-size: 12px;
    color: var(--text-muted);
    padding: 4px 0;
    line-height: 1.4;
}

.mode-hint strong {
    color: var(--text-secondary);
}

/* Feedback content */

.writer-feedback-content {
    padding: 20px 24px;
}

.feedback-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.feedback-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.feedback-stats {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
    padding: 10px 14px;
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

.feedback-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.feedback-stat-value {
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
}

.feedback-stat-label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.feedback-body {
    font-size: 15px;
    line-height: 1.7;
    color: var(--text-primary);
}

/* Reuse message-text formatting for feedback content */
.feedback-body h1, .feedback-body h2, .feedback-body h3, .feedback-body h4 {
    margin: 16px 0 8px;
    font-weight: 600;
}
.feedback-body h2 { font-size: 17px; }
.feedback-body h3 { font-size: 15px; }
.feedback-body p { margin-bottom: 8px; }
.feedback-body ul, .feedback-body ol { padding-left: 20px; margin: 8px 0; }
.feedback-body li { margin-bottom: 4px; }
.feedback-body code {
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
}
.feedback-body pre {
    background: var(--bg-tertiary);
    padding: 14px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 8px 0;
}
.feedback-body blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    color: var(--text-secondary);
    margin: 8px 0;
}
.feedback-body strong { font-weight: 600; }
.feedback-body hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 16px 0;
}

.feedback-sources {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.feedback-sources h4 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.feedback-source-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
    margin-right: 6px;
    margin-bottom: 4px;
}

/* Writer loading spinner */

.writer-loading {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    color: var(--text-muted);
    font-size: 14px;
}

.writer-loading-spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: writerSpin 0.8s linear infinite;
}

@keyframes writerSpin {
    to { transform: rotate(360deg); }
}

/* =====================================================
   CITATION SIDEBAR
   ===================================================== */

.citation-style-selector {
    padding: 4px 12px 8px;
}

.citation-style-selector select {
    width: 100%;
    padding: 6px 8px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 12px;
    font-family: var(--font);
    cursor: pointer;
    appearance: auto;
}

.citation-list {
    max-height: 280px;
    overflow-y: auto;
    padding: 0 8px;
}

.citation-item {
    padding: 8px 10px;
    margin-bottom: 6px;
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    transition: all 0.15s;
}

.citation-item:hover {
    border-color: var(--accent);
}

.citation-header-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 4px;
}

.citation-header-row svg {
    flex-shrink: 0;
    color: var(--text-muted);
}

.citation-item-actions {
    display: flex;
    gap: 2px;
    margin-left: auto;
    opacity: 0;
    transition: opacity 0.15s;
}

.citation-item:hover .citation-item-actions {
    opacity: 1;
}

.btn-cite-copy,
.btn-cite-edit,
.btn-cite-delete {
    background: transparent;
    border: none;
    padding: 3px 4px;
    border-radius: 3px;
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: all 0.15s;
}

.btn-cite-copy:hover { color: var(--accent); }
.btn-cite-edit:hover { color: var(--accent); }
.btn-cite-delete:hover { color: #ef4444; }

.citation-formatted {
    font-size: 12px;
    line-height: 1.5;
    color: var(--text-secondary);
    word-break: break-word;
}

.citation-missing {
    font-size: 11px;
    color: #f59e0b;
    margin-top: 4px;
    font-style: italic;
}

/* Verification status badges */
.citation-status {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    flex-shrink: 0;
}

.citation-status.verified {
    background: rgba(34, 197, 94, 0.1);
}
.citation-status.partial {
    background: rgba(245, 158, 11, 0.1);
}
.citation-status.unverified {
    background: rgba(239, 68, 68, 0.1);
}

/* Citation actions bar */
.citation-actions {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    flex-wrap: wrap;
}

.btn-sm {
    font-size: 11px;
    padding: 4px 8px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

/* Edit citation form styles */
.form-group {
    margin-bottom: 14px;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.form-input {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 14px;
    font-family: var(--font);
    transition: border-color 0.15s;
}

.form-input:focus {
    outline: none;
    border-color: var(--accent);
}

.field-verified {
    font-size: 11px;
    color: #22c55e;
    font-weight: 400;
    margin-left: 4px;
}

.field-unverified {
    font-size: 11px;
    color: #f59e0b;
    font-weight: 400;
    margin-left: 4px;
}

.input-hint {
    text-align: center;
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 8px;
    max-width: 800px;
    margin-left: auto;
    margin-right: auto;
}

/* =====================================================
   MODALS
   ===================================================== */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
    backdrop-filter: blur(4px);
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 16px;
    max-width: 480px;
    width: 100%;
    box-shadow: var(--shadow-lg);
    animation: modalSlide 0.2s ease;
}

@keyframes modalSlide {
    from { opacity: 0; transform: scale(0.95) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px 0;
}

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.modal-close {
    color: var(--text-muted);
}

.modal-body {
    padding: 20px 24px 24px;
}

.modal-form .form-group {
    margin-bottom: 16px;
}

.modal-form label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.modal-form input,
.modal-form select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 14px;
    font-family: var(--font);
    transition: border-color var(--transition);
}

.modal-form input:focus,
.modal-form select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

.modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 20px;
}

.checkbox-label {
    display: flex !important;
    align-items: center;
    gap: 8px;
    cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
    width: auto;
}

/* =====================================================
   TOASTS
   ===================================================== */

.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 12px 20px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transform: translateX(20px);
    transition: all 0.3s ease;
    max-width: 400px;
}

.toast.show {
    opacity: 1;
    transform: translateX(0);
}

.toast-success {
    background: var(--success);
    color: #fff;
}

.toast-error {
    background: var(--danger);
    color: #fff;
}

.toast-info {
    background: var(--accent);
    color: #fff;
}

/* =====================================================
   ADMIN PAGE
   ===================================================== */

.admin-page {
    min-height: 100vh;
    background: var(--bg-primary);
    padding: 0 24px 40px;
}

.admin-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 0;
    max-width: 1200px;
    margin: 0 auto;
    flex-wrap: wrap;
    gap: 16px;
}

.admin-header-left {
    display: flex;
    align-items: center;
    gap: 20px;
}

.admin-header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.admin-header h1 {
    font-size: 24px;
    font-weight: 700;
}

.btn-back {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}
.btn-back:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    text-decoration: none;
}

.admin-content {
    max-width: 1200px;
    margin: 0 auto;
}

.users-table-wrapper {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-secondary);
}

.users-table {
    width: 100%;
    border-collapse: collapse;
}

.users-table th {
    text-align: left;
    padding: 14px 16px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    background: var(--bg-tertiary);
}

.users-table td {
    padding: 14px 16px;
    font-size: 14px;
    border-bottom: 1px solid var(--border);
}

.users-table tr:last-child td { border-bottom: none; }

.users-table tr:hover td {
    background: var(--bg-hover);
}

.actions-cell {
    display: flex;
    gap: 6px;
}

.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

.badge-admin { background: var(--accent-light); color: var(--accent); }
.badge-user { background: var(--bg-tertiary); color: var(--text-secondary); }
.badge-active { background: rgba(34, 197, 94, 0.1); color: var(--success); }
.badge-inactive { background: rgba(239, 68, 68, 0.1); color: var(--danger); }

.login-history-table-wrap {
    max-height: 450px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.login-history-table code {
    font-size: 12px;
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
}

.login-history-table td small {
    color: var(--text-muted);
}

/* =====================================================
   RESPONSIVE
   ===================================================== */

@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 1000;
    }

    .sidebar.collapsed {
        margin-left: -280px;
    }

    .welcome-features {
        flex-direction: column;
        align-items: center;
    }

    .login-card {
        padding: 36px 24px;
    }

    .admin-header {
        flex-direction: column;
        align-items: flex-start;
    }

    /* Writer responsive */
    .writer-content {
        flex-direction: column;
    }

    .writer-editor-panel {
        border-right: none;
        border-bottom: 1px solid var(--border);
        min-height: 200px;
        max-height: 40vh;
    }

    .writer-feedback-panel {
        min-height: 200px;
    }

    .writer-toolbar {
        padding: 8px 12px;
        gap: 8px;
    }

    .mode-tab span {
        display: none;
    }

    .mode-tab {
        padding: 5px 8px;
    }
}

/* =====================================================
   TEXT SELECTION TOOLBAR
   ===================================================== */

.selection-toolbar {
    position: fixed;
    z-index: 9999;
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 4px 6px;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25), 0 2px 8px rgba(0, 0, 0, 0.15);
    animation: selToolbarIn 0.15s ease-out;
    pointer-events: auto;
}

@keyframes selToolbarIn {
    from {
        opacity: 0;
        transform: translateY(4px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.sel-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 6px 10px;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    border-radius: var(--radius-sm);
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, color 0.15s;
}

.sel-btn:hover {
    background: var(--bg-hover);
    color: var(--accent);
}

.sel-btn:active {
    background: var(--bg-tertiary);
}

.sel-btn svg {
    flex-shrink: 0;
    opacity: 0.75;
}

.sel-btn:hover svg {
    opacity: 1;
}

.sel-highlight {
    color: #f59e0b;
}
.sel-highlight:hover {
    background: rgba(245, 158, 11, 0.12);
    color: #f59e0b;
}

.sel-divider {
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 2px;
    flex-shrink: 0;
}

/* Persistent text highlights */
.text-highlight {
    background: rgba(245, 158, 11, 0.25);
    border-radius: 2px;
    padding: 0 1px;
    cursor: pointer;
    transition: background 0.15s;
}

.text-highlight:hover {
    background: rgba(245, 158, 11, 0.4);
}

/* Selection color in assistant messages */
.message.assistant .message-text ::selection {
    background: rgba(var(--accent-rgb, 99, 102, 241), 0.25);
}

/* Toolbar arrow pointer */
.selection-toolbar::after {
    content: '';
    position: absolute;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%);
    width: 10px;
    height: 10px;
    background: var(--bg-primary);
    border-right: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    transform: translateX(-50%) rotate(45deg);
}

/* When toolbar appears above selection, flip the arrow */
.selection-toolbar.toolbar-below::after {
    bottom: auto;
    top: -5px;
    transform: translateX(-50%) rotate(-135deg);
}
