* {
  box-sizing: border-box;
}

:root {
  --page-bg: #f5f0e8;
  --text-color: #333;
  --group-label-color: #999;
  --label-border: #e0dbd3;
  --empty-color: #bbb;
  --band-gap: 0px;
  --skeleton-a: #e0dbd3;
  --skeleton-b: #ede8e0;
  --detail-bg: #1a1a1a;
  --detail-fg: #fff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page-bg: #171411;
    --text-color: #d8d2c8;
    --group-label-color: #8d8579;
    --label-border: #2c2823;
    --empty-color: #5f594f;
    --skeleton-a: #242019;
    --skeleton-b: #2e2922;
  }
}

body {
  margin: 0;
  padding: 40px 20px;
  background-color: var(--page-bg);
  font-family: 'Inter', system-ui, sans-serif;
  color: var(--text-color);
  display: flex;
  justify-content: center;
  overflow-x: hidden;
}

#app {
  width: 100%;
  max-width: 980px;
}

.shelf-group {
  margin-bottom: 60px;
}

.shelf-group:first-of-type {
  padding-top: 40px;
}

.shelf-group .group-label {
  margin-left: 40px;
  margin-right: 40px;
}

.group-label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--group-label-color);
  margin-bottom: 16px;
  padding: 0 0 12px 28px;
  border-bottom: 1px solid var(--label-border);
}

.books-list {
  display: flex;
  flex-direction: column;
  gap: var(--band-gap);
  margin-bottom: 24px;
  position: relative;
  padding-left: 40px;
  padding-right: 40px;
}

.books-list::after {
  content: '';
  position: absolute;
  bottom: -24px;
  left: 0;
  right: 0;
  width: 100vw;
  margin-left: calc(-50vw + 50%);
  height: 20px;
  background:
    repeating-linear-gradient(
      90deg,
      transparent 0px,
      rgba(0, 0, 0, 0.02) 2px,
      transparent 4px,
      transparent 6px,
      rgba(0, 0, 0, 0.03) 8px,
      transparent 10px
    ),
    repeating-linear-gradient(
      90deg,
      transparent 0px,
      rgba(255, 255, 255, 0.01) 3px,
      transparent 6px
    ),
    linear-gradient(180deg, #8B6914 0%, #6b4e0f 50%, #5a3a0a 100%);
  box-shadow:
    inset 0 1px 2px rgba(255, 255, 255, 0.1),
    inset 0 2px 4px rgba(0, 0, 0, 0.3),
    0 4px 8px rgba(0, 0, 0, 0.2);
}

.books-list.loading {
  opacity: 0.7;
}

.book-band {
  display: flex;
  align-items: center;
  justify-content: var(--align, center);
  width: var(--band-w, 100%);
  /* Page-count thickness is a minimum: a tall wrapped title may grow
     the band instead of overflowing it. */
  height: auto;
  min-height: var(--band-h, 64px);
  padding: 10px 20px;
  background-color: var(--bg, #8B7355);
  /* Subtle spine highlight: light edge on top, soft shade at the bottom. */
  background-image: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.09) 0%,
    rgba(255, 255, 255, 0) 24%,
    rgba(0, 0, 0, 0) 76%,
    rgba(0, 0, 0, 0.08) 100%
  );
  color: var(--fg, #fff);
  font-family: var(--font, 'Playfair Display', serif);
  border: none;
  cursor: pointer;
  transition: filter 0.12s cubic-bezier(0.34, 1.56, 0.64, 1),
              transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1),
              box-shadow 0.12s ease,
              margin-left 0.12s cubic-bezier(0.34, 1.56, 0.64, 1);
  position: relative;
  /* Upper books rest on lower ones: stack order runs top-down so each
     book's contact shadow falls on the book beneath it. */
  z-index: calc(100 - var(--idx, 0));
  border-radius: 2px;
  box-shadow:
    0 1px 0 rgba(0, 0, 0, 0.14),
    0 4px 6px -3px rgba(0, 0, 0, 0.35);
  margin-left: var(--offset, 0px);
  --rot: var(--tilt, 0deg);
  transform: rotate(var(--rot));
}

