:root {
  --ink: #1c1410;
  --ink-soft: #2a211c;
  --ember: #d3542f;
  --ember-deep: #8f2a14;
  --gold: #d6b36a;
  --gold-dim: #a8844a;
  --paper: #e7dcc8;
  --paper-deep: #d8c9ae;
  --line: rgba(28, 20, 16, 0.12);
  --text: #1c1410;
  --muted: rgba(28, 20, 16, 0.55);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --topbar-h: 52px;
  --toolbar-h: 58px;
  --fs: 22px;
  --lh: 1.75;
  --ruby: 10px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--ink);
  color: var(--paper);
  font-family: "Noto Serif TC", "Songti TC", "SimSun", serif;
}

button {
  font: inherit;
  border: 0;
  background: none;
  color: inherit;
  cursor: pointer;
}

#app {
  height: 100%;
  height: 100dvh;
  position: relative;
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}

.screen.active {
  opacity: 1;
  pointer-events: auto;
}

.screen[hidden] {
  display: none !important;
}

/* —— 首页 —— */
.home {
  display: grid;
  place-items: center;
  overflow: hidden;
}

.home-bg {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 55% at 50% 18%, rgba(211, 84, 47, 0.35), transparent 60%),
    radial-gradient(ellipse 70% 50% at 70% 80%, rgba(214, 179, 106, 0.12), transparent 55%),
    linear-gradient(165deg, #2a1710 0%, #140f0c 42%, #0d0a08 100%);
}

.home-bg::before {
  content: "";
  position: absolute;
  inset: -10%;
  background:
    repeating-linear-gradient(
      -18deg,
      transparent 0 14px,
      rgba(214, 179, 106, 0.03) 14px 15px
    );
  animation: drift 18s linear infinite;
}

.home-bg::after {
  content: "";
  position: absolute;
  width: min(72vw, 320px);
  height: min(72vw, 320px);
  left: 50%;
  top: 38%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 1px solid rgba(214, 179, 106, 0.18);
  box-shadow:
    0 0 0 18px rgba(211, 84, 47, 0.04),
    inset 0 0 60px rgba(211, 84, 47, 0.08);
  animation: pulse 5.5s ease-in-out infinite;
}

@keyframes drift {
  to { transform: translateX(-28px); }
}

@keyframes pulse {
  0%, 100% { opacity: 0.7; transform: translate(-50%, -50%) scale(1); }
  50% { opacity: 1; transform: translate(-50%, -50%) scale(1.04); }
}

.home-veil {
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, transparent 40%, rgba(13, 10, 8, 0.75) 100%);
}

.home-content {
  position: relative;
  z-index: 1;
  width: min(88vw, 420px);
  text-align: center;
  padding: calc(var(--safe-top) + 28px) 0 calc(var(--safe-bottom) + 36px);
}

.home-series {
  margin: 0 0 18px;
  letter-spacing: 0.55em;
  font-size: 12px;
  color: var(--gold);
  text-indent: 0.55em;
}

.home-title {
  margin: 0;
  font-family: "ZCOOL XiaoWei", "Noto Serif TC", serif;
  font-weight: 400;
  line-height: 1.15;
}

.home-title-main {
  display: inline-block;
  writing-mode: vertical-rl;
  text-orientation: upright;
  font-size: clamp(42px, 12vw, 56px);
  letter-spacing: 0.18em;
  color: #f3e6cf;
  text-shadow: 0 2px 24px rgba(211, 84, 47, 0.35);
  height: min(52vh, 420px);
  margin-inline: auto;
}

.home-sub {
  margin: 28px 0 0;
  font-size: 13px;
  letter-spacing: 0.18em;
  color: rgba(243, 230, 207, 0.62);
}

.btn-primary {
  margin-top: 34px;
  min-width: 180px;
  padding: 14px 28px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--ember) 0%, var(--ember-deep) 100%);
  color: #fff7ef;
  letter-spacing: 0.28em;
  text-indent: 0.28em;
  box-shadow: 0 10px 30px rgba(211, 84, 47, 0.35);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.btn-primary:active {
  transform: scale(0.97);
}

.home-hint {
  margin: 18px 0 0;
  font-size: 11px;
  letter-spacing: 0.12em;
  color: rgba(243, 230, 207, 0.38);
}

