/* === CSS Variables (Themes) === */
:root {
  --bg: #f5f0e8;
  --bg-card: #fffef9;
  --bg-input: #ede8dd;
  --text: #2c2416;
  --text-secondary: #6b5e4a;
  --accent: #b87333;
  --accent-dark: #8B4513;
  --accent-light: rgba(184, 115, 51, 0.08);
  --accent-glow: rgba(184, 115, 51, 0.15);
  --border: #d9d0c1;
  --shadow: rgba(44, 36, 22, 0.08);
  --nav-bg: #fffef9;
  --radius: 12px;
  --radius-lg: 20px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  --font-size-xs: 15px;
  --font-size-sm: 17px;
  --font-size-base: 21px;
  --font-size-lg: 24px;
  --font-size-xl: 29px;
  --font-size-xxl: 37px;
  --spacing-xs: 4px;
  --spacing-sm: 8px;
  --spacing-md: 16px;
  --spacing-lg: 24px;
  --spacing-xl: 32px;
  --spacing-xxl: 48px;
}

[data-theme="sepia"] {
  --bg: #e8dcc8;
  --bg-card: #f2ead8;
  --bg-input: #ddd2ba;
  --text: #3b2e1a;
  --text-secondary: #6b5535;
  --accent: #8B4513;
  --accent-dark: #5C2E0A;
  --accent-light: rgba(139, 69, 19, 0.08);
  --accent-glow: rgba(139, 69, 19, 0.15);
  --border: #c4b896;
  --shadow: rgba(59, 46, 26, 0.1);
  --nav-bg: #ece4d0;
  --font: "Georgia", "Noto Serif TC", serif;
}

[data-theme="dark"] {
  --bg: #121220;
  --bg-card: #1c1c2e;
  --bg-input: #252540;
  --text: #e0e0e0;
  --text-secondary: #9a9ab0;
  --accent: #e6a756;
  --accent-dark: #d49530;
  --accent-light: rgba(230, 167, 86, 0.12);
  --accent-glow: rgba(230, 167, 86, 0.2);
  --border: #3d3d5c;
  --shadow: rgba(0, 0, 0, 0.4);
  --nav-bg: #1c1c2e;
}

/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: var(--font-size-base);
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  padding-bottom: calc(52px + env(safe-area-inset-bottom, 0px));
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  cursor: pointer;
  border: none;
  background: none;
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

button:active,
a:active,
.nav-item:active {
  transform: scale(0.97);
  transition: transform 0.1s ease;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

textarea:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* === Layout === */
.app-container {
  max-width: 600px;
  margin: 0 auto;
  padding: var(--spacing-md);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow-x: clip;
}

.app-header {
  padding: var(--spacing-lg) 0 var(--spacing-md);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 20;
  backdrop-filter: blur(8px);
}

.app-header h1 {
  font-size: var(--font-size-xl);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.app-header .subtitle {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.app-content {
  flex: 1;
  padding-bottom: 96px;
}

/* Book page: constrain container so chat-area scrolls internally */
.reader-page .app-container {
  height: 100dvh;
  overflow: hidden;
}
.reader-page .app-content {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* === Book Shelf Cards === */
.shelf-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--spacing-md) var(--spacing-md);
}

.book-card-wrapper {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
}

.book-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px var(--shadow);
  overflow: hidden;
  aspect-ratio: 3 / 4;
  position: relative;
}

.book-card-wrapper:active .book-card {
  transform: scale(0.97);
  transition: transform 0.1s ease;
}

.book-card .book-card-link {
  position: absolute;
  inset: 0;
  z-index: 0;
  display: block;
}

.book-card-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.book-card-no-thumb {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  background: var(--bg-input);
  color: var(--text-secondary);
  opacity: 0.5;
}

.book-card.processing {
  cursor: default;
}

.book-card-thumb.dimmed {
  opacity: 0.3;
}

.processing-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
}

.book-card .delete-book-btn {
  position: absolute;
  top: 4px;
  left: 4px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
  font-size: 18px;
  line-height: 28px;
  text-align: center;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.15s;
}

.book-card-wrapper:active .delete-book-btn,
.book-card-wrapper:hover .delete-book-btn {
  opacity: 0.6;
}

.book-card .delete-book-btn:active {
  opacity: 1;
  color: #c0392b;
}

