* { box-sizing: border-box; }
[hidden] { display: none !important; }  /* the hidden attribute must beat any display: rule */
body {
  margin: 0; font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: #1c2230; background: #eef1f5;
}
#toolbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  background: #fff; border-bottom: 1px solid #d5dae2; position: sticky; top: 0; z-index: 10;
}
#toolbar .sep { width: 1px; height: 22px; background: #d5dae2; margin: 0 4px; }
#toolbar .grow { flex: 1; }
select, button, input, .upload {
  font: inherit; padding: 5px 9px; border: 1px solid #c3cad6; border-radius: 6px;
  background: #fff; color: inherit; cursor: pointer;
}
button:hover { background: #f3f5f8; }
button.primary { background: #2f6bf0; color: #fff; border-color: #2f6bf0; }
button.primary:hover { background: #2559d6; }
button:disabled { opacity: .5; cursor: progress; pointer-events: none; }  /* frozen while working */
.upload { cursor: pointer; }
.progress { font-variant-numeric: tabular-nums; color: #4a5568; }

main { display: flex; height: calc(100vh - 49px); }
#viewer { flex: 1; overflow: auto; padding: 20px; display: flex; justify-content: center; align-items: flex-start; }
#page-wrap { position: relative; display: inline-block; box-shadow: 0 2px 14px rgba(0,0,0,.15); }
#page-img { display: block; max-width: 100%; }
#overlays { position: absolute; inset: 0; }
.hint { color: #6b7280; }

.box {
  position: absolute; border: 1.5px solid rgba(47,107,240,.7);
  background: rgba(47,107,240,.10); border-radius: 2px; cursor: pointer;
}
.box:hover { background: rgba(47,107,240,.22); }
.box.labeled { border-color: rgba(22,163,74,.85); background: rgba(22,163,74,.16); }
/* an AI draft awaiting human review: dashed violet, distinct from a confirmed (solid green) field */
.box.suggested { border-style: dashed; border-color: rgba(139,92,246,.95); background: rgba(139,92,246,.16); }
.box.selected { outline: 2px solid #f59e0b; outline-offset: 1px; z-index: 5; }

#panel { width: 320px; background: #fff; border-left: 1px solid #d5dae2; padding: 16px; overflow: auto; }
.panel-empty { color: #8a93a3; }
#field-form label { display: block; margin: 10px 0; font-size: 12px; color: #4a5568; }
#field-form label.row { display: flex; align-items: center; gap: 6px; font-size: 14px; color: inherit; }
#field-form input:not([type=checkbox]), #field-form select {
  width: 100%; margin-top: 3px; cursor: text; font-size: 14px; color: #1c2230;
}
#field-form textarea {
  width: 100%; margin-top: 3px; font: inherit; font-size: 13px; color: #1c2230;
  resize: vertical; border: 1px solid #c3cad6; border-radius: 6px; padding: 5px 8px;
}
#field-form select { cursor: pointer; }
.ctx { background: #f6f8fb; border: 1px solid #e2e7ee; border-radius: 6px; padding: 8px 10px; font-size: 12px; }
.ctx > div { margin: 2px 0; overflow-wrap: anywhere; }
.ctx-k { color: #8a93a3; display: inline-block; min-width: 66px; }
.keyprev { margin: 8px 0 2px; font-size: 13px; color: #4a5568; }
.keyprev code, .ctx code { background: #eef1f5; padding: 1px 5px; border-radius: 4px; overflow-wrap: anywhere; }
.actions { display: flex; gap: 8px; margin-top: 12px; }
/* progress bar shown while the AI works (determinate for draft-all, indeterminate for suggest) */
.ai-bar { position: relative; height: 22px; background: #ede9fe; border-bottom: 1px solid #ddd6fe;
  overflow: hidden; }
.ai-bar-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0%;
  background: rgba(139,92,246,.45); transition: width .25s ease; }
.ai-bar-text { position: relative; line-height: 22px; margin-left: 10px; font-size: 12px;
  color: #5b21b6; }
.ai-bar.indet .ai-bar-fill { width: 30%; animation: ai-slide 1.1s ease-in-out infinite; }
@keyframes ai-slide { 0% { left: -30%; } 100% { left: 100%; } }

/* AI-draft review hints under the field form */
.notes { list-style: none; margin: 10px 0 0; padding: 8px 10px; font-size: 12px;
  background: #f5f3ff; border: 1px solid #ddd6fe; border-radius: 6px; }
.notes li { margin: 2px 0; overflow-wrap: anywhere; }
.notes li.note { color: #6d28d9; }
.notes li.flag { color: #b45309; }
.notes li.flag::before { content: "⚠ "; }
.box.pick { cursor: crosshair; border-color: #f59e0b; background: rgba(245,158,11,.14); }

#field-form fieldset { border: 1px solid #e2e7ee; border-radius: 6px; padding: 8px 10px; margin: 12px 0; }
#field-form legend { font-size: 11px; color: #8a93a3; padding: 0 4px; text-transform: uppercase; letter-spacing: .04em; }
label.lbl { display: block; margin: 10px 0; font-size: 12px; color: #4a5568; }
label.lbl input, label.lbl select { width: 100%; margin-top: 3px; font-size: 14px; color: #1c2230; }
.key-row { display: flex; gap: 6px; }
.key-row #f-namespace { flex: 2; } .key-row #f-instance { flex: 0 0 44px; } .key-row #f-tail { flex: 3; }
.minmax { display: flex; gap: 8px; } .minmax label { flex: 1; }
#choice-list { list-style: none; margin: 0 0 8px; padding: 0; }
#choice-list li { display: flex; align-items: center; gap: 5px; margin: 4px 0; }
#choice-list .c-label { flex: 1; font-size: 13px; }
#choice-list .c-box { font-size: 10px; color: #8a93a3; max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#choice-list button, .cond-row button { border: none; background: none; color: #b91c1c; cursor: pointer; padding: 2px 4px; }
.pick-hint { color: #b45309; font-size: 12px; }
.cond-help { font-size: 11px; color: #8a93a3; margin: 2px 0 8px; line-height: 1.4; }
.cond-row { display: flex; gap: 4px; align-items: center; margin: 5px 0; }
.cond-row select, .cond-row input { font-size: 12px; min-width: 0; flex: 1; }
.cond-row .c-val { flex: 1; display: flex; } .cond-row .c-val select, .cond-row .c-val input { width: 100%; }
.combine { font-size: 12px; color: #4a5568; margin-bottom: 6px; }
.pagekey { width: 90px; }

#code-modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; z-index: 50; }
#code-modal[hidden] { display: none; }
.code-box { background: #fff; border-radius: 8px; width: min(760px, 92vw); max-height: 86vh; display: flex; flex-direction: column; overflow: hidden; }
.code-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid #e2e7ee; }
.code-head #code-close { border: none; background: none; font-size: 16px; cursor: pointer; }
.code-head .code-btn { font-size: 12px; padding: 4px 9px; }
.copy-status { font-size: 12px; color: #16a34a; }
#code-out { margin: 0; padding: 14px; overflow: auto; font: 12px/1.5 ui-monospace, Menlo, monospace; white-space: pre; }

.repeat-row { display: flex; align-items: center; gap: 6px; margin: 10px 0; font-size: 12px; color: #4a5568; }
.repeat-row .repeat-n { width: 56px; }
.repeat-row .repeat-cf { flex: 1; min-width: 0; font-size: 12px; }
.repeat-row { flex-wrap: wrap; }

#form-modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; z-index: 50; }
#form-modal[hidden] { display: none; }
#form-body { padding: 14px 16px 18px; overflow: auto; }
#form-body .lbl input { width: 100%; margin-top: 3px; font-size: 14px; }
.doc-req { border: 1px solid #e2e7ee; border-radius: 6px; padding: 8px 10px; margin: 8px 0; }
.doc-head { display: flex; gap: 8px; align-items: center; }
.doc-head .doc-cat { flex: 1; font-size: 13px; }
.doc-head .row { font-size: 12px; white-space: nowrap; }
.doc-del { border: none; background: none; color: #b91c1c; cursor: pointer; font-size: 15px; }
.doc-cat-custom { width: 100%; margin: 6px 0 0; font-size: 13px; }
.doc-cond-rows, .sec-cond-rows { margin: 6px 0 0; }
.doc-head .sec-path { flex: 1; font: 12px/1.4 ui-monospace, Menlo, monospace; color: #1c2230; overflow-wrap: anywhere; }
.doc-head .subj-prefix, .doc-head .subj-subject { flex: 1; min-width: 0; font-size: 12px; }
.form-foot { display: flex; justify-content: flex-end; align-items: center; gap: 10px; padding: 10px 14px; border-top: 1px solid #e2e7ee; }
#form-status { color: #16a34a; font-size: 13px; font-weight: 500; }
