/* ---- Vision Management Platform - Mobile responsive layer ----
   Loaded after css/chunks/part-*.css so it can override them without
   touching the desktop rules. Every rule in this file lives inside a
   media query capped at 900px: at desktop widths this file is inert.

   Breakpoints follow the ones already used by part-5.css:
     <= 900px  tablet (icon-only nav rail)
     <= 768px  small tablet / large phone
     <= 580px  phone (bottom tab bar, off-canvas nav rail)

   Wide data tables are never squeezed. Their container scrolls
   horizontally and the rows keep a readable minimum width; the card
   chrome around the table (title, filters, footer) stays pinned with
   position: sticky; left: 0 while the rows scroll underneath. */


/* ══════════════════════════════════════════════════════════════════════
   1. APP SHELL - phone nav drawer, header, safe areas
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 580px) {
  /* part-5.css hides the rail outright on phones, which leaves every page
     outside the 4 bottom tabs unreachable. Bring it back as the off-canvas
     drawer that the 768px block already styles. */
  .nav-rail {
    display: flex !important;
    width: 272px !important;
    max-width: 84vw !important;
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* The closed drawer sits at translateX(-100%) but its shadow still
     bled a dark strip onto the left edge of every page. */
  .nav-rail:not(.mobile-open) {
    box-shadow: none !important;
  }

  .mobile-menu-toggle-btn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0 !important;
    margin-right: 10px !important;
    border-radius: 9px !important;
    flex-shrink: 0;
  }

  /* Bottom tab bar items are links (see base.html): keep them looking
     like the buttons they replaced. */
  .mobile-tab-btn {
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
  }

  body.mobile-nav-open::after {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    z-index: 1040;
  }

  .top-nav > div:first-child {
    min-width: 0;
  }

  .mobile-topbar-brand,
  .mobile-brand-text {
    min-width: 0;
  }

  .mobile-brand-page {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Every page scrolls inside .main-content; keep its last row clear of
     the fixed bottom tab bar. */
  .main-content > .tab-content,
  .main-content > [id^="tab-"] {
    padding-bottom: calc(76px + env(safe-area-inset-bottom)) !important;
  }

  #tab-grid {
    padding-bottom: calc(68px + env(safe-area-inset-bottom)) !important;
  }

  /* Command Center manages its own full-height layout (see section 4). */
  #tab-command-center {
    padding-bottom: 0 !important;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   2. GENERIC - tables, inputs, modals
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Real <table> elements: the element that directly holds the table
     scrolls sideways instead of letting the table crush its columns. */
  main :has(> table) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  main table th {
    white-space: nowrap;
  }

  /* 16px text stops iOS Safari from zooming the page on input focus. */
  main input[type="text"],
  main input[type="search"],
  main input[type="email"],
  main input[type="password"],
  main input[type="number"],
  main input[type="tel"],
  main input[type="date"],
  main input[type="datetime-local"],
  main select,
  main textarea,
  .modal input,
  .modal select,
  .modal textarea {
    font-size: 16px !important;
  }

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


/* ══════════════════════════════════════════════════════════════════════
   3. GRID-BASED DATA TABLES (div rows laid out with CSS grid)
   The row's direct parent scrolls; rows keep a minimum width.
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  :has(> .cx-table-header),
  :has(> .ml2-table-header),
  :has(> .ca-table-head),
  :has(> .act2-table-head),
  :has(> .oee2-perf-thead),
  :has(> .oee2-events-header-row),
  :has(> .ra2-thead),
  .upv-table-wrap,
  .alm-act-scroll {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  /* Setup pages (Customers, Users, Sites, Equipment, Cameras, AI Models) */
  .cx-table-header,
  .cx-table-row { min-width: 760px; }
  .cx-cams-grid,
  #cx-cameras-body .cx-table-row { min-width: 820px; }
  .cx-users-grid,
  #cx-users-body .cx-table-row { min-width: 900px; }
  .cx-equip-grid,
  #cx-equipment-body .cx-table-row { min-width: 860px; }
  .cx-aim-grid,
  #cx-ai-models-body .cx-table-row { min-width: 880px; }
  /* Customers uses the generic 8-column grid; at 760px its name column
     only had room for the avatar and one letter. */
  #stab-customers .cx-table-header,
  #cx-customers-body .cx-table-row { min-width: 920px; }

  .ml2-grid,
  .ml2-row { min-width: 780px; }

  .ca-table-head,
  .ca-model-row { min-width: 760px; }

  /* Keep the card chrome in view while the rows scroll sideways. */
  .cx-card > :not(.cx-table-header):not([id$="-body"]),
  :has(> .ml2-table-header) > :not(.ml2-table-header):not([id$="-body"]),
  :has(> .ca-table-head) > :not(.ca-table-head):not([id$="-body"]) {
    position: sticky;
    left: 0;
  }

  /* Uploaded videos */
  .upv-grid-row { min-width: 820px; }
  #archives-tbody { min-width: 820px; }
  .upv-table-wrap { display: block !important; }

  /* Activity management */
  .act2-grid-row { min-width: 900px; }

  /* Productivity */
  .oee2-perf-grid-row,
  #oee2-perf-body { min-width: 760px; }
  .oee2-events-header-row,
  .oee2-events-data-row,
  #oee2-events-body { min-width: 560px; }
  #oee2-events-body {
    max-height: 460px;
    overflow-y: auto;
  }
  .oee2-perf-card > .oee2-card-header,
  .oee2-events-card > .oee2-card-header,
  .oee2-events-card > .oee2-events-footer {
    position: sticky;
    left: 0;
  }

  /* Alarm report + resolved alarms */
  .ar-row { min-width: 960px; }
  .ra2-thead,
  .ra2-row { min-width: 900px; }

  /* Alarms overview - recent activity */
  .alm-act-row { min-width: 460px; }
}


