:root {
  --paper: #f5f1e8;
  --paper-deep: #ebe4d7;
  --ink: #282621;
  --muted: #777166;
  --line: rgba(56, 50, 42, .14);
  --user: rgba(242, 153, 190, .48);
  --ai: rgba(122, 177, 235, .46);
  --both: rgba(186, 151, 220, .42);
  --accent: #7b5c48;
  --white: #fffdf8;
  --shadow: 0 24px 70px rgba(44, 37, 29, .18);
  font-family: ui-serif, "Songti SC", "Noto Serif CJK SC", Georgia, serif;
}

* { box-sizing: border-box; }
html { background: var(--paper); color: var(--ink); }
body { margin: 0; min-height: 100vh; background: var(--paper); }
button, input, textarea { font: inherit; }
button { color: inherit; }
.hidden { display: none !important; }

.app-shell { min-height: 100vh; }
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 20; height: 68px;
  display: flex; align-items: center; justify-content: space-between;
  padding: max(10px, env(safe-area-inset-top)) 20px 8px;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  border-bottom: 1px solid var(--line); backdrop-filter: blur(16px);
}
.brand { display: flex; align-items: center; gap: 10px; letter-spacing: .08em; }
.brand-mark { width: 32px; height: 32px; display: grid; place-items: center; border: 1px solid var(--ink); border-radius: 50%; font-size: 13px; }
.brand strong, .brand small { display: block; }
.brand strong { font-size: 14px; }
.brand small { font: 8px/1.5 ui-sans-serif, system-ui, sans-serif; letter-spacing: .22em; color: var(--muted); }
.icon-button, .text-button, .nav-button, .chapter-picker { border: 0; background: transparent; cursor: pointer; }
.icon-button { width: 40px; height: 40px; font: 24px/1 ui-sans-serif, sans-serif; }
.text-button { padding: 9px 12px; font-size: 13px; letter-spacing: .08em; }

.reader { min-height: 100vh; padding: 105px 20px 100px; }
.reading-view { width: min(760px, 100%); margin: 0 auto; }
.book-heading { text-align: center; margin: 10px 0 34px; }
.book-heading p { margin: 6px 0; color: var(--muted); font-size: 13px; letter-spacing: .08em; }
.book-heading h1 { margin: 8px 0; font-weight: 500; font-size: clamp(28px, 6vw, 45px); letter-spacing: .06em; }
.chapter-bar { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); min-height: 54px; }
.chapter-picker { padding: 14px; font-size: 16px; }
.nav-button { height: 44px; font-size: 28px; color: var(--muted); }
.nav-button:disabled { opacity: .18; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; justify-content: center; margin: 22px 0 28px; color: var(--muted); font: 11px/1.4 ui-sans-serif, system-ui, sans-serif; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 18px; height: 8px; border-radius: 4px; }
.user-color { background: var(--user); } .ai-color { background: var(--ai); } .both-color { background: var(--both); }
.chapter-text { white-space: pre-wrap; font-size: clamp(17px, 2.3vw, 20px); line-height: 2.08; letter-spacing: .035em; }
.sent { border-radius: 4px; padding: 2px 0; cursor: pointer; transition: background .16s, box-shadow .16s; }
.sent:hover { box-shadow: 0 0 0 4px rgba(90, 76, 60, .06); }
.hl-user { background: linear-gradient(transparent 30%, var(--user) 30%, var(--user) 88%, transparent 88%); }
.hl-ai { background: linear-gradient(transparent 30%, var(--ai) 30%, var(--ai) 88%, transparent 88%); }
.hl-both { background: linear-gradient(transparent 30%, var(--both) 30%, var(--both) 88%, transparent 88%); }

