:root {
  --paper: #f7f2e8;
  --paper-deep: #eee5d4;
  --ink: #28231f;
  --muted: #8a7f72;
  --line: rgba(68, 54, 42, 0.12);
  --card: rgba(255, 253, 248, 0.82);
  --accent: #b46f7c;
  --accent-soft: #f1d9dd;
  --ai: #638ca5;
  --ai-soft: #dcebf2;
  --shadow: 0 18px 55px rgba(66, 47, 33, 0.12);
  --serif: "Songti SC", "Noto Serif SC", "STSong", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif;
  color-scheme: light;
}

[data-theme="dark"] {
  --paper: #181817;
  --paper-deep: #232220;
  --ink: #e8e1d6;
  --muted: #9b9389;
  --line: rgba(255, 255, 255, 0.1);
  --card: rgba(38, 37, 35, 0.88);
  --accent: #d58d9b;
  --accent-soft: #4b3036;
  --ai: #8ab4ca;
  --ai-soft: #283f49;
  --shadow: 0 18px 55px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

body {
  background:
    radial-gradient(circle at 15% 8%, rgba(180, 111, 124, 0.10), transparent 30%),
    radial-gradient(circle at 90% 88%, rgba(99, 140, 165, 0.10), transparent 34%),
    var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; }
button { color: inherit; }
[hidden] { display: none !important; }

.shelf-shell { width: min(100% - 32px, 880px); margin: 0 auto; padding: 44px 0 96px; }
.shelf-hero { display: flex; align-items: end; justify-content: space-between; gap: 24px; margin-bottom: 34px; }
.eyebrow { color: var(--accent); letter-spacing: .16em; text-transform: uppercase; font-size: 12px; font-weight: 700; }
.shelf-hero h1 { font-family: var(--serif); font-weight: 600; font-size: clamp(32px, 7vw, 58px); margin: 8px 0 8px; letter-spacing: .04em; }
.shelf-hero p { color: var(--muted); margin: 0; font-family: var(--serif); }
.shelf-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.soft-button {
  display: inline-flex; align-items: center; justify-content: center; min-height: 42px; padding: 0 15px;
  color: var(--ink); text-decoration: none; border: 1px solid var(--line); background: var(--card);
  border-radius: 999px; cursor: pointer; box-shadow: 0 8px 22px rgba(0,0,0,.04); font-size: 13px;
}
.theme-button, .icon-button {
  border: 1px solid var(--line); background: var(--card); border-radius: 999px;
  width: 44px; height: 44px; cursor: pointer; box-shadow: 0 8px 22px rgba(0,0,0,.05);
}

.library-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.book-card {
  position: relative; overflow: hidden; min-height: 184px; padding: 22px;
  border: 1px solid var(--line); border-radius: 24px; background: var(--card);
  backdrop-filter: blur(16px); box-shadow: 0 8px 32px rgba(57,44,33,.06);
  cursor: pointer; transition: transform .25s ease, box-shadow .25s ease, border-color .25s ease;
}
.book-card:hover { transform: translateY(-4px); box-shadow: var(--shadow); border-color: rgba(180,111,124,.32); }
.book-card::after {
  content: ""; position: absolute; width: 130px; height: 130px; right: -40px; top: -45px;
  border-radius: 50%; background: linear-gradient(135deg, var(--accent-soft), transparent 72%); opacity: .75;
}
.book-kicker { color: var(--muted); font-size: 12px; margin-bottom: 42px; }
.book-card h2 { position: relative; z-index: 1; font-family: var(--serif); font-size: 22px; font-weight: 600; margin: 0 0 10px; }
.book-progress-copy { color: var(--muted); font-size: 12px; }
.progress-track { height: 4px; border-radius: 999px; background: var(--line); margin-top: 16px; overflow: hidden; }
.progress-track span { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--accent), var(--ai)); }
.empty-card { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 70px 24px; border: 1px dashed var(--line); border-radius: 24px; }

