/* =============================================================================
   Easy EPUB Reader — Frontend Styles
   Kindle-inspired dark theme with amber accents
   ============================================================================= */

/* Import Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap');

/* ---- CSS Variables ---- */
:root {
  --epub-bg:           #111118;
  --epub-surface:      #1a1a2e;
  --epub-surface-2:    #16213e;
  --epub-border:       rgba(255,255,255,0.08);
  --epub-amber:        #f5a623;
  --epub-amber-dim:    rgba(245,166,35,0.15);
  --epub-amber-glow:   rgba(245,166,35,0.35);
  --epub-text:         #e8e8f0;
  --epub-text-muted:   rgba(232,232,240,0.5);
  --epub-white:        #ffffff;
  --epub-toolbar-h:    56px;
  --epub-bottom-h:     52px;
  --epub-radius:       12px;
  --epub-shadow:       0 8px 32px rgba(0,0,0,0.6);
  --epub-transition:   0.2s cubic-bezier(0.4,0,0.2,1);
  --epub-font:         'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --epub-page-bg:      #f0e9d8;
  --epub-zoom-min:     0.5;
  --epub-zoom-max:     4.0;
}

/* ---- Wrapper ---- */
.easy-epub-reader-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  background: var(--epub-bg);
  border-radius: var(--epub-radius);
  overflow: hidden;
  box-shadow: var(--epub-shadow);
  font-family: var(--epub-font);
  color: var(--epub-text);
  /* Auto-height based on content */
  height: auto;
  min-height: min-content;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none; /* Hammer.js takes over */
}

/* ============= LOADING ============= */
.epub-loading {
  position: absolute;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--epub-bg);
  gap: 16px;
  transition: opacity var(--epub-transition);
}

.epub-loading.hidden {
  opacity: 0;
  pointer-events: none;
}

.epub-loading-spinner {
  width: 48px;
  height: 48px;
  border: 3px solid var(--epub-border);
  border-top-color: var(--epub-amber);
  border-radius: 50%;
  animation: epub-spin 0.8s linear infinite;
}

.epub-loading-text {
  color: var(--epub-text-muted);
  font-size: 14px;
  margin: 0;
}

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

/* ============= TOOLBAR ============= */
.epub-toolbar {
  position: relative;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--epub-toolbar-h);
  padding: 0 12px;
  background: var(--epub-surface);
  border-bottom: 1px solid var(--epub-border);
  backdrop-filter: blur(12px);
  flex-shrink: 0;
  gap: 8px;
  transition: transform var(--epub-transition), opacity var(--epub-transition);
}

.epub-toolbar.hidden {
  transform: translateY(-100%);
  opacity: 0;
  pointer-events: none;
}

.epub-toolbar-left,
.epub-toolbar-right {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.epub-toolbar-center {
  flex: 1;
  overflow: hidden;
  text-align: center;
}

.epub-book-title {
  margin: 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--epub-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 0.01em;
}

/* ---- Toolbar Buttons ---- */
.epub-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--epub-text-muted);
  cursor: pointer;
  transition: background var(--epub-transition), color var(--epub-transition), transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
  min-width: 44px;  /* Accessibility: minimum tap target */
  min-height: 44px;
}

.epub-btn svg {
  width: 18px;
  height: 18px;
  pointer-events: none;
}

.epub-btn:hover {
  background: var(--epub-amber-dim);
  color: var(--epub-amber);
}

.epub-btn:active {
  transform: scale(0.9);
}

.epub-btn:focus-visible {
  outline: 2px solid var(--epub-amber);
  outline-offset: 2px;
}

/* Bookmark active state */
.epub-btn-bookmark.active {
  color: var(--epub-amber);
}
.epub-btn-bookmark.active svg {
  fill: var(--epub-amber);
}

/* Fullscreen active */
.epub-btn-fullscreen.active .icon-fullscreen-enter { display: none; }
.epub-btn-fullscreen.active .icon-fullscreen-exit  { display: block !important; }

/* Zoom level label */
.epub-zoom-level {
  font-size: 11px;
  font-weight: 600;
  color: var(--epub-text-muted);
  min-width: 36px;
  text-align: center;
  letter-spacing: 0.02em;
}

/* ============= VIEWPORT ============= */
.epub-viewport {
  height: auto;
  flex: 1 1 auto;
  position: relative;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Tap Zones */
.epub-tap-zone {
  position: absolute;
  top: 0; bottom: 0;
  width: 25%;
  z-index: 10;
  background: transparent;
  border: none;
  cursor: pointer;
  outline: none;
  display: flex;
  align-items: center;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}
.epub-tap-zone:focus-visible {
  background: rgba(255,255,255,0.05);
}

/* Page wrapper — zoom/pan transform applied here */
.epub-page-wrapper {
  height: auto;
  position: relative;
  display: block;
  max-width: 450px;
  margin: 0 auto;
  transform-origin: center center;
  transition: transform 0.15s ease-out;
  will-change: transform;
}

.epub-page-img {
  width: 100%;
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
  object-fit: contain;
  border-radius: 2px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5);
  background: var(--epub-page-bg);
  pointer-events: none;
  -webkit-user-drag: none;
}

