/* 应用外壳样式。token 全部来自 DESIGN.md（colors / typography / rounded / spacing /
   components），不在这里发明新值。画布内的样式在 canvas.css，两者不要互相渗。

   布局：顶栏一条，工具栏横排在画布上方，导入区紧贴工具栏下方，再往下是画布本身。
   主色一屏只出现一次，就是导出按钮。 */

:root {
  /* colors */
  --ct-chrome-bg: #f5f6f7;
  --ct-chrome-surface: #ffffff;
  --ct-chrome-surface-sunken: #edeff1;
  --ct-chrome-border: #e2e5e8;
  --ct-chrome-border-strong: #cdd2d7;
  --ct-chrome-text: #22262b;
  --ct-chrome-text-secondary: #5b636b;
  --ct-chrome-text-muted: #6a727a;
  --ct-chrome-text-disabled: #9aa1a8;
  --ct-primary: #2660b4;
  --ct-primary-hover: #2055a0;
  --ct-primary-active: #1a4a8c;
  --ct-primary-subtle: #eaf1fb;
  --ct-on-primary: #ffffff;
  --ct-success: #1e7a45;
  --ct-warning: #8a5a00;
  --ct-warning-subtle: #fbf1e0;
  --ct-error: #b3261e;
  --ct-error-subtle: #fceae8;
  --ct-focus-ring: #2660b4;
  --ct-scrim: rgba(17, 20, 23, 0.45);
  --ct-canvas-placeholder: #9aa1a8;
  --ct-canvas-border: #c9cdd2;

  /* rounded */
  --ct-rounded-sm: 4px;
  --ct-rounded-md: 6px;
  --ct-rounded-lg: 10px;

  /* 布局 */
  --ct-topbar-height: 52px;
  --ct-control-height: 32px;
  /* 移动端（DESIGN.md 的「移动端布局尺寸」）：断点 >=721 / <=720 / <=480 */
  --ct-touch-min: 44px;
  --ct-topbar-height-phone: 44px;
  --ct-toolbar-height-touch: 44px;
  --ct-chip-height: 40px;
  --ct-chip-height-phone: 36px;
  --ct-actionbar-height: 56px;

  /* 动效：整个界面只有三处 */
  --ct-motion: cubic-bezier(0.2, 0, 0.2, 1);

  --ct-chrome-font: system-ui, "PingFang SC", "Microsoft YaHei", sans-serif;
  --ct-mono-font: "JetBrains Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/* hidden 属性必须压过组件自己的 display：面板、抽屉、横幅、toast 都靠它开关 */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  display: grid;
  /* 三行：顶栏 / 内置浏览器横幅 / 工作区。横幅收起时不留空行，所以用 auto 而不是写死高度 */
  grid-template-rows: auto auto minmax(0, 1fr);
  /* 显式列定义：不给的话 690 宽的内容会把列撑宽，窄屏出现横向滚动（已知陷阱 #4） */
  grid-template-columns: minmax(0, 1fr);
  /* 不用 height: 100%：iOS 地址栏伸缩会让画布反复跳动 */
  height: 100vh;
  background-color: var(--ct-chrome-bg);
  color: var(--ct-chrome-text);
  font-family: var(--ct-chrome-font);
  font-size: 13px;
  line-height: 1.55;
}

/* 三个区块按行显式落位：#wechat-banner 用 hidden 收起来时，不能让 #stage 顶到第 2 行 */
#topbar {
  grid-row: 1;
}

#wechat-banner {
  grid-row: 2;
}

#stage {
  grid-row: 3;
}

@supports (height: 100dvh) {
  body {
    height: 100dvh;
  }
}

.icon-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.icon {
  /* 图标画在 24x24 网格上，内容只占中间 20 格，所以框要比视觉尺寸大一档：
     22px 的框对应约 18px 的实际笔画范围，跟原来的手感一致 */
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon--sm {
  width: 18px;
  height: 18px;
}

/* ------------------------------------------------------------------ 顶栏 */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  /* min-height 而不是 height：手机档要加 safe-area 的 padding-top，写死高度会把它挤掉 */
  min-height: var(--ct-topbar-height);
  /* 定位上下文：<=720 的设置浮层挂在它下面（top: 100%）。
     这里以及它的祖先链上不能出现 transform / filter / backdrop-filter / overflow: hidden，
     否则 <=480 的 position: fixed 底栏会退化成相对它定位（已知陷阱 #6） */
  position: relative;
  min-width: 0;
  padding: 0 20px;
  border-bottom: 1px solid var(--ct-chrome-border);
  background-color: var(--ct-chrome-surface);
}

#topbar-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 20px;
  min-width: 0;
  flex-wrap: nowrap;
}

/* 顶栏设置入口：只在紧凑档与手机档出现（桌面档顶栏本来就有常驻的按字数分页控件）。
   不用 hidden 属性控制：可见性完全交给 CSS，开合状态用 aria-expanded。 */
#settings-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: var(--ct-control-height);
  height: var(--ct-control-height);
  padding: 0;
  border: 0;
  border-radius: var(--ct-rounded-md);
  background: none;
  color: var(--ct-chrome-text-secondary);
  cursor: pointer;
}

#settings-toggle:hover {
  background-color: var(--ct-chrome-surface-sunken);
  color: var(--ct-chrome-text);
}

/* 顶栏右上角的补充设置：目前只有按字数分页一项 */
.topbar-settings {
  display: flex;
  align-items: center;
  gap: 6px;
}

