/* ---- Design tokens ---- */
:root {
    /* Surfaces */
    --bg-primary: #faf9f7;
    --bg-secondary: #f3f1ed;
    --bg-card: #ffffff;
    --bg-sidebar: #e4e1da;

    /* Text */
    --text-primary: #191918;
    --text-secondary: #4a4a48;
    --text-muted: #8b8b89;

    /* Accent */
    --accent: #c4704b;
    --accent-hover: #b35f3c;
    --accent-light: #d4896a;
    --gradient-accent: linear-gradient(135deg, #c4704b 0%, #b35f3c 100%);

    /* Status */
    --status-blue: #5b7a9e;
    --status-green: #5a8a72;
    --status-yellow: #b8943d;
    --status-red: #c25e5e;
    --status-purple: #8b7aab;

    /* Borders */
    --border-light: #e8e5e1;
    --border-medium: #d5d1cb;

    /* Shadows */
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.1);

    /* Radii */
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-base: 200ms ease;
    --transition-slow: 300ms ease;

    /* Typography */
    --font-heading: 'Source Serif 4', Georgia, serif;
    --font-body: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-code: 'JetBrains Mono', 'Fira Code', monospace;

    /* Layout */
    --header-height: 3.5rem;
}

/* ---- Reset & base ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: var(--font-body);
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    line-height: 1.6;
}
h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-heading);
    font-weight: 600;
}
code, pre {
    font-family: var(--font-code);
}

/* ---- Login ---- */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: var(--bg-secondary);
}
.login-card {
    background: var(--bg-card);
    padding: 2.5rem;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 400px;
    text-align: center;
    border: 1px solid var(--border-light);
}
.login-card h1 {
    font-size: 1.75rem;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}
.login-subtitle {
    color: var(--text-muted);
    margin-bottom: 1.5rem;
    font-size: 0.9rem;
}
.form-group {
    text-align: left;
    margin-bottom: 1rem;
}
.form-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.3rem;
}
.form-group input {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 0.95rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.form-group input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(196,112,75,0.15);
}
.flash {
    padding: 0.6rem 0.75rem;
    border-radius: var(--radius-sm);
    margin-bottom: 1rem;
    font-size: 0.85rem;
}
.flash-success {
    background: #f0f7f1;
    color: var(--status-green);
    border: 1px solid #c8dfd0;
}
.flash-error {
    background: #fdf2f2;
    color: var(--status-red);
    border: 1px solid #ebc8c8;
}

/* ---- Buttons ---- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.6rem 1.25rem;
    border: none;
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition-fast), transform var(--transition-fast);
    text-decoration: none;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
    background: var(--gradient-accent);
    color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary {
    background: var(--bg-secondary);
    color: var(--text-secondary);
    border: 1px solid var(--border-medium);
}
.btn-secondary:hover { background: var(--border-light); }
.btn-sm { padding: 0.35rem 0.75rem; font-size: 0.8rem; }

/* ---- App shell (sidebar + main column, sits below the full-width header) ---- */
.app-shell {
    display: flex;
    height: calc(100vh - var(--header-height));
    overflow: hidden;
}