.book-card .format-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 10px;
  background: var(--accent-light);
  color: var(--accent);
  padding: 2px 8px;
  border-radius: 20px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  z-index: 2;
}

.book-card-info {
  padding: 0 var(--spacing-xs);
  min-height: 36px;
}

.book-card-info .book-title {
  font-size: var(--font-size-sm);
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-bottom: 2px;
}

.book-card-info .book-author {
  font-size: 11px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-card-progress {
  padding: 0 var(--spacing-xs);
  min-height: 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.book-card-progress .progress-bar {
  height: 3px;
  background: var(--bg-input);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 2px;
}

.book-card-progress .progress-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.3s ease;
}

.book-card-progress .progress-text {
  font-size: 10px;
  color: var(--text-secondary);
}

/* === Upload Card === */
.upload-card {
  background: var(--bg-card);
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: var(--spacing-lg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 100px;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  gap: var(--spacing-sm);
  margin-bottom: var(--spacing-md);
}

.upload-card:hover, .upload-card:active {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

.upload-card .upload-icon {
  font-size: 32px;
  opacity: 0.6;
}

.upload-card .upload-hint {
  font-size: 11px;
  opacity: 0.5;
}

/* === Book Screen Header === */
.book-header {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) 0 var(--spacing-md);
}

.book-header .back-btn {
  font-size: 20px;
  padding: var(--spacing-sm);
  color: var(--text-secondary);
}

.book-header .book-info {
  flex: 1;
  min-width: 0;
}

.book-header .book-info h2 {
  font-size: var(--font-size-lg);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-header .book-info .chapter-label {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.book-header .direction-btn {
  font-size: 18px;
  padding: var(--spacing-sm);
  color: var(--text-secondary);
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.book-header .theme-btn {
  font-size: 18px;
  padding: var(--spacing-sm);
  color: var(--text-secondary);
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* === Tab Switcher === */
.tab-bar {
  display: flex;
  gap: 0;
  background: var(--bg-input);
  border-radius: var(--radius);
  padding: 3px;
  margin-bottom: var(--spacing-md);
  position: sticky;
  top: 56px;
  z-index: 9;
}

.tab-btn {
  flex: 1;
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: 10px;
  text-align: center;
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--text-secondary);
  transition: all 0.15s ease;
}

.tab-btn.active {
  background: var(--bg-card);
  color: var(--text);
  box-shadow: 0 1px 3px var(--shadow);
}

/* === Chapter Nav === */
.chapter-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-xs) 0 var(--spacing-sm);
  gap: var(--spacing-md);
  position: sticky;
  top: 0;
  z-index: 8;
  background: var(--bg);
}

.chapter-nav select {
  flex: 1;
  min-width: 0;
  max-width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  font-family: inherit;
  font-size: var(--font-size-sm);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%236b5e4a' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
  text-overflow: ellipsis;
}

.whole-book-toggle {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  cursor: pointer;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  white-space: nowrap;
  user-select: none;
}
.whole-book-toggle input[type="checkbox"] {
  width: 36px;
  height: 22px;
  appearance: none;
  background: var(--border);
  border-radius: 11px;
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
}
.whole-book-toggle input[type="checkbox"]::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.whole-book-toggle input[type="checkbox"]:checked {
  background: var(--accent);
}
.whole-book-toggle input[type="checkbox"]:checked::after {
  transform: translateX(14px);
}

/* === Chat Area === */
/* === Chat Tab === */
#tab-chat {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

#tab-pdf {
  flex: 1;
  min-height: 0;
}

#text-reader {
  flex: 1;
  min-height: 0;
}

.chat-area {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding-bottom: var(--spacing-md);
  display: flex;
  flex-direction: column;
  gap: var(--spacing-md);
}

.chat-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--text-secondary);
  padding: var(--spacing-xxl);
  gap: var(--spacing-md);
}

.chat-placeholder .placeholder-icon {
  font-size: 48px;
  margin-bottom: var(--spacing-sm);
}

.chat-placeholder .placeholder-title {
  font-size: var(--font-size-lg);
  font-weight: 600;
  color: var(--text);
}

.chat-placeholder .mode-buttons {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  width: 100%;
  max-width: 280px;
}

