/* =============================================================================
 * TWE UI — surfaces.css
 * Depth, liquid glass, overlays, and designed states.
 *
 *   Version   : 2.1.0
 *   Date      : 2026-09-11
 *   Requires  : tokens.css, linked FIRST. Every value here is a var() read from
 *               it. This file defines no tokens and no :root block — a theme is
 *               a token swap, so nothing below is ever theme-conditional.
 *   Baseline  : Chromium 120 (Tatara / Electron 28.3.3 is the fleet floor).
 *               Anything newer sits inside @supports and is additive only.
 *
 * WHAT THIS FILE IS FOR
 *   1. Making surfaces read as physical objects (the "not flat, not pre-2020"
 *      requirement) — paired shadows plus a lit top edge, never one soft blur.
 *   2. Liquid glass as a *primitive with rules*, not decoration sprayed on.
 *   3. Replacing alert()/confirm() — 17 fleet tools still ship native dialogs
 *      inside an otherwise modern shell, which is the single loudest defect.
 *   4. Designed states. The audit found bare "Loading…" text used as the loading
 *      layer in 10+ places and ZERO skeleton components fleet-wide. A blank div
 *      and a raw string are both treated here as bugs, not as neutral defaults.
 *
 * DOES NOT own buttons, inputs, tables, or the app-shell layout — those are
 * controls.css / forms.css / data.css / shell.css. Do not add them here.
 *
 * PSEUDO-ELEMENT BUDGET (read before extending a component)
 *   ::before is RESERVED on every surface class for the specular top-light ring.
 *   ::after  is RESERVED on .twe-gradient-border for the hairline.
 *   Consumers who need their own pseudo-elements should nest a child element.
 *
 * WHY THE TOP-LIGHT IS A PSEUDO-ELEMENT AND NOT PART OF box-shadow
 *   The obvious form is `box-shadow: var(--twe-elev-1), var(--twe-highlight)`.
 *   It is a trap: the field theme sets --twe-highlight: none, and `none` is only
 *   legal as the SOLE value of box-shadow. The substituted list becomes invalid
 *   at computed-value time, the whole property computes to its initial value,
 *   and every card in the field theme silently loses its shadow. Splitting the
 *   two across element (elevation) and ::before (highlight) makes `none` a valid
 *   standalone value and keeps both tokens live in all four themes.
 * ========================================================================== */


/* ==========================================================================
 * 1. CARDS, PANELS, SURFACES
 *
 * ELEVATION BY MEANING — copied from tokens.css §3 so it is enforced here.
 * Assign by what the thing *is*, never by how much shadow looks nice:
 *   --twe-elev-0  flush     sits in the page (table rows, list items)
 *   --twe-elev-1  resting   cards, tiles, chips              <- .twe-card
 *   --twe-elev-2  raised    hover of a resting element, sticky headers
 *   --twe-elev-3  floating  dropdowns, popovers, tooltips, palette
 *   --twe-elev-4  overlay   modals, sheets, dialogs          <- .twe-dialog
 *   --twe-elev-5  detached  drag-in-flight only
 * Two elements at the same elevation are a claim that they are the same kind of
 * object. If that is not true, one of them is wrong.
 * ========================================================================== */

.twe-surface,
.twe-panel,
.twe-card {
  position: relative;              /* containing block for the ::before ring */
  border: var(--twe-border-width) solid var(--twe-border);
  color: var(--twe-text);
  border-radius: var(--twe-radius-md);
}

/* The lit top edge. One inset line does more for perceived dimensionality than
 * any amount of blur: it is what makes a surface read as a panel catching light
 * rather than a coloured div. pointer-events:none so it never eats a click. */
.twe-surface::before,
.twe-panel::before,
.twe-card::before,
.twe-glass::before,
.twe-dialog::before,
.twe-popover::before,
.twe-toast::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: var(--twe-highlight);
  pointer-events: none;
}

/* Flush surface: structural background, no lift. Use for the page's own regions
 * — a card floating inside a card is depth inflation and reads as noise. */
.twe-surface {
  background: var(--twe-bg-surface);
  box-shadow: var(--twe-elev-0);
}

/* Panel: a named, bordered region of the app — filter rail, detail pane. */
.twe-panel {
  background: var(--twe-bg-raised);
  border-radius: var(--twe-radius-lg);
  padding: var(--twe-space-5);
  box-shadow: var(--twe-elev-1);
}

/* Card: a discrete object the user can think about as one thing. */
.twe-card {
  background: var(--twe-bg-card);
  padding: var(--twe-space-4);
  box-shadow: var(--twe-elev-1);
}

.twe-card--flush { padding: 0; }
.twe-card--roomy { padding: var(--twe-space-6); }

/* Interactive card: only for a card that is genuinely one big activation
 * target. A hover lift on a card the user cannot click is a lie about
 * affordance, and it is how "everything wiggles" UIs happen. */
.twe-card--interactive {
  cursor: pointer;
  transition:
    box-shadow  var(--twe-transition),
    translate   var(--twe-transition),
    background  var(--twe-transition),
    border-color var(--twe-transition);
}

.twe-card--interactive:hover {
  background: var(--twe-bg-card-hover);
  border-color: var(--twe-border-strong);
  box-shadow: var(--twe-elev-2);
  translate: 0 -1px;               /* exactly 1px: any more reads as a toy */
}

.twe-card--interactive:active {
  box-shadow: var(--twe-elev-1);
  translate: 0 0;                  /* press returns it to the page */
}

/* Focus is not optional and is never hover-only — keyboard users get the same
 * affordance. :focus-visible so a mouse click does not leave a ring behind. */
.twe-card--interactive:focus-visible {
  outline: none;
  box-shadow: var(--twe-elev-2), var(--twe-elev-glow);
}

/* Selected state is dual-encoded: accent ring AND an accent-tinted ground, so
 * it survives greyscale printing and colour-vision deficiency. Callers should
 * still set aria-selected / aria-pressed — CSS is not an accessible name. */
.twe-card--selected {
  border-color: transparent;
  background: var(--twe-bg-card-hover);
  box-shadow: var(--twe-elev-2), var(--twe-elev-glow);
}

/* --- Squircle upgrade (Chromium 139+) -------------------------------------
 * A true superellipse is the difference between "rounded rectangle" and the
 * Apple-studio silhouette. Guarded and additive: at the floor the plain
 * border-radius above is already correct, so nothing degrades to a hard corner.
 * The radius steps up because a superellipse consumes visual radius — the same
 * number reads tighter than a circular arc does. */
@supports (corner-shape: superellipse(1.8)) {
  .twe-card,
  .twe-panel,
  .twe-surface {
    corner-shape: superellipse(1.8);
    border-radius: var(--twe-radius-lg);
  }
  .twe-dialog,
  .twe-glass,
  .twe-popover {
    corner-shape: superellipse(1.8);
    border-radius: var(--twe-radius-xl);
  }
}


