:root {
  --navy: #1B2A3E;
  --mid: #2E4A6A;
  --steel: #4A6382;
  --mist: #C8D8E8;
  --cream: #F7F5F0;
  --white: #FFFFFF;
  --subtle: #6B8099;
  --border: #E2E6EA;
  --border-strong: #C8D0D8;
  --success: #2A7A4B;
  --success-bg: #EAF5EF;
  --danger: #8B3A3A;
  --danger-bg: #FAEAEA;
  --radius: 6px;
  --shadow: 0 1px 4px rgba(27,42,62,0.08);
  --shadow-md: 0 4px 16px rgba(27,42,62,0.10);
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: #F0F2F5;
  color: var(--navy);
  min-height: 100vh;
}

/* HEADER */
header {
  background: var(--white);
  border-bottom: 1px solid var(--border);
  padding: 0 2rem;
  height: 56px;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 50;
  box-shadow: var(--shadow);
}
.header-brand { display: flex; align-items: center; gap: 10px; }
.header-brand svg { height: 28px; width: auto; }
.header-badge {
  font-size: 11px; font-weight: 500; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--subtle);
  border-left: 1px solid var(--border); padding-left: 12px; margin-left: 4px;
}
.header-status { font-size: 12px; color: var(--subtle); display: flex; align-items: center; gap: 6px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: #CCC; }
.status-dot.active { background: var(--success); }

/* LAYOUT */
.app-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 1.5rem;
  max-width: 1280px;
  margin: 0 auto;
  padding: 1.5rem;
  align-items: start;
}
.sidebar { display: flex; flex-direction: column; gap: 1rem; }

/* PANELS */
.panel {
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.panel-title { font-size: 13px; font-weight: 600; color: var(--navy); letter-spacing: 0.02em; }
.panel-subtitle { font-size: 11px; color: var(--subtle); margin-top: 2px; }
.panel-body { padding: 1.25rem; }

/* UPLOAD */
#doc-panel { min-height: 600px; }
.doc-upload-zone {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 1rem;
  min-height: 500px;
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius);
  background: #FAFBFC;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  padding: 3rem;
  text-align: center;
}
.doc-upload-zone:hover, .doc-upload-zone.dragover { border-color: var(--mid); background: #F0F4F8; }
.upload-icon {
  width: 48px; height: 48px; background: var(--mist); border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.upload-icon svg { width: 22px; height: 22px; }
.upload-title { font-size: 15px; font-weight: 600; color: var(--navy); }
.upload-sub { font-size: 13px; color: var(--subtle); }
.upload-btn {
  padding: 8px 20px; background: var(--navy); color: var(--white);
  border: none; border-radius: var(--radius); font-size: 13px;
  font-weight: 500; cursor: pointer; transition: background 0.2s; font-family: inherit;
}
.upload-btn:hover { background: var(--mid); }
.upload-btn.small { font-size: 12px; padding: 6px 14px; }
#file-input { display: none; }

/* VIEWER */
#pdf-viewer { display: none; }
.tool-hint {
  padding: 0.65rem 1rem;
  background: #FAFBFC;
  border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--subtle);
}
.tool-hint.active { background: var(--mist); color: var(--navy); font-weight: 500; }
#pages-scroll {
  overflow-y: auto; max-height: 78vh;
  background: #E8EAED;
  padding: 16px;
}
#pages { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.page {
  position: relative; width: 100%; max-width: 820px;
  background: var(--white); box-shadow: var(--shadow-md);
}
.page canvas { display: block; width: 100%; height: 100%; }
.page-overlay { position: absolute; inset: 0; }
.placing .page-overlay { cursor: crosshair; }