/* Page block showing at the end of the book, between the covers. */
.book-band::after {
  content: '';
  position: absolute;
  top: 3px;
  bottom: 3px;
  right: 0;
  width: 5px;
  border-radius: 0 1px 1px 0;
  background: repeating-linear-gradient(180deg, #f4eee0 0 1px, #ddd3bd 1px 2px);
  box-shadow: inset 1px 0 0 rgba(0, 0, 0, 0.15);
  pointer-events: none;
}

.band-skeleton::after {
  display: none;
}

/* Raise only for real hover devices and keyboard focus — a tap on a
   touch screen leaves :focus behind, which would keep the band stuck
   in its raised state. */
@media (hover: hover) {
  .book-band:hover {
    z-index: 200;
    filter: brightness(1.12) saturate(1.1);
    transform: translateY(-8px) rotate(var(--rot)) scale(1.01);
    box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25);
  }
}

.book-band:focus {
  outline: none;
}

.book-band:focus-visible {
  outline: 2px solid var(--fg);
  outline-offset: -3px;
  z-index: 200;
  filter: brightness(1.12) saturate(1.1);
  transform: translateY(-8px) rotate(var(--rot)) scale(1.01);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25);
}

/* A press sinks the book slightly, so a tap feels like touching it. */
.book-band:active {
  transform: translateY(1px) rotate(var(--rot)) scale(0.995);
  transition-duration: 0.05s;
}

/* The opened book slides out of the pile toward its sheet. */
.book-band.is-pulled {
  z-index: 200;
  transform: translateX(28px) rotate(var(--rot));
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.3);
  transition-duration: 0.28s;
}

.band-label {
  display: none;
}

