.town-field--library {
  overflow: hidden;
}

.library-root {
  position: relative;
  z-index: 2;
  box-sizing: border-box;
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: calc(var(--gs-u-h) * 0.5) calc(var(--gs-u-w) * 0.9) calc(var(--gs-u-h) * 0.6);
  background: rgba(5, 3, 12, 0.86);
}

/* Wallet HUD is absolutely top-right of .town-field; keep the mode bar clear. */
.town-field--library:has(.game-wallet-hud) .game-mode-bar__inner {
  padding-right: clamp(calc(var(--gs-u-w) * 10), calc(var(--gs-u-w) * 20), calc(var(--gs-u-w) * 14));
}

.library-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 0.26fr) minmax(0, 1fr) minmax(0, 0.32fr);
  gap: clamp(calc(var(--gs-u-h) * 0.45), calc(var(--gs-u-w) * 1.2), calc(var(--gs-u-h) * 0.65));
  align-items: stretch;
}

.library-sidebar {
  display: flex;
  flex-direction: column;
  gap: calc(var(--gs-u-h) * 0.45);
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: calc(var(--gs-u-h) * 0.35) calc(var(--gs-u-h) * 0.4);
  border-radius: var(--gs-radius-sm);
  background: rgba(4, 2, 10, 0.3);
}

.library-filter-block {
  min-width: 0;
  border-radius: var(--gs-radius-xs);
  padding: calc(var(--gs-u-h) * 0.35);
}

.library-filter-block__title {
  margin: 0 0 calc(var(--gs-u-h) * 0.3);
  font-size: var(--gs-text-xs);
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--gs-text-nav-hover);
}

.library-icon-filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(calc(var(--gs-u-w) * 2), 1fr));
  gap: calc(var(--gs-u-h) * 0.24);
}

.library-icon-filter {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: calc(var(--gs-u-h) * 2);
  border-radius: var(--gs-radius-xs);
  border: 1px solid rgba(255, 210, 150, 0.28);
  background: rgba(255, 195, 120, 0.1);
}

.library-icon-filter:hover {
  border-color: rgba(255, 215, 160, 0.5);
  background: rgba(255, 195, 120, 0.18);
}

.library-icon-filter--excluded {
  border-color: rgba(140, 130, 120, 0.2);
  background: rgba(20, 18, 26, 0.8);
}

/* Icon (img/svg) carries the "off" treatment: grayscale + reduced opacity.
   Applied to the inner img/svg only so the surrounding tile keeps full chrome
   and the badge stays clearly visible (just desaturated). The previous
   `brightness(0.72)` darkened already-dark race emblems on a dark background
   to the point of invisibility, which read as “the filter disappeared”. */
.library-icon-filter--excluded .library-icon-filter__img,
.library-icon-filter--excluded .library-icon-filter__svg {
  filter: grayscale(1);
  opacity: 0.55;
}

.library-icon-filter__img {
  display: block;
  width: calc(var(--gs-u-w) * 1.4);
  height: calc(var(--gs-u-h) * 1.4);
  object-fit: contain;
}

.library-icon-filter__svg {
  display: block;
  width: calc(var(--gs-u-w) * 1.4);
  height: calc(var(--gs-u-h) * 1.4);
  color: #f3e7d3;
}

/* Pool tabs on Army Roster: styled here so we never load formation-builder.css on this page —
   its .library-icon-filter rules fight card-library and make excluded damage icons vanish. */
.library-root .formation-filters__row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: calc(var(--gs-u-h) * 0.35);
  margin-bottom: calc(var(--gs-u-h) * 0.35);
}

.library-root .formation-filters__row--sub {
  margin-left: 0;
  padding-left: calc(var(--gs-u-w) * 0.35);
  border-left: 2px solid rgba(255, 200, 120, 0.2);
}

.library-root .formation-filter-link {
  display: inline-flex;
  align-items: center;
  padding: calc(var(--gs-u-h) * 0.26) calc(var(--gs-u-h) * 0.48);
  border-radius: var(--gs-radius-xs);
  font-size: var(--gs-text-xs);
  color: rgba(240, 228, 210, 0.92);
  text-decoration: none;
  border: 1px solid transparent;
}

.library-root .formation-filter-link:hover {
  background: rgba(255, 200, 120, 0.08);
  color: var(--gs-text-nav-hover);
}

.library-root .formation-filter-link--current {
  background: rgba(255, 200, 120, 0.14);
  border-color: rgba(255, 210, 150, 0.35);
  color: #fff4e0;
  font-weight: 700;
}

.library-center {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  padding: calc(var(--gs-u-h) * 0.35);
  border-radius: var(--gs-radius-sm);
  background: rgba(4, 2, 10, 0.24);
  display: flex;
  flex-direction: column;
}

/* Filter toggles (sidebar / legacy library). Army spellbook: left green icon ribbons in army-catalog-book.css */
.library-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--gs-u-h) * 0.3) calc(var(--gs-u-h) * 0.55);
  border-radius: var(--gs-radius-sm);
  border: var(--gs-hairline) solid rgba(255, 200, 140, 0.2);
  background: rgba(6, 4, 12, 0.65);
  color: #d4c4a4;
  font-size: calc(var(--gs-u-h) * 2.02);
  font-weight: 700;
  text-decoration: none;
  gap: calc(var(--gs-u-h) * 0.25);
}

