:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #eef1f5;
  --text: #1c2530;
  --muted: #6b7785;
  --border: #e2e6ec;
  --accent: #2f6df0;
  --accent-soft: #e7f0ff;
  --shadow: 0 6px 24px rgba(20, 40, 80, 0.08);
  --read-bg: #ffffff;
  --read-text: #20262e;
  --read-max: 760px;
}
body[data-theme="sepia"] {
  --bg: #efe6d3; --panel: #f7f0e1; --panel-2: #ece2cb; --text: #4a3f2c;
  --muted: #8a7a5e; --border: #ddd0b4; --accent: #b07d2b; --accent-soft: #f0e6cf;
  --read-bg: #f7f0e1; --read-text: #4a3f2c; --shadow: 0 6px 24px rgba(80,60,20,.12);
}
body[data-theme="dark"] {
  --bg: #14171c; --panel: #1c2128; --panel-2: #232a33; --text: #e6eaf0;
  --muted: #8b97a6; --border: #2c333d; --accent: #5b8dff; --accent-soft: #1f2c44;
  --read-bg: #1a1f26; --read-text: #dde3ec; --shadow: 0 6px 24px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text);
  display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden;
}

/* ===== 顶部栏 ===== */
.topbar {
  flex: 0 0 auto; height: 54px; display: flex; align-items: center;
  justify-content: space-between; padding: 0 16px; background: var(--panel);
  border-bottom: 1px solid var(--border); z-index: 30;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; }
.topbar-center { flex: 1; text-align: center; font-weight: 600; opacity: .9;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 12px; }
.logo { font-weight: 700; letter-spacing: .3px; }
.icon-btn {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 9px; padding: 7px 11px; cursor: pointer; font-size: 14px;
  transition: .15s; white-space: nowrap;
}
.icon-btn:hover { background: var(--accent-soft); border-color: var(--accent); }

/* ===== 视图 ===== */
.view { flex: 1 1 auto; overflow: hidden; display: flex; flex-direction: column; }

/* ===== 书架 ===== */
.lib-head { padding: 18px 22px 6px; }
.lib-head-row { display: flex; gap: 10px; align-items: center; }
.search {
  width: 100%; max-width: 520px; padding: 12px 16px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  font-size: 15px; outline: none;
}
.search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.chips#range-chips { margin-top: 8px; }
.chip {
  padding: 7px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--panel); color: var(--muted); cursor: pointer; font-size: 13px;
  transition: .15s;
}
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.chip.range { font-size: 12px; padding: 5px 12px; opacity: .85; }
#btn-refresh.spinning { animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.lib-meta { padding: 4px 22px 12px; color: var(--muted); font-size: 13px; }
.grid {
  flex: 1; overflow-y: auto; padding: 4px 22px 40px;
  display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
}
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; display: flex; flex-direction: column; gap: 10px;
  box-shadow: var(--shadow); transition: .18s; position: relative;
}
.card:hover { transform: translateY(-3px); border-color: var(--accent); }
.card .cat-tag {
  align-self: flex-start; font-size: 12px; padding: 3px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-weight: 600;
}
.card h3 { margin: 0; font-size: 16px; line-height: 1.35; }
.card .sub { color: var(--muted); font-size: 12.5px; }
.card .read-btn {
  margin-top: auto; background: var(--accent); color: #fff; border: none;
  padding: 9px 12px; border-radius: 10px; cursor: pointer; font-size: 14px; font-weight: 600;
}
.card .read-btn:hover { filter: brightness(1.07); }
.card .cached-dot {
  position: absolute; top: 14px; right: 14px; font-size: 11px; color: var(--muted);
  background: var(--panel-2); padding: 2px 8px; border-radius: 999px;
}

