:root {
  color-scheme: dark;
  --bg: #1a2332;
  --panel: #243044;
  --text: #e8eef7;
  --muted: #9aa8bc;
  --accent: #40916c;
  --accent-soft: #2d6a4f;
  --warn: #f4a261;
  --error: #e76f51;
  --border: #334155;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 1rem 1rem 2.5rem;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.topbar h1 {
  margin: 0;
  font-size: 1.25rem;
  flex: 1;
}

.title-sm {
  font-size: 1rem !important;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
}

.input {
  width: 100%;
  margin: 0.75rem 0;
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #111827;
  color: var(--text);
}

.btn {
  border: 1px solid var(--border);
  background: #111827;
  color: var(--text);
  border-radius: 999px;
  padding: 0.55rem 1rem;
  font-size: 0.95rem;
  cursor: pointer;
}

.btn.primary {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.btn.ghost {
  background: transparent;
}

.btn.small {
  font-size: 0.85rem;
  padding: 0.35rem 0.75rem;
}

.btn.danger {
  border-color: var(--error);
  color: var(--error);
}

.upload-btn {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.upload-btn.disabled {
  opacity: 0.6;
  pointer-events: none;
  cursor: not-allowed;
}

.upload-status {
  margin-bottom: 0.75rem;
}

.upload-status .status {
  margin: 0 0 0.35rem;
}

.progress-bar {
  height: 4px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.15s ease;
}

.book-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.book-item {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
}

.book-open {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  align-items: center;
  background: none;
  border: none;
  color: inherit;
  padding: 0;
  text-align: left;
  cursor: pointer;
}

.book-title { font-weight: 600; }

.book-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.badge {
  font-size: 0.75rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: #111827;
  white-space: nowrap;
}

.status-ready { color: #95d5b2; }
.status-processing, .status-queued { color: #90e0ef; }
.status-needs_ocr, .status-failed { color: var(--warn); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.status { color: #90e0ef; }
.error { color: var(--error); }
.warn {
  background: rgba(244, 162, 97, 0.12);
  border: 1px solid rgba(244, 162, 97, 0.35);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
}

.reader-body h1, .reader-body h2, .reader-body h3 {
  margin-top: 1.25rem;
}

.reader-body p {
  margin: 0.75rem 0;
}

.reader-img {
  max-width: 100%;
  border-radius: 8px;
  border: 1px solid var(--border);
  margin: 0.75rem 0;
}

.reader-img-error {
  min-height: 3rem;
  background: var(--surface);
}

.pager {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

.reader-mode-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.75rem;
  margin-bottom: 1rem;
}

.mode-badge {
  font-size: 0.8rem;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  background: #111827;
  border: 1px solid var(--border);
  color: #95d5b2;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mode-toggle {
  display: flex;
  gap: 0.35rem;
}

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

.chapters-empty {
  text-align: center;
}

.chapters-empty p {
  margin-top: 0;
}

.chapters-layout {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
}

.toc-sidebar {
  flex: 0 0 11rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem;
  max-height: 70vh;
  overflow-y: auto;
}

.toc-heading {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  padding: 0.4rem 0.35rem;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  line-height: 1.35;
}

.toc-item:hover {
  background: rgba(255, 255, 255, 0.05);
}

.toc-item.active {
  background: var(--accent-soft);
  color: #fff;
}

.toc-item.done {
  color: var(--ok, #6bcf7f);
}

.section-progress {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0.75rem 0 1rem;
  font-size: 0.9rem;
  cursor: pointer;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.5rem 0 1rem;
}

.card-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.anki-card {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.card-meta {
  margin: 0;
}

.anki-field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.anki-field-label {
  font-size: 0.7rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted, #9aa3b2);
}

.anki-field-input {
  width: 100%;
  box-sizing: border-box;
  resize: vertical;
  min-height: 2.5rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.25);
  color: var(--text, #e8ecf4);
  font: inherit;
  line-height: 1.4;
}

.anki-field-input:focus {
  outline: 2px solid var(--accent, #6ea8fe);
  border-color: transparent;
}

.anki-field:first-of-type .anki-field-input {
  font-weight: 600;
}

.quote-ok { color: var(--ok, #6bcf7f); }
.quote-bad { color: var(--warn, #e6b84d); }

.chapters-main {
  flex: 1;
  min-width: 0;
}

.toc-toggle {
  margin-bottom: 0.75rem;
}

@media (max-width: 480px) {
  #app { padding: 0.75rem; }
  .topbar { flex-wrap: wrap; }

  .toc-sidebar {
    display: none;
    position: fixed;
    inset: 0 30% 0 0;
    z-index: 20;
    max-height: none;
    border-radius: 0;
  }

  .toc-sidebar.open {
    display: block;
  }

  .chapters-layout {
    flex-direction: column;
  }
}