.library-toggle__icon {
  font-size: calc(var(--gs-u-h) * 1.75);
}

.library-toggle--sidebar {
  width: 100%;
  box-sizing: border-box;
  margin-top: calc(var(--gs-u-h) * 0.12);
}

.library-toggle:hover {
  border-color: rgba(255, 200, 120, 0.42);
  color: var(--gs-text-nav-hover);
}

.library-toggle--on {
  border-color: rgba(255, 190, 100, 0.55);
  background: rgba(255, 170, 80, 0.12);
  color: var(--gs-text-nav-hover);
}

.library-grid-wrap {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  padding: calc(var(--gs-u-h) * 0.45) calc(var(--gs-u-h) * 0.1) calc(var(--gs-u-h) * 0.2);
  box-sizing: border-box;
}

.library-grid {
  display: grid;
  /* Exactly 6 columns so each page (LIBRARY_PAGE_SIZE = 18) is always 3 rows. */
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: clamp(calc(var(--gs-u-h) * 0.35), calc(var(--gs-u-w) * 1.05), calc(var(--gs-u-h) * 0.55));
  align-items: start;
  min-height: 100%;
  align-content: start;
}

.library-tile-shell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: calc(var(--gs-u-h) * 0.22);
  min-width: 0;
}

/* Library tile is navigable, but card body itself is not an action target. */
[data-library-href] .game-card,
[data-library-href] .game-card * {
  cursor: default;
}
[data-library-href] .game-card .game-card__full-view,
[data-library-href] .game-card .game-card__full-view * {
  cursor: pointer;
}

.library-pagination {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: calc(var(--gs-u-h) * 0.5) calc(var(--gs-u-w) * 0.75);
  padding: calc(var(--gs-u-h) * 0.4) calc(var(--gs-u-h) * 0.25) calc(var(--gs-u-h) * 0.15);
  border-top: 1px solid rgba(255, 200, 140, 0.1);
}

.library-pagination__status {
  font-size: var(--gs-text-xs);
  font-weight: 600;
  color: rgba(235, 220, 200, 0.88);
  font-variant-numeric: tabular-nums;
}

.library-pagination__btn--disabled {
  opacity: 0.38;
  pointer-events: none;
  font-size: var(--gs-text-xs);
  padding: calc(var(--gs-u-h) * 0.32) calc(var(--gs-u-h) * 0.65);
  border-radius: var(--gs-radius-xs);
  border: 1px solid rgba(255, 200, 120, 0.15);
  color: rgba(220, 200, 180, 0.5);
}

.library-empty {
  margin: calc(var(--gs-u-h) * 2) auto;
  max-width: 28ch;
  text-align: center;
  font-size: var(--gs-text-sm);
  color: rgba(220, 200, 175, 0.75);
}

.library-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  aspect-ratio: 5 / 7;
  border-radius: var(--gs-radius-xs);
  overflow: hidden;
  text-decoration: none;
  color: inherit;
  border: 1px solid rgba(255, 200, 140, 0.14);
  background: var(
    --card-race-surface,
    linear-gradient(165deg, #2a2238 0%, #120c18 100%)
  );
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.45);
}

.library-tile--selected {
  outline: 2px solid rgba(255, 200, 120, 0.65);
  outline-offset: 2px;
}

.library-tile--locked {
  filter: grayscale(0.85) brightness(0.55);
}

.library-tile--unowned {
  opacity: 0.72;
  filter: grayscale(0.65) saturate(0.35) brightness(0.85);
}

.library-tile__headline {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: calc(var(--gs-u-h) * 0.2);
  padding: calc(var(--gs-u-h) * 0.28) calc(var(--gs-u-h) * 0.35) calc(var(--gs-u-h) * 0.12);
  min-height: calc(var(--gs-u-h) * 1.65);
  box-sizing: border-box;
}

.library-tile .game-card__race--badge .game-card__race-badge {
  height: calc(var(--gs-u-h) * 1.2);
  max-height: none;
  max-width: calc(var(--gs-u-w) * 2.1);
  min-height: calc(var(--gs-u-h) * 1);
}

.library-tile__name {
  flex: 1 1 auto;
  min-width: 0;
  font-size: var(--gs-text-2xs);
  font-weight: 800;
  line-height: 1.08;
  text-align: center;
  text-shadow: 0 1px 6px rgba(0, 0, 0, 0.9);
  color: #f5ebe0;
  overflow: hidden;
  display: -webkit-box;
  line-clamp: 2;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.library-tile__cd-wrap {
  flex: 0 0 calc(var(--gs-u-w) * 1.15);
  width: calc(var(--gs-u-w) * 1.15);
  height: calc(var(--gs-u-h) * 1.15);
  display: grid;
  place-items: center;
}

.library-tile__art {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0 calc(var(--gs-u-h) * 0.45) calc(var(--gs-u-h) * 0.25);
  border-radius: var(--gs-radius-xs);
  overflow: hidden;
  border: 1px solid rgba(255, 200, 140, 0.1);
}

.library-tile__art img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 18%;
  display: block;
}