.mode-btn {
  padding: var(--spacing-md);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-card);
  text-align: left;
  font-size: var(--font-size-sm);
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.mode-btn:active {
  background: var(--accent-light);
  border-color: var(--accent);
}

.mode-btn .mode-emoji { font-size: 24px; }
.mode-btn .mode-label { font-weight: 600; }
.mode-btn .mode-desc { font-size: 12px; color: var(--text-secondary); }

/* Followup Questions (LLM-generated) */
.followup-questions {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  width: 100%;
  max-width: 85%;
  margin: var(--spacing-sm) 0;
}

.fq-btn {
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-card);
  text-align: left;
  font-size: var(--font-size-sm);
  transition: all 0.15s ease;
  color: var(--text-secondary);
}

.fq-btn:active {
  background: var(--accent-light);
  border-color: var(--accent);
  color: var(--text);
  transform: scale(0.98);
}

.chapter-indicator {
  width: 100%;
  text-align: center;
  padding: var(--spacing-md) 0;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--spacing-md);
  opacity: 0.7;
}

.chat-orphan-note {
  width: 100%;
  max-width: 85%;
  padding: var(--spacing-sm) var(--spacing-md);
  margin: var(--spacing-sm) 0;
  border-radius: var(--radius);
  background: rgba(255, 193, 7, 0.1);
  border: 1px solid rgba(255, 193, 7, 0.3);
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  text-align: center;
}

/* Chat Messages */
.chat-message {
  padding: var(--spacing-md);
  border-radius: var(--radius-lg);
  max-width: 85%;
  font-size: var(--font-size-sm);
  line-height: 1.6;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-message.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: var(--spacing-xs);
}

.chat-message.assistant {
  align-self: flex-start;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: var(--spacing-xs);
  box-shadow: 0 1px 3px var(--shadow);
}

.chat-message.streaming {
  border-left: 2px solid var(--accent);
}

.typing-dots {
  display: inline-flex;
  gap: 3px;
  padding: 4px 0;
}
.typing-dots span {
  width: 5px;
  height: 5px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: dotPulse 1.4s infinite ease-in-out both;
}
.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }
.typing-dots span:nth-child(3) { animation-delay: 0s; }

@keyframes dotPulse {
  0%, 80%, 100% { transform: scale(0.4); opacity: 0.3; }
  40% { transform: scale(1); opacity: 1; }
}

/* Chat Input (pill design) */
.chat-input-area {
  position: fixed;
  bottom: calc(52px + env(safe-area-inset-bottom, 0px));
  left: 0;
  right: 0;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: var(--spacing-sm) var(--spacing-md);
  padding-bottom: max(var(--spacing-md), env(safe-area-inset-bottom));
  z-index: 20;
}

.input-pill {
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 28px;
  padding: 6px 6px 6px 16px;
  transition: border-color 0.15s ease;
}
.input-pill:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.pill-center {
  flex: 1;
  display: flex;
  align-items: center;
  min-height: 36px;
}
.pill-center textarea {
  display: block;
  width: 100%;
  resize: none;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: var(--font-size-base);
  line-height: 1.4;
  max-height: 120px;
  min-height: 24px;
  padding: 0;
  outline: none;
}
.pill-center textarea::placeholder {
  color: var(--text-secondary);
  opacity: 0.6;
}

.pill-voice-hint {
  display: none;
  flex: 1;
  text-align: center;
  font-size: var(--font-size-base);
  color: var(--text-secondary);
  -webkit-touch-callout: none;
  user-select: none;
  cursor: pointer;
  padding: 6px 0;
  border-radius: 20px;
  transition: background 0.15s ease;
}
.pill-voice-hint:active {
  background: var(--accent-light);
}
.pill-voice-hint.recording {
  color: #ef4444;
  animation: micPulse 1.5s ease-in-out infinite;
}
@keyframes micPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  50% { box-shadow: 0 0 0 12px rgba(239, 68, 68, 0); }
}

.pill-clear-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: none;
  border: none;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
  color: var(--text-secondary);
  opacity: 0.5;
  transition: opacity 0.15s ease;
}
.pill-clear-btn:hover,
.pill-clear-btn:active {
  opacity: 1;
}

.pill-mode-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent-light);
  border: none;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
  transition: background 0.15s ease;
  color: var(--accent);
}
.pill-mode-btn:active {
  background: var(--accent);
  color: #fff;
}

