/* =============================================================================
 * TWE UI - controls.css
 * Requires: tokens.css (linked FIRST). Consumes tokens only; defines none.
 *
 *   CONTRACT: no control in a TWE tool may render OS chrome.
 *
 * Every native form control ships with an operating-system skin. Dropped into a
 * modern dark shell, that skin is the single most-reported defect in the fleet:
 * "the buttons don't match the aesthetics - you have the default buttons built
 * into this nice modern stack." Found in 27 of 39 tools, 21 at blocker
 * severity, reported independently by all nine audited clusters. So the rule
 * here is absolute: strip `appearance` and draw every affordance ourselves, in
 * CSS, with no image assets, no data URIs, no icon font and no JS.
 *
 * BASELINE = CHROMIUM 120 (Tatara, Electron 28). `appearance:none` is the real
 * cure and works there, so the mandatory layer is safe everywhere. Modern
 * niceties (`appearance:base-select`, `::picker()`, `corner-shape`) live in
 * @supports blocks ONLY - unguarded, they fall back to a native widget, i.e.
 * they reproduce the exact complaint this file exists to end.
 *
 * TWO TRAPS, both learned the hard way:
 *  1. `--twe-highlight` is `none` in the field theme, and `none` is illegal as a
 *     comma item in box-shadow. `box-shadow: var(--twe-elev-1), var(--twe-highlight)`
 *     therefore drops the ENTIRE declaration on field tablets. The lit top edge
 *     is applied on a ::before instead, where `none` is a legal sole value.
 *  2. Chromium suppresses hover/pointer events on a real `:disabled` control, so
 *     a "why is this off?" tooltip never appears. A disabled-with-explanation
 *     control uses `[aria-disabled="true"]` (styled identically below) plus JS
 *     that swallows activation - never the `disabled` attribute.
 * ========================================================================== */

/* ------------------------------------------------------------ 0. shared base -
 * One reset for everything interactive. `font: inherit` matters more than it
 * looks: a native control ignores the page font entirely, and Segoe UI inside
 * an Outfit layout reads as "two apps stapled together" even to people who
 * cannot name why. `accent-color` is belt-and-braces for any control a tool
 * adds that this file has not themed yet.
 */
.twe-btn,
.twe-input,
.twe-textarea,
.twe-select > select,
.twe-checkbox,
.twe-radio,
.twe-switch,
.twe-range,
.twe-file,
.twe-chip {
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
  margin: 0;
  font-family: var(--twe-font-sans);
  font-size: var(--twe-text-base);
  line-height: var(--twe-leading-snug);
  color: var(--twe-text);
  accent-color: var(--twe-accent);
  /* Kills the grey/blue OS flash on tap, and the 300ms double-tap-zoom wait on
   * the field tablets. Both are OS chrome by another name. */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* =================================================================== BUTTONS =
 * ONE hover voice fleet-wide: colour steps per variant, but the physical
 * behaviour - rise one elevation level on hover, press 1px down on active - is
 * shared here so eight tools cannot drift. That consistency IS what the client
 * means by "the buttons match the aesthetics".
 */
.twe-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--twe-space-2);
  position: relative;
  padding: var(--twe-space-2) var(--twe-space-4);
  min-height: 2.25rem;                 /* no control-height token exists; see gaps */
  border: var(--twe-border-width) solid transparent;
  border-radius: var(--twe-radius-sm);
  background-color: transparent;
  font-weight: 550;
  text-decoration: none;               /* <a class="twe-btn"> must match <button> */
  white-space: nowrap;
  user-select: none;
  cursor: pointer;
  box-shadow: var(--twe-elev-1);
  transition:
    background-color var(--twe-transition),
    border-color var(--twe-transition),
    color var(--twe-transition),
    box-shadow var(--twe-transition),
    transform var(--twe-dur-instant) var(--twe-ease-out);
}

/* The lit top edge. Separate pseudo, not a box-shadow comma item - see trap 1. */
.twe-btn--primary::before,
.twe-btn--danger::before,
.twe-segmented__label::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: var(--twe-highlight);
  pointer-events: none;
}

/* Shared hover/press voice. `:not(:disabled, [aria-disabled])` guards every
 * state rule so a disabled control never lights up under the cursor. */
.twe-btn:not(:disabled, [aria-disabled='true']):hover {
  box-shadow: var(--twe-elev-2);
}
.twe-btn:not(:disabled, [aria-disabled='true']):active {
  transform: translateY(1px);
  box-shadow: var(--twe-elev-1);
}

/* Focus ring uses outline, not box-shadow, because the elevation ramp already
 * owns box-shadow on buttons. Outline has followed border-radius since
 * Chromium 94, so it still hugs the corner at the floor. */
.twe-btn:focus-visible {
  outline: 2px solid var(--twe-accent);
  outline-offset: 2px;
}

.twe-btn:disabled,
.twe-btn[aria-disabled='true'] {
  opacity: 0.5;
  cursor: not-allowed;
  box-shadow: var(--twe-elev-0);
  /* pointer-events deliberately NOT removed - see trap 2. */
}

/* --- variants ------------------------------------------------------------- */
.twe-btn--primary {
  background-color: var(--twe-accent);
  border-color: var(--twe-accent-active);
  color: var(--twe-text-inverse);
}
.twe-btn--primary:not(:disabled, [aria-disabled='true']):hover {
  background-color: var(--twe-accent-hover);
}
.twe-btn--primary:not(:disabled, [aria-disabled='true']):active {
  background-color: var(--twe-accent-active);
}

.twe-btn--secondary {
  background-color: var(--twe-bg-card);
  border-color: var(--twe-border);
  color: var(--twe-text);
}
.twe-btn--secondary:not(:disabled, [aria-disabled='true']):hover {
  background-color: var(--twe-bg-card-hover);
  border-color: var(--twe-border-strong);
}

