:root {
  color-scheme: light;
  --ui-font: "PingFang SC", "Noto Sans SC", "Helvetica Neue", sans-serif;
  --text-font: "Songti SC", "Noto Serif SC", "Source Han Serif SC", serif;
  --surface: rgba(255, 255, 255, 0.78);
  --paper: #fbfaf7;
  --paper-strong: #ffffff;
  --ink: #181818;
  --muted: #707070;
  --line: rgba(24, 24, 24, 0.08);
  --accent: #b25b33;
  --shadow: 0 12px 40px rgba(24, 24, 24, 0.06);
  --content-width: 46rem;
  --font-size: 1.1875rem;
  --line-height: 2;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

body[data-theme="mist"] {
  --surface: rgba(248, 242, 232, 0.84);
  --paper: #f3ece2;
  --paper-strong: #fbf6ef;
  --ink: #211c18;
  --muted: #756b63;
  --line: rgba(33, 28, 24, 0.09);
  --accent: #9d5b36;
}

body[data-theme="night"] {
  color-scheme: dark;
  --surface: rgba(24, 24, 24, 0.82);
  --paper: #121212;
  --paper-strong: #1d1d1d;
  --ink: #e0e0e0;
  --muted: #a8a39d;
  --line: rgba(224, 224, 224, 0.12);
  --accent: #d3a07b;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.24);
}

body[data-width="narrow"] {
  --content-width: 40rem;
}

body[data-width="wide"] {
  --content-width: 52rem;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--ui-font);
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(178, 91, 51, 0.08), transparent 28%),
    radial-gradient(circle at top right, rgba(178, 91, 51, 0.04), transparent 24%),
    var(--paper);
}

button {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
}

.progress-bar {
  position: fixed;
  inset: 0 0 auto;
  height: 2px;
  z-index: 40;
  background: transparent;
}

.progress-bar span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 120ms ease-out;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 12px 24px rgba(17, 17, 17, 0.03);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.topbar-title {
  min-width: 0;
  display: grid;
  gap: 0.18rem;
  text-align: center;
}

.topbar-title strong,
.topbar-title span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-title strong {
  font-size: 0.94rem;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.topbar-title span {
  font-size: 0.78rem;
  color: var(--muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.ghost-button {
  min-width: 3rem;
  border: 1px solid var(--line);
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 0.55rem 0.85rem;
}

.ghost-link-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 3rem;
  border: 1px solid var(--line);
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 0.55rem 0.85rem;
  text-decoration: none;
  white-space: nowrap;
}

.author-x-short {
  display: none;
}

.ghost-button:focus-visible,
.ghost-link-button:focus-visible,
.ghost-icon-button:focus-visible,
.segmented-control button:focus-visible,
.chapter-link:focus-visible,
.bottom-nav button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.page {
  padding: 1.2rem 1rem calc(7rem + var(--safe-bottom));
}

.settings-panel {
  position: fixed;
  top: calc(4.85rem + env(safe-area-inset-top, 0px));
  right: 1rem;
  z-index: 34;
  display: grid;
  gap: 0.8rem;
  width: min(22rem, calc(100vw - 2rem));
  max-height: min(calc(100vh - 7rem), 30rem);
  overflow: auto;
  padding: 0.9rem;
  border-radius: 1.25rem;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper-strong) 94%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}

.settings-panel[hidden] {
  display: none;
}

.settings-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.settings-panel-title {
  font-size: 0.82rem;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.ghost-icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: inherit;
  line-height: 1;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.settings-row span {
  font-size: 0.88rem;
  color: var(--muted);
  flex: 0 0 2.7rem;
}

.segmented-control {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  justify-content: flex-end;
  flex: 1 1 auto;
}

.segmented-control button {
  border: 1px solid var(--line);
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 0.48rem 0.8rem;
}

.segmented-control button.is-active {
  background: var(--ink);
  color: var(--paper-strong);
  border-color: transparent;
}

.reader {
  width: min(100%, var(--content-width));
  margin: 0 auto;
  transition: opacity 0.15s ease-in-out;
  will-change: opacity;
}

.reader.is-transitioning {
  opacity: 0;
}

.intro,
.chapter-shell {
  display: grid;
  gap: 1.4rem;
}

.intro-hero,
.chapter-hero {
  padding: clamp(1.4rem, 3vw, 2.1rem);
  border: 1px solid var(--line);
  border-radius: 1.6rem;
  background: color-mix(in srgb, var(--paper-strong) 92%, transparent);
  box-shadow: var(--shadow);
}

.intro-hero h1,
.chapter-hero h1 {
  margin: 0 0 0.6rem;
  font-size: clamp(1.9rem, 4vw, 3rem);
  line-height: 1.12;
  letter-spacing: -0.02em;
}

.intro-hero p,
.chapter-meta,
.intro-meta {
  margin: 0;
  color: var(--muted);
}

.intro-points,
.intro-actions {
  display: grid;
  gap: 0.8rem;
}

.point {
  padding: 1rem 1.05rem;
  border-radius: 1.2rem;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper-strong) 88%, transparent);
}

.point strong {
  display: block;
  margin-bottom: 0.3rem;
  font-size: 0.96rem;
}

