/* ============================================================================
   responsive.css — one shared responsive layer for Connect
   ----------------------------------------------------------------------------
   Loaded LAST in every page's <head> so it wins the cascade over the big
   inline <style> blocks each page carries.

   Why this exists
   ---------------
   Every module page was built at ~1800px and then dropped inside the portal
   shell (home.html), which spends 300px on the sidebar before the module
   iframe gets a single pixel. On a 1280–1536px laptop the iframe is only
   ~980–1240px wide, and the cards those pages use are `overflow: hidden`,
   so anything past the card edge is CLIPPED with no scrollbar — the user
   simply cannot reach it (this is why the Actions column on Admin ▸ Users
   was invisible).

   Three things fix that, in order of importance:
     1. Nothing is ever clipped. Any element that overflows its parent gets a
        real horizontal scrollbar on that parent (added by responsive.js,
        styled here) instead of silently losing content.
     2. The shell gives the content more room as the viewport shrinks — the
        sidebar steps 300px → 240px → 64px rail → off-canvas drawer.
     3. Dense layouts (stat grids, filter rows, modals, toolbars) reflow at
        each breakpoint rather than squeezing.

   Breakpoints (match responsive.js)
   ---------------------------------
     xl   ≥ 1600    large desktop / external monitor
     lg   1367–1599 desktop
     md   1181–1366 laptop            ← most reported problems live here
     sm   901–1180  small laptop / landscape tablet
     xs   641–900   tablet
     xxs  ≤ 640     phone

   NOTE: module pages render inside an iframe, so these queries measure the
   iframe's width, not the screen's. That is the correct behaviour — the page
   responds to the space it actually has.
   ========================================================================= */

:root {
  --rsp-bp-xl: 1600px;
  --rsp-bp-lg: 1366px;
  --rsp-bp-md: 1180px;
  --rsp-bp-sm: 900px;
  --rsp-bp-xs: 640px;
  --rsp-gutter: 24px;
}

/* ===========================================================================
   1. BASE GUARDS
   Stop the classic causes of a page that is wider than its window.
   ========================================================================= */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

/* Media never forces the page wider than its container. */
img, svg, video, canvas, embed, object {
  max-width: 100%;
}
img, video { height: auto; }

/* Long unbroken strings (emails, UPNs, URLs, hashes) are a very common cause
   of a blown-out table column in this app. */
td, th, p, li, dd, dt,
.um-identity-username, .um-chip, .badge, .chip, .tag {
  overflow-wrap: break-word;
}

/* Code / preformatted blocks scroll instead of stretching the page. */
pre { max-width: 100%; overflow-x: auto; }

/* ===========================================================================
   2. HORIZONTAL OVERFLOW RESCUE
   responsive.js tags any element whose content is wider than it is with
   .rsp-xscroll. That element becomes a scroller so the content is reachable
   instead of clipped. No DOM is restructured, so selectors like
   `#companyDetails > table` keep matching.
   ========================================================================= */

.rsp-xscroll {
  overflow-x: auto !important;
  /* A clipped card can't show a scrollbar; this is the whole point. */
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: rgba(100, 116, 139, 0.45) transparent;
  /* Keep momentum scrolling from hijacking the page's vertical scroll. */
  overscroll-behavior-x: contain;
}

/* Make the scrollbar visible rather than the hairline overlay Windows draws —
   an invisible scrollbar is the same problem as no scrollbar. */
