/* ═══════════════════════════════════════════════════
   style_mobile.css — Tối ưu toàn diện cho mobile
   Load SAU style_default.css và style_dark.css
   ═══════════════════════════════════════════════════ */

/* ── CSS CUSTOM PROPERTIES ─────────────────────────────────────────── */
:root {
    /* Safe area insets (notch / home indicator iOS & Android) */
    --safe-top:    env(safe-area-inset-top,    0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left:   env(safe-area-inset-left,   0px);
    --safe-right:  env(safe-area-inset-right,  0px);

    /* Minimum touch target */
    --tap-min: 44px;
}

/* ── PREVENT DOUBLE-TAP ZOOM & TEXT RESIZE ──────────────────────────── */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    touch-action: manipulation;
}

/* ── FOCUS OUTLINE — keyboard accessible, không xấu trên mobile ─────── */
:focus-visible {
    outline: 2px solid currentColor;
    outline-offset: 2px;
}
button:focus:not(:focus-visible),
input:focus:not(:focus-visible) {
    outline: none;
}

/* ════════════════════════════════════════════════════
   MOBILE BREAKPOINT CHÍNH — max-width: 640px
   ════════════════════════════════════════════════════ */
@media (max-width: 640px) {

    /* ── BODY / ROOT ──────────────────────────────────────────── */
    body {
        flex-direction: column;
        height: 100%;
        height: 100dvh; /* Dynamic Viewport Height — tránh bàn phím che */
        min-height: -webkit-fill-available; /* iOS Safari fallback */
        overflow: hidden;
        position: fixed;   /* Ngăn bounce scroll trên iOS */
        width: 100%;
    }

    /* ── SIDEBAR ──────────────────────────────────────────────── */
    #sidebar {
        position: fixed;
        top: 0; left: 0;
        height: 100%;
        height: 100dvh;
        width: min(280px, 85vw);  /* Không chiếm quá 85% màn hình */
        z-index: 300;
        transform: translateX(-100%);
        /* Padding top để tránh notch */
        padding-top: var(--safe-top);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    /* Backdrop mờ khi sidebar mở */
    #sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.45);
        z-index: 299;
        -webkit-backdrop-filter: blur(2px);
        backdrop-filter: blur(2px);
        animation: backdropIn 0.2s ease;
    }

    #sidebar-backdrop.visible {
        display: block;
    }

    @keyframes backdropIn {
        from { opacity: 0; }
        to   { opacity: 1; }
    }

    /* Sidebar toggle button — đảm bảo tap target đủ lớn */
    #btn-toggle-sidebar {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: var(--tap-min);
        height: var(--tap-min);
        padding: 0;
        margin: 0 4px 0 -8px;
    }

    /* ── CHAT WRAP ────────────────────────────────────────────── */
    #chat-wrap {
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        width: 100%;
        height: 100%;
        height: 100dvh;
    }

    /* ── HEADER ───────────────────────────────────────────────── */
    #chat-header {
        padding: 10px 14px 10px calc(14px + var(--safe-left));
        padding-top: calc(10px + var(--safe-top));
        flex-shrink: 0;
        /* Tăng font size một chút cho dễ đọc */
        font-size: 14px;
    }

    #chat-header small {
        font-size: 11px;
        /* Ẩn trên màn hình rất nhỏ để tiết kiệm chiều cao */
        display: none;
    }

    /* ── MESSAGES ─────────────────────────────────────────────── */
    #messages {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 14px 12px 8px;
        gap: 10px;
        /* Smooth scroll trên iOS */
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
        /* Ngăn messages bị che bởi input area */
        padding-bottom: 6px;
    }

    /* Bubbles — mở rộng tối đa trên mobile */
    .msg {
        max-width: 94%;
        font-size: 14px;       /* Tăng từ 13.5px */
        line-height: 1.65;
        padding: 11px 14px;
        border-radius: 16px;
    }

    .msg-user {
        border-bottom-right-radius: 4px;
    }

    .msg-bot {
        border-bottom-left-radius: 4px;
    }

    /* Sources & suggestions */
    .sources-wrap,
    .suggest-wrap {
        max-width: 96%;
    }

    .suggest-btn {
        font-size: 12.5px;
        padding: 8px 14px;
        min-height: 36px;
    }

    /* ── WELCOME SCREEN ───────────────────────────────────────── */
    #welcome {
        padding: 16px 14px;
        gap: 12px;
        justify-content: flex-start;  /* Cuộn được khi nội dung dài */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    #welcome .avatar {
        width: 60px;
        height: 60px;
        font-size: 28px;
        flex-shrink: 0;
    }

    #welcome h3 {
        font-size: 18px;
        margin-bottom: 2px;
    }

    #welcome > p {
        font-size: 13px;
        line-height: 1.6;
        margin-bottom: 4px;
    }

    .topic-group {
        width: 100%;
        max-width: 100%;
        margin-bottom: 10px;
    }

    .topic-label {
        font-size: 10px;
        margin-bottom: 7px;
    }

    /* 1 cột trên mobile nhỏ */
    .sample-grid {
        grid-template-columns: 1fr;
        gap: 6px;
    }

    .sample-btn {
        padding: 10px 13px;
        font-size: 13px;
        min-height: var(--tap-min);
        line-height: 1.5;
        border-radius: 12px;
    }

    /* ── INPUT AREA ───────────────────────────────────────────── */
    #input-area {
        flex-shrink: 0;
        padding: 10px 12px;
        /* Safe area bottom — quan trọng cho iPhone có home indicator */
        padding-bottom: calc(10px + var(--safe-bottom));
        gap: 6px;
        /* Không bị đẩy lên quá cao khi bàn phím mở */
        position: relative;
        z-index: 10;
    }

    #input-row {
        gap: 8px;
        align-items: center;
    }

    /* Nút đính kèm file */
    #btn-attach {
        width: var(--tap-min);
        height: var(--tap-min);
        flex-shrink: 0;
        font-size: 18px;
        border-radius: 50%;
    }

    /* Input text — tăng font để iOS không tự zoom vào */
    #user-input {
        font-size: 16px;    /* >= 16px: iOS KHÔNG zoom khi focus */
        padding: 10px 14px;
        height: var(--tap-min);
        border-radius: 22px;
        /* Tránh zoom */
        -webkit-appearance: none;
        appearance: none;
    }

    /* Nút gửi */
    #btn-send {
        width: var(--tap-min);
        height: var(--tap-min);
        flex-shrink: 0;
        font-size: 16px;
        border-radius: 50%;
    }

    /* File badge */
    #file-badge {
        font-size: 12px;
        padding: 5px 10px;
        border-radius: 10px;
        max-width: 100%;
    }

    #file-badge-name {
        max-width: calc(100vw - 120px);
    }

    /* File banner */
    #file-banner {
        padding: 7px 12px;
        font-size: 12px;
    }

    /* Progress bar */
    #embed-progress {
        font-size: 11px;
        border-radius: 6px;
    }

    /* ── BUTTONS TRONG SIDEBAR ────────────────────────────────── */
    #btn-new-chat {
        min-height: var(--tap-min);
        font-size: 13px;
        padding: 10px 14px;
    }

    .hist-item {
        min-height: 40px;
    }

    .hist-title {
        font-size: 13px;
        padding: 8px 4px;
    }

    .hist-del {
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        opacity: 1; /* Luôn hiện trên mobile (không có hover) */
        padding: 0;
    }

    .hist-clear-all {
        min-height: 40px;
        font-size: 12px;
    }

    /* ── THEME TOGGLE BUTTON ──────────────────────────────────── */
    #btn-theme-toggle {
        bottom: calc(76px + var(--safe-bottom));
        left: calc(12px + var(--safe-left));
        width: 40px;
        height: 40px;
        font-size: 17px;
        /* Luôn nổi trên input area */
        z-index: 50;
    }

    /* ── PARTICLES — tắt trên mobile để tiết kiệm pin ────────── */
    #particles-js {
        display: none;
    }
}

