:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --border: #e2ded6;
  --accent: #c0392b;
  --accent-text: #ffffff;
  --pivot: #d0342c;
  --focus: #2f6fdf;
  --radius: 10px;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --reading-font: Georgia, 'Iowan Old Style', 'Times New Roman', serif;
  color-scheme: light;
}

/* Dark colours: when chosen explicitly, or by the device when the theme is "Auto". */
:root[data-theme='dark'] {
  --bg: #16171a;
  --surface: #1f2024;
  --text: #e8e6e3;
  --muted: #9a9aa0;
  --border: #33343a;
  --accent: #e0574a;
  --pivot: #ff6b5e;
  --focus: #6ea0ff;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #16171a;
    --surface: #1f2024;
    --text: #e8e6e3;
    --muted: #9a9aa0;
    --border: #33343a;
    --accent: #e0574a;
    --pivot: #ff6b5e;
    --focus: #6ea0ff;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
}

#app { min-height: 100%; }

a { color: var(--accent); }

button, input, select { font: inherit; color: inherit; }

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

[hidden] { display: none !important; }

.btn {
  display: inline-block;
  padding: 0.55em 1.1em;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.big { font-size: 1.1rem; padding: 0.8em 1.6em; }
.btn.small { font-size: 0.85rem; padding: 0.35em 0.75em; }

.icon-btn {
  min-width: 44px;
  min-height: 44px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  cursor: pointer;
  font-size: 1.15rem;
}
.icon-btn:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.icon-btn.small { min-width: 36px; min-height: 36px; font-size: 0.95rem; }

.error { color: var(--accent); }

/* ---- Sign in ---- */

.login {
  max-width: 34rem;
  margin: 0 auto;
  padding: 12vh 16px 32px;
  text-align: center;
}
.login h1 { font-family: var(--reading-font); font-size: 2.2rem; margin-bottom: 0.4em; }
.login .lead { color: var(--muted); line-height: 1.5; margin-bottom: 2em; }
.key-login { margin-top: 2.5em; text-align: left; }
.key-login summary { cursor: pointer; color: var(--muted); text-align: center; }
.key-login form { display: grid; gap: 0.8em; margin-top: 1em; }
.key-login p { color: var(--muted); font-size: 0.9rem; line-height: 1.5; margin: 0; }
.key-login label { display: grid; gap: 0.3em; font-size: 0.9rem; }
.key-login input {
  padding: 0.6em 0.8em;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-family: ui-monospace, monospace;
}
.fine { color: var(--muted); font-size: 0.8rem; margin-top: 3em; }

/* ---- Library ---- */

.library { max-width: 46rem; margin: 0 auto; padding: 0 16px 48px; }

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px 0;
}
.topbar h1 { font-family: var(--reading-font); font-size: 1.4rem; margin: 0; }
.topbar .spacer { flex: 1; }
.topbar .user { color: var(--muted); font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.lib-scope {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin-bottom: 10px;
}
.lib-scope > label:first-child { font-size: 0.85rem; color: var(--muted); }
.lib-scope select {
  flex: 1;
  min-width: 12rem;
  padding: 0.55em 0.7em;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.lib-scope .check { display: flex; align-items: center; gap: 6px; font-size: 0.85rem; color: var(--muted); }

.lib-tools { display: flex; gap: 8px; align-items: stretch; }
.lib-tools input {
  flex: 1;
  min-width: 0;
  padding: 0.7em 0.9em;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
/* Info page */
.info-page { max-width: 42rem; margin: 0 auto; padding: 0 16px 48px; }
.info-content { line-height: 1.6; }
.info-content h1 { font-family: var(--reading-font); font-size: 1.8rem; margin: 0.4em 0 0.6em; }
.info-content h2 { font-family: var(--reading-font); font-size: 1.2rem; margin-top: 1.6em; }
.info-content li { margin: 0.3em 0; }

.lib-status { color: var(--muted); font-size: 0.9rem; min-height: 1.2em; }

.library h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 1.5em 0 0.6em; }

.book-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.book {
  position: relative;
  display: grid;
  gap: 4px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}
.book:hover { border-color: var(--muted); }
.book-title { font-family: var(--reading-font); font-size: 1.05rem; line-height: 1.3; }
.book-link { color: inherit; text-decoration: none; }
/* The title link covers the whole card, so the card is clickable. */
.book-link::after { content: ''; position: absolute; inset: 0; border-radius: inherit; }
.book-link:focus-visible { outline: none; }
.book:has(.book-link:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.book-meta { color: var(--muted); font-size: 0.85rem; display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px; }
.book-details { min-width: 0; overflow: hidden; text-overflow: ellipsis; }

/* "Open in Zotero" links sit above the card's link. */
.zotero-link {
  position: relative;
  z-index: 1;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
}
.zotero-link::after { content: ' ↗'; }
.zotero-link:hover { text-decoration: underline; }
/* List / Covers toggle */
.view-toggle { display: flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; flex-shrink: 0; }
.view-toggle button { border: none; background: var(--surface); padding: 0 0.9em; cursor: pointer; font-size: 0.85rem; }
.view-toggle button[aria-pressed='true'] { background: var(--text); color: var(--bg); }

/* Cover grid */
.book-list.book-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 20px 16px; }
.book.tile { padding: 0; border: none; background: none; gap: 3px; align-content: start; }
.book.tile .book-title { font-size: 0.9rem; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-top: 6px; }
.book.tile .book-meta { font-size: 0.78rem; display: grid; gap: 2px; }
.book.tile .book-details { white-space: nowrap; }
.book.tile .zotero-link { justify-self: start; font-size: 0.75rem; }
.cover {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  aspect-ratio: 2 / 3;
  padding: 12px 10px;
  border-radius: 4px 8px 8px 4px;
  overflow: hidden;
  background: linear-gradient(160deg, hsl(var(--hue) 38% 42%), hsl(var(--hue) 45% 26%));
  color: #fff;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.15), 0 4px 12px rgb(0 0 0 / 0.12);
  transition: transform 0.15s, box-shadow 0.15s;
}
.book.tile:hover .cover { transform: translateY(-2px); box-shadow: 0 2px 4px rgb(0 0 0 / 0.18), 0 8px 20px rgb(0 0 0 / 0.16); }
.cover-title { font-family: var(--reading-font); font-size: 0.95rem; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }
.cover-author { font-size: 0.72rem; opacity: 0.85; }
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity 0.25s; }
.cover.has-image img { opacity: 1; }
.cover.has-image > span { visibility: hidden; }
.book.tile .book-progress { margin-top: 2px; }
.continue .book-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }

