:root {
  --bg: #e9ebef;
  --panel: #ffffff;
  --ink: #1b1f27;
  --muted: #677084;
  --line: #d9dde5;
  --accent: #1d3a8a;
  --accent-2: #2a4fb8;
  --danger: #b4232c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 6px 20px rgba(16, 24, 40, .08);
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; background: var(--bg); overflow: hidden; }
[hidden] { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .45em;
  height: 38px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  font: inherit; font-size: 14px; font-weight: 500; cursor: pointer; white-space: nowrap;
  user-select: none; transition: background .12s, border-color .12s, box-shadow .12s;
}
.btn:hover { background: #f4f6f9; border-color: #c8cdd8; }
.btn:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-2); border-color: var(--accent-2); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: rgba(0,0,0,.05); }
.btn.icon { width: 38px; padding: 0; font-size: 20px; line-height: 1; }
.btn.small { height: 30px; padding: 0 10px; font-size: 13px; }
.btn.big { height: 48px; padding: 0 22px; font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.spacer { flex: 1; }
.sm { display: none; }

/* ---------- Top bar & tray ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--accent); margin-right: 6px; }
.brand svg { width: 26px; height: 26px; }
.brand span { color: var(--ink); }
.group { display: flex; align-items: center; gap: 6px; }
.filename { font-size: 13px; color: var(--muted); max-width: 32ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.zoom .btn:not(.icon) { min-width: 62px; }

.tray {
  display: flex; align-items: center; gap: 10px; padding: 8px 14px;
  background: #f7f8fa; border-bottom: 1px solid var(--line); overflow-x: auto; min-height: 62px;
}
.tray-list { display: flex; gap: 8px; }
.tray-hint { font-size: 13px; color: var(--muted); }
.sig-thumb {
  position: relative; height: 46px; min-width: 70px; max-width: 160px; padding: 4px 8px;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.sig-thumb:hover { border-color: var(--accent-2); }
.sig-thumb.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(29,58,138,.25); }
.sig-thumb img { max-height: 36px; max-width: 140px; display: block; pointer-events: none; }
.sig-thumb .x {
  position: absolute; top: -7px; right: -7px; width: 20px; height: 20px; border-radius: 50%;
  border: 1px solid var(--line); background: #fff; color: var(--muted); font-size: 11px; line-height: 1;
  display: none; align-items: center; justify-content: center; cursor: pointer; padding: 0;
}
.sig-thumb:hover .x, .sig-thumb:focus-within .x { display: flex; }
@media (hover: none) { .sig-thumb .x { display: flex; } }

/* ---------- Viewer ---------- */
.viewer { flex: 1; overflow: auto; position: relative; overscroll-behavior: contain; }
.pages { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 20px 12px calc(110px + var(--safe-b)); min-width: min-content; }
.page { position: relative; background: #fff; box-shadow: var(--shadow); flex: none; }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.page .layer { position: absolute; inset: 0; }
.page .pnum { position: absolute; bottom: -16px; right: 0; font-size: 11px; color: var(--muted); }
body.placing .page .layer { cursor: crosshair; }
body.placing .page { outline: 2px dashed rgba(29,58,138,.35); outline-offset: 3px; }

.empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 20px; }
.empty-card { max-width: 440px; text-align: center; background: #fff; padding: 34px 28px; border-radius: 16px; box-shadow: var(--shadow); }
.empty-card svg { width: 56px; height: 56px; fill: none; stroke: var(--accent); stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; }
.empty-card h1 { margin: 10px 0 8px; font-size: 22px; }
.empty-card p { margin: 0 0 20px; line-height: 1.5; color: #3d4556; }
.empty-card p.small { margin: 14px 0 0; }

/* ---------- Placed signatures ---------- */
.sig {
  position: absolute; touch-action: none; cursor: move; user-select: none;
  outline: 1px dashed transparent; outline-offset: 2px;
}
.sig img { width: 100%; height: 100%; display: block; pointer-events: none; -webkit-user-drag: none; }
.sig:hover { outline-color: rgba(29,58,138,.45); }
.sig.sel { outline: 1.5px solid var(--accent-2); }
.sig .h {
  position: absolute; width: 22px; height: 22px; display: none; touch-action: none;
  right: -13px; bottom: -13px; cursor: nwse-resize;
}
.sig .h::after { content: ""; position: absolute; inset: 5px; background: #fff; border: 2px solid var(--accent-2); border-radius: 50%; }
.sig .h-nw { left: -13px; top: -13px; right: auto; bottom: auto; }
.sig .h-ne { right: -13px; top: -13px; bottom: auto; cursor: nesw-resize; }
.sig .h-sw { left: -13px; bottom: -13px; right: auto; cursor: nesw-resize; }
.sig.sel .h { display: block; }
@media (pointer: coarse) {
  .sig .h { width: 34px; height: 34px; right: -19px; bottom: -19px; }
  .sig .h-nw { left: -19px; top: -19px; }
  .sig .h-ne { right: -19px; top: -19px; }
  .sig .h-sw { left: -19px; bottom: -19px; }
  .sig .h::after { inset: 9px; }
}

/* ---------- Floating bars ---------- */
.banner, .selbar {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(18px + var(--safe-b));
  display: flex; align-items: center; gap: 10px; padding: 8px 10px 8px 16px;
  background: #1b1f27; color: #fff; border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.25);
  font-size: 14px; z-index: 20; max-width: calc(100vw - 20px); flex-wrap: wrap; justify-content: center;
}
.selbar { padding: 8px; gap: 6px; }
.selbar .btn, .banner .btn { background: #2b313d; border-color: #3a4150; color: #fff; }
.selbar .btn:hover, .banner .btn:hover { background: #363d4b; }
.selbar .btn.danger { color: #ff9aa0; }
.selsize { min-width: 52px; white-space: nowrap; text-align: center; font-variant-numeric: tabular-nums; font-size: 13px; color: #cfd5e1; }
.selbar .sep { width: 1px; height: 24px; background: #3a4150; margin: 0 4px; }

.toast {
  position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: #1b1f27; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,.25); max-width: calc(100vw - 24px);
}
.toast.err { background: var(--danger); }
.drop-overlay { position: fixed; inset: 0; z-index: 50; background: rgba(29,58,138,.12); border: 3px dashed var(--accent); display: flex; align-items: center; justify-content: center; pointer-events: none; }
.drop-overlay div { background: #fff; padding: 14px 22px; border-radius: 12px; font-weight: 600; box-shadow: var(--shadow); }

/* ---------- Editor modal ---------- */
.modal { position: fixed; inset: 0; z-index: 40; background: rgba(15, 20, 30, .55); display: flex; align-items: center; justify-content: center; padding: 18px; }
.modal-card {
  width: min(1100px, 100%); max-height: 100%; display: flex; flex-direction: column;
  background: #fff; border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.3); overflow: hidden;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px 8px 20px; }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-foot { display: flex; align-items: center; gap: 8px; padding: 12px 16px calc(12px + var(--safe-b)); border-top: 1px solid var(--line); flex-wrap: wrap; }
.ed-start { padding: 10px 20px 26px; }
.ed-start p { margin: 0 0 18px; line-height: 1.5; color: #3d4556; max-width: 62ch; }
.ed-start-buttons { display: flex; gap: 10px; flex-wrap: wrap; }
.ed-work { overflow: auto; padding: 0 16px 14px; }
.ed-panes { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.pane { min-width: 0; }
.pane-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; font-weight: 600; color: #3d4556; margin-bottom: 6px; min-height: 30px; flex-wrap: wrap; }
.pane-tools { display: flex; gap: 4px; }
.seg .btn.on { background: #e8ecf6; border-color: #b9c3dc; }
.photo-wrap, .result-wrap {
  position: relative; height: clamp(180px, 34vh, 340px); border-radius: 10px; border: 1px solid var(--line);
  overflow: hidden; display: flex; align-items: center; justify-content: center; background: #2a2f39;
}
.photo-wrap { touch-action: none; cursor: crosshair; }
.photo-wrap canvas, .result-wrap canvas { max-width: 100%; max-height: 100%; display: block; }
.result-wrap { background: #fff; }
.result-wrap.checker {
  background-color: #fff;
  background-image: linear-gradient(45deg, #e6e8ec 25%, transparent 25%), linear-gradient(-45deg, #e6e8ec 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #e6e8ec 75%), linear-gradient(-45deg, transparent 75%, #e6e8ec 75%);
  background-size: 16px 16px; background-position: 0 0, 0 8px, 8px -8px, -8px 0;
}
.result-wrap canvas { padding: 10px; }
.result-msg { position: absolute; inset: auto 10px 10px; text-align: center; font-size: 13px; background: #fff3cd; color: #6b4e00; padding: 6px 10px; border-radius: 8px; }
.crop { position: absolute; border: 2px solid #fff; box-shadow: 0 0 0 9999px rgba(0,0,0,.5); cursor: move; touch-action: none; }
.crop i { position: absolute; width: 22px; height: 22px; touch-action: none; }
.crop i::after { content: ""; position: absolute; inset: 5px; background: #fff; border-radius: 3px; box-shadow: 0 0 0 1px rgba(0,0,0,.35); }
.crop i[data-h=nw] { left: -12px; top: -12px; cursor: nwse-resize; }
.crop i[data-h=ne] { right: -12px; top: -12px; cursor: nesw-resize; }
.crop i[data-h=sw] { left: -12px; bottom: -12px; cursor: nesw-resize; }
.crop i[data-h=se] { right: -12px; bottom: -12px; cursor: nwse-resize; }
@media (pointer: coarse) {
  .crop i { width: 34px; height: 34px; }
  .crop i::after { inset: 10px; }
  .crop i[data-h=nw] { left: -18px; top: -18px; }
  .crop i[data-h=ne] { right: -18px; top: -18px; }
  .crop i[data-h=sw] { left: -18px; bottom: -18px; }
  .crop i[data-h=se] { right: -18px; bottom: -18px; }
}

.controls { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px 20px; margin-top: 16px; }
.ctl { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.ctl label:not(.check), .ctl .lbl { display: flex; justify-content: space-between; font-size: 13px; font-weight: 600; }
.ctl output { font-weight: 500; color: var(--muted); font-variant-numeric: tabular-nums; }
.ctl small { font-size: 12px; color: var(--muted); }
.ctl input[type=range] { width: 100%; accent-color: var(--accent); height: 26px; margin: 0; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-top: 8px; cursor: pointer; }
.swatches { display: flex; gap: 8px; align-items: center; margin-top: 2px; }
.sw { width: 30px; height: 30px; border-radius: 50%; border: 2px solid #fff; background: var(--c); box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; position: relative; }
.sw.on { box-shadow: 0 0 0 2px var(--accent-2); }
.sw.custom { background: conic-gradient(#e33, #ec3, #3c5, #3ae, #63e, #e33); overflow: hidden; }
.sw.custom input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; border: 0; padding: 0; }

@media (max-width: 760px) {
  .topbar { gap: 8px; padding: 8px 10px; }
  .brand span { display: none; }
  .filename { display: none; }
  .btn { padding: 0 11px; }
  .lg { display: none; }
  .sm { display: inline; }
  .topbar .btn { height: 36px; }
  .topbar .btn.icon { width: 34px; }
  .zoom .btn:not(.icon) { min-width: 0; padding: 0 8px; }
  .selbar { flex-wrap: nowrap; gap: 4px; padding: 6px; bottom: calc(10px + var(--safe-b)); }
  .selbar .btn { height: 34px; padding: 0 10px; }
  .selbar .btn.icon { width: 34px; padding: 0; }
  .selsize { min-width: 0; font-size: 12px; }
  .selbar .sep { display: none; }
  #downloadBtn span { display: none; }
  #downloadBtn::after { content: "Save"; }
  .tray { padding: 8px 10px; }
  .tray-hint { display: none; }
  .modal { padding: 0; }
  .modal-card { border-radius: 0; height: 100%; }
  .ed-panes { grid-template-columns: 1fr; }
  .photo-wrap, .result-wrap { height: 28vh; }
}
.page .perr { position: absolute; inset: 40% 10% auto; padding: 12px 14px; border-radius: 8px; background: #fff3cd; color: #6b4e00; font-size: 13px; text-align: center; pointer-events: none; }
