@layer components {
  /* ---------- Icon ---------- */
  .icon {
    width: 1.15em;
    height: 1.15em;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.85;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .icon--sm {
    width: 14px;
    height: 14px;
  }
  .icon--md {
    width: 18px;
    height: 18px;
  }
  .icon--lg {
    width: 24px;
    height: 24px;
    stroke-width: 1.6;
  }
  .icon--xl {
    width: 32px;
    height: 32px;
    stroke-width: 1.4;
  }
  .icon--spin {
    animation: ui-spin 0.8s linear infinite;
  }
  @keyframes ui-spin {
    to {
      transform: rotate(360deg);
    }
  }

  /* ---------- Button ---------- */
  .btn {
    --btn-bg: var(--surface);
    --btn-fg: var(--text);
    --btn-border: var(--border-strong);
    --btn-bg-hover: var(--surface-hover);
    --btn-bg-active: var(--surface-active);
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    height: var(--control-h);
    padding: 0 calc(var(--control-px) + 2px);
    border: 1px solid var(--btn-border);
    border-radius: var(--radius-sm);
    background: var(--btn-bg);
    color: var(--btn-fg);
    font-size: calc(var(--control-font) - 1px);
    font-weight: var(--weight-medium);
    line-height: 1;
    white-space: nowrap;
    text-decoration: none;
    user-select: none;
    box-shadow: var(--shadow-1);
    transition:
      background-color var(--dur-fast) var(--ease),
      border-color var(--dur-fast) var(--ease),
      box-shadow var(--dur-fast) var(--ease),
      transform var(--dur-fast) var(--ease);
  }
  .btn:hover,
  .btn.is-hover {
    background: var(--btn-bg-hover);
    color: var(--btn-fg);
  }
  .btn:active:not(:disabled, [aria-disabled="true"]) {
    background: var(--btn-bg-active);
    transform: scale(0.97);
  }
  .btn .icon {
    transition: transform var(--dur) var(--ease-spring);
  }
  .btn:focus-visible,
  .btn.is-focus {
    outline: none;
    box-shadow: var(--focus-ring);
  }
  .btn:disabled,
  .btn[aria-disabled="true"] {
    cursor: not-allowed;
    opacity: 0.5;
    box-shadow: none;
  }
  .btn--primary {
    --btn-bg: var(--accent-gradient);
    --btn-fg: var(--accent-contrast);
    --btn-border: transparent;
    --btn-bg-hover: var(--accent-gradient-hover);
    --btn-bg-active: var(--accent-active);
    font-weight: var(--weight-semibold);
    box-shadow: var(--shadow-accent);
  }
  .btn--primary:hover:not(:disabled, [aria-disabled="true"]),
  .btn--primary.is-hover {
    transform: translateY(-1px);
    box-shadow:
      0 1px 0 rgb(255 255 255 / 0.18) inset,
      0 10px 22px -8px var(--accent);
  }
  .btn--primary:hover .icon {
    transform: scale(1.12);
  }
  .btn--primary:focus-visible,
  .btn--primary.is-focus {
    box-shadow: var(--shadow-accent), var(--focus-ring);
  }
  .btn--danger {
    --btn-bg: var(--danger);
    --btn-fg: var(--n-0);
    --btn-border: var(--danger);
    --btn-bg-hover: var(--danger-hover);
    --btn-bg-active: var(--red-700);
  }
  .btn--danger:focus-visible {
    box-shadow: var(--focus-ring-danger);
  }
  .btn--soft {
    --btn-bg: var(--accent-soft);
    --btn-fg: var(--accent-text);
    --btn-border: transparent;
    --btn-bg-hover: var(--accent-soft-hover);
    --btn-bg-active: var(--cobalt-200);
    box-shadow: none;
  }
  .btn--ghost {
    --btn-bg: transparent;
    --btn-fg: var(--text-muted);
    --btn-border: transparent;
    --btn-bg-hover: var(--surface-active);
    --btn-bg-active: var(--n-150);
    box-shadow: none;
  }
  .btn--ghost:hover,
  .btn--ghost.is-hover {
    color: var(--text);
  }
  /* Toggle buttons: aria-pressed is the state. */
  .btn[aria-pressed="true"] {
    --btn-bg: var(--accent-soft);
    --btn-bg-hover: var(--accent-soft-hover);
    --btn-fg: var(--accent-text);
    color: var(--accent-text);
  }
  .btn--link {
    --btn-bg: transparent;
    --btn-fg: var(--accent-text);
    --btn-border: transparent;
    --btn-bg-hover: transparent;
    --btn-bg-active: transparent;
    padding-inline: 0;
    height: auto;
    box-shadow: none;
  }
  .btn--link:hover {
    text-decoration: underline;
  }
  .btn--sm {
    height: var(--control-h-sm);
    padding-inline: var(--space-3);
    font-size: var(--text-sm);
    gap: var(--space-1-5);
  }
  .btn--lg {
    height: var(--control-h-lg);
    padding-inline: var(--space-6);
    border-radius: var(--radius-md);
    font-size: var(--text-md);
  }
  .btn--icon {
    width: var(--control-h);
    padding: 0;
  }
  .btn--icon.btn--sm {
    width: var(--control-h-sm);
  }
  .btn--icon.btn--lg {
    width: var(--control-h-lg);
  }
  .btn--block {
    width: 100%;
  }
  .btn__spinner {
    display: none;
  }
  .btn[aria-busy="true"] {
    cursor: progress;
  }
  .btn[aria-busy="true"] > :not(.btn__spinner) {
    opacity: 0; /* not visibility: the label must stay the accessible name */
  }
  .btn[aria-busy="true"] .btn__spinner {
    display: block;
    position: absolute;
    inset: 0;
    margin: auto;
    animation: ui-spin 0.8s linear infinite;
  }
  .btn-group {
    display: inline-flex;
  }
  .btn-group > .btn:not(:first-child) {
    border-start-start-radius: 0;
    border-end-start-radius: 0;
    margin-inline-start: -1px;
  }
  .btn-group > .btn:not(:last-child) {
    border-start-end-radius: 0;
    border-end-end-radius: 0;
  }

  /* ---------- Field wrapper ---------- */
  .field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1-5);
    min-width: 0;
  }
  .field__label,
  .field legend {
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--text);
    padding: 0;
  }
  .field__req {
    color: var(--danger);
    margin-inline-start: 2px;
  }
  .field__optional {
    color: var(--text-subtle);
    font-weight: var(--weight-regular);
    margin-inline-start: var(--space-1);
  }
  .field__footer {
    display: flex;
    gap: var(--space-3);
    align-items: flex-start;
    font-size: var(--text-xs);
    color: var(--text-muted);
  }
  .field__footer:empty {
    display: none;
  }
  .field__hint {
    flex: 1;
    color: var(--text-muted);
    font-size: var(--text-xs);
  }
  .field__error {
    display: flex;
    gap: var(--space-1);
    align-items: flex-start;
    color: var(--danger-text);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    list-style: none;
    padding: 0;
  }
  .field__error .icon {
    flex: none;
    margin-top: 2px;
  }
  .field__error li {
    display: flex;
    gap: var(--space-1);
    align-items: flex-start;
  }
  .field fieldset {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
  }
  /* A legend is no flex item of its fieldset: its own margin keeps the label-to-control gap of
     .field, so a fieldset stands level with the plain fields beside it. */
  .field fieldset > legend {
    margin-bottom: var(--space-1-5);
  }
  .form-errors {
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-sm);
    background: var(--danger-soft);
    color: var(--danger-text);
    font-size: var(--text-sm);
  }
  .form-grid {
    display: grid;
    gap: var(--space-5) var(--space-4);
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
  }
  .form-grid > .field--wide {
    grid-column: 1 / -1;
  }
  .form-grid--tight {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
    gap: var(--space-4);
  }
  @media (min-width: 720px) {
    .form-grid > .field--span2 {
      grid-column: span 2;
    }
  }
  /* A long form in titled parts, laid out across the width instead of one tall column. */
  .form-sections {
    display: flex;
    flex-direction: column;
    gap: var(--section-gap);
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
  }
  .form-section__title {
    margin-bottom: var(--space-3);
    color: var(--text-muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }

  /* ---------- Text input (wrapper is the visible box) ---------- */
  .input {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
    min-height: var(--control-h);
    padding-inline: var(--control-px);
    border: 1px solid var(--border-input);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: var(--control-font);
    box-shadow: var(--shadow-1);
    transition:
      border-color var(--dur-fast) var(--ease),
      box-shadow var(--dur) var(--ease);
  }
  .input:hover,
  .input.is-hover {
    border-color: var(--border-input-hover);
  }
  .input:focus-within,
  .input.is-focus {
    border-color: var(--accent);
    box-shadow: var(--focus-ring);
  }
  .input:has(.input__control[aria-invalid="true"]),
  .input.is-invalid {
    border-color: var(--danger);
  }
  .input:has(.input__control[aria-invalid="true"]):focus-within,
  .input.is-invalid.is-focus {
    box-shadow: var(--focus-ring-danger);
  }
  .input:has(.input__control:disabled) {
    background: var(--surface-sunken);
    border-color: var(--border);
    color: var(--text-disabled);
    cursor: not-allowed;
  }
  .input:has(.input__control[readonly]) {
    background: var(--surface-2);
  }
  .input__control {
    flex: 1;
    min-width: 0;
    height: calc(var(--control-h) - 2px);
    padding: 0;
    border: 0;
    background: transparent;
    font-size: inherit;
  }
  .input__control:focus-visible {
    outline: none;
  }
  .input__control::placeholder {
    color: var(--text-subtle);
  }
  .input__control:disabled {
    cursor: not-allowed;
  }
  .input__affix {
    display: inline-flex;
    align-items: center;
    color: var(--text-subtle);
    font-size: var(--control-font);
    white-space: nowrap;
  }
  .input__clear,
  .input__reveal {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    margin-inline-end: -6px;
    border-radius: var(--radius-sm);
    color: var(--text-subtle);
  }
  .input__clear:hover,
  .input__reveal:hover {
    color: var(--text);
    background: var(--surface-active);
  }
  .input__clear:focus-visible,
  .input__reveal:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 0;
  }
  .input__counter {
    font-size: var(--text-xs);
    color: var(--text-subtle);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }
  .input__counter[data-over="true"] {
    color: var(--danger-text);
    font-weight: var(--weight-medium);
  }

  /* Number input with steppers. */
  .input--number {
    padding-inline: 0;
  }
  .input--number .input__control {
    text-align: right;
    padding-inline: var(--space-2);
  }
  .input__step {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    align-self: stretch;
    width: calc(var(--control-h) - 4px);
    color: var(--text-muted);
    border-radius: 0;
  }
  .input__step:first-child {
    border-inline-end: 1px solid var(--border);
    border-start-start-radius: calc(var(--radius-sm) - 1px);
    border-end-start-radius: calc(var(--radius-sm) - 1px);
  }
  .input__step:last-child {
    border-inline-start: 1px solid var(--border);
    border-start-end-radius: calc(var(--radius-sm) - 1px);
    border-end-end-radius: calc(var(--radius-sm) - 1px);
  }
  .input__step:hover:not(:disabled) {
    background: var(--surface-hover);
    color: var(--text);
  }
  .input__step:active:not(:disabled) {
    background: var(--surface-active);
  }
  .input__step:disabled {
    color: var(--text-disabled);
    cursor: not-allowed;
  }
  .input__step:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
  }

  /* Textarea. */
  .input--textarea {
    align-items: stretch;
    padding: 0;
  }
  .input--textarea .input__control {
    height: auto;
    min-height: calc(var(--control-h) * 2.2);
    padding: calc((var(--control-h) - 2px - 1.5em) / 2 + 2px) var(--control-px);
    resize: vertical;
    line-height: var(--leading);
    field-sizing: content;
    max-height: 24em;
  }
  .input--textarea[data-autogrow] .input__control {
    resize: none;
    overflow-y: auto;
  }

  /* ---------- Checkbox & radio (native inputs, custom appearance) ---------- */
  .check {
    display: inline-flex;
    align-items: flex-start;
    gap: var(--space-2-5);
    cursor: pointer;
    font-size: var(--control-font);
    line-height: var(--leading-tight);
    user-select: none;
  }
  .check__input {
    appearance: none;
    flex: none;
    display: grid;
    place-content: center;
    width: var(--check-size);
    height: var(--check-size);
    margin: 0;
    margin-top: calc((1.25em - var(--check-size)) / 2);
    border: 1.5px solid var(--border-input);
    border-radius: 5px;
    background: var(--surface);
    cursor: pointer;
    transition:
      background-color var(--dur-fast) var(--ease),
      border-color var(--dur-fast) var(--ease),
      box-shadow var(--dur-fast) var(--ease);
  }
  .check__input::before {
    content: "";
    width: calc(var(--check-size) * 0.5);
    height: calc(var(--check-size) * 0.28);
    border-left: 2px solid var(--accent-contrast);
    border-bottom: 2px solid var(--accent-contrast);
    transform: translateY(-15%) rotate(-45deg) scale(0);
    transition: transform var(--dur) var(--ease);
  }
  .check__input:hover,
  .check:hover .check__input {
    border-color: var(--border-input-hover);
  }
  .check__input:focus-visible,
  .check__input.is-focus {
    outline: none;
    box-shadow: var(--focus-ring);
  }
  .check__input:checked,
  .check__input:indeterminate {
    background: var(--accent);
    border-color: var(--accent);
  }
  .check__input:checked::before {
    transform: translateY(-15%) rotate(-45deg) scale(1);
  }
  .check__input:indeterminate::before {
    width: calc(var(--check-size) * 0.5);
    height: 0;
    border-left: 0;
    transform: none;
  }
  .check__input[aria-invalid="true"] {
    border-color: var(--danger);
  }
  .check__input--radio {
    border-radius: 50%;
  }
  .check__input--radio::before {
    width: calc(var(--check-size) * 0.4);
    height: calc(var(--check-size) * 0.4);
    border: 0;
    border-radius: 50%;
    background: var(--accent-contrast);
    transform: scale(0);
  }
  .check__input--radio:checked::before {
    transform: scale(1);
  }
  .check:has(.check__input:disabled) {
    cursor: not-allowed;
    color: var(--text-disabled);
  }
  .check__input:disabled {
    cursor: not-allowed;
    background: var(--surface-sunken);
    border-color: var(--border);
  }
  .check__input:disabled:checked {
    background: var(--n-300);
    border-color: var(--n-300);
  }
  .check__text {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding-top: 1px;
  }
  .check__hint {
    color: var(--text-muted);
    font-size: var(--text-xs);
  }
  .check-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2-5);
  }
  .check-group--inline {
    flex-direction: row;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-5);
  }
  .check-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
  }
  .check-list__tools {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
  }
  .check-list__tools[hidden] {
    display: none;
  }
  .check-list__search {
    flex: 1 1 220px;
    max-width: 320px;
  }
  .check-list__count {
    color: var(--text-muted);
    font-size: var(--text-sm);
    font-variant-numeric: tabular-nums;
  }
  .check-list .check[hidden] {
    display: none;
  }
  .check-list .check-group {
    max-height: 420px;
    overflow-y: auto;
    padding: var(--space-1);
  }

  /* ---------- Choice cards (radio with a picture) ---------- */
  .choice-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: var(--space-3);
  }
  .choice-card {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding: var(--space-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface);
    cursor: pointer;
    transition:
      border-color var(--dur) var(--ease),
      box-shadow var(--dur) var(--ease),
      transform var(--dur) var(--ease);
  }
  .choice-card:hover {
    border-color: var(--border-strong);
    transform: translateY(-1px);
  }
  .choice-card:has(.check__input:checked) {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent);
  }
  .choice-card:has(.check__input:focus-visible) {
    box-shadow: var(--focus-ring);
  }
  .choice-card__picture {
    display: grid;
    place-items: center;
    padding: var(--space-2);
    border-radius: var(--radius-md);
    background: var(--surface-sunken);
  }
  .choice-card__image {
    max-width: 100%;
    height: auto;
    box-shadow: var(--shadow-1);
  }
  .choice-card__head {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }
  .choice-card__title {
    font-weight: var(--weight-semibold);
  }
  .choice-card__note {
    color: var(--text-muted);
    font-size: var(--text-sm);
  }

  /* ---------- Switch ---------- */
  .switch {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2-5);
    cursor: pointer;
    font-size: var(--control-font);
    user-select: none;
  }
  .switch__input {
    --sw-h: calc(var(--check-size) + 2px);
    --sw-w: calc(var(--sw-h) * 1.8);
    appearance: none;
    flex: none;
    position: relative;
    width: var(--sw-w);
    height: var(--sw-h);
    margin: 0;
    border-radius: var(--radius-full);
    background: var(--n-300);
    cursor: pointer;
    transition: background-color var(--dur) var(--ease);
  }
  .switch__input::before {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: calc(var(--sw-h) - 4px);
    height: calc(var(--sw-h) - 4px);
    border-radius: 50%;
    background: var(--n-0);
    box-shadow: 0 1px 2px rgb(16 19 24 / 0.25);
    transition: transform var(--dur) var(--ease);
  }
  .switch__input:hover {
    background: var(--n-400);
  }
  .switch__input:checked {
    background: var(--accent);
  }
  .switch__input:checked:hover {
    background: var(--accent-hover);
  }
  .switch__input:checked::before {
    transform: translateX(calc(var(--sw-w) - var(--sw-h)));
  }
  .switch__input:focus-visible,
  .switch__input.is-focus {
    outline: none;
    box-shadow: var(--focus-ring);
  }
  .switch:has(.switch__input:disabled) {
    cursor: not-allowed;
    color: var(--text-disabled);
  }
  .switch__input:disabled {
    cursor: not-allowed;
    opacity: 0.5;
  }

  /* ---------- Segmented control (radio group) ---------- */
  .segmented {
    display: inline-flex;
    padding: 3px;
    gap: 2px;
    border-radius: calc(var(--radius-sm) + 2px);
    background: var(--surface-sunken);
    max-width: 100%;
    overflow-x: auto;
  }
  .segmented__item {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: var(--space-1-5);
    height: calc(var(--control-h) - 6px);
    padding: 0 var(--space-3);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: var(--control-font);
    font-weight: var(--weight-medium);
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    transition:
      background-color var(--dur-fast) var(--ease),
      color var(--dur-fast) var(--ease),
      box-shadow var(--dur-fast) var(--ease);
  }
  .segmented__item:hover {
    color: var(--text);
  }
  .segmented__item:has(input:checked) {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-1), 0 0 0 1px var(--border);
  }
  .segmented__item:has(input:focus-visible) {
    box-shadow: var(--focus-ring);
  }
  .segmented__item:has(input:disabled) {
    color: var(--text-disabled);
    cursor: not-allowed;
  }
  .segmented__item input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
  }
  .segmented--block {
    display: flex;
  }
  .segmented--block .segmented__item {
    flex: 1;
    justify-content: center;
  }
}

