:root,
[data-theme="light"] {
    --bg-app: #f7f7f8;
    --bg-sidebar: #171717;
    --bg-sidebar-hover: #2f2f2f;
    --bg-main: #ffffff;
    --bg-input: #ffffff;
    --bg-message-user: #f0f4f8;
    --bg-message-ai: transparent;
    --bg-welcome: #fafafa;
    --text-primary: #0d0d0d;
    --text-secondary: #6e6e80;
    --text-sidebar: #ececec;
    --text-sidebar-muted: #8e8ea0;
    --border: #e5e5e5;
    --accent: #0d4a6f;
    --accent-hover: #1a6b9a;
    --accent-soft: rgba(13, 74, 111, 0.1);
    --success: #2e8b57;
    --danger: #dc3545;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    --radius: 12px;
    --sidebar-width: 280px;
}

[data-theme="dark"] {
    --bg-app: #212121;
    --bg-sidebar: #171717;
    --bg-sidebar-hover: #2f2f2f;
    --bg-main: #212121;
    --bg-input: #2f2f2f;
    --bg-message-user: #303030;
    --bg-message-ai: transparent;
    --bg-welcome: #2a2a2a;
    --text-primary: #ececec;
    --text-secondary: #acacbe;
    --text-sidebar: #ececec;
    --text-sidebar-muted: #8e8ea0;
    --border: #424242;
    --accent: #4da3d9;
    --accent-hover: #6bb8e8;
    --accent-soft: rgba(77, 163, 217, 0.15);
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

/* Không set html/body height:100% — tránh phá layout flex của style.css (#form_admin) */

.chat-app {
    display: flex;
    flex-direction: row;
    align-items: stretch;
    height: 100%;
    min-height: 0;
    max-height: 100%;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
.chat-sidebar {
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    color: var(--text-sidebar);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    min-height: 0;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    transition: transform 0.25s ease;
    /* Thấp hơn .site-header (z-index: 1000) để không đè header khi cuộn trang */
    z-index: 1;
    position: relative;
}

.sidebar-top,
.sidebar-search,
.sidebar-bottom {
    flex-shrink: 0;
}

.sidebar-top {
    padding: 0.85rem;
}

.sidebar-search {
    position: relative;
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    color: var(--text-sidebar);
    text-decoration: none;
    margin-bottom: 0.85rem;
    padding: 0.35rem 0.5rem;
}

.logo-icon {
    width: 32px;
    height: 32px;
    background: var(--accent);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.95rem;
}

.logo-text {
    font-weight: 700;
    font-size: 1.05rem;
}

.btn-new-chat {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.65rem 0.85rem;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 10px;
    color: var(--text-sidebar);
    font-size: 0.875rem;
    cursor: pointer;
    transition: background 0.2s;
}

.btn-new-chat:hover {
    background: var(--bg-sidebar-hover);
}

.sidebar-search {
    padding: 0 0.85rem 0.65rem;
}

.sidebar-search i {
    position: absolute;
    left: 1.35rem;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.8rem;
    color: var(--text-sidebar-muted);
}

.sidebar-search input {
    width: 100%;
    padding: 0.5rem 0.75rem 0.5rem 2rem;
    background: var(--bg-sidebar-hover);
    border: none;
    border-radius: 8px;
    color: var(--text-sidebar);
    font-size: 0.82rem;
}

.sidebar-search input::placeholder {
    color: var(--text-sidebar-muted);
}

.sidebar-search input:focus {
    outline: 1px solid rgba(255, 255, 255, 0.25);
}

.sidebar-history {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 0 0.5rem 0.35rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: stable;
}

.sidebar-history-empty {
    margin: 0;
    padding: 0.5rem 0.85rem 1rem;
    font-size: 0.8rem;
    color: var(--text-sidebar-muted);
}

.history-item-wrap {
    display: flex;
    align-items: stretch;
    gap: 0.15rem;
    border-radius: 8px;
    margin-bottom: 0.1rem;
}

.history-item-wrap:hover,
.history-item-wrap:focus-within {
    background: var(--bg-sidebar-hover);
}

.history-item-wrap:has(.history-item.active) {
    background: var(--bg-sidebar-hover);
}

.history-item-wrap .history-item {
    flex: 1;
    min-width: 0;
}

.history-item-wrap .history-item:hover,
.history-item-wrap .history-item.active {
    background: transparent;
}

.history-item-delete {
    flex-shrink: 0;
    width: 2rem;
    margin: 0.2rem 0.15rem 0.2rem 0;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--text-sidebar-muted);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.78rem;
}

.history-item-wrap:hover .history-item-delete,
.history-item-wrap:focus-within .history-item-delete {
    opacity: 1;
}

.history-item-delete:hover {
    color: #ff8a8a;
    background: rgba(220, 53, 69, 0.2);
}

.history-group-label {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-sidebar-muted);
    padding: 0.75rem 0.5rem 0.35rem;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.55rem 0.65rem;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.82rem;
    color: var(--text-sidebar);
    transition: background 0.15s;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
}

