/* epub — styles. Themes are sets of CSS variables keyed by [data-theme]; the
   same attribute on a swatch button makes it preview its own theme. */

/* ---------- themes ---------- */

:root,
[data-theme="paper"] {
  color-scheme: light;
  --bg: #fbfaf7; --surface: #ffffff; --surface-2: #f1eee8; --border: #e4dfd5;
  --text: #1f1d1a; --muted: #6b665e; --accent: #2f6fdb; --accent-text: #ffffff;
  --hl: #fff0a6; --hl-word: #ffd23f; --danger: #c0392b;
}
[data-theme="snow"] {
  color-scheme: light;
  --bg: #ffffff; --surface: #f7f8fa; --surface-2: #eceff3; --border: #e2e5ea;
  --text: #111827; --muted: #5f6b7a; --accent: #0f766e; --accent-text: #ffffff;
  --hl: #ccf5ec; --hl-word: #7ce0cb; --danger: #c0392b;
}
[data-theme="sepia"] {
  color-scheme: light;
  --bg: #f4ecd8; --surface: #fbf5e6; --surface-2: #ebe0c5; --border: #dccfae;
  --text: #433422; --muted: #7a6650; --accent: #9c4f24; --accent-text: #fffaf0;
  --hl: #f3d58f; --hl-word: #e7ad55; --danger: #a8321e;
}
[data-theme="sand"] {
  color-scheme: light;
  --bg: #fdf6e3; --surface: #fffbef; --surface-2: #eee8d5; --border: #e3dbc4;
  --text: #36454b; --muted: #6f7f85; --accent: #1f7fc4; --accent-text: #ffffff;
  --hl: #f2e3a3; --hl-word: #e5c35c; --danger: #c0392b;
}
[data-theme="mint"] {
  color-scheme: light;
  --bg: #eef7f1; --surface: #f8fcf9; --surface-2: #dcefe3; --border: #cde3d5;
  --text: #17301f; --muted: #557062; --accent: #1b7f45; --accent-text: #ffffff;
  --hl: #c4efd2; --hl-word: #86dca4; --danger: #b83227;
}
[data-theme="sky"] {
  color-scheme: light;
  --bg: #eef5fc; --surface: #f8fbff; --surface-2: #dde9f7; --border: #cfdff0;
  --text: #13263b; --muted: #546a83; --accent: #1d69c9; --accent-text: #ffffff;
  --hl: #cde5ff; --hl-word: #8cc3ff; --danger: #c0392b;
}
[data-theme="rose"] {
  color-scheme: light;
  --bg: #fcf1f3; --surface: #fff8f9; --surface-2: #f6e1e6; --border: #efd2d9;
  --text: #3b1d26; --muted: #80596a; --accent: #b8185a; --accent-text: #ffffff;
  --hl: #ffd5e2; --hl-word: #ff9fbf; --danger: #b3261e;
}
[data-theme="lavender"] {
  color-scheme: light;
  --bg: #f3f0fb; --surface: #faf8ff; --surface-2: #e6e0f6; --border: #dcd3f0;
  --text: #2a2140; --muted: #685f82; --accent: #6342e8; --accent-text: #ffffff;
  --hl: #e2d9ff; --hl-word: #bba8ff; --danger: #b3261e;
}
[data-theme="contrast-light"] {
  color-scheme: light;
  --bg: #ffffff; --surface: #ffffff; --surface-2: #ececec; --border: #000000;
  --text: #000000; --muted: #2b2b2b; --accent: #0000c8; --accent-text: #ffffff;
  --hl: #ffff00; --hl-word: #ffb000; --danger: #b00000;
}
[data-theme="slate"] {
  color-scheme: dark;
  --bg: #1e232b; --surface: #262c35; --surface-2: #303741; --border: #3a424e;
  --text: #e3e7ed; --muted: #9ba5b3; --accent: #7aa2f7; --accent-text: #0b1020;
  --hl: #3a4a66; --hl-word: #56709f; --danger: #ff8a80;
}
[data-theme="midnight"] {
  color-scheme: dark;
  --bg: #0e1320; --surface: #151c2c; --surface-2: #1e2740; --border: #26304a;
  --text: #d8def0; --muted: #8d97b3; --accent: #5eb1ff; --accent-text: #06121f;
  --hl: #1f3a5f; --hl-word: #2f5f99; --danger: #ff8a80;
}
[data-theme="fjord"] {
  color-scheme: dark;
  --bg: #2e3440; --surface: #3b4252; --surface-2: #434c5e; --border: #4c566a;
  --text: #eceff4; --muted: #b0b9c9; --accent: #88c0d0; --accent-text: #1f2530;
  --hl: #4b6782; --hl-word: #5f83a8; --danger: #ffab91;
}
[data-theme="forest"] {
  color-scheme: dark;
  --bg: #17211b; --surface: #1e2b23; --surface-2: #27372d; --border: #304336;
  --text: #dfeadf; --muted: #95aa99; --accent: #6fcf8f; --accent-text: #0e1a12;
  --hl: #2c4a37; --hl-word: #417052; --danger: #ff8a80;
}
[data-theme="plum"] {
  color-scheme: dark;
  --bg: #282a36; --surface: #313442; --surface-2: #3a3d4e; --border: #474a5e;
  --text: #f1f1ec; --muted: #a9adc7; --accent: #bd93f9; --accent-text: #1d1b2a;
  --hl: #4b3f6b; --hl-word: #6c55a6; --danger: #ff8a80;
}
[data-theme="espresso"] {
  color-scheme: dark;
  --bg: #231b16; --surface: #2d231d; --surface-2: #382c25; --border: #46382f;
  --text: #ecdfd2; --muted: #b19e8c; --accent: #e0a46a; --accent-text: #24170e;
  --hl: #5a4030; --hl-word: #845a3c; --danger: #ff9e80;
}
[data-theme="deep-sea"] {
  color-scheme: dark;
  --bg: #002b36; --surface: #073642; --surface-2: #0d4250; --border: #144d5b;
  --text: #dfe6e4; --muted: #93a1a1; --accent: #2aa198; --accent-text: #00191f;
  --hl: #16535f; --hl-word: #23757f; --danger: #ff8a80;
}
[data-theme="black"] {
  color-scheme: dark;
  --bg: #000000; --surface: #0b0b0b; --surface-2: #171717; --border: #262626;
  --text: #e6e6e6; --muted: #8f8f8f; --accent: #ffb000; --accent-text: #000000;
  --hl: #3a2f00; --hl-word: #6b5600; --danger: #ff6b6b;
}
[data-theme="contrast-dark"] {
  color-scheme: dark;
  --bg: #000000; --surface: #000000; --surface-2: #1c1c1c; --border: #ffffff;
  --text: #ffffff; --muted: #e6e6e6; --accent: #ffff00; --accent-text: #000000;
  --hl: #003f88; --hl-word: #0060d0; --danger: #ff7070;
}

