/* ===================================================================
 * JM 阅读器 · iOS Web Clip / PWA 样式
 *
 * 设计约束：
 *   - iPhone 刘海/灵动岛 + Home Indicator：用 env(safe-area-inset-*) 让位
 *   - 全屏 Web Clip 无地址栏：状态栏是 black-translucent，顶部要自己垫高
 *   - 触屏优先：按钮最小 44px，点击区够大
 *   - 深色底：漫画阅读场景下深色更护眼，也贴合原 App 的深色主题
 * =================================================================== */

:root {
  --bg: #12141a;
  --bg-2: #1b1e26;
  --bg-3: #232733;
  --fg: #e8eaf0;
  --fg-dim: #9aa1b1;
  --accent: #ff5c7a;
  --accent-2: #4a9eff;
  --line: #2e3340;
  --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: 48px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/*
 * 关键：隐藏语义必须由作者样式强制接管。
 *
 * 浏览器 UA 样式表里的 `[hidden] { display: none }` 特异性只有 (0,1,0)，
 * 而本文档里 #reader / #chapter-sheet / #scrim 这类 ID 选择器写了
 * `display: flex`，特异性 (1,0,0) 直接盖掉 UA 规则 —— 结果是
 * `<section id="reader" hidden>` 在首帧就是一个 position:fixed; inset:0;
 * background:#000 的全屏黑色遮罩，整个 App 被盖住 → 打开就是黑屏。
 *
 * !important 让这条规则的优先级高于任何作者样式里的 display 声明，
 * 同时不影响 JS 用 .hidden 属性切换显隐（隐藏时置 true，属性在即 display:none）。
 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overscroll-behavior-y: none;
  -webkit-text-size-adjust: 100%;
}

body { padding-top: calc(var(--safe-t) + var(--topbar-h)); }

button, input, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

.muted { color: var(--fg-dim); }
.small { font-size: 12px; }
.wide { width: 100%; }

/* ---------------- 顶部栏 ---------------- */

#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: calc(var(--safe-t) + var(--topbar-h));
  padding: var(--safe-t) calc(var(--safe-r) + 6px) 0 calc(var(--safe-l) + 6px);
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(18, 20, 26, 0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
  z-index: 40;
  /* 顶栏跟随下层页面一起推（iOS 导航栏随导航栈移动） */
  transition: transform var(--dur-ios) var(--ease-ios),
              filter var(--dur-ios) var(--ease-ios);
  will-change: transform;
}

/*
 * 详情页推开时，顶栏整体左移出屏。
 *
 * iOS 的 push 是「整条导航栏被新页面盖住」—— 新页面自带导航栏，
 * 旧导航栏随着旧页面一起退到屏幕左边外。
 * 只移 -26% 会让两个标题同时露在屏幕上（旧的居中标题 + 新的导航栏），
 * 所以这里直接移出屏（-100%），由详情页自己的导航栏独占顶部。
 */
body.detail-open #topbar {
  transform: translateX(-100%);
  filter: brightness(.62) saturate(.9);
}

#title {
  flex: 1;
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.icon-btn {
  min-width: 40px;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--fg);
  cursor: pointer;
  padding: 8px;
  border-radius: 10px;
}

.icon-btn:active { background: var(--bg-3); }

#btn-menu { flex-direction: column; gap: 4px; }
#btn-menu .bar {
  display: block;
  width: 19px;
  height: 2px;
  background: currentColor;
  border-radius: 2px;
}

/* ---------------- 抽屉 ---------------- */

#scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 50;
}

#drawer {
  position: fixed;
  top: 0; bottom: 0; left: 0;
  width: min(84vw, 340px);
  padding: calc(var(--safe-t) + 12px) 16px calc(var(--safe-b) + 20px) calc(var(--safe-l) + 16px);
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  z-index: 51;
  transform: translateX(-102%);
  transition: transform .24s cubic-bezier(.4, 0, .2, 1);
}

#drawer.open { transform: translateX(0); }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.drawer-sec { margin-bottom: 22px; }
.drawer-sec h3 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--fg-dim);
  letter-spacing: .04em;
}

.tag-groups { display: flex; flex-direction: column; gap: 12px; }
.tag-group-title {
  font-size: 12px;
  color: var(--accent-2);
  margin-bottom: 6px;
  font-weight: 600;
}

