:root {
  color-scheme: dark;
  --bg: #0a0a0f;
  --surface: #14141c;
  --surface-2: #1c1c28;
  --text: #f2f2f7;
  --muted: #8e8e93;
  --accent: #5e5ce6;
  --accent-2: #bf5af2;
  --ok: #30d158;
  --warn: #ffd60a;
  --err: #ff453a;
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); min-height: 100dvh; }

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(56px + var(--safe-bottom));
}

body.player-active .app {
  padding-bottom: calc(56px + 132px + var(--safe-bottom));
}

header.top {
  padding: calc(16px + var(--safe-top)) 18px 8px;
  background: linear-gradient(180deg, #12122a 0%, transparent 100%);
}
.top h1 { margin: 0; font-size: 1.35rem; font-weight: 700; }
.top p { margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; }

main.content { flex: 1; padding: 8px 16px 24px; }

.card {
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.upload-zone {
  border: 2px dashed rgba(94,92,230,0.45);
  border-radius: var(--radius);
  padding: 28px 16px;
  text-align: center;
  background: rgba(94,92,230,0.08);
  cursor: pointer;
}
.upload-zone.drag { border-color: var(--accent-2); background: rgba(191,90,242,0.12); }
.upload-zone input { display: none; }

.book-row {
  display: flex;
  gap: 12px;
  align-items: center;
}
.book-cover {
  width: 48px; height: 64px; border-radius: 8px;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  display: grid; place-items: center; font-weight: 700; font-size: 1.1rem;
  flex-shrink: 0;
}
.book-meta { flex: 1; min-width: 0; }
.book-meta h3 { margin: 0; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-meta p { margin: 4px 0 0; font-size: 0.8rem; color: var(--muted); }

.status-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
}
.status-queued, .status-extracting, .status-indexing { background: rgba(255,214,10,0.15); color: var(--warn); }
.status-ready { background: rgba(48,209,88,0.15); color: var(--ok); }
.status-failed { background: rgba(255,69,58,0.15); color: var(--err); }

.progress {
  height: 6px; border-radius: 999px; background: rgba(255,255,255,0.08); margin-top: 10px; overflow: hidden;
}
.progress > span {
  display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width 0.3s ease;
}

.player-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(56px + var(--safe-bottom));
  background: linear-gradient(180deg, rgba(28,28,40,0.98) 0%, rgba(20,20,28,0.99) 100%);
  border-top: 1px solid rgba(255,255,255,0.1);
  padding: 10px 16px 12px;
  display: none;
  z-index: 20;
  backdrop-filter: blur(16px);
}
.player-bar.active { display: block; }
.player-bar h4 {
  margin: 0 0 4px;
  font-size: 0.88rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.player-backend {
  margin: 0 0 8px;
  font-size: 0.72rem;
  color: var(--muted);
}
.player-bar audio { display: none; }

.player-track {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.player-time {
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 2.6rem;
}
.player-time:last-child { text-align: right; }

.player-seek {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 999px;
  background: rgba(255,255,255,0.12);
  outline: none;
  cursor: pointer;
}
.player-seek::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 0 3px rgba(94,92,230,0.25);
}
.player-seek::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
}

.player-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 28px;
}
.player-btn {
  border: 0;
  background: transparent;
  color: var(--text);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
}
.player-btn svg { width: 26px; height: 26px; }
.player-btn-main {
  width: 56px;
  height: 56px;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #fff;
  box-shadow: 0 4px 20px rgba(94,92,230,0.35);
}
.player-btn-main svg { width: 30px; height: 30px; }
.player-btn-main .icon-pause { display: none; }
.player-btn-main.is-playing .icon-play { display: none; }
.player-btn-main.is-playing .icon-pause { display: block; }
#btn-seek-back svg,
#btn-seek-fwd svg { width: 32px; height: 32px; opacity: 0.92; }

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(56px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  background: rgba(20,20,28,0.96);
  border-top: 1px solid rgba(255,255,255,0.08);
  backdrop-filter: blur(12px);
}
.tabbar button {
  flex: 1; border: 0; background: transparent; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  gap: 4px; padding-bottom: 6px; font-size: 0.7rem; cursor: pointer;
}
.tabbar button.active { color: var(--accent); }
.tabbar svg { width: 22px; height: 22px; }