.topbar-settings__label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--ct-chrome-text-secondary);
  font-size: 12px;
  white-space: nowrap;
}

/* 输入框与三角合成一个控件，浮层挂在它下面，位置才对得上 */
.topbar-settings__field {
  position: relative;
  display: flex;
  align-items: center;
  height: 28px;
  border: 1px solid var(--ct-chrome-border);
  border-radius: var(--ct-rounded-sm);
  background-color: var(--ct-chrome-surface);
}

.topbar-settings__field:focus-within {
  border-color: var(--ct-chrome-border-strong);
}

.topbar-settings__input {
  width: 76px;
  height: 100%;
  padding: 0 2px 0 8px;
  border: 0;
  background: transparent;
  color: var(--ct-chrome-text);
  font-family: inherit;
  font-size: 12px;
}

.topbar-settings__input:focus {
  outline: none;
}

.topbar-settings__toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ct-chrome-text-secondary);
  cursor: pointer;
}

.topbar-settings__toggle .icon {
  width: 14px;
  height: 14px;
}

.topbar-settings__toggle:hover {
  color: var(--ct-chrome-text);
}

/* 预设列表。不用 <datalist>：输入框的值与某个 option 相同时浏览器只列那一项。 */
/* hidden 属性默认 display:none 会被这里的 display:flex 覆盖，补回隐藏态，浮层才不会一直显示 */
.topbar-settings__menu[hidden] {
  display: none;
}

.topbar-settings__menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  min-width: 100%;
  padding: 4px;
  border: 1px solid var(--ct-chrome-border);
  border-radius: var(--ct-rounded-sm);
  background-color: var(--ct-chrome-surface);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.14);
}

.topbar-settings__option {
  padding: 6px 10px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--ct-chrome-text);
  font-family: inherit;
  font-size: 12px;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}

.topbar-settings__option:hover,
.topbar-settings__option:focus {
  background-color: var(--ct-chrome-bg);
  outline: none;
}

.topbar-settings__help {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--ct-chrome-text-secondary);
  cursor: pointer;
}

.topbar-settings__help:hover {
  background-color: var(--ct-chrome-bg);
  color: var(--ct-chrome-text);
}

/* 确定按钮：只放一个勾形图标，输入的字数点了它才生效 */
.topbar-settings__confirm {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--ct-chrome-border-strong);
  border-radius: var(--ct-rounded-sm);
  background-color: var(--ct-chrome-surface);
  color: var(--ct-chrome-text);
  cursor: pointer;
}

.topbar-settings__confirm:hover:not(:disabled) {
  border-color: var(--ct-primary);
  color: var(--ct-primary);
}

.topbar-settings__confirm:disabled {
  border-color: var(--ct-chrome-border);
  color: var(--ct-chrome-text-disabled);
  cursor: default;
}

.topbar__mark {
  display: flex;
  align-items: baseline;
  gap: 12px;
  min-width: 0;
}

.product-mark {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.product-note {
  color: var(--ct-chrome-text-muted);
  font-size: 12px;
}

/* ------------------------------------------------------------------ 内置浏览器横幅 */

/* 微信 / QQ / 微博内置浏览器会拦下载。它是提示不是错误：只用 1px 边框加一行说明，
   不拿主色或警告底色抢注意力；不阻塞导出，可关闭（关闭只在本次会话内有效）。 */
#wechat-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--ct-chrome-border);
  background-color: var(--ct-chrome-bg);
  color: var(--ct-chrome-text-secondary);
  font-size: 12px;
}

#wechat-banner-text {
  min-width: 0;
}

#wechat-banner-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: var(--ct-rounded-sm);
  background: none;
  color: inherit;
  cursor: pointer;
}

#wechat-banner-close:hover {
  background-color: var(--ct-chrome-surface-sunken);
  color: var(--ct-chrome-text);
}

/* ------------------------------------------------------------------ 「更多」面板与背板 */

/* 面板 / 抽屉打开时铺一层透明背板：替画布吃掉点击，点背板即关闭。
   非模态，不做焦点陷阱（PRD/04 5.6）。 */
.changtu-backdrop {
  position: fixed;
  inset: 0;
  z-index: 44;
  background: transparent;
}

/* 工具栏右端的「更多」按钮。桌面档 19 个控件全在，不需要它 */
#toolbar-more {
  display: none;
}

/* 块结构与插入项的底部面板。开合只有一处动效，见下面的 prefers-reduced-motion */
#toolbar-more-panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 45;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  max-height: 60vh;
  padding: 12px;
  overflow-y: auto;
  border-top: 1px solid var(--ct-chrome-border);
  background-color: var(--ct-chrome-surface);
  box-shadow: 0 -12px 28px rgba(0, 0, 0, 0.16);
  animation: ct-sheet-in 140ms var(--ct-motion);
}

@keyframes ct-sheet-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.toolbar-more__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  min-height: 64px;
  padding: 8px 4px;
  border: 0;
  border-radius: var(--ct-rounded-md);
  background: none;
  color: var(--ct-chrome-text-secondary);
  font-family: inherit;
  font-size: 12px;
  line-height: 1.2;
  cursor: pointer;
}

.toolbar-more__item:hover {
  background-color: var(--ct-chrome-surface-sunken);
  color: var(--ct-chrome-text);
}

.toolbar-more__item[aria-pressed="true"] {
  background-color: var(--ct-primary-subtle);
  color: var(--ct-primary);
}

