/* ============ 主题变量 ============ */
:root{
  --fs: 19px;
  --lh: 1.9;
  --ff: "Songti SC", "Noto Serif SC", "Source Han Serif SC", "SimSun", serif;
  --content-width: 38em;
  --accent: #2f6d5a;
  --radius: 14px;
}
:root[data-theme="paper"]{
  --bg:#f6f2e9; --fg:#2c2a26; --muted:#8b8478; --card:#fffdf8;
  --line:#e5dece; --chrome:rgba(246,242,233,.94); --scrim:rgba(30,28,25,.36);
  --accent:#2f6d5a;
}
:root[data-theme="sepia"]{
  --bg:#f2e6cf; --fg:#43341f; --muted:#8d7757; --card:#f9f0dd;
  --line:#e0cfae; --chrome:rgba(242,230,207,.94); --scrim:rgba(50,38,20,.36);
  --accent:#8a5a2b;
}
:root[data-theme="night"]{
  --bg:#121316; --fg:#c6c7c4; --muted:#7f827f; --card:#1b1d21;
  --line:#2a2d32; --chrome:rgba(18,19,22,.94); --scrim:rgba(0,0,0,.55);
  --accent:#7fb8a4;
}
:root[data-theme="green"]{
  --bg:#e8efe4; --fg:#26302a; --muted:#73806f; --card:#f3f8ef;
  --line:#d3dfcd; --chrome:rgba(232,239,228,.94); --scrim:rgba(28,38,30,.36);
  --accent:#3c6b4a;
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0; background:var(--bg); color:var(--fg);
  font-family:-apple-system,BlinkMacSystemFont,"PingFang SC","Hiragino Sans GB","Microsoft YaHei",system-ui,sans-serif;
  -webkit-font-smoothing:antialiased; -webkit-tap-highlight-color:transparent;
  overscroll-behavior:none;
  transition:background-color .25s ease, color .25s ease;
}
button{ font:inherit; color:inherit; }
h1,h2,h3{ margin:0; font-weight:600; }
.muted{ color:var(--muted); }
[hidden]{ display:none !important; }

