:root {
  --bg: #f7f3ea; --bg-2: #efe9db; --ink: #2a2620; --muted: #7a7466; --rule: #ddd5c2;
  --accent: #2f4a3f; --accent-ink: #f7f3ea; --mark: #b5503c; --hl: #f3e2a9;
  --font-ui: "Pretendard", "Noto Sans KR", system-ui, -apple-system, sans-serif;
  --r-fs: 18px; --r-lh: 1.9; --r-font: "Noto Serif KR", "Nanum Myeongjo", serif;
}
:root[data-theme="dark"] {
  --bg: #17181a; --bg-2: #202225; --ink: #d9d6cf; --muted: #8b8a85; --rule: #34363a;
  --accent: #8fb5a2; --accent-ink: #14201a; --mark: #e08a73; --hl: #4a4326;
}
:root[data-theme="sepia"] {
  --bg: #f1e6cf; --bg-2: #e8dbbf; --ink: #3b3224; --muted: #8a7c60; --rule: #d6c6a3;
  --accent: #6b4a2b; --accent-ink: #f1e6cf; --mark: #a3402c; --hl: #e6d08c;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font-ui); }
body { min-height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }

/* ---------- 공통 ---------- */
.btn { border: 1px solid var(--rule); background: transparent; padding: 10px 16px; border-radius: 10px; min-height: 44px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--mark); }
.icon-btn { width: 44px; height: 44px; border: 0; background: transparent; border-radius: 50%; font-size: 20px; display: grid; place-items: center; }
.icon-btn:active { background: var(--bg-2); }
.inp { width: 100%; padding: 12px 14px; border: 1px solid var(--rule); border-radius: 10px; background: var(--bg-2); color: var(--ink); font: inherit; }
#toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; z-index: 500; max-width: 90vw; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 로그인 ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login form { width: min(320px, 100%); display: grid; gap: 12px; }
.brand { font-size: 28px; margin: 0 0 8px; letter-spacing: -0.02em; }
.err { color: var(--mark); min-height: 1.2em; font-size: 14px; }