.toolbar-more__label {
  font-size: 12px;
  line-height: 1.2;
}

/* 「行高」那一格里是下拉，不是按钮 */
.toolbar-more__select {
  width: 100%;
  max-width: 76px;
  height: 28px;
  padding: 0 2px;
  border: 1px solid var(--ct-chrome-border);
  border-radius: var(--ct-rounded-sm);
  background-color: var(--ct-chrome-surface);
  color: var(--ct-chrome-text-secondary);
  font-family: inherit;
  font-size: 12px;
}

#export-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
}

/* 手动分页符。它把后面的内容推到下一列（下一张大图），所以编辑时它出现在新页的开头，
   导出时它本身不进任何一张图（app/render/split.py 的 is_page_break 会把它从每一组里排掉）。
   注意：hr 在浏览器里是 replaced element，::before / ::after 不会渲染。 */
#card hr[data-changtu-split="1"] {
  height: 0;
  border: 0;
  border-top: 1px dashed var(--ct-chrome-border-strong, #b6bcc7);
  margin: 0 0 28px;
}

.export-bar__main {
  display: flex;
  align-items: center;
  gap: 12px;
}

.export-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 18px;
  border: 0;
  border-radius: var(--ct-rounded-md);
  background-color: var(--ct-primary);
  color: var(--ct-on-primary);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

.export-button:hover:not(:disabled) {
  background-color: var(--ct-primary-hover);
}

.export-button:active:not(:disabled) {
  background-color: var(--ct-primary-active);
}

.export-button:disabled {
  background-color: var(--ct-chrome-surface-sunken);
  color: var(--ct-chrome-text-disabled);
  cursor: not-allowed;
}

.export-button__spinner {
  width: 12px;
  height: 12px;
  border: 1.5px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: ct-spin 800ms linear infinite;
}

@keyframes ct-spin {
  to {
    transform: rotate(360deg);
  }
}

.export-bar__readout {
  display: flex;
  flex-direction: column;
  min-width: 132px;
  color: var(--ct-chrome-text-secondary);
  font-family: var(--ct-mono-font);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.02em;
  line-height: 1.4;
}

.export-bar__readout-note {
  font-family: var(--ct-chrome-font);
  font-size: 12px;
  font-weight: 400;
  letter-spacing: 0;
  color: var(--ct-chrome-text-muted);
}

/* 常驻一行：总字数与导出张数 */
.export-bar__readout-summary {
  font-family: var(--ct-chrome-font);
  font-size: 12px;
  font-weight: 400;
  letter-spacing: 0;
  color: var(--ct-chrome-text-secondary);
}

.export-bar__readout-summary[hidden] {
  display: none;
}

.export-bar__readout-note--warn {
  color: var(--ct-warning);
}

.export-bar__error {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  max-width: 420px;
  padding: 2px 0;
  color: var(--ct-error);
  font-size: 12px;
  text-align: right;
  animation: ct-fade-up 120ms var(--ct-motion);
}

@keyframes ct-fade-up {
  from {
    opacity: 0;
    transform: translateY(2px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ------------------------------------------------------------------ 工作区 */

#stage {
  /* 用 flex 而不是 grid：行数会随内容变（导入区、缩略图都可能不出现），
     写死行数会让画布落到不弹性的那一行上。flex 里画布总是吃掉剩余空间。 */
  display: flex;
  flex-direction: column;
  min-height: 0;
  /* flex / grid 子项的 min-width 默认是 auto，会被 690 宽的内容撑破视口，
     窄屏下就会出现横向滚动条、缩放预览也就不触发了 */
  min-width: 0;
}

/* ------------------------------------------------------------------ 工具栏（横排） */

#toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 9px 20px;
  border-bottom: 1px solid var(--ct-chrome-border);
  background-color: var(--ct-chrome-surface);
}

.toolbar__group {
  display: flex;
  align-items: center;
  gap: 4px;
  height: var(--ct-control-height);
}

.toolbar__group + .toolbar__group {
  padding-left: 12px;
  border-left: 1px solid var(--ct-chrome-border);
}

.toolbar__button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--ct-control-height);
  height: var(--ct-control-height);
  padding: 0;
  border: 0;
  border-radius: var(--ct-rounded-md);
  background-color: transparent;
  color: var(--ct-chrome-text-secondary);
  cursor: pointer;
}

.toolbar__button:hover {
  background-color: var(--ct-chrome-surface-sunken);
  color: var(--ct-chrome-text);
}

.toolbar__button[aria-pressed="true"] {
  background-color: var(--ct-primary-subtle);
  color: var(--ct-primary);
}

.toolbar__button:disabled {
  color: var(--ct-chrome-text-disabled);
  cursor: default;
}

.toolbar__select {
  height: var(--ct-control-height);
  padding: 0 6px;
  border: 1px solid var(--ct-chrome-border);
  border-radius: var(--ct-rounded-sm);
  background-color: var(--ct-chrome-surface);
  color: var(--ct-chrome-text-secondary);
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
}

.toolbar__select--header {
  width: 62px;
}

.toolbar__select--font-size {
  width: 62px;
}

.toolbar__select--line-height {
  width: 66px;
}

/* 色块是正方形，右边另起一格放小三角开取色器（两格，中间一条分隔线）。
   取色器 input 与色块重合，原生弹窗才会从控件这里出来，而不是跑到左上角。 */