/* ══════════════════════════════════════════════════════════════════════
   4. COMMAND CENTER - stacks into one scrolling column
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  #tab-command-center {
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  #cc-root .cc-shell {
    height: auto !important;
    min-height: 100%;
    display: flex !important;
    flex-direction: column;
    overflow: visible !important;
  }

  #cc-root .cc-header {
    height: auto !important;
    flex-wrap: wrap;
    gap: 10px;
    padding: 12px 14px !important;
  }

  #cc-root .cc-header-left,
  #cc-root .cc-header-right {
    flex-wrap: wrap;
    gap: 8px !important;
  }

  #cc-root .cc-header-sep {
    display: none !important;
  }

  #cc-root .cc-kpis {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  #cc-root .cc-kpi {
    padding: 10px 14px !important;
    border-bottom: 1px solid #172336;
    min-width: 0;
  }

  #cc-root .cc-kpi:nth-child(2n) {
    border-right: none !important;
  }

  #cc-root .cc-main {
    display: flex !important;
    flex-direction: column;
  }

  #cc-root .cc-left {
    border-right: none !important;
    border-bottom: 1px solid #172336;
  }

  #cc-root .cc-left-body {
    max-height: 320px;
  }

  #cc-root .cc-right {
    display: flex !important;
    flex-direction: column;
  }

  #cc-root .cc-chart {
    height: 240px;
    flex-shrink: 0;
  }

  #cc-root .cc-donut {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
    padding: 14px !important;
  }

  #cc-root .cc-rank {
    max-height: 320px;
  }

  #cc-root .cc-rank-row {
    grid-template-columns: minmax(0, 110px) minmax(0, 1fr) 44px !important;
    gap: 10px !important;
  }

  #cc-root .cc-feed {
    height: 360px !important;
  }

  #cc-root .cc-feed-head {
    flex-wrap: wrap;
    gap: 8px;
  }

  #cc-root .cc-feed-body {
    overflow-x: auto !important;
  }

  #cc-root .cc-feed-row {
    min-width: 640px;
  }
}

@media (max-width: 768px) {
  /* Chart panel donut view needs more room once stacked */
  #cc-root .cc-chart:has(.cc-donut) {
    height: auto;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   5. ALARMS - overview dashboard
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .alm-tr-row {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 8px;
  }

  .alm-tr-left {
    flex-wrap: wrap;
    gap: 8px !important;
    max-width: 100%;
  }

  .alm-tr-left .alm-time-track {
    max-width: 100%;
    overflow-x: auto;
  }

  .alm-kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 10px !important;
  }

  .alm-body-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .alm-resp-grid {
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 10px !important;
  }

  .alm-recent-head {
    flex-wrap: wrap;
    gap: 10px;
    padding: 16px !important;
  }
}