/* A ghost that casts a shadow is not a ghost: it opts out of the elevation
 * ramp at rest and rises INTO it on hover, which keeps the shared voice
 * (hover = one level up) while staying visually weightless in a toolbar. */
.twe-btn--ghost {
  background-color: transparent;
  border-color: transparent;
  color: var(--twe-text-secondary);
  box-shadow: var(--twe-elev-0);
}
.twe-btn--ghost:not(:disabled, [aria-disabled='true']):hover {
  background-color: var(--twe-bg-card-hover);
  border-color: var(--twe-border);
  color: var(--twe-text);
  box-shadow: var(--twe-elev-1);
}

/* --twe-danger over white is 3.8:1 - short of AA for normal text. Mixing the
 * fill one step toward --twe-text fixes it in BOTH themes at once, because
 * --twe-text is near-black on light and near-white on dark: the fill darkens
 * where it must and brightens where it must, from tokens only, no new hex.
 * color-mix() is Chromium 111, comfortably under the 120 floor. */
.twe-btn--danger {
  background-color: color-mix(in srgb, var(--twe-danger) 86%, var(--twe-text));
  border-color: color-mix(in srgb, var(--twe-danger) 60%, var(--twe-text));
  color: var(--twe-text-inverse);
}
.twe-btn--danger:not(:disabled, [aria-disabled='true']):hover {
  background-color: color-mix(in srgb, var(--twe-danger) 76%, var(--twe-text));
}
.twe-btn--danger:not(:disabled, [aria-disabled='true']):active {
  background-color: color-mix(in srgb, var(--twe-danger) 68%, var(--twe-text));
}

.twe-btn--subtle {
  background-color: var(--twe-bg-inset);
  border-color: var(--twe-border-light);
  color: var(--twe-text-secondary);
}
.twe-btn--subtle:not(:disabled, [aria-disabled='true']):hover {
  background-color: var(--twe-bg-card-hover);
  color: var(--twe-text);
}

/* --- sizes --------------------------------------------------------------- */
.twe-btn--sm {
  padding: var(--twe-space-1) var(--twe-space-3); min-height: 1.75rem;
  font-size: var(--twe-text-xs); border-radius: var(--twe-radius-xs);
}
.twe-btn--lg {
  padding: var(--twe-space-3) var(--twe-space-6); min-height: 2.75rem;
  font-size: var(--twe-text-md); border-radius: var(--twe-radius);
}
/* An icon button has no label to widen its hit area, so it carries the tap
 * floor by default. `.twe-btn--icon.twe-btn--sm` drops below it for dense
 * desktop toolbars only; the field-theme block at the bottom puts it back. */
.twe-btn--icon {
  padding: 0; aspect-ratio: 1;
  min-inline-size: var(--twe-tap-min); min-block-size: var(--twe-tap-min);
}
.twe-btn--icon.twe-btn--sm { min-inline-size: 2rem; min-block-size: 2rem; }
.twe-btn--block { display: flex; width: 100%; }

/* --- busy ---------------------------------------------------------------- */
/* prefers-reduced-motion freezes this ring (tokens.css zeroes animations), so
 * a busy button must ALSO change its accessible name or update an aria-live
 * region - a spinner is never the only signal. */
.twe-btn[aria-busy='true'] {
  cursor: progress;
}
.twe-btn[aria-busy='true']::after {
  content: ''; width: 1em; height: 1em;
  border: 2px solid currentColor; border-top-color: transparent;
  border-radius: var(--twe-radius-pill);
  opacity: 0.8;
  animation: twe-btn-spin 700ms linear infinite;
}
@keyframes twe-btn-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .twe-btn[aria-busy='true']::after {
    animation: none;
    border-top-color: currentColor;
    opacity: 0.45;
  }
}

/* ============================================================ TEXT INPUTS ====
 * Fields read as recessed (inner shadow) while buttons read as raised - that
 * push/pull is most of what makes a form look designed rather than drawn. The
 * inset shadow is built from --twe-shadow-rgb/--twe-shadow-alpha, the same
 * construction the elevation ramp uses, so it follows the theme.
 */
.twe-input,
.twe-textarea {
  display: block;
  width: 100%;
  padding: var(--twe-space-2) var(--twe-space-3);
  min-height: 2.25rem;
  background-color: var(--twe-bg-input);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-sm);
  box-shadow: inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.10 * var(--twe-shadow-alpha)));
  transition:
    border-color var(--twe-transition),
    box-shadow var(--twe-transition),
    background-color var(--twe-transition);
}
.twe-textarea { min-height: 5rem; line-height: var(--twe-leading-body); resize: vertical; }
.twe-input::placeholder,
.twe-textarea::placeholder {
  color: var(--twe-text-muted);
}
.twe-input:hover:not(:disabled, [readonly]),
.twe-textarea:hover:not(:disabled, [readonly]) {
  border-color: var(--twe-border-strong);
}
/* Ring via box-shadow, not outline: it hugs the radius exactly and can be
 * softened, which an outline cannot. */
.twe-input:focus-visible,
.twe-textarea:focus-visible,
.twe-input:focus,
.twe-textarea:focus {
  outline: none;
  border-color: var(--twe-accent);
  box-shadow: 0 0 0 3px var(--twe-accent-ring),
              inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.10 * var(--twe-shadow-alpha)));
}
.twe-input:disabled,
.twe-textarea:disabled {
  opacity: 0.55; cursor: not-allowed; background-color: var(--twe-bg-inset);
}
.twe-input[readonly],
.twe-textarea[readonly] {
  background-color: var(--twe-bg-inset); color: var(--twe-text-secondary);
}
/* Invalidity is never colour alone: the red border pairs with the helper text
 * below, which is what a screen reader and a colourblind user actually get. */
