:root {
    --bg-0: #111827;
    --bg-1: #1f2937;
    --bg-2: #0b1220;
    --panel: #111827cc;
    --panel-border: #334155;
    --text: #e2e8f0;
    --text-soft: #94a3b8;
    --primary: #0ea5e9;
    --primary-soft: #38bdf8;
    --danger: #ef4444;
    --ok-bg: #064e3b;
    --ok-text: #d1fae5;
    --err-bg: #7f1d1d;
    --err-text: #fee2e2;
    --msg-user-bg: #0c4a6e;
    --msg-assistant-bg: #0f172a;
    --input-height-mobile: 50px;
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    width: 100%;
}

body {
    margin: 0;
    padding: 0;
    font-family: "Segoe UI", Tahoma, sans-serif;
    color: var(--text);
    background: radial-gradient(circle at 10% 10%, #1e293b, transparent 38%),
        radial-gradient(circle at 90% 85%, #0f766e44, transparent 32%),
        linear-gradient(150deg, var(--bg-2), var(--bg-0));
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

/* ── Login ─────────────────────────────────────────────── */
.login-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    padding: 14px;
    overflow: auto;
    background: radial-gradient(circle at 10% 10%, #1e293b, transparent 38%),
        radial-gradient(circle at 90% 85%, #0f766e44, transparent 32%),
        linear-gradient(150deg, var(--bg-2), var(--bg-0));
}

.login-overlay.hidden { display: none; }

.login-card {
    background: var(--bg-1);
    border: 1px solid var(--panel-border);
    border-radius: 12px;
    padding: 2rem 2.5rem;
    width: min(380px, calc(100vw - 28px));
    max-width: 90vw;
    box-shadow: 0 8px 32px rgba(0,0,0,.4);
}

.login-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1.5rem;
}
.login-header h1 {
    font-size: 1.15rem;
    margin: 0;
    color: var(--text);
}

.login-field { margin-bottom: 1rem; }
.login-field label {
    display: block;
    font-size: .82rem;
    color: var(--text-soft);
    margin-bottom: .3rem;
}
.login-field input {
    width: 100%;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid var(--panel-border);
    background: var(--bg-0);
    color: var(--text);
    font-size: 14px;
    transition: border-color 200ms ease, box-shadow 200ms ease;
}
.login-field input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.1);
}

.login-error {
    color: var(--danger);
    font-size: .85rem;
    margin-bottom: .8rem;
}

/* ── Header right section (user + status + logout) ─── */
.header-right {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: auto;
    font-size: 12px;
    color: var(--text-soft);
    min-width: 0;
}
.header-username {
    white-space: nowrap;
    max-width: 34vw;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
}
.header-language {
    font-size: 10px;
    color: var(--text-soft);
    border: 1px solid var(--panel-border);
    border-radius: 999px;
    padding: 3px 6px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    flex: 0 0 auto;
}
.header-right .btn-sm {
    padding: 6px 10px;
    font-size: 12px;
    border-radius: 4px;
    background: transparent;
    border: 1px solid var(--panel-border);
    color: var(--text-soft);
    cursor: pointer;
    min-height: 36px;
    transition: all 200ms ease;
    touch-action: manipulation;
}
.header-right .btn-sm:active {
    transform: scale(0.98);
}
.header-right .btn-sm:hover {
    border-color: var(--danger);
    color: var(--danger);
}

.chat-wrapper {
    width: min(1180px, calc(100vw - 32px));
    height: 100%;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0 auto;
    padding: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    gap: 0;
    min-height: 0;
    overflow: hidden;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    border: none;
    border-bottom: 1px solid var(--panel-border);
    border-radius: 0;
    background: var(--panel);
    backdrop-filter: blur(8px);
    min-width: 0;
    flex-wrap: nowrap;
    gap: 8px;
    min-height: 52px;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.brand-logo {
    display: block;
    object-fit: contain;
}

.brand-logo-login {
    height: 38px;
    width: auto;
    max-width: 220px;
}

.brand-logo-header {
    height: 28px;
    width: auto;
    max-width: 160px;
}

.brand-orb {
    width: 14px;
    height: 14px;
    border-radius: 999px;
    background: linear-gradient(135deg, #0ea5e9, #14b8a6);
    box-shadow: 0 0 24px #14b8a688;
}

.header h1 {
    margin: 0;
    font-size: 18px;
    letter-spacing: 0.4px;
}

.status {
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 11px;
    background: #1e293b;
    color: var(--text-soft);
    border: 1px solid #334155;
    flex: 0 0 auto;
    font-weight: 500;
}

.tenant-input-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
    font-size: 12px;
}

.tenant-value {
    color: var(--text);
    font-weight: 600;
    letter-spacing: 0.2px;
}

.status.ok {
    background: var(--ok-bg);
    border-color: #10b981;
    color: var(--ok-text);
}

.status.err {
    background: var(--err-bg);
    border-color: #dc2626;
    color: var(--err-text);
}

.control-bar {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 8px;
    align-items: center;
}

.tenant-label {
    font-size: 12px;
    color: var(--text-soft);
}

input,
textarea,
button {
    font: inherit;
}

input,
textarea {
    border: 1px solid #334155;
    border-radius: 8px;
    padding: 10px 12px;
    background: #0b1220;
    color: var(--text);
    min-width: 0;
    font-size: 14px;
    transition: all 200ms ease;
}

input:focus,
textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.1);
}

