/* ==========================================================================
   Trợ Lý Ngữ Văn AI — Học Văn Cùng Ricky
   Phong cách "Giấy & Mực" (Light) & "Đêm Ôn Thi" (Dark)
   ========================================================================== */

:root {
    --bg-app: #FAF9F6;
    --bg-sidebar: #F2EFE9;
    --bg-surface: #FFFFFF;
    --bg-surface-hover: #F5F2EC;
    --bg-elevated: #FFFFFF;
    --bg-user-bubble: #E8F2F0;
    --border-user-bubble: #CFE3DF;
    --bg-quote: #FBF6F1;
    --bg-code: #F1EFEA;
    --bg-chip: #F3F1EC;
    --bg-chip-active: #2C5F5D;

    --text-primary: #1B2A29;
    --text-secondary: #556866;
    --text-muted: #849492;
    --text-on-brand: #FFFFFF;

    --brand: #2C5F5D;
    --brand-hover: #214947;
    --brand-soft: rgba(44, 95, 93, 0.10);
    --brand-ring: rgba(44, 95, 93, 0.22);
    --accent: #E07A5F;
    --accent-soft: rgba(224, 122, 95, 0.12);
    --cream: #F7D1B4;

    --border-subtle: #E6E2D9;
    --border-strong: #D5CFC3;
    --shadow-sm: 0 1px 3px rgba(27, 42, 41, 0.05);
    --shadow-md: 0 6px 24px rgba(27, 42, 41, 0.08);
    --shadow-lg: 0 14px 40px rgba(27, 42, 41, 0.12);

    --danger: #C94A4A;
    --warning: #D9822B;
    --success: #2A8C6A;

    --sidebar-width: 284px;
    --content-max: 790px;
    --radius-sm: 10px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --font-sans: 'Be Vietnam Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-serif: 'Playfair Display', Georgia, serif;
}

[data-theme="dark"] {
    --bg-app: #0F1618;
    --bg-sidebar: #141E21;
    --bg-surface: #1A262A;
    --bg-surface-hover: #223136;
    --bg-elevated: #1E2C31;
    --bg-user-bubble: #1F393A;
    --border-user-bubble: #2D5152;
    --bg-quote: #182327;
    --bg-code: #121B1E;
    --bg-chip: #1B282C;
    --bg-chip-active: #5CA19B;

    --text-primary: #E9F0F0;
    --text-secondary: #A2B5B3;
    --text-muted: #708583;
    --text-on-brand: #0B1515;

    --brand: #68A39E;
    --brand-hover: #7BB5B0;
    --brand-soft: rgba(104, 163, 158, 0.16);
    --brand-ring: rgba(104, 163, 158, 0.30);
    --accent: #E8927A;
    --accent-soft: rgba(232, 146, 122, 0.16);

    --border-subtle: #243439;
    --border-strong: #31464D;
    --shadow-sm: 0 1px 4px rgba(0, 0, 0, 0.28);
    --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.36);
    --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.48);
}

*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    font-family: var(--font-sans);
    background: var(--bg-app);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    overflow: hidden;
    overscroll-behavior: none;
}

button, input, textarea {
    font-family: inherit;
    color: inherit;
    touch-action: manipulation;
}

a {
    color: inherit;
    text-decoration: none;
    touch-action: manipulation;
}

[hidden] {
    display: none !important;
}

/* ==================== APP LAYOUT ==================== */

.ai-app {
    display: flex;
    position: fixed;
    top: var(--app-top, 0px);
    left: 0;
    right: 0;
    height: var(--app-height, 100dvh);
    width: 100%;
    overflow: hidden;
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(12, 22, 23, 0.45);
    backdrop-filter: blur(2px);
    z-index: 40;
}

/* ==================== SIDEBAR ==================== */

.ai-sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    transition: margin-left 0.24s cubic-bezier(0.22, 1, 0.36, 1), transform 0.24s cubic-bezier(0.22, 1, 0.36, 1);
    z-index: 50;
}

.ai-app.sidebar-collapsed .ai-sidebar {
    margin-left: calc(var(--sidebar-width) * -1);
}

.sidebar-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 14px 12px;
    gap: 8px;
}

