:root {
  --bg: #e9ebef;
  --panel: #ffffff;
  --text: #1a1d23;
  --muted: #667085;
  --line: #d9dce3;
  --hover: #f1f3f6;
  --accent: #2563eb;
  --accent-soft: #e6eefe;
  --accent-text: #ffffff;
  --danger: #c0262d;
  --field: rgba(37, 99, 235, 0.08);
  --field-line: rgba(37, 99, 235, 0.45);
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .08);
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b;
    --panel: #1f2228;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --line: #333841;
    --hover: #2a2e36;
    --accent: #4d84f5;
    --accent-soft: #23314f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 15px; margin-right: 4px; color: var(--accent); }
.brand span { color: var(--text); }
.brand svg { width: 24px; height: 24px; stroke: none; }
.brand svg path { stroke: #fff; }
.group { display: flex; align-items: center; gap: 4px; }
.group.end { margin-left: auto; gap: 12px; }
.tools { background: var(--hover); padding: 3px; border-radius: 10px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--panel); cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 550; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.tool {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 10px;
  border: 0; border-radius: 7px; background: transparent; cursor: pointer;
}
.tool:hover { background: var(--panel); }
.tool.active { background: var(--panel); color: var(--accent); box-shadow: var(--shadow); font-weight: 550; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; border: 0; border-radius: 7px;
  background: transparent; cursor: pointer;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn.danger { color: var(--danger); }
.zoom-label { min-width: 54px; height: 32px; border: 0; background: transparent; cursor: pointer; border-radius: 7px; font-variant-numeric: tabular-nums; }
.zoom-label:hover { background: var(--hover); }

.date-fmt select {
  height: 34px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--panel); color: var(--text); padding: 0 8px; font: inherit;
}
.flatten { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); cursor: pointer; user-select: none; }

/* ---------- Context bar ---------- */
.context-bar {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 6px;
  padding: 6px 8px 6px 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); z-index: 30;
}
.ctx-label { font-weight: 600; margin-right: 6px; }
.ctx-text { display: flex; align-items: center; gap: 2px; }
.ctx-text span { min-width: 44px; text-align: center; font-variant-numeric: tabular-nums; color: var(--muted); }
.swatches { display: flex; gap: 6px; padding: 0 6px; }
.swatch {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--panel);
  background: var(--c); cursor: pointer; box-shadow: 0 0 0 1px var(--line);
}
.swatch.active { box-shadow: 0 0 0 2px var(--accent); }

/* ---------- Viewer ---------- */
.viewer { flex: 1; overflow: auto; position: relative; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 20px; padding: 24px 16px 120px; width: max-content; min-width: 100%; }
.page {
  position: relative; background: #fff; box-shadow: var(--shadow);
  flex: none;
}
.page canvas.render { display: block; width: 100%; height: 100%; }
.page .layer { position: absolute; inset: 0; }
.page-num { position: absolute; top: 100%; right: 0; margin-top: 4px; font-size: 12px; color: var(--muted); }
.viewer.placing .layer { cursor: crosshair; }