.twe-input[aria-invalid='true'],
.twe-textarea[aria-invalid='true'] {
  border-color: var(--twe-danger);
}
.twe-input[aria-invalid='true']:focus,
.twe-textarea[aria-invalid='true']:focus {
  box-shadow: 0 0 0 3px var(--twe-danger-bg);
}

/* Outfit has no true tabular figures, so numbers must not be set in it - a
 * column of misaligned digits is a legacy tell in its own right. */
.twe-input--num {
  font-family: var(--twe-font-mono); font-variant-numeric: var(--twe-nums);
  text-align: right;
}
/* The number spinner and the date picker glyph are pure OS chrome. The glyph
 * is kept (there is no CSS-only replacement for the picker trigger) but
 * re-housed; `color-scheme` in tokens.css already flips it light-on-dark. */
.twe-input::-webkit-outer-spin-button,
.twe-input::-webkit-inner-spin-button {
  -webkit-appearance: none; appearance: none; margin: 0;
}
.twe-input::-webkit-calendar-picker-indicator {
  padding: var(--twe-space-1);
  border-radius: var(--twe-radius-xs);
  cursor: pointer;
  opacity: 0.6;
  transition: opacity var(--twe-transition), background-color var(--twe-transition);
}
.twe-input::-webkit-calendar-picker-indicator:hover {
  opacity: 1; background-color: var(--twe-bg-card-hover);
}

@supports (field-sizing: content) {
  /* Grows with typing instead of stranding a scrollbar inside a 3-row box.
   * Enhancement only (Chromium 123+); max-height keeps it from eating a form. */
  .twe-textarea--auto { field-sizing: content; max-height: 18rem; }
}

/* --- field wrapper ------------------------------------------------------- */
/* :focus-within lets the label and helper text answer the focus too, so the
 * whole field lights as one object rather than just the box. */
.twe-field { display: flex; flex-direction: column; gap: var(--twe-space-1); }
.twe-field__label {
  font-size: var(--twe-text-xs); font-weight: 550;
  color: var(--twe-text-secondary);
  transition: color var(--twe-transition);
}
.twe-field:focus-within .twe-field__label { color: var(--twe-accent-text); }
.twe-field__help {
  font-size: var(--twe-text-xs); color: var(--twe-text-muted);
  line-height: var(--twe-leading-snug);
}
/* Pair with aria-describedby on the control and role="alert" if it appears
 * after submit; the icon carries the meaning for anyone who cannot see red. */
.twe-field__error {
  display: flex; align-items: center; gap: var(--twe-space-1);
  font-size: var(--twe-text-xs); color: var(--twe-danger);
}
.twe-field__error::before { content: '\26A0'; }

/* ================================================================= SELECT ====
 * The worst offender in the fleet. The BASELINE below must look finished on its
 * own: six Electron tools (Chromium 120-128) will NEVER see the base-select
 * enhancement, so the closed control is fully drawn here and the chevron is a
 * ::after on the wrapper - no asset, no data URI, nothing to 404 offline.
 */
.twe-select { position: relative; display: inline-grid; align-items: center; width: 100%; }
.twe-select > select {
  grid-area: 1 / 1;
  width: 100%;
  padding: var(--twe-space-2) var(--twe-space-8) var(--twe-space-2) var(--twe-space-3);
  min-height: 2.25rem;
  background-color: var(--twe-bg-input);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-sm);
  box-shadow: inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.10 * var(--twe-shadow-alpha)));
  cursor: pointer;
  transition: border-color var(--twe-transition), box-shadow var(--twe-transition);
}
.twe-select > select:hover:not(:disabled) { border-color: var(--twe-border-strong); }
.twe-select > select:focus-visible,
.twe-select > select:focus {
  outline: none;
  border-color: var(--twe-accent);
  box-shadow: 0 0 0 3px var(--twe-accent-ring);
}
.twe-select > select:disabled { opacity: 0.55; cursor: not-allowed; }
.twe-select::after {                    /* the chevron: two borders, rotated */
  content: ''; grid-area: 1 / 1; justify-self: end;
  width: 0.45em; height: 0.45em;
  margin-right: var(--twe-space-4); margin-top: -0.2em;
  border-right: 2px solid var(--twe-text-secondary);
  border-bottom: 2px solid var(--twe-text-secondary);
  transform: rotate(45deg);
  pointer-events: none;                /* clicks must reach the <select> */
  transition: border-color var(--twe-transition);
}
.twe-select:focus-within::after { border-color: var(--twe-accent); }
/* :has() is Chromium 105 - below the 120 floor, so this is baseline, not an
 * enhancement. Without :has (non-Chromium embeds) the chevron simply overlaps
 * a list box; add .twe-select--multiple by hand there. */
.twe-select:has(select[multiple])::after,
.twe-select--multiple::after { display: none; }
.twe-select > select[multiple] { padding-right: var(--twe-space-3); min-height: 6rem; }

