*, *::before, *::after { box-sizing: border-box; }

html { scroll-behavior: smooth; }
html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 20px;
  -webkit-font-smoothing: antialiased;
}

img { display: block; max-width: 100%; }
button, input, select { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }

.skip-link {
  position: absolute; left: var(--s-200); top: -80px;
  background: var(--surface-2); color: var(--text);
  padding: var(--s-75) var(--s-100); border-radius: var(--r-lg); z-index: 100;
}
.skip-link:focus { top: var(--s-75); }

/* ── top bar ─────────────────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  width: min(var(--page), 100% - var(--s-400));
  margin: 0 auto;
  height: 56px;
  display: flex; align-items: center; gap: var(--s-200);
}

.brand {
  display: inline-flex; align-items: center; gap: var(--s-75);
  font-size: 15px; line-height: 22px; font-weight: 600;
  white-space: nowrap; color: var(--text);
}
.brand svg { width: 18px; height: 18px; color: var(--accent); }

.chips { display: flex; align-items: center; gap: var(--s-25); overflow-x: auto; }

.chip {
  border: 0; background: transparent; cursor: pointer;
  padding: var(--s-50) var(--s-75);
  border-radius: var(--r-lg);
  font-size: 13px; line-height: 18px; font-weight: 500;
  color: var(--text-muted);
  white-space: nowrap;
  transition: background var(--dur-color) var(--ease), color var(--dur-color) var(--ease);
}
.chip:hover { background: var(--surface-1); color: var(--text); }
.chip[aria-pressed='true'] { background: var(--accent-soft); color: var(--accent); }

.search {
  margin-left: auto;
  display: flex; align-items: center; gap: var(--s-50);
  background: var(--surface-1);
  border: 1px solid transparent;
  border-radius: var(--r-lg);
  padding: 0 var(--s-75);
  height: 34px;
  width: 240px;
  transition: border-color var(--dur-color) var(--ease), background var(--dur-color) var(--ease);
}
.search:focus-within { background: var(--bg); border-color: var(--accent); }
.search svg { width: 15px; height: 15px; color: var(--text-dim); flex: none; }
.search input {
  flex: 1; min-width: 0; background: transparent; border: 0; outline: 0;
  font-size: 13px; line-height: 18px;
}
.search input::placeholder { color: var(--text-dim); }

.icon-btn {
  width: 34px; height: 34px; flex: none;
  display: grid; place-items: center;
  border: 0; border-radius: var(--r-lg);
  background: var(--surface-1); cursor: pointer;
  transition: background var(--dur-color) var(--ease);
}
.icon-btn:hover { background: var(--surface-2); }
.theme-dot {
  width: 14px; height: 14px; border-radius: var(--r-full);
  background: var(--text);
  box-shadow: inset -4px -4px 0 0 var(--bg);
}

/* ── buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-50);
  padding: var(--s-50) var(--s-100);
  border: 0; border-radius: var(--r-lg);
  font-size: 13px; line-height: 18px; font-weight: 600;
  cursor: pointer;
  transition: background var(--dur-color) var(--ease), transform var(--dur-color) var(--ease),
              opacity var(--dur-color) var(--ease);
}
.btn:active { transform: scale(0.98); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not([disabled]) { filter: brightness(0.94); }

.btn-ghost { background: var(--surface-1); color: var(--text); }
.btn-ghost:hover:not([disabled]) { background: var(--surface-2); }

.select {
  background: var(--surface-1); border: 1px solid transparent;
  border-radius: var(--r-lg); height: 34px; padding: 0 var(--s-75);
  font-size: 13px; line-height: 18px; outline: 0;
}
.select:focus { border-color: var(--accent); }
.select-sm { height: 30px; }

.btn-icon {
  padding: var(--s-50) var(--s-75);
  font-size: 13px; line-height: 18px;
  min-width: 40px;
}

/* ── layout ──────────────────────────────────────────────────────────────── */
main {
  width: min(var(--page), 100% - var(--s-400));
  margin: 0 auto;
  padding: var(--s-300) 0 var(--s-700);
}

.banner-slot { width: min(var(--page), 100% - var(--s-400)); margin: 0 auto; }

