/**
 * Production responsive certification — CSS fallbacks that do not depend on
 * DeviceService / .mobile-mode being applied before first paint.
 *
 * Canonical breakpoints (Phase 53B):
 *   Phone  ≤768px
 *   Tablet 769–1024px
 *   Desktop >1024px
 */

:root {
    --fo-r-page-pad: 24px;
    --fo-r-card-pad: 20px;
    --fo-r-title: 1.5rem;
    --fo-r-section: 1.1rem;
    --fo-r-body: 0.9375rem;
    --fo-r-btn-min: 40px;
    --fo-r-input-min: 38px;
    --fo-r-row: 2.5rem;
    --fo-r-modal-radius: 0.5rem;
}

/* ── Phone (≤768px) ─────────────────────────────────────────────── */
@media (max-width: 768px) {
    :root {
        --fo-r-page-pad: 0.65rem;
        --fo-r-card-pad: 0.75rem;
        --fo-r-title: 1.2rem;
        --fo-r-section: 1rem;
        --fo-r-body: 0.875rem;
        --fo-r-btn-min: 44px;
        --fo-r-input-min: 44px;
        --fo-r-row: 2.75rem;
        --fo-r-modal-radius: 0.75rem;
    }

    .fieldops-shell .mobile-nav-bar {
        display: grid;
    }

    html body .fieldops-shell .main-content .content-area,
    html body .fieldops-shell .content-area,
    html body .content-area {
        padding: 0.65rem !important;
        padding-bottom: calc(4.25rem + env(safe-area-inset-bottom, 0)) !important;
    }

    .fieldops-topbar {
        height: 48px;
        min-height: 48px;
        padding: 0 0.5rem;
        gap: 8px;
    }

    .company-branding-nav-btn {
        display: inline-flex !important;
        width: 44px;
        height: 44px;
    }

    .company-branding-powered {
        display: none;
    }

    .company-branding-name {
        font-size: 0.85rem;
        max-width: 140px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .topbar-brand {
        max-width: none;
        flex: 1;
        min-width: 0;
    }

    .topbar-search-label,
    .topbar-search-kbd {
        display: none;
    }

    /* Density: headings + cards */
    h1, .h1, .fo-page-title, .page-title {
        font-size: var(--fo-r-title) !important;
        line-height: 1.25;
        margin-bottom: 0.35rem;
    }

    h2, .h2, .fo-section-title {
        font-size: var(--fo-r-section) !important;
        line-height: 1.3;
    }

    .kpi-card,
    .card,
    .fo-card,
    .fo-kpi-card {
        padding: var(--fo-r-card-pad) !important;
        border-radius: 0.5rem;
    }

    .kpi-value {
        font-size: 1.25rem !important;
    }

    .fo-kpi-card {
        min-width: 0 !important;
    }

    .fo-kpi-strip {
        gap: 0.5rem;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .fo-page-body,
    .fo-workspace-landing {
        padding: var(--fo-r-page-pad);
    }

    .fo-side-panel {
        width: 100vw;
        max-width: 100vw;
    }

    /* Modals → near full-screen sheets */
    .modal-dialog {
        margin: 0.35rem;
        max-width: calc(100vw - 0.7rem);
    }

    .modal-content,
    .dispatch-popover,
    .fo-modal-content {
        max-height: calc(100dvh - 0.7rem);
        overflow-y: auto;
        border-radius: var(--fo-r-modal-radius);
    }

    .modal-header,
    .modal-footer,
    .dispatch-popover-header,
    .dispatch-popover-footer {
        padding: 0.65rem 0.85rem;
    }

    .modal-body,
    .dispatch-popover-body {
        padding: 0.75rem 0.85rem;
    }

    .fo-action-bar {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }

    .fo-action-bar-actions {
        width: 100%;
        justify-content: flex-end;
        flex-wrap: wrap;
        gap: 0.4rem;
    }

    .estimate-layout,
    .dispatch-container,
    .fo-two-col,
    .fo-split-layout,
    .row.g-4 > [class*="col-"] {
        /* keep bootstrap cols; force stack for known splits */
    }

    .estimate-layout,
    .dispatch-container,
    .fo-two-col,
    .fo-split-layout {
        flex-direction: column !important;
    }

    .table-responsive,
    .fo-table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin-inline: -0.15rem;
    }

    .fo-table,
    .table {
        min-width: 0;
        font-size: 0.8125rem;
    }

    /* Prefer card lists when marked; otherwise allow compact scroll */
    .fo-table:not(.fo-table--force-scroll) {
        min-width: 100%;
    }

    .fo-table--force-scroll {
        min-width: 480px;
    }

    .table > :not(caption) > * > * {
        padding: 0.45rem 0.5rem;
    }

    button.btn,
    .btn,
    .fo-btn,
    .fo-workspace-tool-btn {
        min-height: var(--fo-r-btn-min);
        padding-top: 0.4rem;
        padding-bottom: 0.4rem;
        font-size: 0.875rem;
    }

    .btn-sm,
    .btn-group-sm > .btn {
        min-height: 40px;
        padding: 0.35rem 0.65rem;
    }

    input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="range"]),
    select,
    textarea,
    .form-control,
    .form-select {
        font-size: 16px !important; /* iOS zoom guard */
        min-height: var(--fo-r-input-min);
        padding-top: 0.4rem;
        padding-bottom: 0.4rem;
    }

    .form-label,
    label.form-label {
        margin-bottom: 0.2rem;
        font-size: 0.8rem;
    }

    .nav-tabs .nav-link {
        padding: 0.45rem 0.7rem;
        font-size: 0.85rem;
    }

    .badge {
        font-size: 0.7rem;
    }

    .container-xl,
    .container-lg,
    .container {
        padding-left: var(--fo-r-page-pad);
        padding-right: var(--fo-r-page-pad);
        max-width: 100%;
    }

    /* Dispatch phone chrome */
    .dispatch-board--mobile .dispatch-mobile-summary {
        gap: 0.5rem;
        padding: 0.5rem;
    }

    .dispatch-mobile-stat .val {
        font-size: 1.15rem;
    }

    .dispatch-popover {
        position: fixed;
        left: 0.35rem;
        right: 0.35rem;
        bottom: calc(0.35rem + env(safe-area-inset-bottom, 0));
        top: auto;
        max-height: min(88dvh, 640px);
        width: auto;
        transform: none;
    }
}

