/* ===== 主题变量 ===== */
:root {
  --bg: #f7f5f0;
  --bg2: #ffffff;
  --bg3: #efece4;
  --fg: #2b2820;
  --fg2: #6f6a5e;
  --line: #e2ddd2;
  --accent: #b3552c;
  --accent-soft: #f3e2d8;
  --shadow: 0 4px 24px rgba(60, 50, 30, .12);
  --hl-word: rgba(255, 196, 66, .45);
  --hl-phrase: rgba(120, 200, 130, .40);
  --hl-sentence: rgba(120, 170, 255, .35);
  --radius: 12px;
}
html[data-theme="dark"] {
  --bg: #1d1c19;
  --bg2: #282622;
  --bg3: #32302a;
  --fg: #e8e4da;
  --fg2: #a09a8c;
  --line: #3d3a33;
  --accent: #e0805a;
  --accent-soft: #45322a;
  --shadow: 0 4px 24px rgba(0, 0, 0, .45);
  --hl-word: rgba(255, 196, 66, .30);
  --hl-phrase: rgba(120, 200, 130, .28);
  --hl-sentence: rgba(120, 170, 255, .26);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--fg);
  min-height: 100vh;
  overscroll-behavior-y: none;
}
.hidden { display: none !important; }
.spacer { flex: 1; }

::selection { background: rgba(179, 85, 44, .30); }

/* ===== 高亮（生词本标记） ===== */
::highlight(en-word) { background-color: var(--hl-word); }
::highlight(en-phrase) { background-color: var(--hl-phrase); }
::highlight(en-sentence) { background-color: var(--hl-sentence); }

/* ===== 顶栏 ===== */
#topbar, #readerbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 17px; white-space: nowrap; }
#mainNav { display: flex; gap: 4px; }
.nav-btn {
  border: none; background: none; color: var(--fg2);
  font-size: 15px; padding: 7px 14px; border-radius: 20px; cursor: pointer;
}
.nav-btn.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.icon-btn {
  border: none; background: none; color: var(--fg);
  font-size: 18px; width: 36px; height: 36px; border-radius: 10px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.icon-btn:hover { background: var(--bg3); }
.reader-title {
  font-size: 14px; font-weight: 600; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; max-width: 40vw;
}
.page-ctrl { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--fg2); }
.page-ctrl input {
  width: 52px; padding: 5px 6px; text-align: center;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg2); color: var(--fg); font-size: 13px;
}
.zoom-ctrl { display: flex; }

