/* ============================================================
   Form component API — load variables.css first.

   AI/code-generation contract:
   - Use .input-container for each field, .input-container__label
     for its label, .input-container__control for input/select/
     textarea, and one .input-container__message slot.
   - Use .choice-group for a radio group and .choice for each native
     checkbox/radio. Keep real inputs, unique IDs, labels, and names.
   - File inputs keep the native <input type="file"> with
     .input-container__control; an image preview is an <img
     class="input-container__preview" image_upload_preview> and an
     upload bar is <progress class="input-container__progress">.
   - Use .dropdown as a native <details> disclosure with checkboxes;
     do not add listbox roles without implementing its keyboard model.
   - Use token variables for component styling. Keep shared control
     styles here; reserve Tailwind utilities for page-specific layout.
   - Link non-empty field messages with aria-describedby. Set
     aria-invalid="true" only while a field actually has an error.

   The dropdown does not close on outside click or update its summary
   with selected counts; both behaviors require application JavaScript.
   ============================================================ */

@layer base {

  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  /* Unstyled-fallback reset for raw elements outside .input-container,
     e.g. third-party embeds or markup that skips the wrapper. */
  input,
  select,
  textarea,
  button {
    font-family: inherit;
    font-size: 100%;
    color: inherit;
  }
}

@layer components {

  /* ============ .form ============ */
  .form {
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
    /* rhythm between input-groups */
  }

  .form__heading {
    font-size: var(--font-size-2xl);
    font-weight: var(--font-weight-semibold);
    letter-spacing: var(--letter-spacing-tight);
    color: var(--color-text);
    line-height: var(--line-height-tight);
    margin: 0;
  }

  .form__sub-heading {
    font-size: var(--font-size-base);
    color: var(--color-text-muted);
    margin: var(--space-1) 0 0;
  }

  .form__description {
    font-size: var(--font-size-sm);
    color: var(--color-text-subtle);
    line-height: var(--line-height-normal);
    margin: var(--space-2) 0 0;
  }

  /* ============ .input-group ============ */
  .input-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    /* rhythm between fields within a group */
  }

  .input-group__heading {
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-semibold);
    letter-spacing: var(--letter-spacing-tight);
    color: var(--color-text);
    margin: 0;
  }

  .input-group__sub-heading {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    margin: 0;
  }

  .input-group__description {
    font-size: var(--font-size-sm);
    color: var(--color-text-subtle);
    margin: 0;
  }

  /* ============ .field-grid ============ */
  /* Fields side by side on a create or edit form: one column on phones, two from 40rem.
     __wide spans the whole row (a textarea, an address). */
  .field-grid {
    display: grid;
    gap: var(--space-4) var(--space-5);
    grid-template-columns: minmax(0, 1fr);
  }

  @media (min-width: 40rem) {
    .field-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .field-grid__wide {
      grid-column: 1 / -1;
    }
  }

  /* ============ .input-container (single field unit) ============ */
  .input-container {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
  }

  .input-container__label {
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    letter-spacing: var(--letter-spacing-normal);
    color: var(--color-text);
  }

  .input-container__control-row {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
  }

  .input-container__control-row .input-container__control {
    flex: 1;
  }

  .choice-group {
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
  }

  /* A legend sits outside the fieldset's flex gap, so it spaces itself like a label */
  .choice-group__legend {
    margin: 0 0 var(--space-2);
    padding: 0;
  }

  /* A group that appears on demand (the reasons of a picked status), set apart in a soft panel */
  .choice-group--panel {
    padding: var(--space-3) var(--space-4) var(--space-4);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-bg-subtle);
  }

  /* Inside a bordered fieldset a legend would sit on the border; floated, it is an ordinary first line */
  .choice-group--panel > .choice-group__legend {
    float: inline-start;
    width: 100%;
  }

  .choice-group--panel > .choice-group__legend + * {
    clear: both;
  }

  .choice-group--panel:not([hidden]) {
    animation: choice-group-in var(--transition-base);
  }

  @keyframes choice-group-in {
    from { opacity: 0; transform: translateY(-0.25rem); }
  }


  .choice-group__options {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
  }

  /* Card choices in even columns, e.g. the statuses of the change-status popup */
  .choice-group__options--cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr));
    gap: var(--space-2);
  }

  .choice-group__options--cards .choice__label {
    flex: 1;
    align-items: center;
    font-weight: var(--font-weight-medium);
  }

  /* Many choices: one scrolling column of rows (pair it with a .search-field above to filter them) */
  .choice-group__options--list {
    display: grid;
    gap: var(--space-1);
    max-height: 15rem;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: var(--space-1);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
  }

  .choice-group__options--list .choice__label {
    flex: 1;
    padding: var(--space-2) var(--space-3);
    padding-inline-start: 2.6em;
    border-radius: var(--radius-md);
  }

  .choice-group__options--list .choice__label::before,
  .choice-group__options--list .choice__label::after {
    inset-inline-start: var(--space-3);
  }

  .choice-group__options--list .choice--radio .choice__label::after {
    inset-inline-start: calc(var(--space-3) + 0.24em);
  }

  .choice-group__options--list .choice__label:hover {
    background: var(--color-bg-subtle);
  }

  .choice-group__options--list .choice__input:checked + .choice__label {
    background: var(--color-primary-subtle);
    font-weight: var(--font-weight-medium);
  }

  /* A person per row (the assign popup): avatar, name and detail, with a "Current" tag at the end */
  .person-option .choice__label {
    gap: var(--space-2);
    padding-block: var(--space-2);
  }

  .person-option .person {
    flex: 1;
  }

  .person-option__current {
    flex-shrink: 0;
    padding: 0.0625rem var(--space-2);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-full);
    background: var(--color-surface);
    color: var(--color-text-muted);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
  }

  .input-container__label--required::after {
    content: " *";
    color: var(--color-error);
  }

  /* --- Layout modifier: horizontal (label beside element) ---
     Default (no modifier) is the vertical stack already defined above. */
  .input-container--horizontal {
    display: grid;
    grid-template-columns: minmax(8rem, 12rem) minmax(0, 1fr);
    align-items: center;
    gap: var(--space-3);
  }

  @media (max-width: 40rem) {
    .input-container--horizontal {
      grid-template-columns: minmax(0, 1fr);
    }
  }

  .input-container--horizontal .input-container__label {
    margin: 0;
  }

  /* message, preview and progress rows span the full width under the label+element row */
  .input-container--horizontal .input-container__message,
  .input-container--horizontal .message,
  .input-container--horizontal .input-container__preview,
  .input-container--horizontal .input-container__progress {
    grid-column: 1 / -1;
  }

  /* Align label to the top instead of center when paired with a textarea */
  .input-container--horizontal.input-container--align-start {
    align-items: start;
  }

  /* --- Layout modifier: label-on-border (notched/floating label) --- */
  .input-container--label-border {
    position: relative;
  }

  .input-container--label-border .input-container__label {
    position: absolute;
    top: 0;
    inset-inline-start: var(--space-3);
    transform: translateY(-50%);
    margin: 0;
    padding: 0 var(--space-1);
    background: var(--color-surface);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-muted);
    pointer-events: none;
    z-index: 1;
  }

  .input-container__control {
    box-sizing: border-box;
    min-width: 0;
    width: 100%;
    font-family: inherit;
    font-size: var(--control-font-size);
    line-height: var(--line-height-normal);
    letter-spacing: var(--letter-spacing-normal);
    padding: var(--control-padding-y) var(--control-padding-x);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-xs);
    transition: border-color var(--transition-fast),
      box-shadow var(--transition-fast),
      background-color var(--transition-fast);
  }

  /* The record's name as a large heading-like input at the top of a create page */
  .input-container__control--title {
    font-size: var(--font-size-2xl);
    font-weight: var(--font-weight-semibold);
    letter-spacing: var(--letter-spacing-tight);
    padding-block: var(--space-2);
  }

  .input-container__control:hover {
    border-color: var(--color-border-strong);
  }

  .input-container__control:focus-visible {
    outline: none;
    border-color: var(--focus-ring-color);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus-ring-color) 22%, transparent),
                var(--shadow-xs);
  }

  .input-container__control:disabled {
    background: var(--color-bg-muted);
    color: var(--color-text-subtle);
    cursor: not-allowed;
    box-shadow: none;
  }

  .input-container__control[aria-invalid="true"] {
    border-color: var(--color-error);
  }

  .input-container__control[aria-invalid="true"]:focus-visible {
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-error) 22%, transparent),
                var(--shadow-xs);
  }

  .input-container__control::placeholder {
    color: var(--color-text-subtle);
    opacity: 1;
    /* Firefox lowers this by default — normalize across browsers */
  }

  /* Chrome/WebKit force a yellow/blue background on autofilled inputs
     that background-color cannot override directly. Standard workaround:
     push the browser's own background-color transition out to an
     effectively-never delay so it never visually applies, and force
     the text color via the WebKit-specific fill property. Firefox does
     not need this — it respects normal styling on autofilled fields. */
  .input-container__control:-webkit-autofill,
  .input-container__control:-webkit-autofill:hover,
  .input-container__control:-webkit-autofill:focus {
    -webkit-text-fill-color: var(--color-text);
    transition: background-color 600000s 0s, color 600000s 0s;
  }

  /* --- textarea --- */
  textarea.input-container__control {
    min-height: 6rem;
    /* rem: fixed floor, independent of the field's own font-size */
    resize: vertical;
    line-height: var(--line-height-relaxed);
  }

  /* --- select: strip native arrow, draw our own ---
     background-position has no reliably-supported logical
     ("inline-start/end") keyword form yet, so the arrow position
     is flipped explicitly for RTL below rather than relying on
     logical properties alone. */
  select.input-container__control {
    appearance: none;
    -webkit-appearance: none;
    padding-inline-end: 2.25em;
    background-repeat: no-repeat;
    background-position: right 0.75em center;
    background-size: 0.875em;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%235a6270' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    cursor: pointer;
  }

  [dir="rtl"] select.input-container__control {
    background-position: left 0.75em center;
  }

  /* --- select multiple ---
     KNOWN LIMITATION: the "selected" highlight color on <option> inside
     a native multi-select listbox is inconsistently themeable across
     browsers — Chrome/WebKit largely ignore background-color/color on
     selected options and fall back to the OS accent color regardless of
     what you set here. Border/radius/font on the select box itself are
     safe to style; the option highlight is not. If you need a reliably
     "on-brand" multi-select, use the .dropdown checkbox-list pattern
     below instead of a native <select multiple>. */
  select[multiple].input-container__control {
    padding: var(--space-2);
    background-image: none;
    /* no single-value chevron needed for a listbox */
  }

  select[multiple].input-container__control option {
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
  }

  /* A gradient (not a plain colour) is the one background Chrome and Safari
     honour on a selected option, so the highlight follows the theme. */
  select[multiple].input-container__control option:checked {
    background: linear-gradient(var(--color-primary-subtle), var(--color-primary-subtle));
    color: var(--color-text);
  }

  /* --- file input ---
     Keep the native <input type="file"> (keyboard, drag-and-drop and
     screen readers work as-is) and restyle only its button. */
  input[type="file"].input-container__control {
    padding: var(--space-1);
    cursor: pointer;
    color: var(--color-text-muted);
  }

  input[type="file"].input-container__control::file-selector-button {
    margin-inline-end: var(--space-3);
    padding: var(--space-1) var(--space-3);
    border: var(--border-width) solid var(--color-border-strong);
    border-radius: var(--radius-sm);
    background: var(--color-bg-subtle);
    color: var(--color-text);
    font: inherit;
    font-weight: var(--font-weight-medium);
    cursor: pointer;
    transition: background-color var(--transition-fast);
  }

  input[type="file"].input-container__control:hover:not(:disabled)::file-selector-button {
    background: var(--color-bg-muted);
  }

  input[type="file"].input-container__control:disabled::file-selector-button {
    border-color: var(--color-border);
    background: transparent;
    color: var(--color-text-subtle);
    cursor: not-allowed;
  }

  /* Image preview filled by the library from [image_upload_input];
     hidden until an image has been chosen. */
  .input-container__preview {
    display: block;
    align-self: flex-start;
    /* keep its own size inside the flex-column field container */
    max-width: 100%;
    max-height: 10rem;
    object-fit: contain;
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-bg-subtle);
  }

  .input-container__preview:not([src]),
  .input-container__preview[src=""] {
    display: none;
  }

  /* --- upload progress (a native <progress> passed as progress_bar) --- */
  progress.input-container__progress {
    appearance: none;
    width: 100%;
    height: 0.5rem;
    border: none;
    border-radius: var(--radius-full);
    background: var(--color-bg-muted);
    overflow: hidden;
  }

  progress.input-container__progress::-webkit-progress-bar {
    background: var(--color-bg-muted);
  }

  progress.input-container__progress::-webkit-progress-value {
    background: var(--color-primary);
    transition: width var(--transition-fast);
  }

  progress.input-container__progress::-moz-progress-bar {
    background: var(--color-primary);
  }

  /* No value yet (indeterminate): a segment slides along the track. */
  progress.input-container__progress:indeterminate {
    background: linear-gradient(var(--color-primary), var(--color-primary)) no-repeat var(--color-bg-muted);
    background-size: 30% 100%;
    animation: input-container-progress-indeterminate 1.2s linear infinite;
  }

  progress.input-container__progress:indeterminate::-webkit-progress-bar {
    background: transparent;
  }

  progress.input-container__progress:indeterminate::-moz-progress-bar {
    background: transparent;
  }

  @keyframes input-container-progress-indeterminate {
    from { background-position: -50% 0; }
    to { background-position: 150% 0; }
  }

  /* Not built from the transition tokens, so it needs its own reduced-motion rule. */
  @media (prefers-reduced-motion: reduce) {
    progress.input-container__progress:indeterminate {
      animation: none;
      background-position: 50% 0;
    }
  }

  /* ============================================================
     .choice — checkbox / radio built as hidden <input> + styled
     <label>. The input stays in the DOM and fully keyboard/screen-
     reader operable; it's visually hidden (never display:none,
     which would break focus and AT). The label's ::before/::after
     draw the visible control. This supersedes styling the input's
     own appearance directly — no reliance on ::after generating on
     a replaced <input> element, so no cross-browser uncertainty here.

     Markup:
     <div class="choice choice--checkbox">
       <input type="checkbox" id="x" class="choice__input">
       <label for="x" class="choice__label">Label text</label>
     </div>
  ============================================================ */
  .choice {
    display: inline-flex;
    align-items: center;
  }

  .choice__input {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  .choice__label {
    position: relative;
    display: inline-flex;
    align-items: center;
    padding-inline-start: 1.85em;
    font-size: var(--font-size-sm);
    color: var(--color-text);
    line-height: var(--line-height-normal);
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
  }

  /* the visible box/circle */
  .choice__label::before {
    content: "";
    position: absolute;
    inset-inline-start: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 1.1875em;
    height: 1.1875em;
    border: var(--border-width-thick) solid var(--color-border-strong);
    background: var(--color-surface);
    box-shadow: var(--shadow-xs);
    transition: background var(--transition-fast),
      border-color var(--transition-fast),
      box-shadow var(--transition-fast),
      transform var(--transition-fast);
  }

  .choice__label:hover::before {
    border-color: var(--color-primary);
  }

  /* the checkmark / dot, hidden until checked */
  .choice__label::after {
    content: "";
    position: absolute;
    inset-inline-start: 0;
    top: 50%;
    width: 1.1875em;
    height: 1.1875em;
    transform: translateY(-50%) scale(0.8);
    opacity: 0;
    transition: opacity var(--transition-fast),
      transform var(--transition-fast);
  }

  .choice--checkbox .choice__label::before {
    border-radius: var(--radius-sm);
  }

  .choice--checkbox .choice__label::after {
    background-color: var(--color-text-inverse);
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Cpath d='M3 8.5L6.5 12L13 4.5' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Cpath d='M3 8.5L6.5 12L13 4.5' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    mask-size: 1em;
    mask-repeat: no-repeat;
    mask-position: center;
  }

  .choice--radio .choice__label::before {
    border-radius: var(--radius-full);
  }

  .choice--radio .choice__label::after {
    inset-inline-start: 0.24em;
    width: 0.7em;
    height: 0.7em;
    border-radius: var(--radius-full);
    background: var(--color-text-inverse);
  }

  .choice__input:checked+.choice__label::before {
    background: var(--color-primary);
    border-color: var(--color-primary);
    box-shadow: var(--shadow-xs);
  }

  .choice__input:checked+.choice__label::after {
    opacity: 1;
    transform: translateY(-50%) scale(1);
  }

  /* forced-colors: background-color alone is not a reliable checked
     indicator once the system palette is enforced — add a border-width
     change so checked state is still visible via a shape difference,
     not color alone. */
  @media (forced-colors: active) {
    .choice__input:checked+.choice__label::before {
      border-width: calc(var(--border-width-thick) * 2);
    }
  }

  .choice__input:focus-visible+.choice__label::before {
    box-shadow: 0 0 0 2px var(--color-surface),
                0 0 0 4px color-mix(in srgb, var(--focus-ring-color) 80%, transparent);
  }

  .choice__input:disabled+.choice__label {
    color: var(--color-text-subtle);
    cursor: not-allowed;
  }

  .choice__input:disabled+.choice__label::before {
    background: var(--color-bg-muted);
    border-color: var(--color-border);
    box-shadow: none;
  }

  /* --- .choice--card: whole option as a selectable bordered card,
     common in modern plan/option pickers --- */
  .choice--card .choice__label {
    display: flex;
    padding: var(--space-3) var(--space-4);
    padding-inline-start: 2.85em;
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    box-shadow: var(--shadow-xs);
    transition: border-color var(--transition-fast),
      background var(--transition-fast),
      box-shadow var(--transition-fast),
      transform var(--transition-fast);
    line-height: var(--line-height-normal);
  }

  .choice--card .choice__label:hover {
    border-color: var(--color-border-strong);
    background: var(--color-bg-subtle);
  }

  .choice--card .choice__label::before {
    inset-inline-start: var(--space-3);
  }

  .choice--card.choice--checkbox .choice__label::after {
    inset-inline-start: var(--space-3);
  }

  .choice--card.choice--radio .choice__label::after {
    inset-inline-start: calc(var(--space-3) + 0.24em);
  }

  .choice--card .choice__input:checked+.choice__label {
    border-color: var(--color-primary);
    background: var(--color-primary-subtle);
    box-shadow: 0 0 0 1px var(--color-primary), var(--shadow-xs);
  }

  .choice--card .choice__input:focus-visible+.choice__label {
    box-shadow: 0 0 0 2px var(--color-surface),
                0 0 0 4px color-mix(in srgb, var(--focus-ring-color) 80%, transparent);
  }

  /* ============================================================
     .dropdown — custom option panel built on native <details>/
     <summary>, no JS required for open/close.
  ============================================================ */
  .dropdown {
    position: relative;
    display: inline-block;
  }

  .dropdown__trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    min-width: 12rem;
    padding: var(--control-padding-y) var(--control-padding-x);
    font-size: var(--control-font-size);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-xs);
    cursor: pointer;
    list-style: none;
    transition: border-color var(--transition-fast),
      box-shadow var(--transition-fast);
  }

  .dropdown__trigger:hover {
    border-color: var(--color-border-strong);
  }

  .dropdown__trigger:focus-visible {
    outline: none;
    border-color: var(--focus-ring-color);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus-ring-color) 22%, transparent),
                var(--shadow-xs);
  }

  .dropdown__trigger::-webkit-details-marker {
    display: none;
  }

  .dropdown__trigger::after {
    content: "";
    width: 0.875em;
    height: 0.875em;
    flex-shrink: 0;
    background-color: var(--color-text-muted);
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    mask-size: contain;
    mask-repeat: no-repeat;
    transition: transform var(--transition-fast);
  }

  .dropdown[open]>.dropdown__trigger::after {
    transform: rotate(180deg);
  }

  .dropdown[open]>.dropdown__trigger {
    border-color: var(--focus-ring-color);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus-ring-color) 22%, transparent);
  }

  .dropdown__panel {
    position: absolute;
    top: calc(100% + var(--space-2));
    inset-inline-start: 0;
    z-index: var(--z-dropdown);
    min-width: 100%;
    max-height: 16rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-2);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    box-shadow: var(--shadow-lg);
    scrollbar-width: thin;
  }

  .dropdown__panel .choice__label {
    display: flex;
    width: 100%;
    padding: var(--space-2);
    padding-inline-start: 1.85em;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast);
  }

  .dropdown__panel .choice__label:hover {
    background: var(--color-bg-subtle);
  }

  /* ============================================================
     selector.form-selector
  ============================================================ */
  selector.form-selector {
    position: relative;
    display: block;
    width: 100%;
    min-width: 0;
    color: var(--color-text);
    cursor: pointer;
  }

  selector.form-selector selected {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-min-height);
    padding: calc(var(--control-padding-y) * 0.8) var(--control-padding-x);
    font-size: var(--control-font-size);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    box-shadow: var(--shadow-xs);
    transition: border-color var(--transition-fast),
      box-shadow var(--transition-fast);
  }

  selector.form-selector:hover selected {
    border-color: var(--color-border-strong);
  }

  selector.form-selector selected::after {
    content: "";
    flex: 0 0 0.75rem;
    width: 0.75rem;
    height: 0.75rem;
    margin-inline-start: auto;
    background: var(--color-text-muted);
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
    transition: transform var(--transition-fast);
  }

  selector.form-selector.open selected {
    border-color: var(--focus-ring-color);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus-ring-color) 22%, transparent),
                var(--shadow-xs);
  }

  selector.form-selector selected:focus-visible {
    outline: none;
    border-color: var(--focus-ring-color);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus-ring-color) 22%, transparent);
  }

  /* validation.js marks the <selected> trigger invalid, like .input-container__control */
  selector.form-selector selected[aria-invalid="true"] {
    border-color: var(--color-error);
  }

  selector.form-selector.open selected::after {
    transform: rotate(180deg);
  }

  selector.form-selector selected items {
    display: flex;
    flex: 1;
    flex-wrap: wrap;
    gap: var(--space-2);
    min-width: 0;
  }

  selector.form-selector selected items item {
    display: block;
    min-width: 0;
    overflow-wrap: anywhere;
  }

  selector.form-selector[multi] selected items item {
    padding: 0.2rem 0.55rem;
    border-radius: var(--radius-sm);
    background: var(--color-bg-subtle);
    border: var(--border-width) solid var(--color-border);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    display: inline-flex;
    align-items: center;
  }

  selector.form-selector [remove_multiple_select_item] {
    margin: 0;
    margin-inline-start: var(--space-2);
    padding: 0 0.15rem;
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--color-text-muted);
    font: inherit;
    font-size: 1.1em;
    line-height: 1;
    cursor: pointer;
    transition: color var(--transition-fast);
  }

  selector.form-selector [remove_multiple_select_item]:hover {
    color: var(--color-error);
  }

  selector.form-selector [remove_multiple_select_item]:focus-visible {
    outline: none;
    color: var(--color-error);
    box-shadow: 0 0 0 2px var(--focus-ring-color);
  }

  selector.form-selector options {
    position: fixed;
    inset: auto;
    z-index: var(--z-dropdown);
    display: flex;
    flex-direction: column;
    max-height: 18.75rem;
    height: 0;
    margin: 0;
    overflow: hidden;
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-lg);
    padding-block: var(--space-1);
    box-sizing: border-box;
    overscroll-behavior: contain;
  }

  selector.form-selector options:not(:popover-open) {
    display: none;
  }

  selector.form-selector .form-selector__listbox {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
  }

  /* Items and headings set their own display, so the hidden attribute needs an explicit rule. */
  selector.form-selector options [hidden] {
    display: none;
  }

  selector.form-selector options search {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    border-block-end: var(--border-width) solid var(--color-border);
    background: var(--color-surface);
  }

  selector.form-selector options search input {
    flex: 1;
    min-width: 0;
    padding: var(--space-1) 0;
    border: 0;
    outline: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    font-size: var(--font-size-sm);
  }

  selector.form-selector options search input:focus-visible {
    outline: none;
  }

  /* The input itself has no border, so the search row shows focus with an underline. */
  selector.form-selector options search:focus-within {
    border-block-end-color: var(--focus-ring-color);
    box-shadow: inset 0 -1px 0 var(--focus-ring-color);
  }

  selector.form-selector options search .icon {
    display: block;
    flex: 0 0 1rem;
    width: 1rem;
    height: 1rem;
    background: var(--color-text-muted);
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Ccircle cx='6.75' cy='6.75' r='4.75' stroke='black' stroke-width='1.5'/%3E%3Cpath d='m10.25 10.25 4 4' stroke='black' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E") center / contain no-repeat;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none'%3E%3Ccircle cx='6.75' cy='6.75' r='4.75' stroke='black' stroke-width='1.5'/%3E%3Cpath d='m10.25 10.25 4 4' stroke='black' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E") center / contain no-repeat;
  }

  selector.form-selector options item {
    display: block;
    padding: var(--space-2) var(--space-3);
    margin-inline: var(--space-1);
    margin-block: 1px;
    border-radius: var(--radius-sm);
    font-size: var(--font-size-sm);
    cursor: pointer;
    overflow-wrap: anywhere;
    transition: background var(--transition-fast), color var(--transition-fast);
  }

  selector.form-selector[multi] options item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
  }

  selector.form-selector[multi] options item input[type="checkbox"] {
    order: -1;
    flex: 0 0 auto;
    margin: 0;
  }

  selector.form-selector options item:hover,
  selector.form-selector options item.selectedItem,
  selector.form-selector options item.activeOption {
    background: var(--color-primary-subtle);
    color: var(--color-primary);
  }

  selector.form-selector options heading {
    display: block;
    padding: var(--space-2) var(--space-3);
    color: var(--color-text-subtle);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    letter-spacing: var(--letter-spacing-wide);
    text-transform: uppercase;
  }

  /* Contextual help — separate from validation messaging below */
  .input-container__info-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 1.5rem;
    font-size: var(--font-size-sm);
    border: none;
    border-radius: var(--radius-full);
    background: var(--color-bg-muted);
    color: var(--color-text-muted);
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast);
  }

  .input-container__info-button:hover {
    background: var(--color-secondary-subtle);
    color: var(--color-text);
  }

  .input-container__info-button:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--color-surface),
                0 0 0 4px var(--focus-ring-color);
  }

  @media (forced-colors: active) {
    .input-container__control:focus-visible,
    .input-container__info-button:focus-visible,
    selector.form-selector options search input:focus-visible,
    selector.form-selector [remove_multiple_select_item]:focus-visible,
    .choice__input:focus-visible+.choice__label::before {
      outline: var(--focus-ring-width) solid Highlight;
      outline-offset: var(--focus-ring-offset);
      box-shadow: none;
    }
  }

  .input-container__message {
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
    margin: 0;
  }

  .input-container__message:empty {
    display: none;
  }

  .input-container__message--error {
    color: var(--color-error-text);
    font-weight: var(--font-weight-medium);
  }

  .input-container__message--success {
    color: var(--color-success-text);
    font-weight: var(--font-weight-medium);
  }

  .input-container__message--warning {
    color: var(--color-warning-text);
    font-weight: var(--font-weight-medium);
  }

  /* ============ .message (shared: field-level or page-level) ============ */
  .message {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-normal);
    border: var(--border-width) solid transparent;
  }

  .message--success {
    background: var(--color-success-subtle);
    color: var(--color-success-text);
    border-color: var(--color-success-border);
  }

  .message--warning {
    background: var(--color-warning-subtle);
    color: var(--color-warning-text);
    border-color: var(--color-warning-border);
  }

  .message--error {
    background: var(--color-error-subtle);
    color: var(--color-error-text);
    border-color: var(--color-error-border);
  }

  .message--info {
    background: var(--color-info-subtle);
    color: var(--color-info-text);
    border-color: var(--color-info-border);
  }

  /* ============================================================
     Date range picker (daterange.js): <input date_range> becomes
     .date-range > input.date-range__value (hidden, still the field)
     + button.date-range__trigger + .date-range__panel.
     State attributes: [aria-expanded] on the trigger, [data-empty]
     when no dates are chosen; on days [data-start], [data-end],
     [data-in-range], [aria-current="date"] (today), :disabled.
     ============================================================ */
  .date-range {
    position: relative;
    display: block;
  }

  .date-range__value {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }

  .date-range__trigger {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-min-height);
    text-align: start;
    cursor: pointer;
  }

  .date-range__trigger[data-empty] .date-range__text {
    color: var(--color-text-subtle);
  }

  .date-range__text {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .date-range__icon {
    flex: none;
    width: 1.1em;
    height: 1.1em;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    color: var(--color-text-muted);
  }

  .date-range__panel {
    position: fixed;
    inset: auto;
    margin: 0;
    z-index: var(--z-dropdown);
    display: flex;
    gap: var(--space-4);
    width: max-content;
    max-width: calc(100vw - 2 * var(--space-4));
    max-height: calc(100dvh - 2 * var(--space-4));
    overflow: auto;
    overscroll-behavior: contain;
    padding: var(--space-3);
    background: var(--color-surface-raised);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    color: var(--color-text);
  }

  .date-range__panel:not(:popover-open) {
    display: none;
  }

  .date-range__presets {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    min-width: 10rem;
    padding-inline-end: var(--space-3);
    border-inline-end: var(--border-width) solid var(--color-border-subtle);
  }

  .date-range__preset {
    padding: var(--space-2) var(--space-3);
    border: 0;
    border-radius: var(--radius-md);
    background: none;
    color: var(--color-text);
    font: inherit;
    font-size: var(--font-size-sm);
    text-align: start;
    cursor: pointer;
    transition: background-color var(--transition-fast);
  }

  .date-range__preset:hover {
    background: var(--color-bg-muted);
  }

  .date-range__preset[aria-pressed="true"] {
    background: var(--color-primary-subtle);
    color: var(--color-primary);
    font-weight: var(--font-weight-semibold);
  }

  .date-range__body {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
  }

  .date-range__months {
    position: relative;
  }

  .date-range__previous,
  .date-range__next {
    position: absolute;
    top: 0;
  }

  .date-range__previous { inset-inline-start: 0; }
  .date-range__next { inset-inline-end: 0; }

  .date-range__calendars {
    display: flex;
    gap: var(--space-5);
  }

  .date-range__title {
    margin: 0 0 var(--space-2);
    line-height: 2rem;
    text-align: center;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-semibold);
  }

  .date-range__grid {
    border-collapse: collapse;
  }

  .date-range__grid th {
    width: 2.25rem;
    height: 2rem;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-subtle);
  }

  .date-range__grid td {
    padding: 0;
  }

  .date-range__day {
    width: 2.25rem;
    height: 2.25rem;
    border: 0;
    border-radius: var(--radius-md);
    background: none;
    color: var(--color-text);
    font: inherit;
    font-size: var(--font-size-sm);
    font-variant-numeric: tabular-nums;
    cursor: pointer;
    transition: background-color var(--transition-fast);
  }

  .date-range__day:hover:not(:disabled, [data-start], [data-end]) {
    background: var(--color-bg-muted);
  }

  .date-range__day[aria-current="date"] {
    font-weight: var(--font-weight-bold);
    text-decoration: underline;
    text-underline-offset: 3px;
  }

  .date-range__day[data-in-range] {
    border-radius: 0;
    background: var(--color-primary-subtle);
  }

  .date-range__day[data-start],
  .date-range__day[data-end] {
    background: var(--color-primary);
    color: var(--color-text-inverse);
  }

  .date-range__day:disabled {
    color: var(--color-text-subtle);
    opacity: 0.5;
    cursor: not-allowed;
  }

  .date-range__day:focus-visible,
  .date-range__preset:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--color-surface), 0 0 0 4px color-mix(in srgb, var(--focus-ring-color) 80%, transparent);
  }

  .date-range__inputs {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
    gap: var(--space-3);
  }

  .date-range__input {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    font-size: var(--font-size-xs);
    color: var(--color-text-muted);
  }

  .date-range__footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-2);
    padding-top: var(--space-3);
    border-top: var(--border-width) solid var(--color-border-subtle);
  }

  /* Narrow screens: keep quick ranges above the calendars so both months fit. */
  @media (max-width: 64rem) {
    .date-range__panel {
      flex-direction: column;
    }

    .date-range__presets {
      flex-direction: row;
      flex-wrap: wrap;
      flex-shrink: 0;
      min-width: 0;
      padding-inline-end: 0;
      padding-bottom: var(--space-3);
      border-inline-end: 0;
      border-bottom: var(--border-width) solid var(--color-border-subtle);
    }

    .date-range__body {
      flex-shrink: 0;
    }
  }

  /* Phones: the panel opens as a sheet at the bottom of the screen */
  @media (max-width: 40rem) {
    .date-range__panel {
      position: fixed;
      top: auto;
      inset-inline: 0;
      bottom: 0;
      z-index: var(--z-modal);
      flex-direction: column;
      width: auto;
      max-width: none;
      max-height: 85dvh;
      border-end-start-radius: 0;
      border-end-end-radius: 0;
    }

    .date-range__calendars {
      justify-content: center;
    }
  }

  @media (forced-colors: active) {
    .date-range__day[data-start],
    .date-range__day[data-end],
    .date-range__preset[aria-pressed="true"] {
      outline: var(--border-width-thick) solid Highlight;
    }

    .date-range__day[data-in-range] {
      border: var(--border-width) dashed CanvasText;
    }

    .date-range__day:focus-visible,
    .date-range__preset:focus-visible {
      outline: var(--focus-ring-width) solid Highlight;
    }
  }

}

