/* Library filter sidebar and sort row patch. Uses existing theme tokens only. */
.library-with-filters {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 22px;
  align-items: start;
  margin-top: 18px;
}

.library-filter-panel {
  display: grid;
  gap: 16px;
  min-width: 0;
  padding: 18px 14px;
  border: 1px solid var(--panel-border);
  border-radius: 22px;
  background: color-mix(in srgb, var(--surface-panel) 72%, transparent);
  box-shadow: 0 16px 30px rgba(0, 0, 0, 0.12);
}

.library-filter-head,
.library-filter-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.library-filter-title,
.library-filter-section-title {
  margin: 0;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.library-filter-reset {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
}

.library-filter-reset:hover,
.library-filter-reset:focus-visible {
  color: var(--text);
  outline: none;
}

.library-filter-list,
.library-quick-filter-list {
  display: grid;
  gap: 8px;
}

.library-filter-field {
  position: relative;
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) 18px;
  align-items: center;
  min-height: 62px;
  padding: 0 13px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--surface-panel);
  color: var(--text);
  transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.library-filter-field:focus-within,
.library-filter-menu[open] > .library-filter-field {
  border-color: color-mix(in srgb, var(--accent) 42%, var(--panel-border) 58%);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
}

.library-filter-icon,
.library-quick-icon {
  display: grid;
  place-items: center;
  color: var(--muted);
}

.library-filter-icon svg,
.library-quick-icon svg {
  width: 15px;
  height: 15px;
}

.library-filter-copy {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.library-filter-label {
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 700;
  line-height: 1.15;
}

.library-filter-value {
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-filter-select {
  position: absolute;
  inset: 0;
  width: 100%;
  min-height: 100%;
  padding: 0;
  border: 0;
  border-radius: inherit;
  opacity: 0;
  cursor: pointer;
}

.library-filter-caret {
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
}

.library-filter-menu,
.library-sort-menu,
.library-page-size-menu {
  position: relative;
}

.library-filter-menu[open],
.library-sort-menu[open],
.library-page-size-menu[open] {
  z-index: 40;
}

.library-filter-menu > summary,
.library-sort-menu > summary,
.library-page-size-menu > summary {
  list-style: none;
  cursor: pointer;
}

.library-filter-menu > summary::-webkit-details-marker,
.library-sort-menu > summary::-webkit-details-marker,
.library-page-size-menu > summary::-webkit-details-marker {
  display: none;
}

.library-filter-options,
.library-sort-options,
.library-page-size-options {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: 0;
  z-index: 60;
  display: grid;
  gap: 4px;
  max-height: 250px;
  overflow: auto;
  padding: 6px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: var(--surface-panel);
  box-shadow: 0 18px 34px rgba(0, 0, 0, 0.24);
}

.library-filter-option,
.library-sort-option,
.library-page-size-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  min-height: 36px;
  padding: 8px 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

.library-filter-option:hover,
.library-filter-option:focus-visible,
.library-filter-option.is-active,
.library-sort-option:hover,
.library-sort-option:focus-visible,
.library-sort-option.is-active,
.library-page-size-option:hover,
.library-page-size-option:focus-visible,
.library-page-size-option.is-active {
  background: color-mix(in srgb, var(--accent-soft) 42%, var(--surface-panel) 58%);
  outline: none;
}

.library-filter-option {
  display: grid;
  grid-template-columns: 16px minmax(0, 1fr);
  justify-content: initial;
}

.library-filter-option-checkbox {
  display: grid;
  width: 16px;
  height: 16px;
  place-items: center;
  border: 1px solid color-mix(in srgb, var(--muted) 56%, transparent);
  border-radius: 5px;
  color: var(--surface-panel);
  background: transparent;
}

.library-filter-option-checkbox svg {
  display: block;
  width: 12px;
  height: 12px;
}

.library-filter-option-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-filter-option.is-active .library-filter-option-checkbox {
  border-color: var(--accent);
  background: var(--accent);
}

.library-favorite-toggle {
  grid-template-columns: 26px minmax(0, 1fr) auto;
  cursor: pointer;
}

.library-favorite-toggle[aria-pressed="true"] .library-toggle-track {
  background: color-mix(in srgb, var(--accent) 58%, var(--panel-border) 42%);
}

.library-favorite-toggle[aria-pressed="true"] .library-toggle-track::after {
  transform: translateX(14px);
}

.library-favorite-checkbox {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.library-toggle-track {
  position: relative;
  width: 38px;
  height: 24px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel-border) 82%, var(--surface-panel) 18%);
  transition: background 160ms ease;
}

.library-toggle-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface-panel);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.18);
  transition: transform 160ms ease;
}

.library-favorite-checkbox:checked + .library-toggle-track {
  background: color-mix(in srgb, var(--accent) 58%, var(--panel-border) 42%);
}

.library-favorite-checkbox:checked + .library-toggle-track::after {
  transform: translateX(14px);
}

.library-quick-filter {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: center;
  justify-items: start;
  min-height: 48px;
  padding: 0 13px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--surface-panel);
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

.library-quick-filter span {
  min-width: 0;
}

.library-quick-filter:hover,
.library-quick-filter:focus-visible,
.library-quick-filter.is-active {
  border-color: color-mix(in srgb, var(--accent) 42%, var(--panel-border) 58%);
  background: color-mix(in srgb, var(--accent-soft) 42%, var(--surface-panel) 58%);
  outline: none;
}

.library-results-shell {
  min-width: 0;
}

.library-sort-row {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  margin: 0 0 16px;
}

.library-sort-menu > summary {
  min-height: 42px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  background: var(--surface-panel);
  color: var(--text);
  font-weight: 700;
}

.library-sort-menu {
  min-width: min(320px, 100%);
}

