/* ==========================================================================
   Family RSVP Speed Reader — Stylesheet (Dark / Light / Sepia + iOS PWA)
   ========================================================================== */

:root,
html[data-theme="dark"] {
  --bg: #0e0e11;
  --bg-elevated: #17171d;
  --bg-card: #1c1c24;
  --bg-card-hover: #24242f;
  --bg-input: #131318;
  --border: #2c2c38;
  --border-subtle: #22222c;
  --text: #f3f3f7;
  --text-muted: #9898a8;
  --text-faint: #626273;
  --orp: #ef4444;
  --accent: #ef4444;
  --accent-hover: #dc2626;
  --accent-soft: rgba(239, 68, 68, 0.14);
  --rail: #2f2f3d;
  --danger: #f87171;
  --success: #34d399;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  --backdrop: rgba(0, 0, 0, 0.68);
}

html[data-theme="light"] {
  --bg: #f6f7f9;
  --bg-elevated: #ffffff;
  --bg-card: #ffffff;
  --bg-card-hover: #f1f3f7;
  --bg-input: #f2f4f8;
  --border: #dde1ea;
  --border-subtle: #e9ecf2;
  --text: #16181f;
  --text-muted: #5d6475;
  --text-faint: #8e96a8;
  --orp: #dc2626;
  --accent: #dc2626;
  --accent-hover: #b91c1c;
  --accent-soft: rgba(220, 38, 38, 0.11);
  --rail: #d2d7e2;
  --danger: #dc2626;
  --success: #059669;
  --shadow: 0 12px 30px rgba(15, 23, 42, 0.1);
  --backdrop: rgba(15, 23, 42, 0.45);
}

html[data-theme="sepia"] {
  --bg: #f4ecd8;
  --bg-elevated: #fbf5e6;
  --bg-card: #fbf5e6;
  --bg-card-hover: #efe4ca;
  --bg-input: #ede2c7;
  --border: #d9caa8;
  --border-subtle: #e4d8bb;
  --text: #322618;
  --text-muted: #6e5c45;
  --text-faint: #9b886e;
  --orp: #c83327;
  --accent: #c83327;
  --accent-hover: #a6251b;
  --accent-soft: rgba(200, 51, 39, 0.12);
  --rail: #cfc09f;
  --danger: #b91c1c;
  --success: #2f7d4d;
  --shadow: 0 12px 30px rgba(50, 38, 24, 0.14);
  --backdrop: rgba(38, 28, 16, 0.5);
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button,
input {
  font-family: inherit;
  color: inherit;
}

button {
  touch-action: manipulation;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.orp-accent {
  color: var(--orp);
}

/* Views */
.view {
  display: none;
  min-height: 100vh;
  min-height: 100dvh;
}

.view.active {
  display: block;
}

.view-center.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: calc(20px + env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
}

/* Splash & Spinner */
.splash-logo {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  margin-bottom: 18px;
}

.spinner {
  width: 30px;
  height: 30px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

.spinner-sm {
  width: 18px;
  height: 18px;
  border-width: 2px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 11px;
  padding: 10px 16px;
  font-size: 0.94rem;
  font-weight: 600;
  background: transparent;
  color: var(--text);
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--accent);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-card-hover);
}

.btn-danger-ghost {
  color: var(--danger);
  border-color: transparent;
}

.btn-danger-ghost:hover:not(:disabled) {
  background: rgba(239, 68, 68, 0.12);
}

.btn-sm {
  padding: 7px 12px;
  font-size: 0.86rem;
  border-radius: 9px;
}

.btn-block {
  width: 100%;
}

.btn-icon-only {
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid transparent;
  color: var(--text-muted);
}

.btn-icon-only:hover {
  background: var(--bg-card-hover);
  color: var(--text);
}

/* Inputs & Forms */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.field label {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--text-muted);
}

.field-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field input[type="search"],
.search-box input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  font-size: 1rem; /* >=16px prevents iOS Safari auto-zoom on focus */
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease;
}

