/* ============================================================
   墨读 · 全局样式与书架
   ============================================================ */

:root {
  --brand: #3A4A6A;
  --brand-strong: #2E3A52;
  --bg: #F3F4F7;
  --bg-elev: #FFFFFF;
  --surface: #FFFFFF;
  --surface-2: #F7F8FA;
  --text: #191C22;
  --text-2: #6A7280;
  --text-3: #9AA1AE;
  --line: #E4E7EC;
  --line-strong: #D3D7DE;
  --danger: #C0392B;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
  --shadow-md: 0 4px 14px -4px rgba(16, 24, 40, .16);
  --shadow-lg: 0 12px 40px -12px rgba(16, 24, 40, .30);
  --radius: 14px;
  --radius-sm: 9px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --topbar-h: 56px;
}

body.theme-dark {
  --bg: #101216;
  --bg-elev: #181B21;
  --surface: #181B21;
  --surface-2: #20242C;
  --text: #E6E8EC;
  --text-2: #9BA3B0;
  --text-3: #6E7683;
  --line: #262B34;
  --line-strong: #333A45;
  --shadow-lg: 0 12px 40px -12px rgba(0, 0, 0, .7);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
               "Microsoft YaHei", "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  transition: background-color .25s ease, color .25s ease;
}

button, input { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
svg { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- 视图容器 ---------- */
.view {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}
.view.is-active { display: flex; }

/* ============================================================
   顶栏
   ============================================================ */
.topbar {
  position: relative;
  z-index: 20;
  flex: 0 0 auto;
  padding-top: var(--safe-t);
  padding-left: calc(var(--safe-l));
  padding-right: calc(var(--safe-r));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--line);
}
@supports not (backdrop-filter: blur(1px)) {
  .topbar { background: var(--bg); }
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: var(--topbar-h);
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px;
}

.brand { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.brand-mark { width: 26px; height: 26px; stroke: none; }
.brand-name {
  font-size: 19px;
  font-weight: 650;
  letter-spacing: .04em;
  color: var(--brand-strong);
}
body.theme-dark .brand-name { color: #C9D1E4; }

.topbar-actions { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1 1 auto; justify-content: flex-end; }

.searchbox {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 36px;
  padding: 0 11px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  flex: 0 1 240px;
  min-width: 0;
  transition: border-color .18s ease, box-shadow .18s ease;
}
.searchbox:focus-within {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 15%, transparent);
}
.searchbox svg { width: 15px; height: 15px; flex: 0 0 auto; color: var(--text-3); }
.searchbox input {
  flex: 1 1 auto;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 14px;
}
.searchbox input::-webkit-search-cancel-button { -webkit-appearance: none; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border-radius: 10px;
  color: var(--text-2);
  background: var(--surface);
  border: 1px solid var(--line);
  transition: background-color .16s ease, color .16s ease, transform .12s ease;
}
.icon-btn:active { transform: scale(.94); }
.icon-btn svg { width: 17px; height: 17px; }

.shelf-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  max-width: 1180px;
  margin: 0 auto;
  padding: 0 16px 9px;
  font-size: 12.5px;
  color: var(--text-3);
}
.storage-info { font-variant-numeric: tabular-nums; }
.storage-info.is-warn { color: #B7791F; }

/* ============================================================
   书架
   ============================================================ */
.shelf-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 18px calc(16px + var(--safe-r)) calc(96px + var(--safe-b)) calc(16px + var(--safe-l));
}

.shelf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 22px 16px;
  max-width: 1180px;
  margin: 0 auto;
}
@media (min-width: 700px) {
  .shelf-grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 30px 24px; }
}
@media (min-width: 1080px) {
  .shelf-grid { grid-template-columns: repeat(auto-fill, minmax(148px, 1fr)); }
}

/* --- 网格卡片 --- */
.book-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 9px;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  animation: card-in .32s cubic-bezier(.22, .8, .3, 1) backwards;
}
@keyframes card-in {
  from { opacity: 0; transform: translateY(10px) scale(.97); }
  to { opacity: 1; transform: none; }
}

