:root {
  --zoom: 1;
  --bg: #f3f0e9;
  --surface: #fffdf8;
  --surface-2: #f7f3eb;
  --text: #2f2a24;
  --muted: #877e72;
  --line: rgba(68, 55, 42, 0.12);
  --accent: #8b5e3c;
  --accent-soft: rgba(139, 94, 60, 0.12);
  --shadow: 0 12px 34px rgba(54, 42, 30, 0.12);
  --page-filter: none;
  color-scheme: light;
}

:root[data-theme="sepia"] {
  --bg: #ece0c8;
  --surface: #f8edd6;
  --surface-2: #f1e3c8;
  --text: #4b3a27;
  --muted: #8b7658;
  --line: rgba(90, 65, 34, 0.16);
  --accent: #9a6533;
  --accent-soft: rgba(154, 101, 51, 0.14);
  --shadow: 0 12px 34px rgba(84, 60, 28, 0.14);
  color-scheme: light;
}

:root[data-theme="night"] {
  --bg: #111310;
  --surface: #1c1f1a;
  --surface-2: #242820;
  --text: #e9e5db;
  --muted: #9b9b90;
  --line: rgba(255, 255, 255, 0.1);
  --accent: #d5a56f;
  --accent-soft: rgba(213, 165, 111, 0.14);
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.34);
  --page-filter: brightness(0.78) contrast(0.96) sepia(0.1);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  transition: background-color 0.25s ease, color 0.25s ease;
}

button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app { min-height: 100vh; }