@supports (appearance: base-select) {
  /* Chromium 135+. This is the first time the OPEN dropdown can be themed at
   * all; before it, the popup was drawn by the OS and untouchable. Everything
   * here is additive - remove the block and the baseline above still stands. */
  .twe-select > select { appearance: base-select; }
  .twe-select::after { display: none; }   /* base-select draws ::picker-icon */

  .twe-select > select::picker-icon {
    color: var(--twe-text-secondary);
    transition: transform var(--twe-transition), color var(--twe-transition);
  }
  .twe-select > select:open::picker-icon { transform: rotate(180deg); }

  .twe-select > select::picker(select) {
    padding: var(--twe-space-1);
    background-color: var(--twe-glass-bg);
    -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));
    border: var(--twe-border-width) solid var(--twe-glass-border);
    border-radius: var(--twe-radius-md);
    box-shadow: var(--twe-elev-3);
    z-index: var(--twe-z-dropdown);
    opacity: 1;
    translate: 0 0;
    transition: opacity var(--twe-dur-fast) var(--twe-ease),
                translate var(--twe-dur-fast) var(--twe-ease),
                overlay var(--twe-dur-fast) allow-discrete,
                display var(--twe-dur-fast) allow-discrete;
  }
  /* @starting-style is Chromium 117 - safe even at the floor, but it only has
   * anything to animate inside this block. */
  @starting-style {
    .twe-select > select::picker(select) { opacity: 0; translate: 0 -0.35rem; }
  }
  .twe-select > select option {
    padding: var(--twe-space-2) var(--twe-space-3);
    border-radius: var(--twe-radius-xs);
    color: var(--twe-text); background-color: transparent;
  }
  .twe-select > select option:hover,
  .twe-select > select option:focus { background-color: var(--twe-bg-card-hover); }
  /* Selected = tinted fill AND the built-in checkmark, never colour alone. */
  .twe-select > select option:checked {
    background-color: var(--twe-accent-dim);
    color: var(--twe-accent-text); font-weight: 550;
  }
  .twe-select > select option::checkmark { color: var(--twe-accent); }
}

/* ==================================================== CHECKBOX AND RADIO =====
 * The real <input> stays in the DOM. The hidden-input-plus-styled-div pattern
 * that circulates on the web loses keyboard activation, the indeterminate
 * state and the screen-reader role - it trades one defect for three.
 */
.twe-checkbox,
.twe-radio {
  position: relative; flex: none;
  display: inline-grid; place-content: center;
  width: 1.125rem; height: 1.125rem;
  background-color: var(--twe-bg-input);
  border: var(--twe-border-width) solid var(--twe-border-strong);
  box-shadow: inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.10 * var(--twe-shadow-alpha)));
  cursor: pointer;
  transition: background-color var(--twe-dur-fast) var(--twe-ease),
              border-color var(--twe-dur-fast) var(--twe-ease),
              box-shadow var(--twe-transition);
}
.twe-checkbox { border-radius: var(--twe-radius-xs); }
.twe-radio    { border-radius: var(--twe-radius-pill); }
.twe-checkbox:hover:not(:disabled),
.twe-radio:hover:not(:disabled) { border-color: var(--twe-accent); }
.twe-checkbox:checked,
.twe-radio:checked,
.twe-checkbox:indeterminate {
  background-color: var(--twe-accent); border-color: var(--twe-accent-active);
}
.twe-checkbox:focus-visible,
.twe-radio:focus-visible { outline: 2px solid var(--twe-accent); outline-offset: 2px; }
.twe-checkbox:disabled,
.twe-radio:disabled { opacity: 0.5; cursor: not-allowed; }

/* The tick is two borders of an empty box, rotated - no glyph, no asset, and
 * it scales with font-size. Chromium renders pseudo-elements on a checkbox or
 * radio once appearance is none; if a future engine disagrees, the accent fill
 * still states the state unambiguously. */
.twe-checkbox::after {
  content: ''; width: 0.55em; height: 0.3em;
  border-left: 2px solid var(--twe-text-inverse);
  border-bottom: 2px solid var(--twe-text-inverse);
  transform: rotate(-45deg) translateY(-0.06em);
  opacity: 0;
  transition: opacity var(--twe-dur-instant) var(--twe-ease);
}
.twe-checkbox:checked::after { opacity: 1; }
/* Indeterminate is a bar, not a tick - a different SHAPE, so "some selected"
 * is distinguishable from "all selected" without relying on colour. */
.twe-checkbox:indeterminate::after {
  width: 0.55em; height: 0; opacity: 1;
  border-left: none; border-bottom: 2px solid var(--twe-text-inverse);
  transform: none;
}
.twe-radio::after {
  content: ''; width: 0.4em; height: 0.4em;
  border-radius: var(--twe-radius-pill);
  background-color: var(--twe-text-inverse);
  transform: scale(0);
  transition: transform var(--twe-dur-fast) var(--twe-ease);
}
.twe-radio:checked::after { transform: scale(1); }

/* Label row. This is what carries the tap target for the small box inside it. */
.twe-check {
  display: inline-flex; align-items: center; gap: var(--twe-space-2);
  min-height: var(--twe-tap-min);
  font-size: var(--twe-text-base);
  cursor: pointer;
}
.twe-check:has(:disabled) { cursor: not-allowed; color: var(--twe-text-muted); }

/* ================================================================= SWITCH ====
 * A real checkbox, restyled. Needs role="switch" + aria-checked mirrored by the
 * tool for the correct screen-reader announcement; state is carried by knob
 * POSITION as well as colour, so it survives greyscale.
 */
.twe-switch {
  position: relative; flex: none;
  width: 2.5rem; height: 1.5rem;
  background-color: var(--twe-bg-inset);
  border: var(--twe-border-width) solid var(--twe-border-strong);
  border-radius: var(--twe-radius-pill);
  box-shadow: inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.14 * var(--twe-shadow-alpha)));
  cursor: pointer;
  transition: background-color var(--twe-transition), border-color var(--twe-transition);
}
.twe-switch::after {
  content: ''; position: absolute; top: 50%; left: 0.1875rem;
  width: 0.875rem; height: 0.875rem;
  border-radius: var(--twe-radius-pill);
  /* Knob colour is chosen so it stays visible in all three themes: grey on the
   * trough when off, inverse-text on the accent fill when on. */
  background-color: var(--twe-text-secondary);
  box-shadow: var(--twe-elev-1);
  transform: translateY(-50%);
  transition: transform var(--twe-transition), background-color var(--twe-transition);
}
.twe-switch:checked {
  background-color: var(--twe-accent); border-color: var(--twe-accent-active);
}
.twe-switch:checked::after {
  background-color: var(--twe-text-inverse);
  transform: translateY(-50%) translateX(1rem);
}
.twe-switch:focus-visible { outline: 2px solid var(--twe-accent); outline-offset: 2px; }
.twe-switch:disabled { opacity: 0.5; cursor: not-allowed; }

