/* ============================================================================
   Showcase - responsive layer
   ----------------------------------------------------------------------------
   Loaded AFTER v2-styles.css and after every page stylesheet (see _App.cshtml),
   so a rule here wins on order. Nothing in this file applies at 1024px and up:
   the desktop shell renders exactly as the office app does.

     <= 1023px  shell: drawer, phone bar, scrolling tabs, body scroll
     <=  767px  grids become cards
     <=  639px  single column, bottom-sheet modals

   Per-module fixes go in css/<area>/responsive.css, never here.
   See DOCS/Showcase-Mobile.md.
   ============================================================================ */

/* Phone-only elements exist in the markup at every width; they are shown
   only inside the breakpoint below. */
.mobile-bar,
.drawer-backdrop,
.drawer-close,
.drawer-utilities,
.mobile-search-toggle,
.mobile-search-row,
.filters-collapse-btn {
    display: none;
}

/* ============================================================================
   <= 1023px  SHELL
   ============================================================================ */
@media (max-width: 1023.98px) {

    /* clip, not hidden: a hidden overflow can still be scrolled by script, so
       a stray wide widget let the whole page shift sideways. clip cannot. */
    html, body {
        height: auto;
        overflow-x: hidden;
        overflow-x: clip;
    }

    .main-container {
        position: static;
        display: flex;
        flex-direction: column;
        min-height: 100vh;
        min-height: 100dvh;
    }

    /* ---- Phone bar ---- */
    .mobile-bar {
        position: sticky;
        top: 0;
        z-index: 900;
        height: 56px;
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 0 8px 0 4px;
        background: #ffffff;
        border-bottom: 1px solid #e1e8ef;
    }

    .mobile-bar .topbar-logo {
        max-height: 32px;
        width: auto;
        margin-left: 4px;
    }

    .mobile-bar .spacer {
        flex: 1;
    }

    .mobile-bar .icon-btn.plain,
    .drawer-close {
        border-color: transparent;
        width: 44px;
        height: 44px;
        font-size: 20px;
        color: #334155;
    }

    .mobile-bar .user-avatar-menu {
        top: calc(100% + 6px);
    }

    /* ---- Left column becomes a drawer ---- */
    .left-column {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        width: min(300px, 86vw);
        transform: translateX(-102%);
        transition: transform 0.22s ease;
        z-index: 1001;
        box-shadow: none;
    }

    .main-container.drawer-open .left-column {
        transform: none;
        box-shadow: 0 0 40px rgba(15, 23, 42, 0.25);
    }

    .drawer-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.45);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.22s ease;
        z-index: 1000;
    }

    .main-container.drawer-open .drawer-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    .logo-section {
        height: 56px;
        padding: 0 12px 0 20px;
        justify-content: space-between;
    }

    .logo-section .topbar-logo {
        max-height: 36px;
    }

    .drawer-close {
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 8px;
        background: transparent;
        cursor: pointer;
    }

    .sidebar {
        padding-bottom: 12px;
    }

    /* 44px tall nav items: a real thumb target */
    .nav-item {
        padding: 12px 16px;
    }

    .drawer-utilities {
        display: block;
        border-top: 1px solid #e1e8ef;
        padding-top: 8px;
        margin-top: auto;
    }

    /* ---- Right column ---- */
    .right-column {
        min-width: 0;
        overflow: visible;
    }

    .topbar-top-row {
        position: sticky;
        top: 56px;
        z-index: 890;
    }

    .module-tabs-row {
        padding: 0 8px;
        gap: 2px;
        align-items: stretch;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
        scroll-snap-type: x proximity;
        /* fade on the right edge says there is more */
        mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
        -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
    }

    .module-tabs-row::-webkit-scrollbar {
        display: none;
    }

    .module-tab {
        padding: 0 12px;
        font-size: 13px;
        border-radius: 0;
        margin-bottom: 0;
        scroll-snap-align: start;
        gap: 7px;
    }

    .module-tab:hover,
    .module-tab.active {
        background: transparent;
    }

    /* ---- Title row ---- */
    .topbar-bottom-row {
        flex-wrap: wrap;
        align-items: center;
        gap: 12px;
        padding: 10px 16px;
        min-height: 0;
    }

    .topbar-bottom-row .page-info {
        min-width: 0;
        flex: 1;
    }

    .topbar-bottom-row h1 {
        font-size: 20px;
        letter-spacing: -0.4px;
        line-height: 1.2;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .topbar-bottom-row .breadcrumb {
        font-size: 12px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .recents-strip {
        display: none !important;
    }

    .topbar-right .assistant-wrapper,
    .topbar-right .search-wrapper,
    .topbar-right .icon-btn,
    .topbar-right .user-avatar-wrapper {
        display: none;
    }

    .topbar-right .mobile-search-toggle {
        display: inline-flex;
    }

    .mobile-search-row {
        flex-basis: 100%;
    }

    .topbar-bottom-row.search-open .mobile-search-row {
        display: block;
    }

    .mobile-search-row .search-wrapper {
        display: block;
    }

    .mobile-search-row .search-box {
        width: 100% !important;
    }

    /* Toasts sit under the phone bar + tabs */
    .main-container ~ #toast-container.toast-top-right {
        top: 108px;
    }

    /* ---- Content: the body scrolls, not the page ---- */
    .content-area,
    .main-content {
        overflow: visible;
    }

    .dashboard-content {
        overflow: visible;
        padding: 12px 12px 48px;
    }

    .page-content {
        height: auto;
        overflow: visible;
        padding: 4px;
    }

    /* ---- Page header ---- */
    .page-header,
    .page-header-actions {
        flex-wrap: wrap;
        gap: 10px;
    }

    /* ---- Stat card grids: two up ---- */
    .dashboard-stats,
    .customer-stats-grid,
    .ai-stats-grid,
    .agreement-stats,
    .kpi-row,
    .kpi-band,
    .jobs-panel-stats,
    .aging-cards,
    .aging-buckets,
    .quote-summary,
    .pipeline-summary,
    .info-metrics,
    .module-summaries,
    .billing-summary-grid,
    .pulse-band,
    .exposure-strip,
    .entity-type-grid,
    [class$="-stats-grid"],
    [class$="-stat-grid"],
    [class$="-kpi-grid"] {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 12px !important;
    }

    .stat-card {
        padding: 18px;
    }

    .stat-value {
        font-size: 26px;
    }

    /* ---- Two-column page layouts: stack ---- */
    .dashboard-grid,
    .overview-grid,
    .billing-grid,
    .charts-grid,
    .chart-grid,
    .form-grid,
    .form-grid-3,
    [class$="-two-col"],
    [class$="-columns"] {
        grid-template-columns: 1fr !important;
    }

    /* ---- Filters ---- */
    .filter-section {
        padding: 12px;
    }

    .filter-section > div[style*="display: flex"] {
        flex-wrap: wrap;
    }

    /* The inline "flex: 1" shorthand sets flex-basis: 0%, so this needs the
       hammer to take the search input onto its own full-width line. */
    .filter-section > div[style*="display: flex"] > div[style*="flex: 1"],
    .filter-section input.filter-input[style*="flex: 1"] {
        flex-basis: 100% !important;
    }

    .filter-section .filter-input {
        width: 100%;
    }

    .filter-section .filters-collapse-btn {
        display: inline-flex;
    }

    .filter-section:not(.filters-open) .filter-toggle-btn {
        display: none;
    }

    .filter-section.filters-open .filter-toggle-btn {
        flex: 1 1 calc(50% - 12px);
        justify-content: center;
    }

    .filter-dropdown {
        left: 12px;
        right: 12px;
        width: auto;
        max-width: none;
    }

    /* ---- Tables: never clip ---- */
    .table-container {
        overflow-x: auto;
    }

    /* ---- Inline flex rows wrap instead of overflowing the phone ----
       Not inside a grid cell: an icon + text pair in a cell must stay a pair. */
    .page-content div[style*="display: flex"]:not(td *, .data-table *),
    .dashboard-content > div[style*="display: flex"] {
        flex-wrap: wrap;
    }
}