.history-item:hover {
    background: var(--bg-sidebar-hover);
}

.history-item.active {
    background: var(--bg-sidebar-hover);
}

.history-item i {
    opacity: 0.6;
    font-size: 0.75rem;
}

.history-item span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-bottom {
    padding: 0.75rem;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-action {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    width: 100%;
    padding: 0.55rem 0.65rem;
    background: none;
    border: none;
    border-radius: 8px;
    color: var(--text-sidebar);
    font-size: 0.82rem;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}

.sidebar-action:hover {
    background: var(--bg-sidebar-hover);
    color: var(--text-sidebar);
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 998;
}

/* Main: absolute layout — tin nhắn cuộn giữa header và ô nhập */
.chat-main {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    background: var(--bg-main);
}

.chat-header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.65rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-main);
    min-height: 56px;
}

.btn-icon {
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.btn-icon:hover {
    background: var(--accent-soft);
    color: var(--accent);
}

.header-title {
    flex: 1;
    min-width: 0;
}

.header-title h1 {
    font-size: 0.95rem;
    font-weight: 600;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.header-sub {
    font-size: 0.72rem;
    color: var(--text-secondary);
}

.header-actions {
    display: flex;
    gap: 0.25rem;
}

/* Messages — top/bottom do JS đặt --chat-header-height, --chat-input-stack */
.chat-messages {
    position: absolute;
    left: 0;
    right: 0;
    top: var(--chat-header-height, 56px);
    bottom: var(--chat-input-stack, 110px);
    overflow-x: hidden;
    overflow-y: scroll;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
    scrollbar-gutter: stable;
    -webkit-overflow-scrolling: touch;
}

.welcome-screen {
    margin: 0 auto;
    padding: 3rem 1.5rem 2rem;
    text-align: center;
}

.welcome-icon {
    width: 64px;
    height: 64px;
    margin: 0 auto 1.25rem;
    background: var(--accent-soft);
    color: var(--accent);
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.75rem;
}

.welcome-screen h2 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
}

.welcome-screen > p {
    color: var(--text-secondary);
    font-size: 0.95rem;
    margin-bottom: 2rem;
}

.suggestions-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
    text-align: left;
}

.suggestion-card {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.65rem;
    background: var(--bg-welcome);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.suggestion-card:hover {
    border-color: var(--accent);
    box-shadow: var(--shadow);
}

.suggestion-card i {
    color: var(--accent);
    font-size: 1rem;
    flex-shrink: 0;
    line-height: 1;
}

.suggestion-card p {
    margin: 0;
    font-size: 0.85rem;
    line-height: 1.35;
    color: var(--text-primary);
    flex: 1;
    min-width: 0;
}

.followup-suggestions {
    margin-top: 0.85rem;
    padding-top: 0.75rem;
    border-top: 1px dashed var(--border);
}

.followup-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
    font-style: italic;
}

.followup-chips {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
}

.followup-chip {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    text-align: left;
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-welcome);
    color: var(--text-primary);
    font-size: 0.82rem;
    line-height: 1.4;
    cursor: pointer;
    transition: border-color 0.2s, background 0.2s;
}

.followup-chip:hover {
    border-color: var(--accent);
    background: var(--bg-main);
}

.followup-chip i {
    color: var(--accent);
    margin-top: 0.15rem;
    flex-shrink: 0;
}

/* Nhảy tới câu hỏi sau khi bấm gợi ý */
.message-row.user.message-highlight .message-inner {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 12px;
    transition: outline-color 0.3s ease;
}

/* Cuộn trang (scrollIntoView) — tránh bị site-header sticky che */
body.page-chat .message-row.user {
    scroll-margin-top: calc(var(--site-header-stack, 72px) + 12px);
}

.chat-messages .message-row.user {
    scroll-margin-top: 12px;
}

.message-row {
    border-bottom: 1px solid var(--border);
}

.message-row:last-child {
    border-bottom: none;
}

.message-inner {
    max-width: 768px;
    margin: 0 auto;
    padding: 1.25rem 1.5rem;
    display: flex;
    gap: 1rem;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    flex-shrink: 0;
}

