/* ALTCP mobile UI layer — 2026-10-30.
   Mọi quy tắc nằm trong @media (max-width: 680px), nên giao diện máy tính không đổi.
   Gỡ bỏ: xóa file này và dòng <link ... mobile.css> trong template. */

@media (max-width: 680px) {
    /* ---------- Chung ---------- */
    html { -webkit-text-size-adjust: 100%; }
    body { overflow-x: hidden; }
    /* iOS tự phóng to khi ô nhập < 16px — giữ 16px để không bị zoom */
    input:not([type="checkbox"]):not([type="radio"]),
    select,
    textarea { font-size: 16px !important; }
    [hidden] { display: none !important; }

    /* ---------- Thanh điều hướng: 1 hàng cuộn ngang ---------- */
    .sidebar {
        position: relative; z-index: 30;
        display: flex !important; flex-direction: column; align-items: stretch;
        gap: 10px; height: auto; padding: 10px 12px 10px !important;
        box-shadow: 0 6px 20px rgba(7, 20, 38, .18);
    }
    .sidebar-brand { justify-content: flex-start !important; margin: 0 !important; padding: 0 !important; }
    .sidebar-logo-image { width: 104px !important; padding: 5px 8px; border-radius: 8px; }
    .sidebar-nav {
        flex: none; flex-direction: row !important; justify-content: flex-start !important;
        gap: 6px; margin: 0 -12px; padding: 0 12px 2px;
        overflow-x: auto; overscroll-behavior-x: contain; scroll-snap-type: x proximity;
        -webkit-overflow-scrolling: touch; scrollbar-width: none;
    }
    .sidebar-nav::-webkit-scrollbar { display: none; }
    .sidebar-nav a {
        flex: 0 0 auto !important; gap: 7px; min-height: 38px !important;
        padding: 8px 13px !important; border-color: rgba(255, 255, 255, .10);
        border-radius: 999px; background: rgba(255, 255, 255, .05);
        font-size: 12.5px !important; line-height: 1.2; white-space: nowrap;
        scroll-snap-align: start;
    }
    .sidebar-nav a:hover { transform: none; }
    .sidebar-nav a.active { border-color: rgba(126, 174, 255, .45); background: rgba(59, 130, 246, .32); box-shadow: none; }
    .sidebar-nav svg { width: 16px !important; }

    /* ---------- Phần đầu trang ---------- */
    .dashboard-main { padding: 18px 12px 96px !important; }
    .topbar { margin-bottom: 16px !important; }
    .topbar h1 { font-size: 25px !important; line-height: 1.15; }
    .topbar .subtitle { font-size: 13px; }
    .topbar-actions { gap: 8px; }
    .account-identity { min-width: 0; }
    .account-identity strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    /* ---------- Thẻ hạn mức: gọn thành từng dòng ---------- */
    .commercial-grid { gap: 8px !important; margin: 0 0 14px !important; }
    .commercial-grid .summary-card {
        display: flex; align-items: center; justify-content: space-between; gap: 12px;
        padding: 13px 16px !important; border-radius: 14px;
    }
    .commercial-grid .summary-card span { font-size: 13px; }
    .commercial-grid .summary-card strong { margin: 0 !important; font-size: 18px !important; text-align: right; }

    .token-promo-card { padding: 18px 16px !important; margin-bottom: 14px; }
    .token-promo-card h2 { font-size: 20px !important; line-height: 1.3; }
    .token-promo-card p { font-size: 13.5px; }
    .promo-actions { width: 100%; }
    .promo-actions a { flex: 1 1 100%; justify-content: center; text-align: center; }

    /* ---------- Danh sách API key ---------- */
    .api-management-panel { padding: 18px 16px !important; }
    .api-management-panel h2 { font-size: 20px; }
    .inline-key-form button { min-height: 46px; }
    .managed-key-row { gap: 10px !important; }
    .managed-key-row strong { overflow-wrap: anywhere; }

    /* ---------- Bộ lọc & chỉ số ---------- */
    .filter-panel { padding: 14px !important; }
    .filter-button { min-height: 38px; font-size: 12.5px; }
    .date-filter { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important; gap: 8px !important; }
    .date-filter label { min-width: 0 !important; }
    .date-filter input { width: 100% !important; min-width: 0 !important; max-width: 100%; padding: 8px 8px !important; }
    .member-date-filter input { min-width: 0 !important; max-width: 100%; }
    .metric-grid { grid-template-columns: 1fr 1fr !important; gap: 8px !important; }
    .metric-card { min-width: 0; overflow: hidden; min-height: 0 !important; padding: 13px !important; gap: 10px; flex-direction: column; align-items: flex-start; }
    .metric-card .metric-icon { width: 34px !important; height: 34px !important; }
    .metric-card > div { min-width: 0; max-width: 100%; }
    .metric-card span { font-size: 11.5px; }
    .metric-card strong { font-size: 16px !important; overflow-wrap: anywhere; line-height: 1.25; }
    .metric-card small { font-size: 10.5px; }
    .card-heading { padding: 16px !important; gap: 10px; }
    .card-heading h2 { font-size: 19px; }

    /* ---------- Bảng → thẻ (cards) ---------- */
    .table-wrap table thead { display: none; }
    .table-wrap table,
    .table-wrap table tbody,
    .table-wrap table tfoot { display: block; width: 100%; min-width: 0 !important; }
    .table-wrap table tr {
        display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px;
        padding: 14px 16px; border-bottom: 1px solid var(--line, #e5e7eb);
    }
    .table-wrap table td,
    .table-wrap table th { display: block; min-width: 0; padding: 0 !important; border: 0 !important; text-align: left; white-space: normal; }
    .table-wrap table td.empty,
    .table-wrap table td[colspan] { grid-column: 1 / -1; padding: 18px 4px !important; text-align: center; }
    .table-wrap table td::before,
    .table-wrap table tfoot th::before {
        display: block; margin-bottom: 3px; color: var(--muted, #64748b);
        font-size: 10.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
    }

    /* Chi phí theo API (bảng không class ở trang tổng quan) */
    .table-wrap > table:not([class]) td:nth-child(1) { grid-column: 1 / -1; }
    .table-wrap > table:not([class]) td:nth-child(2)::before { content: "Số request"; }
    .table-wrap > table:not([class]) td:nth-child(3)::before { content: "Chi phí"; }
    .table-wrap > table:not([class]) td:nth-child(3) { text-align: right; }
    .table-wrap > table:not([class]) td:nth-child(3)::before { text-align: right; }
    .table-wrap > table:not([class]) tfoot tr { background: var(--surface-soft, #f8fafc); font-weight: 700; }
    .table-wrap > table:not([class]) tfoot th:nth-child(1) { grid-column: 1 / -1; }
    .table-wrap > table:not([class]) tfoot th:nth-child(2)::before { content: "Tổng request"; }
    .table-wrap > table:not([class]) tfoot th:nth-child(3) { text-align: right; }
    .table-wrap > table:not([class]) tfoot th:nth-child(3)::before { content: "Tổng chi phí"; }
    .table-wrap .api-cell { min-width: 0; }
    .table-wrap .api-cell strong { overflow-wrap: anywhere; }

    /* Nhật ký request (trang tổng quan: 7 cột) */
    .activity-table-wrap { overflow: visible !important; }
    .activity-table { min-width: 0 !important; }
    .activity-table tr { grid-template-columns: 1fr auto !important; }
    .activity-table td:nth-child(2) { text-align: right; }
    .activity-table td:nth-child(3) { grid-column: 1 / -1; }
    .activity-table td small { overflow-wrap: anywhere; }
    .activity-table td:nth-child(4) code { white-space: normal; overflow-wrap: anywhere; }
    .activity-table td:nth-child(5) { text-align: right; align-self: center; }
    .activity-table td:nth-child(6)::before { content: "Token vào / ra"; }
    .activity-table td:nth-child(7) { text-align: right; }
    .activity-table td:nth-child(7)::before { content: "Chi phí"; }

    /* Lịch sử request của 1 người dùng (trang quản lý: 8 cột) */
    #request-history .activity-table td:nth-child(3) { grid-column: auto; }
    #request-history .activity-table td:nth-child(3)::before { content: "API"; }
    #request-history .activity-table td:nth-child(4) { text-align: right; }
    #request-history .activity-table td:nth-child(4)::before { content: "Model"; }
    #request-history .activity-table td:nth-child(5) { text-align: left; }
    #request-history .activity-table td:nth-child(5) code { white-space: normal; overflow-wrap: anywhere; }
    #request-history .activity-table td:nth-child(6) { text-align: right; align-self: center; }
    #request-history .activity-table td:nth-child(6)::before { content: none; }
    #request-history .activity-table td:nth-child(7) { text-align: left; }
    #request-history .activity-table td:nth-child(7)::before { content: "Token vào / ra"; }
    #request-history .activity-table td:nth-child(8) { text-align: right; }
    #request-history .activity-table td:nth-child(8)::before { content: "Chi phí"; }

    /* Chi phí theo thành viên (trang quản lý: 9 cột) */
    .member-cost-scroll { max-height: none !important; overflow: visible !important; }
    .member-cost-table { min-width: 0 !important; }
    .member-cost-table td:nth-child(1),
    .member-cost-table td:nth-child(2),
    .member-cost-table td:nth-child(9) { grid-column: 1 / -1; }
    .member-cost-table td:nth-child(2)::before { content: "API được cấp"; }
    .member-cost-table td:nth-child(3)::before { content: "Hạn mức"; }
    .member-cost-table td:nth-child(4)::before { content: "Đã sử dụng"; }
    .member-cost-table td:nth-child(5)::before { content: "Còn lại"; }
    .member-cost-table td:nth-child(6)::before { content: "Tỷ lệ dùng"; }
    .member-cost-table td:nth-child(7)::before { content: "Trạng thái"; }
    .member-cost-table td:nth-child(8)::before { content: "Chi phí kỳ chọn"; }
    .member-cost-table td:nth-child(4),
    .member-cost-table td:nth-child(6),
    .member-cost-table td:nth-child(8) { text-align: right; }
    .member-cost-table .usage-progress { justify-content: flex-end; }
    .member-cost-table .table-actions { display: flex; flex-wrap: wrap; gap: 6px; }
    .member-cost-table .table-actions > a,
    .member-cost-table .table-actions > form { flex: 1 1 calc(50% - 6px); }
    .member-cost-table .table-actions a,
    .member-cost-table .table-actions button { display: flex; align-items: center; justify-content: center; width: 100%; min-height: 38px; }

    /* Ô số trong form quản trị (đang chưa có kiểu) */
    .customer-form input[type="number"] {
        width: 100%; min-height: 46px; padding: 11px 13px;
        border: 1px solid var(--line-strong, #cbd5e1); border-radius: 10px; background: #fbfdff; color: var(--text, #0f172a);
    }

    /* Thẻ thống kê coupon: 2 cột cho gọn */
    .coupon-stat-grid { grid-template-columns: 1fr 1fr !important; gap: 8px !important; }
    .coupon-stat-grid article { min-width: 0; min-height: 0 !important; padding: 14px !important; }
    .coupon-stat-grid strong { font-size: 26px !important; }

    /* ---------- Nút Zalo nổi: thu gọn để không che nội dung ---------- */
    .zalo-contact { right: 12px !important; bottom: 14px !important; min-height: 0 !important; padding: 5px !important; border-radius: 50% !important; }
    .zalo-contact__copy { display: none !important; }
    .zalo-contact__icon { width: 44px !important; height: 44px !important; border-radius: 50% !important; }

    /* ---------- Trang công khai (landing / đăng nhập / đăng ký / mua) ---------- */
    .login-page { padding: 12px 12px 84px !important; }
    .login-card { min-height: 0 !important; padding: 28px 20px !important; }
    .login-heading h2 { font-size: 24px !important; }
}

@media (max-width: 380px) {
    .metric-grid { grid-template-columns: 1fr !important; }
    .commercial-grid .summary-card strong { font-size: 16px !important; }
    .sidebar-nav a { padding: 8px 11px !important; font-size: 12px !important; }
}