.line-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}
.line-name {
  font-size: 12px;
  color: var(--fg-dim);
  word-break: break-all;
}

.text-btn {
  background: none;
  border: none;
  color: var(--accent-2);
  padding: 6px 0;
  cursor: pointer;
  font-size: 14px;
}

.row-gap { display: flex; gap: 8px; margin: 8px 0; }
.row-gap input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--fg);
}
.row-gap input:focus { outline: none; border-color: var(--accent-2); }

/* ---------------- 按钮 ---------------- */

.primary-btn {
  padding: 10px 18px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  min-height: 40px;
}

.ghost-btn {
  padding: 10px 18px;
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  min-height: 40px;
}

.primary-btn:active, .ghost-btn:active { opacity: .75; }

/* ---------------- 芯片 / 标签 ---------------- */

.chip-wrap { display: flex; flex-wrap: wrap; gap: 7px; }

.chip {
  padding: 6px 12px;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  color: var(--fg);
}

.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.chip:active { transform: scale(.96); }

.chip-row {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  padding: 8px calc(var(--safe-r) + 12px) 8px calc(var(--safe-l) + 12px);
  scrollbar-width: none;
}
.chip-row::-webkit-scrollbar { display: none; }

/* ---------------- 排序栏 / 分类条 / 标签条 ---------------- */

#sortbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px calc(var(--safe-r) + 12px) 4px calc(var(--safe-l) + 12px);
}

.mini-select {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  color: var(--fg);
  min-height: 32px;
}

#list-info { margin-left: auto; }

#catbar, #tagbar {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  padding: 4px calc(var(--safe-r) + 12px) 4px calc(var(--safe-l) + 12px);
  scrollbar-width: none;
}
#catbar::-webkit-scrollbar, #tagbar::-webkit-scrollbar { display: none; }
#catbar .chip, #tagbar .chip { flex: 0 0 auto; }

/* ---------------- 搜索面板 ---------------- */

#search-panel {
  position: fixed;
  top: calc(var(--safe-t) + var(--topbar-h));
  left: 0; right: 0;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  padding: 10px 0 8px;
  z-index: 35;
}

.search-bar {
  display: flex;
  gap: 8px;
  padding: 0 calc(var(--safe-r) + 12px) 8px calc(var(--safe-l) + 12px);
}

.search-bar input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--fg);
}
.search-bar input:focus { outline: none; border-color: var(--accent-2); }

/* ---------------- 主内容 / 瀑布流 ---------------- */

#main {
  padding: 8px calc(var(--safe-r) + 12px) calc(var(--safe-b) + 24px) calc(var(--safe-l) + 12px);
}

/*
 * iOS 风格转场：push / modal
 *
 * 参照 UIKit 的 UINavigationController push 与 modal present：
 *   - push：详情页从右侧滑入，同时下层页面左移并压暗（视差）
 *   - modal：阅读器从底部升起，同时下层页面等比缩小并后退
 * 缓动曲线用 cubic-bezier(.32,.72,0,1) —— 这是 iOS 导航转场的手感曲线，
 * 比默认的 ease 快出慢入，接近原生。
 */
:root {
  --ease-ios: cubic-bezier(.32, .72, 0, 1);
  --dur-ios: .36s;
}

/*
 * 下层页面被推走时的状态（push 的视差背景）
 *
 * transform 用 CSS 变量组合，而不是各写一条 transform ——
 * 因为 #detail 自身既要参与 push（右滑进入），又可能作为 modal 的下层被缩小，
 * 两条规则各写一条 transform 会互相覆盖。
 * 拆成 --push-x / --modal-s 两个变量后就能叠加。
 */
#main, #detail {
  --push-x: 0%;
  --modal-s: 1;
  transform: translateX(var(--push-x)) scale(var(--modal-s));
  transition: transform var(--dur-ios) var(--ease-ios),
              filter var(--dur-ios) var(--ease-ios);
  will-change: transform;
}

#main.pushed-back {
  --push-x: -26%;
  filter: brightness(.62) saturate(.9);
}

#main.modal-back {
  --modal-s: .9;
  filter: brightness(.55) saturate(.85);
  border-radius: 14px;
}

/* 详情页：改成固定层，做右侧滑入 */
#detail {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 38;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  padding-top: calc(var(--safe-t) + var(--topbar-h));
  --push-x: 100%;
  transition: transform var(--dur-ios) var(--ease-ios),
              filter var(--dur-ios) var(--ease-ios);
  will-change: transform;
}

