:root {
  --bg: #f5efe6;
  --surface: #fffaf3;
  --surface-2: #efe6d9;
  --ink: #2a241d;
  --muted: #7b6f60;
  --line: #e2d7c7;
  --accent: #b84f24;
  --accent-ink: #ffffff;
  --accent-soft: rgba(184, 79, 36, 0.14);
  --hl: rgba(232, 160, 70, 0.32);
  --shadow: 0 10px 30px rgba(60, 40, 20, 0.12);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, "Helvetica Neue", sans-serif;
  --player-h: 132px;
  --top-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15120f;
    --surface: #1e1a16;
    --surface-2: #2a241e;
    --ink: #ede5d8;
    --muted: #a0937f;
    --line: #342d25;
    --accent: #e27a4a;
    --accent-ink: #1a120c;
    --accent-soft: rgba(226, 122, 74, 0.16);
    --hl: rgba(226, 150, 74, 0.28);
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}
body.has-player { padding-bottom: calc(var(--player-h) + env(safe-area-inset-bottom)); }
[hidden] { display: none !important; }
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg .fill { fill: currentColor; stroke: none; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: #c0392b; background: rgba(192, 57, 43, 0.08); padding: 10px 14px; border-radius: 10px; }
.section-label { font: 600 0.75rem/1 var(--sans); letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin: 0 0 12px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--top-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font: 700 1.15rem/1 var(--serif); color: var(--ink); text-decoration: none; margin-right: auto; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; }
.brand-mark svg { width: 22px; height: 22px; stroke: #fff; stroke-width: 2.6; }
.icon-btn.drawer-btn { display: none; }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 16px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 600; font-size: 0.92rem; cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { border-color: var(--accent); }
.btn svg { width: 18px; height: 18px; }
.btn-ghost { background: transparent; }
.btn-small { padding: 6px 12px; font-size: 0.82rem; }
.link-btn { background: none; border: 0; padding: 0 0 0 8px; color: var(--accent); font-weight: 600; text-decoration: underline; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  display: inline-grid; place-items: center;
  border: 0; background: transparent; color: var(--ink);
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:disabled { opacity: 0.35; cursor: default; background: transparent; }

/* Home */
.home { max-width: 980px; margin: 0 auto; padding: 56px 20px 64px; }
.hero { max-width: 640px; margin: 0 auto; text-align: center; }
.hero h1 { font: 700 clamp(2rem, 5vw, 3rem)/1.1 var(--serif); margin: 0 0 14px; letter-spacing: -0.01em; }
.lede { font-size: 1.1rem; color: var(--muted); margin: 0 0 32px; }
.drop {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 44px 24px;
  border: 2px dashed var(--line); border-radius: 20px;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.15s;
}
.drop:hover, .drop:focus-visible, .drop.over { border-color: var(--accent); background: var(--accent-soft); outline: none; }
.drop-icon { width: 52px; height: 52px; stroke: var(--accent); stroke-width: 1.8; margin-bottom: 6px; }
.drop strong { font-size: 1.15rem; }
.drop span { color: var(--muted); }
.drop.busy { cursor: progress; }
.drop-bar { width: 60%; height: 4px; border-radius: 4px; background: var(--line); overflow: hidden; margin-top: 12px; }
.drop-bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }
.hero .error { margin-top: 16px; text-align: left; }

.library-section { margin-top: 56px; }
.library { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 24px; }
.book-card { position: relative; display: flex; flex-direction: column; gap: 8px; text-decoration: none; color: inherit; }
.book-card .cover { width: 100%; aspect-ratio: 2 / 3; box-shadow: var(--shadow); transition: transform 0.2s; }
.book-card:hover .cover { transform: translateY(-3px); }
.book-card .t { font-weight: 600; font-size: 0.92rem; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book-card .a { font-size: 0.82rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-card .progress { height: 3px; border-radius: 3px; background: var(--line); overflow: hidden; }
.book-card .progress i { display: block; height: 100%; background: var(--accent); }
.book-card .remove {
  position: absolute; top: 6px; right: 6px;
  width: 28px; height: 28px; border-radius: 50%; border: 0;
  background: rgba(0, 0, 0, 0.55); color: #fff; font-size: 16px; line-height: 1;
  opacity: 0; transition: opacity 0.15s;
}
.book-card:hover .remove, .book-card .remove:focus-visible { opacity: 1; }

/* Covers */
.cover { border-radius: 6px; overflow: hidden; background: var(--surface-2); flex: none; }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover .ph {
  width: 100%; height: 100%; padding: 12% 10%;
  display: flex; flex-direction: column; justify-content: space-between;
  color: #fff; font: 600 0.95rem/1.2 var(--serif);
  background: linear-gradient(160deg, var(--ph1, #8a4b2a), var(--ph2, #3b2a1e));
}
.cover .ph small { font: 500 0.7rem/1.2 var(--sans); opacity: 0.85; }

/* Book view */
.book {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  min-height: calc(100vh - var(--top-h) - var(--player-h));
}
.sidebar {
  position: sticky; top: var(--top-h);
  height: calc(100vh - var(--top-h) - var(--player-h));
  overflow-y: auto;
  padding: 24px 16px 24px 20px;
  border-right: 1px solid var(--line);
  background: var(--bg);
}
.book-meta { display: flex; gap: 14px; margin-bottom: 26px; align-items: flex-start; }
.book-meta .cover { width: 84px; aspect-ratio: 2 / 3; box-shadow: var(--shadow); }
.book-meta .cover .ph { font-size: 0.7rem; }
.book-meta h2 { font: 700 1.1rem/1.25 var(--serif); margin: 2px 0 4px; }
.book-meta p { margin: 0 0 4px; }
.chapters { list-style: none; margin: 0; padding: 0; }
.chapters button {
  width: 100%; display: flex; gap: 10px; align-items: baseline; justify-content: space-between;
  text-align: left; padding: 8px 10px; border: 0; border-radius: 8px;
  background: transparent; font-size: 0.92rem; line-height: 1.35;
}
.chapters button:hover { background: var(--surface-2); }
.chapters .d1 button { padding-left: 24px; }
.chapters .d2 button, .chapters .d3 button { padding-left: 38px; font-size: 0.87rem; }
.chapters .dur { color: var(--muted); font-size: 0.78rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.chapters .active button { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chapters .active .dur { color: var(--accent); }
.scrim { display: none; }

.reader { padding: 40px 24px 80px; min-width: 0; }
.reader-head, .reader-text, .reader-hint { max-width: 40rem; margin-left: auto; margin-right: auto; }
.reader-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.eyebrow { margin: 0; font: 600 0.78rem/1.3 var(--sans); letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.reader-text { font: 1.2rem/1.75 var(--serif); }
.reader-text p { margin: 0 0 1em; }
.reader-text h1, .reader-text h2 { font: 700 1.7rem/1.25 var(--serif); margin: 0.2em 0 0.9em; }
.reader-text .toc-title { color: var(--ink); }
.seg { border-radius: 4px; cursor: pointer; transition: background 0.2s; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
.seg:hover { background: var(--surface-2); }
.seg.on { background: var(--hl); }
.reader-hint { margin-top: 32px; text-align: center; }

/* Player */
.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: var(--surface);
  border-top: 1px solid var(--line);
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.06);
  padding: 10px 20px calc(12px + env(safe-area-inset-bottom));
}
.status {
  position: absolute; left: 50%; bottom: calc(100% + 10px); transform: translateX(-50%);
  max-width: min(92vw, 640px);
  background: var(--ink); color: var(--bg);
  padding: 8px 14px; border-radius: 10px; font-size: 0.85rem;
  box-shadow: var(--shadow);
}
.status.err { background: #c0392b; color: #fff; }
.status .link-btn { color: inherit; }
.scrub { display: flex; align-items: center; gap: 12px; }
.time { font-size: 0.78rem; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 54px; }
.time.right { text-align: right; }
input[type=range] {
  -webkit-appearance: none; appearance: none;
  flex: 1; height: 20px; background: transparent; margin: 0; cursor: pointer;
  --pct: 0%;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 5px; border-radius: 5px;
  background: linear-gradient(to right, var(--accent) var(--pct), var(--line) var(--pct));
}
input[type=range]::-moz-range-track { height: 5px; border-radius: 5px; background: var(--line); }
input[type=range]::-moz-range-progress { height: 5px; border-radius: 5px; background: var(--accent); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 15px; height: 15px; margin-top: -5px;
  border-radius: 50%; background: var(--accent); border: 2px solid var(--surface);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}
input[type=range]::-moz-range-thumb { width: 13px; height: 13px; border-radius: 50%; background: var(--accent); border: 2px solid var(--surface); }

.controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 16px; margin-top: 6px; }
.now { display: flex; align-items: center; gap: 12px; min-width: 0; text-decoration: none; color: inherit; }
.mini-cover { width: 40px; height: 56px; border-radius: 4px; }
.mini-cover .ph { padding: 4px; font-size: 0.45rem; }
.now-text { min-width: 0; }
.now-chapter { font-weight: 600; font-size: 0.92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.now-text .small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.transport { display: flex; align-items: center; gap: 6px; }
.skip { position: relative; }
.skip svg { width: 30px; height: 30px; stroke-width: 1.6; }
.skip span { position: absolute; inset: 0; display: grid; place-items: center; font-size: 0.6rem; font-weight: 700; padding-top: 1px; }
.play-btn {
  position: relative;
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; margin: 0 6px;
  box-shadow: 0 4px 14px var(--accent-soft);
  transition: transform 0.1s;
}
.play-btn:active { transform: scale(0.95); }
.play-btn svg { width: 26px; height: 26px; grid-area: 1 / 1; }
.play-btn .i-pause, .playing .play-btn .i-play { display: none; }
.playing .play-btn .i-pause { display: block; }
.play-btn .spinner {
  grid-area: 1 / 1; width: 62px; height: 62px; border-radius: 50%;
  border: 3px solid transparent; border-top-color: var(--accent);
  position: absolute; inset: -3px;
  display: none; animation: spin 0.9s linear infinite;
}
.buffering .play-btn .spinner { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }

.extras { display: flex; justify-content: flex-end; gap: 8px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--bg);
  font-weight: 700; font-size: 0.88rem; font-variant-numeric: tabular-nums;
}
.pill:hover, .pill[aria-expanded=true] { border-color: var(--accent); color: var(--accent); }
.pill svg { width: 17px; height: 17px; }
#voiceLabel { max-width: 140px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.pop-wrap { position: relative; }
.popover {
  position: absolute; right: 0; bottom: calc(100% + 12px);
  width: 250px; padding: 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow); z-index: 50;
}
.popover.wide { width: 360px; }
.pop-title { margin: 0 0 12px; font-weight: 700; }
.chips { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.chips button {
  padding: 8px 0; border-radius: 8px; border: 1px solid var(--line); background: var(--bg);
  font-weight: 600; font-size: 0.88rem; font-variant-numeric: tabular-nums;
}
.chips button:hover { border-color: var(--accent); }
.chips button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.engine-pick { display: grid; gap: 8px; margin-bottom: 14px; }
.engine-pick button {
  text-align: left; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg);
  display: flex; flex-direction: column; gap: 2px;
}
.engine-pick small { color: var(--muted); font-size: 0.78rem; line-height: 1.35; }
.engine-pick button.on { border-color: var(--accent); background: var(--accent-soft); }
.engine-pick button.on strong { color: var(--accent); }
.field-group { display: grid; gap: 10px; }
.field-group p { margin: 0; }
.field { display: grid; gap: 4px; font-size: 0.82rem; font-weight: 600; color: var(--muted); }
.field select {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
  font-weight: 500; font-size: 0.9rem;
}

.drag-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(4px);
  pointer-events: none;
}
.drag-overlay div {
  padding: 40px 56px; border: 3px dashed var(--accent); border-radius: 24px;
  font: 700 1.4rem var(--serif); color: var(--accent); background: var(--surface);
}

/* Narrow screens */
@media (max-width: 860px) {
  :root { --player-h: 150px; }
  .topbar { padding: 0 12px; }
  .icon-btn.drawer-btn { display: inline-grid; }
  body:not(.in-book) .icon-btn.drawer-btn { display: none; }
  .book { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; z-index: 35; left: 0; top: var(--top-h); bottom: 0;
    width: min(86vw, 340px); height: auto;
    padding-bottom: calc(var(--player-h) + 24px);
    transform: translateX(-102%); transition: transform 0.25s ease;
    box-shadow: var(--shadow);
  }
  .drawer-open .sidebar { transform: none; }
  .drawer-open .scrim { display: block; position: fixed; inset: var(--top-h) 0 0 0; z-index: 34; background: rgba(0, 0, 0, 0.35); }
  .reader { padding: 24px 18px 60px; }
  .reader-text { font-size: 1.1rem; }
  .player { padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); }
  .controls { grid-template-columns: 1fr auto; grid-template-areas: "now extras" "transport transport"; gap: 4px 10px; }
  .now { grid-area: now; }
  .extras { grid-area: extras; }
  .transport { grid-area: transport; justify-content: center; }
  .mini-cover { width: 30px; height: 42px; }
  #voiceLabel { display: none; }
  .popover.wide { width: min(360px, calc(100vw - 24px)); }
  .btn span { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation-duration: 0s !important; }
}

/* Sync dialog */
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: 0.5; cursor: default; }
#syncBtn.busy #syncLabel { color: var(--accent); font-variant-numeric: tabular-nums; }
.dialog {
  width: min(460px, calc(100vw - 24px)); max-height: calc(100vh - 40px);
  padding: 24px; border: 1px solid var(--line); border-radius: 18px;
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.45); backdrop-filter: blur(2px); }
.dialog h2 { font: 700 1.35rem/1.2 var(--serif); margin: 0 0 12px; }
.dialog h3 { font-size: 0.95rem; margin: 0 0 8px; }
.dialog p { margin: 0 0 12px; }
.dialog .field { margin-bottom: 12px; }
.dialog input[type=url] {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink); font-size: 0.9rem;
}
.dlg-x { position: absolute; top: 12px; right: 12px; }
.dlg-sec { border-top: 1px solid var(--line); padding-top: 16px; margin-top: 16px; }
.ok-line { display: flex; align-items: center; gap: 8px; font-size: 0.92rem; }
.ok-line .dot { width: 9px; height: 9px; border-radius: 50%; background: #3fa35b; box-shadow: 0 0 0 3px rgba(63, 163, 91, 0.2); }
.meter { height: 6px; border-radius: 6px; background: var(--line); overflow: hidden; margin: 0 0 12px; }
.meter i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.4s; }
.qr-row { display: flex; gap: 16px; align-items: center; }
.qr { flex: none; width: 132px; height: 132px; background: #fff; border-radius: 10px; padding: 6px; }
.qr svg { width: 100%; height: 100%; display: block; }
.dialog > .link-btn { margin-top: 16px; padding: 0; color: var(--muted); }
.book-card .cloud-badge {
  position: absolute; top: 6px; left: 6px; padding: 2px 7px; border-radius: 999px;
  background: rgba(0, 0, 0, 0.55); color: #fff; font-size: 0.68rem; font-weight: 600;
}
@media (max-width: 860px) {
  .qr-row { flex-direction: column; align-items: flex-start; }
  #syncBtn span { display: none; }
}
