/*
 * The catalog screen: the collection masthead, the filter rail, and the
 * two views the same list of books can take — a grid of covers and a
 * shelf of spines. Both views render from the same markup (each <li>
 * carries a cover face and a spine face); switching modes toggles one
 * class on the list, which is what lets GSAP Flip animate between them.
 */

/* --------------------------------------------------------------------
   Masthead
   -------------------------------------------------------------------- */

.masthead {
  position: relative;
  overflow: hidden;
  margin-bottom: var(--space-6);
  padding: var(--space-6) var(--space-6) var(--space-5);
  background:
    radial-gradient(120% 140% at 8% -10%, var(--ember-soft) 0%, transparent 55%),
    radial-gradient(100% 120% at 100% 0%, var(--sage-soft) 0%, transparent 50%),
    var(--surface-raised);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-sm);
}

/* A faint run of spines bleeding off the top-right corner. */
.masthead::after {
  content: "";
  position: absolute;
  top: -20%;
  right: -4%;
  width: 18rem;
  height: 160%;
  pointer-events: none;
  opacity: 0.1;
  background: repeating-linear-gradient(
    96deg,
    var(--ink) 0 9px,
    transparent 9px 17px,
    var(--ink) 17px 23px,
    transparent 23px 40px
  );
  mask-image: linear-gradient(to left, black, transparent 72%);
  -webkit-mask-image: linear-gradient(to left, black, transparent 72%);
}

.masthead__inner {
  position: relative;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-5);
}

.masthead h1 {
  font-size: var(--text-3xl);
  font-weight: 600;
  font-variation-settings: "opsz" 120;
}

.masthead .lede { margin-top: var(--space-2); }

@media (max-width: 47.9375rem) {
  .masthead { padding: var(--space-5) var(--space-4); border-radius: var(--radius-lg); }
}

/* --------------------------------------------------------------------
   Filter rail
   -------------------------------------------------------------------- */

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.search-field {
  position: relative;
  flex: 1 1 16rem;
  min-width: 12rem;
}

.search-field .icon {
  position: absolute;
  top: 50%;
  left: 0.85rem;
  transform: translateY(-50%);
  color: var(--ink-subtle);
  pointer-events: none;
}

.search-field input {
  padding-left: 2.5rem;
  padding-right: 3.2rem;
  border-radius: var(--radius-pill);
}

.search-field kbd {
  position: absolute;
  top: 50%;
  right: 0.7rem;
  transform: translateY(-50%);
  padding: 0.1rem 0.4rem;
  background: var(--surface-sunken);
  border: 1px solid var(--hairline-strong);
  border-radius: var(--radius-xs);
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  color: var(--ink-subtle);
  pointer-events: none;
}

.search-field input:not(:placeholder-shown) ~ kbd { opacity: 0; }

.search-field__clear {
  position: absolute;
  top: 50%;
  right: 0.45rem;
  transform: translateY(-50%);
  display: grid;
  place-items: center;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  background: var(--surface-sunken);
  border: 1px solid var(--hairline);
  border-radius: 50%;
  color: var(--ink-muted);
  cursor: pointer;
}

.search-field__clear:hover { color: var(--ink-strong); border-color: var(--hairline-strong); }

.view-switch {
  display: inline-flex;
  padding: 3px;
  background: var(--surface-sunken);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-pill);
}

.view-switch button {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0.35rem 0.85rem;
  background: none;
  border: none;
  border-radius: var(--radius-pill);
  color: var(--ink-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color var(--dur-base) var(--ease-out),
              color var(--dur-base) var(--ease-out),
              box-shadow var(--dur-base) var(--ease-out);
}

.view-switch button[aria-pressed="true"] {
  background: var(--surface-raised);
  color: var(--ink-strong);
  box-shadow: var(--shadow-xs);
}

.filter-groups {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
  padding-bottom: var(--space-5);
  border-bottom: 1px solid var(--hairline);
}

.filter-group {
  display: flex;
  align-items: baseline;
  gap: var(--space-3);
}

.filter-group__label {
  flex-shrink: 0;
  width: 5.75rem;
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
  font-weight: 500;
  letter-spacing: var(--tracking-wider);
  text-transform: uppercase;
  color: var(--ink-subtle);
  padding-top: 0.35rem;
}

@media (max-width: 35.9375rem) {
  .filter-group { flex-direction: column; gap: var(--space-2); }
  .filter-group__label { width: auto; padding-top: 0; }
}

.result-count {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--ink-subtle);
}

/* --------------------------------------------------------------------
   Generated covers
   -------------------------------------------------------------------- */

