/* Lớp responsive dùng chung cho mobile + PWA. Nạp qua pwa_head.jspf. */

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

.mobile-nav-toggle,
.mobile-nav-backdrop {
    display: none;
}

@media (max-width: 768px), (hover: none) and (pointer: coarse) and (max-width: 1024px) {
    /* clip (không phải hidden): hidden trên cả html lẫn body biến body thành vùng cuộn riêng, chặn cuộn trang */
    html,
    body {
        overflow-x: clip;
        max-width: 100%;
    }

    body {
        -webkit-tap-highlight-color: rgba(102, 126, 234, 0.15);
    }

    img,
    video,
    canvas,
    iframe {
        max-width: 100%;
    }

    /* ===== Sidebar thành ngăn kéo trượt ===== */
    html body .sidebar,
    html body .sidebar:hover {
        width: 272px !important;
        max-width: 85vw;
        transform: translateX(-105%);
        transition: transform 0.28s ease !important;
        z-index: 1050 !important;
        /* 100vh trên iOS Safari cao hơn vùng nhìn thấy → mục cuối bị thanh công cụ che */
        top: 0 !important;
        bottom: 0 !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        /* nền đặc: backdrop-filter + transform trên iOS Safari dễ render trong suốt, lộ trang phía sau */
        background: linear-gradient(145deg, #0f172a 0%, #1e293b 35%, #334155 70%, #1e293b 100%) !important;
        -webkit-backdrop-filter: none !important;
        backdrop-filter: none !important;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        overscroll-behavior: contain;
    }

    html body .sidebar.mobile-open {
        transform: translateX(0);
    }

    html body .sidebar .sidebar-nav a span {
        opacity: 1 !important;
    }

    html body .sidebar .sidebar-nav a,
    html body .sidebar:hover .sidebar-nav a {
        justify-content: flex-start;
        padding: 14px 20px !important;
        margin: 0 10px !important;
    }

    html body .sidebar-toggle-btn {
        display: none !important;
    }

    .mobile-nav-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.5);
        z-index: 1045;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.28s ease, visibility 0.28s ease;
    }

    .mobile-nav-backdrop.show {
        opacity: 1;
        visibility: visible;
    }

    html.mobile-nav-locked {
        overflow: hidden;
    }

    /* ===== Nội dung chiếm toàn bộ chiều ngang ===== */
    /* header.js/user_header.js chèn .header:hover { margin-left: 240px !important } — trên iOS hover bị "dính" khi chạm */
    html body .main-content,
    html body .header,
    html body .header:hover {
        margin-left: 0 !important;
        width: auto !important;
        max-width: 100% !important;
    }

    html body .main-content {
        padding: 12px 12px calc(16px + env(safe-area-inset-bottom)) !important;
    }

    /* Flex item mặc định không co nhỏ hơn nội dung (bảng rộng) → đẩy tràn cả trang */
    html body .d-flex > .flex-grow-1,
    html body .d-flex > .main-content {
        min-width: 0;
    }

    /* Hàng nút / badge / tiêu đề + nút: cho xuống dòng thay vì tràn */
    html body .main-content .d-flex:has(> .btn + .btn),
    html body .main-content .d-flex:has(> .badge + .badge),
    html body .main-content .d-flex.justify-content-between:has(> .btn),
    html body .main-content .d-flex.justify-content-between:has(> .filter-section),
    html body .main-content .d-flex:has(> h2, > h3, > h4, > h5) {
        flex-wrap: wrap;
        row-gap: 8px;
    }

    /* ===== Header gọn ===== */
    html body .header {
        min-height: 56px !important;
        height: auto !important;
        padding: calc(6px + env(safe-area-inset-top)) 10px 6px !important;
        gap: 8px;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        align-items: center !important;
    }

    html body .header > :first-child {
        min-width: 0;
        flex: 1 1 auto;
        gap: 10px !important;
    }

    html body .header > :last-child {
        flex: 0 0 auto;
        margin: 0 !important;
        width: auto !important;
    }

    html body .header #pageTitle {
        font-size: 1.05rem !important;
        display: block;
        max-width: 48vw;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    html body .header .gap-4 {
        gap: 6px !important;
    }

    html body .header .dropdown-toggle {
        padding: 4px !important;
    }

    html body .header .dropdown-toggle > span,
    html body .header .dropdown-toggle::after {
        display: none !important;
    }

    html body .header .avatar {
        width: 36px !important;
        height: 36px !important;
    }

    html body .header .notification-bell {
        padding: 8px !important;
    }

    html body .header .notification-bell .fs-4 {
        font-size: 1.15rem !important;
    }

    .mobile-nav-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex: 0 0 auto;
        width: 40px;
        height: 40px;
        border: 1px solid rgba(102, 126, 234, 0.25);
        border-radius: 12px;
        background: rgba(255, 255, 255, 0.7);
        color: #4f46e5;
        font-size: 1.15rem;
        cursor: pointer;
    }

    .mobile-nav-toggle.floating {
        position: fixed;
        top: calc(10px + env(safe-area-inset-top));
        left: 10px;
        z-index: 1040;
        box-shadow: 0 4px 14px rgba(0, 0, 0, 0.15);
    }

    /* ===== Bảng: cuộn ngang thay vì tràn trang ===== */
    .table-responsive,
    .mobile-table-wrap {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
    }

    .mobile-table-wrap > table {
        min-width: 100%;
    }

    html body .table tbody tr:hover {
        transform: none !important;
    }

    /* ===== Bảng dạng thẻ (table[data-mobile-cards]) ===== */
    html body table[data-mobile-cards],
    html body table[data-mobile-cards] > tbody,
    html body table[data-mobile-cards] > tbody > tr,
    html body table[data-mobile-cards] > tbody > tr > td {
        display: block;
        width: 100% !important;
        min-width: 0 !important;
    }

    html body table[data-mobile-cards] {
        border: 0 !important;
        background: transparent !important;
        margin: 0 !important;
    }

    html body table[data-mobile-cards] > thead {
        display: none !important;
    }

    html body .table-responsive:has(> table[data-mobile-cards]),
    html body .mobile-table-wrap:has(> table[data-mobile-cards]) {
        overflow: visible !important;
        box-shadow: none !important;
        border: 0 !important;
        background: transparent !important;
    }

    html body table[data-mobile-cards] > tbody > tr {
        background: #fff !important;
        border: 1px solid #e2e8f0 !important;
        border-radius: 14px;
        padding: 4px 14px;
        margin-bottom: 12px;
        box-shadow: 0 2px 8px rgba(15, 23, 42, 0.05);
    }

    html body table[data-mobile-cards] > tbody > tr > td {
        line-height: 1.9;
        max-width: none !important;
        padding: 9px 0 !important;
        border: 0 !important;
        border-bottom: 1px dashed #e9eef5 !important;
        background: transparent !important;
        box-shadow: none !important;
        text-align: right !important;
        font-size: 0.92rem;
    }

    html body table[data-mobile-cards] > tbody > tr > td:last-child {
        border-bottom: 0 !important;
    }

    html body table[data-mobile-cards] > tbody > tr > td::before {
        content: attr(data-label);
        float: left;
        margin-right: 12px;
        font-size: 0.8rem;
        font-weight: 600;
        color: #64748b;
        text-align: left;
    }

    html body table[data-mobile-cards] > tbody > tr > td::after {
        content: "";
        display: table;
        clear: both;
    }

    html body table[data-mobile-cards] > tbody > tr > td[data-label=""]::before {
        content: none;
    }

    html body table[data-mobile-cards] > tbody > tr > td.mc-title {
        text-align: left !important;
        font-size: 1rem;
        font-weight: 700;
        color: #1e293b;
        padding: 12px 0 10px !important;
        border-bottom: 1px solid #e2e8f0 !important;
    }

    html body table[data-mobile-cards] > tbody > tr > td.mc-title::before {
        content: none;
    }

    html body table[data-mobile-cards] > tbody > tr > td.mc-hide {
        display: none !important;
    }

    html body table[data-mobile-cards] > tbody > tr > td.mc-full {
        text-align: center !important;
        border-bottom: 0 !important;
        padding: 16px 0 !important;
    }

    html body table[data-mobile-cards] > tbody > tr > td.mc-full::before {
        content: none;
    }

    html body table[data-mobile-cards] .text-truncate {
        max-width: 60vw !important;
        vertical-align: top;
    }

    html body table[data-mobile-cards] td > .btn,
    html body table[data-mobile-cards] td > .btn-group {
        margin: 0;
    }

    /* ===== Form: cỡ chữ 16px để iOS không tự phóng to khi nhập ===== */
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
    select,
    textarea {
        font-size: 16px !important;
    }

    /* ===== Modal & hộp thoại ===== */
    html body .modal-dialog {
        max-width: calc(100vw - 16px) !important;
        margin: 8px auto !important;
    }

    html body .modal-content {
        border-radius: 16px;
    }

    html body .modal-footer {
        flex-wrap: wrap;
        gap: 8px;
    }

    html body .modal-footer > .btn {
        flex: 1 1 auto;
        margin: 0;
    }

    html body .swal2-popup {
        width: calc(100vw - 24px) !important;
        max-width: 480px;
        padding: 1.25em 1em !important;
    }

    html body .dropdown-menu {
        max-width: calc(100vw - 20px);
    }

    /* ===== Tiêu đề trang, khối nội dung ===== */
    html body .page-header {
        padding: 18px !important;
        border-radius: 16px !important;
        margin-bottom: 16px !important;
    }

    html body .page-header h2,
    html body .main-content h2 {
        font-size: 1.25rem !important;
    }

    html body .main-box,
    html body .card {
        border-radius: 14px;
    }

    html body .main-box {
        padding: 16px !important;
    }

    html body .row {
        --bs-gutter-x: 0.75rem;
    }

    html body .nav-tabs,
    html body .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    html body .nav-tabs .nav-link,
    html body .nav-pills .nav-link {
        white-space: nowrap;
    }

    html body .btn-group {
        flex-wrap: wrap;
    }
}

@media (max-width: 768px), (hover: none) and (pointer: coarse) and (max-width: 1024px) {
    /* Thẻ thống kê: 2 cột thay vì xếp chồng 1 cột */
    html body .row > [class*="col-"]:has(> .stat-card) {
        flex: 0 0 50%;
        max-width: 50%;
    }

    html body .stat-card {
        padding: 14px 10px !important;
        border-radius: 14px !important;
    }

    html body .stat-card .stat-icon {
        font-size: 1.6rem !important;
        margin-bottom: 4px !important;
    }

    html body .stat-card .stat-value {
        font-size: 1.4rem !important;
    }

    html body .stat-card .stat-label {
        font-size: 0.8rem !important;
    }
}