/* —— 阅读 —— */
.reader {
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--text);
}

.topbar {
  flex: 0 0 auto;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) 10px 0;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  background: linear-gradient(to bottom, #efe4cf, var(--paper));
  border-bottom: 1px solid var(--line);
  z-index: 5;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  color: var(--ink-soft);
  font-size: 16px;
}

.icon-btn:active {
  background: rgba(28, 20, 16, 0.06);
}

.topbar-center {
  text-align: center;
  min-width: 0;
}

.topbar-title {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.08em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-progress {
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.08em;
}

.reader-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(214, 179, 106, 0.1), transparent 50%),
    var(--paper);
}

/* 外层保持横排坐标系，才能可靠响应左右滑动 */
.page-scroll {
  height: 100%;
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: auto;
  outline: none;
  overscroll-behavior-x: contain;
  touch-action: pan-x;
  scrollbar-width: thin;
  scrollbar-color: rgba(143, 42, 20, 0.35) transparent;
}

.page-scroll::-webkit-scrollbar {
  height: 3px;
}

.page-scroll::-webkit-scrollbar-thumb {
  background: rgba(143, 42, 20, 0.3);
  border-radius: 999px;
}

.vert-page {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  height: 100%;
  width: max-content;
  min-width: 100%;
  box-sizing: border-box;
  /* 右=起读侧，左=续读侧；左侧加宽避免裁切 */
  padding: 16px 26px 18px 56px;
  font-size: var(--fs);
  line-height: var(--lh);
  letter-spacing: 0.06em;
}

.vert-page.size-sm { --fs: 18px; --ruby: 9px; --lh: 1.95; }
.vert-page.size-md { --fs: 22px; --ruby: 10px; --lh: 2.05; }
.vert-page.size-lg { --fs: 26px; --ruby: 11px; --lh: 2.1; }
.vert-page.size-xl { --fs: 30px; --ruby: 12px; --lh: 2.15; }

body:not(.hide-pinyin) .vert-page {
  /* 注音打开时加宽行距，避免拼音与邻列重叠/被裁 */
  line-height: calc(var(--lh) + 0.35);
  padding-top: 22px;
  padding-bottom: 22px;
}

.para {
  display: block;
  margin-block-end: 1.25em; /* 竖排 block 轴：段落后侧（向左）间距 */
}

.para:last-child {
  margin-block-end: 0.35em;
}

.line {
  display: block;
  margin-block-end: 0.15em;
}

.heading-line {
  font-weight: 700;
  color: var(--ember-deep);
  letter-spacing: 0.16em;
}

.char {
  display: inline-block;
  position: relative;
  padding: 0.05em 0;
  border-radius: 3px;
  transition: background 0.15s ease;
}

.char.is-han {
  cursor: pointer;
}

.char.is-han:active,
.char.active {
  background: rgba(211, 84, 47, 0.14);
}

.char .glyph {
  font-family: "Noto Serif TC", "Songti TC", serif;
}

ruby {
  ruby-position: over;
  ruby-align: center;
}

rt {
  font-family: "Noto Serif TC", system-ui, sans-serif;
  font-size: var(--ruby);
  font-weight: 400;
  letter-spacing: 0;
  color: var(--ember-deep);
  opacity: 0.9;
  user-select: none;
  /* 防止注音被父级裁切时过于贴边 */
  padding: 0 1px;
}

body.hide-pinyin rt {
  display: none;
}

.punct {
  color: rgba(28, 20, 16, 0.55);
}

.scroll-hint {
  position: absolute;
  left: 12px;
  bottom: 14px;
  font-size: 11px;
  letter-spacing: 0.12em;
  color: rgba(28, 20, 16, 0.35);
  writing-mode: horizontal-tb;
  pointer-events: none;
  transition: opacity 0.4s ease;
}

.scroll-hint.hide {
  opacity: 0;
}

.toolbar {
  flex: 0 0 auto;
  height: calc(var(--toolbar-h) + var(--safe-bottom));
  padding: 6px 8px var(--safe-bottom);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  background: #efe4cf;
  border-top: 1px solid var(--line);
  z-index: 5;
}

.tool {
  min-height: 44px;
  border-radius: 12px;
  font-size: 13px;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
}