.field input:focus,
.search-box input:focus {
  border-color: var(--accent);
}

.alert-error {
  background: rgba(239, 68, 68, 0.13);
  border: 1px solid rgba(239, 68, 68, 0.35);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.88rem;
  margin-bottom: 14px;
}

/* Auth Card */
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 18px 24px 24px;
  box-shadow: var(--shadow);
}

.auth-top-row {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
}

.lang-switch {
  display: inline-flex;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: 9px;
  padding: 3px;
  gap: 2px;
}

.lang-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 4px 9px;
  border-radius: 6px;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.lang-btn.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.btn-lang-cycle {
  min-width: 38px;
  padding: 7px 9px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.auth-brand {
  text-align: center;
  margin-bottom: 22px;
}

.brand-reticle {
  display: inline-block;
  border-top: 2px solid var(--rail);
  border-bottom: 2px solid var(--rail);
  padding: 6px 20px;
  position: relative;
}

.brand-reticle::before,
.brand-reticle::after {
  content: "";
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 7px;
  background: var(--orp);
}

.brand-reticle::before { top: 0; }
.brand-reticle::after { bottom: 0; }

.brand-word {
  font-size: 1.85rem;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.auth-subtitle {
  margin: 10px 0 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.segmented-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 18px;
}

.tab-btn {
  border: none;
  background: transparent;
  padding: 8px 10px;
  border-radius: 9px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-muted);
  transition: background-color 0.15s ease, color 0.15s ease;
}

.tab-btn.active {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

/* Library Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border-subtle);
  padding-top: env(safe-area-inset-top);
}

.topbar-inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.user-pill {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  padding: 3px 9px;
  border-radius: 999px;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Library Main */
.library-main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 18px 16px calc(32px + env(safe-area-inset-bottom));
}

.library-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.filter-pills {
  display: inline-flex;
  gap: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 4px;
}

.pill {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 7px 13px;
  border-radius: 8px;
  font-size: 0.86rem;
  font-weight: 600;
}

.pill.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.search-box {
  flex: 1 1 220px;
  max-width: 340px;
}

.search-box input {
  padding: 9px 13px;
  font-size: 0.94rem;
}

/* Storage Quota Bar */
.storage-bar-wrap {
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 10px 14px;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.storage-bar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.storage-bar-header strong {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.storage-track {
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: var(--bg-input);
  overflow: hidden;
}

.storage-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--success);
  transition: width 0.25s ease, background-color 0.2s ease;
}

.storage-fill.warn {
  background: #f59e0b;
}

.storage-fill.full {
  background: var(--danger);
}

.dropzone-quota {
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--text-faint);
  margin-top: 2px;
}

/* Book Cards Grid */
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}

.book-card {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  cursor: pointer;
  transition: transform 0.14s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.book-card:hover {
  border-color: var(--border);
  background: var(--bg-card-hover);
}

.book-card:active {
  transform: scale(0.99);
}

.book-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.book-badges {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  border-radius: 6px;
}

.badge-epub {
  background: rgba(99, 102, 241, 0.16);
  color: #818cf8;
}

.badge-pdf {
  background: rgba(239, 68, 68, 0.15);
  color: var(--orp);
}

.badge-shared {
  background: rgba(16, 185, 129, 0.15);
  color: var(--success);
  text-transform: none;
  letter-spacing: 0;
}

.book-menu-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
}

.book-menu-btn:hover {
  background: var(--bg-input);
  color: var(--text);
}

.book-info {
  min-height: 48px;
}

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

.book-author {
  margin: 4px 0 0;
  font-size: 0.86rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-faint);
}

.book-progress-wrap {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.book-progress-bar {
  width: 100%;
  height: 6px;
  background: var(--bg-input);
  border-radius: 999px;
  overflow: hidden;
}

.book-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.25s ease;
}