@layer components {
  .input__counter--corner {
    position: absolute;
    right: var(--space-2);
    bottom: var(--space-1-5);
    pointer-events: none;
  }
  .input--textarea:has(.input__counter--corner) .input__control {
    padding-bottom: var(--space-6);
  }
  .form-grid > .field--textarea,
  .form-grid > .field--radio,
  .form-grid > .field--checkbox_group {
    grid-column: 1 / -1;
  }
  .form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    justify-content: flex-end;
    margin-top: var(--space-6);
    padding-top: var(--space-4);
    border-top: 1px solid var(--border);
  }
}

@layer components {
  /* ---------- Select / combobox ---------- */
  .select {
    position: relative;
    display: block;
    min-width: 0;
  }
  /* No-JS fallback: the native control, styled like our inputs. Hidden once Alpine takes over. */
  .select__native,
  .tags__native {
    width: 100%;
    min-height: var(--control-h);
    padding-inline: var(--control-px);
    border: 1px solid var(--border-input);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: var(--control-font);
  }
  .select__native[multiple] {
    padding-block: var(--space-1);
  }
  .select[data-ready] .select__native {
    display: none;
  }
  .select__box {
    cursor: pointer;
    padding-inline-end: var(--space-2);
  }
  .select__box:has(.select__input:disabled) {
    cursor: not-allowed;
  }
  .select__input[readonly] {
    cursor: pointer;
    caret-color: transparent;
  }
  .select__box:has(.select__input[readonly]) {
    background: var(--surface);
  }
  .select__chevron,
  .select__spinner {
    display: inline-flex;
    color: var(--text-subtle);
  }
  .select__chevron .icon {
    transition: transform var(--dur) var(--ease);
  }
  .select__box:has([aria-expanded="true"]) .select__chevron .icon {
    transform: rotate(180deg);
  }
  .select--multiple .select__box {
    flex-wrap: wrap;
    gap: var(--space-1);
    padding-block: 3px;
    padding-inline-start: 4px;
  }
  .select--multiple .select__input {
    flex: 1 0 6ch;
    height: calc(var(--control-h) - 8px);
    padding-inline-start: var(--space-1-5);
  }
  .select__chips {
    display: contents;
  }
  .token {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    max-width: 100%;
    height: calc(var(--control-h) - 10px);
    padding-inline: var(--space-2) 2px;
    border-radius: var(--radius-sm);
    background: var(--surface-sunken);
    color: var(--text);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    white-space: nowrap;
    cursor: default;
  }
  .token__label {
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .token__remove {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    border-radius: 4px;
    color: var(--text-subtle);
  }
  .token__remove:hover {
    background: var(--n-200);
    color: var(--text);
  }
  .popover.select__popover {
    max-width: min(480px, calc(100vw - 16px));
  }
  .select__list,
  .select__group-list {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .select__list {
    max-height: min(320px, 50vh);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  .select__option {
    display: flex;
    align-items: center;
    gap: var(--space-2-5);
    min-height: calc(var(--control-h) - 4px);
    padding: var(--space-1-5) var(--space-2-5);
    border-radius: var(--radius-sm);
    cursor: pointer;
    user-select: none;
  }
  .select__option.is-active {
    background: var(--surface-active);
  }
  .select__option[aria-selected="true"] {
    font-weight: var(--weight-medium);
  }
  .select__option[aria-disabled="true"] {
    color: var(--text-disabled);
    cursor: not-allowed;
  }
  .select__option[data-create] {
    color: var(--accent-text);
    font-weight: var(--weight-medium);
  }
  .select__option[data-create] .select__check {
    display: none;
  }
  .select__option-text {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
  }
  .select__label {
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .select__hint {
    color: var(--text-subtle);
    font-size: var(--text-xs);
    font-variant-numeric: tabular-nums;
  }
  .select__check {
    visibility: hidden;
    margin-inline-start: auto;
    color: var(--accent);
  }
  .select__option[aria-selected="true"] .select__check {
    visibility: visible;
  }
  .select__group-label {
    display: block;
    padding: var(--space-2) var(--space-2-5) var(--space-1);
    color: var(--text-subtle);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    letter-spacing: 0.02em;
  }
  .select__status {
    padding: var(--space-2) var(--space-2-5);
    color: var(--text-muted);
    font-size: var(--text-sm);
  }
}

@layer components {
  /* ---------- Date picker / range ---------- */
  .date {
    position: relative;
    display: block;
    min-width: 0;
  }
  .date__box {
    padding-inline-end: var(--space-1);
  }
  .date--range .date__box .input__control {
    flex: 1 1 0;
    min-width: 10ch;
    text-align: center;
  }
  .date__dash {
    color: var(--text-subtle);
  }
  .date__toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: calc(var(--control-h) - 8px);
    height: calc(var(--control-h) - 8px);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
  }
  .date__toggle:hover:not(:disabled) {
    background: var(--surface-active);
    color: var(--text);
  }
  .date__toggle:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 0;
  }
  .date__toggle:disabled {
    color: var(--text-disabled);
    cursor: not-allowed;
  }
  .popover.date__popover {
    display: flex;
    gap: var(--space-3);
    max-width: calc(100vw - 16px);
    max-height: calc(100dvh - 16px);
    overflow-y: auto;
    padding: var(--space-3);
  }
  .date__presets {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 128px;
    padding-inline-end: var(--space-3);
    border-inline-end: 1px solid var(--border);
  }
  .date:not(.date--range) .date__popover {
    flex-direction: column-reverse;
  }
  .date:not(.date--range) .date__presets {
    flex-direction: row;
    padding: var(--space-2) 0 0;
    border: 0;
    border-top: 1px solid var(--border);
  }
  .date__preset {
    padding: var(--space-1-5) var(--space-2-5);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: var(--text-sm);
    text-align: start;
    white-space: nowrap;
  }
  .date__preset:hover {
    background: var(--surface-active);
  }
  .date__preset[aria-pressed="true"] {
    background: var(--accent-soft);
    color: var(--accent-text);
    font-weight: var(--weight-medium);
  }
  .date__preset:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
  }
  @media (max-width: 639px) {
    .date--range .date__popover {
      flex-direction: column-reverse;
    }
    .date--range .date__presets {
      flex-direction: row;
      flex-wrap: wrap;
      padding: var(--space-2) 0 0;
      border: 0;
      border-top: 1px solid var(--border);
    }
  }

  /* Calendar. */
  .cal {
    --cal-cell: 36px;
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
  }
  [data-density="compact"] .cal {
    --cal-cell: 32px;
  }
  .cal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-1);
  }
  .cal__title {
    font-weight: var(--weight-semibold);
    color: var(--text);
  }
  .cal__months {
    display: flex;
    gap: var(--space-5);
  }
  .cal__grid {
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
  }
  .cal__grid th {
    width: var(--cal-cell);
    height: 28px;
    color: var(--text-subtle);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    text-transform: capitalize;
  }
  .cal__grid td {
    padding: 1px 0;
  }
  .cal__grid td.in-range {
    background: var(--accent-soft);
  }
  .cal__grid td.range-start {
    border-start-start-radius: var(--radius-sm);
    border-end-start-radius: var(--radius-sm);
  }
  .cal__grid td.range-end {
    border-start-end-radius: var(--radius-sm);
    border-end-end-radius: var(--radius-sm);
  }
  .cal__day {
    position: relative;
    display: grid;
    place-items: center;
    width: var(--cal-cell);
    height: var(--cal-cell);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: var(--text-sm);
  }
  .cal__day:hover {
    background: var(--surface-active);
  }
  .cal__day:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
  }
  .cal__day.is-today::after {
    content: "";
    position: absolute;
    bottom: 5px;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--accent);
  }
  .cal__day.is-selected {
    background: var(--accent);
    color: var(--accent-contrast);
    font-weight: var(--weight-semibold);
  }
  .cal__day.is-selected::after {
    background: var(--accent-contrast);
  }
  .cal__day.is-disabled {
    color: var(--text-disabled);
    text-decoration: line-through;
    cursor: not-allowed;
    background: none;
  }
  .cal__cells {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--space-1);
    min-width: calc(var(--cal-cell) * 7);
  }
  .cal__cell {
    height: 40px;
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: var(--text-sm);
  }
  .cal__cell:hover {
    background: var(--surface-active);
  }
  .cal__cell:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
  }
  .cal__cell[aria-pressed="true"] {
    background: var(--accent);
    color: var(--accent-contrast);
    font-weight: var(--weight-semibold);
  }
}