.tool:active {
  background: rgba(28, 20, 16, 0.07);
}

.tool.toggle[aria-pressed="true"] {
  background: rgba(211, 84, 47, 0.14);
  color: var(--ember-deep);
  font-weight: 600;
}

/* —— 抽屉 —— */
.sheet {
  position: absolute;
  inset: 0;
  z-index: 40;
}

.sheet[hidden] {
  display: none !important;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 8, 6, 0.45);
  animation: fade-in 0.2s ease;
}

.sheet-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: min(78dvh, 640px);
  background: #f3ead8;
  color: var(--text);
  border-radius: 22px 22px 0 0;
  padding: 10px 18px calc(18px + var(--safe-bottom));
  overflow: auto;
  animation: sheet-up 0.28s ease;
}

.sheet-panel-sm {
  max-height: none;
}

.sheet-handle {
  width: 42px;
  height: 4px;
  border-radius: 999px;
  background: rgba(28, 20, 16, 0.18);
  margin: 4px auto 14px;
}

.sheet-panel h2 {
  margin: 0 0 14px;
  font-size: 16px;
  letter-spacing: 0.2em;
  text-align: center;
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-list li {
  border-bottom: 1px solid var(--line);
}

.toc-list button {
  width: 100%;
  text-align: left;
  padding: 14px 4px;
  display: grid;
  grid-template-columns: 2.2em 1fr;
  gap: 8px;
  align-items: baseline;
}

.toc-list .idx {
  color: var(--ember-deep);
  font-size: 12px;
  letter-spacing: 0.06em;
}

.toc-list .name {
  font-size: 15px;
  letter-spacing: 0.06em;
}

.toc-list li.current button {
  color: var(--ember-deep);
  font-weight: 700;
}

.toc-list li.toc-sep {
  border-bottom: 0;
  padding: 18px 4px 8px;
  pointer-events: none;
}

.toc-list li.toc-sep span {
  display: block;
  font-size: 11px;
  letter-spacing: 0.28em;
  color: var(--ember-deep);
  opacity: 0.75;
}

.toc-list li.annotation .idx {
  letter-spacing: 0;
  font-size: 11px;
}

.size-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.size-btn {
  padding: 14px 0;
  border-radius: 12px;
  background: rgba(28, 20, 16, 0.05);
  letter-spacing: 0.12em;
}

.size-btn.active {
  background: rgba(211, 84, 47, 0.16);
  color: var(--ember-deep);
  font-weight: 700;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes sheet-up {
  from { transform: translateY(24%); opacity: 0.6; }
  to { transform: translateY(0); opacity: 1; }
}

/* —— 单字浮层 —— */
.char-pop {
  position: absolute;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(10, 8, 6, 0.4);
  writing-mode: horizontal-tb;
  animation: fade-in 0.15s ease;
}

.char-pop[hidden] {
  display: none !important;
}

.char-pop-card {
  width: min(78vw, 280px);
  background: #f7efe0;
  color: var(--text);
  border-radius: 20px;
  padding: 22px 20px 18px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 16px;
  align-items: center;
  border: 1px solid rgba(143, 42, 20, 0.18);
}

.char-pop-glyph {
  width: 84px;
  height: 84px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  font-size: 52px;
  background: linear-gradient(160deg, #2a1710, #140f0c);
  color: #f3e6cf;
  box-shadow: inset 0 0 0 1px rgba(214, 179, 106, 0.25);
}

.char-pop-meta {
  display: grid;
  gap: 8px;
  font-size: 15px;
}

.char-pop-meta > div {
  display: grid;
  grid-template-columns: 3em 1fr;
  gap: 8px;
  align-items: baseline;
}

.meta-label {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.08em;
}

#pop-pinyin {
  color: var(--ember-deep);
  font-weight: 700;
  letter-spacing: 0.04em;
}

/* 桌面宽屏稍微收窄阅读区 */
@media (min-width: 768px) {
  .reader {
    max-width: 480px;
    margin: 0 auto;
    border-left: 1px solid rgba(255, 255, 255, 0.04);
    border-right: 1px solid rgba(255, 255, 255, 0.04);
  }

  #app {
    background: #0a0806;
  }
}