.book-progress-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.empty-state {
  text-align: center;
  padding: 64px 20px;
  max-width: 380px;
  margin: 0 auto;
}

.empty-icon {
  font-size: 2.8rem;
  margin-bottom: 10px;
}

.empty-state h3 {
  margin: 0 0 8px;
}

.empty-state p {
  margin: 0 0 20px;
  color: var(--text-muted);
  font-size: 0.92rem;
}

/* ==========================================================================
   RSVP Reader View — Distraction-Free & Strictly Centered ORP
   ========================================================================== */

.reader-view.active {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
}

.reader-topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg);
  transition: opacity 0.25s ease;
  z-index: 5;
}

.reader-title-wrap {
  flex: 1 1 auto;
  min-width: 0;
  text-align: center;
}

.reader-book-title {
  font-size: 0.92rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reader-chapter-title {
  font-size: 0.76rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reader-top-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Stage */
.rsvp-stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  width: 100%;
  padding: 16px 12px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  outline: none;
  overflow: hidden;
}

.rsvp-focus-frame {
  width: 100%;
  max-width: 780px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 18px;
}

.rsvp-rail {
  position: relative;
  height: 2px;
  background: var(--rail);
  width: 100%;
  border-radius: 2px;
}

.rsvp-tick {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 2.5px;
  height: 14px;
  background: var(--orp);
  border-radius: 2px;
}

.rsvp-rail-top .rsvp-tick {
  top: 0;
}

.rsvp-rail-bottom .rsvp-tick {
  bottom: 0;
}

.rsvp-word-box {
  width: 100%;
  min-height: 92px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
}

/* CRITICAL ORP ALIGNMENT:
   minmax(0, 1fr) on both sides guarantees the center `auto` track (the red ORP letter)
   stays locked at exact horizontal 50% regardless of left/right text lengths! */
.rsvp-word {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: baseline;
  width: 100%;
  font-size: var(--reader-font-size, 44px);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: 0.01em;
  font-variant-ligatures: none;
  transform-origin: center center;
}

.rsvp-left {
  grid-column: 1;
  min-width: 0;
  text-align: right;
  white-space: pre;
  overflow: visible;
  direction: ltr;
}

.rsvp-orp {
  grid-column: 2;
  text-align: center;
  color: var(--orp);
  font-weight: 700;
  white-space: pre;
}

.rsvp-right {
  grid-column: 3;
  min-width: 0;
  text-align: left;
  white-space: pre;
  overflow: visible;
}

/* Context preview visible during playback & pause (fixed height prevents ORP vertical jitter) */
.rsvp-context {
  margin-top: 24px;
  max-width: 640px;
  width: 100%;
  height: 4.6em;
  overflow: hidden;
  text-align: center;
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--text-muted);
  padding: 0 12px;
  transition: opacity 0.2s ease;
}

.rsvp-context strong {
  color: var(--text);
  font-weight: 700;
  text-decoration: underline;
  text-decoration-color: var(--orp);
  text-underline-offset: 3px;
}

.rsvp-tap-hint {
  margin-top: 10px;
  min-height: 1.2em;
  font-size: 0.78rem;
  color: var(--text-faint);
  transition: opacity 0.2s ease;
}

/* Distraction-free dimming while actively playing */
.reader-view.is-playing .reader-topbar,
.reader-view.is-playing .reader-controls {
  opacity: 0.28;
}

.reader-view.is-playing .reader-topbar:hover,
.reader-view.is-playing .reader-controls:hover {
  opacity: 0.95;
}

.reader-view.is-playing .rsvp-context {
  opacity: 0.72;
  pointer-events: none;
}

.reader-view.is-playing .rsvp-tap-hint {
  opacity: 0;
  pointer-events: none;
}

/* Literature References Panel (directly below the reading field) */
.rsvp-refs-panel {
  flex: 0 0 auto;
  width: min(680px, calc(100% - 24px));
  margin: 0 auto 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  max-height: 30dvh;
  overflow: hidden;
  z-index: 6;
  animation: toastIn 0.18s ease;
}

