/*
 * Giao diện quản trị Zalo Gateway.
 *
 * Không dùng framework hay CDN: gateway thường nằm trên VPS bị siết mạng, và
 * một trang phụ thuộc CDN sẽ hỏng ngay khi máy chủ không ra được Internet.
 *
 * Bảng màu định nghĩa đầy đủ ở :root (sáng), rồi ghi đè cho nền tối.
 */

:root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #fafbfc;
    --border: #e2e5e9;
    --border-strong: #cfd4da;
    --text: #1b1f24;
    --text-muted: #6a737d;
    --text-faint: #8b949e;

    --primary: #0068ff;          /* xanh Zalo */
    --primary-hover: #0055d4;
    --primary-soft: #e6f0ff;

    --success: #1a7f37;
    --success-soft: #e6f6ec;
    --warning: #9a6700;
    --warning-soft: #fff8e5;
    --danger: #cf222e;
    --danger-soft: #ffeef0;

    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 1px 3px rgba(16, 22, 26, .08), 0 1px 2px rgba(16, 22, 26, .04);
    --shadow-lg: 0 12px 32px rgba(16, 22, 26, .16);

    --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
    --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1216;
        --surface: #171b21;
        --surface-2: #1c2128;
        --border: #2a3038;
        --border-strong: #3a424c;
        --text: #e6edf3;
        --text-muted: #9198a1;
        --text-faint: #6e7681;

        --primary: #4a9eff;
        --primary-hover: #6cb0ff;
        --primary-soft: #10243e;

        --success: #3fb950;
        --success-soft: #0f2a16;
        --warning: #d29922;
        --warning-soft: #2b2412;
        --danger: #f85149;
        --danger-soft: #2d1517;

        --shadow: 0 1px 3px rgba(0, 0, 0, .4);
        --shadow-lg: 0 12px 32px rgba(0, 0, 0, .6);
    }
}

* { box-sizing: border-box; }

/*
 * Thuộc tính `hidden` phải luôn thắng.
 *
 * Trình duyệt đặt [hidden]{display:none} trong stylesheet mặc định, mà stylesheet
 * đó có độ ưu tiên THẤP HƠN mọi quy tắc ta viết. Nên chỉ cần một dòng như
 * `.modal { display: grid }` là phần tử vẫn hiện dù đã gắn hidden — chính xác là
 * lỗi khiến hộp thoại QR bật lên ngay từ màn hình đăng nhập.
 */
[hidden] { display: none !important; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.55 var(--sans);
    -webkit-font-smoothing: antialiased;
    /* Chặn viền xám nhấp nháy khi chạm trên Android, và chặn kéo quá đà cả trang */
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: none;
}

/* Menu trượt đang mở thì khoá cuộn nền, tránh cuộn xuyên qua lớp phủ */
body:has(#app.nav-open) { overflow: hidden; }

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }

code { font-family: var(--mono); font-size: .92em; }
.muted { color: var(--text-muted); }

/* ── Màn hình token ────────────────────────────────────────────────── */

.gate {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}

.gate-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    padding: 32px 28px;
    text-align: center;
}

.gate-logo { font-size: 40px; line-height: 1; margin-bottom: 12px; }
.gate-card h1 { margin-bottom: 6px; }
.gate-card > p { margin: 0 0 22px; font-size: 13px; }
.gate-card .field { text-align: left; }

.gate-error {
    margin: 14px 0 0;
    padding: 9px 12px;
    background: var(--danger-soft);
    color: var(--danger);
    border-radius: var(--radius-sm);
    font-size: 13px;
    text-align: left;
}

/* ── Bố cục: menu dọc bên trái + vùng nội dung ─────────────────────── */

#app { display: flex; min-height: 100vh; }

.sidebar {
    /* sticky + 100vh: menu đứng yên khi vùng nội dung cuộn */
    position: sticky;
    top: 0;
    z-index: 30;
    flex: none;
    width: 236px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.brand {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 15px 18px;
    border-bottom: 1px solid var(--border);
}
.brand-mark { font-size: 20px; }
.brand-name { font-weight: 600; font-size: 15px; }

.sidebar-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 11px 12px;
    border-top: 1px solid var(--border);
}

.status { display: inline-flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text-muted); min-width: 0; }
.status-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot-ok { background: var(--success); box-shadow: 0 0 0 3px var(--success-soft); }
.dot-bad { background: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }
.dot-idle { background: var(--text-faint); }

/* ── Thanh trên vùng nội dung ──────────────────────────────────────── */

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.page-title { font-size: 15px; font-weight: 600; }

