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

:root {
  --bg: #f7f4ee;
  --fg: #2b2a26;
  --muted: #8a8577;
  --line: #e3ddd0;
  --card: #fffdf8;
  --accent: #8c2f1b;
  --accent-fg: #fff;
  --mark: #f5e0a0;
  --mark-done: #ddd8cb;
  --danger: #b3402e;
  --topbar-h: 50px;
  --bottombar-h: 56px;
  --fs: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171512;
    --fg: #d9d4c7;
    --muted: #807a6c;
    --line: #2d2a24;
    --card: #201d18;
    --accent: #c9714d;
    --accent-fg: #171512;
    --mark: #57481e;
    --mark-done: #34312a;
    --danger: #d0705c;
  }
}

html { background: var(--bg); }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Noto Sans SC", "Helvetica Neue", sans-serif;
  font-size: 16px;
  overscroll-behavior-y: none;
}

button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

::selection { background: rgba(140, 47, 27, .28); }

/* ---------- topbar ---------- */

#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 40;
  display: flex; align-items: center; gap: 2px;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 6px 0;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

#topbar-title {
  flex: 1; text-align: center; font-size: .95rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 2px;
}

.icon-btn { width: 44px; height: 44px; font-size: 1.15rem; border-radius: 10px; flex: none; }
.icon-btn:active { background: var(--line); }

#api-banner {
  position: fixed; z-index: 39; left: 0; right: 0;
  top: calc(var(--topbar-h) + env(safe-area-inset-top));
  background: var(--danger); color: #fff;
  text-align: center; font-size: .78rem; padding: 4px 10px;
}

/* ---------- main / article ---------- */

#view {
  padding-top: calc(var(--topbar-h) + env(safe-area-inset-top) + 10px);
  padding-bottom: 48px;
  min-height: 100vh;
}

body.in-chapter #view {
  padding-bottom: calc(var(--bottombar-h) + env(safe-area-inset-bottom) + 36px);
}

.loading { text-align: center; color: var(--muted); padding: 70px 0; }

#article {
  max-width: 42rem; margin: 0 auto; padding: 0 1.25rem 2rem;
  font-family: "Noto Serif SC", "Source Han Serif SC", "Songti SC", "STSong", serif;
  font-size: var(--fs); line-height: 1.95; letter-spacing: .01em;
}

#article h1 { font-size: 1.45em; margin: 1.1em 0 .9em; text-align: center; line-height: 1.5; }
#article h2 { font-size: 1.02em; color: var(--muted); margin: 1.9em 0 .6em; font-weight: 600; }
#article p { margin: .85em 0; text-align: justify; }
#article hr { border: 0; border-top: 1px solid var(--line); width: 32%; margin: 2.2em auto; }
#article blockquote {
  margin: 1.4em 0; padding: .7em 1em;
  border-left: 3px solid var(--accent);
  background: rgba(140, 47, 27, .05);
  color: var(--muted); font-size: .92em;
}
#article ul, #article ol { padding-left: 1.5em; margin: .8em 0; }
#article li { margin: .3em 0; }
#article table { border-collapse: collapse; margin: 1em 0; font-size: .88em; display: block; overflow-x: auto; max-width: 100%; }
#article th, #article td { border: 1px solid var(--line); padding: .4em .6em; text-align: left; }
#article code { font-size: .9em; background: var(--line); padding: .1em .35em; border-radius: .3em; }

mark.ann {
  background: var(--mark); color: inherit;
  border-radius: .15em; padding: .06em 0; cursor: pointer;
}
mark.ann.done { background: var(--mark-done); }
mark.ann.flash { animation: flash 1.5s ease; }
@keyframes flash {
  0%, 100% { box-shadow: none; }
  25%, 60% { box-shadow: 0 0 0 3px var(--accent); }
}

/* ---------- bottombar ---------- */

#bottombar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  display: flex; gap: 7px;
  padding: 7px 10px calc(7px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
}

#bottombar button {
  flex: 1; height: 42px; border-radius: 10px; font-size: .95rem;
  border: 1px solid var(--line); background: var(--card);
  white-space: nowrap;
}
#bottombar button:active { background: var(--line); }
#bottombar button:disabled { opacity: .35; }

.badge {
  display: inline-block; min-width: 1.55em; padding: .12em .5em;
  border-radius: 999px; background: var(--accent); color: var(--accent-fg);
  font-size: .72rem; text-align: center; vertical-align: .1em;
}

/* ---------- home ---------- */

.home { max-width: 32rem; margin: 0 auto; padding: 20px 20px 30px; }
.home-title { font-size: 1.75rem; text-align: center; margin: 22px 0 6px; letter-spacing: .08em; }
.home-sub { text-align: center; color: var(--muted); font-size: .9rem; margin-bottom: 28px; }