/* FIELDS ON THE DOCUMENT */
.field {
  position: absolute;
  border: 1px dashed rgba(46,74,106,0.35);
  background: rgba(200,216,232,0.18);
  touch-action: none;
  cursor: move;
  user-select: none;
}
.field:hover { border-color: var(--mid); }
.field.selected {
  border: 1.5px solid var(--mid);
  background: rgba(46,74,106,0.07);
  z-index: 5;
}
.field img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; display: block; }
.field input {
  width: 100%; height: 100%; border: none; background: transparent;
  padding: 0 4px; color: var(--navy);
  font-family: Helvetica, Arial, sans-serif;
  outline: none; pointer-events: none;
}
.field.editing input { pointer-events: auto; cursor: text; }
.field-checkbox { background: rgba(255,255,255,0.6); border: 1.5px solid var(--navy); }
.field-checkbox svg { width: 100%; height: 100%; display: none; pointer-events: none; }
.field-checkbox.checked svg { display: block; }
.field .del {
  position: absolute; top: -11px; right: -11px;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--danger); color: var(--white); border: 2px solid var(--white);
  font-size: 12px; line-height: 18px; cursor: pointer; display: none; padding: 0;
  font-family: inherit;
}
.field .resize {
  position: absolute; right: -7px; bottom: -7px;
  width: 14px; height: 14px; background: var(--mid);
  border: 2px solid var(--white); border-radius: 3px;
  cursor: nwse-resize; display: none;
}
.field.selected .del, .field.selected .resize { display: block; }

/* TOOLS */
.tool-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tool-btn {
  padding: 10px; border: 1px solid var(--border); background: var(--white);
  border-radius: var(--radius); font-size: 13px; font-weight: 500;
  color: var(--navy); cursor: pointer; font-family: inherit; transition: all 0.15s;
}
.tool-btn:hover { background: #F0F4F8; border-color: var(--border-strong); }
.tool-btn.active { background: var(--navy); color: var(--white); border-color: var(--navy); }
.tool-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.help-text { font-size: 11px; color: var(--subtle); margin-top: 10px; line-height: 1.5; }
.help-text.center { text-align: center; }

/* SIGNATURE AND INITIALS CARDS */
.mark-card { margin-bottom: 14px; }
.mark-card:last-child { margin-bottom: 0; }
.mark-label {
  font-size: 11px; font-weight: 500; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--subtle); margin-bottom: 6px;
}
.mark-preview {
  border: 1px dashed var(--border-strong); border-radius: 4px; background: var(--white);
  min-height: 64px; display: flex; align-items: center; justify-content: center;
  margin-bottom: 8px; overflow: hidden; padding: 4px;
}
.mark-preview img { max-height: 56px; max-width: 100%; }
.sig-preview-empty { font-size: 12px; color: var(--subtle); }

/* FORMS */
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.form-group { margin-bottom: 10px; }
.form-group:last-child { margin-bottom: 0; }
.form-label {
  display: block; font-size: 11px; font-weight: 500;
  letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--subtle); margin-bottom: 4px;
}
.form-input {
  width: 100%; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 4px;
  font-size: 13px; color: var(--navy); outline: none;
  font-family: inherit; background: var(--white); transition: border-color 0.2s;
}
.form-input:focus { border-color: var(--mid); }

