:root {
  --fs: 19px;
  --bar-h: 46px;
  --bg: #12100e;
  --bg2: #1a1714;
  --fg: #d9d2c6;
  --fg-dim: #8e877d;
  --line: #2a2622;
  --accent: #b08d57;
  --reader-w: 40em;
}

html[data-theme="light"] {
  --bg: #f7f3ea;
  --bg2: #fbf8f2;
  --fg: #2b2723;
  --fg-dim: #7c7267;
  --line: #e2dacd;
  --accent: #9a7434;
}

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

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, "PingFang SC", "Noto Serif CJK SC", "Source Han Serif SC", "Songti SC", serif;
}

#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: center;
  gap: 8px;
  padding-left: 10px; padding-right: 10px;
  background: var(--bg2);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}

#bookTitle {
  flex: 1;
  text-align: center;
  font-size: 15px;
  letter-spacing: .04em;
  color: var(--fg-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--fg-dim);
  font-size: 14px;
  font-family: inherit;
  padding: 8px 10px;
  border-radius: 8px;
  min-width: 48px;
}
.icon-btn:active { background: var(--line); }

#loadbar {
  position: fixed;
  top: calc(var(--bar-h) + env(safe-area-inset-top));
  left: 0; right: 0;
  height: 2px;
  z-index: 21;
  opacity: 0;
  transition: opacity .2s;
}
#loadbar.on { opacity: 1; }
#loadbar i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .25s ease-out;
}

#reader {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: calc(var(--bar-h) + env(safe-area-inset-top)) 0 calc(45vh + env(safe-area-inset-bottom));
  overscroll-behavior: contain;
  overflow-anchor: none;
}

#reader section {
  max-width: var(--reader-w);
  margin: 0 auto;
  padding: 0 22px;
}

#reader p {
  margin: 0 0 .85em;
  text-indent: 2em;
  line-height: 1.95;
  font-size: var(--fs);
  text-align: justify;
  word-break: break-word;
}

#reader h2 {
  margin: 2.4em 0 1.4em;
  font-size: calc(var(--fs) + 1px);
  font-weight: 600;
  letter-spacing: .03em;
  color: var(--fg);
  text-align: left;
  border-left: 3px solid var(--accent);
  padding-left: .6em;
  line-height: 1.5;
}
#reader section:first-child h2 { margin-top: .8em; }

#reader section > p:first-of-type { color: var(--fg-dim); }

#tocPanel {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform .26s cubic-bezier(.3,.8,.3,1);
  padding-top: env(safe-area-inset-top);
}
#tocPanel.open { transform: translateX(0); }

#tocHead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: var(--bar-h);
  flex: 0 0 auto;
  border-bottom: 1px solid var(--line);
  font-size: 15px;
  color: var(--fg-dim);
}
#tocClose { font-size: 22px; padding: 0 8px; }

#tocList {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 0 calc(30vh + env(safe-area-inset-bottom));
}

#tocList button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--fg);
  font-family: inherit;
  font-size: 15px;
  padding: 11px 20px;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
  content-visibility: auto;
  contain-intrinsic-size: 44px;
}
#tocList button.cur { color: var(--accent); font-weight: 600; }

#settingsPanel {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0,0,0,.45);
  display: none;
  align-items: flex-end;
}
#settingsPanel.open { display: flex; }
.sheet {
  width: 100%;
  background: var(--bg2);
  border-top: 1px solid var(--line);
  border-radius: 14px 14px 0 0;
  padding: 8px 22px calc(22px + env(safe-area-inset-bottom));
  font-size: 15px;
}
.sheet .row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
  color: var(--fg);
}
.sheet .row:last-child { border-bottom: 0; }
.ctrl { display: flex; align-items: center; gap: 14px; }
.ctrl button, .pill {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--fg);
  font-family: inherit;
  font-size: 15px;
  padding: 6px 14px;
  border-radius: 999px;
}
.ctrl b { font-weight: 500; min-width: 26px; text-align: center; color: var(--fg-dim); }
.muted { color: var(--fg-dim) !important; font-size: 13px !important; }

#loginPanel {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--bg);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
#loginPanel.open { display: flex; }
.card {
  width: 100%;
  max-width: 340px;
  text-align: center;
}
.card h1 { font-size: 22px; font-weight: 600; letter-spacing: .06em; margin: 0 0 8px; }
.card p { margin: 0 0 22px; font-size: 13px; }
.card input {
  width: 100%;
  padding: 13px 14px;
  font-size: 16px;
  font-family: inherit;
  color: var(--fg);
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}
.card input:focus { border-color: var(--accent); }
.card button {
  width: 100%;
  margin-top: 14px;
  padding: 13px;
  font-size: 16px;
  font-family: inherit;
  color: #12100e;
  background: var(--accent);
  border: 0;
  border-radius: 10px;
  font-weight: 600;
}
.card button:disabled { opacity: .5; }
.err { margin-top: 12px; font-size: 13px; color: #c0654f; min-height: 18px; }

@media (min-width: 720px) {
  #reader p { text-align: left; }
}