/* ============ 通用按钮 ============ */
.btn{
  border:1px solid transparent; background:transparent; color:var(--fg);
  padding:9px 16px; border-radius:999px; cursor:pointer;
  transition:transform .12s ease, background-color .2s ease, opacity .2s;
}
.btn:active{ transform:scale(.97); }
.btn.primary{ background:var(--accent); color:#fff; font-weight:600; }
:root[data-theme="night"] .btn.primary{ color:#0d1512; }
.btn.ghost{ border-color:var(--line); }
.btn.lg{ padding:13px 26px; font-size:16px; }
.btn[disabled]{ opacity:.4; pointer-events:none; }
.link{ background:none; border:0; color:var(--muted); padding:6px 4px; cursor:pointer; font-size:13px; }

/* ============ 书架 ============ */
.view{ min-height:100dvh; }
#shelfView{
  display:flex; flex-direction:column;
  padding:calc(env(safe-area-inset-top) + 22px) 20px calc(env(safe-area-inset-bottom) + 14px);
  max-width:960px; margin:0 auto;
}
.shelf-head{ display:flex; align-items:flex-end; justify-content:space-between; gap:14px; }
.brand h1{
  font-family:var(--ff); font-size:30px; letter-spacing:.14em; font-weight:700;
}
.brand .muted{ margin:6px 0 0; font-size:12.5px; letter-spacing:.05em; }

.grid{
  display:grid; grid-template-columns:repeat(auto-fill,minmax(138px,1fr));
  gap:16px 14px; padding:26px 0 12px;
}
.book-card{ background:none; border:0; padding:0; text-align:left; cursor:pointer; user-select:none; -webkit-user-select:none; -webkit-touch-callout:none; }
.book-card.drag-placeholder{ opacity:.22; }
.grid-dragging{ cursor:grabbing; }
.dragging-book{ user-select:none; -webkit-user-select:none; }
.drag-ghost{
  position:fixed; left:0; top:0; z-index:85; margin:0; pointer-events:none;
  transform-origin:center; filter:drop-shadow(0 18px 24px rgba(0,0,0,.38));
  will-change:transform;
}
.cover{
  position:relative; aspect-ratio:3/4; border-radius:var(--radius); overflow:hidden;
  box-shadow:0 8px 20px -12px rgba(0,0,0,.45);
  display:flex; align-items:flex-end;
  transition:transform .18s ease, box-shadow .18s ease;
}
.book-card:active .cover{ transform:translateY(1px) scale(.985); }
.cover::after{
  content:""; position:absolute; inset:0;
  background:linear-gradient(180deg, rgba(0,0,0,0) 42%, rgba(0,0,0,.62) 100%);
}
.cover.has-photo{ background-size:cover; background-position:center; background-repeat:no-repeat; }
.cover.has-photo::after{
  content:""; position:absolute; inset:0;
  background:linear-gradient(180deg, rgba(0,0,0,.08) 38%, rgba(0,0,0,.74) 100%);
}
.cover-title{
  position:relative; z-index:1; padding:12px 12px 16px; color:#fff;
  font-family:var(--ff); font-size:15px; line-height:1.35; font-weight:600;
  display:-webkit-box; -webkit-line-clamp:3; -webkit-box-orient:vertical; overflow:hidden;
  text-shadow:0 1px 3px rgba(0,0,0,.3); word-break:break-word;
}
.cover-menu{
  position:absolute; z-index:2; top:6px; right:6px; width:30px; height:30px;
  border:0; border-radius:50%; background:rgba(0,0,0,.28); color:#fff;
  font-size:16px; line-height:1; cursor:pointer; backdrop-filter:blur(4px);
}
.cover-badge{
  position:absolute; z-index:2; top:8px; left:8px; font-size:10.5px; letter-spacing:.04em;
  padding:2px 7px; border-radius:999px; background:rgba(255,255,255,.86); color:#333;
}
.cover-progress{ position:absolute; z-index:2; left:0; right:0; bottom:0; height:3px; background:rgba(255,255,255,.24); }
.cover-progress i{ display:block; height:100%; background:#fff; }
.book-meta{ padding:8px 2px 0; }
.book-meta strong{ display:block; font-size:13.5px; font-weight:600; line-height:1.4; overflow:hidden; display:-webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; }
.book-meta span{ font-size:11.5px; color:var(--muted); }

.empty{
  flex:1; display:flex; flex-direction:column; align-items:center; justify-content:center;
  text-align:center; gap:10px; padding:48px 20px 64px;
}
.empty-icon{ font-size:54px; filter:saturate(.9); }
.empty h2{ font-family:var(--ff); font-size:21px; }
.empty p{ margin:0; width:100%; max-width:20em; color:var(--muted); font-size:14px; line-height:1.75; }
.empty .btn{ margin-top:14px; }
.hint{ font-size:12px !important; opacity:.75; }

.shelf-foot{ display:flex; align-items:center; justify-content:center; gap:4px; padding-top:18px; margin-top:auto; }
.shelf-foot .dot{ color:var(--muted); opacity:.5; }

/* ============ 阅读器 ============ */
.reader{ position:fixed; inset:0; z-index:10; }
.reader-scroll{
  position:absolute; inset:0; overflow-y:auto; overflow-x:hidden;
  -webkit-overflow-scrolling:touch; overscroll-behavior:contain;
  padding:calc(env(safe-area-inset-top) + 66px) 22px calc(env(safe-area-inset-bottom) + 92px);
}
.reader-scroll::-webkit-scrollbar{ width:0; height:0; }

#content{
  max-width:var(--content-width); margin:0 auto;
  font-family:var(--ff); font-size:var(--fs); line-height:var(--lh);
  letter-spacing:.01em;
}
#content h2{
  font-size:1.28em; text-align:center; margin:4px 0 1.4em; font-weight:600;
  letter-spacing:.05em; opacity:.92;
}
#content h2 small{ display:block; font-size:.55em; font-weight:400; color:var(--muted); margin-top:.5em; letter-spacing:.1em; }
#content p{
  margin:0 0 .82em; text-indent:2em; text-align:justify;
  word-break:break-word; overflow-wrap:anywhere;
}
#content p.speaking{ background:rgba(47,109,90,.12); border-radius:4px; }
:root[data-theme="night"] #content p.speaking{ background:rgba(127,184,164,.16); }
#content p.flash{ animation:flash 1.4s ease; }
@keyframes flash{ 0%,60%{ background:rgba(180,140,60,.28); } 100%{ background:transparent; } }

