/* =====================================================================================
   Athena layout contract

   This is intentionally the last stylesheet. Feature styles own their visual language;
   this file owns the geometry shared by every feature: page gutters, header/action
   alignment, intrinsic-width containment, responsive toolbars, and local table scrolling.
   Keeping those rules here prevents a new dashboard from reintroducing page-level
   horizontal scrolling or a one-off spacing system.
   ===================================================================================== */

:root {
  --layout-max: 1480px;
  --layout-gutter: clamp(16px, 2vw, 28px);
  --layout-block: clamp(16px, 1.8vw, 24px);
  --layout-gap: clamp(12px, 1.35vw, 18px);
  --layout-card-pad: clamp(14px, 1.3vw, 20px);
}

/* Intrinsic-width containment is the most important alignment rule in a data-heavy UI. */
#app, #main, .main, .page-h, .page-body,
.page-h > *, .page-body > *,
.card, .nw-card, .ev-sec, .st-card, .sx-card, .of-panel, .fn-card,
.grid > *, .g2 > *, .g3 > *, .g4 > *,
.nw-cols > *, .nw-grid > *, .po-grid > *, .st-grid > *, .ev-cols > *,
.of-grid > *, .of-cards > *, .vz-grid3 > *, .fn-cols > * {
  min-width: 0;
}

img, video, canvas { max-width: 100%; }
.page-body svg:not(.ic) { max-width: 100%; }
.page-body :where(h1, h2, h3, h4, p, b, strong, small, span, a) { overflow-wrap: break-word; }
.page-body :where(.ellipsis, [class*="-title"], [class*="-name"]) { min-width: 0; }

/* One shell rhythm, regardless of which module renders the page. */
.page-h {
  min-width: 0;
  min-height: 72px;
  padding: 13px var(--layout-gutter);
  column-gap: 12px;
  row-gap: 8px;
}
.page-h > .grow { flex: 1 1 300px; min-width: 220px; }
.page-h h1 { line-height: 1.18; }
.page-h .sub { max-width: 82ch; line-height: 1.4; }
.page-h .actions,
.page-h .ev-head-r,
.page-h .nw-head-r {
  min-width: 0;
  max-width: 100%;
  margin-left: auto;
  align-items: center;
}
.page-h .actions > *,
.page-h .ev-head-r > *,
.page-h .nw-head-r > * { flex: 0 0 auto; }

.page-body:not(.flush) { padding: var(--layout-block) var(--layout-gutter); }
.page-inner, .page-narrow { width: 100%; }
.page-inner { max-width: min(var(--layout-max), 100%); }
.page-narrow { max-width: min(940px, 100%); }

/* Analytics roots share the same usable width without forcing canvas/editor screens narrow. */
.page-body > :where(.nw, .po, .ev, .st, .sx, .tp, .ld, .lx, .of-page, .fn-wrap, .reports-home, .cn) {
  width: 100%;
  max-width: var(--layout-max);
  margin-inline: auto;
}

/* Page actions must wrap as a unit; period controls scroll inside their own lane. */
.period-slot { min-width: 0; max-width: 100%; }
.period-bar { min-width: 0; max-width: 100%; scrollbar-width: thin; }
.dashboard-fullscreen-btn { flex: 0 0 auto; }

/* Consistent wrapping for the many filter/action strips used throughout Athena. */
:where(.nw-toolbar, .st-filters, .ev-filters, .tp-filters, .of-filter, .fn-filters,
  .sx-bar, .ig-analytics-toolbar, .cn-actions, .nw-maptools, .of-platform-filter) {
  min-width: 0;
  max-width: 100%;
}
:where(.nw-toolbar, .st-filters, .ev-filters, .tp-filters, .fn-filters,
  .ig-analytics-toolbar, .cn-actions, .nw-maptools, .of-platform-filter) > * {
  min-width: 0;
}
:where(.input, select, textarea, input[type="search"], input[type="text"]) { max-width: 100%; }
.nw-search { min-width: min(200px, 100%); }
#nw-apply {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
  justify-content: center;
}
#nw-apply:hover { background: var(--primary-h); color: var(--on-primary); }

/* Wide evidence stays usable: the page does not drift sideways, the evidence itself scrolls. */
:where(.table-wrap, .table-scroll, .nw-scroll, .ev-scroll, .cn-scroll, .sx-table-wrap,
  .po-table-wrap, .of-table-wrap, .nw-matrix-wrap, .st-table-wrap) {
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  scrollbar-gutter: stable;
}
:where(.table, .nw-table, .sx-tbl, .po-table) { max-width: 100%; }
.table :where(th, td), .nw-table :where(th, td), .sx-tbl :where(th, td) { vertical-align: top; }

/* Long multilingual labels must truncate inside their cell rather than enlarge the grid. */
:where(.nw-trow-name, .nw-pc-foot, .tp-name, .po-post, .vz-dist-p, .of-source-rank b,
  .ev-status, .st-voice, .sx-author) { min-width: 0; }
:where(.nw-trow-name, .tp-name, .po-post, .of-source-rank b) > :where(b, span, small) {
  max-width: 100%;
}