.brand-link {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.brand-mark {
    position: relative;
    width: 38px;
    height: 38px;
    border-radius: 12px;
    background: linear-gradient(145deg, #173D3C 0%, #2C5F5D 55%, #4A8B86 100%);
    color: #FFFFFF;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 4px 12px rgba(44, 95, 93, 0.28), inset 0 1px 1px rgba(255, 255, 255, 0.25);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.brand-link:hover .brand-mark {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(44, 95, 93, 0.36);
}

.brand-mark .mini-bot-eye,
.ai-avatar .mini-bot-eye {
    transform-box: fill-box;
    transform-origin: center;
    animation: miniBotBlink 4.6s ease-in-out infinite;
}

.brand-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.brand-text strong {
    font-size: 14.5px;
    font-weight: 700;
    letter-spacing: -0.01em;
    line-height: 1.2;
}

.brand-text small {
    font-size: 11.5px;
    color: var(--text-muted);
    margin-top: 2px;
}

.sidebar-close-mobile {
    display: none;
}

.sidebar-actions {
    padding: 4px 14px 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.new-chat-btn {
    width: 100%;
    height: 42px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--brand);
    color: var(--text-on-brand);
    font-size: 13.5px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(44, 95, 93, 0.18);
    transition: background 0.16s ease, transform 0.12s ease;
}

.new-chat-btn:hover {
    background: var(--brand-hover);
}

.new-chat-btn:active {
    transform: scale(0.99);
}

.sidebar-search {
    position: relative;
    display: flex;
    align-items: center;
}

.sidebar-search svg {
    position: absolute;
    left: 11px;
    color: var(--text-muted);
    pointer-events: none;
}

.sidebar-search input {
    width: 100%;
    height: 36px;
    padding: 0 12px 0 34px;
    border-radius: 9px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface);
    font-size: 13px;
    outline: none;
    transition: border-color 0.16s ease, box-shadow 0.16s ease;
}

.sidebar-search input:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-ring);
}

/* Conversation List */

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 10px 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

.conv-group-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    padding: 12px 10px 6px;
}

.conv-empty {
    text-align: center;
    padding: 28px 14px;
    font-size: 12.5px;
    color: var(--text-muted);
    line-height: 1.5;
}

.conv-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 9px 10px;
    border-radius: 10px;
    cursor: pointer;
    position: relative;
    transition: background 0.14s ease;
    margin-bottom: 2px;
}

.conv-item:hover {
    background: var(--bg-surface-hover);
}

.conv-item.active {
    background: var(--bg-surface);
    box-shadow: var(--shadow-sm);
    font-weight: 600;
}

.conv-item.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 8px;
    bottom: 8px;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--brand);
}

.conv-pin-dot {
    color: var(--accent);
    flex-shrink: 0;
    display: inline-flex;
}

.conv-title {
    flex: 1;
    font-size: 13.2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-primary);
}

.conv-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    opacity: 0;
    transition: opacity 0.14s ease;
}

.conv-item:hover .conv-actions,
.conv-item.active .conv-actions {
    opacity: 1;
}

.conv-action-btn {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.14s ease, color 0.14s ease;
}

.conv-action-btn:hover {
    background: var(--brand-soft);
    color: var(--brand);
}

.conv-action-btn.delete:hover {
    background: rgba(201, 74, 74, 0.12);
    color: var(--danger);
}

/* Sidebar Footer */

.sidebar-footer {
    padding: 12px 14px 14px;
    border-top: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.quota-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: 12px;
    padding: 10px 12px;
}

.quota-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    margin-bottom: 7px;
}

.quota-label {
    color: var(--text-secondary);
    font-weight: 500;
}

.quota-count {
    font-weight: 700;
    color: var(--brand);
}

.quota-card.low .quota-count {
    color: var(--warning);
}

.quota-card.empty .quota-count {
    color: var(--danger);
}

.quota-bar {
    height: 6px;
    border-radius: 99px;
    background: var(--bg-chip);
    overflow: hidden;
}

.quota-bar-fill {
    height: 100%;
    border-radius: 99px;
    background: linear-gradient(90deg, var(--brand), #68A39E);
    transition: width 0.35s ease;
}

.quota-card.low .quota-bar-fill {
    background: linear-gradient(90deg, #E09F3E, #E07A5F);
}

.quota-card.empty .quota-bar-fill {
    background: var(--danger);
}

.quota-sub {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 6px;
}

.user-chip,
.login-chip {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 10px;
    transition: background 0.15s ease;
}

.user-chip:hover {
    background: var(--bg-surface-hover);
}

.login-chip {
    justify-content: center;
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    font-size: 12.8px;
    font-weight: 600;
    color: var(--brand);
}

.login-chip:hover {
    border-color: var(--brand);
    background: var(--brand-soft);
}

.user-chip-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--brand-soft);
    color: var(--brand);
    font-weight: 700;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
}

