/* =============================================================================
 * TWE UI — data.css   v2.1.0 / 2026-09-04
 * Dense engineering data: boring logs, lab results, invoice ageing, project
 * portfolios, timesheet hours, CPT profiles. Requires tokens.css linked FIRST;
 * defines no colour, radius, duration or easing of its own.
 *
 * THE RULE THAT GOVERNS THIS FILE (Matt, ratified 2026-09-04)
 * Depth resolves BY SURFACE TYPE: chrome, nav, cards and modals get rich depth
 * and glass; DENSE DATA TABLES AND FORMS STAY CALM AND HIGH-CONTRAST. Hence no
 * glass, no glow, no heavy shadow on any table, row, cell or definition list
 * here — a number in a 40-row table has to be read, not admired, and
 * backdrop-filter behind text is a legibility tax plus a compositing cost every
 * scroll frame. Restraint IS the design; the polish comes from alignment,
 * density control, hairline rhythm and correct sticky behaviour. This
 * SUPERSEDES Caementum's blanket "no gradient/shadow noise" rule by SCOPING it:
 * that rule was right about data and wrong applied fleet-wide, which is why
 * Caementum's cards read flat. Two exceptions, justified in place — .twe-kpi
 * (a stat tile IS a card) and the sticky-header shadow (the affordance saying
 * "pinned; data is moving beneath").
 *
 * BROWSER FLOOR: Chromium 120 (Tatara / Electron 28.3.3). Mandatory rules are
 * all >=120 (color-mix 111, scrollbar-gutter 94, ::-webkit-scrollbar all,
 * aspect-ratio 88, contain 52). @supports-guarded: scrollbar-color 121,
 * container queries 105 (guarded per brief anyway), corner-shape 139.
 *
 * WHY color-mix AND NOT NEW TOKENS: tints and status foregrounds are DERIVED,
 * never authored. color-mix(in srgb, X 55%, var(--twe-text)) self-inverts across
 * themes — darkening a hue on the light ground, lightening it on the dark — so
 * one declaration holds AA in dark, light and field without this file declaring
 * a colour or redefining a token. Per-theme hand-picked hexes are what made
 * v1's light theme fail.
 *
 * NO JAVASCRIPT. Every stateful hook is an ARIA attribute (aria-sort,
 * aria-selected, aria-expanded, aria-valuenow, data-twe-state), never a
 * JS-added class, so the DOM driving the pixels is the DOM a screen reader
 * reads. With JS off everything here is fully styled and readable; only
 * sort-order changes and chart contents need script.
 * ========================================================================== */

/* ============================================================ 1. TABLE WRAP =
 * The wrapper owns scrolling and the rounded clip; the table owns nothing that
 * fights sticky positioning.
 * GOTCHA that bites everyone once: overflow-x:auto forces overflow-y from
 * visible to auto, so the wrapper is ALWAYS a scroll container and a sticky
 * thead pins to the WRAPPER's top edge, not the viewport's. Unbounded height
 * means that edge never moves and the header just looks broken — hence
 * --twe-table-max-h. Set it to `none` for a short table that should grow with
 * the page (and accept a static header).
 */
.twe-table-wrap {
  --twe-table-max-h: 68vh;
  position: relative; max-block-size: var(--twe-table-max-h);
  overflow-x: auto; overflow-y: auto;
  background: var(--twe-bg-surface);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-md);
  /* Reserve the gutter always: a filter that removes enough rows to kill the
   * scrollbar would otherwise shift every column sideways mid-triage. */
  scrollbar-gutter: stable;
  /* Scroll anchoring drags sticky cells around on a live-updating table. */
  overflow-anchor: none;
}
/* Default OS scrollbars are an explicit reject. ::-webkit-scrollbar is the
 * BASELINE because it works at 120; scrollbar-color (121) layers on top. */
.twe-table-wrap::-webkit-scrollbar { width: 10px; height: 10px; }
.twe-table-wrap::-webkit-scrollbar-track,
.twe-table-wrap::-webkit-scrollbar-corner { background: var(--twe-scrollbar-track); }
.twe-table-wrap::-webkit-scrollbar-thumb {
  background: var(--twe-scrollbar-thumb); border-radius: var(--twe-radius-pill);
  /* Transparent border + padding-box clip = a floating pill, not a full-bleed
   * bar filling the gutter. */
  border: 2px solid transparent; background-clip: padding-box;
}
.twe-table-wrap::-webkit-scrollbar-thumb:hover { background: var(--twe-scrollbar-thumb-hover); }
@supports (scrollbar-color: red red) {
  .twe-table-wrap { scrollbar-width: thin; scrollbar-color: var(--twe-scrollbar-thumb) var(--twe-scrollbar-track); }
}