/* ===== 通用控件 ===== */
.btn {
  border: 1px solid var(--line); background: var(--bg2); color: var(--fg);
  font-size: 14px; padding: 9px 18px; border-radius: 10px; cursor: pointer;
  transition: filter .15s;
}
.btn:hover { filter: brightness(.96); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.danger { color: #c0392b; border-color: #c0392b40; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.big { padding: 13px 24px; font-size: 16px; width: 100%; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--bg2); color: var(--fg2);
  font-size: 13px; padding: 6px 13px; border-radius: 16px; cursor: pointer;
}
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chips.small .chip { padding: 4px 11px; font-size: 12px; }
.hint { color: var(--fg2); font-size: 13px; }

.empty { text-align: center; padding: 70px 20px; color: var(--fg2); }
.empty-icon { font-size: 44px; margin-bottom: 12px; }
.empty p { font-size: 16px; margin: 4px 0; }
.empty .sub { font-size: 13px; }

/* ===== 书架 ===== */
#view-library { max-width: 1100px; margin: 0 auto; padding: 20px 16px 80px; }
.lib-toolbar { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 20px;
}
.book-card {
  background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; position: relative;
  transition: transform .15s, box-shadow .15s;
}
.book-card:hover { transform: translateY(-3px); box-shadow: var(--shadow); }
.book-cover {
  width: 100%; aspect-ratio: 3 / 4; object-fit: cover; display: block;
  background: var(--bg3);
}
.book-cover.placeholder {
  display: flex; align-items: center; justify-content: center;
  font-size: 40px; color: var(--fg2);
}
.book-info { padding: 10px 12px 12px; }
.book-title {
  font-size: 13px; font-weight: 600; line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  min-height: 2.7em;
}
.book-progress { font-size: 12px; color: var(--fg2); margin-top: 6px; }
.book-bar { height: 3px; background: var(--bg3); border-radius: 2px; margin-top: 5px; overflow: hidden; }
.book-bar i { display: block; height: 100%; background: var(--accent); border-radius: 2px; }
.book-del {
  position: absolute; top: 6px; right: 6px; width: 28px; height: 28px;
  border: none; border-radius: 50%; cursor: pointer;
  background: rgba(0,0,0,.45); color: #fff; font-size: 13px;
  opacity: 0; transition: opacity .15s;
}
.book-card:hover .book-del { opacity: 1; }
@media (hover: none) { .book-del { opacity: .8; } }
.drag-over { outline: 3px dashed var(--accent); outline-offset: -3px; border-radius: var(--radius); }

/* ===== 阅读器 ===== */
#view-reader { padding: 14px 0 60px; }
#pages { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.pageEl {
  position: relative; background: #fff;
  box-shadow: 0 2px 12px rgba(40, 30, 10, .14);
  border-radius: 3px; overflow: hidden;
}
html[data-theme="dark"] .pageEl { box-shadow: 0 2px 12px rgba(0,0,0,.5); }
html[data-theme="dark"] .pageEl canvas { filter: brightness(.88); }
.pageEl canvas { display: block; user-select: none; -webkit-user-select: none; }
.page-num {
  position: absolute; bottom: 4px; right: 8px; font-size: 11px;
  color: #999; pointer-events: none; z-index: 3;
}

/* PDF.js 文字层：透明文字覆盖在画布上，用于选中 */
.textLayer {
  position: absolute; inset: 0; overflow: hidden;
  line-height: 1; text-align: initial;
  -webkit-text-size-adjust: none; text-size-adjust: none;
  forced-color-adjust: none; transform-origin: 0 0; z-index: 2;
  caret-color: transparent;
}
.textLayer span, .textLayer br {
  color: transparent; position: absolute; white-space: pre;
  cursor: text; transform-origin: 0% 0%;
  -webkit-user-select: text; user-select: text;
}
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(80, 130, 255, .32); }

/* ===== 选中工具条 ===== */
#selbar {
  position: fixed; z-index: 90; display: flex; overflow: hidden;
  background: var(--fg); border-radius: 10px; box-shadow: var(--shadow);
}
#selbar button {
  border: none; background: none; color: var(--bg);
  font-size: 14px; font-weight: 600; padding: 10px 16px; cursor: pointer;
}
#selbar button + button { border-left: 1px solid color-mix(in srgb, var(--bg) 25%, transparent); }
#selbar button:active { background: rgba(255,255,255,.12); }

