/* =============================================================================
   TABLE COLUMNS — shedding the middle when the table outgrows its container.

   ⚠️ **The problem, measured on /devices 2026-10-27.** Expanding the right rail
   takes the table's container from 1173px to **977px**, while the table itself
   needs **1126px** (its columns are `table-layout: auto`, so their widths come
   from content). The wrapper is `overflow-x-auto`, so the surplus 149px simply
   scrolls off the right — and the column sitting there is **Actions**:

       Status 102 | Name 226 | Importance 136 | Tenant 142 |
       Gateways 121 | Active Alerts 170 | Last Seen 122 | Actions 107
                                                          ^ 0px visible,
                                                            137px hidden

   So widening the rail hid the only part of a row that DOES anything, and an
   operator had to scroll sideways to find the buttons. Nothing was hiding
   columns on purpose; there was no responsive column mechanism at all.

   ⚠️ **Pinning Actions to the right edge was tried first and does not work.**
   `position: sticky; right: 0` on the cell was measured against five table
   recipes — `min-width`, `table-layout: fixed`, `width: max-content`, and both
   border models — and pinned in none of them once the table genuinely
   overflowed. (An early "it works" reading came from a fixture whose table had
   quietly shrunk to fit, so nothing was off-screen to begin with.) Confirmed
   in the running app before being removed.

   **So the middle gets shed instead**, in an order each table declares. The
   rule, enforced in `components/tables/flowbite.html` rather than trusted to
   callers: never the first two columns, and never Actions.

   ⚠️ **Opt-in.** A column sheds only if it asks to with `'drop': N`. A table
   that declares nothing behaves exactly as it does today, which is what makes
   this safe to turn on one page at a time.
   ============================================================================= */

/* The query container is the table's own scroll box, not the viewport.

   ⚠️ Load-bearing: the rail expanding does not change the viewport at all, so
   a media query would never fire. What changes is this element's width —
   1173px to 977px — which is exactly what a container query reads.
   `design-system/table-toolbar.css` keys off `.table-header` for the same
   reason, and its first set of thresholds were viewport-sized guesses that
   never fired once. These are measured. */
.table-body-scroll {
    container-type: inline-size;
    container-name: patitur-table;
}

/* Thresholds are PER TABLE, keyed by the table's own id.

   ⚠️ **They cannot be global.** A threshold answers "is this table wider than
   its box", and every table has a different natural width — the devices table
   needs 1126px, the alerts table more. A single set derived from one table
   sheds a narrower table's columns while it still fits, and sheds a wider
   one's too late to stop the overflow. This block was global when only
   /devices declared `drop`, which hid the problem: correct for the one table
   it was measured on, wrong for the next one added.

   **Adding a table means measuring it**, on the real page with real data,
   because `table-layout: auto` takes its column widths from content. The
   recipe, run with the right rail expanded:

     const w = document.querySelector('.table-body-scroll'), t = w.querySelector('table');
     ({ container: w.clientWidth, table: Math.round(t.getBoundingClientRect().width),
        cols: [...t.querySelectorAll('thead th')].map(th =>
          [th.textContent.trim(), Math.round(th.getBoundingClientRect().width)]) })

   Then each step's threshold is the table's width with the previous columns
   already shed, rounded up a little — early rather than late, since shedding
   one column too eagerly costs a glance at the detail page while shedding too
   late costs the buttons.

   ⚠️ A table with no block here sheds nothing, whatever its columns declare.
   That is the safe direction: it keeps today's behaviour. */

/* ── gateways-table ─────────────────────────────────────────────────────
   Measured on /gateways?view=list 2026-10-29 (the page rendered with the
   fleet's 26 gateways, 1440px viewport, right rail expanded):

       container               824px   (rail open)
       full table             1078px  -> needs a container of 1081px
       less drop-1 Tenant (142)      936px  -> needs 939px
       less drop-2 Importance (136)  800px  -> needs 803px, fits the open-rail 824px
       less drop-3 Devices (107)     693px  -> the floor

   Columns: Status 102 | Gateway 152 | Devices 107 | Tenant 142 |
   Importance 136 | Active Alerts 170 | Last Heartbeat 163 | Actions 107.
   Each threshold is the table's width with the previous columns already shed,
   rounded up a little. ⚠️ Gateway is 152px with this fleet's EMPTY names (the
   EUI shows instead); a fleet with long names widens that column and these
   steps fire earlier, which is the safe direction. */
