:root {
  --bg: #fafaf7;
  --panel: #ffffff;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --line: #e7e4dc;
  --accent: #8b3a2f;
  --accent-soft: #f2e8e4;
  --code: #f4f1ea;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ================ 视图切换 ================ */
#library, #reader {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: opacity 0.2s ease;
}
body.view-library #reader { opacity: 0; pointer-events: none; }
body.view-reader #library { opacity: 0; pointer-events: none; }

/* ================ 目录页 ================ */
#lib-head {
  padding: calc(18px + var(--safe-top)) 18px 0;
  background: var(--bg);
  flex-shrink: 0;
}
#lib-head h1 {
  margin: 0 0 14px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.5px;
  color: var(--accent);
}
#search {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 15px;
  background: var(--panel);
  -webkit-appearance: none;
}
#search:focus { outline: none; border-color: var(--accent); }

#tabs {
  display: flex;
  margin-top: 16px;
  gap: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 4px;
  border-radius: 10px;
}
#tabs button {
  flex: 1;
  padding: 9px 10px;
  background: transparent;
  border: none;
  border-radius: 7px;
  cursor: pointer;
  font-size: 14px;
  color: var(--muted);
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}
#tabs button.active { background: var(--accent); color: #fff; }

/* ---- 堆叠面板 ---- */
#stack {
  flex: 1;
  position: relative;
  margin-top: 16px;
  overflow: hidden;
}
.panel {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  will-change: transform;
}
.panel[data-level="1"] { transform: translateX(0); }
.panel[data-level="2"] { transform: translateX(100%); }
.panel[data-level="3"] { transform: translateX(100%); }
#stack[data-level="2"] .panel[data-level="1"] { transform: translateX(-30%); }
#stack[data-level="2"] .panel[data-level="2"] { transform: translateX(0); }
#stack[data-level="3"] .panel[data-level="1"] { transform: translateX(-30%); }
#stack[data-level="3"] .panel[data-level="2"] { transform: translateX(-30%); }
#stack[data-level="3"] .panel[data-level="3"] { transform: translateX(0); }

.panel-head {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px 12px 4px;
  flex-shrink: 0;
}
.panel-head .back {
  background: transparent;
  border: none;
  font-size: 28px;
  line-height: 1;
  padding: 4px 12px;
  color: var(--accent);
  cursor: pointer;
  font-weight: 300;
}
.panel-head .back:active { opacity: 0.5; }
.panel-head .crumb {
  font-size: 15px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- 列表 ---- */
.list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 18px calc(40px + var(--safe-bottom));
}
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 8px;
  font-size: 15.5px;
  cursor: pointer;
  user-select: none;
  transition: background 0.12s, transform 0.12s;
}
.row:active { background: var(--accent-soft); transform: scale(0.985); }
.row .arrow {
  color: var(--muted);
  font-size: 18px;
  flex-shrink: 0;
  margin-left: 8px;
}
.row .badge {
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
  margin-left: 8px;
}
.row.version-row {
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}
.row.version-row .v-name { font-size: 15.5px; font-weight: 500; }
.row.version-row .v-source { font-size: 12.5px; color: var(--muted); }

.empty {
  padding: 40px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

/* ================ 阅读页 ================ */
#reader-back {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: 12px;
  z-index: 10;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  transition: opacity 0.2s ease;
  opacity: 0.85;
}
#reader-back:hover, #reader-back:focus { opacity: 1; }
#reader-back:active { transform: scale(0.94); }

#reader { overflow-y: auto; -webkit-overflow-scrolling: touch; }
#article {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(64px + var(--safe-top)) 22px calc(60px + var(--safe-bottom));
  line-height: 1.95;
  font-size: 16.5px;
  color: var(--ink);
}
#article h1, #article h2, #article h3 { color: var(--accent); margin-top: 1.7em; line-height: 1.4; }
#article h1 { font-size: 22px; }
#article h2 { font-size: 19px; }
#article h3 { font-size: 17px; }
#article p { margin: 1em 0; }
#article blockquote {
  border-left: 3px solid var(--accent);
  padding: 6px 16px;
  color: var(--muted);
  margin: 18px 0;
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
}
#article code {
  background: var(--code);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.9em;
}
#article pre {
  background: var(--code);
  padding: 14px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 16px -6px;
}
#article a { color: var(--accent); }
#article hr { border: none; border-top: 1px solid var(--line); margin: 28px 0; }
#article img { max-width: 100%; height: auto; border-radius: 4px; }
#article .loading, #article .error {
  padding: 40px 0;
  text-align: center;
  color: var(--muted);
}

/* ================ 桌面端：限制宽度居中 ================ */
@media (min-width: 720px) {
  #library {
    max-width: 560px;
    left: 50%;
    transform: translateX(-50%);
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
    background: var(--bg);
  }
  #lib-head { padding-top: 28px; }
  #article {
    padding-top: 80px;
    padding-bottom: 80px;
    font-size: 17px;
  }
  #reader-back {
    top: 24px;
    left: 24px;
    width: 44px;
    height: 44px;
  }
}