.toolbar__color-control {
  position: relative;
  display: inline-flex;
  align-items: stretch;
  height: var(--ct-control-height);
  border: 1px solid var(--ct-chrome-border);
  border-radius: var(--ct-rounded-md);
  overflow: hidden;
}

.toolbar__color-swatch {
  /* 容器有 1px 边框，内部高度是 control-height - 2；宽度跟着高度走才是正方形 */
  aspect-ratio: 1;
  height: 100%;
  padding: 4px;
  border: 0;
  background-color: var(--ct-chrome-surface);
  cursor: pointer;
}

.toolbar__color-swatch::after {
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 3px;
  background-color: var(--ct-swatch, #b3261e);
}

/* 小三角自己占一格 */
.toolbar__color-pick {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--ct-chrome-border);
  background-color: var(--ct-chrome-surface);
  cursor: pointer;
}

.toolbar__color-pick::after {
  content: "";
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 6px solid var(--ct-chrome-text-secondary);
}

.toolbar__color-pick:hover {
  background-color: var(--ct-chrome-bg);
}

.toolbar__color-pick:hover::after {
  border-top-color: var(--ct-primary);
}

/* 不用 display:none：隐藏的元素开不了系统取色器。
   贴在色块上、全透明、不接鼠标事件，程序化 click 仍可触发。 */
.toolbar__color-input {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--ct-control-height);
  height: var(--ct-control-height);
  padding: 0;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

/* ------------------------------------------------------------------ 导入区 */

#import-hint {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 20px;
  border-bottom: 1px solid var(--ct-chrome-border);
  background-color: var(--ct-primary-subtle);
  color: var(--ct-chrome-text-secondary);
  font-size: 13px;
}

#import-hint[hidden] {
  display: none;
}

.import-hint__icon {
  display: inline-flex;
  color: var(--ct-primary);
}

.import-hint__text {
  color: var(--ct-chrome-text);
}

.import-hint__kbd {
  padding: 1px 5px;
  border: 1px solid var(--ct-chrome-border-strong);
  border-radius: var(--ct-rounded-sm);
  background-color: var(--ct-chrome-surface);
  font-family: var(--ct-mono-font);
  font-size: 11px;
  color: var(--ct-chrome-text-secondary);
}

.import-hint__button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 12px;
  border: 1px solid var(--ct-primary);
  border-radius: var(--ct-rounded-md);
  background-color: var(--ct-chrome-surface);
  color: var(--ct-primary);
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
}

.import-hint__button:hover:not(:disabled) {
  background-color: var(--ct-primary-subtle);
}

.import-hint__button:disabled {
  border-color: var(--ct-chrome-border);
  color: var(--ct-chrome-text-disabled);
  cursor: default;
}

.import-hint__progress {
  color: var(--ct-chrome-text-secondary);
  font-size: 12px;
}

#import-hint[data-dragging="true"] {
  background-color: var(--ct-primary);
  color: var(--ct-on-primary);
}

#import-hint[data-dragging="true"] .import-hint__icon,
#import-hint[data-dragging="true"] .import-hint__text,
#import-hint[data-dragging="true"] .import-hint__progress,
#import-hint[data-dragging="true"] .import-hint__kbd {
  color: var(--ct-on-primary);
}

#import-hint[data-reject="true"] {
  background-color: var(--ct-error-subtle);
}

/* ------------------------------------------------------------------ 动效 */

/* ------------------------------------------------------------------ 画布 */

/* ------------------------------------------------------------------ 页面导航 */

/* 左侧缩略图：每页一条（页码 + 首行摘要），点一下跳到那一页。
   只有多页时才显示，单页时整条收起，版面还给画布。 */
#stage-body {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
}

#page-thumbs {
  flex: none;
  width: 148px;
  padding: 16px 10px;
  overflow-y: auto;
  border-right: 1px solid var(--ct-chrome-border);
  background-color: var(--ct-chrome-surface);
}

#page-thumbs[hidden] {
  display: none;
}

.page-thumbs__item {
  display: block;
  width: 100%;
  margin: 0 0 8px;
  padding: 10px;
  border: 1px solid var(--ct-chrome-border);
  border-radius: var(--ct-rounded-md);
  background-color: var(--ct-chrome-surface);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}

.page-thumbs__item:hover {
  border-color: var(--ct-chrome-border-strong);
}

.page-thumbs__item[data-active="true"] {
  border-color: var(--ct-primary);
  background-color: var(--ct-primary-subtle);
}

.page-thumbs__number {
  display: block;
  font-size: 11px;
  color: var(--ct-chrome-text-secondary);
}

.page-thumbs__text {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--ct-chrome-text);
  overflow-wrap: anywhere;
}

#canvas-scroller {
  /* 块级 + overflow，不用 flex：画布可能比视口宽（多页并排），
     块级布局里 margin: 0 auto 在溢出时会自动归零，内容从左边开始、溢出到右边；
     flex 的 justify-content 与 auto margin 在这种情况下都会把左边缘推到视口外。 */
  display: block;
  /* 吃掉工具栏与导入区之外的全部高度；内容超出时自己滚动 */
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  padding: 24px 16px 48px;
  overflow: auto;
  background-color: var(--ct-chrome-bg);
}