/* ==========================================================================
 * 2. LIQUID GLASS
 *
 * Ported from TWE-Website components/ui/GlassSurface.tsx to dependency-free
 * CSS. Three tiers, degrading in this order:
 *   .twe-glass          blur + saturate + specular top-light + 1px glass border
 *   .twe-glass--frost   same, no refraction (the default here — see below)
 *   solid               automatic: tokens.css already collapses --twe-glass-bg
 *                       to --twe-bg-raised and --twe-glass-blur to 0px under
 *                       @media (prefers-reduced-transparency: reduce) and in
 *                       the field theme. VERIFIED: with blur(0px) saturate(1)
 *                       the filter is a no-op and the ground is opaque, so the
 *                       rules below render a plain readable panel with a real
 *                       border and shadow. No extra class, no JS.
 *
 * ---------------------------------------------------------------------------
 * WHEN NOT TO USE GLASS — this list is mandatory, not advisory.
 * Glass over content systematically fails WCAG AA because the backdrop is
 * unknown at authoring time: contrast becomes a property of whatever happens to
 * scroll underneath. Do NOT use it for:
 *   - Dense data tables. Text over a moving backdrop is unreadable at 13px, and
 *     backdrop-filter on a tall scroller repaints the whole region per frame.
 *   - Long-form reading (reports, logs, help). Sustained reading needs an
 *     opaque, stable ground.
 *   - The field theme. Translucency is unusable on a tablet in Gulf-Coast
 *     sunlight; the theme turns it off for that reason. Never re-enable it with
 *     a literal colour.
 *   - Anything over photography, maps, plates or borehole imagery. This is the
 *     one that always ships and always fails the contrast check.
 * Use it for CHROME over content: floating toolbars, command palettes, nav
 * rails, dialog surfaces, toasts. Chrome is short-lived and short-text.
 *
 * STACKING-CONTEXT / CONTAINING-BLOCK WARNING
 * backdrop-filter creates a new stacking context AND a containing block for
 * fixed- and absolutely-positioned descendants. Consequences that have already
 * cost the fleet time:
 *   - position:fixed inside a glass panel is trapped inside the panel. Never
 *     nest .twe-toaster, .twe-dialog or .twe-scrim inside .twe-glass — mount
 *     them on <body>.
 *   - z-index inside a glass panel is scoped to it, so a child cannot climb
 *     above a sibling of the panel. Fix the layering at the panel, not inside.
 *   - Nested glass double-blurs and washes out. One glass layer per stack.
 * isolation:isolate is set below to make that stacking context explicit rather
 * than emergent, so the layering is legible to the next reader.
 * ========================================================================== */

.twe-glass,
.twe-glass--frost {
  position: relative;
  isolation: isolate;
  background: var(--twe-glass-bg);
  border: var(--twe-border-width) solid var(--twe-glass-border);
  border-radius: var(--twe-radius-lg);
  box-shadow: var(--twe-elev-3);
  color: var(--twe-text);
  /* -webkit- first: Electron 28/30 (Chromium 120/124) still prefer the
   * prefixed property on some GPU/ANGLE paths on this fleet's Intel iGPUs. */
  -webkit-backdrop-filter: blur(var(--twe-glass-blur)) saturate(var(--twe-glass-sat));
  backdrop-filter: blur(var(--twe-glass-blur)) saturate(var(--twe-glass-sat));
}

/* Refraction tier — OPT-IN, AND IT HAS A HOST REQUIREMENT.
 * The warp is an SVG feDisplacementMap referenced from backdrop-filter. Chromium
 * resolves filter url() only against the CURRENT document, so a data: URI or an
 * external file does not work: the host page must inline the filter once. If the
 * id is missing, Chromium treats the filter reference as invalid and the panel
 * can render with NO backdrop-filter at all — so never add this class
 * speculatively. Paste this into the page, immediately after <body>:
 *
 *   <svg aria-hidden="true" width="0" height="0" style="position:absolute">
 *     <filter id="twe-glass-refract" x="-20%" y="-20%" width="140%" height="140%">
 *       <feTurbulence type="fractalNoise" baseFrequency="0.009 0.012"
 *                     numOctaves="2" seed="7" result="n"/>
 *       <feDisplacementMap in="SourceGraphic" in2="n" scale="28"
 *                          xChannelSelector="R" yChannelSelector="G"/>
 *     </filter>
 *   </svg>
 *
 * Cost: a per-frame displacement pass. Acceptable on one hero panel; not on a
 * list. The unprefixed -webkit- fallback deliberately omits the warp so that a
 * failed url() still leaves frost rather than nothing. */
@supports (backdrop-filter: url('#twe-glass-refract')) {
  .twe-glass--refract {
    -webkit-backdrop-filter: blur(var(--twe-glass-blur)) saturate(var(--twe-glass-sat));
    backdrop-filter:
      blur(var(--twe-glass-blur))
      saturate(var(--twe-glass-sat))
      url('#twe-glass-refract');
  }
}

/* Reduced transparency: the tokens already went opaque, but the refraction is a
 * *filter*, not a colour, so it has to be switched off here explicitly or the
 * panel keeps warping a ground the user asked to have flattened. */
@media (prefers-reduced-transparency: reduce) {
  .twe-glass,
  .twe-glass--frost,
  .twe-glass--refract {
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
}


/* ==========================================================================
 * 3. GRADIENT HAIRLINES
 * A 1px gradient edge is the cheapest way to say "someone designed this", and
 * the fastest way to wreck a UI if it goes on everything. Opt-in only.
 * ========================================================================== */

/* Primary technique: two-layer background with different clip boxes. No mask,
 * no pseudo-element, no hardcoded colour, and it works to Chromium 4 — so this
 * is the one to reach for. Trade-off: it OWNS the element's background, so it
 * cannot be combined with .twe-glass (which needs its own translucent ground).
 * For that case use .twe-gradient-border--overlay below. */
.twe-gradient-border {
  border: var(--twe-border-width) solid transparent;
  border-radius: var(--twe-radius-md);
  background:
    linear-gradient(var(--twe-bg-card), var(--twe-bg-card)) padding-box,
    linear-gradient(135deg, var(--twe-accent), var(--twe-border-strong) 65%) border-box;
}

/* Overlay technique for elements that already own their background (glass,
 * images, video). mask-composite punches the interior out of a padding-inset
 * rectangle, leaving only the 1px frame.
 * The mask gradient uses currentColor rather than a literal: a mask only reads
 * the ALPHA channel, and --twe-text is fully opaque in all four themes, so
 * currentColor is a token-derived way to say "solid". If a caller sets
 * color:transparent on the element, the hairline disappears — that is the one
 * documented failure mode. */
.twe-gradient-border--overlay { position: relative; }

.twe-gradient-border--overlay::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  padding: var(--twe-border-width);
  background: linear-gradient(135deg, var(--twe-accent), var(--twe-border-strong) 65%);
  -webkit-mask:
    linear-gradient(currentColor 0 0) content-box,
    linear-gradient(currentColor 0 0);
  mask:
    linear-gradient(currentColor 0 0) content-box,
    linear-gradient(currentColor 0 0);
  -webkit-mask-composite: xor;      /* legacy keyword, kept for Electron 28 */
  mask-composite: exclude;
  pointer-events: none;
}

