:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1f2329;
  --muted: #8a919f;
  --primary: #2f6fed;
  --danger: #e5484d;
  --border: #e5e6eb;
  --accent: #ffeb3b;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
a { color: inherit; text-decoration: none; }

/* ---------- 登录层 ---------- */
.login-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(15, 23, 42, .55);
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(4px);
}
.login-box {
  width: 320px; max-width: 90vw; padding: 28px 24px;
  background: var(--card); border-radius: 14px;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
  text-align: center;
}
.login-box h2 { margin-bottom: 18px; font-size: 20px; }
.login-box input {
  width: 100%; padding: 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 15px; margin-bottom: 12px;
}
.login-box button {
  width: 100%; padding: 12px; border: 0; border-radius: 8px;
  background: var(--primary); color: #fff; font-size: 15px; cursor: pointer;
}
.login-box .err { color: var(--danger); margin-top: 10px; font-size: 13px; }

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20; flex-wrap: wrap;
}
.topbar h1 { font-size: 20px; margin-right: auto; }
.topbar input[type=text] {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 14px; width: 180px;
}
.btn {
  display: inline-block; padding: 8px 14px; border: 0; border-radius: 8px;
  background: #eceef1; color: var(--text); font-size: 14px; cursor: pointer;
  text-align: center;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.danger { background: #fde8e8; color: var(--danger); }
.btn:active { opacity: .8; }

/* ---------- 书库网格 ---------- */
.drop-zone {
  position: fixed; inset: 10px; z-index: 90; pointer-events: none;
  border: 3px dashed var(--primary); border-radius: 16px;
  background: rgba(47,111,237,.06);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; color: var(--primary);
}
.grid {
  display: grid; gap: 16px; padding: 20px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; overflow: hidden;
  display: flex; flex-direction: column; transition: box-shadow .15s;
}
.card:hover { box-shadow: 0 6px 18px rgba(0,0,0,.08); }
.card .cover {
  width: 100%; aspect-ratio: 3/4; object-fit: cover; background: #eef0f3;
  display: block;
}
.card .no-cover { width: 100%; aspect-ratio: 3/4; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 40px; background: #eef0f3; }
.card-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card .title {
  font-size: 14px; font-weight: 600; line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  min-height: 38px;
}
.card .meta { font-size: 12px; color: var(--muted); }
.progress { height: 5px; border-radius: 3px; background: #eceef1; overflow: hidden; }
.progress .bar { height: 100%; background: var(--primary); border-radius: 3px; transition: width .3s; }
.card .actions { display: flex; gap: 8px; margin-top: 2px; }
.card .actions .btn { flex: 1; font-size: 13px; padding: 7px 4px; }
.empty {
  text-align: center; color: var(--muted); padding: 80px 20px; font-size: 16px;
}

/* ---------- 阅读器 ---------- */
.reader-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; background: #20242b; color: #fff;
  position: sticky; top: 0; z-index: 30;
  flex-wrap: wrap; font-size: 13px;
}
.reader-bar .back { font-size: 20px; padding: 2px 8px; color: #fff; }
.reader-bar .rtitle {
  max-width: 20vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-weight: 600;
}
.reader-bar .pageinfo { color: #c8cdd4; white-space: nowrap; }
.reader-bar .pageinfo b { color: #fff; }
.tools { display: flex; gap: 4px; }
.tool {
  padding: 5px 10px; border: 0; border-radius: 6px;
  background: #333a44; color: #dfe3e9; font-size: 13px; cursor: pointer;
}
.tool.active { background: var(--primary); color: #fff; }
.sep { width: 1px; height: 22px; background: #4a515c; margin: 0 4px; }
.colors { display: flex; align-items: center; gap: 5px; }
.swatch {
  width: 20px; height: 20px; border-radius: 50%; border: 2px solid transparent;
  cursor: pointer; display: inline-block;
}
.swatch.active { border-color: #fff; }
#custom-color { width: 26px; height: 26px; border: 0; background: none; cursor: pointer; padding: 0; }
.zoom { display: flex; align-items: center; gap: 4px; }
.zoom button {
  padding: 4px 9px; border: 0; border-radius: 5px;
  background: #333a44; color: #fff; font-size: 15px; cursor: pointer;
}
.zoom #zoom-label { min-width: 46px; text-align: center; color: #c8cdd4; font-size: 12px; }
#clear-page { padding: 5px 9px; border: 0; border-radius: 6px; background: #5a3a3a; color: #ffd9d9; font-size: 12px; cursor: pointer; }

#viewer {
  position: relative; height: calc(100vh - 52px);
  overflow: hidden; background: #3a3f47;
  touch-action: pan-y;
}
#page-scroll { position: absolute; inset: 0; overflow: auto; padding-bottom: 40px; overflow-anchor: none; }
#pages { position: relative; }
.spacer { width: 1px; }
.page-wrap {
  position: relative; margin: 0 auto; background: #fff;
  box-shadow: 0 4px 30px rgba(0,0,0,.35);
}
.page-wrap canvas, .page-wrap > div { position: absolute; left: 0; top: 0; }
.pdf-canvas { z-index: 1; }
.ann-layer { z-index: 2; pointer-events: none; overflow: hidden; }
.text-layer { z-index: 3; }
.draw-canvas { z-index: 4; }
.draw-canvas.handwriting { touch-action: none; cursor: crosshair; }
.draw-canvas.hit { cursor: pointer; }
.page-loading {
  position: absolute; inset: 0; z-index: 5;
  display: flex; align-items: center; justify-content: center;
}
.page-loading::before {
  content: ""; width: 28px; height: 28px;
  border: 3px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 高亮块 */
.hl {
  position: absolute; border-radius: 2px; pointer-events: none;
  mix-blend-mode: multiply;
}

/* pdf.js 文本层(选中文字) */
.textLayer {
  position: absolute; inset: 0; overflow: hidden;
  line-height: 1; text-align: initial; opacity: 1;
}
.textLayer span, .textLayer br {
  color: transparent; position: absolute; white-space: pre;
  cursor: text; transform-origin: 0% 0%;
}
.textLayer span { line-height: 1; }
.textLayer ::selection { background: rgba(0, 0, 255, 0.25); }
.textLayer span::selection { background: rgba(0, 0, 255, 0.25); }

/* 浮动"高亮"按钮 */
#float-highlight {
  position: fixed; z-index: 50; display: flex; gap: 6px;
  background: #20242b; color: #fff; padding: 6px 10px; border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,.3); font-size: 13px;
}
#float-highlight button { border: 0; background: var(--primary); color: #fff; padding: 5px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; }

.nav-arrow {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 44px; height: 72px; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.35); color: #fff; font-size: 30px;
  border-radius: 10px; cursor: pointer; user-select: none;
}
.nav-arrow:hover { background: rgba(0,0,0,.55); }
.nav-arrow.prev { left: 10px; }
.nav-arrow.next { right: 10px; }
@media (max-width: 768px) { .nav-arrow { display: none; } }

#toast {
  position: fixed; left: 50%; bottom: 40px; transform: translateX(-50%);
  background: rgba(20,24,30,.9); color: #fff; padding: 10px 18px;
  border-radius: 10px; font-size: 14px; z-index: 99;
  opacity: 0; transition: opacity .25s; pointer-events: none;
}
#toast.show { opacity: 1; }