.book-cover {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
  border-radius: 8px;
  overflow: hidden;
  background: #3A4A6A;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .14), 0 8px 20px -8px rgba(16, 24, 40, .42);
  transition: transform .16s ease, box-shadow .16s ease;
}
.book-card:active .book-cover { transform: scale(.965); }
.book-cover img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.cover-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 11px 10px;
  color: #fff;
}
.cover-fallback::after {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 5px;
  background: rgba(0, 0, 0, .22);
}
.cover-title {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .18);
}
.cover-badge {
  font-size: 9px;
  letter-spacing: .12em;
  font-weight: 600;
  opacity: .78;
  text-transform: uppercase;
}

.book-progress {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 3px;
  background: rgba(0, 0, 0, .18);
}
.book-progress > i {
  display: block;
  height: 100%;
  background: rgba(255, 255, 255, .92);
  border-radius: 0 2px 2px 0;
}
.book-finished-flag {
  position: absolute;
  top: 7px; right: 7px;
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 9.5px;
  font-weight: 600;
  color: #fff;
  background: rgba(0, 0, 0, .46);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.book-info { padding: 0 1px; }
.book-title {
  font-size: 13.5px;
  font-weight: 550;
  line-height: 1.35;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}
.book-sub {
  margin-top: 2px;
  font-size: 11.5px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- 列表视图 --- */
.shelf-grid.is-list { grid-template-columns: 1fr; gap: 4px; }
.shelf-grid.is-list .book-card {
  flex-direction: row;
  align-items: center;
  gap: 13px;
  padding: 9px 4px;
  border-radius: 10px;
}
.shelf-grid.is-list .book-cover { width: 46px; flex: 0 0 46px; border-radius: 5px; box-shadow: var(--shadow-sm); }
.shelf-grid.is-list .cover-fallback { padding: 4px; }
.shelf-grid.is-list .cover-title { font-size: 7px; -webkit-line-clamp: 3; }
.shelf-grid.is-list .cover-badge,
.shelf-grid.is-list .book-progress,
.shelf-grid.is-list .book-finished-flag { display: none; }
.shelf-grid.is-list .book-info { flex: 1 1 auto; min-width: 0; }
.shelf-grid.is-list .book-title { -webkit-line-clamp: 1; }
.shelf-grid.is-list .book-sub { margin-top: 3px; }

/* --- 空状态 --- */
.empty-state {
  display: none;
  max-width: 420px;
  margin: 0 auto;
  text-align: center;
  color: var(--text-2);
  animation: card-in .4s ease backwards;
}
.empty-state.is-shown {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 64vh;
}
.empty-art { color: var(--brand); width: 128px; margin: 0 auto 6px; }
.empty-art svg { width: 100%; height: auto; stroke: none; }
.empty-state h2 { margin: 4px 0 10px; font-size: 19px; font-weight: 600; color: var(--text); }
.empty-state p { margin: 0 0 22px; font-size: 14px; line-height: 1.75; }
.empty-state b { color: var(--brand); }
.empty-tip { margin-top: 18px !important; font-size: 12.5px !important; color: var(--text-3) !important; }
.empty-state.is-search-empty .empty-art,
.empty-state.is-search-empty .empty-tip { display: none; }

/* ---------- 按钮 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 20px;
  border-radius: 11px;
  font-size: 15px;
  font-weight: 550;
  transition: transform .12s ease, background-color .16s ease, opacity .16s ease;
}
.btn:active { transform: scale(.97); }
.btn svg { width: 18px; height: 18px; }
.btn-primary { background: var(--brand-strong); color: #fff; box-shadow: var(--shadow-md); }
.btn-primary:active { background: #26314A; }
.btn-ghost { background: var(--surface-2); color: var(--text-2); border: 1px solid var(--line); }

/* ---------- FAB ---------- */
.fab {
  position: absolute;
  right: calc(20px + var(--safe-r));
  bottom: calc(24px + var(--safe-b));
  z-index: 30;
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  color: #fff;
  background: var(--brand-strong);
  box-shadow: 0 6px 20px -4px rgba(46, 58, 82, .55), 0 2px 6px rgba(0, 0, 0, .12);
  transition: transform .16s cubic-bezier(.3, 1.4, .5, 1), opacity .2s ease;
}
.fab:active { transform: scale(.9); }
.fab svg { width: 25px; height: 25px; stroke-width: 2; }
.fab.is-hidden { opacity: 0; pointer-events: none; transform: scale(.6); }

/* ============================================================
   导入浮层
   ============================================================ */
.import-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(16, 20, 30, .48);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade-in .2s ease;
}
@keyframes fade-in { from { opacity: 0; } }

.import-card {
  width: 100%;
  max-width: 400px;
  padding: 22px;
  border-radius: 16px;
  background: var(--bg-elev);
  box-shadow: var(--shadow-lg);
  animation: pop-in .26s cubic-bezier(.22, .9, .3, 1);
}
@keyframes pop-in { from { opacity: 0; transform: scale(.94) translateY(8px); } }

.import-head { display: flex; align-items: center; gap: 10px; font-size: 15px; font-weight: 600; }
.import-file {
  margin: 12px 0 10px;
  font-size: 12.5px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}
.import-bar { height: 4px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.import-bar-fill {
  height: 100%;
  width: 0%;
  border-radius: 3px;
  background: var(--brand);
  transition: width .22s ease;
}
.import-log {
  margin-top: 12px;
  max-height: 108px;
  overflow-y: auto;
  font-size: 12px;
  line-height: 1.75;
  color: var(--text-2);
}
.import-log .log-ok::before { content: "✓ "; color: #2F855A; font-weight: 700; }
.import-log .log-fail::before { content: "✕ "; color: var(--danger); font-weight: 700; }
.import-log .log-warn::before { content: "! "; color: #B7791F; font-weight: 700; }

.spinner {
  width: 17px;
  height: 17px;
  flex: 0 0 auto;
  border: 2px solid color-mix(in srgb, var(--text-3) 35%, transparent);
  border-top-color: var(--text-3);
  border-radius: 50%;
  animation: spin .72s linear infinite;
}
.spinner-dark { border-color: #D6DAE2; border-top-color: var(--brand-strong); }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================
   书籍操作面板
   ============================================================ */
.sheet-scrim {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(16, 20, 30, .42);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  animation: fade-in .18s ease;
}
.book-sheet {
  position: fixed;
  z-index: 151;
  left: 50%;
  bottom: calc(14px + var(--safe-b));
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 28px));
  padding: 18px;
  border-radius: 18px;
  background: var(--bg-elev);
  box-shadow: var(--shadow-lg);
  animation: sheet-in .28s cubic-bezier(.22, .9, .3, 1);
}
@keyframes sheet-in { from { opacity: 0; transform: translateX(-50%) translateY(22px); } }

.sheet-head { display: flex; gap: 13px; padding-bottom: 15px; border-bottom: 1px solid var(--line); }
.sheet-cover {
  width: 54px;
  height: 81px;
  flex: 0 0 54px;
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface-2);
  box-shadow: var(--shadow-sm);
  display: grid;
  place-items: center;
}
.sheet-cover img { width: 100%; height: 100%; object-fit: cover; }
.sheet-meta { flex: 1 1 auto; min-width: 0; padding-top: 3px; }
.sheet-title {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.sheet-sub { margin-top: 5px; font-size: 12.5px; color: var(--text-3); line-height: 1.5; }

.sheet-actions { display: flex; flex-direction: column; padding-top: 6px; }
.sheet-action {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 48px;
  padding: 0 4px;
  font-size: 15px;
  color: var(--text);
  text-align: left;
  border-radius: 10px;
}
.sheet-action:active { background: var(--surface-2); }
.sheet-action svg { width: 19px; height: 19px; color: var(--text-2); flex: 0 0 auto; }
.sheet-action.danger { color: var(--danger); }
.sheet-action.danger svg { color: var(--danger); }

/* ============================================================
   对话框 / Toast
   ============================================================ */
.dialog-wrap {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 26px;
  background: rgba(16, 20, 30, .48);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade-in .18s ease;
}
.dialog-card {
  width: 100%;
  max-width: 380px;
  padding: 22px;
  border-radius: 16px;
  background: var(--bg-elev);
  box-shadow: var(--shadow-lg);
  animation: pop-in .24s cubic-bezier(.22, .9, .3, 1);
}
.dialog-card h3 { margin: 0 0 10px; font-size: 17px; font-weight: 600; }
.dialog-body { font-size: 14px; line-height: 1.7; color: var(--text-2); max-height: 46vh; overflow-y: auto; }
.dialog-body b { color: var(--text); }
.dialog-body .kv { display: flex; gap: 10px; padding: 3px 0; font-size: 13.5px; }
.dialog-body .kv > span:first-child { flex: 0 0 72px; color: var(--text-3); }
.dialog-body .kv > span:last-child { flex: 1 1 auto; word-break: break-all; }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.dialog-actions .btn { min-height: 42px; padding: 0 18px; font-size: 14.5px; }

.toast {
  position: fixed;
  z-index: 400;
  left: 50%;
  bottom: calc(84px + var(--safe-b));
  transform: translateX(-50%) translateY(14px);
  max-width: min(86vw, 460px);
  padding: 11px 18px;
  border-radius: 11px;
  font-size: 13.5px;
  line-height: 1.5;
  text-align: center;
  color: #fff;
  background: rgba(24, 28, 38, .93);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease, transform .22s cubic-bezier(.22, .9, .3, 1);
}
.toast.is-shown { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============================================================
   添加到主屏引导
   ============================================================ */
.a2hs-tip {
  position: fixed;
  z-index: 120;
  left: 12px;
  right: 12px;
  bottom: calc(12px + var(--safe-b));
  animation: sheet-in-flat .3s cubic-bezier(.22, .9, .3, 1);
}
@keyframes sheet-in-flat { from { opacity: 0; transform: translateY(16px); } }
.a2hs-inner {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 560px;
  margin: 0 auto;
  padding: 13px 14px;
  border-radius: 14px;
  color: #fff;
  background: rgba(24, 28, 38, .95);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-lg);
}
.a2hs-icon { width: 22px; height: 22px; flex: 0 0 auto; stroke-width: 1.7; }
.a2hs-text { display: flex; flex-direction: column; gap: 1px; flex: 1 1 auto; min-width: 0; }
.a2hs-text b { font-size: 13.5px; font-weight: 600; }
.a2hs-text span { font-size: 12px; opacity: .74; }
.a2hs-close {
  width: 30px; height: 30px;
  flex: 0 0 auto;
  display: grid; place-items: center;
  border-radius: 8px;
  font-size: 14px;
  color: rgba(255, 255, 255, .62);
}

/* ============================================================
   通用
   ============================================================ */
[hidden] { display: none !important; }

::selection { background: color-mix(in srgb, var(--brand) 26%, transparent); }

.shelf-scroll::-webkit-scrollbar,
.import-log::-webkit-scrollbar,
.dialog-body::-webkit-scrollbar { width: 6px; }
.shelf-scroll::-webkit-scrollbar-thumb,
.import-log::-webkit-scrollbar-thumb,
.dialog-body::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--text-3) 40%, transparent);
  border-radius: 3px;
}

/* ---------- 拖拽导入反馈 ---------- */
body.is-dragging::after {
  content: "松手即可导入";
  position: fixed;
  inset: 14px;
  z-index: 500;
  display: grid;
  place-items: center;
  border: 2px dashed var(--brand);
  border-radius: 20px;
  font-size: 17px;
  font-weight: 550;
  color: var(--brand);
  background: color-mix(in srgb, var(--brand) 9%, transparent);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
