/* ============================================================
   Cloud Reader — Reading View Styles
   Mobile-first reading experience with theme support
   ============================================================ */

/* --- CSS Custom Properties (overridden by JS) --- */
:root {
  --reader-bg: #f5f0eb;
  --reader-text: #1a1a1a;
  --reader-ui-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, sans-serif;
  --reader-margin: 16px;
  --reader-brightness: 100%;
  --reader-toolbar-bg: rgba(26, 26, 26, 0.95);
  --reader-toolbar-text: #e0e0e0;
  --reader-accent: #6c8cff;
}

/* --- Reading Container --- */
.reader-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--reader-bg);
  color: var(--reader-text);
  font-family: var(--reader-ui-font);
  filter: brightness(var(--reader-brightness));
  display: flex;
  flex-direction: column;
  z-index: 100;
  overflow: hidden;
}

/* Orientation lock */
@supports (--reader-orientation: portrait) {
  .reader-container {
    orientation: var(--reader-orientation);
  }
}

@media (orientation: landscape) {
  .reader-container {
    --reader-margin: 24px;
  }
}

/* --- Top Toolbar --- */
.reader-top-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 52px;
  background: var(--reader-toolbar-bg);
  color: var(--reader-toolbar-text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  z-index: 120;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.reader-bar-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: transparent;
  border: none;
  color: var(--reader-toolbar-text);
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.15s;
}

.reader-bar-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.reader-bar-title {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  min-width: 0;
  flex: 1;
  padding: 0 8px;
}

.reader-bar-book {
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
  font-family: var(--reader-ui-font);
}

.reader-bar-chapter {
  font-size: 0.7rem;
  opacity: 0.7;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
  font-family: var(--reader-ui-font);
}

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

/* --- Progress Bar --- */
.reader-progress-bar {
  position: fixed;
  top: 52px;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(255, 255, 255, 0.1);
  z-index: 120;
}

.reader-progress-fill {
  height: 100%;
  background: var(--reader-accent);
  transition: width 0.3s ease;
}

/* --- EPUB Reader (react-reader) --- */
.reader-epub-container {
  position: fixed;
  top: 55px;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  height: calc(100vh - 55px);
  isolation: isolate;
}

.reader-epub-container > div {
  height: 100% !important;
}

.reader-page-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
  border: none;
  background: none;
  font-size: 48px;
  line-height: 1;
  padding: 0 10px;
  color: rgba(184, 168, 152, 0.9);
  cursor: pointer;
  user-select: none;
}

.reader-page-nav:hover {
  color: rgba(138, 122, 106, 1);
}

.reader-page-nav--prev {
  left: 4px;
}

.reader-page-nav--next {
  right: 4px;
}

.reader-resume-reading {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(calc(-50% - 56px));
  z-index: 12;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 12px;
  border: none;
  border-radius: 12px;
  background: #ea580c;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.2;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(234, 88, 12, 0.45);
  max-width: 72px;
  text-align: center;
}

.reader-resume-reading:hover {
  background: #c2410c;
}

.reader-resume-reading-icon {
  font-size: 20px;
  line-height: 1;
}

.reader-resume-reading-label {
  display: block;
}

.reader-container[data-theme="dark"] .reader-page-nav {
  color: rgba(102, 102, 102, 0.9);
}

.reader-container[data-theme="dark"] .reader-page-nav:hover {
  color: rgba(170, 170, 170, 1);
}

.reader-container[data-theme="light"] .reader-page-nav,
.reader-container[data-theme="paper"] .reader-page-nav {
  color: rgba(204, 204, 204, 0.9);
}

.reader-container[data-theme="light"] .reader-page-nav:hover,
.reader-container[data-theme="paper"] .reader-page-nav:hover {
  color: rgba(136, 136, 136, 1);
}

/* --- Loading State --- */
.reader-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 48px;
  color: var(--reader-text);
  min-height: 100vh;
}

.reader-loading .spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(108, 140, 255, 0.2);
  border-top-color: var(--reader-accent);
  border-radius: 50%;
  animation: readerSpin 0.8s linear infinite;
}

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

.back-button {
  padding: 10px 20px;
  background: var(--reader-accent);
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 0.9rem;
  cursor: pointer;
  font-family: var(--reader-ui-font);
}

.back-button:hover {
  background: #5a7ae8;
}

.reader-error {
  color: #ef4444;
  text-align: center;
}

.settings-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 130;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.settings-drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 300px;
  max-width: 85vw;
  background: var(--reader-toolbar-bg);
  color: var(--reader-toolbar-text);
  font-family: var(--reader-ui-font);
  z-index: 140;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  right: 0;
  transform: translateX(100%);
}

.settings-drawer--open {
  transform: translateX(0);
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.settings-title {
  font-size: 1.1rem;
  font-weight: 700;
  font-family: var(--reader-ui-font);
}

.settings-close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: transparent;
  border: none;
  color: var(--reader-toolbar-text);
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.15s;
}

.settings-close-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* --- TOC Drawer (custom, epub.js nav) --- */
.toc-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 130;
  animation: fadeIn 0.2s ease;
}