/* ============================================================== SEGMENTED ====
 * Replaces short <select>s and bare radio groups, and is one of the highest
 * impact "looks designed" primitives in the kit: all options visible, one tap,
 * no popup. Built from real radios so keyboard arrow navigation is free.
 * :has() is Chromium 105 (under the floor). If a target lacks it, have the tool
 * add .is-active to the checked item's label - the rule below accepts both.
 */
.twe-segmented {
  display: inline-flex; gap: var(--twe-space-1);
  padding: var(--twe-space-1);
  background-color: var(--twe-bg-inset);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-md);
  box-shadow: inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.12 * var(--twe-shadow-alpha)));
  overflow: hidden;                    /* parent clips the pills' corners */
}
.twe-segmented__item { position: relative; display: inline-flex; }
.twe-segmented__item > input {
  position: absolute; inset: 0; width: 100%; height: 100%; margin: 0;
  opacity: 0;                          /* present and focusable, just not drawn */
  cursor: pointer;
}
.twe-segmented__label {
  position: relative;
  display: inline-flex; align-items: center; gap: var(--twe-space-1);
  padding: var(--twe-space-1) var(--twe-space-4);
  min-height: 1.75rem;
  border-radius: var(--twe-radius-sm);  /* md parent - space-1 padding = sm */
  color: var(--twe-text-secondary);
  font-size: var(--twe-text-sm); font-weight: 550;
  white-space: nowrap;
  transition: background-color var(--twe-transition), color var(--twe-transition),
              box-shadow var(--twe-transition);
}
.twe-segmented__item:hover .twe-segmented__label { color: var(--twe-text); }
.twe-segmented__item:has(input:checked) .twe-segmented__label,
.twe-segmented__label.is-active {
  background-color: var(--twe-bg-card); color: var(--twe-text);
  box-shadow: var(--twe-elev-1);
}
.twe-segmented__item:has(input:focus-visible) .twe-segmented__label {
  outline: 2px solid var(--twe-accent); outline-offset: 1px;
}
.twe-segmented__item:has(input:disabled) .twe-segmented__label {
  opacity: 0.5; cursor: not-allowed;
}

/* ================================================================== RANGE ====
 * The vendor pseudo-elements are unavoidable here; there is no standard way to
 * style a slider track yet. The -moz- pair is included so the file is portable
 * even though the fleet is 100% Chromium.
 */
.twe-range { width: 100%; height: 1.25rem; background: transparent; cursor: pointer; }
.twe-range::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: var(--twe-radius-pill);
  /* Fill-left-of-thumb. --range-fill is a component-local variable, NOT a
   * token: one line of JS sets it (`el.style.setProperty('--range-fill',
   * pct + '%')`). With no JS it stays 0% and degrades to a plain trough - the
   * slider still works, it just does not colour its travelled distance. */
  background:
    linear-gradient(to right,
      var(--twe-accent) 0 var(--range-fill, 0%),
      var(--twe-bg-inset) var(--range-fill, 0%) 100%);
  box-shadow: inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.16 * var(--twe-shadow-alpha)));
}
.twe-range::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 1.125rem; height: 1.125rem;
  margin-top: -6px;                    /* centres an 18px thumb on a 6px track */
  border: var(--twe-border-width) solid var(--twe-accent-active);
  border-radius: var(--twe-radius-pill);
  background-color: var(--twe-accent);
  box-shadow: var(--twe-elev-1);
  transition: box-shadow var(--twe-transition), transform var(--twe-dur-fast) var(--twe-ease);
}
.twe-range:hover::-webkit-slider-thumb { box-shadow: var(--twe-elev-2); }
.twe-range:active::-webkit-slider-thumb { transform: scale(1.1); }
.twe-range:focus-visible { outline: none; }
.twe-range:focus-visible::-webkit-slider-thumb { box-shadow: var(--twe-elev-glow); }
.twe-range:disabled { opacity: 0.5; cursor: not-allowed; }
/* -moz- pair kept for portability; ::-moz-range-progress gives Firefox the
 * fill effect natively, with no JS and no --range-fill at all. */
.twe-range::-moz-range-track {
  height: 6px; border-radius: var(--twe-radius-pill);
  background-color: var(--twe-bg-inset);
}
.twe-range::-moz-range-progress {
  height: 6px; border-radius: var(--twe-radius-pill);
  background-color: var(--twe-accent);
}
.twe-range::-moz-range-thumb {
  width: 1.125rem; height: 1.125rem;
  border: var(--twe-border-width) solid var(--twe-accent-active);
  border-radius: var(--twe-radius-pill);
  background-color: var(--twe-accent);
}

/* ============================================================ FILE / DROP ====
 * ::file-selector-button cannot take a class, so .twe-btn--secondary is
 * duplicated here by necessity. Keep the two in sync - it is the one place in
 * this file where the same look is written twice.
 */
.twe-file { width: 100%; font-size: var(--twe-text-sm); color: var(--twe-text-secondary); }
.twe-file::file-selector-button {
  margin-right: var(--twe-space-3);
  padding: var(--twe-space-2) var(--twe-space-4);
  min-height: 2.25rem;
  font-family: var(--twe-font-sans); font-size: var(--twe-text-base); font-weight: 550;
  color: var(--twe-text);
  background-color: var(--twe-bg-card);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-sm);
  box-shadow: var(--twe-elev-1);
  cursor: pointer;
  transition: background-color var(--twe-transition), border-color var(--twe-transition),
              box-shadow var(--twe-transition);
}
.twe-file::file-selector-button:hover {
  background-color: var(--twe-bg-card-hover);
  border-color: var(--twe-border-strong);
  box-shadow: var(--twe-elev-2);
}
.twe-file:focus-visible::file-selector-button {
  outline: 2px solid var(--twe-accent); outline-offset: 2px;
}