.rsvp-refs-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
}

.rsvp-refs-title-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.rsvp-refs-badge {
  background: var(--accent-soft);
  color: var(--accent);
}

.rsvp-refs-cite {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rsvp-refs-list {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rsvp-ref-item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 0.85rem;
  line-height: 1.42;
  color: var(--text);
  text-decoration: none;
  border-radius: 9px;
  padding: 5px 7px;
  margin: -5px -7px;
  transition: background-color 0.14s ease;
}

.rsvp-ref-item.is-link {
  cursor: pointer;
}

.rsvp-ref-item.is-link:hover {
  background: var(--bg-card-hover);
}

.rsvp-ref-item.is-link:hover .rsvp-ref-url {
  text-decoration: underline;
}

.rsvp-ref-num {
  flex-shrink: 0;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.rsvp-ref-body {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  flex: 1 1 auto;
}

.rsvp-ref-text {
  color: var(--text-muted);
  word-break: break-word;
}

.rsvp-ref-item.is-link .rsvp-ref-text {
  color: var(--text);
}

.rsvp-ref-url {
  font-size: 0.76rem;
  color: var(--accent);
  word-break: break-all;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

@media (max-height: 700px) {
  .rsvp-refs-panel {
    max-height: 24dvh;
  }
}

/* Bottom Controls */
.reader-controls {
  flex: 0 0 auto;
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: opacity 0.25s ease;
  z-index: 5;
}

.transport-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.btn-control {
  flex: 1 1 0;
  max-width: 104px;
  flex-direction: column;
  gap: 2px;
  padding: 8px 6px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 13px;
  color: var(--text);
}

.btn-control:hover {
  background: var(--bg-card-hover);
}

.ctrl-symbol {
  font-size: 1.05rem;
  line-height: 1.1;
}

.ctrl-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-weight: 600;
}

.btn-play {
  width: 62px;
  height: 54px;
  border-radius: 16px;
  background: var(--accent);
  color: #ffffff;
  font-size: 1.35rem;
  flex-shrink: 0;
  box-shadow: 0 6px 20px rgba(239, 68, 68, 0.32);
}

.btn-play:hover {
  background: var(--accent-hover);
}

.speed-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-mini {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  font-size: 1.2rem;
  font-weight: 600;
  flex-shrink: 0;
}

.btn-mini:hover {
  background: var(--bg-card-hover);
}

.speed-slider-wrap {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.speed-header {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.speed-header strong {
  color: var(--text);
}

.progress-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.progress-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.76rem;
  color: var(--text-muted);
  gap: 8px;
}

.save-indicator {
  color: var(--success);
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.save-indicator.visible {
  opacity: 1;
}

/* Custom Range Sliders (WebKit iOS + Firefox) */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: var(--border);
  outline: none;
  margin: 6px 0;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-elevated);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-elevated);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

/* ==========================================================================
   Modals, Switches, Dropzone, Drawers & Toasts
   ========================================================================== */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--backdrop);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(16px + env(safe-area-inset-top)) 14px calc(16px + env(safe-area-inset-bottom));
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-card {
  width: 100%;
  max-width: 460px;
  max-height: 88dvh;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-card-lg {
  max-width: 580px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 18px;
  border-bottom: 1px solid var(--border-subtle);
}

.modal-header h2 {
  margin: 0;
  font-size: 1.08rem;
}

.modal-body {
  padding: 18px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.modal-footer-between {
  justify-content: space-between;
  align-items: center;
}

.modal-footer-right {
  display: flex;
  gap: 8px;
}

/* Dropzone */
.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 6px;
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 22px 14px;
  margin-bottom: 16px;
  cursor: pointer;
  background: var(--bg-input);
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.dropzone.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.dropzone input[type="file"] {
  display: none;
}

.dropzone-icon {
  font-size: 1.8rem;
}

.dropzone-text {
  font-weight: 600;
  font-size: 0.94rem;
  word-break: break-word;
}

.dropzone-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.upload-status {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  color: var(--text-muted);
  padding: 8px 0;
}

/* Toggle Switch */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 14px;
  cursor: pointer;
}

.muted-small {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.switch-input {
  -webkit-appearance: none;
  appearance: none;
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  flex-shrink: 0;
  cursor: pointer;
  transition: background-color 0.18s ease;
}

.switch-input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  transition: transform 0.18s ease;
}

.switch-input:checked {
  background: var(--accent);
}

.switch-input:checked::after {
  transform: translateX(20px);
}

/* Theme & Language Pickers + Font Preview */
.lang-picker {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.lang-option {
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--bg-input);
  border: 2px solid var(--border-subtle);
  font-size: 0.9rem;
  font-weight: 600;
  transition: border-color 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}

.lang-option.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.theme-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.theme-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 8px;
  border-radius: 12px;
  background: var(--bg-input);
  border: 2px solid var(--border-subtle);
  font-size: 0.84rem;
  font-weight: 600;
}

.theme-option.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.theme-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--border);
}

