* { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

:root {
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --ink: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --bg: #f3f4f6;
  --card: #ffffff;
  --green: #059669;
  --amber: #d97706;
  --red: #dc2626;
  --r1: #2563eb;
  --r2: #d97706;
  --r3: #7c3aed;
  --r4: #059669;
  --r5: #db2777;
  --r6: #0891b2;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
.topbar {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar .logo {
  font-size: 19px;
  font-weight: 700;
  color: var(--blue);
  text-decoration: none;
  letter-spacing: -0.3px;
}
.topbar .logo span { color: var(--ink); }
.topbar .spacer { flex: 1; }
.topbar .doc-title { font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  background: var(--blue);
  color: #fff;
  transition: background 0.15s;
}
.btn:hover { background: var(--blue-dark); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.secondary { background: #fff; color: var(--ink); border-color: var(--border); }
.btn.secondary:hover { background: var(--bg); }
.btn.danger { background: #fff; color: var(--red); border-color: var(--border); }
.btn.danger:hover { background: #fef2f2; }
.btn.small { padding: 5px 10px; font-size: 12.5px; }

/* ---------- Status chips ---------- */
.chip {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  text-transform: capitalize;
}
.chip.draft { background: #eef2ff; color: #4338ca; }
.chip.sent { background: #fef3c7; color: #92400e; }
.chip.completed { background: #d1fae5; color: #065f46; }
.chip.voided, .chip.declined { background: #fee2e2; color: #991b1b; }
.chip.pending { background: #f3f4f6; color: var(--muted); }

/* ---------- Layout ---------- */
.container { max-width: 960px; margin: 32px auto; padding: 0 20px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 20px; }

h1 { font-size: 22px; margin-bottom: 4px; }
.sub { color: var(--muted); font-size: 14px; margin-bottom: 20px; }

/* ---------- Dashboard table ---------- */
.env-table { width: 100%; border-collapse: collapse; }
.env-table th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px;
  color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.env-table td { padding: 14px 12px; border-bottom: 1px solid var(--border); font-size: 14px; vertical-align: middle; }
.env-table tr:last-child td { border-bottom: none; }
.env-table tr.rowlink { cursor: pointer; }
.env-table tr.rowlink:hover { background: #f9fafb; }
.env-title { font-weight: 600; }
.env-meta { color: var(--muted); font-size: 12.5px; margin-top: 2px; }

.empty { text-align: center; padding: 48px 20px; color: var(--muted); }

/* ---------- Forms ---------- */
input[type="text"], input[type="email"], textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  background: #fff;
  color: var(--ink);
}
input:focus, textarea:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--blue); }
label.lbl { display: block; font-size: 12.5px; font-weight: 600; color: var(--muted); margin: 12px 0 4px; }

/* ---------- Prepare (editor) layout ---------- */
.editor-layout { display: flex; height: calc(100vh - 56px); }
.sidebar {
  width: 300px; min-width: 300px; background: #fff; border-right: 1px solid var(--border);
  padding: 18px; overflow-y: auto;
}
.sidebar h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); margin: 18px 0 8px; }
.sidebar h3:first-child { margin-top: 0; }

.recip-item {
  display: flex; align-items: center; gap: 8px; padding: 8px; border: 1px solid var(--border);
  border-radius: 6px; margin-bottom: 6px; cursor: pointer; font-size: 13.5px;
}
.recip-item.selected { border-color: var(--blue); background: #eff6ff; }

.doc-item { transition: transform 0.1s, opacity 0.1s; }
.doc-item.dragging { opacity: 0.4; }
.doc-item.drag-over { border-color: var(--blue); border-style: dashed; background: #eff6ff; }
.drag-handle {
  color: #9ca3af; font-size: 14px; line-height: 1; cursor: grab; flex-shrink: 0;
  letter-spacing: -3px; padding-right: 1px;
}
.drag-handle:active { cursor: grabbing; }
.recip-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.recip-info { flex: 1; min-width: 0; }
.recip-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recip-email { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recip-x { color: var(--muted); background: none; border: none; cursor: pointer; font-size: 15px; padding: 2px 6px; }
.recip-x:hover { color: var(--red); }

.seg-toggle {
  display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden;
}
.seg-toggle button {
  flex: 1; border: none; background: #fff; padding: 7px 0; font-size: 13px; font-weight: 600;
  color: var(--muted); cursor: pointer;
}
.seg-toggle button:first-child { border-right: 1px solid var(--border); }
.seg-toggle button.active { background: var(--blue); color: #fff; }

.palette { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.palette button {
  border: 1px solid var(--border); background: #fff; border-radius: 6px; padding: 10px 6px;
  font-size: 13px; font-weight: 600; cursor: pointer; color: var(--ink);
}
.palette button:hover { border-color: var(--blue); color: var(--blue); }
.palette button.armed { background: var(--blue); color: #fff; border-color: var(--blue); }

/* ---------- PDF viewer ---------- */
.pdf-scroll { flex: 1; overflow: auto; padding: 28px 20px; }
.pdf-page {
  position: relative; margin: 0 auto 24px; box-shadow: 0 2px 10px rgba(0,0,0,0.12);
  background: #fff; width: fit-content;
}
.pdf-page canvas { display: block; }
.page-num { text-align: center; color: var(--muted); font-size: 12px; margin: -16px 0 20px; }

/* ---------- Fields ---------- */
.field-box {
  position: absolute; border: 2px solid; border-radius: 3px;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; cursor: move; user-select: none;
  overflow: hidden; white-space: nowrap;
}
.field-box .resize {
  position: absolute; right: -2px; bottom: -2px; width: 14px; height: 14px;
  cursor: nwse-resize; background: inherit; border-top-left-radius: 4px;
  border: 2px solid #fff; box-sizing: border-box; z-index: 2;
}
.field-box .del {
  position: absolute; top: -1px; right: -1px; width: 15px; height: 15px; line-height: 13px;
  text-align: center; cursor: pointer; font-size: 11px; background: rgba(255,255,255,0.85);
  border-bottom-left-radius: 4px; display: none; z-index: 2;
}
.field-box:hover .del { display: block; }
.field-box .req-toggle {
  position: absolute; left: 0; top: 0; font-size: 9px; font-weight: 700;
  padding: 1px 5px 1px 4px; cursor: pointer; line-height: 1.5;
  background: rgba(255,255,255,0.9); border-bottom-right-radius: 4px; white-space: nowrap;
}
.field-box .req-toggle.optional { opacity: 0.6; font-style: italic; }

/* signer-side fields */
.sign-field {
  position: absolute; border: 2px dashed var(--amber); background: rgba(251, 191, 36, 0.18);
  border-radius: 3px; cursor: pointer; display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600; color: #92400e; overflow: hidden;
}
.sign-field.filled { border-style: solid; border-color: var(--green); background: rgba(5, 150, 105, 0.06); color: var(--green); }
.sign-field.optional-field { border-color: var(--muted); background: rgba(107, 114, 128, 0.08); color: var(--muted); }
@keyframes field-jump-pulse {
  0% { box-shadow: 0 0 0 0 rgba(37, 99, 235, 0.55); }
  70% { box-shadow: 0 0 0 12px rgba(37, 99, 235, 0); }
  100% { box-shadow: 0 0 0 0 rgba(37, 99, 235, 0); }
}
.sign-field.jump-highlight { animation: field-jump-pulse 0.8s ease-out 2; }
.sign-field img { max-width: 100%; max-height: 100%; }
.sign-field input[type="text"] { border: none; background: transparent; width: 100%; height: 100%; font-size: inherit; text-align: center; }
.sign-field input[type="text"]:focus { outline: none; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(17, 24, 39, 0.55); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}

.field-ctx-menu {
  position: fixed; z-index: 150; background: #fff; border: 1px solid var(--border);
  border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,0.18); padding: 4px; min-width: 160px;
}
.field-ctx-menu button {
  display: block; width: 100%; text-align: left; background: none; border: none;
  padding: 8px 10px; font-size: 13.5px; color: var(--ink); cursor: pointer; border-radius: 5px;
}
.field-ctx-menu button:hover { background: var(--bg); }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 560px; padding: 24px; max-height: 92vh; overflow-y: auto; }
.modal h2 { font-size: 18px; margin-bottom: 14px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs button {
  background: none; border: none; padding: 10px 18px; font-size: 14px; font-weight: 600;
  color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent;
}
.tabs button.active { color: var(--blue); border-bottom-color: var(--blue); }

.sig-canvas-wrap { border: 1px solid var(--border); border-radius: 8px; background: #fafafa; position: relative; }
.sig-canvas-wrap canvas { display: block; width: 100%; height: 180px; cursor: crosshair; touch-action: none; }
.sig-line { position: absolute; left: 8%; right: 8%; bottom: 38px; border-bottom: 1.5px solid #9ca3af; pointer-events: none; }
.sig-hint { position: absolute; bottom: 14px; left: 0; right: 0; text-align: center; color: #9ca3af; font-size: 12px; pointer-events: none; }
.sig-clear { position: absolute; top: 8px; right: 10px; background: none; border: none; color: var(--blue); font-size: 13px; cursor: pointer; font-weight: 600; }

.type-preview {
  border: 1px solid var(--border); border-radius: 8px; height: 120px; margin-top: 12px;
  display: flex; align-items: center; justify-content: center; font-size: 44px; background: #fafafa;
  font-family: 'Segoe Script', 'Brush Script MT', cursive; color: #111;
}

/* ---------- Signer top bar ---------- */
.sign-progress { font-size: 13.5px; color: var(--muted); }
.sign-progress b { color: var(--ink); }

/* ---------- Detail page ---------- */
.detail-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 800px) { .detail-grid { grid-template-columns: 1fr; } }

.timeline { list-style: none; }
.timeline li { padding: 10px 0 10px 22px; border-left: 2px solid var(--border); position: relative; margin-left: 6px; }
.timeline li::before {
  content: ''; position: absolute; left: -6px; top: 15px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--blue); border: 2px solid #fff;
}
.timeline .t-event { font-weight: 600; font-size: 13.5px; }
.timeline .t-detail { font-size: 12.5px; color: var(--muted); }
.timeline .t-time { font-size: 11.5px; color: #9ca3af; margin-top: 2px; }

.link-row { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.link-row input { font-size: 12px; color: var(--muted); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 20px; border-radius: 8px;
  font-size: 14px; z-index: 200; opacity: 0; transition: opacity 0.25s; pointer-events: none;
}
#toast.show { opacity: 1; }

.center-note { max-width: 480px; margin: 80px auto; text-align: center; }
.center-note .big { font-size: 44px; margin-bottom: 12px; }
.center-note h2 { margin-bottom: 8px; }
.center-note p { color: var(--muted); }