/* ── Tablet (769–1024px) ──────────────────────────────────────── */
@media (min-width: 769px) and (max-width: 1024px) {
    :root {
        --fo-r-page-pad: 14px;
        --fo-r-card-pad: 14px;
        --fo-r-title: 1.35rem;
        --fo-r-section: 1.05rem;
        --fo-r-btn-min: 42px;
        --fo-r-input-min: 40px;
    }

    .main-sidebar {
        width: 180px;
        min-width: 180px;
    }

    .content-area {
        padding: var(--fo-r-page-pad) !important;
    }

    .topbar-brand {
        max-width: 180px;
    }

    h1, .h1, .fo-page-title {
        font-size: var(--fo-r-title) !important;
    }

    .kpi-card,
    .card,
    .fo-card,
    .fo-kpi-card {
        padding: var(--fo-r-card-pad) !important;
    }

    .kpi-value {
        font-size: 1.35rem !important;
    }

    .fo-kpi-card {
        min-width: 132px;
    }

    .fo-workspace-landing-grid {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    }

    .dispatch-columns {
        grid-template-columns: 220px 1fr;
        gap: 0.65rem;
    }

    .dispatch-col--right {
        display: none;
    }

    .dispatch-crew-columns {
        gap: 0.5rem;
    }

    .dispatch-crew-column {
        min-width: 200px;
    }

    .modal-dialog {
        max-width: min(560px, calc(100vw - 2rem));
    }

    button.btn,
    .btn,
    .fo-btn {
        min-height: var(--fo-r-btn-min);
    }

    .container-xl {
        max-width: 100%;
        padding-left: 1rem;
        padding-right: 1rem;
    }
}

/* ── Phone landscape ──────────────────────────────────────────── */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
    .fieldops-topbar {
        height: 42px;
        min-height: 42px;
    }

    .fieldops-shell .content-area {
        padding-bottom: calc(3.5rem + env(safe-area-inset-bottom, 0)) !important;
    }

    .mobile-nav-bar {
        height: 48px;
    }

    h1, .h1, .fo-page-title {
        font-size: 1.1rem !important;
    }
}

/* ── Small laptop (≤1366px) ───────────────────────────────────── */
@media (max-width: 1366px) {
    .fo-kpi-card {
        min-width: 140px;
    }
}

/* Class-based density (DeviceService .mobile-mode / .tablet-mode) mirrors media queries */
.fieldops-shell.mobile-mode .content-area {
    padding: var(--fo-r-page-pad, 0.65rem) !important;
    padding-bottom: calc(4.25rem + env(safe-area-inset-bottom, 0)) !important;
}

.fieldops-shell.mobile-mode h1,
.fieldops-shell.mobile-mode .h1,
.fieldops-shell.mobile-mode .fo-page-title {
    font-size: 1.2rem !important;
}

.fieldops-shell.tablet-mode .main-sidebar {
    width: 180px;
    min-width: 180px;
}

.fieldops-shell.tablet-mode .content-area {
    padding: 14px !important;
}

.fieldops-shell.tablet-mode .kpi-card,
.fieldops-shell.tablet-mode .card,
.fieldops-shell.tablet-mode .fo-card {
    padding: 14px !important;
}

.fieldops-shell.tablet-mode h1,
.fieldops-shell.tablet-mode .fo-page-title {
    font-size: 1.35rem !important;
}

/* Prevent horizontal page scroll from wide children */
.fieldops-shell,
.fieldops-body,
.main-content,
.content-area {
    max-width: 100%;
}

html,
body {
    overflow-x: hidden;
}

/* Long text safety */
.kpi-card,
.fo-card,
.dispatch-wo-title,
.dispatch-wo-customer,
.dispatch-block-customer,
.dispatch-block-title {
    overflow-wrap: anywhere;
    word-break: break-word;
}
