@layer components {
  /* One row under the header at every width: the filter box, then quick filters, in 14px words and 32px controls. */
  .filter-bar {
    align-items: center;
    display: flex;
    font-size: var(--text-control);
    gap: var(--control-gap);
    margin-block-end: var(--block-space);
    padding-block-start: 2px; /* prevents input focus-ring clipping on mobile */
    position: relative;
    view-transition-name: filter-bar;
    z-index: 1;

    .btn:not(.btn--link) {
      --btn-border-color: var(--color-ink-medium);
      --input-background: var(--color-canvas);
    }

    &:has(dialog[open]) {
      z-index: calc(var(--z-nav) + 1);
    }

    .popup-anchor {
      min-inline-size: 0;
    }
  }

  /* On the sheet the list starts a heading's space under the row. */
  .sheet .filter-bar {
    margin-block-end: var(--page-header-space);
  }

  /* A field that says in its own words what it looks through is wide enough to say it. */
  .filter-bar--worded {
    --filter-input-width: calc(var(--filter-field-size) * 1.6);   /* 240px */
  }

  /* The page's main action, first on the row and never squeezed. */
  .filter-bar__actions {
    align-items: center;
    display: flex;
    flex: none;
    gap: var(--control-gap);
  }

  /* A log's row leads with a sentence of menus, "Showing [every site] by [everyone]": the words on a pale band, each menu a button on it. Its filter box is 150 wide, 120 on a phone. */
  .filter-bar:has(.showing) {
    --filter-input-width: 7.5rem;

    gap: 0.57em;                     /* 8px */

    @media (width >= 640px) {
      --filter-input-width: 9.375rem;
    }

    @media (min-width: 100ch) {
      --filter-input-width: 7.9rem;
    }
  }

  .showing {
    align-items: center;
    background-color: var(--color-ink-lightest);
    border-radius: var(--radius-lg);
    box-shadow: inset 0 0 0 1px var(--color-ink-lighter);
    display: flex;
    min-inline-size: 0;
    order: -1;

    .input--select {
      --btn-border-color: var(--tint-ink-25);
      --input-border-color: var(--tint-ink-25);

      box-shadow: 0 1px 2px var(--tint-ink-5);
      font-weight: 600;

      .icon {
        display: none;
      }
    }
  }

  .showing__word {
    color: var(--color-ink-darker);
    padding-inline: 0.75em;
  }

  /* Its menu opens as a small sheet: what it asks in bold, a field to narrow the choices, the chosen one in bold with its tick.
     So do the search's, from its rail. */
  :is(.showing, .search-rail) .popup {
    --panel-padding: 1em 1em 0.75em;      /* 16px, 12px at the foot */
    --panel-size: 14.25em;                /* 228px: "Todas las empresas" whole, in bold */
    --popup-item-padding-inline: 0.5em;

    font-size: 1rem;                      /* 16px */

    @media (min-width: 100ch) {
      font-size: 0.842rem;                /* 16px */
    }

    .popup__title {
      color: var(--color-ink);
      font-size: 1em;
      font-weight: 700;
      padding: 0 0 0.375em;
    }

    .input {
      --input-padding: 0 0.5em;

      block-size: 2em;                    /* 32px */
      font-size: 1em;
      margin-block-end: 0.5em;
    }

    /* A phone keeps the menus' taller row for a finger. */
    @media (width >= 640px) {
      .popup__btn {
        min-block-size: 2.125em;          /* 34px */
      }
    }

    .popup__item[aria-checked="true"] .popup__btn {
      font-weight: 700;
    }
  }

  /* 14px from 800 up; 16px below, where a phone would zoom in on anything smaller. */
  .filter-bar__terms:is(.input) {
    --input-background: var(--color-canvas);
    --input-padding: 0 0.5em;

    block-size: var(--control-size);
    flex: 0 100 var(--filter-input-width, var(--filter-field-size));
    font-size: 1rem;
    min-inline-size: min(8rem, var(--filter-field-size));

    @media (width >= 800px) {
      font-size: var(--text-control);
    }

    &:not(:placeholder-shown) {
      --input-background: var(--color-highlight);
    }

    &::-webkit-search-cancel-button {
      appearance: none;
    }
  }

  /* The filter box. Below 800 its shrink factor dwarfs the filters', so it gives way first, down to its floor, and then the filters ellipsize; from 800 it is at its floor already and only the filters give. */
  .filter-box {
    flex: 0 100 var(--filter-input-width, var(--filter-field-size));
    min-inline-size: min(8rem, var(--filter-field-size));
    position: relative;

    .filter-bar__terms {
      --input-padding: 0 2.7em 0 0.5em;
    }
  }

  /* With quick filters the box leads and the filters follow it, dropping to the next line before their words are cut. */
  .filter-bar:has(.quick-filter) {
    flex-wrap: wrap;

    .quick-filter {
      min-inline-size: 0;
    }
  }

  /* Beside a row of states the box starts narrower and takes the room left, so the toggle stays on the line. */
  @container layout (inline-size >= 640px) {
    .filter-bar:has(> .segments) .filter-box {
      flex: 1 1 10rem;
      max-inline-size: var(--filter-input-width, var(--filter-field-size));
    }
  }

  /* Only a menu set off its default shows, until the toggle expands them all. */
  .filter-bar:not(.filter-bar--expanded) .quick-filter:not([data-filter-show=true]) {
    display: none;
  }

  /* The order's menu reads its choice. Its icon, two arrows turned on end, is for a phone. */
  .quick-filter--sorted-by .input--select .icon {
    --icon-size: 1.1em;

    display: none;
    rotate: 90deg;
  }

  .filter-bar--expanded .filter-toggle {
    --btn-background: var(--color-ink);
    --btn-color: var(--color-canvas);
    --btn-hover-color: black;
    --btn-hover-amount: 8%;
    --focus-ring-color: var(--color-ink);
  }

  .filter-bar__show-when-expanded {
    .filter-bar:not(.filter-bar--expanded) & {
      display: none;
    }
  }

  .filter-bar__show-when-collapsed {
    .filter-bar--expanded & {
      display: none;
    }
  }

  .filter-box__hint,
  .filter-box__clear:is(.btn) {
    inset-block-start: 50%;
    position: absolute;
    translate: 0 -50%;
  }

  .filter-box__hint {
    font-size: var(--text-xx-small);
    inset-inline-end: 0.9rem;
    line-height: 1.2;
    pointer-events: none;
  }

  .filter-box__clear:is(.btn) {
    --btn-background: transparent;
    --btn-border-size: 0;
    --btn-color: var(--color-ink-dark);
    --btn-hover-amount: 8%;
    --btn-size: 1.5rem;

    inset-inline-end: 0.4rem;

    @media (any-hover: hover) {
      &:hover {
        --btn-color: var(--color-ink);
      }
    }
  }

  .filter-bar__terms:is(:focus, :not(:placeholder-shown)) ~ .filter-box__hint,
  .filter-bar__terms:placeholder-shown ~ .filter-box__clear {
    display: none;
  }

  /* Over a calendar the row wraps where it must, its field giving way first. Beside the month's rail the page's
     action goes to the row's far end. On a phone the views, the kinds and the field share the first line, and the
     sites and the action the second. */
  .calendar-layout .filter-bar {
    flex-wrap: wrap;
    row-gap: 8px;

    > .segments {
      flex: none;
      flex-wrap: nowrap;
      order: 0;
    }

    &:has(.calendar-views) .filter-box {
      flex: 1 1 5rem;
      max-inline-size: var(--filter-field-size);
      min-inline-size: 5rem;
    }

    @media (width < 640px) {
      &:has(.calendar-views) .filter-box {
        max-inline-size: none;
      }

      .calendar-rail-menu {
        order: 1;
      }

      .filter-bar__actions {
        margin-inline-start: auto;
        order: 2;
      }

      .segments__item {
        padding-inline: 0.45em;
      }
    }
  }

  .calendar-layout--railed.calendar-layout--month .filter-bar__actions {
    margin-inline-start: auto;
    order: 1;
  }

  .filter__match {
    background-color: var(--color-highlight);
    border-radius: 0.2em;
    color: var(--color-ink);
    padding: 0;
  }

  /* Shown only while a typed query hides every row, and late, so it does not flash between keystrokes. */
  .filter__empty {
    display: none;
  }

  [data-filtering]:not([data-has-match]):has([data-quick-find-target~="item"]:not(.quick-filter *)) > .filter__empty {
    animation: fade-in var(--duration-medium) ease-out 500ms both;
    display: block;
  }

  /* Below 16px, iOS zooms the page when a field takes focus */
  @media (any-hover: none) {
    .filter-bar__terms:is(.input) {
      font-size: max(16px, var(--text-x-small));
    }
  }

  @container layout (inline-size < 640px) {
    .filter-bar:has(.quick-filter) .filter-box {
      flex: 1 1 var(--filter-input-width, var(--filter-field-size));
    }

    /* Beside the page's action the box gives way first, so the action, the box and the toggle share one line. */
    .filter-bar:has(.filter-bar__actions) .filter-box {
      flex-basis: 8rem;
      min-inline-size: 10rem;
    }

    /* A row of states goes under that line. */
    .filter-bar:has(> .segments) {
      flex-wrap: wrap;
    }

    .filter-bar > .segments {
      flex-basis: 100%;
      order: 1;
    }

    .filter-box__hint {
      display: none;
    }

    .showing__word--lead {
      display: none;
    }

    /* Beside the filter box the order's menu is its icon alone, so the row stays on one line; its words stay for a screen reader. A log's menu is too, until it narrows the log. */
    .showing .quick-filter--with-default .input--select,
    .filter-box ~ form .quick-filter--sorted-by .input--select {
      --input-padding: 0;

      background-image: none;
      inline-size: var(--control-size);
      justify-content: center;

      .icon {
        display: inline-block;
      }

      [data-combobox-target="label"] {
        block-size: 1px;
        clip-path: inset(50%);
        inline-size: 1px;
        position: absolute;
      }
    }
  }

  /* Beside a log's menus the box keeps its own width at every size, so this follows the phone's rules for the lists' box. */
  .filter-bar:has(.showing) .filter-box {
    flex: 0 1 var(--filter-input-width);
    min-inline-size: 7.5rem;
  }
}