/* Drop zone: the input is stretched invisibly over the whole area, so it stays
 * keyboard-focusable and clickable without any JS at all. [data-dragover] is
 * the only JS-dependent part (supplied by dropzone.js); without it
 * the zone still works as click-to-browse. */
.twe-dropzone {
  position: relative;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: var(--twe-space-2);
  padding: var(--twe-space-8) var(--twe-space-5);
  text-align: center;
  color: var(--twe-text-secondary);
  background-color: var(--twe-bg-inset);
  border: 2px dashed var(--twe-border-strong);   /* 2px, never 1.5 - see DPI rule */
  border-radius: var(--twe-radius-lg);
  transition: border-color var(--twe-transition), background-color var(--twe-transition),
              box-shadow var(--twe-transition);
}
.twe-dropzone input[type='file'] {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; cursor: pointer;
}
.twe-dropzone:hover,
.twe-dropzone[data-dragover] {
  border-color: var(--twe-accent); background-color: var(--twe-accent-dim);
  color: var(--twe-text);
}
.twe-dropzone[data-dragover] { box-shadow: var(--twe-elev-2); }
.twe-dropzone:focus-within {
  border-color: var(--twe-accent); box-shadow: 0 0 0 3px var(--twe-accent-ring);
}
/* dropzone.js parts. __status is the picked filename (aria-live); __note is the
 * data-twe-note line. Both sit under the invisible input, so they stay readable
 * but never intercept the click. [data-invalid] pairs the colour with the
 * status text ("Not accepted: ...") - never colour alone. */
.twe-dropzone__status { min-height: 1lh; font-size: var(--twe-text-sm); color: var(--twe-text); word-break: break-all; }
.twe-dropzone__note { font-size: var(--twe-text-2xs); color: var(--twe-text-muted); }
.twe-dropzone[data-has-file] { border-style: solid; }
/* data-twe-clearable: the button sits above the stretched input so it takes its own clicks */
.twe-dropzone__clear { position: relative; z-index: 1; }
.twe-dropzone[data-invalid] { border-color: var(--twe-danger); background-color: var(--twe-danger-bg); }
.twe-dropzone[data-invalid] .twe-dropzone__status { color: var(--twe-danger-ink); }

/* =========================================================== CHIPS / TAGS ====
 * .twe-chip--selected is paired with a check glyph and heavier weight, and the
 * element must carry aria-pressed or aria-selected: the tint alone would be
 * colour-only encoding, which Section 508 exposure makes a non-starter.
 */
.twe-chip {
  display: inline-flex; align-items: center; gap: var(--twe-space-1);
  padding: var(--twe-space-1) var(--twe-space-3);
  background-color: var(--twe-bg-inset);
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-pill);
  font-size: var(--twe-text-xs);
  color: var(--twe-text-secondary);
  white-space: nowrap;
  transition: background-color var(--twe-transition), border-color var(--twe-transition),
              color var(--twe-transition);
}
.twe-tag { font-family: var(--twe-font-mono); font-size: var(--twe-text-2xs); }
button.twe-chip, .twe-chip[role='button'] { cursor: pointer; }
button.twe-chip:hover, .twe-chip[role='button']:hover {
  border-color: var(--twe-border-strong);
  color: var(--twe-text);
}
.twe-chip--selected {
  background-color: var(--twe-accent-dim);
  border-color: var(--twe-accent-ring);
  color: var(--twe-accent-text);
  font-weight: 550;                    /* weight + glyph, so not colour alone */
}
.twe-chip--selected::before { content: '\2713'; }
.twe-chip:focus-visible { outline: 2px solid var(--twe-accent); outline-offset: 2px; }

/* Dismiss X drawn as two rotated bars - crisper than a glyph at any DPI, and
 * it needs a real aria-label ("Remove <tag>") on the button. */
.twe-chip__dismiss {
  position: relative; flex: none;
  width: 1rem; height: 1rem;
  margin-right: calc(var(--twe-space-1) * -1);
  padding: 0;
  background: transparent; border: none;
  border-radius: var(--twe-radius-pill);
  cursor: pointer;
  transition: background-color var(--twe-transition);
}
.twe-chip__dismiss:hover { background-color: var(--twe-bg-card-hover); }
.twe-chip__dismiss:focus-visible { outline: 2px solid var(--twe-accent); outline-offset: 1px; }
.twe-chip__dismiss::before,
.twe-chip__dismiss::after {
  content: ''; position: absolute; top: 50%; left: 50%;
  width: 0.5rem; height: 2px;
  margin: -1px 0 0 -0.25rem;
  background-color: currentColor;
  border-radius: var(--twe-radius-pill);
}
.twe-chip__dismiss::before { transform: rotate(45deg); }
.twe-chip__dismiss::after  { transform: rotate(-45deg); }

/* ================================================================= SEARCH ====
 * Magnifier is a bordered circle plus a rotated bar; the native cancel button
 * is repainted as a themed X built from two gradients, because the grey default
 * X is the most visible OS-chrome leak on any search field.
 */