/* --- The single animated case: "this is live right now" -------------------
 * Private, component-scoped custom property. The leading underscore marks it as
 * NOT a design token: it exists only because a conic gradient angle cannot be
 * animated unless the property is registered with a type. Do not read it from
 * another file.
 * PERF COST, stated so it is a decision and not an accident: this repaints the
 * border every frame for as long as it is in the DOM, and it cannot be
 * composited on the GPU because the paint itself changes. Budget ONE per screen,
 * only for a genuinely live thing (a running job, an open socket, a recording),
 * and remove the class the moment it stops. It is not a decoration. */
@property --_twe-border-angle {
  syntax: '<angle>';
  inherits: false;
  initial-value: 0deg;
}

.twe-gradient-border--live::after {
  background: conic-gradient(
    from var(--_twe-border-angle),
    transparent 0% 55%,
    var(--twe-accent) 78%,
    transparent 100%
  );
  animation: twe-border-spin 4s linear infinite;
}

@keyframes twe-border-spin {
  to { --_twe-border-angle: 360deg; }
}

/* Frozen mid-sweep would read as a rendering bug, so reduced motion gets a
 * static accent hairline instead of a stopped animation. */
@media (prefers-reduced-motion: reduce) {
  .twe-gradient-border--live::after {
    animation: none;
    background: linear-gradient(135deg, var(--twe-accent), var(--twe-accent));
  }
}


/* ==========================================================================
 * 4. DIALOG — the alert()/confirm() replacement
 *
 * WHY NATIVE <dialog> AND NOT A DIV
 * Called with .showModal(), the platform gives away for free, and correctly,
 * every part that hand-rolled div modals in this fleet get wrong:
 *   - focus trap inside the dialog while it is open
 *   - Escape to dismiss, firing a cancel event
 *   - the rest of the page made inert (not just visually dimmed — actually
 *     unreachable by keyboard, screen reader and pointer)
 *   - the top layer, so it paints above everything without a z-index war
 *   - aria-modal semantics without an attribute
 * There is no CSS-only equivalent of any of that. This is why it replaces the
 * 17 tools still using alert()/confirm().
 *
 * NO-JS DEGRADATION: a <dialog> without .showModal() is simply not displayed —
 * it never blocks content. For a flow that must work scriptless, render the same
 * markup as a .twe-panel inline in the page instead.
 *
 * MARKUP
 *   <dialog class="twe-dialog" aria-labelledby="t1">
 *     <form method="dialog" class="twe-dialog__header">…</form>   (or a div)
 *     <div class="twe-dialog__header"><h2 id="t1">Title</h2></div>
 *     <div class="twe-dialog__body">…</div>
 *     <div class="twe-dialog__footer">…buttons…</div>
 *   </dialog>
 * A footer button inside <form method="dialog"> closes with a returnValue and
 * needs no JS at all.
 * ========================================================================== */

.twe-dialog {
  /* Reset the UA's 1990s box: solid border, 1em padding, auto sizing. */
  padding: 0;
  border: var(--twe-border-width) solid var(--twe-border-strong);
  border-radius: var(--twe-radius-lg);
  background: var(--twe-bg-raised);
  color: var(--twe-text);
  box-shadow: var(--twe-elev-4);
  /* FLEET BUG, found live 2026-09-11 (Ajera MCP payroll UI — Matt: the
   * confirm dialog rendered top-left instead of centered): this was
   * `position: relative`, which overrides the UA's own `dialog:modal {
   * position: fixed; inset: 0; margin: auto; }` centering rule. A
   * top-layer dialog with `position: relative` and no inset falls back
   * to its static position instead of being centered in the viewport —
   * every tool using .twe-dialog for a confirm/info modal had this same
   * silent bug, not just this one tool. `position: fixed` restores the
   * UA contract (still needed explicitly since the rest of this ruleset
   * — width/max-height/etc. below — must apply to the SAME box the UA
   * centers, not a second wrapper). .twe-dialog--sheet's own
   * `margin: auto auto 0` bottom-anchoring also depends on this fixed +
   * full-viewport containing block, so this fixes both variants at once.
   *
   * SECOND half of the same bug, found testing this fix live: base.css's
   * global reset (`*, *::before, *::after { margin: 0; }`) also clobbers
   * the UA's `dialog:modal { margin: auto; }` — author-origin styles beat
   * UA styles for a given property regardless of specificity, so the
   * reset wins even though it's a lower-specificity universal selector.
   * `inset: 0` + an explicit `margin: auto` below restore centering
   * without relying on either the UA default OR getting lucky against
   * the reset — the fix has to be self-contained here. */
  position: fixed;
  inset: 0;
  margin: auto;

  width: min(32rem, calc(100vw - var(--twe-space-8)));
  /* Height is capped so the dialog never grows past the viewport and takes its
   * own buttons off-screen — the classic long-confirmation failure. The body
   * scrolls; the header and footer do not. */
  max-height: min(85vh, calc(100vh - var(--twe-space-8)));
  max-width: calc(100vw - var(--twe-space-6));
  overflow: visible;

  opacity: 0;
  scale: 0.97;
  translate: 0 8px;
  transition:
    opacity   var(--twe-dur-enter) var(--twe-ease),
    scale     var(--twe-dur-enter) var(--twe-ease),
    translate var(--twe-dur-enter) var(--twe-ease),
    /* allow-discrete is what lets display and the top-layer membership animate
     * instead of snapping. Chromium 117+, i.e. safe at the 120 floor. */
    overlay   var(--twe-dur-enter) var(--twe-ease) allow-discrete,
    display   var(--twe-dur-enter) var(--twe-ease) allow-discrete;
}

/* display MUST be scoped to [open]: the UA keeps a closed dialog at
 * display:none, and an unconditional display:grid here would leave every dialog
 * in the app permanently visible. */
.twe-dialog[open] {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  opacity: 1;
  scale: 1;
  translate: 0 0;
}

/* Entry side of the animation. Without @starting-style the element has no
 * "before" style to interpolate from and the dialog pops in. */
@starting-style {
  .twe-dialog[open] {
    opacity: 0;
    scale: 0.97;
    translate: 0 8px;
  }
}

/* --- Backdrop -------------------------------------------------------------
 * KNOWN FLEET GAP, measured: ::backdrop did not inherit custom properties until
 * Chromium 122. Every tool on 124+ (Daishi, EdaphOS, Fjarri, Nedra, OCC,
 * Caementum, Lilly, and everything browser-served on the LAN host) resolves
 * these vars correctly. TATARA (Electron 28.3.3 / Chromium 120) DOES NOT — the
 * declarations below become invalid there and its dialogs get no scrim at all.
 * Two remedies, both real:
 *   1. bump Tatara's Electron, or
 *   2. add .twe-dialog--scrim-fallback (below), which paints the scrim from the
 *      dialog element itself, where inheritance works.
 * Do not "fix" this by hardcoding a colour here. */