.speaker-toggle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: none;
  border: 1px solid var(--border);
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: all 0.15s ease;
}
.speaker-toggle.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.chat-message.assistant {
  position: relative;
}
.speak-btn {
  position: absolute;
  bottom: 4px;
  right: 4px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease;
}
.chat-message.assistant:hover .speak-btn,
.chat-message.assistant .speak-btn.playing {
  opacity: 1;
}
.speak-btn.playing {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.voice-settings {
  padding: var(--spacing-sm) 0;
}
.voice-row {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  margin-bottom: var(--spacing-sm);
}
.voice-row select {
  flex: 1;
  padding: var(--spacing-xs) var(--spacing-sm);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  font-family: inherit;
  font-size: var(--font-size-sm);
}
.voice-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}
.voice-row span {
  min-width: 36px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* === PDF Viewer === */
.pdf-container {
  width: 100%;
  height: calc(100vh - 155px);
  height: calc(100dvh - 155px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
  background: var(--bg-card);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  position: relative;
}

.pdf-flip-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  z-index: 5;
  background: var(--bg-card);
}
.pdf-flip-overlay canvas {
  max-width: 100%;
  max-height: 100%;
}

.pdf-container iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.pdf-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-md);
  padding: var(--spacing-md) 0;
}

.pdf-controls button {
  padding: var(--spacing-sm) var(--spacing-md);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: var(--font-size-sm);
  color: var(--text);
}

.pdf-controls button:active {
  background: var(--accent-light);
}

.pdf-controls .page-indicator {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  min-width: 80px;
  text-align: center;
}

/* === Loading / Processing === */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(245, 240, 232, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  gap: var(--spacing-lg);
}

.loading-cartridge {
  width: 80px;
  height: 56px;
  background: var(--text);
  border-radius: var(--spacing-xs);
  position: relative;
  animation: cartridgeInsert 0.6s ease;
}

.loading-cartridge::after {
  content: '';
  position: absolute;
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 30px;
  height: 6px;
  background: var(--accent);
  border-radius: 0 0 4px 4px;
}

@keyframes cartridgeInsert {
  from { transform: translateY(-60px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.loading-text {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  text-align: center;
  max-width: 280px;
}

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

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

/* === Empty State === */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--spacing-xxl);
  color: var(--text-secondary);
  gap: var(--spacing-md);
}

.empty-state .empty-icon {
  font-size: 48px;
  opacity: 0.4;
}

/* === Toast === */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: var(--spacing-sm) var(--spacing-lg);
  border-radius: var(--radius-lg);
  font-size: var(--font-size-sm);
  font-weight: 600;
  z-index: 200;
  animation: toastIn 0.3s ease;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(16px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* === RTL Support === */
.text-reader-content.rtl {
  direction: rtl;
  text-align: right;
}

/* === Text Reader (EPUB) === */
.reader-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-sm) 0;
  gap: var(--spacing-sm);
}

.reader-controls button {
  padding: var(--spacing-sm) var(--spacing-md);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: var(--font-size-sm);
  color: var(--text);
}

.reader-controls button:active {
  background: var(--accent-light);
}

.reader-controls button:disabled {
  opacity: 0.4;
}

