:root {
  --bg: #f1f3f7;
  --ink: #111827;
  --muted: #6b7280;
  --accent: #1d4ed8;
  --accent-dark: #1e3a8a;
  --border: #d7dce4;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

/* ---------- toolbar ---------- */

.toolbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
  flex-wrap: wrap;
}

.doc-badge {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.doc-info { display: flex; flex-direction: column; min-width: 0; margin-right: auto; }
.doc-info strong { font-size: 14.5px; }
.doc-info span { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.paybar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.paybar-label { font-size: 13px; color: var(--muted); margin-right: 4px; }

.pay-opt {
  font-size: 12.5px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #fff;
}
.pay-opt:hover { border-color: var(--accent); color: var(--accent); }
.pay-opt.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button, .as-btn {
  padding: 8px 16px;
  font-size: 13.5px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  line-height: 1.4;
}
button:hover, .as-btn:hover { border-color: var(--accent); color: var(--accent); }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
button.primary:hover { background: var(--accent-dark); color: #fff; }

.ghost { font-size: 13px; padding: 7px 12px; }

/* ---------- the page ---------- */

.page-wrap {
  padding: 28px 16px 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: auto;
}

.paper {
  width: 210mm;
  min-height: 297mm;
  padding: 22mm 25mm 18mm;
  background: #fff;
  box-shadow: 0 4px 18px rgba(16, 24, 40, 0.14);
  font-family: "Times New Roman", Tinos, "Nimbus Roman", serif;
  font-size: 12pt;
  line-height: 1.45;
  color: #111;
  outline: none;
}

[contenteditable]:focus { outline: none; }

.doc-title {
  text-align: center;
  font-size: 16pt;
  letter-spacing: 0.5px;
  margin: 0 0 9mm;
  font-weight: 700;
}

.paper p {
  margin: 0 0 3.2mm;
  text-align: justify;
}

.paper .kw {
  font-weight: 700;
  margin-top: 5mm;
}

.paper .indent { margin-left: 8mm; text-align: left; }

.paper strong { font-weight: 700; }

.paper a { color: #0563C1; text-decoration: underline; overflow-wrap: anywhere; }

/* ---------- signature blocks (DocuSign-style) ---------- */

.sig-row {
  display: flex;
  gap: 12mm;
  margin-top: 12mm;
}

.sig-col { flex: 1; min-width: 0; }

.sig-date {
  font-size: 10pt;
  color: #333;
  margin-bottom: 3mm;
}

.sig-stamp {
  position: relative;
  width: 56mm;
  max-width: 100%;
  padding: 0;
  min-height: 40pt;
}

.sig-signed-by {
  display: block;
  font-size: 8pt;
  color: #7a8ce0;
  letter-spacing: 0.3px;
  margin-bottom: 3pt;
}

.sig-area {
  min-height: 36pt;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}

.sig-area:empty::after {
  content: "(click ✎ to sign)";
  font-style: italic;
  color: #c0c6d0;
  font-size: 8.5pt;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

.sig-area img {
  max-width: 100%;
  max-height: 40pt;
  object-fit: contain;
}

.sig-id {
  font-size: 7pt;
  color: #9aa3af;
  margin-top: 2pt;
  min-height: 10pt;
  font-family: Consolas, "Courier New", monospace;
  overflow: hidden;
  white-space: nowrap;
}

.sig-name {
  margin-top: 3mm;
  font-weight: 400;
  font-size: 11pt;
}

.sig-pen {
  position: absolute;
  top: -8pt;
  right: 6pt;
  font-size: 10px;
  padding: 1px 7px;
  border-radius: 5px;
  background: #eef2fb;
  border: 1px solid #c7d2f0;
  color: var(--accent-dark);
  opacity: 0;
  transition: opacity 0.15s;
  line-height: 1.5;
}

.sig-col:hover .sig-pen { opacity: 1; }
.sig-pen:hover { color: var(--accent); border-color: var(--accent); }

.doc-ref {
  font-size: 8pt;
  font-style: italic;
  color: #666;
  text-align: center;
  margin-top: 12pt;
}

/* hide editing aids when generating PDF */
body.pdf-mode .sig-pen { display: none; opacity: 0; }
body.pdf-mode .sig-area:empty::after {
  content: "(click ✎ to sign)";
  font-style: italic;
  color: #c0c6d0;
  font-size: 8.5pt;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

/* ---------- modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}

.modal.hidden { display: none; }

.modal-card {
  background: #fff;
  border-radius: 10px;
  padding: 18px 20px;
  width: 100%;
  max-width: 640px;
  box-shadow: 0 12px 40px rgba(16, 24, 40, 0.25);
}

.modal-card h3 { margin: 0 0 12px; font-size: 16px; }

#sig-canvas {
  width: 100%;
  aspect-ratio: 800 / 260;
  border: 2px dashed var(--border);
  border-radius: 6px;
  background: #fff;
  cursor: crosshair;
  touch-action: none;
  display: block;
}

.btn-row { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.btn-row:last-of-type { justify-content: flex-end; }

.hint { font-size: 12px; color: var(--muted); margin: 12px 0 0; }

/* ---------- misc ---------- */

.under-note {
  max-width: 210mm;
  margin-top: 14px;
  font-size: 12.5px;
  color: var(--muted);
  text-align: center;
}

::selection { background: rgba(29, 78, 216, 0.18); }

@media (max-width: 900px) {
  .paper { width: min(210mm, 100%); }
  .under-note { width: 100%; }
}