/* Page transition overlay (flash effect) */
.epub-page-transition-overlay {
  position: absolute;
  inset: 0;
  background: var(--epub-white);
  opacity: 0;
  pointer-events: none;
  border-radius: 2px;
}

.epub-page-transition-overlay.flash {
  animation: epub-flash 0.25s ease-out;
}

@keyframes epub-flash {
  0%   { opacity: 0.25; }
  100% { opacity: 0; }
}

/* Flip layer (animated page turn effect) */
.epub-flip-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  perspective: 1500px;
}

/* ---- Tap Zones ---- */
.epub-tap-prev { right: 0; justify-content: flex-end; padding-right: 16px; }
.epub-tap-next { left: 0; justify-content: flex-start; padding-left: 16px; }

.epub-tap-arrow {
  font-size: 36px;
  color: var(--epub-amber);
  background: rgba(17, 17, 24, 0.4);
  backdrop-filter: blur(4px);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 300;
  opacity: 0.2;
  transition: opacity 0.2s, background 0.2s;
  line-height: 1;
  padding-bottom: 4px;
}

.easy-epub-reader-wrap:hover .epub-tap-zone { opacity: 1; }
.easy-epub-reader-wrap:hover .epub-tap-arrow { opacity: 0.8; }
.epub-tap-zone:hover .epub-tap-arrow { opacity: 1; background: rgba(17, 17, 24, 0.8); }
.epub-tap-zone:active .epub-tap-arrow { opacity: 0.5; }

/* ---- RTL Support (Reverse reading direction) ---- */
.easy-epub-reader-wrap[dir="rtl"] .epub-tap-prev {
  left: auto;
  right: 0;
  justify-content: flex-end;
  padding-left: 0;
  padding-right: 16px;
}
.easy-epub-reader-wrap[dir="rtl"] .epub-tap-next {
  right: auto;
  left: 0;
  justify-content: flex-start;
  padding-right: 0;
  padding-left: 16px;
}
.easy-epub-reader-wrap[dir="rtl"] .epub-tap-arrow {
  transform: scaleX(-1);
}

/* ============= BOTTOM BAR ============= */
.epub-bottom-bar {
  position: relative;
  z-index: 20;
  bottom: auto;
  margin-top: auto;
  width: 100%;
  flex: 0 0 auto;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  background: rgba(17, 17, 24, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  box-sizing: border-box;
  transition: transform var(--epub-transition), opacity var(--epub-transition);
}

.epub-btn-bottom-next,
.epub-btn-bottom-prev {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.1);
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 600;
  color: var(--epub-text);
  border: none;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
  flex-shrink: 0;
  min-width: max-content;
}
.epub-btn-bottom-next:hover,
.epub-btn-bottom-prev:hover {
  background: rgba(255, 255, 255, 0.2);
}

.epub-bottom-bar.hidden {
  transform: translateY(100%);
  opacity: 0;
  pointer-events: none;
}

.epub-page-counter,
.epub-progress-pct {
  font-size: 12px;
  color: var(--epub-text-muted);
  white-space: nowrap;
  min-width: 60px;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.epub-progress-pct { text-align: right; }

/* Scrubber */
.epub-scrubber-wrap {
  flex: 1;
}

.epub-scrubber {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 4px;
  background: rgba(255,255,255,0.1);
  outline: none;
  cursor: pointer;
  transition: height 0.15s;
}

.epub-scrubber:hover { height: 6px; }

.epub-scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--epub-amber);
  cursor: pointer;
  box-shadow: 0 0 6px var(--epub-amber-glow);
  transition: transform 0.15s;
}

.epub-scrubber::-webkit-slider-thumb:hover {
  transform: scale(1.3);
}

.epub-scrubber::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: none;
  border-radius: 50%;
  background: var(--epub-amber);
  cursor: pointer;
  box-shadow: 0 0 6px var(--epub-amber-glow);
}

/* Scrubber fill (dynamic via JS CSS variable) */
.epub-scrubber {
  background: linear-gradient(
    to right,
    var(--epub-amber) 0%,
    var(--epub-amber) var(--epub-progress, 0%),
    rgba(255,255,255,0.1) var(--epub-progress, 0%),
    rgba(255,255,255,0.1) 100%
  );
}

/* ============= FULLSCREEN ============= */
:fullscreen .easy-epub-reader-wrap,
:-webkit-full-screen .easy-epub-reader-wrap {
  max-width: 100vw;
  width: 100vw;
  height: 100vh;
  max-height: 100vh;
  border-radius: 0;
  margin: 0;
}

:fullscreen .epub-page-img,
:-webkit-full-screen .epub-page-img {
  max-height: calc(100vh - var(--epub-toolbar-h) - var(--epub-bottom-h));
}

