:root {
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --reader-scale: 1;
  --topbar-h: 56px;
  --player-h: 120px;
}

/* Themes */
:root[data-theme="sepia"] {
  --bg: #f4ecd8; --bg-soft: #ece2c9; --surface: #fbf5e6;
  --text: #43392b; --muted: #8a7c63; --accent: #9a6b3f; --accent-contrast:#fff;
  --hl-bg: #f6e2b3; --hl-text: #38301f; --border: #e0d3b4; --shadow: rgba(90,70,40,.16);
}
:root[data-theme="light"] {
  --bg: #ffffff; --bg-soft: #f2f2f5; --surface: #ffffff;
  --text: #1c1c1e; --muted: #86868b; --accent: #b1591f; --accent-contrast:#fff;
  --hl-bg: #fff1cc; --hl-text: #1c1c1e; --border: #e5e5ea; --shadow: rgba(0,0,0,.12);
}
:root[data-theme="dark"] {
  --bg: #1f2023; --bg-soft: #292a2e; --surface: #2a2b2f;
  --text: #e7e3d9; --muted: #9b978c; --accent: #d9a066; --accent-contrast:#26210f;
  --hl-bg: #463a24; --hl-text: #fff5df; --border: #3a3b40; --shadow: rgba(0,0,0,.4);
}
:root[data-theme="night"] {
  --bg: #000000; --bg-soft: #0d0d0f; --surface: #101012;
  --text: #cfc9bd; --muted: #7c786e; --accent: #c9945a; --accent-contrast:#1a1409;
  --hl-bg: #2b2213; --hl-text: #f5e6c8; --border: #1d1d20; --shadow: rgba(0,0,0,.6);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-serif);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  transition: background .3s ease, color .3s ease;
}

/* Top bar */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 6px;
  padding: 0 8px; padding-top: env(safe-area-inset-top);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}
.topbar-spacer { flex: 1; min-width: 0; }
.muted { color: var(--muted); }

.icon-btn {
  appearance: none; border: none; background: transparent; color: var(--text);
  width: 42px; height: 42px; border-radius: 12px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s ease, opacity .15s ease;
}
.icon-btn:hover { background: var(--bg-soft); }
.icon-btn:active { transform: scale(.93); }
.icon-btn:disabled { opacity: .3; cursor: default; }

/* Reader */
.reader {
  padding: calc(var(--topbar-h) + 30px) 22px calc(var(--player-h) + 44px);
  max-width: 720px; margin: 0 auto;
  transition: padding-bottom .28s cubic-bezier(.4,0,.2,1);
}
html.player-collapsed .reader {
  padding-bottom: calc(52px + env(safe-area-inset-bottom));
}
.content {
  font-size: calc(20px * var(--reader-scale));
  line-height: 1.75;
  letter-spacing: .002em;
}

/* Book opener */
.opener { text-align: center; margin: 6px 0 34px; }
.opener .kicker {
  font-family: var(--font-sans); font-size: 12px; letter-spacing: .22em;
  text-transform: uppercase; color: var(--accent); margin-bottom: 14px;
}
.opener .head {
  display: block; margin: 0; font-weight: 600;
  font-size: calc(28px * var(--reader-scale)); line-height: 1.2;
  letter-spacing: .01em;
}
.opener .head.sub {
  font-weight: 400; font-style: italic; color: var(--muted);
  font-size: calc(16px * var(--reader-scale)); margin-top: 10px; letter-spacing: 0;
}
.opener .rule { width: 56px; height: 2px; background: var(--accent); opacity: .5; margin: 22px auto 0; border-radius: 2px; }

.prose p { margin: 0 0 1.15em; text-align: justify; -webkit-hyphens: auto; hyphens: auto; }

/* Sentence + highlight */
.sent { transition: background-color .15s ease, color .15s ease; border-radius: 4px; padding: 0 1px; cursor: pointer; }
.sent.active { background: var(--hl-bg); color: var(--hl-text); box-shadow: 0 0 0 3px var(--hl-bg); }
.head.active { color: var(--accent); }