.point p {
  margin: 0;
  color: var(--muted);
  line-height: 1.75;
}

.primary-button {
  width: fit-content;
  border: none;
  border-radius: 999px;
  padding: 0.78rem 1.15rem;
  background: var(--ink);
  color: var(--paper-strong);
}

.chapter-hero .eyebrow {
  display: inline-block;
  margin-bottom: 0.5rem;
  font-size: 0.82rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

.chapter-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  font-size: 0.92rem;
}

.chapter-body {
  padding: clamp(1.45rem, 3vw, 2.2rem);
  border-radius: 1.5rem;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper-strong) 90%, transparent);
  box-shadow: var(--shadow);
}

.chapter-body h2 {
  margin: 2rem 0 0.8rem;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.03em;
}

.chapter-body p {
  margin: 0 0 1.6em;
  font-family: var(--text-font);
  font-size: var(--font-size);
  line-height: var(--line-height);
  text-wrap: pretty;
  text-align: justify;
  text-justify: inter-ideograph;
  color: var(--ink);
}

.chapter-body p:last-child {
  margin-bottom: 0;
}

.chapter-body h2 + p {
  margin-top: 0;
}

.chapter-body p + h2 {
  margin-top: 2.25rem;
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: min(23rem, 88vw);
  transform: translateX(-102%);
  transition: transform 180ms ease;
  z-index: 32;
}

.sidebar.is-open {
  transform: translateX(0);
}

.sidebar-inner {
  height: 100%;
  overflow-y: auto;
  padding: 1.25rem;
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper-strong) 94%, transparent);
  backdrop-filter: blur(18px);
}

.sidebar-inner h1 {
  margin: 0.55rem 0 0.3rem;
  font-size: 1.35rem;
}

.sidebar-inner p {
  margin: 0 0 1rem;
  color: var(--muted);
  line-height: 1.7;
}

.book-chip {
  display: inline-flex;
  align-items: center;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent);
  font-size: 0.8rem;
}

.sidebar ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 0.35rem;
}

.chapter-link {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  border-radius: 1rem;
  padding: 0.8rem 0.9rem;
}

.chapter-link span {
  display: block;
}

.chapter-link .label {
  font-size: 0.92rem;
  line-height: 1.55;
}

.chapter-link .meta {
  margin-top: 0.2rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.chapter-link.is-active {
  border-color: var(--line);
  background: color-mix(in srgb, var(--paper) 88%, transparent);
}

.intro-link {
  margin: 0 0 0.65rem;
}

.bottom-nav {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + var(--safe-bottom));
  transform: translate(-50%, 0);
  z-index: 28;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.9rem;
  width: min(calc(100% - 1.5rem), 34rem);
  padding: 0.7rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.bottom-nav button {
  border: none;
  background: transparent;
  color: inherit;
  padding: 0.4rem 0.45rem;
}

.bottom-nav button:disabled {
  opacity: 0.36;
}

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

.bottom-meta strong,
.bottom-meta span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bottom-meta strong {
  font-size: 0.9rem;
}

.bottom-meta span {
  font-size: 0.75rem;
  color: var(--muted);
}

.scrim {
  position: fixed;
  inset: 0;
  border: none;
  background: rgba(0, 0, 0, 0.18);
  z-index: 31;
}

.only-mobile {
  display: inline-flex;
}

body[data-chrome="hidden"] .topbar {
  transform: translateY(calc(-100% - 0.75rem));
}

body[data-chrome="hidden"] .bottom-nav {
  transform: translate(-50%, calc(180% + var(--safe-bottom)));
}

@media (min-width: 1024px) {
  .only-mobile {
    display: none;
  }

  .sidebar {
    transform: translateX(0);
    width: 21rem;
  }

  .page {
    padding-left: calc(21rem + 2rem);
    padding-right: 2rem;
  }

  .topbar {
    padding-left: calc(21rem + 2rem);
    padding-right: 2rem;
  }

  .settings-panel {
    right: 2rem;
    width: min(22rem, calc(100vw - 25rem));
  }
}

@media (max-width: 640px) {
  :root {
    --font-size: 1.08rem;
    --line-height: 1.95;
  }

  .topbar {
    padding: 0.9rem 0.85rem;
  }

  .topbar-actions {
    gap: 0.4rem;
  }

  .ghost-link-button {
    padding: 0.55rem 0.7rem;
  }

  .author-x-long {
    display: none;
  }

  .author-x-short {
    display: inline;
  }

  .page {
    padding: 1rem 0.8rem calc(7rem + var(--safe-bottom));
  }

  .chapter-body,
  .intro-hero,
  .chapter-hero {
    border-radius: 1.25rem;
    padding: 1.15rem;
  }

  .settings-panel {
    top: calc(4.35rem + env(safe-area-inset-top, 0px));
    right: 0.8rem;
    width: calc(100vw - 1.6rem);
    max-height: min(calc(100vh - 6.5rem), 28rem);
    padding: 0.85rem;
  }

  .settings-row {
    align-items: flex-start;
    flex-direction: column;
  }

  .settings-row span {
    flex-basis: auto;
  }

  .segmented-control {
    justify-content: flex-start;
  }

  .bottom-nav {
    width: calc(100% - 1rem);
    gap: 0.55rem;
  }
}