.reader-chapter-label {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  text-align: center;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.text-reader-content {
  padding: var(--spacing-lg);
  max-width: 680px;
  margin: 0 auto;
  font-size: var(--font-size-base);
  line-height: 1.85;
  word-break: break-word;
  max-height: calc(100vh - 200px);
  max-height: calc(100dvh - 200px);
  overflow-y: auto;
}

.text-reader-content p {
  margin-bottom: var(--spacing-md);
  text-indent: 0;
}

/* === Markdown Content === */
.message-text h1, .message-text h2, .message-text h3, .message-text h4 {
  margin: var(--spacing-sm) 0 var(--spacing-xs);
  font-weight: 600;
  line-height: 1.3;
}
.message-text h1 { font-size: 1.2em; }
.message-text h2 { font-size: 1.1em; }
.message-text h3 { font-size: 1em; }
.message-text p { margin-bottom: var(--spacing-sm); }
.message-text ul, .message-text ol {
  margin: var(--spacing-xs) 0 var(--spacing-sm);
  padding-left: var(--spacing-lg);
}
.message-text li { margin-bottom: 2px; }
.message-text blockquote {
  border-left: 3px solid var(--accent);
  margin: var(--spacing-sm) 0;
  padding: var(--spacing-xs) var(--spacing-md);
  color: var(--text-secondary);
  background: var(--accent-light);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.message-text code {
  background: var(--bg-input);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.9em;
  font-family: "SF Mono", "Fira Code", monospace;
}
.message-text pre {
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  padding: var(--spacing-sm);
  overflow-x: auto;
  margin: var(--spacing-sm) 0;
  font-size: 0.85em;
}
.message-text pre code {
  background: none;
  padding: 0;
}
.message-text table {
  width: 100%;
  border-collapse: collapse;
  margin: var(--spacing-sm) 0;
  font-size: 0.9em;
}
.message-text th, .message-text td {
  border: 1px solid var(--border);
  padding: var(--spacing-xs) var(--spacing-sm);
  text-align: left;
}
.message-text th { background: var(--bg-input); font-weight: 600; }
.message-text strong { font-weight: 600; color: var(--text); }
.message-text a { color: var(--accent); text-decoration: underline; }
.message-text hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: var(--spacing-md) 0;
}
.message-text .md-error { color: #c0392b; }

/* === Login Page === */
.login-page {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
}
.login-card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: var(--spacing-xl);
  box-shadow: 0 2px 12px var(--shadow);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.login-card h1 { font-size: var(--font-size-xl); margin-bottom: var(--spacing-lg); }
.form-label {
  display: block;
  text-align: left;
  font-size: var(--font-size-sm);
  font-weight: 600;
  margin-bottom: var(--spacing-xs);
  color: var(--text-secondary);
}
.form-input {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: var(--font-size-base);
  margin-bottom: var(--spacing-md);
}
.form-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
.login-btn {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: var(--font-size-base);
  font-weight: 600;
  cursor: pointer;
  border: none;
  margin-top: var(--spacing-sm);
}
.login-btn:active { opacity: 0.85; }
.login-error {
  margin-top: var(--spacing-md);
  color: #c0392b;
  font-size: var(--font-size-sm);
}

.password-field {
  position: relative;
  width: 100%;
}

.password-field .form-input {
  padding-right: 44px;
  margin-bottom: 0;
}

.password-field .eye-toggle {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 18px;
  padding: 4px 8px;
  background: none;
  border: none;
  cursor: pointer;
  opacity: 0.5;
  transition: opacity 0.15s;
  z-index: 2;
  line-height: 1;
}

.password-field .eye-toggle:active {
  opacity: 1;
}

.link-result-card {
  margin-top: var(--spacing-md);
}

.link-result-url {
  margin-bottom: var(--spacing-sm) !important;
  font-size: 12px !important;
  text-align: center;
}

.link-result-actions {
  display: flex;
  gap: var(--spacing-sm);
}

.link-result-inline {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 260px;
}

.link-result-inline .link-result-url {
  font-size: 11px !important;
  margin-bottom: 0 !important;
}

.link-result-inline .link-result-actions {
  gap: 4px;
}

.link-result-inline .link-result-actions button {
  font-size: 11px !important;
  padding: 2px 10px;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  cursor: pointer;
  font-family: inherit;
}

/* === Admin Page === */
.admin-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--spacing-lg);
  margin-bottom: var(--spacing-md);
}
.admin-section h2 {
  font-size: var(--font-size-base);
  margin-bottom: var(--spacing-md);
}
.admin-form {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}
.admin-form .form-input { margin-bottom: 0; }
.form-check {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  font-size: var(--font-size-sm);
}
.form-msg { margin-top: var(--spacing-sm); font-size: var(--font-size-sm); }
.form-msg.success { color: #27ae60; }
.form-msg.error { color: #c0392b; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--spacing-sm);
}
.stat-card {
  text-align: center;
  background: var(--bg);
  border-radius: var(--radius);
  padding: var(--spacing-md);
}
.stat-num { font-size: var(--font-size-xl); font-weight: 700; color: var(--accent); }
.stat-label { font-size: var(--font-size-sm); color: var(--text-secondary); }

.user-table-wrap { overflow-x: auto; }
.user-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--font-size-sm);
}
.user-table th, .user-table td {
  padding: var(--spacing-sm);
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}
.user-table th { font-weight: 600; color: var(--text-secondary); }
.danger-btn-sm {
  padding: 2px 8px;
  background: #c0392b;
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 11px;
  cursor: pointer;
  border: none;
}