@media (max-width: 768px) {
  /* Sub-nav: tabs scroll sideways, Export collapses to its icon. */
  .alm-subnav {
    padding: 0 12px 0 16px !important;
    gap: 10px;
  }

  .alm-subnav-tabs {
    gap: 20px !important;
    min-width: 0;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .alm-subnav-tabs::-webkit-scrollbar {
    display: none;
  }

  .alm-subnav-tabs .alm-tab-btn {
    white-space: nowrap;
    flex-shrink: 0;
  }

  .alm-export-btn {
    flex-shrink: 0;
    white-space: nowrap;
    padding: 8px 11px !important;
  }

  .alm-body {
    padding: 16px 14px 32px !important;
  }

  .alm-time-btn {
    white-space: nowrap;
    padding: 7px 12px;
  }

  .alm-kpi-card {
    padding: 14px 14px 13px 16px !important;
    min-width: 0;
  }

  .alm-kpi-valrow {
    flex-wrap: wrap;
    row-gap: 2px !important;
  }

  .alm-kpi-valrow > span:first-child {
    font-size: 28px !important;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

@media (max-width: 420px) {
  .alm-export-btn {
    font-size: 0 !important;
    gap: 0 !important;
    width: 38px;
    height: 38px;
    justify-content: center;
  }

  .alm-export-btn i {
    font-size: 14px;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   6. SEGMENTED CONTROLS + FILTER BARS (all pages)
   Segmented pills never wrap mid-label; when they don't fit they scroll
   sideways inside their own track.
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .ar-sev-track,
  .cx-seg-ctrl,
  .ml2-seg-track,
  .ml2-aim-tabs,
  .act2-seg,
  .ra2-seg,
  .pp2-seg-track,
  .ba2-seg,
  .ca-pill-seg,
  .alm-time-track {
    display: flex !important;
    flex-wrap: nowrap !important;
    max-width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .ar-sev-track::-webkit-scrollbar,
  .cx-seg-ctrl::-webkit-scrollbar,
  .ml2-seg-track::-webkit-scrollbar,
  .ml2-aim-tabs::-webkit-scrollbar,
  .act2-seg::-webkit-scrollbar,
  .ra2-seg::-webkit-scrollbar,
  .pp2-seg-track::-webkit-scrollbar,
  .ba2-seg::-webkit-scrollbar,
  .ca-pill-seg::-webkit-scrollbar,
  .alm-time-track::-webkit-scrollbar {
    display: none;
  }

  .ar-seg-btn,
  .cx-seg-btn,
  .ml2-seg-btn,
  .act2-seg-btn,
  .ra2-seg-btn,
  .pp2-seg-btn,
  .ba2-seg-btn {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .ar-search-wrap,
  .cx-search-wrap,
  .ra2-search-wrap {
    flex: 1 1 100% !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   7. SHARED PAGE PATTERNS - KPI strips, card headers, two-column rows
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .ra2-kpi-strip,
  .act2-kpi-strip,
  .ca-kpi-strip,
  .cam2-kpi-strip,
  .cx-kpi-strip,
  .ml2-kpi-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 10px !important;
  }

  .cx-kpi-strip {
    padding: 16px 14px 12px !important;
  }

  .ml2-kpi-strip {
    padding: 0 14px 16px !important;
  }

  .oee2-kpi-row,
  .oee2-hero,
  .an2-hero-row,
  .an2-mid-row,
  .oee2-mid-row,
  .oee2-safety-row,
  .act2-two-col,
  .ram-body {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Card / page headers: title block on top, actions wrap underneath. */
  .ra2-card-header,
  .cx-card-header,
  .act2-card-header,
  .oee2-card-header,
  .oee2-page-header,
  .ba2-header,
  .pp2-header {
    flex-wrap: wrap !important;
    gap: 12px !important;
    align-items: flex-start !important;
  }

  .cx-card-header {
    padding: 16px 14px 12px !important;
  }

  .ra2-card-header,
  .act2-card-header,
  .oee2-card-header {
    padding-left: 16px !important;
    padding-right: 16px !important;
  }

  .ra2-card-header-left,
  .cx-card-header > div:first-child {
    min-width: 0;
    flex: 1 1 220px;
  }

  .cx-filter-bar,
  .ml2-filter-bar,
  .act2-filter-bar,
  .ra2-filter-bar {
    padding-left: 14px !important;
    padding-right: 14px !important;
  }

  /* Resolved alarms tab owns its own padding on top of .alm-body. */
  #alert-tab-resolved {
    padding: 0 !important;
  }

  /* switchAlertTab() shows it as display: block, so its flex gap never
     applied and the banner sat flush against the cards around it. */
  #alert-tab-resolved > * + * {
    margin-top: 12px;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   8. PAGE SHELLS - drop desktop gutters and nested card frames
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Setup (/setup/*): the outer white frame around every sub-page adds a
     second border + 24px gutter; on a phone that cost a third of the width. */
  #tab-setup {
    padding: 0 !important;
  }

  #tab-setup > div:first-child {
    border: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    background: transparent !important;
  }

  .setup-subtab-bar {
    overflow-x: auto;
    scrollbar-width: none;
    flex-wrap: nowrap !important;
    background: #ffffff;
  }

  .setup-subtab-bar::-webkit-scrollbar {
    display: none;
  }

  .setup-subtab {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .cx-card {
    margin: 0 12px 16px !important;
  }

  /* Uploaded videos (bottom padding clears the fixed phone tab bar) */
  #tab-archive {
    padding: 10px 10px calc(76px + env(safe-area-inset-bottom)) !important;
  }

  /* The header box is flattened so its title and its action buttons become
     separate rows of the card: the actions then sit inside the collapsible
     filter panel (section 12) instead of above the Filters bar. */
  .upv-card-header {
    display: contents !important;
  }

  .upv-card-header > div:first-child {
    order: 0;
    padding: 14px;
    border-bottom: 1px solid #f1f5f9;
  }

  .upv-card-actions {
    order: 3;
    flex-wrap: wrap;
    padding: 0 14px 14px;
    border-bottom: 1px solid #f1f5f9;
  }

  .upv-card-actions > button {
    flex: 1 1 auto;
    justify-content: center;
  }

  #archive-filters {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding: 12px 14px !important;
  }

  #archive-filters > div,
  #archive-filters .upv-input {
    min-width: 0 !important;
    width: 100% !important;
  }

  #archive-filters > .upv-btn-clear {
    grid-column: 1 / -1;
  }
}

@media (max-width: 768px) {
  /* Activity analytics filters: label column + full-width control. */
  .an2-filter-card,
  .an2-filter-left,
  .an2-filter-right {
    width: 100%;
    flex-wrap: wrap;
  }

  .an2-filter-right {
    margin-left: 0 !important;
    justify-content: flex-end;
  }

  .an2-filter-group {
    width: 100%;
    min-width: 0;
  }

  .an2-filter-group .an2-filter-label {
    width: 64px;
    flex-shrink: 0;
  }

  .an2-filter-group select {
    flex: 1 1 auto;
    min-width: 0 !important;
    width: auto !important;
    max-width: 100%;
  }

  /* AI Workflow lanes: rows scroll sideways as one unit (the row's
     background and dividers extend under the scrolled columns). */
  .pp2-item-list > .pp2-item {
    width: max-content;
    min-width: 100%;
  }

  .pp2-item > .pp2-col-id,
  .pp2-item > .pp2-col-meta {
    flex-shrink: 0;
  }

  .pp2-lane-footer {
    flex-wrap: wrap;
    gap: 10px;
    row-gap: 8px;
  }

  .pp2-range-label {
    white-space: nowrap;
  }

  .pp2-pager-right {
    flex-wrap: wrap;
    gap: 8px;
    min-width: 0;
    max-width: 100%;
  }

  .pp2-rows-select-wrap {
    white-space: nowrap;
  }

  .pp2-page-btns {
    max-width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   9. TABLE FOOTERS + PAGERS
   Count label on its own line, page buttons scroll if they don't fit.
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .upv-footer-row,
  .cx-footer,
  .ra2-footer,
  .ar-footer,
  .ml2-footer,
  .act2-footer,
  .asgn-footer,
  .ca-footer,
  .cam2-footer,
  .oee2-events-footer {
    flex-wrap: wrap !important;
    gap: 10px !important;
    row-gap: 8px !important;
  }

  .upv-footer-row {
    padding-left: 14px !important;
    padding-right: 14px !important;
  }

  .upv-pager,
  .cx-pager,
  .ra2-pager,
  .ar-pager,
  .ml2-pager,
  .act2-pager,
  .asgn-pager,
  .ca-pager {
    max-width: 100%;
    overflow-x: auto;
    scrollbar-width: none;
    flex-wrap: nowrap;
  }

  .upv-pager > *,
  .cx-pager > *,
  .ra2-pager > *,
  .ar-pager > *,
  .ml2-pager > *,
  .act2-pager > *,
  .asgn-pager > *,
  .ca-pager > * {
    flex-shrink: 0;
  }
}

@media (max-width: 768px) {
  /* Activity > Camera Assignment: camera list on top, editor below. */
  .cam2-workspace {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .cam2-cam-list {
    max-height: 360px !important;
  }

  .cam2-panel-header {
    flex-wrap: wrap;
    gap: 10px;
  }

  /* Activity checklist is a 4-column table: scroll it, don't squash it. */
  :has(> .cam2-act-row) {
    overflow-x: auto !important;
  }

  .cam2-act-row,
  .cam2-act-thead {
    min-width: 620px;
  }
}

@media (max-width: 768px) {
  /* AI Models > Assignments: camera group headers stay pinned while the
     model rows under them scroll sideways. */
  .ca-cam-group {
    min-width: 760px;
  }

  .ca-cam-hdr {
    position: sticky;
    left: 0;
    width: calc(100vw - 26px);
    max-width: calc(100vw - 26px);
    box-sizing: border-box;
  }

  .ca-filter-row {
    flex-wrap: wrap !important;
    gap: 10px !important;
  }

  .ca-search-wrap,
  .ca-search-input {
    flex: 1 1 100% !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  .ca-filter-stats {
    margin-left: 0 !important;
    width: 100%;
    text-align: right;
  }
}

@media (max-width: 768px) {
  /* Setup tables scroll both ways inside a bounded card, column headers
     pinned to the top of that card. */
  .cx-card,
  :has(> .ml2-table-header),
  :has(> .ca-table-head) {
    max-height: calc(100dvh - 120px);
    overflow-y: auto !important;
  }

  .cx-table-header,
  .ml2-table-header,
  .ca-table-head {
    position: sticky !important;
    top: 0;
    z-index: 3;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   10. DRAWERS + MODALS
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 580px) {
  /* Alarm detail drawer: full-width sheet, footer actions wrap. */
  .ar-drawer {
    left: 0 !important;
    width: auto !important;
    max-width: none !important;
  }

  .ar-drawer-header,
  .ar-drawer-body,
  .ar-drawer-footer {
    padding-left: 16px !important;
    padding-right: 16px !important;
  }

  .ar-drawer-footer {
    flex-wrap: wrap;
    padding-bottom: calc(14px + env(safe-area-inset-bottom)) !important;
  }

  .ar-drawer-footer > * {
    flex: 1 1 100%;
    justify-content: center;
  }

  .ar-drawer-footer > div > button {
    flex: 1 1 0;
    white-space: nowrap;
  }
}

@media (max-width: 580px) {
  /* Alert details modal */
  .adm-fact-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .adm-fact-cell {
    border-bottom: 1px solid #f4f7fa;
  }

  .adm-fact-cell:nth-child(2n) {
    border-right: none !important;
  }

  .adm-resolve-section {
    padding: 14px 16px 16px !important;
  }

  .adm-resolve-header {
    flex-wrap: wrap;
    gap: 4px;
  }

  .adm-footer {
    flex-wrap: wrap;
    padding: 12px 16px !important;
  }

  .adm-footer-state:empty {
    display: none;
  }

  .adm-footer-actions {
    flex: 1 1 100%;
    flex-wrap: wrap;
  }

  .adm-footer-actions > button {
    flex: 1 1 auto;
    justify-content: center;
    white-space: nowrap;
  }
}

@media (max-width: 768px) {
  /* AI Workflow analysis modal (road / segmentation): full-screen sheet,
     panels stacked in one scrolling column with fixed panel heights. */
  .ram-overlay {
    padding: 0 !important;
  }

  .ram-container {
    height: 100dvh !important;
    max-height: none !important;
    border-radius: 0 !important;
    border: none !important;
  }

  .ram-header {
    flex-wrap: wrap;
    gap: 10px;
    padding: 12px 14px !important;
  }

  .ram-header-left {
    flex: 1 1 0;
    min-width: 0;
  }

  .ram-header-left > div:last-child {
    min-width: 0;
  }

  .ram-title-row {
    flex-wrap: wrap;
    row-gap: 4px;
  }

  .ram-title {
    font-size: 16px !important;
  }

  .ram-subline {
    white-space: normal !important;
    overflow-wrap: anywhere;
  }

  .ram-body {
    display: flex !important;
    flex-direction: column;
    overflow-y: auto !important;
    padding: 10px !important;
    gap: 10px !important;
  }

  .ram-left-col,
  .ram-right-col {
    display: flex !important;
    flex-direction: column;
    gap: 10px;
    overflow: visible !important;
    flex-shrink: 0;
  }

  .ram-panel-a {
    height: 56vw;
    min-height: 200px;
    flex-shrink: 0;
  }

  .ram-panel-c {
    height: 360px;
    flex-shrink: 0;
  }

  .ram-panel-b {
    height: 440px !important;
    flex-shrink: 0;
  }

  .ram-panel-c-header,
  .ram-panel-b-header {
    flex-wrap: wrap;
    gap: 8px;
  }

  .ram-panel-c-body {
    overflow: auto !important;
  }
}

@media (max-width: 768px) {
  /* Archive video preview: part-5.css pads every .modal-content by 16px,
     which framed the player in a thick black border. Let it go edge to edge. */
  .video-modal-content {
    padding: 0 !important;
    overflow: hidden;
    border-radius: 12px;
  }

  .video-modal-content .modal-header {
    padding: 12px 14px !important;
  }

  .video-modal-content .modal-header h2 {
    font-size: 15px !important;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .video-modal-content .modal-body {
    padding: 0 !important;
  }

  .video-modal-content .video-preview-player {
    width: 100% !important;
    max-height: 70dvh;
    display: block;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   11. TABLET (581px - 900px) - hierarchy drawer
   The drawer's closed state is translateX(-380px), which assumes it sits
   right of the 220px desktop rail. On tablet the rail is 52px (and the
   768px block pins the drawer to left: 0), so ~200px of the "closed"
   drawer stayed on screen over every page. Anchor it to the icon rail and
   hide it fully when closed.
   ══════════════════════════════════════════════════════════════════════ */
@media (min-width: 581px) and (max-width: 900px) {
  .hierarchy-drawer,
  .hierarchy-scrim {
    left: 52px !important;
  }

  .hierarchy-drawer {
    width: min(360px, calc(100vw - 52px)) !important;
    max-width: none !important;
  }

  .hierarchy-drawer:not(.open) {
    transform: translateX(calc(-100% - 60px)) !important;
    visibility: hidden;
    transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), visibility 0s linear 0.25s;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   12. COLLAPSIBLE FILTERS (Uploaded videos, Activity > Logbook)
   A .mob-filter-toggle bar (aria-controls = the panel id) opens and closes
   a [data-mobile-filters] panel; toggleFilterPanel() in mobile-ux.js.
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  .mob-filter-toggle {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 12px 14px;
    background: #ffffff;
    border: none;
    border-bottom: 1px solid #f1f5f9;
    font-family: inherit;
    font-size: 13px;
    font-weight: 700;
    color: #334155;
    cursor: pointer;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
  }

  .mob-filter-toggle-label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
  }

  .mob-filter-toggle-label > i {
    color: #64748b;
  }

  .mob-filter-count {
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 9px;
    background: #2563eb;
    color: #ffffff;
    font-size: 11px;
    line-height: 18px;
    text-align: center;
  }

  .mob-filter-count[hidden] {
    display: none;
  }

  .mob-filter-chevron {
    color: #94a3b8;
    font-size: 12px;
    transition: transform 0.2s ease;
  }

  .mob-filter-toggle[aria-expanded="true"] .mob-filter-chevron {
    transform: rotate(180deg);
  }

  /* :is() lifts this to ID specificity so it beats the per-page
     "#archive-filters { display: grid !important }" style rules. */
  :is(#archive-filters, #lb-filters, #aiw-workflow-filters, [data-mobile-filters]):not(.is-open) {
    display: none !important;
  }

  [data-mobile-filters].is-open {
    animation: mobFiltersDown 0.18s ease-out;
  }

  /* Uploaded videos card rows, top to bottom: title, Filters bar, filters,
     actions (Delete selected / Delete all / Refresh), table, pager. */
  .upv-filter-toggle { order: 1; }
  #archive-filters { order: 2; border-bottom: none !important; }
  .upv-card > .upv-table-wrap { order: 4; }
  .upv-card > #archives-pagination { order: 5; }

  .upv-card:has(#archive-filters:not(.is-open)) .upv-card-actions {
    display: none !important;
  }

  .upv-card:has(#archive-filters.is-open) .upv-card-actions {
    animation: mobFiltersDown 0.18s ease-out;
  }

  /* Empty/loading state: centre it in the visible width, not across the
     full 820px scroll width of the table. */
  #archives-tbody:has(> .upv-empty-block),
  #archives-tbody:has(> tr) {
    min-width: 0;
    position: sticky;
    left: 0;
    width: calc(100vw - 22px);
  }

  /* AI Workflow: the bar is a card of its own above the filter card. */
  .pp2-filter-toggle {
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    margin-bottom: 10px;
  }

  /* Activity > Logbook */
  #atab-logbook {
    padding: 10px !important;
  }

  .lb-filter-card {
    padding: 0 !important;
    overflow: hidden;
    margin-bottom: 10px !important;
  }

  .lb-filter-card > .mob-filter-toggle {
    border-radius: 12px;
  }

  .lb-filter-card > .mob-filter-toggle[aria-expanded="true"] {
    border-radius: 12px 12px 0 0;
  }

  #lb-filters {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px !important;
    padding: 12px 14px 14px;
  }

  #lb-filters .form-group:first-child,
  #lb-filters .form-group:has(input[type="datetime-local"]),
  #lb-filters > button {
    grid-column: 1 / -1;
  }

  #lb-filters select,
  #lb-filters input {
    width: 100% !important;
    min-width: 0 !important;
    box-sizing: border-box;
  }

  /* Passages table: scrolls both ways inside the card, header pinned
     (the <th>s are already position: sticky; top: 0). */
  .lb-table-card {
    overflow-x: auto !important;
    max-height: calc(100dvh - 200px);
  }

  .lb-table {
    min-width: 640px;
  }

  .lb-table td {
    white-space: nowrap;
  }
}

@keyframes mobFiltersDown {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}



/* ══════════════════════════════════════════════════════════════════════
   13. VIDEO WALL - phones
   One camera per row at full width (16:9) so each feed is actually
   watchable; the wall scrolls vertically as cameras are added (up to
   MOBILE_WALL_CAPACITY in grid.js), ending in a single Add camera tile.
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 580px) {
  #camera-grid.multiplexer-canvas,
  #camera-grid.multiplexer-canvas.grid-2,
  #camera-grid.multiplexer-canvas.grid-3,
  #camera-grid.multiplexer-canvas.grid-4 {
    grid-template-columns: minmax(0, 1fr) !important;
    /* the desktop NxN layouts also fix N equal rows; rows size to content */
    grid-template-rows: none !important;
    grid-auto-rows: auto !important;
    gap: 10px !important;
  }

  #camera-grid .mpx-tile {
    aspect-ratio: 16 / 9 !important;
    border-radius: 12px !important;
    background: #000 !important;
  }

  /* Show the whole frame (4:3 DVR channels letterbox instead of losing
     their edges). */
  #camera-grid .mpx-tile video {
    object-fit: contain !important;
  }

  /* Keep the camera name readable above the SIGNAL LOST overlay (z 4),
     otherwise offline tiles don't say which camera they are. */
  #camera-grid .mpx-identity-scrim {
    z-index: 6 !important;
  }

  #camera-grid .mpx-live-dot {
    width: 8px !important;
    height: 8px !important;
    top: 10px !important;
    left: 10px !important;
  }

  #camera-grid .mpx-timestamp {
    font-size: 10px !important;
    top: 8px !important;
    right: 44px !important;
  }

  #camera-grid .mpx-cam-name {
    font-size: 14px !important;
    bottom: 10px !important;
    left: 12px !important;
    right: 12px !important;
  }

  #camera-grid .mpx-cam-sub {
    font-size: 11px !important;
    bottom: 30px !important;
    left: 12px !important;
    right: 12px !important;
  }

  #camera-grid .mpx-mobile-remove-btn {
    width: 28px !important;
    height: 28px !important;
    top: 8px !important;
    right: 8px !important;
  }

  #camera-grid .mpx-mobile-expand-icon {
    width: 40px !important;
    height: 40px !important;
  }

  /* The single Add camera tile at the end of the wall. */
  #camera-grid .mpx-add-cam-tile {
    aspect-ratio: auto !important;
    height: 84px;
    flex-direction: row !important;
    gap: 10px !important;
    border-radius: 12px !important;
  }

  #camera-grid .mpx-add-cam-ring {
    width: 28px !important;
    height: 28px !important;
  }

  #camera-grid .mpx-add-cam-label {
    font-size: 11px !important;
  }
}