.twe-dialog::backdrop {
  background: var(--twe-bg-overlay);
  -webkit-backdrop-filter: blur(var(--twe-glass-blur));
  backdrop-filter: blur(var(--twe-glass-blur));
  opacity: 0;
  transition:
    opacity var(--twe-dur-enter) var(--twe-ease),
    overlay var(--twe-dur-enter) var(--twe-ease) allow-discrete,
    display var(--twe-dur-enter) var(--twe-ease) allow-discrete;
}

.twe-dialog[open]::backdrop { opacity: 1; }

@starting-style {
  .twe-dialog[open]::backdrop { opacity: 0; }
}

/* Chromium <122 escape hatch: a viewport-spanning spread shadow drawn by the
 * dialog, which inherits tokens normally. No blur (a box-shadow cannot blur its
 * backdrop) but it restores the modal separation, which is the part that
 * matters. Apply ONLY on tools at the floor — on 122+ it doubles the scrim. */
.twe-dialog--scrim-fallback[open] { box-shadow: var(--twe-elev-4), 0 0 0 100vmax var(--twe-bg-overlay); }

.twe-dialog__header {
  display: flex;
  align-items: center;
  gap: var(--twe-space-3);
  padding: var(--twe-space-4) var(--twe-space-5);
  border-bottom: var(--twe-border-width) solid var(--twe-border);
  font-size: var(--twe-text-lg);
  line-height: var(--twe-leading-snug);
}

.twe-dialog__title {
  margin: 0;
  font-size: inherit;
  font-weight: 400;              /* only weight 400 is a real vendored face */
  color: var(--twe-text);
}

.twe-dialog__body {
  padding: var(--twe-space-5);
  overflow-y: auto;
  /* Keeps a wheel/trackpad flick at the end of the list from scrolling the page
   * behind the modal. */
  overscroll-behavior: contain;
  font-size: var(--twe-text-base);
  line-height: var(--twe-leading-body);
  color: var(--twe-text-secondary);
  /* Global ::-webkit-scrollbar styling lives in the base layer; this is the
   * standards-track pair, ignored below Chromium 121 and harmless there. */
  scrollbar-width: thin;
  scrollbar-color: var(--twe-scrollbar-thumb) var(--twe-scrollbar-track);
}

.twe-dialog__footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--twe-space-2);
  padding: var(--twe-space-4) var(--twe-space-5);
  border-top: var(--twe-border-width) solid var(--twe-border);
  background: var(--twe-bg-surface);
  border-radius: 0 0 var(--twe-radius-lg) var(--twe-radius-lg);
}

.twe-dialog--sm { width: min(24rem, calc(100vw - var(--twe-space-8))); }
.twe-dialog--lg { width: min(56rem, calc(100vw - var(--twe-space-8))); }

/* Glass dialog: legitimate use — chrome, short text, short-lived. Still opaque
 * under reduced transparency and in the field theme via the tokens. */
.twe-dialog--glass {
  background: var(--twe-glass-bg);
  border-color: var(--twe-glass-border);
  -webkit-backdrop-filter: blur(var(--twe-glass-blur)) saturate(var(--twe-glass-sat));
  backdrop-filter: blur(var(--twe-glass-blur)) saturate(var(--twe-glass-sat));
}

/* --- Bottom sheet --------------------------------------------------------
 * Slides up from the bottom edge and stays thumb-reachable — the correct shape
 * on the field tablets. The bottom corners are square because they sit off the
 * viewport edge; that is the one sanctioned exception to "nothing has a 0
 * radius". Safe-area insets keep the footer clear of the iOS home indicator and
 * Android gesture bar. */
.twe-dialog--sheet {
  width: 100vw;
  max-width: 100vw;
  max-height: 88vh;
  margin: auto auto 0;
  border-radius: var(--twe-radius-xl) var(--twe-radius-xl) 0 0;
  border-bottom: 0;
  translate: 0 100%;
  scale: 1;
}

.twe-dialog--sheet[open] {
  translate: 0 0;
  scale: 1;
}

@starting-style {
  .twe-dialog--sheet[open] { translate: 0 100%; scale: 1; }
}

.twe-dialog--sheet .twe-dialog__footer {
  border-radius: 0; /* twe-ui-lint: allow tell-5 -- a bottom sheet is anchored to the bottom edge, so its footer is flush with the viewport; a radius here would show a sliver of page behind the corners */
  padding-bottom: calc(var(--twe-space-4) + env(safe-area-inset-bottom, 0px));
}

.twe-dialog--sheet .twe-dialog__body {
  padding-left: max(var(--twe-space-5), env(safe-area-inset-left, 0px));
  padding-right: max(var(--twe-space-5), env(safe-area-inset-right, 0px));
}

/* A drag handle is an affordance, not a control — the sheet is still dismissed
 * by Escape and by the footer button, and dragging it needs JS the sheet does
 * not require. position:relative on the header so the handle anchors to the
 * header rather than to the dialog; the extra top padding is its clearance. */
.twe-dialog--sheet .twe-dialog__header {
  position: relative;
  padding-block-start: var(--twe-space-5);
}

.twe-dialog--sheet .twe-dialog__header::before {
  content: '';
  position: absolute;
  inset-block-start: var(--twe-space-2);
  inset-inline: 0;
  margin: 0 auto;
  width: var(--twe-space-8);
  height: var(--twe-space-1);
  border-radius: var(--twe-radius-pill);
  background: var(--twe-border-strong);
}


/* ==========================================================================
 * 5. POPOVER + MENU
 * [popover] (Chromium 114+, safe at the floor) gives light-dismiss, Escape and
 * top-layer promotion from one HTML attribute — no outside-click listener, no
 * z-index, no scroll-lock hack. Use popover="auto" with popovertarget on the
 * trigger and the whole thing works with zero JS.
 * ========================================================================== */

.twe-popover {
  position: fixed;
  /* Reset the UA popover box (inset:0 + margin:auto centres it, border:solid). */
  inset: auto;
  margin: 0;
  padding: var(--twe-space-2);
  width: max-content;
  max-width: min(22rem, calc(100vw - var(--twe-space-6)));
  max-height: min(60vh, 26rem);
  overflow: auto;
  overscroll-behavior: contain;
  border: var(--twe-border-width) solid var(--twe-border-strong);
  border-radius: var(--twe-radius-md);
  background: var(--twe-bg-raised);
  color: var(--twe-text);
  box-shadow: var(--twe-elev-3);
  scrollbar-width: thin;
  scrollbar-color: var(--twe-scrollbar-thumb) var(--twe-scrollbar-track);

  opacity: 0;
  translate: 0 -4px;
  transition:
    opacity   var(--twe-dur-fast) var(--twe-ease),
    translate var(--twe-dur-fast) var(--twe-ease),
    overlay   var(--twe-dur-fast) var(--twe-ease) allow-discrete,
    display   var(--twe-dur-fast) var(--twe-ease) allow-discrete;
}

.twe-popover:popover-open { opacity: 1; translate: 0 0; }

@starting-style {
  .twe-popover:popover-open { opacity: 0; translate: 0 -4px; }
}

.twe-popover::backdrop { background: transparent; }

