:root {
  --bg: #0f1115;
  --bg-2: #161920;
  --bg-3: #1d212a;
  --border: #2a2f3a;
  --border-soft: #232833;
  --text: #e6e9ef;
  --text-dim: #9aa3b2;
  --text-faint: #6b7484;
  --accent: #5b9cff;
  --accent-dark: #3d7de0;
  --success: #34c98a;
  --danger: #ff6b6b;
  --radius: 9px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  font-size: 14px;
  overflow: hidden;
}
button, input { font-family: inherit; }
button { cursor: pointer; }

.app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- 顶栏 ---------- */
.topbar {
  height: 62px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 18px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 190px; }
.brand-icon {
  display: grid; place-items: center;
  width: 34px; height: 34px;
  border-radius: 8px;
  background: linear-gradient(135deg, #5b9cff, #7b5cff);
  font-size: 11px; font-weight: 800; letter-spacing: .5px;
  color: #fff;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand-text strong { font-size: 14px; }
.brand-text small { font-size: 11px; color: var(--text-faint); }

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; }
.tool-group { display: flex; gap: 2px; padding: 3px; background: var(--bg-3); border-radius: var(--radius); }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }

.btn {
  border: 1px solid var(--border);
  background: var(--bg-3);
  color: var(--text);
  padding: 7px 13px;
  border-radius: 7px;
  font-size: 13px;
  white-space: nowrap;
  transition: .15s;
}
.btn:hover:not(:disabled) { border-color: #3c4454; background: #232833; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-success { background: #1c8f61; border-color: #1c8f61; color: #fff; font-weight: 600; }
.btn-success:hover:not(:disabled) { background: #167a52; border-color: #167a52; }
.btn.tool { border: none; background: transparent; padding: 6px 12px; color: var(--text-dim); }
.btn.tool:hover { background: #262c37; color: var(--text); }
.btn.tool.is-active { background: var(--accent); color: #fff; font-weight: 600; }

/* ---------- 主体三栏 ---------- */
.workspace { flex: 1; display: flex; min-height: 0; position: relative; }
.sidebar, .inspector {
  flex: none;
  background: var(--bg-2);
  overflow-y: auto;
  display: flex; flex-direction: column;
  transition: width .18s ease;
}
.sidebar { width: 190px; border-right: 1px solid var(--border); }
.inspector { width: 270px; border-left: 1px solid var(--border); }
.viewer { flex: 1; position: relative; min-width: 0; display: flex; flex-direction: column; background: #0b0d11; }

/* 侧栏折叠轨道：点击收起/展开，把空间让给 PDF 预览 */
.rail-toggle {
  flex: none; width: 16px; padding: 0; border: 0;
  border-right: 1px solid var(--border);
  background: var(--bg-3); color: var(--text-faint);
  font-size: 15px; line-height: 1;
  transition: color .15s, background .15s;
}
.rail-toggle:hover { color: var(--accent); background: #252b36; }
.rail-right { border-right: 0; border-left: 1px solid var(--border); }
/* 折叠后侧栏宽度归零，完全不占用预览区域 */
.app.hide-sidebar .sidebar,
.app.hide-inspector .inspector {
  width: 0 !important;
  min-width: 0;
  padding: 0;
  border-width: 0;
  overflow: hidden;
}
/* 移动端抽屉遮罩，桌面端始终隐藏 */
.side-scrim { display: none; }

.panel-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 14px 8px;
  font-size: 12px; font-weight: 700; letter-spacing: .5px;
  color: var(--text-dim);
}
.badge {
  background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 20px; padding: 1px 8px; font-size: 11px; color: var(--text-dim);
}
.panel-hint { margin: 0 14px 10px; font-size: 11px; color: var(--text-faint); line-height: 1.5; }
.sidebar-empty { padding: 30px 12px; text-align: center; color: var(--text-faint); font-size: 12px; }

/* 缩略图 */
.thumb-list { padding: 0 10px 16px; display: flex; flex-direction: column; gap: 9px; }
.thumb {
  position: relative;
  display: flex; gap: 7px; align-items: flex-start;
  padding: 7px 7px 7px 4px;
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  background: var(--bg-3);
  cursor: pointer;
  transition: .15s;
}
.thumb:hover { border-color: #3a4252; }
.thumb.is-selected { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(91,156,255,.35); }
.thumb-drag {
  flex: none; width: 16px; align-self: stretch;
  display: grid; place-items: center;
  color: var(--text-faint); font-size: 13px; cursor: grab;
}
.thumb-drag:active { cursor: grabbing; }
.thumb-body { flex: 1; min-width: 0; }
.thumb canvas {
  display: block; max-width: 100%;
  border-radius: 3px; background: #fff;
  box-shadow: 0 1px 5px rgba(0,0,0,.4);
}
.thumb-num { margin-top: 4px; font-size: 11px; color: var(--text-dim); }
.thumb-mark {
  position: absolute; right: 8px; top: 8px;
  background: rgba(91,156,255,.9); color: #fff;
  font-size: 10px; border-radius: 4px; padding: 1px 4px;
}
.sortable-ghost { opacity: .35; }
.sortable-chosen { background: #2b3240 !important; }

/* ---------- 主视图 ---------- */
.empty-state {
  margin: auto; text-align: center; color: var(--text-dim);
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 40px;
}
.empty-illustration { font-size: 64px; opacity: .8; }
.empty-state h2 { margin: 4px 0 0; font-size: 19px; color: var(--text); }
.empty-state p { margin: 0; font-size: 13px; }
.drop-zone-hint { margin-top: 10px; font-size: 11px; color: var(--text-faint); }

.stage-toolbar {
  height: 42px; flex: none;
  display: flex; align-items: center; gap: 8px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
  font-size: 12px; color: var(--text-dim);
}
.mini-btn {
  min-width: 26px; height: 24px; padding: 0 7px;
  background: var(--bg-3); border: 1px solid var(--border);
  color: var(--text); border-radius: 5px; font-size: 12px;
}
.mini-btn:hover { border-color: #455066; }
.stage-sep { width: 1px; height: 16px; background: var(--border); }
.stage-tip { margin-left: auto; color: var(--text-faint); }
/* 关键：page-stage 必须参与 flex 布局且允许收缩，否则 canvas-scroller 没有固定高度，overflow:auto 不会生效 */
.page-stage { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.canvas-scroller { flex: 1; min-height: 0; overflow: auto; padding: 24px; }
.page-wrap {
  position: relative; margin: 0 auto;
  background: #fff;
  box-shadow: 0 6px 26px rgba(0,0,0,.5);
}
.page-wrap canvas { display: block; }
/* PDF 原生文字层：覆盖在 canvas 上，支持框选复制 */
.text-layer {
  position: absolute; inset: 0;
  z-index: 1;
  overflow: hidden;
  line-height: 1;
  transform-origin: 0 0;
  -webkit-text-size-adjust: none; text-size-adjust: none;
}
.text-layer span, .text-layer br {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
}
.text-layer ::selection { background: rgba(91,156,255,.4); }
.text-layer .endOfContent {
  display: block; position: absolute;
  left: 100%; top: 100%; right: 0; bottom: 0;
  z-index: -1; cursor: default; user-select: none;
}
.overlay { position: absolute; inset: 0; z-index: 2; }
/* 选择模式：overlay 让出事件，允许在 PDF 文字上拖动框选；
   元素与表单热区仍可点击 */
.overlay.tool-select { pointer-events: none; }
.overlay.tool-select .ov-item,
.overlay.tool-select .form-hotspot { pointer-events: auto; }

/* 放置的元素 */
.ov-item {
  position: absolute;
  z-index: 6;
  border: 1px dashed transparent;
  border-radius: 2px;
  cursor: default;
  user-select: none;
}
.ov-item:hover { border-color: rgba(91,156,255,.55); }
.ov-item.is-selected { border-color: var(--accent); background: rgba(91,156,255,.06); }
.ov-item[data-type="text"] { white-space: pre-wrap; overflow: hidden; line-height: 1.25; }
.ov-item img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; display: block; }
.ov-del {
  position: absolute; top: -9px; right: -9px;
  width: 18px; height: 18px; border-radius: 50%;
  border: 1px solid #fff; background: var(--danger); color: #fff;
  font-size: 12px; line-height: 1; padding: 0;
  display: none; place-items: center;
}
.ov-item:hover .ov-del, .ov-item.is-selected .ov-del { display: grid; }
.ov-resize {
  position: absolute; right: -10px; bottom: -10px;
  width: 17px; height: 17px; border-radius: 50%;
  background: var(--accent); border: 2px solid #fff;
  color: #fff; font-size: 11px; line-height: 1;
  display: none; place-items: center;
  cursor: nwse-resize; z-index: 8;
}
.ov-item.is-selected .ov-resize { display: grid; }
.overlay.tool-text, .overlay.tool-sign { cursor: crosshair; }

/* 表单字段热区：直接在页面上点击填写 */
.form-hotspot {
  position: absolute; z-index: 4;
  box-sizing: border-box;
  /* 禁用 PDF 内嵌表单外观后，用淡边框标出可填写区域 */
  border: 1px solid rgba(60,90,140,.45);
  background: rgba(255,255,255,.75);
  border-radius: 2px;
  cursor: text;
}
.form-hotspot:hover { border-color: var(--accent); background: rgba(91,156,255,.12); }
.form-hotspot.fh-check, .form-hotspot.fh-choice { cursor: pointer; }
.form-hotspot.fh-check { background: rgba(255,255,255,.9); }
/* 复选框：页面内呈现勾选状态（canvas 已禁用内嵌表单外观，避免两个勾） */
.form-hotspot .fh-checkbox {
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%;
  box-sizing: border-box;
  font-size: 13px; line-height: 1; color: #14213d; font-weight: 700;
}
.form-hotspot .fh-checkbox.is-checked { background: rgba(91,156,255,.16); }
/* 文本框 / 下拉框：显示当前填写值 */
.form-hotspot .fh-value {
  display: block; box-sizing: border-box;
  width: 100%; height: 100%;
  padding: 1px 3px;
  font-family: inherit; font-size: 13px; line-height: 1.2;
  color: #111827; white-space: pre-wrap; word-break: break-all;
  overflow: hidden;
}
.form-inline-edit {
  position: absolute; z-index: 30; margin: 0;
  box-sizing: border-box; padding: 1px 3px;
  border: 2px solid var(--accent); border-radius: 2px;
  background: #fff; color: #111; outline: none;
  font-family: inherit; font-size: 14px;
}
.overlay.tool-text .ov-item, .overlay.tool-sign .ov-item { pointer-events: none; }
/* 删除/缩放手柄始终可操作，避免放置后还要切换工具才能调整 */
.overlay.tool-text .ov-item .ov-del,
.overlay.tool-sign .ov-item .ov-del,
.overlay.tool-text .ov-item .ov-resize,
.overlay.tool-sign .ov-item .ov-resize { pointer-events: auto; }
.ov-edit {
  position: absolute; z-index: 20; margin: 0; padding: 2px 4px;
  border: 1px solid var(--accent); border-radius: 3px;
  background: #fff; color: #111; resize: both; outline: none;
  font-family: inherit;
}

/* 拖拽 PDF 遮罩 */
.drop-mask {
  position: absolute; inset: 0; z-index: 40;
  display: grid; place-items: center;
  background: rgba(11,13,17,.86);
  border: 2px dashed var(--accent);
  pointer-events: none;
}
.drop-mask div { font-size: 20px; color: var(--accent); font-weight: 700; }

/* ---------- 右侧面板 ---------- */
.insp-section { padding-bottom: 14px; border-bottom: 1px solid var(--border-soft); }
.insp-section:last-child { border-bottom: none; }
.insp-section .panel-title { padding-top: 14px; }
.field-row { padding: 6px 14px; display: flex; flex-direction: column; gap: 5px; }
.field-row label { font-size: 12px; color: var(--text-dim); }
.inline-control { display: flex; align-items: center; gap: 9px; }
.inline-control input[type="range"] { flex: 1; accent-color: var(--accent); }
.inline-control span { min-width: 44px; text-align: right; font-size: 12px; color: var(--text); }
input[type="color"] { width: 100%; height: 30px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg-3); }
.tiny-hint { margin: 6px 14px 0; font-size: 11px; color: var(--text-faint); line-height: 1.55; }
.muted { color: var(--text-faint); font-size: 12px; line-height: 1.6; padding: 4px 14px; }

.sign-palette { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 14px; }
.sign-card {
  width: 100%; height: 64px;
  display: grid; place-items: center;
  border: 1px solid var(--border); border-radius: 7px;
  background: #fff; cursor: pointer; padding: 6px;
}
.sign-card:hover { border-color: #4a5a72; }
.sign-card.is-active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.sign-card img { max-width: 100%; max-height: 100%; object-fit: contain; }

.form-fields { padding: 2px 14px 6px; display: flex; flex-direction: column; gap: 9px; }
.form-item { display: flex; flex-direction: column; gap: 4px; }
.form-item label { font-size: 11px; color: var(--text-dim); word-break: break-all; }
.form-item input[type="text"], .form-item select {
  width: 100%; padding: 6px 8px; font-size: 12px;
  background: var(--bg-3); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
}
.form-item input[type="checkbox"] { accent-color: var(--accent); }
.form-check { flex-direction: row; align-items: center; gap: 8px; }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  background: rgba(0,0,0,.65);
}
.modal-box {
  width: min(760px, calc(100vw - 32px));
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 13px; box-shadow: var(--shadow); padding: 18px;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h3 { margin: 0 0 12px; font-size: 16px; }
.icon-btn { border: none; background: transparent; color: var(--text-dim); font-size: 22px; line-height: 1; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.tab {
  border: none; background: transparent; color: var(--text-dim);
  padding: 8px 14px; font-size: 13px; border-bottom: 2px solid transparent;
}
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tab-panel { min-height: 230px; }
.sign-canvas-wrap {
  position: relative; border: 1px dashed #3b4352; border-radius: 8px;
  background: #fff; overflow: hidden;
}
#signCanvas { display: block; width: 100%; height: 220px; touch-action: none; cursor: crosshair; }
.sign-baseline {
  position: absolute; left: 6%; right: 6%; bottom: 32px;
  border-bottom: 1px dashed #d6dbe3; pointer-events: none;
}
.modal-row { display: flex; align-items: center; gap: 18px; margin-top: 12px; font-size: 12px; color: var(--text-dim); }
.modal-row label { display: flex; align-items: center; gap: 6px; }
.modal-row input[type="color"] { width: 34px; height: 26px; }
.chk input { accent-color: var(--accent); }
.text-input {
  width: 100%; padding: 9px 11px; font-size: 14px;
  background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 7px; color: var(--text);
}
.type-preview {
  margin-top: 12px; height: 170px; border: 1px dashed #3b4352;
  border-radius: 8px; background: #fff; color: #9aa3b2;
  display: grid; place-items: center; font-size: 14px; overflow: hidden;
}
.type-preview img { max-width: 100%; max-height: 100%; }
.modal-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 16px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  z-index: 200; background: #222938; border: 1px solid var(--border);
  color: var(--text); padding: 10px 18px; border-radius: 8px;
  box-shadow: var(--shadow); font-size: 13px;
}
.toast.error { border-color: var(--danger); }

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #333a47; border-radius: 6px; }
::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 1050px) {
  .inspector { width: 220px; }
  .sidebar { width: 155px; }
  .brand-text small { display: none; }
}

/* ---------- 手机 / 小屏 ---------- */
@media (max-width: 760px) {
  .topbar {
    height: auto; min-height: 54px; flex-wrap: wrap;
    gap: 8px; padding: 8px 10px;
  }
  .brand { min-width: 0; flex: 1 1 100%; }
  .toolbar { width: 100%; flex-wrap: wrap; gap: 6px; }
  .toolbar .sep { display: none; }
  .btn { padding: 6px 9px; font-size: 12px; }
  .btn.tool { padding: 6px 9px; }
  .tool-group { padding: 2px; }

  /* 侧栏改为覆盖式抽屉，展开时不挤压预览区 */
  .sidebar, .inspector {
    position: absolute; top: 0; bottom: 0; z-index: 60;
    width: min(82vw, 320px);
    box-shadow: var(--shadow);
    transition: transform .2s ease;
  }
  .sidebar {
    left: 0; transform: translateX(-100%);
    border-right: 1px solid var(--border) !important;
  }
  .inspector {
    right: 0; transform: translateX(100%);
    border-left: 1px solid var(--border) !important;
  }
  .app:not(.hide-sidebar) .sidebar,
  .app:not(.hide-inspector) .inspector { transform: translateX(0); }
  .rail-toggle { position: absolute; top: 0; bottom: 0; z-index: 70; height: 100%; }
  .rail-left { left: 0; }
  .rail-right { right: 0; }
  .side-scrim {
    display: block; position: absolute; inset: 0; z-index: 55;
    background: rgba(0, 0, 0, .5);
  }

  .canvas-scroller { padding: 10px; }
  .stage-toolbar { padding: 0 8px; gap: 6px; }
  .stage-tip, .stage-sep { display: none; }
  .page-wrap { box-shadow: 0 4px 16px rgba(0, 0, 0, .45); }

  .modal-box { padding: 12px; width: calc(100vw - 16px); }
  .modal-row { flex-wrap: wrap; gap: 10px; }
  #signCanvas { height: 170px; }
  .tab { padding: 8px 10px; font-size: 12px; }
  .tab-panel { min-height: 190px; }
  .type-preview { height: 140px; }
  .toast { max-width: calc(100vw - 32px); text-align: center; }
}