@media (max-width: 580px) {
  /* Full-screen camera viewer: the feed area is portrait, so cover cropped
     a 16:9 stream down to a narrow vertical slice. Show the whole frame. */
  .mcd-feed-wrap {
    background: #000 !important;
  }

  .mcd-video {
    object-fit: contain !important;
  }
}

@media (max-width: 580px) {
  /* Camera picker drawer: it covers the header, so the 54px top padding
     (sized for the old fixed header) only left a blank strip. */
  .csp-header {
    padding-top: calc(14px + env(safe-area-inset-top)) !important;
  }
}

@media (max-width: 768px) {
  /* Activity analytics: breakdown names wrap to two lines instead of being
     cut off mid-word (the chip used to overlap the clipped name). */
  .an2-br-name {
    white-space: normal !important;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    line-height: 1.3;
    margin-bottom: 4px;
  }

  .an2-unclass-chip {
    display: inline-block;
    vertical-align: middle;
  }

  /* Live stream header: title stays on one line, the status note wraps
     underneath it when there's no room beside it. */
  .an2-live-header {
    flex-wrap: wrap;
    row-gap: 4px;
  }

  #an2-live-title {
    white-space: nowrap;
  }
}

@media (max-width: 768px) {
  /* Activity > Camera Assignment editor panel */
  .cam2-panel-header-right {
    flex-shrink: 1 !important;
    flex-wrap: wrap;
    min-width: 0;
  }

  .cam2-footer {
    flex-wrap: wrap;
    padding: 12px 14px !important;
  }

  .cam2-footer > :first-child {
    flex: 1 1 100%;
    white-space: normal !important;
  }

  .cam2-footer-btns {
    flex: 1 1 100%;
  }

  .cam2-footer-btns > button {
    flex: 1 1 0;
    justify-content: center;
    white-space: nowrap;
  }

  /* Header + footer stay put while the activity checklist scrolls; give
     the panel most of the screen so that list has room. */
  .cam2-panel-card {
    height: 78dvh;
    min-height: 480px !important;
    /* part-3.css caps it at 100vh - 310px, sized for the desktop header */
    max-height: none !important;
  }
}