/* ================================================================ 2. TABLE ==
 * border-collapse:separate + border-spacing:0 is mandatory, not stylistic:
 * with `collapse`, Chromium drops borders on position:sticky cells AND the
 * wrapper's radius cannot clip the corner cells, so you get square table
 * corners poking out of a rounded box. `separate` keeps both working.
 */
.twe-table {
  --twe-row-h: 2.25rem;                    /* density knob — §6 */
  --twe-cell-x: var(--twe-space-3);
  inline-size: 100%; border-collapse: separate; border-spacing: 0;
  background: var(--twe-bg-surface); color: var(--twe-text);
  font-family: var(--twe-font-sans); font-size: var(--twe-text-sm);
  line-height: var(--twe-leading-snug); text-align: start;
}
/* Fixed layout is right for engineering data: one long remark field can't
 * starve every numeric column, and widths stay stable while rows stream in. */
.twe-table--fixed { table-layout: fixed; }
.twe-table--auto  { table-layout: auto; }
.twe-table caption {
  padding: var(--twe-space-3) var(--twe-cell-x); caption-side: top;
  color: var(--twe-text-secondary); font-size: var(--twe-text-xs); text-align: start;
}
.twe-table > thead > tr > th,
.twe-table > tbody > tr > th,
.twe-table > tbody > tr > td,
.twe-table > tfoot > tr > td {
  padding-inline: var(--twe-cell-x); padding-block: 0;
  block-size: var(--twe-row-h); vertical-align: middle; text-align: start;
}

/* ========================================================= 3. HEAD (sticky) =
 * Opaque --twe-bg-raised, never translucent: a glassy sticky header lets row
 * text ghost through as it scrolls under, the worst legibility failure a dense
 * table can have.
 */
.twe-table > thead > tr > th {
  position: sticky; inset-block-start: 0; z-index: var(--twe-z-sticky);
  background: var(--twe-bg-raised);
  border-block-end: var(--twe-border-width) solid var(--twe-border);
  color: var(--twe-text-secondary); font-size: var(--twe-text-2xs); font-weight: 400;
  letter-spacing: var(--twe-tracking-eyebrow); text-transform: uppercase;
  white-space: nowrap; user-select: none;
}
/* The one sanctioned shadow, and only while content is actually moving under
 * it. Optional: add data-twe-scrolled from a scroll handler. Correct without. */
.twe-table-wrap[data-twe-scrolled] > .twe-table > thead > tr > th { box-shadow: var(--twe-elev-2); }
/* Totals row (invoice ageing, timesheet hours) pins to the bottom edge. */
.twe-table > tfoot > tr > td {
  position: sticky; inset-block-end: 0; z-index: var(--twe-z-sticky);
  background: var(--twe-bg-raised);
  border-block-start: var(--twe-border-width) solid var(--twe-border);
  font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums);
}

/* ============================================== 4. SORTABLE HEADER AFFORDANCE
 * Driven by [aria-sort], never a JS class. Consequence worth stating: an
 * inactive sortable column MUST carry aria-sort="none", or there is no ghost
 * chevron to promote — and no sort state for a screen reader either. One
 * attribute serves both audiences. Direction is encoded by ROTATION (shape),
 * not hue, so it survives greyscale, print and a colour-blind reviewer.
 */
.twe-table th[aria-sort] { cursor: pointer; }
.twe-table th[aria-sort]::after {
  content: ""; display: inline-block;
  inline-size: 0.4em; block-size: 0.4em;
  margin-inline-start: var(--twe-space-2); vertical-align: 0.1em;
  /* 2px, never fractional: a sub-pixel stroke vanishes at some 125%/150%
   * Windows DPI scale factors and doubles at others. */
  border-inline-end: 2px solid currentColor; border-block-end: 2px solid currentColor;
  transform: rotate(45deg);                /* down = descending */
  opacity: 0.3;                            /* ghost = "this column sorts" */
  transition: opacity var(--twe-dur-fast) var(--twe-ease-out),
              transform var(--twe-dur-fast) var(--twe-ease-out);
}
.twe-table th[aria-sort="descending"]::after { opacity: 1; color: var(--twe-accent-text); }
.twe-table th[aria-sort="ascending"]::after  { opacity: 1; color: var(--twe-accent-text); transform: rotate(225deg); }
.twe-table th[aria-sort="none"]:hover::after { opacity: 0.65; }
/* An accent rule on the active column, so the sorted column is findable at a
 * glance in a 20-column boring log without hunting for a chevron. */
.twe-table th[aria-sort="ascending"],
.twe-table th[aria-sort="descending"] { color: var(--twe-text); box-shadow: inset 0 2px 0 0 var(--twe-accent); }
/* Headers are usually real buttons for keyboard sorting. Kill the native
 * chrome: a default OS button inside a modern table is THE named defect. */