#detail.push-in { --push-x: 0%; }

#detail.modal-back {
  --modal-s: .9;
  filter: brightness(.55) saturate(.85);
  border-radius: 14px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 10px;
}

/*
 * 卡片按压回弹。
 * iOS 上按下会有轻微缩小 + 变暗，抬起回弹 —— 这里用 cubic-bezier(.2,.9,.3,1.3)
 * 做出那一点点过冲的「弹」。
 */
.card {
  cursor: pointer;
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg-2);
  transition: transform .18s cubic-bezier(.2, .9, .3, 1.3), opacity .18s ease;
}
.card:active {
  transform: scale(.955);
  opacity: .82;
}

/* 列表加载中的骨架微光（首屏没有内容时给个呼吸感） */
.grid.is-loading { opacity: .55; transition: opacity .25s ease; }

/*
 * 骨架屏：首屏列表还没数据时先摆一批灰色占位块。
 * 没有它的话，首屏是「空网格 + 底部一个加载更多按钮」，
 * 看起来像页面坏了；有了它至少是有节奏的加载状态。
 */
.skeleton-card {
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg-2);
}
.skeleton-card .sk-cover {
  width: 100%;
  aspect-ratio: 3 / 4;
  background: linear-gradient(100deg, var(--bg-3) 28%, #2b3040 46%, var(--bg-3) 64%);
  background-size: 240% 100%;
  animation: sk-shimmer 1.25s linear infinite;
}
.skeleton-card .sk-line {
  height: 9px;
  margin: 8px 7px 0;
  border-radius: 5px;
  background: var(--bg-3);
}
.skeleton-card .sk-line.short { width: 52%; margin-bottom: 9px; }

@keyframes sk-shimmer {
  from { background-position: 120% 0; }
  to { background-position: -120% 0; }
}

/* 按钮加载态：转圈 + 禁用连点 */
.ghost-btn.is-busy, .primary-btn.is-busy {
  opacity: .6;
  pointer-events: none;
}
.ghost-btn.is-busy::after {
  content: '';
  display: inline-block;
  width: 12px; height: 12px;
  margin-left: 7px;
  vertical-align: -2px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

.card-cover {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  overflow: hidden;
  /* 封面加载中的占位底色（图片到位前是浅灰渐变，不是纯黑） */
  background: linear-gradient(100deg, var(--bg-3) 30%, #2b3040 50%, var(--bg-3) 70%);
  background-size: 220% 100%;
}

.card-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  /*
   * 封面默认直接可见。
   *
   * 曾经用 opacity:0 + .is-in 做淡入（0.3s），但那段时间图片是半透明的，
   * 整屏看起来像「图片加载不出来」—— 首屏观感反而更差。
   * 淡入的收益（重绘时不突兀）远小于代价（首屏半透明），所以去掉。
   */
}

.card-cover .badge {
  position: absolute;
  top: 5px; right: 5px;
  padding: 2px 6px;
  font-size: 10px;
  border-radius: 6px;
  background: rgba(0, 0, 0, .62);
  color: #ffd66b;
}

.card-title {
  padding: 6px 7px 8px;
  font-size: 12px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-author {
  padding: 0 7px 7px;
  font-size: 11px;
  color: var(--fg-dim);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

#pager { padding: 18px 0; text-align: center; }

/* ---------------- 详情面板 ---------------- */
/* #detail 的布局已在「iOS 风格转场」一节统一定义（fixed 层 + 右侧滑入） */

/* 详情页自己的导航栏 */
#detail-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  height: calc(var(--topbar-h));
  margin: calc(var(--safe-t) * -1) calc(var(--safe-l) * -1) 0 calc(var(--safe-r) * -1);
  padding: 0 calc(var(--safe-r) + 6px) 0 calc(var(--safe-l) + 6px);
  background: rgba(18, 20, 26, 0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}

#detail-title {
  flex: 1;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.head-spacer { width: 40px; flex: 0 0 auto; }

#detail > * { padding-left: calc(var(--safe-l) + 14px); padding-right: calc(var(--safe-r) + 14px); }
#detail > .detail-top { padding-left: 0; padding-right: 0; }
#detail > .chip-wrap { padding-left: calc(var(--safe-l) + 14px); padding-right: calc(var(--safe-r) + 14px); }
#detail > .btn-detail-close { margin: 20px calc(var(--safe-r) + 14px) calc(var(--safe-b) + 28px) calc(var(--safe-l) + 14px); width: auto; }

.detail-top { display: flex; gap: 14px; margin-bottom: 14px; }

.d-cover {
  width: 116px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 10px;
  background: var(--bg-3);
  flex: 0 0 auto;
}

.d-meta { flex: 1; min-width: 0; }
.d-meta h2 { margin: 0 0 6px; font-size: 17px; line-height: 1.3; }
.d-meta p { margin: 2px 0; }

.d-desc {
  color: var(--fg-dim);
  font-size: 13px;
  white-space: pre-wrap;
  margin: 12px 0;
}

.sec-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin: 18px 0 10px;
}
.sec-head h3 { margin: 0; font-size: 15px; }

.chapter-list { display: flex; flex-direction: column; gap: 7px; }

.chapter-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  width: 100%;
}

.chapter-item:active { background: var(--bg-3); }
.chapter-item .ch-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.chapter-item .ch-idx { color: var(--fg-dim); font-size: 12px; }

/* ---------------- 阅读器 ---------------- */

#reader {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 60;
  display: flex;
  flex-direction: column;
  /* iOS modal present：从底部升起 */
  transform: translateY(100%);
  transition: transform var(--dur-ios) var(--ease-ios);
  will-change: transform;
}

#reader.modal-in { transform: translateY(0); }

#reader-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-t) + 6px) calc(var(--safe-r) + 10px) 6px calc(var(--safe-l) + 10px);
  background: rgba(10, 10, 12, .93);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid #1e1e22;
  flex: 0 0 auto;
}

