:root {
  --bg-primary: #0f0f12;
  --bg-secondary: #17171c;
  --bg-elevated: #202028;
  --bg-glass: rgba(23, 23, 28, 0.88);
  --border-subtle: rgba(255, 255, 255, 0.09);
  --border-focus: #d4af37;
  --text-primary: #f4f3ef;
  --text-secondary: #a6a49e;
  --text-muted: #6e6c66;
  --accent-gold: #d4af37;
  --accent-gold-dim: rgba(212, 175, 55, 0.16);
  --badge-pdf: #e05a47;
  --badge-epub: #3b9eed;
  --danger: #ef5350;
  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --shadow-card: 0 8px 28px rgba(0, 0, 0, 0.42);
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-serif: "Georgia", "Times New Roman", serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ==================== PIN Authentication Screen ==================== */
.auth-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: radial-gradient(circle at 50% 20%, #1f1e29 0%, #0f0f12 70%);
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 2.25rem 1.75rem;
  text-align: center;
  box-shadow: var(--shadow-card);
}

.brand-badge {
  width: 52px;
  height: 52px;
  margin: 0 auto 1rem;
  border-radius: 50%;
  background: var(--accent-gold-dim);
  border: 1px solid var(--accent-gold);
  color: var(--accent-gold);
  font-family: var(--font-serif);
  font-size: 1.25rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  letter-spacing: 0.06em;
}

.auth-title {
  font-family: var(--font-serif);
  font-size: 1.6rem;
  font-weight: 600;
  margin-bottom: 0.4rem;
  letter-spacing: 0.02em;
}

.auth-subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.45;
  margin-bottom: 1.75rem;
}

.pin-dots {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.pin-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--text-secondary);
  background: transparent;
  transition: all 0.15s ease;
}

.pin-dot.filled {
  background: var(--accent-gold);
  border-color: var(--accent-gold);
  transform: scale(1.1);
}

.auth-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-bottom: 1rem;
  min-height: 1.2em;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin-top: 0.5rem;
}

.key-btn {
  height: 56px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 1.25rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.key-btn:hover {
  background: #2b2b36;
  border-color: rgba(255, 255, 255, 0.2);
}

.key-btn:active {
  transform: scale(0.96);
  background: var(--accent-gold-dim);
  border-color: var(--accent-gold);
}

.key-action {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

/* ==================== Main App Shell ==================== */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  height: 64px;
  padding: 0 1.25rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-shrink: 0;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent-gold-dim);
  border: 1px solid var(--accent-gold);
  color: var(--accent-gold);
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-name {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
  display: block;
}

.storage-badge {
  font-size: 0.7rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.topbar-center {
  flex: 1;
  max-width: 440px;
}

.search-input {
  width: 100%;
  height: 38px;
  padding: 0 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 0.9rem;
}

.search-input:focus {
  outline: none;
  border-color: var(--accent-gold);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.format-filters {
  display: flex;
  background: var(--bg-primary);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  padding: 2px;
}

.filter-chip {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.filter-chip.active {
  background: var(--accent-gold);
  color: #111;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-outline {
  height: 36px;
  padding: 0 0.85rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.82rem;
  cursor: pointer;
}

.btn-outline:hover {
  color: var(--text-primary);
  border-color: var(--text-secondary);
}

/* ==================== Workspace & Sidebar ==================== */
.workspace {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

.sidebar {
  width: 260px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

.sidebar-header {
  padding: 1.1rem 1.15rem 0.6rem;
}

.sidebar-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--text-muted);
}

.category-list {
  display: flex;
  flex-direction: column;
  padding: 0.25rem 0.65rem 1.25rem;
  gap: 0.2rem;
}

.category-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}

.category-item:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.category-item.active {
  background: var(--accent-gold-dim);
  color: var(--accent-gold);
  font-weight: 600;
}

.category-count {
  font-size: 0.75rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
}

/* ==================== Catalog Grid ==================== */
.catalog-main {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem 2.5rem;
}

.catalog-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.15rem;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.breadcrumb-bar {
  font-family: var(--font-serif);
  font-size: 1.25rem;
  font-weight: 600;
}

.book-count-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1.25rem;
}

.book-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  cursor: pointer;
  transition: transform 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
}

.book-card:hover {
  transform: translateY(-3px);
  border-color: var(--accent-gold);
  box-shadow: var(--shadow-card);
}

.book-cover {
  height: 260px;
  padding: 0.85rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background: linear-gradient(145deg, #24222f 0%, #15151b 100%);
  border-bottom: 1px solid var(--border-subtle);
  position: relative;
  overflow: hidden;
}

.book-cover[data-format="EPUB"] {
  background: linear-gradient(145deg, #1b2838 0%, #131820 100%);
}

.book-cover-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
  z-index: 1;
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.3s ease;
}

.book-cover.has-thumb .book-cover-img {
  opacity: 1;
}

.book-card:hover .book-cover.has-thumb .book-cover-img {
  transform: scale(1.03);
}

.book-cover.has-thumb .book-spine-title {
  opacity: 0;
}

.book-cover-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 2;
}

.format-badge {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  background: var(--badge-pdf);
  color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
}

.format-badge.epub {
  background: var(--badge-epub);
}

.book-progress-pill {
  font-size: 0.7rem;
  color: var(--accent-gold);
  background: rgba(15, 15, 18, 0.82);
  border: 1px solid var(--accent-gold-dim);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  backdrop-filter: blur(4px);
}

.book-spine-title {
  font-family: var(--font-serif);
  font-size: 1.05rem;
  line-height: 1.35;
  color: var(--text-primary);
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  position: relative;
  z-index: 2;
  transition: opacity 0.2s ease;
}

.book-card-title {
  font-family: var(--font-serif);
  font-size: 0.94rem;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-primary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-info {
  padding: 0.9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
}

.book-category-tag {
  font-size: 0.76rem;
  color: var(--accent-gold);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-meta-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: auto;
}

.empty-state {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--text-secondary);
}

.empty-title {
  font-family: var(--font-serif);
  font-size: 1.2rem;
  margin-bottom: 0.4rem;
  color: var(--text-primary);
}

/* ==================== Reader Overlay ==================== */
.reader-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: #0a0a0c;
  display: flex;
  flex-direction: column;
}