/* ===== 翻译面板 ===== */
#panelScrim { position: fixed; inset: 0; z-index: 95; background: rgba(0,0,0,.25); }
#panel {
  position: fixed; z-index: 96; left: 0; right: 0; bottom: 0;
  margin: 0 auto; max-width: 640px; max-height: 78vh;
  background: var(--bg2); border: 1px solid var(--line); border-bottom: none;
  border-radius: 18px 18px 0 0; box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
  animation: slideUp .22s ease-out;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.panel-head {
  display: flex; align-items: center; gap: 6px;
  padding: 12px 16px 8px; font-weight: 700; font-size: 15px;
}
.panel-body { padding: 0 18px 12px; overflow-y: auto; }
.p-text {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 17px; line-height: 1.5; max-height: 110px; overflow-y: auto;
  padding-bottom: 10px; border-bottom: 1px solid var(--line); margin-bottom: 10px;
}
.p-text[contenteditable]:hover { background: var(--bg3); border-radius: 8px; cursor: text; }
.p-text[contenteditable]:focus { outline: 2px solid var(--accent); outline-offset: 2px; background: var(--bg3); border-radius: 8px; }
.p-edit-hint { font-size: 11.5px; color: var(--fg2); margin: -4px 0 10px; }
.p-phonetic { color: var(--fg2); font-size: 14px; margin-bottom: 8px; font-family: Georgia, serif; }
.p-trans { font-size: 16px; line-height: 1.55; margin-bottom: 10px; }
.p-trans .loading { color: var(--fg2); font-size: 14px; }
.p-trans .err { color: #c0392b; font-size: 14px; }
.p-trans .retry { margin-left: 8px; }
.p-dict { font-size: 13.5px; color: var(--fg2); line-height: 1.7; margin-bottom: 12px; }
.p-dict b { color: var(--accent); font-weight: 600; font-style: italic; margin-right: 6px; }
.dict-sec { margin-bottom: 4px; }
.dict-h {
  font-size: 11px; font-weight: 700; color: var(--accent);
  letter-spacing: 1px; margin: 10px 0 4px;
}
.dict-def { color: var(--fg); margin-bottom: 4px; }
.dict-ex {
  color: var(--fg2); font-family: Georgia, "Times New Roman", serif;
  font-style: italic; margin: 2px 0 7px; line-height: 1.6;
}
.dict-ex b { color: var(--fg); font-style: normal; background: var(--hl-word); border-radius: 3px; margin: 0; }
.dict-syns { line-height: 1.9; }
.p-meta { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.p-label { font-size: 12px; color: var(--fg2); }
#panelNote {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--fg); font-size: 14px; line-height: 1.5;
  padding: 9px 12px; resize: vertical; font-family: inherit;
}
.p-source { font-size: 12px; color: var(--fg2); margin-top: 8px; }
.panel-foot {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 18px 14px; border-top: 1px solid var(--line);
}

/* ===== 生词本 ===== */
#view-vocab { max-width: 760px; margin: 0 auto; padding: 18px 16px 80px; }
.vocab-head { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.filter-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
#bookFilter {
  border: 1px solid var(--line); background: var(--bg2); color: var(--fg);
  font-size: 13px; padding: 7px 10px; border-radius: 10px; max-width: 180px;
}
#vocabSearch {
  flex: 1; min-width: 150px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg2); color: var(--fg); font-size: 14px; padding: 8px 13px;
}
.stats { font-size: 13px; color: var(--fg2); }
.vocab-list { display: flex; flex-direction: column; gap: 10px; }
.v-item {
  background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 13px 15px; cursor: pointer; transition: box-shadow .15s;
}
.v-item:hover { box-shadow: var(--shadow); }
.v-top { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.v-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 8px; font-weight: 600; flex: none;
}
.v-badge.word { background: var(--hl-word); }
.v-badge.phrase { background: var(--hl-phrase); }
.v-badge.sentence { background: var(--hl-sentence); }
.v-text { font-family: Georgia, serif; font-size: 16px; font-weight: 600; line-height: 1.4; }
.v-trans { font-size: 14px; margin-top: 5px; line-height: 1.5; }
.v-note { font-size: 13px; color: var(--fg2); margin-top: 5px; line-height: 1.5; }
.v-note::before { content: "✏️ "; }
.v-meta { font-size: 12px; color: var(--fg2); margin-top: 7px; display: flex; gap: 10px; align-items: center; }
.v-meta .spacer { flex: 1; }
.v-actions { display: flex; gap: 2px; }
.v-actions .icon-btn { width: 30px; height: 30px; font-size: 14px; }