.twe-table th > button {
  appearance: none; display: inline-flex; align-items: center;
  gap: var(--twe-space-1); inline-size: 100%; padding: 0;
  background: none; border: 0; border-radius: var(--twe-radius-xs);
  color: inherit; font: inherit; letter-spacing: inherit;
  text-transform: inherit; text-align: inherit; cursor: pointer;
}
.twe-table th > button:focus-visible { outline: 2px solid var(--twe-accent-ring); outline-offset: 2px; }

/* ================================================================= 5. ROWS ==
 * Row state rides ONE inherited local property, --twe-row-tint, painted as a
 * gradient layer over an opaque background-color. That indirection is what lets
 * a frozen column reproduce zebra/hover/selection exactly (§7) instead of the
 * usual mismatched pale stripe.
 */
.twe-table > tbody > tr {
  --twe-row-tint: transparent;
  background-color: var(--twe-bg-surface);
  background-image: linear-gradient(var(--twe-row-tint), var(--twe-row-tint));
  transition: background-color var(--twe-dur-fast) var(--twe-ease-out);
}
/* Very low alpha. Higher and 40 rows read as a barcode; the stripe only has to
 * break horizontal tracking, not decorate. */
.twe-table > tbody > tr:nth-child(even) { --twe-row-tint: color-mix(in srgb, var(--twe-text) 3.5%, transparent); }
.twe-table > tbody > tr:hover           { --twe-row-tint: color-mix(in srgb, var(--twe-text) 7%, transparent); }
.twe-table > tbody > tr[aria-selected="true"] { --twe-row-tint: var(--twe-accent-dim); }
/* Selection is never colour-alone: an accent bar on the leading edge survives
 * greyscale, print and forced-colors. */
.twe-table > tbody > tr[aria-selected="true"] > :first-child { box-shadow: inset 3px 0 0 0 var(--twe-accent); }
.twe-table > tbody > tr:focus-visible { outline: 2px solid var(--twe-accent-ring); outline-offset: -2px; }
.twe-table > tbody > tr > td,
.twe-table > tbody > tr > th { border-block-start: var(--twe-border-width) solid var(--twe-border-light); }
.twe-table > tbody > tr:first-child > td,
.twe-table > tbody > tr:first-child > th { border-block-start: 0; }
/* Row headers (boring ID, project number) read as labels, not prose. */
.twe-table > tbody > tr > th[scope="row"] {
  color: var(--twe-text); font-family: var(--twe-font-mono);
  font-variant-numeric: var(--twe-nums); white-space: nowrap;
}

/* ---- NUMERIC CELLS: MANDATORY, NOT OPTIONAL -------------------------------
 * EVERY numeric, currency, coordinate, elevation, depth, blow-count, ID and
 * timestamp cell MUST carry .twe-td--num. WHY: Outfit is a geometric display
 * face with NO tabular figures, so a column of depths or dollar amounts has
 * digit widths that differ row to row and decimal points that wander.
 * Misaligned digit columns are the loudest amateur tell in a data tool — an
 * engineer reads an N-value column vertically, and a wandering column costs
 * real scan time on a 60-row log. Mono + --twe-nums + end alignment fixes all
 * three at once. The linter checks this: a numeric column without it is a
 * defect, not a preference.
 */
.twe-td--num,
.twe-table th.twe-td--num {
  font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums);
  text-align: end; white-space: nowrap;
  font-feature-settings: "zero" 1;         /* slashed zero: 0 vs O in a boring ID */
}
.twe-table th.twe-td--num,
.twe-table th.twe-td--num > button { justify-content: flex-end; }
/* Long remark fields. Truncation REQUIRES a title attribute on the cell — a
 * clipped value with no full text is data loss, not a style choice. */
