/**
 * Responsive overrides — tablet & mobile only.
 * Desktop layout (min-width: 1024px) is not modified here.
 * Loaded after app.css and ui-enhancements.css.
 */

/* ==========================================================================
   TABLET: 768px – 1023px
   ========================================================================== */
@media (min-width: 768px) and (max-width: 1023.98px) {
    /* Tablet: keep content within viewport; sidebar rules stay in app.css */
    .admin-content {
        padding: 20px;
        max-width: 100%;
    }

    .page-header {
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    .page-header-actions {
        width: 100%;
        justify-content: flex-start;
    }

    .list-toolbar-row,
    .customers-toolbar-row {
        flex-wrap: wrap;
    }

    .reports-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .role-picker-grid,
    .role-list-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .permission-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .collect-loan-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .form-stack-card,
    .customer-form-card {
        max-width: 100%;
    }

    .table-responsive {
        -webkit-overflow-scrolling: touch;
    }
}

/* ==========================================================================
   MOBILE: up to 767px
   ========================================================================== */
@media (max-width: 767.98px) {
    /* Mobile: prevent horizontal page scroll */
    html,
    body {
        overflow-x: hidden;
        max-width: 100%;
    }

    .admin-wrapper,
    .admin-main,
    .admin-content {
        max-width: 100%;
        min-width: 0;
    }

    .admin-main {
        margin-left: 0 !important;
        width: 100% !important;
        /* Mobile: header only (TIMS, Fancies, etc.) */
        padding-top: 56px !important;
    }

    /* Mobile: Loan Finance app also has fixed search bar */
    .admin-wrapper:has(.mobile-search-bar) .admin-main {
        padding-top: calc(56px + 48px) !important;
    }

    .admin-content {
        padding: 12px 14px 1.25rem;
        margin-top: 0 !important;
        overflow-x: hidden;
    }

    /* Mobile: fixed search below header */
    .mobile-search-bar {
        position: fixed;
        top: 56px;
        left: 0;
        right: 0;
        z-index: 1025;
        background: var(--content-bg, #f5f7fa);
        border-bottom: 1px solid var(--border-color, #e9ecef);
        padding-top: 0.5rem !important;
        padding-bottom: 0.5rem !important;
        margin: 0 !important;
    }

    .mobile-search-bar .form-control {
        font-size: 16px; /* Mobile: prevent iOS input zoom */
    }

    .mobile-title {
        font-size: 0.9rem;
        max-width: 52vw;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Mobile: media and cards */
    img,
    video,
    canvas {
        max-width: 100%;
        height: auto;
    }

    .card,
    .app-card {
        max-width: 100%;
    }

    .card-body,
    .app-card.p-4 {
        padding: 1rem !important;
    }

    /* Mobile: page headers & toolbars stack */
    .page-header {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.75rem;
    }

    .page-header h2,
    .page-title {
        font-size: 1.25rem;
        line-height: 1.3;
    }

    .page-header-actions {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .page-header-actions .btn {
        flex: 1 1 auto;
        min-width: 0;
    }

    .list-toolbar-row,
    .customers-toolbar-row {
        flex-direction: column;
        align-items: stretch;
        gap: 0.65rem;
    }

    .list-toolbar-title,
    .customers-toolbar-title {
        width: 100%;
    }

    .list-toolbar-search,
    .customers-toolbar-search,
    .page-header-search {
        width: 100%;
        max-width: 100%;
        order: 2;
    }

    .list-toolbar-actions,
    .customers-toolbar-actions {
        width: 100%;
        justify-content: stretch;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .list-toolbar-actions .btn,
    .customers-toolbar-actions .btn,
    .list-add-btn,
    .customers-add-btn {
        flex: 1 1 calc(50% - 0.25rem);
        min-width: 0;
    }

    /* Mobile: forms */
    .form-control,
    .form-select,
    textarea.form-control {
        font-size: 16px; /* Mobile: prevent iOS zoom */
        max-width: 100%;
    }

    .form-stack-card,
    .customer-form-card,
    .login-card {
        max-width: 100% !important;
    }

    .form-actions {
        flex-direction: column;
        align-items: stretch;
    }

    .form-actions .btn {
        width: 100%;
    }

    .role-picker-grid,
    .role-list-grid,
    .permission-grid,
    .reports-grid,
    .collect-loan-grid,
    .collect-paytype-grid,
    .schedule-preview-grid {
        grid-template-columns: 1fr !important;
    }

    /* Mobile: dashboard & stats */
    .stats-grid-v2,
    .dashboard-stats-grid {
        grid-template-columns: 1fr !important;
        gap: 0.65rem;
    }

    .dashboard-hero-v2 {
        padding: 1rem !important;
    }

    .dashboard-hero-v2 .user-name {
        font-size: 1.25rem !important;
        word-break: break-word;
    }

    .todays-tasks-strip {
        gap: 0.5rem;
    }

    .task-chip {
        font-size: 0.8rem;
        padding: 0.5rem 0.75rem;
    }

    /* Mobile: reports */
    .reports-hero-inner {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }

    .reports-export-actions {
        width: 100%;
    }

    .reports-export-dropdown {
        width: 100%;
    }

    .reports-export-dropdown .btn {
        width: 100%;
    }

    /* Mobile: tables → card rows (uses data-label from app.js) */
    .table-responsive {
        overflow: visible;
        border: none;
    }

    .table-mobile-ready {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
    }

    .table-mobile-ready thead {
        display: none;
    }

    .table-mobile-ready tbody {
        display: block;
    }

    .table-mobile-ready tbody tr {
        display: block;
        background: #fff;
        border: 1px solid #e9ecef;
        border-radius: 12px;
        margin-bottom: 0.75rem;
        padding: 0.65rem 0.85rem;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
    }

    .table-mobile-ready tbody tr:hover {
        background: #f8f9ff;
    }

    .table-mobile-ready tbody td {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 0.75rem;
        padding: 0.45rem 0;
        border: none;
        white-space: normal;
        text-align: right;
        font-size: 0.875rem;
    }

    .table-mobile-ready tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.72rem;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        color: #6c757d;
        text-align: left;
        flex: 0 0 42%;
        max-width: 42%;
        padding-right: 0.35rem;
    }

    .table-mobile-ready tbody td:not([data-label])::before,
    .table-mobile-ready tbody td[data-label=""]::before {
        display: none;
    }

    .table-mobile-ready tbody td.list-avatar-cell {
        justify-content: flex-start;
        padding-bottom: 0.25rem;
        border-bottom: 1px solid #f1f3f5;
        margin-bottom: 0.25rem;
    }

    .table-mobile-ready tbody td.list-avatar-cell::before {
        display: none;
    }

    .table-mobile-ready tbody td.list-row-actions {
        justify-content: flex-end;
        padding-top: 0.35rem;
        border-top: 1px solid #f1f3f5;
        margin-top: 0.25rem;
    }

    .table-mobile-ready tbody td.list-row-actions::before {
        display: none;
    }

    .table-mobile-ready tbody td[colspan] {
        display: block;
        text-align: center;
    }

    .table-mobile-ready tbody td[colspan]::before {
        display: none;
    }

    /* Mobile: modals & dropdowns */
    .modal-dialog {
        margin: 0.75rem;
        max-width: calc(100% - 1.5rem);
    }

    .dropdown-menu {
        max-width: min(100vw - 2rem, 320px);
    }

    .notification-menu {
        max-width: min(100vw - 1.5rem, 340px);
    }

    /* Mobile: login (unauthenticated layout) */
    .login-shell {
        padding: 1rem;
        align-items: flex-start;
        padding-top: 2rem;
    }

    .login-card {
        max-width: 100%;
    }

    .login-card-inner {
        padding: 1.25rem !important;
    }

    /* Mobile: collection payment sticky footer spacing */
    .admin-content:has(.collect-sticky-summary) {
        padding-bottom: 5.5rem;
    }

    /* Mobile: searchable select dropdown */
    .searchable-select-dropdown {
        max-width: 100%;
        left: 0 !important;
        right: 0 !important;
    }
}

/* ==========================================================================
   SMALL PHONES: 360px – 430px
   ========================================================================== */
@media (max-width: 430px) {
    .admin-content {
        padding-left: 10px;
        padding-right: 10px;
    }

    .mobile-header {
        padding-left: 10px;
        padding-right: 10px;
    }

    .table-mobile-ready tbody td {
        flex-direction: column;
        align-items: flex-start;
        text-align: left;
        gap: 0.2rem;
    }

    .table-mobile-ready tbody td::before {
        max-width: 100%;
        flex: none;
    }

    .list-toolbar-actions .btn,
    .customers-toolbar-actions .btn {
        flex: 1 1 100%;
    }

    .btn-group {
        display: flex;
        flex-direction: column;
        width: 100%;
    }

    .btn-group .btn {
        width: 100%;
        border-radius: 0.375rem !important;
        margin-left: 0 !important;
    }
}

/* ==========================================================================
   VERY SMALL PHONES: below 360px
   ========================================================================== */
@media (max-width: 359.98px) {
    html {
        font-size: 15px;
    }

    .mobile-logo {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }
}
