/* =============================================================================
   MOBILE-FIRST APP SHELL  —  additive stylesheet
   =============================================================================
   Loaded AFTER css/styles.css so it can refine, never replace. Every rule that
   restructures layout is inside a max-width media query, so the desktop ERP is
   byte-for-byte the same experience it was before. Nothing here touches data,
   Firebase, the cache or any business logic.
   ========================================================================== */

:root {
    --mobile-nav-h: 60px;
    --mobile-topbar-h: 56px;
    --tap: 44px;                 /* minimum comfortable touch target */
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-t: env(safe-area-inset-top, 0px);
    --ok: #10B981;
    --warn: #F59E0B;
    --bad: #EF4444;
}

/* Standalone (installed) app: honour the device notch/home indicator. */
@media (display-mode: standalone) {
    body { padding-top: var(--safe-t); }
}

/* ---------------------------------------------------------------------------
   SHARED — applies at every width
   --------------------------------------------------------------------------- */

/* Never let a long id/name blow the layout sideways. */
body { overscroll-behavior-y: contain; }
table { max-width: 100%; }

/* Status is never communicated by colour alone: every status badge keeps its
   text label, and these add a shape cue for colour-blind users. */
.badge { display: inline-flex; align-items: center; gap: 4px; }

/* Skeleton loading — shown only until real data arrives, never a fake %. */
@keyframes erp-shimmer { 0% { background-position: -400px 0; } 100% { background-position: 400px 0; } }
.erp-skeleton-bar {
    height: 12px; border-radius: 6px;
    background: linear-gradient(90deg, #EEEEF1 25%, #F7F7F8 50%, #EEEEF1 75%);
    background-size: 800px 100%;
    animation: erp-shimmer 1.3s linear infinite;
}
.erp-skeleton-row td { padding: 14px 20px !important; }

/* Connection pill — 🟢 synced / 🟡 syncing / 🔴 offline */
.erp-conn {
    display: inline-flex; align-items: center; gap: 6px;
    font-size: 10.5px; font-weight: 700; text-transform: uppercase;
    letter-spacing: .04em; padding: 4px 9px; border-radius: 999px;
    border: 1px solid transparent; white-space: nowrap;
}
.erp-conn .dot { width: 7px; height: 7px; border-radius: 999px; flex: 0 0 auto; }
.erp-conn[data-state="synced"]  { color: #047857; background: #ECFDF5; border-color: #A7F3D0; }
.erp-conn[data-state="synced"] .dot  { background: var(--ok); }
.erp-conn[data-state="syncing"] { color: #B45309; background: #FFFBEB; border-color: #FDE68A; }
.erp-conn[data-state="syncing"] .dot { background: var(--warn); animation: erp-pulse 1.1s ease-in-out infinite; }
.erp-conn[data-state="offline"] { color: #B91C1C; background: #FEF2F2; border-color: #FECACA; }
.erp-conn[data-state="offline"] .dot { background: var(--bad); }
@keyframes erp-pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

/* Mobile topbar IST clock — mirrors the conn-pill's sizing so the two sit
   comfortably together between the title and the search button. */
.mt-clock {
    font-size: 11px; font-weight: 700; color: var(--text-muted);
    white-space: nowrap; flex: 0 0 auto; letter-spacing: .01em;
}

/* Mobile-only chrome is hidden on desktop. */
#mobile-topbar, #mobile-bottom-nav, #mobile-more-sheet, #mobile-search-panel, .mobile-fab, .as-filters-btn { display: none; }

/* ---------------------------------------------------------------------------
   TABLET — 768px to 1023px: keep the sidebar, tighten the gutters
   --------------------------------------------------------------------------- */
@media (max-width: 1023px) and (min-width: 768px) {
    .sidebar { width: var(--sidebar-collapsed); }
    .sidebar .sidebar-text,
    .sidebar .sidebar-badge,
    .sidebar .sidebar-logo-text { display: none; }
    .sidebar .sidebar-btn { justify-content: center; padding-left: 0; padding-right: 0; }
    header { padding-left: 16px !important; padding-right: 16px !important; }
    main, .p-6 { padding: 16px !important; }
}

/* ---------------------------------------------------------------------------
   MOBILE — up to 767px: full app shell
   --------------------------------------------------------------------------- */
@media (max-width: 767px) {

    html, body { height: 100%; }
    body { font-size: 15px; }

    /* --- the desktop sidebar is replaced by the bottom nav + More sheet.
           It stays in the DOM (permissions, badges and switchView all still
           drive it) — it is simply not displayed. --- */
    #main-sidebar { display: none !important; }

    /* --- top app bar --- */
    #mobile-topbar {
        display: flex; align-items: center; gap: 10px;
        height: calc(var(--mobile-topbar-h) + var(--safe-t));
        padding: var(--safe-t) 14px 0;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        position: sticky; top: 0; z-index: 30; flex: 0 0 auto;
    }
    #mobile-topbar .mt-title {
        font-family: 'Inter Tight', Inter, sans-serif;
        font-weight: 800; font-size: 16px; letter-spacing: -.02em;
        flex: 1 1 auto; min-width: 0;
        overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    }
    #mobile-topbar .mt-btn {
        width: var(--tap); height: var(--tap); flex: 0 0 auto;
        display: flex; align-items: center; justify-content: center;
        border-radius: 12px; color: var(--text-muted); background: transparent;
        border: 0; position: relative;
    }
    #mobile-topbar .mt-btn:active { background: var(--surface-soft); }

    /* the desktop header row is redundant on mobile */
    .flex.h-screen > .flex-1 > header { display: none !important; }

    /* --- main scroll area sits between the bars --- */
    .flex.h-screen { display: block; height: 100dvh; overflow: hidden; }
    .flex.h-screen > .flex-1 {
        height: 100dvh; display: flex; flex-direction: column;
        padding-bottom: calc(var(--mobile-nav-h) + var(--safe-b));
    }
    .flex.h-screen > .flex-1 > main,
    .flex.h-screen > .flex-1 > div[class*="overflow"] {
        padding: 14px !important;
    }

    /* --- search panel (holds the real global search widget, moved here) --- */
    #mobile-search-panel {
        display: none; padding: 10px 14px 12px;
        background: var(--surface); border-bottom: 1px solid var(--border);
        position: sticky; top: calc(var(--mobile-topbar-h) + var(--safe-t)); z-index: 29;
    }
    #mobile-search-panel.open { display: block; }
    #mobile-search-panel #global-search-wrap { display: block !important; max-width: none; margin-left: 0; }
    #mobile-search-panel #global-order-search { font-size: 16px; padding-top: 12px; padding-bottom: 12px; }

    /* --- floating "+ New enquiry / order" action button ---------------------
       Restores the same entry point as the desktop header button (which is
       hidden on mobile along with the rest of the header) so enquiry
       creation is always one tap away, matching the desktop's "always
       visible" placement. Uses only existing design tokens, so it follows
       the app's colours automatically, light or dark. --- */
    .mobile-fab {
        display: flex; align-items: center; justify-content: center;
        position: fixed; z-index: 45;
        right: 16px;
        bottom: calc(var(--mobile-nav-h) + var(--safe-b) + 16px);
        width: 56px; height: 56px; border-radius: 999px; border: 0;
        background: linear-gradient(135deg, var(--accent), #7C3AED);
        color: #fff;
        box-shadow: var(--shadow-lg);
        -webkit-tap-highlight-color: transparent;
    }
    .mobile-fab:active { transform: scale(0.94); }
    /* Hidden while the search panel or the More sheet is open (they live in
       different DOM parents, so this is toggled in JS rather than via a CSS
       sibling combinator — see toggleMobileSearch()/openMobileMore() in
       js/19-mobile-shell.js). */
    .mobile-fab.mf-hidden { display: none; }

    /* --- bottom navigation --- */
    #mobile-bottom-nav {
        display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
        height: calc(var(--mobile-nav-h) + var(--safe-b));
        padding-bottom: var(--safe-b);
        background: rgba(255,255,255,.97);
        backdrop-filter: saturate(180%) blur(12px);
        border-top: 1px solid var(--border);
    }
    #mobile-bottom-nav .mb-item {
        flex: 1 1 0; min-width: 0; border: 0; background: none;
        display: flex; flex-direction: column; align-items: center; justify-content: center;
        gap: 3px; padding: 6px 2px; color: var(--text-faint);
        font-size: 10px; font-weight: 700; letter-spacing: .01em;
        position: relative; -webkit-tap-highlight-color: transparent;
    }
    #mobile-bottom-nav .mb-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
    #mobile-bottom-nav .mb-item.active { color: var(--accent); }
    #mobile-bottom-nav .mb-item.active::before {
        content: ''; position: absolute; top: 0; width: 26px; height: 3px;
        border-radius: 0 0 3px 3px; background: var(--accent);
    }
    #mobile-bottom-nav .mb-badge {
        position: absolute; top: 4px; left: 50%; margin-left: 5px;
        min-width: 16px; height: 16px; padding: 0 4px; border-radius: 999px;
        background: var(--bad); color: #fff; font-size: 9px; font-weight: 800;
        display: flex; align-items: center; justify-content: center;
    }

    /* --- "More" sheet: every remaining module, nothing dropped --- */
    #mobile-more-sheet {
        display: none; position: fixed; inset: 0; z-index: 60;
        background: rgba(24,24,27,.45); backdrop-filter: blur(2px);
    }
    #mobile-more-sheet.open { display: block; }
    #mobile-more-sheet .ms-panel {
        position: absolute; left: 0; right: 0; bottom: 0;
        max-height: 88dvh; overflow-y: auto;
        background: var(--surface);
        border-radius: 20px 20px 0 0;
        padding: 10px 14px calc(18px + var(--safe-b));
        animation: erp-sheet-up .22s cubic-bezier(.4,0,.2,1);
    }
    @keyframes erp-sheet-up { from { transform: translateY(14%); opacity: .6; } to { transform: none; opacity: 1; } }
    #mobile-more-sheet .ms-grip {
        width: 38px; height: 4px; border-radius: 999px; background: var(--border-strong);
        margin: 4px auto 12px;
    }
    #mobile-more-sheet .ms-group-label {
        font-size: 10px; font-weight: 800; letter-spacing: .1em; text-transform: uppercase;
        color: var(--text-faint); margin: 14px 4px 8px;
    }
    #mobile-more-sheet .ms-item {
        width: 100%; min-height: var(--tap);
        display: flex; align-items: center; gap: 12px;
        padding: 11px 12px; border-radius: 12px; border: 1px solid var(--border);
        background: var(--surface); margin-bottom: 7px;
        font-size: 14px; font-weight: 600; color: var(--text); text-align: left;
    }
    #mobile-more-sheet .ms-item:active { background: var(--surface-soft); }
    #mobile-more-sheet .ms-item.active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-dark); }
    #mobile-more-sheet .ms-item .ms-ic { width: 20px; text-align: center; flex: 0 0 auto; }
    #mobile-more-sheet .ms-item .ms-label { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    /* --- TABLES BECOME CARDS -------------------------------------------------
       Every table in the app is restructured, not scrolled sideways. The
       column headings are carried down as labels by the shell script, so no
       information is lost and no per-table markup was rewritten. --- */
    .erp-cardify thead { display: none; }
    .erp-cardify, .erp-cardify tbody, .erp-cardify tr, .erp-cardify td { display: block; width: auto; }
    .erp-cardify tbody tr {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: 14px;
        margin-bottom: 10px;
        padding: 6px 2px;
        box-shadow: var(--shadow-sm);
    }
    .erp-cardify tbody tr:active { border-color: var(--accent); }
    .erp-cardify tbody td {
        border: 0 !important;
        padding: 7px 14px !important;
        text-align: left !important;
        display: flex; align-items: center; justify-content: space-between; gap: 12px;
        min-height: 34px;
    }
    .erp-cardify tbody td::before {
        content: attr(data-m-label);
        font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em;
        color: var(--text-faint); flex: 0 0 auto; padding-right: 8px;
    }
    .erp-cardify tbody td[data-m-label=""]::before { display: none; }
    /* first cell = the card title, full width, no label */
    .erp-cardify tbody td:first-child {
        display: block; padding-top: 11px !important;
        border-bottom: 1px solid var(--border) !important; margin-bottom: 4px;
    }
    .erp-cardify tbody td:first-child::before { display: none; }
    /* action cell stacks full-width buttons */
    .erp-cardify tbody td.erp-actions-cell { display: block; padding-bottom: 11px !important; }
    .erp-cardify tbody td.erp-actions-cell::before { display: none; }
    .erp-cardify tbody td.erp-actions-cell .btn { width: 100%; min-height: 40px; margin-bottom: 6px; }
    .erp-cardify tbody td.erp-actions-cell input { width: 100%; min-height: 40px; font-size: 16px; }
    .erp-cardify tbody td[colspan] { display: block; }
    .erp-cardify tbody td[colspan]::before { display: none; }
    /* Bottlenecks "Longest wait · held by" cell (v2.2, js/41): the order
       button + "held by @name" line are wrapped in .cc-bn-longest, which
       takes the full remaining width of the cardified row — as two
       separate flex items the name was squeezed and cut at the card
       edge ("helc @as", 26 Nov 2026). Right-aligned inside the wrapper
       so the order id and the name sit under each other; the button
       may wrap instead of overflowing. */
    .erp-cardify .cc-bn-longest { text-align: right; }
    .erp-cardify .cc-bn-longest button { white-space: normal; overflow-wrap: anywhere; }
    /* continuation rows (remarks, store notes, serial rows) join the card above
       them instead of breaking off into a card of their own */
    .erp-cardify tbody tr.erp-has-cont {
        margin-bottom: 0; border-bottom: 0;
        border-radius: 14px 14px 0 0; box-shadow: none;
    }
    .erp-cardify tbody tr.erp-cont {
        border-top: 0; border-radius: 0 0 14px 14px;
        margin-bottom: 10px; padding-top: 0;
        box-shadow: var(--shadow-sm);
    }
    .erp-cardify tbody tr.erp-cont td { padding-top: 0 !important; }

    /* empty-state rows must not look like cards */
    .erp-cardify tbody tr.erp-skeleton-row { padding: 0; }

    /* Tables that genuinely need to stay tabular (PI preview, print areas) */
    #pi-printable-area table, .erp-keep-table { display: table !important; }
    #pi-printable-area thead, .erp-keep-table thead { display: table-header-group !important; }
    #pi-printable-area tr, .erp-keep-table tr { display: table-row !important; }
    #pi-printable-area td, #pi-printable-area th,
    .erp-keep-table td, .erp-keep-table th { display: table-cell !important; }
    #pi-printable-area { overflow-x: auto; }

    /* --- MODALS become full-screen sheets --- */
    .modal-backdrop { padding: 0 !important; align-items: flex-end !important; }
    .modal-backdrop > div {
        width: 100% !important; max-width: 100% !important;
        height: 100dvh; max-height: 100dvh !important;
        border-radius: 0 !important;
        padding: 0 !important;
        display: flex; flex-direction: column;
        overflow: hidden !important;
    }
    /* the modal's own first child row becomes a sticky header, the rest scrolls */
    .modal-backdrop > div > * {
        padding-left: 16px; padding-right: 16px;
    }
    .modal-backdrop > div > *:first-child {
        position: sticky; top: 0; z-index: 5;
        background: var(--surface);
        padding-top: calc(14px + var(--safe-t)); padding-bottom: 12px;
        border-bottom: 1px solid var(--border);
        margin-bottom: 0 !important;
        flex: 0 0 auto;
    }
    .modal-backdrop > div > *:not(:first-child) {
        overflow-y: auto; -webkit-overflow-scrolling: touch;
        padding-bottom: calc(20px + var(--safe-b));
        flex: 1 1 auto;
    }
    /* bigger, unmissable close control */
    .modal-backdrop > div > *:first-child button i { width: 24px !important; height: 24px !important; }
    .modal-backdrop > div > *:first-child button { min-width: var(--tap); min-height: var(--tap); }

    /* Photo viewer keeps its own dark full-screen treatment */
    #photoViewerModal > div { height: auto !important; border-radius: 0 !important; }

    /* --- FILTER / SEARCH PANELS become bottom sheets --- */
    /* #gf-panel (Graphics tab's own advanced filters, js/21-graphics.js) added
       here additively so it gets the identical mobile bottom-sheet treatment
       as the existing per-department filter panels — no new mobile rules
       needed, no existing rule changed.
       #as-filter-panel (Assembly Desk filters, js/39) joins the same list,
       with two differences (v2.1): it opens with an .open class instead of
       the hidden class (it must stay a visible in-flow card on desktop, so
       it is display:none only under this query until .open is set), and it
       sits ABOVE the bottom nav instead of covering it. Its z-index also
       clears the chat button (z-150) so the sheet's fields stay tappable. */
    #qf-panel, #adv-search-panel, #gf-panel, #as-filter-panel {
        position: fixed !important;
        left: 0 !important; right: 0 !important; bottom: 0 !important;
        top: auto !important; width: 100% !important; max-width: 100% !important;
        margin: 0 !important;
        border-radius: 18px 18px 0 0 !important;
        max-height: 82dvh; overflow-y: auto;
        padding: 16px 16px calc(18px + var(--safe-b)) !important;
        z-index: 70;
        box-shadow: 0 -10px 40px rgba(0,0,0,.18);
    }
    #qf-panel::before, #adv-search-panel::before, #gf-panel::before, #as-filter-panel::before {
        content: ''; display: block; width: 38px; height: 4px; border-radius: 999px;
        background: var(--border-strong); margin: -4px auto 14px;
    }
    #qf-panel .grid, #adv-search-panel .grid, #gf-panel .grid { grid-template-columns: minmax(0,1fr) !important; gap: 12px !important; }
    #qf-panel .col-span-2, #adv-search-panel .col-span-2, #gf-panel .col-span-2 { grid-column: span 1 / span 1 !important; }
    /* Assembly filter sheet only: hidden until the Filters button opens it;
       parked above the bottom nav (never covering it) and above the chat
       button, so every field stays reachable. */
    #as-filter-panel { display: none; bottom: calc(var(--mobile-nav-h) + var(--safe-b)) !important; z-index: 160; }
    #as-filter-panel.open { display: block; }
    /* the filter row is a wrapping flex row on desktop; in the sheet every
       field stacks full-width so nothing is squeezed or cut off. */
    #as-filter-panel .flex { flex-direction: column; align-items: stretch !important; }
    #as-filter-panel .flex > div, #as-filter-panel .flex > button { width: 100%; }
    #as-filter-panel select, #as-filter-panel input { width: 100%; }
    /* the Filters button itself: mobile-only, next to + New job. */
    .as-filters-btn { display: inline-flex; align-items: center; justify-content: center; }

    /* --- ASSEMBLY JOB CARDS: the Ageing / Time-on-task column sits beside
       the title on desktop, where the floating buttons can cover its right
       edge on phones. On mobile it stacks underneath, left-aligned, with a
       full-width progress bar — nothing sits under a FAB any more. --- */
    .as-job-head { flex-direction: column; }
    .as-job-stats { width: 100%; text-align: left !important; }
    .as-job-stats .w-36 { width: 100% !important; max-width: 260px; }

    /* --- TEAM TASKS assign form: compact just enough that the Assign
       button is reachable without hunting (textarea + card padding). --- */
    #task-assign-card { padding: 16px !important; }
    #tk-desc { height: 72px; }

    /* --- FORMS & INPUTS: 16px prevents iOS zoom-on-focus --- */
    input, select, textarea { font-size: 16px !important; }
    .input, input.input, select.input, textarea.input {
        min-height: var(--tap); padding-top: 11px; padding-bottom: 11px;
    }
    .input.text-xs, .input.py-1\.5, .input.input-sm { min-height: 40px; }
    .label { font-size: 11.5px; margin-bottom: 6px; }
    /* inputs sitting inside table cards should not be squeezed */
    .erp-cardify td .input { width: 100% !important; max-width: none; }

    /* --- BUTTONS: comfortable targets everywhere --- */
    .btn { min-height: var(--tap); padding-left: 16px; padding-right: 16px; font-size: 14px; }
    .btn-xs { min-height: 38px; font-size: 12.5px; }
    .btn-sm { min-height: 40px; }
    .btn-block { width: 100%; }
    /* action button groups stack instead of cramming */
    .grid.grid-cols-3 > .btn, .grid.grid-cols-2 > .btn { width: 100%; }

    /* --- Sticky primary action inside modals --- */
    .erp-sticky-actions {
        position: sticky; bottom: 0;
        background: var(--surface);
        border-top: 1px solid var(--border);
        padding: 12px 0 calc(12px + var(--safe-b));
        margin-top: 12px; z-index: 4;
    }

    /* --- KPI / dashboard cards --- */
    /* v2.1 — minmax(0,1fr), NOT plain 1fr: plain 1fr is minmax(auto,1fr),
       whose auto minimum lets a card whose min-content is wider than the
       track (e.g. Top Products' truncated product-name row) overflow the
       track and push the whole dashboard sideways on phones. minmax(0,1fr)
       is what Tailwind's grid-cols-1 itself uses; it restores the shrink
       protection this rule used to defeat. */
    .grid.grid-cols-2.lg\:grid-cols-4,
    .grid.lg\:grid-cols-2,
    .grid.grid-cols-1.lg\:grid-cols-2 { grid-template-columns: minmax(0,1fr) !important; gap: 12px !important; }
    #kpi-dashboard { grid-template-columns: repeat(2, minmax(0,1fr)) !important; gap: 10px !important; }
    .card { border-radius: 14px; }

    /* sub-tab strips scroll horizontally instead of wrapping into chaos */
    .erp-subtabs, .acc-subtabs {
        display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px;
        scrollbar-width: none; -webkit-overflow-scrolling: touch;
    }
    .erp-subtabs::-webkit-scrollbar { display: none; }
    .erp-subtabs > * { flex: 0 0 auto; }

    /* chat / notification panels fit the screen */
    #chat-panel, #notif-panel {
        position: fixed !important; inset: auto 0 0 0 !important;
        width: 100% !important; max-width: 100% !important;
        max-height: 80dvh; border-radius: 18px 18px 0 0 !important;
        padding-bottom: var(--safe-b);
    }

    /* empty states get room to breathe */
    .erp-empty { padding: 32px 18px; text-align: center; }
}

/* --- very small phones (320–359px) --- */
@media (max-width: 359px) {
    #mobile-bottom-nav .mb-item { font-size: 9px; }
    #mobile-topbar .mt-title { font-size: 15px; }
    #kpi-dashboard { grid-template-columns: minmax(0,1fr) !important; }
    .erp-cardify tbody td { padding-left: 11px !important; padding-right: 11px !important; }
}

/* --- reduced motion --- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* --- printing (PI / invoices) must ignore the app chrome entirely --- */
@media print {
    #mobile-topbar, #mobile-bottom-nav, #mobile-more-sheet, #mobile-search-panel,
    #main-sidebar, header { display: none !important; }
}
