/* Reset */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;
  font-size: 13.5px;
  color: #1f2430;
  background: #eef1f6;
  overflow: hidden;
  --bg: #eef1f6;
  --panel: #ffffff;
  --panel-2: #f6f8fc;
  --panel-3: #eceff6;
  --border: #dde3ee;
  --border-strong: #c8d2e3;
  --text: #1f2430;
  --muted: #667085;
  --accent: #2f6df6;
  --accent-weak: #e8effe;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
  --mono: "SF Mono", "JetBrains Mono", Menlo, Consolas, "Courier New", monospace;
}
body.dark {
  --bg: #12151c;
  --panel: #1a1e27;
  --panel-2: #21262f;
  --panel-3: #262c37;
  --border: #2e3542;
  --border-strong: #3b4453;
  --text: #e6e9ef;
  --muted: #98a2b3;
  --accent-weak: #1e2a44;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
  background: var(--bg);
  color: var(--text);
}
button, input, select, textarea { font: inherit; color: inherit; }

/* ─────────────── 顶栏 ─────────────── */
.topbar {
  height: 52px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 30;
}
body.dark .topbar { background: #171b23; }
.brand { display: flex; align-items: center; gap: 9px; flex: none; }
.logo {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, #2f6df6, #6f8ede);
  color: #fff; font-weight: 800; font-size: 12.5px;
  display: grid; place-items: center; letter-spacing: .5px;
  box-shadow: 0 2px 6px rgba(47, 109, 246, .35);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-size: 13.5px; letter-spacing: .2px; }
.brand-text small { color: var(--muted); font-size: 11px; }

.topbar-group { display: flex; align-items: center; gap: 8px; }
.topbar-group:last-child { margin-left: auto; }

.file-select { display: flex; align-items: center; gap: 6px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 3px 4px 3px 9px; }
.file-select .lbl { font-size: 11px; color: var(--muted); }
.file-select select { border: none; background: transparent; outline: none; font-size: 12.5px; max-width: 190px; cursor: pointer; }
body.dark .file-select select option { background: #1a1e27; }

.dirty-dot { width: 8px; height: 8px; border-radius: 50%; background: #f59e0b; box-shadow: 0 0 0 3px rgba(245, 158, 11, .18); }

.user-badge, .quota-badge {
  font-size: 11.5px; color: var(--muted); white-space: nowrap;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 10px;
}
.user-badge:empty, .quota-badge:empty { display: none; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.view-switch { background: var(--panel-2); border: 1px solid var(--border); border-radius: 9px; padding: 3px; gap: 2px; }
.vbtn {
  border: none; background: transparent; cursor: pointer;
  padding: 4px 12px; border-radius: 6px; font-size: 12.5px; color: var(--muted);
}
.vbtn:hover { background: var(--panel-3); color: var(--text); }
.vbtn.active { background: var(--accent); color: #fff; box-shadow: 0 1px 4px rgba(47, 109, 246, .35); }

.btn {
  border: 1px solid var(--border-strong); background: var(--panel);
  padding: 6px 12px; border-radius: 8px; cursor: pointer; font-size: 12.5px;
  transition: background .12s, border-color .12s, transform .06s;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #245fe0; }
.btn.ghost { border-color: transparent; background: transparent; }
#btnTheme { padding: 6px 9px; }

/* ─────────────── 布局 ─────────────── */
.shell { display: flex; height: calc(100% - 52px); min-height: 0; }

.sidebar {
  width: 296px; flex: none;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
body.dark .sidebar { background: #171b23; }
.side-tabs { display: flex; gap: 2px; padding: 8px 8px 0; }
.sbtn {
  flex: 1; border: none; background: transparent; cursor: pointer;
  padding: 7px 4px; font-size: 12.5px; color: var(--muted);
  border-radius: 7px 7px 0 0;
}
.sbtn:hover { color: var(--text); background: var(--panel-2); }
.sbtn.active { color: var(--accent); background: var(--panel-2); font-weight: 600; box-shadow: inset 0 -2px 0 var(--accent); }
.side-pane { display: none; overflow-y: auto; padding: 12px; flex: 1; min-height: 0; }
.side-pane.active { display: block; }
.pane-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: .1em;
  color: var(--muted); margin: 14px 0 8px; font-weight: 700;
  display: flex; justify-content: space-between; align-items: baseline;
}
.pane-title:first-child { margin-top: 2px; }
.pane-title small { text-transform: none; letter-spacing: 0; font-weight: 500; }

.insert-grid { display: flex; flex-direction: column; gap: 6px; }
.ins {
  text-align: left; border: 1px dashed var(--border-strong); background: var(--panel-2);
  padding: 7px 10px; border-radius: 8px; cursor: pointer; font-size: 12.5px; transition: .12s;
}
.ins:hover { border-color: var(--accent); background: var(--accent-weak); color: var(--accent); }
.ins.toggle-row { display: flex; align-items: center; gap: 7px; cursor: pointer; }

.outline { display: flex; flex-direction: column; gap: 2px; }
.outline-item {
  display: flex; align-items: center; gap: 7px; padding: 5px 8px; border-radius: 6px;
  cursor: pointer; font-size: 12.5px; border: 1px solid transparent;
}
.outline-item:hover { background: var(--panel-2); border-color: var(--border); }
.outline-item .kind {
  font-size: 9.5px; padding: 1px 5px; border-radius: 4px; background: var(--accent-weak);
  color: var(--accent); font-weight: 700; flex: none; text-transform: uppercase;
}
.outline-sub { font-size: 11.5px; color: var(--muted); padding: 2px 8px 2px 26px; cursor: pointer; border-radius: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.outline-sub:hover { background: var(--panel-2); color: var(--accent); }
.outline-sub.hidden { display: none; }

.stats { font-size: 12px; color: var(--muted); line-height: 1.9; }
.stats b { color: var(--text); font-weight: 600; }

/* 表单 */
.form { display: flex; flex-direction: column; gap: 9px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.form input[type="text"], .form textarea, .form select, .contact-row input, .contact-row select {
  border: 1px solid var(--border); background: var(--panel-2); border-radius: 7px;
  padding: 6px 9px; outline: none; font-size: 12.5px; width: 100%; color: var(--text);
}
.form input:focus, .form textarea:focus, .form select:focus, .contact-row input:focus { border-color: var(--accent); background: var(--panel); }
.form textarea { resize: vertical; font-family: inherit; }
.color-row { display: flex; align-items: center; gap: 10px; }
input[type="color"] { width: 40px; height: 26px; border: 1px solid var(--border); border-radius: 6px; background: none; padding: 1px; cursor: pointer; }
.swatches { display: flex; gap: 5px; }
.swatches i { width: 16px; height: 16px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; }
.swatches i:hover { border-color: var(--text); }

.contact-list { display: flex; flex-direction: column; gap: 6px; }
.contact-row { display: grid; grid-template-columns: 74px 1fr 22px; gap: 5px; align-items: center; }
.contact-row .del { border: none; background: transparent; cursor: pointer; color: var(--muted); font-size: 15px; border-radius: 5px; }
.contact-row .del:hover { color: #dc2626; background: #fee2e2; }
body.dark .contact-row .del:hover { background: #40181a; }

.hint { font-size: 11.5px; color: var(--muted); margin-top: 8px; line-height: 1.6; }

/* 图片库 */
.drop-zone {
  border: 1.5px dashed var(--border-strong); border-radius: 10px; padding: 18px 10px;
  text-align: center; font-size: 12px; color: var(--muted); margin-bottom: 8px;
  background: var(--panel-2); transition: .15s;
}
.drop-zone.over { border-color: var(--accent); background: var(--accent-weak); color: var(--accent); }
.asset-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.asset {
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--panel-2);
  display: flex; flex-direction: column;
}
.asset img { width: 100%; height: 66px; object-fit: cover; background:
  linear-gradient(45deg, #e9edf5 25%, transparent 25%, transparent 75%, #e9edf5 75%) 0 0/12px 12px,
  linear-gradient(45deg, #e9edf5 25%, transparent 25%, transparent 75%, #e9edf5 75%) 6px 6px/12px 12px; }
.asset .meta { padding: 4px 6px; font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.asset .acts { display: flex; border-top: 1px solid var(--border); }
.asset .acts button {
  flex: 1; border: none; background: transparent; cursor: pointer; font-size: 11px; padding: 4px 0; color: var(--muted);
}
.asset .acts button:hover { background: var(--accent-weak); color: var(--accent); }
.asset .acts button + button { border-left: 1px solid var(--border); }

/* 帮助 */
.help { font-size: 12.5px; line-height: 1.75; color: var(--text); }
.help p { margin: 12px 0 6px; color: var(--muted); }
.help pre {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 11px; font-family: var(--mono); font-size: 11.5px; line-height: 1.65;
  overflow-x: auto; white-space: pre; margin: 0;
}
.help code { font-family: var(--mono); font-size: .92em; background: var(--panel-3); padding: 1px 4px; border-radius: 4px; }
.help-list { margin: 6px 0 0; padding-left: 18px; color: var(--muted); }
.help-list li { margin: 3px 0; line-height: 1.7; }
.help-list b { color: var(--text); }
.kbd-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.kbd-table td { padding: 4px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
.kbd-table td:first-child { color: var(--accent); font-family: var(--mono); font-size: 11.5px; white-space: nowrap; }

/* ─────────────── 工作区 ─────────────── */
.workspace { flex: 1; display: flex; min-width: 0; min-height: 0; }
.pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--panel); }
.pane-editor { flex: 0 0 46%; }
.pane-preview { flex: 1; background: var(--bg); }
.pane-head {
  height: 33px; flex: none; display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; border-bottom: 1px solid var(--border); background: var(--panel);
  font-size: 11.5px; color: var(--muted);
}
.pane-label { font-weight: 600; letter-spacing: .03em; }
.pane-tools { display: flex; align-items: center; gap: 4px; }
.mini {
  border: 1px solid var(--border); background: var(--panel-2); border-radius: 6px;
  width: 24px; height: 20px; cursor: pointer; font-size: 12px; line-height: 1; color: var(--muted);
}
.mini:hover { border-color: var(--accent); color: var(--accent); }
#btnFit { width: auto; padding: 0 8px; font-size: 11px; }
.zoom-label { font-size: 11px; min-width: 36px; text-align: center; font-variant-numeric: tabular-nums; }

.editor-wrap { flex: 1; display: flex; min-height: 0; position: relative; background: var(--panel); }
.gutter {
  width: 48px; flex: none; overflow: hidden; text-align: right;
  padding: 14px 8px 14px 0; font-family: var(--mono); font-size: 12.5px; line-height: 1.65;
  color: #a9b3c6; background: var(--panel-2); border-right: 1px solid var(--border);
  user-select: none; white-space: pre;
}
body.dark .gutter { color: #566072; }
#editor {
  flex: 1; border: none; outline: none; resize: none; background: transparent;
  padding: 14px 16px; font-family: var(--mono); font-size: 12.5px; line-height: 1.65;
  color: var(--text); tab-size: 2; white-space: pre; overflow: auto;
}
#editor::placeholder { color: var(--muted); }
#editor.drop-over { background: var(--accent-weak); }

.splitter { width: 5px; flex: none; cursor: col-resize; background: var(--border); position: relative; }
.splitter:hover, .splitter.dragging { background: var(--accent); }
.splitter::after { content: ""; position: absolute; inset: 0 -4px; }

.preview-scroll { flex: 1; overflow: auto; padding: 22px; min-height: 0; }
.preview-stage { display: flex; justify-content: center; transform-origin: top center; width: max-content; min-width: 100%; }
#previewPaper {
  width: 210mm; min-height: 297mm; background: #fff; box-shadow: var(--shadow);
  padding: 18mm; border-radius: 3px; box-sizing: border-box;
  /* 纸张是「文档」而非弹性项：不允许被压缩，宽度不够时交给滚动容器 */
  flex: none;
}
#previewPaper[data-paper="letter"] { width: 8.5in; min-height: 11in; }
#previewPaper[data-paper="a5"] { width: 148mm; min-height: 210mm; padding: 13mm; }
/* 超长页：宽度保持纸张宽度，高度随内容增长（不用滚动条裁切）
   align-self:flex-start 很关键 —— 否则作为弹性项会被拉伸到滚动区高度 */
#previewPaper[data-page-mode="long"] { min-height: 0; height: auto; align-self: flex-start; }
#previewPaper[data-page-mode="long"][data-paper="letter"] { width: 8.5in; }
#previewPaper[data-page-mode="long"][data-paper="a5"] { width: 148mm; }
#previewPaper .r-root { background: transparent; }

/* 视图模式 */
body[data-view="source"] .pane-preview, body[data-view="source"] .splitter { display: none; }
body[data-view="source"] .pane-editor { flex: 1 1 auto; }
body[data-view="preview"] .pane-editor, body[data-view="preview"] .splitter { display: none; }
body[data-view="preview"] .pane-preview { flex: 1 1 auto; }

/* 预览交互 */
#previewRoot [data-line]:hover { background: rgba(47, 109, 246, .055); box-shadow: 0 0 0 6px rgba(47, 109, 246, .055); border-radius: 3px; }
#previewRoot .r-entry, #previewRoot .r-section { position: relative; }
.entry-tools {
  position: absolute; top: 0; right: 0;
  display: none; gap: 3px; padding: 2px; background: #2f6df6; border-radius: 0 6px 6px 6px;
  box-shadow: 0 3px 10px rgba(47, 109, 246, .4); z-index: 5;
}
#previewRoot .r-entry:hover > .entry-tools { display: flex; }
.entry-tools button {
  border: none; background: rgba(255, 255, 255, .16); color: #fff; cursor: pointer;
  font-size: 10.5px; padding: 2px 7px; border-radius: 4px; white-space: nowrap;
}
.entry-tools button:hover { background: #fff; color: #2f6df6; }
.img-tools {
  position: absolute; display: none; gap: 3px; padding: 2px; background: #111827; border-radius: 6px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, .35); z-index: 6;
}
.img-wrap { position: relative; display: inline-block; }
.img-wrap:hover .img-tools { display: flex; }
.img-tools button { border: none; background: rgba(255, 255, 255, .16); color: #fff; cursor: pointer; font-size: 10.5px; padding: 2px 7px; border-radius: 4px; }
.img-tools button:hover { background: #fff; color: #111827; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 20px);
  background: #111827; color: #fff; padding: 9px 18px; border-radius: 10px;
  font-size: 12.5px; opacity: 0; pointer-events: none; transition: .22s; z-index: 200;
  box-shadow: 0 8px 26px rgba(0, 0, 0, .3); max-width: 70vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: #b91c1c; }
.toast.ok { background: #047857; }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(15, 20, 32, .5); backdrop-filter: blur(2px); display: grid; place-items: center; z-index: 150; }
.modal[hidden] { display: none; }
.modal-card {
  width: min(520px, 92vw); background: var(--panel); border-radius: 14px; padding: 18px 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .35); border: 1px solid var(--border);
}
.modal-card h3 { margin: 0 0 12px; font-size: 15px; }
.modal-body { display: flex; flex-direction: column; gap: 10px; font-size: 12.5px; }
.modal-body label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
.modal-body input, .modal-body select, .modal-body textarea {
  border: 1px solid var(--border); background: var(--panel-2); border-radius: 7px;
  padding: 7px 10px; outline: none; font-size: 12.5px; color: var(--text); width: 100%;
}
.modal-body input:focus { border-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* 滚动条 */
::-webkit-scrollbar { width: 11px; height: 11px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #c4cddd; border-radius: 6px; border: 3px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: #9aa7bd; background-clip: content-box; border: 3px solid transparent; }
body.dark ::-webkit-scrollbar-thumb { background: #39424f; background-clip: content-box; border: 3px solid transparent; }

@media (max-width: 1000px) {
  .sidebar { width: 250px; }
  .brand-text { display: none; }
  .pane-editor { flex: 0 0 50%; }
}
