:root {
  --navy: #14213d;
  --navy-2: #1f3160;
  --gold: #e0a526;
  --gold-soft: #fff4d6;
  --paper: #eef0f4;
  --panel: #ffffff;
  --ink: #1b1f2a;
  --muted: #677086;
  --line: #d9dde6;
  --target: #2f7df6;
  --target-sig: #d9480f;
  --target-date: #0f9d58;
  --hover: #e0a526;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #171a21; --panel: #20242d; --ink: #e8eaf0; --muted: #9aa3b5; --line: #343a46; --gold-soft: #3a3120;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; display: flex; flex-direction: column; background: var(--paper); color: var(--ink);
  font: 14px/1.4 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
body:not(.has-doc) .doc-only { display: none !important; }

/* ---------- top bar ---------- */
.bar {
  display: flex; align-items: center; gap: 12px; padding: 8px 12px; background: var(--navy); color: #fff;
  box-shadow: 0 2px 10px rgba(0,0,0,.25); z-index: 10; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 9px; background: none; border: 0; padding: 2px 6px 2px 0; color: #fff; }
.brand-text { display: flex; flex-direction: column; text-align: left; line-height: 1.1; }
.brand-text b { font-size: 17px; letter-spacing: .2px; }
.brand-text small { color: var(--gold); font-size: 11px; letter-spacing: .4px; }
.tools { display: flex; gap: 4px; flex: 1; justify-content: center; overflow-x: auto; scrollbar-width: none; }
body:not(.has-doc) .tools { display: none; }
.tools button {
  display: flex; flex-direction: column; align-items: center; gap: 1px; min-width: 54px; padding: 5px 8px 4px;
  border: 1px solid transparent; border-radius: 8px; background: transparent; color: #cfd6e6;
}
.tools button:hover { background: var(--navy-2); color: #fff; }
.tools button.on { background: var(--gold); color: var(--navy); border-color: #f5c55a; }
.tools .ic { font-size: 17px; line-height: 20px; height: 20px; font-weight: 600; }
.tools .ic.serif { font-family: Georgia, serif; font-size: 16px; }
.tools .lb { font-size: 11px; }
.actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.group { display: flex; border: 1px solid #33456f; border-radius: 8px; overflow: hidden; }
.group button { background: transparent; border: 0; color: #fff; min-width: 32px; height: 32px; padding: 0 8px; }
.group button:hover { background: var(--navy-2); }
.group button:disabled { opacity: .35; cursor: default; }
#zoomLabel { min-width: 54px; font-size: 12px; }
.inks { display: flex; gap: 5px; padding: 0 4px; }
.inks button { width: 20px; height: 20px; border-radius: 50%; border: 2px solid #fff3; background: var(--c); padding: 0; }
.inks button.on { border-color: var(--gold); box-shadow: 0 0 0 2px var(--navy), 0 0 0 3px var(--gold); }
button.primary { background: var(--gold); color: var(--navy); border: 0; border-radius: 8px; padding: 7px 14px; font-weight: 600; }
button.primary:hover { filter: brightness(1.06); }
button.primary.big { font-size: 16px; padding: 11px 24px; }
button.ghost { background: transparent; border: 1px solid currentColor; border-radius: 8px; padding: 6px 12px; opacity: .9; }
.bar button.ghost { color: #fff; border-color: #33456f; }
.bar button.ghost:hover { background: var(--navy-2); }
#settingsBtn { font-size: 17px; padding: 4px 10px; }

/* ---------- home ---------- */
.home { flex: 1; overflow: auto; padding: 28px 16px 40px; display: grid; gap: 22px; align-content: start; justify-items: center; }
.drop {
  width: min(760px, 100%); border: 2px dashed #b8c0d0; border-radius: 18px; background: var(--panel);
  padding: 44px 20px; text-align: center; transition: border-color .15s, background .15s;
}
.drop.over, body.dragging .drop { border-color: var(--gold); background: var(--gold-soft); }
.drop h1 { margin: 10px 0 6px; font-size: 26px; }
.drop p { color: var(--muted); max-width: 520px; margin: 0 auto 18px; }
.seal {
  width: 64px; height: 64px; margin: 0 auto; border-radius: 50%; background: var(--navy); color: var(--gold);
  display: grid; place-items: center; font-size: 36px; font-weight: 700; box-shadow: 0 0 0 5px var(--gold-soft);
}
.fine { color: var(--muted); font-size: 12px; }
.drop .fine { margin-top: 14px; }
.recent { width: min(760px, 100%); }
.recent-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.recent h2 { font-size: 15px; margin: 0 0 8px; }
.recent ul { list-style: none; margin: 0; padding: 0; background: var(--panel); border-radius: var(--radius); border: 1px solid var(--line); }
.recent li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-top: 1px solid var(--line); }
.recent li:first-child { border-top: 0; }
.recent li.empty { color: var(--muted); }
.recent .name { flex: 1; min-width: 0; }
.recent .name b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.recent .name span { color: var(--muted); font-size: 12px; }
.recent li button { background: transparent; border: 1px solid var(--line); border-radius: 7px; padding: 4px 10px; font-size: 12px; }
.recent li button:hover { border-color: var(--gold); }
.recent li button.open { background: var(--navy); color: #fff; border-color: var(--navy); }

/* ---------- viewer ---------- */
.viewer { flex: 1; overflow: auto; position: relative; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 20px 12px 60px; width: max-content; min-width: 100%; }
.page { position: relative; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15), 0 6px 20px rgba(20,33,61,.1); }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.page svg.overlay { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.page .pnum { position: absolute; top: -16px; right: 0; font-size: 11px; color: var(--muted); }
.page.scanning::after {
  content: "Finding boxes…"; position: absolute; top: 8px; left: 8px; font-size: 11px; background: var(--navy); color: #fff;
  padding: 3px 8px; border-radius: 20px; opacity: .85;
}
svg.overlay .t { fill: none; stroke: none; }
.pages.show-targets svg.overlay .t { stroke: var(--target); stroke-opacity: .45; stroke-dasharray: 2 1.5; fill: var(--target); fill-opacity: .04; }
.pages.show-targets svg.overlay .t.hint-signature, .pages.show-targets svg.overlay .t.hint-initials { stroke: var(--target-sig); fill: var(--target-sig); }
.pages.show-targets svg.overlay .t.hint-date { stroke: var(--target-date); fill: var(--target-date); }
.pages.show-targets svg.overlay .t.has-mark { stroke-opacity: .15; fill-opacity: 0; }
svg.overlay .hover { fill: var(--hover); fill-opacity: .16; stroke: var(--hover); stroke-width: 1.2; stroke-opacity: 1; stroke-dasharray: none; pointer-events: none; }
svg.overlay .ghost { opacity: .45; pointer-events: none; }
svg.overlay .hover-label { font: 600 7px "Segoe UI", system-ui, sans-serif; fill: #7a5200; pointer-events: none; }
svg.overlay .hover-label-bg { fill: #fff4d6; stroke: var(--hover); stroke-width: .5; pointer-events: none; }
svg.overlay .whiteout { fill: #fff; }
svg.overlay .whiteout-edge { fill: none; stroke: #9aa3b5; stroke-width: .5; stroke-dasharray: 2 2; }
svg.overlay .sel { fill: none; stroke: var(--gold); stroke-width: 1; stroke-dasharray: 3 2; }
svg.overlay .handle { fill: #fff; stroke: var(--gold); stroke-width: 1.2; }
svg.overlay .del circle { fill: #c92a2a; }
svg.overlay .comment { cursor: pointer; }
svg.overlay .comment .bubble { fill: #fbcc2e; stroke: #8c6608; stroke-width: .8; }
svg.overlay .comment .lines { stroke: #5a4000; stroke-width: .9; stroke-linecap: round; }
svg.overlay .comment.empty .bubble { fill: #fff4d6; stroke-dasharray: 2 1.5; }
.comment-pop {
  position: absolute; z-index: 6; width: 280px; background: #fffbea; color: #1b1f2a; border: 1px solid #e0a526; border-radius: 10px;
  box-shadow: 0 8px 28px rgba(20,33,61,.25); padding: 10px; display: grid; gap: 8px; font-size: 13px;
}
.comment-pop header { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-weight: 600; }
.comment-pop header small { font-weight: 400; color: #677086; font-size: 11px; }
.comment-pop textarea { width: 100%; min-height: 110px; resize: vertical; font: 13px/1.4 "Segoe UI", system-ui, sans-serif; padding: 7px 8px; border: 1px solid #e3cf95; border-radius: 7px; background: #fff; color: #1b1f2a; }
.comment-pop .row { justify-content: flex-end; }
.comment-pop .row .spacer { flex: 1; }
.comment-pop button { border-radius: 7px; padding: 4px 12px; font-size: 12px; }
.comment-pop .del { background: transparent; border: 1px solid #e3cf95; color: #c92a2a; }
svg.overlay .dup circle { fill: #14213d; cursor: copy; }
svg.overlay .dup rect { fill: #14213d; stroke: #fff; }
svg.overlay .del path { stroke: #fff; stroke-width: 1.2; stroke-linecap: round; }
svg.overlay text { white-space: pre; }
svg.overlay .editing { visibility: hidden; }

.text-editor {
  position: absolute; z-index: 5; margin: 0; padding: 0 2px; border: 1px dashed var(--gold); background: rgba(255, 244, 214, .6);
  font-family: Helvetica, Arial, sans-serif; line-height: 1.15; outline: none; min-width: 60px; color: #1a2b6d;
}

/* ---------- status bar ---------- */
.status {
  display: flex; align-items: center; gap: 14px; padding: 5px 12px; background: var(--panel); border-top: 1px solid var(--line);
  font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden;
}
.status #hint { color: var(--ink); overflow: hidden; text-overflow: ellipsis; }
.status .spacer, .dlg-foot .spacer { flex: 1; }
.toggle { display: flex; align-items: center; gap: 5px; cursor: pointer; }
#saveState.saved { color: #0f9d58; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 44px; transform: translateX(-50%) translateY(20px); background: var(--navy); color: #fff;
  padding: 10px 16px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.25); opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; max-width: min(560px, calc(100vw - 32px)); z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- dialogs ---------- */
.dialog { border: 0; border-radius: 14px; padding: 0; width: min(640px, calc(100vw - 24px)); background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.dialog::backdrop { background: rgba(20, 33, 61, .5); }
.dlg { padding: 18px 20px; display: grid; gap: 12px; }
.dlg h2 { margin: 0; font-size: 18px; }
.dlg h3 { margin: 0 0 6px; font-size: 13px; }
.dlg-foot { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.dlg button.ghost { color: var(--ink); border-color: var(--line); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 6px 12px; color: var(--muted); }
.tabs button.on { border-color: var(--gold); color: var(--ink); font-weight: 600; }
.pad-wrap { position: relative; }
#sigPad, #sigTypedPreview, #sigUploadPreview { width: 100%; height: auto; background: #fff; border: 1px solid var(--line); border-radius: 10px; touch-action: none; display: block; }
#sigPad { cursor: crosshair; aspect-ratio: 3 / 1; }
.pad-line { position: absolute; left: 8%; right: 8%; bottom: 26%; border-bottom: 1px dashed #c3c9d6; pointer-events: none; }
.dlg input[type=text], .dlg select, .dlg textarea { width: 100%; font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--ink); }
#sigTyped { margin-bottom: 8px; font-size: 16px; }
.field { display: grid; gap: 5px; font-weight: 600; }
.field small { font-weight: 400; color: var(--muted); }
.check { display: flex; gap: 8px; align-items: flex-start; }
.check input { margin-top: 3px; }
.set-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.sig-preview { height: 70px; border: 1px solid var(--line); border-radius: 8px; background: #fff center / contain no-repeat; margin-bottom: 6px; display: grid; place-items: center; color: #9aa3b5; font-size: 12px; }
.row { display: flex; gap: 6px; align-items: center; }
.pw summary { cursor: pointer; font-weight: 600; }
.pw-grid { display: grid; gap: 8px; margin-top: 8px; }
.pw-grid input { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--ink); }
#pwMsg.err { color: #c92a2a; }
#pwMsg.ok { color: #0f9d58; }
.row button { padding: 3px 10px; font-size: 12px; }

@media (max-width: 900px) {
  .brand-text small { display: none; }
  .tools { order: 3; flex-basis: 100%; justify-content: flex-start; }
  .actions { margin-left: auto; }
  .inks { display: none; }
  .status .toggle, .status #found { display: none; }
}
@media (max-width: 560px) {
  .bar { padding: 6px 8px; gap: 6px; }
  .brand-text { display: none; }
  .group.doc-only:nth-of-type(2) { display: none; }
  #openBtn { display: none; }
  .set-grid { grid-template-columns: 1fr; }
}