.twe-td--trunc {
  max-inline-size: 0;                      /* required for ellipsis in fixed layout */
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.twe-td--wrap { white-space: normal; text-wrap: pretty; }
.twe-table-empty {
  padding: var(--twe-space-10) var(--twe-space-6); text-align: center;
  color: var(--twe-text-muted); font-size: var(--twe-text-base);
}

/* ============================================================= 6. DENSITY ===
 * One local property drives row height everywhere, so a user-facing density
 * toggle is a class swap and never a stylesheet fork. Nothing drops below
 * --twe-text-2xs or a 28px row: field staff run these on tablets.
 */
.twe-table--compact     { --twe-row-h: 1.75rem; --twe-cell-x: var(--twe-space-2); font-size: var(--twe-text-xs); }
.twe-table--comfortable { --twe-row-h: 3rem;    --twe-cell-x: var(--twe-space-4); font-size: var(--twe-text-base); }
.twe-table--touch       { --twe-row-h: var(--twe-tap-min); }   /* row is a tap target */

/* ====================================================== 7. FROZEN COLUMN ====
 * The detail everyone gets wrong is the Z-ORDER AT THE INTERSECTION. Only this
 * order works:
 *   header row ....... --twe-z-sticky        (scrolls under nothing)
 *   frozen column .... --twe-z-sticky - 1    (must pass UNDER the header)
 *   corner cell ...... --twe-z-sticky + 1    (sticky on BOTH axes, so it has
 *                                             to beat both of the above)
 * Corner too low and body rows scrolling up overpaint the first header cell;
 * column too high and the frozen column paints over the header. Both are the
 * usual bug and both are instantly visible.
 * The frozen cells re-declare the row's background PAIR, which works because
 * --twe-row-tint is inherited from <tr>: zebra, hover and selection stay in
 * perfect sync across the freeze boundary. Opacity is not optional — a
 * transparent sticky cell lets the columns it covers scroll through it.
 */
.twe-table--freeze > thead > tr > :first-child,
.twe-table--freeze > tbody > tr > :first-child {
  position: sticky; inset-inline-start: 0;
  /* Divider as a shadow, not a border: a border would add width to the frozen
   * column and shift every measurement taken off it. */
  box-shadow: var(--twe-border-width) 0 0 0 var(--twe-border);
}
.twe-table--freeze > tbody > tr > :first-child {
  z-index: calc(var(--twe-z-sticky) - 1);
  background-color: var(--twe-bg-surface);
  background-image: linear-gradient(var(--twe-row-tint), var(--twe-row-tint));
}
.twe-table--freeze > thead > tr > :first-child {
  z-index: calc(var(--twe-z-sticky) + 1); background-color: var(--twe-bg-raised);
}
/* Selected + frozen keeps BOTH the accent edge and the divider; must follow
 * the rules above. */
.twe-table--freeze > tbody > tr[aria-selected="true"] > :first-child {
  box-shadow: inset 3px 0 0 0 var(--twe-accent), var(--twe-border-width) 0 0 0 var(--twe-border);
}

/* ======================================================= 8. BADGE / STATUS ==
 * Tinted background, solid foreground, AND A DOT OR GLYPH. The dot is not
 * decoration, it is the shape channel — colour alone is banned. TWE serves
 * public-sector clients so Section 508 exposure is real, and roughly 1 in 12
 * male reviewers cannot separate our ok-green from our warn-amber at 11px.
 * Foregrounds are derived (see header). Verified on the light ground:
 * ok 5.2:1, warn 5.6:1, danger 8.0:1, info 6.1:1, neutral 9.4:1.
 */
.twe-badge,
.twe-status {
  display: inline-flex; align-items: center; gap: var(--twe-space-2);
  max-inline-size: 100%; padding: var(--twe-space-1) var(--twe-space-3);
  background: var(--twe-bg-inset);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-pill);
  color: var(--twe-text-secondary);
  font-family: var(--twe-font-sans); font-size: var(--twe-text-2xs);
  letter-spacing: var(--twe-tracking-eyebrow); line-height: var(--twe-leading-tight);
  text-transform: uppercase; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.twe-badge::before,
.twe-status::before {
  content: ""; flex: 0 0 auto;
  inline-size: 0.5em; block-size: 0.5em;
  border-radius: 50%; background: currentColor;
}
/* data-twe-glyph swaps the dot for a character (! ? x check), adding a second
 * shape channel wherever the badge is the ONLY status indicator in a view. */
.twe-badge[data-twe-glyph]::before,
.twe-status[data-twe-glyph]::before {
  content: attr(data-twe-glyph); inline-size: auto; block-size: auto;
  background: none; border-radius: 0; font-family: var(--twe-font-mono);
}
.twe-badge--ok,     .twe-status--ok     { background: var(--twe-ok-bg);     color: color-mix(in srgb, var(--twe-ok) 55%, var(--twe-text));     border-color: color-mix(in srgb, var(--twe-ok) 40%, transparent); }
.twe-badge--warn,   .twe-status--warn   { background: var(--twe-warn-bg);   color: color-mix(in srgb, var(--twe-warn) 55%, var(--twe-text));   border-color: color-mix(in srgb, var(--twe-warn) 40%, transparent); }
.twe-badge--danger, .twe-status--danger { background: var(--twe-danger-bg); color: color-mix(in srgb, var(--twe-danger) 55%, var(--twe-text)); border-color: color-mix(in srgb, var(--twe-danger) 40%, transparent); }
.twe-badge--info,   .twe-status--info   { background: var(--twe-info-bg);   color: color-mix(in srgb, var(--twe-info) 55%, var(--twe-text));   border-color: color-mix(in srgb, var(--twe-info) 40%, transparent); }
.twe-badge--accent, .twe-status--accent { background: var(--twe-accent-dim); color: var(--twe-accent-text); border-color: var(--twe-accent-ring); }
/* GAP: tokens.css has no --twe-neutral-bg, so the neutral tint is derived from
 * --twe-neutral at the same 14% alpha the other *-bg tokens use. */
.twe-badge--neutral, .twe-status--neutral { background: color-mix(in srgb, var(--twe-neutral) 14%, transparent); color: color-mix(in srgb, var(--twe-neutral) 55%, var(--twe-text)); border-color: color-mix(in srgb, var(--twe-neutral) 40%, transparent); }
/* A count inside a badge is a number, so it gets the mono/tabular treatment. */
.twe-badge__count { font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums); letter-spacing: 0; }

