:root{
  --bg:#F6F7F9; --surface:#fff; --surface-2:#EEF0F3; --line:#E1E4EA; --text:#16181D; --muted:#5B6170;
  --accent:#0B3D66; --accent-ink:#fff; --accent-tint:rgba(11,61,102,.10); --err:#D93B3B; --shadow:0 8px 28px rgba(0,0,0,.14);
  --font:system-ui,-apple-system,"Segoe UI","PingFang TC","Noto Sans TC","Microsoft JhengHei",sans-serif;
  --sat:env(safe-area-inset-top,0px); --sab:env(safe-area-inset-bottom,0px);
  color-scheme:light;
}
@media (prefers-color-scheme:dark){:root{--bg:#121316;--surface:#1C1D22;--surface-2:#272930;--line:#2E3037;--text:#F2F3F5;--muted:#9AA0AE;--accent:#8CC4F0;--accent-ink:#04182A;--accent-tint:rgba(140,196,240,.16);--err:#FF7A7A;color-scheme:dark}}
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
html,body{height:100%;margin:0}
body{background:var(--bg);color:var(--text);font:400 16px/1.45 var(--font);overscroll-behavior:none}
button{font:inherit;color:inherit;border:0;background:none;padding:0;cursor:pointer;touch-action:manipulation}
svg{display:block;flex:none}
[hidden]{display:none!important}
button:focus-visible,input:focus-visible,a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}

#app{height:100%;display:flex;flex-direction:column;max-width:720px;margin:0 auto;background:var(--bg)}
#top{display:flex;align-items:center;gap:6px;padding:calc(var(--sat) + 6px) 8px 6px;background:var(--surface);border-bottom:1px solid var(--line);min-height:56px}
#title{flex:1;font-weight:700;font-size:18px;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.ib{width:44px;height:44px;border-radius:12px;display:grid;place-items:center;color:var(--text)}
.ib:active{background:var(--surface-2)} .ib.txt{font-size:14px;font-weight:700;color:var(--accent)}
#back:not([hidden]),#backBtn:not([hidden]){display:grid}
#view{flex:1;min-height:0;overflow:auto;-webkit-overflow-scrolling:touch;padding:16px 16px calc(var(--sab) + 24px);display:flex;flex-direction:column;gap:14px}
#view.v-scan,#view.v-sign{padding-bottom:0}

.tagline{margin:0;color:var(--muted);text-align:center}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:12px}
.tile{display:flex;flex-direction:column;align-items:flex-start;gap:4px;text-align:left;padding:16px;border-radius:20px;background:var(--surface);border:1px solid var(--line);min-height:132px;box-shadow:0 1px 2px rgba(0,0,0,.04);transition:transform .12s}
.tile:active{transform:scale(.97)}
.tile b{font-size:17px;margin-top:6px} .tile small{color:var(--muted);font-size:13px;line-height:1.35}
.tic{width:46px;height:46px;border-radius:14px;display:grid;place-items:center;color:#fff}
.c1 .tic{background:#0B3D66}.c2 .tic{background:#7B5BD6}.c3 .tic{background:#D9822B}.c4 .tic{background:#3B82C4}.c5 .tic{background:#C4453B}.c6 .tic{background:#2E9E6B}.c7 .tic{background:#6B7280}.c8 .tic{background:#B8892B}
.note{display:flex;flex-direction:column;gap:2px;font-size:13px;color:var(--muted);padding:12px 14px;border-radius:14px;background:var(--surface-2)}
.note b{color:var(--text)}

.btn{display:inline-flex;align-items:center;justify-content:center;gap:8px;min-height:48px;padding:0 20px;border-radius:14px;background:var(--accent);color:var(--accent-ink);font-weight:700}
.btn:active{filter:brightness(.92)} .btn:disabled{opacity:.4;pointer-events:none}
.btn.ghost{background:var(--surface);color:var(--text);border:1px solid var(--line)}
.btn.danger{background:var(--err);color:#fff} .btn.wide{width:100%} .btn.sm{min-height:38px;padding:0 14px;border-radius:11px;font-size:14px}
.row{display:flex;gap:10px;align-items:center} .row>.btn{flex:1} .row.wrap{flex-wrap:wrap}
.inp{width:100%;min-height:48px;padding:0 14px;border-radius:12px;border:1px solid var(--line);background:var(--surface);color:var(--text);font:inherit}
.lbl{font-size:13px;color:var(--muted);font-weight:600;margin:2px 0}
.empty{padding:36px 16px;text-align:center;color:var(--muted);border:2px dashed var(--line);border-radius:18px}
.card{background:var(--surface);border:1px solid var(--line);border-radius:16px;padding:14px}

.seg{display:flex;background:var(--surface-2);border-radius:12px;padding:3px;gap:2px}
.seg button{flex:1;min-height:40px;border-radius:10px;font-weight:600;color:var(--muted);white-space:nowrap;padding:0 8px}
.seg button.on{background:var(--surface);color:var(--text);box-shadow:0 1px 3px rgba(0,0,0,.14)}
.rng{width:100%;accent-color:var(--accent);height:32px}
.chips{display:flex;gap:8px;flex-wrap:wrap}
.chipb{min-height:40px;padding:0 14px;border-radius:20px;border:1px solid var(--line);background:var(--surface);font-weight:600;font-size:14px;display:inline-flex;align-items:center;gap:6px}
.chipb.on{background:var(--accent-tint);border-color:var(--accent);color:var(--accent)}
.sw{width:30px;height:30px;border-radius:50%;border:3px solid var(--surface);box-shadow:0 0 0 1px var(--line)} .sw.on{box-shadow:0 0 0 3px var(--accent)}

.bar{position:sticky;bottom:0;margin-top:auto;margin:0 -16px;padding:10px 16px calc(var(--sab) + 10px);background:var(--surface);border-top:1px solid var(--line);display:flex;flex-wrap:wrap;gap:10px;z-index:5}
.bar>.btn{flex:1 1 calc(50% - 5px);padding:6px 10px;min-width:0;line-height:1.2;text-align:center}
.bar>.btn[aria-label]{flex:0 0 56px;padding:0}

/* 覆蓋層與對話框 */
.overlay{position:fixed;inset:0;z-index:50;background:var(--bg);display:flex;flex-direction:column;animation:ov .18s ease-out}
@keyframes ov{from{opacity:0;transform:translateY(12px)}}
.overlay.dim{background:rgba(0,0,0,.45);justify-content:flex-end}
.dlg{margin:auto 20px;max-width:420px;align-self:center;width:calc(100% - 40px);background:var(--surface);border-radius:20px;padding:20px;display:flex;flex-direction:column;gap:14px;box-shadow:var(--shadow)}
.dlg p{margin:0}
.ov-top{display:flex;align-items:center;gap:6px;padding:calc(var(--sat) + 6px) 8px 6px;background:var(--surface);border-bottom:1px solid var(--line);min-height:56px}
.ov-top b{flex:1;text-align:center;font-size:17px}
.ov-body{flex:1;min-height:0;overflow:auto;padding:14px 16px;display:flex;flex-direction:column;gap:12px}
.sheet{background:var(--surface);border-radius:22px 22px 0 0;padding:16px 16px calc(var(--sab) + 16px);display:flex;flex-direction:column;gap:12px;max-height:75vh;overflow:auto;width:100%;max-width:720px;margin:0 auto;animation:sh .2s ease-out}
@keyframes sh{from{transform:translateY(40px);opacity:.5}}

#toast{position:fixed;left:50%;bottom:calc(var(--sab) + 90px);transform:translate(-50%,20px);background:#222;color:#fff;padding:12px 18px;border-radius:14px;font-size:14px;max-width:86vw;opacity:0;pointer-events:none;transition:.2s;z-index:99;text-align:center}
#toast.show{opacity:1;transform:translate(-50%,0)}
#busy{position:fixed;inset:0;z-index:90;background:rgba(0,0,0,.45);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:14px;color:#fff}
.spin{width:44px;height:44px;border-radius:50%;border:4px solid rgba(255,255,255,.3);border-top-color:#fff;animation:sp .8s linear infinite}
@keyframes sp{to{transform:rotate(360deg)}}

/* 四角編輯器 */
.qwrap{flex:1;min-height:260px;display:flex;align-items:center;justify-content:center;background:var(--surface-2);border-radius:14px;overflow:hidden;position:relative;touch-action:none}
.qstage{position:relative;touch-action:none} .qstage canvas{display:block}
.qsvg{position:absolute;inset:0;width:100%;height:100%;pointer-events:none}
.qh{position:absolute;width:52px;height:52px;margin:-26px 0 0 -26px;display:grid;place-items:center;touch-action:none;cursor:grab}
.qh i{width:22px;height:22px;border-radius:50%;background:#fff;border:3px solid var(--accent);box-shadow:0 1px 6px rgba(0,0,0,.4);transition:transform .1s}
.qh.on i{transform:scale(1.35)}

/* 掃描頁面縮圖 */
.thumbs{display:grid;grid-template-columns:repeat(3,1fr);gap:10px}
.thumb{position:relative;border-radius:12px;overflow:hidden;border:2px solid var(--line);background:var(--surface-2);aspect-ratio:3/4;display:grid;place-items:center}
.thumb.on{border-color:var(--accent);box-shadow:0 0 0 2px var(--accent)}
.thumb canvas,.thumb img{max-width:100%;max-height:100%;display:block}
.thumb .no{position:absolute;left:6px;top:6px;background:rgba(0,0,0,.6);color:#fff;border-radius:8px;font-size:12px;padding:1px 7px}
.thumb .ck{position:absolute;right:6px;top:6px;width:24px;height:24px;border-radius:50%;background:var(--accent);color:var(--accent-ink);display:grid;place-items:center}
.preview{flex:1;min-height:240px;display:grid;place-items:center;background:var(--surface-2);border-radius:14px;overflow:hidden;padding:8px}
.preview canvas{max-width:100%;max-height:60vh;display:block;box-shadow:0 2px 12px rgba(0,0,0,.2);background:#fff}

/* 簽名 */
.sigs{display:grid;grid-template-columns:1fr 1fr;gap:10px}
.sig{position:relative;border-radius:14px;border:1px solid var(--line);background:#fff;aspect-ratio:2/1;display:grid;place-items:center;padding:10px;overflow:hidden}
.sig img{max-width:100%;max-height:100%;display:block}
.sig .x{position:absolute;right:6px;top:6px;width:40px;height:40px;border-radius:50%;background:#C4453B;display:grid;place-items:center;color:#fff;box-shadow:0 1px 4px rgba(0,0,0,.3)}
.sig.pick{cursor:pointer} .sig.pick:active{border-color:var(--accent)}
.checker{background:conic-gradient(#e7e9ee 25%,#fff 0 50%,#e7e9ee 0 75%,#fff 0) 0 0/16px 16px}
.pad{touch-action:none;border-radius:14px;border:1px solid var(--line);background:#fff;width:100%;aspect-ratio:2/1;display:block}

/* 簽名蓋章編輯 */
.stagebox{flex:1;min-height:0;display:flex;overflow:auto;background:var(--surface-2);position:relative;touch-action:none}
.page{position:relative;margin:auto;flex:none;box-shadow:0 2px 14px rgba(0,0,0,.25);background:#fff;touch-action:pan-x pan-y}
.page>canvas{display:block}
.item{position:absolute;touch-action:none;cursor:move}
.item img{width:100%;height:100%;display:block;pointer-events:none;-webkit-user-drag:none}
.item.sel{outline:2px dashed var(--accent)}
.hnd{position:absolute;width:34px;height:34px;margin:-17px;border-radius:50%;background:var(--accent);color:var(--accent-ink);display:grid;place-items:center;touch-action:none;box-shadow:0 2px 6px rgba(0,0,0,.35)}
.hnd.rs{right:-17px;bottom:-17px;margin:0;right:0;bottom:0;transform:translate(50%,50%)} .hnd.ro{left:50%;top:-30px}
.tools{display:flex;gap:8px;overflow:auto;padding:2px 0}
.tools .chipb{flex:none}
.pgnav{display:flex;align-items:center;justify-content:center;gap:10px;font-size:14px;color:var(--muted)}

/* 列表 */
.list{display:flex;flex-direction:column;gap:8px}
.li{display:flex;align-items:center;gap:10px;padding:10px 12px;border-radius:14px;background:var(--surface);border:1px solid var(--line)}
.li .nm{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap} .li small{color:var(--muted);display:block}
.li .ib{width:38px;height:38px}
.hint{font-size:13px;color:var(--muted);margin:0}
.warn{font-size:13px;color:var(--muted);padding:10px 12px;border-radius:12px;background:var(--surface-2);margin:0}
@media (min-width:600px){.thumbs{grid-template-columns:repeat(4,1fr)}.grid{grid-template-columns:repeat(3,1fr)}}

/* 放大鏡與邊中點 */
.loupe{position:fixed;z-index:200;width:116px;height:116px;border-radius:50%;border:3px solid #fff;box-shadow:0 4px 18px rgba(0,0,0,.55);overflow:hidden;pointer-events:none;background:#222}
.qe i{width:16px;height:16px;border-radius:5px;border-width:2px;opacity:.92;border-color:#fff;background:var(--accent)}
.qe{width:44px;height:44px;margin:-22px 0 0 -22px}

/* 預覽 */
.vscroll{flex:1;min-height:0;overflow:auto;background:#1b1c20;display:flex;padding:10px}
.vcv{margin:auto;display:block;max-width:100%;max-height:100%;background:#fff;box-shadow:0 2px 14px rgba(0,0,0,.5);cursor:zoom-in;flex:none}
.vcv.z{max-width:none;max-height:none;width:250%;cursor:zoom-out}
/* 效果滑桿 */
.look{display:flex;flex-direction:column;gap:2px}
.look .sl{display:grid;grid-template-columns:1fr auto;align-items:center;column-gap:8px;font-size:13px;color:var(--muted)}
.look .sl output{font-variant-numeric:tabular-nums;min-width:34px;text-align:right}
.look .sl input{grid-column:1/-1;width:100%;accent-color:var(--accent);height:30px;margin:0}
.presets{display:flex;gap:8px;overflow:auto;padding:2px 0 6px;scrollbar-width:none}
.presets::-webkit-scrollbar{display:none}
.pre{flex:none;display:flex;flex-direction:column;align-items:center;gap:4px;font-size:12px;font-weight:600;color:var(--muted);width:68px}
.pre canvas{width:60px;height:76px;border-radius:10px;border:2px solid var(--line);object-fit:cover;background:#fff}
.pre.on{color:var(--accent)} .pre.on canvas{border-color:var(--accent)}
.cmp{touch-action:none;user-select:none;-webkit-user-select:none}

/* 我的檔案 */
.docs{display:grid;grid-template-columns:repeat(2,1fr);gap:10px}
.doc{text-align:left;display:flex;flex-direction:column;gap:2px;padding:8px;border-radius:14px;background:var(--surface);border:1px solid var(--line)}
.doc:active{border-color:var(--accent)}
.dth{position:relative;aspect-ratio:3/4;border-radius:10px;overflow:hidden;background:var(--surface-2);display:grid;place-items:center}
.dth img{width:100%;height:100%;object-fit:cover;display:block}
.dth .tag{position:absolute;left:6px;top:6px;background:#C4453B;color:#fff;font-size:11px;font-weight:700;border-radius:6px;padding:1px 6px}
.dnm{font-weight:600;font-size:14px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.doc small{color:var(--muted);font-size:12px}
@media (min-width:600px){.docs{grid-template-columns:repeat(4,1fr)}}

/* 覆蓋層：內容區可捲動，主要按鈕固定在底部 */
.ov-body>*{flex-shrink:0}
.ov-body>.qwrap{flex:1 0 260px}
.preview{flex:none;height:38vh;min-height:200px}
.preview canvas{max-height:100%}
.ov-foot{padding:10px 16px calc(var(--sab) + 10px);background:var(--surface);border-top:1px solid var(--line);display:flex;gap:10px}
.ov-foot:empty{display:none}
.ov-foot>.btn{flex:1}
.pre canvas{flex:none}
.preview canvas{width:100%;height:100%;object-fit:contain;box-shadow:none}

/* 連拍相機 */
.cam{position:absolute;inset:0;background:#000;display:flex;flex-direction:column;color:#fff}
.camtop{display:flex;align-items:center;gap:6px;padding:calc(var(--sat) + 6px) 8px 6px}
.camhint{flex:1;text-align:center;font-size:14px;opacity:.9}
.camstage{flex:1;min-height:0;position:relative;display:flex}
.camv{width:100%;height:100%;object-fit:contain;background:#000}
.camg{position:absolute;inset:0;width:100%;height:100%;pointer-events:none}
.camflash{position:absolute;inset:0;background:#fff;opacity:0;pointer-events:none}
.camflash.go{opacity:.85;transition:opacity .05s}
.cambar{display:grid;grid-template-columns:1fr auto 1fr;align-items:center;padding:14px 16px calc(var(--sab) + 18px);gap:10px}
.camleft,.camright{display:flex;gap:8px;align-items:center}
.camright{justify-content:flex-end}
.shutter{width:76px;height:76px;border-radius:50%;background:#fff;border:6px solid rgba(255,255,255,.4);background-clip:padding-box;box-shadow:0 0 0 3px #fff}
.shutter:active{transform:scale(.92)}
.cambtn{min-height:40px;padding:0 12px;border-radius:20px;background:rgba(255,255,255,.16);color:#fff;font-weight:600;font-size:14px;display:inline-flex;align-items:center;gap:6px}
.cambtn.on{background:#8CC4F0;color:#04182A}
.cambtn.done{background:var(--accent);color:var(--accent-ink)}
.camthumbwrap{position:relative;width:44px;height:56px}
.camthumb{display:none;width:44px;height:56px;object-fit:cover;border-radius:8px;border:2px solid #fff}
.camcount{position:absolute;right:-6px;top:-6px;min-width:20px;height:20px;border-radius:10px;background:var(--accent);color:var(--accent-ink);font-size:12px;font-weight:700;display:grid;place-items:center;padding:0 5px}
.camcount:empty{display:none}
.padclean{touch-action:none;max-width:100%;display:block;background:#fff;box-shadow:0 2px 12px rgba(0,0,0,.2)}

/* 效果預覽：實際大小由 JS 算（fitPrev），這裡只負責置中與裁掉多餘 */
.preview{display:flex;align-items:center;justify-content:center;overflow:hidden;padding:8px;height:42vh;min-height:220px}
.preview canvas{width:auto;height:auto;max-width:none;max-height:none;object-fit:fill;box-shadow:0 2px 12px rgba(0,0,0,.25)}
.pre canvas{width:60px;height:76px;object-fit:cover}

.preview .padclean{width:100%;height:auto;max-width:100%;max-height:none}
.doc.sel{border-color:var(--accent);box-shadow:0 0 0 2px var(--accent)}
.doc .ck{position:absolute;right:8px;top:8px;width:26px;height:26px;border-radius:50%;background:var(--accent);color:var(--accent-ink);display:grid;place-items:center}
.doc{position:relative}

.thumb .warnb{position:absolute;right:6px;top:6px;width:24px;height:24px;border-radius:50%;background:#E8A21A;color:#fff;font-weight:800;display:grid;place-items:center}