.book-progress { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; margin-top: 4px; }
.book-progress span { display: block; height: 100%; background: var(--accent); }

/* ---- Reader ---- */

.reader {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  position: relative;
}

.reader-bar {
  position: relative;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.reader-heading { flex: 1; min-width: 0; display: grid; gap: 2px; }
.reader-title-row { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.reader-title { font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.reader-title-row .zotero-link { flex-shrink: 0; }
.chapter-select {
  max-width: 100%;
  padding: 0.25em 0.4em;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  font-size: 0.85rem;
}

.text-size-btn { font-family: var(--reading-font); font-size: 1.05rem; flex-shrink: 0; }
.size-pop {
  position: absolute;
  top: calc(100% + 4px);
  right: 12px;
  display: grid;
  gap: 6px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
}
.size-pop[hidden] { display: none; }
.pop-row { display: flex; align-items: center; justify-content: space-between; gap: 4px; }
.pop-label { font-size: 0.8rem; color: var(--muted); margin-right: 6px; }
.theme-choice { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.theme-choice button { border: none; background: transparent; padding: 0.35em 0.7em; cursor: pointer; font-size: 0.82rem; }
.theme-choice button[aria-pressed='true'] { background: var(--text); color: var(--bg); }
.size-val { min-width: 3.2em; text-align: center; font-variant-numeric: tabular-nums; font-size: 0.9rem; }

.mode-toggle { display: flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; flex-shrink: 0; }
.mode-toggle button { border: none; background: transparent; padding: 0.45em 0.8em; cursor: pointer; font-size: 0.85rem; min-height: 36px; }
.mode-toggle button[aria-pressed='true'] { background: var(--text); color: var(--bg); }

.reader-main { flex: 1; min-height: 0; position: relative; }

.speed-pane, .page-pane { position: absolute; inset: 0; display: flex; flex-direction: column; }
/* The page view stays laid out (not display: none) so epub.js keeps its size,
   but must be hidden with !important: epub.js sets visibility: visible inline
   on its iframes. */
.reader[data-mode='speed'] .page-pane,
.reader[data-mode='speed'] .page-pane * { visibility: hidden !important; pointer-events: none; }
.reader:not([data-mode='speed']) .speed-pane { display: none; }

/* Speed mode */

.rsvp-stage {
  flex: 1;
  min-height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  position: relative;
}
/* Fixation guides above and below the pivot letter */
.rsvp-stage::before, .rsvp-stage::after {
  content: '';
  position: absolute;
  left: 50%;
  width: 2px;
  height: 14px;
  background: var(--border);
}
.rsvp-stage::before { top: calc(50% - 2.4em * var(--rsvp-scale, 1)); }
.rsvp-stage::after { bottom: calc(50% - 2.4em * var(--rsvp-scale, 1)); }

.rsvp-word {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  width: 100%;
  font-family: var(--reading-font);
  font-size: calc(clamp(2rem, 8vw, 3.6rem) * var(--rsvp-scale, 1));
  line-height: 1.2;
  white-space: pre;
}
.rsvp-word .l { text-align: right; }
.rsvp-word .p { color: var(--pivot); }
.rsvp-word .r { text-align: left; }
.rsvp-stage.heading .rsvp-word { font-weight: 700; }

.rsvp-context {
  max-width: 40rem;
  margin: 0 auto;
  padding: 0 16px;
  min-height: 4.5em;
  color: var(--muted);
  font-family: var(--reading-font);
  line-height: 1.6;
  text-align: center;
  transition: opacity 0.2s;
}
.reader.playing .rsvp-context { opacity: 0; }
.ctx-word { cursor: pointer; border-radius: 3px; }
.ctx-word:hover { color: var(--text); }
.ctx-word.current { color: var(--text); background: color-mix(in srgb, var(--pivot) 18%, transparent); }

.rsvp-bottom {
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  display: grid;
  gap: 8px;
  justify-items: center;
}
.progress { width: 100%; max-width: 40rem; height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }
.progress-label { color: var(--muted); font-size: 0.8rem; text-align: center; }

.rsvp-controls { display: flex; align-items: center; gap: 4px; }
.play-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 1.4rem;
  cursor: pointer;
  margin: 0 8px;
}
.wpm-controls { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.9rem; }
.wpm strong { color: var(--text); font-variant-numeric: tabular-nums; }

/* Page mode */

.page-view { flex: 1; min-height: 0; padding: 16px 16px 0; }
.page-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
}
.page-bar .page-label { flex: 1; }

/* Loading / errors */

.reader-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  background: var(--bg);
  color: var(--muted);
  text-align: center;
  z-index: 5;
}
.reader-overlay.error { color: var(--accent); }
.reader-overlay.error .spinner { display: none; }
.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.sync-status { font-size: 0.75rem; color: var(--muted); text-align: center; padding: 2px 8px 6px; min-height: 1.2em; }
.sync-status[data-kind='error'] { color: var(--accent); }

@media (max-width: 520px) {
  .book-list.book-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 16px 12px; }
  .reader-bar { flex-wrap: wrap; }
  .reader-heading { order: 3; flex-basis: 100%; }
  .reader-title { font-size: 0.8rem; }
  .mode-toggle button { padding: 0.45em 0.6em; }
}