/* Password visibility keeps the native field and its validation/label bindings. */
.password-control {
    display: flex;
    align-items: center;
    gap: .5rem;
    min-width: 0;
    width: 100%;
}
.password-control > input {
    flex: 1;
    min-width: 0;
}
.password-control__toggle {
    flex: none;
    min-width: 3.5rem;
    min-height: 44px;
}

/* Segmented choice (field.segmented): a radio group drawn as .segmented options.
   The radios stay real (keyboard, screen readers, validation); only their circles are hidden. */
@layer components {
  .segmented-choice {
    min-inline-size: 0;
    margin: 0;
    padding: 0;
    border: 0;
  }

  .segmented-choice__options {
    display: flex;
    flex-wrap: wrap;
    max-inline-size: 100%;
  }

  .segmented-choice__option {
    position: relative;
    display: inline-flex;
    align-items: center;
    min-block-size: 2rem;
  }

  .segmented-choice__input {
    position: absolute;
    inset: 0;
    margin: 0;
    opacity: 0;
    cursor: pointer;
  }

  .segmented-choice__option:has(:checked) {
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-sm);
  }

  .segmented-choice__option:has(:focus-visible) {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: 0;
  }

  .segmented-choice__option:has(:disabled) {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .segmented-choice:has([aria-invalid="true"]) .segmented-choice__options {
    box-shadow: 0 0 0 var(--border-width) var(--color-error);
  }

  /* Compact file drop (field.file + file-drop.js) */
  .file-drop__zone {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-block-size: var(--control-min-height);
    padding: var(--space-2) var(--space-3);
    border: var(--border-width) dashed var(--color-border-strong);
    border-radius: var(--radius-md);
    background: var(--color-bg-subtle);
    color: var(--color-text-muted);
    cursor: pointer;
  }

  .file-drop__text {
    min-inline-size: 0;
    overflow-wrap: anywhere;
  }

  .file-drop[data-has-files] .file-drop__text {
    color: var(--color-text);
  }

  .file-drop__input {
    position: absolute;
    inset: 0;
    inline-size: 100%;
    block-size: 100%;
    opacity: 0;
    cursor: pointer;
  }

  .file-drop__zone:has(:focus-visible) {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
  }

  .file-drop[data-dragging] .file-drop__zone {
    border-color: var(--color-primary);
    background: var(--color-primary-subtle);
  }

  .file-drop__input:disabled,
  .file-drop:has(:disabled) .file-drop__zone {
    cursor: not-allowed;
    opacity: 0.6;
  }

  .file-drop__limits {
    margin: var(--space-1) 0 0;
    font-size: var(--font-size-xs);
    color: var(--color-text-subtle);
  }
}
