/* Shared responsive safeguards; keep wide tables inside their own scrollers. */
html {
  /* Stop horizontal edge gestures from pulling the document out of its viewport. */
  overscroll-behavior-x: none;
  overflow-x: clip;
}
body {
  /* clip preserves the viewport as the sticky header's scrolling ancestor. */
  overflow-x: clip;
  overscroll-behavior-x: none;
}
html.drawer-open,
html.drawer-open body {
  overflow: hidden !important;
  overscroll-behavior: none;
}
body.drawer-open {
  max-width: 100vw;
}
.search-starters, .category-grid, .highest-grid, .ov-nav, .ov-legend,
.ov-matrix-scroll, .history-table-details {
  /* Anchor absolute screen-reader labels inside the scrolling content. */
  position: relative;
  min-width: 0;
  max-width: 100%;
  /* Keep swipes local even after the first/last column or card is reached. */
  overscroll-behavior-x: none;
}
.filter-done { display: none; }
.filter-panel, .filter-group, .budget-card, .shelf-body > *,
.drawer-section, .definition-grid > div { min-width: 0; }
.filter-panel select { min-width: 0; max-width: 100%; }
.detail-drawer {
  max-width: 100vw;
  max-height: 100dvh;
  min-width: 0;
  overflow: hidden;
}
.drawer-head, .drawer-body, .drawer-actions { min-width: 0; max-width: 100%; }
.drawer-head h2 { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-body { overscroll-behavior: contain; overflow-x: hidden; }
.history-table-details { max-width: 100%; overflow-x: auto; }
.toast { width: max-content; max-width: calc(100% - 32px); text-align: center; overflow-wrap: anywhere; }

@media (max-width: 960px) {
  .filter-panel { height: 100dvh; padding-bottom: max(20px, env(safe-area-inset-bottom)); }
  .filter-panel h2, .filter-panel .panel-note { padding-right: 48px; }
  .filter-done {
    display: block; position: sticky; bottom: 0; width: 100%; min-height: 48px;
    margin-top: 16px; padding: 12px; border: 1px solid var(--white);
    border-radius: 12px; background: var(--white); color: var(--navy);
    font-weight: 600; cursor: pointer; box-shadow: 0 0 0 8px var(--navy);
  }
  .filter-panel input[type="search"], .filter-panel select, .page-size select { font-size: max(16px, 1rem); }
  .detail-drawer { left: 0; right: 0; width: auto; height: 100dvh; }
  .check-list label { min-height: 44px; }
  .catalog #results { scroll-margin-top: calc(var(--header-height) + 80px); }
}

@media (max-width: 720px) {
  .shell { width: calc(100% - 32px); }
  main, .hero .shell > *, .section-head > *, .ov-step-head > * { min-width: 0; }
  .hero h1 { line-height: 1.4; }
  .hero-stats { grid-template-columns: 1fr; gap: 14px; max-width: 360px; }
  .hero-stats .stat-value { flex-wrap: wrap; white-space: normal; }
  .search-starters button { min-height: 44px; }
  .search-results a { flex-direction: column; align-items: stretch; gap: 6px; }
  .search-results .result-name, .search-results .result-amount { overflow-wrap: anywhere; }
  .search-results { max-height: 55dvh; }
  .category-card, .highest-card { aspect-ratio: auto; min-height: 280px; }
  .highest-detail strong, .category-card .card-amount { overflow-wrap: anywhere; }
  .catalog-summary, .results-count, .budget-card, .drawer-body, .footer-contact { overflow-wrap: anywhere; }
  .drawer-head { gap: 6px; padding: 10px 12px; }
  .drawer-head h2 { min-width: 0; font-size: .85rem; }
  .drawer-nav-button, .drawer-close { flex: 0 0 44px; padding: 0; }
  .drawer-body { padding: 16px 12px 24px; }
  .drawer-item-hero { padding: 16px; }
  .drawer-item-hero .drawer-amount { font-size: clamp(1.5rem, 7vw, 2.5rem); }
  .drawer-actions { gap: 6px; padding: 10px 12px; }
  .drawer-actions .pill-button { min-height: 44px; flex: 1 1 40%; white-space: normal; }
  .drawer-actions #drawer-share { flex-basis: 100%; }
  .explanation-modes { flex-wrap: wrap; width: 100%; }
  .explanation-modes button { flex: 1 1 auto; min-height: 44px; }
  .reading-clause-head, .funding-list li { flex-wrap: wrap; }
  .definition-grid { grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr)); }
  .view-review .budget-card .card-amount, .clause-amount, .funding-list li b { white-space: normal; }
  .view-switch { flex-wrap: wrap; }
  .view-switch button { min-height: 44px; }
  .pagination { flex-wrap: wrap; }
  .filter-chip { max-width: 100%; overflow-wrap: anywhere; }
  .accessibility-close { width: 44px; height: 44px; }
}

/* Keep the reading area usable in landscape and with enlarged text. */
@media (max-height: 500px) and (max-width: 960px) {
  .detail-drawer { overflow: hidden; overscroll-behavior: contain; }
  .drawer-head { position: sticky; top: 0; z-index: 1; }
  .drawer-body { overflow-y: auto; overflow-x: hidden; flex: 1 1 auto; }
  .drawer-actions { flex-shrink: 0; }
}
