/* ==========================================================================
   KINDLE E-INK COMPONENTS
   Header, Thư viện, Trình đọc thông minh, Modal Đăng nhập, Aa Controls, TOC Drawer
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. APP BAR / HEADER
   -------------------------------------------------------------------------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
  padding: 0 var(--space-4);
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: transform var(--transition-normal), background-color var(--transition-normal);
}

.app-header.hidden {
  transform: translateY(-100%);
}

.header-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: var(--text-lg);
  letter-spacing: -0.01em;
  color: var(--text-ink);
}

.brand-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--accent-contrast);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  flex-shrink: 0;
}

.brand-icon svg {
  width: 100%;
  height: 100%;
  display: block;
}

.brand-subtitle {
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  display: block;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  color: var(--text-ink);
  border: 1px solid transparent;
  transition: all var(--transition-fast);
}

.btn-icon:hover {
  background-color: var(--border-subtle);
}

.btn-icon:active {
  transform: scale(0.95);
}

.user-badge {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 4px 12px 4px 6px;
  background-color: var(--bg-primary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-secondary);
}

.user-avatar {
  width: 26px;
  height: 26px;
  border-radius: var(--radius-full);
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: var(--text-xs);
}

/* --------------------------------------------------------------------------
   2. LIBRARY / CATALOG VIEW
   -------------------------------------------------------------------------- */
.library-container {
  max-width: 1080px;
  margin: 0 auto;
  padding: var(--space-6) var(--space-4) var(--space-12);
}

.library-hero {
  margin-bottom: var(--space-6);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
}

.hero-title {
  font-family: var(--font-serif);
  font-size: clamp(1.5rem, 3.5vw, 2.25rem);
  font-weight: 700;
  line-height: 1.25;
  color: var(--text-ink);
  margin-bottom: var(--space-2);
}

.hero-description {
  font-size: var(--text-base);
  color: var(--text-secondary);
  max-width: 680px;
  line-height: 1.6;
}

.library-toolbar {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin-bottom: var(--space-6);
}

@media (min-width: 640px) {
  .library-toolbar {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 420px;
}

.search-input {
  width: 100%;
  padding: 10px 14px 10px 40px;
  background-color: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  color: var(--text-ink);
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.category-tags {
  display: flex;
  gap: var(--space-2);
  overflow-x: auto;
  padding-bottom: 4px;
  -webkit-overflow-scrolling: touch;
}

.tag-btn {
  padding: 6px 14px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
  border: 1px solid var(--border-subtle);
  background-color: var(--bg-card);
  color: var(--text-secondary);
  white-space: nowrap;
}

.tag-btn.active, .tag-btn:hover {
  background-color: var(--text-ink);
  color: var(--bg-primary);
  border-color: var(--text-ink);
}

/* Document Cards */
.document-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-5);
}

@media (min-width: 768px) {
  .document-grid {
    grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  }
}

.doc-card {
  display: flex;
  flex-direction: column;
  background-color: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-paper);
  transition: transform var(--transition-fast), box-shadow var(--transition-fast), border-color var(--transition-fast);
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

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

/* Subtle e-ink spine bar on the left */
.doc-card::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background-color: var(--accent);
  opacity: 0.6;
}

.doc-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.doc-badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  font-weight: 600;
  background-color: var(--badge-bg);
  color: var(--badge-text);
  letter-spacing: 0.03em;
}

.doc-meta-info {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.doc-title {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.4;
  color: var(--text-ink);
  margin-bottom: var(--space-2);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.doc-desc {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: var(--space-4);
  flex-grow: 1;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.doc-highlights {
  list-style: none;
  margin-bottom: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.doc-highlight-item {
  font-size: var(--text-xs);
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.doc-highlight-item::before {
  content: '•';
  color: var(--accent);
  font-size: 1.1em;
}

.doc-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: var(--space-3);
  border-top: 1px solid var(--border-subtle);
  font-size: var(--text-xs);
}

.read-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 7px 14px;
  background-color: var(--text-ink);
  color: var(--bg-primary);
  border-radius: var(--radius-full);
  font-weight: 600;
  font-size: var(--text-xs);
  transition: opacity var(--transition-fast);
}

.read-btn:hover {
  opacity: 0.9;
}

/* --------------------------------------------------------------------------
   3. SMART READER VIEW
   -------------------------------------------------------------------------- */
.reader-wrapper {
  position: fixed;
  inset: 0;
  z-index: 200;
  background-color: var(--bg-primary);
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-normal);
}

.reader-wrapper.active {
  opacity: 1;
  pointer-events: auto;
}

.reader-header {
  height: 52px;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  z-index: 10;
  transition: transform var(--transition-normal);
}

.reader-header.hidden {
  transform: translateY(-100%);
}

.reader-title-area {
  flex: 1;
  min-width: 0;
  padding: 0 var(--space-3);
  text-align: center;
}

.reader-doc-title {
  font-family: var(--font-serif);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.reader-content-frame {
  flex: 1;
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.reader-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background-color: var(--bg-primary);
}

/* Reading Progress Bar */
.reading-progress-track {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--progress-track);
  z-index: 30;
}

.reading-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--progress-fill);
  transition: width 120ms ease;
}

