/* ==================================================================
   EpubReader — Foliate-inspired styling
   ================================================================== */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; cursor: pointer; }

/* ==================================================================
   Library page
   ================================================================== */

.library-page {
  background: var(--chrome-bg);
  color: var(--chrome-fg);
  min-height: 100vh;
}

.lib-header {
  background: var(--surface);
  border-bottom: 1px solid var(--chrome-border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.lib-header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 32px;
  height: 64px;
  display: flex;
  align-items: center;
  gap: 14px;
}

.lib-title {
  font-size: 1.25rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

.lib-logo { margin-right: 4px; }

.lib-version {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--chrome-muted);
  background: var(--chrome-hover);
  border-radius: 999px;
  padding: 3px 9px;
  margin-left: 8px;
  vertical-align: middle;
  letter-spacing: 0.02em;
}

.lib-count {
  color: var(--chrome-muted);
  font-size: 0.85rem;
  margin-top: 2px;
}

.lib-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 32px 64px;
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 36px 28px;
}

.book-card {
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: inherit;
  transition: transform 0.15s ease;
}

.book-card:hover { transform: translateY(-4px); }

.cover-wrap {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  background: #d8d8dc;
}

.cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #4a5568, #2d3748);
}

.cover-initial {
  font-size: 3.4rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.85);
}

.badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(20, 20, 24, 0.82);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  backdrop-filter: blur(4px);
}

.badge.finished { background: rgba(34, 122, 62, 0.92); }

.book-meta {
  display: flex;
  flex-direction: column;
  padding-top: 12px;
  gap: 3px;
}

.book-title {
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-author {
  font-size: 0.8rem;
  color: var(--chrome-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lib-progress {
  height: 4px;
  border-radius: 2px;
  background: var(--chrome-border);
  margin-top: 8px;
  overflow: hidden;
}

.lib-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
}

.book-action {
  font-size: 0.78rem;
  color: var(--accent);
  font-weight: 600;
  margin-top: 6px;
}

.empty-state {
  text-align: center;
  color: var(--chrome-muted);
  padding: 80px 0;
  line-height: 1.7;
}

.empty-state code {
  background: var(--chrome-hover);
  padding: 2px 6px;
  border-radius: 4px;
}

.lib-logout {
  margin-left: auto;
  color: var(--chrome-muted);
  text-decoration: none;
  font-size: 0.85rem;
  padding: 6px 10px;
  border-radius: 8px;
}

.lib-logout:hover { background: var(--chrome-hover); color: var(--chrome-fg); }

/* ==================================================================
   Login page
   ================================================================== */

.login-page {
  background: #f2f2f4;
  color: #26262a;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: #ffffff;
  border: 1px solid #e2e2e6;
  border-radius: 16px;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.08);
  padding: 40px 36px;
  width: min(360px, calc(100vw - 32px));
  text-align: center;
}

.login-title {
  font-size: 1.3rem;
  margin: 0 0 6px;
}

.login-sub {
  color: #8a8a90;
  font-size: 0.88rem;
  margin: 0 0 20px;
}

.login-error {
  color: #c0392b;
  background: #fdecea;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.85rem;
  margin: 0 0 16px;
}

.login-input {
  width: 100%;
  padding: 11px 14px;
  font-size: 1rem;
  border: 1px solid #d5d5da;
  border-radius: 10px;
  margin-bottom: 14px;
  outline: none;
}

.login-input:focus { border-color: #4a6cf7; box-shadow: 0 0 0 3px #4a6cf722; }

.login-btn {
  width: 100%;
  padding: 11px 14px;
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  background: #4a6cf7;
  border: none;
  border-radius: 10px;
}

.login-btn:hover { background: #3b5be0; }

/* ==================================================================
   Themes — data-theme on <body>, shared by library and reader pages
   ================================================================== */

body[data-theme="light"] {
  --chrome-bg: #f7f7f8;
  --surface: #ffffff;
  --chrome-fg: #2c2c30;
  --chrome-muted: #8a8a90;
  --chrome-border: #e0e0e4;
  --chrome-hover: rgba(0, 0, 0, 0.06);
  --accent: #4a6cf7;
  --page-bg: #ffffff;
  color-scheme: light;
}

body[data-theme="sepia"] {
  --chrome-bg: #ece4d3;
  --surface: #f7f0e1;
  --chrome-fg: #4a3f2a;
  --chrome-muted: #9b8c70;
  --chrome-border: #d9cdb6;
  --chrome-hover: rgba(90, 70, 30, 0.08);
  --accent: #8a6d3b;
  --page-bg: #f4ecd8;
  color-scheme: light;
}

body[data-theme="dark"] {
  --chrome-bg: #141518;
  --surface: #1e2025;
  --chrome-fg: #cfcfd4;
  --chrome-muted: #74747c;
  --chrome-border: #2a2b30;
  --chrome-hover: rgba(255, 255, 255, 0.08);
  --accent: #7aa2ff;
  --page-bg: #1b1d21;
  color-scheme: dark;
}

body[data-theme="black"] {
  --chrome-bg: #0a0a0a;
  --surface: #141414;
  --chrome-fg: #e0e0e0;
  --chrome-muted: #6e6e6e;
  --chrome-border: #1e1e1e;
  --chrome-hover: rgba(255, 255, 255, 0.1);
  --accent: #7aa7ff;
  --page-bg: #000000;
  color-scheme: dark;
}

body[data-theme="nord"] {
  --chrome-bg: #242933;
  --surface: #2e3440;
  --chrome-fg: #d8dee9;
  --chrome-muted: #7b8494;
  --chrome-border: #3b4252;
  --chrome-hover: rgba(216, 222, 233, 0.08);
  --accent: #88c0d0;
  --page-bg: #2e3440;
  color-scheme: dark;
}

.reader-page {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--chrome-bg);
  color: var(--chrome-fg);
}

/* ---------- top bar ---------- */

.topbar {
  flex: 0 0 auto;
  border-bottom: 1px solid var(--chrome-border);
  background: var(--chrome-bg);
  z-index: 20;
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  height: 56px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.tb-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--chrome-fg);
  font-size: 0.9rem;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 8px;
  text-decoration: none;
  white-space: nowrap;
}

.tb-btn:hover, .tb-btn.active { background: var(--chrome-hover); }

#settings-btn { font-size: 1.05rem; font-weight: 700; }

.tb-title {
  flex: 1;
  text-align: center;
  font-size: 0.92rem;
  color: var(--chrome-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 12px;
}

.tb-actions { position: relative; }

/* ---------- settings popover ---------- */

.popover {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: 260px;
  background: var(--chrome-bg);
  border: 1px solid var(--chrome-border);
  border-radius: 12px;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.22);
  padding: 16px;
  z-index: 50;
}

.popover.hidden { display: none; }

.pop-section { margin-bottom: 16px; }
.pop-section:last-child { margin-bottom: 0; }

.pop-label {
  display: block;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--chrome-muted);
  margin-bottom: 8px;
  font-weight: 600;
}