/* ---- Conversation sidebar ---- */
.conv-sidebar {
    flex: 0 0 260px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.conv-sidebar-header {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-light);
    flex-shrink: 0;
}
.conv-new-btn {
    width: 100%;
    justify-content: center;
}
.conv-new-btn span {
    margin-right: 0.25rem;
    font-weight: 700;
}
.conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0.4rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}
.conv-list-empty {
    padding: 1rem;
    color: var(--text-muted);
    font-size: 0.85rem;
    text-align: center;
    font-style: italic;
}
.conv-item {
    position: relative;
    padding: 0.55rem 0.65rem;
    padding-right: 3.6rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--transition-fast);
    border: 1px solid transparent;
}
.conv-item:hover {
    background: var(--bg-card);
    border-color: var(--border-light);
}
.conv-item.active {
    background: var(--bg-card);
    border-color: var(--accent-light);
    box-shadow: var(--shadow-sm);
}
.conv-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.3;
}
.conv-meta {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 0.15rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.conv-actions {
    position: absolute;
    bottom: 0.4rem;
    right: 0.5rem;
    display: none;
    gap: 0.15rem;
}
.conv-item:hover .conv-actions,
.conv-item.active .conv-actions {
    display: inline-flex;
}
.conv-action-btn {
    background: none;
    border: none;
    padding: 0.15rem 0.3rem;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.8rem;
    line-height: 1;
    border-radius: 4px;
    transition: background var(--transition-fast), color var(--transition-fast);
}
.conv-action-btn:hover {
    background: var(--border-light);
    color: var(--text-primary);
}
.conv-action-btn.danger:hover {
    color: var(--status-red);
}
.conv-title-input {
    width: 100%;
    background: var(--bg-primary);
    border: 1px solid var(--accent);
    border-radius: 4px;
    padding: 0.15rem 0.3rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    font-family: var(--font-body);
    outline: none;
}

/* ---- App layout (right column inside shell, fills shell height) ---- */
.app-layout {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
    height: 100%;
}
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1.5rem;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border-light);
    flex-shrink: 0;
    min-height: var(--header-height);
    position: relative;
    z-index: 60;
}
.app-header h1 {
    font-size: 1.25rem;
    color: var(--text-primary);
}
.header-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}
.header-right select {
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 0.85rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border-color var(--transition-fast);
}
.header-right select:focus {
    outline: none;
    border-color: var(--accent);
}

/* ---- Chat area ---- */
.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    background: var(--bg-primary);
}
.chat-messages {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* ---- Messages ---- */
.message {
    padding: 0.85rem 1rem;
    border-radius: var(--radius-lg);
    max-width: 85%;
    line-height: 1.6;
    font-size: 0.95rem;
}
.user-message {
    background: var(--accent);
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: var(--radius-sm);
}
.assistant-message {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    box-shadow: var(--shadow-sm);
    align-self: flex-start;
    border-bottom-left-radius: var(--radius-sm);
    color: var(--text-primary);
}
.assistant-message.welcome {
    background: var(--bg-secondary);
    border: none;
    box-shadow: none;
    color: var(--text-muted);
    align-self: center;
    text-align: center;
}
.message.error {
    background: #fdf2f2;
    border: 1px solid #ebc8c8;
    color: var(--status-red);
    align-self: stretch;
    max-width: 100%;
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    font-size: 0.9rem;
}
.error-icon {
    font-size: 1rem;
    line-height: 1.4;
    flex-shrink: 0;
}
.streaming-error-marker {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    margin-top: 0.6rem;
    padding-top: 0.6rem;
    border-top: 1px dashed var(--border-medium);
    color: var(--status-red);
    font-size: 0.85rem;
}
.answer-text {
    line-height: 1.6;
}
.streaming-text {
    white-space: pre-wrap;
}
.streaming-stopped-marker {
    margin-top: 0.5rem;
    color: var(--text-muted);
    font-size: 0.85rem;
}
.answer-text > *:first-child { margin-top: 0; }
.answer-text > *:last-child { margin-bottom: 0; }
.answer-text p { margin: 0 0 0.6rem; }
.answer-text h1, .answer-text h2, .answer-text h3, .answer-text h4 {
    margin: 0.75rem 0 0.4rem;
    font-family: var(--font-heading);
    color: var(--text-primary);
}
.answer-text h1 { font-size: 1.15rem; }
.answer-text h2 { font-size: 1.05rem; }
.answer-text h3 { font-size: 0.98rem; }
.answer-text ul, .answer-text ol {
    margin: 0 0 0.6rem 1.25rem;
}
.answer-text li { margin-bottom: 0.2rem; }
.answer-text code {
    background: var(--bg-secondary);
    padding: 0.05rem 0.3rem;
    border-radius: 4px;
    font-size: 0.88em;
}
.answer-text pre {
    background: var(--bg-secondary);
    padding: 0.6rem 0.75rem;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 0 0 0.6rem;
    border: 1px solid var(--border-light);
}
.answer-text pre code {
    background: none;
    padding: 0;
}
.answer-text strong { font-weight: 600; }
.answer-text em { font-style: italic; }
.answer-text blockquote {
    border-left: 3px solid var(--accent-light);
    padding-left: 0.75rem;
    color: var(--text-secondary);
    margin: 0 0 0.6rem;
}
.answer-text table {
    border-collapse: collapse;
    margin-bottom: 0.6rem;
    font-size: 0.9rem;
}
.answer-text th, .answer-text td {
    padding: 0.35rem 0.55rem;
    border: 1px solid var(--border-light);
    text-align: left;
}
.answer-text th { background: var(--bg-secondary); font-weight: 600; }

/* ---- Auto-chart (horizontal bars in assistant messages) ---- */
.chart-block {
    margin-top: 0.85rem;
    padding: 0.85rem 0.95rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
}
.chart-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    margin-bottom: 0.1rem;
}
.chart-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.chart-actions {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    flex-shrink: 0;
}
.chart-expand,
.chart-csv {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    background: none;
    border: 1px solid transparent;
    color: var(--text-muted);
    font-family: var(--font-body);
    font-size: 0.85rem;
    line-height: 1;
    padding: 0.2rem 0.45rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast), border-color var(--transition-fast);
}
.chart-expand:hover,
.chart-csv:hover {
    background: var(--bg-card);
    color: var(--accent);
    border-color: var(--border-medium);
}
.chart-btn-text {
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.04em;
}