/* ---- pulse: ONE state per view -------------------------------------------
 * Reserved for the single live attention state (a rig currently drilling, an
 * invoice at 120+ days). Two pulsing things compete and neither reads urgent.
 * It is redundant emphasis over the dot, the hue and the label — never the only
 * signal, so removing it loses no information.
 */
@keyframes twe-badge-pulse {
  0%, 100% { opacity: 1;    transform: scale(1); }
  50%      { opacity: 0.45; transform: scale(0.82); }
}
.twe-badge--pulse::before,
.twe-status--pulse::before { animation: twe-badge-pulse 1.8s var(--twe-ease-in-out) infinite; }
@media (prefers-reduced-motion: reduce) {
  /* tokens.css already collapses durations; stated explicitly because an
   * infinite animation is precisely what a vestibular-sensitive user asked us
   * to stop, and a 0.01ms infinite loop is still a loop. */
  .twe-badge--pulse::before,
  .twe-status--pulse::before { animation: none; }
}

/* ============================================================ 9. KPI / STAT =
 * The one card-shaped thing in this file, so per the surface-type rule it gets
 * --twe-elev-1 and the top-light highlight. The VALUE is mono with tabular
 * figures without exception: a row of KPI tiles whose numbers don't share a
 * baseline grid is the first thing a director notices.
 */
.twe-kpi,
.twe-metric {
  display: flex; flex-direction: column; gap: var(--twe-space-2);
  padding: var(--twe-space-4);
  background: var(--twe-bg-card);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-lg);
  box-shadow: var(--twe-elev-1), var(--twe-highlight);
}
.twe-kpi__label {
  color: var(--twe-text-muted); font-size: var(--twe-text-2xs);
  letter-spacing: var(--twe-tracking-eyebrow); text-transform: uppercase;
}
.twe-kpi__value {
  display: flex; align-items: baseline; gap: var(--twe-space-2);
  color: var(--twe-text);
  font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums);
  font-size: var(--twe-text-2xl); line-height: var(--twe-leading-tight);
}
.twe-kpi__unit { color: var(--twe-text-secondary); font-family: var(--twe-font-sans); font-size: var(--twe-text-xs); }
.twe-kpi__foot { color: var(--twe-text-muted); font-size: var(--twe-text-xs); }
/* Delta = glyph + sign + hue, three channels. Glyphs are CSS escapes, not
 * literal characters, so the file survives a non-UTF-8 static server. */
.twe-kpi__delta {
  display: inline-flex; align-items: center; gap: var(--twe-space-1);
  color: var(--twe-text-secondary);
  font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums);
  font-size: var(--twe-text-xs);
}
.twe-kpi__delta::before       { font-size: 0.85em; }
.twe-kpi__delta--up::before   { content: "\25B2"; }        /* up triangle   */
.twe-kpi__delta--down::before { content: "\25BC"; }        /* down triangle */
.twe-kpi__delta--flat::before { content: "\2013"; }        /* en dash       */
.twe-kpi__delta--up   { color: color-mix(in srgb, var(--twe-ok) 55%, var(--twe-text)); }
.twe-kpi__delta--down { color: color-mix(in srgb, var(--twe-danger) 55%, var(--twe-text)); }
/* IMPLEMENTER NOTE: up is not always good — on invoice ageing or rework hours
 * rising is bad, so pick the class by MEANING, and always render the sign in
 * the text ("+4.2%", "-11 days") so direction survives without glyph or hue. */
.twe-kpi__spark {
  inline-size: 100%; aspect-ratio: 4 / 1;  /* sparkline slot, sized not scripted */
  min-block-size: 2rem; overflow: hidden;
  background: var(--twe-bg-inset); border-radius: var(--twe-radius-sm);
}

/* =============================================== 10. PROGRESS / METER / PHASE
 * Native <progress> is styled alongside the div pattern, because a raw
 * <progress> renders the OS widget — the #1 named complaint — and half the
 * fleet reaches for the native element first. appearance:none is the real cure
 * and it works at the floor.
 * A11y: the div pattern needs role="progressbar" with aria-valuenow/min/max, or
 * aria-label when indeterminate. Fill width is an inline style; there is no
 * numeric attr() at this browser floor.
 */