/* End-of-book nav */
.end-nav { display: flex; justify-content: center; margin-top: 34px; }
.end-nav button {
  font-family: var(--font-sans); font-size: 14px; color: var(--accent);
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  padding: 11px 22px; cursor: pointer;
}
.end-nav button:hover { background: var(--bg-soft); }

/* Drawers */
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(90vw, 360px);
  background: var(--surface); z-index: 50; transform: translateX(-102%);
  transition: transform .28s cubic-bezier(.4,0,.2,1);
  display: flex; flex-direction: column; box-shadow: 0 0 40px var(--shadow);
  padding-top: env(safe-area-inset-top);
}
.drawer.drawer-right { left: auto; right: 0; transform: translateX(102%); }
.drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 10px 12px 20px; border-bottom: 1px solid var(--border);
}
.drawer-head h2 { margin: 0; font-family: var(--font-sans); font-size: 17px; }

.toc { overflow-y: auto; padding: 6px 0 24px; }
.toc button {
  width: 100%; text-align: left; background: transparent; cursor: pointer;
  border: none; border-left: 3px solid transparent;
  font-family: var(--font-serif); font-size: 17px; color: var(--text);
  padding: 11px 20px 11px 17px; display: flex; flex-direction: column; gap: 3px;
}
.toc button:hover { background: var(--bg-soft); }
.toc button.current { color: var(--accent); font-weight: 600; border-left-color: var(--accent); }
.toc button .sub {
  font-family: var(--font-sans); font-size: 10.5px; font-weight: 500;
  color: var(--muted); letter-spacing: .07em; text-transform: uppercase; line-height: 1.4;
}

.settings-body { overflow-y: auto; padding: 6px 20px 40px; font-family: var(--font-sans); }

/* Grouped setting sections */
.set-group { padding: 18px 0; border-bottom: 1px solid var(--border); }
.set-group:last-child { border-bottom: none; }
.set-heading {
  font-size: 11px; font-weight: 700; letter-spacing: .1em; text-transform: uppercase;
  color: var(--muted); margin: 0 0 14px;
}
.set-row { display: flex; flex-direction: column; gap: 11px; margin: 16px 0; }
.set-row:first-of-type { margin-top: 0; }
.set-row:last-child { margin-bottom: 0; }
.set-row-inline { flex-direction: row; align-items: center; justify-content: space-between; gap: 14px; }
.set-label {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; font-size: 14px; font-weight: 600;
}
.set-label > span { display: inline-flex; align-items: center; gap: 7px; }
.set-label em { font-style: normal; color: var(--muted); font-weight: 500; }
.lock { color: var(--muted); }

/* Range sliders */
input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 6px;
  border-radius: 999px; background: var(--bg-soft); outline: none; cursor: pointer;
  border: 1px solid var(--border);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); border: 3px solid var(--surface);
  box-shadow: 0 1px 5px var(--shadow); cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 22px; height: 22px; border-radius: 50%; background: var(--accent);
  border: 3px solid var(--surface); box-shadow: 0 1px 5px var(--shadow); cursor: pointer;
}

/* Segmented control (theme) */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  gap: 4px; padding: 4px; border-radius: 13px;
  background: var(--bg-soft); border: 1px solid var(--border);
}
.segmented .seg {
  appearance: none; border: none; cursor: pointer; font-family: var(--font-sans);
  font-size: 13px; font-weight: 600; color: var(--muted);
  padding: 9px 6px; border-radius: 9px; background: transparent;
  transition: background .15s ease, color .15s ease, box-shadow .15s ease;
}
.segmented .seg:hover { color: var(--text); }
.segmented .seg.active {
  background: var(--surface); color: var(--accent); box-shadow: 0 1px 4px var(--shadow);
}

/* Toggle switch */
.switch {
  appearance: none; border: none; cursor: pointer; padding: 0; flex: none;
  width: 52px; height: 31px; border-radius: 999px; background: var(--border);
  position: relative; transition: background .2s ease;
}
.switch.on { background: var(--accent); }
.switch-knob {
  position: absolute; top: 3px; left: 3px; width: 25px; height: 25px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.35); transition: transform .2s ease;
}
.switch.on .switch-knob { transform: translateX(21px); }