/* ---- Chart full-screen modal ---- */
.chart-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(25, 25, 24, 0.55);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 2rem;
}
.chart-modal-overlay.open {
    display: flex;
}
.chart-modal-card {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-light);
    width: min(1100px, 100%);
    max-height: calc(100vh - 4rem);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.chart-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.85rem 1.25rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-light);
}
.chart-modal-title {
    font-family: var(--font-heading);
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.chart-modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    line-height: 1;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0 0.25rem;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast), color var(--transition-fast);
}
.chart-modal-close:hover {
    background: var(--border-light);
    color: var(--text-primary);
}
.chart-modal-body {
    padding: 2rem 2rem 1.25rem;
    overflow: auto;
    flex: 1;
}
.chart-modal-body .bar-chart {
    height: min(60vh, 520px);
    gap: 0.75rem;
    padding-top: 2.25rem;
}
.chart-modal-body .bar-row {
    min-width: 56px;
}
.chart-modal-body .bar-label {
    font-size: 0.85rem;
    margin-top: 0.6rem;
}
.chart-modal-body .bar-value {
    font-size: 0.85rem;
    margin-bottom: 6px;
}
.chart-axis-label {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.bar-chart {
    display: flex;
    align-items: stretch;
    gap: 0.35rem;
    padding: 1.4rem 0.25rem 0;
    height: 240px;
    overflow-x: auto;
    overflow-y: hidden;
}
.bar-row {
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    min-width: 42px;
    align-items: stretch;
}
.bar-track {
    flex: 1;
    position: relative;
    border-bottom: 1px solid var(--border-medium);
}
.bar-fill {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background: var(--gradient-accent);
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    transition: height var(--transition-base);
    min-height: 4px;
}
.bar-value {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: 4px;
    font-family: var(--font-code);
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
}
.bar-label {
    margin-top: 0.4rem;
    font-size: 0.68rem;
    color: var(--text-secondary);
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.2;
}

/* ---- Sources pills row (web answers) ---- */
.citation-pills-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.75rem;
    padding-top: 0.6rem;
    border-top: 1px solid var(--border-light);
}
.citation-pills-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-right: 0.25rem;
}
.citation-source-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.2rem 0.6rem;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-size: 0.78rem;
    border: 1px solid var(--border-light);
    border-radius: 100px;
    cursor: pointer;
    transition: background var(--transition-fast), border-color var(--transition-fast);
    max-width: 100%;
}
.citation-source-pill:hover {
    background: var(--border-light);
    border-color: var(--accent-light);
}
.citation-source-pill .citation-pill-num {
    color: var(--accent);
    font-weight: 600;
}
.citation-source-pill .citation-pill-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 220px;
}
.message-meta {
    margin-top: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ---- Web fallback toggle (header) ---- */
.web-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.8rem;
    color: var(--text-secondary);
    cursor: pointer;
    user-select: none;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast);
}
.web-toggle:hover {
    background: var(--bg-secondary);
}
.web-toggle input[type="checkbox"] {
    width: 14px;
    height: 14px;
    accent-color: var(--accent);
    cursor: pointer;
}
.web-toggle select {
    border: 1px solid var(--border-medium);
    border-radius: 4px;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.78rem;
    padding: 0.1rem 0.35rem;
    font-family: var(--font-body);
    cursor: pointer;
}
.web-toggle select:focus {
    outline: none;
    border-color: var(--accent);
}

