:root {
  --paper: #f4efe6;
  --card: #fbf8f2;
  --ink: #26221e;
  --muted: #7a7067;
  --line: #e2d9cb;
  --accent: #9a3b2e;
  --accent-ink: #fff;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --reader-bg: #fbf8f2;
  --reader-fg: #26221e;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --paper: #18171b; --card: #222126; --ink: #ece7df; --muted: #9c948a; --line: #34323a; --accent: #d9846f; --accent-ink: #1a1012; color-scheme: dark; }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--paper); color: var(--ink); font: 15px/1.45 var(--sans); -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
a { color: inherit; text-decoration: none; }
code { font-size: .9em; background: var(--line); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: #c0392b; margin: 0; }

.wordmark { font: 600 26px/1 var(--serif); letter-spacing: -.01em; }
.wordmark::before { content: ""; display: inline-block; width: .55em; height: .8em; margin-right: .35em; border-radius: 2px 4px 4px 2px; background: var(--accent); vertical-align: -.04em; box-shadow: inset 3px 0 0 rgba(0,0,0,.18); }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; height: 38px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--ink); font: 500 14px var(--sans); cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.icon { width: 38px; padding: 0; justify-content: center; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
input[type=search], input[type=password], dialog input { height: 38px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--ink); font: 15px var(--sans); outline: none; }
input:focus { border-color: var(--accent); }

/* ---------- login ---------- */
#login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(360px, 100%); display: grid; gap: 14px; text-align: center; }
.login-card .wordmark { font-size: 34px; margin-bottom: 4px; }

/* ---------- library ---------- */
.lib-header { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: calc(14px + env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) 14px max(20px, env(safe-area-inset-left)); background: color-mix(in srgb, var(--paper) 88%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-bottom: 1px solid var(--line); }
.lib-actions { display: flex; gap: 8px; align-items: center; flex: 1; justify-content: flex-end; }
#search { width: min(280px, 100%); }
.lib-main { max-width: 1180px; margin: 0 auto; padding: 24px max(20px, env(safe-area-inset-left)) 60px; }
.section-title { font: 600 13px var(--sans); text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 8px 0 16px; }

.continue-card { display: flex; gap: 24px; padding: 22px; margin-bottom: 34px; background: var(--card); border: 1px solid var(--line); border-radius: 16px; align-items: center; }
.continue-card .cover { width: 120px; flex: none; }
.continue-info { display: grid; gap: 6px; min-width: 0; flex: 1; justify-items: start; }
.continue-info h2 { margin: 0; font: 600 26px/1.15 var(--serif); }
.continue-info .chapter { font-style: italic; color: var(--muted); font-family: var(--serif); }
.eyebrow { font: 600 12px var(--sans); text-transform: uppercase; letter-spacing: .1em; color: var(--accent); }
.bar-track { width: min(320px, 100%); height: 5px; border-radius: 3px; background: var(--line); overflow: hidden; margin-top: 6px; }
.bar-track > div { height: 100%; background: var(--accent); }
.continue-info .btn { margin-top: 8px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 28px 22px; }
.book { display: grid; gap: 3px; align-content: start; }
.book:hover .cover { transform: translateY(-3px); box-shadow: 0 10px 24px rgba(40,25,10,.22); }
.cover { position: relative; aspect-ratio: 2 / 3; border-radius: 4px 8px 8px 4px; overflow: hidden; background: var(--line); box-shadow: 0 3px 10px rgba(40,25,10,.16); transition: transform .18s, box-shadow .18s; margin-bottom: 8px; }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover::after { content: ""; position: absolute; inset: 0 auto 0 0; width: 6px; background: linear-gradient(90deg, rgba(0,0,0,.18), transparent); pointer-events: none; }
.cover-progress { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(0,0,0,.25); }
.cover-progress > div { height: 100%; background: var(--accent); }
.ph-cover { --h: 20; width: 100%; height: 100%; display: flex; flex-direction: column; justify-content: center; gap: 8px; padding: 14px; text-align: center; color: #fff; background: linear-gradient(160deg, hsl(var(--h) 38% 38%), hsl(calc(var(--h) + 30) 42% 24%)); font-family: var(--serif); }
.ph-cover span { font-size: 15px; font-weight: 600; line-height: 1.2; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.ph-cover small { font-size: 11px; opacity: .8; }
.book-title { font: 600 14px/1.3 var(--sans); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book-author { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-meta { font-size: 12px; color: var(--muted); }
.empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty h2 { font: 600 24px var(--serif); color: var(--ink); }

@media (max-width: 600px) {
  .lib-header { flex-wrap: wrap; }
  .lib-actions { width: 100%; }
  #search { flex: 1; width: auto; }
  .lib-actions .btn span { display: none; }
  .lib-actions label.btn { width: 38px; padding: 0; justify-content: center; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 22px 14px; }
  .continue-card { padding: 16px; gap: 16px; }
  .continue-card .cover { width: 84px; }
  .continue-info h2 { font-size: 20px; }
}

/* ---------- reader ---------- */
body[data-view=reader] { background: var(--reader-bg); color: var(--reader-fg); overflow: hidden; }
#reader { position: fixed; inset: 0; background: var(--reader-bg); color: var(--reader-fg); }
#viewer { position: absolute; top: calc(44px + env(safe-area-inset-top)); bottom: calc(40px + env(safe-area-inset-bottom)); left: max(5vw, 16px, env(safe-area-inset-left)); right: max(5vw, 16px, env(safe-area-inset-right)); }
@media (min-width: 960px) { #viewer { left: 7vw; right: 7vw; } }
.reader-loading { position: absolute; inset: 0; display: grid; place-items: center; font: italic 18px var(--serif); opacity: .6; }

.bar { position: absolute; left: 0; right: 0; display: flex; align-items: center; gap: 4px; padding: 0 max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left)); transition: opacity .2s, transform .2s; z-index: 3; }
.bar .btn { color: var(--reader-fg); }
.bar.top { top: 0; height: calc(48px + env(safe-area-inset-top)); padding-top: env(safe-area-inset-top); background: var(--reader-bg); border-bottom: 1px solid color-mix(in srgb, var(--reader-fg) 12%, transparent); }
.bar-title { flex: 1; text-align: center; font: 600 14px var(--sans); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 8px; }
.bar.bottom { bottom: 0; height: calc(38px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); justify-content: space-between; font-size: 12px; opacity: .55; pointer-events: none; }
.bar-chapter { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--serif); font-style: italic; padding-left: 8px; }
.bar-percent { padding-right: 8px; font-variant-numeric: tabular-nums; }
body:not(.chrome) .bar.top { opacity: 0; transform: translateY(-100%); pointer-events: none; }
.progress-line { position: absolute; left: 0; right: 0; bottom: 0; height: 2px; background: color-mix(in srgb, var(--reader-fg) 8%, transparent); z-index: 3; }
#progress-fill { height: 100%; background: var(--accent); width: 0; transition: width .3s; }
body[data-theme=dark] { --accent: #d9846f; }

.panel { position: absolute; top: 0; bottom: 0; left: 0; width: min(360px, 88vw); z-index: 6; background: var(--reader-bg); color: var(--reader-fg); border-right: 1px solid color-mix(in srgb, var(--reader-fg) 14%, transparent); box-shadow: 8px 0 30px rgba(0,0,0,.18); display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 8px 20px; }
.panel-head h3 { margin: 0; font: 600 18px var(--serif); }
.panel-head .btn { color: var(--reader-fg); }
#toc-list { overflow-y: auto; padding: 0 0 calc(20px + env(safe-area-inset-bottom)); }
#toc-list a { display: block; padding: 9px 20px 9px calc(20px + var(--d) * 16px); font-family: var(--serif); font-size: 15px; border-bottom: 1px solid color-mix(in srgb, var(--reader-fg) 7%, transparent); }
#toc-list a:hover { background: color-mix(in srgb, var(--reader-fg) 6%, transparent); }
#toc-list p { padding: 0 20px; }

.popover { position: absolute; top: calc(52px + env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right)); width: min(320px, calc(100vw - 20px)); z-index: 6; background: var(--reader-bg); color: var(--reader-fg); border: 1px solid color-mix(in srgb, var(--reader-fg) 16%, transparent); border-radius: 14px; box-shadow: 0 12px 40px rgba(0,0,0,.22); padding: 8px 16px 12px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid color-mix(in srgb, var(--reader-fg) 9%, transparent); }
.popover .btn { background: transparent; color: var(--reader-fg); border-color: color-mix(in srgb, var(--reader-fg) 20%, transparent); height: 34px; }
.stepper { display: flex; align-items: center; gap: 8px; }
#font-size-label, #lh-label { min-width: 42px; text-align: center; font-variant-numeric: tabular-nums; font-size: 13px; }
.seg { display: flex; gap: 6px; }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 1px solid rgba(0,0,0,.2); cursor: pointer; }
.swatch.light { background: #fbf8f2; } .swatch.sepia { background: #f1e6cf; } .swatch.dark { background: #16151a; }
.swatch.on { outline: 2px solid var(--accent); outline-offset: 2px; }
.seg.text button { font-size: 12px; padding: 0 9px; }
.seg.text button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.popover p { margin: 10px 0 0; opacity: .7; }

/* ---------- dialog & toast ---------- */
dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--card); color: var(--ink); padding: 0; width: min(380px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.dialog-body { display: grid; gap: 10px; padding: 20px; }
.dialog-body h3 { margin: 0; font: 600 20px var(--serif); }
.dialog-body p { margin: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.toast { position: fixed; left: 50%; bottom: calc(52px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20; display: flex; align-items: center; gap: 10px; max-width: calc(100vw - 24px); padding: 10px 10px 10px 16px; border-radius: 12px; background: #2a2620; color: #f4efe6; font-size: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.25); animation: toast-in .2s ease-out; }
.toast .btn { color: #f0b9a6; height: 30px; font-weight: 600; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
.popover .btn:disabled { opacity: .35; cursor: default; }

/* Offline */
.offline-status { margin: 0 0 18px; font-size: 13px; color: var(--muted, #7a6f63); display: flex; align-items: center; gap: 8px; }
.offline-status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #5b8a5a; flex: none; }
.offline-status.offline::before { background: #c08a2e; }
.book.unavailable { opacity: .4; }