.empty-state { min-height: calc(100vh - 180px); display: grid; place-items: center; align-content: center; text-align: center; max-width: 520px; margin: auto; }
.empty-state > * { margin: 8px 0; }
.empty-state h1 { font-weight: 500; font-size: clamp(30px, 7vw, 48px); }
.empty-state p { color: var(--muted); line-height: 1.9; }
.empty-glyph { font-size: 66px; font-weight: 200; color: #a99b88; }

.shelf { position: fixed; inset: 0 auto 0 0; z-index: 50; width: min(380px, 88vw); background: #ede6da; padding: 34px 24px; transform: translateX(-102%); transition: transform .28s ease; overflow-y: auto; box-shadow: var(--shadow); }
.shelf.open { transform: translateX(0); }
.scrim { position: fixed; inset: 0; z-index: 45; background: rgba(30, 27, 23, .32); opacity: 0; pointer-events: none; transition: opacity .28s; }
.scrim.open { opacity: 1; pointer-events: auto; }
.shelf-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 28px; }
.shelf-heading small, .dialog-card small { font: 9px/1 ui-sans-serif, system-ui, sans-serif; letter-spacing: .23em; color: var(--muted); }
.shelf-heading h2 { margin: 7px 0 0; font-weight: 500; }
.book-list { display: grid; gap: 10px; }
.book-card { text-align: left; border: 1px solid transparent; background: rgba(255,255,255,.38); border-radius: 12px; padding: 15px 16px; cursor: pointer; }
.book-card.active { border-color: rgba(86, 70, 54, .35); background: rgba(255,255,255,.72); }
.book-card strong, .book-card span { display: block; }
.book-card strong { font-weight: 500; margin-bottom: 6px; }
.book-card span { color: var(--muted); font: 11px/1.5 ui-sans-serif, system-ui, sans-serif; }
.token-button { width: 100%; margin-top: 28px; border: 0; background: transparent; color: var(--muted); font-size: 12px; cursor: pointer; }

.primary-button, .secondary-button { min-height: 44px; border-radius: 999px; padding: 10px 20px; cursor: pointer; }
.primary-button { border: 1px solid #3f3931; color: var(--white); background: #3f3931; }
.secondary-button { border: 1px solid rgba(63,57,49,.3); background: transparent; }
.wide { width: 100%; }

.bottom-sheet { position: fixed; z-index: 70; left: 50%; bottom: 0; width: min(720px, 100%); max-height: 88vh; overflow-y: auto; transform: translate(-50%, 105%); transition: transform .3s ease; background: var(--white); border-radius: 24px 24px 0 0; padding: 12px 24px calc(24px + env(safe-area-inset-bottom)); box-shadow: var(--shadow); }
.bottom-sheet.open { transform: translate(-50%, 0); }
.sheet-handle { width: 42px; height: 4px; border-radius: 4px; background: #d4cec3; margin: 0 auto 13px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; font-size: 14px; color: var(--muted); }
blockquote { margin: 12px 0 18px; padding: 15px 17px; background: #f2eee6; border-left: 3px solid #b49d82; border-radius: 3px 10px 10px 3px; line-height: 1.8; }
.field-label { display: block; color: var(--muted); font-size: 12px; margin: 14px 0 7px; }
textarea, input { width: 100%; border: 1px solid rgba(65, 58, 50, .18); border-radius: 10px; background: #fffdfa; padding: 12px 13px; color: var(--ink); outline: none; }
textarea:focus, input:focus { border-color: rgba(65, 58, 50, .5); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.existing-notes { display: grid; gap: 8px; }
.note-row { padding: 10px 12px; border-radius: 10px; background: rgba(240,235,225,.66); font-size: 14px; line-height: 1.65; }
.note-row.ai { border-left: 3px solid #72a9e3; }
.note-row.user { border-left: 3px solid #e38eae; }
.note-row small { display: block; color: var(--muted); font: 10px/1.4 ui-sans-serif, system-ui, sans-serif; margin-bottom: 4px; }

.chat-messages { display: grid; gap: 10px; max-height: 34vh; overflow-y: auto; }
.chat-message { max-width: 88%; padding: 10px 13px; border-radius: 14px; line-height: 1.65; white-space: pre-wrap; }
.chat-message.user { justify-self: end; background: #e9dfd2; }
.chat-message.assistant { justify-self: start; background: #e4efec; }
.chat-composer { display: grid; grid-template-columns: 1fr auto; gap: 9px; margin-top: 14px; align-items: end; }
.handoff-box { margin-top: 12px; }
.handoff-box p { color: var(--muted); font-size: 13px; line-height: 1.7; }

.dialog-card { width: min(460px, calc(100vw - 32px)); border: 0; border-radius: 18px; background: var(--white); padding: 26px; box-shadow: var(--shadow); color: var(--ink); }
.dialog-card::backdrop { background: rgba(30,27,23,.48); backdrop-filter: blur(4px); }
.dialog-card h2 { margin: 10px 0; font-weight: 500; }
.dialog-card p { color: var(--muted); line-height: 1.7; font-size: 13px; }
.dialog-card input { margin: 10px 0; }
.dialog-card label { display: block; margin: 13px 0; font-size: 13px; color: var(--muted); }
.dialog-close { float: right; border: 0; background: transparent; font-size: 24px; cursor: pointer; }
.form-error { min-height: 18px; color: #a44f45 !important; }
.form-help { margin-top: -5px !important; font-size: 11px !important; }
.toast { position: fixed; z-index: 100; left: 50%; bottom: 28px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; background: #302d28; color: white; border-radius: 999px; padding: 11px 17px; font: 13px/1.3 ui-sans-serif, system-ui, sans-serif; transition: .2s; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@media (min-width: 920px) {
  .topbar { padding-left: 32px; padding-right: 32px; }
  .reader { padding-top: 122px; }
}

@media (max-width: 520px) {
  .reader { padding-left: 18px; padding-right: 18px; }
  .legend { gap: 10px; }
  .bottom-sheet { padding-left: 18px; padding-right: 18px; }
  .sheet-actions { grid-template-columns: 1fr; }
  .chapter-text { line-height: 2; }
}