/* Empty state */
.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; }
.drop {
  width: min(520px, 100%); padding: 48px 24px; text-align: center;
  border: 2px dashed var(--line); border-radius: 16px; background: var(--panel);
}
.drop svg { width: 56px; height: 56px; color: var(--accent); stroke-width: 1.6; }
.drop h1 { font-size: 22px; margin: 12px 0 6px; }
.drop p { margin: 6px 0; }
.drop.over, .viewer.over .drop { border-color: var(--accent); background: var(--accent-soft); }
.link { border: 0; background: none; color: var(--accent); padding: 0; cursor: pointer; text-decoration: underline; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* ---------- Form fields (AcroForm) ---------- */
.field {
  position: absolute; margin: 0; padding: 0 2px;
  border: 1px solid var(--field-line); border-radius: 2px;
  background: var(--field); color: #000;
  font-family: Helvetica, Arial, sans-serif;
  outline: none;
}
.field:focus { border-color: var(--accent); background: rgba(37, 99, 235, 0.14); box-shadow: 0 0 0 2px rgba(37,99,235,.25); }
textarea.field { resize: none; line-height: 1.15; padding-top: 1px; }
select.field { padding: 0; }
.field.check { appearance: none; -webkit-appearance: none; cursor: pointer; display: grid; place-items: center; }
.field.check.radio { border-radius: 50%; }
.field.check:checked {
  background: var(--field) center / 80% no-repeat url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M4 12.5l5 5L20 6.5' fill='none' stroke='black' stroke-width='3.2' stroke-linecap='round' stroke-linejoin='round'/></svg>");
}
.field.check.radio:checked {
  background: var(--field) center / 55% no-repeat url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><circle cx='12' cy='12' r='11' fill='black'/></svg>");
}
.field[readonly] { background: transparent; border-style: dashed; }

/* ---------- Placed items ---------- */
.item {
  position: absolute; cursor: move; touch-action: none;
  outline: 1px dashed transparent; outline-offset: 1px;
}
.item:hover { outline-color: rgba(37, 99, 235, .5); }
.item.selected { outline: 1.5px solid var(--accent); }
.item img { display: block; width: 100%; height: 100%; pointer-events: none; user-select: none; -webkit-user-drag: none; }
.item .txt {
  font-family: Helvetica, Arial, sans-serif; line-height: 1.2; white-space: pre;
  min-width: 4px; outline: none; cursor: inherit;
}
.item.editing { cursor: text; }
.item.editing .txt { cursor: text; }
.item .txt:empty::before { content: "Type here"; color: #9aa1ad; }
.handle {
  position: absolute; right: -6px; bottom: -6px; width: 12px; height: 12px;
  background: #fff; border: 2px solid var(--accent); border-radius: 50%;
  cursor: nwse-resize; display: none; touch-action: none;
}
.item.selected .handle { display: block; }

/* ---------- Hint + toast ---------- */
.hint, .toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  padding: 8px 14px; border-radius: 999px; z-index: 40;
  font-size: 13px; box-shadow: var(--shadow); pointer-events: none;
}
.hint { top: 64px; background: var(--text); color: var(--panel); }
.toast { bottom: 76px; background: var(--text); color: var(--panel); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- Signature dialog ---------- */
.sig-dialog {
  width: min(600px, calc(100vw - 32px)); padding: 0; border: 0; border-radius: 14px;
  background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.sig-dialog::backdrop { background: rgba(15, 18, 25, .45); }
.sig-inner { padding: 18px 20px 16px; display: flex; flex-direction: column; gap: 14px; }
.sig-inner header { display: flex; align-items: center; justify-content: space-between; }
.sig-inner h2 { font-size: 17px; margin: 0; }
.sig-inner footer { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }
.remember { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; cursor: pointer; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: none; padding: 8px 12px; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; color: var(--muted); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 550; }

.pad-wrap { position: relative; height: 200px; border: 1px solid var(--line); border-radius: 10px; background: #fff; overflow: hidden; }
#sigPad { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
.pad-line { position: absolute; left: 24px; right: 24px; bottom: 48px; border-bottom: 1px solid #c9ced8; pointer-events: none; }
.pad-placeholder { position: absolute; left: 28px; bottom: 54px; color: #b6bcc7; pointer-events: none; font-size: 13px; }
.pad-tools { display: flex; align-items: center; gap: 4px; }
.pad-tools .swatches { padding-left: 0; margin-right: auto; }

.sig-text {
  width: 100%; height: 42px; padding: 0 12px; font: inherit; font-size: 16px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--text);
}
.sig-text:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.font-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; max-height: 260px; overflow: auto; }
.font-opt {
  height: 72px; border: 1px solid var(--line); border-radius: 10px; background: #fff; color: #111;
  font-size: 30px; cursor: pointer; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; padding: 0 10px;
}
.font-opt.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

.upload-box {
  display: grid; place-items: center; gap: 8px; min-height: 160px; padding: 16px;
  border: 2px dashed var(--line); border-radius: 10px; cursor: pointer; color: var(--muted); background: var(--hover);
}
.upload-box img { max-width: 100%; max-height: 140px; background: #fff; border-radius: 6px; }

.saved { display: flex; flex-direction: column; gap: 6px; }
.saved-label { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.saved-list { display: flex; gap: 8px; flex-wrap: wrap; }
.saved-item { position: relative; }
.saved-item > button.use {
  height: 64px; min-width: 120px; max-width: 220px; padding: 6px 10px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; cursor: pointer;
}
.saved-item > button.use:hover { border-color: var(--accent); }
.saved-item img { max-height: 50px; max-width: 200px; display: block; }
.saved-item .del {
  position: absolute; top: -7px; right: -7px; width: 20px; height: 20px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; display: grid; place-items: center; padding: 0;
}
.saved-item .del svg { width: 12px; height: 12px; }

/* ---------- Small screens ---------- */
@media (max-width: 900px) {
  .tool span { display: none; }
  .brand span { display: none; }
}
@media (max-width: 640px) {
  .toolbar { gap: 8px; padding: 8px; }
  .group.end { margin-left: 0; }
  .date-fmt { display: none; }
  .font-list { grid-template-columns: 1fr; }
}

/* ---------- Additions ---------- */
.item svg { display: block; width: 100%; height: 100%; stroke-width: 2.6; }
.handle { width: calc(12px * var(--inv, 1)); height: calc(12px * var(--inv, 1));
  right: calc(-6px * var(--inv, 1)); bottom: calc(-6px * var(--inv, 1)); border-width: calc(2px * var(--inv, 1)); }
.item.selected { outline-width: calc(1.5px * var(--inv, 1)); }
.viewer.placing .field, .viewer.placing .item { pointer-events: none; }
.field.sigfield {
  cursor: pointer; font-size: 9px; color: var(--accent); background: rgba(250, 204, 21, .18);
  border: 1px dashed rgba(202, 138, 4, .8); display: grid; place-items: center; white-space: nowrap; overflow: hidden;
}
.field.sigfield:hover { background: rgba(250, 204, 21, .32); }