.message-row.user .message-avatar {
    background: var(--accent);
    color: #fff;
}

.message-row.assistant .message-avatar {
    background: var(--success);
    color: #fff;
}

.message-body {
    flex: 1;
    min-width: 0;
}

.message-meta {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.35rem;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.message-meta .role-name {
    font-weight: 600;
    color: var(--text-primary);
}

.message-content {
    font-size: 0.9375rem;
    line-height: 1.65;
    word-break: break-word;
}

.message-content:not(.markdown-body) {
    white-space: pre-wrap;
}

.message-content.markdown-body {
    white-space: normal;
}

.message-content.markdown-body h1,
.message-content.markdown-body h2,
.message-content.markdown-body h3 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--accent);
    margin: 1rem 0 0.5rem;
}

.message-content.markdown-body h1:first-child,
.message-content.markdown-body h2:first-child,
.message-content.markdown-body h3:first-child {
    margin-top: 0;
}

/*
 * Bảng chat — ngang native (scroll-x), dọc JS+fling (scroll-y).
 * .chat-table-port > .chat-table-scroll-y > .chat-table-scroll-x > table
 */
.message-content .chat-table-port,
.message-content .chat-table-wrap.chat-table-port {
    width: 100%;
    max-width: 100%;
    margin: 0.75rem 0 1rem;
    position: relative;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-main);
    isolation: isolate;
}

.message-content .chat-table-port::before {
    content: 'Vuốt dọc/ngang trong khung · hàng đầu và cột đầu cố định';
    display: block;
    padding: 0.4rem 0.65rem;
    text-align: center;
    font-size: 0.72rem;
    line-height: 1.35;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border);
    background: var(--bg-welcome, #f4f8fb);
}

.message-content .chat-table-port .chat-table-port::before {
    display: none !important;
}

.message-content .chat-table-scroll-y {
    display: block;
    width: 100%;
    max-height: min(70vh, 520px);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    outline: none;
}

.message-content .chat-table-scroll-y:focus-visible {
    box-shadow: inset 0 0 0 2px var(--accent);
}

.message-content .chat-table-scroll-x {
    display: block;
    width: 100%;
    height: auto;
    min-height: min-content;
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    touch-action: pan-x;
}

.message-content .chat-table-scroll-x table,
.message-content .chat-table-scroll-x table.chat-data-table,
.message-content.markdown-body .chat-table-scroll-x table {
    width: 100% !important;
    max-width: 100%;
    table-layout: fixed;
    border-collapse: separate !important;
    border-spacing: 0;
    margin: 0;
    font-size: 0.875rem;
    display: table;
}

.message-content .chat-table-scroll-y thead tr > th,
.message-content .chat-table-scroll-y thead tr > td,
.message-content .chat-table-scroll-y tr.chat-table-sticky-head > th,
.message-content .chat-table-scroll-y tr.chat-table-sticky-head > td {
    position: sticky;
    top: 0;
    z-index: 12;
    background-color: #e8f1f6 !important;
    background-clip: padding-box;
    font-weight: 600;
    box-shadow: 0 1px 0 var(--border), 0 4px 8px -2px rgba(0, 0, 0, 0.12);
}

/* Bảng 2 hàng tiêu đề: chỉ sticky hàng chi tiết */
.message-content .chat-table-scroll-y .chat-table-dual-head thead tr > th,
.message-content .chat-table-scroll-y .chat-table-dual-head thead tr > td,
.message-content .chat-table-scroll-y .chat-table-dual-head tr.chat-table-group-head > th,
.message-content .chat-table-scroll-y .chat-table-dual-head tr.chat-table-group-head > td {
    position: static;
    z-index: auto;
    box-shadow: none;
}

.message-content .chat-table-scroll-y .chat-table-dual-head tr.chat-table-sticky-head > th,
.message-content .chat-table-scroll-y .chat-table-dual-head tr.chat-table-sticky-head > td {
    position: sticky;
    top: 0;
    z-index: 13;
    background-color: #e8f1f6 !important;
    background-clip: padding-box;
    font-weight: 600;
    box-shadow: 0 1px 0 var(--border), 0 4px 8px -2px rgba(0, 0, 0, 0.12);
}

[data-theme="dark"] .message-content .chat-table-scroll-y thead tr > th,
[data-theme="dark"] .message-content .chat-table-scroll-y thead tr > td,
[data-theme="dark"] .message-content .chat-table-scroll-y tr.chat-table-sticky-head > th,
[data-theme="dark"] .message-content .chat-table-scroll-y tr.chat-table-sticky-head > td {
    background-color: #2d3f4d !important;
}