.topbar {
  position: fixed;
  z-index: 40;
  inset: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
  transition: transform 0.25s ease;
}
.topbar.is-hidden { transform: translateY(-110%); }
.topbar-title { flex: 1; min-width: 0; text-align: center; }
.topbar-title strong { display: block; font-size: 14px; letter-spacing: 0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title span { display: block; margin-top: 2px; font-size: 11px; color: var(--muted); }
.icon-button, .nav-button, .close-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
.icon-button svg, .nav-button svg, .search-wrap svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.progress-track { position: fixed; z-index: 41; top: 0; left: 0; right: 0; height: 2px; background: transparent; }
.progress-bar { width: 0; height: 100%; background: var(--accent); transition: width 0.12s linear; }

.reader {
  width: min(100%, 720px);
  margin: 0 auto;
  padding: calc(82px + env(safe-area-inset-top)) 14px calc(104px + env(safe-area-inset-bottom));
}

.loading-state { display: grid; place-items: center; min-height: 52vh; color: var(--muted); font-size: 14px; }
.loading-spinner { width: 26px; height: 26px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.cover-card {
  position: relative;
  margin: 8px 0 34px;
  padding: 28px 24px 24px;
  border-radius: 24px;
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.cover-card::after {
  content: "";
  position: absolute;
  width: 210px;
  height: 210px;
  right: -90px;
  top: -80px;
  border-radius: 50%;
  background: var(--accent-soft);
}
.cover-eyebrow { position: relative; z-index: 1; margin: 0 0 10px; font-size: 12px; letter-spacing: 0.16em; color: var(--accent); }
.cover-card h1 { position: relative; z-index: 1; margin: 0; font-size: clamp(28px, 8vw, 42px); line-height: 1.18; letter-spacing: 0.03em; }
.cover-card p { position: relative; z-index: 1; margin: 14px 0 0; color: var(--muted); font-size: 14px; line-height: 1.7; }
.cover-actions { position: relative; z-index: 1; display: flex; gap: 10px; margin-top: 22px; flex-wrap: wrap; }
.primary-button, .secondary-button, .text-button {
  border: 0;
  border-radius: 12px;
  padding: 11px 18px;
  font-size: 14px;
  cursor: pointer;
}
.primary-button { color: #fff; background: var(--accent); }
.secondary-button { color: var(--text); background: var(--accent-soft); }
.text-button { padding: 8px 0; color: var(--accent); background: transparent; text-decoration: underline; text-underline-offset: 3px; }

.chapter { margin: 0 0 34px; }
.chapter-heading {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin: 30px 0 16px;
  padding: 18px;
  border-radius: 18px;
  background: var(--surface);
  border: 1px solid var(--line);
}
.chapter-index {
  flex: 0 0 auto;
  min-width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 15px;
}
.chapter-heading h2 { margin: 0; font-size: 19px; line-height: 1.4; }
.chapter-heading p { margin: 5px 0 0; font-size: 12px; color: var(--muted); }

.chapter-marker {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 22px 2px 12px;
  padding: 7px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
}
.chapter-marker::before { content: ""; width: 5px; height: 5px; border-radius: 50%; background: currentColor; }

.page { margin: 0 0 18px; scroll-margin-top: calc(76px + env(safe-area-inset-top)); }
.page-frame {
  border-radius: 14px;
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--surface);
  box-shadow: var(--shadow);
  -webkit-overflow-scrolling: touch;
}
.page-frame img {
  display: block;
  width: calc(100% * var(--zoom));
  max-width: none;
  height: auto;
  opacity: 0;
  filter: var(--page-filter);
  transition: opacity 0.25s ease, filter 0.25s ease;
}
.page-frame img.is-loaded { opacity: 1; }
.page figcaption {
  display: flex;
  justify-content: space-between;
  padding: 8px 4px 0;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.03em;
}

.bottombar {
  position: fixed;
  z-index: 35;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
.nav-button { width: 36px; height: 36px; border-radius: 50%; border-color: transparent; background: transparent; }
.page-pill {
  min-width: 108px;
  padding: 7px 12px;
  border: 0;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  cursor: pointer;
  line-height: 1.25;
}
.page-pill span { display: block; font-size: 13px; font-weight: 600; }
.page-pill small { display: block; font-size: 10px; color: var(--muted); }

.sheet-backdrop { position: fixed; z-index: 50; inset: 0; background: rgba(20, 16, 12, 0.42); backdrop-filter: blur(2px); }
.sheet {
  position: fixed;
  z-index: 51;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  max-height: min(82vh, 720px);
  padding: 8px 18px calc(18px + env(safe-area-inset-bottom));
  border-radius: 24px 24px 0 0;
  background: var(--surface);
  box-shadow: 0 -18px 48px rgba(0, 0, 0, 0.18);
  animation: sheet-in 0.24s ease-out;
}
.sheet--compact { max-height: min(58vh, 460px); }
@keyframes sheet-in { from { transform: translateY(30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.sheet-handle { width: 38px; height: 4px; margin: 0 auto 12px; border-radius: 999px; background: var(--line); }
.sheet-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 14px; }
.sheet-header h2 { margin: 0; font-size: 20px; }
.sheet-header p { margin: 4px 0 0; font-size: 12px; color: var(--muted); }
.close-button { border: 0; background: var(--accent-soft); color: var(--text); font-size: 24px; line-height: 1; }
.sheet-body { overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

.search-wrap { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; padding: 10px 12px; border-radius: 12px; background: var(--surface-2); color: var(--muted); }
.search-wrap svg { width: 18px; height: 18px; flex: 0 0 auto; }
.search-wrap input { width: 100%; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 14px; }

.toc-group { margin-bottom: 14px; }
.toc-group-title { display: flex; justify-content: space-between; gap: 12px; width: 100%; padding: 12px 4px 8px; border: 0; background: transparent; color: var(--text); text-align: left; cursor: pointer; }
.toc-group-title strong { font-size: 15px; }
.toc-group-title span { flex: 0 0 auto; color: var(--muted); font-size: 12px; }
.toc-item { display: flex; justify-content: space-between; gap: 12px; width: 100%; padding: 10px 12px 10px 18px; border: 0; border-radius: 10px; background: transparent; color: var(--muted); text-align: left; font-size: 13px; cursor: pointer; }
.toc-item:hover, .toc-item.is-active { background: var(--accent-soft); color: var(--accent); }
.toc-item span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toc-item span:last-child { flex: 0 0 auto; font-variant-numeric: tabular-nums; }
.toc-empty { padding: 28px 0; text-align: center; color: var(--muted); font-size: 14px; }

.settings-body { padding-bottom: 8px; }
.setting-group { margin-bottom: 22px; }
.setting-group h3 { margin: 0 0 10px; font-size: 14px; }
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; padding: 4px; border-radius: 12px; background: var(--surface-2); }
.segmented button { padding: 10px 6px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-size: 13px; cursor: pointer; }
.segmented button.is-active { background: var(--surface); color: var(--accent); font-weight: 600; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06); }
.hint, .about-group p { margin: 10px 0 0; color: var(--muted); font-size: 12px; line-height: 1.7; }

.jump-body { padding: 4px 0 8px; }
.jump-input-row { display: flex; align-items: center; gap: 10px; }
.jump-input-row input[type="number"] { width: 96px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); color: var(--text); font-size: 16px; }
.jump-input-row span { color: var(--muted); }
.jump-input-row .primary-button { margin-left: auto; }
.jump-body input[type="range"] { width: 100%; margin-top: 24px; accent-color: var(--accent); }

.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: calc(78px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  max-width: min(86vw, 360px);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(35, 30, 25, 0.9);
  color: #fff;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 720px) {
  .reader { padding-left: 24px; padding-right: 24px; }
  .sheet { left: 50%; right: auto; width: min(100%, 560px); transform: translateX(-50%); border-radius: 24px 24px 0 0; }
  @keyframes sheet-in { from { transform: translate(-50%, 30px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }
}