textarea {
    resize: vertical;
    max-height: 30dvh;
}

button {
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 10px 14px;
    color: #f8fafc;
    background: #1e293b;
    cursor: pointer;
    touch-action: manipulation;
    transition: all 200ms ease;
    font-size: 14px;
    min-height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

button:active:not(:disabled) {
    transform: scale(0.98);
}

button:hover:not(:disabled) {
    transform: translateY(-1px);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.btn.primary {
    background: linear-gradient(140deg, var(--primary), var(--primary-soft));
    border-color: #0284c7;
    color: #082f49;
    font-weight: 700;
}

.btn.secondary {
    background: #0f172a;
    border-color: #334155;
}

.btn.danger {
    background: #7f1d1d;
    border-color: #dc2626;
}

.history-meta {
    min-height: 0;
    font-size: 12px;
    color: var(--text-soft);
    display: none;
}

.chat {
    border: none;
    border-radius: 0;
    background: transparent;
    overflow: auto;
    overflow-x: hidden;
    padding: 8px 12px;
    min-height: 0;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.msg {
    margin: 4px 0;
    border-radius: 12px;
    padding: 10px 12px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    max-width: 100%;
    display: flex;
    flex-direction: column;
    gap: 4px;
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.msg.user {
    background: var(--msg-user-bg);
    border-left: 3px solid var(--primary-soft);
    margin-left: auto;
    width: min(90%, 700px);
    border-radius: 16px;
    border-left: none;
    align-self: flex-end;
}

.msg.assistant {
    background: var(--msg-assistant-bg);
    border-left: 3px solid #14b8a6;
    width: min(95%, 700px);
    border-left: none;
    align-self: flex-start;
}

.msg.error {
    background: #7f1d1d33;
    border-left: 3px solid #ef4444;
    width: min(95%, 700px);
    border-left: none;
}

.msg-head {
    margin-bottom: 4px;
    font-size: 11px;
    font-weight: 600;
    color: #ef4444;
    opacity: 0.9;
}

.msg-content {
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
}

.msg-time {
    margin-bottom: 6px;
    color: #e0f2fe;
    font-size: 11px;
    line-height: 1;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    opacity: 0.92;
    text-align: right;
}

.composer {
    display: grid;
    gap: 12px;
    align-items: start;
    align-self: end;
    padding: 10px 12px 12px;
    background: var(--panel);
    border-top: 1px solid var(--panel-border);
    min-height: auto;
    width: 100%;
}

.route-mode-card {
    border: none;
    border-radius: 0;
    background: transparent;
    padding: 0 0 4px 0;
    height: auto;
    min-height: 0;
    align-self: start;
    margin-top: 0;
    border-top: none;
}

.route-mode-card-bottom {
    margin-top: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    flex-wrap: wrap;
    overflow: visible;
}

.route-mode-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.route-mode-head-inline {
    flex: 0 0 auto;
    width: 100%;
}

.route-mode-label {
    font-size: 11px;
    font-weight: 700;
    color: var(--text-soft);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.route-mode-collapse {
    min-height: 28px;
    width: 28px;
    padding: 0;
    border-radius: 999px;
    border: 1px solid #334155;
    background: #0b1220;
    color: var(--text-soft);
    font-size: 12px;
    line-height: 1;
    margin-left: auto;
}

.route-mode-collapse:hover {
    border-color: var(--primary);
    color: var(--text);
}


.route-mode-help {
    margin-top: 2px;
    font-size: 11px;
    gap: 6px;
}

.route-mode-toolbar {
    padding-top: 0.25rem;
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    flex: 1 1 auto;
    min-width: 0;
    overflow-x: visible;
    overflow-y: hidden;
}

.route-mode-toolbar::-webkit-scrollbar {
    display: none;
}

.route-mode-card.is-hidden {
    display: none;
}

.route-mode-btn {
    width: auto;
    box-shadow: none;
    font-size: 12px;
    padding: 6px 12px;
    background: #0b1220;
    color: #cbd5e1;
    font-weight: 600;
    flex: 0 0 auto;
    min-height: 32px;
    cursor: pointer;
    transition: all 200ms ease;
}

.route-mode-btn:active {
    transform: scale(0.98);
}

.route-mode-btn.active {
    background: #115e59;
    border-color: #14b8a6;
    color: #ccfbf1;
}

.context-setting-row {
    margin-top: 2px;
    padding: 4px 2px;
}

.context-setting-row-sidebar {
    margin-top: 8px;
    padding: 6px 0 2px;
}

.context-setting-row-sidebar .context-setting-label {
    align-items: flex-start;
}

.context-setting-label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--text-soft);
    user-select: none;
}

.context-setting-label input[type="checkbox"] {
    width: 15px;
    height: 15px;
    accent-color: #14b8a6;
}

.context-alert {
    margin-top: 6px;
    border: 1px solid #92400e;
    background: #78350f33;
    color: #fde68a;
    border-radius: 8px;
    padding: 6px 8px;
    font-size: 11px;
    line-height: 1.35;
}

.context-alert strong {
    color: #fbbf24;
}

.input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    width: 100%;
}

.route-mode-badge {
    flex: 0 0 auto;
    min-height: 44px;
    padding: 0 12px;
    border-radius: 999px;
    border: 1px solid #155e75;
    background: #0b1220;
    color: #67e8f9;
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
}

.route-mode-badge[hidden] {
    display: none;
}

textarea#question {
    flex: 1 1 auto;
    min-height: 44px;
    max-height: 120px;
    padding: 10px 12px;
    border-radius: 20px;
    border: 1px solid var(--panel-border);
    background: var(--bg-2);
    color: var(--text);
    font-size: 14px;
    resize: none;
    font-family: inherit;
}

textarea#question:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(14, 165, 233, 0.1);
}

