/* ===== 绘本馆共享样式 · 移动优先 / 响应式 / 无障碍 ===== */
:root {
  --primary: #ff9a3c;
  --primary-dark: #e67e22;
  --bg: #f5f1e8;
  --paper: #fffef8;
  --ink: #33352f;
  --muted: #8a877c;
  --shadow-sm: 0 2px 10px rgba(40, 50, 30, 0.10);
  --shadow-md: 0 8px 30px rgba(40, 50, 30, 0.16);
  --radius: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", "Noto Sans SC", sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior-y: none;
}

img { display: block; max-width: 100%; }

button { font-family: inherit; cursor: pointer; }

/* 可访问性：键盘聚焦可见 */
:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

/* ===== 顶部条 ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: rgba(245, 241, 232, 0.92);
  backdrop-filter: saturate(160%) blur(8px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.topbar .back {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #ff9a3c;
  box-shadow: 0 2px 10px rgba(255, 154, 60, 0.35);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.15s;
}
.topbar .back svg { width: 22px; height: 22px; }
.topbar .back:hover { background: #f58522; transform: scale(1.05); }

/* 返回顶部悬浮按钮：与 .back / .speaker 同款橙色圆形图标 */
.to-top {
  position: fixed;
  right: 18px;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 30;
  width: 46px;
  height: 46px;
  border: none;
  border-radius: 50%;
  background: #ff9a3c;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(255, 154, 60, 0.40);
  opacity: 0;
  visibility: hidden;
  transform: translateY(12px);
  transition: background 0.15s, transform 0.2s, opacity 0.2s, visibility 0.2s;
}
.to-top svg { width: 24px; height: 24px; }
.to-top.show { opacity: 1; visibility: visible; transform: translateY(0); }
.to-top:hover { background: #f58522; transform: translateY(-2px); }
.to-top:active { transform: translateY(0) scale(0.96); }

/* ===== 图书馆首页 ===== */
.library { max-width: 1100px; margin: 0 auto; padding: 22px 16px 48px; }

.library-hero {
  text-align: center;
  padding: 14px 10px 26px;
}
.library-hero h1 { font-size: 1.7rem; font-weight: 800; letter-spacing: 0.5px; }
.library-hero p { color: var(--muted); margin-top: 6px; font-size: 0.92rem; }

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 18px;
}