.user-chip-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.user-chip-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.user-chip-info strong {
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-chip-info small {
    font-size: 11px;
    color: var(--text-muted);
}

/* ==================== MAIN AREA ==================== */

.ai-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100%;
    position: relative;
}

/* Topbar */

.ai-topbar {
    height: 58px;
    min-height: 58px;
    padding: 0 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-subtle);
    background: color-mix(in srgb, var(--bg-app) 88%, transparent);
    backdrop-filter: blur(10px);
    z-index: 20;
}

.topbar-left,
.topbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-secondary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

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

[data-theme="light"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: none; }

/* Topbar Brand Title */

.topbar-title {
    font-size: 14.5px;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

/* Model Picker */

.model-picker {
    position: relative;
}

.model-picker-trigger {
    height: 36px;
    padding: 0 12px;
    border-radius: 10px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface);
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.model-picker-trigger:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-sm);
}

.model-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-soft);
}

.model-dropdown {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    width: 300px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: 14px;
    box-shadow: var(--shadow-lg);
    padding: 6px;
    display: none;
    z-index: 60;
}

.model-picker.open .model-dropdown {
    display: block;
}

.model-option {
    width: 100%;
    text-align: left;
    border: none;
    background: transparent;
    padding: 10px 11px;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 3px;
    transition: background 0.14s ease;
}

.model-option:hover:not(:disabled) {
    background: var(--bg-surface-hover);
}

.model-option.selected {
    background: var(--brand-soft);
}

.model-option:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.model-option-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.model-option-name {
    font-size: 13.5px;
    font-weight: 600;
    color: var(--text-primary);
}

.model-provider-tag {
    font-size: 10.5px;
    font-weight: 600;
    text-transform: uppercase;
    padding: 2px 7px;
    border-radius: 99px;
    background: var(--bg-chip);
    color: var(--text-secondary);
}

.model-provider-tag.vip {
    background: var(--accent-soft);
    color: var(--accent);
}

.model-option-desc {
    font-size: 12px;
    color: var(--text-muted);
    line-height: 1.4;
}

.topbar-quota-pill {
    font-size: 12.2px;
    color: var(--text-secondary);
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    padding: 6px 11px;
    border-radius: 99px;
    white-space: nowrap;
}

.topbar-quota-pill strong {
    color: var(--brand);
}

.topbar-quota-pill.empty strong {
    color: var(--danger);
}

.home-pill-btn {
    height: 36px;
    padding: 0 12px;
    border-radius: 10px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface);
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: border-color 0.15s ease, color 0.15s ease;
}

.home-pill-btn:hover {
    border-color: var(--brand);
    color: var(--brand);
}

/* ==================== CHAT SCROLL AREA ==================== */

.chat-scroll {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 24px 20px 32px;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
    outline: none;
}

.chat-scroll::-webkit-scrollbar {
    width: 6px;
}

.chat-scroll::-webkit-scrollbar-track {
    background: transparent;
}

.chat-scroll::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 99px;
}

/* Welcome Screen */

.welcome-screen {
    max-width: var(--content-max);
    margin: 4vh auto 0;
    text-align: center;
    padding: 12px 4px 24px;
    animation: fadeUp 0.35s ease;
}

.welcome-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 6px 15px;
    border-radius: 99px;
    background: var(--brand-soft);
    border: 1px solid color-mix(in srgb, var(--brand) 18%, transparent);
    color: var(--brand);
    font-size: 12.4px;
    font-weight: 600;
    letter-spacing: -0.005em;
    margin-bottom: 18px;
}

.badge-spark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--accent);
    flex-shrink: 0;
    animation: aiSparkGentle 3.2s ease-in-out infinite;
}

.spark-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent);
}

.welcome-title {
    font-family: var(--font-serif);
    font-size: clamp(26px, 3.6vw, 36px);
    font-weight: 700;
    line-height: 1.25;
    margin: 0 0 12px;
    color: var(--text-primary);
    letter-spacing: -0.015em;
}

