:root {
  --bg: #f6f1e8;
  --ink: #1c1916;
  --muted: #6b635a;
  --panel: #fffdf8;
  --line: #ddd4c6;
  --accent: #8b4513;
  --accent-soft: #f0e0d0;
  --ok: #2f6f4e;
  --err: #a33;
  --topbar-h: 3.25rem;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC",
    "Microsoft YaHei", ui-sans-serif, system-ui, sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
}

body {
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.hidden,
[hidden] {
  display: none !important;
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.5rem;
  min-height: var(--topbar-h);
  padding: calc(0.55rem + var(--safe-top)) 0.75rem 0.55rem;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 40;
}

.topbar-center {
  min-width: 0;
  text-align: center;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.04em;
  font-size: 0.95rem;
}

.session-meta {
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 0.1rem;
}

.icon-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 999px;
  min-height: 2.25rem;
  padding: 0 0.85rem;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}

.icon-btn.ghost {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.gate {
  max-width: 36rem;
  margin: 3rem auto;
  padding: 0 1.25rem;
}

.gate h1 {
  font-size: 1.45rem;
  margin-bottom: 0.5rem;
}

.gate p {
  color: var(--muted);
  line-height: 1.7;
}

.workspace {
  position: relative;
  min-height: calc(100dvh - var(--topbar-h) - var(--safe-top));
}

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(28, 25, 22, 0.35);
  z-index: 50;
}

.sidebar,
.comment-panel {
  background: var(--panel);
  font-family: inherit;
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.sidebar-head {
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0;
}

.sidebar-head.sub {
  margin: 1.25rem 0 0.75rem;
}

.toc {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  padding-bottom: calc(1rem + var(--safe-bottom));
}

.toc a {
  color: var(--ink);
  text-decoration: none;
  padding: 0.7rem 0.65rem;
  border-radius: 10px;
  font-size: 0.95rem;
  line-height: 1.4;
  min-height: 2.75rem;
  display: flex;
  align-items: flex-start;
}

.toc a:active,
.toc a.active {
  background: var(--accent-soft);
}

.toc a .num {
  color: var(--muted);
  margin-right: 0.35rem;
  flex: none;
}

.reader {
  padding: 1.1rem 1.05rem calc(5.5rem + var(--safe-bottom));
  max-width: 40rem;
  margin: 0 auto;
  width: 100%;
}

.chapter-head h1 {
  font-family: "Source Han Serif SC", "Noto Serif SC", "Songti SC", "SimSun",
    Georgia, serif;
  font-size: 1.35rem;
  margin: 0 0 1.1rem;
  line-height: 1.4;
  font-weight: 700;
}

.chapter-body {
  font-family: "Source Han Serif SC", "Noto Serif SC", "Songti SC", "SimSun",
    Georgia, serif;
  -webkit-user-select: text;
  user-select: text;
}

.chapter-body p {
  margin: 0 0 1.05em;
  line-height: 1.95;
  font-size: 1.12rem;
  text-indent: 2em;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.hint {
  color: var(--muted);
  font-size: 0.82rem;
  margin: 1.5rem 0 1rem;
}

.chapter-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
  margin-top: 0.5rem;
}

.nav-btn {
  min-height: 2.75rem;
  width: 100%;
}

.selection-preview {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 0.75rem;
  min-height: 4rem;
  font-size: 0.92rem;
  line-height: 1.5;
  margin-bottom: 0.75rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.selection-preview.empty {
  color: var(--muted);
}

textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.85rem;
  font: inherit;
  font-size: 16px; /* 避免 iOS 聚焦自动放大 */
  line-height: 1.5;
  resize: vertical;
  background: #fff;
  margin-bottom: 0.75rem;
  min-height: 7rem;
}

.btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  min-height: 2.75rem;
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  width: 100%;
}

.btn.compact {
  width: auto;
  min-height: 2.35rem;
  padding: 0.45rem 0.9rem;
  flex: none;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.status {
  min-height: 1.25rem;
  font-size: 0.85rem;
  margin: 0.5rem 0 0;
}

.status.ok {
  color: var(--ok);
}

.status.err {
  color: var(--err);
}

.comment-list {
  list-style: none;
  padding: 0 0 calc(1rem + var(--safe-bottom));
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.comment-list li {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.75rem;
  background: #fff;
  font-size: 0.9rem;
  line-height: 1.45;
}

.comment-list .quote {
  color: var(--muted);
  border-left: 3px solid var(--accent-soft);
  padding-left: 0.55rem;
  margin-bottom: 0.4rem;
}

.comment-list .meta {
  color: var(--muted);
  font-size: 0.75rem;
  margin-top: 0.35rem;
}

.selection-bar {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 0.65rem;
  max-width: min(92vw, 28rem);
  padding: 0.55rem 0.65rem 0.55rem 0.9rem;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  box-shadow: 0 10px 28px rgba(28, 25, 22, 0.28);
}

.selection-bar-text {
  font-size: 0.82rem;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 14rem;
}

/* —— 手机：正文全宽，目录/评论为抽屉 —— */
@media (max-width: 960px) {
  .drawer {
    position: fixed;
    z-index: 55;
    background: var(--panel);
    overflow: auto;
    overscroll-behavior: contain;
    transition: transform 0.22s ease;
  }

  .drawer-left {
    top: 0;
    left: 0;
    bottom: 0;
    width: min(86vw, 20rem);
    padding: calc(0.75rem + var(--safe-top)) 0.75rem 0.75rem;
    border-right: 1px solid var(--line);
    transform: translateX(-105%);
  }

  .drawer-right {
    left: 0;
    right: 0;
    bottom: 0;
    max-height: min(78dvh, 36rem);
    padding: 0.85rem 0.9rem calc(0.85rem + var(--safe-bottom));
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    transform: translateY(110%);
    box-shadow: 0 -8px 30px rgba(28, 25, 22, 0.12);
  }

  body.toc-open .drawer-left {
    transform: translateX(0);
  }

  body.comment-open .drawer-right {
    transform: translateY(0);
  }

  body.drawer-open {
    overflow: hidden;
  }

  .reader {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .chapter-body p {
    font-size: 1.15rem;
    line-height: 2;
  }
}

/* —— 桌面：三栏布局 —— */
@media (min-width: 961px) {
  .topbar {
    grid-template-columns: 1fr;
    padding-left: 1.25rem;
    padding-right: 1.25rem;
  }

  .topbar .icon-btn {
    display: none !important;
  }

  .topbar-center {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    text-align: left;
    gap: 1rem;
  }

  .session-meta {
    font-size: 0.85rem;
    margin-top: 0;
  }

  .workspace {
    display: grid;
    grid-template-columns: minmax(13rem, 16rem) minmax(0, 1fr) minmax(17rem, 21rem);
  }

  .backdrop,
  .drawer-head .icon-btn,
  .selection-bar {
    display: none !important;
  }

  .sidebar,
  .comment-panel {
    position: sticky;
    top: calc(var(--topbar-h) + var(--safe-top));
    height: calc(100dvh - var(--topbar-h) - var(--safe-top));
    overflow: auto;
    padding: 0.85rem;
  }

  .sidebar {
    border-right: 1px solid var(--line);
  }

  .comment-panel {
    border-left: 1px solid var(--line);
  }

  .reader {
    padding: 1.5rem 2rem 3rem;
  }

  .chapter-head h1 {
    font-size: 1.55rem;
  }

  .chapter-body p {
    font-size: 1.08rem;
    line-height: 1.9;
  }
}
