/* Reader: one calm reading experience. Themes: light / sepia / dark (auto = follow system, resolved in JS). */
:root {
  --bg: #f7f4ef; --paper: #fdfaf5; --ink: #2c2825; --muted: #7a7068; --border: #e2dcd3; --hover: #efe9df;
  --accent: #8b5e3c; --err: #a33a2a; --bar: #fdfaf5; --shade: rgba(0,0,0,.28);
  --serif: 'Iowan Old Style', 'Palatino Linotype', Palatino, 'Book Antiqua', Georgia, 'Noto Serif', 'DejaVu Serif', serif;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --easy: Verdana, Tahoma, 'DejaVu Sans', sans-serif;
  --fs: 19px; --rfont: var(--serif); --lh: 1.62;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
  --rtopH: 26px; --rbotH: 26px;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #efe5cd; --paper: #f4ecd8; --ink: #4f3b2b; --muted: #8a7258; --border: #dccfb0; --hover: #e8dcc0;
  --accent: #8b5e3c; --bar: #f4ecd8;
}
:root[data-theme="dark"] {
  --bg: #1b1917; --paper: #1b1917; --ink: #d8d1c7; --muted: #948a80; --border: #35302c; --hover: #272421;
  --accent: #d0a074; --err: #e0806e; --bar: #1b1917; --shade: rgba(0,0,0,.55); color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #1b1917; --paper: #1b1917; --ink: #d8d1c7; --muted: #948a80; --border: #35302c; --hover: #272421;
    --accent: #d0a074; --err: #e0806e; --bar: #1b1917; --shade: rgba(0,0,0,.55); color-scheme: dark;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { font: 16px/1.45 var(--sans); min-height: 100vh; overflow-x: hidden; }
html.reading, html.reading body { overflow: hidden; height: 100%; overscroll-behavior: none; background: var(--paper); }
[hidden] { display: none !important; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
.err { color: var(--err); }

/* ---------- library ---------- */
#lib .wrap { max-width: 760px; margin: 0 auto; padding: calc(var(--sat) + 14px) max(16px, var(--sal)) calc(var(--sab) + 40px) max(16px, var(--sar)); }
#searchbar { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 2px 0 10px; }
#q { width: 100%; height: 48px; font: inherit; font-size: 17px; color: var(--ink); background: var(--paper); border: 1px solid var(--border);
  border-radius: 12px; padding: 0 16px; -webkit-appearance: none; appearance: none; }
#q::placeholder { color: var(--muted); }
#q:focus { border-color: var(--accent); outline: 0; }
h2.sec { font-size: .78rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 600; margin: 22px 0 6px; }
.crumb { display: flex; align-items: center; gap: 4px; margin: 6px 0 2px; min-height: 44px; }
.crumb a.back { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; height: 44px; margin-left: -12px; font-size: 28px; color: var(--accent); }
.crumb h1 { font-size: 1.25rem; margin: 0; font-weight: 600; line-height: 1.25; }
.sub { color: var(--muted); font-size: .9rem; margin: 0 0 6px; }
.list { list-style: none; margin: 0; padding: 0; }
.row { display: flex; align-items: center; gap: 12px; min-height: 52px; padding: 8px 4px; border-bottom: 1px solid var(--border); }
.row:active, .row:hover { background: var(--hover); }
.row .main { flex: 1; min-width: 0; }
.row .t { display: block; font-size: 1rem; line-height: 1.3; overflow-wrap: anywhere; }
.row .s { display: block; font-size: .85rem; color: var(--muted); line-height: 1.3; margin-top: 1px; overflow-wrap: anywhere; }
.row .p { flex: none; width: 56px; text-align: right; font-size: .78rem; color: var(--muted); }
.crow { display: flex; align-items: stretch; border-bottom: 1px solid var(--border); }
.crow .row { flex: 1; min-width: 0; border-bottom: 0; }
.rmore { flex: none; width: 48px; min-height: 52px; display: flex; align-items: center; justify-content: center; color: var(--muted); border-radius: 10px; }
.rmore svg { fill: currentColor; }
.rmore:hover, .rmore:active, .rmore[aria-expanded="true"] { background: var(--hover); color: var(--ink); }
#cmenu { position: fixed; z-index: 40; min-width: 180px; background: var(--paper); border: 1px solid var(--border); border-radius: 12px; padding: 4px; box-shadow: 0 6px 24px rgba(0,0,0,.18); }
#cmenu button { display: flex; align-items: center; width: 100%; min-height: 48px; padding: 0 14px; border-radius: 8px; font-size: 1rem; text-align: left; }
#cmenu button:hover, #cmenu button:focus-visible { background: var(--hover); }
.dlg { position: fixed; inset: 0; z-index: 60; background: var(--shade); display: flex; align-items: center; justify-content: center; padding: 20px; }
.dbox { width: 100%; max-width: 340px; background: var(--paper); border: 1px solid var(--border); border-radius: 18px; padding: 22px 20px 18px; box-shadow: 0 10px 40px rgba(0,0,0,.25); text-align: center; }
.dbox h2 { margin: 0 0 6px; font: 600 1.2rem/1.3 var(--serif); }
.dbox p { margin: 0 0 6px; color: var(--muted); font-size: .95rem; overflow-wrap: anywhere; }
.dbox .dbook { color: var(--ink); font-size: .95rem; }
.dacts { display: flex; gap: 10px; margin-top: 18px; }
.dacts .btn { flex: 1; min-height: 48px; padding: 0 8px; font-weight: 600; white-space: nowrap; }
.btn.danger { background: var(--err); border-color: var(--err); color: #fff; }
:root[data-theme="dark"] .btn.danger { color: #1b1917; }
@media (prefers-color-scheme: dark) { :root:not([data-theme]) .btn.danger { color: #1b1917; } }
#toast { position: fixed; width: max-content; z-index: 70; left: 50%; transform: translateX(-50%); bottom: calc(var(--sab) + 24px); max-width: calc(100vw - 32px); background: var(--ink); color: var(--paper); padding: 12px 18px; border-radius: 12px; font-size: .92rem; text-align: center; box-shadow: 0 6px 24px rgba(0,0,0,.25); }
.bar { display: block; height: 3px; border-radius: 2px; background: var(--border); margin-top: 6px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }
.done { color: var(--accent); }
.letters { display: grid; grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); gap: 4px; margin: 4px 0 10px; }
.letters a { display: flex; align-items: center; justify-content: center; height: 38px; border-radius: 10px; border: 1px solid var(--border); background: var(--paper); font-weight: 600; font-size: .95rem; }
.letters a.on { background: var(--accent); border-color: var(--accent); color: var(--paper); }
.authors { columns: 2 260px; column-gap: 20px; }
.authors a { display: flex; justify-content: space-between; gap: 8px; min-height: 44px; align-items: center; padding: 4px 4px; border-bottom: 1px solid var(--border); break-inside: avoid; overflow-wrap: anywhere; }
.authors a:active, .authors a:hover { background: var(--hover); }
.authors .n { color: var(--muted); font-size: .8rem; flex: none; }
.more { text-align: center; padding: 14px 0; }
.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 18px; border-radius: 10px; border: 1px solid var(--border); background: var(--paper); }
.btn.primary, button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.foot { margin-top: 28px; color: var(--muted); font-size: .8rem; display: flex; justify-content: space-between; align-items: center; }
.foot button { min-height: 44px; padding: 0 4px; color: var(--muted); text-decoration: underline; }
.empty { color: var(--muted); padding: 24px 4px; }

/* login */
.overlay { position: fixed; inset: 0; background: var(--bg); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.card h2 { margin: 0 0 6px; font: 600 1.6rem var(--serif); text-align: center; }
.card input { height: 48px; font-size: 17px; border-radius: 12px; border: 1px solid var(--border); background: var(--paper); color: var(--ink); padding: 0 14px; }
.card button { height: 48px; border-radius: 12px; font-weight: 600; }
.card .err { min-height: 1.2em; margin: 0; text-align: center; font-size: .9rem; }

/* ---------- reader chrome ---------- */
#reader { position: fixed; inset: 0; background: var(--paper); color: var(--ink); z-index: 20; overflow: hidden; overscroll-behavior: none; }
#rtop { position: absolute; left: 0; right: 0; top: 0; height: calc(var(--sat) + var(--rtopH)); padding: var(--sat) max(18px, var(--sal)) 0 max(18px, var(--sar));
  display: flex; align-items: flex-end; justify-content: center; font: 12px/1 var(--sans); color: var(--muted); padding-bottom: 6px; }
#rtop span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 680px; }
#rfoot { position: absolute; left: 0; right: 0; bottom: 0; height: calc(var(--sab) + var(--rbotH)); padding: 6px max(18px, var(--sal)) var(--sab) max(18px, var(--sar));
  display: flex; justify-content: space-between; align-items: flex-start; font: 12px/1 var(--sans); color: var(--muted); max-width: 100%; }
#rfoot span { font-variant-numeric: tabular-nums; }
#stage { position: absolute; left: 0; right: 0; top: calc(var(--sat) + var(--rtopH)); bottom: calc(var(--sab) + var(--rbotH)); overflow: hidden; }
#scroller { position: absolute; inset: 0; overflow-y: auto; overflow-x: hidden; touch-action: pan-y; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; outline: 0; scrollbar-width: none; }
#scroller::-webkit-scrollbar { display: none; }
@media (hover: hover) and (pointer: fine) { #scroller { scrollbar-width: thin; scrollbar-color: var(--border) transparent; } #scroller::-webkit-scrollbar { display: block; width: 8px; } #scroller::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; } }
#page { max-width: 34em; margin: 0 auto; padding: 8px max(22px, var(--sal)) 28px max(22px, var(--sar)); font-family: var(--rfont); font-size: var(--fs); line-height: var(--lh);
  overflow-wrap: break-word; hyphens: manual; -webkit-hyphens: manual; text-rendering: optimizeLegibility; }
:root[data-font="easy"] { --rfont: var(--easy); --lh: 1.78; }
:root[data-font="easy"] #page { letter-spacing: .012em; }
:root[data-font="sans"] { --rfont: var(--sans); --lh: 1.6; }
#page p { margin: 0 0 .8em; }
#page h1, #page h2, #page h3, #page h4, #page h5, #page h6 { line-height: 1.25; margin: 1.5em 0 .6em; font-weight: 600; text-align: left; break-after: avoid; }
#page h1 { font-size: 1.5em; } #page h2 { font-size: 1.3em; } #page h3 { font-size: 1.15em; } #page h4, #page h5, #page h6 { font-size: 1em; }
#page > :first-child { margin-top: .4em; }
#page blockquote { margin: .8em 0 .8em 1.1em; padding-left: .9em; border-left: 2px solid var(--border); }
#page ul, #page ol { padding-left: 1.4em; margin: 0 0 .8em; } #page li { margin: .2em 0; }
#page img { max-width: 100%; height: auto; display: block; margin: .8em auto; }
#page a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
#page sup a, #page sup { text-decoration: none; }
#page small { font-size: .82em; color: var(--muted); }
#page hr { border: 0; border-top: 1px solid var(--border); margin: 1.6em 30%; }
#page table { display: block; max-width: 100%; overflow-x: auto; border-collapse: collapse; font-size: .9em; margin: .8em 0; }
#page td, #page th { border: 1px solid var(--border); padding: .25em .5em; vertical-align: top; }
#page pre { white-space: pre-wrap; font: .85em/1.5 ui-monospace, Menlo, Consolas, monospace; }
#page sup, #page sub { line-height: 0; }
#endmark { max-width: 34em; margin: 0 auto; padding: 24px 22px 70px; text-align: center; color: var(--muted); font: 14px var(--sans); }
#endmark .btn { margin-top: 14px; }

/* states: preparing / error */
#state { position: absolute; inset: 0; background: var(--paper); display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 30px 28px; z-index: 3; }
#state .back { position: absolute; left: max(4px, var(--sal)); top: calc(var(--sat) + 4px); width: 48px; height: 48px; font-size: 32px; color: var(--muted); display: flex; align-items: center; justify-content: center; }
#state h1 { font: 600 1.25rem/1.3 var(--serif); margin: 0 0 4px; max-width: 22em; }
#state .by { color: var(--muted); font-size: .95rem; margin: 0 0 28px; max-width: 24em; }
#state .msg { font-size: 1rem; margin: 0; }
#state .note { color: var(--muted); font-size: .88rem; margin: 8px 0 0; min-height: 1.3em; }
#state .acts { display: flex; gap: 10px; margin-top: 22px; flex-wrap: wrap; justify-content: center; }
.shimmer { width: 140px; height: 3px; border-radius: 2px; background: var(--border); position: relative; overflow: hidden; margin: 0 auto 16px; }
.shimmer::after { content: ''; position: absolute; inset: 0; width: 45%; background: var(--accent); border-radius: 2px; animation: sh 1.4s ease-in-out infinite; }
@keyframes sh { 0% { transform: translateX(-110%); } 100% { transform: translateX(260%); } }
@media (prefers-reduced-motion: reduce) { .shimmer::after { animation: none; width: 100%; opacity: .5; } }

/* overlay bars */
#ui { position: absolute; inset: 0; pointer-events: none; z-index: 6; }
#ui.off #uitop, #ui.off #uibot { visibility: hidden; opacity: 0; transition: opacity .15s, transform .15s, visibility 0s .15s; }
#ui.off #uitop { transform: translateY(-8px); } #ui.off #uibot { transform: translateY(8px); }
#uitop, #uibot { position: absolute; left: 0; right: 0; background: var(--bar); pointer-events: auto; transition: opacity .15s, transform .15s; }
#uitop { top: 0; padding: var(--sat) max(6px, var(--sal)) 0 max(6px, var(--sar)); height: calc(var(--sat) + 56px); display: flex; align-items: center; border-bottom: 1px solid var(--border); }
#uitop button { width: 48px; height: 48px; flex: none; display: flex; align-items: center; justify-content: center; border-radius: 10px; }
#uback { font-size: 34px; line-height: 1; padding-bottom: 4px; }
#uaa { font: 600 1.05rem var(--serif); letter-spacing: -.02em; }
#utitle { flex: 1; min-width: 0; text-align: center; font-size: .9rem; line-height: 1.25; padding: 0 6px; }
#utitle b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#utitle span { display: block; color: var(--muted); font-size: .78rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#uibot { bottom: 0; padding: 12px max(22px, var(--sal)) calc(var(--sab) + 14px) max(22px, var(--sar)); border-top: 1px solid var(--border); }
#uinfo { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); margin-bottom: 4px; font-variant-numeric: tabular-nums; }
input[type=range] { width: 100%; height: 36px; margin: 0; background: transparent; -webkit-appearance: none; appearance: none; touch-action: pan-x; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--border); }
input[type=range]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--border); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; margin-top: -10px; border-radius: 50%; background: var(--accent); border: 0; box-shadow: 0 1px 4px rgba(0,0,0,.3); }
input[type=range]::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: var(--accent); border: 0; }