.toc-drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: 300px;
  max-width: 85vw;
  background: var(--reader-toolbar-bg);
  color: var(--reader-toolbar-text);
  font-family: var(--reader-ui-font);
  z-index: 140;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.toc-drawer--open {
  transform: translateX(0);
}

.toc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  margin-top: 52px;
}

.toc-title {
  font-size: 1.1rem;
  font-weight: 700;
  font-family: var(--reader-ui-font);
}

.toc-close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: transparent;
  border: none;
  color: var(--reader-toolbar-text);
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.15s;
}

.toc-close-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.toc-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.toc-empty {
  padding: 24px 16px;
  text-align: center;
  opacity: 0.6;
  font-size: 0.9rem;
}

.toc-item {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 10px 16px;
  background: transparent;
  border: none;
  color: var(--reader-toolbar-text);
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
  font-family: var(--reader-ui-font);
}

.toc-item:hover {
  background: rgba(255, 255, 255, 0.08);
}

.toc-item-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- Settings Panel --- */
.settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px 32px;
}

.settings-section {
  margin-bottom: 20px;
}

.settings-section-title {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: rgba(255, 255, 255, 0.5);
  margin-bottom: 8px;
  font-family: var(--reader-ui-font);
}

/* Theme grid */
.theme-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.theme-btn {
  padding: 10px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  background: transparent;
  color: var(--reader-toolbar-text);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--reader-ui-font);
}

.theme-btn:hover {
  border-color: rgba(255, 255, 255, 0.3);
}

.theme-btn--active {
  border-color: var(--reader-accent);
  background: rgba(108, 140, 255, 0.15);
  color: var(--reader-accent);
  font-weight: 600;
}

/* Font grid */
.font-grid,
.orientation-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.font-btn,
.orientation-btn {
  padding: 10px 8px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  background: transparent;
  color: var(--reader-toolbar-text);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--reader-ui-font);
}

.font-btn:hover,
.orientation-btn:hover {
  border-color: rgba(255, 255, 255, 0.3);
}

.font-btn--active,
.orientation-btn--active {
  border-color: var(--reader-accent);
  background: rgba(108, 140, 255, 0.15);
  color: var(--reader-accent);
  font-weight: 600;
}

/* Sliders */
.settings-slider {
  width: 100%;
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}

.settings-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--reader-accent);
  border: 2px solid white;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

.settings-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--reader-accent);
  border: 2px solid white;
  cursor: pointer;
}

/* --- Start Reading Button (in book detail) --- */
.start-reading-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  background: var(--reader-accent);
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  margin-top: 16px;
  margin-bottom: 16px;
  font-family: var(--reader-ui-font);
}

.start-reading-btn:hover {
  background: #5a7ae8;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(108, 140, 255, 0.3);
}

/* --- Responsive --- */
@media (max-width: 480px) {
  .settings-drawer,
  .toc-drawer {
    max-width: 100vw;
  }

  .reader-epub-container {
    top: 52px;
    height: calc(100vh - 52px);
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .reader-top-bar {
    height: 44px;
  }

  .reader-epub-container {
    top: 47px;
    height: calc(100vh - 47px);
  }
}

/* --- Selection popover --- */
.selection-popover-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: transparent;
}

.selection-popover {
  position: fixed;
  z-index: 210;
  width: min(280px, calc(100vw - 24px));
  padding: 12px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  border: 1px solid #e5e7eb;
}

.selection-popover-quote {
  font-size: 0.8125rem;
  font-style: italic;
  color: #374151;
  margin: 0 0 10px;
  line-height: 1.5;
}

.bookmark-color-picker {
  margin-bottom: 10px;
}

.bookmark-color-picker-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: #6b7280;
  margin-bottom: 6px;
}

.bookmark-color-picker-swatches {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.bookmark-color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.bookmark-color-swatch--active {
  border-color: #1f2937;
  box-shadow: 0 0 0 2px #fff, 0 0 0 4px #1f2937;
}

.bookmark-color-custom {
  position: relative;
  display: inline-flex;
  cursor: pointer;
}

.bookmark-color-custom-input {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
  pointer-events: none;
}

.bookmark-color-swatch--custom {
  pointer-events: none;
}

.selection-popover-input {
  width: 100%;
  box-sizing: border-box;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.875rem;
  resize: vertical;
  font-family: var(--reader-ui-font);
}

.selection-popover-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

/* --- Reader side panel (markers) --- */
.reader-panel-backdrop {
  position: fixed;
  inset: 0;
  z-index: 140;
  background: rgba(0, 0, 0, 0.35);
}

.reader-side-panel {
  position: fixed;
  top: 55px;
  right: 0;
  bottom: 0;
  width: min(360px, 100vw);
  z-index: 150;
  background: #fff;
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.12);
  display: flex;
  flex-direction: column;
}

.reader-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid #eee;
}

.reader-panel-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.reader-panel-close {
  border: none;
  background: none;
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  color: #666;
}

.reader-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
}

/* --- Dark mode theme override when reader uses light theme but system is dark --- */
@media (prefers-color-scheme: dark) {
  .reader-container[data-theme="light"] {
    --reader-toolbar-bg: rgba(255, 255, 255, 0.95);
    --reader-toolbar-text: #1a1a1a;
  }
}