.twe-search { position: relative; display: flex; align-items: center; width: 100%; }
.twe-search .twe-input {
  padding-left: calc(var(--twe-space-4) + var(--twe-space-4));
}
.twe-search__icon {
  position: absolute; left: var(--twe-space-3);
  width: 0.75rem; height: 0.75rem;
  border: 2px solid var(--twe-text-muted);
  border-radius: var(--twe-radius-pill);
  pointer-events: none;                /* clicks must reach the input */
  transition: border-color var(--twe-transition);
}
.twe-search__icon::after {              /* the handle */
  content: ''; position: absolute; top: 0.55rem; left: 0.5rem;
  width: 0.4rem; height: 2px;
  background-color: var(--twe-text-muted);
  border-radius: var(--twe-radius-pill);
  transform: rotate(45deg);
  transition: background-color var(--twe-transition);
}
.twe-search:focus-within .twe-search__icon { border-color: var(--twe-accent); }
.twe-search:focus-within .twe-search__icon::after { background-color: var(--twe-accent); }
.twe-search .twe-input[type='search']::-webkit-search-cancel-button {
  -webkit-appearance: none; appearance: none;
  width: 0.875rem; height: 0.875rem;
  border-radius: var(--twe-radius-pill);
  cursor: pointer;
  opacity: 0.55;
  background:
    linear-gradient(45deg,  transparent 45%, currentColor 45% 55%, transparent 55%),
    linear-gradient(-45deg, transparent 45%, currentColor 45% 55%, transparent 55%);
  transition: opacity var(--twe-transition), background-color var(--twe-transition);
}
.twe-search .twe-input[type='search']::-webkit-search-cancel-button:hover { opacity: 1; }

/* ============================================================ FIELD THEME ====
 * Tablets, gloves, direct sunlight. Every control is lifted to the tap floor
 * here rather than everywhere, because a 48px row is correct in a boring pit
 * and wrong in a 40-column desktop table.
 */
[data-twe-theme='field'] .twe-btn,
[data-twe-theme='field'] .twe-btn--sm,
[data-twe-theme='field'] .twe-btn--icon.twe-btn--sm,
[data-twe-theme='field'] .twe-input,
[data-twe-theme='field'] .twe-textarea,
[data-twe-theme='field'] .twe-select > select,
[data-twe-theme='field'] .twe-file::file-selector-button,
[data-twe-theme='field'] .twe-segmented__label {
  min-height: var(--twe-tap-min);
  min-inline-size: auto;
  font-size: var(--twe-text-base);
}
[data-twe-theme='field'] .twe-btn--icon {
  min-inline-size: var(--twe-tap-min);
  min-block-size: var(--twe-tap-min);
}
[data-twe-theme='field'] .twe-checkbox,
[data-twe-theme='field'] .twe-radio { width: 1.5rem; height: 1.5rem; }
[data-twe-theme='field'] .twe-switch { width: 3.25rem; height: 1.875rem; }
[data-twe-theme='field'] .twe-switch::after { width: 1.25rem; height: 1.25rem; }
[data-twe-theme='field'] .twe-switch:checked::after { transform: translateY(-50%) translateX(1.25rem); }
[data-twe-theme='field'] .twe-range::-webkit-slider-thumb { width: 1.75rem; height: 1.75rem; margin-top: -11px; }

/* ================================================= PROGRESSIVE ENHANCEMENT ===
 * Chromium 139+. A superellipse is the difference between "rounded rectangle"
 * and the squircle the client recognises as Apple-studio work. Purely cosmetic
 * and fully guarded: below 139 the radius tokens already stand on their own.
 */
@supports (corner-shape: superellipse(1.5)) {
  .twe-btn,
  .twe-input,
  .twe-textarea,
  .twe-select > select,
  .twe-checkbox,
  .twe-segmented,
  .twe-segmented__label,
  .twe-dropzone {
    corner-shape: superellipse(1.6);
  }
}

/* ==================================================== 99. THE SAFETY NET =====
 * Everything above is opt-in: it applies only when a class is present. That
 * leaves a silent failure mode, and it is not hypothetical — the first person
 * to use this file (the author of the kit, with the whole system in front of
 * them) wrote `<select class="twe-select">` instead of wrapping the element,
 * no rule matched, and the control rendered full native Windows chrome. The
 * page looked finished everywhere else, so nothing announced the mistake.
 *
 * A future agent will make exactly that error. So the DEFAULT must be safe:
 * a bare or mis-classed control still gets de-nativised, just with plainer
 * styling than the full component. The house rule is "no control in a TWE tool
 * may render OS chrome" — a rule that depends on remembering a class is a rule
 * that will be broken.
 *
 * SPECIFICITY — the subtle part, and the part this file got WRONG on first write.
 * The original net was `input[type='checkbox']:not([data-twe-native])`, which is
 * (0,1,1): `:not()` inherits the specificity of its argument, so the attribute
 * selector inside it counts. Every classed control above — .twe-switch,
 * .twe-checkbox, .twe-radio, .twe-range — is only (0,1,0). The net therefore
 * BEAT them all, and a .twe-switch silently rendered as an 18px checkbox with
 * its knob sitting on the first letter of the label.
 *
 * That was found by a fresh agent building a real tool from this kit, not by
 * review — the bug is invisible in source and obvious on screen.
 *
 * The fix is to narrow WHAT MATCHES rather than to fight over specificity:
 * `:not([class*='twe-'])` makes the net apply only to controls carrying no kit
 * class at all, and `:not(.twe-select > select)` releases the wrapper's own
 * select. Raising specificity or reaching for !important would have "worked"
 * and left the classed components permanently shadowed.
 *
 * Escape hatch: add data-twe-native to opt a control out on purpose. There is
 * almost never a good reason; if you use it, comment why.
 */

select:not([data-twe-native]):not([class*='twe-']):not(.twe-select > select),
input[type='checkbox']:not([data-twe-native]):not([class*='twe-']),
input[type='radio']:not([data-twe-native]):not([class*='twe-']),
input[type='range']:not([data-twe-native]):not([class*='twe-']) {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  color: var(--twe-text);
  accent-color: var(--twe-accent);
  -webkit-tap-highlight-color: transparent;
}