.nav-toggle {
    display: none;          /* chỉ hiện ở màn hình hẹp */
    appearance: none;
    background: none;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text);
    padding: 5px;
    cursor: pointer;
    place-items: center;
}
.nav-toggle svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.nav-toggle:hover { background: var(--surface-2); }

.nav-backdrop { display: none; }

/* ── Mục menu ──────────────────────────────────────────────────────── */

.tabs {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
    min-height: 0;
    padding: 10px;
    overflow-y: auto;
}

.tab {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    appearance: none;
    background: none;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font: inherit;
    font-size: 13.5px;
    text-align: left;
    padding: 9px 11px;
    cursor: pointer;
    white-space: nowrap;
    transition: background .12s, color .12s;
}

.tab-icon {
    width: 17px;
    height: 17px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: .8;
}

.tab-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.tab.active .tab-icon { opacity: 1; }

.wrap { width: 100%; max-width: 1080px; margin: 0 auto; padding: 22px 24px 60px; }

/* Màn hình hẹp: menu trượt ra từ cạnh trái */
@media (max-width: 900px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        height: auto;
        transform: translateX(-100%);
        transition: transform .18s ease-out;
        box-shadow: var(--shadow-lg);
    }
    #app.nav-open .sidebar { transform: none; }

    .nav-toggle { display: grid; }

    #app.nav-open .nav-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 25;
        background: rgba(0, 0, 0, .45);
    }
}

.panel { display: none; }
.panel.active { display: block; animation: fade .16s ease-out; }

@keyframes fade { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 18px;
    overflow: hidden;
}

.card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 13px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
}

.form-card { padding: 20px; }
.form-card h2 { margin-bottom: 4px; }
.form-card > .muted { margin: 0 0 16px; font-size: 13px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
@media (max-width: 880px) { .grid-2 { grid-template-columns: 1fr; } }

.filters { display: flex; gap: 8px; align-items: center; }

/* ── Ô thống kê ────────────────────────────────────────────────────── */

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
    margin-bottom: 18px;
}

.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 15px 16px;
    box-shadow: var(--shadow);
}

.stat-value { font-size: 26px; font-weight: 600; line-height: 1.15; letter-spacing: -.02em; }
.stat-label { font-size: 11.5px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; margin-top: 3px; }
.stat-sub { font-size: 11.5px; color: var(--text-faint); margin-top: 5px; }

/* ── Biểu mẫu ──────────────────────────────────────────────────────── */

.field { display: block; margin-bottom: 15px; }
.field > span { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; }
.field > span em { font-weight: 400; font-style: normal; color: var(--text-faint); }
.field small { display: block; margin-top: 5px; font-size: 11.5px; color: var(--text-muted); }

input[type=text], input[type=password], input[type=number], textarea, select {
    width: 100%;
    padding: 8px 11px;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text);
    font: inherit;
    font-size: 13.5px;
}

textarea { resize: vertical; min-height: 84px; line-height: 1.5; }

input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

input:invalid:not(:placeholder-shown) { border-color: var(--danger); }

.check { display: flex; align-items: center; gap: 8px; margin-bottom: 15px; font-size: 13px; cursor: pointer; }
.check input { width: 15px; height: 15px; accent-color: var(--primary); cursor: pointer; }

.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row .field { flex: 1 1 180px; margin-bottom: 15px; }
.row .field-sm { flex: 0 1 130px; }

.actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }

/* ── Nút ───────────────────────────────────────────────────────────── */

.btn {
    appearance: none;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    padding: 8px 14px;
    font: inherit;
    font-size: 13.5px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: background .12s, border-color .12s, opacity .12s;
}

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

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }

.btn-ghost { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); border-color: var(--text-faint); }

.btn-danger { background: var(--surface); border-color: var(--border-strong); color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); }

.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-block { width: 100%; margin-top: 6px; }

/* ── Bảng ──────────────────────────────────────────────────────────── */

.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th {
    text-align: left;
    padding: 9px 16px;
    font-size: 11.5px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
.table td { padding: 11px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--surface-2); }

.table-scroll { overflow-x: auto; }

.cell-actions { display: flex; gap: 5px; justify-content: flex-end; }
.mono { font-family: var(--mono); font-size: 11.5px; color: var(--text-muted); }
.nowrap { white-space: nowrap; }

.name-main { font-weight: 500; }
.name-sub { font-size: 11.5px; color: var(--text-muted); margin-top: 1px; }

/* ── Nhãn trạng thái ───────────────────────────────────────────────── */

.tag {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 20px;
    font-size: 11.5px;
    font-weight: 500;
    white-space: nowrap;
}