.page-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--s-200); flex-wrap: wrap;
  margin-bottom: var(--s-200);
}
.page-head h1 { margin: 0; font-size: 20px; line-height: 28px; font-weight: 600; }
.page-head p { margin: 0; }

.filters { display: flex; gap: var(--s-100); align-items: center; }

/* ── library with filter sidebar ─────────────────────────────────────────── */
.library-layout {
  display: grid;
  grid-template-columns: 210px minmax(0, 1fr);
  gap: var(--s-300);
  align-items: start;
}

.filters-panel {
  position: sticky;
  top: 72px;
  max-height: calc(100vh - 96px);
  display: flex;
  flex-direction: column;
  gap: var(--s-200);
  background: var(--surface-1);
  border-radius: var(--r-xl);
  padding: var(--s-200) var(--s-100);
}

.filter-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-75);
  padding: 0 var(--s-50);
}

.filter-section { display: flex; flex-direction: column; gap: var(--s-25); }

.filter-title {
  margin: 0 0 var(--s-25);
  padding: 0 var(--s-50);
  font-size: 12px;
  line-height: 16px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.filter-option {
  display: flex;
  align-items: center;
  gap: var(--s-50);
  padding: var(--s-25) var(--s-50);
  border-radius: var(--r-md);
  cursor: pointer;
  font-size: 13px;
  line-height: 18px;
  color: var(--text-muted);
  transition: background var(--dur-color) var(--ease), color var(--dur-color) var(--ease);
}
.filter-option:hover { background: var(--surface-2); color: var(--text); }

.filter-box {
  width: 14px; height: 14px; flex: none;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.filter-label {
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.filter-option:has(.filter-box:checked) .filter-label { color: var(--text); font-weight: 600; }

.filter-count { color: var(--text-dim); font-size: 11px; line-height: 16px; }

.filter-scroll {
  display: flex;
  flex-direction: column;
  gap: var(--s-25);
  max-height: 52vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: var(--s-25);
}

.link-btn {
  border: 0; background: transparent; padding: 0;
  font-size: 12px; line-height: 16px; font-weight: 500;
  color: var(--accent); cursor: pointer;
}
.link-btn:hover { text-decoration: underline; }


/* ── continue reading ────────────────────────────────────────────────────── */
.continue { margin-bottom: var(--s-400); }
.continue h2 { margin: 0 0 var(--s-100); font-size: 15px; line-height: 22px; }

.continue-grid {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 220px;
  gap: var(--s-75);
  overflow-x: auto;
  padding-bottom: var(--s-50);
  scroll-snap-type: x proximity;
}

.continue-card {
  display: flex; gap: var(--s-75); align-items: center;
  background: var(--surface-1);
  border-radius: var(--r-lg);
  padding: var(--s-75);
  scroll-snap-align: start;
  transition: background var(--dur-color) var(--ease);
}
.continue-card:hover { background: var(--surface-2); }

.continue-cover {
  width: 34px; height: 50px; flex: none;
  border-radius: var(--r-sm); overflow: hidden; background: var(--surface-3);
}
.continue-cover img { width: 100%; height: 100%; object-fit: cover; }

.continue-body { display: flex; flex-direction: column; gap: var(--s-25); min-width: 0; }
.continue-title {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; text-wrap: pretty;
}
.continue-bar { height: 3px; border-radius: var(--r-full); background: var(--surface-3); overflow: hidden; }
.continue-bar span { display: block; height: 100%; background: var(--accent); }

/* ── book grid ───────────────────────────────────────────────────────────── */
.grid {
  display: grid; gap: var(--s-200) var(--s-100);
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
}

.card { display: block; min-width: 0; }
.card:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: var(--r-md); }

.card-cover {
  aspect-ratio: 2 / 3;
  border-radius: var(--r-lg);
  overflow: hidden;
  background: var(--surface-2);
  display: grid; place-items: center;
  color: var(--text-dim); font-size: 11px;
  box-shadow: var(--shadow-card);
  transition: transform var(--dur-layout) var(--ease), box-shadow var(--dur-layout) var(--ease);
}
.card:hover .card-cover { transform: translateY(-3px); box-shadow: 0 8px 20px rgba(20, 20, 30, 0.12); }
.card-cover img { width: 100%; height: 100%; object-fit: cover; }

.card-body { padding: var(--s-75) var(--s-25) 0; display: flex; flex-direction: column; gap: var(--s-25); }
.card-title {
  font-size: 13px; line-height: 18px; font-weight: 500;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; text-wrap: pretty;
}
.card-meta {
  display: flex; align-items: center; gap: var(--s-50);
  color: var(--text-dim); font-size: 12px; line-height: 16px;
}

.badge {
  display: inline-block;
  font-size: 11px; line-height: 16px; font-weight: 500;
  padding: 0 var(--s-50); border-radius: var(--r-md);
  background: var(--surface-2); color: var(--text-muted);
}
.badge.is-ongoing { color: var(--warn); background: transparent; }
.badge.is-complete { color: var(--ok); background: transparent; }

/* ── book detail ─────────────────────────────────────────────────────────── */
.book {
  display: grid; gap: var(--s-300);
  grid-template-columns: 180px minmax(0, 1fr);
  align-items: start;
}
.book-cover {
  aspect-ratio: 2 / 3; border-radius: var(--r-xl); overflow: hidden;
  background: var(--surface-2); box-shadow: var(--shadow-card);
}
.book-cover img { width: 100%; height: 100%; object-fit: cover; }

.book h1 { margin: 0 0 var(--s-50); text-wrap: balance; }
.book-info { display: flex; flex-direction: column; gap: var(--s-200); }
.book-meta { display: flex; gap: var(--s-75); flex-wrap: wrap; color: var(--text-muted); align-items: center; }
.book-actions { display: flex; gap: var(--s-75); flex-wrap: wrap; align-items: center; }

.tag-row { display: flex; gap: var(--s-25); flex-wrap: wrap; margin-bottom: var(--s-200); }
.book-summary { color: var(--text-muted); max-width: 72ch; margin: 0; }
.book-tags { display: flex; gap: var(--s-25); flex-wrap: wrap; }

.progress { height: 4px; border-radius: var(--r-full); background: var(--surface-3); overflow: hidden; width: 160px; }
.progress span { display: block; height: 100%; width: 0%; background: var(--accent); transition: width var(--dur-color) var(--ease); }

.chapters { margin-top: var(--s-400); border-top: 1px solid var(--line); padding-top: var(--s-200); }
.chapters h2 { margin: 0 0 var(--s-75); font-size: 15px; line-height: 22px; }
.chapters ol { margin: 0; padding: 0; list-style: none; display: grid; gap: 1px; }
.chapters li a {
  display: block; padding: var(--s-50) var(--s-75);
  border-radius: var(--r-md); color: var(--text-muted);
  font-size: 13px; line-height: 18px;
  transition: background var(--dur-color) var(--ease), color var(--dur-color) var(--ease);
}
.chapters li a:hover { background: var(--surface-1); color: var(--text); }

/* ── reader ──────────────────────────────────────────────────────────────── */
.reader-layout {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
  gap: var(--s-200);
  align-items: start;
}

.reader-sidebar {
  position: sticky; top: 72px;
  max-height: calc(100vh - 96px);
  display: flex; flex-direction: column; gap: var(--s-50);
  background: var(--surface-1);
  border-radius: var(--r-xl);
  padding: var(--s-75);
}

.toc-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 var(--s-50); }
.toc { overflow-y: auto; display: flex; flex-direction: column; gap: 1px; overscroll-behavior: contain; }

.toc-link {
  display: flex; gap: var(--s-50);
  padding: var(--s-50) var(--s-75);
  border-radius: var(--r-md);
  color: var(--text-muted);
  font-size: 13px; line-height: 18px;
  text-wrap: pretty;
  transition: background var(--dur-color) var(--ease), color var(--dur-color) var(--ease);
}
.toc-link:hover { background: var(--surface-2); color: var(--text); }
.toc-link.is-read { color: var(--text-dim); }
.toc-link.is-read::before { content: '✓'; font-size: 11px; opacity: 0.7; }
.toc-link.is-current { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.toc-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.reader-area {
  background: var(--reader-paper);
  color: var(--reader-ink);
  border-radius: var(--r-xl);
  border: 1px solid var(--line);
  padding: var(--s-400) var(--s-500);
  display: flex; flex-direction: column; gap: var(--s-200);
  transition: background var(--dur-color) var(--ease), color var(--dur-color) var(--ease);
}

.reader-area[data-theme='sepia'] { --reader-paper: #f6ecd9; --reader-ink: #3b2f21; }
.reader-area[data-theme='light'] { --reader-paper: #ffffff; --reader-ink: #191919; }
.reader-area[data-theme='dark'] { --reader-paper: #141416; --reader-ink: #e8e6e3; }

.reader-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--s-100); flex-wrap: wrap;
}
.reader-head h1 { margin: 0; font-size: 19px; line-height: 26px; font-weight: 600; text-wrap: balance; }
.reader-controls { display: flex; align-items: center; gap: var(--s-50); }

.reader-meta { display: flex; gap: var(--s-50); color: var(--text-dim); }

.reader-settings {
  display: flex; flex-direction: column; gap: var(--s-75);
  background: var(--surface-1);
  border-radius: var(--r-lg);
  padding: var(--s-100);
}
.reader-area[data-theme='sepia'] .reader-settings { background: #ece0c9; }
.reader-area[data-theme='light'] .reader-settings { background: #f4f4f6; }

.setting-row { display: flex; align-items: center; justify-content: space-between; gap: var(--s-100); flex-wrap: wrap; }
.theme-chips { display: flex; gap: var(--s-25); }

.reader-body { color: inherit; }
/* Thai novels indent the first line of every paragraph rather than adding space
   between them, which keeps long chapters as one continuous block of text. */
.reader-body p {
  margin: 0 0 var(--s-75);
  text-indent: 1.5em;
  text-wrap: pretty;
}
.reader-body p:last-child { margin-bottom: 0; }
.reader-body h1, .reader-body h2, .reader-body h3 { margin: var(--s-200) 0 var(--s-100); }
.reader-body img { margin: var(--s-200) auto; border-radius: var(--r-md); }

.reader-nav { display: flex; justify-content: space-between; gap: var(--s-75); margin-top: var(--s-200); flex-wrap: wrap; }

/* ── states ──────────────────────────────────────────────────────────────── */
.skeleton { background: var(--surface-2); border-radius: var(--r-lg); animation: pulse 1.4s var(--ease) infinite; }
.skeleton.cover { aspect-ratio: 2 / 3; }
.skeleton.line { height: 12px; border-radius: var(--r-md); }
@keyframes pulse { 0%, 100% { opacity: 0.8; } 50% { opacity: 0.45; } }

.empty {
  border-radius: var(--r-xl); background: var(--surface-1);
  padding: var(--s-600) var(--s-300); text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: var(--s-75);
}
.empty h2 { margin: 0; }
.empty p { margin: 0; color: var(--text-muted); max-width: 44ch; }

.banner {
  border-radius: var(--r-lg); padding: var(--s-75) var(--s-100);
  background: var(--surface-2); color: var(--err);
  font-size: 13px; line-height: 18px; margin: var(--s-100) 0;
}

.footer {
  width: min(var(--page), 100% - var(--s-400));
  margin: 0 auto; padding: var(--s-200) 0 var(--s-300);
  border-top: 1px solid var(--line);
  color: var(--text-dim); font-size: 12px; line-height: 16px;
  display: flex; justify-content: space-between; gap: var(--s-100); flex-wrap: wrap;
}

[hidden] { display: none !important; }

@media (max-width: 860px) {
  .topbar-inner { gap: var(--s-100); height: auto; padding: var(--s-75) 0; flex-wrap: wrap; }
  .search { width: 100%; order: 3; margin-left: 0; }
  .book { grid-template-columns: 120px minmax(0, 1fr); gap: var(--s-200); }
  .reader-layout { grid-template-columns: 1fr; }
  .reader-sidebar { position: static; max-height: 200px; }
  .reader-area { padding: var(--s-200) var(--s-100); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: var(--s-200) var(--s-75); }
  .library-layout { grid-template-columns: 1fr; }
  .filters-panel { position: static; max-height: none; }
  .filter-scroll { max-height: 160px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
  .card:hover .card-cover { transform: none; }
  html { scroll-behavior: auto; }
}