@container patitur-table (max-width: 1081px) {
    #gateways-table .patitur-col-drop-1 {
        display: none;
    }
}

@container patitur-table (max-width: 939px) {
    #gateways-table .patitur-col-drop-2 {
        display: none;
    }
}

@container patitur-table (max-width: 803px) {
    #gateways-table .patitur-col-drop-3 {
        display: none;
    }
}

/* ── tenant-list-table ──────────────────────────────────────────────────
   Measured on /tenants (List tab) 2026-10-31, 10 tenants, 1400px viewport,
   sidebar open, after the Alerts and Applications headers were shortened:

       container              1057px
       full table             1188px  -> needs 1191px; Actions 131px off-screen
       less drop-1 Support Level (135)  1053px  -> needs 1056px, fits
       less drop-2 Gateways (121)        932px  -> needs 935px
       less drop-3 Applications (144)    788px  -> the floor

   Columns: Tenant Name 146 | Status 111 | % Devices Online 174 | Alerts 120 |
   Support Level 135 | Gateways 121 | Applications 144 | Actions 235. Support
   Level goes first because it is an em dash for the whole fleet on penv. */
@container patitur-table (max-width: 1191px) {
    #tenant-list-table .patitur-col-drop-1 {
        display: none;
    }
}

@container patitur-table (max-width: 1056px) {
    #tenant-list-table .patitur-col-drop-2 {
        display: none;
    }
}

@container patitur-table (max-width: 935px) {
    #tenant-list-table .patitur-col-drop-3 {
        display: none;
    }
}

/* ── alerts-table ──────────────────────────────────────────────────────
   Measured on /alerts 2026-10-30 (org scope, 15 rows, 1440px viewport, right
   rail expanded):

       container               879px   (rail open)
       full table              911px  -> needs 914px; 32px of Actions clipped
       less drop-1 Tenant (106)      805px  -> needs 808px, fits the open rail
       less drop-2 Timestamp (127)   678px  -> the floor

   Columns: select 49 | Severity 113 | Device Name 178 | Tenant 106 |
   Alert Type 129 | Timestamp 127 | Status 101 | Actions 107. The tenant-scoped
   page renders no Tenant column at all; its table is the 805px row above and
   needs only the second step. */
@container patitur-table (max-width: 914px) {
    #alerts-table .patitur-col-drop-1 {
        display: none;
    }
}

@container patitur-table (max-width: 808px) {
    #alerts-table .patitur-col-drop-2 {
        display: none;
    }
}

/* ── devices-table ──────────────────────────────────────────────────────
   Measured on /devices 2026-10-27, right rail expanded:

       full table            1126px  -> needs a container of 1130px
       less drop-1 (142px)    984px  -> still 7px over the open-rail 977px
       less drop-2 (136px)    848px  -> fits, with room
       less drop-3 (121px)    800px  -> the table's own min-width floor

   Dropping only drop-1 leaves it 7px over, which is why the second step
   lands at 1000px rather than somewhere nearer 900px. */
@container patitur-table (max-width: 1129px) {
    #devices-table .patitur-col-drop-1 {
        display: none;
    }
}

@container patitur-table (max-width: 999px) {
    #devices-table .patitur-col-drop-2 {
        display: none;
    }
}

@container patitur-table (max-width: 859px) {
    #devices-table .patitur-col-drop-3 {
        display: none;
    }
}

/* ── devices-table with its Battery column ──────────────────────────────
   The column shows only while the battery filter is on (2026-11-11), and the
   table then carries .devices-table--battery. The widths above were measured
   without it, so the wider table shed too late and Actions scrolled off the
   right edge. Measured on /devices?battery=low 2026-11-11, at a 1600px window:

       Battery column         109px
       full table            1220px
       rail open, container   999px  -> 1098px rendered, 20 of Actions' 107px
                                        visible
       rail closed, container 1195px -> 1220px rendered, 94px visible

   Same columns, same order, each threshold 110px later than the plain
   table's. With the rail open that sheds Tenant and Importance (961px); with
   it closed, Tenant (1097px). */
@container patitur-table (max-width: 1239px) {
    #devices-table.devices-table--battery .patitur-col-drop-1 {
        display: none;
    }
}

@container patitur-table (max-width: 1109px) {
    #devices-table.devices-table--battery .patitur-col-drop-2 {
        display: none;
    }
}

@container patitur-table (max-width: 969px) {
    #devices-table.devices-table--battery .patitur-col-drop-3 {
        display: none;
    }
}
