/* ==========================================================================
   Toolbar — page controls above the library content
   ========================================================================== */

.library-page {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

.library-page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

/* ---- Toolbar row -------------------------------------------------------- */
.controls-toolbar {
  position: sticky;
  top: 0;
  z-index: 12;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
  padding: var(--space-2) 0 var(--space-3);
  background: color-mix(in srgb, var(--color-page) 88%, transparent);
  backdrop-filter: blur(8px);
}

.toolbar-group {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.toolbar-spacer {
  flex: 1 1 auto;
}

.toolbar-divider {
  width: 1px;
  height: 22px;
  background: var(--color-border);
  margin: 0 var(--space-1);
}

.toolbar-search {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1 1 220px;
  max-width: 340px;
  min-width: 0;
}

.toolbar-search svg {
  position: absolute;
  left: 10px;
  width: 15px;
  height: 15px;
  color: var(--color-text-muted);
  pointer-events: none;
}

.toolbar-search .input {
  padding-left: 32px;
  height: 34px;
}

.toolbar-sort {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

.toolbar-sort .select {
  height: 34px;
  width: auto;
  min-width: 132px;
  font-size: var(--app-text-sm);
}

.toolbar-order {
  width: 34px;
  height: 34px;
}

/* Soft filters button */
.toolbar-filters[aria-expanded="true"] {
  background: var(--color-active-surface);
  border-color: color-mix(in srgb, var(--color-primary) 30%, transparent);
  color: var(--color-primary-text);
}

.filter-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: var(--radius-pill);
  background: var(--color-primary);
  color: var(--color-on-primary);
  font-size: 0.5625rem;
  font-weight: 700;
  line-height: 1;
}

/* ---- Filter popover ----------------------------------------------------- */
.filter-popover {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 60;
  width: min(340px, calc(100vw - 32px));
  padding: var(--space-4);
  border-radius: var(--radius-lg);
  background: var(--color-card);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-pop);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.filter-popover[hidden] {
  display: none;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.filter-group-title {
  font-size: var(--app-text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-text-secondary);
}

.filter-options {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.filter-option {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--color-border);
  background: var(--color-card);
  font-size: var(--app-text-sm);
  color: var(--color-text-secondary);
  transition: all var(--duration-micro);
}

.filter-option:hover {
  border-color: color-mix(in srgb, var(--color-primary) 40%, var(--color-border));
  color: var(--color-text);
}

.filter-option.is-active {
  background: var(--color-active-surface);
  border-color: color-mix(in srgb, var(--color-primary) 40%, transparent);
  color: var(--color-primary-text);
  font-weight: var(--weight-medium);
}

.filter-popover-footer {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border);
}

.tag-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 150px;
  overflow-y: auto;
}

/* ---- Active chips ------------------------------------------------------- */
.active-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.active-filters:empty {
  display: none;
}

.active-filters-label {
  font-size: var(--app-text-xs);
  font-weight: var(--weight-medium);
  color: var(--color-text-muted);
}

/* ---- Selection bar ------------------------------------------------------ */
.selection-bar {
  display: none;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
  width: 100%;
  padding: var(--space-2) var(--space-3);
  margin-bottom: var(--space-3);
  border-radius: var(--radius-md);
  background: var(--color-active-surface);
  border: 1px solid color-mix(in srgb, var(--color-primary) 30%, transparent);
}

.selection-bar.is-visible {
  display: flex;
}

.selection-bar-count {
  font-size: var(--app-text-sm);
  font-weight: var(--weight-semibold);
  color: var(--color-primary-text);
}

/* ---- Empty / error states ---------------------------------------------- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding-block: 4rem;
  text-align: center;
  color: var(--color-text-muted);
}

.empty-state-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--color-text) 6%, transparent);
  color: var(--color-text-muted);
}

.empty-state-icon svg {
  width: 26px;
  height: 26px;
}

.empty-state-title {
  font-size: var(--app-text-base);
  font-weight: var(--weight-medium);
  color: var(--color-text-secondary);
}

.empty-state-desc {
  font-size: var(--app-text-sm);
  max-width: 42ch;
}

.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  padding-block: 3rem;
  text-align: center;
  color: var(--color-text-secondary);
}

/* ---- Load more ---------------------------------------------------------- */
.load-more-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-6) 0 var(--space-4);
}

.load-more-sentinel {
  height: 1px;
}

.result-count {
  font-size: var(--app-text-xs);
  color: var(--color-text-muted);
}

/* ---- Table mode --------------------------------------------------------- */
.book-table-wrap {
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  background: var(--color-card);
  overflow: hidden;
}

.book-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--app-text-sm);
}

.book-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  text-align: left;
  font-size: var(--app-text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--color-text-secondary);
  background: var(--color-card);
  padding: 10px var(--space-3);
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

.book-table tbody td {
  padding: 10px var(--space-3);
  border-bottom: 1px solid color-mix(in srgb, var(--color-border) 70%, transparent);
  vertical-align: middle;
  color: var(--color-text-secondary);
}

.book-table tbody tr {
  cursor: pointer;
  transition: background var(--duration-micro);
}

.book-table tbody tr:hover {
  background: var(--color-surface-hover);
}

.book-table tbody tr:last-child td {
  border-bottom: none;
}

.book-table .cell-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.book-table .cell-thumb {
  width: 32px;
  height: 45px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--color-surface-200);
}

.book-table .cell-title-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.book-table .cell-title-text strong {
  font-weight: var(--weight-medium);
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 320px;
}

.book-table .cell-title-text span {
  font-size: var(--app-text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 320px;
}

.book-table .cell-actions {
  display: flex;
  gap: 2px;
  justify-content: flex-end;
}

.book-table tbody tr.is-selected {
  background: var(--color-active-surface);
}

/* Mobile: hide table, show grid fallback behaviour handled in JS. */
@media (max-width: 720px) {
  .book-table .col-series,
  .book-table .col-size,
  .book-table .col-added {
    display: none;
  }
}