/* Aa sheet */
#scrim { position: absolute; inset: 0; background: var(--shade); z-index: 8; }
#sheet { position: absolute; z-index: 9; left: 0; right: 0; bottom: 0; background: var(--paper); border-top: 1px solid var(--border); border-radius: 18px 18px 0 0;
  padding: 18px max(20px, var(--sal)) calc(var(--sab) + 20px) max(20px, var(--sar)); display: flex; flex-direction: column; gap: 14px; box-shadow: 0 -8px 30px rgba(0,0,0,.18); max-width: 480px; margin: 0 auto; }
@media (min-width: 520px) { #sheet { bottom: 24px; border-radius: 18px; border: 1px solid var(--border); } }
.srow { display: flex; align-items: center; gap: 12px; }
.srow .sa { font-family: var(--serif); color: var(--muted); width: 22px; text-align: center; flex: none; }
.seg { gap: 8px; }
.seg button { flex: 1; min-height: 48px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg); font-size: 1rem; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; }
.seg button[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.f-serif { font-family: var(--serif); } .f-sans { font-family: var(--sans); } .f-easy { font-family: var(--easy); }
.themes button { font-size: .8rem; min-height: 64px; }
.sw { width: 26px; height: 26px; border-radius: 50%; border: 1px solid var(--border); display: block; }
.sw.light { background: #fdfaf5; } .sw.sepia { background: #f4ecd8; } .sw.dark { background: #1b1917; }
.sw.auto { background: linear-gradient(90deg, #fdfaf5 50%, #1b1917 50%); }
#sheet.pages #rsize, #sheet.pages #rfont { display: none; }

/* page view (scanned / fixed-layout books) */
#pdfview { position: absolute; inset: 0; overflow: auto; touch-action: pan-x pan-y; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; background: var(--paper); scrollbar-width: none; }
#pdfview::-webkit-scrollbar { display: none; }
@media (hover: hover) and (pointer: fine) { #pdfview { scrollbar-width: thin; } #pdfview::-webkit-scrollbar { display: block; width: 8px; height: 8px; } #pdfview::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; } }
#pages { position: relative; margin: 0 auto; padding: 8px; display: flex; flex-direction: column; gap: 10px; }
#pages .pg { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.15); position: relative; flex: none; }
#pages canvas { display: block; }
#pages .pgerr { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #777; font-size: .85rem; }
:root[data-theme="dark"] #pages .pg { background: #141311; }
:root[data-theme="dark"] #pages canvas { filter: invert(.9) hue-rotate(180deg) contrast(.92); }
:root[data-theme="sepia"] #pages .pg { background: #f4ecd8; }
:root[data-theme="sepia"] #pages canvas { filter: sepia(.4) brightness(.96); mix-blend-mode: multiply; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) #pages .pg { background: #141311; }
  :root:not([data-theme]) #pages canvas { filter: invert(.9) hue-rotate(180deg) contrast(.92); }
}
