* {
  box-sizing: border-box;
}

:root {
  --page-bg: #f5f0e8;
  --text-color: #333;
  --group-label-color: #999;
  --label-border: #e0dbd3;
  --empty-color: #bbb;
  --band-gap: 3px;
  --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;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-left: var(--offset, 0px);
}

/* 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 {
    filter: brightness(1.12) saturate(1.1);
    transform: translateY(-8px) 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;
  filter: brightness(1.12) saturate(1.1);
  transform: translateY(-8px) scale(1.01);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.25);
}

.band-label {
  display: none;
}

.band-title {
  flex: 0 1 auto;
  min-width: 0;
  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;
    width: 100%;
    margin-left: 0;
    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;
  }

  .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-tooltip,
  .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;
  }
}

/* --- Detail tooltip --- */
.detail-panel {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  pointer-events: none;
}

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

.book-tooltip {
  position: absolute;
  width: 400px;
  max-width: min(92vw, 440px);
  background-color: var(--detail-bg);
  color: var(--detail-fg);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  padding: 20px;
  display: flex;
  gap: 18px;
  z-index: 101;
  pointer-events: auto;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.08s ease, transform 0.08s ease;
}

.book-tooltip.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.95);
  width: 92%;
  max-width: 440px;
  max-height: 80vh;
}

.book-tooltip.modal.visible {
  transform: translate(-50%, -50%) scale(1);
}

/* Dim the page behind the mobile modal. The backdrop lives on the
   panel (not a ::before of the tooltip) because the tooltip's transform
   would turn position: fixed into tooltip-relative positioning. */
.detail-panel:has(.book-tooltip.modal.visible) {
  background: rgba(0, 0, 0, 0.5);
  pointer-events: auto;
  transition: background 0.15s ease;
}

.book-tooltip.visible {
  opacity: 1;
  transform: translateY(0);
}

/* Book covers are portrait: keep a 2:3 aspect ratio everywhere. */
.tooltip-cover {
  flex-shrink: 0;
  width: 96px;
  aspect-ratio: 2 / 3;
  height: auto;
  align-self: flex-start;
  border-radius: 4px;
  background-color: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.book-tooltip.modal .tooltip-cover {
  width: 128px;
}

@media (max-width: 380px) {
  .book-tooltip.modal .tooltip-cover {
    width: 104px;
  }
}

.tooltip-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.tooltip-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tooltip-title {
  font-size: 17px;
  font-weight: 600;
  line-height: 1.35;
  word-break: break-word;
  text-wrap: balance;
}

.tooltip-author {
  font-size: 14px;
  opacity: 0.85;
}

.tooltip-meta {
  font-size: 12.5px;
  opacity: 0.65;
  margin-top: 2px;
}

.tooltip-rating {
  font-size: 13px;
  margin-top: 6px;
  color: #e9b949;
  letter-spacing: 2.5px;
}