.btn {
  border: 0; border-radius: 10px; padding: 8px 12px; font-size: 0.85rem; cursor: pointer;
  background: rgba(255,255,255,0.08); color: var(--text);
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger { background: rgba(255,69,58,0.2); color: var(--err); }
.btn-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

.empty { text-align: center; color: var(--muted); padding: 32px 12px; }
.hidden { display: none !important; }

.catalog-section { margin-top: 4px; }
.catalog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.catalog-head .section-title { margin-bottom: 4px; }
.catalog-head .section-sub { margin: 0; }
.catalog-head .btn { flex-shrink: 0; white-space: nowrap; }
.section-title {
  margin: 0 0 4px;
  font-size: 1rem;
  font-weight: 700;
}
.section-sub {
  margin: 0 0 12px;
  font-size: 0.8rem;
  color: var(--muted);
}
.catalog-card .book-meta h3 { font-size: 0.95rem; }
.catalog-cover { opacity: 0.92; }
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.login-page {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(ellipse 80% 60% at 50% -10%, #1a1a3a 0%, #0a0a0f 55%);
}
.login-card {
  width: min(400px, 100%); background: var(--surface);
  border: 1px solid rgba(255,255,255,0.08); border-radius: 20px; padding: 28px 24px;
}
.login-card h1 { margin: 0 0 8px; }
.login-card label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 6px; }
.login-card input {
  width: 100%; padding: 12px 14px; border-radius: 10px; margin-bottom: 14px;
  border: 1px solid rgba(255,255,255,0.12); background: rgba(0,0,0,0.35); color: inherit;
}
.login-card button {
  width: 100%; padding: 12px; border: 0; border-radius: 10px;
  background: var(--accent); color: #fff; font-size: 1rem; cursor: pointer;
}
.login-err { color: var(--err); font-size: 0.85rem; min-height: 1.2em; margin-bottom: 8px; }

.reader-article { padding-bottom: 8px; }
.reader-header { margin-bottom: 12px; }
.reader-header h2 {
  margin: 0;
  font-size: 1.1rem;
  line-height: 1.35;
}
.reader-meta {
  margin: 6px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}
.reader-body {
  font-size: 1.05rem;
  line-height: 1.85;
  letter-spacing: 0.02em;
}
.reader-section { margin-bottom: 20px; }
.reader-section-title {
  margin: 0 0 10px;
  font-size: 0.9rem;
  color: var(--accent-2);
  font-weight: 600;
}
.reader-section-body { text-align: justify; }
.reader-chunk {
  border-radius: 4px;
  padding: 1px 2px;
  transition: background 0.2s ease, color 0.2s ease;
  cursor: pointer;
}
.reader-chunk.active {
  background: rgba(94, 92, 230, 0.28);
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(191, 90, 242, 0.35);
}

.reader-mode-bar {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}
.reader-mode-btn {
  flex: 1;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(255,255,255,0.04);
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
}
.reader-mode-btn.active {
  color: #fff;
  border-color: rgba(94, 92, 230, 0.55);
  background: rgba(94, 92, 230, 0.22);
}

.reader-body-scan { padding-bottom: 4px; }
.reader-scan-section { margin-bottom: 22px; }
.reader-scan-page {
  position: relative;
  width: 100%;
  border-radius: 12px;
  overflow: hidden;
  background: #111;
  box-shadow: 0 8px 28px rgba(0,0,0,0.35);
}
.reader-scan-image {
  display: block;
  width: 100%;
  height: auto;
  vertical-align: top;
}
.ocr-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.ocr-line {
  position: absolute;
  pointer-events: auto;
  margin: 0;
  padding: 0;
  border: 2px solid rgba(94, 92, 230, 0.45);
  background: rgba(94, 92, 230, 0.06);
  border-radius: 3px;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
}
.ocr-line.active {
  border-color: rgba(191, 90, 242, 0.95);
  background: rgba(191, 90, 242, 0.28);
  box-shadow: 0 0 0 2px rgba(255,255,255,0.2);
  z-index: 2;
}
.ocr-line:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: 1px;
}
.ocr-segment {
  position: absolute;
  pointer-events: auto;
  margin: 0;
  padding: 0;
  border: 2px solid rgba(94, 92, 230, 0.5);
  background: rgba(94, 92, 230, 0.08);
  border-radius: 4px;
  cursor: pointer;
  min-width: 0;
  min-height: 0;
}
.ocr-segment.active {
  border-color: rgba(191, 90, 242, 0.95);
  background: rgba(191, 90, 242, 0.28);
  box-shadow: 0 0 0 2px rgba(255,255,255,0.2);
  z-index: 2;
}
.ocr-segment:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: 1px;
}

.settings-mode-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}
.chip {
  border: 1px solid rgba(255,255,255,0.12);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.85rem;
  cursor: pointer;
}
.chip.active {
  border-color: rgba(94,92,230,0.7);
  background: rgba(94,92,230,0.22);
  color: #fff;
}
.settings-speed {
  display: grid;
  gap: 8px;
  margin: 0 0 10px;
  font-size: 0.85rem;
  color: var(--muted);
}
.settings-speed input[type="range"] { width: 100%; }
.settings-hint {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
}
