/* ============================================================
   Phones and small tablets: shared fixes for every signed-in page
   The dashboard's content panel (.dash-content) hides sideways overflow, so anything wider than a
   phone was silently cut off. Rather than editing hundreds of pages, these rules relax the
   desktop-only patterns the pages share, on narrow screens only:
     - MudStack rows wrap instead of running off the edge;
     - form controls given a fixed (min-)width take the full row instead;
     - any inline fixed width is capped at the available width;
     - a bare <table> scrolls in its own strip (tables already in a scroll container are left alone);
     - dialogs, popovers and menus stay inside the screen.
   Desktop is untouched: everything is inside max-width media queries. Loaded after app.css.
   ============================================================ */

@media (max-width: 760px) {
    /* a page's top-level blocks never outgrow the panel (a centred block with auto margins otherwise
       shrinks to fit its widest unbreakable line, e.g. a no-wrap table) */
    .dash-content > * { max-width: 100%; min-width: 0; }
    /* a centred wrapper whose own inline max-width beats that cap: make it fill instead of shrink-fit */
    .dash-content [style*="margin:0 auto"], .dash-content [style*="margin: 0 auto"],
    .dash-content [style*="margin:auto"], .dash-content .mx-auto { width: 100%; }

    /* rows wrap; space-between rows put their trailing actions on the next line. MudStack Row
       renders as "d-flex flex-row"; plain d-flex rows too. A row whose items fit doesn't change. */
    .dash-content .d-flex:not(.flex-column):not(.flex-nowrap),
    .mud-dialog .d-flex:not(.flex-column):not(.flex-nowrap) {
        flex-wrap: wrap;
        row-gap: 8px;
    }

    /* inline widths never exceed the panel */
    .dash-content [style*="width"],
    .mud-dialog [style*="width"] {
        max-width: 100%;
    }

    /* fixed-width inputs, selects and pickers become full-width rows */
    .dash-content .mud-input-control[style*="width"],
    .dash-content .mud-picker[style*="width"],
    .mud-dialog .mud-input-control[style*="width"],
    .mud-dialog .mud-picker[style*="width"] {
        min-width: 0 !important;
        width: 100% !important;
        flex: 1 1 100% !important;
    }

    /* other boxes with an inline minimum width may shrink; table cells keep theirs (they scroll) */
    .dash-content :not(th):not(td):not(col)[style*="min-width"],
    .mud-dialog :not(th):not(td):not(col)[style*="min-width"] {
        min-width: 0 !important;
    }

    /* a bare table that isn't already in a scroll container scrolls on its own */
    .dash-content :not(.mud-table-container):not(.overflow-auto):not(.overflow-x-auto):not([style*="overflow"]) > table {
        display: block;
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* tables MudBlazor renders always scroll sideways rather than being clipped */
    .dash-content .mud-table-container,
    .mud-dialog .mud-table-container {
        overflow-x: auto;
    }

    /* data grids too */
    .dash-content .mud-data-grid .mud-table-container { overflow-x: auto; }

    /* MudTabs headers scroll instead of squeezing */
    .dash-content .mud-tabs-tabbar-wrapper { overflow-x: auto; }

    /* dialogs: never wider than the screen, content scrolls when it must */
    .mud-dialog {
        max-width: calc(100vw - 16px) !important;
        margin: 8px !important;
    }

    .mud-dialog .mud-dialog-content {
        overflow-x: auto;
    }

    .mud-dialog .mud-dialog-actions {
        flex-wrap: wrap;
        gap: 8px;
    }

    /* menus and popovers stay on screen */
    .mud-popover {
        max-width: calc(100vw - 16px);
    }

    /* long words and URLs wrap instead of pushing the layout wider */
    .dash-content p,
    .dash-content .mud-typography {
        overflow-wrap: anywhere;
    }

    /* AD/BS date pickers (ndp.css) size themselves to their content; a range shows two full dates */
    /* (scoped selectors: ndp.css loads after this file) */
    .dash-content .dp-shell, .mud-dialog .dp-shell { max-width: 100%; }
    .dash-content .dp-row, .dash-content .dp-picker-wrap, .dash-content .ndp-wrapper, .dash-content .ndp-input, .dash-content .ndp-input-body,
    .mud-dialog .dp-row, .mud-dialog .dp-picker-wrap, .mud-dialog .ndp-wrapper, .mud-dialog .ndp-input, .mud-dialog .ndp-input-body { min-width: 0; max-width: 100%; }
    .dash-content .ndp-input-body { overflow: hidden; text-overflow: ellipsis; }
    /* a date range shows two dates: its grid cell takes the whole row, and the picker fills it */
    .dash-content .mud-grid-item:has(.dp-range-trigger) { flex-basis: 100% !important; max-width: 100% !important; }
    .dash-content .mud-grid-item > .dp-shell, .dash-content .dp-shell:has(.dp-range-trigger) { width: 100%; }
    .dash-content .dp-shell:has(.dp-range-trigger) .dp-picker-wrap, .dash-content .dp-shell:has(.dp-range-trigger) .ndp-wrapper { flex: 1 1 auto; width: 100%; }

    /* page header actions are flex-shrink:0 (app.css), so their own wrapping never started */
    .page-head-actions { flex-shrink: 1; min-width: 0; max-width: 100%; }

    /* grid toolbars: the status filter and the export / column buttons wrap under the stats */
    .stat-strip-grid-controls, .stat-strip-export { flex-shrink: 1; min-width: 0; max-width: 100%; flex-wrap: wrap; }
    .stat-strip-export { margin-left: 0; }
    .vh-status-filter { flex-wrap: wrap; max-width: 100%; }
    .export-bar { max-width: 100%; }

    /* charts and images shrink with the screen */
    .dash-content img,
    .dash-content svg:not(.mud-icon-root),
    .dash-content canvas,
    .dash-content .apexcharts-canvas {
        max-width: 100%;
    }
}

@media (max-width: 600px) {
    /* MudGrid items with no phone size of their own take the full row */
    .dash-content .mud-grid-item:not([class*="mud-grid-item-xs-"]),
    .mud-dialog .mud-grid-item:not([class*="mud-grid-item-xs-"]) {
        flex-basis: 100%;
        max-width: 100%;
    }

    /* buttons in a wrapped toolbar row share it evenly */
    .dash-content .page-head .mud-button-root { flex: 0 1 auto; }
}

/* Phone width, two things that cost more room than the screen has:
   - the page header no longer sticks. With its buttons wrapped onto two or three rows it covered a third of the
     screen and the filters under it slid beneath;
   - a frozen first grid column scrolls with the rest. The frozen Actions column stays: it is one button wide here
     (the three-dots sheet below). */
@media (max-width: 760px) {
    .dash-content .page-head { position: static; }
    /* in a fill-height page (a column flex) the header and toolbar were squeezed to their minimum height and their
       wrapped buttons spilled over the row below */
    .dash-content .page-head, .dash-content .stat-strip { flex-shrink: 0; }
    .vh-datagrid.freeze-first thead th:first-child,
    .vh-datagrid.freeze-first tbody td:first-child { left: auto; box-shadow: none; }
}

/* Phone width: a row's actions (.row-actions) fold into one three-dots button; tapping it lifts them as a sheet from
   the bottom of the screen, one action per line. app.js ("row actions sheet") sets and clears data-open. The open
   cell is raised so the sheet is not trapped under the grid header. */
@media (max-width: 760px) {
    .vh-datagrid td:has(.row-actions[data-open]) { z-index: 1150 !important; }
    .row-actions:not([data-open]) { display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: 10px; cursor: pointer; border: 1px solid var(--mud-palette-lines-default, rgba(11,16,32,.14)); }
    .row-actions:not([data-open]) > * { display: none !important; }
    .row-actions:not([data-open])::before { content: "\22EE"; font-size: 20px; line-height: 1; font-weight: 700; color: var(--mud-palette-text-primary, #0f172a); }
    .row-actions[data-open] { position: fixed; left: 0; right: 0; bottom: 0; z-index: 1150; display: flex; flex-direction: column; align-items: stretch; gap: 6px; max-height: 70vh; overflow-y: auto; margin: 0; padding: 10px 14px calc(14px + env(safe-area-inset-bottom, 0px)); border-radius: 18px 18px 0 0; background: var(--mud-palette-surface, #fff); box-shadow: 0 0 0 100vmax rgba(0,0,0,.5); text-align: left; white-space: normal; }
    .row-actions[data-open]::before { content: ""; align-self: center; width: 40px; height: 4px; border-radius: 4px; margin-bottom: 6px; background: var(--mud-palette-lines-default, rgba(11,16,32,.2)); }
    /* every action is one full-width line, whatever wraps it (a tooltip, a menu); the sheet sits under MudBlazor's
       popovers (1200) so a menu opened from it shows on top */
    .row-actions[data-open] > *, .row-actions[data-open] .mud-tooltip-root, .row-actions[data-open] .mud-menu { display: flex !important; width: 100%; margin: 0 !important; }
    .row-actions[data-open] :is(button, a) { display: flex !important; align-items: center; justify-content: flex-start; gap: 12px; width: 100% !important; height: auto !important; min-height: 46px; margin: 0 !important; padding: 10px 14px !important; border-radius: 12px !important; font-size: 15px; text-align: left; white-space: normal; }
    /* an icon-only action gets its words: data-label, written in the page or filled in by app.js from its title */
    .row-actions[data-open] [data-label]::after { content: attr(data-label); font-size: 15px; font-weight: 500; text-transform: none; letter-spacing: 0; }
    /* the cell holding the three-dots button stays at the right edge while the row scrolls sideways */
    .dash-content td:has(> .row-actions), .dash-content table:has(.row-actions) thead th:last-child { position: sticky; right: 0; z-index: 2; background: var(--mud-palette-surface, #fff); box-shadow: -2px 0 4px -2px rgba(11,16,32,.18); }
    .dash-content td:has(> .row-actions[data-open]) { z-index: 1150 !important; }
    .row-actions[data-open] .mud-icon-button-label, .row-actions[data-open] .mud-button-label { width: auto; flex: none; }
    .row-actions[data-open] [data-label]::after { flex: 1 1 auto; text-align: left; }
}