/* 缩放层：包住画布，是 preview-toggle.js 唯一写宽高的盒子（尺寸 = 缩放后的可视尺寸）。
   未缩放（桌面档）时它是 flex 容器：min-height: 100% 让「至少占满一屏」成立，
   #canvas-wrapper 靠 flex 拉伸跟着满高。
   注意 min-height 不能留在 #canvas-wrapper 上：百分比高度对 auto 高度的父元素解析为 0，
   留在原位反而会失效，还会和这里 JS 写的高度形成循环依赖（已知陷阱 #2）。 */
#canvas-scaler {
  position: relative;
  display: flex;
  min-height: 100%;
  /* 「点纸下方空白回焦」的宿主，原来挂在 #canvas-wrapper 上（已知陷阱 #3） */
  cursor: text;
  transform-origin: top left;
}

/* 缩放或 1:1 放大时，scaler 的尺寸由 JS 显式给定（可能是缩放后的值），
   wrapper 必须回到自己的内容高度、不能再被拉伸，否则会被压扁 */
#canvas-scaler[data-sized="true"] {
  display: block;
}

/* 多页编辑视图：内容按分页符分成多页，左右并排。纸的视觉从外层挪到每一列上，
   所以外层只负责撑开宽度与横向滚动，白纸由 #card 的背景按页画出来。 */
#canvas-wrapper {
  position: relative;
  /* 宽度由内容（多列并排的编辑器）决定，而不是被视口定宽 */
  width: max-content;
  /* flex 子项默认 flex-shrink: 1，多页比容器宽时会被压到容器宽，并排布局就没了 */
  flex: none;
  /* 水平位置由 pages.js 算：块级与 flex 的 auto margin 在内容溢出时都不可靠
     （Chrome 仍会居中，最左边那页滚不到），所以那边自己设 margin-left */
  margin: 0;
  /* transform 加在这一层，原点是左上：多页并排时以第一页左边缘对齐视口 */
  transform-origin: top left;
}

/* 编辑器不要整块描边：多页并排时框住一整片，看不出正在编辑哪一页。
   当前页的边框由 #page-focus 单独画。 */
.ql-editor:focus-visible {
  outline: none;
}

/* 当前光标所在那一页的描边。位置与尺寸由 pages.js 按光标落在哪一列算。
   用与全局焦点环同一个颜色，2px，保证一眼看得出正在编辑哪一页。 */
#page-focus {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  pointer-events: none;
  border-radius: 2px;
  box-shadow: 0 0 0 2px var(--ct-focus-ring);
  display: none;
}

#page-focus[data-visible="true"] {
  display: block;
}

/* 每页的第一块从新列开始。这个 class 由 pages.js 按导出规则（分页符 > 标题 > 块边界）
   打上，导出前会被 normalize.js 剥掉，不进产物。 */
.ql-editor > .changtu-page-start {
  break-before: column;
}

/* 页码层：压在纸上，不进导出结果。位置由 pages.js 按页数算。 */

/* 用 #canvas-wrapper > #card 提高特异性：canvas.css 里的 #card 把宽锁成 690（那份样式
   是给服务端导出用的），多页并排时编辑区要比 690 宽，得在编辑器这一侧覆盖掉它。 */
#canvas-wrapper > #card {
  /* 每页一张白纸，页缝里露出画布底色，所以底色要显式给（canvas.css 给的是纯白）。
     左右各 40 是首尾两页自己的页边距：列宽是 610（= 690 减左右各 40），
     列间距 112（= 32 的页间距加左右各 40），合起来正好一页 690 加页间距 32。 */
  width: max-content;
  max-width: none;
  padding: 36px 40px;
  background-color: var(--ct-chrome-bg);
  background-image: repeating-linear-gradient(
    to right,
    #ffffff 0 690px,
    transparent 690px 722px
  );
}

.preview-badge {
  position: fixed;
  left: 50%;
  bottom: 16px;
  z-index: 20;
  display: none;
  align-items: center;
  gap: 8px;
  transform: translateX(-50%);
  padding: 6px 12px;
  border: 1px solid var(--ct-chrome-border);
  border-radius: 9999px;
  background-color: var(--ct-chrome-surface);
  color: var(--ct-chrome-text-secondary);
  font-size: 12px;
}

.preview-badge[data-visible="true"] {
  display: inline-flex;
}

.preview-badge__button {
  border: 0;
  padding: 0;
  background: none;
  color: var(--ct-primary);
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
}

/* ------------------------------------------------------------------ 悬停浮窗 */

/* 顶栏「按字数分页」的问号说明。鼠标移上去就显示，不用点开，也不用管关。
   pointer-events: none 让鼠标穿过去，所以移开触发元素就自动收起。 */
.tooltip {
  position: fixed;
  z-index: 60;
  max-width: 380px;
  padding: 12px 14px;
  border: 1px solid var(--ct-chrome-border);
  border-radius: var(--ct-rounded-lg);
  background-color: var(--ct-chrome-surface);
  box-shadow: 0 8px 24px rgba(16, 20, 24, 0.16);
  pointer-events: none;
}

.tooltip[hidden] {
  display: none;
}

.tooltip__title {
  margin: 0 0 8px;
  color: var(--ct-chrome-text);
  font-size: 13px;
  font-weight: 600;
}

.tooltip__body p {
  margin: 0 0 10px;
  color: var(--ct-chrome-text-secondary);
  font-size: 12px;
  line-height: 1.75;
}

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

.tooltip__body strong {
  color: var(--ct-chrome-text);
  font-weight: 600;
}

