:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-strong: #d1d5db;
  --accent: #111827;
  --accent-ink: #ffffff;
  --ok: #047857;
  --ok-bg: #ecfdf5;
  --warn: #b45309;
  --warn-bg: #fffbeb;
  --err: #b91c1c;
  --err-bg: #fef2f2;
  --info: #1d4ed8;
  --info-bg: #eff6ff;
  --customer: #2563eb;
  --provider: #7c3aed;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { font-family: var(--font); color: var(--ink); background: var(--bg); font-size: 15px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
a { color: var(--ink); }
h1 { font-size: 24px; margin: 0 0 6px 0; letter-spacing: -0.3px; }
h2 { font-size: 18px; margin: 0 0 10px 0; }
h3 { font-size: 15px; margin: 0 0 8px 0; }
p { margin: 0 0 10px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); font-size: 12px; word-break: break-all; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }

.topbar { background: var(--panel); border-bottom: 1px solid var(--line); }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 0 20px; display: flex; align-items: center; gap: 28px; height: 56px; }
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; font-weight: 700; font-size: 16px; }
.brand img { max-height: 30px; max-width: 150px; }
.brand-sub { color: var(--muted); font-weight: 500; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a { text-decoration: none; padding: 6px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; }
.nav a.active, .nav a:hover { background: var(--bg); color: var(--ink); }
.user { display: flex; align-items: center; gap: 12px; font-size: 14px; color: var(--muted); }
.main { max-width: 1100px; margin: 0 auto; padding: 28px 20px 60px; }
.main.wide { max-width: 1400px; }

.flash { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 18px; border: 1px solid; }
.flash.ok { background: var(--ok-bg); border-color: #a7f3d0; color: var(--ok); }
.flash.err { background: var(--err-bg); border-color: #fecaca; color: var(--err); }
.flash.warn { background: var(--warn-bg); border-color: #fde68a; color: var(--warn); }
.flash.info { background: var(--info-bg); border-color: #bfdbfe; color: var(--info); }
.flash ul { margin: 0; padding-left: 18px; }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 18px; }
.panel h2 { margin-bottom: 14px; }
.grid { display: grid; gap: 18px; }
.grid.two { grid-template-columns: 1fr 1fr; }
.grid.three { grid-template-columns: repeat(3, 1fr); }
.grid.side { grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); }
@media (max-width: 860px) { .grid.two, .grid.three, .grid.side { grid-template-columns: 1fr; } .topbar-inner { gap: 12px; } .nav { overflow-x: auto; } .user-name { display: none; } }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; text-decoration: none; }
.stat b { font-size: 24px; display: block; }
.stat span { color: var(--muted); font-size: 13px; }
.stat.hot { border-color: #c4b5fd; background: #f5f3ff; }
@media (max-width: 860px) { .stats { grid-template-columns: 1fr 1fr; } }

.btn { display: inline-flex; align-items: center; gap: 6px; background: var(--accent); color: var(--accent-ink); border: 1px solid var(--accent); padding: 9px 16px; border-radius: 8px; font-weight: 600; font-size: 14px; cursor: pointer; text-decoration: none; line-height: 1.2; font-family: var(--font); }
.btn:hover { opacity: .92; }
.btn.secondary { background: var(--panel); color: var(--ink); border-color: var(--line-strong); }
.btn.danger { background: var(--err-bg); color: var(--err); border-color: #fecaca; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.big { padding: 13px 22px; font-size: 16px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
button.link { background: none; border: 0; padding: 0; color: var(--muted); cursor: pointer; font: inherit; text-decoration: underline; }

form .field { margin-bottom: 14px; }
label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; color: #374151; }
label .opt { font-weight: 400; color: var(--muted); }
input[type=text], input[type=email], input[type=number], input[type=date], input[type=tel], input[type=file], select, textarea {
  width: 100%; font: inherit; padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: 8px; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #93c5fd; outline-offset: 0; border-color: #60a5fa; }
textarea { min-height: 90px; resize: vertical; }
.check { display: flex; gap: 10px; align-items: flex-start; font-weight: 400; font-size: 14px; }
.check input { margin-top: 4px; width: 16px; height: 16px; }
.help { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.inline-form { display: inline; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

table.list { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
table.list th, table.list td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--line); vertical-align: top; font-size: 14px; }
table.list th { background: #f9fafb; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); font-weight: 600; }
table.list tr:last-child td { border-bottom: 0; }
table.list tr.clickable:hover td { background: #fafafa; }
table.list a.title { font-weight: 600; text-decoration: none; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.draft { background: #f3f4f6; color: #374151; }
.badge.sent { background: var(--info-bg); color: var(--info); }
.badge.customer_signed { background: #f5f3ff; color: var(--provider); }
.badge.completed { background: var(--ok-bg); color: var(--ok); }
.badge.declined, .badge.voided { background: var(--err-bg); color: var(--err); }
.badge.expired { background: var(--warn-bg); color: var(--warn); }
.badge.role-customer { background: #dbeafe; color: var(--customer); }
.badge.role-provider { background: #ede9fe; color: var(--provider); }

.filters { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.filters input[type=text] { width: 280px; }
.pill { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line-strong); text-decoration: none; font-size: 13px; background: #fff; }
.pill.active { background: var(--ink); color: #fff; border-color: var(--ink); }

.pagination { display: flex; gap: 6px; margin-top: 14px; }
.pagination a, .pagination span { padding: 6px 10px; border: 1px solid var(--line); border-radius: 6px; text-decoration: none; font-size: 13px; background: #fff; }
.pagination .active span { background: var(--ink); color: #fff; }

dl.kv { display: grid; grid-template-columns: 150px 1fr; gap: 6px 14px; margin: 0; font-size: 14px; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13.5px; display: grid; grid-template-columns: 150px 1fr; gap: 10px; }
.timeline li:last-child { border-bottom: 0; }
.timeline .when { color: var(--muted); white-space: nowrap; }
.timeline .meta { color: var(--muted); font-size: 12.5px; }

.auth-wrap { max-width: 420px; margin: 60px auto; }
.auth-wrap .panel { padding: 28px; }

.choice-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.choice-card { border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; background: #fff; }
.choice-card h3 { margin-bottom: 4px; }

.sections .section { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 12px; background: #fff; }
.sections .section-head { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #fafafa; border-radius: var(--radius) var(--radius) 0 0; }
.sections .section-head input[type=text] { flex: 1; font-weight: 600; }
.sections .section-body { padding: 0; }
.ql-container { font-family: var(--font); font-size: 14px; min-height: 140px; border: 0 !important; }
.ql-toolbar { border: 0 !important; border-bottom: 1px solid var(--line) !important; background: #fff; }
.ql-editor { min-height: 140px; }

.repeater table { width: 100%; border-collapse: collapse; }
.repeater th, .repeater td { padding: 4px 4px; vertical-align: top; }
.repeater th { font-size: 12px; color: var(--muted); text-align: left; font-weight: 600; }
.repeater input { padding: 7px 9px; }
.repeater .num input { text-align: right; }
.repeater .del { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 18px; line-height: 1; padding: 6px; }

.vars { display: flex; flex-wrap: wrap; gap: 6px; }
.vars code { background: #f3f4f6; border: 1px solid var(--line); padding: 2px 6px; border-radius: 6px; font-size: 12px; }

.sticky-actions { position: sticky; bottom: 0; background: rgba(244,245,247,.92); backdrop-filter: blur(6px); padding: 12px 0; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; margin-top: 10px; }

/* ---------- PDF viewer / field overlay (shared by placer and signer) ---------- */
.viewer-wrap { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 18px; align-items: start; }
@media (max-width: 900px) { .viewer-wrap { grid-template-columns: 1fr; } .viewer-side { position: static !important; } }
.viewer { background: #d9dbe0; border: 1px solid var(--line-strong); border-radius: var(--radius); padding: 16px; overflow: auto; max-height: calc(100vh - 140px); }
.viewer-side { position: sticky; top: 16px; }
.pdf-page { position: relative; margin: 0 auto 16px auto; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.25); }
.pdf-page canvas { display: block; width: 100%; height: auto; }
.pdf-page .page-num { position: absolute; top: -14px; left: 0; font-size: 11px; color: #555; }
.fld { position: absolute; border: 1.5px dashed; border-radius: 3px; font-size: 11px; display: flex; align-items: center; overflow: hidden; cursor: pointer; background: rgba(255,255,255,.05); }
.fld.role-customer { border-color: var(--customer); background: rgba(37,99,235,.09); color: var(--customer); }
.fld.role-provider { border-color: var(--provider); background: rgba(124,58,237,.09); color: var(--provider); }
.fld .tag { position: absolute; top: 0; left: 0; font-size: 9px; padding: 1px 4px; background: inherit; opacity: .9; font-weight: 700; text-transform: uppercase; letter-spacing: .3px; pointer-events: none; }
.fld.selected { border-style: solid; box-shadow: 0 0 0 2px rgba(0,0,0,.15); }
.fld .handle { position: absolute; right: -5px; bottom: -5px; width: 12px; height: 12px; background: #fff; border: 1.5px solid currentColor; border-radius: 2px; cursor: nwse-resize; }
.fld.mine { cursor: pointer; }
.fld.mine.pending { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(37,99,235,.35);} 50% { box-shadow: 0 0 0 5px rgba(37,99,235,0);} }
.fld.done { border-style: solid; background: rgba(255,255,255,.0); }
.fld.other { opacity: .55; cursor: default; border-style: dotted; }
.fld img.sig { max-width: 100%; max-height: 100%; object-fit: contain; }
.fld .val { padding: 0 4px; white-space: nowrap; color: #111; font-size: 12px; }
.fld input.inline { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: transparent; font-size: 12px; padding: 0 4px; outline: none; color: #111; font-family: var(--font); }
.fld.type-checkbox { justify-content: center; font-size: 14px; }
.palette button { display: flex; width: 100%; align-items: center; gap: 8px; margin-bottom: 6px; justify-content: flex-start; }
.swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.swatch.customer { background: var(--customer); } .swatch.provider { background: var(--provider); }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.progress { font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.progress b { color: var(--ink); }

/* ---------- Signing page ---------- */
body.sign { background: #eef0f3; }
.sign-top { background: #fff; border-bottom: 1px solid var(--line); }
.sign-top-inner { max-width: 1280px; margin: 0 auto; padding: 12px 20px; display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.sign-main { max-width: 1280px; margin: 0 auto; padding: 18px 20px 120px; }
.consent { background: var(--info-bg); border: 1px solid #bfdbfe; color: #1e3a8a; padding: 12px 16px; border-radius: var(--radius); margin-bottom: 14px; font-size: 14px; }
.sign-bar { position: fixed; bottom: 0; left: 0; right: 0; background: #fff; border-top: 1px solid var(--line); padding: 12px 20px; display: flex; gap: 12px; align-items: center; justify-content: center; flex-wrap: wrap; z-index: 50; box-shadow: 0 -2px 10px rgba(0,0,0,.05); }
.modal-bg { position: fixed; inset: 0; background: rgba(17,24,39,.55); display: none; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.modal-bg.open { display: flex; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 560px; padding: 22px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal h2 { margin-bottom: 6px; }
.tabs { display: flex; gap: 6px; margin: 12px 0; }
.tabs button { flex: 1; padding: 8px; border: 1px solid var(--line-strong); background: #fff; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 600; }
.tabs button.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.sigpad { width: 100%; height: 180px; border: 1px dashed var(--line-strong); border-radius: 8px; background: #fff; touch-action: none; }
.sigpad-wrap { position: relative; }
.sigpad-line { position: absolute; left: 20px; right: 20px; bottom: 44px; border-top: 1px solid #cbd5e1; pointer-events: none; }
.typed-preview { height: 120px; display: flex; align-items: center; justify-content: center; border: 1px dashed var(--line-strong); border-radius: 8px; font-family: "Homemade Apple", "Brush Script MT", "Segoe Script", "Snell Roundhand", cursive; font-size: 40px; color: #111; overflow: hidden; white-space: nowrap; }
.modal-actions { display: flex; justify-content: space-between; gap: 10px; margin-top: 14px; align-items: center; }
.done-card { max-width: 560px; margin: 60px auto; text-align: center; }
.done-card .big-check { font-size: 48px; color: var(--ok); }