/* ===== 复习 ===== */
#view-review { max-width: 560px; margin: 0 auto; padding: 24px 16px 80px; }
.review-hero { text-align: center; padding: 30px 0 20px; }
.review-counts { font-size: 15px; color: var(--fg2); line-height: 1.8; }
.review-counts b { color: var(--accent); font-size: 22px; }
.review-options { display: flex; flex-direction: column; gap: 12px; align-items: center; }
.review-options .chips { justify-content: center; margin-bottom: 6px; }
.review-progress {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; color: var(--fg2); margin-bottom: 14px;
}
.review-card {
  position: relative; background: var(--bg2); border: 1px solid var(--line);
  border-radius: 16px; box-shadow: var(--shadow);
  padding: 34px 24px; min-height: 260px; text-align: center;
  display: flex; flex-direction: column; justify-content: center; gap: 14px;
}
.rc-type { font-size: 12px; color: var(--fg2); }
.rc-text { font-family: Georgia, serif; font-size: 24px; font-weight: 700; line-height: 1.4; }
.rc-context { font-family: Georgia, serif; font-size: 14px; color: var(--fg2); line-height: 1.6; font-style: italic; }
.rc-context b { color: var(--fg); background: var(--hl-word); border-radius: 3px; font-style: normal; }
.rc-speak { position: absolute; top: 10px; right: 10px; }
.rc-answer { border-top: 1px dashed var(--line); padding-top: 14px; }
.rc-trans { font-size: 18px; line-height: 1.5; }
.rc-note { font-size: 13.5px; color: var(--fg2); margin-top: 8px; line-height: 1.5; }
.review-actions { margin-top: 18px; }
.grade-btns { display: flex; gap: 10px; }
.grade { flex: 1; padding: 13px 0; font-size: 15px; font-weight: 600; border-radius: 12px; }
.grade.bad { color: #c0392b; }
.grade.mid { color: #b07d1a; }
.grade.good { color: #1f8a4c; }

/* ===== Toast ===== */
#toast {
  position: fixed; z-index: 120; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: var(--fg); color: var(--bg); font-size: 14px;
  padding: 10px 20px; border-radius: 20px; box-shadow: var(--shadow);
  max-width: 84vw; text-align: center;
  animation: slideUp .2s ease-out;
}

/* ===== 云同步 ===== */
#authScrim { position: fixed; inset: 0; z-index: 110; background: rgba(0, 0, 0, .35); }
#authModal {
  position: fixed; z-index: 111; left: 50%; top: 46%; transform: translate(-50%, -50%);
  width: min(92vw, 400px); background: var(--bg2);
  border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
  animation: slideUp .2s ease-out;
}
.auth-body { padding: 4px 20px 18px; }
#authLoggedOut, #authLoggedIn { display: flex; flex-direction: column; gap: 10px; }
#authLoggedOut.hidden, #authLoggedIn.hidden { display: none; }
.auth-desc { font-size: 14px; line-height: 1.6; }
.auth-desc.sub { color: var(--fg2); font-size: 13px; }
.auth-body input {
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
  color: var(--fg); font-size: 15px; padding: 10px 13px; width: 100%;
}
.auth-error { color: #c0392b; font-size: 13px; min-height: 1.1em; }
.auth-btns { display: flex; gap: 10px; }
.auth-btns .btn { flex: 1; }
.auth-note { font-size: 12px; color: var(--fg2); line-height: 1.6; }
.book-cloud {
  position: absolute; top: 8px; left: 8px; font-size: 11px;
  background: rgba(0, 0, 0, .55); color: #fff; padding: 3px 8px; border-radius: 10px;
  pointer-events: none;
}

/* ===== 移动端适配 ===== */
@media (max-width: 640px) {
  #topbar { padding-left: 12px; padding-right: 12px; }
  .brand { display: none; }
  .lib-toolbar { flex-wrap: wrap; }
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 14px; }
  .reader-title { display: none; }
  #panel { max-height: 88vh; }
  .rc-text { font-size: 21px; }
}
