/* ============================================================================
   Mobile / narrow-screen refinements
   ----------------------------------------------------------------------------
   Loaded LAST from Components/App.razor so it can harmonize the whole app.

   IMPORTANT: every rule in this file lives inside a max-width media query.
   Desktop rendering is therefore untouched for mouse devices. The navigation
   switches to overlay mode on narrow screens (<=1199px) AND on touch devices
   (tablets in any orientation, up to 1399px); the top bar / grid chips are
   relaxed in the same cases. All other mobile rules stop at 768px.
   ========================================================================== */

/* Close control added to the drawer header. Hidden on desktop; the overlay
   drawer (phones AND tablets) turns it into a proper touch target. */
.drawer-close-mobile {
    display: none;
    position: absolute;
    top: 50%;
    right: -0.25rem;
    transform: translateY(-50%);
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 0.5rem;
    color: #fff;
    text-decoration: none;
    font-size: 1.1rem;
}

.drawer-close-mobile:hover,
.drawer-close-mobile:focus-visible {
    color: #fff;
    background-color: rgba(255, 255, 255, 0.16);
}

/* Tablets 769px-1199px: the navigation opens as an overlay on top of the
   content instead of shrinking the page. Only the drawer is affected;
   every other desktop rule stays untouched. */
@media (max-width: 1199.98px), (pointer: coarse) and (max-width: 1399.98px) {
    .drawer-close-mobile {
        display: inline-flex;
    }

    .navigation-drawer-header {
        position: relative;
    }
}

/* Tablet-only panel width: a fixed 320px reads better than 86vw on an
   800px-wide tablet. Phones keep their 86vw rule further down. */
@media (min-width: 769px) and (max-width: 1199.98px), (min-width: 769px) and (pointer: coarse) and (max-width: 1399.98px) {
    .navigation-drawer.mobile > .dxbl-drawer-panel {
        width: 320px !important;
        max-width: 86vw !important;
    }
}

/* ============================================================================
   Tablet: top bar must never spill outside its buttons
   ----------------------------------------------------------------------------
   The labelled top-bar buttons are wider than a tablet viewport. When the row
   ran out of space the flex items shrank to their min-width and the caption +
   icon (no overflow:hidden on .dxbl-btn) painted *outside* the button.
   Fix: items no longer shrink, captions collapse to icons and the
   non-essential entries are hidden - same idea as the phone layout.
   ========================================================================== */
@media (max-width: 1199.98px), (pointer: coarse) and (max-width: 1399.98px) {
    body.edasys-app .topbar-actions {
        flex-wrap: nowrap;
        gap: 0.25rem;
        min-width: 0;
    }

    /* A button is never narrower than its own content. */
    body.edasys-app .topbar-actions > *,
    body.edasys-app .topbar-actions .menu-button-nav.dxbl-btn {
        flex: 0 0 auto;
    }

    body.edasys-app .topbar-actions .menu-button-nav .dxbl-btn-caption {
        display: none !important;
    }

    /* Environment badge stays readable. */
    body.edasys-app .topbar-actions .menu-button-nav.env-demo .dxbl-btn-caption,
    body.edasys-app .topbar-actions .menu-button-nav.env-live .dxbl-btn-caption {
        display: inline-block !important;
    }
}