/* ── MÀN HÌNH RẤT NHỎ (iPhone SE, 375px và nhỏ hơn) ─────────────── */
@media (max-width: 380px) {
    #chat-header {
        padding: 9px 10px;
        padding-top: calc(9px + var(--safe-top));
        font-size: 13.5px;
    }

    .msg {
        font-size: 13.5px;
        padding: 10px 12px;
    }

    #welcome h3 {
        font-size: 17px;
    }

    #input-area {
        padding: 8px 10px;
        padding-bottom: calc(8px + var(--safe-bottom));
    }

    #user-input {
        padding: 9px 12px;
    }
}

/* ── LANDSCAPE MOBILE — chiều ngang ──────────────────────────────── */
@media (max-width: 932px) and (max-height: 430px) and (orientation: landscape) {
    #welcome .avatar {
        display: none; /* Ẩn avatar khi màn hình quá thấp */
    }

    #welcome {
        gap: 8px;
        padding: 10px 14px;
    }

    #welcome h3 {
        font-size: 16px;
    }

    #messages {
        padding: 10px 12px 6px;
    }

    #input-area {
        padding: 8px 12px;
        padding-bottom: calc(8px + var(--safe-bottom));
    }

    #user-input {
        height: 40px;
        padding: 8px 14px;
    }

    #btn-send, #btn-attach {
        width: 40px;
        height: 40px;
    }

    #btn-theme-toggle {
        bottom: calc(52px + var(--safe-bottom));
    }
}

/* ── TABLET (641px – 1024px) ─────────────────────────────────────── */
@media (min-width: 641px) and (max-width: 1024px) {
    .msg {
        max-width: 85%;
        font-size: 14px;
    }

    .sample-grid {
        grid-template-columns: 1fr 1fr;
    }

    #user-input {
        font-size: 15px;
    }

    #btn-send, #btn-attach {
        width: 44px;
        height: 44px;
    }
}

/* ── KEYBOARD VIEWPORT FIX — Visual Viewport API ─────────────────── */
/* Khi bàn phím mở trên mobile, #chat-wrap co lại đúng cách */
@supports (height: 100dvh) {
    @media (max-width: 640px) {
        body,
        #chat-wrap {
            height: 100dvh;
        }
    }
}