/* ============ 基础与主题 ============ */
:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --fg: #26282e;
  --muted: #9096a0;
  --line: #e8eaef;
  --accent: #4e6ef2;
  --accent-weak: #eef1fe;
  --danger: #e5544f;
  --r-fs: 19px;
  --r-lh: 1.8;
  --r-ff: Georgia, "Noto Serif SC", "Source Han Serif SC", "STSong", "SimSun", serif;
  --r-bg: #ffffff;
  --r-fg: #2b2d33;
  --r-muted: #9aa0aa;
  --r-panel: #ffffff;
  --r-line: #ececef;
  --shadow: 0 8px 30px rgba(25, 34, 60, .08);
  --veil: rgba(247, 248, 250, .82);
  --auth-veil-a: rgba(255, 253, 248, .35);
  --auth-veil-b: rgba(80, 62, 38, .22);
}
body.theme-sepia {
  --bg: #f4ecda; --panel: #faf3e3; --fg: #4b3d2d; --muted: #a0927a;
  --line: #e4d9c0; --accent: #b8863b; --accent-weak: #f3e8cf;
  --r-bg: #f7f0de; --r-fg: #4b3d2d; --r-muted: #a3947c; --r-panel: #faf3e3; --r-line: #e7dcc2;
  --veil: rgba(244, 236, 218, .84);
  --auth-veil-a: rgba(250, 243, 227, .40);
  --auth-veil-b: rgba(87, 66, 38, .26);
}
body.theme-green {
  --bg: #e6efe4; --panel: #eff5ed; --fg: #2e3b2e; --muted: #7f9480;
  --line: #d3e0d0; --accent: #3f8f5a; --accent-weak: #dcebdc;
  --r-bg: #dce8d8; --r-fg: #2e3b2e; --r-muted: #7f9480; --r-panel: #eff5ed; --r-line: #cdddd0;
  --veil: rgba(230, 239, 228, .84);
  --auth-veil-a: rgba(238, 246, 235, .40);
  --auth-veil-b: rgba(52, 74, 52, .26);
}
body.theme-dark {
  --bg: #121419; --panel: #1c1f26; --fg: #e7e9ee; --muted: #7f8593;
  --line: #2a2e37; --accent: #6b83f6; --accent-weak: #232838;
  --r-bg: #121419; --r-fg: #aab0bc; --r-muted: #6f7684; --r-panel: #1c1f26; --r-line: #262a33;
  --shadow: 0 8px 30px rgba(0, 0, 0, .45);
  --veil: rgba(15, 17, 22, .80);
  --auth-veil-a: rgba(13, 14, 19, .66);
  --auth-veil-b: rgba(13, 14, 19, .48);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { overscroll-behavior: none; }
html, body { height: 100%; }
body {
  font-family: system-ui, -apple-system, "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--fg);
  overflow: hidden; touch-action: manipulation;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
[hidden] { display: none !important; }
::selection { background: var(--accent-weak); }

.view { position: fixed; inset: 0; display: flex; flex-direction: column; background: var(--bg); color: var(--fg); }

/* ============ 登录 / 注册 ============ */
#view-auth {
  align-items: center; justify-content: center;
  padding:
    calc(24px + env(safe-area-inset-top))
    calc(24px + env(safe-area-inset-right))
    calc(24px + env(safe-area-inset-bottom))
    calc(24px + env(safe-area-inset-left));
  background:
    linear-gradient(180deg, var(--auth-veil-a), var(--auth-veil-b)),
    url("/static/img/login-bg.jpg") center / cover no-repeat;
}
.auth-wrap { width: min(88vw, 340px); text-align: center; }
.auth-logo {
  width: 64px; height: 64px; margin: 0 auto 14px; border-radius: 18px;
  background: linear-gradient(135deg, #5b7bfa, #3d55d8); color: #fff;
  font-size: 30px; font-weight: 700; display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
}
.auth-brand h1 { font-size: 26px; letter-spacing: 8px; text-indent: 8px; }
.auth-brand p { color: var(--muted); font-size: 13px; margin: 6px 0 22px; }
.auth-card { background: var(--panel); border-radius: 16px; padding: 22px 20px; box-shadow: var(--shadow); text-align: left; }
.seg { display: flex; background: var(--bg); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 8px 0; border-radius: 8px; font-size: 14px; transition: color .15s; }
.seg button.on { background: var(--panel); color: var(--fg); font-weight: 600; box-shadow: 0 1px 4px rgba(0, 0, 0, .1); }
#auth-form input {
  width: 100%; margin-top: 12px; padding: 12px 14px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--fg); outline: none;
  transition: border-color .15s;
}
#auth-form input:focus { border-color: var(--accent); }
.err { color: var(--danger); font-size: 13px; min-height: 20px; margin: 8px 0 2px; }
.btn-primary {
  width: 100%; border: 0; background: var(--accent); color: #fff;
  font-size: 16px; padding: 12px; border-radius: 10px; font-weight: 600; letter-spacing: 3px;
  transition: opacity .15s;
}
.btn-primary:active { opacity: .82; }
.btn-primary.small { width: auto; padding: 8px 18px; font-size: 14px; letter-spacing: 1px; }

