/*!
 * assets/mobile.css — global mobile layer for the whole app
 *
 * Loaded by nav.php (every signed-in page) and by the standalone pages (login, activate, …).
 * Everything lives inside phone / touch media queries, so desktop layouts are untouched.
 *
 *   1. Touch devices   bigger tap targets, no 300ms delay, no iOS auto-zoom on inputs
 *   2. Phones ≤768px   forms, grids, toolbars, tabs, modals, charts
 *   3. Tables          list tables become labelled cards; statement tables scroll sideways
 *                      (assets/mobile.js marks them: .m-stack / .m-scroll)
 *   4. Bottom bar      thumb-reachable quick navigation (rendered by nav.php)
 *   5. Safe areas      iPhone notch / home indicator (needs viewport-fit=cover)
 *
 * Opt-outs:  <table data-stack="off">  keep it as a scrolling table
 *            <table data-stack="on">   force card layout
 *            any element with [data-no-mobile] is left alone by the grid rules
 */

:root {
    --m-safe-top: env(safe-area-inset-top, 0px);
    --m-safe-bottom: env(safe-area-inset-bottom, 0px);
    --m-safe-left: env(safe-area-inset-left, 0px);
    --m-safe-right: env(safe-area-inset-right, 0px);
    --m-bn-h: 58px;                 /* bottom bar height */
}

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
img, svg, video, canvas { max-width: 100%; }
img, video { height: auto; }

/* ════════════ 1. TOUCH DEVICES (phones + tablets) ════════════ */
@media (hover: none) and (pointer: coarse) {
    a, button, summary, label, select, input, textarea, [role="button"], [onclick] {
        -webkit-tap-highlight-color: rgba(79, 70, 229, .14);
    }
    button, [type="button"], [type="submit"], [type="reset"], select, summary, a.btn, .btn, .act, label.btn {
        touch-action: manipulation;           /* no double-tap zoom / 300ms delay */
    }
    button, [type="button"], [type="submit"], [type="reset"], a.btn, .btn { min-height: 40px; }
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]):not([type="hidden"]),
    select { min-height: 42px; }
    input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; }
    button, [type="button"], [type="submit"], .btn, .act { min-width: 36px; }
    summary { padding-block: 8px; cursor: pointer; }
    /* links in tables / lists / cards get a comfortable hit area without changing the layout */
    td a:not(.btn):not(.act), li a:not(.btn), .row a:not(.btn), .prow, a.oid, a.more, .more > summary {
        padding-block: 6px; margin-block: -6px; display: inline-block;
    }
    a.oid { padding-inline: 4px; margin-inline: -4px; }
}