.reader-body { overflow: hidden; }
.reader-app { height: 100dvh; display: grid; grid-template-rows: 58px 1fr 66px; }
.reader-topbar, .reader-bottombar {
  position: relative; z-index: 20; display: flex; align-items: center;
  border-color: var(--line); background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: blur(18px);
}
.reader-topbar { border-bottom: 1px solid var(--line); padding: 0 max(14px, env(safe-area-inset-left)); gap: 10px; }
.reader-bottombar { border-top: 1px solid var(--line); padding: 0 max(12px, env(safe-area-inset-right)); justify-content: space-between; }
.back-link { color: var(--muted); text-decoration: none; font-size: 14px; white-space: nowrap; }
.title-stack { flex: 1; text-align: center; min-width: 0; }
.book-name { font-family: var(--serif); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chapter-name { color: var(--muted); font-size: 10px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-actions { display: flex; gap: 6px; }
.top-actions .icon-button { width: 36px; height: 36px; box-shadow: none; background: transparent; }

.reading-stage { position: relative; min-height: 0; }
.page-viewport {
  height: 100%; overflow: hidden; width: min(100%, 760px); margin: 0 auto;
  padding: clamp(20px, 4vw, 44px) clamp(24px, 7vw, 74px) 22px;
  font-family: var(--serif); font-size: var(--reader-size, 18px); line-height: 2;
  letter-spacing: .035em; touch-action: pan-y; user-select: text;
}
.chapter-heading { font-size: 1.08em; text-align: center; font-weight: 600; letter-spacing: .12em; margin: 0 0 1.7em; }
.paragraph { margin: 0 0 1em; text-indent: 2em; position: relative; }
.paragraph.no-indent { text-indent: 0; text-align: center; color: var(--muted); }
.page-transition { animation: pageIn .22s ease; }
@keyframes pageIn { from { opacity: .25; transform: translateX(var(--page-shift, 10px)); } to { opacity: 1; transform: none; } }

.turn-zone { position: absolute; top: 0; bottom: 0; width: 18%; z-index: 3; border: 0; background: transparent; cursor: pointer; opacity: 0; }
.turn-zone:focus-visible { opacity: 1; outline: 2px solid var(--accent); }
.turn-zone.prev { left: 0; }
.turn-zone.next { right: 0; }

.mark { cursor: pointer; border-radius: 2px; }
.mark-highlight { background: color-mix(in srgb, var(--accent) 25%, transparent); padding: .05em 0; }
.mark-underline { text-decoration-line: underline; text-decoration-color: var(--accent); text-decoration-thickness: 2px; text-underline-offset: .22em; }
.mark-wavy { text-decoration-line: underline; text-decoration-style: wavy; text-decoration-color: var(--accent); text-decoration-thickness: 1.5px; text-underline-offset: .25em; }
.mark.has-ai { --accent: var(--ai); }

.page-counter { min-width: 108px; text-align: center; color: var(--muted); font-size: 12px; cursor: pointer; }
.nav-button { border: 0; background: transparent; padding: 12px; cursor: pointer; color: var(--accent); font-size: 14px; }
.nav-button:disabled { opacity: .28; cursor: default; }

.selection-toolbar {
  position: fixed; display: none; z-index: 60; gap: 4px; padding: 6px;
  background: #292725; color: #fff; border-radius: 14px; box-shadow: 0 12px 32px rgba(0,0,0,.28);
}
.selection-toolbar.visible { display: flex; }
.selection-toolbar button { border: 0; color: inherit; background: transparent; border-radius: 9px; padding: 7px 9px; cursor: pointer; font-size: 12px; }
.selection-toolbar button:hover { background: rgba(255,255,255,.12); }
.tool-swatch { display: block; width: 22px; height: 9px; margin: 0 auto 4px; }
.tool-swatch.underline { border-bottom: 2px solid #f3a6b5; }
.tool-swatch.wavy { text-decoration: underline wavy #f3a6b5; text-underline-offset: 3px; }
.tool-swatch.highlight { background: rgba(243,166,181,.45); }

.overlay { position: fixed; inset: 0; z-index: 40; background: rgba(20,18,16,.28); opacity: 0; pointer-events: none; transition: opacity .2s; }
.overlay.visible { opacity: 1; pointer-events: auto; }
.drawer, .sheet {
  position: fixed; z-index: 50; background: var(--paper); border: 1px solid var(--line); box-shadow: var(--shadow);
  transition: transform .25s ease; overflow: auto;
}
.drawer { top: 0; bottom: 0; right: 0; width: min(88vw, 390px); transform: translateX(105%); padding: 24px 18px; }
.drawer.visible { transform: none; }
.sheet { left: 50%; bottom: 0; width: min(100%, 640px); max-height: 78dvh; transform: translate(-50%, 105%); border-radius: 24px 24px 0 0; padding: 22px; }
.sheet.visible { transform: translate(-50%, 0); }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 18px; }
.panel-head h2 { font-family: var(--serif); font-size: 19px; margin: 0; }
.close-button { border: 0; background: transparent; color: var(--muted); font-size: 22px; cursor: pointer; }
.toc-list { display: grid; gap: 3px; }
.toc-item { border: 0; width: 100%; text-align: left; border-radius: 12px; padding: 11px 12px; background: transparent; color: var(--muted); cursor: pointer; }
.toc-item.current { background: var(--accent-soft); color: var(--ink); font-weight: 600; }

.quote-box { font-family: var(--serif); color: var(--muted); border-left: 3px solid var(--accent); padding: 8px 12px; line-height: 1.7; margin-bottom: 16px; }
.annotation-thread { display: grid; gap: 9px; }
.bubble { width: fit-content; max-width: 88%; border-radius: 16px; padding: 10px 13px; line-height: 1.65; font-size: 14px; }
.bubble.user { justify-self: end; background: var(--accent-soft); border-bottom-right-radius: 5px; }
.bubble.ai { justify-self: start; background: var(--ai-soft); border-bottom-left-radius: 5px; }
.bubble .who { display: block; color: var(--muted); font-size: 10px; margin-bottom: 3px; }
.thread-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; border-top: 1px solid var(--line); margin-top: 18px; padding-top: 14px; color: var(--muted); font-size: 11px; }
.thread-actions button { border: 1px solid var(--line); background: var(--card); color: var(--muted); border-radius: 999px; padding: 7px 11px; cursor: pointer; }
.thread-actions button.active { border-color: var(--accent); color: var(--accent); }
.thread-actions button.danger { margin-left: auto; color: #bd5b5b; }
.annotation-form textarea { width: 100%; min-height: 96px; resize: vertical; padding: 12px; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 14px; outline: none; line-height: 1.6; }
.annotation-form textarea:focus { border-color: var(--accent); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
.form-actions button { border: 0; border-radius: 999px; padding: 10px 18px; cursor: pointer; }
.button-ghost { background: transparent; color: var(--muted); }
.button-primary { background: var(--accent); color: #fff; }
.wide-button { width: 100%; border: 0; border-radius: 14px; padding: 12px 16px; cursor: pointer; }
.wide-button:disabled { opacity: .55; cursor: wait; }
.upload-sheet { padding-bottom: max(28px, env(safe-area-inset-bottom)); }
.upload-sheet .panel-head h2 { margin-top: 5px; }
.private-login, .book-import { display: grid; gap: 15px; }
.private-login p, .form-hint, .form-status { color: var(--muted); font-size: 13px; line-height: 1.65; margin: 0; }
.private-login label, .book-import label { display: grid; gap: 7px; font-size: 13px; }
.book-import label span { color: var(--muted); font-weight: 400; }
.private-login input, .book-import input {
  width: 100%; min-width: 0; padding: 12px 13px; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; outline: none;
}
.private-login input:focus, .book-import input:focus { border-color: var(--accent); }
.book-import input[type="file"] { padding: 10px; }
.form-status { min-height: 22px; margin-top: 14px; color: var(--accent); }
.annotation-list { display: grid; gap: 10px; }
.annotation-card { border: 1px solid var(--line); border-radius: 16px; padding: 13px; cursor: pointer; background: var(--card); }
.annotation-card .meta { color: var(--muted); font-size: 10px; margin-bottom: 6px; }
.annotation-card .anchor { font-family: var(--serif); font-size: 13px; line-height: 1.6; }
.annotation-card .note { color: var(--accent); font-size: 12px; margin-top: 7px; }

.jump-grid { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.jump-grid input { min-width: 0; padding: 11px 12px; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 12px; outline: none; }
.reader-settings { display: flex; align-items: center; justify-content: space-between; gap: 12px; border-top: 1px solid var(--line); margin-top: 18px; padding-top: 18px; }
.size-controls { display: flex; gap: 8px; }
.size-controls button { width: 40px; height: 36px; border: 1px solid var(--line); background: var(--card); border-radius: 10px; cursor: pointer; }
.toast { position: fixed; z-index: 90; left: 50%; bottom: 88px; transform: translate(-50%, 18px); opacity: 0; pointer-events: none; background: #292725; color: #fff; border-radius: 999px; padding: 9px 14px; font-size: 12px; transition: .2s; }
.toast.visible { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 640px) {
  .shelf-shell { width: min(100% - 24px, 880px); padding-top: 30px; }
  .shelf-hero { align-items: flex-start; flex-direction: column; }
  .shelf-actions { justify-content: flex-start; }
  .library-grid { grid-template-columns: 1fr; }
  .book-card { min-height: 154px; }
  .book-kicker { margin-bottom: 28px; }
  .page-viewport { font-size: var(--reader-size, 17px); padding-top: 24px; }
  .reader-app { grid-template-rows: 54px 1fr calc(62px + env(safe-area-inset-bottom)); }
  .reader-bottombar { padding-bottom: env(safe-area-inset-bottom); }
  .back-label { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition-duration: .01ms !important; }
}
