:root {
  --blue: #1f5fbf; --blue-d: #174a96; --bg: #f2f4f7; --card: #fff; --ink: #1c2330;
  --muted: #667085; --line: #d6dbe3; --sel: #1f5fbf; --red: #c0262d;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body { font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); background: var(--bg);
  display: flex; flex-direction: column; overscroll-behavior-y: contain; }
button { font: inherit; color: inherit; border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  padding: 8px 12px; cursor: pointer; touch-action: manipulation; }
button:disabled { opacity: .4; }
button.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
button.ghost { background: transparent; }
button.danger { color: var(--red); border-color: var(--red); }
button.link { border: 0; background: none; color: var(--blue); padding: 4px 6px; }
[hidden] { display: none !important; }

.top { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--blue); color: #fff;
  position: sticky; top: 0; z-index: 5; padding-top: max(10px, env(safe-area-inset-top)); }
.top .logo { width: 30px; height: 30px; border-radius: 7px; }
.top h1 { font-size: 18px; margin: 0; flex: 0 0 auto; }
.top .count { flex: 1; font-size: 13px; opacity: .85; }
.top button { color: #fff; border-color: rgba(255,255,255,.5); padding: 6px 10px; }

main { flex: 1; overflow: auto; padding: 12px 12px calc(150px + var(--safe-b)); }
.empty { max-width: 460px; margin: 8vh auto 0; text-align: center; }
.empty .big { font-size: 20px; font-weight: 600; }
.small { font-size: 13px; color: var(--muted); }
.big-btn { font-size: 18px; padding: 14px 26px; margin: 14px 0; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.pg { position: relative; background: var(--card); border: 2px solid transparent; border-radius: 10px; padding: 6px;
  box-shadow: 0 1px 3px rgba(0,0,0,.12); user-select: none; }
.pg.sel { border-color: var(--sel); background: #e8f0fc; }
.pg .thumb { aspect-ratio: 1 / 1.3; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.pg canvas, .pg img { max-width: 100%; max-height: 100%; box-shadow: 0 0 0 1px var(--line); background: #fff; }
.pg .cap { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); margin-top: 4px; gap: 4px; }
.pg .cap .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pg .tick { position: absolute; top: 10px; left: 10px; width: 26px; height: 26px; border-radius: 50%; background: #fff;
  border: 2px solid var(--line); display: flex; align-items: center; justify-content: center; font-size: 15px; color: #fff; }
.pg.sel .tick { background: var(--sel); border-color: var(--sel); }
.pg .edit { position: absolute; top: 8px; right: 8px; padding: 4px 8px; font-size: 14px; }
.pg .badge { position: absolute; bottom: 30px; right: 10px; background: var(--red); color: #fff; font-size: 11px; border-radius: 8px; padding: 1px 6px; }

.bar { position: fixed; left: 0; right: 0; bottom: 0; background: var(--card); border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + var(--safe-b)); z-index: 6; box-shadow: 0 -2px 8px rgba(0,0,0,.06); }
.selinfo { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); padding: 0 4px 4px; }
.selinfo span { flex: 1; }
.tools { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.tools::-webkit-scrollbar { display: none; }
.tools button { flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; min-width: 62px; padding: 6px 6px; font-size: 19px; }
.tools button span { font-size: 11px; margin-top: 2px; }

dialog { border: 0; border-radius: 14px; padding: 18px; width: min(94vw, 460px); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin: 0 0 12px; font-size: 19px; }
dialog label { display: block; margin: 10px 0; font-size: 14px; color: var(--muted); }
dialog input:not([type=radio]):not([type=checkbox]), dialog textarea, dialog select {
  display: block; width: 100%; font: inherit; font-size: 16px; padding: 10px; margin-top: 4px; border: 1px solid var(--line); border-radius: 8px; color: var(--ink); }
dialog label.check, .seg label { display: inline-flex; align-items: center; gap: 6px; color: var(--ink); margin-right: 14px; }
.dlg-btns { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; align-items: center; }
.spacer { flex: 1; }
#sigPad { width: 100%; aspect-ratio: 3 / 1; border: 1px dashed var(--muted); border-radius: 8px; touch-action: none; background: #fff; }

.modal { position: fixed; inset: 0; background: #2b313b; z-index: 20; display: flex; flex-direction: column; }
.ed-head { display: flex; align-items: center; gap: 8px; padding: 10px; padding-top: max(10px, env(safe-area-inset-top)); background: #1c2330; color: #fff; }
.ed-head .ghost { color: #fff; border-color: rgba(255,255,255,.4); }
.ed-body { flex: 1; overflow: auto; display: flex; align-items: flex-start; justify-content: center; padding: 14px; }
.stage { position: relative; background: #fff; box-shadow: 0 4px 18px rgba(0,0,0,.5); flex: 0 0 auto; }
.stage .frame { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.stage canvas { display: block; }
.ov { position: absolute; transform-origin: 50% 50%; touch-action: none; cursor: move; white-space: pre; line-height: 1.3; }
.ov.on { outline: 2px dashed #1f8fff; outline-offset: 3px; }
.ov.stamp { color: var(--red); border: solid var(--red); font-family: Helvetica, Arial, sans-serif; font-weight: 700; text-align: center; }
.ov.text { font-family: Helvetica, Arial, sans-serif; }
.ov.sig img { display: block; height: 100%; width: auto; pointer-events: none; }
.ed-foot { background: #1c2330; color: #fff; padding: 8px 10px calc(8px + var(--safe-b)); }
.ed-foot button { background: #2f3846; color: #fff; border-color: #48536a; }
.ed-foot .danger { color: #ff8a8f; border-color: #ff8a8f; }
.edsel { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.hint { font-size: 13px; opacity: .75; margin-bottom: 8px; }
.ed-add { display: flex; gap: 8px; }
.ed-add button { flex: 1; }

.toast { position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%) translateY(20px); background: #1c2330; color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; z-index: 40; max-width: 90vw; text-align: center; }
.toast.show { opacity: 1; transform: translateX(-50%); }
.busy { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #fff; gap: 12px; }
.spin { width: 40px; height: 40px; border: 4px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: sp 0.9s linear infinite; }
@keyframes sp { to { transform: rotate(360deg); } }