.tag-ok { background: var(--success-soft); color: var(--success); }
.tag-warn { background: var(--warning-soft); color: var(--warning); }
.tag-bad { background: var(--danger-soft); color: var(--danger); }
.tag-idle { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }
.tag-info { background: var(--primary-soft); color: var(--primary); }

/* ── Trạng thái rỗng / đang tải ────────────────────────────────────── */

.loading, .empty { padding: 34px 16px; text-align: center; color: var(--text-muted); font-size: 13px; }
.empty-hint { display: block; margin-top: 6px; font-size: 12px; color: var(--text-faint); }

/* ── Kết quả thao tác ──────────────────────────────────────────────── */

.result {
    margin-top: 14px;
    padding: 11px 13px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    line-height: 1.5;
}

.result-ok { background: var(--success-soft); color: var(--success); }
.result-bad { background: var(--danger-soft); color: var(--danger); }
.result-warn { background: var(--warning-soft); color: var(--warning); }
.result dl { margin: 7px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; font-size: 12px; }
.result dt { color: inherit; opacity: .75; }
.result dd { margin: 0; font-family: var(--mono); font-size: 11.5px; word-break: break-all; }

/* ── Hộp thoại ─────────────────────────────────────────────────────── */

.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    display: grid;
    place-items: center;
    padding: 20px;
    z-index: 50;
}

.modal-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 620px;
    overflow: hidden;
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
}

.qr-body { display: grid; grid-template-columns: auto 1fr; gap: 22px; padding: 22px; }
@media (max-width: 620px) { .qr-body { grid-template-columns: 1fr; } }

.qr-frame {
    width: 236px;
    height: 236px;
    display: grid;
    place-items: center;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px;
}

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

.qr-placeholder { text-align: center; color: #6a737d; font-size: 12.5px; }
.qr-placeholder p { margin: 10px 0 0; }

.spinner {
    width: 26px;
    height: 26px;
    margin: 0 auto;
    border: 2.5px solid #d8dce0;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.qr-side { display: flex; flex-direction: column; }
.qr-state { font-weight: 600; margin: 0 0 12px; font-size: 13.5px; }
.qr-state.is-ok { color: var(--success); }
.qr-state.is-bad { color: var(--danger); }
.qr-state.is-warn { color: var(--warning); }

.qr-steps { margin: 0 0 14px; padding-left: 20px; font-size: 13px; color: var(--text-muted); }
.qr-steps li { margin-bottom: 5px; }
.qr-steps strong { color: var(--text); }

.qr-note { font-size: 12px; margin: 0 0 14px; }
.qr-side .btn { align-self: flex-start; margin-top: auto; }

/* ── Trò chuyện ────────────────────────────────────────────────────── */

.tab-badge {
    display: inline-block;
    min-width: 17px;
    padding: 1px 5px;
    margin-left: auto;      /* dồn về mép phải của mục menu */
    background: var(--danger);
    color: #fff;
    border-radius: 9px;
    font-size: 10.5px;
    font-weight: 600;
    line-height: 15px;
    text-align: center;
    vertical-align: 1px;
}

.chat {
    display: grid;
    grid-template-columns: 260px 1fr;
    /* Chiều cao cố định để khung tin nhắn tự cuộn, thay vì kéo dài cả trang.
       Trừ đi: thanh trên (53) + padding trên của .wrap (22) + khoảng thở dưới */
    height: calc(100vh - 150px);
    min-height: 420px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.chat-list {
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--surface-2);
}

.chat-list-head { padding: 9px 10px; border-bottom: 1px solid var(--border); }
.chat-list-head select { font-size: 12.5px; padding: 5px 8px; }

.chat-threads { overflow-y: auto; flex: 1; min-height: 0; }

.thread {
    display: flex;
    gap: 9px;
    align-items: center;
    padding: 10px 11px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: background .1s;
}

.thread:hover { background: var(--surface); }
.thread.active { background: var(--primary-soft); }

.thread-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    flex: none;
    display: grid;
    place-items: center;
    background: var(--primary);
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    overflow: hidden;
}

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

.thread-body { min-width: 0; flex: 1; }
.thread-top { display: flex; justify-content: space-between; gap: 6px; align-items: baseline; }
.thread-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-time { font-size: 10.5px; color: var(--text-faint); flex: none; }
.thread-preview {
    font-size: 11.5px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}
.thread.unread .thread-name { font-weight: 700; }
.thread.unread .thread-preview { color: var(--text); font-weight: 500; }

.thread-badge {
    min-width: 17px;
    height: 17px;
    padding: 0 5px;
    background: var(--danger);
    color: #fff;
    border-radius: 9px;
    font-size: 10.5px;
    font-weight: 600;
    line-height: 17px;
    text-align: center;
    flex: none;
}

.chat-main { display: flex; flex-direction: column; min-height: 0; }

.chat-empty {
    flex: 1;
    display: grid;
    place-content: center;
    text-align: center;
    color: var(--text-muted);
    padding: 30px;
    gap: 4px;
}
.chat-empty-icon { font-size: 34px; }
.chat-empty p { margin: 0; }

.chat-view { display: flex; flex-direction: column; min-height: 0; flex: 1; }

.chat-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 11px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
}
.chat-head-id { flex: 1; min-width: 0; }
.chat-head-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head-sub { font-size: 11px; color: var(--text-faint); margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat-back {
    display: none;          /* chỉ hiện khi khung chat co về một cột */
    appearance: none;
    background: none;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text);
    flex: none;
    margin-left: -6px;
    padding: 6px;
    cursor: pointer;
    place-items: center;
}
.chat-back svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chat-back:hover { background: var(--border); }