/* Auto-hide toolbar in fullscreen (JS adds/removes .fs-idle) */
:fullscreen .epub-toolbar.fs-idle,
:-webkit-full-screen .epub-toolbar.fs-idle {
  transform: translateY(-100%);
  opacity: 0;
}

:fullscreen .epub-bottom-bar.fs-idle,
:-webkit-full-screen .epub-bottom-bar.fs-idle {
  transform: translateY(100%);
  opacity: 0;
}

/* ============= TOAST NOTIFICATION ============= */
.epub-toast {
  position: absolute;
  bottom: calc(var(--epub-bottom-h) + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--epub-surface-2);
  color: var(--epub-text);
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 13px;
  font-weight: 500;
  border: 1px solid var(--epub-border);
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  white-space: nowrap;
  z-index: 50;
}

.epub-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ============= BOOKMARKS PANEL ============= */
.epub-bookmarks-panel {
  position: absolute;
  top: var(--epub-toolbar-h);
  right: 0;
  bottom: var(--epub-bottom-h);
  width: min(300px, 100%);
  background: var(--epub-surface);
  border-left: 1px solid var(--epub-border);
  display: flex;
  flex-direction: column;
  z-index: 30;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
  box-shadow: -8px 0 24px rgba(0,0,0,0.3);
}

.epub-bookmarks-panel:not([hidden]) {
  transform: translateX(0);
}

.epub-bookmarks-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--epub-border);
  flex-shrink: 0;
}

.epub-bookmarks-header h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  color: var(--epub-text);
}

.epub-bookmarks-close {
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
}

.epub-bookmarks-list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 8px 0;
}

.epub-bookmark-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  gap: 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--epub-border);
  transition: background var(--epub-transition);
}

.epub-bookmark-item:hover {
  background: var(--epub-amber-dim);
}

.epub-bookmark-item:last-child { border-bottom: none; }

.epub-bookmark-thumb {
  width: 40px;
  height: 56px;
  object-fit: cover;
  border-radius: 2px;
  flex-shrink: 0;
  background: var(--epub-bg);
}

.epub-bookmark-info {
  flex: 1;
  min-width: 0;
}

.epub-bookmark-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--epub-text);
}

.epub-bookmark-page {
  font-size: 11px;
  color: var(--epub-text-muted);
}

.epub-bookmark-delete {
  background: transparent;
  border: none;
  color: var(--epub-text-muted);
  cursor: pointer;
  font-size: 14px;
  padding: 4px;
  border-radius: 4px;
  flex-shrink: 0;
  transition: color var(--epub-transition);
}

.epub-bookmark-delete:hover { color: #ff6b6b; }

.epub-bookmarks-empty {
  text-align: center;
  color: var(--epub-text-muted);
  font-size: 13px;
  padding: 32px 16px;
  line-height: 1.6;
}

/* Scrollbar styling for bookmarks list */
.epub-bookmarks-list::-webkit-scrollbar { width: 4px; }
.epub-bookmarks-list::-webkit-scrollbar-track { background: transparent; }
.epub-bookmarks-list::-webkit-scrollbar-thumb { background: var(--epub-border); border-radius: 2px; }

/* ============= PAGE SLIDE ANIMATIONS ============= */
@keyframes epub-slide-in-right {
  from { transform: translateX(60px); opacity: 0; }
  to   { transform: translateX(0);   opacity: 1; }
}

@keyframes epub-slide-in-left {
  from { transform: translateX(-60px); opacity: 0; }
  to   { transform: translateX(0);     opacity: 1; }
}

.epub-page-img.slide-in-right {
  animation: epub-slide-in-right 0.25s cubic-bezier(0.4,0,0.2,1);
}

.epub-page-img.slide-in-left {
  animation: epub-slide-in-left 0.25s cubic-bezier(0.4,0,0.2,1);
}

/* ============= ERROR STATE ============= */
.epub-reader-error {
  padding: 16px;
  background: rgba(255,60,60,0.1);
  border-left: 3px solid #ff6b6b;
  color: #ff9999;
  border-radius: 4px;
  font-family: var(--epub-font);
  font-size: 14px;
}

/* ============= RESPONSIVE — MOBILE ============= */
@media (max-width: 480px) {
  .easy-epub-reader-wrap {
    border-radius: 0;
    height: 100svh;
    max-height: none;
    /* Take up full mobile viewport when on a dedicated page */
  }

  .epub-toolbar {
    padding: 0 8px;
  }

  .epub-btn {
    width: 36px;
    height: 36px;
    min-width: 44px;
    min-height: 44px;
  }

  .epub-zoom-level { display: none; }

  .epub-bottom-bar {
    padding: 0 12px;
  }

  .epub-bookmarks-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--epub-border);
  }
}

@media (max-width: 360px) {
  .epub-btn svg { width: 16px; height: 16px; }
}

/* ============= REDUCED MOTION ============= */
@media (prefers-reduced-motion: reduce) {
  .epub-page-img.slide-in-right,
  .epub-page-img.slide-in-left {
    animation: none;
  }
  .epub-loading-spinner {
    animation: none;
    border-top-color: var(--epub-amber);
  }
  * {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