/* ===== 阅读器 ===== */
.reader-loading {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 18px; background: var(--read-bg); z-index: 5;
}
.spinner { width: 38px; height: 38px; border: 4px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress-wrap { width: 240px; height: 6px; background: var(--border); border-radius: 4px; overflow: hidden; }
.progress-bar { height: 100%; width: 0%; background: var(--accent); transition: width .2s; }
.progress-text { color: var(--muted); font-size: 13px; }

.toc {
  flex: 0 0 280px; background: var(--panel); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 14px 8px; display: none;
}
.toc.open { display: block; }
.toc-head { font-weight: 700; padding: 6px 12px 12px; color: var(--muted); font-size: 13px; }
.toc-list a { display: block; padding: 8px 12px; border-radius: 8px; color: var(--text);
  text-decoration: none; font-size: 13.5px; line-height: 1.4; cursor: pointer; }
.toc-list a:hover { background: var(--accent-soft); }

.read-pane { flex: 1; overflow-y: auto; background: var(--read-bg); position: relative; }
.content {
  max-width: var(--read-max); margin: 0 auto; padding: 38px 26px 120px;
  color: var(--read-text); font-size: var(--read-fs, 19px); line-height: 1.85;
  font-family: Georgia, "Times New Roman", "Songti SC", serif;
}
.content h1, .content h2, .content h3 { line-height: 1.3; margin: 1.4em 0 .6em; }
.content p { margin: 0 0 1em; }
.content img { max-width: 100%; height: auto; display: block; margin: 1em auto;
  border-radius: 6px; }
.content a { color: var(--accent); }
.content .chapter-title { font-size: 1.4em; font-weight: 700; margin-top: 0; }
/* 可点击查词的词 */
.content .w { cursor: pointer; border-bottom: 1px dotted transparent; transition: .12s; }
.content .w:hover { border-bottom-color: var(--accent); background: var(--accent-soft); }

/* ===== 单词弹窗 ===== */
.dict-pop {
  position: fixed; z-index: 50; width: min(320px, 92vw); max-height: 60vh; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 12px 40px rgba(20,40,80,.22); padding: 14px 16px;
}
.dict-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dict-word { font-size: 19px; font-weight: 700; }
.dict-close { background: none; border: none; color: var(--muted); font-size: 16px; cursor: pointer; }
.dict-body { margin-top: 8px; font-size: 14px; line-height: 1.6; }
.dict-phon { color: var(--muted); font-style: italic; margin: 2px 0 8px; }
.dict-pos { font-weight: 700; color: var(--accent); margin-top: 8px; }
.dict-def { margin: 2px 0 4px; }
.dict-ex { color: var(--muted); font-style: italic; margin: 0 0 8px; padding-left: 10px;
  border-left: 2px solid var(--border); }
.dict-zh { margin-top: 10px; padding: 8px 10px; background: var(--panel-2);
  border-radius: 8px; }
.dict-foot { display: flex; gap: 8px; margin-top: 12px; }

/* ===== 选中浮标 ===== */
.sel-pop {
  position: fixed; z-index: 60; background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow); padding: 5px; display: flex; gap: 4px;
}
.mini-btn {
  background: var(--accent); color: #fff; border: none; border-radius: 7px;
  padding: 6px 10px; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.mini-btn:hover { filter: brightness(1.08); }
.mini-btn.danger { background: #d9534f; }

/* ===== 生词本抽屉 ===== */
.drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: min(340px, 90vw); z-index: 70;
  background: var(--panel); border-left: 1px solid var(--border); box-shadow: -8px 0 30px rgba(0,0,0,.15);
  display: flex; flex-direction: column;
}
.drawer-head { display: flex; justify-content: space-between; align-items: center;
  padding: 16px; border-bottom: 1px solid var(--border); font-weight: 700; }
.drawer-body { flex: 1; overflow-y: auto; padding: 12px 16px; }
.drawer-foot { padding: 12px 16px; border-top: 1px solid var(--border); }
.vocab-item { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
  margin-bottom: 10px; }
.vocab-item .v-word { font-weight: 700; font-size: 15px; }
.vocab-item .v-def { color: var(--muted); font-size: 13px; margin-top: 3px; line-height: 1.5; }
.vocab-item .v-del { float: right; cursor: pointer; color: #d9534f; font-size: 12px; }

/* ===== 遮罩 & 提示 ===== */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 65; }
.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 90;
  font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.3);
}

.empty { text-align: center; color: var(--muted); padding: 60px 20px; }

/* ============================================================
   响应式：手机 / 窄屏自动适配（<=768px 按手机布局）
   ============================================================ */
@media (max-width: 768px) {
  body { height: 100vh; height: 100dvh; }

  /* 顶部栏：压缩高度与间距，隐藏 logo 文字，按钮变小以容纳更多工具 */
  .topbar { height: 50px; padding: 0 8px; gap: 4px; }
  .logo { display: none; }
  .topbar-left, .topbar-right { gap: 4px; }
  .icon-btn { padding: 6px 8px; font-size: 13px; border-radius: 8px; }
  .topbar-center { font-size: 13px; padding: 0 4px; }
  .toc-label { display: none; }

  /* 书架：缩小左右留白，让卡片更宽 */
  .lib-head { padding: 14px 14px 4px; }
  .lib-head-row { gap: 8px; }
  .lib-meta { padding: 4px 14px 10px; }
  .grid { padding: 4px 14px 26px; gap: 12px; }

  /* 阅读正文：减小边距，留出更大阅读区（字号保持 19px 利于阅读） */
  .content { padding: 20px 16px 110px; }

  /* 目录：桌面是右侧栏，手机改为左侧滑出抽屉 + 遮罩 */
  .toc {
    position: fixed; top: 50px; bottom: 0; left: 0;
    width: 82%; max-width: 320px; z-index: 40;
    display: block; transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,.18);
    border-right: 1px solid var(--border);
  }
  .toc.open { transform: translateX(0); }

  /* 生词本抽屉：手机上接近全宽 */
  .drawer { width: min(340px, 90vw); }
  /* 查词弹窗：不超出屏幕宽度 */
  .dict-pop { width: min(320px, 92vw); }
}

/* 超小屏（iPhone SE 等 < 380px）进一步压缩按钮 */
@media (max-width: 380px) {
  .icon-btn { padding: 6px 6px; font-size: 12.5px; }
}
