:root {
  color-scheme: light;
  --ink: #101820;
  --muted: #52606d;
  --line: #d8d1c4;
  --paper: #f7f3ea;
  --panel: #fffaf0;
  --mint: #00746d;
  --purple: #6f3cc3;
  --danger: #a33b2f;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
}

button, input, select {
  font: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px clamp(16px, 4vw, 40px);
  background: rgba(247, 243, 234, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  font-weight: 800;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--ink);
  color: #00f5d4;
  letter-spacing: 0;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.inline-select {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.inline-select span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
}

.inline-select select {
  min-height: 32px;
  border: 0;
  background: transparent;
  padding: 0;
  font-size: 0.86rem;
}

.icon-button, .install-button, .download-button, .reader-tools button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}

.icon-button {
  width: 40px;
  font-size: 22px;
}

.install-button, .download-button, .reader-tools button {
  padding: 0 14px;
  font-weight: 700;
}

.primary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  margin-top: 14px;
  padding: 0 16px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  text-decoration: none;
}

.library-shell {
  width: min(1180px, calc(100% - 32px));
  margin: 28px auto 56px;
}

.library-modes {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 12px;
}

.library-mode {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font-weight: 700;
  cursor: pointer;
}

.library-mode.active {
  border-color: #111;
  background: #111;
  color: #fff;
}

.library-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 20px;
  margin-bottom: 20px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--purple);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  max-width: 760px;
  font-size: clamp(2rem, 5vw, 4.4rem);
  line-height: 1;
  letter-spacing: 0;
}

.status-pill {
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--panel);
}

.status-pill.offline {
  color: var(--danger);
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) minmax(150px, 220px) minmax(140px, 190px);
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.search-field {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
}

.search-field span {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

input, select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: #fff;
  color: var(--ink);
}

.meta-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin: 16px 0;
  color: var(--muted);
  font-size: 0.92rem;
}

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

.series-group {
  grid-column: 1 / -1;
}

.series-details {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
}

.series-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 14px;
  cursor: pointer;
  list-style: none;
  background: #faf7f2;
}

.series-summary::-webkit-details-marker {
  display: none;
}

.series-summary strong {
  font-size: 1rem;
}

.series-summary span {
  color: var(--muted);
  font-size: 0.86rem;
}

.series-book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
  padding: 14px;
}

.book-card {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  gap: 14px;
  min-height: 184px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.cover-slot {
  width: 96px;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  overflow: hidden;
  background: var(--ink);
}

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

.fallback-cover {
  display: grid;
  align-content: space-between;
  width: 100%;
  height: 100%;
  padding: 10px;
  color: #fff;
  background: linear-gradient(145deg, #101820, #34214f);
  font-size: 0.76rem;
  font-weight: 800;
}

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

.book-kicker {
  color: var(--mint);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.book-card h2 {
  margin: 4px 0 6px;
  font-size: 1rem;
  line-height: 1.2;
}

.book-card p {
  display: -webkit-box;
  min-height: 55px;
  margin: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.35;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.book-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

.read-link {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0 16px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  text-decoration: none;
}

.download-button.cached {
  border-color: #9bd4c6;
  background: #e9fff8;
  color: #00675f;
}

.wishlist-button,
.remove-button {
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
}

.remove-button {
  border-color: #f3b7af;
  color: #8f2e20;
  background: #fff7f6;
}

.reader-shell {
  display: grid;
  grid-template-columns: 300px minmax(0, 820px);
  gap: 26px;
  width: min(1180px, calc(100% - 32px));
  margin: 24px auto 56px;
}

.reader-sidebar {
  position: sticky;
  top: 76px;
  align-self: start;
  max-height: calc(100vh - 96px);
  overflow: auto;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.back-link {
  color: var(--purple);
  font-weight: 800;
  text-decoration: none;
}

.reader-sidebar h2 {
  margin: 18px 0 4px;
  font-size: 1.15rem;
}

.reader-sidebar p {
  margin: 0 0 16px;
  color: var(--muted);
}

.chapter-list {
  display: grid;
  gap: 6px;
}

.chapter-list button {
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 10px;
  text-align: left;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.chapter-list button.active {
  border-color: var(--line);
  background: #fff;
  font-weight: 800;
}

.reader-panel {
  min-width: 0;
  padding: clamp(18px, 4vw, 44px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.reader-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 26px;
}

#chapter-title {
  margin-bottom: 22px;
  font-size: clamp(1.8rem, 4vw, 3rem);
  line-height: 1.08;
}

.chapter-body {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.16rem;
  line-height: 1.76;
}

.chapter-body p {
  margin: 0 0 1.15em;
}

.chapter-body blockquote {
  margin: 1.4em 0;
  padding-left: 1em;
  border-left: 4px solid var(--purple);
  color: #343b44;
}

.empty-state {
  grid-column: 1 / -1;
  padding: 36px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 820px) {
  .library-header, .toolbar, .reader-shell {
    grid-template-columns: 1fr;
  }

  .reader-sidebar {
    position: static;
    max-height: none;
  }
}

@media (max-width: 520px) {
  .book-card {
    grid-template-columns: 78px minmax(0, 1fr);
  }

  .cover-slot {
    width: 78px;
  }

  .brand span:last-child {
    display: none;
  }
}

/* Accessibility: respect users who prefer reduced motion (vestibular safety). */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
