:root {
  --bg-primary: #000000;
  --bg-secondary: #0a0a0f;
  --bg-tertiary: #14141f;
  --bg-card: #1a1a2e;
  --text-primary: #ffffff;
  --text-secondary: #a0a0b0;
  --text-muted: #606070;
  --accent-primary: #00d4ff;
  --accent-secondary: #00ff88;
  --accent-warm: #ff9f43;
  --danger: #ff4466;
  --focus-ring: #00d4ff;
  --focus-glow: rgba(0, 212, 255, 0.4);
  --success: #00ff88;
  --warning: #ffaa00;
  --info: #00b4d8;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;

  /* Reader text size — dynamically set via JS. Defaults to Medium. */
  --reader-font-size: 22px;
  --reader-line-height: 1.5;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  width: 600px;
  height: 600px;
  overflow: hidden;
}

#app {
  width: 100%;
  height: 100%;
  position: relative;
}

/* --- Screens --- */
.screen {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: absolute;
  top: 0;
  left: 0;
}
.screen.hidden { display: none; }

/* --- Header --- */
.header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  background: var(--bg-secondary);
  gap: 12px;
  flex-shrink: 0;
}
.header h1 {
  font-size: 22px;
  font-weight: 600;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-meta {
  font-size: 13px;
  color: var(--text-secondary);
}
.back-btn {
  background: transparent;
  color: var(--text-primary);
  font-size: 22px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 2px solid transparent;
  min-width: 44px;
}

/* --- Content Area --- */
.content {
  flex: 1;
  padding: 16px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* --- Focus States --- */
.focusable {
  transition: all 0.15s ease;
  border: 2px solid transparent;
  cursor: pointer;
  min-height: 44px;
}
.focusable:focus {
  outline: none;
  border-color: var(--focus-ring);
  box-shadow: 0 0 20px var(--focus-glow);
}

/* --- Nav Bar (Home) --- */
.nav-bar {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
  background: var(--bg-secondary);
  flex-shrink: 0;
}
.nav-bar-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.nav-item {
  padding: 14px 16px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 15px;
  font-weight: 500;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.nav-item:focus { background: var(--bg-card); }
.nav-item.primary {
  background: var(--accent-primary);
  color: #0a0a0f;
}
.nav-item.primary:focus { background: #33ddff; }

/* --- Help / Controls Card --- */
.help-card {
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.help-title {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.help-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 14px;
  align-items: center;
}
.help-key {
  font-family: 'SF Mono', Menlo, Consolas, monospace;
  font-size: 14px;
  color: var(--accent-primary);
  text-align: center;
  background: var(--bg-card);
  padding: 3px 8px;
  border-radius: var(--radius-sm);
  min-width: 36px;
}
.help-desc {
  font-size: 13px;
  color: var(--text-primary);
}

/* --- Continue Reading Card --- */
.continue-card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.continue-label {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.continue-title {
  font-size: 20px;
  font-weight: 600;
  line-height: 1.3;
}
.continue-author {
  font-size: 14px;
  color: var(--text-secondary);
}

/* --- Tabs --- */
.tab-bar {
  display: flex;
  gap: 4px;
  padding: 6px;
  margin: 10px 12px 0 12px;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  flex-shrink: 0;
  overflow-x: auto;
}
.tab-item {
  flex: 1;
  padding: 10px 8px;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  white-space: nowrap;
}
.tab-item.active {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}
.tab-item:focus {
  background: var(--bg-card);
  color: var(--text-primary);
}

/* --- List Container & Book Items --- */
.list-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.book-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 16px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  text-align: left;
  width: 100%;
}
.book-item:focus { background: var(--bg-card); }
.book-item-title {
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-item-author {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-item-meta {
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  gap: 8px;
}

/* --- Text Input --- */
.text-input {
  width: 100%;
  padding: 14px 16px;
  font-size: 16px;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-radius: var(--radius-md);
  border: 2px solid transparent;
}
.text-input:focus {
  border-color: var(--focus-ring);
  box-shadow: 0 0 20px var(--focus-glow);
  outline: none;
}
.text-input::placeholder { color: var(--text-muted); }

/* --- Book Detail --- */
.detail-meta {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px 0;
}
.detail-author {
  font-size: 16px;
  color: var(--text-secondary);
}
.detail-subjects {
  font-size: 13px;
  color: var(--text-muted);
}
.detail-actions {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 10px;
}
.detail-description {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-secondary);
}

/* --- Reader --- */
#reader {
  background: var(--bg-primary);
}
.reader-toolbar {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  background: var(--bg-secondary);
  gap: 8px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--bg-tertiary);
}
.reader-toolbar-title {
  flex: 1;
  font-size: 14px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reader-toolbar-btn {
  background: transparent;
  color: var(--text-primary);
  font-size: 20px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 2px solid transparent;
  min-width: 40px;
}

/* Paginated reading area: fixed height = N * line-height for clean line breaks */
.reader-page {
  flex: 1;
  padding: 16px 22px;
  overflow: hidden;
  position: relative;
}
.reader-page-inner {
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  color: var(--text-primary);
  white-space: pre-wrap;
  word-wrap: break-word;
  hyphens: auto;
}
/* Paragraph spacing MUST be a whole line so page breaks stay on the line grid */
.reader-page-inner p {
  margin: 0 0 1lh 0;
}
.reader-page-inner p:last-child { margin-bottom: 0; }

.reader-footer {
  padding: 8px 16px;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  background: var(--bg-secondary);
  flex-shrink: 0;
}

/* Reader menu overlay */
.reader-menu {
  position: absolute;
  bottom: 50px;
  left: 20px;
  right: 20px;
  background: var(--bg-card);
  border: 1px solid var(--bg-tertiary);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  z-index: 20;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
}
.reader-menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.reader-menu-label {
  font-size: 14px;
  color: var(--text-secondary);
}
.reader-menu-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}
.reader-menu-btn {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  font-size: 16px;
  font-weight: 600;
  min-width: 48px;
  min-height: 40px;
}
.reader-menu-btn:focus {
  background: var(--bg-card);
  border-color: var(--focus-ring);
  box-shadow: 0 0 20px var(--focus-glow);
}
.reader-menu-value {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  min-width: 40px;
  text-align: center;
}

/* --- Settings --- */
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
}
.settings-label {
  font-size: 15px;
  color: var(--text-primary);
}
.settings-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}
.settings-meta {
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.settings-meta-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.settings-meta-value {
  font-size: 13px;
  color: var(--text-secondary);
  font-family: 'SF Mono', Menlo, Consolas, monospace;
  word-break: break-all;
}

/* --- Load More --- */
.load-more-area {
  margin-top: 4px;
  min-height: 1px;
}
.load-more-area:empty { display: none; }
.load-more-area .nav-item {
  width: 100%;
  margin-top: 4px;
}

/* --- Loading / Error placeholders --- */
/* Every "Loading…" string gets an inline rotating ring before it. Pure CSS
   animation so the motion keeps going even if the JS thread is busy — that's
   the whole point: visible proof the app isn't frozen. */
.loading-row {
  padding: 20px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}
.loading-row::before {
  content: '';
  width: 18px;
  height: 18px;
  border: 2px solid var(--bg-tertiary);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.empty-row {
  padding: 30px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}
.error-row {
  padding: 20px;
  text-align: center;
  color: var(--danger);
  font-size: 14px;
}

/* --- Progress Bar --- */
.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--accent-primary);
  border-radius: 3px;
  transition: width 0.3s ease;
  width: 0%;
}

/* --- Toast --- */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(120px);
  background: var(--bg-card);
  color: var(--text-primary);
  padding: 12px 24px;
  border-radius: var(--radius-md);
  font-size: 14px;
  border: 1px solid var(--accent-primary);
  transition: transform 0.3s ease;
  z-index: 100;
  pointer-events: none;
  max-width: 80%;
  text-align: center;
}
.toast.visible { transform: translateX(-50%) translateY(0); }
.toast.error { border-color: var(--danger); }
.toast.success { border-color: var(--success); }

/* --- Utility --- */
.hidden { display: none !important; }