.welcome-subtitle {
    max-width: 560px;
    margin: 0 auto 30px;
    font-size: 14.8px;
    line-height: 1.65;
    color: var(--text-secondary);
}

.prompt-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    text-align: left;
}

.prompt-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 15px 16px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 7px;
    transition: transform 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
    text-align: left;
}

.prompt-card:hover {
    transform: translateY(-2px);
    border-color: var(--brand);
    box-shadow: var(--shadow-md);
}

.prompt-card-badge {
    font-size: 11px;
    font-weight: 600;
    color: var(--accent);
    letter-spacing: 0.02em;
}

.prompt-card-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.45;
}

/* Messages Thread */

.messages-container {
    max-width: var(--content-max);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 24px;
    padding-bottom: 12px;
}

.msg-row {
    display: flex;
    width: 100%;
    animation: fadeUp 0.22s ease;
}

.msg-row.user {
    justify-content: flex-end;
}

.msg-row.assistant {
    justify-content: flex-start;
    gap: 14px;
    align-items: flex-start;
}

.ai-msg-header {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.ai-sender-name,
.ai-status-pill {
    display: none;
}

.user-bubble {
    max-width: min(82%, 620px);
    background: var(--bg-user-bubble);
    border: 1px solid var(--border-user-bubble);
    color: var(--text-primary);
    padding: 13px 17px;
    border-radius: 18px 18px 4px 18px;
    font-size: 14.8px;
    line-height: 1.65;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    word-break: break-word;
}

.ai-avatar {
    position: relative;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: linear-gradient(145deg, #173D3C 0%, #2C5F5D 55%, #4A8B86 100%);
    color: #FFFFFF;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-top: 2px;
    box-shadow: 0 3px 10px rgba(44, 95, 93, 0.24), inset 0 1px 1px rgba(255, 255, 255, 0.24);
}

.msg-row.assistant.is-streaming .ai-avatar::before {
    content: '';
    position: absolute;
    inset: -3px;
    border-radius: 50%;
    padding: 2px;
    background: conic-gradient(from 0deg, #5EEAD4, #38BDF8, #818CF8, #E07A5F, #F7D1B4, #5EEAD4);
    -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    animation: aiAvatarSpin 2.2s linear infinite;
    pointer-events: none;
}

.msg-row.assistant.is-streaming .ai-avatar .mini-bot-eye {
    animation: miniBotBlink 1.3s ease-in-out infinite;
}

.ai-body {
    flex: 1;
    min-width: 0;
    max-width: 100%;
}

/* Markdown Literary Typography */

.ai-markdown {
    font-size: 15px;
    line-height: 1.78;
    color: var(--text-primary);
    overflow-wrap: break-word;
    word-break: break-word;
    max-width: 100%;
}

.ai-markdown img {
    max-width: 100%;
    height: auto;
    border-radius: 10px;
}

.ai-markdown > *:first-child { margin-top: 0; }
.ai-markdown > *:last-child { margin-bottom: 0; }

.ai-markdown h1, .ai-markdown h2, .ai-markdown h3, .ai-markdown h4 {
    margin: 18px 0 8px;
    font-weight: 700;
    line-height: 1.35;
    color: var(--text-primary);
}

.ai-markdown h1 { font-size: 1.25em; }
.ai-markdown h2 { font-size: 1.15em; color: var(--brand); }
.ai-markdown h3 { font-size: 1.04em; }

.ai-markdown p {
    margin: 0 0 11px;
}

.ai-markdown ul, .ai-markdown ol {
    margin: 0 0 12px;
    padding-left: 22px;
}

.ai-markdown li {
    margin-bottom: 5px;
}

.ai-markdown strong {
    font-weight: 700;
    color: var(--text-primary);
}

.ai-markdown blockquote {
    margin: 12px 0;
    padding: 11px 16px;
    border-left: 3.5px solid var(--accent);
    background: var(--bg-quote);
    border-radius: 0 10px 10px 0;
    font-style: italic;
    color: var(--text-primary);
}

.ai-markdown blockquote p:last-child {
    margin-bottom: 0;
}

.ai-markdown code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9em;
    background: var(--bg-code);
    padding: 2px 6px;
    border-radius: 5px;
}

.ai-markdown pre {
    background: var(--bg-code);
    padding: 12px 14px;
    border-radius: 10px;
    overflow-x: auto;
    border: 1px solid var(--border-subtle);
}

.ai-markdown pre code {
    background: transparent;
    padding: 0;
}

.ai-table-wrap {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 14px 0;
    border-radius: 10px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface);
}

.ai-markdown table {
    width: 100%;
    border-collapse: collapse;
    margin: 14px 0;
    font-size: 13.8px;
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid var(--border-subtle);
}

.ai-table-wrap table {
    margin: 0;
    border: none;
    border-radius: 0;
    min-width: 100%;
}

.ai-markdown th, .ai-markdown td {
    padding: 9px 12px;
    border: 1px solid var(--border-subtle);
    text-align: left;
    word-break: normal;
    overflow-wrap: break-word;
    vertical-align: top;
}

.ai-markdown th {
    background: var(--bg-sidebar);
    font-weight: 600;
    white-space: nowrap;
}

.ai-markdown hr {
    border: none;
    border-top: 1px solid var(--border-subtle);
    margin: 16px 0;
}

/* Streaming indicator & cursor */

.typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 10px 2px;
}