.message-content .chat-table-scroll-x tbody td,
.message-content .chat-table-scroll-x tbody th {
    background-color: var(--bg-main) !important;
    background-clip: padding-box;
}

.message-content .chat-table-scroll-x tbody tr:nth-child(even) td {
    background-color: #f4f8fb !important;
}

[data-theme="dark"] .message-content .chat-table-scroll-x tbody tr:nth-child(even) td {
    background-color: #2a2f33 !important;
}

/* Thanh tiêu đề nổi — overflow-x phá position:sticky; cuộn dọc trên #chatMessages / .chat-main */
.message-content .chat-table-head-float {
    position: fixed;
    z-index: 26;
    display: block;
    overflow: hidden;
    margin: 0;
    padding: 0;
    border-bottom: 1px solid var(--border);
    background: var(--bg-main);
    box-shadow: 0 4px 10px -2px rgba(0, 0, 0, 0.14);
    pointer-events: none;
    box-sizing: border-box;
}

.message-content .chat-table-head-float-track {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: stretch;
    width: max-content;
    min-width: 100%;
    will-change: transform;
}

.message-content .chat-table-head-float-cell {
    flex: 0 0 auto;
    padding: 0.5rem 0.65rem;
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1.35;
    color: var(--text-primary);
    background-color: #e8f1f6 !important;
    border-right: 1px solid var(--border);
    border-bottom: none;
    box-sizing: border-box;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.message-content .chat-table-head-float-cell:last-child {
    border-right: none;
}

.message-content .chat-table-head-float-cell--first {
    position: sticky;
    left: 0;
    z-index: 2;
    box-shadow: 4px 0 8px -2px rgba(0, 0, 0, 0.12);
}

[data-theme="dark"] .message-content .chat-table-head-float-cell {
    background-color: #2d3f4d !important;
}

.message-content .chat-table-port.chat-table-head-is-stuck thead tr > th,
.message-content .chat-table-port.chat-table-head-is-stuck thead tr > td,
.message-content .chat-table-port.chat-table-head-is-stuck tr.chat-table-sticky-head > th,
.message-content .chat-table-port.chat-table-head-is-stuck tr.chat-table-sticky-head > td {
    visibility: hidden;
}

.message-content table,
.message-content.markdown-body table,
.message-content table.chat-data-table {
    width: 100% !important;
    max-width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    margin: 0;
    font-size: 0.875rem;
    display: table;
}

.message-content table colgroup,
.message-content table col {
    width: auto !important;
}

.message-content th,
.message-content td,
.message-content.markdown-body th,
.message-content.markdown-body td {
    border: 1px solid var(--border);
    padding: 0.5rem 0.65rem;
    text-align: left;
    vertical-align: top;
    width: auto !important;
    min-width: 0;
    word-wrap: break-word;
    overflow-wrap: break-word;
    white-space: normal;
}

.message-content.markdown-body th,
.message-content table.chat-data-table th {
    background: var(--accent-soft);
    font-weight: 600;
}

.message-content.markdown-body tr:nth-child(even) td {
    background: rgba(13, 74, 111, 0.03);
}

[data-theme="dark"] .message-content.markdown-body tr:nth-child(even) td {
    background: rgba(255, 255, 255, 0.03);
}

.message-content.markdown-body blockquote {
    margin: 0.75rem 0;
    padding: 0.65rem 0.85rem;
    border-left: 3px solid var(--accent);
    background: var(--accent-soft);
    border-radius: 0 8px 8px 0;
    color: var(--text-primary);
}

.message-content.markdown-body blockquote p:last-child {
    margin-bottom: 0;
}

.message-content.markdown-body hr {
    border: none;
    border-top: 1px dashed var(--border);
    margin: 1rem 0;
}

.message-content.markdown-body em {
    font-style: italic;
    color: var(--text-secondary);
}

.message-content.markdown-body code {
    font-size: 0.85em;
    background: var(--accent-soft);
    padding: 0.1rem 0.35rem;
    border-radius: 4px;
}

.message-content.markdown-body pre {
    overflow-x: auto;
    padding: 0.75rem;
    background: var(--bg-message-user);
    border-radius: 8px;
    font-size: 0.85rem;
}

.message-content p {
    margin: 0 0 0.75rem;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content ul,
.message-content ol {
    margin: 0.5rem 0 0.75rem;
    padding-left: 1.25rem;
}

.message-content li {
    margin-bottom: 0.35rem;
}

.message-content strong {
    color: var(--accent);
}

.message-row.user {
    background: var(--bg-message-user);
}

.message-row.assistant {
    background: var(--bg-message-ai);
}

/* Citations */
.citations-block {
    margin-top: 1rem;
    padding-top: 0.85rem;
    border-top: 1px dashed var(--border);
}

.citations-label {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.citation-item {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    padding: 0.5rem 0.65rem;
    background: var(--accent-soft);
    border-radius: 8px;
    margin-bottom: 0.4rem;
    font-size: 0.8rem;
    transition: opacity 0.15s;
}

.citation-item:hover {
    opacity: 0.92;
}

.citation-body {
    flex: 1;
    min-width: 0;
}

.citation-head {
    display: flex;
    align-items: flex-start;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.cite-badge {
    font-size: 0.62rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    padding: 0.1rem 0.35rem;
    border-radius: 4px;
    white-space: nowrap;
}

.cite-badge-sotay {
    background: rgba(34, 139, 34, 0.15);
    color: #2d6a2d;
}

.cite-badge-kb {
    background: rgba(13, 110, 253, 0.12);
    color: #0a58ca;
}

.cite-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.35rem;
}

.cite-action {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--accent);
    text-decoration: none;
}

.cite-action:hover {
    text-decoration: underline;
}

.citation-item i {
    color: var(--accent);
    margin-top: 2px;
}

.citation-item .cite-title {
    font-weight: 600;
    display: block;
}

.citation-item .cite-ref {
    color: var(--text-secondary);
    font-size: 0.72rem;
}

/* Typing */
.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 0.25rem 0;
}

.typing-indicator span {
    width: 7px;
    height: 7px;
    background: var(--text-secondary);
    border-radius: 50%;
    animation: typingBounce 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.3s; }

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

.response-time {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.7rem;
    color: var(--text-secondary);
}

.response-time i {
    font-size: 0.65rem;
}

/* Nguồn RAG: DuLieu vs Kho tri thức */
.chat-source-picker {
    max-width: 768px;
    margin: 0 auto 0.65rem;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
}

.chat-source-option {
    margin: 0;
    cursor: pointer;
}

.chat-source-option input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
    pointer-events: none;
}

