:root {
  color-scheme: dark;
  --bg: #111317;
  --panel: #191c22;
  --panel2: #22262e;
  --topbar: #14171c;
  --workspace: #0c0e11;
  --line: #343a45;
  --item-line: #252a31;
  --text: #eef1f5;
  --muted: #aeb7c4;
  --accent: #4aa3ff;
  --accent-strong: #1769aa;
  --accent-border: #277fc2;
  --danger: #ff5d68;
  --button-hover: #2a303a;
  --tool-active: #18314c;
  --item-hover: #222832;
  --item-active: #193755;
  --help-bg: #15181e;
  --canvas-bg: #12151a;
  --checker: #11151a;
  --mask: #00e94f;
  --mask-fill: rgba(0, 233, 79, .035);
  --mask-hover: #70ff9a;
  --selected: #00e5ff;
  --selected-fill: rgba(0, 229, 255, .18);
  --id-text: #22f064;
  --id-stroke: #111318;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    color-scheme: light;
    --bg: #f4f6f9;
    --panel: #ffffff;
    --panel2: #f5f7fa;
    --topbar: #ffffff;
    --workspace: #dfe4ea;
    --line: #d1d7df;
    --item-line: #e4e8ee;
    --text: #1f2732;
    --muted: #667385;
    --accent: #1677d2;
    --accent-strong: #1769aa;
    --accent-border: #1769aa;
    --danger: #d73545;
    --button-hover: #eaf1f8;
    --tool-active: #e4f2ff;
    --item-hover: #f2f6fa;
    --item-active: #dceeff;
    --help-bg: #f7f9fc;
    --canvas-bg: #ffffff;
    --checker: #d5dae0;
    --mask: #00b83f;
    --mask-fill: rgba(0, 184, 63, .045);
    --mask-hover: #00e94f;
    --selected: #00a9d6;
    --selected-fill: rgba(0, 169, 214, .17);
    --id-text: #00b83f;
    --id-stroke: #20242a;
  }
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6f9;
  --panel: #ffffff;
  --panel2: #f5f7fa;
  --topbar: #ffffff;
  --workspace: #dfe4ea;
  --line: #d1d7df;
  --item-line: #e4e8ee;
  --text: #1f2732;
  --muted: #667385;
  --accent: #1677d2;
  --accent-strong: #1769aa;
  --accent-border: #1769aa;
  --danger: #d73545;
  --button-hover: #eaf1f8;
  --tool-active: #e4f2ff;
  --item-hover: #f2f6fa;
  --item-active: #dceeff;
  --help-bg: #f7f9fc;
  --canvas-bg: #ffffff;
  --checker: #d5dae0;
  --mask: #00b83f;
  --mask-fill: rgba(0, 184, 63, .045);
  --mask-hover: #00e94f;
  --selected: #00a9d6;
  --selected-fill: rgba(0, 169, 214, .17);
  --id-text: #00b83f;
  --id-stroke: #20242a;
}

:root[data-theme="dark"] {
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; font-family: Inter, "Segoe UI", "Microsoft YaHei UI", Arial, sans-serif; background: var(--bg); color: var(--text); }
button, input, select { font: inherit; }
button { border: 1px solid var(--line); background: var(--panel2); color: var(--text); border-radius: 7px; padding: 8px 11px; cursor: pointer; transition: background .15s ease, border-color .15s ease; }
button:hover:not(:disabled) { border-color: var(--accent); background: var(--button-hover); }
button:disabled { opacity: .42; cursor: default; }
button.primary { background: var(--accent-strong); color: #fff; border-color: var(--accent-border); }
button.tool.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: var(--tool-active); }