.swatch-dark { background: #141419; }
.swatch-light { background: #ffffff; }
.swatch-sepia { background: #f4ecd8; }

.font-preview-box {
  margin-top: 8px;
  background: var(--bg);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 16px 8px;
  overflow: hidden;
}

.preview-word {
  font-size: var(--reader-font-size, 44px);
}

/* Password Details */
.password-details {
  margin-top: 14px;
  border-top: 1px solid var(--border-subtle);
  padding-top: 12px;
}

.password-details summary {
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-muted);
  user-select: none;
}

.password-form {
  margin-top: 12px;
}

/* Admin Invites */
.invite-create-box {
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 18px;
}

.invite-create-box h3,
.admin-section h3 {
  margin: 0 0 10px;
  font-size: 0.94rem;
}

.invite-form-grid {
  display: grid;
  grid-template-columns: 1fr 140px;
  gap: 10px;
}

.checkbox-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
  color: var(--text-muted);
  margin-bottom: 12px;
  cursor: pointer;
}

.admin-section {
  margin-bottom: 18px;
}

.admin-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.admin-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: 11px;
  padding: 10px 12px;
}

.invite-code-mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.invite-meta {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.invite-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* Chapters Drawer */
.drawer-backdrop {
  justify-content: flex-end;
  padding: 0;
}

.drawer-panel {
  width: min(360px, 88vw);
  height: 100%;
  background: var(--bg-card);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.chapters-list {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
}

.chapter-item {
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 11px 12px;
  border-radius: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  color: var(--text);
}

.chapter-item:hover {
  background: var(--bg-card-hover);
}

.chapter-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
}

.chapter-pct {
  font-size: 0.76rem;
  color: var(--text-faint);
  flex-shrink: 0;
}

.toc-filter-bar {
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
}

.toc-filter-btn {
  flex: 1 1 auto;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.toc-filter-btn:hover {
  background: var(--bg-card-hover);
  color: var(--text);
}

.toc-filter-btn.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.chapter-item.is-media-item {
  padding-top: 7px;
  padding-bottom: 7px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.chapter-item.is-media-item:hover {
  color: var(--text);
}

.chapter-item-main {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  flex: 1 1 auto;
}

.chapter-item-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toc-kind-chip {
  flex-shrink: 0;
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 6px;
  border-radius: 5px;
}

.toc-kind-chip.is-table {
  background: rgba(16, 185, 129, 0.15);
  color: var(--success);
}

.toc-kind-chip.is-formula {
  background: rgba(99, 102, 241, 0.16);
  color: #818cf8;
}

.toc-kind-chip.is-figure {
  background: var(--accent-soft);
  color: var(--accent);
}

/* Media Modal (Figures & Display Formulas) */
.modal-media-card {
  max-width: 720px;
  max-height: 92dvh;
}

.media-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex-wrap: wrap;
}

.media-kind-badge {
  background: var(--accent-soft);
  color: var(--accent);
}

.media-kind-badge.is-formula {
  background: rgba(99, 102, 241, 0.16);
  color: #818cf8;
}

.media-kind-badge.is-table {
  background: rgba(16, 185, 129, 0.16);
  color: var(--success);
}

.media-zoom-hint {
  font-size: 0.76rem;
  color: var(--text-faint);
}

.media-modal-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 16px 18px;
}

/* Light paper plate ensures black formulas & PDF diagrams stay sharp in dark mode */
.media-img-plate {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  padding: 12px;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  cursor: zoom-in;
  overflow: hidden;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.media-img-plate:hover {
  border-color: var(--accent);
}

.media-img-plate.light-surface {
  background: #f8f9fb;
  border-color: rgba(255, 255, 255, 0.16);
}

html[data-theme="sepia"] .media-img-plate.light-surface {
  background: #fbf7ec;
  border-color: var(--border);
}

.media-img-plate img {
  display: block;
  max-width: 100%;
  max-height: 54dvh;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 6px;
}

.media-img-plate.is-formula-plate {
  padding: 18px 16px;
}

.media-img-plate.is-formula-plate img {
  max-height: 36dvh;
}

/* EPUB HTML Table Viewer inside Media Modal */
.media-table-wrap {
  width: 100%;
  max-height: 54dvh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-input);
}

.epub-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
  line-height: 1.4;
}