/* ---- Info tooltip for header toggles ---- */
.toggle-group {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
}
.info-tip {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: var(--border-light);
    color: var(--text-muted);
    font-family: Georgia, "Times New Roman", serif;
    font-size: 0.7rem;
    font-style: italic;
    font-weight: 700;
    line-height: 1;
    cursor: help;
    user-select: none;
    transition: background var(--transition-fast), color var(--transition-fast);
}
.info-tip:hover,
.info-tip:focus {
    background: var(--accent-light);
    color: #fff;
    outline: none;
}
.info-tip::after {
    content: attr(data-tip);
    position: absolute;
    top: calc(100% + 10px);
    right: -6px;
    width: max-content;
    max-width: 240px;
    padding: 0.5rem 0.7rem;
    background: var(--text-primary);
    color: var(--bg-primary);
    font-family: var(--font-body);
    font-size: 0.72rem;
    font-style: normal;
    font-weight: 400;
    line-height: 1.45;
    text-align: left;
    white-space: normal;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
    z-index: 1000;
}
.info-tip::before {
    content: "";
    position: absolute;
    top: calc(100% + 4px);
    right: 3px;
    border: 6px solid transparent;
    border-bottom-color: var(--text-primary);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
    z-index: 1000;
}
.info-tip:hover::after,
.info-tip:hover::before,
.info-tip:focus::after,
.info-tip:focus::before {
    opacity: 1;
}

/* ---- Source badge (Web search indicator on assistant message) ---- */
.source-badge {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 0.15rem 0.55rem;
    border-radius: 100px;
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.source-badge-web {
    background: rgba(91, 122, 158, 0.12);
    color: var(--status-blue);
    border: 1px solid rgba(91, 122, 158, 0.3);
}
.source-badge-web::before {
    content: "\1F310  ";
    margin-right: 0.15rem;
}
.source-badge-ai {
    background: var(--bg-secondary);
    color: var(--text-muted);
    border: 1px solid var(--border-medium);
}
.source-badge-ai::before {
    content: "\1F4AD  ";
    margin-right: 0.15rem;
}

/* ---- Open-source link in reference panel ---- */
.reference-open {
    display: inline-block;
    margin-top: 0.5rem;
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
    font-size: 0.85rem;
}
.reference-open:hover {
    text-decoration: underline;
}

/* ---- Stop button (Send morphs into Stop while in-flight) ---- */
.btn-stop {
    background: var(--bg-secondary);
    color: var(--status-red);
    border: 1px solid var(--status-red);
}
.btn-stop:hover {
    background: var(--status-red);
    color: #fff;
}

/* ---- Stopped marker (replaces thinking bubble on abort) ---- */
.stopped-message {
    background: transparent;
    border: 1px dashed var(--border-medium);
    box-shadow: none;
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 0.5rem 0.85rem;
}

/* ---- Thinking bubble (assistant placeholder while waiting) ---- */
.thinking-bubble {
    padding: 0.75rem 1rem;
}
.thinking-dots {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 1rem;
}
.thinking-dots span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: thinking-pulse 1.3s ease-in-out infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.18s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.36s; }
@keyframes thinking-pulse {
    0%, 60%, 100% { opacity: 0.25; transform: scale(0.85); }
    30%           { opacity: 1;    transform: scale(1); }
}