.cover {
  position: relative;
  display: block;
  aspect-ratio: 2 / 3;
  border-radius: 3px var(--radius-sm) var(--radius-sm) 3px;
  overflow: hidden;
  background: var(--surface-sunken);
  box-shadow:
    var(--shadow-md),
    inset 0 0 0 1px rgba(0, 0, 0, 0.12);
  transition: box-shadow var(--dur-slow) var(--ease-out);
}

.cover__art,
.cover__photo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.cover__photo { object-fit: cover; }

/* When a real cover photo is present, the generated cover art sits
   underneath it, hidden — until the photo fails to load (`.is-broken`,
   set by the `onerror` attribute in `book_cover`), at which point it
   takes over instead of a broken image icon. */
.cover:has(.cover__photo) .cover__art { display: none; }
.cover:has(.cover__photo.is-broken) .cover__art { display: block; }
.cover:has(.cover__photo.is-broken) .cover__photo { display: none; }

/* The inner spine shadow and the gloss sweep are what stop a flat
   rectangle from reading as a flat rectangle. */
.cover::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(90deg,
      rgba(0, 0, 0, 0.34) 0,
      rgba(0, 0, 0, 0.1) 5px,
      rgba(255, 255, 255, 0.12) 8px,
      transparent 16px),
    linear-gradient(105deg,
      rgba(255, 255, 255, 0.22) 0%,
      rgba(255, 255, 255, 0.04) 28%,
      transparent 52%);
}

/* --------------------------------------------------------------------
   The list, in grid mode
   -------------------------------------------------------------------- */

.catalog {
  position: relative; /* GSAP Flip positions items absolutely mid-transition */
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
  gap: var(--space-6) var(--space-5);
  list-style: none;
  margin: 0;
  padding: 0;
}

.book-item { min-width: 0; }

.book-card {
  display: block;
  color: inherit;
  text-decoration: none;
  perspective: 900px;
}

.book-card__cover {
  transition: transform var(--dur-slow) var(--ease-spring);
  transform-style: preserve-3d;
}

.book-card:hover .book-card__cover,
.book-card:focus-visible .book-card__cover {
  transform: translateY(-8px) rotateY(-11deg) rotateX(3deg) scale(1.03);
}

.book-card:hover .cover,
.book-card:focus-visible .cover { box-shadow: var(--shadow-xl); }

.book-card__body { margin-top: var(--space-3); }