.library-tile__desc {
  flex-shrink: 0;
  margin: 0 calc(var(--gs-u-h) * 0.42) calc(var(--gs-u-h) * 0.22);
  padding: calc(var(--gs-u-h) * 0.18) calc(var(--gs-u-h) * 0.24);
  border-radius: var(--gs-radius-xs);
  border: 1px solid rgba(255, 200, 140, 0.1);
  background: rgba(4, 2, 10, 0.48);
  color: rgba(235, 222, 205, 0.9);
  font-size: var(--gs-text-2xs);
  line-height: 1.15;
  max-height: calc(var(--gs-u-h) * 1.55);
  overflow: hidden;
}
.library-tile__desc p {
  margin: 0;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
}
.library-tile__desc br {
  display: none;
}

.library-tile__cd {
  width: calc(var(--gs-u-w) * 1.2);
  height: calc(var(--gs-u-h) * 1.2);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: var(--gs-text-2xs);
  font-weight: 800;
  color: #ffd78a;
  background: radial-gradient(circle at 35% 28%, #4f3a68 0%, #1a1224 72%);
  border: 1px solid rgba(255, 190, 120, 0.35);
}

.library-tile__stat-row {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: calc(var(--gs-u-h) * 0.12);
  width: 100%;
  padding: calc(var(--gs-u-h) * 0.15) calc(var(--gs-u-h) * 0.35) calc(var(--gs-u-h) * 0.32);
  box-sizing: border-box;
}

.library-tile__stat {
  min-width: 0;
  font-size: var(--gs-text-2xs);
  font-weight: 800;
  line-height: 1;
}

.library-tile__stat--atk {
  justify-self: start;
  color: #e2eaf5;
  text-shadow: 0 0 6px rgba(0, 0, 0, 0.8);
}

.library-tile__stat--hp {
  justify-self: end;
  text-align: right;
  color: #f0b0b0;
  text-shadow: 0 0 6px rgba(0, 0, 0, 0.8);
}

.library-tile__stat-value {
  display: inline-block;
}

.library-tile__stars {
  justify-self: center;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 0;
  max-width: 100%;
  font-size: var(--gs-text-2xs);
  line-height: 1;
  color: #e8eef7;
  text-shadow: 0 0 4px rgba(200, 210, 230, 0.6);
}

.library-tile__star {
  display: inline-block;
}

.library-tile__stars--gold {
  color: #ffd76a;
  text-shadow: 0 0 4px rgba(255, 200, 90, 0.5);
}

.library-tile__copies {
  font-size: var(--gs-text-2xs);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: rgba(240, 228, 210, 0.88);
  line-height: 1.2;
  text-align: center;
  width: 100%;
}

.library-tile__lock {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.library-tile__lock svg {
  width: calc(var(--gs-u-w) * 1.5);
  height: calc(var(--gs-u-h) * 1.5);
  color: rgba(255, 220, 160, 0.9);
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.8));
}

.library-detail {
  min-height: 0;
  overflow: auto;
  padding: calc(var(--gs-u-h) * 0.45) calc(var(--gs-u-h) * 0.35);
  border-radius: var(--gs-radius-sm);
  background: rgba(4, 2, 10, 0.3);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: calc(var(--gs-u-h) * 0.5);
}

.library-detail__placeholder {
  margin: auto calc(var(--gs-u-h) * 0.5);
  text-align: center;
  font-size: var(--gs-text-xs);
  color: rgba(220, 200, 175, 0.72);
}

/* Detail column host for exactly one card. */
.library-detail > .library-detail-card {
  width: 100%;
}

/* Only the detail column card root, not dialog innards. */
.library-detail > .library-detail-card > .game-card-sizing {
  width: 100%;
  --game-card-width: 100%;
}

/* GameCard grid tile (6×3 library). Army spellbook 3×3: army-catalog-book.css */
.library-game-tile {
  position: relative;
  display: block;
  width: 100%;
  min-width: 0;
  cursor: pointer;
}

/* Direct child only: grid tile root `.game-card-sizing`, not dialog innards. */
.library-game-tile > .game-card-sizing {
  /* Slight shrink-to-fit so 3 rows don't clip on shorter viewports. */
  width: 96.5%;
  margin: 0 auto;
  --game-card-width: 100%;
}

.library-game-tile--selected .game-card {
  outline: 2px solid rgba(255, 200, 120, 0.65);
  outline-offset: 2px;
}

.library-game-tile--locked .game-card {
  filter: grayscale(0.85) brightness(0.55);
}

.library-game-tile--unowned .game-card {
  opacity: 0.72;
  filter: grayscale(0.65) saturate(0.35) brightness(0.85);
}

.library-game-tile:focus-visible {
  outline: none;
}

.library-game-tile:focus-visible .game-card {
  outline: 2px solid var(--gs-focus-outline);
  outline-offset: 2px;
}
