/* 阅读器样式：按「简阅」的阅读体验做的排版系统
   主题色、字体、字号、版宽、行距、首行缩进 都可以在阅读页设置面板里调 */

/* ========== 名义主题 ==========
   每套主题只定义三个颜色（底色、文字色、强调色），
   其余（边框、次要文字、卡片底）由 color-mix 自动推出来，省得维护八套色板 */
:root {
  --bg: #f7f6f2;
  --fg: #2b2620;
  --accent: #a63d32;

  /* 派生颜色：跟着上面三个自动变 */
  --muted: color-mix(in srgb, var(--fg) 42%, var(--bg));
  --border: color-mix(in srgb, var(--fg) 15%, var(--bg));
  --card: color-mix(in srgb, var(--fg) 4%, var(--bg));
  --code-bg: color-mix(in srgb, var(--fg) 7%, var(--bg));

  /* 阅读排版（阅读页可调，列表页也用得上） */
  --read-size: 26px;      /* 正文字号 */
  --measure: 88%;         /* 版面宽度：占视口百分比，跟着窗口一起伸缩 */
  --read-lh: 1.75;        /* 行间距 */
  --read-ls: 0em;         /* 字间距 */
  --indent: 2em;          /* 首行缩进 */
  --read-font: -apple-system, "PingFang SC", "Hiragino Sans GB",
               "Microsoft YaHei", system-ui, sans-serif;
}

/* 跟随系统时，深色模式下用深灰主题 */
@media (prefers-color-scheme: dark) {
  html:not([data-theme]) {
    --bg: #2a2a2d;
    --fg: #c9c9c9;
    --accent: #d98a7c;
  }
}