:root {
  --reader-font: Charter, 'Bitstream Charter', 'Sitka Text', Cambria, Georgia, serif;
  --reader-size: 20px;
  --reader-lh: 1.6;
  --ui-font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --bar-h: 56px;
  --top-offset: calc(var(--bar-h) + env(safe-area-inset-top, 0px));
  --radius: 12px;
  --shadow: 0 6px 24px rgb(0 0 0 / 0.12);
  --mark: color-mix(in srgb, var(--accent) 24%, transparent);
}

/* ---------- base ---------- */

*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--ui-font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: 0.6; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon { flex: none; display: block; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.noscript { padding: 24px; text-align: center; }

/* ---------- buttons & forms ---------- */

.icon-btn {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  text-decoration: none;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn[aria-expanded="true"],
.icon-btn[aria-current="page"] { background: var(--surface-2); color: var(--accent); }
.icon-btn.danger { color: var(--danger); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-weight: 550;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-small { min-height: 34px; padding: 5px 12px; font-size: 14px; }
.btn-block { width: 100%; }
.link-btn { padding: 0; border: 0; background: none; color: var(--accent); font: inherit; font-size: 14px; text-decoration: underline; }

.field { display: grid; gap: 6px; font-weight: 550; font-size: 14px; }
.field small { font-weight: 400; color: var(--muted); }
.field input,
.field select,
.select {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-size: 16px; /* prevents iOS zoom on focus */
  font-weight: 400;
}
.field input:focus,
.field select:focus,
.select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.select { width: auto; min-height: 40px; padding: 7px 10px; font-size: 15px; }

.form-grid { display: grid; gap: 14px; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.stack { display: grid; gap: 16px; }

input[type="range"] { accent-color: var(--accent); width: 100%; min-width: 0; }

.spinner {
  width: 28px;
  height: 28px;
  margin: 48px auto;
  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); } }

.count {
  display: inline-block;
  min-width: 22px;
  padding: 0 7px;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  line-height: 20px;
  text-align: center;
}
.pill {
  padding: 1px 8px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.02em;
}
.notice {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  font-size: 14px;
  overflow-wrap: anywhere;
}

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 6px;
  height: var(--top-offset);
  padding: env(safe-area-inset-top, 0px) 8px 0;
  padding-left: max(8px, env(safe-area-inset-left));
  padding-right: max(8px, env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  color: var(--accent);
  font-weight: 700;
  font-size: 18px;
  text-decoration: none;
}
.brand span { color: var(--text); }
.topbar-title { min-width: 0; overflow: hidden; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.tabs-nav { display: flex; gap: 2px; margin-left: 8px; }
.tab {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 38px;
  padding: 0 12px;
  border-radius: 10px;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab[aria-current="page"] { background: var(--surface-2); color: var(--text); }
.tab-badge {
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  line-height: 20px;
  text-align: center;
}
@media (max-width: 520px) {
  .brand span { display: none; }
  .tabs-nav { margin-left: 0; }
  .tab { padding: 0 10px; }
  .hide-sm { display: none; }
}

/* offline */
.offline-badge {
  display: none;
  align-items: center;
  gap: 5px;
  flex: none;
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}
body.is-offline .offline-badge { display: inline-flex; }
@media (max-width: 560px) {
  .offline-badge { width: 32px; padding: 0; justify-content: center; }
  .offline-badge span { display: none; }
}
.offline-note-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 18px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--muted);
  font-size: 14px;
}
.offline-note-bar .icon { color: var(--accent); }
.book-card.unsaved { opacity: 0.4; filter: grayscale(0.6); }
.offline-note { display: flex; align-items: center; gap: 5px; margin: 6px 0 0; color: var(--muted); font-size: 13px; }
.offline-note .icon { color: #1b9e55; }
.chapter-body img.img-missing { display: none; }

.main { padding: 20px 28px 96px; padding-left: max(28px, env(safe-area-inset-left)); padding-right: max(28px, env(safe-area-inset-right)); }
@media (max-width: 640px) { .main { padding: 14px 16px 80px; } }

.page { max-width: 760px; margin: 0 auto; display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; }
.view-title { margin: 4px 0 0; font-size: 26px; line-height: 1.2; letter-spacing: -0.01em; }
.title-note { margin-left: 6px; color: var(--muted); font-size: 15px; font-weight: 500; letter-spacing: 0; }
.card { padding: 20px; border: 1px solid var(--border); border-radius: 16px; background: var(--surface); }
.card h2 { margin: 0 0 14px; font-size: 17px; }
.card-title { margin: 0 0 14px; font-size: 20px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.card > p:first-of-type { margin-top: 0; }

.empty { padding: 48px 16px; color: var(--muted); text-align: center; }
.empty .icon { margin: 0 auto 8px; color: var(--accent); }
.empty h2 { margin: 8px 0 4px; color: var(--text); font-size: 20px; }
.empty p { max-width: 420px; margin: 0 auto 18px; }
.empty-shelf { padding-top: 72px; }

/* ---------- covers ---------- */

.cover {
  position: relative;
  display: block;
  overflow: hidden;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  background: var(--surface-2);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.12), 0 8px 20px -8px rgb(0 0 0 / 0.3);
}
.cover img { display: block; width: 100%; height: 100%; object-fit: cover; }
.gen-cover {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
  height: 100%;
  padding: 16% 10% 12%;
  overflow: hidden;
  background: linear-gradient(155deg, hsl(var(--h, 210) 42% 44%), hsl(calc(var(--h, 210) + 35) 48% 26%));
  color: #fff;
  font-family: Georgia, 'Times New Roman', serif;
  text-align: center;
}
.gc-title { display: -webkit-box; overflow: hidden; font-size: 15px; font-weight: 700; line-height: 1.25; -webkit-line-clamp: 6; line-clamp: 6; -webkit-box-orient: vertical; overflow-wrap: anywhere; }
.gc-author { overflow: hidden; font-size: 11.5px; opacity: 0.85; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- shelf ---------- */

.shelf { max-width: 1180px; margin: 0 auto; }
.shelf-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.shelf-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.search {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
}
.search:focus-within { outline: 2px solid var(--accent); border-color: transparent; }
.search input { width: 150px; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 15px; }

.shelf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 28px 20px;
  margin: 0;
  padding: 0;
  list-style: none;
}
@media (max-width: 520px) { .shelf-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 22px 14px; } }
.shelf-none { grid-column: 1 / -1; padding: 32px 0; text-align: center; }

.book-card { display: grid; gap: 4px; color: inherit; text-decoration: none; }
.book-card .cover { margin-bottom: 6px; transition: transform 0.15s ease, box-shadow 0.15s ease; }
.book-card:hover .cover { transform: translateY(-3px); box-shadow: 0 2px 4px rgb(0 0 0 / 0.12), 0 14px 28px -10px rgb(0 0 0 / 0.35); }
.bc-title { display: -webkit-box; overflow: hidden; font-size: 14.5px; font-weight: 650; line-height: 1.3; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; }
.bc-author { overflow: hidden; color: var(--muted); font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.bc-progress { height: 3px; margin-top: 4px; overflow: hidden; border-radius: 2px; background: var(--surface-2); }
.bc-progress span { display: block; height: 100%; background: var(--accent); }
.cover-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 7px 2px 5px;
  border-radius: 10px;
  background: rgb(0 0 0 / 0.62);
  color: #fff;
  font-size: 12px;
  font-weight: 650;
}

.upload-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 18px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
}
.upload-banner .icon { color: var(--accent); animation: bob 1.4s ease-in-out infinite; }
.upload-banner > span:last-child { color: var(--accent); }
@keyframes bob { 50% { transform: translateY(-3px); } }

/* ---------- uploads ---------- */

.dropzone {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 32px 16px;
  border: 2px dashed var(--border);
  border-radius: 14px;
  background: var(--bg);
  color: var(--muted);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover,
.dropzone:focus-visible,
body.dragging .dropzone { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 6%, var(--bg)); }
.dropzone .icon { color: var(--accent); }
.dz-main { margin: 6px 0 0; color: var(--text); font-size: 16px; }
.dz-link { color: var(--accent); text-decoration: underline; }
.dz-hint { margin: 0; font-size: 13px; }
body.dragging::after {
  content: 'Drop to upload';
  position: fixed;
  inset: 12px;
  z-index: 100;
  display: grid;
  place-items: center;
  border: 3px dashed var(--accent);
  border-radius: 20px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  color: var(--accent);
  font-size: 22px;
  font-weight: 700;
  pointer-events: none;
}

.upload-queue { display: grid; gap: 8px; margin: 14px 0 0; padding: 0; list-style: none; }
.upload-queue:empty { display: none; }
.up-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
}
.up-icon { color: var(--muted); }
.up-row.is-done .up-icon { color: #1b9e55; }
.up-row.is-error .up-icon,
.up-row.is-error .up-status { color: var(--danger); }
.up-main { display: grid; flex: 1; min-width: 0; gap: 5px; }
.up-name { overflow: hidden; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.up-name .muted { font-weight: 400; font-size: 13px; }
.up-status { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.up-bar { height: 5px; overflow: hidden; border-radius: 3px; background: var(--surface-2); }
.up-bar span { display: block; height: 100%; background: var(--accent); transition: width 0.2s; }
.up-bar.busy span {
  width: 100% !important;
  background: linear-gradient(90deg, var(--accent) 0 40%, color-mix(in srgb, var(--accent) 35%, transparent) 50%, var(--accent) 60% 100%);
  background-size: 250% 100%;
  animation: busy 1.2s linear infinite;
}
@keyframes busy { from { background-position: 100% 0; } to { background-position: -150% 0; } }
.up-actions { display: flex; align-items: center; gap: 2px; }
.queue-foot { margin-top: 10px; text-align: right; }
.queue-foot:empty { display: none; }

.lib-list { margin: 0; padding: 0; list-style: none; }
.lib-row { display: flex; align-items: center; gap: 14px; padding: 12px 0; }
.lib-row + .lib-row { border-top: 1px solid var(--border); }
.lib-cover { width: 46px; flex: none; border-radius: 4px; }
.lib-cover .gen-cover { padding: 8px 4px; }
.lib-cover .gc-title { font-size: 7px; }
.lib-cover .gc-author { display: none; }
.lib-text { flex: 1; min-width: 0; }
.lib-title { color: var(--text); font-weight: 650; text-decoration: none; overflow-wrap: anywhere; }
.lib-title:hover { text-decoration: underline; }
.lib-author { color: var(--muted); font-size: 14px; }
.lib-meta { overflow: hidden; color: var(--muted); font-size: 12.5px; text-overflow: ellipsis; white-space: nowrap; }
.lib-actions { display: flex; gap: 2px; }
.lib-edit { flex: 1; }
.lib-empty { padding: 12px 0; }

/* ---------- book: chapters & bookmarks ---------- */

.book-page { gap: 22px; }
.book-hero { display: grid; grid-template-columns: 150px minmax(0, 1fr); gap: 24px; align-items: center; }
@media (max-width: 520px) { .book-hero { grid-template-columns: 104px minmax(0, 1fr); gap: 16px; align-items: start; } }
.hero-title { margin: 0; font-size: 26px; line-height: 1.2; letter-spacing: -0.01em; overflow-wrap: anywhere; }
@media (max-width: 520px) { .hero-title { font-size: 21px; } }
.hero-author { margin: 6px 0 0; color: var(--muted); font-size: 16px; }
.hero-meta { margin: 6px 0 0; color: var(--muted); font-size: 13.5px; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.hero-progress { display: grid; gap: 6px; max-width: 360px; margin-top: 14px; }
.hp-bar { height: 4px; overflow: hidden; border-radius: 2px; background: var(--surface-2); }
.hp-bar span { display: block; height: 100%; background: var(--accent); }
.hp-text { overflow: hidden; color: var(--muted); font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }

.seg-tabs { display: flex; gap: 4px; padding: 4px; border-radius: 12px; background: var(--surface-2); }
.seg-tabs [role="tab"] {
  display: inline-flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 38px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}
.seg-tabs [role="tab"][aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / 0.1); }
.seg-tabs .count { background: var(--accent); color: var(--accent-text); }

.chapter-list { margin: 0; padding: 0; list-style: none; }
.ch { padding-left: calc(var(--depth, 0) * 18px); }
.ch + .ch { border-top: 1px solid var(--border); }
.ch-link {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  margin: 2px -10px;
  padding: 8px 10px;
  border-radius: 10px;
  color: var(--text);
  text-decoration: none;
}
.ch-link:hover { background: var(--surface-2); }
.ch-title { min-width: 0; overflow-wrap: anywhere; font-weight: 550; }
.ch.reading .ch-title { color: var(--accent); font-weight: 650; }
.ch-bm { display: inline-flex; flex: none; align-items: center; gap: 2px; color: var(--accent); font-size: 12px; font-weight: 700; }
.ch-meta { display: inline-flex; flex: none; align-items: center; gap: 8px; margin-left: auto; padding-left: 8px; color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.ch-subs { margin: -4px 0 6px; padding: 0 0 0 14px; list-style: none; }
.ch-subs a { display: block; padding: 5px 0; color: var(--muted); font-size: 14px; text-decoration: none; }
.ch-subs a:hover { color: var(--accent); }

.bm-group + .bm-group { margin-top: 18px; }
.bm-group-title { margin: 0 0 6px; font-size: 14px; }
.bm-group-title a { color: var(--muted); text-decoration: none; }
.bm-group-title a:hover { color: var(--accent); }
.bm-list { display: grid; gap: 6px; margin: 0; padding: 0; list-style: none; }
.bm-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 6px 8px 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.bm-play {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: none;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
}
.bm-play .icon { margin-left: 2px; }
.bm-text {
  display: -webkit-box;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  color: var(--text);
  font-family: var(--reader-font);
  font-size: 15px;
  line-height: 1.45;
  text-decoration: none;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
}
.bm-text mark { padding: 0 2px; border-radius: 3px; background: var(--mark); color: inherit; font-weight: 700; }
.bm-text:hover { color: var(--accent); }

/* ---------- reader ---------- */

.reader-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  height: var(--top-offset);
  padding: env(safe-area-inset-top, 0px) 8px 0;
  padding-left: max(8px, env(safe-area-inset-left));
  padding-right: max(8px, env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.rb-title { display: grid; flex: 1; min-width: 0; padding: 0 6px; line-height: 1.25; }
.rb-chapter { overflow: hidden; font-size: 14.5px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.rb-book { overflow: hidden; color: var(--muted); font-size: 12.5px; text-overflow: ellipsis; white-space: nowrap; }
.rb-tools { display: flex; align-items: center; gap: 2px; }
.rb-tts { display: inline-flex; align-items: center; gap: 6px; margin-left: 6px; padding-left: 8px; border-left: 1px solid var(--border); }
@media (max-width: 560px) {
  .rb-title { visibility: hidden; }
  .rb-tts { margin-left: 2px; padding-left: 6px; }
}
.aa { font: 700 18px/1 Georgia, serif; }
.aa small { font-size: 13px; }
.tool-count {
  position: absolute;
  top: 3px;
  right: 1px;
  min-width: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 10.5px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
}
.play-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
}
.play-btn[data-playing="false"] .icon,
.play-btn:not([data-playing]) .icon { margin-left: 2px; }
.play-btn.nudge { animation: nudge 0.9s ease-in-out 3; }
@keyframes nudge { 50% { transform: scale(1.14); box-shadow: 0 0 0 8px color-mix(in srgb, var(--accent) 25%, transparent); } }
.speed-btn {
  min-width: 50px;
  height: 34px;
  padding: 0 9px;
  border: 1px solid var(--border);
  border-radius: 17px;
  background: var(--bg);
  font-size: 13.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.speed-btn[aria-expanded="true"] { border-color: var(--accent); color: var(--accent); }
.rb-progress { position: absolute; right: 0; bottom: -1px; left: 0; height: 2px; }
.rb-progress span { display: block; width: 0; height: 100%; background: var(--accent); }

.panel {
  position: fixed;
  top: calc(var(--top-offset) + 8px);
  right: max(8px, env(safe-area-inset-right));
  z-index: 30;
  display: grid;
  gap: 16px;
  width: min(420px, calc(100vw - 16px));
  max-height: calc(100dvh - var(--top-offset) - 24px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-family: var(--ui-font);
}
.panel-head h2 { margin: 0; font-size: 16px; }
.panel-note { margin: 0; color: var(--muted); font-size: 14px; }
.panel-link { display: inline-flex; align-items: center; gap: 4px; font-size: 14px; font-weight: 600; text-decoration: none; }
.panel .bm-text { font-size: 14px; }

.type-box { display: grid; gap: 16px; }
.type-group { display: grid; gap: 8px; }
.type-label { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; font-weight: 650; }
.type-label output { color: var(--text); font-weight: 550; }
.font-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
@media (max-width: 400px) { .font-grid { grid-template-columns: repeat(2, 1fr); } }
.font-opt {
  min-height: 42px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-size: 16px;
}
.font-opt[aria-checked="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); color: var(--accent); }
.stepper { display: flex; align-items: center; gap: 10px; }
.step-btn {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-family: Georgia, serif;
  font-size: 20px;
  font-weight: 700;
}
.a-small { font-size: 13px; }
.a-large { font-size: 21px; }
.step-icon { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; cursor: pointer; }
.switch-text { display: grid; gap: 2px; }
.switch-text .type-label { justify-content: flex-start; }
.switch-text small { color: var(--muted); font-size: 12.5px; line-height: 1.4; }
.switch { position: relative; flex: none; display: inline-flex; }
.switch input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.switch-track {
  position: relative;
  width: 40px;
  height: 24px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  transition: background 0.15s;
}
.switch-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.15s, background 0.15s;
}
.switch input:checked + .switch-track { border-color: var(--accent); background: var(--accent); }
.switch input:checked + .switch-track::after { background: var(--accent-text); transform: translateX(16px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.chip {
  min-width: 48px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg);
  font-size: 13.5px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
.chip[aria-pressed="true"] { border-color: var(--accent); background: var(--accent); color: var(--accent-text); }
.tts-transport { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.tts-pos { margin-left: auto; color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }

.swatch-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); gap: 12px; }
.swatch-grid.compact { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.swatch {
  display: block;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
  text-align: left;
  transition: transform 0.12s;
}
.swatch:hover { transform: translateY(-2px); }
.swatch[aria-checked="true"] { outline: 3px solid var(--accent); outline-offset: 2px; }
.swatch-preview { position: relative; display: block; height: 70px; background: var(--bg); }
.compact .swatch-preview { height: 52px; }
.sw-aa { position: absolute; top: 9px; left: 11px; font: 700 22px/1 Georgia, serif; color: var(--text); }
.compact .sw-aa { top: 7px; left: 9px; font-size: 18px; }
.sw-dot { position: absolute; top: 11px; right: 11px; width: 16px; height: 16px; border-radius: 50%; background: var(--accent); }
.compact .sw-dot { top: 9px; right: 9px; width: 13px; height: 13px; }
.sw-line { position: absolute; left: 11px; right: 30px; bottom: 24px; height: 5px; border-radius: 3px; background: var(--muted); opacity: 0.45; }
.sw-line.sw-hl { right: 46px; bottom: 11px; height: 8px; border-radius: 4px; background: var(--hl); opacity: 1; }
.compact .sw-line { left: 9px; bottom: 18px; height: 4px; }
.compact .sw-line.sw-hl { bottom: 8px; height: 6px; }
.sw-check {
  position: absolute;
  right: 7px;
  bottom: 7px;
  display: none;
  width: 20px;
  height: 20px;
  place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
}
.swatch[aria-checked="true"] .sw-check { display: grid; }
.swatch-name { display: block; overflow: hidden; padding: 6px 9px; border-top: 1px solid var(--border); background: var(--surface); font-size: 12px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }

.type-sample { margin: 16px 0 0; padding: 14px 16px; border-radius: 10px; background: var(--bg); }

/* the text itself */

.reader-main {
  padding: 8px 24px calc(40px + env(safe-area-inset-bottom, 0px));
  padding-left: max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
}
@media (max-width: 640px) { .reader-main { padding: 4px 18px calc(32px + env(safe-area-inset-bottom, 0px)); } }
.reader {
  max-width: 720px;
  margin: 0 auto;
  font-family: var(--reader-font);
  font-size: var(--reader-size);
  line-height: var(--reader-lh);
  overflow-wrap: break-word;
  -webkit-tap-highlight-color: transparent;
}
.chapter-kicker { margin: 18px 0 0; color: var(--muted); font: 600 12px/1.4 var(--ui-font); letter-spacing: 0.06em; text-transform: uppercase; }
.chapter-body { padding-bottom: 1em; touch-action: manipulation; }
.chapter-body > :first-child { margin-top: 0.8em; }
.chapter-body p { margin: 0 0 0.85em; }
.chapter-body h1 { font-size: 1.55em; }
.chapter-body h2 { font-size: 1.32em; }
.chapter-body h3 { font-size: 1.16em; }
.chapter-body h4, .chapter-body h5, .chapter-body h6 { font-size: 1em; }
.chapter-body h1, .chapter-body h2, .chapter-body h3, .chapter-body h4, .chapter-body h5, .chapter-body h6 { margin: 1.4em 0 0.6em; line-height: 1.25; }
.chapter-body img { max-width: 100%; height: auto; border-radius: 3px; }
.chapter-body figure { margin: 1.4em 0; text-align: center; }
.chapter-body figure img,
.chapter-body div > img:only-child,
.chapter-body p > img:only-child { display: block; margin: 0.6em auto; }
.chapter-body figcaption { margin-top: 6px; color: var(--muted); font-size: 0.82em; line-height: 1.45; }
.chapter-body blockquote { margin: 1.2em 0; padding: 0.1em 0 0.1em 1em; border-left: 3px solid var(--border); }
.chapter-body pre { overflow-x: auto; padding: 0.9em 1em; border-radius: 8px; background: var(--surface-2); font-size: 0.82em; line-height: 1.5; white-space: pre-wrap; }
.chapter-body code { font-family: ui-monospace, 'Cascadia Code', Menlo, Consolas, monospace; font-size: 0.88em; }
.chapter-body table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; font-size: 0.9em; }
.chapter-body th, .chapter-body td { padding: 6px 10px; border: 1px solid var(--border); text-align: left; vertical-align: top; }
.chapter-body hr { margin: 2em auto; width: 30%; border: 0; border-top: 1px solid var(--border); }
.chapter-body ul, .chapter-body ol { padding-left: 1.4em; }
.chapter-body sup, .chapter-body sub { line-height: 0; }
.chapter-body a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 0.15em; }
.chapter-body mark:not(.bm) { background: var(--hl); color: inherit; }
.chapter-body .gap { height: 1em; }

/* "Book's formatting" off: plain text, without the book's bold, italics, small caps or alignment.
   Headings keep the reader's own bold. */
.plain-book .reader :is(b, strong, .b):not(h1, h2, h3, h4, h5, h6) { font-weight: inherit; }
.plain-book .reader :is(i, em, cite, dfn, var, address, .it) { font-style: inherit; }
.plain-book .reader :is(.sc, small) { font-variant: normal; font-size: inherit; }
.plain-book .reader :is(u, ins) { text-decoration: none; }
.plain-book .reader :is(.ta-center, .ta-right) { text-align: inherit; }
.chapter-body .ta-center { text-align: center; }
.chapter-body .ta-right { text-align: right; }
.chapter-body .it { font-style: italic; }
.chapter-body .b { font-weight: 700; }
.chapter-body .sc { font-variant: small-caps; }
.chapter-body .ta-center img { margin-left: auto; margin-right: auto; }

/* bookmarked words */
.chapter-body mark.bm {
  border-radius: 0.2em;
  background: var(--mark);
  box-shadow: 0 0 0 0.12em var(--mark);
  color: inherit;
  cursor: pointer;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}
.chapter-body mark.bm-first { position: relative; }
.chapter-body mark.bm-first::after {
  content: '';
  position: absolute;
  top: -0.42em;
  left: 50%;
  width: 0.42em;
  height: 0.58em;
  background: var(--accent);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 72%, 0 100%);
  transform: translateX(-50%);
  pointer-events: none;
}
.chapter-body mark.bm-new { animation: pop 0.35s ease-out; }
.chapter-body mark.flash { animation: flash 1.2s ease-out; }
@keyframes pop { 0% { box-shadow: 0 0 0 0.5em transparent; } 40% { box-shadow: 0 0 0 0.35em var(--mark); } 100% { box-shadow: 0 0 0 0.12em var(--mark); } }
@keyframes flash { 0%, 60% { background: var(--accent); color: var(--accent-text); } }

/* read-aloud highlighting */
.tts-seg.tts-current {
  background: var(--hl);
  color: var(--text);
  border-radius: 0.2em;
  box-shadow: 0 0 0 0.12em var(--hl);
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}
::highlight(tts-word) { background-color: var(--hl-word, #ffd23f); color: var(--text, inherit); }
.play-btn.on { box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent); }

.chapter-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  max-width: 720px;
  margin: 8px auto 0;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.cn-link {
  display: grid;
  gap: 2px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}
.cn-link:hover { border-color: var(--accent); }
.cn-next { text-align: right; }
.cn-dir { display: inline-flex; align-items: center; gap: 4px; color: var(--accent); font-size: 13px; font-weight: 650; }
.cn-next .cn-dir { justify-content: flex-end; }
.cn-title { display: -webkit-box; overflow: hidden; font-size: 14px; font-weight: 550; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; }

/* the popover shown on a bookmarked word */
.bm-pop {
  position: fixed;
  z-index: 40;
  display: grid;
  gap: 10px;
  width: max-content;
  max-width: min(340px, calc(100vw - 16px));
  padding: 10px 10px 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-family: var(--ui-font);
}
.bp-head { display: flex; align-items: center; gap: 8px; color: var(--accent); }
.bp-head .icon-btn { width: 32px; height: 32px; margin-left: auto; color: var(--muted); }
.bp-word { min-width: 0; overflow: hidden; color: var(--text); font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.bp-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  z-index: 60;
  max-width: min(92vw, 480px);
  padding: 10px 18px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  font-weight: 550;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  overflow-wrap: anywhere;
  transform: translate(-50%, 12px);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- sign in / register ---------- */

.auth { display: grid; place-items: center; min-height: 100vh; min-height: 100dvh; padding: 24px 16px; }
.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 28px 24px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.auth-brand { display: flex; align-items: center; gap: 8px; color: var(--accent); font-size: 20px; font-weight: 700; }
.auth-brand span { color: var(--text); }
.auth-card h1 { margin: 18px 0 16px; font-size: 24px; }
.auth-note { margin: 14px 0 0; color: var(--muted); font-size: 13px; text-align: center; }
.auth-alt { margin: 18px 0 0; text-align: center; font-size: 14px; }
.alert {
  margin: 0 0 16px;
  padding: 10px 14px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  font-size: 14px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