.typing-dots span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--brand);
    opacity: 0.35;
    animation: pulseDot 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

.ai-markdown.streaming::after {
    content: '';
    display: inline-block;
    width: 2.5px;
    height: 1.05em;
    margin-left: 3px;
    vertical-align: text-bottom;
    background: var(--accent);
    animation: blinkCursor 0.85s steps(1) infinite;
}

/* Citation Cards (Internal Blogs) */

.citations-box {
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px dashed var(--border-subtle);
}

.citations-label {
    font-size: 11.8px;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.citations-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 1fr));
    gap: 8px;
}

.citation-card {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    transition: border-color 0.15s ease, transform 0.14s ease;
}

.citation-card:hover {
    border-color: var(--brand);
    transform: translateY(-1px);
}

.citation-cat {
    font-size: 10.5px;
    font-weight: 600;
    color: var(--brand);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.citation-title {
    font-size: 12.8px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.38;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Message Action Bar */

.msg-toolbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 10px;
}

.msg-feedback-group {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.msg-tool-btn {
    height: 30px;
    padding: 0 9px;
    border-radius: 8px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    transition: background 0.14s ease, color 0.14s ease;
}

.msg-tool-btn:hover {
    background: var(--bg-surface-hover);
    color: var(--text-primary);
}

.msg-tool-btn.active-like {
    color: var(--success);
    background: rgba(42, 140, 106, 0.10);
}

.msg-tool-btn.active-dislike {
    color: var(--danger);
    background: rgba(201, 74, 74, 0.10);
}

.msg-meta-tag {
    margin-left: auto;
    font-size: 11.2px;
    color: var(--text-muted);
}

.msg-error-box {
    padding: 11px 14px;
    border-radius: 12px;
    background: rgba(201, 74, 74, 0.09);
    border: 1px solid rgba(201, 74, 74, 0.25);
    color: var(--danger);
    font-size: 13.5px;
    line-height: 1.5;
}

/* Scroll Bottom Button */

.scroll-bottom-btn {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    top: -46px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--border-subtle);
    background: var(--bg-elevated);
    color: var(--text-secondary);
    box-shadow: var(--shadow-md);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 25;
    transition: transform 0.15s ease, border-color 0.15s ease;
}

.scroll-bottom-btn:hover {
    border-color: var(--brand);
    color: var(--brand);
    transform: translateX(-50%) translateY(-2px);
}

/* ==================== COMPOSER DOCK ==================== */

.composer-dock {
    position: relative;
    padding: 6px 20px 14px;
    background: linear-gradient(180deg, transparent 0%, var(--bg-app) 24%);
    z-index: 18;
}

.composer-inner {
    max-width: var(--content-max);
    margin: 0 auto;
}

.quota-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    margin-bottom: 10px;
    border-radius: 12px;
    background: var(--accent-soft);
    border: 1px solid rgba(224, 122, 95, 0.30);
    font-size: 13px;
    color: var(--text-primary);
}

.quota-banner-cta {
    padding: 6px 12px;
    border-radius: 8px;
    background: var(--accent);
    color: #FFFFFF;
    font-size: 12.2px;
    font-weight: 600;
    white-space: nowrap;
}

/* Mode Chips */

