:root {
  --paper: #fbf6ec;
  --paper-2: #f3ead8;
  --ink: #1f1a14;
  --ink-soft: #6b5f50;
  --line: #e4d8c2;
  --accent: #c2410c;
  --accent-soft: #fde4d3;
  --new-word: #fff1a8;
  --shadow: 0 1px 2px rgba(60, 40, 20, .08), 0 6px 20px rgba(60, 40, 20, .08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: "Lexend", system-ui, -apple-system, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}

/* ---------- Library ---------- */

.lib-header {
  padding: calc(var(--safe-top) + 28px) 20px 8px;
  max-width: 1000px;
  margin: 0 auto;
}
.lib-header h1 {
  font-size: clamp(30px, 7vw, 44px);
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}
.lib-header p { margin: 6px 0 0; color: var(--ink-soft); font-size: 17px; }

.shelf {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px 20px calc(var(--safe-bottom) + 40px);
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 22px;
}
@media (min-width: 700px) {
  .shelf { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 28px; }
}

.book {
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-decoration: none;
  color: inherit;
}
.cover {
  position: relative;
  aspect-ratio: 3 / 4;
  border-radius: 6px 12px 12px 6px;
  background: var(--c, var(--accent));
  color: #fff;
  box-shadow: var(--shadow), inset 10px 0 0 rgba(0,0,0,.14), inset 12px 0 0 rgba(255,255,255,.12);
  padding: 18px 16px 16px 26px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  transition: transform .15s ease;
}
.book:active .cover { transform: scale(.97); }
.cover-title {
  font-size: clamp(20px, 5vw, 26px);
  font-weight: 700;
  line-height: 1.15;
  text-wrap: balance;
}
.cover-art { align-self: flex-end; width: 56%; opacity: .9; }
.badge {
  position: absolute;
  bottom: 14px;
  left: 26px;
  background: rgba(255,255,255,.92);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 999px;
}
.book-meta { font-size: 15px; color: var(--ink-soft); }
.progress {
  height: 6px;
  background: var(--line);
  border-radius: 999px;
  overflow: hidden;
}
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; }

.empty, .error { padding: 40px 20px; color: var(--ink-soft); text-align: center; font-size: 18px; }

/* ---------- Reader ---------- */

