/* ============================================================
   Shared phone/tablet layout rules (≤768px). Loaded by every page
   after its own styles. Goal: pages scroll DOWN only — wide content
   (tables, charts) scrolls inside its own box, never the whole page.
   Desktop is untouched: everything here is inside the media query.
   ============================================================ */
@media (max-width: 768px) {
    /* Nothing pushes the page wider than the screen. `clip` (not
       `hidden`) so position:sticky headers keep working. */
    html, body { max-width: 100vw; overflow-x: clip; }
    @supports not (overflow: clip) { html, body { overflow-x: hidden; } }

    /* Media and form fields never exceed their container */
    img, svg, video, canvas, iframe { max-width: 100%; }
    input, select, textarea, button { max-width: 100%; box-sizing: border-box; }
    input[type="text"], input[type="number"], input[type="email"], input[type="date"],
    input[type="tel"], input[type="search"], select, textarea { font-size: 16px; } /* stops iOS zoom-on-focus */

    /* Header / toolbar rows: let buttons wrap onto more lines instead
       of running off the right edge. */
    .header, .top-bar, .page-header, .page-title, .card-header,
    .header-left, .header-right, .header-actions, .header-nav,
    .nav-links, .controls, .controls-right, .toolbar, .actions, .actions-bar,
    .filter-bar, .filters, .job-selector, .view-toggle, .zoom-controls,
    .gantt-toolbar, .ribbon, .stats-bar, .summary-bar, .summary-stats {
        flex-wrap: wrap !important;
        row-gap: 8px;
    }
    .header, .top-bar, .page-header { padding-left: 14px !important; padding-right: 14px !important; }
    .header h1 { font-size: 1.15rem !important; }
    .logo-text h1, .logo-text .subtitle { letter-spacing: 1px !important; }
    .stats-bar, .summary-bar { gap: 12px 18px !important; }
    .job-selector select { min-width: 0 !important; width: 100%; }

    /* Inline-styled flex rows (hundreds of them) and flex headings: wrap
       when they'd otherwise run off the edge. */
    [style*="display: flex"], [style*="display:flex"], h1, h2, h3 { flex-wrap: wrap; }
    [style*="display: flex"] > input, [style*="display:flex"] > input,
    [style*="display: flex"] > select, [style*="display:flex"] > select { min-width: 0; }
    /* ...but a column layout (e.g. a modal: header / body / footer) must
       never wrap, or a height-limited box spills its body into a second
       column beside the header. */
    [style*="flex-direction: column"], [style*="flex-direction:column"] { flex-wrap: nowrap; }

    /* Tab strips: one row that swipes sideways (the page itself doesn't) */
    .tabs, .tab-nav, .tab-bar, .ribbon-tabs {
        display: flex; flex-wrap: nowrap !important; overflow-x: auto;
        -webkit-overflow-scrolling: touch; scrollbar-width: none; white-space: nowrap;
    }
    .tabs::-webkit-scrollbar, .tab-nav::-webkit-scrollbar, .tab-bar::-webkit-scrollbar { display: none; }
    .tabs .tab, .tab-nav .tab { flex: 0 0 auto; padding-left: 14px !important; padding-right: 14px !important; }

    /* Tables: mobile.js wraps each table in .mob-tbl-scroll so only the
       table scrolls sideways. Cells keep their natural width. */
    .mob-tbl-scroll {
        display: block; max-width: 100%; overflow-x: auto;
        -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain;
        margin-bottom: 4px;
    }
    .mob-tbl-scroll > table { min-width: max-content; }
    /* A subtle fade on the right hints that there is more to swipe */
    .mob-tbl-scroll.has-more {
        background: linear-gradient(to left, rgba(0,0,0,0.06), transparent 24px) right / 24px 100% no-repeat local;
    }

    /* Cards / content containers never force a width */
    .container, .card, .content, .main-content, .page-content { max-width: 100% !important; }
    .container { padding-left: 12px !important; padding-right: 12px !important; }

    /* Modals: full screen on phones, scroll inside, buttons reachable */
    .modal-content { width: 100% !important; max-width: 100% !important; margin: 0 !important; }

    /* ---- Schedule page ---- */
    /* Toolbar ribbon: one row that swipes sideways instead of stacking
       into a tall block. */
    .ribbon { flex-wrap: nowrap !important; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .ribbon-group { flex: 0 0 auto; }
    /* PM Cost Analysis side panel: a bottom sheet on phones. Closed, only
       its 40px tab shows at the right edge; open, it fills the width. */
    .pm-sidebar { width: 100vw !important; top: auto !important; height: 70vh; }
    .pm-sidebar.collapsed { transform: translateX(calc(100vw - 40px)) !important; }
    .summary-bar { gap: 10px 16px !important; }
    /* Task list and Gantt chart stack (side by side leaves the chart a
       sliver wide on a phone). The task list scrolls within itself. */
    .main-container { flex-direction: column !important; height: auto !important; }
    .task-panel { width: 100% !important; max-width: 100% !important; min-width: 0 !important;
                  border-right: none !important; border-bottom: 1px solid #ddd; max-height: 50vh; overflow: auto; }
    .panel-resizer { display: none !important; }
    .gantt-panel { min-height: 65vh; }
    .gantt-toolbar { height: auto !important; min-height: 0 !important; max-height: none !important; flex-wrap: wrap; gap: 8px; }

    /* Utility: opt a table out of the auto scroll-wrap */
    .no-mob-wrap { }
}

/* Never print inside a clipped scroll box */
@media print {
    .mob-tbl-scroll { overflow: visible !important; display: block; background: none !important; }
}
