:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --text: #16181d;
  --muted: #5d6573;
  --border: #dde1e7;
  --accent: #1f4fd1;
  --accent-ink: #ffffff;
  --accent-soft: #e8eefc;
  --ok: #157a3f;
  --ok-soft: #e5f5eb;
  --warn: #8a5a00;
  --warn-soft: #fff4dc;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --page-shadow: 0 1px 3px rgba(16, 24, 40, .12), 0 1px 2px rgba(16, 24, 40, .06);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317; --surface: #1a1d23; --surface-2: #20242b; --text: #e8eaee; --muted: #9aa3b2; --border: #2e333c;
    --accent: #6b93ff; --accent-ink: #0b1020; --accent-soft: #1d2740; --ok: #4ade80; --ok-soft: #12291b;
    --warn: #fbbf24; --warn-soft: #2d2410; --danger: #f87171; --danger-soft: #2f1616;
    --page-shadow: 0 1px 3px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111317; --surface: #1a1d23; --surface-2: #20242b; --text: #e8eaee; --muted: #9aa3b2; --border: #2e333c;
  --accent: #6b93ff; --accent-ink: #0b1020; --accent-soft: #1d2740; --ok: #4ade80; --ok-soft: #12291b;
  --warn: #fbbf24; --warn-soft: #2d2410; --danger: #f87171; --danger-soft: #2f1616;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
h1 { font-size: 1.5rem; line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1.25rem 0 .5rem; }
p { margin: .4rem 0; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.hash { word-break: break-all; }
.error { color: var(--danger); }
.ok { color: var(--ok); }
.nowrap { white-space: nowrap; }
.eyebrow { color: var(--muted); font-size: .85rem; margin: 0 0 .2rem; }

.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .6rem 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar nav { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; font-size: .9rem; }
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 650; color: var(--text); text-decoration: none; }
.brand-mark { width: 22px; height: 22px; border-radius: 6px; background: var(--accent) url(/favicon.svg) center/cover; display: inline-block; }

main.narrow { max-width: 620px; margin: 2rem auto; padding: 0 16px; }
main.page-wrap { max-width: 1040px; margin: 1.5rem auto; padding: 0 16px 4rem; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; }
.card.tight { padding: .75rem; }
.stack { display: flex; flex-direction: column; gap: .75rem; }
.row { display: flex; gap: .5rem; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; }
.row.two > * { flex: 1 1 200px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; }

label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; font-weight: 550; color: var(--muted); margin-bottom: .6rem; }
label.check { flex-direction: row; align-items: center; gap: .5rem; color: var(--text); font-weight: 450; }
label.check.big { font-size: 1rem; margin: 1rem 0; }
label.check input { width: 18px; height: 18px; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: .5rem .65rem; width: 100%; min-width: 0;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }

button, .btn {
  font: inherit; font-weight: 550; display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .5rem .95rem; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; white-space: nowrap; min-height: 38px;
}
button:hover, .btn:hover { background: var(--surface-2); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover, .btn.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
button:focus-visible, .btn:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.link-btn { border: 0; background: none; color: var(--accent); padding: .25rem .2rem; min-height: 0; font-weight: 500; }
.link-btn:hover { background: none; text-decoration: underline; }
.link-btn.danger { color: var(--danger); }
.icon-btn { border: 0; background: none; font-size: 1.4rem; line-height: 1; padding: .2rem .5rem; min-height: 0; color: var(--muted); }

.badge { display: inline-block; padding: .15rem .55rem; border-radius: 99px; font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.badge.s-sent, .badge.s-viewed { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.s-completed, .badge.s-signed { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.s-voided, .badge.s-declined { background: var(--danger-soft); color: var(--danger); border-color: transparent; }

.notice { background: var(--warn-soft); color: var(--warn); padding: .6rem .8rem; border-radius: 8px; }
.notice.error { background: var(--danger-soft); color: var(--danger); }
.note { background: var(--surface-2); border-left: 3px solid var(--accent); margin: .75rem 0; padding: .5rem .8rem; border-radius: 4px; white-space: pre-wrap; }

.login { max-width: 400px; margin: 3rem auto; }
.section-title { margin-top: 2rem; }
.back { display: inline-block; margin-bottom: .75rem; font-size: .9rem; }
.back::before { content: "\2190  "; }

.dropzone { border: 2px dashed var(--border); border-radius: var(--radius); padding: 2rem 1rem; text-align: center; background: var(--surface); }
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }

.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { text-align: left; padding: .6rem .75rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); background: var(--surface-2); }
.table tr:last-child td { border-bottom: 0; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover td { background: var(--surface-2); }
.scroll-x { overflow-x: auto; }

/* Editor */
main.editor { display: grid; grid-template-columns: 330px 1fr; min-height: calc(100vh - 53px); }
.side { background: var(--surface); border-right: 1px solid var(--border); padding: 1rem; display: flex; flex-direction: column; position: sticky; top: 53px; height: calc(100vh - 53px); overflow-y: auto; }
.side-foot { margin-top: auto; padding-top: 1rem; display: flex; flex-direction: column; gap: .5rem; border-top: 1px solid var(--border); }
.signer-card { border: 1px solid var(--border); border-left: 4px solid; border-radius: 8px; padding: .6rem; margin-bottom: .6rem; display: flex; flex-direction: column; gap: .4rem; }
.signer-card.active { background: var(--surface-2); }
.palette-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .4rem; margin: .3rem 0 .4rem; }
.palette-btn { border-left: 4px solid; justify-content: flex-start; }
.palette-btn.on { background: var(--accent-soft); outline: 2px solid var(--accent); }
.selected-box { margin-top: .5rem; }

.doc-area { padding: 16px; min-width: 0; }
.doc-area.placing .page-overlay { cursor: crosshair; }
.pdf-view { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.page { position: relative; background: #fff; box-shadow: var(--page-shadow); flex: none; }
.page-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.page-overlay { position: absolute; inset: 0; }
.page-num { position: absolute; right: -2px; top: -18px; font-size: .72rem; color: var(--muted); }

.field {
  --c: #e5a50a;
  position: absolute; border: 1.5px solid var(--c); background: color-mix(in srgb, var(--c) 18%, transparent);
  border-radius: 3px; touch-action: none; user-select: none; display: flex; align-items: center; overflow: visible; color: #1a1a1a;
  container-type: size;
}
main.editor .field { cursor: move; }
.field.sel { box-shadow: 0 0 0 2px var(--accent); }
.field-label { font-size: clamp(7px, 40cqh, 10px); font-weight: 600; padding: 0 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #1a1a1a; pointer-events: none; }
.field .handle { position: absolute; right: -5px; bottom: -5px; width: 10px; height: 10px; background: var(--c); border: 1.5px solid #fff; border-radius: 2px; cursor: nwse-resize; }
.field-del { position: absolute; top: -10px; right: -10px; width: 20px; height: 20px; min-height: 0; padding: 0; border-radius: 50%; font-size: 13px; line-height: 1; display: none; background: #fff; color: #333; border: 1px solid #bbb; }
.field.sel .field-del, .field:hover .field-del { display: inline-flex; }

/* Signing */
.sign-head { max-width: 1012px; margin: 1rem auto 0; padding: 0 16px; display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.sign-head h1 { margin: 0; }
.doc-area.sign { padding-bottom: 96px; }
.field.mine { --c: #e5a50a; cursor: pointer; }
.field.mine.req::after { content: "*"; position: absolute; top: -4px; right: -9px; color: var(--danger); font-weight: 700; font-size: 14px; }
.field.mine .cta { font-size: clamp(7px, 42cqh, 12px); font-weight: 700; padding: 0 4px; color: #6b4b00; white-space: nowrap; overflow: hidden; }
.field.mine.done { background: rgba(255, 255, 255, .0); border-style: dashed; border-color: color-mix(in srgb, var(--c) 60%, transparent); }
.field.mine.t-text { cursor: text; }
.field.mine.t-date { cursor: default; }
.field.mine input { border: 0; background: transparent; padding: 0 3px; height: 100%; border-radius: 0; color: #111; font-size: clamp(7px, 62cqh, 16px); }
.field.mine input:focus { outline: none; background: rgba(255, 255, 255, .6); }
.field.other { --c: #94a3b8; border-style: dashed; background: transparent; }
.field img { max-width: 100%; max-height: 100%; object-fit: contain; object-position: left center; pointer-events: none; }
.field .val { font-size: clamp(6px, 62cqh, 16px); padding: 0 3px; white-space: nowrap; overflow: hidden; color: #111; }
.field .val.center { width: 100%; text-align: center; font-weight: 700; }
.field.pulse { animation: pulse 1.2s ease-out 2; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(229, 165, 10, .7); } 100% { box-shadow: 0 0 0 12px rgba(229, 165, 10, 0); } }
.signbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; justify-content: space-between; align-items: center; gap: .75rem;
  padding: .7rem 16px calc(.7rem + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(0, 0, 0, .06);
}
.progress-text { font-weight: 550; font-size: .95rem; }
.signbar button.primary:not(.ready) { opacity: .65; }
.consent { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: .8rem 1rem; margin-top: 1rem; font-size: .9rem; max-height: 300px; overflow-y: auto; }
.code-input { font-size: 1.6rem; letter-spacing: .3em; text-align: center; font-family: ui-monospace, Menlo, Consolas, monospace; }
.done-mark { width: 56px; height: 56px; border-radius: 50%; background: var(--ok-soft); color: var(--ok); display: grid; place-items: center; font-size: 30px; font-weight: 700; margin-bottom: .75rem; }

.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); }
.tab { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: .4rem .9rem; }
.tab.on { border-bottom-color: var(--accent); color: var(--accent); }
.typed-preview {
  font-family: "Sheetset Script", "Segoe Script", "Brush Script MT", cursive; font-weight: 600; font-size: 44px; color: #14215a;
  background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: .5rem 1rem; min-height: 86px; display: flex; align-items: center;
  overflow: hidden; white-space: nowrap;
}
.typed-preview.small { font-size: 32px; min-height: 64px; max-width: 220px; }
.draw-wrap { background: #fff; border: 1px solid var(--border); border-radius: 8px; }
.sigpad { display: block; width: 100%; touch-action: none; cursor: crosshair; }

.modal-back { position: fixed; inset: 0; z-index: 100; background: rgba(15, 18, 25, .5); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal { background: var(--surface); border-radius: 12px; width: 100%; max-width: 480px; max-height: calc(100vh - 32px); overflow-y: auto; padding: 1rem 1.25rem 1.25rem; box-shadow: 0 20px 50px rgba(0, 0, 0, .3); }
.modal.wide { max-width: 640px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .5rem; }
.modal-head h2 { margin: 0; }

.toast { position: fixed; left: 50%; bottom: 84px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; z-index: 200;
  background: #1d2433; color: #fff; padding: .6rem 1rem; border-radius: 8px; transition: all .2s; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

.signer-detail .steps { margin: .5rem 0; padding-left: 1.1rem; }
.link-row input { flex: 1; font-size: .78rem; }
.events td { font-size: .82rem; }

.verify-out { margin-top: 1rem; }
.result.good { border-color: var(--ok); background: var(--ok-soft); }
.result.bad { border-color: var(--danger); background: var(--danger-soft); }
.result.warn { border-color: var(--warn); background: var(--warn-soft); }

@media (max-width: 820px) {
  main.editor { grid-template-columns: 1fr; }
  .side { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .doc-area { padding: 12px 8px; }
  .table th:nth-child(4), .table td:nth-child(4) { display: none; }
  h1 { font-size: 1.3rem; }
}