.reader {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(var(--safe-top) + 6px) 8px 6px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.topbar .title {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 15px;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  min-width: 44px;
  height: 44px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 600;
  padding: 0 10px;
}
.icon-btn:active { background: var(--paper-2); }
a.icon-btn { color: inherit; text-decoration: none; }
.icon-btn svg { width: 24px; height: 24px; }

.stage {
  position: relative;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px 22px;
}

.page {
  width: 100%;
  max-width: 760px;
  font-size: calc(var(--fs-base, 30px) * var(--scale, 1));
  line-height: 1.75;
  letter-spacing: 0.02em;
  word-spacing: 0.12em;
}
@media (min-width: 700px) { .page { --fs-base: 40px; } }

.line { display: block; margin: 0 0 0.45em; padding: 0 .15em; border-radius: 8px; }
.line.speaking { background: var(--accent-soft); }

.w {
  border-radius: 6px;
  padding: 0 1px;
  cursor: pointer;
}
.w.new { background: var(--new-word); box-shadow: 0 0 0 2px var(--new-word); }
.hide-new .w.new { background: none; box-shadow: none; }
.w.tapped { background: var(--accent); color: #fff; box-shadow: 0 0 0 2px var(--accent); }

/* Chapter opener */
.chapter-open { text-align: center; }
.chapter-open .kicker {
  font-size: .5em;
  text-transform: uppercase;
  letter-spacing: .14em;
  color: var(--accent);
  font-weight: 600;
}
.chapter-open h2 { font-size: 1.15em; line-height: 1.25; margin: .2em 0 1em; }
.chapter-open .label { font-size: .5em; color: var(--ink-soft); margin-bottom: .5em; }
.cards { display: flex; justify-content: center; flex-wrap: wrap; gap: .4em; }
.card {
  background: #fff;
  border: 2px solid var(--line);
  border-radius: 16px;
  padding: .25em .8em;
  font-weight: 600;
  font-size: 1.1em;
  box-shadow: var(--shadow);
}
.card:active { border-color: var(--accent); }

/* Talk page */
.talk { font-size: .72em; line-height: 1.5; }
.talk .kicker { font-size: .7em; text-transform: uppercase; letter-spacing: .12em; color: var(--ink-soft); font-weight: 600; margin-bottom: .6em; }
.talk ul { padding-left: 1.1em; margin: 0; }
.talk li { margin-bottom: .6em; }
.talk .note { font-size: .7em; color: var(--ink-soft); margin-top: 1em; }

/* End page */
.the-end { text-align: center; }
.the-end h2 { font-size: 1.4em; margin: 0 0 .4em; }
.the-end p { font-size: .6em; color: var(--ink-soft); margin: 0 0 1.2em; }
.big-btn {
  display: inline-block;
  font-size: 20px;
  font-weight: 600;
  padding: 14px 26px;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  margin: 6px;
}
.big-btn.secondary { background: var(--paper-2); color: var(--ink); }

/* Bottom nav */
.navbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 12px calc(var(--safe-bottom) + 8px);
  border-top: 1px solid var(--line);
  background: var(--paper);
}
.nav-btn {
  width: 64px;
  height: 56px;
  border-radius: 16px;
  background: var(--paper-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.nav-btn:active { background: var(--line); }
.nav-btn:disabled { opacity: .3; }
.nav-btn svg { width: 28px; height: 28px; }
.nav-mid { display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 0; }
.read-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
}
.read-btn svg { width: 20px; height: 20px; }
.read-btn[aria-pressed="true"] { background: var(--ink); }
.page-count { font-size: 13px; color: var(--ink-soft); }
.navbar .progress { width: 100%; max-width: 320px; height: 4px; }

/* Sheet (chapters + settings) */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 14, 8, .35);
  display: none;
  z-index: 10;
}
.sheet-backdrop.open { display: block; }
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 80dvh;
  overflow-y: auto;
  background: var(--paper);
  border-radius: 20px 20px 0 0;
  padding: 18px 20px calc(var(--safe-bottom) + 20px);
  z-index: 11;
  transform: translateY(105%);
  transition: transform .22s ease;
  max-width: 640px;
  margin: 0 auto;
}
.sheet.open { transform: translateY(0); }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.sheet .row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  font-size: 17px;
}
.sheet .row:last-child { border-bottom: 0; }
.chapter-item {
  width: 100%;
  text-align: left;
  padding: 14px 4px;
  border-bottom: 1px solid var(--line);
  font-size: 18px;
  display: flex;
  gap: 12px;
}
.chapter-item .n { color: var(--ink-soft); width: 1.6em; flex: none; }
.chapter-item.current { color: var(--accent); font-weight: 600; }
.seg { display: inline-flex; background: var(--paper-2); border-radius: 12px; padding: 3px; }
.seg button { padding: 8px 14px; border-radius: 9px; font-weight: 600; }
.seg button[aria-pressed="true"] { background: #fff; box-shadow: var(--shadow); }
.toggle { width: 52px; height: 32px; border-radius: 999px; background: var(--line); position: relative; flex: none; }
.toggle::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: left .15s; box-shadow: var(--shadow); }
.toggle[aria-pressed="true"] { background: var(--accent); }
.toggle[aria-pressed="true"]::after { left: 23px; }

/* Night theme */
[data-theme="night"] {
  --paper: #1c1915;
  --paper-2: #2a2620;
  --ink: #efe6d6;
  --ink-soft: #a89c8a;
  --line: #3a342c;
  --accent-soft: #4a2a17;
  --new-word: #5c4d12;
  color-scheme: dark;
}
[data-theme="night"] .card, [data-theme="night"] .seg button[aria-pressed="true"] { background: #2f2a23; }

@media (prefers-reduced-motion: reduce) {
  .sheet, .cover { transition: none; }
}

.names-label { margin-top: 1.1em; }
.cards.names .card { font-size: .8em; font-weight: 600; background: var(--paper-2); box-shadow: none; }
