/* ─────────────────────────────────────────────
 *  Jung Trilogy Reader — Mobile-first Chinese UX
 * ───────────────────────────────────────────── */
:root {
  --bg: #faf8f5;
  --bg-elevated: #fff;
  --fg: #2a2a2a;
  --fg-subtle: #6b6b6b;
  --accent: #8b5a2b;
  --accent-soft: #d4b896;
  --border: #e8e4de;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --font-serif: "Noto Serif TC", "PingFang TC", "Songti TC", "Source Han Serif TC", serif;
  --font-sans: "PingFang TC", "Noto Sans TC", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
[data-theme="dark"] {
  --bg: #1a1a1a;
  --bg-elevated: #262626;
  --fg: #e8e8e8;
  --fg-subtle: #999;
  --accent: #d4b896;
  --accent-soft: #5a4631;
  --border: #333;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}
[data-theme="sepia"] {
  --bg: #f4ecd8;
  --bg-elevated: #f9f3e4;
  --fg: #3a2e1f;
  --fg-subtle: #7a6c5d;
  --accent: #8b5a2b;
  --accent-soft: #c9ad7e;
  --border: #e0d4b8;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--fg);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

.screen { display: flex; flex-direction: column; height: 100%; }
.hidden { display: none !important; }

/* ── Auth screen ──────────────────────────── */
#auth-screen {
  align-items: center; justify-content: center; padding: 24px;
}
.auth-box {
  background: var(--bg-elevated);
  border: 1px solid var(--border); border-radius: 14px;
  padding: 28px 24px; max-width: 440px; width: 100%;
  box-shadow: var(--shadow);
}
.auth-box h1 { margin: 0 0 12px; font-size: 1.4rem; font-weight: 700; }
.auth-box p { color: var(--fg-subtle); line-height: 1.6; font-size: 0.9rem; }
.help { margin: 12px 0; padding: 10px 14px; background: rgba(139,90,43,0.06); border-radius: 8px; font-size: 0.85rem; }
.help summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.help ol { margin: 8px 0 0 0; padding-left: 20px; line-height: 1.8; }
.help code { background: var(--border); padding: 1px 6px; border-radius: 4px; font-size: 0.85em; }
.help a { color: var(--accent); }
#token-input {
  width: 100%; padding: 12px 14px; margin-top: 12px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 0.95rem; font-family: monospace;
  background: var(--bg); color: var(--fg);
}
#token-input:focus { outline: none; border-color: var(--accent); }
#token-submit {
  width: 100%; padding: 12px; margin-top: 12px;
  background: var(--accent); color: #fff; border: none; border-radius: 8px;
  font-size: 1rem; font-weight: 600; cursor: pointer;
}
#token-submit:active { opacity: 0.8; }
.error {
  margin-top: 10px; padding: 10px 12px;
  background: #fee2e2; color: #991b1b;
  border-radius: 6px; font-size: 0.85rem;
}
[data-theme="dark"] .error { background: #4a1c1c; color: #fca5a5; }

/* ── Top bar ──────────────────────────────── */
.topbar {
  display: flex; align-items: center;
  padding: env(safe-area-inset-top) 8px 0;
  height: calc(50px + env(safe-area-inset-top));
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar button {
  background: none; border: none;
  font-size: 1.3rem; padding: 8px 12px; cursor: pointer;
  color: var(--fg); line-height: 1;
}
.topbar .title {
  flex: 1; text-align: center;
  font-size: 0.9rem; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 0 8px;
}

/* ── Sidebar / TOC ────────────────────────── */
.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: 82%; max-width: 340px;
  background: var(--bg-elevated);
  z-index: 30; transform: translateX(-100%);
  transition: transform 0.25s ease;
  display: flex; flex-direction: column;
  box-shadow: 2px 0 12px rgba(0,0,0,0.08);
  padding-top: env(safe-area-inset-top);
}
.sidebar:not(.hidden) { transform: translateX(0); }
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.sidebar-header h2 { margin: 0; font-size: 1rem; }
.sidebar-header button {
  background: none; border: none; font-size: 1.5rem;
  color: var(--fg); cursor: pointer; line-height: 1;
}
#toc { flex: 1; overflow-y: auto; padding: 8px 0; }
.toc-book {
  font-weight: 700; font-size: 0.95rem; color: var(--accent);
  padding: 14px 18px 6px; letter-spacing: 0.5px;
}
.toc-chapter {
  padding: 10px 18px; cursor: pointer;
  border-left: 3px solid transparent;
  font-size: 0.9rem; line-height: 1.5;
  transition: background 0.15s;
}
.toc-chapter:active { background: rgba(139,90,43,0.08); }
.toc-chapter.active {
  border-left-color: var(--accent);
  background: rgba(139,90,43,0.06);
  color: var(--accent); font-weight: 600;
}
.toc-chapter .num {
  display: inline-block; width: 1.6em; color: var(--fg-subtle);
  font-size: 0.85em;
}
.sidebar-footer {
  padding: 12px 18px; border-top: 1px solid var(--border);
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}
.text-btn {
  background: none; border: none; color: var(--fg-subtle);
  font-size: 0.85rem; cursor: pointer; padding: 0;
}
.text-btn:hover { color: var(--accent); }

.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 20;
}

/* ── Content ──────────────────────────────── */
.content {
  flex: 1; overflow-y: auto;
  padding: 24px 20px 80px;
  font-family: var(--font-serif);
  font-size: 1.05rem; line-height: 1.85;
  color: var(--fg);
  -webkit-overflow-scrolling: touch;
}
.content.landing {
  padding: 40px 24px;
}
.loader {
  text-align: center; color: var(--fg-subtle);
  padding: 60px 20px; font-size: 0.9rem;
}