/* Floating Kindle Controls Dock */
.floating-dock {
  position: fixed;
  bottom: var(--space-5);
  left: 50%;
  transform: translateX(-50%) translateY(0);
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-full);
  padding: 6px 12px;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  box-shadow: var(--shadow-float);
  z-index: 250;
  transition: transform var(--transition-normal), opacity var(--transition-normal);
}

.floating-dock.hidden {
  transform: translateX(-50%) translateY(140%);
  opacity: 0;
  pointer-events: none;
}

.dock-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-ink);
}

.dock-btn:hover {
  background: var(--border-subtle);
}

.dock-divider {
  width: 1px;
  height: 18px;
  background: var(--border-subtle);
}

/* --------------------------------------------------------------------------
   4. Aa APPEARANCE SETTINGS PANEL
   -------------------------------------------------------------------------- */
.settings-popover {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) scale(0.95);
  width: 90%;
  max-width: 380px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-modal);
  z-index: 300;
  opacity: 0;
  pointer-events: none;
  transition: transform var(--transition-fast), opacity var(--transition-fast);
}

.settings-popover.active {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) scale(1);
}

.popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-4);
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: var(--text-base);
}

.setting-row {
  margin-bottom: var(--space-4);
}

.setting-row:last-child {
  margin-bottom: 0;
}

.setting-label {
  font-size: var(--text-xs);
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: var(--space-2);
  display: block;
}

/* Theme Switcher in Aa */
.theme-options {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-2);
}

.theme-pill {
  height: 42px;
  border-radius: var(--radius-md);
  border: 2px solid transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-paper);
}

.theme-pill.theme-warm { background: #FAF6ED; color: #242628; border-color: rgba(45, 38, 25, 0.15); }
.theme-pill.theme-sepia { background: #F5EBD4; color: #382A1B; border-color: rgba(70, 48, 20, 0.2); }
.theme-pill.theme-chalk { background: #FFFFFF; color: #1C1E21; border-color: rgba(0, 0, 0, 0.15); }
.theme-pill.theme-night { background: #151618; color: #E4E1D8; border-color: rgba(255, 255, 255, 0.2); }

.theme-pill.selected {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Font Selection */
.btn-group {
  display: flex;
  background: var(--bg-primary);
  padding: 3px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
}

.btn-group-option {
  flex: 1;
  padding: 6px 12px;
  font-size: var(--text-xs);
  font-weight: 500;
  border-radius: var(--radius-sm);
  text-align: center;
}

.btn-group-option.active {
  background: var(--bg-card);
  color: var(--text-ink);
  font-weight: 600;
  box-shadow: var(--shadow-paper);
}

/* Size Slider Controls */
.slider-control {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.slider-input {
  flex: 1;
  accent-color: var(--accent);
}

/* --------------------------------------------------------------------------
   5. TOC (TABLE OF CONTENTS) DRAWER
   -------------------------------------------------------------------------- */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 350;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-normal);
}

.drawer-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

.toc-drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: 88%;
  max-width: 380px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  box-shadow: var(--shadow-modal);
  z-index: 400;
  transform: translateX(-100%);
  transition: transform var(--transition-normal);
  display: flex;
  flex-direction: column;
}

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

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

.drawer-title {
  font-family: var(--font-serif);
  font-size: var(--text-lg);
  font-weight: 700;
}

.toc-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-2) 0;
  list-style: none;
}

.toc-item {
  padding: 10px var(--space-4);
  font-size: var(--text-sm);
  color: var(--text-secondary);
  border-left: 3px solid transparent;
  cursor: pointer;
  line-height: 1.4;
  transition: all var(--transition-fast);
}

.toc-item:hover {
  background: var(--border-subtle);
  color: var(--text-ink);
}

.toc-item.active {
  border-left-color: var(--accent);
  background: var(--accent-soft);
  color: var(--text-ink);
  font-weight: 600;
}

.toc-item.level-2 {
  padding-left: calc(var(--space-4) + 14px);
  font-size: var(--text-xs);
}

.toc-item.level-3 {
  padding-left: calc(var(--space-4) + 26px);
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* --------------------------------------------------------------------------
   6. FULLSCREEN LOGIN GATEKEEPER & AUTH
   -------------------------------------------------------------------------- */
.login-gate-screen {
  position: fixed;
  inset: 0;
  background-color: var(--bg-primary);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  overflow-y: auto;
}

.login-gate-screen.hidden {
  display: none !important;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: var(--space-8) var(--space-6);
  box-shadow: var(--shadow-modal);
  position: relative;
  overflow: hidden;
}

/* Subtle E-ink spine accent */
.auth-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--accent);
}