.reader-header {
  min-height: 56px;
  padding: 0.4rem 1rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.reader-header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.reader-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.reader-title {
  font-size: 0.95rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 340px;
}

.reader-controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.btn-reader {
  height: 34px;
  padding: 0 0.7rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 0.82rem;
  cursor: pointer;
}

.btn-reader:hover {
  border-color: var(--accent-gold);
}

.reader-indicator {
  font-size: 0.82rem;
  color: var(--text-secondary);
  min-width: 54px;
  text-align: center;
}

.reader-select {
  height: 34px;
  max-width: 190px;
  padding: 0 0.5rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 0.82rem;
}

.divider {
  width: 1px;
  height: 20px;
  background: var(--border-subtle);
  margin: 0 0.2rem;
}

.reader-body {
  flex: 1;
  position: relative;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.reader-loading {
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 12, 0.86);
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
}

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

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

.pdf-stage {
  width: 100%;
  height: 100%;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  padding: 1rem;
  -webkit-overflow-scrolling: touch;
}

.pdf-canvas-wrapper {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  margin: auto;
  flex-shrink: 0;
}

.pdf-canvas {
  display: block;
  background: #fff;
  box-shadow: 0 6px 28px rgba(0, 0, 0, 0.65);
  flex-shrink: 0;
}

.epub-stage {
  width: 100%;
  height: calc(100dvh - 104px);
  display: flex;
  align-items: stretch;
  justify-content: center;
  background: #f8f5ec;
  overflow: hidden;
}

.epub-viewer {
  width: 100%;
  max-width: 960px;
  height: 100%;
  min-height: 400px;
  background: #f8f5ec;
  color: #1c1b18;
  position: relative;
}

.reader-footer {
  height: 46px;
  padding: 0 1.25rem;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: 1rem;
}

.reader-scrubber {
  flex: 1;
  accent-color: var(--accent-gold);
  cursor: pointer;
}

.reader-footer-status {
  font-size: 0.78rem;
  color: var(--text-secondary);
  min-width: 80px;
  text-align: right;
}

/* ==================== Responsive Mobile Layout ==================== */
.mobile-only {
  display: none;
}

@media (max-width: 768px) {
  .mobile-only {
    display: flex;
  }

  .topbar {
    flex-wrap: wrap;
    height: auto;
    padding: 0.65rem 0.9rem;
  }

  .topbar-center {
    order: 3;
    max-width: 100%;
    flex-basis: 100%;
  }

  .sidebar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow-card);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .books-grid {
    grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
    gap: 0.85rem;
  }

  .book-cover {
    height: 145px;
    padding: 0.9rem;
  }

  .reader-title {
    max-width: 160px;
  }

  .pdf-stage {
    padding: 0.5rem;
  }
}
