/* ========== Reset & Themes ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #f5f0e8; --card: #faf7f0; --text: #3d3226; --text-secondary: #8b7b68;
  --accent: #b5443c; --accent-hover: #93302a; --border: #e0d6c8;
  --shadow: 0 1px 4px rgba(0,0,0,0.06);
  --reading-bg: #faf7f0; --reading-text: #3d3226; --topbar-bg: #faf7f0;
  --active-paragraph: #fdf0e6;
  --hero-bg: linear-gradient(180deg, #f5f0e8 0%, #ede4d6 40%, #e5d9c5 100%);
  --ink-color: #2c2416;
}
body.theme-sepia {
  --bg: #f4ecd8; --card: #faf3e0; --text: #5b4636; --text-secondary: #8b7355;
  --accent: #b85c1a; --border: #d4c5a9;
  --reading-bg: #f4ecd8; --reading-text: #4a3728; --topbar-bg: #faf3e0;
  --active-paragraph: #ffe0b2;
}
body.theme-dark {
  --bg: #1a1612; --card: #231f1a; --text: #d4ccbe; --text-secondary: #8a8070;
  --accent: #c77d4b; --border: #3a3228; --shadow: 0 1px 4px rgba(0,0,0,0.4);
  --reading-bg: #1a1612; --reading-text: #c4bbab; --topbar-bg: #231f1a;
  --active-paragraph: #2a2018;
}
body.theme-green {
  --bg: #e8efe0; --card: #f5faf2; --text: #2d3a28; --text-secondary: #6b7a65;
  --accent: #4a7a32; --border: #c8d8c0;
  --reading-bg: #e8efe0; --reading-text: #2d3a28; --topbar-bg: #f5faf2;
  --active-paragraph: #d4e8c8;
}
body.theme-blue {
  --bg: #e8edf5; --card: #f5f8fc; --text: #1a2a3a; --text-secondary: #7a8a9a;
  --accent: #3c6ea8; --border: #c8d4e4;
  --reading-bg: #e8edf5; --reading-text: #1a2a3a; --topbar-bg: #f5f8fc;
  --active-paragraph: #d0e0f5;
}

@font-face {
  font-family: 'Classical Serif';
  src: local('KaiTi'), local('STKaiti'), local('SimSun'), local('Songti SC'), local('serif');
}

html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent; transition: background 0.3s;
}
#app { height: 100%; display: flex; flex-direction: column; }

/* Pages */
.page { display: none; flex-direction: column; height: 100%; overflow: hidden; }
.page.active { display: flex; }

/* ========== Hero (classical) ========== */
.hero {
  position: relative; padding: 36px 24px 20px;
  background: var(--hero-bg, linear-gradient(180deg, #f5f0e8, #ede4d6));
  flex-shrink: 0; overflow: hidden;
  border-bottom: 1px solid var(--border);
}
.hero-ink {
  position: absolute; top: -40px; right: -30px; width: 180px; height: 180px;
  background: radial-gradient(ellipse at 40% 50%, rgba(181,68,60,0.12) 0%, transparent 70%);
  pointer-events: none;
}
.hero::after {
  content: ''; position: absolute; bottom: 0; left: 0; right: 0; height: 1px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  opacity: 0.3;
}
.hero-content { position: relative; z-index: 1; text-align: center; }
.hero-title {
  font-family: 'Classical Serif', 'KaiTi', 'STKaiti', serif;
  font-size: 32px; font-weight: 400; letter-spacing: 0.3em;
  color: var(--ink-color, #2c2416); margin-bottom: 6px;
}
.hero-sub {
  font-size: 13px; color: var(--text-secondary);
  letter-spacing: 0.15em; margin-bottom: 16px;
}

/* Login area in hero */
.hero-login { margin: 10px 0 16px; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; }
.btn-login-hero {
  padding: 7px 24px; background: var(--accent); color: #fff; border: none;
  border-radius: 20px; font-size: 14px; cursor: pointer; letter-spacing: 0.15em;
  font-family: 'Classical Serif', 'KaiTi', 'STKaiti', serif;
}
.btn-login-hero:active { background: var(--accent-hover); }
.hero-login-hint { font-size: 12px; color: var(--text-secondary); }
.logged-in-badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 5px 14px; background: rgba(181,68,60,0.1); border-radius: 20px;
  font-size: 13px; color: var(--accent);
}

/* Hero search */
.hero-search {
  max-width: 360px; margin: 0 auto;
  background: var(--card); border: 1px solid var(--border); border-radius: 24px;
  padding: 2px; box-shadow: var(--shadow);
}
.hero-search input {
  width: 100%; padding: 10px 18px; border: none; outline: none;
  font-size: 15px; background: transparent; color: var(--text); text-align: center;
}
.hero-search input::placeholder { color: var(--text-secondary); }

/* Category scroll */
.cat-scroll {
  padding: 12px 16px; display: flex; gap: 8px; overflow-x: auto; flex-shrink: 0;
  scrollbar-width: none; border-bottom: 1px solid var(--border);
}
.cat-scroll::-webkit-scrollbar { display: none; }
.categories { display: flex; gap: 8px; }
.cat-tab {
  padding: 6px 14px; border: 1px solid var(--border); border-radius: 16px;
  font-size: 13px; white-space: nowrap; cursor: pointer; background: var(--card);
  color: var(--text-secondary); transition: all 0.2s;
}
.cat-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.cat-tab:active { opacity: 0.8; }

/* Book list */
.book-list {
  flex: 1; overflow-y: auto; padding: 12px 16px 60px;
}
.book-item {
  padding: 14px 16px; background: var(--card); border-radius: 8px; margin-bottom: 8px;
  box-shadow: var(--shadow); cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
  border-left: 3px solid transparent; transition: border-color 0.2s;
}
.book-item:active { opacity: 0.8; border-left-color: var(--accent); }
.book-item .book-title { font-size: 15px; font-weight: 500; flex: 1; }
.book-item .book-meta { font-size: 12px; color: var(--text-secondary); margin-left: 8px; white-space: nowrap; }
.book-item .book-dir { font-size: 11px; color: var(--text-secondary); margin-right: 8px; opacity: 0.7; }

/* Home footer */
.home-footer {
  text-align: center; padding: 16px; font-size: 12px; color: var(--text-secondary);
  border-top: 1px solid var(--border); flex-shrink: 0; letter-spacing: 0.1em;
}
.home-footer em { color: var(--accent); font-style: normal; }

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--topbar-bg); border-bottom: 1px solid var(--border);
  flex-shrink: 0; min-height: 50px;
}
.logo { font-size: 18px; font-weight: 700; display: flex; align-items: center; gap: 6px; }
.reader-topbar { justify-content: flex-start; gap: 8px; }
.reader-title-text { flex:1; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 2px; }