@media (max-width: 580px) {
  /* Empty wall: the Add camera tile sits in the middle of the screen as an
     empty state. As soon as a camera is added it drops back into the flow
     below the cameras (and returns to the middle once the last one is
     removed); :has() re-evaluates on every add/remove, no JS involved.
     The height is the viewport minus the 48px header and the tab-grid
     padding that clears the bottom tab bar. */
  #camera-grid.multiplexer-canvas:not(:has(.mpx-tile)) {
    min-height: calc(100dvh - 48px - 10px - 68px - env(safe-area-inset-bottom)) !important;
    align-content: center !important;
  }

  #camera-grid:not(:has(.mpx-tile)) .mpx-add-cam-tile {
    height: 168px;
    flex-direction: column !important;
    gap: 12px !important;
  }

  #camera-grid:not(:has(.mpx-tile)) .mpx-add-cam-ring {
    width: 44px !important;
    height: 44px !important;
  }

  #camera-grid:not(:has(.mpx-tile)) .mpx-add-cam-ring svg {
    width: 18px;
    height: 18px;
  }

  #camera-grid:not(:has(.mpx-tile)) .mpx-add-cam-label {
    font-size: 12px !important;
  }
}


/* ══════════════════════════════════════════════════════════════════════
   14. AI MODELS > ASSIGNMENTS > BULK ASSIGN (phones)
   Full-screen sheet showing one column at a time; the step bar (markup in
   setup-bulk-assign.js, ba2SetStep) switches Cameras / Models / Plan and
   shows the live selection counts. Create/Cancel live on the Plan step.
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 580px) {
  #ba2-overlay {
    align-items: stretch !important;
    justify-content: stretch !important;
  }

  .ba2-dialog {
    width: 100% !important;
    height: 100dvh;
    max-height: none !important;
    border-radius: 0 !important;
  }

  .ba2-header {
    padding: 12px 14px !important;
  }

  /* "left / middle / right" only describes the desktop layout */
  .ba2-header-sub {
    display: none;
  }

  .ba2-steps {
    display: flex !important;
    gap: 4px;
    margin: 10px 12px 0;
    padding: 3px;
    background: #e2e8f0;
    border-radius: 10px;
    flex-shrink: 0;
  }

  .ba2-step {
    flex: 1 1 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 6px;
    border: none;
    border-radius: 8px;
    background: transparent;
    font-family: inherit;
    font-size: 13px;
    font-weight: 700;
    color: #475569;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .ba2-step.active {
    background: #ffffff;
    color: #0f172a;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
  }

  .ba2-step-count {
    min-width: 20px;
    padding: 1px 6px;
    border-radius: 9px;
    background: #cbd5e1;
    color: #334155;
    font-size: 11px;
    line-height: 16px;
  }

  .ba2-step.active .ba2-step-count {
    background: #2563eb;
    color: #ffffff;
  }

  .ba2-body {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) !important;
    padding: 10px 12px 12px !important;
    padding-bottom: calc(12px + env(safe-area-inset-bottom)) !important;
  }

  .ba2-body[data-step="cams"] > :not(#ba2-cam-col),
  .ba2-body[data-step="mods"] > :not(#ba2-mod-col),
  .ba2-body[data-step="plan"] > :not(#ba2-plan-col) {
    display: none !important;
  }
}
