/* ========== Mobile & touch — global responsive layer ========== */

/* Safer touch scrolling for all data grids */
.table-responsive,
.projects-table-wrap,
.rk-hscroll {
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
}

.rk-hscroll {
    position: relative;
    margin-bottom: 0.25rem;
}

/* Scroll hint (shown on touch devices via JS class) */
.rk-hscroll.rk-show-scroll-hint::after {
    content: "Swipe →";
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    font-size: 0.65rem;
    font-weight: 600;
    color: #64748b;
    background: linear-gradient(90deg, transparent, #f8fafc 40%, #f8fafc);
    padding: 0.25rem 0.5rem 0.25rem 2rem;
    border-radius: 4px 0 0 4px;
}

/* Wide grids — horizontal scroll, minimum table width */
.rk-wide-grid,
.table-responsive > table.projects-table,
.table-responsive > table.construction-tracking-grid,
.table-responsive > .construction-tracking-grid,
.table-responsive > table.table-bordered.table-hover:not(.rk-mobile-stack-table) {
    min-width: 720px;
}

.table-responsive > table.table-sm:not(.rk-mobile-stack-table) {
    min-width: 480px;
}

.table-responsive > table.rk-admin-data-table.rk-mobile-stack-table,
.client-companies-table-wrap > table.rk-mobile-stack-table,
.table-responsive > table.lead-payment-stages-table.rk-mobile-stack-table,
.table-responsive > table.lead-saved-proposals-table.rk-mobile-stack-table,
.table-responsive > table.lead-negotiation-table.rk-mobile-stack-table,
.cost-estimator-table-wrap > table.cost-estimator-table.rk-mobile-stack-table,
.package-inclusions-table-wrap > table.package-inclusions-table--matrix {
    min-width: 0 !important;
}

/* Stack simple tables as cards on small screens */
@media (max-width: 767.98px) {
    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    .page-wrapper,
    .main-content.page-wrapper {
        padding: 0.75rem !important;
    }

    .page-title {
        font-size: 1.25rem;
    }

    .page-header {
        flex-direction: column;
        align-items: stretch !important;
    }

    .page-header .btn,
    .page-header .d-flex {
        width: 100%;
        justify-content: stretch;
    }

    .page-header .btn-add {
        width: 100%;
        justify-content: center;
    }

    /* Filter toolbars */
    .uniform-filter-card,
    .admin-filter-card,
    .dashboard-filter-card {
        padding: 0.75rem 1rem;
    }

    .uniform-filter-card .row > [class*="col-auto"],
    .dashboard-filter-card .row > [class*="col-auto"] {
        width: 100%;
        max-width: 100%;
    }

    .uniform-filter-card .form-select,
    .uniform-filter-card .form-control,
    .dashboard-filter-card .form-select,
    .dashboard-filter-card .form-control {
        width: 100% !important;
        max-width: 100% !important;
    }

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

    /* iOS: prevent zoom on input focus */
    .form-control,
    .form-select,
    textarea.form-control {
        font-size: 16px;
    }

    .rk-form-grid,
    .rk-form-auto {
        grid-template-columns: 1fr;
    }

    .page-wrapper .rk-form [class*="col-md-"] {
        flex: 0 0 100%;
        max-width: 100%;
    }

    /* Modals use full width on phone */
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }

    .modal-dialog.modal-xl,
    .modal-dialog.modal-lg {
        max-width: calc(100% - 1rem);
    }

    .modal-body {
        padding: 0.75rem 1rem;
    }

    .modal-footer {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .modal-footer .btn {
        flex: 1 1 auto;
        min-height: 44px;
    }

    /* Touch-friendly controls */
    .btn-sm {
        min-height: 38px;
        padding: 0.35rem 0.65rem;
    }

    .btn {
        min-height: 42px;
    }

    .nav-link-top,
    .navbar-toggler-top {
        min-height: 44px;
        min-width: 44px;
    }

    /* Dashboard / P&L cards */
    .dashboard-summary-row .col-md-6,
    .dashboard-type-revenue-row .col-md-4,
    .company-pl-card .col-6 {
        flex: 0 0 100%;
        max-width: 100%;
    }

    .construction-dashboard-finance .col-6 {
        flex: 0 0 50%;
        max-width: 50%;
    }

    /* Construction tracking stat cards — horizontal scroll row */
    .construction-tracking-page .row.g-3.mb-4 {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 0.5rem;
        margin-right: -0.75rem;
        padding-right: 0.75rem;
    }

    .construction-tracking-page .row.g-3.mb-4 > [class*="col-"] {
        flex: 0 0 75%;
        max-width: 75%;
    }

    /* Task site cost tabs */
    .task-site-cost-panel .nav-tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-bottom: 1px solid #dee2e6;
    }

    .task-site-cost-panel .nav-tabs .nav-link {
        white-space: nowrap;
        font-size: 0.8rem;
        padding: 0.5rem 0.65rem;
    }

    /* Daily form rows */
    .construction-daily-add-panel .row > [class*="col-"] {
        flex: 0 0 100%;
        max-width: 100%;
    }

    /* Projects — mobile-first */
    .projects-page {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }

    .projects-view-toolbar {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.65rem;
    }

    .projects-view-toolbar > .d-flex {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .projects-view-toolbar .form-select-sm {
        flex: 1;
        min-width: 4.5rem;
    }

    .projects-view-toggle {
        width: 100%;
        display: flex;
    }

    .projects-view-toggle .btn {
        flex: 1;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .projects-mobile-filters-inner {
        display: grid;
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }

    .projects-mobile-filters {
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 10px;
        padding: 0.75rem;
    }

    .projects-grid {
        grid-template-columns: 1fr;
        gap: 0.85rem;
        padding: 0.75rem;
    }

    .projects-view-cards--mobile-fallback .projects-grid {
        padding-top: 0;
    }

    .project-card {
        padding: 1rem;
    }

    .project-card-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
    }

    .project-card-actions .btn-primary.project-card-btn {
        grid-column: 1 / -1;
    }

    .project-card-btn {
        min-height: 44px;
        font-size: 0.9rem;
        width: 100%;
        justify-content: center;
    }

    .projects-card .table-responsive.projects-table-wrap {
        display: none;
    }

    /* Stacked table cards */
    table.rk-mobile-stack-table thead {
        display: none;
    }

    table.rk-mobile-stack-table tbody tr {
        display: block;
        margin-bottom: 0.75rem;
        border: 1px solid #e2e8f0;
        border-radius: var(--bb-radius, 0.5rem);
        background: #fff;
        box-shadow: var(--bb-shadow-sm, 0 1px 3px rgba(0,0,0,.08));
        padding: 0.35rem 0;
    }

    table.rk-mobile-stack-table tbody tr:nth-of-type(odd) {
        background: #fff;
    }

    table.rk-mobile-stack-table tbody td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 0.75rem;
        padding: 0.45rem 0.75rem;
        border: none;
        border-bottom: 1px solid #f1f5f9;
        text-align: right !important;
    }

    table.rk-mobile-stack-table tbody td:last-child {
        border-bottom: none;
    }

    table.rk-mobile-stack-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.75rem;
        color: #64748b;
        text-align: left;
        flex: 0 0 42%;
        max-width: 42%;
    }

    table.rk-mobile-stack-table tbody td.text-end,
    table.rk-mobile-stack-table tbody td .text-end {
        text-align: right !important;
    }

    table.rk-mobile-stack-table tbody td .btn {
        min-height: 36px;
    }

    .admin-grid-card .table-responsive,
    .vendor-grid-card .table-responsive,
    .client-companies-table-wrap,
    .task-config-table-wrap,
    .payment-schedule-config-page .table-responsive,
    .admin-settings-grid .table-responsive,
    .create-lead-page .lead-payment-stages-wrap,
    .create-lead-page .lead-saved-proposals-wrap,
    .lead-negotiation-table-wrap,
    .cost-estimator-table-wrap,
    .cost-estimator-table-wrap--scroll,
    .package-inclusions-table-wrap {
        overflow: visible;
    }

    table.rk-admin-data-table.rk-mobile-stack-table {
        min-width: 0 !important;
    }

    table.rk-admin-data-table.rk-mobile-stack-table tbody td.text-end {
        flex-direction: column;
        align-items: stretch !important;
        text-align: left !important;
    }

    table.rk-admin-data-table.rk-mobile-stack-table tbody td.text-end .btn {
        width: 100%;
        margin-top: 0.2rem;
    }

    /* Hide filter row tables from stack — scroll only */
    table.rk-wide-grid thead {
        display: table-header-group;
    }

    table.rk-wide-grid tbody tr {
        display: table-row;
    }

    table.rk-wide-grid tbody td {
        display: table-cell;
    }

    table.rk-wide-grid tbody td::before {
        content: none;
    }

    /* Admin grid filter row — hide on very small screens inside scroll */
    .projects-table thead .grid-filter-row th {
        min-width: 5rem;
    }

    /* Pagination */
    .grid-pagination {
        flex-wrap: wrap;
        justify-content: center;
        gap: 0.25rem;
    }

    .grid-pagination .page-link {
        min-width: 40px;
        min-height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Alerts with buttons */
    .alert.d-flex {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.5rem;
    }

    /* Lead / create lead fixed modals */
    .modal-dialog[style*="max-width"] {
        max-width: calc(100% - 1rem) !important;
    }

    .project-edit-modal-dialog {
        max-width: 100% !important;
        margin: 0;
        min-height: 100%;
    }

    .project-edit-modal-content {
        min-height: 100dvh;
        border-radius: 0;
    }

    .project-edit-modal-body {
        max-height: none;
    }

    .app-wrapper {
        overflow-x: hidden;
    }

    /* Revenue report — mobile polish */
    .revenue-report-page {
        padding-left: 0.65rem;
        padding-right: 0.65rem;
    }

    .revenue-report-header .page-title {
        font-size: 1.2rem;
    }

    .revenue-report-filters {
        padding: 0.85rem !important;
        border-radius: 0.75rem;
    }

    .revenue-report-filter-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
    }

    .revenue-report-filter-actions .btn-primary {
        grid-column: 1 / -1;
    }

    .revenue-report-filter-actions .btn-outline-success {
        grid-column: 1 / -1;
    }

    .revenue-report-kpis .col-6 {
        padding-left: 0.35rem;
        padding-right: 0.35rem;
    }

    .revenue-kpi-card {
        flex-direction: column;
        gap: 0.45rem;
        padding: 0.7rem 0.75rem;
        min-height: 100%;
    }

    .revenue-kpi-card__icon {
        width: 2rem;
        height: 2rem;
        flex-basis: 2rem;
        font-size: 0.95rem;
    }

    .revenue-kpi-card__value {
        font-size: 0.92rem;
    }

    .revenue-kpi-card__hint {
        font-size: 0.62rem;
    }

    .revenue-ledger-card__header {
        flex-direction: column;
        align-items: flex-start;
    }

    .revenue-ledger-card__header .badge {
        align-self: flex-start;
    }

    .revenue-ledger-pagination {
        flex-direction: column;
        align-items: stretch;
    }

    .revenue-ledger-pagination .btn-group {
        width: 100%;
    }

    .revenue-ledger-pagination .btn-group .btn {
        flex: 1;
        min-height: 44px;
    }
}

@media (max-width: 575.98px) {
    .construction-dashboard-finance .col-6 {
        flex: 0 0 100%;
        max-width: 100%;
    }

    .dashboard-summary-card .card-body h2 {
        font-size: 1.35rem;
    }
}

/* Prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
    .table-responsive {
        scroll-behavior: auto;
    }
}