.mode-chips {
    display: flex;
    align-items: center;
    gap: 7px;
    overflow-x: auto;
    padding-bottom: 9px;
    scrollbar-width: none;
}

.mode-chips::-webkit-scrollbar {
    display: none;
}

.mode-chip {
    flex-shrink: 0;
    height: 32px;
    padding: 0 12px;
    border-radius: 99px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-surface);
    color: var(--text-secondary);
    font-size: 12.5px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.mode-chip:hover {
    border-color: var(--brand);
    color: var(--brand);
}

.mode-chip.active {
    background: var(--bg-chip-active);
    border-color: var(--bg-chip-active);
    color: var(--text-on-brand);
    font-weight: 600;
}

/* Composer Box */

.composer-box {
    background: var(--bg-surface);
    border: 1.5px solid var(--border-strong);
    border-radius: var(--radius-lg);
    padding: 12px 14px 10px;
    box-shadow: var(--shadow-md);
    transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.composer-box:focus-within {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-ring), var(--shadow-md);
}

.composer-box textarea {
    width: 100%;
    border: none;
    outline: none;
    resize: none;
    background: transparent;
    font-size: 15px;
    line-height: 1.55;
    max-height: 180px;
    overflow-y: hidden;
    color: var(--text-primary);
}

.composer-box textarea::placeholder {
    color: var(--text-muted);
}

.composer-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 8px;
    gap: 10px;
}

.composer-meta {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 11.5px;
    color: var(--text-muted);
}

.char-counter.over {
    color: var(--danger);
    font-weight: 600;
}

.send-btn {
    width: 38px;
    height: 38px;
    border-radius: 12px;
    border: none;
    background: var(--brand);
    color: var(--text-on-brand);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s ease, transform 0.12s ease, opacity 0.15s ease;
}

.send-btn:hover:not(:disabled) {
    background: var(--brand-hover);
    transform: translateY(-1px);
}

.send-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.send-btn .icon-stop { display: none; }
.send-btn.is-streaming {
    background: var(--accent);
    color: #FFFFFF;
}
.send-btn.is-streaming .icon-send { display: none; }
.send-btn.is-streaming .icon-stop { display: block; }

.composer-disclaimer {
    margin: 8px 0 0;
    text-align: center;
    font-size: 11.4px;
    color: var(--text-muted);
    line-height: 1.4;
}

/* ==================== MODAL & TOAST ==================== */

.ai-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(12, 22, 23, 0.5);
    backdrop-filter: blur(3px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    z-index: 100;
}

.ai-modal {
    width: 100%;
    max-width: 400px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: 16px;
    padding: 20px;
    box-shadow: var(--shadow-lg);
}

.ai-modal h3 {
    margin: 0 0 14px;
    font-size: 16px;
}

.ai-modal input {
    width: 100%;
    height: 40px;
    padding: 0 12px;
    border-radius: 10px;
    border: 1px solid var(--border-strong);
    background: var(--bg-app);
    font-size: 14px;
    outline: none;
}

.ai-modal input:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-ring);
}

.ai-modal-actions {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.btn-ghost,
.btn-primary {
    height: 36px;
    padding: 0 15px;
    border-radius: 9px;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    border: none;
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    background: var(--bg-surface-hover);
}

.btn-primary {
    background: var(--brand);
    color: var(--text-on-brand);
}

.btn-primary:hover {
    background: var(--brand-hover);
}

.toast-stack {
    position: fixed;
    top: 70px;
    right: 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 120;
    pointer-events: none;
}

.toast-item {
    background: var(--bg-elevated);
    color: var(--text-primary);
    border: 1px solid var(--border-subtle);
    border-left: 3.5px solid var(--brand);
    padding: 10px 14px;
    border-radius: 10px;
    box-shadow: var(--shadow-md);
    font-size: 13px;
    max-width: 320px;
    animation: fadeUp 0.2s ease;
}

.toast-item.error {
    border-left-color: var(--danger);
}

/* ==================== ANIMATIONS ==================== */

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

@keyframes pulseDot {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.85); }
    40% { opacity: 1; transform: scale(1.1); }
}