/* ════════════ 2. PHONES ════════════ */
@media screen and (max-width: 768px) {

    /* — Breathing room for the bottom bar + notch (the bar's height is 0 on pages that don't have one) — */
    body:not(.has-bottomnav) { --m-bn-h: 0px; }
    body {
        padding-bottom: calc(var(--m-bn-h) + var(--m-safe-bottom) + 14px) !important;
        padding-left: var(--m-safe-left); padding-right: var(--m-safe-right);
        overflow-x: hidden;                    /* a stray element can never create sideways page scroll */
    }

    /* — Grid / flex children may shrink below their content (otherwise a wide table inside a card
         stretches the whole page past the screen and the phone zooms out) — */
    [class*="grid"] > *, [style*="display:grid"] > *, [style*="display: grid"] > *,
    [class*="flex"] > *, [style*="display:flex"] > *, [style*="display: flex"] > *,
    .card, .tbl-card, .panel, .wrap > *, main > * { min-width: 0; }
    .card, .tbl-card, .panel { max-width: 100%; }
    .tbl-wrap, .table-wrap, .table-responsive { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

    /* — Text that must never push the layout wider than the screen — */
    td, th, .card, .cust, .prod, h1, h2, h3, h4, p, label, .badge, .chip { overflow-wrap: anywhere; }
    pre, code { white-space: pre-wrap; word-break: break-word; }
    /* …but short labels (buttons, links styled as buttons, pills) keep their words whole */
    button, .btn, a.btn, .act, summary, .chip, .badge, .pill, label.btn, [type="button"], [type="submit"] {
        overflow-wrap: normal; word-break: normal; hyphens: none; min-width: min(max-content, 100%);
    }
    h1 { font-size: clamp(1.15rem, 5.2vw, 1.5rem); line-height: 1.25; }

    /* — Forms: full width, 16px text so iOS doesn't zoom when a field is focused — */
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]):not([type="hidden"]),
    select, textarea {
        font-size: 16px !important;
        max-width: 100%;
        box-sizing: border-box;
    }
    .form-group input, .form-group select, .form-group textarea, .fg input, .fg select, .fg textarea,
    .fld input, .fld select, .fld textarea, form .form-control { width: 100%; }
    form .btn-primary[type="submit"], form button.btn-primary[type="submit"], form .submit-btn, form .btn-save { width: 100%; justify-content: center; }
    textarea { min-height: 84px; }
    input[type="date"], input[type="month"], input[type="time"], input[type="datetime-local"] { min-width: 0; -webkit-appearance: none; appearance: none; }

    /* — Grids collapse — */
    .form-grid, .form-grid-2, .filters-grid, .billing-grid, .doc-grid, .detail-grid, .grid-2, .grid-3, .grid-5,
    .sub-grid, .budget-grid, .as-grid, .vd-grid, .slip-grid, .signature-grid, .doc-footer-grid, .groups-grid,
    .chart-grid, .charts-grid, .bottom-grid, .pr-exp-grid, .pr-grid, .fg-4 { grid-template-columns: 1fr !important; }
    .kpis, .kpi-grid, .kpi-row, .kpi-strip, .stats-grid, .stat-grid, .summary-grid, .metrics-grid, .financials-grid,
    .dashboard-grid, .vd-stats, .vendor-stat-chips, .modal-mini-stats {
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)) !important;
    }
    /* grids declared inline (style="display:grid;grid-template-columns:…") */
    [style*="grid-template-columns"]:not([data-no-mobile]) {
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)) !important;
    }
    [style*="grid-column"]:not([data-no-mobile]) { grid-column: auto !important; }
    /* …and spans set by CSS classes (a "span 2" child in a 1-column grid silently creates a 2nd column) */
    .form-grid > *, .form-grid-2 > *, .filters-grid > *, .billing-grid > *, .doc-grid > *, .detail-grid > *, .grid-2 > *, .grid-3 > *,
    .sub-grid > *, .budget-grid > *, .as-grid > *, .vd-grid > *, .slip-grid > *, .chart-grid > *, .charts-grid > *, .bottom-grid > *,
    .fg-4 > *, .pr-grid > *, .pr-exp-grid > *, [class*="col-"]:not([class*="column"]), [class*="span-"] { grid-column: auto !important; }

    /* — Toolbars, filters and button rows wrap instead of overflowing — */
    .page-head, .page-title, .top-bar, .toolbar, .actions, .btn-group, .filter-bar, .filter-group, .filters,
    .head, .header-row, .controls, .btn-row { flex-wrap: wrap; gap: 8px; }
    .filter-bar > *, .filter-group > *, .filters > *, .toolbar > select, .toolbar > input { min-width: 0; }
    .filter-search, .filter-bar input[type="search"], .toolbar input[type="search"] { flex: 1 1 100%; width: 100%; }

    /* — Tabs: one swipeable strip, nothing clipped — */
    .tab-nav, .tabs, .tab-bar {
        display: flex; flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden;
        -webkit-overflow-scrolling: touch; scroll-snap-type: x proximity; scrollbar-width: none;
        gap: 6px; max-width: 100%;
    }
    .tab-nav::-webkit-scrollbar, .tabs::-webkit-scrollbar, .tab-bar::-webkit-scrollbar { display: none; }
    .tab-nav > *, .tabs > *, .tab-bar > * { flex: 1 0 auto; scroll-snap-align: center; white-space: nowrap; padding-inline: 12px; font-size: .86rem; }

    /* — Modals: fit the screen, scroll inside — */
    .modal-content, .modal-box, .modal-card, .modal-dialog, .modal-inner, .md, .p-modal {
        width: 100% !important; max-width: 100% !important; margin: 0 !important;
        max-height: calc(100vh - 20px); max-height: calc(100dvh - 20px);
        overflow-y: auto; -webkit-overflow-scrolling: touch; border-radius: 16px;
    }
    .modal-overlay, .modal-backdrop, .modal-ov, .mo { padding: 10px !important; align-items: center; }
    .modal-actions, .mact { flex-wrap: wrap; gap: 8px; }
    .modal-actions > button, .modal-actions > .btn { flex: 1 1 auto; }

    /* — Charts never overflow their card — */
    canvas { max-width: 100% !important; }
    .chart-wrap, .chart-box, .chart-container { max-width: 100%; }

    /* — Floating bits sit above the bottom bar — */
    .bulk, .toast, .floating-bar, #floatingCheckout.in-app, #toast, #vqToasts, #toastContainer, .rt-toast {
        bottom: calc(var(--m-bn-h) + var(--m-safe-bottom) + 14px) !important;
    }
    /* — Bars that stick to the bottom of the page (Walk-in desk total, Vendors "Proceed to assign") stop above the bottom bar — */
    .save-bar, .cart-checkout-bar {
        bottom: calc(var(--m-bn-h) + var(--m-safe-bottom) + 8px) !important;
        border-radius: 14px;
    }
    .bulk { visibility: hidden; }
    .bulk.show { visibility: visible; }
    .app-watermark { bottom: calc(var(--m-bn-h) + var(--m-safe-bottom) + 4px) !important; font-size: 9px; opacity: .55; }

    /* — App shell — */
    .app-topbar { padding-top: calc(.5rem + var(--m-safe-top)); }
    .app-sidebar { padding-top: var(--m-safe-top); }
    @supports (height: 100dvh) { .app-sidebar { height: 100dvh; } }
}

