/* Palette mirrors the iOS app (ios/mbooks/Sources/Theme.swift). */
:root {
  --background: #10140e;
  --card: #1b211a;
  --card-elevated: #232b22;
  --chip: #2a3228;
  --cream: #f0e8d4;
  --gold: #d9a848;
  --teal: #4bb8a9;
  --muted: #9aa294;
  --green: #69b788;
  --red-text: #e5907b;
  --border: rgba(255, 255, 255, 0.06);

  --panel-width: 420px;
  --gutter: 32px;
  --radius: 20px;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--background);
  color: var(--cream);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Locked while the detail panel is open, so the page behind stays put. */
body.panel-open {
  overflow: hidden;
}

/* ---------- gallery ---------- */

.gallery {
  max-width: 1720px;
  margin: 0 auto;
  padding: 0 var(--gutter) 80px;
}

.masthead {
  position: sticky;
  top: 0;
  z-index: 5;
  margin: 0 calc(var(--gutter) * -1) 26px;
  padding: 26px var(--gutter) 18px;
  background: var(--background);
  border-bottom: 1px solid transparent;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

/* Set by the script once the page is scrolled. */
.masthead.stuck {
  border-bottom-color: var(--border);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.brand h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--cream);
}

.tagline {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.search {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 16px;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
  pointer-events: none;
}

#search {
  width: 100%;
  padding: 14px 44px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--card);
  color: var(--cream);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}

#search::placeholder {
  color: var(--muted);
}

#search:focus {
  border-color: rgba(217, 168, 72, 0.55);
  background: var(--card-elevated);
}

#search::-webkit-search-cancel-button {
  display: none;
}

.search-clear {
  position: absolute;
  right: 12px;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 50%;
  background: var(--chip);
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.search-clear:hover {
  color: var(--cream);
}

/* ---------- filters ---------- */

.controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
}

.segmented button {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 8px 14px;
  border: 0;
  border-radius: 9px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.segmented button:hover {
  color: var(--cream);
}

.segmented button[aria-pressed="true"] {
  background: var(--chip);
  color: var(--cream);
}

.segmented .count {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.segmented button[aria-pressed="true"] .count {
  color: var(--gold);
}

.select-wrap {
  position: relative;
  display: inline-flex;
}

#category-filter {
  appearance: none;
  -webkit-appearance: none;
  padding: 11px 38px 11px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  color: var(--cream);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  outline: none;
  max-width: 280px;
}

#category-filter:focus {
  border-color: rgba(217, 168, 72, 0.55);
}

.select-chevron {
  position: absolute;
  right: 13px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

/* ---------- grid ---------- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 30px 22px;
}

.cell {
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.cover {
  position: relative;
  aspect-ratio: 0.7;
  border-radius: 10px;
  overflow: hidden;
  background: var(--card);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
  transition: transform 0.18s ease, box-shadow 0.18s ease, outline-color 0.18s ease;
  outline: 2px solid transparent;
  outline-offset: 3px;
}

.cell:hover .cover {
  transform: translateY(-4px);
  box-shadow: 0 16px 32px rgba(0, 0, 0, 0.55);
}

.cell:focus-visible {
  outline: none;
}

.cell:focus-visible .cover,
.cell[aria-current="true"] .cover {
  outline-color: var(--gold);
}

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

/* Placeholder shown when a book has no cover image. */
.cover-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 12px;
  background: var(--card-elevated);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.35;
  text-align: center;
  overflow: hidden;
}

.cell-title {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--cream);
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.cell-author {
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: -4px;
}

/* ---------- detail panel ---------- */

.scrim {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(6, 8, 5, 0.6);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.24s ease, visibility 0.24s;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.scrim.open {
  opacity: 1;
  visibility: visible;
}

.detail {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 21;
  width: var(--panel-width);
  max-width: 100vw;
  height: 100dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--background);
  border-left: 1px solid var(--border);
  box-shadow: -24px 0 60px rgba(0, 0, 0, 0.5);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform 0.26s cubic-bezier(0.32, 0.72, 0, 1), visibility 0.26s;
}

.detail.open {
  transform: translateX(0);
  visibility: visible;
}

.detail-inner {
  padding: 30px 30px 70px;
}

.detail-close {
  position: sticky;
  top: 18px;
  float: right;
  z-index: 2;
  width: 34px;
  height: 34px;
  margin: 18px 18px 0 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--card-elevated);
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease;
}

.detail-close:hover {
  color: var(--cream);
  background: var(--chip);
}

.detail-cover {
  width: 180px;
  aspect-ratio: 0.7;
  border-radius: 12px;
  overflow: hidden;
  background: var(--card);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.6);
  margin-bottom: 22px;
}

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

.detail h2 {
  margin: 0 0 6px;
  font-size: 23px;
  line-height: 1.25;
  font-weight: 700;
  color: var(--cream);
}

.detail-author {
  margin: 0 0 18px;
  font-size: 14.5px;
  color: var(--gold);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 22px;
}

.chip {
  padding: 6px 11px;
  border-radius: 999px;
  background: var(--chip);
  color: var(--cream);
  font-size: 12px;
  font-weight: 600;
}

.chip.read {
  background: rgba(105, 183, 136, 0.16);
  color: var(--green);
}

.chip.unread {
  background: rgba(154, 162, 148, 0.14);
  color: var(--muted);
}

.chip.gold {
  background: rgba(217, 168, 72, 0.16);
  color: var(--gold);
}

.chip.teal {
  background: rgba(75, 184, 169, 0.16);
  color: var(--teal);
}

.section {
  margin-bottom: 22px;
}

.section-label {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 3px;
  text-transform: uppercase;
  color: var(--gold);
}

.description {
  margin: 0;
  font-size: 14px;
  line-height: 1.62;
  color: var(--cream);
  white-space: pre-wrap;
}

.facts {
  display: grid;
  gap: 1px;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--border);
  border: 1px solid var(--border);
}

.fact {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  padding: 12px 16px;
  background: var(--card);
  font-size: 13.5px;
}

.fact dt {
  color: var(--muted);
  flex: none;
}

.fact dd {
  margin: 0;
  text-align: right;
  color: var(--cream);
  word-break: break-word;
}

.tracks {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.track {
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--card);
  border: 1px solid var(--border);
}

.track-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--cream);
}

.track audio {
  width: 100%;
  height: 34px;
}

/* ---------- more by the same author ---------- */

.related {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 16px 12px;
}

.related-cell {
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.related-cover {
  aspect-ratio: 0.7;
  border-radius: 8px;
  overflow: hidden;
  background: var(--card-elevated);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
  transition: transform 0.15s ease;
}

.related-cell:hover .related-cover {
  transform: translateY(-3px);
}

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

.related-title {
  font-size: 11px;
  line-height: 1.3;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ---------- states ---------- */

.state {
  padding: 60px 8px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

.state strong {
  color: var(--cream);
  display: block;
  margin-bottom: 6px;
  font-size: 15px;
}

/* ---------- responsive ---------- */

@media (max-width: 860px) {
  :root {
    --gutter: 18px;
  }

  .grid {
    grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
    gap: 22px 14px;
  }

  .masthead {
    padding-top: 20px;
  }

  .filters {
    gap: 8px;
  }

  .segmented button {
    padding: 8px 11px;
    font-size: 12.5px;
  }

  #category-filter {
    max-width: 100%;
  }

  /* Full-screen sheet rather than a side panel. */
  .detail {
    width: 100vw;
    border-left: 0;
  }

  .detail-inner {
    padding: 24px 20px 60px;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  .detail,
  .scrim,
  .cover {
    transition: none;
  }
}