.chat-log {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.chat-day {
    align-self: center;
    font-size: 11px;
    color: var(--text-faint);
    background: var(--surface-2);
    border: 1px solid var(--border);
    padding: 2px 10px;
    border-radius: 12px;
    margin: 6px 0;
}

.bubble { max-width: 66%; display: flex; flex-direction: column; gap: 2px; }
.bubble.in { align-self: flex-start; }
.bubble.out { align-self: flex-end; align-items: flex-end; }

.bubble-sender { font-size: 11px; color: var(--text-muted); padding: 0 4px; }

.bubble-body {
    padding: 8px 12px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
}

.bubble.in .bubble-body {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.bubble.out .bubble-body {
    background: var(--primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.bubble.recalled .bubble-body { font-style: italic; opacity: .65; }

.bubble-media {
    display: block;
    max-width: 240px;
    max-height: 240px;
    border-radius: 10px;
    margin-top: 4px;
    border: 1px solid var(--border);
}

.bubble-time { font-size: 10.5px; color: var(--text-faint); padding: 0 4px; }

.chat-composer {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    padding: 11px 14px;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
}

.chat-composer textarea {
    flex: 1;
    min-height: 38px;
    max-height: 140px;
    resize: none;
    line-height: 1.45;
}

.chat-composer .btn { flex: none; }

/*
 * Màn hình hẹp không đủ chỗ cho hai cột. Xếp chồng danh sách hội thoại và khung
 * tin nhắn vào cùng một ô lưới rồi cho chúng thay phiên nhau — giống ứng dụng
 * nhắn tin trên điện thoại. `.showing-thread` do app.js bật/tắt.
 *
 * Khối này phải nằm SAU các quy tắc .chat-main ở trên: cùng độ ưu tiên thì cái
 * viết sau thắng, đặt trước là khung tin nhắn vẫn hiện chồng lên danh sách.
 */
@media (max-width: 780px) {
    .chat {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr;
        height: calc(100vh - 134px);
    }

    .chat-list, .chat-main { grid-area: 1 / 1; min-width: 0; }
    .chat-list { border-right: none; }

    .chat-main { display: none; }
    .chat.showing-thread .chat-list { display: none; }
    .chat.showing-thread .chat-main { display: flex; }

    .chat-back { display: grid; }
    .bubble { max-width: 82%; }
}

/* ── Thông báo nổi ─────────────────────────────────────────────────── */

.toasts {
    position: fixed;
    right: 18px;
    bottom: 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 90;
    max-width: min(400px, calc(100vw - 36px));
}

.toast {
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-left: 3px solid var(--text-faint);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    padding: 11px 14px;
    font-size: 13px;
    animation: slide .18s ease-out;
}

.toast-ok { border-left-color: var(--success); }
.toast-bad { border-left-color: var(--danger); }
.toast-warn { border-left-color: var(--warning); }

@keyframes slide { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: none; } }

/* ── Điện thoại ────────────────────────────────────────────────────── */

/*
 * 100vh trên trình duyệt di động tính cả phần thanh địa chỉ sẽ tự thu lại khi
 * cuộn, nên khung chat bị hụt đáy. 100dvh bám theo chiều cao thật.
 */
@supports (height: 100dvh) {
    .gate { min-height: 100dvh; }
    .sidebar { height: 100dvh; }
    .chat { height: calc(100dvh - 150px); }
    @media (max-width: 780px) { .chat { height: calc(100dvh - 134px); } }
    @media (max-width: 640px) { .chat { height: calc(100dvh - 106px); } }
}

/* Menu trượt: vùng chạm rộng hơn, chừa lề an toàn của máy tai thỏ */
@media (max-width: 900px) {
    .sidebar {
        width: min(292px, 84vw);
        padding-left: env(safe-area-inset-left);
    }

    .brand { padding: 16px 18px; }
    .tabs { padding: 12px 10px; gap: 4px; }
    .tab { padding: 12px; font-size: 14.5px; }
    .tab-icon { width: 19px; height: 19px; }
    .sidebar-foot { padding: 12px calc(12px + env(safe-area-inset-left)) calc(12px + env(safe-area-inset-bottom)) 12px; }
}

@media (max-width: 640px) {
    /* Lề gọn lại, nhưng vẫn né tai thỏ khi xoay ngang */
    .topbar {
        padding: 10px max(14px, env(safe-area-inset-right)) 10px max(14px, env(safe-area-inset-left));
    }

    .wrap {
        padding: 14px max(14px, env(safe-area-inset-right)) 40px max(14px, env(safe-area-inset-left));
    }

    .card { border-radius: 12px; margin-bottom: 14px; }
    .card-head { padding: 12px 14px; flex-wrap: wrap; }
    .form-card { padding: 16px 14px; }
    .grid-2 { gap: 14px; }

    /* Bộ lọc xuống hàng riêng, không chen với tiêu đề rồi cắt cụt chữ trong select */
    .filters { flex: 1 1 100%; }
    .filters select { flex: 1; min-width: 0; }

    /*
     * iOS Safari tự phóng to trang khi con trỏ vào ô nhập có cỡ chữ dưới 16px,
     * và không tự thu lại. 16px là ngưỡng duy nhất tránh được.
     */
    input[type=text], input[type=password], input[type=number], textarea, select {
        font-size: 16px;
        padding: 10px 12px;
    }

    /* Nút to hơn cho vừa đầu ngón tay */
    .btn { padding: 10px 15px; }
    .btn-sm { padding: 7px 11px; font-size: 13px; }
    .actions { flex-wrap: wrap; }

    .stats { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
    .stat { padding: 13px 14px; }
    .stat-value { font-size: 22px; }

    /*
     * Bảng 5–6 cột không thể cuộn ngang cho tử tế trên màn hình 360px. Xếp mỗi
     * hàng thành một thẻ dọc, lấy data-label (app.js gắn) làm tên cột.
     */
    .table-scroll { overflow-x: visible; }
    .table, .table tbody, .table td { display: block; width: auto; }
    .table thead { display: none; }

    /* flex column để [data-primary] nhảy lên đầu thẻ bất kể thứ tự cột gốc */
    .table tr {
        display: flex;
        flex-direction: column;
        padding: 12px 14px;
        border-bottom: 1px solid var(--border);
    }
    .table tr:last-child { border-bottom: none; }
    .table tbody tr:hover { background: none; }

    .table td {
        border: none;
        padding: 3px 0;
        font-size: 13px;
        white-space: normal;    /* đè .nowrap — trên hẹp thì xuống dòng còn hơn tràn */
    }

    .table td.mono { font-size: 12.5px; }

    .table td[data-label]::before {
        content: attr(data-label);
        display: inline-block;
        width: 96px;
        font-family: var(--sans);
        font-size: 11px;
        font-weight: 500;
        letter-spacing: .04em;
        text-transform: uppercase;
        color: var(--text-faint);
    }

    /* Ô chính đứng riêng làm tiêu đề thẻ */
    .table td[data-primary] {
        order: -1;
        padding: 0 0 8px;
        font-size: 14px;
        line-height: 1.4;
        color: var(--text);
    }

    .table td[data-actions] { order: 1; padding-top: 10px; }
    .cell-actions { flex-wrap: wrap; justify-content: flex-start; gap: 6px; }

    .empty, .loading { padding: 26px 14px; }

    /* Thông báo nổi trải ngang, tránh bị thanh cử chỉ che */
    .toasts {
        left: 12px;
        right: 12px;
        bottom: calc(12px + env(safe-area-inset-bottom));
        max-width: none;
    }

    /* Hộp thoại QR vừa khít màn hình hẹp */
    .modal { padding: 12px; align-items: start; }
    .modal-card { max-height: calc(100vh - 24px); overflow-y: auto; }
    .qr-body { padding: 16px; gap: 16px; }
    .qr-frame { width: 100%; max-width: 236px; height: auto; aspect-ratio: 1; margin: 0 auto; }
    .qr-side .btn { margin-top: 0; }

    .gate { padding: 16px; }
    .gate-card { padding: 26px 20px; }

    .chat { border-radius: 12px; }
    .chat-head { padding: 10px 12px; }
    .chat-log { padding: 12px; }
    .chat-composer { padding: 10px 12px; }
}