textarea#question::placeholder {
    color: var(--text-soft);
}

#send,
#stop {
    flex: 0 0 44px;
    min-height: 44px;
    width: 72px;
    padding: 0 10px;
    border-radius: 10px;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 200ms ease;
    font-size: 18px;
}

#send {
    background: linear-gradient(140deg, var(--primary), var(--primary-soft));
    color: #082f49;
    font-weight: 700;
}

#send:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(14, 165, 233, 0.4);
}

#send:active:not(:disabled) {
    transform: scale(0.98);
}

#stop {
    background: #7f1d1d;
    border: 1px solid #dc2626;
    color: #fecaca;
}

#stop:hover:not(:disabled) {
    background: #991b1b;
}

.result-box {
    margin-top: 8px;
    padding: 8px;
    border-radius: 8px;
    border: 1px solid #334155;
    background: #0b1220;
}

.result-table-wrap {
    margin-top: 8px;
    overflow: auto;
    border: 1px solid #334155;
    border-radius: 8px;
    background: #030712;
    overscroll-behavior: contain;
}

.result-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.result-table th,
.result-table td {
    border-bottom: 1px solid #1e293b;
    padding: 6px 8px;
    text-align: left;
    vertical-align: top;
}

.result-table th {
    background: #0b1220;
    position: sticky;
    top: 0;
    z-index: 1;
}