.rsp-xscroll::-webkit-scrollbar { height: 10px; }
.rsp-xscroll::-webkit-scrollbar-track {
  background: rgba(148, 163, 184, 0.12);
  border-radius: 999px;
}
.rsp-xscroll::-webkit-scrollbar-thumb {
  background: rgba(100, 116, 139, 0.45);
  border-radius: 999px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
.rsp-xscroll::-webkit-scrollbar-thumb:hover {
  background: rgba(71, 85, 105, 0.65);
  background-clip: padding-box;
}

/* Affordance note: deliberately NO gradient mask or inset shadow here.
   A mask creates a containing block that clips row menus and tooltips drawn
   inside the card, and an inset shadow would overwrite the card's own
   elevation. A permanently visible scrollbar (above) is the standard, safe
   signal that there is more to the right — which is exactly the thing that
   was missing. responsive.js still sets .rsp-overflow-left/right for anything
   that wants to style on it. */

/* On a touch device the scrollbar is an overlay that fades away, so reserve a
   little room and keep a hairline rule visible at the bottom of the scroller. */
@media (pointer: coarse) {
  .rsp-xscroll { scrollbar-width: auto; }
}

/* Tables keep their natural column widths inside a scroller instead of
   compressing into unreadable slivers. */
.rsp-xscroll > table,
.rsp-xscroll > div > table { max-width: none; }

/* ===========================================================================
   3. PORTAL SHELL (home.html)
   The sidebar is the single biggest consumer of horizontal space. Step it
   down as the window narrows so the module always gets usable width.
   home.html keeps its own .sidebar-collapsed / .sidebar-open classes; these
   rules only change the DEFAULT width at each size.
   ========================================================================= */

/* Desktop below 1600px: trim the sidebar; 300px is a lot of chrome. */
@media (max-width: 1599px) {
  .portal-shell:not(.sidebar-collapsed) { grid-template-columns: 260px 1fr; }
  .topbar { height: 80px; }
  .portal-shell { grid-template-rows: 80px 1fr; }
  .topbar-brand img { height: 64px; }
}

/* Laptop: 1366px and below — the band where the reported problems happen. */
@media (max-width: 1366px) {
  .portal-shell:not(.sidebar-collapsed) { grid-template-columns: 236px 1fr; }
  .topbar { height: 72px; padding: 0 16px 0 12px; }
  .portal-shell { grid-template-rows: 72px 1fr; }
  .topbar-brand img { height: 54px; }
  .user-avatar { width: 34px; height: 34px; font-size: 13px; }
  .nav-item { padding: 9px 12px; }
}

/* Small laptop / landscape tablet: collapse to the icon rail by default.
   responsive.js adds .sidebar-collapsed here unless the user has explicitly
   expanded it, so the toggle still wins. */
@media (max-width: 1180px) {
  .portal-shell:not(.sidebar-collapsed) { grid-template-columns: 220px 1fr; }
  .topbar { height: 64px; }
  .portal-shell { grid-template-rows: 64px 1fr; }
  .topbar-brand img { height: 46px; }
  .user-text .user-role { display: none; }
}

/* Tablet and below: the sidebar leaves the flow entirely and becomes a drawer
   over the content. home.html already ships the backdrop + .sidebar-open; this
   moves the trigger up from 768px to 900px, where the content actually runs
   out of room. */
@media (max-width: 900px) {
  /* Every combination, because home.html's own .sidebar-collapsed rule is
     more specific than a bare .portal-shell and would otherwise leave a
     64px rail sitting next to the drawer. */
  .portal-shell,
  .portal-shell:not(.sidebar-collapsed),
  .portal-shell.sidebar-collapsed { grid-template-columns: 0 1fr; }
  .portal-shell.sidebar-open,
  .portal-shell.sidebar-open.sidebar-collapsed,
  .portal-shell.sidebar-open:not(.sidebar-collapsed) { grid-template-columns: 280px 1fr; }
  .sidebar {
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.18);
    position: relative;
    z-index: 6;
  }
  .portal-shell.sidebar-open .sidebar-backdrop { display: block; }
  /* The rail styles must not apply while the drawer is open. */
  .portal-shell.sidebar-open.sidebar-collapsed .nav-label,
  .portal-shell.sidebar-open.sidebar-collapsed .nav-badge { display: block; }
  .portal-shell.sidebar-open.sidebar-collapsed .nav-item {
    padding: 11px 14px; justify-content: flex-start;
  }
  .portal-shell.sidebar-open.sidebar-collapsed .sidebar-section-title { opacity: 1; }
  .portal-shell.sidebar-open.sidebar-collapsed .vm-submenu { display: block !important; }
  .user-text { display: none; }
}