.tooltip__body code {
  padding: 1px 4px;
  border-radius: var(--ct-rounded-sm);
  background-color: var(--ct-chrome-surface-sunken);
  color: var(--ct-chrome-text);
  font-family: var(--ct-mono-font);
  font-size: 11px;
}

/* ------------------------------------------------------------------ 拖拽遮罩 */

#dropzone {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--ct-scrim);
}

#dropzone[hidden] {
  display: none;
}

.dropzone__inner {
  padding: 24px 32px;
  border: 1px dashed rgba(255, 255, 255, 0.7);
  border-radius: var(--ct-rounded-lg);
  color: #ffffff;
  font-size: 13px;
  font-weight: 500;
  text-align: center;
}

.dropzone__inner--reject {
  border-color: #ffb4ad;
  color: #ffdad6;
}

.dropzone__title {
  display: block;
  margin-bottom: 4px;
  font-size: 16px;
}

.dropzone__note {
  display: block;
  color: rgba(255, 255, 255, 0.78);
  font-weight: 400;
}

/* ------------------------------------------------------------------ toast */

#toast-region {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-end;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 380px;
  padding: 10px 14px;
  border-radius: var(--ct-rounded-md);
  box-shadow: 0 8px 24px rgba(16, 20, 24, 0.12);
  color: #ffffff;
  font-size: 13px;
  pointer-events: auto;
}

.toast--info {
  background-color: var(--ct-chrome-text);
}

.toast--success {
  background-color: var(--ct-success);
}

.toast--error {
  background-color: var(--ct-error);
}

.toast__action {
  border: 0;
  padding: 0;
  background: none;
  color: inherit;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.toast__close {
  border: 0;
  padding: 2px;
  background: none;
  color: inherit;
  cursor: pointer;
}

/* ------------------------------------------------------------------ Quill 覆盖 */

/* Quill 自带的主题样式要压掉成画布样式，编辑区看到的就是出图结果 */
.ql-container.ql-snow {
  border: 0;
  font-family: inherit;
  font-size: inherit;
}

.ql-editor {
  position: relative;
  padding: 0;
  line-height: inherit;
  color: inherit;
  font-family: inherit;
  font-size: inherit;
  overflow-wrap: inherit;

  /* 多页编辑视图：内容先填满一列再流到右边一列，分页符强制换列。
     列宽 610（= 690 页宽减左右各 40 的页边距），列间距 112（= 32 的页间距加左右各 40）。
     宽度与高度由 pages.js 按页数和最高页重设，这里给的是单页时的值。 */
  column-width: 610px;
  column-gap: 112px;
  column-fill: auto;
  width: 610px;
  height: 1200px;
}

.ql-editor > hr[data-changtu-split="1"] {
  break-before: column;
}

/* 每页右上角的页码。绝对定位压在纸上，不进导出结果。 */
#page-labels {
  position: absolute;
  top: 0;
  left: 0;
  height: 0;
  pointer-events: none;
}

#page-labels span {
  position: absolute;
  top: 10px;
  font-size: 11px;
  line-height: 1;
  color: var(--ct-chrome-text-secondary);
  white-space: nowrap;
}

.ql-editor.ql-blank::before {
  left: 0;
  right: 0;
  color: var(--ct-canvas-placeholder);
  font-style: normal;
}

.ql-editor blockquote {
  border-left: 3px solid #d0d4d9;
  margin: 12px 0;
  padding-left: 16px;
}

.ql-editor hr {
  border: 0;
  border-top: 1px solid var(--ct-canvas-border);
}

/* ------------------------------------------------------------------ 焦点可见 */

:focus-visible {
  outline: 2px solid var(--ct-focus-ring);
  outline-offset: 2px;
  border-radius: var(--ct-rounded-sm);
}

.export-button:focus-visible,
.toolbar__button:focus-visible {
  outline-offset: 1px;
}

/* ------------------------------------------------------------------ 移动工具栏（窄屏或矮横屏） */

/* 19 个控件全排在主栏里，窄屏放不下：改成单行横滑，块结构与插入项收进右端的「更多」面板
   （D6 / 3.2 的分组原则）。矮横屏（844×390 的微信横屏）宽度够但高度只剩 390，同样按移动档
   处理，画布优先（第 6 节）。控件边长统一 44：直接在容器上改写 token，子控件跟着走 */
@media (max-width: 720px), (orientation: landscape) and (max-height: 520px) {
  /* 工具栏：单行横滑 + 右端固定「更多」（D6 / 3.2）。
     控件边长统一 44：直接在容器上改写 token，子控件跟着走 */
  #toolbar {
    --ct-control-height: var(--ct-touch-min);
    flex-wrap: nowrap;
    justify-content: flex-start;
    gap: 8px;
    min-height: var(--ct-toolbar-height-touch);
    padding: 0 12px;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scroll-snap-type: x proximity;
  }

  #toolbar .toolbar__group {
    scroll-snap-align: start;
  }

  #toolbar .toolbar__button,
  #toolbar .toolbar__select {
    height: var(--ct-control-height);
  }

  #toolbar .toolbar__group + .toolbar__group {
    padding-left: 8px;
  }

  /* 色块控件去掉外框：1px 边框会把内部 44 的有效边长吃掉 2px */
  #toolbar .toolbar__color-control {
    height: var(--ct-control-height);
    border: 0;
  }

  #toolbar .toolbar__color-pick {
    width: var(--ct-touch-min);
  }

  /* 块结构与插入项收进「更多」面板：主栏只留字符与段落的外观（3.2）。桌面档它们仍在
     主栏里——那个档位 #toolbar-more 是隐藏的，面板也打不开 */
  #toolbar .toolbar__block-tool,
  #toolbar .toolbar__group--block {
    display: none;
  }

  #toolbar-more {
    display: inline-flex;
    position: sticky;
    right: 0;
    flex: none;
    align-items: center;
    justify-content: center;
    width: var(--ct-touch-min);
    height: var(--ct-touch-min);
    padding: 0;
    border: 0;
    border-radius: var(--ct-rounded-md);
    background-color: var(--ct-chrome-surface);
    color: var(--ct-chrome-text-secondary);
    cursor: pointer;
    scroll-snap-align: end;
  }

  #toolbar-more:hover,
  #toolbar-more[aria-expanded="true"] {
    background-color: var(--ct-primary-subtle);
    color: var(--ct-primary);
  }

  /* 面板不在 #toolbar 里，拿不到那边改写的 --ct-control-height：行高下拉也得有 44（第 6 节） */
  #toolbar-more-panel .toolbar__select {
    height: var(--ct-touch-min);
  }
}

