/*
 * Site-wide responsive and visual layer.
 *
 * Lives in public/ and is linked from <body> on purpose:
 *  - after every <head> stylesheet (including the webpack bundle), so it wins ties;
 *  - outside the build, so PurgeCSS cannot strip rules aimed at library classes
 *    (Mui*, rdt_*, rdr*, react-datepicker) whose names only exist at runtime.
 *
 * Breakpoints match Bootstrap: 991.98 drawer/tablet, 767.98 phone landscape, 575.98 phone.
 * Layout rules are screen-only: print width falls under these breakpoints too.
 */

:root {
    --oims-primary: var(--primary-bg-color, #1170b4);
    --oims-bg: #f4f6fb;
    --oims-border: #e6eaf2;
    --oims-radius: 14px;
    --oims-radius-sm: 9px;
    --oims-shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 6px 18px rgba(16, 24, 40, 0.05);
    --oims-shadow-lg: 0 18px 48px rgba(16, 24, 40, 0.18);
    --oims-ease: cubic-bezier(0.32, 0.72, 0, 1);
    --oims-safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- Look and feel, all widths ---------- */

body.app {
    background-color: var(--oims-bg);
    text-rendering: optimizeLegibility;
}

.page .card {
    border-color: var(--oims-border);
    border-radius: var(--oims-radius);
    box-shadow: var(--oims-shadow);
}

.page .card > .card-header:first-child {
    border-top-left-radius: var(--oims-radius);
    border-top-right-radius: var(--oims-radius);
}

.page .card > .card-footer:last-child {
    border-bottom-left-radius: var(--oims-radius);
    border-bottom-right-radius: var(--oims-radius);
}

.app-header.header {
    border-bottom-color: var(--oims-border);
    box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 4px 14px rgba(16, 24, 40, 0.04);
}

.page :not(.btn-group):not(.input-group) > .btn:not(.rounded-pill):not(.btn-pill):not(.support-button) {
    border-radius: var(--oims-radius-sm);
}

.page .btn,
.modal .btn {
    transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}

.page .btn:active:not(:disabled),
.modal .btn:active:not(:disabled) {
    transform: scale(0.98);
}

.page :not(.input-group) > .form-control,
.page :not(.input-group) > .form-select,
.modal :not(.input-group) > .form-control,
.modal :not(.input-group) > .form-select {
    border-radius: var(--oims-radius-sm);
}

.page .form-control:focus,
.page .form-select:focus,
.modal .form-control:focus,
.modal .form-select:focus {
    border-color: var(--oims-primary);
    box-shadow: 0 0 0 3px var(--primary02, rgba(17, 112, 180, 0.2));
}

.modal-content {
    border: 0;
    border-radius: 16px;
    box-shadow: var(--oims-shadow-lg);
}

.modal-header {
    border-top-left-radius: 16px;
    border-top-right-radius: 16px;
}

.modal-footer {
    border-bottom-left-radius: 16px;
    border-bottom-right-radius: 16px;
}

/* ---------- Small laptops and below: content column is ~730px beside the sidebar ---------- */

@media screen and (max-width: 1199.98px) {
    .main-content .d-flex.flex-row {
        flex-wrap: wrap;
        gap: 8px;
    }

    .pa-header {
        flex-wrap: wrap;
        gap: 12px;
        height: auto;
    }

    .pa-header-center,
    .pa-header-right {
        flex-wrap: wrap;
    }

    /* Portal rows: the action buttons run ~20px past the column at 1024px */
    .pa-row {
        flex-wrap: wrap;
        row-gap: 10px;
    }

    .pa-col--actions {
        flex: 1 1 100%;
        min-width: 0;
        justify-content: flex-start;
    }

    /* List pages float their search box over the table with inline styles, often at w-25 */
    .main-content .d-flex[style*='position: absolute']:has(input, select) {
        position: static !important;
        float: none !important;
        width: 100% !important;
        margin: 0 0 10px !important;
        gap: 6px;
    }

    .main-content .d-flex[style*='position: absolute']:has(input, select) > input[type='text'],
    .main-content .d-flex[style*='position: absolute']:has(input, select) > input[type='search'],
    .main-content .d-flex[style*='position: absolute']:has(input, select) > div {
        flex: 1 1 auto;
        min-width: 0;
    }

    /* Claim create forms indent fields with ms-5, which pushes them off the column */
    .main-content .form-group.ms-5 {
        margin-left: 0 !important;
    }

    .main-content :has(> .form-group.ms-5) {
        flex-wrap: wrap;
        gap: 8px;
    }

    .cw-container div[style*='display: flex'] {
        flex-wrap: wrap;
    }

    .cw-container div {
        max-width: 100%;
    }

    .cw-registry-header {
        flex-wrap: wrap;
        gap: 12px;
    }

    /* The page title floats over the same corner as the search box */
    .main-content .card-body > .d-flex[style*='position: absolute']:has(h3, h4, h5) {
        position: static !important;
        float: none !important;
        width: 100% !important;
        margin: 0 0 8px !important;
    }

    /* Claim view action row: Super Admin gets enough buttons to overflow at laptop width */
    .main-content .card-body.d-flex,
    .main-content .card-body.d-flex > .d-flex {
        flex-wrap: wrap;
        max-width: 100%;
    }

    .main-content .card-body.d-flex > .d-flex {
        gap: 8px;
    }

    .main-content .card-body.d-flex > .d-flex > .btn {
        margin-left: 0 !important;
    }
}

/* One- and two-column filter fields are too narrow to use until the desktop grid has room */
@media screen and (min-width: 768px) and (max-width: 1199.98px) {
    .main-content .row > :is(.col-md-1, .col-md-2, .col-lg-1, .col-lg-2):has(input, select, [class*='-control']) {
        flex: 0 0 33.333%;
        width: 33.333%;
        max-width: 33.333%;
    }
}

/* ---------- Tablet and below: drawer navigation and page shell ---------- */

@media screen and (max-width: 991.98px) {
    /* The template stretches the logo across the whole header, on top of both buttons */
    .app-header .logo-horizontal {
        left: 56px;
        right: 52px;
        width: auto;
        margin: 0;
        text-align: center;
    }

    .app-header .logo-horizontal .header-brand-img {
        width: auto;
        max-width: 100%;
        max-height: 38px;
        margin: 15px auto 0;
    }

    .app-header .app-sidebar__toggle {
        z-index: 2;
        min-width: 44px;
        min-height: 44px;
    }

    .app .app-sidebar {
        left: 0;
        max-width: 86vw;
        transform: translate3d(-105%, 0, 0);
        transition: transform 280ms var(--oims-ease);
        will-change: transform;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        padding-bottom: var(--oims-safe-bottom);
    }

    .app.sidenav-toggled .app-sidebar,
    .app.sidebar-mini.sidebar-gone.sidenav-toggled .app-sidebar {
        left: 0;
        transform: none;
        box-shadow: var(--oims-shadow-lg);
    }

    .app .app-sidebar__overlay {
        z-index: 98;
        background: rgba(15, 23, 42, 0.45);
        opacity: 0;
        transition: opacity 240ms ease, visibility 240ms;
    }

    .app.sidenav-toggled .app-sidebar__overlay {
        opacity: 1;
    }

    /* The page behind an open drawer must not scroll with the finger */
    body.app.sidenav-toggled {
        overflow: hidden;
    }

    body.app.sidenav-toggled .support-container {
        display: none;
    }

    .app-sidebar .menu-search-container {
        top: 0 !important;
    }

    .announcement-bar {
        margin-left: 12px !important;
        margin-right: 12px !important;
    }

    .announcement-bar .alert {
        flex-wrap: wrap;
        gap: 8px;
    }

    .app-content .side-app {
        padding: 0 12px 76px !important;
    }

    .app-content .main-container {
        padding-left: 0;
        padding-right: 0;
    }

    /* The template sets overflow-x:hidden on body here, so anything wider than the
       screen is cut off with no way to reach it. Keep content inside its column. */
    .main-content :where(img, video, canvas, iframe) {
        max-width: 100%;
    }

    .main-content .card-body,
    .modal-body {
        overflow-wrap: break-word;
    }

    /* Any table without its own wrapper scrolls inside its parent, not off the page */
    .main-content div:has(> table),
    .modal-body div:has(> table),
    .modal-body span:has(> table) {
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal-body span:has(> table) {
        display: block;
    }

    .main-content table th,
    .modal-body table th {
        white-space: nowrap;
    }

    .MuiTablePagination-toolbar,
    .rdt_Pagination {
        flex-wrap: wrap;
        justify-content: flex-end;
        height: auto !important;
        min-height: 48px;
    }

    .MuiTablePagination-spacer {
        display: none;
    }

    .page-header {
        flex-wrap: wrap;
        gap: 8px;
    }

    .card-header {
        flex-wrap: wrap;
        gap: 8px;
    }

    .card-options {
        margin-left: 0;
    }

    /* Portal Automation */
    .pa-header {
        flex-wrap: wrap;
        gap: 12px;
        height: auto;
    }

    .pa-header-center,
    .pa-header-right {
        flex: 1 1 100%;
        flex-wrap: wrap;
        justify-content: flex-start;
    }

    .pa-row {
        flex-wrap: wrap;
        gap: 12px;
        padding: 14px;
    }

    .pa-row:hover {
        transform: none;
    }

    .pa-col--identity,
    .pa-col--status,
    .pa-col--config,
    .pa-col--actions {
        flex: 1 1 100%;
        min-width: 0;
        flex-wrap: wrap;
        justify-content: flex-start;
    }

    .pa-col--config {
        gap: 14px;
    }

    .pa-action-group {
        flex-wrap: wrap;
    }

    /* Claim Workflow */
    .cw-container {
        padding: 1rem 0;
    }

    .cw-grid {
        grid-template-columns: minmax(0, 1fr);
        gap: 1.25rem;
    }

    .cw-sidebar-card {
        position: static;
        padding: 1.25rem;
        border-radius: 18px;
    }

    .cw-canvas-placeholder {
        margin-top: 1rem;
        padding: 1.5rem;
        border-radius: 18px;
    }

    .cw-registry-header {
        flex-wrap: wrap;
        gap: 12px;
        margin-top: 2rem;
        padding-top: 2rem;
    }

    .cw-registry-title-block,
    .cw-header,
    .cw-header > div {
        flex-wrap: wrap;
        min-width: 0;
    }

    .cw-header {
        gap: 12px;
    }

}

/* ---------- Phones ---------- */

@media screen and (max-width: 767.98px) {
    /* iOS zooms the whole page when a focused field is under 16px */
    .page input:not([type='checkbox']):not([type='radio']),
    .page select,
    .page textarea,
    .modal input:not([type='checkbox']):not([type='radio']),
    .modal select,
    .modal textarea {
        font-size: 16px;
    }

    .main-content .btn:not(.btn-sm):not(.btn-xs):not(.btn-icon):not(table *),
    .modal .btn:not(.btn-sm):not(.btn-xs):not(.btn-icon):not(table *) {
        min-height: 42px;
    }

    .main-content .card-body .d-flex.justify-content-between,
    .card-header-modern,
    .card-header-modern > .d-flex,
    .grid-table-controls,
    .hc-head {
        flex-wrap: wrap;
        gap: 8px;
    }

    .main-content select:not(table *),
    .main-content input:not(table *),
    .main-content .input-group:not(table *) {
        max-width: 100%;
    }

    .grid-table-search,
    .grid-table-search .input-group {
        width: 100% !important;
        max-width: 100%;
    }

    .hc-head > label {
        flex: 1 1 100%;
        min-width: 0;
    }

    /* Action rows on claim edit, leave balance and list items */
    .main-content .d-flex.gap-2:not(table *),
    .main-content .d-flex.gap-3:not(table *),
    .main-content .d-flex:has(> .d-flex.gap-2),
    .main-content li.list-group-item.d-flex,
    .cw-log-dates,
    .cw-log-head-actions {
        flex-wrap: wrap;
        max-width: 100%;
    }

    .main-content .row > .col-auto {
        max-width: 100%;
    }

    .main-content .d-flex.gap-3 > .btn {
        min-width: 0 !important;
    }

    .cw-log-dates .cw-input {
        flex: 1 1 130px;
        width: auto;
    }

    /* Outstanding pages float a fixed-width filter */
    .main-content .float-start:has(select, input) {
        float: none !important;
        width: 100% !important;
        max-width: 100%;
    }

    .main-content .float-start:has(select, input) > *,
    .main-content .float-start:has(select, input) [class*='-control'] {
        max-width: 100%;
    }

    /* Inputs inside tables collapse to ~20px because the column has no minimum */
    .main-content table input.form-control,
    .main-content table select.form-select {
        min-width: 72px;
    }

    .main-content .btn:not(table *) {
        max-width: 100%;
        white-space: normal;
    }

    /* Archive lists put a dropdown and the search box in one unwrapped row */
    .main-content .d-flex.justify-content-start:has(> .d-flex[style*='position: absolute']) {
        flex-wrap: wrap;
        gap: 8px;
    }

    .main-content .d-flex.justify-content-start:has(> .d-flex[style*='position: absolute']) > div {
        flex: 1 1 100%;
        min-width: 0;
    }

    .main-content .card-body {
        padding: 14px;
    }

    .react-datepicker-popper {
        max-width: calc(100vw - 16px);
    }

    .rdrDateRangePickerWrapper {
        flex-direction: column;
        max-width: 100%;
    }

    .rdrDefinedRangesWrapper {
        display: none;
    }

    .rdrCalendarWrapper,
    .rdrMonth {
        width: 100%;
        max-width: 100%;
    }

    .rdrMonths {
        flex-direction: column;
    }
}

@media screen and (max-width: 575.98px) {
    h1,
    .h1 {
        font-size: 1.5rem;
    }

    h2,
    .h2 {
        font-size: 1.3rem;
    }

    h3,
    .h3 {
        font-size: 1.15rem;
    }

    .main-content :has(> .legend-item-compact),
    .main-content .d-flex:has(> .ms-auto),
    .main-content :has(> .badge-light-glass),
    .main-content :has(> .pa-action-group),
    .main-content .card-header > div,
    .main-content .card-header > div > .d-flex,
    .main-content .d-flex.ms-auto,
    .pa-header-left {
        flex-wrap: wrap;
        flex-shrink: 1;
        min-width: 0;
        gap: 8px;
    }

    .hc-head select {
        width: 100%;
        min-width: 0;
    }

    .auth-logo-badge {
        flex-wrap: wrap;
        justify-content: center;
        max-width: 100%;
    }

    .modal-dialog {
        margin: 8px auto;
        max-width: calc(100vw - 16px);
    }

    .modal-title,
    .modal-title h1,
    .modal-title h2,
    .modal-title h3 {
        font-size: 1.05rem;
        line-height: 1.3;
    }

    .modal-body.p-5 {
        padding: 1.25rem !important;
    }

    .modal-footer {
        flex-wrap: wrap;
        gap: 6px;
        padding-bottom: calc(12px + var(--oims-safe-bottom));
    }

    .modal-footer > span.d-flex {
        flex-wrap: wrap;
        gap: 6px;
    }

    .modal-footer > span.d-flex > .btn {
        margin-left: 0 !important;
        margin-right: 0 !important;
    }

    .support-container {
        right: 14px !important;
        bottom: calc(14px + var(--oims-safe-bottom)) !important;
    }

    .support-modal .modal-dialog {
        left: 8px !important;
        right: 8px !important;
        bottom: calc(74px + var(--oims-safe-bottom)) !important;
        width: auto !important;
        max-width: none !important;
    }

    .support-modal .modal-content {
        max-height: calc(100vh - 96px);
        overflow-y: auto;
    }
}

@media (prefers-reduced-motion: reduce) {
    .app .app-sidebar,
    .app .app-sidebar__overlay,
    .page .btn,
    .modal .btn {
        transition: none;
    }
}
