:root {
  --bg: #f7f3ea;
  --paper: #fffdf8;
  --ink: #191713;
  --muted: #746d61;
  --line: rgba(40, 34, 25, 0.16);
  --panel: rgba(255, 253, 248, 0.94);
  --accent: #8b5e2f;
  --font-size: 20px;
  --font-weight: 400;
  --line-height: 1.75;
  --letter-spacing: 0.02em;
  --measure: 44em;
  --reader-font: "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", serif;
}

[data-theme="dark"] {
  --bg: #0f1115;
  --paper: #151922;
  --ink: #e7e2d8;
  --muted: #aaa39a;
  --line: rgba(231, 226, 216, 0.16);
  --panel: rgba(21, 25, 34, 0.94);
  --accent: #c89b5c;
}

[data-theme="sepia"] {
  --bg: #efe4cf;
  --paper: #fbf3e3;
  --ink: #2b2118;
  --muted: #7a6857;
  --line: rgba(65, 47, 30, 0.16);
  --panel: rgba(251, 243, 227, 0.94);
  --accent: #936633;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100%;
  color: var(--ink);
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  overflow-x: hidden;
}

button,
select,
input {
  font: inherit;
}

button {
  color: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  min-height: 100vh;
}

.app-shell.toc-collapsed {
  grid-template-columns: 0 minmax(0, 1fr);
}

.app-shell.toc-collapsed .library-panel {
  width: 0;
  padding-left: 0;
  padding-right: 0;
  overflow: hidden;
  border-right: 0;
}

.library-panel {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  padding: 22px 18px;
  color: var(--ink);
  background: var(--panel);
  border-right: 1px solid var(--line);
  backdrop-filter: blur(16px);
  z-index: 20;
}

.library-head {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 14px;
  align-items: center;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.library-head img {
  width: 72px;
  aspect-ratio: 0.625;
  object-fit: cover;
  border-radius: 4px;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.24);
}

.library-head h1 {
  margin: 0 0 8px;
  font-size: 18px;
  line-height: 1.25;
}

.library-head p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.toc {
  padding: 14px 0 40px;
}

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

.toc summary {
  cursor: pointer;
  list-style: none;
  padding: 13px 2px;
  font-weight: 700;
  line-height: 1.35;
}

.toc summary::-webkit-details-marker {
  display: none;
}

.toc-list {
  display: grid;
  gap: 2px;
  padding: 0 0 12px 8px;
}

.toc a {
  display: block;
  padding: 7px 8px;
  color: var(--muted);
  text-decoration: none;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1.35;
}

.toc a.active,
.toc a:hover {
  color: var(--ink);
  background: rgba(139, 94, 47, 0.12);
}

.reader-main {
  position: relative;
  min-height: 100vh;
}

.reader-toolbar {
  position: sticky;
  top: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 10px max(18px, env(safe-area-inset-right)) 10px max(18px, env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(18px);
  z-index: 15;
  transition: transform 180ms ease, opacity 180ms ease;
}

.chrome-hidden .reader-toolbar,
.chrome-hidden .settings-panel {
  transform: translateY(-110%);
  opacity: 0;
  pointer-events: none;
}

.toolbar-title {
  overflow: hidden;
  color: var(--muted);
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 14px;
}

.toolbar-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.icon-btn,
.text-btn {
  min-height: 34px;
  border: 1px solid var(--line);
  background: transparent;
  border-radius: 7px;
  cursor: pointer;
}

.icon-btn {
  min-width: 38px;
  padding: 0 10px;
}

.text-btn {
  padding: 0 12px;
}

.settings-panel {
  position: sticky;
  top: 55px;
  z-index: 14;
  display: grid;
  grid-template-columns: repeat(6, minmax(120px, 1fr));
  gap: 10px;
  padding: 12px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(18px);
}

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

.settings-panel label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

.settings-panel select,
.settings-panel input {
  width: 100%;
}

.reader {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 56px 26px 120px;
  font-family: var(--reader-font);
  font-size: var(--font-size);
  font-weight: var(--font-weight);
  line-height: var(--line-height);
  letter-spacing: var(--letter-spacing);
}

.book-front {
  min-height: calc(100vh - 150px);
  display: grid;
  align-content: center;
  gap: 24px;
  text-align: center;
}

.book-front img {
  width: min(220px, 44vw);
  margin: 0 auto;
  border-radius: 4px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

.book-front h2 {
  margin: 0;
  font-size: 1.9em;
  letter-spacing: 0;
}

.book-front p {
  margin: 0;
  color: var(--muted);
  text-indent: 0;
}

.volume-title {
  margin: 0 0 20px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
  font-size: 1.45em;
  text-align: center;
}

.story-title {
  margin: 0 0 18px;
  font-size: 1.34em;
  line-height: 1.35;
  letter-spacing: 0;
}

.brief {
  margin: 20px 0 26px;
  padding: 16px 18px;
  background: rgba(139, 94, 47, 0.10);
  border: 1px solid var(--line);
  border-left: 5px solid var(--accent);
  border-radius: 8px;
}

.brief-title,
.afterthought-title {
  margin: 0 0 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  font-size: 0.82em;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.brief ul {
  margin: 0;
  padding-left: 1.2em;
}

.brief li {
  margin: 0.35em 0;
}

.reader p {
  margin: 0.86em 0;
  text-indent: 2em;
}

.afterthought {
  margin-top: 30px;
  padding: 16px 18px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  color: var(--muted);
}

.afterthought p {
  margin: 0;
  text-indent: 0;
}

.story-nav {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  margin-top: 36px;
  padding-top: 24px;
  border-top: 1px solid var(--line);
}

.story-nav button {
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
}

.progress-wrap {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: transparent;
  z-index: 30;
}

.progress-bar {
  width: 0;
  height: 100%;
  background: var(--accent);
}

.drawer-scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.32);
  z-index: 18;
}

:fullscreen .library-panel,
:fullscreen .reader-toolbar,
:fullscreen .settings-panel {
  display: none;
}

:fullscreen .app-shell {
  display: block;
}

:fullscreen .reader {
  padding-top: 42px;
}

@media (max-width: 920px) {
  html,
  body {
    min-height: 100dvh;
  }

  .app-shell {
    display: block;
    min-height: 100dvh;
  }

  .app-shell.toc-collapsed .library-panel {
    width: min(86vw, 360px);
    padding: 22px 18px;
    border-right: 1px solid var(--line);
  }

  .library-panel {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(86vw, 360px);
    height: 100dvh;
    transform: translateX(-105%);
    transition: transform 180ms ease;
  }

  .library-panel.open {
    transform: translateX(0);
  }

  .settings-panel {
    position: fixed;
    left: 10px;
    right: 10px;
    top: 58px;
    grid-template-columns: 1fr 1fr;
    border: 1px solid var(--line);
    border-radius: 10px;
  }

  .toolbar-actions .text-btn {
    display: none;
  }

  .reader {
    padding: 44px 18px 96px;
  }
}

@media (max-width: 560px) {
  .reader-toolbar {
    grid-template-columns: auto 1fr auto;
  }

  .toolbar-title {
    font-size: 12px;
  }

  .reader {
    font-size: calc(var(--font-size) - 1px);
    padding-left: 16px;
    padding-right: 16px;
  }

  .settings-panel {
    grid-template-columns: 1fr;
  }
}