/* ---- Citation badges (inline, clickable) ---- */
.citation-badge {
    display: inline-block;
    background: rgba(196,112,75,0.12);
    color: var(--accent);
    font-size: 0.8rem;
    font-weight: 600;
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    border: 1px solid transparent;
    cursor: pointer;
    font-family: var(--font-body);
    line-height: 1.3;
    transition: background var(--transition-fast), border-color var(--transition-fast);
}
.citation-badge:hover {
    background: rgba(196,112,75,0.2);
    border-color: var(--accent-light);
}
.citation-badge:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(196,112,75,0.2);
}

/* ---- Reference panel (slide-out) ---- */
.reference-panel {
    position: fixed;
    top: var(--header-height);
    right: 0;
    width: 420px;
    max-width: 90vw;
    height: calc(100vh - var(--header-height));
    background: var(--bg-card);
    border-left: 1px solid var(--border-light);
    box-shadow: var(--shadow-lg);
    transform: translateX(100%);
    transition: transform var(--transition-slow);
    display: flex;
    flex-direction: column;
    z-index: 50;
}
.reference-panel.open {
    transform: translateX(0);
}
.reference-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.85rem 1.25rem;
    border-bottom: 1px solid var(--border-light);
    background: var(--bg-secondary);
    flex-shrink: 0;
}
.reference-panel-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
}
.reference-ref {
    font-weight: 600;
    color: var(--accent);
    font-size: 0.85rem;
    flex-shrink: 0;
}
.reference-filename {
    font-family: var(--font-code);
    font-size: 0.85rem;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.reference-panel-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    line-height: 1;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0 0.25rem;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast), color var(--transition-fast);
}
.reference-panel-close:hover {
    background: var(--border-light);
    color: var(--text-primary);
}
.reference-panel-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.5rem 1.25rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border-light);
    flex-shrink: 0;
}
.reference-toggle {
    background: none;
    border: none;
    color: var(--accent);
    font-family: var(--font-body);
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0.15rem 0.4rem;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast);
}
.reference-toggle:hover {
    background: rgba(196,112,75,0.08);
    text-decoration: underline;
}
.reference-toggle:disabled {
    color: var(--text-muted);
    cursor: wait;
    text-decoration: none;
}

.doc-chunk {
    padding: 0.5rem 0;
}
.doc-chunk + .doc-chunk {
    border-top: 1px dashed var(--border-light);
    margin-top: 0.5rem;
}
.doc-chunk-cited {
    background: rgba(196,112,75,0.08);
    border-left: 3px solid var(--accent);
    padding-left: 0.75rem;
    margin-left: -0.75rem;
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    scroll-margin-top: 0.5rem;
}
.reference-panel-body {
    padding: 1.25rem;
    overflow-y: auto;
    color: var(--text-primary);
    font-size: 0.9rem;
    line-height: 1.65;
}
.reference-panel-body h1,
.reference-panel-body h2,
.reference-panel-body h3,
.reference-panel-body h4 {
    margin: 1rem 0 0.5rem;
    color: var(--text-primary);
}
.reference-panel-body h1:first-child,
.reference-panel-body h2:first-child,
.reference-panel-body h3:first-child {
    margin-top: 0;
}
.reference-panel-body h1 { font-size: 1.15rem; }
.reference-panel-body h2 { font-size: 1.05rem; }
.reference-panel-body h3 { font-size: 0.95rem; }
.reference-panel-body p {
    margin-bottom: 0.75rem;
}
.reference-panel-body ul,
.reference-panel-body ol {
    margin: 0 0 0.75rem 1.25rem;
}
.reference-panel-body li {
    margin-bottom: 0.25rem;
}
.reference-panel-body code {
    background: var(--bg-secondary);
    padding: 0.05rem 0.3rem;
    border-radius: 4px;
    font-size: 0.85em;
    color: var(--text-primary);
}
.reference-panel-body pre {
    background: var(--bg-secondary);
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin-bottom: 0.75rem;
    border: 1px solid var(--border-light);
}
.reference-panel-body pre code {
    background: none;
    padding: 0;
    font-size: 0.85em;
}
.reference-panel-body table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 0.75rem;
    font-size: 0.85rem;
}
.reference-panel-body th,
.reference-panel-body td {
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--border-light);
    text-align: left;
}
.reference-panel-body th {
    background: var(--bg-secondary);
    font-weight: 600;
}
.reference-panel-body blockquote {
    border-left: 3px solid var(--accent-light);
    padding-left: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
}

