/* ── CSS Variables / Themes ──────────────────────────────────────────────── */
:root {
  --bg: #ffffff;
  --text: #1a1a1a;
  --surface: #f5f5f5;
  --border: #e0e0e0;
  --accent: #4a90d9;
  --accent-text: #ffffff;
  --muted: #888888;
  --header-h: 52px;
  --footer-h: 52px;
  --font-system: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-serif: 'Noto Serif KR', Georgia, 'Times New Roman', serif;
  --font-gothic: 'Noto Sans KR', 'Malgun Gothic', sans-serif;
  --reader-font: var(--font-system);
  --reader-size: 18px;
  --reader-lh: 1.8;
  --reader-pad: 20px;
}

[data-theme="dark"] {
  --bg: #1a1a1a;
  --text: #e8e8e8;
  --surface: #2d2d2d;
  --border: #444444;
  --accent: #5aa3e8;
  --muted: #999999;
}

[data-theme="sepia"] {
  --bg: #f4ecd8;
  --text: #3d2b1f;
  --surface: #ede0c4;
  --border: #c8b99a;
  --accent: #8b6914;
  --muted: #7a6040;
}

/* ── Reset & Base ────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-system);
  background: var(--bg);
  color: var(--text);
  transition: background 0.2s, color 0.2s;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100%;
  position: relative;
}

/* ── Pages ───────────────────────────────────────────────────────────────── */
.page {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

.page.hidden { display: none; }

/* ── Page Header ─────────────────────────────────────────────────────────── */
.page-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.page-title {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Icon Buttons ────────────────────────────────────────────────────────── */
.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  white-space: nowrap;
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--border); }
.icon-btn:active { opacity: 0.7; }

/* ── Loading / Empty ─────────────────────────────────────────────────────── */
.loading, .empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
  font-size: 15px;
}

/* ── Library Grid ────────────────────────────────────────────────────────── */
.novel-grid {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 14px;
  align-content: start;
}

.novel-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 14px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.novel-card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.12); }
.novel-card:active { transform: scale(0.97); }

.novel-card-title {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.3;
}

.novel-card-meta {
  font-size: 12px;
  color: var(--muted);
}

.novel-card-progress {
  font-size: 11px;
  color: var(--accent);
  margin-top: 2px;
}

.hidden { display: none !important; }

/* ── Upload status (library page) ────────────────────────────────────────── */
.upload-status {
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--muted);
  flex-shrink: 0;
}

/* ── Cache bar (episodes page) ───────────────────────────────────────────── */
.cache-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.cache-status {
  font-size: 13px;
  color: var(--muted);
  flex: 1;
}

.cache-status.cached  { color: #22a45d; }
.cache-status.partial { color: #e07b00; }

.cache-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 12px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  transition: background 0.12s;
}
.cache-btn:hover   { background: var(--border); }
.cache-btn.cached  { border-color: #22a45d; color: #22a45d; }
.cache-btn.cancel-btn { color: var(--muted); }

.cache-progress-bar {
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  order: 99; /* push to next row */
}

.cache-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  width: 0%;
  transition: width 0.3s ease;
}

/* ── Cache badge on novel cards ──────────────────────────────────────────── */
.cache-badge {
  display: inline-block;
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  margin-left: 4px;
  vertical-align: middle;
}
.cache-badge.full    { background: #d4f0e1; color: #1a7a45; }
.cache-badge.partial { background: #fef3cd; color: #8a5c00; }

/* ── Episode List ────────────────────────────────────────────────────────── */
.episode-list {
  flex: 1;
  overflow-y: auto;
}

.episode-item {
  display: flex;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.12s;
  gap: 12px;
}
.episode-item:hover { background: var(--surface); }
.episode-item:active { background: var(--border); }

.episode-num {
  font-size: 13px;
  color: var(--muted);
  min-width: 40px;
  flex-shrink: 0;
}

.episode-title {
  font-size: 15px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.episode-item.current .episode-title {
  color: var(--accent);
  font-weight: 600;
}

.episode-item.current .episode-num::before {
  content: '▶ ';
}

/* ── Reader ──────────────────────────────────────────────────────────────── */
.reader-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
  transition: opacity 0.3s;
}

.reader-title {
  flex: 1;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

#reader-container {
  flex: 1;
  overflow: hidden;
  position: relative;
  /* touch-action managed by JS: pan-y during vertical, none during horizontal swipe */
  touch-action: pan-y;
}

#reader-content {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--reader-pad);
  font-family: var(--reader-font);
  font-size: var(--reader-size);
  line-height: var(--reader-lh);
  color: var(--text);
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

#reader-content p {
  margin-bottom: 1em;
  word-break: keep-all;
  overflow-wrap: break-word;
}

/* Swipe animation helper */
#reader-content.swiping {
  transition: none;
}
#reader-content.snap-back {
  transition: transform 0.22s cubic-bezier(.25,.46,.45,.94);
}

.reader-footer {
  height: var(--footer-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.nav-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  transition: background 0.12s;
}
.nav-btn:hover:not(:disabled) { background: var(--border); }
.nav-btn:disabled { opacity: 0.35; cursor: not-allowed; }

#reader-progress {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

/* ── Settings Overlay ────────────────────────────────────────────────────── */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.settings-overlay.hidden { display: none; }

.settings-box {
  width: 100%;
  max-width: 480px;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  padding: 16px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.2);
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  font-size: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.settings-row label {
  font-size: 14px;
  color: var(--muted);
  min-width: 64px;
  flex-shrink: 0;
}

.settings-control {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: flex-end;
}

.step-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.12s;
}
.step-btn:hover { background: var(--border); }

.theme-btns, .font-btns {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.theme-btn, .font-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 10px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.12s, border-color 0.12s;
}
.theme-btn.active, .font-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

/* ── Scrollbar Styling ───────────────────────────────────────────────────── */
#reader-content::-webkit-scrollbar { width: 4px; }
#reader-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.episode-list::-webkit-scrollbar { width: 4px; }
.episode-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (min-width: 600px) {
  .novel-grid { padding: 24px; gap: 18px; }
  .settings-box { border-radius: 18px; max-width: 420px; margin: auto; }
  .settings-overlay { align-items: center; }
}