/* Anchor positioning — Chromium 125+, so guarded.
 * STATIC FALLBACK AT THE FLOOR: outside this block a .twe-popover is a fixed
 * element with no computed position, which lands at the viewport's top-left.
 * Tools on Electron 28–32 must therefore either (a) give the popover an explicit
 * inset in a wrapper class, or (b) set inset-block-start/inset-inline-start from
 * the trigger's getBoundingClientRect() in the popovertoggle handler — about six
 * lines, no library. Either way it is positioned, never accidental.
 * --_twe-anchor carries the private underscore prefix on purpose: it is a
 * dashed-ident naming an anchor, NOT a design token, and the token linter greps
 * for bare --twe-* names. Anything in this file spelled --twe-* is a real token
 * from tokens.css; anything spelled --_twe-* is component-private. */
@supports (anchor-name: --_twe-a) and (position-anchor: --_twe-a) {
  .twe-popover[data-twe-anchor] {
    position-anchor: --_twe-anchor;
    inset-block-start: anchor(bottom);
    inset-inline-start: anchor(left);
    margin-block-start: var(--twe-space-1);
    position-try-fallbacks: flip-block, flip-inline;
  }
  [data-twe-anchor-name] { anchor-name: --_twe-anchor; }
}

/* --- Menu (roving-tabindex listbox look) ---------------------------------
 * NEEDS JS for the roving tabindex and Up/Down/Home/End handling — that is a
 * keyboard contract, not a style. Without it, degrade by making every item a
 * real <button> or <a>: Tab reaches each one and the menu stays fully operable,
 * just without arrow keys. Never ship <div> items.
 * Set role="menu"/role="menuitem" (or listbox/option) on the elements. */
.twe-menu {
  display: flex;
  flex-direction: column;
  gap: 1px;                        /* 1px, never fractional: 125%/150% DPI */
  margin: 0;
  padding: 0;
  list-style: none;
}

.twe-menu__item {
  display: flex;
  align-items: center;
  gap: var(--twe-space-3);
  width: 100%;
  min-height: var(--twe-tap-min);
  padding: var(--twe-space-2) var(--twe-space-3);
  border: 0;
  border-radius: var(--twe-radius-sm);
  background: transparent;
  color: var(--twe-text);
  font: inherit;
  font-size: var(--twe-text-base);
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition: background var(--twe-dur-fast) var(--twe-ease);
}

.twe-menu__item:hover { background: var(--twe-bg-card-hover); }

.twe-menu__item:focus-visible {
  outline: none;
  background: var(--twe-bg-card-hover);
  box-shadow: var(--twe-elev-glow);
}

/* Selection carries a ✓ as well as the tint — never colour alone (Section 508
 * exposure is real on public-sector work). */
.twe-menu__item[aria-selected='true'],
.twe-menu__item[aria-checked='true'] {
  background: var(--twe-accent-dim);
  color: var(--twe-accent-text);
}

.twe-menu__item[aria-selected='true']::after,
.twe-menu__item[aria-checked='true']::after {
  content: '\2713';
  margin-inline-start: auto;
  font-size: var(--twe-text-sm);
}

.twe-menu__item[aria-disabled='true'] {
  color: var(--twe-text-muted);
  cursor: not-allowed;
  /* aria-disabled, not the disabled attribute: a disabled control is removed
   * from the tab order and the user can no longer discover WHY it is off. */
}

.twe-menu__label {
  padding: var(--twe-space-2) var(--twe-space-3) var(--twe-space-1);
  font-size: var(--twe-text-2xs);
  letter-spacing: var(--twe-tracking-eyebrow);
  text-transform: uppercase;
  color: var(--twe-text-muted);
}

.twe-menu__separator {
  height: var(--twe-border-width);
  margin: var(--twe-space-1) 0;
  border: 0;
  background: var(--twe-border);
}


/* ==========================================================================
 * 6. TOOLTIP
 * RULE: a tooltip may only ever REPEAT or ABBREVIATE something available
 * elsewhere. It is invisible on touch, invisible to many magnifier users, and
 * gone the moment focus moves — so anything it alone carries is information the
 * user cannot get. Icon-only controls need an aria-label as well as this.
 * ========================================================================== */

/* The host must be positioned; the tooltip anchors to it. Hover AND
 * :focus-within, so keyboard users get the same affordance. */
.twe-tooltip-host { position: relative; }

.twe-tooltip {
  /* Private offset var: the open state has to override only the Y component,
   * and it cannot rebuild `translate` without losing the -50% X centring. */
  --_twe-tt-y: 2px;
  position: absolute;
  z-index: var(--twe-z-tooltip);
  inset-block-start: calc(100% + var(--twe-space-2));   /* default: below */
  inset-inline-start: 50%;
  translate: -50% var(--_twe-tt-y);
  width: max-content;
  max-width: 18rem;
  padding: var(--twe-space-1) var(--twe-space-2);
  border: var(--twe-border-width) solid var(--twe-border-strong);
  border-radius: var(--twe-radius-sm);
  background: var(--twe-bg-raised);
  color: var(--twe-text);
  box-shadow: var(--twe-elev-3);
  font-size: var(--twe-text-xs);
  line-height: var(--twe-leading-snug);
  pointer-events: none;            /* must never block the control it describes */
  opacity: 0;
  visibility: hidden;
  /* This is the CLOSING transition (it lives on the resting state), so it
   * carries no delay — a delayed exit strands the bubble over whatever the
   * cursor reached next. visibility is held for the fade duration so the
   * bubble is still painted while it fades, then leaves the a11y tree. */
  transition:
    opacity    var(--twe-dur-fast) var(--twe-ease),
    translate  var(--twe-dur-fast) var(--twe-ease),
    visibility 0s linear var(--twe-dur-fast);
}

.twe-tooltip-host:hover > .twe-tooltip,
.twe-tooltip-host:focus-within > .twe-tooltip,
.twe-tooltip[data-twe-open] {
  opacity: 1;
  translate: -50% 0;
  visibility: visible;
  /* Delay IN only. Declared here, on the open state, because a delay on the
   * resting state would also delay the exit. Without it the tooltip fires on
   * every incidental pointer transit across the control. */
  transition-delay: var(--twe-dur-slow);
}

/* Arrow: a rotated square, so it takes the bubble's own surface colour and
 * border and cannot drift out of sync the way a border-triangle does. Two
 * adjacent borders only — the other two sit under the bubble. */
.twe-tooltip::after {
  content: '';
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 50%;
  width: var(--twe-space-2);
  height: var(--twe-space-2);
  translate: -50% -50%;
  rotate: 45deg;
  background: var(--twe-bg-raised);
  border-left: var(--twe-border-width) solid var(--twe-border-strong);
  border-top: var(--twe-border-width) solid var(--twe-border-strong);
}

/* Above the host: flip both the bubble's offset and the arrow's edge. */
.twe-tooltip--above {
  --_twe-tt-y: -2px;
  inset-block-start: auto;
  inset-block-end: calc(100% + var(--twe-space-2));
}