.result-pre {
    margin-top: 8px;
    padding: 8px;
    border-radius: 8px;
    border: 1px solid #334155;
    background: #030712;
    max-height: 280px;
    overflow: auto;
}

.sql-query-panel {
    margin-top: 8px;
    margin-bottom: 14px;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid #1e3a5f;
    background: rgba(8, 47, 73, 0.42);
}

.sql-query-title {
    margin-bottom: 5px;
    color: #7dd3fc;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.sql-query-code {
    margin: 0;
    max-height: 180px;
    overflow: auto;
    color: #dbeafe;
    font-family: Consolas, "SFMono-Regular", "Liberation Mono", monospace;
    font-size: 11px;
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
}

.streaming-text {
    margin-top: 10px;
}

.workflow-result-wrap {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.workflow-json-wrap {
    margin-top: 0;
}

.workflow-json-pane {
    overflow: hidden;
}

.workflow-json-pre {
    margin-top: 0;
    max-height: 360px;
}

/* ── Synthesized doc answer (ChatGPT-style) ────────────────────────────────── */
.doc-answer-wrap {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* ── Synthesized data analysis answer ──────────────────────────────────────── */
.data-answer-wrap {
    margin-bottom: 14px;
    padding: 12px 14px;
    background: rgba(56, 189, 248, 0.06);
    border-left: 3px solid #38bdf8;
    border-radius: 6px;
}

.data-answer-text {
    font-size: 0.93rem;
    line-height: 1.65;
    color: #e2e8f0;
    white-space: pre-wrap;
}

.data-answer-text p {
    margin: 0 0 8px 0;
}

.data-answer-text strong {
    color: #f8fafc;
    font-weight: 700;
}

.data-answer-text em {
    color: #cbd5e1;
}

.doc-answer-text {
    font-size: 0.95rem;
    line-height: 1.65;
    color: #e2e8f0;
    white-space: pre-wrap;
}

.doc-answer-text p {
    margin: 0 0 10px 0;
}

.doc-answer-text strong {
    color: #f8fafc;
    font-weight: 700;
}

.doc-answer-text em {
    color: #cbd5e1;
}

.doc-answer-title {
    margin: 0 0 8px 0;
    font-size: 1rem;
    line-height: 1.3;
    font-weight: 700;
    color: #f8fafc;
}

.doc-cite {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 500;
    color: #38bdf8;
    background: rgba(56, 189, 248, 0.12);
    border: 1px solid rgba(56, 189, 248, 0.3);
    border-radius: 999px;
    padding: 1px 7px;
    margin: 0 2px;
    vertical-align: baseline;
}

.doc-cite em {
    color: #7dd3fc;
    font-style: italic;
}

.doc-sources-header {
    margin-top: 4px;
}

.doc-sources-toggle {
    background: none;
    border: none;
    color: #94a3b8;
    font-size: 0.82rem;
    cursor: pointer;
    padding: 4px 0;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: color 0.15s;
}

.doc-sources-toggle:hover {
    color: #e2e8f0;
}

.doc-sources-toggle-icon {
    font-size: 0.65rem;
    line-height: 1;
}

.doc-sources-section {
    margin-top: 4px;
}

/* ── Documentation cards ───────────────────────────────────────────────────── */
.doc-results {
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.doc-card {
    border: 1px solid #334155;
    border-radius: 10px;
    background: #030712;
    overflow: hidden;
    transition: all 200ms ease;
    cursor: pointer;
}

.doc-card-head {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: baseline;
    padding: 10px 12px 6px;
    background: #0b1220;
    border-bottom: 1px solid #1e293b;
}

.doc-card-title {
    font-weight: 700;
    color: #e2e8f0;
}

.doc-card-score {
    font-size: 12px;
    color: #94a3b8;
}

.doc-card-path {
    padding: 6px 12px 0;
    font-size: 12px;
    color: #94a3b8;
    word-break: break-all;
}

.doc-card-actions {
    padding: 8px 12px 0;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.doc-action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 6px;
    border: 1px solid #334155;
    background: #0b1220;
    color: #cbd5e1;
    font-size: 12px;
    text-decoration: none;
    cursor: pointer;
    min-height: 36px;
    transition: all 200ms ease;
    touch-action: manipulation;
}

.doc-action-btn:active {
    transform: scale(0.95);
}

.doc-action-btn:hover {
    border-color: #3b82f6;
    color: #dbeafe;
}

.doc-card-preview {
    padding: 10px 12px 12px;
    color: #cbd5e1;
    line-height: 1.5;
}

.doc-card-preview.is-collapsed {
    max-height: 220px;
    overflow: hidden;
    mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 1) 78%, rgba(0, 0, 0, 0));
}

.doc-card-preview.is-expanded {
    max-height: none;
    overflow: visible;
    mask-image: none;
}

.doc-card-preview h1,
.doc-card-preview h2,
.doc-card-preview h3,
.doc-card-preview h4,
.doc-card-preview h5,
.doc-card-preview h6 {
    margin: 10px 0 6px;
    color: #e2e8f0;
}

.doc-card-preview p {
    margin: 8px 0;
}

.doc-preview-image {
    margin: 10px 0;
}

.doc-preview-image img {
    max-width: 100%;
    border-radius: 8px;
    border: 1px solid #334155;
}

.doc-preview-table-wrap {
    margin: 10px 0;
    overflow: auto;
    border: 1px solid #334155;
    border-radius: 8px;
    overscroll-behavior: contain;
}

.doc-preview-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

.doc-preview-table th,
.doc-preview-table td {
    border-bottom: 1px solid #1e293b;
    padding: 6px 8px;
    text-align: left;
    vertical-align: top;
}

.doc-preview-table th {
    background: #0b1220;
    color: #e2e8f0;
}

.route-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 8px;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.2px;
}