/* ===== 等级筛选条（单行横滑） ===== */
.level-bar {
  position: sticky;
  top: 0;
  z-index: 15;
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  padding: 6px 2px 14px;
  margin-bottom: 8px;
  background: linear-gradient(var(--bg) 70%, rgba(245, 241, 232, 0));
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.level-bar::-webkit-scrollbar { display: none; }
.level-bar.hidden {
  transform: translateY(-130%);
  opacity: 0;
  pointer-events: none;
}
.level-chip {
  flex: 0 0 auto;
  border: 1px solid rgba(255, 154, 60, 0.35);
  background: #fff;
  color: var(--primary-dark);
  border-radius: 999px;
  padding: 7px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, transform 0.15s;
}
.level-chip:hover { transform: translateY(-1px); }
.level-chip.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.level-chip.empty {
  background: #e8e4db;
  color: #a5a295;
  border-color: rgba(0, 0, 0, 0.08);
  cursor: default;
}
.level-chip.empty:hover { transform: none; }

.book-card {
  background: var(--paper);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  text-decoration: none;
  color: inherit;
  display: flex;
  flex-direction: column;
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.book-card:hover { transform: translateY(-4px); box-shadow: var(--shadow-md); }
.book-card:active { transform: translateY(-1px); }

.book-card .cover {
  aspect-ratio: 792 / 1224;       /* 精确匹配真实封面比例，避免封面被裁切 */
  background: #efe9da;
  overflow: hidden;
}
.book-card .cover img {
  width: 100%; height: 100%;
  object-fit: contain;          /* 横版封面(翻面书)也完整显示,不裁切 */
  object-position: center;
  background: #fff;
}
.book-card .meta { padding: 9px 11px 12px; text-align: center; }
.book-card .meta .title { font-size: 0.92rem; font-weight: 700; line-height: 1.25; }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 60px 20px;
}

/* 阅读页作用域：锁定视口，禁整页滚动/橡皮筋/双击缩放/长按菜单 */
body.reader-page {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
body.reader-page .reader {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ===== 阅读器 ===== */
.reader { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

.reader .stage {
  position: relative;
  flex: 1;
  min-height: 0;                 /* 关键：允许收缩，避免 page-frame 超出被裁 */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 10px 6px;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
}

.page-frame {
  position: relative;
  height: 100%;
  aspect-ratio: 792 / 1224;      /* 精确匹配真实内页比例，完整显示不裁切 */
  max-height: 100%;
  max-width: 100%;
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  overflow: hidden;
}
.page-frame img {
  width: 100%; height: 100%;
  object-fit: contain;
  background: #fff;
  user-select: none;
  -webkit-touch-callout: none;
}

/* 翻页淡入淡出 */
.page-frame img.is-leaving { animation: fadeOut 0.18s ease forwards; }
.page-frame img.is-entering { animation: fadeIn 0.22s ease forwards; }
@keyframes fadeIn { from { opacity: 0; transform: scale(0.99); } to { opacity: 1; transform: scale(1); } }
@keyframes fadeOut { to { opacity: 0; } }

/* 左右点击区（移动端友好） */
.tap-zone {
  position: absolute;
  top: 0; bottom: 0;
  width: 28%;
  z-index: 8;
  border: none;
  background: transparent;
}
.tap-zone.prev { left: 0; }
.tap-zone.next { right: 0; }

/* 箭头按钮 */
.arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 46px; height: 46px;
  border: none; border-radius: 50%;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: var(--shadow-sm);
  font-size: 1.5rem; color: #555;
  z-index: 12;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, color 0.15s, transform 0.15s;
}
.arrow:hover { background: var(--primary); color: #fff; }
.arrow:disabled { opacity: 0.22; cursor: default; }
.arrow.prev { left: 10px; }
.arrow.next { right: 10px; }

/* 喇叭按钮（底部栏，不遮挡图片） */
.speaker {
  width: 56px; height: 56px;
  border: none; border-radius: 50%;
  background: #ff9a3c;
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 16px rgba(255, 154, 60, 0.40);
  transition: background 0.15s, transform 0.15s;
  user-select: none;
  flex: 0 0 auto;
}
.speaker svg { width: 26px; height: 26px; }
.speaker:hover { background: #f58522; transform: scale(1.06); }
.speaker:active { transform: scale(0.96); }
.speaker:disabled { background: #c9c4b8; box-shadow: none; cursor: default; }
.speaker.speaking { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.12); } }

/* 底部开关（自动朗读 / 译文） */
.toggle {
  flex: 0 0 auto;
  border: 1px solid rgba(255, 154, 60, 0.4);
  background: #fff;
  color: var(--primary-dark);
  border-radius: 999px;
  padding: 9px 16px;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, transform 0.15s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.toggle:hover { transform: translateY(-1px); }
.toggle.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* 中文翻译层（图片下方独立条，不遮挡） */
.translation {
  flex: 0 0 auto;
  text-align: center;
  padding: 10px 18px;
  margin: 0 16px;
  background: rgba(255, 154, 60, 0.12);
  color: var(--primary-dark);
  border-radius: 12px;
  font-size: 1.05rem;
  line-height: 1.5;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.translation[hidden] { display: none; }

/* 底部进度条 */
.progress-bar {
  flex-shrink: 0;
  height: 5px;
  background: rgba(0, 0, 0, 0.08);
  position: relative;
}
.progress-bar .fill {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  background: var(--primary);
  width: 0;
  transition: width 0.25s ease;
}

.reader .footer {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 12px 16px calc(14px + env(safe-area-inset-bottom));
}

/* ===== 响应式 ===== */
@media (min-width: 720px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 22px; }
  .library-hero h1 { font-size: 2rem; }
  .arrow { width: 54px; height: 54px; font-size: 1.7rem; }
  .speaker { width: 58px; height: 58px; font-size: 1.6rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