.auth-header {
  text-align: center;
  margin-bottom: var(--space-6);
}

.auth-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 54px;
  height: 54px;
  border-radius: var(--radius-md);
  background: transparent;
  margin-bottom: var(--space-3);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.auth-logo svg {
  width: 100%;
  height: 100%;
  display: block;
}

.auth-title {
  font-family: var(--font-serif);
  font-size: var(--text-2xl);
  font-weight: 700;
  color: var(--text-ink);
  margin-bottom: var(--space-1);
}

.auth-subtitle {
  font-size: var(--text-xs);
  color: var(--text-secondary);
}

.form-group {
  margin-bottom: var(--space-4);
}

.form-label {
  display: block;
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  color: var(--text-ink);
  outline: none;
  transition: all var(--transition-fast);
}

.form-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.auth-submit-btn {
  width: 100%;
  padding: 13px;
  background: var(--text-ink);
  color: var(--bg-primary);
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: var(--text-sm);
  margin-top: var(--space-3);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: var(--shadow-paper);
  cursor: pointer;
}

.auth-submit-btn:hover {
  opacity: 0.92;
}

.auth-submit-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.auth-status-box {
  margin-top: var(--space-4);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-size: var(--text-xs);
  display: none;
}

.auth-status-box.error {
  display: block;
  background: #FEE2E2;
  color: #991B1B;
  border: 1px solid #FCA5A5;
}

.auth-status-box.success {
  display: block;
  background: #ECFDF5;
  color: #065F46;
  border: 1px solid #A7F3D0;
}

/* --------------------------------------------------------------------------
   7. TOPICS BOOKSHELF & NAVIGATION
   -------------------------------------------------------------------------- */
.topics-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-4);
  margin-bottom: var(--space-8);
}

@media (min-width: 640px) {
  .topics-grid {
    grid-template-columns: repeat(auto-fit, minmax(310px, 1fr));
  }
}

.topic-card {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-paper);
  cursor: pointer;
  position: relative;
  transition: all var(--transition-fast);
  text-align: left;
}

.topic-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
  box-shadow: var(--shadow-float);
}

.topic-card.active {
  border-color: var(--accent);
  background: var(--bg-surface);
}

.topic-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.topic-icon {
  font-size: 1.75rem;
  line-height: 1;
}

.topic-count-badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: var(--radius-full);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}

.topic-title {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-ink);
  margin-bottom: var(--space-2);
  line-height: 1.35;
}

.topic-desc {
  font-size: var(--text-xs);
  color: var(--text-secondary);
  line-height: 1.6;
  flex: 1;
  margin-bottom: var(--space-3);
}

.topic-action-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--accent);
}

/* Breadcrumb bar for topic view */
.breadcrumb-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  margin-bottom: var(--space-5);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
}

.breadcrumb-back-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border-subtle);
  background: var(--bg-card);
}

.breadcrumb-back-btn:hover {
  background: var(--text-ink);
  color: var(--bg-primary);
  border-color: var(--text-ink);
}

.current-topic-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-ink);
}

/* Spinner */
.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}

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