/* Tablet-only top bar: keep the username (capped) and drop the rest. */
@media (min-width: 769px) and (max-width: 1199.98px), (min-width: 769px) and (pointer: coarse) and (max-width: 1399.98px) {
    body.edasys-app .topbar-actions .topbar-username-button .dxbl-btn-caption {
        display: inline-block !important;
        max-width: 9rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Non-essential entries on a tablet. */
    body.edasys-app .topbar-actions .topbar-whatsnew-wrap,
    body.edasys-app .topbar-actions .topbar-info-wrap {
        display: none !important;
    }
}

/* ============================================================================
   Chips inside data grids
   ----------------------------------------------------------------------------
   The chip styles declare `min-width: 120px` + `white-space: nowrap`. In a
   percentage column (π.χ. "Χαρακτηρισμός" 12% στο Products) the chip was wider
   than its cell and the grid clipped it. Below the desktop breakpoint the chip
   fits the cell and wraps instead of being cut.
   ========================================================================== */
@media (max-width: 1199.98px), (pointer: coarse) and (max-width: 1399.98px) {
    body.edasys-app .dxbl-grid .chip,
    body.edasys-app .dxbl-grid .chip-sm,
    body.edasys-app .dxbl-grid .products-outlined-chip,
    body.edasys-app .dxbl-grid .status-chip,
    body.edasys-app .dxbl-grid .b2g-status-chip,
    body.edasys-app .dxbl-grid .pc-type-chip,
    body.edasys-app .dxbl-grid .acc-assign-chips span {
        min-width: 0;
        max-width: 100%;
        margin: 0.15rem 0;
        padding: 0.15rem 0.5rem;
        font-size: 0.78rem;
        line-height: 1.25;
        white-space: normal;
        overflow-wrap: anywhere;
    }

    body.edasys-app .dxbl-grid .chip i,
    body.edasys-app .dxbl-grid .products-outlined-chip i {
        margin-right: 0.3rem;
        font-size: 0.85rem;
    }

    /* The cell content itself must be allowed to shrink below its content. */
    body.edasys-app .dxbl-grid .dxbl-grid-cell-content {
        min-width: 0;
    }
}

@media (max-width: 768px) {
    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    /* The mobile layout is designed to fit the viewport. Prevent any residual
       one-pixel/oversize element from creating a page-level horizontal bar. */
    html,
    body {
        overflow-x: hidden;
    }

    /* Keep content clear of the notch in landscape on modern phones. */
    body.edasys-app {
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
    }

    /* ---------------------------------------------------------------------
       1. Page frame
       ------------------------------------------------------------------ */
    body.edasys-app .page,
    body.edasys-app .page > div,
    body.edasys-app .page [data-tour="content"],
    .navigation-drawer > .dxbl-drawer-content {
        min-width: 0;
    }

    /* Bootstrap's p-4 (1.5rem per side) eats ~48px of a phone's width.
       Target only the layout content wrapper, not nested p-4 components. */
    body.edasys-app .page [data-tour="content"] {
        padding: 0.75rem !important;
    }

    /* ---------------------------------------------------------------------
       2. Top bar
       ------------------------------------------------------------------ */
    .nav-buttons-container {
        align-items: center;
        flex-wrap: nowrap;
        gap: 0.5rem;
        min-width: 0;
        padding: 0.5rem 0.75rem;
    }

    .nav-buttons-container > * {
        min-width: 0;
    }

    /* Top bar actions: on phones the long Greek captions no longer fit.
       Keep the icons (and the short DEMO/LIVE badge) and drop the rest. */
    body.edasys-app .topbar-actions {
        flex-wrap: nowrap;
        gap: 0.25rem;
        min-width: 0;
    }

    body.edasys-app .topbar-actions .menu-button-nav .dxbl-btn-caption {
        display: none !important;
    }

    body.edasys-app .topbar-actions .menu-button-nav.env-demo .dxbl-btn-caption,
    body.edasys-app .topbar-actions .menu-button-nav.env-live .dxbl-btn-caption {
        display: inline-block !important;
    }

    body.edasys-app .topbar-actions .menu-button-nav.dxbl-btn {
        --dxbl-btn-padding-x: 0.45rem;
        min-width: 2rem;
    }

    /* Non-essential on a phone: the date and the "what's new" announcement. */
    body.edasys-app .topbar-actions .topbar-calendar-button,
    body.edasys-app .topbar-actions .topbar-whatsnew-wrap {
        display: none !important;
    }

    .nav-buttons-container .menubutton-float-end {
        margin-left: auto;
    }

    /* ---------------------------------------------------------------------
       3. Navigation drawer
       ------------------------------------------------------------------ */
    .navigation-drawer.mobile > .dxbl-drawer-panel {
        width: 86vw !important;
        max-width: 86vw !important;
    }

    .navigation-drawer > .dxbl-drawer-content {
        height: 100vh; /* fallback for older browsers */
        height: 100dvh;
    }

    .navigation-drawer-header {
        position: relative;
    }

    /* ---------------------------------------------------------------------
       4. Toolbars
       DevExpress toolbars are single-line and clip their items on phones.
       Let them wrap and scroll horizontally when they still do not fit.
       ------------------------------------------------------------------ */
    .dxbl-toolbar {
        align-items: center;
        flex-wrap: wrap !important;
        height: auto !important;
        min-height: 2.25rem;
        row-gap: 0.35rem;
        column-gap: 0.375rem;
    }

    .dxbl-toolbar .dxbl-toolbar-item {
        max-width: 100%;
    }

    body.edasys-app .dxbl-toolbar .edasys-search-box {
        width: 100%;
    }

    .dxbl-grid-toolbar-container,
    .dxbl-grid-top-panel,
    .dxbl-grid-bottom-panel,
    .dxbl-grid-pager-and-pager-summary-container {
        flex-wrap: wrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* ---------------------------------------------------------------------
       5. Data grids
       Keep the grid horizontally scrollable and make the text a bit denser
       so more columns fit before the user has to scroll.
       ------------------------------------------------------------------ */
    body.edasys-app .dxbl-grid {
        --dxbl-grid-font-size: 0.8rem;
    }

    body.edasys-app .dxbl-grid .dxbl-grid-table {
        min-width: 100%;
    }

    body.edasys-app .dxbl-grid .dxbl-grid-header-row > th {
        white-space: normal;
        vertical-align: middle;
    }

    body.edasys-app .dxbl-grid .dxbl-scroll-viewer,
    body.edasys-app .dxbl-grid .dxbl-scroll-viewer-content {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    /* ---------------------------------------------------------------------
       5b. Grid card mode (see js/mobile-grid.js)
       Each data row becomes a labelled card, so nothing needs horizontal
       scrolling. The script adds the labels/classes; these rules only style
       them and only below the mobile breakpoint.
       ------------------------------------------------------------------ */
    body.edasys-app .dxbl-grid.dxbl-mobile-cards {
        --dxbl-grid-min-width: 0;
        max-height: none !important;
        min-height: 0 !important;
    }

    body.edasys-app .dxbl-mobile-cards .dxbl-scroll-viewer,
    body.edasys-app .dxbl-mobile-cards .dxbl-scroll-viewer-content {
        width: auto !important;
        min-width: 100% !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }

    body.edasys-app .dxbl-mobile-cards colgroup,
    body.edasys-app .dxbl-mobile-cards col {
        display: none !important;
    }

    body.edasys-app .dxbl-mobile-cards .dxbl-grid-table {
        display: block !important;
        width: 100% !important;
        min-width: 100% !important;
        table-layout: auto !important;
        border-collapse: separate !important;
    }

    body.edasys-app .dxbl-mobile-cards thead {
        display: block !important;
    }

    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-header-row {
        display: none !important;
    }

    /* The optional filter row stays available above the cards. */
    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row {
        display: block !important;
        margin: 0 0 0.65rem;
        padding: 0.5rem 0.7rem;
        border: 1px dashed var(--bs-border-color, #dee2e6);
        border-radius: 12px;
        background: var(--bs-tertiary-bg, #f8fafc);
    }

    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > td,
    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > th {
        display: block !important;
        width: auto !important;
        padding: 0.25rem 0 !important;
        border: 0 !important;
    }

    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > td::before,
    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > th::before {
        content: attr(data-label);
        display: block;
        margin-bottom: 0.2rem;
        font-size: 0.75rem;
        font-weight: 600;
        color: var(--bs-secondary-color, #5f6b7a);
    }

    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > td:not([data-label])::before,
    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > th:not([data-label])::before {
        content: none;
        display: none;
    }

    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > td > *,
    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > th > * {
        width: 100% !important;
        max-width: none !important;
    }

    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > td.dxbl-grid-command-cell {
        display: flex !important;
        justify-content: flex-end;
        padding-top: 0.4rem !important;
    }

    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > td.dxbl-grid-command-cell > * {
        width: auto !important;
    }

    body.edasys-app .dxbl-mobile-cards tbody {
        display: block !important;
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr {
        display: block !important;
        width: auto !important;
        height: auto !important;
        transform: none !important;
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr:not(.dxbl-mobile-card--full) {
        margin: 0 0 0.65rem;
        padding: 0.5rem 0.7rem;
        border: 1px solid var(--bs-border-color, #dee2e6);
        border-radius: 12px;
        background: var(--bs-body-bg, #fff);
        box-shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr > td {
        display: flex !important;
        align-items: flex-start !important;
        justify-content: space-between !important;
        gap: 0.85rem;
        width: auto !important;
        max-width: none !important;
        height: auto !important;
        padding: 0.3rem 0 !important;
        border: 0 !important;
        position: static !important;
        left: auto !important;
        right: auto !important;
        white-space: normal !important;
        text-align: left !important;
        font-size: 0.85rem;
        line-height: 1.35;
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr > td::before {
        content: attr(data-label);
        flex: 0 0 38%;
        max-width: 38%;
        font-weight: 600;
        color: var(--bs-secondary-color, #5f6b7a);
        overflow-wrap: anywhere;
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr > td:not([data-label])::before {
        content: none;
        display: none;
    }

    /* DevExpress draws the selected/focused row highlight through td::before.
       In card mode that pseudo-element collides with our label: the overlay
       escapes the row (a gray block under the card) and the label disappears.
       Re-assert the label and neutralise the overlay. */
    body.edasys-app .dxbl-mobile-cards tbody > tr.dxbl-grid-selected-row > td::before,
    body.edasys-app .dxbl-mobile-cards tbody > tr.dxbl-grid-focused-row > td::before {
        content: attr(data-label) !important;
        display: block !important;
        position: static !important;
        inset: auto !important;
        z-index: auto !important;
        opacity: 1 !important;
        background: none !important;
        box-shadow: none !important;
        border: 0 !important;
        flex: 0 0 38%;
        max-width: 38%;
        font-weight: 600;
        color: var(--bs-secondary-color, #5f6b7a);
        overflow-wrap: anywhere;
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr.dxbl-grid-selected-row > td:not([data-label])::before,
    body.edasys-app .dxbl-mobile-cards tbody > tr.dxbl-grid-focused-row > td:not([data-label])::before {
        content: none !important;
        display: none !important;
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr > td > * {
        min-width: 0;
        max-width: 100%;
    }

    /* Action/command buttons sit on their own row at the bottom of the card. */
    body.edasys-app .dxbl-mobile-cards tbody > tr > td.dxbl-grid-command-cell {
        justify-content: flex-end !important;
        margin-top: 0.3rem;
        padding-top: 0.45rem !important;
        border-top: 1px dashed var(--bs-border-color, #dee2e6) !important;
    }

    body.edasys-app .dxbl-mobile-cards .dxbl-mobile-hide {
        display: none !important;
    }

    /* Structural cells (empty / indent) must stay hidden even inside a
       full-width card row or the filter row, where the higher-specificity
       `td { display: ... !important }` rules above would otherwise win. */
    body.edasys-app .dxbl-mobile-cards tbody > tr > td.dxbl-mobile-hide,
    body.edasys-app .dxbl-mobile-cards .dxbl-mobile-card--full > td.dxbl-mobile-hide,
    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > td.dxbl-mobile-hide,
    body.edasys-app .dxbl-mobile-cards thead .dxbl-grid-filter-row > th.dxbl-mobile-hide {
        display: none !important;
    }

    /* Group / detail / empty rows span the whole card and carry no labels. */
    body.edasys-app .dxbl-mobile-cards .dxbl-mobile-card--full {
        margin: 0.35rem 0 0.65rem;
        padding: 0.25rem 0.15rem;
    }

    body.edasys-app .dxbl-mobile-cards .dxbl-mobile-card--full > td {
        display: block !important;
        width: auto !important;
        padding: 0.15rem 0 !important;
    }

    body.edasys-app .dxbl-mobile-cards .dxbl-mobile-card--full > td::before {
        content: none !important;
        display: none !important;
    }

    body.edasys-app .dxbl-mobile-cards tbody > tr.dxbl-grid-group-row {
        display: flex !important;
        align-items: center;
        flex-wrap: wrap;
        gap: 0.35rem;
    }

    body.edasys-app .dxbl-mobile-cards .dxbl-grid-group-row > td[colspan] {
        flex: 1 1 auto;
        font-weight: 600;
        color: var(--bs-body-color, #12384f);
    }

    body.edasys-app .dxbl-mobile-cards .dxbl-grid-empty-row > td {
        text-align: center !important;
        padding: 1.5rem 0 !important;
        color: #6c757d;
    }

    /* Keep in-card buttons easy to tap and readable. */
    body.edasys-app .dxbl-mobile-cards tbody > tr > td .dxbl-btn,
    body.edasys-app .dxbl-mobile-cards tbody > tr > td .btn {
        white-space: normal;
        text-align: left;
    }

    /* ---------------------------------------------------------------------
       6. MDI tab strip
       ------------------------------------------------------------------ */
    .dxbl-tabs-tablist {
        flex-wrap: nowrap !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
    }

    .dxbl-tabs-tablist > * {
        flex: 0 0 auto;
    }

    /* ---------------------------------------------------------------------
       7. Dialogs / popups
       Never let a modal be wider or taller than the viewport.
       ------------------------------------------------------------------ */
    .dxbl-modal-content,
    .dxbl-popup,
    .dxbl-popup-content {
        max-width: calc(100vw - 1rem) !important;
        max-height: calc(100dvh - 1rem) !important;
    }

    .dxbl-modal-body,
    .dxbl-popup-content {
        overflow: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* ---------------------------------------------------------------------
       8. Form controls
       16px prevents iOS Safari from zooming the page on focus.
       ------------------------------------------------------------------ */
    body.edasys-app input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    body.edasys-app select,
    body.edasys-app textarea,
    body.edasys-app .dxbl-text-edit-input,
    body.edasys-app .dxbl-memo textarea {
        font-size: 16px;
    }

    /* CrudEdt uses an inner 77vh scroller with overflow-x:hidden. On phones
       that clips fields, so let the whole page scroll instead. */
    body.edasys-app [data-tour="crud-fields"] {
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
    }

    /* ---------------------------------------------------------------------
       9. Dashboard
       The dashboard uses a 2fr / 1fr DxGridLayout. Stack it on phones.
       ------------------------------------------------------------------ */
    .dashboard-shell > .dxbl-gridlayout-root,
    .dashboard-shell.dxbl-gridlayout-root {
        grid-template-columns: minmax(0, 1fr) !important;
        grid-auto-rows: auto;
    }

    .dashboard-shell > .dxbl-gridlayout-root > *,
    .dashboard-shell.dxbl-gridlayout-root > * {
        grid-column: 1 / -1 !important;
        grid-row: auto !important;
        min-width: 0;
    }

    /* ---------------------------------------------------------------------
       10. Login / account pages
       ------------------------------------------------------------------ */
    .login-foreground {
        padding: 1rem 0.75rem;
    }

    .login-card .links-container {
        flex-wrap: wrap !important;
        row-gap: 0.35rem;
    }

    /* Decorative fixed-width panel would overflow a phone viewport. */
    .bottom-left-box {
        display: none !important;
    }

    /* ---------------------------------------------------------------------
       11. Generic page layouts built with DxGridLayout
       Items are allowed to shrink instead of overflowing the viewport.
       ------------------------------------------------------------------ */
    body.edasys-app .dxbl-gridlayout,
    body.edasys-app .dxbl-gridlayout-root,
    body.edasys-app .dxbl-gridlayout-root > * {
        min-width: 0;
    }

    /* Wide fixed-column layouts scroll inside their own box instead of
       pushing the whole page sideways. */
    body.edasys-app .dxbl-gridlayout {
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    body.edasys-app .dxbl-gridlayout-root > * > * {
        max-width: 100%;
    }
}

/* ============================================================================
   Very small phones (<= 480px)
   ========================================================================== */
@media (max-width: 480px) {
    body.edasys-app .page [data-tour="content"] {
        padding: 0.5rem !important;
    }

    body.edasys-app .dxbl-grid {
        --dxbl-grid-font-size: 0.75rem;
    }

    .nav-buttons-container {
        gap: 0.35rem;
        padding: 0.4rem 0.5rem;
    }

    /* Long document/company names should wrap rather than force a scrollbar. */
    body.edasys-app h1,
    body.edasys-app h2,
    body.edasys-app h3,
    body.edasys-app .dashboard-title h2 {
        overflow-wrap: anywhere;
    }
}