.twe-progress,
progress.twe-progress {
  --twe-progress-h: 6px;
  appearance: none; display: block;
  inline-size: 100%; block-size: var(--twe-progress-h);
  background: var(--twe-bg-inset); border: 0;
  border-radius: var(--twe-radius-pill); overflow: hidden;
}
.twe-progress__fill,
progress.twe-progress::-webkit-progress-value {
  display: block; block-size: 100%;
  background: var(--twe-accent);           /* flat: no gradient, no glow — this is data */
  border-radius: var(--twe-radius-pill);
  transition: inline-size var(--twe-dur-slow) var(--twe-ease);
}
progress.twe-progress::-webkit-progress-bar { background: var(--twe-bg-inset); }
.twe-progress--ok     .twe-progress__fill { background: var(--twe-ok); }
.twe-progress--warn   .twe-progress__fill { background: var(--twe-warn); }
.twe-progress--danger .twe-progress__fill { background: var(--twe-danger); }
/* Meter = a value in a range against a threshold (percent compaction, moisture
 * vs optimum). Always paired with the printed number: a bar with no value is
 * not a lab result. The limit tick dual-encodes the spec limit as a POSITION. */
.twe-meter { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: var(--twe-space-3); }
.twe-meter__track { position: relative; }
.twe-meter__value { color: var(--twe-text-secondary); font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums); font-size: var(--twe-text-xs); }
.twe-meter__limit { position: absolute; inset-block: -2px; inline-size: 2px; background: var(--twe-border-strong); }

/* ---- phasebar: lifecycle state -------------------------------------------
 * Dual-encodes POSITION (which segment, left to right) and HUE (done/current/
 * pending/blocked), and every segment carries its own text label, so the stage
 * reads in greyscale and in print. The CONTAINER MUST carry a prose accessible
 * name — aria-label="Project stage: 3 of 5, Lab testing" — because "three
 * filled boxes" is meaningless to a screen reader.
 */