.library-sort-options {
  left: auto;
  width: min(270px, 80vw);
}

.library-sort-control {
  position: relative;
  min-width: min(320px, 100%);
}

.library-sort-control select {
  min-height: 42px;
  padding: 0 42px 0 16px;
  border-radius: 12px;
  background: var(--surface-panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
  appearance: none;
}

.library-sort-control::after {
  content: "⌄";
  position: absolute;
  right: 16px;
  top: 50%;
  color: var(--muted);
  transform: translateY(-54%);
  pointer-events: none;
}

.library-empty-results {
  display: grid;
  place-items: center;
  min-height: 220px;
  padding: 28px;
  border: 1px dashed var(--panel-border);
  border-radius: 18px;
  background: color-mix(in srgb, var(--surface-panel) 70%, transparent);
  color: var(--muted);
  text-align: center;
}

.library-empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  min-height: 220px;
  padding: 40px 28px;
  border: 1px dashed var(--panel-border);
  border-radius: 18px;
  background: color-mix(in srgb, var(--surface-panel) 70%, transparent);
  text-align: center;
}

.library-empty-state-text {
  margin: 0;
  color: var(--muted);
  font-size: 1rem;
  font-weight: 600;
}

.library-empty-state-btn {
  min-width: 200px;
}

.library-pagination-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--panel-border);
}

.library-pagination-count {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.library-pagination-controls,
.library-pagination-pages {
  display: flex;
  align-items: center;
  gap: 8px;
}

.library-pagination-button,
.library-pagination-ellipsis,
.library-page-size-menu > summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--panel-border);
  border-radius: 9px;
  background: var(--surface-panel);
  color: var(--text);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 800;
}

.library-pagination-button {
  cursor: pointer;
}

.library-pagination-button:hover,
.library-pagination-button:focus-visible,
.library-pagination-button.is-active {
  border-color: color-mix(in srgb, var(--accent) 46%, var(--panel-border) 54%);
  background: color-mix(in srgb, var(--accent-soft) 58%, var(--surface-panel) 42%);
  color: var(--accent);
  outline: none;
}

.library-pagination-button:disabled {
  cursor: default;
  opacity: 0.48;
}

.library-pagination-ellipsis {
  color: var(--muted);
}

.library-page-size-menu > summary {
  min-width: 150px;
  gap: 8px;
  padding: 0 14px;
  justify-content: space-between;
  white-space: nowrap;
}

.library-page-size-options {
  left: auto;
  min-width: 170px;
}

body[data-theme="light"][data-page="library"] .library-filter-panel,
body[data-theme="light"][data-page="library"] .library-filter-field,
body[data-theme="light"][data-page="library"] .library-quick-filter,
body[data-theme="light"][data-page="library"] .library-sort-control select,
body[data-theme="light"][data-page="library"] .library-sort-menu > summary,
body[data-theme="light"][data-page="library"] .library-filter-options,
body[data-theme="light"][data-page="library"] .library-sort-options,
body[data-theme="light"][data-page="library"] .library-page-size-menu > summary,
body[data-theme="light"][data-page="library"] .library-page-size-options,
body[data-theme="light"][data-page="library"] .library-pagination-button,
body[data-theme="light"][data-page="library"] .library-pagination-ellipsis,
body[data-theme="light"][data-page="library"] .library-empty-results,
body[data-theme="light"][data-page="library"] .library-empty-state {
  background: var(--bg-card);
  border-color: var(--border-color);
  box-shadow: 0 8px 18px rgba(84, 64, 40, 0.05);
}

body[data-theme="light"][data-page="library"] .library-quick-filter:hover,
body[data-theme="light"][data-page="library"] .library-quick-filter:focus-visible,
body[data-theme="light"][data-page="library"] .library-quick-filter.is-active,
body[data-theme="light"][data-page="library"] .library-filter-option:hover,
body[data-theme="light"][data-page="library"] .library-filter-option:focus-visible,
body[data-theme="light"][data-page="library"] .library-filter-option.is-active,
body[data-theme="light"][data-page="library"] .library-sort-option:hover,
body[data-theme="light"][data-page="library"] .library-sort-option:focus-visible,
body[data-theme="light"][data-page="library"] .library-sort-option.is-active,
body[data-theme="light"][data-page="library"] .library-page-size-option:hover,
body[data-theme="light"][data-page="library"] .library-page-size-option:focus-visible,
body[data-theme="light"][data-page="library"] .library-page-size-option.is-active,
body[data-theme="light"][data-page="library"] .library-pagination-button:hover,
body[data-theme="light"][data-page="library"] .library-pagination-button:focus-visible,
body[data-theme="light"][data-page="library"] .library-pagination-button.is-active {
  background: color-mix(in srgb, var(--accent-soft) 48%, var(--bg-card) 52%);
}

@media (max-width: 980px) {
  .library-with-filters {
    grid-template-columns: 1fr;
  }

  .library-filter-panel {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .library-with-filters {
    gap: 16px;
    margin-top: 14px;
  }

  .library-filter-panel {
    padding: 14px 12px;
    border-radius: 18px;
  }

  .library-filter-list {
    gap: 7px;
  }

  .library-filter-field {
    min-height: 58px;
  }

  .library-sort-row {
    justify-content: stretch;
  }

  .library-sort-menu,
  .library-sort-control {
    width: 100%;
  }

  .library-pagination-footer {
    align-items: stretch;
    flex-direction: column;
  }

  .library-pagination-controls {
    align-items: flex-start;
    flex-direction: column;
  }

  .library-pagination-pages {
    flex-wrap: wrap;
  }

  .library-page-size-menu,
  .library-page-size-menu > summary {
    width: 100%;
  }
}
