:root {
  --paper: #f5f0e8;
  --paper-raised: #fbf8f2;
  --ink: #292722;
  --muted: #766e63;
  --line: #ddd5c9;
  --accent: #86674c;
  --accent-soft: #ece2d3;
  --danger: #9b5750;
  --shadow: rgba(38, 31, 23, 0.12);
  --reader-font-size: 18px;
  --reader-line-height: 1.8;
  --top-safe: env(safe-area-inset-top, 0px);
  --bottom-safe: env(safe-area-inset-bottom, 0px);
}

html[data-theme="dark"] {
  --paper: #151718;
  --paper-raised: #1c1f20;
  --ink: #e7e2d9;
  --muted: #a9a195;
  --line: #343738;
  --accent: #d1a87a;
  --accent-soft: #292522;
  --danger: #d07c72;
  --shadow: rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

html {
  background: var(--paper);
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font-family: "Noto Serif SC", "Source Han Serif SC", "Songti SC", STSong, serif;
  transition: background 180ms ease, color 180ms ease;
}

button,
input,
textarea {
  font: inherit;
}

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

.lock-screen {
  min-height: 100svh;
  padding: 28px 22px;
  display: grid;
  place-items: center;
  background: var(--paper);
}

.lock-card {
  width: min(100%, 340px);
  padding: 35px 26px 30px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--paper-raised);
  box-shadow: 0 12px 36px var(--shadow);
}

.lock-title {
  margin-bottom: 9px;
  text-align: center;
  font-size: 27px;
  font-weight: 650;
  letter-spacing: 0.14em;
}

.lock-description {
  margin: 0 0 34px;
  text-align: center;
  color: var(--muted);
  font: 14px system-ui, sans-serif;
}

.lock-label {
  display: block;
  margin-bottom: 9px;
  color: var(--muted);
  font: 13px system-ui, sans-serif;
}

.lock-input {
  display: block;
  width: 100%;
  height: 48px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 11px;
  outline: none;
  background: var(--paper);
  color: var(--ink);
  font: 18px system-ui, sans-serif;
}

.lock-input:focus {
  border-color: var(--accent);
}

.lock-error {
  margin: 10px 0 0;
  color: var(--danger);
  font: 13px system-ui, sans-serif;
}

.lock-submit {
  width: 100%;
  height: 48px;
  margin-top: 20px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--paper-raised);
  font: 15px system-ui, sans-serif;
}

.topbar {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  right: 0;
  height: calc(54px + var(--top-safe));
  padding: var(--top-safe) 8px 0;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--paper);
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}

.icon-button {
  flex: none;
  height: 44px;
  width: 42px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  font-family: system-ui, sans-serif;
  font-size: 22px;
  color: var(--muted);
}

.icon-button:active,
.tool:active,
.drawer-action:active {
  background: var(--accent-soft);
}

.book-status {
  min-width: 0;
  flex: 1;
  padding: 0 4px;
}

.book-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
  font-weight: 600;
}

.reading-progress {
  margin-top: 2px;
  font-family: system-ui, sans-serif;
  color: var(--muted);
  font-size: 12px;
}

.progress-track {
  position: fixed;
  z-index: 21;
  left: 0;
  right: 0;
  top: calc(53px + var(--top-safe));
  height: 2px;
  background: transparent;
}

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

.content {
  min-height: 100vh;
}

.manuscript {
  max-width: 42rem;
  margin: 0 auto;
  padding: calc(78px + var(--top-safe)) clamp(19px, 6vw, 34px) calc(98px + var(--bottom-safe));
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  letter-spacing: 0.02em;
  word-break: break-word;
}

.manuscript h1 {
  margin: 1.1em 0 2.8em;
  font-size: 1.52em;
  font-weight: 650;
  text-align: center;
  letter-spacing: 0.14em;
}

.manuscript h2 {
  scroll-margin-top: calc(75px + var(--top-safe));
  margin: 3.5em 0 2.2em;
  font-size: 1.2em;
  font-weight: 650;
  text-align: center;
  letter-spacing: 0.06em;
}

.manuscript h1 + h2 {
  margin-top: 0;
}

.manuscript p {
  margin: 0 0 1.15em;
  text-align: justify;
  text-indent: 2em;
}

.manuscript h2 + p,
.manuscript hr + p,
.manuscript p.dialogue,
.manuscript p.document {
  text-indent: 0;
}

.manuscript ul {
  margin: 0.3em 0 1.4em;
  padding-left: 1.5em;
  font-family: "Noto Sans SC", "Source Han Sans SC", system-ui, sans-serif;
  color: var(--ink);
  color: color-mix(in srgb, var(--ink) 90%, var(--muted));
  font-size: 0.92em;
}

.manuscript li {
  margin: 0.35em 0;
}

.manuscript hr {
  width: 34%;
  margin: 3em auto;
  border: 0;
  border-top: 1px solid var(--line);
}

.manuscript code {
  padding: 0.12em 0.35em;
  border-radius: 5px;
  font-family: ui-monospace, monospace;
  font-size: 0.88em;
  background: var(--accent-soft);
}

.manuscript .flash {
  border-radius: 5px;
  background: var(--accent-soft);
  transition: background 900ms ease;
}

.editor-view {
  position: fixed;
  inset: calc(56px + var(--top-safe)) 0 calc(62px + var(--bottom-safe));
  z-index: 5;
  display: flex;
  flex-direction: column;
  background: var(--paper);
}

