*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #e9ece7;
  --surface: #f5f4ee;
  --surface-muted: #e2e6df;
  --text: #343832;
  --muted: #6d756d;
  --border: #cfd5cd;
  --accent: #356b5a;
  --accent-hover: #285445;
  --danger: #a44040;
  --reader-bg: #f1eee4;
  --shadow: 0 8px 24px rgba(31, 36, 33, 0.09);
}

[data-dark] {
  --bg: #252925;
  --surface: #2d322d;
  --surface-muted: #383e38;
  --text: #d8d5c8;
  --muted: #a6aa9e;
  --border: #454c45;
  --accent: #78a990;
  --accent-hover: #91bba5;
  --danger: #e08080;
  --reader-bg: #292d28;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.26);
}

[hidden] { display: none !important; }

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  line-height: 1.6;
}

button, input { font: inherit; letter-spacing: 0; }
button { color: inherit; }

#app-header {
  height: 58px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}
#app-header.reader-header { gap: 12px; }
#app-header.reader-header .header-left { flex: 0 1 240px; }
#app-header.reader-header .header-actions { flex: 0 0 auto; }

.header-left, .header-actions, .dict-actions, .font-controls { display: flex; align-items: center; gap: 8px; }
.header-left { min-width: 0; }
#header-title { font-size: 17px; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--surface-muted); }
.icon-btn:disabled { opacity: .55; cursor: wait; }

#app-main { width: 100%; max-width: 1240px; margin: 0 auto; padding: 28px; }
.view { display: none; }
.view.active { display: block; }

