/* Phone layout (≤640px). Loaded last so it can override the page sheets.
   Rule for this file: nothing scrolls sideways. Tab strips and filter
   segments wrap, and the data tables turn into stacked rows. */

@media (max-width: 640px) {
  /* Safety net: a stray wide child must never widen the page. */
  html, body { overflow-x: clip; }
  .main-content, .view { min-width: 0; max-width: 100%; }
  img, svg, video, canvas, iframe { max-width: 100%; }
  pre, code, .api-doc-pre { overflow-wrap: anywhere; white-space: pre-wrap; }

  /* ── Segmented filters and tab strips wrap into chips ──────────────────── */
  .seg {
    display: flex; flex-wrap: wrap; gap: 6px;
    border: 0; background: transparent; overflow: visible; max-width: 100%;
  }
  .seg > button,
  .seg > button:first-child {
    border: 1px solid var(--border-strong); border-radius: var(--radius-pill);
    padding: 5px 11px; background: var(--card-bg);
  }
  .seg > button[aria-pressed="true"] { border-color: var(--primary); }

  .pd-tabs, .pdx-page .pdx-tabs {
    flex-wrap: wrap; overflow: visible; gap: 4px; padding: 4px 0 0;
    border-bottom: 1px solid var(--border);
  }
  .pd-tabs::after { display: none; }
  .pdx-page .pdx-tabs .pd-tab { padding: 7px 10px; font-size: var(--t-small); }

  .set-nav {
    flex-wrap: wrap; overflow: visible; gap: 4px;
  }
  .set-nav-item { flex: 0 1 auto; padding: 6px 11px; }
  .set-pane .int-tabbar,
  .int-tabbar { display: flex; flex-wrap: wrap; overflow: visible; width: 100%; }
  .int-tab { padding: 7px 11px; }

  /* ── Data tables become stacked rows ──────────────────────────────────── */
  .comp-table-wrap, .pt-table-wrap, .pdx-ctable-wrap { overflow: visible; }
  .comp-table, .pt-table, .pdx-ctable { display: block; width: 100%; }
  .comp-table tbody, .pt-table tbody, .pdx-ctable tbody { display: block; }
  .comp-table thead, .pt-table thead, .pdx-ctable thead { display: block; }
  .comp-table thead tr, .pt-table thead tr {
    display: flex; flex-wrap: wrap; align-items: center; gap: 0 4px;
    background: var(--bg-alt); border-bottom: 1px solid var(--border);
  }
  .comp-table thead th, .pt-table thead th { border-bottom: 0; position: static; padding: 7px 8px; }
  .pdx-ctable thead { display: none; }
  .comp-table td[colspan], .pt-table td[colspan], .pdx-ctable td[colspan] { grid-column: 1 / -1; }
  .comp-table td, .pt-table td, .pdx-ctable td { border-bottom: 0; padding: 0; min-width: 0; }
  .comp-table tbody tr:hover td, .pt-table tbody tr:hover td, .pdx-ctable tbody tr:hover td { background: transparent; }

  /* Computers: host + pills on top, version · last seen · printers below. */
  .comp-table thead .col-client, .comp-table thead .col-folder, .comp-table thead .col-user,
  .comp-table thead .col-os, .comp-table thead .num, .comp-table thead .comp-menu-cell { display: none; }
  .comp-table tbody tr {
    display: grid; align-items: center; gap: 2px 10px;
    grid-template-columns: 22px auto minmax(0, 1fr) auto;
    grid-template-areas: "cb host host menu" "cb ver seen pr";
    padding: 9px 8px 9px 10px; border-bottom: 1px solid var(--border);
  }
  .comp-table tbody tr:last-child { border-bottom: 0; }
  .comp-table tbody tr.is-selected { background: var(--select-bg); }
  .comp-table tbody tr.comp-panel-row { display: block; padding: 0; background: var(--bg-alt); }
  .comp-table tbody tr.comp-panel-row > td { display: block; width: auto; }
  .comp-printers-panel { min-width: 0; overflow-wrap: anywhere; }
  .comp-printers-panel * { min-width: 0; }
  .comp-printer-sublabel { white-space: normal; flex-shrink: 1; overflow-wrap: anywhere; }
  .comp-table .comp-cb-cell { grid-area: cb; width: auto; align-self: start; padding-top: 2px; }
  .comp-table .comp-host-cell { grid-area: host; white-space: normal; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 0; }
  .comp-table .comp-host-cell .comp-tag { margin-left: 8px; }
  .comp-table .comp-ver { grid-area: ver; font-size: var(--t-meta); }
  .comp-table .comp-seen { grid-area: seen; font-size: var(--t-small); overflow: hidden; text-overflow: ellipsis; }
  .comp-table .comp-pr-cell { grid-area: pr; text-align: right; white-space: nowrap; }
  .comp-table .comp-menu-cell { grid-area: menu; width: auto; }
  .comp-table .col-client, .comp-table .col-folder, .comp-table .col-user, .comp-table .col-os { display: none; }

  /* Grouped ("By client") rows: let the right-hand pills and buttons wrap. */
  .comp-grow { flex-wrap: wrap; row-gap: 6px; }
  .comp-grow .pr-body { flex: 1 1 calc(100% - 64px); min-width: 0; }
  .comp-grow .pr-right { flex: 1 1 100%; justify-content: flex-end; flex-wrap: wrap; }
  .comp-grow .pr-line1 { flex-wrap: wrap; row-gap: 4px; }

  /* Printers: name + chips on top, status below, actions on the right. */
  .pt-table thead .pt-col-folder, .pt-table thead .pt-col-ip, .pt-table thead .pt-col-toner,
  .pt-table thead .pt-col-comp, .pt-table thead .pt-actions { display: none; }
  .pt-table thead .pt-cb { width: auto; }
  .pt-table tbody.pt-group { border-bottom: 1px solid var(--border); }
  .pt-table tbody.pt-group:last-child { border-bottom: 0; }
  .pt-table tr.pt-row {
    display: grid; align-items: center; gap: 4px 8px;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas: "cb name act" "cb status act";
    padding: 9px 6px 9px 10px;
  }
  .pt-table tr.pt-row.is-selected { background: var(--select-bg); }
  .pt-table .pt-vrow { background: var(--bg-alt); padding-left: 22px; }
  .pt-table .pt-vrow td { background: transparent; }
  .pt-table .pt-vrow td.pt-col-printer { padding-left: 0; }
  .pt-table td.pt-cb { grid-area: cb; width: auto; align-self: start; padding-top: 2px; }
  .pt-table td.pt-col-printer { grid-area: name; min-width: 0; }
  .pt-table td.pt-col-status { grid-area: status; }
  .pt-table td.pt-actions { grid-area: act; width: auto; }
  .pt-table td.pt-actions .btn { display: none; }
  .pt-table td.pt-col-folder, .pt-table td.pt-col-ip,
  .pt-table td.pt-col-toner, .pt-table td.pt-col-comp { display: none; }
  .pt-name { overflow-wrap: anywhere; }
  .pt-table tbody tr.pt-row td { border-bottom: 0; }
  .pt-table .pt-caret-sp { display: none; }
  .pt-crumb-meta { flex-basis: 100%; margin-left: 0; }
  .pt-crumb-meta::before { content: none; }

  /* Printer → Computers: host on top, install · default · last seen below. */
  .pdx-ctable tbody tr {
    display: grid; align-items: center; gap: 4px 10px;
    grid-template-columns: 10px auto auto minmax(0, 1fr) auto;
    grid-template-areas: "dot host host host host" ". inst def seen seen" ". acts acts acts acts";
    padding: 10px 10px; border-bottom: 1px solid var(--border);
  }
  .pdx-ctable tbody tr:last-child { border-bottom: 0; }
  .pdx-ctable.no-install tbody tr { grid-template-areas: "dot host host host host" ". def def seen seen" ". acts acts acts acts"; }
  .pdx-ctable .pdx-col-dot { grid-area: dot; width: auto; }
  .pdx-ctable .pdx-col-dot .sdot { margin-top: 0; }
  .pdx-ctable .pdx-col-host { grid-area: host; }
  .pdx-ctable .pdx-col-install { grid-area: inst; }
  .pdx-ctable .pdx-col-def { grid-area: def; }
  .pdx-ctable .pdx-col-seen { grid-area: seen; font-size: var(--t-small); text-align: right; }
  .pdx-ctable .pdx-col-acts { grid-area: acts; text-align: left; }
  .pdx-ctable .pdx-row-acts { justify-content: flex-start; margin-left: -8px; }
  .pdx-ctable .pdx-ierr { overflow-wrap: anywhere; }

  /* Any other table keeps its own scroll box rather than widening the page. */
  .dtable-wrap { max-width: 100%; }

  /* ── Settings tables ──────────────────────────────────────────────────── */
  .api-endpoints-table td, .api-endpoints-table td:first-child,
  .api-endpoints-table td:nth-child(2) { white-space: normal; overflow-wrap: anywhere; }
  .argus-map-table thead { display: none; }
  .argus-map-table, .argus-map-table tbody { display: block; }
  .argus-map-table tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center;
    border-bottom: 1px solid var(--border); padding: 4px 0;
  }
  .argus-map-table td { border-bottom: 0; width: auto !important; }
  .argus-map-table td:nth-child(2) { grid-column: 1 / -1; grid-row: 2; padding-top: 0 !important; }
  .argus-map-table select { min-width: 0; }

  /* ── Drivers ──────────────────────────────────────────────────────────── */
  .tree-row .tree-actions { flex: 0 0 calc(100% - 30px); }
  .driver-printers-panel { margin-left: 0 !important; }

  /* ── Modals (already bottom sheets), menus and confirms stay on screen ──── */
  .modal-body { min-width: 0; overflow-wrap: anywhere; }
  .float-menu { max-width: calc(100vw - 16px); }
  .pop-confirm { max-width: calc(100vw - 24px); }
}
