:root {
  --bg: #f6f2e8;
  --surface: #fbf9f3;
  --surface-elev: #ffffff;
  --text: #221f1a;
  --text-muted: #6d665b;
  --border: rgba(34, 31, 26, 0.11);
  --accent: #c86a17;
  --accent-soft: rgba(200, 106, 23, 0.16);
  --sent-bg: rgba(224, 152, 46, 0.22);
  --sent-soft: rgba(224, 152, 46, 0.11);
  --word-bg: rgba(217, 119, 6, 0.34);
  --word-text: #1c140c;
  --shadow: 0 12px 32px rgba(28, 23, 16, 0.10);
  --font-reading: Georgia, "Times New Roman", serif;
  --font-size: 20px;
  --line-height: 1.75;
}

body[data-theme="light"] {
  --bg: #ffffff;
  --surface: #f7f7f8;
  --surface-elev: #ffffff;
  --text: #18181b;
  --text-muted: #666870;
  --border: rgba(24, 24, 27, 0.10);
  --sent-bg: rgba(245, 166, 35, 0.22);
  --sent-soft: rgba(245, 166, 35, 0.10);
  --word-bg: rgba(234, 136, 15, 0.34);
  --word-text: #111;
}

body[data-theme="dark"] {
  --bg: #151412;
  --surface: #1e1c19;
  --surface-elev: #26231f;
  --text: #e7e1d5;
  --text-muted: #9b9387;
  --border: rgba(255, 245, 230, 0.12);
  --accent: #e5933a;
  --accent-soft: rgba(229, 147, 58, 0.2);
  --sent-bg: rgba(229, 147, 58, 0.24);
  --sent-soft: rgba(229, 147, 58, 0.12);
  --word-bg: rgba(245, 158, 11, 0.42);
  --word-text: #fff7ed;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

.view {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

/* ============ BUTTONS ============ */
button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: none;
  background: none;
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  padding: 10px 16px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 14px;
  transition: opacity 0.15s, transform 0.1s;
}
.primary-btn:active { transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.primary-btn.large { padding: 13px 24px; font-size: 15px; }
.primary-btn.full-width, .secondary-btn.full-width { width: 100%; }

.secondary-btn {
  background: var(--surface-elev);
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 12px;
  font-weight: 500;
  font-size: 14px;
}

.ghost-btn {
  padding: 9px 14px;
  border-radius: 10px;
  color: var(--text-muted);
  font-size: 14px;
  border: 1px solid var(--border);
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 13px;
  border-radius: 999px;
  background: var(--surface-elev);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 500;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9ca3af;
}
.dot.online { background: #16a34a; }

/* ============ SHELF VIEW ============ */
.shelf-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) 20px 14px;
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand-icon { width: 38px; height: 38px; border-radius: 10px; }
.brand h1 { margin: 0; font-size: 19px; letter-spacing: -0.02em; }
.subtitle { margin: 2px 0 0; font-size: 12px; color: var(--text-muted); }

.shelf-actions { display: flex; align-items: center; gap: 10px; }

.shelf-main {
  flex: 1;
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 20px 20px calc(32px + env(safe-area-inset-bottom));
}

.info-banner {
  margin-bottom: 16px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--accent-soft);
  font-size: 13px;
  line-height: 1.5;
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.book-card {
  display: flex;
  gap: 14px;
  padding: 14px;
  border-radius: 16px;
  background: var(--surface-elev);
  border: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.03);
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
}
.book-card:hover { transform: translateY(-2px); border-color: var(--accent); }

.book-cover {
  width: 72px;
  height: 104px;
  border-radius: 8px;
  object-fit: cover;
  background: linear-gradient(135deg, #3b3026, #69503b);
  color: #f6f2e8;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 24px;
  flex-shrink: 0;
  overflow: hidden;
}
.book-cover img { width: 100%; height: 100%; object-fit: cover; }

.book-card-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.book-title {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-author {
  margin: 3px 0 0;
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-meta-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.meta-tag {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.meta-tag.good {
  background: rgba(22, 163, 74, 0.12);
  border-color: rgba(22, 163, 74, 0.25);
  color: #15803d;
}
body[data-theme="dark"] .meta-tag.good { color: #86efac; }

.book-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-muted);
}
.book-del-btn {
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-muted);
}
.book-del-btn:hover { background: rgba(220, 38, 38, 0.12); color: #dc2626; }

.empty-state {
  text-align: center;
  padding: 64px 20px;
  max-width: 420px;
  margin: 0 auto;
}
.empty-icon { font-size: 46px; margin-bottom: 10px; }
.empty-state h2 { margin: 0 0 8px; font-size: 20px; }
.empty-state p { margin: 0 0 22px; color: var(--text-muted); font-size: 14px; line-height: 1.6; }

/* ============ READER VIEW ============ */
#reader-view {
  height: 100dvh;
  overflow: hidden;
}

.reader-topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 10px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}
.icon-btn:hover { background: var(--accent-soft); }
.icon-btn svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chapter-picker-wrap {
  flex: 1;
  min-width: 0;
}
#chapter-select {
  width: 100%;
  max-width: 320px;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-elev);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  text-overflow: ellipsis;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.segmented {
  display: inline-flex;
  padding: 2px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.seg-btn {
  padding: 5px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}
.seg-btn.active {
  background: var(--accent);
  color: #fff;
}

.badge-text { font-size: 12px; }

.relay-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 16px;
  background: var(--accent-soft);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.relay-actions { display: flex; gap: 8px; flex-shrink: 0; }
.small-primary-btn {
  padding: 5px 11px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
}
.small-ghost-btn {
  padding: 5px 9px;
  border-radius: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

/* Reading Canvas */
.reading-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  padding: 24px 20px 48px;
}

.reading-column {
  max-width: 680px;
  margin: 0 auto;
}

.chapter-status-pill {
  margin-bottom: 18px;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--surface-elev);
  border: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.prose {
  font-family: var(--font-reading);
  font-size: var(--font-size);
  line-height: var(--line-height);
  color: var(--text);
  overflow-wrap: anywhere;
}

.prose p {
  margin: 0 0 1.05em;
  text-align: left;
}

.prose h1, .prose h2, .prose h3 {
  font-family: inherit;
  margin: 0.4em 0 0.8em;
  line-height: 1.3;
  font-weight: 700;
}

.prose .epub-figure {
  margin: 1em auto 1.4em;
  text-align: center;
}
.prose .epub-figure img {
  max-width: 100%;
  max-height: 46vh;
  border-radius: 8px;
}

.prose .s {
  border-radius: 5px;
  padding: 1px 0;
  transition: background-color 0.14s ease;
}
.prose .s.syncable { cursor: pointer; }
.prose .s.syncable:hover { background: var(--sent-soft); }

/* Sentence-tracking mode */
.prose[data-sync-mode="sentence"] .s.active-sentence {
  background: var(--sent-bg);
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

/* Word-tracking mode: subtle sentence context + crisp active word */
.prose[data-sync-mode="word"] .s.active-sentence {
  background: var(--sent-soft);
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.prose .w {
  border-radius: 4px;
  padding: 1px 1px;
  transition: background-color 0.08s ease;
}
.prose .w.syncable { cursor: pointer; }
.prose[data-sync-mode="word"] .w.active-word {
  background: var(--word-bg);
  color: var(--word-text);
}

.chapter-footer-nav {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.return-pill {
  position: fixed;
  right: 18px;
  bottom: calc(122px + env(safe-area-inset-bottom));
  z-index: 25;
  padding: 9px 15px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow);
}

/* Player Dock */
.player-dock {
  flex-shrink: 0;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(18px);
  z-index: 30;
}

.scrub-row {
  max-width: 680px;
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.time-label {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  min-width: 44px;
}
.time-label:last-child { text-align: right; }

.scrub-bar {
  flex: 1;
  accent-color: var(--accent);
  height: 4px;
  cursor: pointer;
}

.controls-row {
  max-width: 680px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.rate-btn {
  min-width: 52px;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  border: 1px solid var(--border);
  background: var(--surface-elev);
}

.transport-group {
  display: flex;
  align-items: center;
  gap: 16px;
}

.ctrl-btn {
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}
.ctrl-btn:hover { background: var(--accent-soft); }

.play-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(200, 106, 23, 0.32);
}
.play-btn svg { width: 22px; height: 22px; fill: currentColor; }

.chapter-hint {
  min-width: 52px;
  text-align: right;
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* ============ POPOVERS & MODALS ============ */
.popover-panel {
  position: fixed;
  top: calc(56px + env(safe-area-inset-top));
  right: 12px;
  width: min(340px, calc(100vw - 24px));
  padding: 16px;
  border-radius: 16px;
  background: var(--surface-elev);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 45;
}

.pop-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
  font-size: 13px;
}
.pop-row:last-child { margin-bottom: 0; }
.pop-row select, .pop-row input[type="range"] { width: 100%; }

.theme-swatches { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.swatch {
  padding: 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 500;
}
.swatch-light { background: #fff; color: #18181b; }
.swatch-paper { background: #f6f2e8; color: #221f1a; }
.swatch-dark { background: #1e1c19; color: #e7e1d5; }
.swatch.active { outline: 2px solid var(--accent); }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.panel-head h2, .panel-head h3 { margin: 0; font-size: 17px; }
.close-x { font-size: 24px; line-height: 1; padding: 2px 6px; color: var(--text-muted); }

.match-actions-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0 8px; }
.match-actions-row { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.muted-note { margin: 0 0 12px; font-size: 13px; color: var(--text-muted); line-height: 1.5; }
.tiny-hint { margin: 0; font-size: 12px; color: var(--text-muted); line-height: 1.45; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(15, 13, 10, 0.56);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-card {
  width: min(460px, 100%);
  max-height: 90dvh;
  overflow-y: auto;
  padding: 20px;
  border-radius: 20px;
  background: var(--surface-elev);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 4px;
  border-radius: 12px;
  background: var(--surface);
  margin-bottom: 14px;
}
.tab-btn {
  padding: 8px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}
.tab-btn.active { background: var(--surface-elev); color: var(--text); box-shadow: 0 1px 4px rgba(0,0,0,0.08); }

.import-form { display: flex; flex-direction: column; gap: 12px; }

.file-drop {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px;
  border-radius: 14px;
  border: 1.5px dashed var(--border);
  background: var(--surface);
  cursor: pointer;
}
.file-drop:hover { border-color: var(--accent); }
.file-drop input { display: none; }
.file-drop-title { font-size: 14px; font-weight: 600; }
.file-drop-name { font-size: 12px; color: var(--text-muted); word-break: break-all; }

.field-label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; }
.field-label input {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
}
.modal-btn-row { display: flex; gap: 10px; }
.modal-btn-row .primary-btn { flex: 1; }
.status-text { margin: 0; font-size: 13px; color: var(--accent); min-height: 18px; }

/* ============ WAKE GUARD MODAL ============ */
.wake-backdrop { background: rgba(15, 13, 10, 0.78); }
.wake-card { border: 2px solid var(--accent); }
.wake-alert-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(217, 119, 6, 0.16);
  color: var(--accent);
  font-size: 14px;
  margin-bottom: 14px;
}
.pulse-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  animation: pulse 1.3s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { transform: scale(0.85); opacity: 0.55; }
  50% { transform: scale(1.25); opacity: 1; }
}

.wake-metrics {
  padding: 14px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  margin: 12px 0;
}
.wake-row, .wake-meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
}
.wake-meta-row { margin-top: 8px; font-size: 12px; color: var(--text-muted); }

.progress-track {
  height: 10px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
  margin: 10px 0;
}
.progress-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.25s ease;
}

.wake-pill {
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.warn-box {
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(220, 38, 38, 0.12);
  color: #dc2626;
  font-size: 12px;
  margin: 10px 0;
}
.wake-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 80;
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(24, 22, 19, 0.92);
  color: #fff;
  font-size: 13px;
  box-shadow: var(--shadow);
  max-width: 90vw;
  text-align: center;
}

@media (max-width: 560px) {
  .hide-xs { display: none; }
  .shelf-header { padding-left: 14px; padding-right: 14px; }
  .brand h1 { font-size: 17px; }
  .subtitle { display: none; }
  .reading-scroll { padding: 18px 16px 40px; }
  .badge-text { display: none; }
}