/* ---- Input bar ---- */
.chat-input-bar {
    padding: 0.75rem 1.5rem;
    background: var(--bg-card);
    border-top: 1px solid var(--border-light);
    flex-shrink: 0;
}
#query-form {
    max-width: 800px;
    margin: 0 auto;
}
.input-row {
    display: flex;
    gap: 0.5rem;
}
.input-row input[type="text"] {
    flex: 1;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 0.95rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.input-row input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(196,112,75,0.15);
}

/* ---- Admin ingest layout ---- */
.ingest-layout {
    flex: 1;
    display: grid;
    grid-template-columns: 320px 1fr;
    overflow: hidden;
}

/* Left panel: corpora list */
.corpora-panel {
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-light);
    padding: 1.5rem;
    overflow-y: auto;
}
.corpora-panel h2 {
    font-size: 1.1rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
}
.corpora-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.corpus-card {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    padding: 0.75rem;
    box-shadow: var(--shadow-sm);
}
.corpus-name {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.35rem;
}
.corpus-stats {
    display: flex;
    gap: 0.5rem;
}
.stat-pill {
    font-size: 0.75rem;
    background: var(--bg-secondary);
    color: var(--text-muted);
    padding: 0.15rem 0.5rem;
    border-radius: 100px;
}
.empty-state {
    color: var(--text-muted);
    font-size: 0.9rem;
    font-style: italic;
}

/* Right panel: upload form */
.upload-panel {
    padding: 1.5rem 2rem;
    overflow-y: auto;
    max-width: 700px;
}
.upload-panel h2 {
    font-size: 1.1rem;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

/* Drop zone */
.drop-zone {
    border: 2px dashed var(--border-medium);
    border-radius: var(--radius-lg);
    padding: 2rem;
    text-align: center;
    cursor: pointer;
    transition: border-color var(--transition-base), background var(--transition-base);
    margin-bottom: 1rem;
}
.drop-zone:hover,
.drop-zone.drag-over {
    border-color: var(--accent);
    background: rgba(196,112,75,0.04);
}
.drop-icon {
    font-size: 2rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    line-height: 1;
}
.drop-zone p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 0.25rem;
}
.drop-limits {
    color: var(--text-muted);
    font-size: 0.8rem;
}
.file-label {
    color: var(--accent);
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
}

/* File list */
.file-list {
    margin-bottom: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}
.file-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0.6rem;
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
}
.file-item.file-error {
    border-color: var(--status-red);
    background: #fdf2f2;
}
.file-name {
    color: var(--text-primary);
    font-weight: 500;
}
.file-size {
    color: var(--text-muted);
    font-size: 0.8rem;
}

/* Full-width button */
.btn-full {
    width: 100%;
    padding: 0.75rem;
}
.btn-full:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Ingest result messages */
.ingest-result {
    margin-top: 1rem;
    padding: 1rem;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
}
.ingest-result.success {
    background: #f0f7f4;
    border: 1px solid #b8d8c7;
    color: var(--status-green);
}
.ingest-result.success h3 {
    font-size: 0.95rem;
    margin-bottom: 0.35rem;
}
.ingest-result.error {
    background: #fdf2f2;
    border: 1px solid #ebc8c8;
    color: var(--status-red);
}

/* ---- HTMX loading indicator ---- */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator { display: inline; }
.htmx-request #send-text { display: none; }
.htmx-request #ingest-text { display: none; }
.htmx-request #eval-text { display: none; }

/* ---- Admin dashboard ---- */
.dashboard-layout {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem 2rem;
}
.dashboard-inner {
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
}
.dashboard-section {
    margin-bottom: 2.5rem;
    scroll-margin-top: 1rem;
}
.dashboard-section h2 {
    font-size: 1.15rem;
    color: var(--text-primary);
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-light);
}