/* Bare <select>. A select element cannot carry a reliable ::after, so the
 * chevron is drawn with two gradients — no asset, no data URI, and it recolours
 * with the theme because it reads a token. */
select:not([data-twe-native]):not([class*='twe-']):not(.twe-select > select) {
  width: 100%;
  min-height: 2.25rem;
  padding: var(--twe-space-2) var(--twe-space-8) var(--twe-space-2) var(--twe-space-3);
  background-color: var(--twe-bg-input);
  background-image:
    linear-gradient(45deg,  transparent 50%, var(--twe-text-secondary) 50%),
    linear-gradient(135deg, var(--twe-text-secondary) 50%, transparent 50%);
  background-position: calc(100% - 1.15rem) center, calc(100% - 0.8rem) center;
  background-size: 0.35rem 0.35rem, 0.35rem 0.35rem;
  background-repeat: no-repeat;
  border: var(--twe-border-width) solid var(--twe-border);
  border-radius: var(--twe-radius-sm);
  box-shadow: inset 0 1px 2px rgb(var(--twe-shadow-rgb) / calc(0.10 * var(--twe-shadow-alpha)));
  cursor: pointer;
  transition: border-color var(--twe-transition), box-shadow var(--twe-transition);
}
select:not([data-twe-native]):not([class*='twe-']):not(.twe-select > select):hover:not(:disabled) { border-color: var(--twe-border-strong); }
select:not([data-twe-native]):not([class*='twe-']):not(.twe-select > select):disabled { opacity: 0.55; cursor: not-allowed; }

/* The open dropdown list itself. Pre-135 Chromium paints this from the OS and
 * CSS cannot reach it, so `color-scheme` (set per theme in tokens.css) is the
 * only lever that makes the popup dark instead of white — which is why every
 * theme block declares it. */
select:not([data-twe-native]):not([class*='twe-']):not(.twe-select > select) option {
  background-color: var(--twe-bg-card);
  color: var(--twe-text);
}

/* Bare checkbox / radio. Drawn box plus a CSS tick, so an unclassed input in a
 * hand-written form still matches the system. */
input[type='checkbox']:not([data-twe-native]):not([class*='twe-']),
input[type='radio']:not([data-twe-native]):not([class*='twe-']) {
  display: inline-grid;
  place-content: center;
  width: 1.125rem;
  height: 1.125rem;
  min-height: 0;
  flex: none;
  background-color: var(--twe-bg-input);
  border: var(--twe-border-width) solid var(--twe-border-strong);
  cursor: pointer;
  transition: background-color var(--twe-dur-fast) var(--twe-ease),
              border-color var(--twe-dur-fast) var(--twe-ease);
}
input[type='checkbox']:not([data-twe-native]):not([class*='twe-']) { border-radius: var(--twe-radius-xs); }
input[type='radio']:not([data-twe-native]):not([class*='twe-'])    { border-radius: var(--twe-radius-pill); }

input[type='checkbox']:not([data-twe-native]):not([class*='twe-']):checked,
input[type='radio']:not([data-twe-native]):not([class*='twe-']):checked {
  background-color: var(--twe-accent);
  border-color: var(--twe-accent);
}
input[type='checkbox']:not([data-twe-native]):not([class*='twe-']):checked::before {
  content: '';
  width: 0.32rem;
  height: 0.62rem;
  border: solid var(--twe-text-inverse);
  border-width: 0 2px 2px 0;
  transform: rotate(40deg) translateY(-8%);
}
input[type='radio']:not([data-twe-native]):not([class*='twe-']):checked::before {
  content: '';
  width: 0.42rem;
  height: 0.42rem;
  border-radius: var(--twe-radius-pill);
  background-color: var(--twe-text-inverse);
}
input[type='checkbox']:not([data-twe-native]):not([class*='twe-']):indeterminate {
  background-color: var(--twe-accent);
  border-color: var(--twe-accent);
}
input[type='checkbox']:not([data-twe-native]):not([class*='twe-']):indeterminate::before {
  content: '';
  width: 0.6rem;
  height: 2px;
  background-color: var(--twe-text-inverse);
}
input[type='checkbox']:not([data-twe-native]):not([class*='twe-']):disabled,
input[type='radio']:not([data-twe-native]):not([class*='twe-']):disabled { opacity: 0.55; cursor: not-allowed; }

/* Bare range. Track and thumb both need explicit painting once appearance is
 * stripped, or the control disappears entirely rather than looking native. */
input[type='range']:not([data-twe-native]):not([class*='twe-']) {
  width: 100%;
  height: 1.25rem;
  background: transparent;
  cursor: pointer;
}
input[type='range']:not([data-twe-native]):not([class*='twe-'])::-webkit-slider-runnable-track {
  height: 0.3rem;
  background-color: var(--twe-bg-inset);
  border-radius: var(--twe-radius-pill);
}
input[type='range']:not([data-twe-native]):not([class*='twe-'])::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 1rem;
  height: 1rem;
  margin-top: -0.35rem;
  background-color: var(--twe-accent);
  border: 2px solid var(--twe-bg-surface);
  border-radius: var(--twe-radius-pill);
  box-shadow: var(--twe-elev-1);
}

/* A bare <button> keeps native chrome ONLY if it has no TWE class. Rather than
 * restyle every button in the fleet from a vendored file (too blunt — some are
 * icon-only affordances with their own treatment), inherit the page font so it
 * at least stops reading as a different application, and let the linter flag
 * the missing .twe-btn. Typography is the tell people notice without knowing
 * why; the linter closes the rest. */
button:not([class*='twe-']):not([data-twe-native]) {
  font: inherit;
  cursor: pointer;
}