/* Selects with custom chevron */
.select-wrap { position: relative; }
.select-wrap::after {
  content: ""; position: absolute; right: 15px; top: 50%; width: 8px; height: 8px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: translateY(-70%) rotate(45deg); pointer-events: none;
}
.select-wrap select {
  -webkit-appearance: none; appearance: none; width: 100%;
  padding: 12px 38px 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--bg-soft);
  color: var(--text); font-family: var(--font-sans); font-size: 14px; cursor: pointer;
  transition: border-color .15s ease;
}
.select-wrap select:focus { outline: none; border-color: var(--accent); }

.picture-options { margin-top: 16px; }
.hint { font-size: 12px; color: var(--muted); line-height: 1.55; margin: 14px 0 0; }
.hint code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px;
  background: var(--bg-soft); border: 1px solid var(--border);
  padding: 1px 5px; border-radius: 5px;
}

/* Password modal */
.modal {
  position: fixed; inset: 0; z-index: 60; display: flex;
  align-items: center; justify-content: center; padding: 24px;
  background: rgba(0,0,0,.5); animation: fade .2s ease;
}
.modal-card {
  width: min(92vw, 360px); background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 20px; padding: 26px 24px;
  box-shadow: 0 24px 70px var(--shadow); font-family: var(--font-sans);
  text-align: center; animation: modalPop .22s cubic-bezier(.2,.8,.3,1);
}
@keyframes modalPop { from { transform: translateY(12px) scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-icon {
  width: 52px; height: 52px; margin: 0 auto 14px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent);
}
.modal-card h3 { margin: 0 0 6px; font-size: 19px; }
.modal-sub { margin: 0 0 18px; font-size: 13px; color: var(--muted); line-height: 1.5; }
.modal-input {
  width: 100%; padding: 13px 15px; border-radius: 12px; text-align: center;
  border: 1px solid var(--border); background: var(--bg-soft); color: var(--text);
  font-family: var(--font-sans); font-size: 15px;
}
.modal-input:focus { outline: none; border-color: var(--accent); }
.modal-error { color: #c0392b; font-size: 12.5px; margin-top: 10px; }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.btn {
  flex: 1; appearance: none; border: none; cursor: pointer; font-family: var(--font-sans);
  font-size: 14px; font-weight: 600; padding: 12px 18px; border-radius: 12px;
  transition: transform .1s ease, filter .15s ease;
}
.btn:active { transform: scale(.96); }
.btn-ghost { background: var(--bg-soft); color: var(--text); border: 1px solid var(--border); }
.btn-primary { background: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { filter: brightness(1.05); }

.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* Player */
.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  backdrop-filter: saturate(140%) blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.player-toggle {
  appearance: none; border: none; cursor: pointer; background: transparent;
  display: flex; align-items: center; justify-content: center;
  width: 100%; padding: 6px 0 2px; color: var(--muted);
}
.player-toggle svg { transition: transform .28s cubic-bezier(.4,0,.2,1); }
.player.collapsed .player-toggle svg { transform: rotate(180deg); }
.player-body {
  overflow: hidden;
  max-height: 220px;
  transition: max-height .28s cubic-bezier(.4,0,.2,1), opacity .2s ease;
}
.player.collapsed .player-body { max-height: 0; opacity: 0; pointer-events: none; }
.progress-wrap { padding: 12px 22px 2px; }
.progress { height: 5px; background: var(--bg-soft); border-radius: 999px; overflow: hidden; cursor: pointer; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); border-radius: 999px; transition: width .25s linear; }
.progress-meta { display: flex; justify-content: space-between; font-family: var(--font-sans); font-size: 11px; margin-top: 6px; }
.controls { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 6px 0 14px; }
.play-btn {
  appearance: none; border: none; cursor: pointer;
  width: 64px; height: 64px; border-radius: 50%;
  background: var(--accent); color: var(--accent-contrast);
  display: inline-flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 18px var(--shadow); transition: transform .12s ease;
}
.play-btn:active { transform: scale(.93); }

@media (max-width: 480px) {
  .reader { padding-left: 18px; padding-right: 18px; }
  .content { font-size: calc(19px * var(--reader-scale)); }
}

/* ---- Picture It mode ---- */
.picture-stage {
  position: fixed; top: var(--topbar-h); right: 0; bottom: var(--player-h);
  width: 56vw; display: none; flex-direction: column;
  padding: 18px 18px 10px; gap: 10px; z-index: 20;
  background: var(--bg-soft); border-left: 1px solid var(--border);
}
body.picture-it .picture-stage { display: flex; }
html.player-collapsed .picture-stage { bottom: 52px; }

.picture-frame {
  position: relative; flex: 1; min-height: 0; border-radius: 16px;
  overflow: hidden; background: color-mix(in srgb, var(--text) 8%, transparent);
  box-shadow: 0 10px 30px var(--shadow);
}
.picture-frame img {
  width: 100%; height: 100%; object-fit: cover; display: block;
  opacity: 0; transition: opacity .5s ease;
}
.picture-frame img.shown { opacity: 1; }

.picture-spinner { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.picture-spinner .spin {
  width: 42px; height: 42px; border-radius: 50%;
  border: 4px solid color-mix(in srgb, var(--text) 20%, transparent);
  border-top-color: var(--accent); animation: pspin 0.9s linear infinite;
}
@keyframes pspin { to { transform: rotate(360deg); } }

.picture-msg {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 24px; font-family: var(--font-sans); font-size: 14px;
  color: var(--muted); line-height: 1.5;
}
.picture-msg.prep {
  background: linear-gradient(120deg,
    color-mix(in srgb, var(--accent) 10%, var(--bg-soft)),
    var(--bg-soft),
    color-mix(in srgb, var(--accent) 10%, var(--bg-soft)));
  background-size: 200% 100%;
  animation: prepShimmer 2.2s ease-in-out infinite;
}
@keyframes prepShimmer { 0% { background-position: 0% 0; } 100% { background-position: -200% 0; } }
.prep-box { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.prep-ring {
  width: 56px; height: 56px; border-radius: 50%;
  border: 5px solid color-mix(in srgb, var(--text) 15%, transparent);
  border-top-color: var(--accent); animation: pspin 0.9s linear infinite;
}
.prep-text { font-size: 16px; font-weight: 600; color: var(--text); }
.prep-sub { font-size: 12px; color: var(--muted); letter-spacing: .02em; }

.picture-hud {
  font-family: var(--font-sans); font-size: 11px; color: var(--muted);
  line-height: 1.5; display: flex; flex-wrap: wrap; gap: 4px 14px;
  min-height: 16px;
}
.picture-hud b { color: var(--text); font-weight: 600; }
.picture-hud .warn { color: var(--accent); }

/* Split reader: text moves left, image fills the right */
body.picture-it .reader {
  max-width: none; width: 44vw; margin: 0;
  padding-left: max(22px, calc((100vw - 44vw - 56vw) / 2 + 22px));
  padding-right: 22px;
}

@media (max-width: 820px) {
  .picture-stage {
    top: var(--topbar-h); left: 0; right: 0; bottom: auto;
    width: auto; height: 42vh; flex-direction: column;
    border-left: none; border-bottom: 1px solid var(--border);
  }
  html.player-collapsed .picture-stage { bottom: auto; }
  body.picture-it .reader {
    width: auto; max-width: 720px; margin: 0 auto;
    padding-top: calc(var(--topbar-h) + 42vh + 20px);
    padding-left: 18px; padding-right: 18px;
  }
  .picture-hud { display: none; }
}

/* In Picture It the prev/next buttons browse images (see app.js). Book skipping
   and seeking stay disabled; forward is enabled only while looking back. */
body.picture-it #nextBook { opacity: .3; pointer-events: none; }
body.picture-it .progress { pointer-events: none; cursor: default; }
body.picture-it .sent { cursor: default; }

/* Badge shown while reviewing earlier images */
.picture-review {
  position: absolute; left: 12px; bottom: 12px; z-index: 3;
  font-family: var(--font-sans); font-size: 12px; font-weight: 600;
  color: #fff; background: color-mix(in srgb, #000 55%, transparent);
  padding: 6px 12px; border-radius: 999px; -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px); pointer-events: none;
}

[hidden] { display: none !important; }