/* Buttons */
.btn-icon {
  width: 36px; height: 36px; border: none; background: none; border-radius: 8px;
  font-size: 16px; cursor: pointer; color: var(--text); display: flex; align-items: center; justify-content: center;
}
.btn-icon:active { opacity: 0.7; background: var(--border); }
.btn-icon.btn-tts { color: var(--accent); font-size: 18px; }
.btn-primary {
  width: 100%; padding: 12px; background: var(--accent); color: #fff; border: none;
  border-radius: 8px; font-size: 16px; cursor: pointer; margin-top: 12px;
}
.btn-primary:active { background: var(--accent-hover); }

/* Drawers */
.drawer {
  position: fixed; top: 0; right: 0; width: 75%; max-width: 320px; height: 100%;
  background: var(--card); z-index: 100; transform: translateX(100%); transition: transform 0.25s ease;
  display: flex; flex-direction: column; box-shadow: -2px 0 20px rgba(0,0,0,0.15);
}
.drawer.open { transform: translateX(0); }
.drawer-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; border-bottom: 1px solid var(--border); font-weight: 600; font-family: 'Classical Serif', 'KaiTi', serif;
}
.drawer-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.25); z-index: 98; display: none;
}
.drawer-overlay.show { display: block; }
.chapter-list { flex: 1; overflow-y: auto; padding: 8px 0; }
.chapter-item, .bm-item {
  padding: 10px 16px; font-size: 14px; cursor: pointer; border-bottom: 1px solid var(--border);
}
.chapter-item:active, .chapter-item.active { color: var(--accent); font-weight: 600; }
.bm-item .bm-snippet { font-size: 11px; color: var(--text-secondary); margin: 2px 0 0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.bm-item .bm-meta { font-size: 10px; color: var(--accent); }
.bm-item .bm-del { float:right; color: var(--text-secondary); cursor:pointer; font-size: 14px; padding: 2px 6px; }

/* Reading area */
.reading-area {
  flex: 1; overflow-y: auto; padding: 20px 16px 24px; -webkit-overflow-scrolling: touch;
}
.chapter-title {
  font-size: 19px; font-weight: 700; margin-bottom: 18px; text-align: center; line-height: 1.5;
  font-family: 'Classical Serif', 'KaiTi', serif;
}
.chapter-content { font-size: var(--font-size, 18px); line-height: 2; letter-spacing: 0.5px; color: var(--reading-text); }
.chapter-content p {
  padding: 4px 8px; border-radius: 4px; cursor: pointer; transition: background 0.15s; text-indent: 0;
  font-family: 'Classical Serif', 'KaiTi', 'SimSun', serif;
}
.chapter-content p:hover { background: var(--border); }
.chapter-content p.reading-active { background: var(--active-paragraph); outline: 2px solid var(--accent); outline-offset: -1px; }
.chapter-nav {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--border);
}
.btn-nav {
  padding: 8px 14px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--card); color: var(--text); font-size: 13px; cursor: pointer;
}
.btn-nav:active { opacity: 0.7; }
.chapter-index { font-size: 12px; color: var(--text-secondary); }