.twe-tooltip--above::after {
  inset-block-start: auto;
  inset-block-end: 0;
  translate: -50% 50%;
  border: 0;
  border-right: var(--twe-border-width) solid var(--twe-border-strong);
  border-bottom: var(--twe-border-width) solid var(--twe-border-strong);
}


/* ==========================================================================
 * 7. TOASTS
 * MARKUP CONTRACT (the aria half is not optional):
 *   <div class="twe-toaster" role="status" aria-live="polite">…</div>
 *   <div class="twe-toaster" role="alert"  aria-live="assertive">…</div>
 * The two regions differ ONLY by role/aria-live. There is deliberately no
 * twe-toaster--urgent modifier: an earlier draft of this comment named one, and
 * every reader who copied it shipped a class that matches no rule. Both regions
 * are visually identical by design — urgency is announced, not coloured.
 * Two regions, because assertive interrupts whatever the screen reader is
 * saying: successes and info go in the polite region, warnings and failures in
 * the assertive one. One region with a switched aria-live is not announced
 * reliably — the live region has to exist in the DOM before the text arrives.
 *
 * NO-JS DEGRADATION: server-rendered toasts work as plain markup in the region;
 * only auto-dismiss needs a timer. A toast that carries the only record of a
 * failure is a bug — mirror it into the page or a log.
 * ========================================================================== */

.twe-toaster {
  position: fixed;
  z-index: var(--twe-z-toast);
  inset-block-end: max(var(--twe-space-4), env(safe-area-inset-bottom, 0px));
  inset-inline-end: max(var(--twe-space-4), env(safe-area-inset-right, 0px));
  display: flex;
  /* `column`, not column-reverse: the region is anchored to the bottom edge, so
   * plain DOM order puts an appendChild'd toast at the BOTTOM — newest nearest
   * the corner, where the eye already is, and older ones pushed up and away.
   * column-reverse inverts that and makes appendChild put the newest furthest
   * from the corner, which is the bug this comment exists to prevent. */
  flex-direction: column;
  gap: var(--twe-space-2);
  width: min(24rem, calc(100vw - var(--twe-space-8)));
  /* The region spans a lot of screen; letting it swallow clicks would make the
   * bottom-right corner of every tool mysteriously dead. */
  pointer-events: none;
}

.twe-toast {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: start;
  gap: var(--twe-space-3);
  padding: var(--twe-space-3) var(--twe-space-4);
  border: var(--twe-border-width) solid var(--twe-border-strong);
  border-radius: var(--twe-radius-md);
  background: var(--twe-bg-raised);
  color: var(--twe-text);
  box-shadow: var(--twe-elev-3);
  font-size: var(--twe-text-base);
  line-height: var(--twe-leading-snug);
  pointer-events: auto;
  animation: twe-toast-in var(--twe-dur-enter) var(--twe-ease) both;
}

@keyframes twe-toast-in {
  from { opacity: 0; translate: 0 var(--twe-space-4); scale: 0.98; }
  to   { opacity: 1; translate: 0 0;                  scale: 1; }
}

/* Exit is class-driven because a removal cannot be transitioned: JS adds
 * --leaving, waits for animationend, then removes the node. */
.twe-toast--leaving { animation: twe-toast-out var(--twe-dur) var(--twe-ease-in-out) both; }

@keyframes twe-toast-out {
  to { opacity: 0; translate: var(--twe-space-4) 0; scale: 0.98; }
}

/* Kind map. The tinted ground and the leading edge come from the paired
 * --twe-*-bg / hue tokens; the glyph and the required text prefix ("Failed:",
 * "Warning:") are what make the kind legible without colour. */
.twe-toast__icon {
  display: grid;
  place-items: center;
  width: var(--twe-space-5);
  height: var(--twe-space-5);
  border-radius: var(--twe-radius-pill);
  font-size: var(--twe-text-xs);
  font-family: var(--twe-font-mono);
  color: var(--twe-text-inverse);
}

.twe-toast__body { min-width: 0; }

.twe-toast__title {
  margin: 0;
  font-size: var(--twe-text-base);
  font-weight: 400;
}

.twe-toast__detail {
  margin: var(--twe-space-1) 0 0;
  font-size: var(--twe-text-xs);
  color: var(--twe-text-secondary);
}

/* Action slot: at most one action plus a dismiss. A toast is a notification,
 * not a form — anything needing two decisions is a .twe-dialog. */
.twe-toast__action {
  align-self: center;
  white-space: nowrap;
}

.twe-toast--ok     { background: var(--twe-ok-bg); }
.twe-toast--warn   { background: var(--twe-warn-bg); }
.twe-toast--danger { background: var(--twe-danger-bg); }
.twe-toast--info   { background: var(--twe-info-bg); }

.twe-toast--ok     .twe-toast__icon { background: var(--twe-ok); }
.twe-toast--warn   .twe-toast__icon { background: var(--twe-warn); }
.twe-toast--danger .twe-toast__icon { background: var(--twe-danger); }
.twe-toast--info   .twe-toast__icon { background: var(--twe-info); }

/* Shape as the third channel, so kind survives greyscale and monochrome print:
 * a left rail whose width differs per kind, plus a distinct glyph. */
.twe-toast--ok     { border-inline-start: var(--twe-space-1) solid var(--twe-ok); }
.twe-toast--warn   { border-inline-start: var(--twe-space-1) solid var(--twe-warn); }
.twe-toast--danger { border-inline-start: var(--twe-space-2) solid var(--twe-danger); }
.twe-toast--info   { border-inline-start: var(--twe-space-1) solid var(--twe-info); }

.twe-toast--ok     .twe-toast__icon::before { content: '\2713'; }  /* check */
.twe-toast--warn   .twe-toast__icon::before { content: '\0021'; }  /* bang  */
.twe-toast--danger .twe-toast__icon::before { content: '\00D7'; }  /* cross */
.twe-toast--info   .twe-toast__icon::before { content: '\0069'; }  /* i     */

/* Sticky: errors do not expire. Auto-dismissing a failure means the one message
 * the user needed is the one they missed while looking at the field they were
 * typing in. Enforced in JS (skip the timer when this class is present); the
 * dotted rail is the visible promise that it will wait. */
.twe-toast--sticky {
  border-inline-start-style: double;
  box-shadow: var(--twe-elev-4);
}


/* ==========================================================================
 * 8. DESIGNED STATES
 * The loading, empty, error and offline states are four of the five screens a
 * user actually sees on a bad day. The audit found them undesigned fleet-wide:
 * a raw "Loading…" string in 10+ places, no skeletons anywhere, and empty lists
 * rendering as a blank div. All four are components here for that reason.
 * ========================================================================== */

/* --- Skeleton -------------------------------------------------------------
 * Draw the SHAPE OF THE CONTENT, not a spinner in a box. A skeleton that
 * matches the incoming layout means nothing jumps when data lands, which is
 * most of what makes a load feel fast. Always pair with aria-busy="true" on the
 * container and aria-hidden="true" on the blocks — a screen reader must not
 * read decorative bars. */