/* === Header Actions === */
.header-actions {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}
.header-user {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}
.header-admin {
  font-size: 18px;
  text-decoration: none;
  opacity: 0.6;
}
.header-logout {
  font-size: 16px;
  text-decoration: none;
  opacity: 0.5;
  padding: var(--spacing-xs);
}
.header-logout:hover { opacity: 1; }

/* === Offline Banner === */
.offline-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #c0392b;
  color: #fff;
  text-align: center;
  padding: var(--spacing-sm);
  font-size: var(--font-size-sm);
  z-index: 200;
}

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

/* === Navbar === */
#top-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--nav-bg);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(12px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
#top-nav .nav-inner {
  max-width: 600px;
  margin: 0 auto;
  display: flex;
  align-items: stretch;
  padding: 0 var(--spacing-sm);
  height: 52px;
}
#top-nav .nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-radius: 0;
  font-size: var(--font-size-xs);
  font-weight: 600;
  color: var(--text-secondary);
  text-decoration: none;
  transition: all 0.15s ease;
  white-space: nowrap;
  position: relative;
}
#top-nav .nav-item:hover {
  color: var(--text);
}
#top-nav .nav-item.active {
  color: var(--accent);
}
#top-nav .nav-item.active::after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: var(--accent);
}
#top-nav .nav-item .nav-icon { font-size: 20px; line-height: 1; }
#top-nav .nav-item .nav-label {
  font-size: var(--font-size-xs);
  line-height: 1;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* === Settings Page === */
.settings-page { padding: var(--spacing-md) 0; }
.settings-section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--spacing-lg);
  margin-bottom: var(--spacing-md);
}
.settings-section h2 {
  font-size: var(--font-size-base);
  font-weight: 700;
  margin-bottom: var(--spacing-md);
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}
.settings-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-sm) 0;
  border-bottom: 1px solid var(--border);
}
.settings-label:last-child { border-bottom: none; }
.settings-label span { font-size: var(--font-size-sm); color: var(--text-secondary); }

.theme-options {
  display: flex;
  gap: var(--spacing-sm);
  margin-bottom: var(--spacing-md);
}
.theme-option {
  flex: 1;
  text-align: center;
  padding: var(--spacing-sm) var(--spacing-md);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  font-size: var(--font-size-sm);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  background: var(--bg);
  color: var(--text-secondary);
}
.theme-option.active {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}
.theme-option .theme-swatch {
  width: 100%;
  height: 24px;
  border-radius: 6px;
  margin-bottom: var(--spacing-xs);
  border: 1px solid var(--border);
}

.font-size-options {
  display: flex;
  gap: var(--spacing-sm);
}
.font-size-option {
  flex: 1;
  padding: var(--spacing-sm);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
  font-size: var(--font-size-sm);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  background: var(--bg);
  color: var(--text-secondary);
}
.font-size-option.active {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}
.font-size-option .size-label { font-size: 11px; margin-top: 2px; color: var(--text-secondary); }

.sc-tc-options {
  display: flex;
  gap: var(--spacing-sm);
}
.sc-tc-option {
  flex: 1;
  padding: var(--spacing-sm);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
  font-size: var(--font-size-sm);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  background: var(--bg);
  color: var(--text-secondary);
}
.sc-tc-option.active {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

.setting-toggle {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-sm) 0;
}
.setting-toggle .toggle-desc { font-size: var(--font-size-sm); color: var(--text-secondary); flex: 1; }