/* Tablet: actions become their own dependable row before labels start colliding. */
@media (max-width: 1100px) {
  .page-h:has(.period-slot) > .actions,
  .page-h:has(.period-slot) > .ev-head-r,
  .page-h:has(.period-slot) > .nw-head-r {
    flex: 1 1 100%;
    width: 100%;
    margin-left: 0;
    justify-content: flex-start;
  }
  .page-h:has(.period-slot) .period-slot { flex: 1 1 auto; }
  .nw-brief-nums { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .nw-brief-nums > div:nth-child(2n) { border-right: 0; }
}

/* viz.css adds a desktop map ratio after its own responsive rule. Reassert the
   single-column contract here so district details never create page-level overflow. */
@media (max-width: 960px) {
  .vz-mapgrid { grid-template-columns: minmax(0, 1fr); }
  .vz-mapwrap, .vz-mapside, .vz-dist-p, .vz-dist-row { min-width: 0; max-width: 100%; }
}

/* Phone: a compact header stack, contained period rail, and controls large enough to use. */
@media (max-width: 700px) {
  :root {
    --layout-gutter: 14px;
    --layout-block: 14px;
    --layout-gap: 12px;
    --layout-card-pad: 14px;
  }

  .page-h {
    min-height: 0;
    padding: 12px 14px 12px 62px;
    align-items: flex-start;
    column-gap: 9px;
    row-gap: 9px;
  }
  .page-h > .grow { flex: 1 1 calc(100% - 42px); min-width: 0 !important; }
  .page-h h1 { font-size: 19px; }
  .page-h .sub { font-size: 12px; }

  .page-h .actions,
  .page-h .ev-head-r,
  .page-h .nw-head-r {
    flex: 1 1 100%;
    width: 100%;
    margin-left: 0;
    justify-content: flex-start !important;
  }
  .page-h .period-slot { flex: 1 1 100% !important; width: 100%; overflow: hidden; }
  .page-h .period-bar {
    display: flex;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-inline: contain;
  }
  .page-h .period-bar button { flex: 0 0 auto; min-height: 30px; }
  .page-h .actions > :where(select, .input) { max-width: 100%; }
  .nw-page-h > .btn-ghost { flex: 1 1 100%; width: 100%; justify-content: flex-start; }
  /* Network headers have only the period rail and full-screen control in actions. Keep
     them on one line; the rail itself scrolls without widening the page. */
  .nw-page-h .actions > .period-slot { flex: 1 1 0 !important; width: auto; }

  .page-body:not(.flush) { padding: 14px; }
  .page-body > :where(.nw, .po, .ev, .st, .sx, .tp, .ld, .lx, .of-page, .fn-wrap, .reports-home, .cn) {
    max-width: 100%;
  }

  .nw-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 8px;
    padding: 10px;
    top: 0;
  }
  .nw-toolbar > :where(select.input, .nw-search) { width: 100%; min-width: 0; }
  .nw-toolbar > :where(.btn, #nw-apply) { grid-column: 1 / -1; width: 100%; min-height: 34px; }
  .nw-toolbar .nw-chips { grid-column: 1 / -1; }

  :where(.nw-card-h, .ev-sec-h, .of-panel-h, .st-card-h) {
    align-items: flex-start;
    flex-wrap: wrap;
  }
  .nw-card-h { padding: 14px 14px 0; }
  .nw-card-b { padding: 12px 14px 14px; }
  .ev-sec { padding: 14px; }
  .nw-hero { align-items: flex-start; flex-direction: column; padding: 14px; }
  .nw-pc-counts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .nw-brief-nums { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ev-watches { grid-template-columns: minmax(0, 1fr); }
  .ev-watch { grid-template-columns: 96px minmax(0, 1fr) auto; gap: 12px; }
  .ev-watch > img { width: 96px; height: 96px; }

  :where(.tabs, .seg, .tp-tabs, .sx-tabs, .nw-tabs) {
    max-width: 100%;
    overflow-x: auto;
    flex-wrap: nowrap;
    overscroll-behavior-inline: contain;
    scrollbar-width: none;
  }
  :where(.tabs, .seg, .tp-tabs, .sx-tabs, .nw-tabs)::-webkit-scrollbar { display: none; }
  :where(.tabs, .seg, .tp-tabs, .sx-tabs, .nw-tabs) > * { flex: 0 0 auto; white-space: nowrap; }

  .table :where(th, td), .nw-table :where(th, td), .sx-tbl :where(th, td) {
    padding-inline: 10px;
  }
}

@media (max-width: 420px) {
  .page-h { padding-right: 12px; }
  .page-h .actions > .dashboard-fullscreen-btn { width: 36px; padding-inline: 0; justify-content: center; }
  .page-h .actions > .dashboard-fullscreen-btn span { display: none; }
  .nw-toolbar { grid-template-columns: 1fr; }
  .nw-toolbar > * { grid-column: 1 !important; }
  .nw-brief-nums, .nw-pc-counts { grid-template-columns: 1fr 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  .period-bar { scroll-behavior: auto; }
}