.twe-skeleton {
  --_twe-sk-w: 100%;
  display: block;
  width: var(--_twe-sk-w);
  height: var(--twe-space-4);
  border-radius: var(--twe-radius-xs);
  background-color: var(--twe-bg-inset);
  background-image: linear-gradient(
    90deg,
    transparent 0%,
    var(--twe-bg-card-hover) 50%,
    transparent 100%
  );
  background-repeat: no-repeat;
  background-size: 200% 100%;
  animation: twe-skeleton-sheen 1.4s var(--twe-ease-in-out) infinite;
}

@keyframes twe-skeleton-sheen {
  from { background-position: -100% 0; }
  to   { background-position:  100% 0; }
}

/* A frozen gradient reads as a rendering artefact, so reduced motion drops the
 * sheen image entirely and leaves a flat tinted block — still clearly a
 * placeholder, no movement. */
@media (prefers-reduced-motion: reduce) {
  .twe-skeleton {
    animation: none;
    background-image: none;
  }
}

.twe-skeleton--title  { height: var(--twe-space-6); --_twe-sk-w: 45%; border-radius: var(--twe-radius-sm); }
.twe-skeleton--text   { height: var(--twe-space-3); }
.twe-skeleton--last   { --_twe-sk-w: 62%; }   /* ragged final line = real text */
.twe-skeleton--circle { width: var(--twe-space-10); height: var(--twe-space-10); border-radius: var(--twe-radius-pill); }
.twe-skeleton--thumb  { height: var(--twe-space-16); border-radius: var(--twe-radius-md); }
.twe-skeleton--card   { height: var(--twe-space-16); border-radius: var(--twe-radius-md); }

.twe-skeleton-group {
  display: flex;
  flex-direction: column;
  gap: var(--twe-space-2);
}

/* --- Empty state ---------------------------------------------------------
 * An empty state must TEACH. A blank div is never acceptable: the user cannot
 * tell "no results" from "still loading" from "it broke", and support tickets
 * are the difference. Required parts: what this region is for, why it is empty,
 * and the one action that fills it. */
.twe-empty,
.twe-error-state,
.twe-offline-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--twe-space-3);
  padding: var(--twe-space-10) var(--twe-space-6);
  text-align: center;
  border: var(--twe-border-width) dashed var(--twe-border);
  border-radius: var(--twe-radius-lg);
  background: var(--twe-bg-inset);
  color: var(--twe-text-secondary);
}

.twe-empty__icon,
.twe-error-state__icon,
.twe-offline-state__icon {
  display: grid;
  place-items: center;
  width: var(--twe-space-12);
  height: var(--twe-space-12);
  border-radius: var(--twe-radius-pill);
  background: var(--twe-bg-card);
  border: var(--twe-border-width) solid var(--twe-border);
  box-shadow: var(--twe-elev-1);
  font-size: var(--twe-text-xl);
  color: var(--twe-text-muted);
}

/* Sentence case, not Title Case: a title-cased sentence reads as a product
 * label, and this is the app talking to a colleague. */
.twe-empty__title,
.twe-error-state__title,
.twe-offline-state__title {
  margin: 0;
  font-size: var(--twe-text-lg);
  line-height: var(--twe-leading-snug);
  color: var(--twe-text);
}

/* One sentence. If it takes two, the region is doing too much. */
.twe-empty__body,
.twe-error-state__body,
.twe-offline-state__body {
  margin: 0;
  max-width: 44ch;
  font-size: var(--twe-text-base);
  line-height: var(--twe-leading-body);
  color: var(--twe-text-secondary);
}

.twe-empty__actions,
.twe-error-state__actions,
.twe-offline-state__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--twe-space-2);
  justify-content: center;
  margin-block-start: var(--twe-space-1);
}

/* Technical detail (a request id, an exception class) belongs in the error
 * state and nowhere else — it is what turns "it broke" into a ticket someone
 * can act on. Mono because it will be copy-pasted. */