.book-card__title {
  font-family: var(--font-display);
  font-size: var(--text-base);
  font-weight: 600;
  line-height: var(--leading-snug);
  letter-spacing: var(--tracking-tight);
  color: var(--ink-strong);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-card:hover .book-card__title { color: var(--ember); }

.book-card__author {
  margin-top: 2px;
  font-size: var(--text-sm);
  color: var(--ink-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.book-card__tail {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-2);
  min-height: 1.35rem;
}

.book-card__spine { display: none; }

/* A filled ember heart on favorites, beside the condition badge. */
.favorite-mark {
  display: inline-flex;
  color: var(--ember);
}

.favorite-mark .icon { fill: currentColor; }

/* --------------------------------------------------------------------
   The same list, in shelf mode
   -------------------------------------------------------------------- */

.catalog.is-shelf {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0;
  row-gap: var(--space-6);
}

.catalog.is-shelf .book-item {
  display: flex;
  align-items: flex-end;
  height: 17.5rem;
  padding-right: 3px;
}

/* Each item paints its own slice of the plank; with no column gap the
   slices butt together into one continuous shelf per row. */
.catalog.is-shelf .book-item::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -14px;
  height: 14px;
  background:
    linear-gradient(180deg,
      color-mix(in srgb, var(--brass) 55%, var(--ink)) 0 3px,
      color-mix(in srgb, var(--brass) 30%, var(--ink)) 3px 9px,
      transparent 9px);
  box-shadow: 0 6px 14px -6px rgba(0, 0, 0, 0.55);
}

.catalog.is-shelf .book-item { position: relative; }

.catalog.is-shelf .book-card {
  display: flex;
  align-items: flex-end;
  height: 100%;
}

.catalog.is-shelf .book-card__cover,
.catalog.is-shelf .book-card__body { display: none; }

.catalog.is-shelf .book-card__spine { display: block; }

.spine {
  position: relative;
  width: var(--spine-width, 2.6rem);
  height: var(--spine-height, 12rem);
  border-radius: 2px 3px 0 0;
  background:
    linear-gradient(90deg,
      rgba(0, 0, 0, 0.35) 0 2px,
      rgba(255, 255, 255, 0.16) 3px 5px,
      transparent 5px calc(100% - 5px),
      rgba(0, 0, 0, 0.28) calc(100% - 3px) 100%),
    var(--spine-color, var(--ink-muted));
  box-shadow: var(--shadow-md);
  color: #fff8ec;
  overflow: hidden;
  transform-origin: bottom center;
  transition: transform var(--dur-base) var(--ease-spring),
              box-shadow var(--dur-base) var(--ease-out);
}

.book-card:hover .spine,
.book-card:focus-visible .spine {
  transform: translateY(-10px) rotate(-2.5deg);
  box-shadow: var(--shadow-lg);
}

/* Brass bands at head and tail, the way a cloth-bound spine is stamped. */
.spine::before,
.spine::after {
  content: "";
  position: absolute;
  left: 4px;
  right: 4px;
  height: 1px;
  background: rgba(255, 248, 236, 0.45);
}

.spine::before { top: 12px; }
.spine::after { bottom: 12px; }

/* On a favorite, a small heart stamped above the head band. */
.spine__favorite {
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  line-height: 0;
}

.spine__favorite .icon {
  width: 9px;
  height: 9px;
  fill: currentColor;
  stroke-width: 1;
}

.spine__title,
.spine__author {
  position: absolute;
  left: 0;
  right: 0;
  writing-mode: vertical-rl;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

.spine__title {
  top: 16px;
  bottom: 36%;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.78rem;
  letter-spacing: 0.01em;
}

.spine__author {
  top: 66%;
  bottom: 16px;
  font-family: var(--font-mono);
  font-size: 0.5rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.82;
}

/* --------------------------------------------------------------------
   Reveal-on-scroll initial state. Only applied once the controller has
   marked the list, so a JS-less page still shows every book.
   -------------------------------------------------------------------- */

.catalog[data-reveal-ready] > .book-item.is-hidden {
  opacity: 0;
}

.book-item.is-filtered-out { display: none; }

/* --------------------------------------------------------------------
   Selection mode (catalog-selection-mode)
   -------------------------------------------------------------------- */

.book-item { position: relative; }

/* The checkbox is a sibling of the card link, laid over whichever face
   is showing: the cover's top-left corner, or the foot of a spine. */
.book-item__select { display: none; }

.catalog.is-selecting .book-item__select {
  display: block;
  position: absolute;
  top: var(--space-2);
  left: var(--space-2);
  z-index: 2;
  width: 1.35rem;
  height: 1.35rem;
  margin: 0;
  accent-color: var(--ember);
  cursor: pointer;
}

.catalog.is-shelf.is-selecting .book-item__select {
  top: auto;
  bottom: var(--space-3);
  left: calc(50% - 1.5px);
  transform: translateX(-50%);
}

.catalog.is-selecting .book-card { cursor: pointer; }

.book-item.is-selected .book-card__cover,
.book-item.is-selected .book-card__spine {
  outline: 3px solid var(--ember);
  outline-offset: 3px;
  border-radius: var(--radius-sm);
}

.selection-toggle[aria-pressed="true"] {
  color: var(--ember);
  border-color: color-mix(in srgb, var(--ember) 45%, transparent);
  background: var(--ember-soft);
}

/* Pinned to the bottom of the viewport while selecting; on narrow
   screens it sits above the fixed tab bar instead of under it. */
.selection-bar {
  position: sticky;
  bottom: var(--space-4);
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3) var(--space-4);
  margin-top: var(--space-5);
  padding: var(--space-3) var(--space-4);
  background: var(--surface-raised);
  border: 1px solid var(--hairline-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md, 0 10px 30px -12px rgba(0, 0, 0, 0.35));
}

.selection-bar[hidden] { display: none; }

.selection-bar__summary { display: flex; flex-direction: column; min-width: 8rem; }
.selection-bar__links { display: flex; flex-wrap: wrap; gap: var(--space-1); }

.selection-bar__assign {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  flex: 1 1 20rem;
  justify-content: flex-end;
}

.selection-bar__assign input[type="text"] { flex: 1 1 10rem; min-width: 0; }

@media (max-width: 47.9375rem) {
  /* Compact on phones: the count, then the two text actions, then the
     genre field full width, then the assign and close buttons. */
  .selection-bar {
    bottom: calc(4.75rem + env(safe-area-inset-bottom));
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
  }
  .selection-bar__summary { min-width: 0; flex: 1 1 auto; }
  .selection-bar__links .btn { padding: var(--space-1) var(--space-2); font-size: var(--text-xs); }
  .selection-bar__assign { flex-basis: 100%; }
  .selection-bar__assign input[type="text"] { flex-basis: 100%; }
  .selection-bar__assign .btn--primary { flex: 1 1 auto; justify-content: center; white-space: nowrap; }
}
