:root {
    --primary: #1d9bf0;
    --primary-dark: #1a8cd8;
    --bg: linear-gradient(160deg, #f0f4f8 0%, #e8eef5 100%);
    --glass: rgba(255, 255, 255, 0.96);
    --text: #0f1419;
    --muted: #536471;
    --border-light: rgba(15, 20, 25, 0.08);
    --err-bg: #fef2f2;
    --err-text: #b91c1c;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.08);
    
    /* LARGER BASE SIZES */
    --font-base: 18px;
    --font-message: 1rem;
    --font-header: 1.25rem;
    --font-subheader: 0.9rem;
}

* { 
    margin: 0; 
    padding: 0; 
    box-sizing: border-box; 
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: "Be Vietnam Pro", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text);
    min-height: 200vh;
    min-height: 100dvh;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    font-size: var(--font-base);
}

.chat-container {
    width: 100%;
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
    background: var(--glass);
    backdrop-filter: blur(16px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

/* Tablet and Desktop */
@media (min-width: 640px) {
    body { 
        padding: 16px;
        background: var(--bg);
    }
    .chat-container {
        max-width: min(800px, 98vw);
        height: min(90vh, 800px);
        border: 1px solid var(--border-light);
        border-radius: 24px;
        box-shadow: var(--shadow-md);
    }
}

/* Larger screen optimization */
@media (min-width: 1024px) {
    :root {
        --font-base: 17px;
        --font-message: 1.05rem;
    }
    .chat-container {
        max-width: 850px;
        height: 85vh;
    }
}

.api-banner {
    display: none;
    padding: 10px 14px;
    font-size: 0.85rem;
    background: #fff8e6;
    color: #7c5d00;
    border-bottom: 1px solid #f0d78c;
    line-height: 1.4;
    flex-shrink: 0;
}
.api-banner.is-visible { 
    display: block; 
    animation: slideDown 0.2s ease;
}

@keyframes slideDown {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

.chat-header {
    padding: 16px 18px;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    gap: 14px;
    flex-shrink: 0;
    background: rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(8px);
}

.header-actions {
    flex-shrink: 0;
    display: flex;
    gap: 8px;
}

@media (max-width: 480px) {
    .chat-header {
        padding: 14px 16px;
        gap: 12px;
    }
}

.logo {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: var(--shadow-sm);
    background: #fff;
    border: 1px solid var(--border-light);
    overflow: hidden;
}

.logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

@media (max-width: 480px) {
    .logo {
        width: 44px;
        height: 44px;
    }
}

.header-text {
    flex: 1;
    min-width: 0;
}

.header-text h1 { 
    font-family: "Sora", sans-serif; 
    font-size: var(--font-header);
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.3;
}

.header-text p { 
    font-size: var(--font-subheader);
    color: var(--muted); 
    margin-top: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
}

@media (max-width: 480px) {
    .header-text h1 { font-size: 1.15rem; }
    .header-text p { font-size: 0.85rem; }
}

.messages {
    flex: 1;
    min-width: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 18px 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}

@media (max-width: 480px) {
    .messages {
        padding: 16px 14px;
        gap: 14px;
    }
}

.message {
    max-width: min(85%, 100%);
    min-width: 0;
    padding: 14px 18px;
    border-radius: 20px;
    font-size: var(--font-message);
    line-height: 1.6;
    animation: slideIn 0.2s ease-out;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    /* overflow-x: auto; */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    box-shadow: var(--shadow-sm);
}

@media (max-width: 480px) {
    .message {
        max-width: 90%;
        padding: 12px 16px;
        font-size: 1rem;
        line-height: 1.55;
    }
}

@media (min-width: 1024px) {
    .message {
        max-width: 80%;
        padding: 16px 20px;
    }
}

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

.bot-message-wrap {
    align-self: flex-start;
    max-width: min(85%, 100%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
}

.follow-up-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 0 2px 4px;
}

.follow-up-chips-label {
    width: 100%;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 2px;
}

.follow-up-chip {
    font-size: 0.82rem;
    line-height: 1.35;
    padding: 8px 12px;
    border-radius: 18px;
    border: 1px solid rgba(29, 155, 240, 0.35);
    background: rgba(29, 155, 240, 0.08);
    color: var(--text);
    cursor: pointer;
    text-align: left;
    max-width: 100%;       
    white-space: normal;   
    word-wrap: break-word; 
    display: inline-block; 
    transition: background 0.15s ease, border-color 0.15s ease;
}

.follow-up-chip:hover,
.follow-up-chip:focus-visible {
    background: rgba(29, 155, 240, 0.16);
    border-color: var(--primary);
    outline: none;
}

.message.bot {
    align-self: flex-start;
    background: #fff;
    border: 1px solid var(--border-light);
    border-bottom-left-radius: 6px;
}

.message.user {
    align-self: flex-end;
    background: var(--primary);
    color: #fff;
    border-bottom-right-radius: 6px;
}

.message.error {
    background: var(--err-bg);
    color: var(--err-text);
    border: 1px solid #fecaca;
}

/* Typography optimizations - LARGER */
.message p { 
    margin-bottom: 0.75em; 
}
.message p:last-child { 
    margin-bottom: 0; 
}
.message ul { 
    margin-left: 1.5em; 
    margin-bottom: 0.75em;
    padding-left: 0.5em;
    list-style-type: disc;
}

.message ol { 
    margin-left: 1.5em; 
    margin-bottom: 0.75em;
    padding-left: 0.5em;
    list-style-type: decimal;
}

.message li { 
    margin-bottom: 0.35em; 
    line-height: 1.6;
    display: list-item;
}

.message code {
    font-size: 0.9em;
    background: rgba(0, 0, 0, 0.05);
    padding: 0.2em 0.4em;
    border-radius: 4px;
}
.message pre {
    overflow-x: auto;
    max-width: 100%;
    font-size: 0.9em;
    background: #f6f8fa;
    padding: 12px;
    border-radius: 8px;
    margin: 10px 0;
    box-sizing: border-box;
}

/* Headers within messages */
.message h1, .message h2, .message h3 {
    margin-top: 0.75em;
    margin-bottom: 0.5em;
    font-weight: 600;
}
.message h1 { font-size: 1.5em; }
.message h2 { font-size: 1.3em; }
.message h3 { font-size: 1.15em; }

.input-area {
    padding: 16px 18px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
    background: #fff;
    border-top: 1px solid var(--border-light);
    display: flex;
    gap: 12px;
    flex-shrink: 0;
    align-items: center;
}

@media (max-width: 480px) {
    .input-area {
        padding: 14px 16px;
        gap: 10px;
    }
}

input {
    flex: 1;
    padding: 14px 18px;
    border-radius: 28px;
    border: 2px solid #e1e8ed;
    outline: none;
    font-size: 1rem;
    font-family: inherit;
    background: #fafbfc;
    transition: all 0.15s;
    min-width: 0;
}

@media (max-width: 480px) {
    input {
        padding: 13px 16px;
        font-size: 1rem;
    }
}

@media (min-width: 1024px) {
    input {
        padding: 15px 20px;
        font-size: 1.05rem;
    }
}

input:focus { 
    border-color: var(--primary); 
    background: #fff;
    box-shadow: 0 0 0 4px rgba(29, 155, 240, 0.12); 
}

input::placeholder {
    color: #8899a6;
    opacity: 0.8;
    font-weight: 400;
}

button {
    padding: 14px 28px;
    border-radius: 28px;
    background: var(--primary);
    color: #fff;
    border: none;
    font-weight: 600;
    font-size: 1rem;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
    flex-shrink: 0;
    box-shadow: var(--shadow-sm);
}

@media (max-width: 480px) {
    button {
        padding: 13px 22px;
        font-size: 1rem;
    }
}

@media (min-width: 1024px) {
    button {
        padding: 15px 32px;
        font-size: 1.05rem;
    }
}

button:hover {
    background: var(--primary-dark);
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(29, 155, 240, 0.2);
}

button:active {
    transform: translateY(0);
    box-shadow: var(--shadow-sm);
}

button:disabled { 
    opacity: 0.5; 
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn-mic {
    width: 48px;
    height: 48px;
    padding: 0;
    border-radius: 50%;
    background: #f0f4f8;
    border: 2px solid #e1e8ed;
    color: var(--muted);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s ease;
}

.btn-mic:hover {
    background: #e8eef5;
    border-color: var(--primary);
    color: var(--primary);
    transform: none;
    box-shadow: 0 4px 12px rgba(29, 155, 240, 0.15);
}

.btn-mic.recording {
    background: #fef2f2;
    border-color: #ef4444;
    color: #ef4444;
    animation: pulse-recording 1s infinite;
}

@keyframes pulse-recording {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

.btn-mic svg {
    width: 22px;
    height: 22px;
}

.btn-lang-toggle {
    flex-shrink: 0;
    height: 48px;
    padding: 0 12px;
    border-radius: 14px;
    background: #f0f4f8;
    border: 2px solid #e1e8ed;
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    line-height: 1;
    box-shadow: none;
}

.btn-lang-toggle svg {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    flex-shrink: 0;
}

.btn-lang-toggle span {
    font-size: inherit;
    font-weight: inherit;
}

.btn-lang-toggle:hover {
    background: #e8eef5;
    border-color: var(--primary);
    color: var(--primary);
    transform: none;
    box-shadow: 0 4px 12px rgba(29, 155, 240, 0.12);
}

.btn-lang-toggle:active {
    transform: none;
}

.btn-tts-toggle {
    flex-shrink: 0;
    width: 48px;
    height: 48px;
    padding: 0;
    border-radius: 14px;
    background: #f0f4f8;
    border: 2px solid #e1e8ed;
    color: var(--muted);
    font-size: 0;
    font-family: inherit;
    cursor: pointer;
    transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: none;
}

.btn-tts-toggle svg {
    width: 22px;
    height: 22px;
    stroke: currentColor;
}

.btn-tts-toggle:hover {
    background: #e8eef5;
    border-color: var(--primary);
    color: var(--primary);
}

.btn-tts-toggle:active {
    transform: none;
}

.typing-indicator {
    display: none;
    padding: 8px 20px;
    font-size: 0.9rem;
    color: var(--muted);
    flex-shrink: 0;
    font-weight: 500;
}

.typing-indicator.is-on { 
    display: block;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.6; }
    50% { opacity: 1; }
}

/* Scrollbar styling - thicker for easier grabbing */
.messages::-webkit-scrollbar {
    width: 6px;
}

.messages::-webkit-scrollbar-track {
    background: transparent;
}

.messages::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 10px;
}

.messages::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.3);
}

@media (min-width: 640px) {
    .messages::-webkit-scrollbar {
        width: 8px;
    }
}

/* Math formula scaling - keep readable */
.katex {
    font-size: 1.1em !important;
}

@media (max-width: 480px) {
    .katex {
        font-size: 1.05em !important;
    }
}

/* Links in messages */
.message a {
    color: var(--primary);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.message.user a {
    color: #fff;
    text-decoration: underline;
}

/* Blockquotes */
.message blockquote {
    border-left: 4px solid var(--primary);
    padding-left: 16px;
    margin: 10px 0;
    color: var(--muted);
    font-style: italic;
}

/* Tables — parent .message scrolls horizontally if bảng quá rộng */
.message table {
    border-collapse: collapse;
    width: 100%;
    max-width: 100%;
    margin: 10px 0;
    font-size: 0.95em;
    table-layout: auto;
}

.message th, .message td {
    border: 1px solid var(--border-light);
    padding: 8px 12px;
    text-align: left;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.message th {
    background: rgba(29, 155, 240, 0.1);
    font-weight: 600;
}

/* KaTeX: khối $$…$$ cuộn ngang trong bong bóng */
.message .katex-display {
    margin: 0.65em 0 !important;
    overflow-x: auto;
    overflow-y: hidden;
    max-width: 100%;
    padding: 4px 0;
    -webkit-overflow-scrolling: touch;
}

.message .katex-display > .katex {
    white-space: nowrap;
}