/* ---------- 서재 ---------- */
.lib { max-width: 860px; margin: 0 auto; padding: calc(16px + env(safe-area-inset-top)) 16px 96px; }
.lib-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.lib-head h1 { font-size: 24px; margin: 0; letter-spacing: -0.02em; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 20px 14px; }
.card { background: none; border: 0; padding: 0; text-align: left; position: relative; }
.cover { aspect-ratio: 2 / 3; background: var(--bg-2); border: 1px solid var(--rule); border-radius: 4px 8px 8px 4px; overflow: hidden; display: grid; place-items: center; }
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover .ph { padding: 12px; font-weight: 600; text-align: center; line-height: 1.4; word-break: keep-all; }
.card .t { margin: 8px 0 2px; font-size: 14px; font-weight: 600; line-height: 1.35; word-break: keep-all; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .m { font-size: 12px; color: var(--muted); }
.bar { height: 3px; background: var(--rule); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.card .del { position: absolute; top: 4px; right: 4px; width: 32px; height: 32px; border-radius: 50%; border: 0; background: rgba(0,0,0,.55); color: #fff; font-size: 14px; opacity: 0; }
.card:focus-within .del, .card:hover .del { opacity: 1; }
@media (hover: none) { .card .del { opacity: .85; } }
.empty { text-align: center; color: var(--muted); padding: 80px 0; line-height: 1.8; }
.fab { position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom)); height: 52px; padding: 0 22px; border-radius: 26px; border: 0; background: var(--accent); color: var(--accent-ink); font-weight: 600; box-shadow: 0 6px 18px rgba(0,0,0,.25); }

/* ---------- 읽기 ---------- */
.reader { min-height: 100dvh; }
.topbar, .botbar { position: fixed; left: 0; right: 0; background: var(--bg); z-index: 50; display: flex; align-items: center; gap: 4px; padding: 0 8px; transition: transform .2s; }
.topbar { top: 0; height: calc(52px + env(safe-area-inset-top)); padding-top: env(safe-area-inset-top); border-bottom: 1px solid var(--rule); }
.botbar { bottom: 0; height: calc(56px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); border-top: 1px solid var(--rule); justify-content: space-between; }
.ui-hidden .topbar { transform: translateY(-100%); }
.ui-hidden .botbar { transform: translateY(100%); }
.topbar .ttl { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.page { max-width: 680px; margin: 0 auto; padding: calc(72px + env(safe-area-inset-top)) 20px calc(96px + env(safe-area-inset-bottom)); font-family: var(--r-font); font-size: var(--r-fs); line-height: var(--r-lh); word-break: keep-all; overflow-wrap: break-word; }
.page h2.ch { font-size: 1.3em; line-height: 1.4; margin: 0 0 1.6em; letter-spacing: -0.01em; }
.page p { margin: 0 0 .9em; }
.page h3, .page h4 { margin: 1.6em 0 .6em; line-height: 1.4; }
.page img { max-width: 100%; display: block; margin: 1em auto; border-radius: 4px; }
.page .hit { background: var(--hl); border-radius: 3px; }
.page .mk { box-shadow: -4px 0 0 var(--mark); padding-left: 8px; margin-left: -12px; }
.nav-ch { display: flex; gap: 12px; margin-top: 3em; }
.nav-ch .btn { flex: 1; }
.pct { font-size: 12px; color: var(--muted); }

/* ---------- 시트(목차·설정) ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 90; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: 78dvh; background: var(--bg); border-radius: 18px 18px 0 0; z-index: 100; display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom); }
.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 8px 20px; border-bottom: 1px solid var(--rule); font-weight: 600; }
.sheet .body { overflow-y: auto; padding: 8px 0; }
.toc-item { display: flex; justify-content: space-between; gap: 12px; width: 100%; border: 0; background: none; text-align: left; padding: 12px 20px; font-size: 15px; }
.toc-item.cur { color: var(--accent); font-weight: 700; background: var(--bg-2); }
.toc-item small { color: var(--muted); white-space: nowrap; }
.tabs { display: flex; gap: 4px; padding: 8px 12px 0; }
.tabs button { flex: 1; border: 0; background: none; padding: 10px; border-bottom: 2px solid transparent; color: var(--muted); }
.tabs button.on { color: var(--ink); border-color: var(--accent); font-weight: 600; }
.set-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 20px; gap: 12px; }
.seg { display: flex; border: 1px solid var(--rule); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: none; padding: 8px 14px; min-height: 40px; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
/* ---------- 페이지 모드 (page-flip: 책장 접힘 · 양쪽 펼침) ---------- */
:root { --desk: #e6dfcc; --paper: var(--bg); }
:root[data-theme="dark"] { --desk: #0e0f10; }
:root[data-theme="sepia"] { --desk: #d9cba6; }
.reader.paged .pv { position: fixed; z-index: 1; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none;
  left: 0; right: 0; background: var(--desk);
  top: calc(var(--pv-top, 0px) + env(safe-area-inset-top)); bottom: calc(var(--pv-bottom, 0px) + env(safe-area-inset-bottom)); }
/* 쪽 나눔을 재기 위해 숨겨 둔 본문(JS 가 폭·높이를 쪽 크기로 맞춘다) */
.reader.paged .page { max-width: none; margin: 0; padding: 0; overflow: visible; }
.reader.paged .page h2.ch { margin-top: 0; break-after: avoid; }
.reader.paged .page h3, .reader.paged .page h4 { break-after: avoid; margin-top: 1em; }
.reader.paged .page p { orphans: 2; widows: 2; }
.reader.paged .page img { max-height: var(--pg-text-h, 70vh); width: auto; max-width: 100%; object-fit: contain; break-inside: avoid; display: block; margin: 1em auto; }
.reader.paged .page .nav-ch { break-inside: avoid; margin-top: 2em; }

/* 쪽마다 독립된 요소. 크기·위치·넘김 변형은 라이브러리가 인라인 스타일로 지정한다 */
.pgx { background: var(--paper); }
.pgx.--left { box-shadow: inset -24px 0 26px -22px rgba(0, 0, 0, .3); }
.pgx.--right { box-shadow: inset 24px 0 26px -22px rgba(0, 0, 0, .3); }
.stf__wrapper.--portrait .pgx { box-shadow: none; }
.pgx-in { position: absolute; inset: 0; box-sizing: border-box; overflow: hidden;
  padding: var(--pgp, 24px) var(--pgp, 24px) var(--pgpb, 36px);
  font-family: var(--r-font); font-size: var(--r-fs); line-height: var(--r-lh); color: var(--ink);
  word-break: keep-all; overflow-wrap: break-word; }
.pgx-in > :first-child { margin-top: 0 !important; }
.pgx-in h2.ch { font-size: 1.3em; line-height: 1.4; margin: 0 0 1.6em; letter-spacing: -0.01em; }
.pgx-in p { margin: 0 0 .9em; }
.pgx-in p.cont-next { margin-bottom: 0; }
.pgx-in h3, .pgx-in h4 { margin: 1.6em 0 .6em; line-height: 1.4; }
.pgx-in img { display: block; margin: 1em auto; max-width: 100%; max-height: var(--pg-text-h, 70vh); width: auto; object-fit: contain; border-radius: 4px; }
.pgx-in .hit { background: var(--hl); border-radius: 3px; }
.pgx-in .mk { box-shadow: -4px 0 0 var(--mark); padding-left: 8px; margin-left: -12px; }
.pgx-in .nav-ch { display: flex; gap: 12px; margin-top: 2em; }
.pgx-in .nav-ch .btn { flex: 1; }
.pgn { position: absolute; left: 0; right: 0; bottom: calc(var(--pgpb, 36px) * .32); text-align: center;
  font: 12px var(--font-ui); color: var(--muted); font-variant-numeric: tabular-nums; pointer-events: none; }
.botbar .pct { flex: 1; min-width: 0; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- 서재: 이어읽기 · 필터 · 시리즈 · 배지 ---------- */
.cover { position: relative; }
.badge { position: absolute; top: 6px; left: 6px; background: rgba(0,0,0,.62); color: #fff; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 10px; line-height: 1.5; }
.badge.st { left: auto; right: 6px; background: var(--accent); color: var(--accent-ink); }
.badge.st.done { background: var(--muted); color: var(--bg); }
.cover.stack { box-shadow: 4px 4px 0 -1px var(--bg-2), 4px 4px 0 0 var(--rule), 8px 8px 0 -1px var(--bg-2), 8px 8px 0 0 var(--rule); margin: 0 8px 8px 0; }
.chips { display: flex; gap: 8px; margin: 0 0 16px; overflow-x: auto; padding-bottom: 2px; }
.chips .chip { white-space: nowrap; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.resume { display: flex; align-items: center; gap: 14px; width: 100%; text-align: left; border: 1px solid var(--rule); background: var(--bg-2); border-radius: 14px; padding: 12px; margin-bottom: 16px; }
.resume .rb { flex: 1; min-width: 0; display: grid; gap: 3px; }
.resume small { color: var(--accent); font-weight: 700; font-size: 12px; }
.resume strong { font-size: 16px; line-height: 1.35; }
.resume span { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.resume .go { font-size: 26px; color: var(--muted); flex: none; }
.cover.sm { width: 58px; flex: none; }
.cover.xs { width: 46px; flex: none; border-radius: 3px 5px 5px 3px; }
.banner { display: flex; align-items: center; justify-content: space-between; gap: 12px; background: var(--hl); border-radius: 12px; padding: 12px 14px; margin-bottom: 16px; font-size: 14px; line-height: 1.5; }
.banner .btn { flex: none; min-height: 40px; padding: 8px 14px; }
.install { border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink); border-radius: 18px; padding: 8px 14px; min-height: 38px; font-size: 14px; font-weight: 600; white-space: nowrap; }
.lib-head .ser { flex: 1; min-width: 0; text-align: center; font-size: 20px; }
.lib-head .icon-btn:first-child { margin-left: -8px; }
.sub { color: var(--muted); font-size: 13px; margin: -6px 0 14px; text-align: center; }
.vols { display: grid; gap: 4px; }
.vol { display: flex; align-items: center; border-bottom: 1px solid var(--rule); }
.vol-main { flex: 1; min-width: 0; display: flex; align-items: center; gap: 14px; border: 0; background: none; padding: 12px 0; text-align: left; }
.vol .vb { flex: 1; min-width: 0; }
.vol .vt { font-size: 15px; line-height: 1.4; }
.vol .vm { font-size: 12px; color: var(--muted); margin: 2px 0 5px; }
.tag { display: inline-block; padding: 0 7px; border-radius: 9px; font-size: 11px; font-weight: 600; background: var(--bg-2); border: 1px solid var(--rule); }
.tag.reading { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.tag.done { color: var(--muted); }

/* ---------- 정보 수정 · 완독 패널 ---------- */
.form, .sheet .body.form { padding: 8px 20px 16px; display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field > span { font-size: 12px; color: var(--muted); }
.two { display: grid; grid-template-columns: 1fr 96px; gap: 10px; }
textarea.inp { resize: vertical; line-height: 1.5; }
.stars { display: flex; gap: 2px; }
.stars button { border: 0; background: none; font-size: 28px; color: var(--rule); padding: 0 2px; line-height: 1; min-width: 36px; min-height: 40px; }
.stars button.on { color: #d6a02c; }
.stars.big button { font-size: 34px; }
.form .lis-btns { padding: 0; }
.form .fin { margin: 6px 0 0; font-size: 16px; line-height: 1.6; text-align: center; }
.form .set-row { padding: 0; }
.form .note { padding: 0; text-align: center; }

/* ---------- 듣기 플레이어 ---------- */
.player { position: fixed; left: 0; right: 0; bottom: calc(56px + env(safe-area-inset-bottom)); z-index: 49; background: var(--bg-2); border-top: 1px solid var(--rule); padding: 8px 12px 6px; transition: bottom .2s; }
.ui-hidden .player { bottom: 0; padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
.listening:not(.paged) .page { padding-bottom: calc(210px + env(safe-area-inset-bottom)); }
.seekrow { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.seek { flex: 1; min-width: 0; height: 28px; accent-color: var(--accent); }
.ctl { display: flex; align-items: center; justify-content: space-around; max-width: 420px; margin: 0 auto; }
.icon-btn.sm { font-size: 13px; font-weight: 600; }
.icon-btn.play { width: 56px; height: 56px; background: var(--accent); color: var(--accent-ink); font-size: 22px; }
.icon-btn.play:active { background: var(--accent); opacity: .85; }
.ctl2 { display: flex; justify-content: center; gap: 8px; margin-top: 2px; }
.chip { border: 1px solid var(--rule); background: transparent; border-radius: 16px; padding: 6px 12px; min-height: 34px; font-size: 13px; }

/* ---------- 듣기 탭 ---------- */
.lis-stat { padding: 8px 20px 12px; }
.lis-stat small { display: block; color: var(--muted); margin-top: 2px; }
.lis-stat small.bad { color: var(--mark); }
.lis-btns { display: flex; gap: 10px; padding: 6px 20px; }
.lis-btns .btn { flex: 1; }
.chlist { margin: 6px 20px; border: 1px solid var(--rule); border-radius: 10px; overflow: hidden; }
.chlist summary { padding: 11px 14px; cursor: pointer; font-size: 14px; background: var(--bg-2); }
.chrow { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--rule); font-size: 13px; }
.chrow.cur { background: var(--bg-2); font-weight: 600; }
.chrow .ct { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chrow small { color: var(--muted); white-space: nowrap; }
.chrow small.bad { color: var(--mark); }
.chrow .mini { border: 1px solid var(--rule); background: none; border-radius: 8px; padding: 4px 10px; min-height: 32px; font-size: 12px; }
.listen .note { color: var(--muted); font-size: 13px; line-height: 1.6; padding: 8px 20px; margin: 0; }
.mark-row { display: flex; align-items: center; }
.mark-row .toc-item { flex: 1; }