/* ============================================================================
   768px - 1023px  TABLET TABLES
   The table scrolls sideways; keep headers and the first column (numbers,
   names) on one line so a narrow column does not wrap an ID onto three rows.
   ============================================================================ */
@media (min-width: 768px) and (max-width: 1023.98px) {
    .data-table th,
    .data-table td:first-child {
        white-space: nowrap;
    }

    /* ---- Charts follow their box ---- */
    canvas {
        max-width: 100%;
    }
}

/* ============================================================================
   <= 767px  GRIDS BECOME CARDS
   ============================================================================ */
@media (max-width: 767.98px) {

    .data-table thead {
        display: none;
    }

    .data-table,
    .data-table tbody,
    .data-table tr,
    .data-table td {
        display: block;
    }

    .data-table {
        min-width: 0 !important;
    }

    .data-table tr {
        padding: 10px 14px 8px;
        border-bottom: 1px solid #f1f5f9;
    }

    .data-table tr:last-child {
        border-bottom: none;
    }

    /* Label left, value(s) clustered right. A cell that holds two pieces
       (date + time, badge + note) keeps them together because only the label
       carries the auto margin. */
    .data-table td {
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-end;
        align-items: center;
        gap: 4px 8px;
        padding: 5px 0 !important;
        text-align: right !important;
        white-space: normal !important;
        min-width: 0;
        width: auto !important;
    }

    .data-table td[data-label]::before {
        content: attr(data-label);
        font-size: 11px;
        font-weight: 700;
        color: #64748b;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        text-align: left;
        flex-shrink: 0;
        margin-right: auto;
    }

    /* The first cell is the card head: bold, no dashed underline needed */
    .data-table td:first-child {
        font-weight: 600;
        color: #0f172a;
        padding-bottom: 8px !important;
        margin-bottom: 2px;
        border-bottom: 1px dashed #f1f5f9;
    }

    /* Empty-state and group rows span the card */
    .data-table td[colspan] {
        display: block;
        text-align: center !important;
        border-bottom: none;
        font-weight: 400;
    }

    .data-table td[colspan]::before {
        content: none;
    }

    /* Action tiles wrap in a row under the card */
    .data-table td.actions-cell {
        justify-content: flex-start;
        flex-wrap: wrap;
        gap: 8px;
        padding-top: 10px !important;
        text-align: left !important;
    }

    .data-table td.actions-cell::before {
        content: none;
    }

    .data-table td.actions-cell > * {
        flex: 1 1 auto;
        justify-content: center;
        margin-left: 0 !important;
    }

    /* Selection checkbox cells */
    .data-table td:has(> input[type="checkbox"]:only-child) {
        justify-content: flex-start;
        border-bottom: none;
        padding-bottom: 0 !important;
    }

    .pagination-container {
        flex-direction: column;
        gap: 12px;
        align-items: stretch;
    }

    .pagination-info,
    .pagination-controls,
    .pagination-buttons,
    .page-size-selector {
        justify-content: center;
        text-align: center;
    }
}

