:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1f2329;
  --muted: #646a73;
  --border: #e3e5e8;
  --primary: #1f5eff;
  --primary-hover: #1149d8;
  --primary-soft: #eaf0ff;
  --success: #12a150;
  --success-soft: #e6f6ec;
  --warning: #d97706;
  --warning-soft: #fdf3e2;
  --danger: #e03131;
  --danger-soft: #fdeaea;
  --gray-soft: #f0f1f3;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 2px 8px rgba(0, 0, 0, .04);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; line-height: 1.3; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; word-break: break-all; }
[hidden] { display: none !important; }

/* 布局 */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 24px;
  height: 56px; padding: 0 24px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 6px;
  background: var(--primary); color: #fff; font-size: 15px;
}
.nav { display: flex; gap: 4px; flex: 1; }
.nav a { padding: 6px 12px; border-radius: 6px; color: var(--muted); }
.nav a:hover { background: var(--gray-soft); text-decoration: none; }
.nav a.active { color: var(--primary); background: var(--primary-soft); font-weight: 500; }
.user-menu { display: flex; align-items: center; gap: 12px; color: var(--muted); }
.container { max-width: 1080px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 4px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 20px; border-bottom: 1px solid var(--border); }
.card-body { padding: 20px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* 表单 */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field > label, .label { font-weight: 500; }
.field .hint { color: var(--muted); font-size: 12px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search], textarea, select {
  width: 100%; padding: 8px 12px; font: inherit; color: inherit;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea { resize: vertical; min-height: 72px; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.check input { width: 16px; height: 16px; accent-color: var(--primary); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 16px; font: inherit; font-weight: 500; line-height: 1.5;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { border-color: #c9ccd1; background: #fafafa; text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { color: #fff; background: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { color: var(--danger); border-color: #f3c2c2; }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-text { border-color: transparent; background: transparent; color: var(--primary); padding: 4px 8px; }
.btn-text:hover { background: var(--primary-soft); border-color: transparent; }
.btn-sm { padding: 3px 10px; font-size: 13px; }
.btn-lg { padding: 10px 24px; font-size: 15px; }
.btn-block { width: 100%; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* 表格 */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-weight: 500; color: var(--muted); background: #fafbfc; font-size: 13px; white-space: nowrap; }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafbfc; }
.empty { padding: 48px 16px; text-align: center; color: var(--muted); }

/* 标签 */
.tag { display: inline-flex; align-items: center; padding: 1px 8px; border-radius: 10px; font-size: 12px; font-weight: 500; white-space: nowrap; }
.tag-signing, .tag-pending { color: var(--primary); background: var(--primary-soft); }
.tag-completed, .tag-signed { color: var(--success); background: var(--success-soft); }
.tag-rejected { color: var(--danger); background: var(--danger-soft); }
.tag-revoked, .tag-expired { color: var(--muted); background: var(--gray-soft); }
.tag-waiting { color: var(--warning); background: var(--warning-soft); }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button { padding: 5px 12px; font: inherit; color: var(--muted); background: none; border: none; border-radius: 6px; cursor: pointer; }
.tabs button:hover { background: var(--gray-soft); }
.tabs button.active { color: var(--primary); background: var(--primary-soft); font-weight: 500; }

/* 详情 */
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 10px 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; }
.signer-row { display: flex; gap: 16px; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.signer-row:last-child { border-bottom: none; }
.signer-idx { flex: none; display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--gray-soft); color: var(--muted); font-weight: 600; font-size: 13px; }
.signer-main { flex: 1; min-width: 0; }
.signer-name { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-weight: 500; }
.signer-meta { color: var(--muted); font-size: 13px; margin-top: 2px; }
.link-box { display: flex; gap: 8px; align-items: center; margin-top: 8px; padding: 8px 10px; background: #fafbfc; border: 1px dashed var(--border); border-radius: 6px; flex-wrap: wrap; }
.link-box code { flex: 1; min-width: 200px; }
.code-pill { font-family: ui-monospace, Menlo, monospace; font-weight: 600; letter-spacing: 2px; color: var(--text); background: var(--warning-soft); padding: 0 6px; border-radius: 4px; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 16px 20px; }
.timeline li::before { content: ""; position: absolute; left: 4px; top: 7px; width: 7px; height: 7px; border-radius: 50%; background: var(--primary); }
.timeline li::after { content: ""; position: absolute; left: 7px; top: 18px; bottom: 0; width: 1px; background: var(--border); }
.timeline li:last-child::after { display: none; }
.timeline .t { color: var(--muted); font-size: 12px; }
.timeline .d { color: var(--muted); font-size: 13px; word-break: break-all; }

/* 签署方编辑 */
.signer-edit { display: grid; grid-template-columns: 28px 1fr 1fr 1fr 32px; gap: 8px; align-items: center; margin-bottom: 8px; }
.icon-btn { display: grid; place-items: center; width: 32px; height: 32px; padding: 0; font-size: 18px; color: var(--muted); background: none; border: none; border-radius: 6px; cursor: pointer; }
.icon-btn:hover { background: var(--danger-soft); color: var(--danger); }

.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  padding: 28px 16px; border: 2px dashed var(--border); border-radius: var(--radius);
  color: var(--muted); text-align: center; cursor: pointer; transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.over { border-color: var(--primary); background: var(--primary-soft); }
.dropzone strong { color: var(--text); }
.dropzone.busy { pointer-events: none; opacity: .6; }

/* 登录 */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 380px; }
.auth-card .brand { justify-content: center; margin-bottom: 24px; font-size: 20px; }
.auth-card .brand-mark { width: 36px; height: 36px; font-size: 18px; border-radius: 8px; }

/* 提示 */
.notice { padding: 10px 14px; border-radius: 6px; background: var(--primary-soft); color: #173a99; }
.notice-warn { background: var(--warning-soft); color: #8a4b00; }
.notice-ok { background: var(--success-soft); color: #0b6b35; }
.notice-err { background: var(--danger-soft); color: #a51d1d; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mt { margin-top: 16px; }
.mb { margin-bottom: 16px; }

#toast { position: fixed; left: 50%; top: 20px; z-index: 1000; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { padding: 10px 18px; border-radius: 6px; color: #fff; background: rgba(31, 35, 41, .92); box-shadow: 0 4px 16px rgba(0, 0, 0, .15); animation: toast-in .2s ease; max-width: 90vw; }
.toast.err { background: rgba(224, 49, 49, .95); }
.toast.ok { background: rgba(18, 161, 80, .95); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }

/* 弹窗 */
.modal-mask { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 16px; background: rgba(0, 0, 0, .45); }
.modal { width: 100%; max-width: 440px; max-height: 90vh; overflow: auto; background: var(--surface); border-radius: 10px; box-shadow: 0 12px 40px rgba(0, 0, 0, .2); }
.modal-head { padding: 16px 20px 0; font-weight: 600; font-size: 16px; }
.modal-body { padding: 16px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 0 20px 16px; }

@media (max-width: 760px) {
  .topbar { padding: 0 16px; gap: 12px; }
  .nav { overflow-x: auto; }
  .nav a { padding: 6px 8px; }
  .user-menu .who { display: none; }
  .container { padding: 16px; }
  .grid-2 { grid-template-columns: 1fr; }
  .signer-edit { grid-template-columns: 1fr 32px; }
  .signer-edit .signer-idx { display: none; }
  .signer-edit input { grid-column: 1; }
  .signer-edit .icon-btn { grid-column: 2; grid-row: 1; }
  .kv { grid-template-columns: 84px 1fr; }
  th, td { padding: 10px 12px; }
  .hide-sm { display: none; }
}

/* PDF 预览与签署位置 */
.pdf-pages { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.pdf-page { position: relative; width: 100%; max-width: 860px; background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .12); container-type: inline-size; }
.pdf-page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.pdf-overlay { position: absolute; inset: 0; }
.pdf-page-no { position: absolute; right: 8px; bottom: 6px; font-size: 11px; color: #999; pointer-events: none; }
.placing .pdf-overlay { cursor: crosshair; }

.sfield {
  position: absolute; display: flex; align-items: center; justify-content: center;
  border: 1.5px dashed var(--fc, var(--primary)); background: color-mix(in srgb, var(--fc, var(--primary)) 10%, transparent);
  border-radius: 3px; font-size: clamp(8px, 1.7cqw, 12px); color: var(--fc, var(--primary)); user-select: none; overflow: visible;
  line-height: 1.2; text-align: center;
}
.sfield img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.sfield .sfield-label { pointer-events: none; padding: 0 2px; overflow: hidden; max-height: 100%; }
.sfield.editable { cursor: move; }
.sfield.dragging { opacity: .8; }
.sfield-resize { position: absolute; right: -6px; bottom: -6px; width: 12px; height: 12px; background: var(--fc, var(--primary)); border: 2px solid #fff; border-radius: 50%; cursor: nwse-resize; }
.sfield-del {
  position: absolute; top: -10px; right: -10px; display: grid; place-items: center; width: 20px; height: 20px; padding: 0;
  font-size: 14px; line-height: 1; color: #fff; background: var(--danger); border: 2px solid #fff; border-radius: 50%; cursor: pointer;
}
.sfield.riding { overflow: hidden; border-right: none; border-radius: 3px 0 0 3px; cursor: ns-resize; touch-action: none; }
.sfield.riding img { position: absolute; top: 0; max-width: none; height: 100%; object-fit: fill; }
.sfield.riding .sfield-del { top: 2px; right: 2px; }
.sfield.sign-target { cursor: pointer; animation: pulse 1.6s ease-in-out infinite; font-weight: 500; }
.sfield.sign-target.filled { animation: none; border-style: solid; background: transparent; border-color: color-mix(in srgb, var(--success) 60%, transparent); }
.sfield.date-text { border-style: dotted; color: #000; font-family: Helvetica, Arial, sans-serif; justify-content: flex-start; white-space: nowrap; }
.sfield.date-text .sfield-label { padding: 0; overflow: visible; }
@keyframes pulse { 50% { background: color-mix(in srgb, var(--fc, var(--primary)) 22%, transparent); } }

/* 签署位置编辑器 */
.editor { display: grid; grid-template-columns: 1fr 280px; gap: 20px; align-items: start; }
.editor-side { position: sticky; top: 72px; }
.editor-doc { min-width: 0; padding: 16px; background: #e9ebef; border-radius: var(--radius); }
.party { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.party:last-child { border-bottom: none; }
.party-name { display: flex; align-items: center; gap: 8px; font-weight: 500; margin-bottom: 8px; }
.party-name .nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.party-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--fc); flex: none; }
.party .btn.armed { color: #fff; background: var(--fc); border-color: var(--fc); }
.party .count { margin-left: auto; flex: none; color: var(--muted); font-size: 12px; font-weight: 400; }

/* 签署身份 */
.identity-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.identity-card { display: flex; flex-direction: column; }
.identity-img { display: grid; grid-template: minmax(0, 1fr) / minmax(0, 1fr); place-items: center; height: 140px; padding: 12px; background: repeating-conic-gradient(#f3f4f6 0 25%, #fff 0 50%) 0 0 / 16px 16px; border-bottom: 1px solid var(--border); border-radius: var(--radius) var(--radius) 0 0; }
.identity-img img { max-width: 100%; max-height: 100%; object-fit: contain; }
.identity-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; }
.seg { display: inline-flex; padding: 2px; background: var(--gray-soft); border-radius: 6px; }
.seg label { padding: 4px 14px; border-radius: 5px; cursor: pointer; color: var(--muted); }
.seg input { display: none; }
.seg label:has(input:checked) { background: var(--surface); color: var(--text); font-weight: 500; box-shadow: var(--shadow); }
.img-preview { display: grid; place-items: center; min-height: 120px; padding: 12px; border: 1px dashed var(--border); border-radius: 6px; background: repeating-conic-gradient(#f3f4f6 0 25%, #fff 0 50%) 0 0 / 16px 16px; }
.img-preview img { max-width: 100%; max-height: 160px; }
.modal:has(.seal-gen) { max-width: 680px; }
.seal-gen { display: grid; grid-template-columns: 1fr 240px; gap: 20px; align-items: start; margin-bottom: 12px; }
.seal-gen .field { margin-bottom: 12px; }
.seal-preview { position: sticky; top: 0; display: grid; place-items: center; aspect-ratio: 1; padding: 12px; border: 1px dashed var(--border); border-radius: 6px; background: repeating-conic-gradient(#f3f4f6 0 25%, #fff 0 50%) 0 0 / 16px 16px; }
.seal-preview canvas { width: 100%; height: auto; }
@media (max-width: 600px) { .seal-gen { grid-template-columns: 1fr; } .seal-preview { position: static; order: -1; width: 200px; justify-self: center; } }

/* 签名板 */
.sigpad { max-width: 640px; }
.sigpad-box { position: relative; height: 240px; border: 1px solid var(--border); border-radius: 6px; background: #fcfcfd; touch-action: none; }
.sigpad-box::after { content: ""; position: absolute; left: 24px; right: 24px; bottom: 56px; border-bottom: 1px dashed #d5d8dd; pointer-events: none; }
.sigpad-box canvas { position: relative; z-index: 1; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.sigpad-ph { position: absolute; inset: 0; display: grid; place-items: center; color: #c3c7cd; font-size: 20px; letter-spacing: 4px; pointer-events: none; }

@media (max-width: 900px) {
  .editor { grid-template-columns: 1fr; }
  .editor-side { position: static; order: -1; }
  .editor-doc { padding: 8px; }
}
@media (max-width: 760px) {
  .sigpad-mask { padding: 8px; }
  .sigpad-box { height: 200px; }
}
