/* Phone and tablet layer for the authenticated shell. Loaded last, after every other stylesheet.
   Below 992px the sidebar becomes an off-canvas drawer (driven by js/mobile-shell.js); below 768px
   pages get phone-sized spacing, 16px form controls (stops iOS zoom-on-focus) and touch targets. */

/* Flex children default to min-width:auto, which lets one wide table stretch the whole page. */
#content-wrapper,
#content,
#main-content {
    min-width: 0;
}

img,
canvas,
svg,
video {
    max-width: 100%;
}

.mobile-scroll {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
}

.mobile-nav-backdrop,
.mobile-nav-close {
    display: none;
}

/* The closed settings flyout sits just off-screen; hide it so its shadow and controls don't leak in. */
.sidebar-settings-panel {
    visibility: hidden;
    transition: transform 0.28s ease, visibility 0s linear 0.28s;
}

.sidebar-settings-panel.is-open {
    visibility: visible;
    transition: transform 0.28s ease, visibility 0s;
}

/* ---------- Drawer navigation (tablet portrait and phones) ---------- */
@media (max-width: 991.98px) {
    #sidebarToggleTop {
        display: inline-flex !important;
    }

    .app-topbar .btn {
        white-space: nowrap;
    }

    #accordionSidebar,
    #accordionSidebar.toggled {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        z-index: 1045;
        width: min(18rem, 86vw) !important;
        flex: none;
        height: 100vh;
        height: 100dvh;
        padding-top: calc(1.1rem + env(safe-area-inset-top));
        padding-bottom: calc(1.2rem + env(safe-area-inset-bottom));
        overflow: hidden;
        visibility: hidden;
        transform: translateX(-100%);
        transition: transform 0.25s ease, visibility 0s linear 0.25s;
        box-shadow: none;
    }

    body.mobile-nav-open #accordionSidebar {
        visibility: visible;
        transform: none;
        transition: transform 0.25s ease, visibility 0s;
        box-shadow: 0 20px 60px rgba(15, 23, 42, 0.35) !important;
    }

    .mobile-nav-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 1040;
        background: rgba(15, 23, 42, 0.5);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
    }

    body.mobile-nav-open .mobile-nav-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    body.mobile-nav-open {
        overflow: hidden;
    }

    .mobile-nav-close {
        display: inline-flex;
    }

    /* Desktop-only rail controls have no meaning inside the drawer. */
    #sidebarToggle,
    [data-settings-compact],
    .sidebar-expand-handle,
    #accordionSidebar.toggled + .sidebar-expand-handle,
    .quick-action kbd,
    .sidebar-pill {
        display: none !important;
    }

    .sidebar-link,
    .sidebar-sublink {
        min-height: 44px;
    }

    .sidebar-quick-actions {
        grid-template-columns: 1fr;
    }
}

/* ---------- Phones ---------- */
@media (max-width: 767.98px) {
    .app-topbar {
        min-height: 56px;
        padding: 0.5rem 0.75rem;
        padding-top: calc(0.5rem + env(safe-area-inset-top));
        gap: 0.5rem !important;
    }

    .app-topbar__mobile-brand {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-topbar .app-icon-button {
        width: 44px;
        height: 44px;
        flex: 0 0 44px;
    }

    #content > .container-fluid,
    #main-content {
        padding: 1rem 0.75rem 1.5rem;
        padding-bottom: calc(1.5rem + env(safe-area-inset-bottom));
    }

    h1, .h1 { font-size: 1.5rem; }
    h2, .h2 { font-size: 1.3rem; }
    h3, .h3 { font-size: 1.15rem; }

    .ui-page-header {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
        margin-bottom: 1rem;
    }

    .ui-page-header__copy,
    .ui-page-header__copy > * {
        min-width: 0;
        overflow-wrap: anywhere;
    }

    .ui-page-header__actions {
        width: 100%;
        flex-wrap: wrap;
        justify-content: flex-start;
    }

    .ws-page-chip,
    .ui-status,
    .badge {
        max-width: 100%;
        white-space: normal;
        overflow-wrap: anywhere;
    }

    /* Any flex row of controls wraps rather than pushing past the screen edge. */
    .card-header,
    .ui-card__header,
    .d-flex.justify-content-between,
    .filter-actions,
    .btn-toolbar {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .card-body,
    .ui-card__body {
        padding: 1rem;
    }

    .row {
        margin-left: -0.5rem;
        margin-right: -0.5rem;
    }

    .row > [class*="col"] {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }

    /* 16px keeps iOS Safari from zooming the page when a field is focused. */
    input.form-control,
    select.form-control,
    textarea.form-control,
    .form-select,
    .custom-select,
    .select2-container .select2-selection,
    .select2-search__field,
    input[type="search"] {
        font-size: 16px !important;
    }

    .form-control:not(textarea),
    .form-select,
    .custom-select {
        min-height: 44px;
    }

    .select2-container {
        width: 100% !important;
        max-width: 100%;
    }

    .select2-container .select2-selection--single {
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    .btn:not(.btn-sm):not(.btn-link):not(.close) {
        min-height: 44px;
    }

    .btn-sm {
        min-height: 36px;
    }

    .input-group {
        flex-wrap: nowrap;
    }

    /* Tables scroll inside their card instead of widening the page. */
    .table-responsive,
    .dataTables_wrapper {
        overflow-x: auto;
        overscroll-behavior-inline: contain;
        -webkit-overflow-scrolling: touch;
    }

    .table th,
    .table td {
        white-space: nowrap;
    }

    .dataTables_wrapper .row > [class*="col-"] {
        flex: 0 0 100%;
        max-width: 100%;
        text-align: left !important;
    }

    .dataTables_wrapper .dataTables_filter input {
        width: 100% !important;
        margin-left: 0 !important;
    }

    .dataTables_wrapper .dataTables_paginate .pagination {
        flex-wrap: wrap;
        justify-content: flex-start !important;
    }

    .nav-tabs,
    .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        scrollbar-width: none;
    }

    .nav-tabs .nav-link,
    .nav-pills .nav-link {
        white-space: nowrap;
    }

    .modal-dialog {
        margin: 0.5rem;
        max-width: none;
    }

    .modal-dialog-scrollable .modal-content,
    .modal-content {
        max-height: calc(100dvh - 1rem);
    }

    .modal-body {
        overflow-y: auto;
    }

    .modal-footer {
        flex-wrap: wrap;
    }

    .dropdown-menu {
        max-width: calc(100vw - 1.5rem);
    }

    #toast-container > div {
        width: calc(100vw - 1.5rem) !important;
    }
}
