/* ==================================================================
   Pageturner — iOS-style PWA design system
   ================================================================== */

:root {
  --accent: #f0a040;
  --accent-press: #d98a2b;
  --danger: #ff453a;

  --bg: #111110;
  --bg-grouped: #000000;
  --bg-raised: #1c1c1e;
  --bg-card: #1c1c1e;
  --bg-sheet: #242426;
  --fill: rgba(240, 237, 230, 0.10);
  --fill-strong: rgba(240, 237, 230, 0.18);
  --text: #f0ede6;
  --text-2: rgba(240, 237, 230, 0.6);
  --text-3: rgba(240, 237, 230, 0.38);
  --separator: rgba(240, 237, 230, 0.12);
  --bar-bg: rgba(17, 17, 16, 0.82);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --tabbar-h: calc(49px + var(--safe-bottom));
}

[data-theme="light"] {
  --bg: #f2f2f0;
  --bg-grouped: #f2f2f0;
  --bg-raised: #ffffff;
  --bg-card: #ffffff;
  --bg-sheet: #ffffff;
  --fill: rgba(20, 20, 20, 0.07);
  --fill-strong: rgba(20, 20, 20, 0.14);
  --text: #1c1c1a;
  --text-2: rgba(28, 28, 26, 0.6);
  --text-3: rgba(28, 28, 26, 0.38);
  --separator: rgba(28, 28, 26, 0.12);
  --bar-bg: rgba(242, 242, 240, 0.82);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* ---------- Nav bar ---------- */
.nav-bar {
  flex-shrink: 0;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px) saturate(1.4);
  backdrop-filter: blur(20px) saturate(1.4);
  border-bottom: 0.5px solid var(--separator);
  z-index: 10;
}
.nav-bar-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 52px;
  padding: 0 12px;
}
.nav-title {
  flex: 1;
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-title-sm { font-size: 17px; font-weight: 600; text-align: center; }
.nav-actions { display: flex; gap: 4px; }
.nav-btn {
  display: grid;
  place-items: center;
  min-width: 38px;
  height: 38px;
  border-radius: 10px;
  color: var(--accent);
}
.nav-btn:active { background: var(--fill); }
.nav-btn[disabled] { color: var(--text-3); }

/* ---------- Views ---------- */
.view {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.view[hidden] { display: none; }
.view-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--tabbar-h) + 16px);
}

.view-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 50;
}

/* ---------- Tab bar ---------- */
.tab-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  height: var(--tabbar-h);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px) saturate(1.4);
  backdrop-filter: blur(20px) saturate(1.4);
  border-top: 0.5px solid var(--separator);
  z-index: 40;
}
.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--text-3);
  font-size: 10px;
  font-weight: 500;
}
.tab-item.active { color: var(--accent); }