@media (max-width: 640px) {
  .topbar { height: 56px; padding: 0 10px; }
  .portal-shell { grid-template-rows: 56px 1fr; }
  .topbar-brand img { height: 38px; }
  .topbar-right { gap: 8px; }
  .logout-btn { padding: 8px 10px; font-size: 0; gap: 0; }
  .logout-btn svg { width: 18px; height: 18px; }
  .portal-shell.sidebar-open { grid-template-columns: min(86vw, 300px) 1fr; }
}

/* ===========================================================================
   4. SHARED COMPONENTS
   Class names used consistently across the module pages.
   ========================================================================= */

/* ---- Page gutters shrink as the window does ------------------------------
   Each page is listed by the ONE wrapper that actually owns its gutter.
   This list is deliberately not generic: these pages nest three or four
   wrappers deep, and a blanket rule puts a gutter on every level, which
   stacks into a bigger margin than the page started with — taking width
   away instead of giving it back. Every selector below is a wrapper
   verified to carry a 24–48px side gutter today; none gains one here.      */
@media (max-width: 1180px) {
  .content-container,          /* admin-console.html   32px  */
  body:not(.login-active) .wrap, /* index.html         28px  */
  .dashboard .tab-content,     /* index.html           28px  */
  .rf-shell,                   /* publish / disputes   28px  */
  .page-body,                  /* vendor-mgmt, catalogue 32px */
  .governance-container,       /* governance.html      48px  */
  .page-header {               /* governance.html      40px  */
    padding-left: 16px !important;
    padding-right: 16px !important;
  }
}
@media (max-width: 640px) {
  .content-container,
  body:not(.login-active) .wrap,
  .dashboard .tab-content,
  .rf-shell,
  .page-body,
  .governance-container,
  .page-header {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }
}

/* ---- Stat / KPI grids ---------------------------------------------------- */
/* These are written as repeat(4, 1fr) all over the app; at laptop width four
   cards become unreadable slivers. */
@media (max-width: 1366px) {
  .um-stats, .stats-grid, .kpi-grid, .summary-grid, .metrics-grid {
    grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)) !important;
  }
}
@media (max-width: 900px) {
  .um-stats, .stats-grid, .kpi-grid, .summary-grid, .metrics-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
}
@media (max-width: 480px) {
  .um-stats, .stats-grid, .kpi-grid, .summary-grid, .metrics-grid {
    grid-template-columns: 1fr !important;
  }
}

/* Generic collapse for any grid responsive.js measured as too tight.
   data-rsp-cols is the track count it counted. */
