:root {
  --bg: #ffffff;
  --bg-2: #f4f4f5;
  --fg: #111113;
  --fg-2: #6b6b73;
  --line: #e6e6ea;
  --accent: #111113;
  --accent-fg: #ffffff;
  --danger: #d92d20;
  --mark: #e8590c;
  --page-bg: #e9e9ec;
  --bar-bg: rgba(255, 255, 255, 0.88);
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 6px 20px rgba(0,0,0,.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 14px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0c0c0e;
  --bg-2: #1a1a1d;
  --fg: #f2f2f3;
  --fg-2: #9a9aa3;
  --line: #2a2a2e;
  --accent: #f2f2f3;
  --accent-fg: #0c0c0e;
  --page-bg: #000000;
  --bar-bg: rgba(18, 18, 20, 0.88);
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.4);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Library ---------- */
#library {
  min-height: 100%;
  padding: calc(var(--safe-top) + 12px) 20px calc(var(--safe-bottom) + 40px);
  max-width: 980px;
  margin: 0 auto;
}
.lib-header { display: flex; align-items: center; justify-content: space-between; padding: 8px 0 18px; }
.lib-header h1 { font-size: 32px; letter-spacing: -0.02em; margin: 0; font-weight: 750; }
.header-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 50%; color: var(--fg); }
.icon-btn:active { background: var(--bg-2); }
.add-btn {
  display: inline-flex; align-items: center; gap: 4px; height: 38px; padding: 0 16px 0 12px;
  border-radius: 999px; background: var(--accent); color: var(--accent-fg); font-weight: 600; cursor: pointer;
}
.add-btn svg { width: 18px; height: 18px; stroke-width: 2.5; }
.add-btn:active { opacity: .8; }

.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--fg-2); margin: 26px 0 12px; font-weight: 600; }