/* ---------- Library ---------- */
.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 20px 14px;
  padding: 16px;
}
.book-card { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.book-cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 8px;
  overflow: hidden;
  background: var(--fill);
  display: grid;
  place-items: center;
  color: var(--text-3);
  box-shadow: 0 2px 10px rgba(0,0,0,0.35);
}
.book-cover img {
  width: 100%; height: 100%;
  object-fit: cover;
}
.book-cover .cover-glyph { width: 36%; height: 36%; }
.book-badge {
  position: absolute;
  top: 6px; right: 6px;
  padding: 2px 7px;
  border-radius: 6px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  background: rgba(0,0,0,0.65);
  color: #f0ede6;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.book-badge-audio { color: var(--accent); }
.book-progress {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 3px;
  background: rgba(0,0,0,0.4);
}
.book-progress i {
  display: block; height: 100%;
  background: var(--accent);
}
.book-card-title {
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-card-author {
  font-size: 11.5px;
  color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.book-needs-meta {
  position: absolute;
  top: 6px; left: 6px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  display: grid; place-items: center;
  color: var(--accent);
  font-size: 13px; font-weight: 700;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 80px 32px;
  text-align: center;
  color: var(--text-3);
}
.empty-title { font-size: 19px; font-weight: 600; color: var(--text); margin: 8px 0 0; }
.empty-sub { font-size: 14px; margin: 0 0 12px; max-width: 320px; }
.empty-state[hidden] { display: none; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  min-height: 44px;
}
.btn-primary { background: var(--accent); color: #16130e; }
.btn-primary:active { background: var(--accent-press); }
.btn-ghost { background: var(--fill); color: var(--accent); }
.btn-ghost:active { background: var(--fill-strong); }
.btn-danger { background: var(--fill); color: var(--danger); }
.btn[disabled] { opacity: 0.45; pointer-events: none; }

/* ---------- Grouped lists (settings) ---------- */
.list-group { margin: 20px 16px 0; }
.list-group-label {
  margin: 0 0 6px 12px;
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-3);
}
.list-group-note {
  margin: 6px 12px 0;
  font-size: 12px;
  color: var(--text-3);
}
.list-card {
  background: var(--bg-card);
  border-radius: 12px;
  overflow: hidden;
}
.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
  padding: 10px 14px;
  font-size: 15px;
  border-bottom: 0.5px solid var(--separator);
  width: 100%;
  text-align: left;
}
.list-row:last-child { border-bottom: 0; }
.list-row-btn:active { background: var(--fill); }
.list-row-slider { padding-top: 2px; padding-bottom: 8px; }
.list-value { color: var(--text-2); font-size: 14px; max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Segmented control ---------- */
.seg {
  display: inline-flex;
  background: var(--fill);
  border-radius: 9px;
  padding: 2px;
}
.seg button {
  padding: 5px 12px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
}
.seg button.active {
  background: var(--bg-raised);
  color: var(--text);
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
[data-theme="light"] .seg button.active { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.15); }

/* ---------- Stepper ---------- */
.stepper {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--fill);
  border-radius: 9px;
}
.stepper button {
  width: 36px; height: 32px;
  font-size: 20px;
  color: var(--accent);
  display: grid; place-items: center;
}
.stepper span { min-width: 52px; text-align: center; font-size: 14px; font-variant-numeric: tabular-nums; }

/* ---------- Sheets ---------- */
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 90;
  opacity: 1;
  transition: opacity 0.25s;
}
.sheet-overlay[hidden] { display: none; }
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  max-height: 82dvh;
  background: var(--bg-sheet);
  border-radius: 14px 14px 0 0;
  z-index: 95;
  padding-bottom: calc(var(--safe-bottom) + 12px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  box-shadow: 0 -8px 40px rgba(0,0,0,0.5);
}
.sheet[hidden] { display: none; }
.sheet-grabber {
  width: 36px; height: 5px;
  border-radius: 3px;
  background: var(--fill-strong);
  margin: 8px auto 4px;
}
.sheet-body { padding: 8px 16px 4px; }
.sheet-title {
  margin: 6px 16px 10px;
  font-size: 17px;
  font-weight: 600;
  text-align: center;
}
.sheet-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
}

/* ---------- Edit fields ---------- */
.edit-field {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  border-bottom: 0.5px solid var(--separator);
  font-size: 15px;
}
.edit-field:last-child { border-bottom: 0; }
.edit-field span { width: 88px; flex-shrink: 0; color: var(--text-2); font-size: 14px; }
.edit-field input, .edit-field textarea {
  flex: 1;
  min-width: 0;
  background: none;
  border: 0;
  outline: none;
  padding: 8px 0;
  color: var(--text);
  -webkit-user-select: text;
  user-select: text;
}
.edit-field-area { align-items: flex-start; }
.edit-field textarea { resize: none; }
.sheet-list { max-height: 60dvh; overflow-y: auto; }
.sheet-list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 16px;
  text-align: left;
  border-bottom: 0.5px solid var(--separator);
}
.sheet-list-item:active { background: var(--fill); }
.sheet-list-item .item-text { flex: 1; min-width: 0; }
.sheet-list-item .item-title {
  font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sheet-list-item .item-sub { font-size: 12.5px; color: var(--text-2); }
.sheet-list-item .item-check { color: var(--accent); font-weight: 700; }
.sheet-list-thumb {
  width: 40px; height: 56px;
  border-radius: 4px;
  object-fit: cover;
  background: var(--fill);
  flex-shrink: 0;
}

/* ---------- Book detail sheet ---------- */
.book-detail-head { display: flex; gap: 16px; }
.book-detail-cover {
  width: 96px; height: 144px;
  border-radius: 8px;
  background: var(--fill);
  overflow: hidden;
  display: grid; place-items: center;
  color: var(--text-3);
  flex-shrink: 0;
}
.book-detail-cover img { width: 100%; height: 100%; object-fit: cover; }
.book-detail-meta { min-width: 0; }
.book-detail-meta h2 { margin: 2px 0 4px; font-size: 19px; line-height: 1.25; }
.book-detail-meta p { margin: 0 0 4px; font-size: 14px; color: var(--text-2); }
.book-detail-format {
  font-size: 11px !important;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-3) !important;
}

/* ---------- Reader ---------- */
.reader-stage {
  position: absolute;
  inset: 0;
  background: var(--bg);
}
.reader-stage foliate-view { display: block; width: 100%; height: 100%; }
#view-reader .reader-bar {
  position: absolute;
  top: 0; left: 0; right: 0;
  transition: transform 0.25s, opacity 0.25s;
}
#view-reader .reader-bottom {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px) saturate(1.4);
  backdrop-filter: blur(20px) saturate(1.4);
  border-top: 0.5px solid var(--separator);
  transition: transform 0.25s, opacity 0.25s;
}
#view-reader.chrome-hidden .reader-bar { transform: translateY(-110%); opacity: 0; }
#view-reader.chrome-hidden .reader-bottom { transform: translateY(110%); opacity: 0; }