/* ------------------------------------------------------------------ 紧凑档（≤720） */

@media (max-width: 720px) {
  /* 顶栏：只留「产品名 + 设置 + 导出栏」（3.2） */
  #topbar {
    gap: 8px;
    padding: 0 12px;
  }

  #topbar-right {
    gap: 8px;
  }

  .product-note {
    display: none;
  }

  /* 触控最小边长 44：设置入口（L-06） */
  #settings-toggle {
    justify-content: center;
    width: var(--ct-touch-min);
    height: var(--ct-touch-min);
    padding: 0;
  }

  #settings-toggle {
    display: inline-flex;
  }

  /* 按字数分页从顶栏移出，改成设置按钮点开的浮层（3.2 / D5 之外的一处） */
  #topbar-settings {
    position: absolute;
    top: 100%;
    right: 12px;
    z-index: 46;
    display: none;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
    width: min(320px, calc(100vw - 24px));
    padding: 12px;
    border: 1px solid var(--ct-chrome-border);
    border-radius: var(--ct-rounded-lg);
    background-color: var(--ct-chrome-surface);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.16);
    animation: ct-sheet-in 140ms var(--ct-motion);
  }

  #topbar-settings[data-open="true"] {
    display: grid;
  }

  #topbar-settings .topbar-settings__label {
    font-size: 13px;
  }

  #topbar-settings .topbar-settings__help {
    justify-self: end;
    width: var(--ct-touch-min);
    height: var(--ct-touch-min);
  }

  #topbar-settings .topbar-settings__confirm {
    height: var(--ct-touch-min);
  }

  /* 外框改内阴影：1px 边框会把输入框与三角的有效高度吃到 42（触控最小边长 44） */
  #topbar-settings .topbar-settings__field {
    height: var(--ct-touch-min);
    border: 0;
    box-shadow: inset 0 0 0 1px var(--ct-chrome-border);
  }

  #topbar-settings .topbar-settings__field:focus-within {
    box-shadow: inset 0 0 0 1px var(--ct-chrome-border-strong);
  }

  #topbar-settings .topbar-settings__confirm {
    width: var(--ct-touch-min);
  }

  /* flex 子项默认会缩：输入框 100% 宽时三角会被压到 37，达不到 44 */
  #topbar-settings .topbar-settings__toggle {
    width: var(--ct-touch-min);
    flex: none;
  }

  /* 低于 16px 时 iOS Safari 在聚焦瞬间会自动放大页面（第 6 节） */
  #topbar-settings .topbar-settings__input {
    width: 100%;
    font-size: 16px;
  }

  #topbar-settings .topbar-settings__option {
    display: flex;
    align-items: center;
    min-height: var(--ct-touch-min);
    font-size: 13px;
  }

  .export-button {
    height: var(--ct-touch-min);
  }

  #import-hint {
    gap: 8px;
    min-height: var(--ct-touch-min);
    padding: 0 12px;
    font-size: 12px;
  }

  .import-hint__text,
  .import-hint__kbd {
    display: none;
  }

  .import-hint__button {
    height: var(--ct-touch-min);
  }

  /* 页面导航：左侧竖栏改成画布上方的横向 chip 条（3.2） */
  #stage-body {
    flex-direction: column;
  }

  #page-thumbs {
    display: flex;
    flex-direction: row;
    align-items: center;
    width: auto;
    height: var(--ct-chip-height);
    padding: 0 12px;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: 0;
    border-bottom: 1px solid var(--ct-chrome-border);
  }

  .page-thumbs__item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex: none;
    width: auto;
    height: 28px;
    margin: 0 8px 0 0;
    padding: 0 12px;
    white-space: nowrap;
    /* 去掉 300ms 点击延迟；不影响长按选择（第 6 节） */
    touch-action: manipulation;
  }

  .page-thumbs__number {
    display: inline;
  }

  .page-thumbs__text {
    display: inline;
    max-width: 12em;
    margin-top: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 11px;
  }

  #canvas-scroller {
    padding: 12px;
    /* 双指滚动与长按选择都不锁方向；不设 user-scalable=no */
    touch-action: pan-x pan-y;
  }

  /* 缩放徽标：动作按钮也要够 44（L-06） */
  .preview-badge__button {
    min-height: var(--ct-touch-min);
    padding: 0 4px;
  }

  /* 窄屏留给输入框与勾，说明浮窗撑满一行免得贴边被裁 */
  .tooltip {
    max-width: none;
    left: 12px !important;
    right: 12px;
  }

  .export-bar__readout {
    min-width: 0;
    font-size: 11px;
  }

  /* 现有规则把 note 隐藏了，这里恢复成单行可省略（3.2 的导出栏一行） */
  .export-bar__readout-note {
    display: block;
    max-width: 32vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* 键盘态：导入区 / chip 条 / 底栏让位，工具栏保留（D7） */
  body[data-keyboard="true"] #import-hint,
  body[data-keyboard="true"] #page-thumbs,
  body[data-keyboard="true"] #export-bar {
    display: none;
  }
}