/* Markdown rendering */
.content h1 {
  font-size: 1.55rem; font-weight: 700; margin: 0.4em 0 0.8em;
  padding-bottom: 0.4em; border-bottom: 2px solid var(--accent-soft);
  line-height: 1.4;
}
.content h2 {
  font-size: 1.2rem; font-weight: 700; margin: 1.8em 0 0.7em;
  color: var(--accent);
}
.content h3 {
  font-size: 1.05rem; font-weight: 700; margin: 1.5em 0 0.5em;
}
.content h4 { font-size: 0.98rem; font-weight: 700; margin: 1.2em 0 0.4em; }
.content p { margin: 0 0 1.1em; }
.content ul, .content ol { padding-left: 1.4em; margin: 0 0 1.1em; }
.content li { margin-bottom: 0.4em; }
.content blockquote {
  margin: 1.2em 0; padding: 0.6em 1em;
  border-left: 3px solid var(--accent-soft);
  background: rgba(139,90,43,0.04);
  color: var(--fg-subtle);
  font-style: normal;
}
.content blockquote p:last-child { margin-bottom: 0; }
.content code {
  background: var(--border); padding: 1px 6px; border-radius: 4px;
  font-size: 0.88em; font-family: "SF Mono", Consolas, monospace;
}
.content pre {
  background: var(--bg-elevated); border: 1px solid var(--border);
  padding: 12px; border-radius: 8px; overflow-x: auto;
  font-size: 0.85rem; line-height: 1.5;
}
.content pre code { background: none; padding: 0; font-size: inherit; }
.content hr {
  border: none; border-top: 1px solid var(--border);
  margin: 2em 0;
}
.content strong { color: var(--accent); font-weight: 700; }
.content a { color: var(--accent); }

/* Landing page */
.landing h1 {
  font-size: 1.6rem; margin-bottom: 0.3em;
  border-bottom: none; text-align: center;
}
.landing .subtitle {
  text-align: center; color: var(--fg-subtle);
  margin-bottom: 2em; font-size: 0.9rem;
}
.landing .book-grid {
  display: grid; gap: 14px; margin-top: 1em;
}
.book-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 18px 20px; cursor: pointer;
  box-shadow: var(--shadow); transition: transform 0.15s;
}
.book-card:active { transform: scale(0.98); }
.book-card .book-title {
  font-size: 1.1rem; font-weight: 700; color: var(--accent);
  margin-bottom: 6px;
}
.book-card .book-desc {
  font-size: 0.85rem; color: var(--fg-subtle); line-height: 1.6;
}

/* ── Bottom bar ───────────────────────────── */
.bottombar {
  display: flex;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottombar button {
  flex: 1; padding: 14px; background: none; border: none;
  color: var(--fg); font-size: 0.92rem; cursor: pointer;
}
.bottombar button:active { background: rgba(139,90,43,0.08); }
.bottombar button:disabled { color: var(--fg-subtle); opacity: 0.4; }
.bottombar button:first-child { border-right: 1px solid var(--border); }

/* Desktop tweaks */
@media (min-width: 768px) {
  .content {
    max-width: 720px; margin: 0 auto;
    padding: 40px 32px 100px;
    font-size: 1.1rem; line-height: 1.9;
  }
  .topbar .title { font-size: 1rem; }
  .content h1 { font-size: 1.8rem; }
}

/* ─────────────────────────────────────────────
 *  Library · 書架畫面
 * ───────────────────────────────────────────── */
.shelf {
  flex: 1; overflow-y: auto;
  padding: 28px 20px 40px;
  -webkit-overflow-scrolling: touch;
}
.shelf-intro { text-align: center; margin-bottom: 24px; }
.shelf-intro h2 {
  font-family: var(--font-serif);
  font-size: 1.5rem; margin: 0 0 6px; color: var(--fg);
}
.shelf-intro .subtitle {
  color: var(--fg-subtle); font-size: 0.9rem; margin: 0;
}
#book-grid {
  display: grid; gap: 14px;
  max-width: 640px; margin: 0 auto;
}
.shelf .book-card {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 14px; align-items: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border); border-radius: 14px;
  padding: 16px 18px; cursor: pointer;
  box-shadow: var(--shadow); transition: transform 0.15s;
}
.shelf .book-card:active { transform: scale(0.98); }
.shelf .book-cover {
  font-size: 2.2rem; text-align: center;
  padding: 10px 0;
  background: rgba(139,90,43,0.06);
  border-radius: 10px;
  line-height: 1;
}
.shelf .book-info { min-width: 0; }
.shelf .book-title {
  font-size: 1.05rem; font-weight: 700;
  color: var(--accent); margin-bottom: 4px;
  font-family: var(--font-serif);
}
.shelf .book-desc {
  font-size: 0.82rem; color: var(--fg-subtle);
  line-height: 1.5;
}
.shelf .book-meta {
  font-size: 0.72rem; color: var(--fg-subtle);
  margin-top: 4px; opacity: 0.7;
}
.shelf-footer {
  max-width: 640px; margin: 40px auto 0;
  text-align: center;
  padding-bottom: env(safe-area-inset-bottom);
}

#back-btn { font-size: 1.4rem; }
.theme-icon { font-size: 1.2rem; }

@media (min-width: 768px) {
  #book-grid { grid-template-columns: 1fr 1fr; }
  .shelf-intro h2 { font-size: 1.8rem; }
}