/* ============================================================================
   <= 639px  SINGLE COLUMN
   ============================================================================ */
@media (max-width: 639.98px) {

    .dashboard-stats,
    .customer-stats-grid,
    .ai-stats-grid,
    .agreement-stats,
    .kpi-row,
    .kpi-band,
    .jobs-panel-stats,
    .aging-cards,
    .aging-buckets,
    .quote-summary,
    .pipeline-summary,
    .info-metrics,
    .module-summaries,
    .billing-summary-grid,
    .pulse-band,
    .exposure-strip,
    .entity-type-grid,
    [class$="-stats-grid"],
    [class$="-stat-grid"],
    [class$="-kpi-grid"] {
        grid-template-columns: 1fr !important;
    }

    .form-row {
        flex-direction: column;
        gap: 12px;
    }

    .form-row .form-group {
        width: 100%;
        flex: none;
    }

    /* Page-header buttons: two up, secondary spans the row */
    .page-header-actions {
        display: grid;
        grid-template-columns: 1fr 1fr;
        width: 100%;
    }

    /* A secondary spans the row; so does a lone button, and a primary left
       alone on the second row after a single secondary (Export + Add). */
    .page-header-actions > .btn-secondary,
    .page-header-actions > .btn:only-child,
    .page-header-actions > .btn-secondary + .btn:last-child {
        grid-column: 1 / -1;
    }

    .page-header-actions > .btn {
        justify-content: center;
        min-height: 44px;
    }

    /* House modals become bottom sheets */
    .modal-overlay {
        padding: 0;
        align-items: flex-end;
    }

    .modal-container,
    .modal-container.modal-sm,
    .modal-container.modal-lg,
    .modal-container.modal-xl {
        max-width: 100%;
        max-height: 96vh;
        border-radius: 16px 16px 0 0;
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 16px;
    }

    .modal-title {
        font-size: 17px;
    }

    .filter-section.filters-open .filter-toggle-btn {
        flex-basis: 100%;
    }
}