.editor-note {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  min-height: 34px;
  padding: 8px 14px;
  font-family: system-ui, sans-serif;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
}

#editor {
  flex: 1;
  width: min(100%, 48rem);
  margin: 0 auto;
  padding: 18px clamp(16px, 5vw, 28px) 32px;
  resize: none;
  border: 0;
  outline: 0;
  background: var(--paper);
  color: var(--ink);
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  font-family: "Noto Serif SC", "Source Han Serif SC", "Songti SC", STSong, serif;
}

.bottom-toolbar {
  position: fixed;
  z-index: 20;
  left: 0;
  bottom: 0;
  right: 0;
  min-height: calc(58px + var(--bottom-safe));
  padding: 5px 5px var(--bottom-safe);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 3px;
  background: var(--paper-raised);
  background: color-mix(in srgb, var(--paper-raised) 95%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}

.tool {
  min-height: 48px;
  border-radius: 12px;
  font-family: system-ui, sans-serif;
  font-size: 13px;
  color: var(--muted);
}

.tool.active {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 600;
}

.tool.danger {
  color: var(--danger);
}

.backdrop {
  position: fixed;
  z-index: 29;
  inset: 0;
  background: rgba(0, 0, 0, 0.32);
}

.drawer {
  position: fixed;
  z-index: 30;
  inset: 0 auto 0 0;
  display: flex;
  flex-direction: column;
  width: min(88vw, 340px);
  padding-top: var(--top-safe);
  background: var(--paper-raised);
  box-shadow: 10px 0 30px var(--shadow);
  transform: translateX(-104%);
  transition: transform 180ms ease;
}

.drawer.open {
  transform: translateX(0);
}

.drawer-head {
  height: 65px;
  padding: 11px 10px 8px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}

.drawer-head h2 {
  margin: 0;
  font-size: 20px;
}

.drawer-head p {
  margin: 3px 0 0;
  max-width: 230px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font: 12px system-ui, sans-serif;
}

.toc {
  flex: 1;
  overflow: auto;
  padding: 10px;
}

.toc button {
  width: 100%;
  padding: 11px 12px;
  border-radius: 10px;
  color: var(--ink);
  text-align: left;
  font-size: 15px;
}

.toc button.current {
  background: var(--accent-soft);
  color: var(--accent);
}

.drawer-section {
  flex: none;
  padding: 12px 15px;
  border-top: 1px solid var(--line);
}

.drawer-section h3 {
  margin: 0 0 10px;
  font: 600 13px system-ui, sans-serif;
  color: var(--muted);
}

.drawer-action {
  width: 100%;
  margin-bottom: 8px;
  padding: 10px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--paper-raised);
  font: 14px system-ui, sans-serif;
}

.drawer-action.secondary {
  color: var(--accent);
  background: var(--accent-soft);
}

.drawer-action.lock-action {
  margin-top: 5px;
  color: var(--danger);
}

.help-text {
  margin: 3px 0 0;
  color: var(--muted);
  font: 12px/1.45 system-ui, sans-serif;
}

.setting-row {
  display: grid;
  grid-template-columns: 1fr 36px 52px 36px;
  gap: 6px;
  align-items: center;
  margin: 5px 0;
  font: 14px system-ui, sans-serif;
}

.setting-row button {
  height: 34px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent);
}

.setting-row output {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.search-panel {
  position: fixed;
  z-index: 25;
  left: 0;
  right: 0;
  top: calc(54px + var(--top-safe));
  max-height: min(65vh, 520px);
  padding: 10px;
  background: var(--paper-raised);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 8px 24px var(--shadow);
}

.search-form {
  display: flex;
  gap: 7px;
}

.search-form input {
  min-width: 0;
  flex: 1;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
  color: var(--ink);
  background: var(--paper);
  font: 15px system-ui, sans-serif;
}

.search-form button {
  padding: 0 12px;
  border-radius: 10px;
  color: var(--accent);
  background: var(--accent-soft);
  font: 14px system-ui, sans-serif;
}

.search-summary {
  min-height: 25px;
  padding: 10px 4px 4px;
  color: var(--muted);
  font: 12px system-ui, sans-serif;
}

.search-results {
  max-height: min(45vh, 400px);
  overflow: auto;
}

.search-result {
  display: block;
  width: 100%;
  padding: 9px 8px;
  text-align: left;
  border-radius: 8px;
  font: 13px/1.45 system-ui, sans-serif;
}

.search-result:active {
  background: var(--accent-soft);
}

.search-result b {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-weight: 500;
}

.empty-state {
  position: fixed;
  inset: 110px 20px 80px;
  display: grid;
  place-content: center;
  text-align: center;
  color: var(--muted);
  font: 15px/1.7 system-ui, sans-serif;
}

.empty-state button {
  margin: 16px auto 0;
  padding: 10px 20px;
  border-radius: 10px;
  color: var(--paper-raised);
  background: var(--accent);
}

.toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(76px + var(--bottom-safe));
  max-width: calc(100vw - 42px);
  padding: 10px 15px;
  border-radius: 20px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  background: var(--ink);
  color: var(--paper);
  font: 13px system-ui, sans-serif;
  transition: opacity 150ms ease, transform 150ms ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

[hidden] {
  display: none !important;
}

@media (min-width: 700px) {
  body {
    background: var(--paper);
    background: color-mix(in srgb, var(--paper) 75%, var(--line));
  }

  .manuscript,
  #editor {
    background: var(--paper);
    box-shadow: 0 0 36px var(--shadow);
  }
}
