/* ================= 基础变量 ================= */
:root {
  --bg: #f7f5f0;
  --bg-soft: #efece5;
  --card: #ffffff;
  --text: #24221e;
  --text-2: #6f6a62;
  --text-3: #a09a90;
  --line: #e3ded4;
  --accent: #8a6a3f;
  --accent-soft: #f0e6d6;
  --danger: #c2453d;
  --ok: #3f8a5a;
  --shadow: 0 1px 2px rgba(40, 32, 20, .06), 0 6px 18px rgba(40, 32, 20, .06);
  --radius: 14px;
  --tabbar-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

:root[data-theme="dark"] {
  --bg: #14161a;
  --bg-soft: #1c1f25;
  --card: #22262d;
  --text: #e6e3dd;
  --text-2: #a3a09a;
  --text-3: #6f6d68;
  --line: #2e333b;
  --accent: #d0a86a;
  --accent-soft: #33302a;
  --danger: #e06c63;
  --ok: #6dbb8a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .25);
}

/* ================= 重置 ================= */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  min-height: 100vh;
  overflow-wrap: break-word;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
img { display: block; }
h1, h2, h3, h4, p { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }

/* ================= 布局 ================= */
.view {
  min-height: 100vh;
  padding: 0 16px calc(var(--tabbar-h) + var(--safe-b) + 20px);
  max-width: 760px;
  margin: 0 auto;
}
body.reading .view { padding: 0; max-width: none; }
body.reading .tabbar { display: none; }

.page-head {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 52px;
  margin: 0 -16px;
  padding: 0 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.page-head h1 { font-size: 17px; font-weight: 600; flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-head .sub { font-size: 12px; color: var(--text-3); font-weight: 400; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 10px; color: var(--text);
  flex: none;
}
.icon-btn:active { background: var(--bg-soft); }
.icon-btn svg { width: 20px; height: 20px; }

.section { margin-top: 18px; }
.section-title {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; color: var(--text-2); margin-bottom: 8px; font-weight: 500;
}

.empty {
  padding: 56px 20px;
  text-align: center;
  color: var(--text-3);
  font-size: 14px;
  line-height: 1.9;
}
.empty b { display: block; color: var(--text-2); font-size: 15px; margin-bottom: 4px; font-weight: 500; }

/* ================= 底部 Tab ================= */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  display: flex;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  color: var(--text-3);
  font-size: 10px;
  transition: color .15s;
}
.tab svg { width: 22px; height: 22px; }
.tab.active { color: var(--accent); }

/* ================= 通用组件 ================= */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 16px;
  border-radius: 10px;
  background: var(--bg-soft);
  color: var(--text);
  font-size: 14px;
  white-space: nowrap;
  transition: opacity .15s;
}
.btn:active { opacity: .65; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.danger { color: var(--danger); }
.btn[disabled] { opacity: .4; pointer-events: none; }
.btn.sm { height: 30px; padding: 0 10px; font-size: 12px; border-radius: 8px; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 8px; border-radius: 20px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 11px; white-space: nowrap;
}

.input {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  outline: none;
  font-size: 15px;
}
.input:focus { border-color: var(--accent); }
.input::placeholder { color: var(--text-3); }

.row {
  display: flex; align-items: center; gap: 10px;
  min-height: 52px;
  padding: 10px 14px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.row + .row { margin-top: 8px; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-size: 14px; }
.row .desc { font-size: 12px; color: var(--text-3); margin-top: 2px; }
.row.tappable:active { transform: scale(.995); opacity: .8; }

.progress {
  height: 3px;
  border-radius: 2px;
  background: var(--line);
  overflow: hidden;
}
.progress > i {
  display: block; height: 100%; width: 0;
  background: var(--accent);
  transition: width .2s;
}

/* 开关 */
.switch {
  position: relative;
  width: 44px; height: 26px;
  flex: none;
  border-radius: 20px;
  background: var(--line);
  transition: background .2s;
}
.switch::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
  transition: transform .2s;
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(18px); }

/* ================= 书架 ================= */
.shelf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 18px 14px;
  margin-top: 16px;
}
.book-card { position: relative; }
.cover {
  position: relative;
  aspect-ratio: 3 / 4;
  border-radius: 8px;
  overflow: hidden;
  background: linear-gradient(150deg, var(--bg-soft), color-mix(in srgb, var(--accent) 14%, var(--bg-soft)));
  box-shadow: var(--shadow);
}
.cover-img {
  width: 100%; height: 100%;
  object-fit: cover;
  position: absolute; inset: 0;
}
.cover-img.bad { display: none; }
.cover-fallback {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 28px;
  color: color-mix(in srgb, var(--accent) 70%, var(--text-2));
  font-weight: 600;
  letter-spacing: 1px;
}
.book-card .name {
  font-size: 12.5px;
  line-height: 1.4;
  margin-top: 8px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-card .author { font-size: 11px; color: var(--text-3); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-card .badge {
  position: absolute; top: 6px; right: 6px;
  padding: 1px 6px; border-radius: 6px;
  background: rgba(0, 0, 0, .55); color: #fff;
  font-size: 10px; backdrop-filter: blur(4px);
}

/* ================= 搜索 ================= */
.search-bar {
  display: flex; gap: 8px;
  position: sticky; top: 0; z-index: 20;
  padding: 10px 0;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.status-line {
  font-size: 12px; color: var(--text-3);
  padding: 2px 2px 8px;
}
.status-line .err { color: var(--danger); }
.findhint { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-3); }

.result-list { display: flex; flex-direction: column; gap: 8px; }
.result {
  display: flex; gap: 12px;
  padding: 10px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.result .cover { width: 56px; flex: none; border-radius: 6px; }
.result .cover-fallback { font-size: 20px; }
.result .info { flex: 1; min-width: 0; }
.result .name { font-size: 14.5px; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result .meta { font-size: 12px; color: var(--text-3); margin-top: 3px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result .tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.result .src { font-size: 11px; color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ================= 详情 ================= */
.detail-head {
  display: flex; gap: 16px;
  padding: 20px 0 4px;
}
.detail-head .cover { width: 100px; flex: none; border-radius: 10px; }
.detail-head .info { flex: 1; min-width: 0; padding-top: 2px; }
.detail-head h2 { font-size: 19px; line-height: 1.35; font-weight: 600; }
.detail-head .author { font-size: 13px; color: var(--text-2); margin-top: 6px; }
.detail-head .tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.actions {
  display: flex; gap: 10px; margin-top: 18px;
}
.actions .btn { flex: 1; height: 42px; }

.src-picker {
  display: flex; align-items: center; gap: 8px;
  margin-top: 12px;
  font-size: 12px; color: var(--text-3);
}
.src-picker select {
  flex: 1; min-width: 0;
  height: 34px; padding: 0 8px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--text-2);
  font-size: 12px;
  outline: none;
}

.intro {
  font-size: 13px; color: var(--text-2);
  line-height: 1.75;
  white-space: pre-wrap;
  padding: 14px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.intro.fold { max-height: 6.2em; overflow: hidden; }

/* 目录 */
.toc-list {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.toc-item {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px;
  font-size: 13.5px;
  border-bottom: 1px solid var(--line);
}
.toc-item:last-child { border-bottom: 0; }
.toc-item .idx { width: 2.4em; flex: none; font-size: 11px; color: var(--text-3);
  text-align: right; }
.toc-item .t { flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toc-item .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex: none; }
.toc-state {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px 16px;
  text-align: center;
  font-size: 13px;
  color: var(--text-3);
}
.toc-state .err-title { color: var(--danger); font-size: 13.5px; font-weight: 600; margin-bottom: 6px; }
.toc-state .err-msg { margin-bottom: 12px; line-height: 1.55; word-break: break-all; }

/* ================= 阅读器 ================= */
.reader {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  background: var(--r-bg, #f5efe2);
  color: var(--r-text, #332f28);
  z-index: 50;
}
.reader[data-rtheme="paper"] { --r-bg: #f5efe2; --r-text: #332f28; --r-sub: #8d8577; }
.reader[data-rtheme="light"]  { --r-bg: #ffffff; --r-text: #1f1f1f; --r-sub: #9a9a9a; }
.reader[data-rtheme="dark"]   { --r-bg: #121316; --r-text: #b9b6b0; --r-sub: #6a6864; }

.reader-top, .reader-bottom {
  position: absolute; left: 0; right: 0; z-index: 3;
  display: flex; align-items: center; gap: 8px;
  background: color-mix(in srgb, var(--r-bg) 92%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: opacity .22s, transform .22s;
}
.reader-top {
  top: 0; height: 52px;
  padding: 0 8px;
  padding-top: env(safe-area-inset-top, 0px);
  border-bottom: 1px solid color-mix(in srgb, var(--r-text) 12%, transparent);
}
.reader-top .r-title {
  flex: 1; min-width: 0; text-align: center;
  font-size: 14px; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.reader-bottom {
  bottom: 0;
  padding: 8px 12px calc(8px + var(--safe-b));
  border-top: 1px solid color-mix(in srgb, var(--r-text) 12%, transparent);
}
.reader-bottom .btn {
  flex: 1; height: 38px;
  background: color-mix(in srgb, var(--r-text) 8%, transparent);
  color: var(--r-text);
}
.reader-bottom .btn[disabled] { opacity: .3; }
.reader .icon-btn { color: var(--r-text); }
.reader .icon-btn:active { background: color-mix(in srgb, var(--r-text) 10%, transparent); }
.reader.hide-chrome .reader-top { opacity: 0; transform: translateY(-100%); pointer-events: none; }
.reader.hide-chrome .reader-bottom { opacity: 0; transform: translateY(100%); pointer-events: none; }

.reader-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 62px 20px 72px;
  padding-top: calc(62px + env(safe-area-inset-top, 0px));
  padding-bottom: calc(72px + var(--safe-b));
  overscroll-behavior: contain;
}
.reader-body::-webkit-scrollbar { width: 0; }
.article {
  font-size: var(--r-font, 18px);
  line-height: var(--r-lh, 1.9);
  letter-spacing: .01em;
  max-width: 680px;
  margin: 0 auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.article p { margin: 0 0 .85em; text-indent: 2em; }
.article .ch-title {
  font-size: 1.12em; font-weight: 600;
  text-indent: 0; margin-bottom: 1.2em; line-height: 1.5;
}
.article .hint {
  text-indent: 0; text-align: center;
  color: var(--r-sub); font-size: .82em; margin: 1.4em 0;
}
.reader-loading {
  padding: 80px 0; text-align: center;
  color: var(--r-sub); font-size: 13px;
}

/* 阅读设置面板 */
.sheet-mask {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(0, 0, 0, .35);
  display: flex; align-items: flex-end;
  animation: fade .18s ease;
}
.sheet {
  width: 100%;
  background: var(--card);
  border-radius: 18px 18px 0 0;
  padding: 18px 18px calc(18px + var(--safe-b));
  animation: slideUp .22s cubic-bezier(.2, .8, .3, 1);
}
@keyframes fade { from { opacity: 0; } }
@keyframes slideUp { from { transform: translateY(100%); } }
.sheet h3 { font-size: 15px; margin-bottom: 16px; font-weight: 600; }
.sheet .line { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.sheet .line > span { width: 4em; flex: none; font-size: 13px; color: var(--text-2); }
.sheet .line input[type=range] { flex: 1; accent-color: var(--accent); }
.theme-dots { display: flex; gap: 12px; }
.theme-dot {
  width: 34px; height: 34px; border-radius: 50%;
  border: 2px solid transparent;
}
.theme-dot.sel { border-color: var(--accent); }
.theme-dot[data-t="paper"] { background: #f5efe2; }
.theme-dot[data-t="light"] { background: #fff; border-color: var(--line); }
.theme-dot[data-t="light"].sel { border-color: var(--accent); }
.theme-dot[data-t="dark"] { background: #121316; }

/* ================= 书源 ================= */
.toolbar {
  display: flex; gap: 8px;
  padding: 10px 0;
}
.toolbar .input { flex: 1; }
.source-row .name { font-size: 14px; }
.source-row .state { font-size: 11px; margin-top: 3px; }
.source-row .state.ok { color: var(--ok); }
.source-row .state.bad { color: var(--danger); }
.source-row .state.testing { color: var(--text-3); }
.source-row .url { font-size: 11px; color: var(--text-3); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ================= 设置 ================= */
.setting-group {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.setting-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  min-height: 54px;
  padding: 12px 14px;
  text-align: left;
  font-size: 14px;
  border-bottom: 1px solid var(--line);
}
.setting-item:last-child { border-bottom: 0; }
.setting-item .grow { flex: 1; }
.setting-item .val { color: var(--text-3); font-size: 13px; }
.setting-item.danger { color: var(--danger); }

.about {
  margin-top: 24px;
  text-align: center;
  font-size: 12px;
  color: var(--text-3);
  line-height: 2;
}

/* ================= Toast / Modal ================= */
.toast {
  position: fixed;
  left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 24px);
  transform: translate(-50%, 12px);
  z-index: 100;
  max-width: 82vw;
  padding: 9px 18px;
  border-radius: 22px;
  background: rgba(30, 28, 25, .92);
  color: #fff;
  font-size: 13px;
  line-height: 1.5;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
body.reading .toast { bottom: calc(72px + var(--safe-b)); }

.modal-mask {
  position: fixed; inset: 0; z-index: 90;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, .4);
  animation: fade .16s;
}
.modal {
  width: 100%; max-width: 360px;
  background: var(--card);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
  animation: pop .18s ease;
}
@keyframes pop { from { transform: scale(.95); opacity: .5; } }
.modal h3 { font-size: 16px; margin-bottom: 10px; }
.modal p { font-size: 13.5px; color: var(--text-2); line-height: 1.7; }
.modal .modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal .modal-actions .btn { flex: 1; }

/* ================= 无障碍/杂项 ================= */
.sr-only {
  position: absolute; width: 1px; height: 1px;
  margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.spin {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: rot .7s linear infinite;
  vertical-align: -2px;
}
@keyframes rot { to { transform: rotate(360deg); } }

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

@media (min-width: 720px) {
  .view { padding-left: 24px; padding-right: 24px; }
  .shelf-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}