.theme-row { display: flex; gap: 8px; }

.theme-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--chrome-border);
  padding: 0;
  outline: none;
  position: relative;
}

.theme-swatch.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

.font-size-row, .font-family-row, .line-height-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.font-size-row span, .line-height-row span {
  flex: 1;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: var(--chrome-fg);
}

.line-height-row .pop-btn { min-width: 44px; }

.pop-btn {
  background: var(--chrome-hover);
  color: var(--chrome-fg);
  border: 1px solid var(--chrome-border);
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 0.88rem;
}

.pop-btn:hover { border-color: var(--accent); }
.pop-btn.selected { background: var(--accent); color: #fff; border-color: var(--accent); }

.font-family-row .pop-btn { flex: 1; }

/* ---------- main split ---------- */

.reader-main {
  flex: 1;
  display: flex;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  min-height: 0;
}

.sidebar {
  flex: 0 0 300px;
  width: 300px;
  border-right: 1px solid var(--chrome-border);
  overflow-y: auto;
  background: var(--chrome-bg);
  transition: margin-left 0.2s ease;
}

.sidebar.collapsed {
  margin-left: -300px;
}

.sidebar-header {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  color: var(--chrome-muted);
  padding: 20px 20px 8px;
}

.toc-list { padding: 0 8px 24px; }

.toc-list ul {
  list-style: none;
  margin: 0;
  padding-left: 14px;
}

.toc-list > ul { padding-left: 0; }

.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--chrome-fg);
  font-size: 0.88rem;
  line-height: 1.35;
  padding: 7px 12px;
  border-radius: 8px;
  margin: 1px 0;
  overflow-wrap: anywhere;
}

.toc-item:hover { background: var(--chrome-hover); }

.toc-item.active {
  background: var(--accent);
  color: #fff;
}

/* ---------- reading column ---------- */

.reader-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.viewport-wrap {
  position: relative;
  flex: 1;
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  min-height: 0;
}

#view {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: var(--page-bg);
  transition: background 0.2s ease;
}

.nav-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 64px;
  border-radius: 10px;
  border: 1px solid var(--chrome-border);
  background: var(--chrome-bg);
  color: var(--chrome-fg);
  font-size: 1.6rem;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.15s ease;
  z-index: 5;
}

.viewport-wrap:hover .nav-arrow { opacity: 0.85; }
.nav-arrow:hover { opacity: 1 !important; background: var(--chrome-hover); }

.nav-left { left: 8px; }
.nav-right { right: 8px; }

@media (hover: none) {
  .nav-arrow { opacity: 0.5; }
}

/* ---------- status bar ---------- */

.statusbar {
  flex: 0 0 auto;
  max-width: 600px;
  width: 100%;
  margin: 0 auto;
  height: 44px;
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  font-size: 0.78rem;
  color: var(--chrome-muted);
}

.status-chapter {
  flex: 0 1 40%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.progress-track {
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: var(--chrome-border);
  overflow: hidden;
}

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

/* ---------- narrow screens ---------- */

@media (max-width: 900px) {
  .sidebar {
    position: absolute;
    z-index: 30;
    height: calc(100% - 56px);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.18);
  }
  .reader-main { position: relative; }
}

@media (max-width: 640px) {
  .tb-btn-label { display: none; }
  .lib-main { padding: 24px 16px 48px; }
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 24px 16px; }
}