/* ------------------------------------------------------------------ 手机档（≤480） */

@media (max-width: 480px) {
  /* 顶栏收成纯标题栏：44 + 安全区，唯一入口是设置（3.2） */
  #topbar {
    min-height: var(--ct-topbar-height-phone);
    padding-top: env(safe-area-inset-top);
    padding-right: 12px;
    padding-left: 12px;
  }

  .topbar__mark {
    overflow: hidden;
  }

  .product-mark {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  /* 导出栏钉到视口底部（D5：CSS 定位，DOM 不动） */
  #export-bar {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 30;
    height: var(--ct-actionbar-height);
    padding: 4px 12px;
    padding-bottom: calc(4px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--ct-chrome-border);
    background-color: var(--ct-chrome-surface);
  }

  /* DOM 顺序是「按钮在前、读数在后」，反排才能把读数放左边，焦点顺序也不变 */
  .export-bar__main {
    flex-direction: row-reverse;
    justify-content: flex-start;
    width: 100%;
  }

  .export-button {
    height: 48px;
    min-width: 104px;
    margin-left: auto;
    justify-content: center;
  }

  .export-bar__readout {
    text-align: left;
  }

  /* note 与错误行叠在动作栏上方，不占栏高 */
  .export-bar__readout-note,
  .export-bar__error {
    position: absolute;
    top: auto;
    right: 12px;
    bottom: 100%;
    left: 12px;
    max-width: none;
    margin: 0 0 4px;
    text-align: left;
  }

  /* 给固定在底部的导出栏让位，最后一页不会压在栏底下（L-04） */
  #canvas-scroller {
    padding-bottom: calc(64px + env(safe-area-inset-bottom));
  }

  /* chip 条再收一档，摘要让位给「第 N 页」 */
  #page-thumbs {
    height: var(--ct-chip-height-phone);
  }

  .page-thumbs__text {
    display: none;
  }

  /* 轻提示：底部居中、压在动作栏之上 */
  #toast-region {
    right: 12px;
    bottom: calc(64px + env(safe-area-inset-bottom) + 8px);
    left: 12px;
    align-items: stretch;
  }

  .toast {
    max-width: none;
  }

  .toast__action {
    min-height: var(--ct-touch-min);
  }

  .toast__close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: var(--ct-touch-min);
    min-height: var(--ct-touch-min);
  }

  .preview-badge {
    bottom: calc(64px + env(safe-area-inset-bottom) + 8px);
  }

  /* 按字数分页改成底部抽屉：480 以内浮层容易被顶栏挤到屏幕外 */
  #topbar-settings {
    position: fixed;
    inset: auto 0 0 0;
    top: auto;
    right: 0;
    z-index: 46;
    width: auto;
    max-height: 70vh;
    overflow-y: auto;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    border-radius: var(--ct-rounded-lg) var(--ct-rounded-lg) 0 0;
  }

  /* 「更多」面板同样压在动作栏之上 */
  #toolbar-more-panel {
    bottom: var(--ct-actionbar-height);
  }
}

/* ------------------------------------------------------------------ 极窄档（≤360）：保证顶栏单行 */

@media (max-width: 360px) {
  .product-mark {
    font-size: 13px;
  }

  #page-thumbs {
    height: 32px;
  }
}

/* ------------------------------------------------------------------ 横屏（矮视口）：画布优先 */

@media (orientation: landscape) and (max-height: 520px) {
  #topbar {
    min-height: 40px;
  }

  /* 外壳高度让给画布（L-09 要求顶栏 ≤41）：读数已收成一行，这一行文字也不能换行，
     装不下就用省略号 */
  .topbar__mark {
    min-width: 0;
    overflow: hidden;
  }

  .product-mark {
    flex: none;
    white-space: nowrap;
  }

  .product-note {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* 读数收成一行：不然顶栏会被三行读数顶到 48 以上 */
  .export-bar__readout {
    flex-direction: row;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
    overflow: hidden;
  }

  .export-bar__readout > span {
    white-space: nowrap;
  }

  .export-bar__readout-note {
    display: none;
  }

  #page-thumbs {
    display: none;
  }
}

@media (max-width: 480px) and (orientation: landscape) and (max-height: 520px) {
  #export-bar {
    height: 48px;
  }

  .export-button {
    height: var(--ct-touch-min);
  }
}

/* ------------------------------------------------------------------ 动效收敛 */

@media (prefers-reduced-motion: reduce) {
  .export-button__spinner {
    animation: none;
  }

  .export-bar__error {
    animation: none;
  }

  /* 面板与抽屉的开合动效走同一条规则（DESIGN.md 的 Motion） */
  #toolbar-more-panel,
  #topbar-settings {
    animation: none;
  }
}