.epub-table th,
.epub-table td {
  padding: 8px 11px;
  border: 1px solid var(--border-subtle);
  text-align: left;
  vertical-align: top;
}

.epub-table th {
  background: var(--bg-card-hover);
  font-weight: 700;
  color: var(--text);
}

.epub-table tr:nth-child(even) td {
  background: rgba(128, 128, 140, 0.05);
}

.media-caption {
  margin: 0;
  width: 100%;
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--text-muted);
  text-align: center;
  max-height: 22dvh;
  overflow-y: auto;
}

.media-modal-footer {
  padding: 12px 18px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border-subtle);
  background: var(--bg-card);
}

.btn-continue-media {
  width: 100%;
  padding: 13px 18px;
  font-size: 1rem;
  border-radius: 13px;
  gap: 10px;
}

.kbd-hint {
  font-size: 0.72rem;
  font-weight: 600;
  opacity: 0.8;
  background: rgba(255, 255, 255, 0.18);
  padding: 2px 7px;
  border-radius: 6px;
}

/* Fullscreen Zoom Lightbox */
.media-zoom-lightbox {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(8, 8, 12, 0.94);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(16px + env(safe-area-inset-top)) 12px calc(16px + env(safe-area-inset-bottom));
  cursor: zoom-out;
}

.media-zoom-close {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  right: 14px;
  z-index: 85;
  background: rgba(30, 30, 38, 0.85);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.media-zoom-scroll {
  max-width: 96vw;
  max-height: 90dvh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: #f8f9fb;
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
}

.media-zoom-scroll img {
  display: block;
  width: auto;
  height: auto;
  max-width: none;
  min-width: min(88vw, 760px);
}

.ctx-media-tag {
  display: inline-block;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

@media (max-width: 600px) {
  .kbd-hint {
    display: none;
  }

  .media-zoom-hint {
    display: none;
  }
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  width: min(340px, 90vw);
}

.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 15px;
  border-radius: 12px;
  font-size: 0.88rem;
  font-weight: 600;
  text-align: center;
  box-shadow: var(--shadow);
  animation: toastIn 0.2s ease;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Responsive tweaks for compact phones */
@media (max-width: 520px) {
  .invite-form-grid {
    grid-template-columns: 1fr;
  }

  .user-pill {
    display: none;
  }

  .topbar-inner {
    padding: 10px 12px;
  }

  .book-grid {
    grid-template-columns: 1fr;
  }
}
