:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1b1f27;
  --muted: #5d6573;
  --primary: #1f4fd1;
  --primary-ink: #ffffff;
  --primary-soft: #e6edff;
  --danger: #c0352b;
  --danger-soft: #fdecea;
  --ok: #187a46;
  --ok-soft: #e4f5ec;
  --warn: #9a6100;
  --warn-soft: #fff4dc;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1a1e26;
    --surface-2: #222733;
    --border: #2f3542;
    --text: #e8eaef;
    --muted: #9aa3b2;
    --primary: #6b93ff;
    --primary-ink: #0b1020;
    --primary-soft: #1e2a4a;
    --danger: #ff7b70;
    --danger-soft: #3a1d1b;
    --ok: #5fd197;
    --ok-soft: #173326;
    --warn: #f0b955;
    --warn-soft: #3a2d12;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 12px; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
p { margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; word-break: break-all; }
.hidden { display: none !important; }

header.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 0 20px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; font-size: 16px; color: var(--text); text-decoration: none; white-space: nowrap; }
.brand span { color: var(--primary); }
nav.main { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
nav.main a {
  color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 6px; white-space: nowrap;
}
nav.main a.active, nav.main a:hover { color: var(--text); background: var(--surface-2); }
.userbox { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); white-space: nowrap; }

main { max-width: 1200px; margin: 0 auto; padding: 24px 20px 60px; }
.narrow { max-width: 440px; margin: 48px auto; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px;
}
.card + .card { margin-top: 16px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1; }
.stack > * + * { margin-top: 12px; }

label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 4px; }
input[type=text], input[type=email], input[type=password], input[type=search], textarea, select {
  width: 100%; padding: 9px 11px; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
input:focus, textarea:focus, select:focus, button:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
textarea { min-height: 80px; resize: vertical; }
.field { margin-bottom: 14px; }
.check { display: flex; gap: 10px; align-items: flex-start; font-weight: 400; font-size: 14px; }
.check input { margin-top: 3px; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; font: inherit; font-weight: 600; font-size: 14px;
  border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.primary:hover { background: var(--primary); filter: brightness(1.08); }
.danger { color: var(--danger); }
.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.link { border: none; background: none; padding: 0; color: var(--primary); font-weight: 500; }
.link:hover { background: none; text-decoration: underline; }
.sm { padding: 4px 9px; font-size: 13px; }
.block { width: 100%; }

.alert { padding: 10px 12px; border-radius: 8px; font-size: 14px; margin-bottom: 14px; }
.alert.error { background: var(--danger-soft); color: var(--danger); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert.info { background: var(--primary-soft); color: var(--text); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--surface-2); color: var(--muted); text-transform: capitalize;
}
.badge.sent, .badge.viewed { background: var(--primary-soft); color: var(--primary); }
.badge.completed, .badge.signed { background: var(--ok-soft); color: var(--ok); }
.badge.declined, .badge.voided { background: var(--danger-soft); color: var(--danger); }
.badge.pending, .badge.draft { background: var(--warn-soft); color: var(--warn); }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 14px; }
.tabs button { border-radius: 999px; padding: 5px 12px; font-size: 13px; }
.tabs button.active { background: var(--text); color: var(--bg); border-color: var(--text); }

/* Editor */
.editor { display: grid; grid-template-columns: 300px 1fr; gap: 20px; align-items: start; }
.editor aside { position: sticky; top: 76px; max-height: calc(100vh - 96px); overflow: auto; }
.signer-item {
  display: grid; grid-template-columns: 14px 1fr auto; gap: 8px; align-items: center;
  padding: 8px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; cursor: pointer;
}
.signer-item.active { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.swatch { width: 14px; height: 14px; border-radius: 4px; }
.palette { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.palette button.active { border-color: var(--primary); background: var(--primary-soft); }

.pages { display: flex; flex-direction: column; gap: 16px; align-items: center; }
.page-wrap { position: relative; background: #fff; box-shadow: var(--shadow); max-width: 100%; }
.page-wrap canvas { display: block; width: 100%; height: auto; }
.page-wrap .overlay { position: absolute; inset: 0; }
.page-label { font-size: 12px; color: var(--muted); align-self: center; }
.editor .overlay { cursor: crosshair; }

.fbox {
  position: absolute; border: 2px solid; border-radius: 4px; font-size: 11px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  overflow: hidden; user-select: none; touch-action: none; line-height: 1.1;
}
.editor .fbox { cursor: move; }
.fbox .del {
  position: absolute; top: -1px; right: -1px; width: 18px; height: 18px; padding: 0;
  font-size: 12px; line-height: 1; border-radius: 0 3px 0 4px; border: none; background: rgba(0,0,0,0.55); color: #fff;
}
.fbox .rsz { position: absolute; right: 0; bottom: 0; width: 12px; height: 12px; cursor: nwse-resize; background: currentColor; opacity: 0.6; clip-path: polygon(100% 0, 100% 100%, 0 100%); }

/* Signer page */
.sign-field {
  position: absolute; border: 2px dashed var(--primary); background: rgba(31, 79, 209, 0.10);
  border-radius: 4px; display: flex; align-items: center; justify-content: center;
  color: #1f4fd1; font-weight: 700; font-size: 12px; cursor: pointer; overflow: hidden; padding: 0;
}
.sign-field.filled { border-style: solid; background: rgba(24, 122, 70, 0.08); border-color: #187a46; }
.sign-field img { max-width: 100%; max-height: 100%; }
.sign-field input {
  width: 100%; height: 100%; border: none; background: transparent; color: #0d1a4d; padding: 0 4px;
  font-size: 12px; border-radius: 0;
}
.sign-field.static { cursor: default; color: #0d1a4d; font-weight: 500; }
.sign-bar {
  position: sticky; bottom: 0; background: var(--surface); border-top: 1px solid var(--border);
  padding: 12px 20px; z-index: 10; box-shadow: 0 -2px 8px rgba(0,0,0,0.05);
}

/* Modal */
.modal-bg {
  position: fixed; inset: 0; background: rgba(10, 14, 22, 0.55); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal { background: var(--surface); border-radius: 12px; max-width: 560px; width: 100%; max-height: 90vh; overflow: auto; padding: 22px; box-shadow: 0 10px 40px rgba(0,0,0,0.3); }
.sigpad { width: 100%; height: 180px; border: 1px solid var(--border); border-radius: 8px; background: #fff; touch-action: none; display: block; }
.typed-sig { font-family: "Segoe Script", "Brush Script MT", "Lucida Handwriting", cursive; font-size: 34px; color: #10204a; }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13.5px; display: grid; grid-template-columns: 170px 1fr; gap: 12px; }
.timeline li:last-child { border-bottom: none; }

.linkbox { display: flex; gap: 8px; align-items: center; }
.linkbox input { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }

.qr { background: #fff; padding: 10px; border-radius: 8px; display: inline-block; }
.qr img, .qr svg { display: block; width: 180px; height: 180px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 14px;
}

@media (max-width: 860px) {
  .editor { grid-template-columns: 1fr; }
  .editor aside { position: static; max-height: none; }
  .userbox .who { display: none; }
  .timeline li { grid-template-columns: 1fr; gap: 2px; }
}
@media (max-width: 600px) {
  main { padding: 16px 16px 60px; }
  header.topbar { padding: 0 12px; gap: 10px; }
  .card { padding: 16px; }
  .hide-sm { display: none; }
}