.topbar { height: 64px; display: flex; align-items: center; gap: 16px; padding: 7px 12px; border-bottom: 1px solid var(--line); background: var(--topbar); }
.brand { min-width: 225px; display: flex; align-items: center; gap: 10px; flex: 0 0 auto; }
.brand-logo { width: 44px; height: 44px; object-fit: contain; flex: 0 0 auto; }
.brand-copy { display: flex; flex-direction: column; line-height: 1.08; }
.brand-copy strong { font-size: 20px; letter-spacing: .2px; }
.brand-copy span { font-size: 12px; color: var(--muted); margin-top: 5px; }
.toolbar { display: flex; gap: 6px; align-items: center; overflow-x: auto; white-space: nowrap; flex: 1; scrollbar-width: thin; }
.toolbar-spacer { flex: 1 1 auto; min-width: 4px; }
.sep { width: 1px; height: 30px; background: var(--line); margin: 0 4px; flex: 0 0 auto; }
.toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--text); font-size: 14px; }
.theme-btn { min-width: 78px; flex: 0 0 auto; }
#zoomLabel { width: 54px; text-align: center; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }

.app-grid { height: calc(100vh - 64px); display: grid; grid-template-columns: 250px minmax(0,1fr) 330px; }
.sidebar, .inspector { background: var(--panel); overflow: auto; }
.sidebar { border-right: 1px solid var(--line); }
.inspector { border-left: 1px solid var(--line); padding-bottom: 22px; }
.panel-title { font-weight: 750; font-size: 17px; padding: 15px 15px 10px; }
.panel-title.secondary { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 16px; }
.folder-name { color: var(--muted); font-size: 14px; padding: 0 15px 11px; word-break: break-all; line-height: 1.45; }
.file-list { border-top: 1px solid var(--line); }
.file-item { display: flex; align-items: center; gap: 9px; width: 100%; padding: 11px 13px; border-bottom: 1px solid var(--item-line); cursor: pointer; font-size: 15px; }
.file-item:hover { background: var(--item-hover); }
.file-item.active { background: var(--item-active); }
.file-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.file-type-icon { width: 20px; height: 20px; flex: 0 0 20px; display: inline-flex; align-items: center; justify-content: center; color: var(--muted); }
.file-type-icon svg { width: 19px; height: 19px; display: block; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.file-item .badge { color: #43b961; font-size: 11px; font-weight: 700; border: 1px solid #43b961; border-radius: 9px; padding: 1px 6px; }
.help-box { margin: 15px 11px; padding: 12px; border: 1px solid var(--line); background: var(--help-bg); color: var(--muted); border-radius: 8px; font-size: 13px; line-height: 1.72; }
.help-box b { color: var(--text); font-size: 14px; }

.workspace-wrap { position: relative; min-width: 0; min-height: 0; background: var(--workspace); }
.viewport { width: 100%; height: 100%; overflow: hidden; position: relative; touch-action: none; cursor: default; background-image: linear-gradient(45deg,var(--checker) 25%,transparent 25%),linear-gradient(-45deg,var(--checker) 25%,transparent 25%),linear-gradient(45deg,transparent 75%,var(--checker) 75%),linear-gradient(-45deg,transparent 75%,var(--checker) 75%); background-size: 28px 28px; background-position: 0 0,0 14px,14px -14px,-14px 0; }
.viewport.pen-mode { cursor: crosshair; }
.viewport.panning { cursor: grabbing; }
.stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; user-select: none; will-change: transform; backface-visibility: hidden; contain: strict; z-index: 1; }
.stage img { position: absolute; left: 0; top: 0; width: 100%; height: 100%; object-fit: fill; pointer-events: none; }
#maskCanvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; image-rendering: auto; z-index: 2; }
#maskSvg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: hidden; pointer-events: none; z-index: 3; }
/* WebGL2 不可用时才显示的备用 SVG。正常 Chrome/Edge 路径不创建 1000 个 SVG 节点。 */
.all-mask-path { fill: none; stroke: var(--mask); stroke-width: 3.1px; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; pointer-events: none; }
/* 选中轮廓也使用固定屏幕线宽。 */
.selected-mask { fill: var(--selected-fill); stroke: var(--selected); stroke-width: 4.1px; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; pointer-events: none; }
/* 点击右侧轮廓编号时，让当前选中轮廓闪烁 3 次，便于在密集细胞中快速定位。 */
.selected-mask.flash-3x {
  animation: selected-mask-flash 300ms ease-in-out 3;
}
@keyframes selected-mask-flash {
  0%, 100% { opacity: 1; stroke-width: 4.1px; filter: drop-shadow(0 0 0 transparent); }
  50% { opacity: .16; stroke-width: 7px; filter: drop-shadow(0 0 5px var(--selected)); }
}
.drawing-line { fill: rgba(0, 200, 255, .08); stroke: #00d8ff; stroke-width: 4; pointer-events: none; }
.drawing-start { fill: #00e5ff; stroke: #00151b; stroke-width: 2.5; vector-effect: non-scaling-stroke; pointer-events: none; }
.hidden-svg { display: none; }

.empty-state { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; text-align: center; color: var(--muted); padding: 30px; }
.empty-logo { width: 92px; height: 92px; object-fit: contain; margin-bottom: 8px; opacity: .92; }
.empty-state h2 { color: var(--text); font-size: 24px; margin: 8px 0; }
.empty-state p { font-size: 15px; max-width: 650px; line-height: 1.6; }
.hidden { display: none !important; }
.busy { position: absolute; inset: 0; z-index: 20; background: rgba(0,0,0,.56); display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center; font-size: 16px; color: #fff; }
.spinner { width: 36px; height: 36px; border: 4px solid #6c7480; border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.stats-grid { padding: 0 15px 10px; display: grid; gap: 10px; }
.stats-grid > div, .selection-info > div { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.stats-grid span, .selection-info span { color: var(--muted); font-size: 14px; }
.stats-grid strong { text-align: right; font-size: 15px; max-width: 195px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted { color: var(--muted); font-size: 14px; padding: 0 15px 11px; line-height: 1.5; }
.selection-info { display: grid; gap: 10px; padding: 0 15px 11px; font-size: 15px; }
.selection-info b { font-size: 15px; }
#distributionCanvas { width: calc(100% - 22px); height: 250px; margin: 0 11px; background: var(--canvas-bg); border: 1px solid var(--line); border-radius: 7px; }
.status-text { margin: 0 15px; color: var(--muted); font-size: 14px; line-height: 1.58; white-space: pre-wrap; word-break: break-word; }

@media (max-width: 1180px) {
  .app-grid { grid-template-columns: 210px minmax(0,1fr) 280px; }
  .brand { min-width: 190px; }
  .brand-logo { width: 38px; height: 38px; }
  .brand-copy strong { font-size: 18px; }
}
@media (max-width: 980px) {
  .brand-copy { display:none; }
  .brand { min-width: 44px; }
  .app-grid { grid-template-columns: 190px minmax(0,1fr) 250px; }
}

/* 轮廓编号虚拟列表：DOM 中只渲染可见项，1000+ 实例仍保持轻量。 */
.mask-index-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.mask-list-count { min-width: 30px; padding: 2px 8px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); font-size: 12px; font-weight: 650; text-align: center; }
.mask-id-list { position: relative; height: 244px; margin: 0 11px 4px; overflow-y: auto; overflow-x: hidden; border: 1px solid var(--line); border-radius: 7px; background: var(--canvas-bg); scrollbar-width: thin; }
.mask-id-list-inner { position: relative; width: 100%; min-height: 100%; }
.mask-index-item { position: absolute; left: 0; top: 0; width: 100%; height: 34px; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 11px; border: 0; border-bottom: 1px solid var(--item-line); border-radius: 0; background: transparent; color: var(--text); font-variant-numeric: tabular-nums; text-align: left; transition: none; }
.mask-index-item:hover { background: var(--item-hover); border-color: var(--item-line); }
.mask-index-item.active { background: var(--item-active); box-shadow: inset 3px 0 0 var(--selected); color: var(--text); }
.mask-index-number { font-size: 14px; font-weight: 700; }
.mask-index-source { color: var(--muted); font-size: 11px; }


/* ===== GMP / QA 功能 ===== */
.mono { font-family: Consolas, "Cascadia Mono", monospace; font-size: 13px !important; }
.operator-box { padding: 0 13px 12px; }
.operator-box input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px; background: var(--panel2); color: var(--text); }
.operator-box input:focus { outline: none; border-color: var(--accent); }
.operator-box input.missing { border-color: var(--danger); }
.operator-hint { color: var(--muted); font-size: 12px; margin-top: 5px; }
.integrity-ok { color: #2fb45a; }
.integrity-bad { color: var(--danger); }
.integrity-warn { color: #d99a00; }
.criteria-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.criteria-heading small { color: var(--muted); font-weight: 500; font-size: 12px; }
.small-btn { padding: 4px 10px; font-size: 13px; }
.criteria-box { padding: 0 15px 6px; display: grid; gap: 8px; }
.result-badge { border-radius: 7px; padding: 8px 10px; font-weight: 750; font-size: 15px; text-align: center; border: 1px solid var(--line); }
.result-badge.pass { background: rgba(47,180,90,.16); color: #2fb45a; border-color: #2fb45a; }
.result-badge.fail { background: rgba(255,93,104,.14); color: var(--danger); border-color: var(--danger); }
.result-badge.inconclusive { background: rgba(217,154,0,.14); color: #d99a00; border-color: #d99a00; }
.result-badge.not-set { color: var(--muted); }
.criteria-summary { color: var(--muted); font-size: 12.5px; line-height: 1.5; }
.criteria-items { display: grid; gap: 4px; font-size: 12.5px; }
.criteria-items > div { display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dashed var(--item-line); padding-bottom: 3px; }
.criteria-items .r-PASS { color: #2fb45a; font-weight: 700; }
.criteria-items .r-FAIL { color: var(--danger); font-weight: 700; }
.criteria-items .r-NA, .criteria-items .r-INFO { color: #d99a00; font-weight: 700; }
.criteria-dialog { width: min(620px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); padding: 18px 20px; }
.criteria-dialog::backdrop { background: rgba(0,0,0,.5); }
.criteria-dialog h3 { margin: 0 0 6px; }
.dialog-note { color: var(--muted); font-size: 13px; margin: 0 0 12px; line-height: 1.5; }
.crit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.crit-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.crit-grid input, .crit-grid select { padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel2); color: var(--text); }
.crit-grid .span2 { grid-column: 1 / -1; }
.crit-section { font-weight: 700; font-size: 14px; color: var(--text); border-top: 1px solid var(--line); padding-top: 10px; margin-top: 2px; }
.dialog-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; white-space: pre-wrap; }
.dialog-actions { display: flex; gap: 8px; margin-top: 8px; }

/* ===== 登录与权限 (v1.4.0) ===== */
.user-card { margin: 0 13px 12px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--help-bg); }
.user-name { font-weight: 750; font-size: 15px; word-break: break-all; }
.user-role { color: var(--muted); font-size: 12.5px; margin-top: 3px; }
.user-actions { display: flex; gap: 6px; margin-top: 9px; }
.auth-overlay { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; background: var(--bg); padding: 16px; }
.auth-overlay.hidden { display: none; }
.auth-card { width: min(400px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 26px 26px 22px; text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.18); }
.auth-logo { width: 64px; height: 64px; object-fit: contain; }
.auth-card h2 { margin: 6px 0 2px; }
.auth-sub { color: var(--muted); font-size: 13px; margin-bottom: 16px; }
.auth-form { display: grid; gap: 11px; text-align: left; }
.auth-form label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.auth-form input { padding: 9px 10px; border: 1px solid var(--line); border-radius: 7px; background: var(--panel2); color: var(--text); }
.auth-form input:focus, .app-dialog input:focus { outline: none; border-color: var(--accent); }
.auth-form button { padding: 10px; margin-top: 2px; }
.auth-notice { background: var(--help-bg); border: 1px solid var(--line); border-radius: 7px; padding: 9px 10px; font-size: 13px; color: var(--text); line-height: 1.5; }
.app-dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); padding: 18px 20px; width: min(760px, calc(100vw - 32px)); }
.app-dialog.narrow { width: min(420px, calc(100vw - 32px)); }
.app-dialog.xwide { width: min(1180px, calc(100vw - 32px)); }
.app-dialog::backdrop { background: rgba(0,0,0,.5); }
.app-dialog h3 { margin: 0 0 6px; }
.crit-grid.one { grid-template-columns: 1fr; }
.new-user-row { display: flex; gap: 8px; margin-bottom: 8px; }
.new-user-row input { flex: 1; min-width: 0; padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px; background: var(--panel2); color: var(--text); }
.temp-pw { border: 1px solid #d99a00; background: rgba(217,154,0,.12); border-radius: 7px; padding: 9px 11px; font-size: 13.5px; line-height: 1.6; }
.temp-pw code { font-family: Consolas, monospace; font-size: 16px; font-weight: 700; user-select: all; background: var(--panel2); padding: 1px 6px; border-radius: 4px; }
.table-wrap { max-height: 340px; overflow: auto; border: 1px solid var(--line); border-radius: 7px; }
.table-wrap.tall { max-height: 60vh; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th { position: sticky; top: 0; background: var(--panel2); text-align: left; font-weight: 700; z-index: 1; }
.data-table th, .data-table td { padding: 6px 9px; border-bottom: 1px solid var(--item-line); vertical-align: top; }
.data-table tr.inactive td { color: var(--muted); }
.data-table tr.bad td { background: rgba(255,93,104,.12); }
.data-table td.nowrap { white-space: nowrap; }
.data-table .row-actions { display: flex; gap: 5px; white-space: nowrap; }
.status-pill { display: inline-block; font-size: 11.5px; font-weight: 700; border-radius: 9px; padding: 1px 7px; border: 1px solid currentColor; }
.status-pill.active { color: #2fb45a; }
.status-pill.locked { color: var(--danger); }
.status-pill.pending { color: #d99a00; }
.status-pill.deleted { color: var(--muted); }
.log-toolbar { display: flex; gap: 8px; margin: 6px 0 8px; }
.log-toolbar select, .log-toolbar input { padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; background: var(--panel2); color: var(--text); }
.log-toolbar select { min-width: 280px; }
.log-toolbar input { flex: 1; min-width: 0; }
.log-verify { border-radius: 7px; padding: 8px 11px; margin-bottom: 8px; font-size: 13.5px; font-weight: 650; }
.log-verify.ok { background: rgba(47,180,90,.14); color: #2fb45a; }
.log-verify.bad { background: rgba(255,93,104,.14); color: var(--danger); white-space: pre-wrap; }
.muted-inline { color: var(--muted); font-size: 13px; align-self: center; }

.new-user-row { flex-wrap: wrap; align-items: end; }
.new-user-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.new-user-row select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px; background: var(--panel2); color: var(--text); }
.new-user-row input[name="validityDays"] { width: 150px; }

.admin-filters { flex-wrap: wrap; }
.admin-filters select { min-width: 160px; }
.archive-files { display: flex; flex-wrap: wrap; gap: 12px; margin: 12px 0; }
.archive-images { display: flex; flex-wrap: wrap; gap: 12px; }
.archive-images figure { flex: 1 1 280px; margin: 0 0 12px; }
.archive-images img { width: 100%; max-height: 360px; object-fit: contain; }

.users-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.admin-session-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.admin-mode > .topbar, .admin-mode > .app-grid { display: none; }
.admin-mode #usersDialog[open] {
  position: fixed;
  inset: 0;
  z-index: 10;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  border: 0;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  overflow: auto;
}
.admin-mode #usersDialog > section { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.admin-mode #usersDialog .table-wrap { flex: 1; min-height: 160px; max-height: none; }
.admin-mode #usersDialog > .dialog-actions { display: none; }