/* ════════════ 3. TABLES ════════════ */

/* Wide tables that stay tables: scroll sideways inside a wrapper, with a soft edge hint. */
.m-scroll { max-width: 100%; }                 /* inert on desktop — it only scrolls on phones, so pop-ups inside cells are never clipped */
@media screen and (max-width: 768px) {
    .m-scroll, .table-wrap {
        overflow-x: auto; -webkit-overflow-scrolling: touch;
        background:
            linear-gradient(to right, #fff 30%, rgba(255,255,255,0)) 0 0 / 36px 100% no-repeat local,
            linear-gradient(to left,  #fff 30%, rgba(255,255,255,0)) 100% 0 / 36px 100% no-repeat local,
            radial-gradient(farthest-side at 0 50%,   rgba(0,0,0,.16), rgba(0,0,0,0)) 0 0 / 12px 100% no-repeat scroll,
            radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,.16), rgba(0,0,0,0)) 100% 0 / 12px 100% no-repeat scroll;
    }
    .m-scroll > table { min-width: 100%; }
    .table-wrap > table.m-stack { min-width: 0; }
    .m-scroll th, .m-scroll td { white-space: nowrap; }
    .m-scroll.m-fit th, .m-scroll.m-fit td { white-space: normal; overflow-wrap: anywhere; }
    .m-scroll.m-fit > table { width: 100%; font-size: .86rem; }
    .m-scroll.m-fit th, .m-scroll.m-fit td { padding-inline: 6px; }
    /* narrow tables the page already scrolls: drop their desktop minimum width so they fit the screen */
    table.m-fit-t { min-width: 0 !important; width: 100% !important; font-size: .86rem; }
    table.m-fit-t th, table.m-fit-t td { white-space: normal !important; overflow-wrap: anywhere; padding-inline: 6px !important; }
    .m-scroll td.wrap, .m-scroll td[style*="white-space"] { white-space: normal; }

    /* List tables: each row becomes a labelled card (labels come from the column headings) */
    table.m-stack { display: block; width: 100% !important; min-width: 0 !important; border: 0 !important; background: transparent; }
    table.m-stack thead { display: none; }
    table.m-stack tbody, table.m-stack tfoot { display: block; }
    table.m-stack tr {
        display: flex; flex-direction: column;
        background: var(--card, #fff); border: 1px solid var(--border, #e5e7eb); border-radius: 14px;
        padding: 10px 14px; margin: 0 0 10px; box-shadow: 0 1px 2px rgba(17, 17, 20, .05);
    }
    table.m-stack tr:hover { background: var(--card, #fff); }
    table.m-stack td {
        display: block; position: relative; box-sizing: border-box;
        padding: 6px 0 6px 40% !important; min-height: 30px;
        border: 0 !important; border-top: 1px solid var(--border-soft, #f1f1f1) !important;
        text-align: left !important; white-space: normal !important; font-size: .9rem;
        width: auto !important; max-width: 100% !important; background: transparent !important;
    }
    table.m-stack td::before {
        content: attr(data-label);
        position: absolute; left: 0; top: 7px; width: 36%;
        font-size: .66rem; font-weight: 800; text-transform: uppercase; letter-spacing: .05em;
        color: var(--muted, #6b7280); line-height: 1.3; padding-right: 8px;
    }
    /* title cell: the row's headline */
    table.m-stack td.m-title {
        order: -2; padding: 2px 0 8px !important; border-top: 0 !important;
        font-size: 1.02rem; font-weight: 800; color: var(--heading, #111); border-bottom: 1px solid var(--border, #e5e7eb) !important; margin-bottom: 2px;
    }
    table.m-stack td.m-title::before { display: none; }
    /* selection checkbox / row number badge */
    table.m-stack td.m-check { order: -3; padding: 0 0 6px !important; border-top: 0 !important; min-height: 0; }
    table.m-stack td.m-check::before { display: none; }
    /* action buttons: a row at the bottom of the card */
    table.m-stack td.m-actions {
        order: 99; padding: 10px 0 2px !important; margin-top: 4px;
        border-top: 1px dashed var(--border, #e5e7eb) !important;
        display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
    }
    table.m-stack td.m-actions::before { display: none; }
    table.m-stack td.m-hide, table.m-stack td.m-empty { display: none !important; }
    /* full-width rows ("No records found", totals with colspan) */
    table.m-stack td.m-wide { padding-left: 0 !important; text-align: center !important; }
    table.m-stack td.m-wide::before { display: none; }
    table.m-stack td.m-title + td { border-top: 0 !important; }
    /* inputs inside card cells use the full value column */
    table.m-stack td input:not([type="checkbox"]):not([type="radio"]), table.m-stack td select, table.m-stack td textarea { width: 100%; }
}

/* ════════════ 4. BOTTOM BAR ════════════ */
.app-bottomnav { display: none; }
@media screen and (max-width: 768px) {
    .app-bottomnav {
        display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 900;
        /* solid, so nothing scrolling underneath shows through as a coloured blur */
        background: var(--card, #fff);
        border-top: 1px solid var(--border, #e5e7eb);
        padding: 6px 6px calc(6px + var(--m-safe-bottom)); gap: 2px; justify-content: space-around;
        box-shadow: 0 -4px 16px rgba(17, 17, 20, .06);
    }
    .app-bottomnav a, .app-bottomnav button {
        flex: 1 1 0; min-width: 0; min-height: 48px;
        display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
        font: inherit; font-size: .66rem; font-weight: 700; color: var(--muted); text-decoration: none;
        background: none; border: 0; border-radius: 12px; padding: 4px 2px; cursor: pointer;
    }
    .app-bottomnav i { font-size: 1.15rem; }
    .app-bottomnav span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .app-bottomnav .on { color: var(--primary, #4f46e5); background: var(--primary-soft, #eef2ff); }
    .app-bottomnav .bn-badge { position: relative; }
    .app-bottomnav .bn-badge::after {
        content: attr(data-count); position: absolute; top: -6px; right: -10px; min-width: 16px; height: 16px; padding: 0 4px;
        border-radius: 999px; background: #e11d48; color: #fff; font-size: .6rem; font-weight: 800; line-height: 16px; text-align: center;
    }
    body.sidebar-open .app-bottomnav, .app-sidebar.open ~ .app-bottomnav { z-index: 800; }
}
@media screen and (max-width: 768px) and (max-height: 480px) and (orientation: landscape) {
    :root { --m-bn-h: 44px; }
    .app-bottomnav a, .app-bottomnav button { min-height: 38px; flex-direction: row; gap: 6px; }
    .app-bottomnav span { display: none; }
}
@media print { .app-bottomnav { display: none !important; } body { padding-bottom: 0 !important; } }