/* Stats grid */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
}
.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    padding: 1.25rem;
    box-shadow: var(--shadow-sm);
    text-align: center;
}
.stat-card .stat-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 0.35rem;
}
.stat-card .stat-value {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-primary);
    font-family: var(--font-heading);
}
.stat-card .stat-unit {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

/* Dashboard table */
.dashboard-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}
.dashboard-table th {
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-weight: 600;
    text-align: left;
    padding: 0.6rem 0.75rem;
    border-bottom: 2px solid var(--border-medium);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.dashboard-table td {
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border-light);
    color: var(--text-primary);
    vertical-align: top;
}
.dashboard-table tr:hover td {
    background: var(--bg-secondary);
}
.query-text {
    max-width: 350px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Query filters */
.query-filters {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1rem;
    align-items: center;
    flex-wrap: wrap;
}
.query-filters select,
.query-filters input[type="search"] {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 0.85rem;
    background: var(--bg-primary);
    color: var(--text-primary);
}
.query-filters select:focus,
.query-filters input[type="search"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(196,112,75,0.15);
}
.query-filters input[type="search"] {
    flex: 1;
    max-width: 300px;
}

/* Expandable query detail */
.query-detail {
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
    padding: 0.75rem;
    margin-top: 0.35rem;
    font-size: 0.85rem;
    color: var(--text-secondary);
    line-height: 1.5;
    white-space: pre-wrap;
}
.query-detail .detail-label {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
    display: block;
}
.query-detail .detail-block {
    margin-bottom: 0.5rem;
}
.query-expand {
    cursor: pointer;
    color: var(--accent);
    font-weight: 600;
    font-size: 0.8rem;
    background: none;
    border: none;
    padding: 0;
    font-family: var(--font-body);
}
.query-expand:hover { text-decoration: underline; }

/* Latency pills */
.latency-pill {
    display: inline-block;
    padding: 0.1rem 0.5rem;
    border-radius: 100px;
    font-size: 0.8rem;
    font-weight: 600;
    font-family: var(--font-code);
}
.latency-fast { background: #f0f7f4; color: var(--status-green); }
.latency-mid  { background: #fdf8ec; color: var(--status-yellow); }
.latency-slow { background: #fdf2f2; color: var(--status-red); }

/* Latency bar chart */
.latency-chart {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    height: 200px;
    padding: 1rem 0;
    border-bottom: 1px solid var(--border-medium);
}
.latency-bar {
    flex: 1;
    background: var(--gradient-accent);
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    min-width: 20px;
    position: relative;
    transition: opacity var(--transition-fast);
}
.latency-bar:hover { opacity: 0.8; }
.bar-tooltip {
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text-primary);
    color: var(--bg-card);
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-fast);
    margin-bottom: 4px;
}
.latency-bar:hover .bar-tooltip { opacity: 1; }
.latency-labels {
    display: flex;
    gap: 4px;
    margin-top: 0.5rem;
}
.latency-labels span {
    flex: 1;
    text-align: center;
    font-size: 0.7rem;
    color: var(--text-muted);
}
.latency-chart-count {
    text-align: right;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* Eval cards */
.eval-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1rem;
}
.eval-card {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg);
    padding: 1.25rem;
    box-shadow: var(--shadow-sm);
}
.eval-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}
.eval-card-header h3 {
    font-size: 0.95rem;
    color: var(--text-primary);
}
.eval-status {
    font-size: 0.75rem;
    padding: 0.15rem 0.5rem;
    border-radius: 100px;
    font-weight: 600;
}
.eval-status.completed { background: #f0f7f4; color: var(--status-green); }
.eval-status.running   { background: #eff6ff; color: var(--status-blue); }
.eval-status.failed    { background: #fdf2f2; color: var(--status-red); }
.eval-meta {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}
.eval-metrics {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}
.eval-metric { text-align: center; }
.eval-metric .metric-name {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 0.15rem;
}
.eval-metric .metric-value {
    font-size: 1.25rem;
    font-weight: 700;
    font-family: var(--font-heading);
}
.eval-card-footer {
    margin-top: 1rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border-light);
}

/* Score colors */
.score-high { color: var(--status-green); }
.score-mid  { color: var(--status-yellow); }
.score-low  { color: var(--status-red); }

/* Eval run form */
.eval-run-form {
    margin-bottom: 1.5rem;
}
.eval-run-form input[type="text"] {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 0.85rem;
    background: var(--bg-primary);
    color: var(--text-primary);
}
.eval-run-form input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(196,112,75,0.15);
}

/* Empty state card */
.empty-state-card {
    background: var(--bg-secondary);
    border: 1px dashed var(--border-medium);
    border-radius: var(--radius-lg);
    padding: 2rem;
    text-align: center;
    color: var(--text-muted);
}
.empty-state-card p { margin-bottom: 0.5rem; }
.empty-state-card code {
    display: block;
    margin-top: 0.75rem;
    background: var(--bg-card);
    padding: 0.5rem 1rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    color: var(--text-secondary);
}

/* Pagination */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1rem;
    font-size: 0.85rem;
}
.pagination a,
.pagination span {
    padding: 0.35rem 0.65rem;
    border-radius: var(--radius-sm);
    text-decoration: none;
}
.pagination a {
    color: var(--accent);
    border: 1px solid var(--border-light);
    cursor: pointer;
}
.pagination a:hover { background: var(--bg-secondary); }
.pagination .current {
    background: var(--accent);
    color: #fff;
    border: 1px solid var(--accent);
}
.pagination .disabled {
    color: var(--text-muted);
    border: 1px solid var(--border-light);
    cursor: default;
}