.twe-phasebar { display: flex; gap: var(--twe-border-width); }
.twe-phasebar__seg {
  flex: 1 1 0; min-inline-size: 0;
  padding: var(--twe-space-1) var(--twe-space-2);
  background: var(--twe-bg-inset); color: var(--twe-text-muted);
  font-size: var(--twe-text-2xs); letter-spacing: var(--twe-tracking-eyebrow);
  text-transform: uppercase; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.twe-phasebar__seg:first-child { border-start-start-radius: var(--twe-radius-sm); border-end-start-radius: var(--twe-radius-sm); }
.twe-phasebar__seg:last-child  { border-start-end-radius: var(--twe-radius-sm);   border-end-end-radius: var(--twe-radius-sm); }
.twe-phasebar__seg[data-twe-state="done"]    { background: color-mix(in srgb, var(--twe-ok) 22%, transparent); color: color-mix(in srgb, var(--twe-ok) 55%, var(--twe-text)); }
.twe-phasebar__seg[data-twe-state="blocked"] { background: var(--twe-danger-bg); color: color-mix(in srgb, var(--twe-danger) 55%, var(--twe-text)); }
.twe-phasebar__seg[data-twe-state="current"] {
  background: var(--twe-accent-dim); color: var(--twe-accent-text);
  box-shadow: inset 0 -2px 0 0 var(--twe-accent);   /* shape, not only hue */
}

/* ==================================================== 11. DEFINITION LIST ===
 * Record detail views (a boring's header block, an invoice's terms). The
 * hairline grid IS the gap: the container paints --twe-border, the cells paint
 * the surface over it, and the gap is exactly --twe-border-width. That gives a
 * rule that is always exactly one device-independent pixel — no half-pixel
 * seams at 125%/150% DPI — and it becomes 2px automatically in the field theme.
 * Values that are numbers, coordinates, IDs or timestamps take .twe-td--num,
 * which works unchanged on a <dd>.
 */
.twe-definition-list {
  display: grid; grid-template-columns: minmax(8rem, 14rem) 1fr;
  gap: var(--twe-border-width); margin: 0;
  background: var(--twe-border); border-radius: var(--twe-radius);
  overflow: hidden; font-size: var(--twe-text-sm);
}
.twe-definition-list > dt,
.twe-definition-list > dd { margin: 0; padding: var(--twe-space-2) var(--twe-space-3); background: var(--twe-bg-surface); }
.twe-definition-list > dd { color: var(--twe-text); }
.twe-definition-list > dt {
  background: var(--twe-bg-raised); color: var(--twe-text-muted);
  font-size: var(--twe-text-2xs); letter-spacing: var(--twe-tracking-eyebrow);
  text-transform: uppercase;
}

/* ============================================ 12. TREE / LIST-ROW PRIMITIVES
 * Hierarchical geotech data: boring -> sample -> test. Indent comes from
 * --twe-depth set inline (style="--twe-depth:2"), so nesting needs no nested
 * markup and a flat virtualised list can still render a tree. Markup should be
 * role="tree"/"treeitem" with aria-level, aria-expanded and aria-selected — the
 * CSS reads those same attributes, so there is no second source of truth.
 * Without JS: render every row expanded. Nothing here is hidden by CSS.
 */
.twe-tree { display: flex; flex-direction: column; }
.twe-list-row {
  --twe-depth: 0;
  display: flex; align-items: center; gap: var(--twe-space-3);
  min-block-size: var(--twe-row-h, 2.25rem);
  padding-inline: var(--twe-space-3);
  padding-inline-start: calc(var(--twe-space-3) + var(--twe-depth) * var(--twe-space-5));
  border-block-end: var(--twe-border-width) solid var(--twe-border-light);
  color: var(--twe-text); font-size: var(--twe-text-sm); text-decoration: none;
  transition: background-color var(--twe-dur-fast) var(--twe-ease-out);
}
.twe-list-row:hover { background: var(--twe-bg-card-hover); }
.twe-list-row[aria-selected="true"] { background: var(--twe-accent-dim); box-shadow: inset 3px 0 0 0 var(--twe-accent); }
.twe-list-row:focus-visible { outline: 2px solid var(--twe-accent-ring); outline-offset: -2px; }
.twe-list-row__label { flex: 1 1 auto; min-inline-size: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.twe-list-row__meta  { flex: 0 0 auto; color: var(--twe-text-muted); font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums); font-size: var(--twe-text-xs); }
/* Depth also shifts a guide rule and the text colour, so hierarchy stays
 * legible without relying on indent width alone (which collapses in a pane). */
.twe-list-row[aria-level="2"] { box-shadow: inset 1px 0 0 0 var(--twe-border); }
.twe-list-row[aria-level="3"] { color: var(--twe-text-secondary); }
/* Disclosure twisty, rotated by aria-expanded — attribute-driven, no class. */
.twe-tree__toggle {
  appearance: none; flex: 0 0 auto; display: grid; place-items: center;
  inline-size: 1.25rem; block-size: 1.25rem; padding: 0;
  background: none; border: 0; border-radius: var(--twe-radius-xs);
  color: var(--twe-text-muted); cursor: pointer;
}
.twe-tree__toggle::before {
  content: ""; inline-size: 0.35em; block-size: 0.35em;
  border-inline-end: 2px solid currentColor; border-block-end: 2px solid currentColor;
  transform: rotate(-45deg);
  transition: transform var(--twe-dur-fast) var(--twe-ease-out);
}
.twe-tree__toggle[aria-expanded="true"]::before { transform: rotate(45deg); }
.twe-tree__toggle:focus-visible { outline: 2px solid var(--twe-accent-ring); outline-offset: 1px; }

/* ========================================================= 13. CHART HOST ===
 * THE THEME-TOGGLE TRAP — read before wiring any chart.
 * ECharts (and Chart.js, and Plotly) resolve colours ONCE at init and copy them
 * into a canvas. They do NOT observe CSS custom properties, so flipping
 * data-twe-theme repaints the page around a chart still drawn in the old
 * theme's colours: a dark chart marooned on a white page. There is NO CSS fix.
 * On every data-twe-theme change the tool MUST (1) read the tokens via
 * getComputedStyle(document.documentElement).getPropertyValue('--twe-text')
 * etc. — they come back as strings, trim them — then (2) chart.setOption({...},
 * {notMerge:true}), or dispose and re-init. Also chart.resize() from a
 * ResizeObserver on this host, because a canvas does not reflow. A
 * MutationObserver on <html data-twe-theme> is the standard hook.
 */
.twe-chart-host {
  --twe-chart-ratio: 16 / 9;
  position: relative; inline-size: 100%;
  aspect-ratio: var(--twe-chart-ratio); min-block-size: 12rem;
  background: var(--twe-bg-surface);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-md);
  /* A canvas repaint must not invalidate layout or paint for the page around
   * it — these live in panes that resize while data streams. */
  contain: layout paint; overflow: hidden;
}
.twe-chart-host--tall    { --twe-chart-ratio: 4 / 3; }
.twe-chart-host--strip   { --twe-chart-ratio: 21 / 6; }
.twe-chart-host--profile { --twe-chart-ratio: 3 / 4; }   /* CPT/boring: read vs depth */
.twe-chart-host > canvas,
.twe-chart-host > svg { display: block; inline-size: 100%; block-size: 100%; }
/* A chart is a picture of data and needs a text equivalent — caption, summary
 * or a "view as table" toggle — or the data is unavailable to a screen-reader
 * user. The slot is styled so there is no excuse for omitting it. */
.twe-chart-caption {
  padding: var(--twe-space-2) var(--twe-space-3);
  color: var(--twe-text-muted); font-size: var(--twe-text-xs);
  line-height: var(--twe-leading-snug);
}