/* Member prompt (in reader for non-logged-in) */
.member-prompt { margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--border); }
.prompt-card {
  text-align: center; padding: 20px 16px; background: linear-gradient(135deg, rgba(181,68,60,0.05), rgba(181,68,60,0.02));
  border-radius: 12px; border: 1px dashed var(--accent); opacity: 0.8;
}
.prompt-card p { margin-bottom: 8px; font-size: 14px; }
.prompt-detail { font-size: 12px !important; color: var(--text-secondary); }
.prompt-card .btn-primary { max-width: 200px; margin: 12px auto 0; }

/* Reader toolbar */
.reader-toolbar {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 8px 12px; background: var(--topbar-bg); border-top: 1px solid var(--border); flex-shrink: 0; flex-wrap: wrap;
}
.font-size-label { font-size: 12px; color: var(--text-secondary); min-width: 20px; text-align: center; }
.tts-speed {
  font-size: 12px; padding: 4px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--card); color: var(--text); outline: none; height: 30px;
}
.tts-status {
  text-align: center; padding: 6px 0; font-size: 12px; color: var(--accent);
  background: var(--topbar-bg); border-top: 1px solid var(--border); flex-shrink: 0;
}

/* Member-only indicators */
.member-only { opacity: 0.4; position: relative; }
.member-only.unlocked { opacity: 1; }
.member-only:not(.unlocked):active { animation: shake 0.3s; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

/* Parts list */
.parts-header { text-align:center; margin-bottom:18px; color:var(--text-secondary); font-size:14px; }
.parts-grid { display:flex; flex-direction:column; gap:6px; }
.part-item {
  padding:12px 14px; background:var(--card); border-radius:8px; cursor:pointer;
  border:1px solid var(--border); display:flex; align-items:center; gap:10px;
}
.part-item:active { background:var(--border); }
.part-num { width:28px; height:28px; border-radius:50%; background:var(--accent); color:#fff;
  display:flex; align-items:center; justify-content:center; font-size:12px; font-weight:600; flex-shrink:0; }
.part-title { flex:1; font-size:14px; }

/* Popups */
.popup {
  position: fixed; bottom: 60px; left: 50%; transform: translateX(-50%); width: 90%; max-width: 360px;
  max-height: 60vh; background: var(--card); border-radius: 12px; box-shadow: 0 4px 24px rgba(0,0,0,0.15);
  z-index: 95; display: flex; flex-direction: column; overflow: hidden;
}
.popup-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 600; flex-shrink: 0;
  font-family: 'Classical Serif', 'KaiTi', serif;
}
.voice-list, .theme-options { flex: 1; overflow-y: auto; padding: 8px 0; }
.voice-item, .theme-option {
  padding: 12px 16px; cursor: pointer; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--border);
}
.voice-item:active, .theme-option:active { background: var(--border); }
.voice-item.active, .theme-option.active { color: var(--accent); font-weight: 600; }
.voice-item .voice-label { flex:1; }
.voice-item .voice-check { color: var(--accent); }
.theme-option .theme-swatch {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--border); flex-shrink: 0;
}

/* Modal */
.modal {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 88%; max-width: 360px;
  background: var(--card); border-radius: 14px; box-shadow: 0 8px 40px rgba(0,0,0,0.2); z-index: 100;
  overflow: hidden;
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px; border-bottom: 1px solid var(--border); font-size: 17px; font-weight: 600;
  font-family: 'Classical Serif', 'KaiTi', serif;
}
.modal-body { padding: 20px 16px; }
.modal-body input {
  width: 100%; padding: 12px; margin-bottom: 10px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 15px; background: var(--bg); color: var(--text); outline: none;
}
.modal-body input:focus { border-color: var(--accent); }
.login-error { color: #c0392b; font-size: 13px; margin: 4px 0; min-height: 20px; }
.login-switch { text-align: center; margin-top: 14px; font-size: 13px; color: var(--text-secondary); }
.login-switch a { color: var(--accent); text-decoration: none; }

/* Toast */
.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: rgba(44,36,22,0.8); color: #faf7f0; padding: 10px 24px; border-radius: 20px;
  font-size: 14px; z-index: 200; display: none; transition: opacity 0.3s;
}

/* Loading */
.loading { text-align: center; padding: 40px; color: var(--text-secondary); }

/* Responsive */
@media (min-width: 768px) {
  .hero { padding: 48px 40px 28px; }
  .hero-title { font-size: 38px; }
  .reading-area { padding: 40px 60px; }
  .book-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; padding: 20px 24px; }
  .book-item { margin-bottom: 0; }
}