.chat-source-option__box {
    display: flex;
    align-items: flex-start;
    gap: 0.55rem;
    padding: 0.55rem 0.65rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-input);
    transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
    min-height: 100%;
}

.chat-source-option__box i {
    margin-top: 0.15rem;
    font-size: 1rem;
    color: var(--accent);
    flex-shrink: 0;
}

.chat-source-option__text {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    min-width: 0;
}

.chat-source-option__title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.25;
}

.chat-source-option__hint {
    font-size: 0.68rem;
    color: var(--text-secondary);
    line-height: 1.3;
}

.chat-source-option input:checked + .chat-source-option__box {
    border-color: var(--accent);
    background: var(--accent-soft);
    box-shadow: 0 0 0 1px var(--accent);
}

.chat-source-option input:focus-visible + .chat-source-option__box {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.chat-source-picker.is-disabled {
    opacity: 0.65;
    pointer-events: none;
}

@media (max-width: 480px) {
    body.page-chat .chat-source-picker {
        grid-template-columns: 1fr;
    }
}

/* Input */
.chat-input-area {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 6;
    padding: 0.75rem 1rem 1rem;
    border-top: 1px solid var(--border);
    background: var(--bg-main);
    box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.06);
}

.attached-files {
    max-width: 768px;
    margin: 0 auto 0.5rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.file-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.35rem 0.65rem;
    background: var(--accent-soft);
    border: 1px solid var(--border);
    border-radius: 20px;
    font-size: 0.78rem;
    color: var(--text-primary);
}

.file-chip .remove-file {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

.file-chip .remove-file:hover {
    color: var(--danger);
}

.input-wrapper {
    max-width: 768px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 0.5rem 0.65rem;
    box-shadow: var(--shadow);
    transition: border-color 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--accent);
}

.btn-attach,
.btn-send {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s, opacity 0.15s;
}

.btn-attach {
    background: transparent;
    color: var(--text-secondary);
}

.btn-attach:hover {
    background: var(--accent-soft);
    color: var(--accent);
}

.btn-send {
    background: var(--accent);
    color: #fff;
}

.btn-send:hover:not(:disabled) {
    background: var(--accent-hover);
}

.btn-send:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