/* Eval detail table (loaded via HTMX) */
.eval-detail-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
    margin-top: 0.5rem;
}
.eval-detail-table th {
    background: var(--bg-secondary);
    padding: 0.4rem 0.6rem;
    text-align: left;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-medium);
}
.eval-detail-table td {
    padding: 0.4rem 0.6rem;
    border-bottom: 1px solid var(--border-light);
    vertical-align: top;
}
.eval-detail-table td.wrap-cell {
    max-width: 300px;
    white-space: pre-wrap;
    word-break: break-word;
}

/* ---- User management page ---- */
.user-form {
    margin-top: 0.5rem;
}
.user-form-row {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
    flex-wrap: wrap;
}
.user-form-field {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}
.user-form-field label {
    font-size: 0.78rem;
    color: var(--text-secondary);
    font-weight: 500;
}
.user-form-field input {
    padding: 0.45rem 0.6rem;
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
    font-size: 0.9rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-width: 220px;
}
.user-form-field input:focus {
    outline: none;
    border-color: var(--accent);
}
.user-form-submit {
    align-self: flex-end;
}
.user-form-hint {
    margin-top: 0.75rem;
    font-size: 0.78rem;
    color: var(--text-muted);
}
.user-inline-form {
    display: inline-flex;
    gap: 0.4rem;
    align-items: center;
}
.user-inline-form input {
    padding: 0.3rem 0.5rem;
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-sm);
    font-size: 0.82rem;
    font-family: var(--font-body);
    background: var(--bg-primary);
    color: var(--text-primary);
}
.user-inline-form input:focus {
    outline: none;
    border-color: var(--accent);
}
.user-empty {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-top: 0.5rem;
}
.status-badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.status-badge.status-active {
    background: #ebf2ec;
    color: var(--status-green);
}
.status-badge.status-disabled {
    background: var(--bg-secondary);
    color: var(--text-muted);
}

/* ---- Header user pill (current user + role) ---- */
.user-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.25rem 0.5rem 0.25rem 0.65rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-light);
    border-radius: 999px;
    font-size: 0.78rem;
    color: var(--text-secondary);
    line-height: 1;
}
.user-pill-name {
    font-weight: 500;
    color: var(--text-primary);
}
.user-pill-role {
    display: inline-block;
    padding: 0.18rem 0.45rem;
    border-radius: 999px;
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    line-height: 1;
}
.user-pill-role-admin {
    background: var(--accent);
    color: #fff;
}
.user-pill-role-user {
    background: var(--border-light);
    color: var(--text-secondary);
}