@keyframes blinkCursor {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

@keyframes aiAvatarSpin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

@keyframes aiStarPulse {
    0%, 100% { transform: scale(0.92); opacity: 0.85; }
    50% { transform: scale(1.12); opacity: 1; }
}

@keyframes aiSparkTwinkle {
    0%, 100% { transform: scale(0.75); opacity: 0.55; }
    50% { transform: scale(1.15); opacity: 1; }
}

@keyframes aiSparkGentle {
    0%, 100% { transform: scale(1) rotate(0deg); }
    50% { transform: scale(1.14) rotate(12deg); }
}

@keyframes miniBotBlink {
    0%, 45%, 50%, 100% { transform: scaleY(1); }
    47.5% { transform: scaleY(0.14); }
}

@keyframes pulseStatus {
    0%, 100% { opacity: 0.65; }
    50% { opacity: 1; }
}

/* ==================== RESPONSIVE (MOBILE) ==================== */

@media (max-width: 860px) {
    .ai-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        transform: translateX(-100%);
        margin-left: 0 !important;
        box-shadow: var(--shadow-lg);
    }

    .ai-app.sidebar-open-mobile .ai-sidebar {
        transform: translateX(0);
    }

    .ai-app.sidebar-open-mobile .sidebar-backdrop {
        display: block;
    }

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

    .prompt-grid {
        grid-template-columns: 1fr;
    }

    .composer-hint,
    .home-pill-btn span {
        display: none;
    }

    .ai-topbar {
        height: 54px;
        min-height: 54px;
        padding: 0 10px;
        gap: 6px;
    }

    .topbar-left {
        min-width: 0;
        gap: 6px;
    }

    .topbar-right {
        flex-shrink: 0;
        gap: 6px;
    }

    .topbar-title {
        font-size: 14px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .icon-btn {
        width: 34px;
        height: 34px;
    }

    .home-pill-btn {
        height: 34px;
        padding: 0 9px;
    }

    .topbar-quota-pill {
        font-size: 11px;
        padding: 4px 8px;
    }

    .chat-scroll {
        padding: 14px 12px 24px;
    }

    .chat-scroll::-webkit-scrollbar {
        width: 3px;
    }

    /* Welcome Screen Mobile */
    .welcome-screen {
        margin: 1.5vh auto 0;
        padding: 6px 2px 16px;
    }

    .welcome-badge {
        font-size: 11.2px;
        padding: 5.5px 11px;
        gap: 6px;
        max-width: 100%;
        line-height: 1.35;
        margin-bottom: 14px;
    }

    .welcome-title {
        font-size: 24px;
        line-height: 1.3;
    }

    .welcome-subtitle {
        font-size: 13.8px;
        margin-bottom: 22px;
    }

    /* Messages Thread Mobile — Căn đều 2 bên, bỏ lệch cột avatar */
    .messages-container {
        gap: 16px;
        padding-bottom: 16px;
        width: 100%;
    }

    .user-bubble {
        max-width: 88%;
        padding: 10px 14px;
        font-size: 14.5px;
        line-height: 1.58;
        border-radius: 16px 16px 4px 16px;
    }

    .msg-row.assistant {
        flex-direction: column;
        align-items: stretch;
        gap: 0;
        background: var(--bg-surface);
        border: 1px solid var(--border-subtle);
        border-radius: 16px;
        padding: 13px 14px 12px;
        box-shadow: var(--shadow-sm);
    }

    .msg-row.assistant .ai-msg-header {
        display: flex;
        align-items: center;
        gap: 8px;
        width: 100%;
        padding-bottom: 9px;
        margin-bottom: 10px;
        border-bottom: 1px solid var(--border-subtle);
    }

    .msg-row.assistant .ai-avatar {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        margin-top: 0;
    }

    .msg-row.assistant .ai-avatar svg {
        width: 22px;
        height: 22px;
    }

    .msg-row.assistant.is-streaming .ai-avatar::before {
        inset: -2.5px;
        border-radius: 50%;
    }

    .msg-row.assistant .ai-sender-name {
        display: inline-block;
        font-size: 12.8px;
        font-weight: 700;
        color: var(--brand);
        letter-spacing: -0.01em;
    }

    .msg-row.assistant .ai-status-pill {
        display: inline-flex;
        align-items: center;
        margin-left: auto;
        padding: 2px 8px;
        border-radius: 99px;
        font-size: 11px;
        font-weight: 600;
        color: var(--brand);
        background: var(--brand-soft);
        animation: pulseStatus 1.6s ease-in-out infinite;
    }

    .msg-row.assistant .ai-body {
        width: 100%;
    }

    /* Typography câu trả lời trên Mobile */
    .ai-markdown {
        font-size: 14.5px;
        line-height: 1.72;
    }

    .ai-markdown h1,
    .ai-markdown h2,
    .ai-markdown h3,
    .ai-markdown h4 {
        margin: 14px 0 6px;
    }

    .ai-markdown h1 { font-size: 1.16em; }
    .ai-markdown h2 { font-size: 1.09em; }
    .ai-markdown h3 { font-size: 1.02em; }

    .ai-markdown p {
        margin: 0 0 10px;
    }

    .ai-markdown ul,
    .ai-markdown ol {
        margin: 0 0 10px;
        padding-left: 18px;
    }

    .ai-markdown ul ul,
    .ai-markdown ol ul,
    .ai-markdown ul ol,
    .ai-markdown ol ol {
        padding-left: 15px;
        margin-top: 4px;
        margin-bottom: 4px;
    }

    .ai-markdown li {
        margin-bottom: 5px;
    }

    .ai-markdown blockquote {
        margin: 10px 0;
        padding: 10px 12px;
        font-size: 14px;
        line-height: 1.65;
    }

    .ai-markdown pre {
        padding: 10px 12px;
        font-size: 12.8px;
    }

    .ai-markdown th,
    .ai-markdown td {
        padding: 8px 10px;
        font-size: 13px;
        line-height: 1.5;
    }

    .ai-table-wrap td {
        min-width: 118px;
    }

    .ai-table-wrap td:first-child {
        min-width: 104px;
    }

    /* Citations & Toolbar Mobile */
    .citations-box {
        margin-top: 12px;
        padding-top: 10px;
    }

    .citations-grid {
        grid-template-columns: 1fr;
        gap: 7px;
    }

    .citation-card {
        background: var(--bg-app);
        padding: 9px 11px;
    }

    .msg-toolbar {
        margin-top: 12px;
        padding-top: 10px;
        border-top: 1px solid var(--border-subtle);
        gap: 6px;
    }

    .msg-tool-btn {
        height: 32px;
        padding: 0 10px;
        border-radius: 8px;
        border: 1px solid var(--border-subtle);
        background: var(--bg-app);
        color: var(--text-secondary);
        font-size: 12px;
    }

    .msg-tool-btn.msg-tool-icon {
        width: 32px;
        padding: 0;
        justify-content: center;
    }

    .msg-feedback-group {
        margin-left: auto;
        gap: 6px;
    }

    /* Composer & Controls Mobile */
    .composer-dock {
        padding: 6px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    }

    .composer-box {
        border-radius: 16px;
        padding: 10px 12px 8px;
    }

    .composer-box textarea {
        font-size: 16px;
        line-height: 1.5;
        overflow-y: hidden;
    }

    .sidebar-search input,
    .ai-modal input {
        font-size: 16px;
    }

    /* Khi bàn phím ảo đang mở trên iOS/mobile: gọn phần đáy để không tạo khoảng trống thừa */
    .ai-app.keyboard-open .composer-dock {
        padding-bottom: 8px;
    }

    .ai-app.keyboard-open .composer-disclaimer {
        display: none;
    }

    .ai-app.keyboard-open .mode-chips {
        padding-bottom: 6px;
    }

    .scroll-bottom-btn {
        top: -42px;
        width: 34px;
        height: 34px;
    }

    .conv-actions {
        opacity: 1;
    }

    .toast-stack {
        top: 62px;
        right: 12px;
        left: 12px;
        align-items: center;
    }

    .toast-item {
        max-width: 100%;
    }
}

/* Chống iOS Safari / iPadOS auto-zoom trên mọi thiết bị cảm ứng (kể cả iPad màn hình ngang > 860px) */
@media (hover: none) and (pointer: coarse) {
    .composer-box textarea,
    .sidebar-search input,
    .ai-modal input {
        font-size: 16px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .brand-mark .mini-bot-eye,
    .ai-avatar .mini-bot-eye,
    .badge-spark,
    .msg-row.assistant.is-streaming .ai-avatar::before,
    .msg-row.assistant.is-streaming .ai-avatar .mini-bot-eye,
    .msg-row.assistant .ai-status-pill {
        animation: none !important;
    }
}
