/* ── Page-level responsive rules: Library, Audit Logs, Settings ──
   Tablet ≤ 900px reflows; phone ≤ 640px turns table rows into cards.
   The app shell (nav drawer) lives in layout.css. */

/* Settings content sits inside .page.active { overflow: hidden } and had no scroll container,
   so anything taller than the window was cut off — at every width. */
#page-settings .page-content {
  flex: 1;
  min-height: 0;
  width: 100%;
  overflow-y: auto;
}

/* ═════════════════════════ Tablet ≤ 900px ═════════════════════════ */
@media (max-width: 900px) {

  /* Filter bars (Library, Audit Logs). The selects carry inline width:/min-width: styles,
     hence !important — scoped to this breakpoint only. */
  .filter-bar { flex-wrap: wrap; }
  .filter-bar .form-input { flex: 1 1 100% !important; min-width: 0 !important; }
  .filter-bar .form-select { flex: 1 1 0; min-width: 0; width: auto !important; }

  /* Library */
  .library-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #page-library .contracts-table-header,
  #page-library .contracts-table-row { grid-template-columns: 30px 2.5fr 1fr 1fr 1.2fr 1fr 80px; }
  #page-library .th-lines,
  #page-library .lr-lines { display: none; }

  /* Detail and wizard/review content */
  .detail-grid { grid-template-columns: minmax(0, 1fr); }
  .result-stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  #page-upload .wizard-container { width: 100%; box-sizing: border-box; padding-inline: 24px; }

  /* Audit Logs */
  .al-grid-v2 { grid-template-columns: 2.4fr 1fr 0.9fr 64px 96px 72px 120px; }
}

/* ═════════════════════════ Phone ≤ 640px ═════════════════════════ */
@media (max-width: 640px) {
  .al-tl-row { grid-template-columns: 80px 1fr 44px; gap: 6px; }
  .al-tl-agents { display: block; overflow-x: auto; }

  .page-scroll { padding: 16px; }

  /* Upload wizard and shared review results */
  #page-upload .wizard-container { padding-inline: 16px; }
  #page-upload .agents-grid { grid-template-columns: minmax(0, 1fr); }
  #page-upload .wizard-progress .step-label:not(.active) { display: none; }
  .result-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .trace-diff-row { grid-template-columns: minmax(0, 1fr); }

  /* ── Library ── */
  .library-stats { gap: 8px; margin-bottom: 14px; }
  .library-stats .stat-card { padding: 12px; min-width: 0; }
  .library-stats .stat-value { font-size: 18px; }
  .library-stats .stat-sub { overflow-wrap: anywhere; }

  /* Header becomes a wrapping row of sort controls; unsortable labels hidden. */
  #page-library .contracts-table-header {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 14px;
    padding: 8px 14px;
  }
  #page-library .th-air,
  #page-library .th-lines { display: none; }

  /* Each row is a card. */
  #page-library .contracts-table-row {
    grid-template-columns: 30px auto 1fr;
    grid-template-areas:
      "select name    name"
      "select type    routing"
      "select dates   dates"
      "select air     status";
    gap: 6px 8px;
    padding: 12px 14px;
  }
  #page-library .cc-select-cell { grid-area: select; }
  #page-library .contract-name-cell { grid-area: name; min-width: 0; }
  #page-library .contract-name,
  #page-library .contract-sub { overflow-wrap: anywhere; }
  #page-library .lr-type    { grid-area: type; }
  #page-library .lr-routing { grid-area: routing; }
  #page-library .lr-status  { grid-area: status; justify-self: end; }
  #page-library .lr-dates   { grid-area: dates; line-height: 1.4; }
  #page-library .lr-air     { grid-area: air; }
  #page-library .lr-date-sep br { display: none; }
  #page-library .lr-date-sep::after { content: " → "; }

  /* ── Audit Logs ── */
  #page-auditlogs .contracts-table-header.al-grid-v2 { display: none; }
  #page-auditlogs .al-row-main.al-grid-v2 {
    grid-template-columns: 1fr auto auto;
    grid-template-areas:
      "file   file   file"
      "ts     ts     routing"
      "flags  dur    res"
      "status status status";
    gap: 6px 10px;
    padding: 12px 14px;
  }
  #page-auditlogs .al-col-file     { grid-area: file; }
  #page-auditlogs .al-col-ts       { grid-area: ts; }
  #page-auditlogs .al-col-ts span  { display: inline !important; margin-right: 6px; }  /* time span is inline-styled display:block */
  #page-auditlogs .al-col-routing  { grid-area: routing; justify-self: end; }
  #page-auditlogs .al-col-flags    { grid-area: flags; }
  #page-auditlogs .al-col-duration { grid-area: dur; }
  #page-auditlogs .al-col-resolved { grid-area: res; }
  #page-auditlogs .al-col-status   { grid-area: status; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
  #page-auditlogs .al-col-status .btn { margin-left: 0 !important; }

  #page-auditlogs .al-detail { padding: 12px; }
  #page-auditlogs .tab-bar { overflow-x: auto; flex-wrap: nowrap; scrollbar-width: thin; }
  #page-auditlogs .tab-bar .tab { flex-shrink: 0; white-space: nowrap; }
  #page-auditlogs .detail-kv th { white-space: nowrap; }
  #page-auditlogs .detail-kv td { overflow-wrap: anywhere; }

  /* ── Settings ── (page-content padding is inline-styled) */
  #page-settings .page-content { padding: 20px 16px !important; }
  .settings-tabbar { overflow-x: auto; scrollbar-width: thin; }
  .settings-tabbar .settings-tab-btn { flex-shrink: 0; white-space: nowrap; }

  .pattern-layout { flex-direction: column; min-height: 0 !important; }
  #pattern-list { width: auto !important; max-height: 200px; overflow-y: auto; }

  #connector-list .panel { flex-wrap: wrap; }
  #connector-list .panel > div { min-width: 0; overflow-wrap: anywhere; }
  #settings-tab-body td { overflow-wrap: anywhere; }
  #settings-tab-body td[style*="display:flex"] { flex-wrap: wrap; }
}

/* ═════════════════════════ Small phone ≤ 480px ═════════════════════════ */
@media (max-width: 480px) {
  .library-stats { grid-template-columns: minmax(0, 1fr); }
}

/* Wizard step strip: 4 stages → 2 columns on tablets, 1 on phones */
@media (max-width: 900px) {
  .step-strip { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 640px) {
  .step-strip { grid-template-columns: 1fr; }
  .md-form { grid-template-columns: minmax(0, 1fr); row-gap: 4px; }
  .md-form > label { padding-top: 6px; }
  /* Manage models list: each connector becomes a small card (name full width, then status badges and Edit / Delete). */
  .mm-table tr { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
  .mm-table td { display: block; min-width: 0; overflow-wrap: anywhere; }
  .mm-table td:first-child { grid-column: 1 / -1; padding-bottom: 2px !important; }
  .mm-table td:last-child { white-space: nowrap; }
  .mkt-covers { min-width: 80px; }
}

/* Reports page */
@media (max-width: 900px) {
  .rp-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 640px) {
  .rp-field, .rp-field-wide { min-width: 0; flex: 1 1 140px; max-width: none; }
  .rp-actions { width: 100%; }
  .rp-chart-tall { height: 220px; }
}

/* Likely duplicates dialog (js/duplicateGroup.js): stack each copy's columns on phones. */
@media (max-width: 640px) {
  .dg-row, .dg-decision { flex-direction: column; }
  .dg-select { flex-direction: row; min-width: 0; gap: 12px; }
  .dg-side { justify-content: flex-start; }
  .dg-card { padding: 16px; }
}