.login-wrap { min-height: calc(100vh - 114px); display: grid; place-items: center; }
.login-panel { width: min(390px, 100%); text-align: center; }
.brand-mark { width: 54px; height: 54px; margin: 0 auto 18px; display: grid; place-items: center; background: var(--accent); color: #fff; font: 700 28px Georgia, serif; border-radius: 6px; }
.login-panel h2 { font-size: 24px; margin-bottom: 5px; }
.login-panel > p { color: var(--muted); }
#login-form { margin-top: 26px; display: grid; gap: 15px; text-align: left; }
#login-form label { color: var(--muted); font-size: 13px; }
#login-form input { width: 100%; margin-top: 5px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); outline: none; }
#login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.primary-btn { border: 0; border-radius: 6px; padding: 9px 16px; background: var(--accent); color: #fff; cursor: pointer; }
.primary-btn:hover { background: var(--accent-hover); }
.primary-btn:disabled { opacity: .55; cursor: wait; }
.error-text { min-height: 24px; margin-top: 10px; color: var(--danger) !important; font-size: 13px; }

.page-heading { display: flex; align-items: end; justify-content: space-between; margin-bottom: 22px; }
.page-heading h2 { font-size: 25px; }
.page-heading p, .page-heading > span { color: var(--muted); font-size: 14px; }

.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 22px; }
.book-card { min-width: 0; cursor: pointer; outline: none; }
.book-card:focus-visible .book-cover { outline: 3px solid var(--accent); outline-offset: 3px; }
.book-cover { width: 100%; aspect-ratio: 3 / 4; border-radius: 5px; background: #315b4b; background-position: center; background-size: cover; display: grid; place-items: center; box-shadow: var(--shadow); overflow: hidden; transition: transform .18s ease, box-shadow .18s ease; }
.book-cover span { font: 700 58px Georgia, serif; color: rgba(255,255,255,.85); }
.book-cover.has-image span { display: none; }
.book-card:hover .book-cover { transform: translateY(-3px); box-shadow: 0 13px 30px rgba(31,36,33,.16); }
.book-info { padding: 13px 2px 0; }
.book-info h3 { font-size: 16px; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; min-height: 43px; }
.book-info p { color: var(--muted); font-size: 13px; margin: 5px 0 11px; }
.book-info .primary-btn { width: 100%; }
.status-text { color: var(--muted); padding: 28px; text-align: center; }
.empty-state { min-height: 220px; grid-column: 1 / -1; border: 1px dashed var(--border); display: grid; place-content: center; gap: 5px; text-align: center; color: var(--muted); }
.empty-state strong { color: var(--text); font-size: 17px; }

#app-main:has(.reader-view.active) { max-width: none; padding: 0; }
.reader-view.active { height: calc(100vh - 58px); overflow: hidden; }
.reader-toolbar { min-width: 0; flex: 0 1 auto; display: flex; align-items: center; gap: 8px; }
.reader-toolbar .icon-btn { width: 32px; height: 32px; }
.reader-status { width: clamp(120px, 13vw, 230px); min-width: 0; display: flex; justify-content: center; gap: 12px; font-size: 13px; }
.reader-status strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reader-status span { color: var(--muted); white-space: nowrap; }
.progress-badge { min-width: 52px; height: 32px; padding: 0 9px; display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 5px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.reading-mode-control { height: 32px; display: inline-flex; align-items: center; border: 1px solid var(--border); border-radius: 5px; overflow: hidden; color: var(--muted); font-size: 12px; }
.reading-mode-control > span { padding: 0 8px; border-right: 1px solid var(--border); white-space: nowrap; }
.reading-mode-control select { height: 100%; padding: 0 24px 0 8px; border: 0; outline: 0; background: var(--surface); color: var(--text); cursor: pointer; }
.reader-layout { height: 100%; display: flex; position: relative; }
.reader-view.audio-mode .reader-layout { height: 100%; }
.reader-audio-bar { min-width: 250px; height: 42px; padding-left: 10px; flex: 1 1 360px; display: flex; align-items: center; gap: 7px; border-left: 1px solid var(--border); }
.reader-audio-bar .icon-btn { width: 32px; height: 32px; }
.audio-play-button { width: 38px; height: 38px; flex: 0 0 auto; border: 0; border-radius: 50%; background: var(--accent); color: #f4f2e9; cursor: pointer; }
.audio-now-playing { min-width: 0; flex: 1; display: flex; flex-direction: column; line-height: 1.3; }
.audio-now-playing strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.audio-now-playing span { color: var(--muted); font-size: 11px; }
.audio-speed { height: 34px; padding: 0 7px; border: 1px solid var(--border); border-radius: 5px; background: var(--surface); color: var(--text); }
.audio-mode-control select { height: 32px; padding: 0 8px; border: 1px solid var(--border); border-radius: 5px; background: var(--surface); color: var(--text); cursor: pointer; }
.bilingual-control { height: 32px; display: inline-flex; align-items: center; gap: 5px; padding: 0 8px; border: 1px solid var(--border); border-radius: 5px; color: var(--muted); font-size: 12px; white-space: nowrap; cursor: pointer; }
.bilingual-control input { margin: 0; accent-color: var(--accent); }
.reader-toc { width: 280px; flex: 0 0 280px; background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; }
.toc-heading { height: 52px; padding: 0 12px 0 18px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; background: var(--surface); border-bottom: 1px solid var(--border); z-index: 2; }
.toc-heading h3 { font-size: 15px; }
#reader-toc-close { display: none; }
.toc-link { display: block; width: 100%; padding: 10px 14px; border: 0; border-bottom: 1px solid var(--border); text-align: left; background: transparent; font-size: 13px; cursor: pointer; }
.toc-link:hover { background: var(--surface-muted); color: var(--accent); }
.reader-stage { min-width: 0; flex: 1; position: relative; background: var(--reader-bg); }
#epub-viewer { position: absolute; inset: 0 52px; }
.reader-stage.is-scrolling #epub-viewer { inset: 0; }
.reader-stage.is-scrolling .page-turn { display: none; }
.reader-view.audio-mode .page-turn { display: none; }
.reader-view.audio-mode:not(.audio-playing) .page-turn { display: block; }
.page-turn { position: absolute; top: 0; bottom: 0; width: 52px; border: 0; background: transparent; color: var(--muted); font-size: 38px; cursor: pointer; z-index: 3; }
.page-turn:hover { color: var(--accent); background: color-mix(in srgb, var(--surface) 70%, transparent); }
.page-turn-prev { left: 0; }
.page-turn-next { right: 0; }

.dict-overlay { position: fixed; inset: 0; z-index: 190; background: rgba(12, 15, 13, .42); }
.dict-popup { position: fixed; left: 50%; bottom: 22px; z-index: 200; width: min(720px, calc(100% - 28px)); max-height: min(560px, calc(100vh - 40px)); transform: translateX(-50%); overflow-y: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 7px; box-shadow: 0 20px 55px rgba(0,0,0,.25); }
.dict-head { padding: 17px 22px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--surface) 88%, var(--accent)); }
.dict-label { display: block; color: var(--accent); font-size: 12px; margin-bottom: 2px; }
#dict-word { font: 700 30px/1.1 "Times New Roman", Times, serif; }
.dict-icon-btn { width: 36px; height: 36px; border: 0; border-radius: 5px; background: var(--surface-muted); cursor: pointer; font-size: 18px; }
.dict-icon-btn:hover { color: var(--accent); }
.dict-icon-btn:disabled { opacity: .35; cursor: not-allowed; }
.dict-content { padding: 18px 22px 20px; }
.dictionary-entry { font-family: "Times New Roman", Times, "Microsoft YaHei", sans-serif; font-size: 17px; line-height: 1.62; }
.dict-zh { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif; }
.dictionary-pronunciation { margin-bottom: 12px; padding-bottom: 11px; border-bottom: 1px solid var(--border); }
.phonetic { color: var(--muted); font: 18px/1.4 "Times New Roman", Times, serif; }
.dictionary-sense { display: grid; grid-template-columns: 38px minmax(0, 1fr); gap: 8px; align-items: start; margin: 8px 0; }
.dictionary-sense p { min-width: 0; }
.pos-badge { margin-top: 4px; padding: 1px 5px; border-radius: 3px; background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); font: italic 13px/1.5 "Times New Roman", Times, serif; text-align: center; }
.sense-number { width: 23px; height: 23px; display: grid; place-items: center; border-radius: 50%; background: var(--accent); color: #f5f4ee; font: 13px/1 "Times New Roman", Times, serif; }
.dictionary-example { display: grid; grid-template-columns: 38px minmax(0, 1fr); gap: 8px; margin: 6px 0 6px 46px; color: var(--muted); }
.example-mark { width: 23px; height: 21px; display: grid; place-items: center; border: 1px solid var(--border); border-radius: 3px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif; font-size: 11px; }
.dictionary-detail { margin: 7px 0; }
.dictionary-meta { margin-top: 18px; padding-top: 13px; border-top: 1px solid var(--border); color: var(--muted); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif; font-size: 12px; line-height: 1.7; }
.dictionary-missing { color: var(--muted); }

.bookmarks-list { display: grid; gap: 10px; }
.bookmark-item { padding: 15px 17px; display: flex; align-items: start; justify-content: space-between; gap: 18px; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; }
.word-button { border: 0; background: none; color: var(--accent); font: 700 19px Georgia, serif; cursor: pointer; }
.bookmark-item p { margin-top: 4px; font-size: 14px; }
.bookmark-item small { display: block; max-width: 720px; margin-top: 5px; color: var(--muted); }
.delete-button { width: 30px; height: 30px; border: 0; background: transparent; color: var(--muted); font-size: 20px; cursor: pointer; }
.delete-button:hover { color: var(--danger); }

@media (max-width: 800px) {
  #app-header { height: 52px; padding: 0 10px; }
  #header-title { font-size: 15px; }
  #app-main { padding: 18px 14px; }
  .book-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px 12px; }
  .book-info h3 { font-size: 14px; }
  #app-header.reader-header { height: 100px; flex-wrap: wrap; align-content: center; gap: 6px 10px; }
  #app-header.reader-header:has(.reader-audio-bar:not([hidden])) { height: 148px; }
  #app-header.reader-header .header-left { flex: 1 1 0; }
  #app-header.reader-header .reader-toolbar { order: 3; width: 100%; flex: 0 0 100%; }
  #app-header.reader-header .reader-audio-bar { order: 4; width: 100%; flex: 0 0 100%; border-left: 0; border-top: 1px solid var(--border); padding: 5px 0 0; }
  #app-header.reader-header .header-actions { order: 2; }
  .reader-view.active { height: calc(100vh - 100px); }
  body:has(.reader-view.active.audio-mode) .reader-view.active { height: calc(100vh - 148px); }
  .reader-toc { position: absolute; inset: 0 auto 0 0; z-index: 20; width: min(86vw, 320px); transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow); }
  .reader-toc.open { transform: translateX(0); }
  #reader-toc-close { display: grid; }
  #epub-viewer { inset: 0 32px; }
  .page-turn { width: 32px; font-size: 30px; }
  .reader-status { justify-content: flex-start; }
  .audio-mode-control select { max-width: 92px; padding: 0 4px; font-size: 12px; }
  .dict-popup { bottom: 0; width: 100%; max-height: 55vh; border-radius: 7px 7px 0 0; border-bottom: 0; }
}

@media (max-width: 420px) {
  .header-actions { gap: 4px; }
  .header-actions .icon-btn { width: 32px; height: 32px; }
  .page-heading h2 { font-size: 21px; }
  .book-info .primary-btn { font-size: 13px; }
  .reader-toolbar { gap: 4px; }
  .font-controls { gap: 3px; }
  .reading-mode-control > span { display: none; }
  .reader-status { gap: 5px; }
  .reader-status strong { font-size: 12px; }
  .reader-status { display: none; }
  .bilingual-control { padding: 0 5px; }
  .progress-badge { display: none; }
  .dict-head { align-items: flex-start; }
  .dict-actions { gap: 4px; }
}