.reader-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
}
.reader-progress-label {
  font-size: 11px;
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
  min-width: 42px;
}
.reader-progress-label:last-child { text-align: right; }

.progress-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 28px;
  background: transparent;
}
.progress-slider::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: var(--fill-strong);
}
.progress-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #fff;
  margin-top: -8px;
  box-shadow: 0 1px 6px rgba(0,0,0,0.4);
}
.progress-slider::-moz-range-track {
  height: 4px; border-radius: 2px; background: var(--fill-strong);
}
.progress-slider::-moz-range-thumb {
  width: 20px; height: 20px; border: 0; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 6px rgba(0,0,0,0.4);
}

/* text/pdf renderer containers */
.text-reader {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  -webkit-user-select: text;
  user-select: text;
  padding: calc(var(--safe-top) + 60px) 22px calc(var(--safe-bottom) + 90px);
  font-size: var(--reader-font-size, 18px);
  line-height: 1.6;
}
.text-reader h1, .text-reader h2, .text-reader h3 { line-height: 1.3; }
.pdf-reader {
  position: absolute;
  inset: 0;
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
}
.pdf-page {
  flex: 0 0 100%;
  height: 100%;
  scroll-snap-align: start;
  display: grid;
  place-items: center;
  padding: calc(var(--safe-top) + 52px) 8px calc(var(--safe-bottom) + 80px);
}
.pdf-page canvas { max-width: 100%; max-height: 100%; object-fit: contain; }

/* reader themes applied to stage */
.reader-theme-light { background: #fbf8f2; color: #222; }
.reader-theme-sepia { background: #f4ecd9; color: #3a2f1d; }
.reader-theme-dark { background: #131311; color: #e8e4da; }

/* ---------- TTS bar ---------- */
.tts-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-bottom: 0.5px solid var(--separator);
}
.tts-status { font-size: 13px; color: var(--text-2); }

/* ---------- Player ---------- */
.player-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 28px calc(var(--safe-bottom) + 20px);
  overflow-y: auto;
}
.player-cover {
  width: min(70vw, 300px);
  aspect-ratio: 1;
  border-radius: 16px;
  background: var(--fill);
  display: grid;
  place-items: center;
  color: var(--text-3);
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0,0,0,0.5);
  margin: 8px 0 22px;
}
.player-cover img { width: 100%; height: 100%; object-fit: cover; }
.player-title {
  margin: 0 0 4px;
  font-size: 20px;
  font-weight: 700;
  text-align: center;
}
.player-author { margin: 0 0 8px; font-size: 15px; color: var(--text-2); }
.player-chapter {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--fill);
  color: var(--accent);
  font-size: 13px;
  font-weight: 500;
  max-width: 90%;
}
.player-chapter span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-progress { width: 100%; margin-top: 18px; }
.player-progress .progress-slider { width: 100%; }
.player-times {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-2);
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}
.player-controls {
  display: flex;
  align-items: center;
  gap: 18px;
  margin-top: 14px;
}
.player-play {
  width: 72px; height: 72px;
  border-radius: 50%;
  background: var(--text);
  color: var(--bg);
  display: grid; place-items: center;
}
.player-play:active { transform: scale(0.95); }
.player-speed {
  margin-top: 18px;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--fill);
  font-size: 14px;
  font-weight: 600;
}

/* ---------- Mini player ---------- */
.mini-player {
  position: fixed;
  left: 8px; right: 8px;
  bottom: calc(var(--tabbar-h) + 6px);
  z-index: 45;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 12px;
  background: var(--bg-raised);
  box-shadow: 0 4px 24px rgba(0,0,0,0.45);
}
.mini-cover {
  width: 38px; height: 38px;
  border-radius: 6px;
  background: var(--fill);
  overflow: hidden;
  flex-shrink: 0;
}
.mini-cover img { width: 100%; height: 100%; object-fit: cover; }
.mini-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.mini-title { font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mini-sub { font-size: 11.5px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: calc(var(--tabbar-h) + 18px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--bg-raised);
  color: var(--text);
  font-size: 14px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  max-width: 88vw;
}
.toast-err { color: var(--danger); }

/* ---------- Import progress ---------- */
.import-progress {
  position: fixed;
  top: calc(var(--safe-top) + 60px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--bg-raised);
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  font-size: 14px;
}
.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--fill-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation-duration: 0.01s !important; }
}