.twe-error-state__detail {
  max-width: 60ch;
  padding: var(--twe-space-2) var(--twe-space-3);
  border-radius: var(--twe-radius-sm);
  background: var(--twe-bg-card);
  border: var(--twe-border-width) solid var(--twe-border);
  font-family: var(--twe-font-mono);
  font-size: var(--twe-text-xs);
  font-variant-numeric: var(--twe-nums);
  color: var(--twe-text-muted);
  text-align: left;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Error and offline are dual-encoded: tinted ground + solid (not dashed) border
 * + hue + a glyph. Set role="alert" on the error state so it is announced. */
.twe-error-state {
  border-style: solid;
  border-color: var(--twe-danger);
  background: var(--twe-danger-bg);
}

.twe-error-state__icon {
  color: var(--twe-danger);
  border-color: var(--twe-danger);
}

.twe-error-state__icon::before { content: '\0021'; }

/* Offline is a WARNING, not a failure: the work is usually still recoverable,
 * and several of these tools run on tablets with intermittent LTE at a boring
 * site. Say what is queued and what will happen on reconnect. */
.twe-offline-state {
  border-style: solid;
  border-color: var(--twe-warn);
  background: var(--twe-warn-bg);
}

.twe-offline-state__icon {
  color: var(--twe-warn);
  border-color: var(--twe-warn);
}

.twe-offline-state__icon::before { content: '\26A0'; }

/* --- Spinner -------------------------------------------------------------
 * LAST RESORT, in this order:
 *   1. Optimistic update — show the result, reconcile after. Best.
 *   2. Skeleton — when the shape is known. Almost always the right answer.
 *   3. Inline progress on the control that was pressed.
 *   4. This, and only past ~400ms. Under 400ms a spinner is a flash of anxiety
 *      that makes a fast action feel broken; over ~10s it needs real progress
 *      or a cancel, because an indeterminate ring says nothing about whether the
 *      job is alive.
 * ALWAYS pair with visible text in an aria-live region: under reduced motion
 * tokens.css stops the animation, so a lone ring conveys nothing at all. */
.twe-spinner {
  display: inline-block;
  width: var(--twe-space-5);
  height: var(--twe-space-5);
  /* x2 so the ring survives 125%/150% Windows DPI without a hairline gap, and
   * scales to 4px in the field theme where --twe-border-width is 2px. */
  border: calc(var(--twe-border-width) * 2) solid var(--twe-border-strong);
  border-block-start-color: var(--twe-accent);
  border-radius: var(--twe-radius-pill);
  animation: twe-spin 750ms linear infinite;
  vertical-align: -0.15em;
}

@keyframes twe-spin {
  to { rotate: 360deg; }
}

.twe-spinner--sm { width: var(--twe-space-4); height: var(--twe-space-4); }
.twe-spinner--lg { width: var(--twe-space-8); height: var(--twe-space-8); }

.twe-loading-row {
  display: flex;
  align-items: center;
  gap: var(--twe-space-3);
  padding: var(--twe-space-6);
  justify-content: center;
  font-size: var(--twe-text-base);
  color: var(--twe-text-secondary);
}


/* ==========================================================================
 * 9. DIVIDER + SCRIM
 * ========================================================================== */

/* Hairline. Always exactly --twe-border-width (never 0.5px, never a scaled
 * transform): sub-pixel borders vanish at some Windows DPI factors and double
 * at others, which is why separation in this system leans on elevation. */
.twe-divider {
  height: var(--twe-border-width);
  margin: var(--twe-space-4) 0;
  border: 0;
  background: var(--twe-border);
}

.twe-divider--strong { background: var(--twe-border-strong); }
.twe-divider--light  { background: var(--twe-border-light); }
.twe-divider--flush  { margin: 0; }

.twe-divider--vertical {
  width: var(--twe-border-width);
  height: auto;
  align-self: stretch;
  margin: 0 var(--twe-space-3);
}

/* Labelled divider ("or", "12 older items"). The rules are ::before/::after on
 * a flex container so the label stays centred at any width. */
.twe-divider--labeled {
  display: flex;
  align-items: center;
  gap: var(--twe-space-3);
  height: auto;
  background: none;
  font-size: var(--twe-text-2xs);
  letter-spacing: var(--twe-tracking-eyebrow);
  text-transform: uppercase;
  color: var(--twe-text-muted);
}

.twe-divider--labeled::before,
.twe-divider--labeled::after {
  content: '';
  flex: 1;
  height: var(--twe-border-width);
  background: var(--twe-border);
}

/* Scrim: the manual overlay ground, for the two cases <dialog>::backdrop cannot
 * cover — a non-dialog overlay (a slide-over rail, a media lightbox) and the
 * Chromium-120 ::backdrop inheritance gap documented in §4.
 * It does NOT give focus trap, Escape or inert. If you are reaching for this to
 * build a modal, use .twe-dialog instead; a scrim plus a div is exactly the
 * hand-rolled modal this file exists to retire. Toggle with the hidden
 * attribute so it leaves the accessibility tree, not with opacity alone. */
.twe-scrim {
  position: fixed;
  inset: 0;
  z-index: var(--twe-z-overlay);
  background: var(--twe-bg-overlay);
  -webkit-backdrop-filter: blur(var(--twe-glass-blur));
  backdrop-filter: blur(var(--twe-glass-blur));
  opacity: 0;
  transition: opacity var(--twe-dur-enter) var(--twe-ease);
}

.twe-scrim[data-twe-open] { opacity: 1; }

.twe-scrim[hidden] { display: none; }


/* ==========================================================================
 * 10. NAV RAIL — pushes the canvas, collapses on leave
 *
 * HOUSE DEFAULT (Matt, 2026-10-08), for every tool built from the kit:
 *   1. Expanding the rail PUSHES the canvas narrower. It never overlays it.
 *      The rail owns a real grid track (`auto`), and its inline-size is what
 *      grows, so the canvas reflows. No position:absolute panel, no scrim.
 *   2. Hover opens it; leaving closes it after a short grace delay (rail.js,
 *      default 250ms, data-twe-rail-delay) so a slight overshoot is forgiven.
 *   3. A click on an item does not change that. The old rule opened on
 *      :focus-within, and a clicked link keeps focus, so the rail stayed open
 *      after navigating. Keyboard focus is :focus-visible, which a mouse click
 *      on a link never sets.
 *   4. PINNED (.twe-rail__pin, aria-pressed) and keyboard focus keep it open;
 *      hover-open is the only state that auto-collapses. Touch has no hover, so
 *      the pin is the touch route.
 *
 * Markup: see app-shell.html. Behaviour: rail.js (one script, no dependencies).
 * Without rail.js the rail still opens on :hover and keyboard focus (no grace
 * delay) — it degrades, it does not break.
 *
 * Host grid: `.twe-app { grid-template-columns: auto minmax(0, 1fr) ... }` with
 * `grid-area: rail` on .twe-rail. Widths are tunable per tool:
 *   --twe-rail-w (collapsed, default 3.5rem) · --twe-rail-w-open (default 13rem)
 * Reduced motion: tokens.css zeroes every transition duration.
 * ========================================================================== */

.twe-rail {
  grid-area: rail;
  position: relative;
  z-index: var(--twe-z-rail);
  inline-size: var(--twe-rail-w, 3.5rem);
  min-inline-size: 0;
  padding-inline-start: env(safe-area-inset-left, 0px);
  transition: inline-size var(--twe-dur) var(--twe-ease);
}

.twe-rail__panel {
  box-sizing: border-box;
  block-size: 100%;
  overflow: hidden auto;
  display: flex;
  flex-direction: column;
  gap: var(--twe-space-1);
  white-space: nowrap;
  padding: var(--twe-space-2);
  border-radius: 0;           /* twe-ui-lint: allow tell-5 -- rail is flush to the viewport edge */
  border-block: 0;
  border-inline-start: 0;
}

.twe-rail__brand,
.twe-rail__item,
.twe-rail__pin {
  display: flex;
  align-items: center;
  gap: var(--twe-space-3);
  min-block-size: var(--twe-tap-min);
  padding-inline: var(--twe-space-2);
  border-radius: var(--twe-radius-sm);
  text-decoration: none;
}

.twe-rail__brand { color: var(--twe-accent-text); }

.twe-rail__item,
.twe-rail__pin {
  color: var(--twe-text-secondary);
  background: transparent;
  border: 0;
  font: inherit;
  cursor: pointer;
  transition: background-color var(--twe-transition), color var(--twe-transition);
}

.twe-rail__item:hover,
.twe-rail__pin:hover { background: var(--twe-bg-card-hover); color: var(--twe-text); }

/* Triple-encoded: accent bar, tint and a real aria-current="page". */
.twe-rail__item[aria-current='page'] {
  background: var(--twe-accent-dim);
  color: var(--twe-accent-text);
  box-shadow: inset 3px 0 0 0 var(--twe-accent);
}

.twe-rail__pin { margin-block-start: auto; }
.twe-rail__pin[aria-pressed='true'] { color: var(--twe-accent-text); }

.twe-rail__brand > svg,
.twe-rail__item > svg,
.twe-rail__pin > svg { flex: 0 0 1rem; }

.twe-rail__label {
  opacity: 0;
  transition: opacity var(--twe-dur-fast) var(--twe-ease);
}

/* OPEN = hover-open (rail.js sets data-twe-open), pinned, or KEYBOARD focus. */
.twe-rail[data-twe-open],
.twe-rail[data-twe-pinned],
.twe-rail:has(:focus-visible) { inline-size: var(--twe-rail-w-open, 13rem); }

.twe-rail[data-twe-open] .twe-rail__label,
.twe-rail[data-twe-pinned] .twe-rail__label,
.twe-rail:has(:focus-visible) .twe-rail__label { opacity: 1; }

/* No-JS fallback only: once rail.js has run it sets data-twe-rail-ready and owns
   hover (with the grace delay), so this rule steps aside. */
.twe-rail:not([data-twe-rail-ready]):hover { inline-size: var(--twe-rail-w-open, 13rem); }
.twe-rail:not([data-twe-rail-ready]):hover .twe-rail__label { opacity: 1; }

/* A pin toggle is meaningless without the script. */
.twe-rail:not([data-twe-rail-ready]) .twe-rail__pin { display: none; }