@layer components {
  /* ---------- Tags input ---------- */
  .tags__box {
    flex-wrap: wrap;
    gap: var(--space-1);
    padding-block: 3px;
    padding-inline-start: 4px;
    cursor: text;
  }
  .tags__chips {
    display: contents;
  }
  .tags__entry {
    flex: 1 0 12ch;
    height: calc(var(--control-h) - 8px);
    padding-inline-start: var(--space-1-5);
  }
  .token.is-flash {
    animation: ui-flash 600ms var(--ease);
  }
  @keyframes ui-flash {
    30% {
      background: var(--warning-soft);
      box-shadow: 0 0 0 2px var(--warning);
    }
  }
}

@layer components {
  /* ---------- File dropzone ---------- */
  .dropzone {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-width: 0;
  }
  .dropzone__area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1-5);
    padding: var(--space-6) var(--space-4);
    border: 1.5px dashed var(--border-input);
    border-radius: var(--radius-md);
    background: var(--surface-2);
    color: var(--text-muted);
    text-align: center;
    cursor: pointer;
    transition:
      background-color var(--dur-fast) var(--ease),
      border-color var(--dur-fast) var(--ease),
      box-shadow var(--dur-fast) var(--ease);
  }
  .dropzone__area:hover {
    border-color: var(--border-input-hover);
    background: var(--surface-hover);
  }
  .dropzone__area:focus-within {
    border-color: var(--accent);
    box-shadow: var(--focus-ring);
  }
  .dropzone__area[data-over="true"] {
    border-color: var(--accent);
    border-style: solid;
    background: var(--accent-soft);
    color: var(--accent-text);
  }
  .dropzone__area:has(.dropzone__input:disabled) {
    cursor: not-allowed;
    opacity: 0.6;
  }
  .dropzone:has(.dropzone__input[aria-invalid="true"]) .dropzone__area {
    border-color: var(--danger);
  }
  .dropzone__icon {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--surface);
    color: var(--accent);
    box-shadow: var(--shadow-1), 0 0 0 1px var(--border);
  }
  .dropzone__title {
    color: var(--text);
    font-weight: var(--weight-medium);
  }
  .dropzone__link {
    color: var(--accent-text);
    text-decoration: underline;
    text-underline-offset: 2px;
  }
  .dropzone__hint {
    font-size: var(--text-xs);
  }
  .dropzone[data-ready] .dropzone__input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
  }
  .dropzone__current {
    font-size: var(--text-sm);
    color: var(--text-muted);
  }
  .dropzone__files {
    display: flex;
    flex-direction: column;
    gap: var(--space-1-5);
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .dropzone__file {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-2) var(--space-2) var(--space-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
  }
  .dropzone__file[data-state="error"] {
    border-color: color-mix(in srgb, var(--danger) 40%, transparent);
    background: var(--danger-soft);
  }
  .dropzone__file-icon {
    color: var(--text-subtle);
  }
  .dropzone__file[data-state="done"] .dropzone__file-icon {
    color: var(--success);
  }
  .dropzone__file[data-state="error"] .dropzone__file-icon {
    color: var(--danger);
  }
  .dropzone__meta {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: var(--space-1);
    min-width: 0;
  }
  .dropzone__name {
    display: flex;
    gap: var(--space-2);
    min-width: 0;
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
  }
  .dropzone__size {
    flex: none;
    color: var(--text-subtle);
    font-weight: var(--weight-regular);
    font-variant-numeric: tabular-nums;
  }
  .dropzone__note {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }
  .dropzone__file[data-state="error"] .dropzone__note {
    color: var(--danger-text);
    font-weight: var(--weight-medium);
  }
  .dropzone__file[data-state="done"] .dropzone__note {
    color: var(--success-text);
  }
  .dropzone__progress {
    appearance: none;
    width: 100%;
    height: 4px;
    border: 0;
    border-radius: var(--radius-full);
    background: var(--surface-sunken);
    overflow: hidden;
  }
  .dropzone__progress::-webkit-progress-bar {
    background: var(--surface-sunken);
  }
  .dropzone__progress::-webkit-progress-value {
    background: var(--accent);
    transition: width var(--dur) var(--ease);
  }
  .dropzone__progress::-moz-progress-bar {
    background: var(--accent);
  }
}

@layer components {
  .toolbar {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    flex-wrap: wrap;
    min-width: 0;
  }
  .toolbar__separator {
    align-self: stretch;
    width: 1px;
    margin: var(--space-1) var(--space-1);
    background: var(--border);
  }
  .toolbar__spacer {
    flex: 1 1 auto;
  }
}