/* BUTTONS */
.btn {
  width: 100%; padding: 10px; border: none; border-radius: var(--radius);
  font-size: 13px; font-weight: 500; cursor: pointer;
  font-family: inherit; transition: all 0.2s; letter-spacing: 0.02em;
}
.btn-small { font-size: 12px; padding: 6px; }
.btn-primary { background: var(--navy); color: var(--white); }
.btn-primary:hover { background: var(--mid); }
.btn-secondary { background: var(--white); color: var(--navy); border: 1px solid var(--border); }
.btn-secondary:hover { background: #F0F4F8; }
.btn-success { background: var(--success); color: var(--white); }
.btn-success:hover { background: #236640; }
.btn:disabled { background: #B0B8C4; color: var(--white); cursor: not-allowed; }
.download-btn { margin-top: 14px; }

/* META */
.meta-item {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12px; padding: 6px 0; border-bottom: 1px solid var(--border);
}
.meta-label { color: var(--subtle); }
.meta-value { color: var(--navy); font-weight: 500; text-align: right; max-width: 60%; word-break: break-all; }

/* MODAL */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,28,46,0.55);
  display: none; align-items: center; justify-content: center;
  z-index: 200; padding: 1rem;
}
.modal-backdrop.show { display: flex; }
.modal {
  background: var(--white); border-radius: var(--radius);
  width: 100%; max-width: 520px; box-shadow: var(--shadow-md);
  max-height: 92vh; overflow-y: auto;
}
.modal-close {
  border: none; background: none; font-size: 18px; color: var(--subtle);
  cursor: pointer; font-family: inherit; padding: 0 4px;
}
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 1.25rem; }
.sig-tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.sig-tab {
  flex: 1; padding: 10px; text-align: center; font-size: 12px; font-weight: 500;
  cursor: pointer; background: none; border: none; color: var(--subtle);
  border-bottom: 2px solid transparent; font-family: inherit; transition: all 0.15s;
}
.sig-tab.active { color: var(--navy); border-bottom-color: var(--navy); }
.sig-panel { display: none; }
.sig-panel.active { display: block; }
#mark-canvas {
  width: 100%; aspect-ratio: 3 / 1;
  border: 1px solid var(--border); border-radius: 4px;
  background: var(--white); cursor: crosshair; display: block; touch-action: none;
}
.canvas-hint { font-size: 11px; color: var(--subtle); text-align: center; margin: 6px 0 8px; }
.type-sig-input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 4px;
  font-size: 18px; color: var(--navy); outline: none; background: var(--white);
  font-family: inherit; transition: border-color 0.2s;
}
.type-sig-input:focus { border-color: var(--mid); }
.type-sig-preview {
  margin-top: 10px; padding: 12px; border: 1px solid var(--border); border-radius: 4px;
  background: var(--white); min-height: 72px; font-size: 40px;
  font-family: 'Dancing Script', 'Brush Script MT', cursive;
  color: var(--navy); text-align: center; line-height: 1.3; overflow: hidden;
}
.font-options { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.font-opt {
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 4px;
  background: var(--white); cursor: pointer; font-size: 13px; color: var(--navy);
  font-family: inherit; transition: border-color 0.15s;
}
.font-opt.active { border-color: var(--navy); background: #F0F4F8; }

/* TOAST */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(80px);
  background: var(--navy); color: var(--white);
  padding: 10px 20px; border-radius: var(--radius);
  font-size: 13px; font-weight: 500; box-shadow: var(--shadow-md);
  z-index: 999; transition: transform 0.3s ease; max-width: 90vw; text-align: center;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

@media (max-width: 900px) {
  .app-layout { grid-template-columns: 1fr; padding: 1rem; }
  header { padding: 0 1rem; }
  #pages-scroll { max-height: 70vh; padding: 10px; }
}

/* ===== SENDER AND SIGNER PAGES ===== */

/* Header navigation */
.header-nav { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }
.header-nav a { font-size: 13px; color: var(--steel); text-decoration: none; font-weight: 500; }
.header-nav a:hover { color: var(--navy); }

/* Plain pages: sign in, setup, and notices */
.auth-wrap, .state-wrap { display: flex; justify-content: center; padding: 3rem 1rem; }
.auth-card { width: 100%; max-width: 440px; }
.auth-text { font-size: 13px; line-height: 1.6; margin-bottom: 12px; color: var(--navy); }
.auth-text a { color: var(--mid); }
.error-text { color: var(--danger); }
.link-btn { text-decoration: none; display: inline-block; }
.link-quiet {
  display: block; margin: 12px auto 0; background: none; border: none; font-family: inherit;
  color: var(--subtle); font-size: 12px; text-decoration: underline; cursor: pointer;
}
textarea.form-input { resize: vertical; min-height: 70px; }
.check-row { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 14px; cursor: pointer; }
.btn-danger { background: var(--danger); color: var(--white); }
.btn-danger:hover { background: #6E2D2D; }

/* Dashboard and envelope pages */
.page-wrap { max-width: 1100px; margin: 0 auto; padding: 1.5rem; display: flex; flex-direction: column; gap: 1rem; }
.page-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.page-title { font-size: 22px; font-weight: 600; color: var(--navy); }
.page-sub { font-size: 13px; color: var(--subtle); margin-top: 4px; }
.back-link { font-size: 12px; color: var(--steel); text-decoration: none; }
.back-link:hover { color: var(--navy); }
.filter-row { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 6px 14px; border: 1px solid var(--border); background: var(--white); border-radius: 20px;
  font-size: 12px; font-weight: 500; color: var(--navy); cursor: pointer; font-family: inherit;
}
.chip.active { background: var(--navy); color: var(--white); border-color: var(--navy); }
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  text-align: left; padding: 10px 14px; font-size: 11px; font-weight: 500; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--subtle); border-bottom: 1px solid var(--border); background: #FAFBFC;
}
.data-table td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
.data-table tr.clickable { cursor: pointer; }
.data-table tr.clickable:hover td { background: #F7F9FB; }
.cell-main { font-weight: 500; color: var(--navy); }
.cell-sub { font-size: 12px; color: var(--subtle); margin-top: 2px; word-break: break-word; }
.cell-link { color: var(--navy); font-weight: 600; text-decoration: none; }
.cell-link:hover { text-decoration: underline; }
.nowrap { white-space: nowrap; }
.strong { font-weight: 600; }
.small-cell { font-size: 12px; color: var(--steel); max-width: 340px; word-break: break-word; }
.mono { font-family: Consolas, Menlo, monospace; font-size: 11px; }
.empty-note { padding: 2rem; text-align: center; font-size: 13px; color: var(--subtle); }
.turn-tag {
  display: inline-block; margin-top: 4px; padding: 2px 8px; border-radius: 10px;
  background: var(--navy); color: var(--white); font-size: 11px; font-weight: 500;
}
.badge { display: inline-block; padding: 3px 10px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge-in_progress, .badge-sent { background: #E5EDF6; color: var(--mid); }
.badge-viewed, .badge-finalizing { background: #FFF1CC; color: #7A5A00; }
.badge-waiting { background: #EEF0F3; color: var(--subtle); }
.badge-signed, .badge-completed { background: var(--success-bg); color: var(--success); }
.badge-voided, .badge-declined { background: var(--danger-bg); color: var(--danger); }
.action-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-inline {
  display: inline-block; padding: 8px 16px; border-radius: var(--radius); font-size: 13px; font-weight: 500;
  cursor: pointer; font-family: inherit; text-decoration: none; border: 1px solid var(--border);
}
.btn-main { background: var(--navy); color: var(--white); border-color: var(--navy); }
.btn-main:hover { background: var(--mid); }
.btn-plain { background: var(--white); color: var(--navy); }
.btn-plain:hover { background: #F0F4F8; }
.btn-danger-plain { background: var(--white); color: var(--danger); border-color: #E3C4C4; }
.btn-danger-plain:hover { background: var(--danger-bg); }
.notice { padding: 10px 14px; border-radius: var(--radius); font-size: 13px; margin-bottom: 8px; line-height: 1.5; }
.notice-ok { background: var(--success-bg); color: var(--success); border: 1px solid #C4E3D0; }
.notice-warn { background: #FFF6E0; color: #7A5A00; border: 1px solid #F0DDA8; }
#v-meta .meta-value { max-width: 75%; }

/* Sender console: signers */
.signer-row {
  border: 1px solid var(--border); border-left: 4px solid var(--mid); border-radius: var(--radius);
  padding: 10px; margin-bottom: 10px; background: var(--white); cursor: pointer;
}
.signer-row.active { background: #F4F8FC; box-shadow: 0 0 0 2px rgba(46,74,106,0.22); }
.signer-row .form-input { margin-top: 6px; }
.signer-head { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.signer-num {
  width: 20px; height: 20px; border-radius: 50%; background: var(--navy); color: var(--white);
  font-size: 11px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center;
}
.signer-tag { font-size: 11px; color: var(--subtle); margin-right: auto; margin-left: 8px; font-weight: 500; }
.signer-ctrl { display: flex; gap: 4px; }
.mini-btn {
  font-size: 11px; padding: 3px 8px; border: 1px solid var(--border); background: var(--white);
  border-radius: 4px; cursor: pointer; color: var(--navy); font-family: inherit;
}
.mini-btn:hover { background: #F0F4F8; }
.mini-btn:disabled { opacity: 0.35; cursor: not-allowed; }
#add-signer { margin-top: 2px; }

/* Sender console: items on the page */
.field.sfield {
  border-style: solid; border-width: 1.5px; display: flex; align-items: center; overflow: visible;
}
.sfield .ftag {
  display: block; max-width: 100%; padding: 0 4px; font-weight: 600; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; pointer-events: none;
}
.sfield.selected { box-shadow: 0 0 0 2px rgba(27,42,62,0.35); }

/* Signing page */
.progress-track { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; margin-bottom: 12px; }
.progress-fill { height: 100%; width: 0; background: var(--success); transition: width 0.3s; }
.note-box {
  margin-top: 10px; padding: 10px; background: #FAFBFC; border: 1px solid var(--border);
  border-radius: 4px; font-size: 12px; line-height: 1.5; white-space: pre-wrap; color: var(--navy);
}
.consent-list { margin: 0 0 14px 18px; font-size: 13px; line-height: 1.6; color: var(--navy); }
.sfield-sign {
  position: absolute; display: flex; align-items: center; justify-content: center; cursor: pointer;
  border: 2px solid #E0A100; background: rgba(255,214,102,0.30); border-radius: 3px;
}
.sfield-sign.done { border-color: var(--success); background: rgba(42,122,75,0.07); }
.sfield-sign.optional.todo { border-style: dashed; }
.sfield-sign.kind-date { cursor: default; border-color: var(--border-strong); background: rgba(200,216,232,0.18); }
.sfield-sign img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; display: block; }
.sfield-sign .sf-prompt { font-size: 11px; font-weight: 600; color: #7A5A00; pointer-events: none; }
.sfield-sign .sf-input {
  width: 100%; height: 100%; border: none; background: transparent; padding: 0 4px;
  font-family: Helvetica, Arial, sans-serif; color: var(--navy); outline: none;
}
.sfield-sign .sf-text { padding: 0 4px; color: var(--navy); font-family: Helvetica, Arial, sans-serif; white-space: nowrap; }
.sfield-sign.kind-checkbox svg { width: 100%; height: 100%; pointer-events: none; }
.sfield-sign.pulse { animation: sfpulse 0.7s ease-in-out 2; }
@keyframes sfpulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224,161,0,0.75); }
  50% { box-shadow: 0 0 0 9px rgba(224,161,0,0); }
}
.sfield-static { position: absolute; display: flex; align-items: center; pointer-events: none; }
.sfield-static img { width: 100%; height: 100%; object-fit: contain; display: block; }
.sfield-static.box { border: 1.2px solid var(--navy); }
.sfield-static.box svg { width: 100%; height: 100%; }
.sfield-static .sf-text { padding: 0 4px; color: var(--navy); font-family: Helvetica, Arial, sans-serif; white-space: nowrap; }

@media (max-width: 900px) {
  .page-wrap { padding: 1rem; }
  .header-nav { gap: 12px; }
  .header-badge { display: none; }
}
.header-link { font-size: 12px; color: var(--steel); text-decoration: none; font-weight: 500; margin-right: 14px; }
.header-link:hover { color: var(--navy); }
