/*
 * Filter bar (Programm): format chips, Thema disclosure, "Nur kostenlose" toggle, active filter tags, result count.
 * Desktop: everything in one row, Thema list as popover. Phone (< 1024 px): chip row scrolls past the edge,
 * "Filter (n)" opens the bottom sheet (module filters moves the Thema list and the toggle into it).
 * Without JavaScript the controls stay inline in <details class="filter-more"> and "Filter anwenden" shows.
 */
@layer components {
  .filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: var(--space-12) var(--space-8);
  }

  .filter-bar__chips {
    display: flex;
    align-items: center;
    gap: var(--space-8);
    min-inline-size: 0;
    /* phone: scroll row that runs to the screen edge; the padding keeps focus rings from being clipped */
    inline-size: calc(100% + 2 * var(--layout-margin));
    margin-inline: calc(-1 * var(--layout-margin));
    margin-block: calc(-1 * var(--space-4));
    padding: var(--space-4) var(--layout-margin);
    overflow-x: auto;
    scrollbar-width: none;
    scroll-padding-inline: var(--layout-margin);
  }

  .filter-bar__chips::-webkit-scrollbar {
    display: none;
  }

  .filter-bar__formats {
    display: flex;
    gap: var(--space-8);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .filter-bar__formats > li {
    flex: none;
  }

  /* "Filter (n)" trigger: only with JavaScript on the phone. */
  .filter-bar__open {
    display: none;
    flex: none;
  }

  .filter-bar__open[data-active] {
    background-color: var(--color-action-selected-background);
    border-color: var(--color-action-selected-background);
    color: var(--color-action-selected-text);
  }

  /* Without JavaScript: <details> "Filter" holds the rest of the controls on the phone. */
  .filter-more {
    inline-size: 100%;
  }

  .filter-more__summary {
    display: inline-flex;
    align-items: center;
    gap: var(--space-8);
    min-block-size: var(--component-control-height-s);
    padding: var(--space-8) var(--space-16);
    border: var(--border-width-thin) solid var(--color-border-strong);
    border-radius: var(--radius-pill);
    font-weight: var(--font-weight-bold);
    font-size: var(--type-label-m-size);
    line-height: var(--type-label-m-line-height);
    cursor: pointer;
    list-style: none;
  }

  .filter-more__summary::-webkit-details-marker {
    display: none;
  }

  .filter-more[open] > .filter-more__summary .icon {
    transform: rotate(180deg);
  }

  .filter-more__body {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-12);
    margin-block-start: var(--space-12);
  }

  /* With JavaScript the phone uses the sheet instead. */
  @media (max-width: 1023.98px) {
    .js .filter-more {
      display: none;
    }

    .js .filter-bar__open {
      display: inline-flex;
    }
  }

  .js .filter-bar__apply {
    display: none;
  }

  /* Thema disclosure: a pill like the chips with a popover below. */
  .filter-topics {
    position: relative;
  }

  .filter-topics__summary {
    display: inline-flex;
    align-items: center;
    gap: var(--space-8);
    min-block-size: var(--component-control-height-s);
    padding: var(--space-8) var(--space-16);
    border: var(--border-width-thin) solid var(--color-border-strong);
    border-radius: var(--radius-pill);
    background-color: var(--color-background-default);
    color: var(--color-text-default);
    font-weight: var(--font-weight-bold);
    font-size: var(--type-label-m-size);
    line-height: var(--type-label-m-line-height);
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    list-style: none;
  }

  .filter-topics__summary::-webkit-details-marker {
    display: none;
  }

  .filter-topics__summary:hover {
    background-color: var(--color-background-subtle);
    border-color: var(--color-text-default);
  }

  .filter-topics[open] > .filter-topics__summary {
    border-color: var(--color-text-default);
    border-width: var(--border-width-thin);
    box-shadow: inset 0 0 0 1px var(--color-text-default);
  }

  .filter-topics[open] > .filter-topics__summary .icon {
    transform: rotate(180deg);
  }

  .filter-topics[data-active] > .filter-topics__summary {
    background-color: var(--color-action-selected-background);
    border-color: var(--color-action-selected-background);
    color: var(--color-action-selected-text);
  }

  .filter-topics__panel {
    position: static;
    display: flex;
    flex-direction: column;
    gap: var(--space-16);
    margin-block-start: var(--space-8);
    padding: var(--space-16);
    border: var(--border-width-thin) solid var(--color-text-default);
    border-radius: var(--radius-s);
    background-color: var(--color-background-default);
  }

  .filter-topics__fieldset {
    display: flex;
    flex-direction: column;
    gap: var(--space-12);
    min-inline-size: 0;
    margin: 0;
    padding: 0;
    border: 0;
  }

  .filter-topics__legend {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .filter-topics__foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-16);
  }

  /* "Nur kostenlose": pill with the toggle inside. */
  .filter-toggle .form-field {
    gap: 0;
  }

  .filter-toggle .choice {
    align-items: center;
    gap: var(--space-8);
    min-block-size: var(--component-control-height-s);
    padding: var(--space-4) var(--space-16) var(--space-4) var(--space-8);
    border: var(--border-width-thin) solid var(--color-border-strong);
    border-radius: var(--radius-pill);
    background-color: var(--color-background-default);
    color: var(--color-text-default);
    font-weight: var(--font-weight-bold);
    white-space: nowrap;
  }

  .filter-toggle .choice:hover {
    background-color: var(--color-background-subtle);
    border-color: var(--color-text-default);
  }

  .filter-toggle .choice:has(.toggle:checked) {
    background-color: var(--color-action-selected-background);
    border-color: var(--color-action-selected-background);
    color: var(--color-action-selected-text);
  }

  .filter-toggle .choice:has(.toggle:focus-visible) {
    outline: var(--border-width-focus) solid var(--color-focus);
    outline-offset: 2px;
  }

  .filter-toggle .toggle:focus-visible {
    outline: none;
  }

  .filter-toggle .toggle:checked {
    background-color: transparent;
    border-color: var(--color-text-inverse);
  }

  /* Inside the bottom sheet: plain list, no pills, no popover. */
  .filter-more__body.is-static {
    inline-size: 100%;
    gap: var(--space-16);
    margin: 0;
  }

  .is-static .filter-topics {
    inline-size: 100%;
  }

  .is-static .filter-topics__summary {
    display: none;
  }

  .is-static .filter-topics__panel {
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
  }

  .is-static .filter-topics__fieldset {
    gap: var(--space-16);
  }

  .is-static .filter-topics__legend {
    position: static;
    inline-size: auto;
    block-size: auto;
    margin: 0 0 var(--space-12);
    overflow: visible;
    clip-path: none;
    font-weight: var(--font-weight-bold);
    font-size: var(--type-label-m-size);
    line-height: var(--type-label-m-line-height);
  }

  .is-static .filter-topics__foot,
  .is-static .filter-bar__apply {
    display: none;
  }

  .is-static .filter-toggle {
    inline-size: 100%;
    padding-block-start: var(--space-16);
    border-block-start: var(--border-width-thin) solid var(--color-border-default);
  }

  .is-static .filter-toggle .choice {
    padding: 0;
    border: 0;
    border-radius: 0;
    background: none;
    color: var(--color-text-default);
  }

  .is-static .filter-toggle .choice:has(.toggle:checked) {
    background: none;
    color: var(--color-text-default);
  }

  .is-static .filter-toggle .toggle:checked {
    background-color: var(--color-action-selected-background);
    border-color: var(--color-action-selected-background);
  }

  .is-static .filter-toggle .choice:has(.toggle:focus-visible) {
    outline-offset: 4px;
  }

  /* Active filter tags and the count. */
  .filter-status {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-12) var(--space-16);
    margin-block-start: var(--space-16);
  }

  .filter-status__tags {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-8) var(--space-12);
  }

  .filter-status__label {
    font-weight: var(--font-weight-bold);
    font-size: var(--type-label-s-size);
    line-height: var(--type-label-s-line-height);
  }

  .filter-status__list {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-8);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .filter-tag {
    display: inline-flex;
    align-items: center;
    gap: var(--space-8);
    min-block-size: var(--component-control-height-s);
    padding: var(--space-4) var(--space-12) var(--space-4) var(--space-12);
    border: var(--border-width-thin) solid var(--color-border-strong);
    border-radius: var(--radius-pill);
    background-color: var(--color-background-subtle);
    color: var(--color-text-default);
    font-weight: var(--font-weight-bold);
    font-size: var(--type-label-s-size);
    line-height: var(--type-label-s-line-height);
    text-decoration: none;
  }

  .filter-tag:hover {
    background-color: var(--color-background-muted);
    border-color: var(--color-text-default);
    color: var(--color-text-default);
  }

  .filter-tag .icon {
    flex: none;
  }

  .filter-status__error {
    color: var(--color-feedback-error);
    font-weight: var(--font-weight-semibold);
    font-size: var(--type-text-small-size);
    line-height: var(--type-text-small-line-height);
  }

  .filter-status__count {
    color: var(--color-text-subtle);
    font-size: var(--type-text-small-size);
    line-height: var(--type-text-small-line-height);
  }

  @media (min-width: 1024px) {
    .filter-bar__chips {
      inline-size: auto;
      margin: 0;
      padding: 0;
      overflow: visible;
    }

    .filter-bar__formats {
      flex-wrap: wrap;
    }

    .filter-more {
      inline-size: auto;
    }

    .filter-more__summary {
      display: none;
    }

    .filter-more__body {
      flex-direction: row;
      flex-wrap: wrap;
      align-items: center;
      margin: 0;
      gap: var(--space-8);
    }

    .filter-topics__panel {
      position: absolute;
      z-index: var(--z-popover);
      inset-block-start: 100%;
      inset-inline-start: 0;
      inline-size: 280px;
    }

    .filter-status {
      margin-block-start: var(--space-16);
    }
  }
}