/* ============================================== 14. CONTAINER-QUERY RESPONSE
 * Viewport media queries are the wrong tool: these tables live in resizable
 * split panes, docked rails and modal sheets, so the same table is 1400px wide
 * in one pane and 380px in another ON THE SAME SCREEN. A container query asks
 * the only question that matters — how wide is MY box. Guarded per the brief
 * even though container queries land at 105, below the floor; the fallback is
 * the full table with horizontal scroll, degraded but usable.
 * CARD-MODE CAVEAT, mandatory: display:block on table elements REMOVES their
 * implicit table semantics in Chromium, so a screen reader loses row/column
 * relationships. Put explicit role="table"/"rowgroup"/"row"/"columnheader"/
 * "cell" on the markup so semantics survive the display change, and put the
 * column name in data-twe-label on every <td> so each card self-labels.
 */
@supports (container-type: inline-size) {
  .twe-table-wrap { container-type: inline-size; container-name: twe-data; }

  @container twe-data (max-width: 34rem) {
    .twe-table--cards { --twe-row-h: auto; display: block; }
    .twe-table--cards > thead { position: absolute; clip-path: inset(50%); }
    .twe-table--cards > tbody { display: block; }
    .twe-table--cards > tbody > tr {
      display: grid; grid-template-columns: minmax(6rem, 40%) 1fr;
      gap: var(--twe-border-width); margin-block-end: var(--twe-space-3);
      background: var(--twe-border);       /* gap-as-hairline again */
      border-radius: var(--twe-radius); overflow: hidden;
    }
    .twe-table--cards > tbody > tr > td,
    .twe-table--cards > tbody > tr > th {
      display: grid; grid-column: 1 / -1; grid-template-columns: subgrid;
      block-size: auto; padding-block: var(--twe-space-2);
      border-block-start: 0; background-color: var(--twe-bg-surface);
    }
    /* The hidden header text, restored as an in-card label. */
    .twe-table--cards > tbody > tr > td[data-twe-label]::before {
      content: attr(data-twe-label); text-align: start;
      color: var(--twe-text-muted); font-family: var(--twe-font-sans);
      font-size: var(--twe-text-2xs); letter-spacing: var(--twe-tracking-eyebrow);
      text-transform: uppercase;
    }
    /* Freezing is meaningless once rows are cards. */
    .twe-table--cards.twe-table--freeze > tbody > tr > :first-child { position: static; box-shadow: none; }
  }
  /* KPI tiles respond to their own container, not the window. */
  @container twe-data (max-width: 26rem) { .twe-kpi__value { font-size: var(--twe-text-lg); } }
}

/* ============================================================== 15. PRINT ===
 * Geotech deliverables get printed and PDF'd constantly, and a sticky header in
 * a 68vh scroll box prints as one clipped screenful. Unwind all of it, drop the
 * zebra (it wastes toner and scans worse than a plain rule), and lean on
 * --twe-border-strong for contrast on paper.
 */
@media print {
  .twe-table-wrap { max-block-size: none; overflow: visible; border-color: var(--twe-border-strong); box-shadow: none; }
  .twe-table > thead { display: table-header-group; }          /* repeat per page */
  .twe-table > thead > tr > th,
  .twe-table > tfoot > tr > td,
  .twe-table--freeze > thead > tr > :first-child,
  .twe-table--freeze > tbody > tr > :first-child { position: static; box-shadow: none; }
  .twe-table > tbody > tr { --twe-row-tint: transparent; break-inside: avoid; }
  .twe-kpi, .twe-metric   { box-shadow: none; border-color: var(--twe-border-strong); }
  .twe-badge, .twe-status { border-color: var(--twe-border-strong); }
  .twe-chart-host         { break-inside: avoid; }
}

/* ================================================ 16. FORCED-COLORS / HC ====
 * Windows High Contrast is in real accommodation use at TWE. Forced-colors
 * discards our tints, so every state that depended on a tint must fall back to
 * a BORDER, i.e. to shape — otherwise selection and sort state vanish entirely.
 */
@media (forced-colors: active) {
  .twe-table > tbody > tr[aria-selected="true"] > * { border-block-end: 2px solid Highlight; }
  .twe-table th[aria-sort="ascending"],
  .twe-table th[aria-sort="descending"] { border-block-start: 2px solid Highlight; }
  .twe-badge, .twe-status { border: 1px solid CanvasText; forced-color-adjust: none; }
  .twe-progress__fill { background: Highlight; }
  .twe-phasebar__seg[data-twe-state="current"] { border: 2px solid Highlight; }
  .twe-phasebar__seg[data-twe-state="done"]    { border: 1px solid CanvasText; }
}

/* ====================================== 17. PROGRESSIVE ENHANCEMENT (139+) ==
 * corner-shape is Chromium 139+ against a 120 floor, so it can only ever be a
 * bonus: a squircle on the card-shaped elements, the most Apple-studio detail
 * available in pure CSS. Guarded, and never the sole styling — the
 * border-radius above already stands on its own everywhere.
 */
@supports (corner-shape: superellipse(4)) {
  .twe-kpi, .twe-metric, .twe-chart-host { corner-shape: superellipse(4); }
}