.route-db {
    background: #0c4a6e;
    color: #e0f2fe;
    border: 1px solid #0284c7;
}

.route-docs {
    background: #064e3b;
    color: #d1fae5;
    border: 1px solid #10b981;
}

.route-ambiguous {
    background: #713f12;
    color: #fef3c7;
    border: 1px solid #f59e0b;
}

.route-workflow {
    background: #1f2937;
    color: #bfdbfe;
    border: 1px solid #60a5fa;
}

.route-confidence {
    opacity: 0.82;
    font-weight: 400;
}

.route-meta {
    margin-bottom: 8px;
}

/* Chart / visualization toggle */
.viz-wrap {
    margin-top: 4px;
}

.viz-toggle-row {
    display: flex;
    gap: 6px;
    margin-bottom: 6px;
}

.viz-btn {
    padding: 6px 14px;
    border-radius: 6px;
    border: 1px solid #334155;
    background: #0b1220;
    color: #94a3b8;
    font-size: 12px;
    cursor: pointer;
    transition: all 200ms ease;
    min-height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.viz-btn:active {
    transform: scale(0.98);
}

.viz-btn:hover {
    background: #1e293b;
    color: #e2e8f0;
}

.viz-btn.active {
    background: #1e3a5f;
    color: #93c5fd;
    border-color: #3b82f6;
}

.viz-canvas {
    max-height: 340px;
}

.viz-chart-pane {
    padding: 8px 0;
}

.route-reason {
    margin-top: 4px;
    font-size: 12px;
    color: #cbd5e1;
}

.disclaimer {
    font-size: 11px;
    color: var(--text-soft);
    text-align: center;
    padding: 6px 8px;
    line-height: 1.4;
}

/* Scrollbar styling for better mobile appearance */
.chat::-webkit-scrollbar {
    width: 6px;
}

.chat::-webkit-scrollbar-track {
    background: transparent;
}

.chat::-webkit-scrollbar-thumb {
    background: #334155;
    border-radius: 3px;
}

.chat::-webkit-scrollbar-thumb:hover {
    background: #475569;
}

/* Mobile-friendly sidebar */
.sidebar {
    position: fixed;
    top: 0;
    left: -100%;
    width: 100%;
    max-width: 320px;
    height: 100vh;
    height: 100dvh;
    background: var(--bg-1);
    border-right: 1px solid var(--panel-border);
    display: flex;
    flex-direction: column;
    transition: none;
    z-index: 1000;
    overflow-y: auto;
    box-shadow: none;
}

.sidebar.open {
    left: 0;
    box-shadow: none;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--panel-border);
    display: flex;
    align-items: center;
    gap: 10px;
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--bg-1);
}

