/* Keep desktop tables intact; reflow records only within compact viewports. */
@media screen and (max-width: 1100px), screen and (pointer: coarse) and (max-width: 1400px) {
  body { display: block; }
  .sidebar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 5px 10px;
    padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  }
  .brand { display: flex; align-items: center; flex-wrap: wrap; gap: 3px 10px; margin: 0; min-width: 0; }
  .brand strong { font-size: 17px; }
  .brand span { font-size: 12px; overflow-wrap: anywhere; }
  .brand .app-version { margin: 0; font-size: 11px; }
  .sidebar nav { grid-column: 1 / -1; grid-row: 2; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 4px; }
  .viewer-mode .sidebar nav { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .nav { padding: 6px 3px; min-height: 38px; text-align: center; }
  .logout-btn { grid-column: 2; grid-row: 1; margin: 0; min-height: 34px; padding: 4px 9px; font-size: 12px; }
  main { padding: 10px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
  .topbar { display: flex; gap: 8px; margin-bottom: 8px; }
  .topbar > div { min-width: 0; }
  .topbar .actions { width: min(300px, 44%); flex-shrink: 0; }
  .actions input { flex: 1; min-width: 0; }
  .actions button { flex-shrink: 0; }
  .view-title-row { gap: 6px; }
  h1 { font-size: 20px; }
  h2 { font-size: 15px; margin-bottom: 7px; }
  .topbar p { font-size: 12px; line-height: 1.4; }
  .app-version-badge { min-height: 21px; padding: 1px 6px; font-size: 11px; }
  .panel { padding: 9px 10px; margin-bottom: 8px; }
  .panel-head { flex-wrap: wrap; gap: 6px; margin-bottom: 5px; }
  .panel-head > *, .form label, .form-grid > *, .inline-form > * { min-width: 0; max-width: 100%; }
  .panel-head h2 { margin-bottom: 0; }
  .grid.two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-bottom: 8px; }
  .collection-metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .metrics > div, .metrics > button { padding: 7px 8px; min-width: 0; }
  .metrics span { font-size: 12px; }
  .metrics strong { font-size: 18px; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
  input, select, textarea { min-width: 0; max-width: 100%; font-size: 16px; padding: 6px 7px; }
  input, select { height: 40px; }
  input, select, button, .button { min-height: 40px; }
  input[type="checkbox"] { width: 20px; height: 20px; min-height: 20px; padding: 0; }
  textarea { min-height: 48px; }
  button, .button { padding: 6px 9px; }
  button.tiny, .button.tiny { min-height: 38px; font-size: 12px; }
  button.text-link { min-height: 32px; overflow-wrap: anywhere; white-space: normal; }
  .form, .form-grid { gap: 6px; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel.form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel.form > :not(label), .panel.form > label:has(textarea),
  .panel.form > label:has(input[type="file"]) { grid-column: 1 / -1; }
  #receivableForm > label:nth-of-type(1), #receivableForm > label:nth-of-type(2),
  #receivableForm > label:nth-of-type(3), #receivableForm > label:nth-of-type(4),
  #receivableForm > label:has([name="summary"]), #customerForm > label:first-of-type,
  #linkRutForm > label:has([name="customer_name"]), #bankInfoForm > label,
  #emailBackupForm > label, #clearDataForm > label, #restoreBackupForm > label,
  #restoreBackupFileForm > label, #giftGivingForm > label, #giftItemForm > label { grid-column: 1 / -1; }
  .form label { gap: 3px; align-content: start; }
  .form-actions, .panel-actions { gap: 6px; }
  .form-actions > * { min-width: 0; white-space: normal; }
  .input-action { gap: 5px; }
  .input-action button { padding-inline: 7px; }
  .backup-panel { flex-wrap: wrap; gap: 7px; }
  .backup-panel > div { min-width: 0; max-width: 100%; }
  .backup-panel .inline-form input { width: 100%; }
  .backup-panel .inline-form label { flex: 1 1 210px; }
  .backup-actions { justify-content: flex-start; gap: 5px; }
  .import-inline-fields { display: flex; gap: 6px; }
  .import-inline-fields label { flex: 1 1 220px; }
  .import-hint { padding-bottom: 0; font-size: 12px; }
  .receipt-inline-fields, .collection-profile-fields { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; align-items: start; }
  .receipt-inline-fields .collection-note-field { grid-column: span 2; }
  .collection-profile-fields .collection-wide-field { grid-column: span 3; }
  .collection-inline-fields textarea { height: 40px; min-height: 40px; }
  .collection-rut-hint:empty { display: none; }
  .collection-followup-controls { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 5px; }
  .collection-followup-controls label { max-width: none; grid-column: auto; }
  .collection-workspace-panel .panel-head { flex-direction: column; align-items: stretch; }
  .collection-workspace-actions { gap: 5px; justify-content: flex-start; }
  .collection-workspace-actions > * { flex: 0 1 auto; min-width: 0; }
  .collection-filter-control { display: flex; flex-wrap: wrap; min-width: 0; border-radius: 5px; }
  .collection-filter-control button { flex: 1 1 auto; padding: 5px 7px; white-space: normal; }
  .collection-recent-receipts .panel-head { align-items: stretch; flex-direction: column; }
  .receipt-record-toolbar { width: 100%; gap: 6px; justify-content: flex-start; }
  .receipt-record-search { width: min(100%, 600px); grid-template-columns: minmax(0, 1fr) auto auto; gap: 5px; }
  .receipt-remainder-fields label { min-width: 0; flex: 1 1 200px; }
  .receipt-remainder-fields small { padding-bottom: 0; }
  .credit-apply-panel { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 8px 0; gap: 6px; }
  .credit-apply-panel button, .credit-apply-panel small { grid-column: 1 / -1; }
  .table-pagination { gap: 6px; padding-top: 7px; }
  .page-status { min-width: 0; }
  .table-pagination .page-status { order: 0; width: auto; }
  button.page-button { width: 40px; height: 40px; }
  .table-wrap { max-height: none; overflow: visible; border: 0; border-radius: 0; }
  #receivables .table-wrap.compact { max-height: none; }
  .table-wrap table.responsive-record-table { display: block; position: relative; min-width: 0 !important; width: 100%; table-layout: auto; }
  .responsive-record-table thead { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); }
  .responsive-record-table tbody { display: block; width: 100%; }
  .table-wrap .responsive-record-table tr.responsive-record-row {
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0;
    width: 100%; padding: 5px 0; margin: 0; border: 0; border-bottom: 1px solid var(--line); border-radius: 0;
  }
  .responsive-record-table tr.responsive-record-row:nth-child(even) { background: #f8fbfa; }
  .table-wrap .responsive-record-table tr.responsive-record-row > td {
    display: block; min-width: 0; max-width: none; padding: 4px 6px; border: 0;
    white-space: normal; overflow-wrap: anywhere; text-align: center; line-height: 1.4;
  }
  .table-wrap .responsive-record-table tr.responsive-record-row > td::before {
    content: attr(data-label); display: block; margin-bottom: 2px;
    color: var(--muted); font-size: 11px; font-weight: 500;
  }
  .table-wrap .responsive-record-table tr.responsive-record-row > td.responsive-wide-cell { grid-column: 1 / -1; }
  .table-wrap .responsive-record-table tr.responsive-record-row > td.responsive-empty-cell { display: none; }
  .responsive-record-table td button, .responsive-record-table td .button { margin: 2px; max-width: 100%; }
  .responsive-record-table td button.text-link { margin: 0; }
  .table-wrap .responsive-record-table tr.responsive-message-row { display: block; width: 100%; padding: 8px 0; margin: 0; border: 0; }
  .table-wrap .responsive-record-table tr.responsive-message-row > td { display: block; width: 100%; padding: 6px; text-align: center; }
  .responsive-record-table tr.responsive-message-row > td::before { display: none; }
  .responsive-record-table tfoot { display: block; border-top: 1px solid var(--line); background: #f7faf9; }
  .responsive-record-table tfoot tr { display: flex; justify-content: center; align-items: center; gap: 8px; padding: 7px; }
  .responsive-record-table tfoot th { border: 0; padding: 0; overflow-wrap: anywhere; }
  .receipt-allocation-table tr.responsive-record-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .receipt-allocation-table td input, .receipt-allocation-table td select { min-width: 0; width: 100%; }
  .modal-dialog { max-width: calc(100vw - 24px - env(safe-area-inset-left) - env(safe-area-inset-right)); max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); overflow-y: auto; padding: 12px; }
  .modal-dialog .form { min-width: 0; }
  .store-transfer-dialog .form { padding: 4px; }
  .receipt-customer-suggestions { max-height: min(230px, 35dvh); }
  .gift-giving-details { grid-template-columns: minmax(0, 1fr) 80px; }
  #toast { max-width: calc(100% - 24px); bottom: max(12px, env(safe-area-inset-bottom)); padding: 8px 10px; font-size: 12px; }
}

@media screen and (min-width: 1000px) and (max-width: 1100px), screen and (pointer: coarse) and (min-width: 1000px) and (max-width: 1400px) {
  .table-wrap .responsive-record-table tr.responsive-record-row { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media screen and (max-width: 700px) {
  main { padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); }
  .sidebar { padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left)); gap: 4px 6px; }
  .brand { gap: 1px 7px; }
  .brand strong { font-size: 16px; }
  .brand span { flex-basis: 100%; font-size: 11px; }
  .brand .app-version { display: none; }
  .sidebar nav { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .topbar { flex-wrap: wrap; gap: 5px; }
  .topbar > div:first-child { flex: 1 1 100%; }
  .topbar .actions { width: 100%; }
  .topbar p { margin-top: 2px; }
  .grid.two { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .metrics, .collection-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metrics > div, .metrics > button { padding: 6px 8px; }
  .panel { padding: 8px 9px; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .receipt-inline-fields, .collection-profile-fields { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .receipt-inline-fields .collection-customer-field, .receipt-inline-fields .collection-note-field,
  .collection-profile-fields > label:first-child, .collection-profile-fields .collection-wide-field { grid-column: 1 / -1; }
  .receipt-inline-fields > label:has([name="payment_method"]) { grid-column: 1 / -1; }
  .collection-form-actions > *, #queryForm .form-actions > * { flex: 1 1 calc(50% - 6px); }
  .collection-followup-controls { grid-template-columns: minmax(0, 1fr) auto auto; }
  .collection-followup-controls label { grid-column: 1 / -1; }
  .collection-followup-controls button { grid-row: 2; }
  .collection-followup-controls button:last-child { grid-column: 3; }
  .collection-followup-controls button:not([type]) { grid-column: 1 / 3; }
  .collection-workspace-actions { width: 100%; }
  .collection-agent-filter { flex: 1 1 100%; width: 100%; }
  .collection-agent-filter select { flex: 1; }
  .collection-workspace-actions > .collection-filter-control { flex: 1 1 100%; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .collection-filter-control button { border: 0; border-bottom: 1px solid var(--line); }
  .receipt-record-toolbar > .collection-filter-control { width: 100%; }
  .table-wrap .responsive-record-table tr.responsive-record-row { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 4px 0; }
  .table-wrap .responsive-record-table tr.responsive-record-row > td { padding: 3px 4px; }
  .table-wrap .responsive-record-table td.collection-note,
  .table-wrap .responsive-record-table td.collection-action,
  .table-wrap .responsive-record-table td.dashboard-reminder-customer { grid-column: auto; }
  .table-wrap .responsive-record-table tr.responsive-record-row > td.responsive-wide-cell { grid-column: 1 / -1; }
  .rut-list { justify-content: center; }
  .rut-pill { white-space: normal; overflow-wrap: anywhere; max-width: 100%; }
  .customer-actions { justify-content: center; }
  .receipt-allocation-table tr.responsive-record-row > td:nth-child(2) { grid-column: 1 / -1; grid-row: 2; }
  .receipt-allocation-table tr.responsive-record-row > td:nth-child(3) { grid-column: 2; grid-row: 1; }
  .table-pagination { justify-content: space-between; }
  .table-pagination .page-status { order: 3; width: 100%; }
  .gift-catalog-column { gap: 8px; margin-bottom: 8px; }
  .query-original-name { font-size: 11px; }
  .transfer-payment-row { grid-template-columns: minmax(0, 1fr) 125px; gap: 6px; }
  .transfer-missing-rut-row { grid-template-columns: minmax(0, 1fr); }
  .store-transfer-dialog .form-grid { grid-template-columns: minmax(0, 1fr); }
  .store-transfer-dialog .span2 { grid-column: auto; }
}

@media screen and (pointer: coarse) {
  input, select, textarea { font-size: 16px; }
}