#reader-head span:first-of-type {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 14px;
}

#pages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 0 12px;
}

/*
 * 章节切换转场：内容淡入 + 轻微上移。
 *
 * iOS 上切换章节是「旧内容淡出、新内容淡入」，这里用一层淡入近似，
 * 加上 8px 的上移让方向感更明确（往下切 = 内容从下往上）。
 */
#pages.chapter-enter {
  animation: chapter-in .3s var(--ease-ios) both;
}

@keyframes chapter-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.page-slot {
  position: relative;
  width: 100%;
  background: #000;
}

.page-slot canvas {
  display: block;
  width: 100%;
  height: auto;
  /* 图片到位后淡入，避免「啪」地一下出现 */
  opacity: 0;
  transform: scale(.985);
  transition: opacity .34s ease, transform .34s var(--ease-ios);
}

.page-slot canvas.is-in { opacity: 1; transform: scale(1); }

.page-slot .ph {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
  font-size: 12px;
}

.page-idx {
  position: absolute;
  right: 8px;
  bottom: 6px;
  padding: 1px 7px;
  font-size: 11px;
  border-radius: 8px;
  background: rgba(0, 0, 0, .55);
  color: #ddd;
}

#reader-foot {
  display: flex;
  gap: 10px;
  padding: 8px calc(var(--safe-r) + 10px) calc(var(--safe-b) + 8px) calc(var(--safe-l) + 10px);
  background: rgba(10, 10, 12, .93);
  border-top: 1px solid #1e1e22;
  flex: 0 0 auto;
}
#reader-foot button { flex: 1; }

/* ---------------- 章节浮层 ---------------- */

#chapter-sheet {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .6);
  z-index: 70;
  display: flex;
  align-items: flex-end;
}

.sheet-inner {
  width: 100%;
  max-height: 74vh;
  background: var(--bg-2);
  border-radius: 16px 16px 0 0;
  padding: 14px 0 calc(var(--safe-b) + 14px);
  display: flex;
  flex-direction: column;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px 10px;
  border-bottom: 1px solid var(--line);
}

#chapter-sheet-list {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 16px 0;
}

/* ---------------- 提示条 ---------------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 80px);
  transform: translateX(-50%);
  max-width: 84vw;
  padding: 10px 16px;
  background: rgba(30, 33, 42, .97);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 13px;
  z-index: 90;
  text-align: center;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .5);
}

.spinner {
  display: inline-block;
  width: 15px; height: 15px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 桌面浏览器预览时居中收窄，避免太宽 */
@media (min-width: 700px) {
  #main, #detail { max-width: 720px; margin: 0 auto; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(124px, 1fr)); }
}