.sidebar-header .brand-orb {
    width: 10px;
    height: 10px;
}

.sidebar-header h2 {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
}

.sidebar-close {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--text);
    font-size: 18px;
    cursor: pointer;
    padding: 0;
}

.sidebar-close:hover {
    color: var(--primary);
}

.sidebar-controls {
    padding: 12px;
    border-bottom: 1px solid var(--panel-border);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.sidebar-controls button {
    width: 100%;
    font-size: 13px;
    padding: 8px;
}

.sidebar-close-btn {
    background: transparent;
    border: none;
}

#newChatBtn {
    background: linear-gradient(140deg, var(--primary), var(--primary-soft));
    border: 1px solid #0284c7;
    color: #082f49;
    font-weight: 700;
}

#newChatBtn:hover {
    filter: brightness(1.1);
}

.sidebar-tenant-input {
    font-size: 12px;
    padding: 6px;
}

.sidebar-history {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
}

.sidebar-tenant-section {
    margin-bottom: 12px;
}

.sidebar-tenant-name {
    font-size: 11px;
    font-weight: 600;
    color: var(--primary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 8px 0 6px 0;
    border-bottom: 1px solid #334155;
    margin-bottom: 6px;
}

.sidebar-history-item {
    margin-bottom: 6px;
    padding: 10px 12px;
    border-radius: 8px;
    background: #0f172a;
    border: 1px solid #334155;
    cursor: pointer;
    font-size: 12px;
    color: var(--text);
    display: block;
    width: 100%;
    text-align: left;
    transition: all 200ms ease;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-height: 40px;
    display: flex;
    align-items: center;
    touch-action: manipulation;
}

.sidebar-history-item:active {
    transform: scale(0.98);
}

.sidebar-history-item:hover {
    background: #1e293b;
    border-color: var(--primary);
}

.sidebar-history-item.active {
    background: #0c4a6e;
    border-color: #0284c7;
    color: #e0f2fe;
}

.sidebar-toggle {
    background: none;
    border: none;
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    padding: 8px;
    margin-right: 6px;
    transition: color 200ms ease, transform 200ms ease;
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    min-height: 40px;
    min-width: 40px;
    touch-action: manipulation;
    border-radius: 6px;
}

.sidebar-toggle:active {
    transform: scale(0.95);
}

.sidebar-toggle:hover {
    color: var(--primary);
    background: rgba(14, 165, 233, 0.1);
}

.app-container {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
    height: 100%;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
}

@media (max-width: 900px) {
    .chat-wrapper {
        width: 100%;
        max-width: none;
        padding: 0;
        gap: 0;
    }

    .header {
        padding: 8px 12px;
        border-radius: 0;
        min-height: 48px;
    }

    .header-brand {
        flex: 0 0 auto;
        gap: 8px;
    }

    .brand-logo-header {
        height: 24px;
        max-width: min(120px, 40vw);
    }

    .header-right {
        flex: 1 1 auto;
        justify-content: flex-end;
        gap: 6px;
        flex-wrap: nowrap;
        font-size: 12px;
        overflow: hidden;
    }

    .header-username {
        max-width: 30vw;
        display: none;
    }

    .header-language {
        padding: 2px 6px;
        font-size: 10px;
    }

    .status {
        padding: 4px 8px;
        font-size: 10px;
        flex: 0 0 auto;
    }

    .header-right .btn-sm {
        padding: 6px 10px;
        min-height: 36px;
        font-size: 12px;
    }

    .chat {
        padding: 8px 10px;
        gap: 4px;
    }

    .msg,
    .msg.user,
    .msg.assistant,
    .msg.error {
        width: auto;
        margin: 2px 0;
        padding: 8px 10px;
        max-width: 95%;
    }

    .msg.user {
        margin-left: auto;
    }

    .msg-content,
    .doc-answer-text,
    .data-answer-text {
        font-size: 13px;
        line-height: 1.45;
    }

    .composer {
        padding: 8px 10px;
        gap: 6px;
    }

    .input-row {
        gap: 6px;
    }

    textarea#question {
        font-size: 14px;
        padding: 8px 10px;
    }

    #send,
    #stop {
        min-height: 40px;
        width: 40px;
        font-size: 16px;
    }

    .route-mode-card {
        padding: 0;
    }

    .route-mode-card-bottom {
        gap: 2px;
    }

    .route-mode-head {
        gap: 4px;
        width: 100%;
    }

    .route-mode-head-inline {
        width: 100%;
        justify-content: space-between;
        gap: 4px;
    }

    .route-mode-collapse {
        min-height: 26px;
        width: 26px;
        font-size: 11px;
    }

    .route-mode-current {
        font-size: 10px;
        padding: 2px 6px;
    }

    .route-mode-toolbar {
        width: 100%;
        gap: 4px;
    }

    .route-mode-btn {
        padding: 5px 10px;
        font-size: 11px;
        min-height: 30px;
    }

    .doc-card-head {
        align-items: flex-start;
        flex-direction: column;
        gap: 4px;
    }

    .result-table,
    .doc-preview-table {
        font-size: 12px;
    }

    .sidebar {
        width: 100%;
        left: -100%;
        border-right: none;
    }

    .sidebar.open {
        left: 0;
    }

    .sidebar-history-item {
        white-space: normal;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }
}