.band-title {
  flex: 0 1 auto;
  min-width: 0;
  max-width: 100%;
  padding: 0 16px;
  /* Page-count-based ideal size set in JS; fitTitle() then shrinks it
     per band only as far as needed to fit the line limit. */
  font-size: var(--title-px, 32px);
  font-weight: var(--font-weight, 900);
  letter-spacing: -0.01em;
  /* Tight enough to not waste vertical space, loose enough that display
     serifs' ascenders/descenders clear the line-clamp's overflow: hidden.
     fitTitle() reads the computed value, so this can be tuned freely. */
  line-height: 1.18;
  text-wrap: balance;
  word-wrap: break-word;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  transition: transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@media (hover: hover) {
  .book-band:hover .band-title {
    transform: translateX(7px);
  }
}

.book-band:focus-visible .band-title {
  transform: translateX(7px);
}

.band-meta {
  flex-shrink: 1;
  /* Reserve enough room that typical author names stay readable;
     anything longer ellipsizes (the tooltip shows the full name). */
  min-width: 150px;
  max-width: 45%;
  display: flex;
  flex-direction: column;
  align-items: var(--meta-align, flex-end);
  text-align: var(--meta-text-align, right);
  gap: 3px;
  margin-left: auto;
  order: var(--meta-order, 0);
}

.band-meta[data-pos="left"] {
  --meta-align: flex-start;
  --meta-text-align: left;
  margin-left: 0;
  margin-right: auto;
  order: -1;
}

.band-author {
  font-size: 13px;
  line-height: 1.2;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.band-year {
  font-size: 12px;
  line-height: 1;
  opacity: 0.85;
  font-family: 'Inter', sans-serif;
}

/* --- Small screens: full-bleed, left-aligned, stacked title + meta --- */
@media (max-width: 767px) {
  body {
    padding: 28px 0 48px;
  }

  .shelf-group .group-label {
    margin-left: 20px;
    margin-right: 20px;
  }

  .group-label {
    padding-left: 0;
  }

  .books-list {
    padding-left: 0;
    padding-right: 0;
  }

  .book-band {
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    gap: 6px;
    /* A narrower width range than desktop, shifted sideways per book, so
       the phone pile still has ragged edges instead of a wall of cards. */
    width: var(--band-w-m, 100%);
    margin-left: calc((100% - var(--band-w-m, 100%)) * var(--shift, 0));
    --rot: calc(var(--tilt, 0deg) * 0.6);
    height: auto;
    /* Page-count thickness still shows, scaled down; never below the
       44px touch-target minimum. */
    min-height: max(44px, calc(var(--band-h, 64px) * 0.62));
    padding: 14px 20px;
  }

  .book-band.is-pulled {
    transform: translateX(14px) rotate(var(--rot));
  }

  .band-title {
    padding: 0;
    /* Bounded so a long title can't grow the band into a square block. */
    -webkit-line-clamp: 3;
  }

  .band-meta,
  .band-meta[data-pos="left"] {
    flex-direction: row;
    align-items: baseline;
    gap: 10px;
    max-width: 100%;
    margin: 0;
    order: 0;
    text-align: left;
  }

  .band-author {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    white-space: normal;
  }

  .band-year {
    font-size: 12px;
  }
}

/* --- Motion --- */
@media (prefers-reduced-motion: no-preference) {
  .book-band {
    animation: band-in 0.5s cubic-bezier(0.22, 1, 0.36, 1) backwards;
    animation-delay: calc(var(--idx, 0) * 50ms);
  }
}

@keyframes band-in {
  from {
    opacity: 0;
    transform: translateY(14px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .book-band,
  .band-title,
  .book-sheet,
  .detail-panel,
  .band-skeleton {
    transition: none;
    animation: none;
  }

  .book-band:hover,
  .book-band:focus-visible {
    transform: none;
  }

  .book-band:hover .band-title,
  .book-band:focus-visible .band-title {
    transform: none;
  }
}

.empty-message {
  padding: 40px 20px;
  text-align: center;
  color: var(--empty-color);
  font-size: 14px;
}

.band-skeleton {
  background: linear-gradient(90deg, var(--skeleton-a) 25%, var(--skeleton-b) 50%, var(--skeleton-a) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border: none;
  cursor: default;
}

@keyframes shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* --- Book sheet --- */
html.sheet-open {
  overflow: hidden;
}

.detail-panel {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0);
  transition: background 0.25s ease;
}

.detail-panel.hidden {
  display: none;
}

.detail-panel.open {
  background: rgba(0, 0, 0, 0.45);
}

.book-sheet {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(420px, 92vw);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 56px 36px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  background-color: var(--bg, #333);
  background-image: linear-gradient(180deg, rgba(255, 255, 255, 0.08), rgba(0, 0, 0, 0.12));
  color: var(--fg, #fff);
  box-shadow: -12px 0 40px rgba(0, 0, 0, 0.3);
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

.detail-panel.open .book-sheet {
  transform: none;
}

.sheet-handle {
  display: none;
}

.sheet-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.18);
  color: inherit;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.sheet-close:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

.sheet-cover {
  width: min(220px, 60%);
  aspect-ratio: 2 / 3;
  flex-shrink: 0;
  border-radius: 3px;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.15);
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.2),
    0 18px 36px rgba(0, 0, 0, 0.35);
}

.sheet-cover img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.sheet-body {
  width: 100%;
  text-align: center;
}

.sheet-title {
  margin: 0 0 12px;
  font-family: var(--font, serif);
  font-weight: var(--font-weight, 700);
  font-size: 32px;
  line-height: 1.12;
  text-wrap: balance;
}

.sheet-author {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.sheet-meta {
  margin: 0;
  font-size: 14px;
  opacity: 0.8;
}

.sheet-rating {
  margin: 14px 0 0;
  font-size: 18px;
  letter-spacing: 3px;
}

.sheet-link {
  display: inline-block;
  margin-top: 24px;
  padding: 10px 18px;
  border: 1px solid currentColor;
  border-radius: 999px;
  color: inherit;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
}

.sheet-link:hover {
  background: rgba(255, 255, 255, 0.12);
}

@media (max-width: 767px) {
  .book-sheet {
    top: auto;
    left: 0;
    width: 100%;
    max-height: 88vh;
    padding: 12px 24px calc(32px + env(safe-area-inset-bottom));
    gap: 22px;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.3);
    transform: translateY(100%);
  }

  .sheet-handle {
    display: block;
    width: 40px;
    height: 5px;
    border-radius: 3px;
    background: currentColor;
    opacity: 0.35;
  }

  .sheet-close {
    top: 10px;
    right: 12px;
  }

  .sheet-cover {
    width: min(180px, 48%);
  }

  .sheet-title {
    font-size: 28px;
  }
}