#messageInput {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 0.9375rem;
    line-height: 1.5;
    resize: none;
    max-height: 200px;
    padding: 0.4rem 0;
}

#messageInput:focus {
    outline: none;
}

#messageInput::placeholder {
    color: var(--text-secondary);
}

.input-hint {
    max-width: 768px;
    margin: 0.45rem auto 0;
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-align: center;
}

/*
 * Trang Chat: form cố định 100dvh, main flex:1 (không dùng flex-basis:0).
 * Không hiển thị footer site — toàn bộ chiều cao cho vùng chat.
 */
body.page-chat {
    --site-header-stack: 72px;
    --site-footer-stack: 0;
    font-family: "Be Vietnam Pro", "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg-app);
    color: var(--text-primary);
    overflow: hidden;
}

body.page-chat #form_admin {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    min-height: 100dvh;
    overflow: hidden;
}

body.page-chat .site-header {
    flex: 0 0 auto;
}

body.page-chat .main-content {
    flex: 1 1 auto;
    min-height: 280px;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

body.page-chat .main-content > .container-fluid {
    flex: 1 1 auto;
    min-height: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 0 !important;
    margin: 0;
    overflow: hidden;
}

body.page-chat .main-content > .container-fluid > link {
    display: none;
}

body.page-chat .chat-page-shell {
    flex: 1 1 auto;
    min-height: 280px;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

body.page-chat .chat-app--embedded,
body.page-chat .chat-page-shell .chat-app,
body.page-chat .chat-page-shell [id$="chatApp"] {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: row;
    align-items: stretch;
}

body.page-chat .chat-app--embedded .chat-sidebar {
    height: 100%;
    max-height: 100%;
    min-height: 0;
}

body.page-chat .chat-app--embedded .sidebar-history {
    overflow-y: scroll !important;
}

body.page-chat .chat-app--embedded .chat-main {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    height: 100%;
    max-height: 100%;
}

body.page-chat .chat-app--embedded .chat-messages {
    overflow-y: scroll !important;
    overflow-x: hidden;
}

body.page-chat .chat-app--embedded .chat-input-area {
    z-index: 4;
}

body.page-chat .breadcrumb-bar {
    display: none !important;
}

/* Desktop: sidebar full chiều cao vùng chat (flex), lịch sử cuộn trong sidebar */
@media (min-width: 768px) {
    body.page-chat .chat-app--embedded .chat-sidebar {
        align-self: stretch;
        height: 100%;
        max-height: 100%;
        overflow: hidden;
    }

    body.page-chat .chat-app--embedded .chat-main {
        flex: 1 1 auto;
        min-height: 0;
        min-width: 0;
        height: 100%;
        max-height: 100%;
    }

    .history-item-delete {
        opacity: 0.55;
    }

    .history-item-wrap:hover .history-item-delete,
    .history-item-wrap:focus-within .history-item-delete {
        opacity: 1;
    }

    /* Header chat gọn — tiêu đề + meta một hàng */
    body.page-chat .chat-header {
        min-height: 0;
        padding: 0.4rem 0.85rem;
        gap: 0.45rem;
    }

    body.page-chat .chat-header .btn-icon {
        width: 32px;
        height: 32px;
        flex-shrink: 0;
    }

    body.page-chat .header-title {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 0.45rem;
        line-height: 1.2;
    }

    body.page-chat .header-title h1 {
        font-size: 0.82rem;
        font-weight: 600;
        flex-shrink: 0;
        max-width: 42%;
    }

    body.page-chat .header-sub {
        font-size: 0.7rem;
        min-width: 0;
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    body.page-chat .header-sub::before {
        content: '·';
        margin-right: 0.35rem;
        color: var(--text-secondary);
        opacity: 0.85;
    }

    /* Vùng nhập gọn — ưu tiên không gian cho tin nhắn */
    body.page-chat .chat-input-area {
        padding: 0.45rem 1rem 0.5rem;
    }

    body.page-chat .attached-files {
        margin-bottom: 0.35rem;
        gap: 0.35rem;
    }

    body.page-chat .chat-source-picker {
        margin-bottom: 0.35rem;
        gap: 0.35rem;
    }

    body.page-chat .chat-source-option__box {
        align-items: center;
        gap: 0.4rem;
        padding: 0.3rem 0.55rem;
        border-radius: 8px;
    }

    body.page-chat .chat-source-option__box i {
        margin-top: 0;
        font-size: 0.8rem;
    }

    body.page-chat .chat-source-option__text {
        flex-direction: row;
        align-items: center;
        gap: 0.35rem;
    }

    body.page-chat .chat-source-option__title {
        font-size: 0.75rem;
        line-height: 1.2;
        white-space: nowrap;
    }

    body.page-chat .chat-source-option__hint {
        display: none;
    }

    body.page-chat .input-wrapper {
        padding: 0.3rem 0.45rem;
        border-radius: 12px;
        gap: 0.4rem;
    }

    body.page-chat .btn-send {
        width: 32px;
        height: 32px;
        border-radius: 8px;
    }

    body.page-chat #messageInput {
        font-size: 0.875rem;
        line-height: 1.35;
        padding: 0.2rem 0;
        min-height: 22px;
    }

    body.page-chat .input-hint {
        margin-top: 0.2rem;
        font-size: 0.65rem;
        line-height: 1.2;
    }
}

@media (max-width: 767.98px) {
    .chat-sidebar {
        position: fixed;
        top: var(--site-header-stack, 72px);
        left: 0;
        bottom: var(--site-footer-stack, 0px);
        height: auto;
        max-height: none;
        transform: translateX(-100%);
        z-index: 999;
    }

    .chat-app--embedded .sidebar-overlay {
        top: var(--site-header-stack, 72px);
        bottom: var(--site-footer-stack, 0px);
    }

    .chat-sidebar.open {
        transform: translateX(0);
    }

    .chat-sidebar.open .sidebar-history {
        min-height: 0;
    }

    .history-item-delete {
        opacity: 0.9;
    }

    .sidebar-overlay.show {
        display: block;
    }

    .suggestions-grid {
        grid-template-columns: 1fr;
    }

    .message-inner {
        padding: 1rem;
    }

    .welcome-screen {
        padding: 2rem 1rem;
    }

    /*
     * Mobile: cuộn native trên .chat-main — #chatMessages không còn overflow/height cố định.
     * Bảng chỉ cuộn ngang trong scroll-x; vuốt dọc do trình duyệt (có quán tính).
     */
    body.page-chat .chat-app--embedded,
    body.page-chat .chat-page-shell .chat-app,
    body.page-chat .chat-page-shell [id$="chatApp"] {
        overflow: visible;
        max-height: none;
    }

    body.page-chat .chat-main {
        display: block;
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: auto;
        touch-action: pan-y;
    }

    body.page-chat .chat-app--embedded .chat-messages {
        overflow: visible !important;
        overflow-y: visible !important;
        height: auto !important;
        max-height: none !important;
    }

    body.page-chat .chat-header {
        position: sticky;
        top: 0;
        z-index: 5;
    }

    body.page-chat .chat-messages,
    body.page-chat .chat-app--embedded .chat-messages {
        position: static !important;
        top: auto !important;
        bottom: auto !important;
        display: block;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
        overscroll-behavior: auto;
    }

    body.page-chat .chat-input-area {
        position: sticky;
        bottom: 0;
        z-index: 6;
        padding: 0.45rem 0.65rem 0.55rem;
        overflow-x: hidden;
        max-width: 100%;
        box-sizing: border-box;
    }

    body.page-chat .chat-source-picker {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        margin-bottom: 0.35rem;
        gap: 0.35rem;
        max-width: 100%;
    }

    body.page-chat .chat-source-option {
        min-width: 0;
        overflow: hidden;
    }

    body.page-chat .chat-source-option__box {
        align-items: center;
        gap: 0.35rem;
        padding: 0.32rem 0.45rem;
        border-radius: 8px;
        min-height: 0;
        min-width: 0;
        overflow: hidden;
        width: 100%;
        box-sizing: border-box;
    }

    body.page-chat .chat-source-option__box i {
        margin-top: 0;
        font-size: 0.82rem;
        flex-shrink: 0;
    }

    body.page-chat .chat-source-option__text {
        flex: 1 1 auto;
        flex-direction: row;
        align-items: center;
        gap: 0.28rem;
        flex-wrap: nowrap;
        overflow: hidden;
        min-width: 0;
    }

    body.page-chat .chat-source-option__title {
        font-size: 0.72rem;
        line-height: 1.2;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        min-width: 0;
        flex: 1 1 auto;
    }

    body.page-chat .chat-source-option__hint {
        display: none;
    }

    body.page-chat .input-wrapper {
        padding: 0.35rem 0.5rem;
        border-radius: 12px;
        gap: 0.35rem;
    }

    body.page-chat .btn-send {
        width: 32px;
        height: 32px;
        border-radius: 8px;
    }

    body.page-chat #messageInput {
        font-size: 0.875rem;
        line-height: 1.35;
        padding: 0.15rem 0;
        min-height: 22px;
    }

    body.page-chat .input-hint {
        margin-top: 0.2rem;
        font-size: 0.62rem;
        line-height: 1.2;
    }

    .message-content,
    .message-content .chat-table-port,
    .message-content .chat-table-scroll-y,
    .message-content .chat-table-scroll-x {
        touch-action: auto !important;
        overscroll-behavior-y: auto;
    }

    .message-content .chat-table-scroll-y {
        max-height: none;
        overflow-y: visible;
        overflow-x: hidden;
        overscroll-behavior-y: auto;
    }

    .message-content .chat-table-scroll-x {
        overscroll-behavior-x: contain;
        overscroll-behavior-y: auto;
    }

    .message-content .chat-table-port::before {
        content: 'Vuốt ngang trong khung · vuốt dọc cuộn tin nhắn · hàng đầu cố định';
    }

    .message-content .chat-table-scroll-x table,
    .message-content .chat-table-scroll-x table.chat-data-table,
    .message-content.markdown-body .chat-table-scroll-x table {
        width: max-content !important;
        min-width: 100%;
        max-width: none;
        table-layout: auto !important;
    }

    .message-content .chat-table-scroll-x th,
    .message-content .chat-table-scroll-x td,
    .message-content.markdown-body .chat-table-scroll-x th,
    .message-content.markdown-body .chat-table-scroll-x td {
        white-space: nowrap;
        word-wrap: normal;
        overflow-wrap: normal;
        min-width: max-content;
    }

    .message-content .chat-table-scroll-x th.cell-wrap,
    .message-content .chat-table-scroll-x td.cell-wrap {
        white-space: normal;
        word-wrap: break-word;
        overflow-wrap: break-word;
        min-width: 0;
        max-width: calc(100vw - 2.5rem);
    }

    .message-content .chat-table-scroll-x thead tr > th:first-child,
    .message-content .chat-table-scroll-x thead tr > td:first-child,
    .message-content .chat-table-scroll-x tr.chat-table-sticky-head > th:first-child,
    .message-content .chat-table-scroll-x tr.chat-table-sticky-head > td:first-child {
        left: 0;
        z-index: 14;
    }

    .message-content .chat-table-scroll-x tbody tr > td:first-child,
    .message-content .chat-table-scroll-x tbody tr > th:first-child {
        position: sticky;
        left: 0;
        z-index: 2;
        min-width: 3.25rem;
        background-color: var(--bg-main) !important;
        background-clip: padding-box;
        box-shadow: 4px 0 8px -2px rgba(0, 0, 0, 0.14);
    }

    .message-content .chat-table-scroll-x tbody tr:nth-child(even) > td:first-child {
        background-color: #f4f8fb !important;
    }

    [data-theme="dark"] .message-content .chat-table-scroll-x tbody tr:nth-child(even) > td:first-child {
        background-color: #2a2f33 !important;
    }
}

@media (min-width: 768px) {
    #btnToggleSidebar {
        display: none;
    }

    .message-content .chat-table-port::before {
        display: none;
    }

    /* Desktop: cuộn dọc chỉ trên #chatMessages — không khối scroll-y lồng */
    .message-content .chat-table-scroll-y {
        max-height: none;
        overflow-y: visible;
        overflow-x: hidden;
        overscroll-behavior-y: auto;
    }
}

/* Scrollbar — dễ thấy để cuộn tới nội dung dài / bảng / lịch sử */
.chat-messages,
body.page-chat .chat-main,
.sidebar-history {
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.35) transparent;
}

.chat-messages::-webkit-scrollbar,
.sidebar-history::-webkit-scrollbar {
    width: 8px;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

.sidebar-history::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.25);
    border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

.sidebar-history::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.45);
}

/* Debug bảng chat: ?chatTableDebug=1 hoặc localStorage sotay_chat_table_debug=1 */
#chatTableDebugPanel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 99999;
    max-height: 38vh;
    overflow: auto;
    margin: 0;
    padding: 0.5rem 0.65rem;
    font: 11px/1.4 ui-monospace, Consolas, monospace;
    color: #e8f4fc;
    background: rgba(12, 22, 32, 0.94);
    border-top: 2px solid #3d8bfd;
    pointer-events: none;
}

#chatTableDebugPanel .dbg-title {
    font-weight: 700;
    color: #7ec8ff;
    margin-bottom: 0.25rem;
}

#chatTableDebugPanel .dbg-line {
    margin: 0.1rem 0;
    word-break: break-all;
}