/* ============ 书架 ============ */
#view-shelf {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  background:
    linear-gradient(var(--veil), var(--veil)),
    url("/static/img/shelf-bg.jpg") center / cover no-repeat;
}
.shelf-header { display: flex; align-items: center; padding: calc(14px + env(safe-area-inset-top)) 20px 10px; }
.brand { font-size: 22px; font-weight: 800; letter-spacing: 4px; }
.user-chip { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.user-chip button { border: 1px solid var(--line); background: var(--panel); color: var(--muted); border-radius: 8px; padding: 5px 10px; font-size: 12px; }
.shelf-body { max-width: 640px; width: 100%; margin: 0 auto; padding: 6px 18px calc(30px + env(safe-area-inset-bottom)); }

.continue-card {
  display: flex; gap: 14px; align-items: center;
  background: var(--panel); border-radius: 14px; padding: 14px; margin: 8px 0 6px; box-shadow: var(--shadow);
  cursor: pointer;
}
.cc-cover {
  width: 54px; height: 74px; border-radius: 8px; flex: none;
  display: flex; align-items: center; justify-content: center;
  color: rgba(255, 255, 255, .92); font-weight: 700; font-size: 20px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .2); box-shadow: 0 4px 10px rgba(0, 0, 0, .18);
}
.cc-info { min-width: 0; flex: 1; }
.cc-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cc-sub { font-size: 12px; color: var(--muted); margin: 4px 0 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 18px 14px; padding: 14px 0; }
.book { cursor: pointer; user-select: none; -webkit-user-select: none; }
.book .cover {
  aspect-ratio: 5 / 7; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: rgba(255, 255, 255, .92); font-size: 24px; font-weight: 700; letter-spacing: 2px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .25); box-shadow: 0 6px 14px rgba(20, 30, 60, .18);
  transition: transform .15s;
}
.book:active .cover { transform: scale(.95); }
@media (hover: hover) { .book:hover .cover { transform: translateY(-3px); } }
.btitle { font-size: 13px; margin-top: 8px; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.bmeta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.add-tile .cover { background: transparent; border: 1.5px dashed var(--line); color: var(--muted); font-size: 34px; font-weight: 300; box-shadow: none; text-shadow: none; }
.add-tile .btitle { color: var(--muted); text-align: center; }
.empty { text-align: center; color: var(--muted); font-size: 13px; padding: 40px 0; }

/* ============ 通用弹层（书架侧） ============ */
.mask {
  position: fixed; inset: 0; background: rgba(10, 12, 18, .45); z-index: 40;
  opacity: 0; pointer-events: none; transition: opacity .25s;
}
.mask.on { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  background: var(--panel); border-radius: 18px 18px 0 0;
  padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
  transform: translateY(105%); transition: transform .28s ease; max-width: 640px; margin: 0 auto;
}
.sheet.on { transform: none; }
.sheet-grip { width: 36px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 10px; }
.sheet-btn {
  display: block; width: 100%; text-align: center; padding: 13px; font-size: 15px;
  background: transparent; border: 0; border-bottom: 1px solid var(--line); color: var(--fg);
}
.sheet-btn:last-of-type { border-bottom: 0; }
.sheet-btn.danger { color: var(--danger); }

.uploading {
  position: fixed; inset: 0; z-index: 70; background: rgba(10, 12, 18, .55);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; color: #fff;
}
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, .25); border-top-color: #fff; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ 阅读器 ============ */
#view-reader { background: var(--r-bg); color: var(--r-fg); max-width: 900px; margin: 0 auto; }
@media (min-width: 901px) { #view-reader { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }
#reader-body { position: relative; flex: 1; overflow: hidden; }

#reader-scroll { position: absolute; inset: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; display: none; scrollbar-width: none; }
#reader-scroll::-webkit-scrollbar { width: 0; }
#page-viewport { position: absolute; inset: 0; overflow: hidden; display: none; }
#view-reader.mode-scroll #reader-scroll { display: block; }
#view-reader.mode-page #page-viewport { display: block; }

#scroll-inner { max-width: 680px; margin: 0 auto; }
#page-flow { height: 100%; position: relative; will-change: transform; transition: transform .26s ease; }
#page-flow.no-anim { transition: none; }

/* 正文内边距避开刘海/灵动岛、状态栏与 home 指示条（需 viewport-fit=cover） */
.rcontent {
  padding:
    calc(20px + env(safe-area-inset-top))
    calc(16px + env(safe-area-inset-right))
    calc(30px + env(safe-area-inset-bottom))
    calc(16px + env(safe-area-inset-left));
  font-size: var(--r-fs); line-height: var(--r-lh); font-family: var(--r-ff);
}
.rcontent p { text-indent: 2em; margin: 0 0 .7em; text-align: justify; word-break: break-word; }
.ch-head { font-size: 1.22em; font-weight: 700; margin: 8px 0 1.1em; text-indent: 0; text-align: center; }
.ch-nav { display: flex; justify-content: space-between; gap: 12px; margin: 26px 0 10px; }
.ch-nav button { flex: 1; padding: 12px; border: 1px solid var(--r-line); background: var(--r-panel); color: var(--r-fg); border-radius: 10px; font-size: 14px; }
.ch-nav button:disabled { opacity: .35; }
.loading { color: var(--r-muted); text-align: center; padding-top: 40%; text-indent: 0; }

/* 顶栏 / 底栏 */
#r-topbar, #r-toolbar {
  position: absolute; left: 0; right: 0; z-index: 20;
  background: var(--r-panel); transition: transform .26s ease;
}
#r-topbar {
  top: 0; display: flex; align-items: center; gap: 6px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  transform: translateY(-102%); box-shadow: 0 1px 0 var(--r-line);
}
#r-toolbar {
  bottom: 0; padding: 10px 14px calc(12px + env(safe-area-inset-bottom));
  transform: translateY(102%); box-shadow: 0 -1px 0 var(--r-line);
}
#view-reader.chrome-on #r-topbar, #view-reader.chrome-on #r-toolbar { transform: none; }
.icon-btn {
  width: 40px; height: 38px; border: 0; background: transparent; color: var(--r-fg);
  border-radius: 10px; display: flex; align-items: center; justify-content: center; flex: none;
}
.r-titles { flex: 1; min-width: 0; text-align: center; }
.rt-book { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rt-ch { font-size: 11px; color: var(--r-muted); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rt-pct { width: 40px; flex: none; font-size: 11px; color: var(--r-muted); text-align: right; }

.tb-row { display: flex; align-items: center; gap: 10px; }
.tb-slider { margin-bottom: 8px; }
.tb-side { border: 0; background: transparent; color: var(--r-fg); font-size: 13px; padding: 6px 4px; flex: none; }
#tb-range { flex: 1; accent-color: var(--accent); height: 26px; min-width: 0; }
.tb-actions { justify-content: space-around; }
.tb-actions button { border: 0; background: transparent; color: var(--r-fg); font-size: 13px; padding: 8px 8px; letter-spacing: 1px; }

/* 目录抽屉 */
#r-mask {
  position: absolute; inset: 0; z-index: 25; background: rgba(8, 10, 16, .4);
  opacity: 0; pointer-events: none; transition: opacity .25s;
}
#view-reader.catalog-on #r-mask, #view-reader.sheet-on #r-mask { opacity: 1; pointer-events: auto; }
#catalog {
  position: absolute; top: 0; bottom: 0; left: 0; width: min(80vw, 320px); z-index: 30;
  background: var(--r-panel); color: var(--r-fg);
  transform: translateX(-104%); transition: transform .28s ease;
  display: flex; flex-direction: column; box-shadow: 6px 0 24px rgba(0, 0, 0, .12);
}
#view-reader.catalog-on #catalog { transform: none; }
.cat-head { padding: calc(14px + env(safe-area-inset-top)) 16px 12px; border-bottom: 1px solid var(--r-line); }
.cat-title { font-weight: 700; font-size: 16px; margin-bottom: 10px; }
.cat-title span { font-size: 12px; color: var(--r-muted); font-weight: 400; margin-left: 8px; }
#cat-search {
  width: 100%; padding: 9px 12px; border: 1px solid var(--r-line); border-radius: 9px;
  background: var(--r-bg); color: var(--r-fg); font-size: 14px; outline: none;
}
#cat-list { flex: 1; overflow-y: auto; padding: 6px 0 calc(20px + env(safe-area-inset-bottom)); -webkit-overflow-scrolling: touch; scrollbar-width: none; }
#cat-list::-webkit-scrollbar { width: 0; }
.cat-vol { padding: 14px 16px 6px; font-size: 12px; color: var(--r-muted); font-weight: 600; letter-spacing: 1px; }
.cat-item {
  display: block; width: 100%; text-align: left; padding: 11px 16px; font-size: 14px;
  background: transparent; border: 0; color: var(--r-fg);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cat-item.active { color: var(--accent); background: var(--accent-weak); font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
.cat-empty { text-align: center; color: var(--r-muted); font-size: 13px; padding: 30px 0; }

/* 设置面板 */
#settings-sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--r-panel); color: var(--r-fg);
  border-radius: 18px 18px 0 0; transform: translateY(105%); transition: transform .28s ease;
  padding: 18px 20px calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .15);
}
#view-reader.sheet-on #settings-sheet { transform: none; }
.ss-row { display: flex; align-items: center; min-height: 54px; gap: 14px; }
.ss-row + .ss-row { border-top: 1px solid var(--r-line); }
.ss-label { width: 44px; font-size: 13px; color: var(--r-muted); flex: none; }
.theme-dots { display: flex; gap: 16px; }
.dot { width: 34px; height: 34px; border-radius: 50%; border: 0; }
.dot.on { outline: 2px solid var(--accent); outline-offset: 2px; }
.t-light { background: #ffffff; box-shadow: inset 0 0 0 1px var(--r-line); }
.t-sepia { background: #f0e6cd; }
.t-green { background: #d8e6d3; }
.t-dark { background: #171a21; box-shadow: inset 0 0 0 1px var(--r-line); }
.stepper { display: flex; align-items: center; border: 1px solid var(--r-line); border-radius: 10px; overflow: hidden; }
.stepper button { width: 52px; height: 40px; border: 0; background: transparent; color: var(--r-fg); font-size: 14px; }
.stepper button:active { background: var(--r-bg); }
.stepper span { width: 56px; text-align: center; font-size: 14px; }
#settings-sheet .seg { background: var(--r-bg); flex: 1; }
#settings-sheet .seg button.on { background: var(--r-panel); }

/* ============ 通用浮层 ============ */
#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(8px);
  background: rgba(28, 30, 38, .92); color: #fff; font-size: 13px;
  padding: 9px 16px; border-radius: 20px; opacity: 0; pointer-events: none;
  transition: all .25s; z-index: 99; max-width: 78vw; text-align: center;
}
#toast.on { opacity: 1; transform: translateX(-50%); }

.modal { position: fixed; inset: 0; z-index: 60; background: rgba(8, 10, 16, .5); display: flex; align-items: center; justify-content: center; padding: 30px; }
.modal-card { width: min(80vw, 320px); background: var(--panel); border-radius: 14px; padding: 22px 20px 14px; text-align: center; }
.modal-title { font-weight: 700; font-size: 16px; margin-bottom: 8px; }
.modal-msg { font-size: 13px; color: var(--muted); line-height: 1.6; margin-bottom: 18px; }
.modal-btns { display: flex; gap: 10px; }
.modal-btns button { flex: 1; padding: 11px; border-radius: 10px; border: 0; font-size: 14px; }
#modal-cancel { background: var(--bg); color: var(--fg); }
#modal-ok { background: var(--danger); color: #fff; font-weight: 600; }