.continue {
  display: flex; gap: 16px; align-items: center; padding: 14px; border-radius: 18px;
  background: var(--bg-2); cursor: pointer; text-align: left; width: 100%;
}
.continue:active { opacity: .85; }
.continue img { width: 72px; aspect-ratio: 3/4; object-fit: cover; border-radius: 6px; box-shadow: var(--shadow); background: #fff; flex: none; }
.continue .meta { min-width: 0; flex: 1; }
.continue .eyebrow { font-size: 12px; color: var(--fg-2); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.continue .title { font-weight: 650; font-size: 17px; margin: 2px 0 8px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.continue .sub { font-size: 13px; color: var(--fg-2); margin-top: 6px; }

.bar { height: 4px; border-radius: 4px; background: var(--line); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--fg); border-radius: 4px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 22px 16px; }
@media (max-width: 420px) { .grid { grid-template-columns: repeat(2, 1fr); } }
.book { position: relative; text-align: left; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.book .cover-wrap { position: relative; }
.book img {
  width: 100%; aspect-ratio: 3/4; object-fit: cover; object-position: top; display: block;
  border-radius: 6px; box-shadow: var(--shadow); background: #fff;
}
.book:active img { transform: scale(.98); }
.book .t { font-size: 14px; font-weight: 600; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book .s { font-size: 12px; color: var(--fg-2); display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.book .more { width: 30px; height: 24px; display: grid; place-items: center; border-radius: 8px; color: var(--fg-2); margin-right: -6px; }
.book .more svg { width: 18px; height: 18px; }
.book .badge {
  position: absolute; top: 8px; left: 8px; font-size: 11px; font-weight: 700; padding: 3px 7px;
  border-radius: 999px; background: rgba(0,0,0,.75); color: #fff;
}

.empty { text-align: center; padding: 70px 24px; color: var(--fg-2); }
.empty-art { width: 76px; height: 76px; border-radius: 22px; background: var(--bg-2); display: grid; place-items: center; margin: 0 auto 18px; color: var(--fg); }
.empty-art svg { width: 34px; height: 34px; stroke-width: 1.6; }
.empty h2 { color: var(--fg); font-size: 20px; margin: 0 0 8px; }
.empty p { max-width: 320px; margin: 0 auto; font-size: 15px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 24px); transform: translateX(-50%);
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  font-weight: 500; box-shadow: var(--shadow); z-index: 60; white-space: nowrap; max-width: 90vw; overflow: hidden; text-overflow: ellipsis;
}

/* ---------- Reader ---------- */
#reader { position: fixed; inset: 0; background: var(--page-bg); }
.pages {
  position: absolute; inset: 0; overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: calc(var(--safe-top) + 56px) 0 calc(var(--safe-bottom) + 60px);
}
.page {
  position: relative; width: 100%; max-width: 900px; margin: 0 auto 6px; background: #fff;
}
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page .num { position: absolute; inset: 0; display: grid; place-items: center; color: #b5b5bb; font-size: 14px; }
:root[data-theme="dark"][data-night="1"] .page { background: #111; }
:root[data-theme="dark"][data-night="1"] .page canvas { filter: invert(0.88) hue-rotate(180deg); }
.page .flag {
  position: absolute; top: 0; right: 14px; width: 18px; height: 28px; background: var(--mark); z-index: 2;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 75%, 0 100%);
}

.reader-bar {
  position: absolute; left: 0; right: 0; z-index: 10; background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  transition: transform .25s ease, opacity .25s ease;
}
.reader-bar.top {
  top: 0; padding: calc(var(--safe-top) + 4px) 6px 4px; display: flex; align-items: center; gap: 2px;
  border-bottom: 0.5px solid var(--line);
}
.reader-bar.bottom { bottom: 0; padding: 10px 20px calc(var(--safe-bottom) + 10px); border-top: 0.5px solid var(--line); }
.reader-title { flex: 1; min-width: 0; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px; }
#bookmark-toggle.on svg { fill: var(--mark); stroke: var(--mark); }
.progress-track { height: 3px; background: var(--line); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--fg); }
.page-indicator { text-align: center; font-size: 12px; color: var(--fg-2); margin-top: 7px; font-variant-numeric: tabular-nums; }
#reader.chrome-hidden .reader-bar.top { transform: translateY(-100%); opacity: 0; pointer-events: none; }
#reader.chrome-hidden .reader-bar.bottom { transform: translateY(100%); opacity: 0; pointer-events: none; }
.page-pill {
  position: absolute; left: 50%; bottom: calc(var(--safe-bottom) + 14px); transform: translateX(-50%);
  background: rgba(0,0,0,.7); color: #fff; font-size: 12px; padding: 4px 10px; border-radius: 999px;
  opacity: 0; transition: opacity .3s; pointer-events: none; z-index: 9; font-variant-numeric: tabular-nums;
}
#reader.chrome-hidden .page-pill.show { opacity: 1; }
.reader-loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--fg-2); background: var(--page-bg); z-index: 5; }

/* ---------- Sheets ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; animation: fade .2s; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50; background: var(--bg); color: var(--fg);
  border-radius: 20px 20px 0 0; padding: 8px 20px calc(var(--safe-bottom) + 20px);
  max-height: 78vh; overflow-y: auto; max-width: 640px; margin: 0 auto; animation: up .25s ease;
}
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
.sheet-handle { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.sheet-head h3 { margin: 0; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.text-btn { font-weight: 600; color: var(--fg); padding: 6px 2px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { border-bottom: 0.5px solid var(--line); display: flex; align-items: center; }
.list li > button:first-child { flex: 1; text-align: left; padding: 14px 0; }
.list .danger { color: var(--danger); }
.list .bm-page { font-weight: 600; }
.list .bm-date { color: var(--fg-2); font-size: 13px; margin-left: 8px; }
.list .bm-del { width: 36px; height: 36px; display: grid; place-items: center; color: var(--fg-2); }
.list .bm-del svg { width: 18px; height: 18px; }
.list .empty-row { padding: 18px 0; color: var(--fg-2); font-size: 14px; border: 0; }
.goto { display: flex; gap: 8px; margin: 6px 0 12px; }
.goto input {
  flex: 1; height: 42px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-2);
  color: var(--fg); padding: 0 14px; font: inherit; font-size: 16px;
}
.pill-btn { height: 42px; padding: 0 18px; border-radius: 12px; background: var(--accent); color: var(--accent-fg); font-weight: 600; }

.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 0.5px solid var(--line); }
.setting small { display: block; color: var(--fg-2); font-size: 13px; }
.setting.info { color: var(--fg-2); font-size: 13px; border: 0; display: block; }
.segmented { display: inline-flex; background: var(--bg-2); border-radius: 10px; padding: 2px; }
.segmented button { padding: 6px 12px; border-radius: 8px; font-size: 14px; font-weight: 500; }
.segmented button.on { background: var(--bg); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.switch {
  appearance: none; -webkit-appearance: none; width: 51px; height: 31px; border-radius: 999px; background: var(--line);
  position: relative; transition: background .2s; flex: none; margin: 0; cursor: pointer;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s;
}
.switch:checked { background: #34c759; }
.switch:checked::after { transform: translateX(20px); }