.logout-btn {
  width: 100%;
  padding: var(--spacing-sm);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: #c0392b;
  font-size: var(--font-size-base);
  font-weight: 600;
  cursor: pointer;
}
.logout-btn:hover { background: #fde8e8; }

/* === Admin Charts === */
.charts-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--spacing-md);
  margin-bottom: var(--spacing-md);
}
.chart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--spacing-lg);
}
.chart-card h3 {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  margin-bottom: var(--spacing-md);
}
.chart-card canvas {
  max-height: 250px;
  width: 100% !important;
}
.cost-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--font-size-sm);
}
.cost-table th, .cost-table td {
  padding: var(--spacing-sm);
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.cost-table th { font-weight: 600; color: var(--text-secondary); }
.cost-table .cost-high { color: #c0392b; font-weight: 600; }

/* === Full Screen PDF === */
.pdf-container:fullscreen,
.pdf-container:-webkit-full-screen {
  height: 100vh;
  height: 100dvh;
  border-radius: 0;
  border: none;
  background: #0d0d0d;
  overflow: hidden;
  align-items: center;
}

.pdf-container:fullscreen .fullscreen-exit-btn,
.pdf-container:-webkit-full-screen .fullscreen-exit-btn,
.pdf-container.fake-fullscreen .fullscreen-exit-btn {
  display: flex;
}

.pdf-container.fake-fullscreen {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  z-index: 9999;
  border-radius: 0;
  border: none;
  margin: 0;
  background: var(--bg);
  overflow: hidden;
  align-items: center;
}
.pdf-container.fake-fullscreen .pdf-flip-overlay {
  background: var(--bg);
}
.fullscreen-btn {
  font-size: 18px;
  padding: var(--spacing-sm);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text);
}

.fullscreen-exit-btn {
  display: none;
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 10000;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(0,0,0,0.5);
  color: #fff;
  font-size: 18px;
  border: none;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

#pdf-canvas {
  touch-action: none;
  display: block;
  margin: auto;
}

/* === Version Toast === */
.version-toast {
  position: fixed;
  bottom: env(safe-area-inset-bottom, 8px);
  right: 12px;
  background: rgba(0,0,0,0.75);
  color: #fff;
  padding: 4px 10px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
  z-index: 99999;
  pointer-events: none;
  opacity: 0;
  animation: version-flash 3s ease-out forwards;
}
@keyframes version-flash {
  0% { opacity: 0; transform: translateY(6px); }
  12% { opacity: 1; transform: translateY(0); }
  78% { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-6px); }
}

@media (max-width: 400px) {
  .shelf-grid {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 600px) {
  .app-container {
    max-width: 900px;
  }
  .shelf-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 1024px) {
  .app-container {
    max-width: 1200px;
  }
  .shelf-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* === Dark Mode Overrides === */
[data-theme="dark"] .logout-btn:hover {
  background: rgba(192, 57, 43, 0.15);
}

[data-theme="dark"] .loading-overlay {
  background: rgba(18, 18, 32, 0.92);
}

[data-theme="dark"] .delete-book-btn {
  color: rgba(224, 224, 224, 0.5);
}

[data-theme="dark"] .book-card-wrapper:hover .delete-book-btn {
  opacity: 0.8;
  color: rgba(255, 255, 255, 0.7);
}

[data-theme="dark"] .book-card-no-thumb {
  background: var(--bg-input);
  color: var(--text-secondary);
  opacity: 0.3;
}

[data-theme="dark"] .mode-btn {
  border-color: var(--border);
}

[data-theme="dark"] .fq-btn {
  border-color: var(--border);
}

[data-theme="dark"] .theme-option,
[data-theme="dark"] .font-size-option,
[data-theme="dark"] .sc-tc-option {
  border-color: var(--border);
}

[data-theme="dark"] .tab-bar {
  background: var(--bg-input);
}

[data-theme="dark"] .tab-btn.active {
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
}

[data-theme="dark"] .upload-card {
  border-color: var(--border);
  background: var(--bg-card);
}

/* === Confirm Popup === */
.confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 0.2s ease;
}
.confirm-popup {
  background: var(--bg-card);
  border-radius: 16px;
  padding: var(--spacing-xl);
  max-width: 320px;
  width: 90%;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
  border: 1px solid var(--border);
  text-align: center;
  animation: scaleIn 0.2s ease;
}
.confirm-title {
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--text);
  margin-bottom: var(--spacing-sm);
}
.confirm-body {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  margin-bottom: var(--spacing-lg);
  line-height: 1.5;
}
.confirm-actions {
  display: flex;
  gap: var(--spacing-sm);
  justify-content: center;
}
.confirm-cancel {
  padding: 10px 24px;
  border-radius: 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  font-weight: 600;
  cursor: pointer;
}
.confirm-ok {
  padding: 10px 24px;
  border-radius: 12px;
  background: #ef4444;
  border: none;
  color: #fff;
  font-size: var(--font-size-sm);
  font-weight: 600;
  cursor: pointer;
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}