@media (max-width: 1180px) {
  [data-rsp-cols="4"], [data-rsp-cols="5"], [data-rsp-cols="6"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
  [data-rsp-cols="3"] { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
}
@media (max-width: 700px) {
  [data-rsp-cols] { grid-template-columns: 1fr !important; }
}

/* ---- Forms --------------------------------------------------------------- */
.form-row { flex-wrap: wrap; }
@media (max-width: 900px) {
  .form-row { flex-direction: column; align-items: stretch; }
  .form-row > .form-group { width: 100% !important; min-width: 0 !important; }
  .user-form { grid-template-columns: 1fr !important; }
}
.form-group { min-width: 0; }
.form-group input, .form-group select, .form-group textarea { max-width: 100%; }

/* ---- Filter / toolbar rows ----------------------------------------------- */
.filter-group, .filters, .toolbar, .um-toolbar, .al-actions, .tab-bar-row {
  flex-wrap: wrap;
}
@media (max-width: 1180px) {
  .filter-group > *, .filters > * { min-width: 0; }
}
@media (max-width: 640px) {
  .filter-group, .filters, .toolbar { flex-direction: column; align-items: stretch; }
  .filter-group > select, .filter-group > input,
  .filters > select, .filters > input { width: 100% !important; }
}

/* ---- Page headers -------------------------------------------------------
   Logo + title + a right-aligned action group (margin-left:auto) is the house
   pattern. It is a nowrap flex row, so on a narrow screen the action group
   hangs off the right edge. Let it wrap under the title instead. */
/* `header` rather than `body > header`: these pages nest it two wrappers
   deep (.wrap > .app-container > header). .topbar is the portal shell's own
   header, which has its own rules above. */
@media (max-width: 900px) {
  header:not(.topbar):not(.top-header),
  .page-header, .app-header, .report-header, .dashboard-header {
    flex-wrap: wrap;
    row-gap: 10px;
  }
  header:not(.topbar):not(.top-header) > *,
  .page-header > *, .app-header > * { min-width: 0; }
}
@media (max-width: 640px) {
  header:not(.topbar):not(.top-header) > div,
  .page-header > div:last-child {
    margin-left: 0 !important;
    width: 100%;
    justify-content: flex-start;
  }
  header:not(.topbar):not(.top-header) h1 { font-size: 18px; }
}

/* ---- Wrapping flex rows -------------------------------------------------
   A flex row only wraps at its own children. The allocation dashboard groups
   two controls inside an un-classed `display:flex` div, and a flex item
   defaults to min-width:auto — so that inner pair can never shrink and one
   long <select> ("🔒 Budget (Locked)") drags the whole page 300px wider than
   the phone it is on. Letting the inner rows shrink and wrap fixes it without
   touching the desktop layout. */
.controls-row, .controls-row > div, .filters > div, .toolbar > div { min-width: 0; }
@media (max-width: 900px) {
  .controls-row > div { flex-wrap: wrap; }
  .controls-row .control-group { min-width: 0 !important; }
  .controls-row select, .controls-row input, .control-group select, .control-group input {
    max-width: 100%;
  }
}
@media (max-width: 640px) {
  .controls-row > div, .controls-row .control-group { width: 100%; }
}

/* ---- Tab strips ---------------------------------------------------------- */
/* Tabs scroll sideways rather than wrapping into a four-line block or being
   cut off at the edge. */
.tab-buttons, .header-nav, .nav-tabs, .um-segments, .section-tabs {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x proximity;
}
.tab-buttons::-webkit-scrollbar,
.header-nav::-webkit-scrollbar,
.um-segments::-webkit-scrollbar { height: 6px; }
.tab-buttons::-webkit-scrollbar-thumb,
.header-nav::-webkit-scrollbar-thumb,
.um-segments::-webkit-scrollbar-thumb {
  background: rgba(100, 116, 139, 0.35); border-radius: 999px;
}
.tab-button, .nav-tab, .um-segment { flex-shrink: 0; scroll-snap-align: start; }
@media (max-width: 1180px) {
  .tab-button { padding: 9px 14px; font-size: 13px; }
}

/* ---- Modals -------------------------------------------------------------- */
/* Fixed-width dialogs are the second most common source of content you cannot
   reach: at 980px of iframe a 1180px dialog loses its right edge AND its
   footer buttons. */
.modal-content, .modal-dialog, .um-modal .modal-content {
  max-width: min(96vw, var(--rsp-modal-max, 1180px));
  max-height: 92vh;
  overflow-y: auto;
}
.modal-body { max-width: 100%; }
@media (max-width: 900px) {
  .modal-content, .modal-dialog {
    width: 96vw !important;
    max-width: 96vw !important;
    max-height: 94vh;
  }
  .modal-header, .modal-body, .modal-footer { padding-left: 16px; padding-right: 16px; }
  .modal-footer { flex-wrap: wrap; gap: 8px; }
  .modal-footer > button { flex: 1 1 auto; }
}
@media (max-width: 640px) {
  .modal-content, .modal-dialog {
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0 !important;
    margin: 0 !important;
  }
}

/* ---- Tables: density steps down with width ------------------------------- */
@media (max-width: 1180px) {
  table th, table td { padding: 8px 10px; }
  table { font-size: 13px; }
}
@media (max-width: 640px) {
  table th, table td { padding: 7px 8px; font-size: 12.5px; }
}

/* ---- Buttons / touch targets --------------------------------------------- */
@media (pointer: coarse) {
  button, .btn, [role="button"], a.btn { min-height: 40px; }
  .btn-sm { min-height: 34px; }
}
@media (max-width: 640px) {
  .btn, button.btn { padding: 9px 14px; font-size: 13px; }
}

/* ===========================================================================
   5. PAGE-SPECIFIC HARDENING
   ========================================================================= */

/* ---- Admin ▸ Users ------------------------------------------------------- */
/* The card clips (overflow: hidden, for the rounded corners). Keep the corners
   but let the table itself be the scroller, so the Actions column is always
   reachable. responsive.js adds .rsp-xscroll to the card when needed; these
   rules shrink the table first so scrolling is the last resort, not the first. */
#users .um-card { overflow: hidden; }

@media (max-width: 1366px) {
  #users .um-agencies { max-width: 260px; }
  #users .um-agencies:not(.expanded) .um-chip:not(.more) { max-width: 110px; }
  #users .um-identity-username { max-width: 220px; }
  #users #usersTable th, #users #usersTable td { padding-left: 12px; padding-right: 12px; }
  #users .um-search input { width: 220px; }
}
@media (max-width: 1180px) {
  #users .um-agencies { max-width: 200px; }
  #users .um-identity-username { max-width: 170px; }
  #users #usersTable th, #users #usersTable td { padding-left: 10px; padding-right: 10px; }
  #users .um-toolbar { gap: 10px; }
  #users .um-search input { width: 100%; min-width: 180px; }
  #users .um-toolbar-controls { flex: 1 1 220px; }
}
@media (max-width: 900px) {
  /* Status is already hidden by the page at this width; drop agency chips to a
     single "+N" summary rather than losing the Actions column. */
  #users .um-agencies { max-width: 140px; }
  #users .um-header-actions { width: 100%; }
}
@media (max-width: 640px) {
  #users #usersTable .um-col-role { display: none; }
  #users .um-segments { width: 100%; }
}

/* Admin header: the blue nav strip must never push the Logout button off. */
.header-container { padding-left: 16px; padding-right: 16px; }
@media (max-width: 1180px) {
  .header-container { padding-left: 12px; padding-right: 12px; }
  .header-left { gap: 16px; }
  .header-logo { font-size: 17px; padding: 14px 0; }
}
@media (max-width: 640px) {
  .header-logo span:not(.header-logo-icon) { display: none; }
}

/* Audit log table — long device / user-agent strings. */
#user-logs td { word-break: break-word; }
@media (max-width: 1180px) {
  #user-logs .al-col-device, #user-logs th:nth-child(6), #user-logs td:nth-child(6) { display: none; }
}

/* ---- Allocation dashboard (index.html) ----------------------------------- */
/* .subtable is min-width: 1200px by design — it genuinely needs the columns.
   Make sure its wrapper scrolls rather than clipping. */
.subtable { min-width: 1200px; }
@media (max-width: 1240px) {
  .subtable { font-size: 12.5px; }
  .subtable th, .subtable td { padding: 7px 8px; }
}

/* ---- Vendor Management --------------------------------------------------- */
@media (max-width: 1180px) {
  .vm-split, .vm-two-col { grid-template-columns: 1fr !important; }
}

/* ===========================================================================
   6. UTILITIES
   ========================================================================= */

.rsp-hide-md { }
@media (max-width: 1180px) { .rsp-hide-md { display: none !important; } }
@media (max-width: 900px)  { .rsp-hide-sm { display: none !important; } }
@media (max-width: 640px)  { .rsp-hide-xs { display: none !important; } }
@media (min-width: 901px)  { .rsp-only-sm { display: none !important; } }

/* Respect reduced motion for the shell transitions we rely on. */
@media (prefers-reduced-motion: reduce) {
  .portal-shell, .sidebar { transition: none !important; }
}