html[data-theme="white"] { --bg: #ffffff; --fg: #1f2124; --accent: #a63d32; }
html[data-theme="paper"] { --bg: #f7f6f2; --fg: #2b2620; --accent: #a63d32; }
html[data-theme="gray"]  { --bg: #ededee; --fg: #26262a; --accent: #8a4a3f; }
html[data-theme="blue"]  { --bg: #e9eef4; --fg: #1f2a35; --accent: #3b5f86; }
html[data-theme="green"] { --bg: #e9f0e7; --fg: #232b22; --accent: #4a7350; }
html[data-theme="cream"] { --bg: #f6efdf; --fg: #3b3225; --accent: #9c6b34; }
html[data-theme="dark"]  { --bg: #2a2a2d; --fg: #c9c9c9; --accent: #d98a7c; }
html[data-theme="black"] { --bg: #121212; --fg: #a8a8a8; --accent: #c98070; }

/* 兼容旧写法（列表页的按钮还在用 light/dark） */
html[data-theme="light"] { --bg: #f7f6f2; --fg: #2b2620; --accent: #a63d32; }

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, "SF Pro Text", "PingFang SC", "Hiragino Sans GB",
               "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ========== 列表页 ========== */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 14px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.search {
  flex: 1;
  min-width: 0;
  padding: 9px 13px;
  font-size: 15px;
  color: var(--fg);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 9px;
  outline: none;
}
.search:focus { border-color: var(--accent); }

.icon-btn {
  flex: none;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  font-size: 16px;
  color: var(--fg);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 9px;
  cursor: pointer;
}
.icon-btn:active { background: var(--code-bg); }

.list {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.item {
  position: relative;
  padding: 16px 18px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  display: block;
}
.item:hover { border-color: color-mix(in srgb, var(--accent) 35%, var(--border)); }

.item-title {
  font-size: 16px;
  font-weight: 500;
  line-height: 1.45;
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.item-excerpt {
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-bottom: 8px;
}

.item-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
}

.dot::before { content: "·"; margin: 0 2px; }

/* 每篇文章底部一条细进度线，读到一半能一眼看出来 */
.item-progress {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 2px;
  background: var(--accent);
  border-radius: 0 0 0 12px;
  opacity: 0.5;
}

.empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.8;
}

/* ========== 阅读页 ========== */

.read-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 2px;
  background: var(--accent);
  z-index: 50;
  transition: width 0.1s linear;
}

.read-header {
  max-width: min(var(--measure), 1500px);
  margin: 0 auto;
  padding: 28px 1.2em 0;
  font-family: var(--read-font);
}

.read-title {
  font-size: calc(var(--read-size) * 1.55);
  font-weight: 600;
  line-height: 1.4;
  margin: 0 0 0.7em;
  letter-spacing: 0;
}

.read-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  font-size: 13px;
  color: var(--muted);
  padding-bottom: 1.5em;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.8em;
}

.read-sub a { color: var(--muted); text-decoration: none; border-bottom: 1px dotted var(--muted); }

/* 正文区：所有排版规则的集中地 */
.reader {
  max-width: min(var(--measure), 1500px);
  margin: 0 auto;
  padding: 0 1.2em 30vh;
  font-family: var(--read-font);
  font-size: min(var(--read-size), 4.8vw);
  line-height: var(--read-lh);
  letter-spacing: var(--read-ls);
  word-spacing: 0;
  /* 中文不用两端对齐，否则字距会忽大忽小 */
  text-align: left;
  word-break: break-word;
  overflow-wrap: break-word;
}

/* 首行缩进：中文排版惯例，缩两个字 */
.reader p { margin: 0 0 1.05em; text-indent: var(--indent); }
/* 纯图片段落、以及列表里的段落不缩进，否则会显得很怪 */
.reader p:has(> img:only-child) { text-indent: 0; }
.reader li p { text-indent: 0; }

.reader h1, .reader h2, .reader h3, .reader h4 {
  line-height: 1.45;
  margin: 1.9em 0 0.7em;
  font-weight: 600;
  text-indent: 0;
}
.reader h1 { font-size: 1.35em; }
.reader h2 { font-size: 1.22em; }
.reader h3 { font-size: 1.1em; }
.reader h4 { font-size: 1em; }

.reader img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.6em auto;
  border-radius: 5px;
}

/* 微信文章里经常有超小尺寸的表情图/图标，放大到能看清 */
.reader img[width="20"], .reader img[width="40"] { display: inline; margin: 0 2px; vertical-align: -0.15em; }

.reader figure { margin: 1.6em 0; }
.reader figcaption {
  font-size: 0.82em;
  color: var(--muted);
  text-align: center;
  margin-top: 0.6em;
}

.reader blockquote {
  margin: 1.4em 0;
  padding: 0.2em 0 0.2em 1em;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.reader blockquote p { text-indent: 0; }

.reader ul, .reader ol { margin: 0 0 1.05em; padding-left: 1.5em; }
.reader li { margin-bottom: 0.4em; }

.reader a { color: var(--accent); text-decoration: none; border-bottom: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); }
.reader a:hover { border-bottom-color: var(--accent); }

.reader pre {
  overflow-x: auto;
  background: var(--code-bg);
  padding: 1em;
  border-radius: 7px;
  font-size: 0.84em;
  line-height: 1.6;
  margin: 1.4em 0;
}
.reader code {
  font-family: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: var(--code-bg);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}
.reader pre code { background: none; padding: 0; font-size: 1em; }

.reader table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86em;
  margin: 1.4em 0;
  display: block;
  overflow-x: auto;
}
.reader th, .reader td { border: 1px solid var(--border); padding: 0.5em 0.7em; text-align: left; }
.reader th { background: var(--code-bg); font-weight: 500; }

.reader hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }

/* ========== 大纲（目录） ========== */

/* 左上角的感应区：鼠标移进来就弹出目录 */
.toc-hot {
  position: fixed;
  top: 0;
  left: 0;
  width: 56px;
  height: 56px;
  z-index: 46;
  /* 只感应真正的鼠标，手机上不挡点击 */
  display: none;
}
@media (hover: hover) { .toc-hot { display: block; } }

.toc {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 45;
  width: 290px;
  max-width: 80vw;
  height: 100%;
  padding: 26px 18px 48px;
  overflow-y: auto;
  background: var(--bg);
  border-right: 1px solid var(--border);
  box-shadow: 6px 0 24px rgba(0, 0, 0, 0.06);

  font-size: 13.5px;
  line-height: 1.75;

  /* 收起状态：稍微左移 + 透明。用「淡入一小段」代替「整块从屏幕外滑进来」，
     因为长距离滑动的速度很难调得舒服，短距离位移反而显得轻 */
  transform: translateX(-24px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  will-change: transform, opacity;

  /* 收起：位移和淡出同时走，visibility 等动画结束再切，
     否则元素会瞬间消失，动画根本看不到 */
  transition:
    transform 0.22s cubic-bezier(0.4, 0, 1, 1),
    opacity 0.18s ease-in,
    visibility 0s linear 0.22s;
}

.toc.open {
  transform: none;
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  /* 展开：先快后慢（ease-out 曲线），像是"落定"而不是匀速平移 */
  transition:
    transform 0.32s cubic-bezier(0.16, 1, 0.3, 1),
    opacity 0.22s ease-out,
    visibility 0s;
}

/* 标题少于 2 个时没有目录，直接不占位 */
.toc.is-empty { display: none; }
.toc-head {
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.04em;
  margin-bottom: 10px;
}
.toc a {
  display: block;
  padding: 3px 0;
  color: var(--muted);
  text-decoration: none;
  border: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.15s ease, transform 0.15s ease;
}
/* 鼠标扫过时轻微右移，给一点"这里可以点"的反馈 */
.toc a:hover { transform: translateX(2px); }
.toc a:hover, .toc a.cur { color: var(--fg); }
.toc a.lv1 { color: var(--fg); font-weight: 500; }
.toc a.lv2 { padding-left: 14px; }
.toc a.lv3 { padding-left: 28px; font-size: 0.95em; }
.toc a.lv4 { padding-left: 42px; font-size: 0.9em; }

/* ========== 设置面板 ========== */

.settings {
  position: fixed;
  left: 50%;
  bottom: 72px;
  transform: translateX(-50%);
  z-index: 41;
  width: 336px;
  max-width: calc(100vw - 28px);
  padding: 6px 16px 14px;
  font-size: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.14);
  max-height: 70vh;
  overflow-y: auto;
}
.settings[hidden] { display: none; }

.settings .row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}
/* .row 是 flex，会盖掉浏览器默认的 [hidden] { display:none }，得单独补一条 */
.settings .row[hidden] { display: none; }
.settings .row > label {
  flex: none;
  width: 60px;
  color: var(--muted);
}
.settings .row input[type="range"] {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
}
.settings .val {
  flex: none;
  width: 42px;
  text-align: right;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.settings .swatches {
  display: flex;
  gap: 7px;
  flex-wrap: wrap;
  flex: 1;            /* 占满剩余宽度，放不下就换行，不会顶出面板 */
  min-width: 0;
}
.settings .sw {
  width: 20px;
  height: 20px;
  padding: 0;
  border-radius: 6px;
  border: 1px solid color-mix(in srgb, var(--fg) 22%, transparent);
  cursor: pointer;
}
.settings .sw.on {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.settings select {
  flex: 1;
  min-width: 0;
  padding: 5px 8px;
  font-size: 13px;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}

.settings .grp {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: 0.04em;
}

/* ========== 底部工具条 ========== */

.toolbar {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 42;
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 7px 10px;
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.tool {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  font-size: 14px;
  color: var(--fg);
  background: none;
  border: none;
  border-radius: 50%;
  cursor: pointer;
}
.tool:hover { background: var(--code-bg); }
.tool.on { background: var(--code-bg); }

.tool-sep { width: 1px; height: 18px; background: var(--border); margin: 0 3px; }

/* ========== 登录遮罩 ========== */

.gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}
.gate-box {
  width: 100%;
  max-width: 340px;
  text-align: center;
}
.gate-box h1 { font-size: 18px; font-weight: 500; margin: 0 0 8px; }
.gate-box p { font-size: 13px; color: var(--muted); margin: 0 0 20px; line-height: 1.6; }
.gate-box input {
  width: 100%;
  padding: 11px 14px;
  font-size: 14px;
  color: var(--fg);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 9px;
  outline: none;
  margin-bottom: 10px;
}
.gate-box input:focus { border-color: var(--accent); }
.gate-box button {
  width: 100%;
  padding: 11px;
  font-size: 14px;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 9px;
  cursor: pointer;
}

/* ========== 加载与提示 ========== */

.spinner {
  width: 20px;
  height: 20px;
  margin: 60px auto;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%);
  z-index: 60;
  padding: 9px 16px;
  font-size: 13px;
  color: var(--bg);
  background: var(--fg);
  border-radius: 999px;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
  white-space: nowrap;
}
.toast.show { opacity: 0.92; }

/* ==========================================================================
   直排（竖排）布局
   --------------------------------------------------------------------------
   思路：把正文容器的书写方向换成 vertical-rl，整篇文字就变成「自上而下、
   自右向左」的古籍排法，滚动方向也随之从上下变成左右。

   直排下的「物理方向」和横排正好对调，记住这张对照表，下面的规则就都能看懂：
     横排的「左右」 → 直排的「上下」（行内方向 inline）
     横排的「上下」 → 直排的「左右」（换行方向 block，从右往左）
   所以横排里的 margin-bottom（段后距），到直排要写成 margin-left（下一列在左边）。
   ========================================================================== */

/* 横向滚动的容器：直排时由它负责滚动，而不是整个窗口。
   为什么不直接把 writing-mode 加在 <html> 上？因为那样根滚动容器在
   滚动量正好为 0 时会画不出东西（Chrome 在这个位置的已知毛病），
   刚进文章就是一片空白。换成子元素当滚动容器就没这个问题。 */
html[data-flow="vertical"] .vscroll {
  /* 竖排元素的 width:auto 在 Chrome 里会退化成 max-content（整个文档那么宽），
     于是自己也滚不动了。必须显式写 100%，把可视宽度钉在视口上 */
  width: 100%;
  height: 100vh;
  height: 100dvh;
  writing-mode: vertical-rl;
  overflow-x: auto;
  overflow-y: hidden;
  /* 横向滚动条又丑又占高度，藏起来，用滚轮/手指滚就够了 */
  scrollbar-width: none;
  /* 滑到头时别触发浏览器的「侧滑后退」手势 */
  overscroll-behavior-x: contain;
}
html[data-flow="vertical"] .vscroll::-webkit-scrollbar { display: none; }

/* 顶部进度线：直排时改成右侧的一条竖线，从顶部长下来 */
.read-progress { width: var(--progress, 0%); }
html[data-flow="vertical"] .read-progress {
  left: auto;
  right: 0;
  top: 0;
  bottom: auto;
  width: 3px;
  height: var(--progress, 0%);
  transition: height 0.1s linear;
}

/* ---------- 标题区 ---------- */

html[data-flow="vertical"] .read-header {
  /* 直排里 height 是「一列能装多高」，也就是行宽；width 才是列数。
     max-content 让它正好包住标题和副标题那几列 */
  width: max-content;
  max-width: none;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  padding: 2em 2em 2em 2.4em;
  /* 分隔线从「标题下方」变成「标题左侧」（标题在右，正文在它左边） */
  border-bottom: none;
  border-left: 1px solid var(--border);
}

html[data-flow="vertical"] .read-title {
  /* 标题和副标题之间原来是「下方留白」，直排改成「左侧留白」 */
  margin: 0 0 0 0.8em;
}

html[data-flow="vertical"] .read-sub {
  padding-bottom: 0;
  padding-left: 1.5em;      /* 原来靠底边距拉开，现在靠左边距 */
  border-bottom: none;
  border-left: 1px solid var(--border);
  margin: 0;
}

/* ---------- 正文 ---------- */

html[data-flow="vertical"] .reader {
  /* 关键三行：高度定死 = 每列的行宽；宽度交给内容自己撑开；
     max-width 必须解除，否则长文章会被 1500px 卡住，后面的字直接看不见 */
  height: 100vh;
  height: 100dvh;
  width: max-content;
  max-width: none;
  margin: 0;
  /* 物理顺序：上 右 下 左。右侧=第一列的起始边，左侧=全文结束的地方，
     留出 25vw 是为了读到最后一段时还能把它拖到屏幕中间，别贴着边 */
  padding: 1.6em 1.2em 1.6em 25vw;
  /* 字宽受「列高」限制，不再是屏幕宽度 */
  font-size: min(var(--read-size), 5.2vh);
  /* text-align:left 在竖排里含义会漂移，明确用 start（= 从顶头开始排） */
  text-align: start;
}

/* 段落：段后距 = 下一列在左边，所以改成 margin-left */
html[data-flow="vertical"] .reader p { margin: 0 0 0 1.05em; }

/* 标题：right 是和上一列的间隔，left 是和下一列的间隔 */
html[data-flow="vertical"] .reader h1,
html[data-flow="vertical"] .reader h2,
html[data-flow="vertical"] .reader h3,
html[data-flow="vertical"] .reader h4 {
  margin: 0 1.9em 0 0.7em;
}

/* 图片：竖排里图片的「高」才是占用的行宽，所以按高度给上限。
   上下 auto = 在列里居中；左右 1.4em = 和前后列拉开距离 */
html[data-flow="vertical"] .reader img {
  margin: auto 1.4em;
  /* 竖排里 max-width:100% 参照的是「列宽」，会把图莫名压小，这里关掉 */
  max-width: none;
  max-height: 80vh;
}

html[data-flow="vertical"] .reader figure { margin: 0 1.6em; }

/* 引用块：竖线从左边移到右边（文字从右边开始读），缩进方向跟着翻 */
html[data-flow="vertical"] .reader blockquote {
  margin: 0 1.4em;
  padding: 0 1em 0 0;
  border-left: none;
  border-right: 3px solid var(--border);
}

/* 列表的符号在排版的「起始边」= 右边，所以留白也挪到右边 */
html[data-flow="vertical"] .reader ul,
html[data-flow="vertical"] .reader ol {
  margin: 0 0 0 1.05em;
  padding: 0 1.5em 0 0;
}
html[data-flow="vertical"] .reader li { margin: 0 0 0 0.4em; }

/* 分隔线：竖排里要竖着画 */
html[data-flow="vertical"] .reader hr {
  border-top: none;
  border-right: 1px solid var(--border);
  margin: 0 2em;
}

/* 代码块横过来放：竖着排的代码没法看，宁可牺牲一点一致性 */
html[data-flow="vertical"] .reader pre {
  writing-mode: horizontal-tb;
  max-width: 78vw;
  margin: 0 1.4em;
}

/* 表格也保持横排，否则列关系和边框会错乱 */
html[data-flow="vertical"] .reader table {
  writing-mode: horizontal-tb;
  max-width: 78vw;
  margin: 0 1.4em;
}

/* ---------- 设置面板里的「横排 / 直排」分段按钮 ---------- */

.settings .seg {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 3px;
  padding: 3px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 9px;
}
.settings .seg button {
  flex: 1;
  padding: 4px 0;
  font-family: inherit;
  font-size: 12.5px;
  color: var(--muted);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}
.settings .seg button.on {
  color: var(--fg);
  background: var(--bg);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
