:root {
  --bg: #f5f6fa;
  --card: #ffffff;
  --border: #e3e6ef;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #4f46e5;
  --primary-d: #4338ca;
  --danger: #dc2626;
  --ok: #16a34a;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

a { color: var(--primary); text-decoration: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.brand { font-weight: 700; font-size: 18px; color: var(--primary); margin-bottom: 6px; }

/* top bar (sign page) */
.topbar {
  display: flex; align-items: center; gap: 16px;
  background: var(--card); border-bottom: 1px solid var(--border);
  padding: 12px 20px; position: sticky; top: 0; z-index: 50;
}
.doc-title { font-weight: 600; }

/* cards */
.center-card {
  max-width: 560px; margin: 40px auto; background: var(--card);
  border: 1px solid var(--border); border-radius: 12px; padding: 28px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.center-card.wide { max-width: 760px; }
.center-card h1 { margin: 0 0 4px; font-size: 22px; }
.center-card label { display: block; margin: 16px 0 6px; font-weight: 600; font-size: 14px; }
.center-card input[type=text],
.center-card textarea,
.center-card input[type=file] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; font-family: inherit; background: #fff;
}
.center-card textarea { resize: vertical; }

.result { margin-top: 20px; padding: 16px; border: 1px solid var(--border); border-radius: 10px; background: #fafafa; }
.result ul { padding-left: 20px; margin: 8px 0; }
.result li { margin: 6px 0; word-break: break-all; }

/* buttons */
.btn {
  display: inline-block; padding: 10px 16px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff; color: var(--text);
  font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn:hover { background: #f3f4f6; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-d); }
.btn-block { width: 100%; margin-top: 8px; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.link-btn { background: none; border: none; color: var(--primary); cursor: pointer; font-size: 13px; padding: 4px 0; }

/* sign layout */
.sign-body { display: flex; flex-direction: column; min-height: 100vh; }
.sign-layout { display: flex; flex: 1; }
.sign-panel {
  width: 380px; min-width: 380px; background: var(--card);
  border-right: 1px solid var(--border); padding: 20px; overflow-y: auto;
  position: sticky; top: 57px; height: calc(100vh - 57px);
}
.pages-area { flex: 1; padding: 24px; display: flex; flex-direction: column; align-items: center; gap: 20px; }
@media (max-width: 900px) {
  .sign-layout { flex-direction: column; }
  .sign-panel { width: 100%; min-width: 0; position: static; height: auto; border-right: none; border-bottom: 1px solid var(--border); }
}

.panel-block { margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid var(--border); }
.panel-block:last-child { border-bottom: none; }
.panel-block h3 { margin: 0 0 10px; font-size: 15px; }
.panel-block label { display: block; margin-bottom: 6px; font-weight: 600; font-size: 13px; }
.panel-block input[type=text] { width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }

/* tabs */
.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tab { flex: 1; padding: 8px; border: 1px solid var(--border); background: #fff; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600; }
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.tab-pane { padding: 4px 0; }
#sig-pad { width: 100%; border: 1px dashed var(--border); border-radius: 8px; background: #fff; touch-action: none; }
#type-text { width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; font-size: 16px; margin-bottom: 8px; }
#sig-file { width: 100%; font-size: 13px; }
.sig-preview-wrap { margin-top: 12px; }
.sig-preview {
  height: 70px; border: 1px solid var(--border); border-radius: 8px; background: #fff;
  background-size: contain; background-repeat: no-repeat; background-position: center;
  display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 12px;
}

/* toolbar */
.toolbar { display: flex; flex-direction: column; gap: 8px; }
.tool { padding: 9px; border: 1px solid var(--border); background: #fff; border-radius: 8px; cursor: pointer; font-size: 14px; text-align: left; font-weight: 600; }
.tool.active { border-color: var(--primary); background: #eef2ff; color: var(--primary); }

/* pages + overlays */
.page-wrap { position: relative; margin-bottom: 20px; box-shadow: 0 1px 6px rgba(0,0,0,.15); border: 1px solid var(--border); line-height: 0; background: #fff; }
.page-wrap canvas { display: block; }
.pages-area.place { cursor: crosshair; }

.overlay { position: absolute; }
.overlay.sign { border: 2px dashed var(--primary); background: rgba(79,70,229,.06); border-radius: 4px; cursor: move; }
.overlay.text { border: 2px dashed var(--ok); background: rgba(22,163,74,.06); border-radius: 4px; cursor: move; }
.overlay-del {
  position: absolute; top: -12px; right: -12px; width: 22px; height: 22px;
  border-radius: 50%; border: none; background: var(--danger); color: #fff;
  font-size: 13px; line-height: 1; cursor: pointer; padding: 0; z-index: 2;
}
.sig-inner {
  width: 100%; height: 100%; background-size: contain; background-repeat: no-repeat;
  background-position: center; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 12px; pointer-events: none;
}
.text-inner {
  width: 100%; height: 100%; border: none; background: transparent; resize: none;
  font-size: 14px; padding: 2px 4px; outline: none; color: var(--text); font-family: inherit; overflow: hidden;
}

/* signed card */
.signed-card { max-width: 480px; margin: 60px auto; background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 32px; text-align: center; }
.signed-card .btn { display: inline-block; margin-top: 16px; }

/* status rows */
.signer-row {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 14px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 10px; flex-wrap: wrap;
}
.signer-row.signed { border-color: #b7e4c7; background: #f0fdf4; }
.signer-email { font-weight: 600; word-break: break-all; }
.signer-badge { font-size: 13px; color: var(--muted); margin-top: 2px; }
.signer-row.signed .signer-badge { color: var(--ok); }
.signer-actions { display: flex; gap: 8px; align-items: center; }
.signer-actions input { width: 220px; padding: 7px 9px; border: 1px solid var(--border); border-radius: 8px; font-size: 12px; }
.actions { margin-top: 18px; display: flex; gap: 10px; flex-wrap: wrap; }

/* toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 100;
}

/* selects + document type toggle */
.center-card select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; font-family: inherit; background: #fff;
}
.doc-type-toggle {
  display: flex; gap: 18px; margin: 10px 0 16px; padding: 10px 14px;
  background: #fafafa; border: 1px solid var(--border); border-radius: 10px;
}
.doc-type-toggle label {
  display: flex; align-items: center; gap: 6px; font-weight: 600;
  font-size: 14px; margin: 0; cursor: pointer;
}

/* save-as radio row */
.save-as {
  display: flex; gap: 16px; flex-wrap: wrap; margin: 10px 0 14px;
  padding: 10px 14px; background: #fafafa; border: 1px solid var(--border); border-radius: 10px;
}
.save-as label {
  display: flex; align-items: center; gap: 6px; font-weight: 600;
  font-size: 13px; margin: 0; cursor: pointer;
}

/* document library */
.library-wrap { max-width: 900px; margin: 24px auto; padding: 0 16px; }
.lib-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.lib-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#tabs { margin-bottom: 16px; }
.lib-list { display: flex; flex-direction: column; gap: 10px; }
.doc-card {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; flex-wrap: wrap;
}
.doc-card-title { font-weight: 600; margin-bottom: 2px; word-break: break-word; }
.doc-card-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.badge {
  display: inline-block; background: #eef2ff; color: var(--primary); border-radius: 6px;
  padding: 1px 8px; font-size: 11px; font-weight: 600; margin-left: 6px; vertical-align: middle;
}
.empty { color: var(--muted); text-align: center; padding: 30px; }

/* modals */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; z-index: 200; padding: 16px;
}
.modal {
  background: #fff; border-radius: 12px; padding: 22px; width: 100%; max-width: 480px;
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
.modal h2 { margin: 0 0 6px; font-size: 18px; }
.modal label { display: block; margin: 14px 0 6px; font-weight: 600; font-size: 13px; }
.modal input[type=text], .modal textarea, .modal input[type=file] {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; font-family: inherit;
}
.modal textarea { resize: vertical; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

/* contract field positioning */
.contract-pages { margin-top: 14px; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.contract-field {
  border: 1px dashed #0ea5e9 !important;
  background: rgba(14,165,233,.08) !important;
  color: #0c4a6e;
  font-size: 12px;
  padding: 2px 7px;
  border-radius: 4px;
  white-space: nowrap;
  cursor: move;
  width: auto !important;
  min-width: 90px;
}

/* fillable field overlays (signer page) */
.overlay.field { border: 1px dashed #0ea5e9; background: rgba(14,165,233,.08); border-radius: 4px; cursor: move; }
.overlay.field.date { min-width: 180px; }
.field-input {
  width: 100%; height: 100%; border: none; background: transparent;
  font-size: 13px; padding: 1px 4px; outline: none; font-family: inherit;
  color: #0c4a6e; box-sizing: border-box; cursor: text;
}
.checkbox-field { display: flex; align-items: center; gap: 6px; padding: 1px 4px; }
.checkbox-box {
  width: 16px; height: 16px; border: 1.5px solid #0c4a6e; border-radius: 3px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; color: #0c4a6e; flex: 0 0 auto;
}
.checkbox-label { font-size: 13px; color: #0c4a6e; white-space: nowrap; }

/* positioning toolbar (create page) */
.pos-toolbar { margin: 10px 0; padding: 10px 12px; background: #fafafa; border: 1px solid var(--border); border-radius: 10px; }
.pos-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 6px 0; }
.pos-row label { display: flex; align-items: center; gap: 4px; font-weight: 600; font-size: 13px; margin: 0; cursor: pointer; }
.pos-row input[type=text] { flex: 1; min-width: 140px; padding: 6px 9px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; }