.book-card {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 18px 16px; margin-bottom: 14px;
}
.book-card:active { background: var(--line); }
.book-title { font-size: 1.12rem; font-weight: 700; }
.book-meta { color: var(--muted); font-size: .85rem; margin-top: 5px; }
.book-ok { color: var(--muted); font-size: .8rem; flex: none; }

/* ---------- drawer ---------- */

#drawer-backdrop, #sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .38); z-index: 45;
}

#drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 50;
  width: min(84vw, 22rem);
  background: var(--card);
  border-right: 1px solid var(--line);
  transform: translateX(-102%);
  transition: transform .22s ease;
  overflow-y: auto;
  padding: calc(env(safe-area-inset-top) + 12px) 0 calc(env(safe-area-inset-bottom) + 18px);
}
#drawer.open { transform: translateX(0); }

.drawer-head { padding: 6px 16px 12px; font-weight: 700; font-size: 1.05rem; }
.drawer-book { padding: 13px 16px 9px; font-weight: 600; border-top: 1px solid var(--line); margin-top: 6px; }
.drawer-book.current { color: var(--accent); }
.drawer-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 16px 10px 30px; font-size: .95rem;
}
.drawer-item.current { color: var(--accent); font-weight: 600; }
.drawer-item:active { background: var(--line); }

/* ---------- sheet ---------- */

#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 55;
  max-width: 32rem; margin: 0 auto;
  max-height: 82vh; overflow-y: auto;
  background: var(--card);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -6px 24px rgba(0, 0, 0, .18);
  padding: 16px 16px calc(18px + env(safe-area-inset-bottom));
  transform: translateY(105%);
  transition: transform .22s ease;
}
#sheet.open { transform: translateY(0); }

.sheet-title { font-weight: 700; font-size: 1.05rem; margin-bottom: 12px; }

.quote-preview {
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; font-size: .9rem; color: var(--muted);
  margin-bottom: 14px; line-height: 1.65; word-break: break-all;
}

.tag-group { margin-bottom: 12px; }
.tag-group-name { font-size: .8rem; color: var(--muted); margin-bottom: 7px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); border-radius: 999px;
  padding: .52em .95em; font-size: .86rem; background: var(--bg);
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }

#sheet textarea {
  width: 100%; min-height: 4.5em; resize: vertical;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--fg);
  padding: 10px 12px; font: inherit; font-size: .95rem; line-height: 1.6;
}

.sheet-actions { display: flex; gap: 10px; margin-top: 16px; }

.btn {
  flex: 1; height: 44px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); font-size: .95rem;
}
.btn:active { opacity: .75; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; flex: none; margin-bottom: 12px; }
.btn.small { height: 38px; font-size: .88rem; margin: 0 16px 10px; width: calc(100% - 32px); flex: none; }

/* ---------- annotation rows ---------- */

.ann-row {
  display: flex; gap: 8px; align-items: stretch;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--bg); padding: 10px 12px; margin-bottom: 10px;
}
.ann-row.done { opacity: .55; }
.ann-row-main { flex: 1; min-width: 0; }
.ann-tags { font-size: .78rem; color: var(--accent); margin-bottom: 3px; }
.ann-quote { font-size: .92rem; line-height: 1.55; word-break: break-all; }
.ann-row.done .ann-quote { text-decoration: line-through; }
.ann-note { font-size: .85rem; color: var(--muted); margin-top: 4px; line-height: 1.5; word-break: break-all; }
.ann-row-btns { display: flex; flex-direction: column; gap: 6px; flex: none; }
.row-btn {
  width: 38px; height: 38px; border: 1px solid var(--line);
  border-radius: 9px; background: var(--card); font-size: 1rem;
}
.row-btn.danger { color: var(--danger); }
.empty { color: var(--muted); font-size: .9rem; padding: 14px 2px; line-height: 1.7; }

/* ---------- fab & toast ---------- */

#fab {
  position: fixed; z-index: 70;
  background: var(--accent); color: var(--accent-fg);
  border-radius: 999px; padding: .55em 1.2em; font-size: .95rem; font-weight: 600;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .3);
}

#toast {
  position: fixed; z-index: 90;
  top: calc(var(--topbar-h) + env(safe-area-inset-top) + 12px);
  left: 50%; transform: translateX(-50%);
  background: rgba(20, 18, 15, .84); color: #f5f2ea;
  padding: .55em 1.15em; border-radius: 999px; font-size: .88rem;
  max-width: 86vw; text-align: center;
}