.chapter-end{ display:flex; gap:10px; justify-content:center; margin:34px 0 10px; }
.chapter-end .btn{ flex:1; max-width:160px; border-color:var(--line); }

.progress-line{ position:fixed; z-index:30; top:0; left:0; right:0; height:3px; background:transparent; }
.progress-line i{ display:block; height:100%; width:0; background:var(--accent); transition:width .2s ease; }

.chrome{
  position:absolute; left:0; right:0; z-index:25;
  background:var(--chrome); backdrop-filter:blur(14px) saturate(1.2); -webkit-backdrop-filter:blur(14px) saturate(1.2);
  transition:transform .24s ease, opacity .2s ease;
}
.chrome.top{
  top:0; display:flex; align-items:center; gap:10px;
  padding:calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom:1px solid var(--line);
}
.reader.chrome-hidden .chrome.top{ transform:translateY(-102%); opacity:0; }
.reader.chrome-hidden .chrome.bottom{ transform:translateY(102%); opacity:0; }
.chrome-title{ flex:1; min-width:0; display:flex; flex-direction:column; line-height:1.25; }
.chrome-title strong{ font-size:14px; font-weight:600; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.chrome-title .muted{ font-size:11.5px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.icon-btn{ width:38px; height:38px; border:0; background:none; font-size:26px; line-height:1; color:var(--fg); cursor:pointer; margin-top:-4px; }
.text-btn{ border:0; background:none; font-size:13px; color:var(--fg); padding:8px 6px; cursor:pointer; }

.chrome.bottom{
  bottom:0; display:flex; align-items:stretch;
  padding:4px 4px calc(env(safe-area-inset-bottom) + 4px);
  border-top:1px solid var(--line);
}
.bar-btn{
  flex:1; border:0; background:none; cursor:pointer;
  display:flex; align-items:center; justify-content:center;
  font-size:12.5px; color:var(--fg); padding:13px 2px; border-radius:10px;
  letter-spacing:.02em;
}
.bar-btn:active{ background:rgba(0,0,0,.04); }
.bar-btn.active{ color:var(--accent); font-weight:600; }

/* ============ 抽屉 ============ */
.sheet-wrap{ position:fixed; inset:0; z-index:60; }
.sheet-backdrop{ position:absolute; inset:0; background:var(--scrim); opacity:0; transition:opacity .22s ease; }
.sheet-wrap.open .sheet-backdrop{ opacity:1; }
.sheet{
  position:absolute; left:0; right:0; bottom:0;
  max-height:78vh; overflow-y:auto; -webkit-overflow-scrolling:touch;
  background:var(--card); border-radius:20px 20px 0 0;
  padding:8px 20px calc(env(safe-area-inset-bottom) + 20px);
  transform:translateY(100%); transition:transform .26s cubic-bezier(.22,.8,.24,1);
  box-shadow:0 -12px 40px -20px rgba(0,0,0,.5);
}
.sheet-wrap.open .sheet{ transform:translateY(0); }
.sheet-grip{ width:38px; height:4px; border-radius:4px; background:var(--line); margin:6px auto 12px; }
.sheet h3{ font-size:15px; margin:4px 0 10px; display:flex; justify-content:space-between; align-items:center; }
.sheet h3 .muted{ font-size:12px; font-weight:400; }

/* 目录 */
.toc-item{
  display:flex; align-items:center; gap:10px; width:100%;
  border:0; background:none; text-align:left; cursor:pointer;
  padding:13px 2px; border-bottom:1px solid var(--line);
  font-size:14px; color:var(--fg);
}
.toc-item .idx{ width:2.2em; flex:none; color:var(--muted); font-size:11.5px; }
.toc-item .name{ flex:1; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.toc-item.current{ color:var(--accent); font-weight:600; }

/* 设置 */
.row{ display:flex; align-items:center; justify-content:space-between; gap:14px; padding:13px 0; border-bottom:1px solid var(--line); }
.row:last-child{ border-bottom:0; }
.row .label{ font-size:13.5px; flex:none; }
.row .val{ font-size:12px; color:var(--muted); width:3.2em; text-align:right; }
.row input[type=range]{ flex:1; accent-color:var(--accent); height:22px; }
.seg{ display:flex; gap:6px; }
.seg button{
  border:1px solid var(--line); background:transparent; color:var(--fg);
  padding:6px 12px; border-radius:9px; font-size:12.5px; cursor:pointer;
}
.seg button.on{ border-color:var(--accent); color:var(--accent); background:rgba(47,109,90,.08); }
:root[data-theme="night"] .seg button.on{ background:rgba(127,184,164,.14); }
.swatches{ display:flex; gap:8px; }
.swatch{ width:30px; height:30px; border-radius:50%; border:2px solid transparent; cursor:pointer; padding:0; }
.swatch.on{ border-color:var(--accent); box-shadow:0 0 0 3px var(--bg) inset; }

/* 书签列表 */
.mark-item{ display:flex; gap:10px; padding:12px 0; border-bottom:1px solid var(--line); cursor:pointer; }
.mark-item .quote{ flex:1; min-width:0; }
.mark-item .quote b{ display:block; font-size:13px; font-weight:600; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.mark-item .quote span{ display:block; font-size:12px; color:var(--muted); overflow:hidden; text-overflow:ellipsis; white-space:nowrap; margin-top:2px; }
.mark-item .quote em{ display:block; font-style:normal; font-size:12.5px; color:var(--accent); margin-top:3px; }
.mark-del{ border:0; background:none; color:var(--muted); font-size:16px; cursor:pointer; padding:0 4px; }

/* 书籍菜单 */
.menu-item{
  display:flex; align-items:center; gap:12px; width:100%;
  border:0; background:none; text-align:left; cursor:pointer;
  padding:15px 4px; border-bottom:1px solid var(--line); font-size:14.5px;
}
.menu-item.danger{ color:#c0392b; }
:root[data-theme="night"] .menu-item.danger{ color:#e07a6a; }

/* ============ 对话框 ============ */
.dialog-wrap{ position:fixed; inset:0; z-index:70; display:flex; align-items:center; justify-content:center; padding:24px; }
.dialog{
  position:relative; width:100%; max-width:420px; background:var(--card);
  border-radius:18px; padding:20px; box-shadow:0 24px 60px -24px rgba(0,0,0,.6);
}
.dialog h3{ font-size:16px; margin-bottom:10px; }
.dialog textarea{
  width:100%; border:1px solid var(--line); border-radius:12px; background:var(--bg);
  color:var(--fg); font:inherit; font-size:14px; padding:12px; resize:vertical;
  line-height:1.65;
}
.dialog-actions{ display:flex; justify-content:flex-end; gap:8px; margin-top:14px; }

/* ============ 选中工具条 ============ */
.selbar{
  position:fixed; z-index:80; transform:translate(-50%,-120%);
  display:flex; background:#20211f; border-radius:12px; overflow:hidden;
  box-shadow:0 10px 30px -10px rgba(0,0,0,.6);
}
.selbar button{ border:0; background:none; color:#fff; padding:10px 16px; font-size:13px; cursor:pointer; }
.selbar button + button{ border-left:1px solid rgba(255,255,255,.14); }

/* ============ Toast / 错误条 ============ */
.toast{
  position:fixed; z-index:90; left:50%; bottom:calc(env(safe-area-inset-bottom) + 84px);
  transform:translateX(-50%); background:rgba(32,33,31,.94); color:#fff;
  font-size:13px; padding:10px 18px; border-radius:999px; max-width:86vw; text-align:center;
  box-shadow:0 8px 24px -10px rgba(0,0,0,.5);
}
#errbar{
  position:fixed; z-index:100; top:0; left:0; right:0;
  background:#b3261e; color:#fff; font-size:12px; padding:8px 12px;
  font-family:ui-monospace,Menlo,Consolas,monospace; white-space:pre-wrap;
}

@media (min-width:720px){
  .grid{ grid-template-columns:repeat(auto-fill,minmax(156px,1fr)); gap:20px 18px; }
  .reader-scroll{ padding-left:34px; padding-right:34px; }
  .sheet{ left:50%; transform:translate(-50%,100%); width:560px; border-radius:20px; bottom:24px; max-height:70vh; }
  .sheet-wrap.open .sheet{ transform:translate(-50%,0); }
}