@media (max-width: 520px) {
    .header {
        padding: 8px 10px;
        min-height: 44px;
        gap: 6px;
    }

    .header-brand {
        gap: 6px;
    }

    .brand-logo-header {
        height: 20px;
        max-width: 110px;
    }

    .header-right {
        gap: 4px;
    }

    .status {
        padding: 3px 6px;
        font-size: 10px;
    }

    .header-right .btn-sm {
        padding: 4px 8px;
        min-height: 32px;
        font-size: 11px;
    }

    .chat {
        padding: 8px;
        gap: 4px;
    }

    .msg {
        margin: 2px 0;
        padding: 8px 10px;
        border-radius: 12px;
    }

    .msg.user {
        width: 85%;
    }

    .msg.assistant,
    .msg.error {
        width: 90%;
    }

    .msg-content {
        font-size: 13px;
    }

    .composer {
        padding: 6px 8px;
    }

    .input-row {
        gap: 6px;
    }

    textarea#question {
        min-height: 40px;
        max-height: 100px;
        font-size: 14px;
        padding: 8px 10px;
    }

    #send,
    #stop {
        min-height: 40px;
        width: 40px;
        font-size: 16px;
    }

    .route-mode-badge {
        min-height: 40px;
        padding: 0 10px;
        font-size: 11px;
    }

    #send {
        position: relative;
        font-size: 0;
        color: transparent;
    }

    #send::before {
        content: "➤";
        font-size: 18px;
        line-height: 1;
        color: #082f49;
        padding-left: 4px;
    }

    .route-mode-btn {
        padding: 4px 8px;
        font-size: 11px;
        min-height: 20px;
    }

    .disclaimer {
        font-size: 10px;
        line-height: 1.3;
    }
}

@media (max-width: 380px) {
    .header-right {
        gap: 4px;
    }

    .header-username {
        max-width: 40vw;
    }

    .status {
        max-width: 50vw;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .msg {
        padding: 7px 9px;
    }

    .msg-content {
        font-size: 12px;
    }
}

@media (max-height: 620px) and (max-width: 900px) {
    .chat-wrapper {
        gap: 0;
    }

    .header {
        padding: 6px 10px;
    }

    .brand-logo-header {
        height: 20px;
    }

    textarea#question {
        min-height: 40px;
    }

    .route-mode-card {
        padding: 0;
    }
}

/* ── Feedback thumbs ──────────────────────────────────── */
.feedback-bar {
    display: flex;
    gap: 6px;
    margin-top: 8px;
    padding-top: 6px;
    border-top: 1px solid #1e293b;
}

.feedback-btn {
    background: none;
    border: 1px solid transparent;
    border-radius: 6px;
    padding: 6px 10px;
    font-size: 16px;
    cursor: pointer;
    opacity: 0.45;
    transition: all 200ms ease;
    line-height: 1;
    min-height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.feedback-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.feedback-btn:hover {
    opacity: 0.85;
    background: #1e293b;
    border-color: #334155;
}

.feedback-btn.active {
    opacity: 1;
    border-color: #14b8a6;
    background: #064e3b;
}

.feedback-btn:disabled {
    cursor: wait;
}
