@imliulian/dsh-zeromem 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2880 @@
1
+ /* ZeroMem (零忆) Embedded DSH Web Workbench — Light Frosted Glass, Spring Tactile UI, Processing Halo, Auto Local Setup */
2
+ (function () {
3
+ if (window.__DSH_ZEROMEM_LOADED__) return;
4
+ window.__DSH_ZEROMEM_LOADED__ = true;
5
+
6
+ const API_BASE = "/dsh-zeromem/api";
7
+ const POS_KEY = "dsh_zeromem_pill_pos_v1";
8
+ const THEME_CACHE_KEY = "dsh_zeromem_theme_v2";
9
+
10
+ const ICONS = {
11
+ brain:
12
+ '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round"><path d="M9.5 2A2.5 2.5 0 0 1 12 4.5v15a2.5 2.5 0 0 1-4.96.44 2.5 2.5 0 0 1-2.96-3.08 3 3 0 0 1-.34-5.58 2.5 2.5 0 0 1 1.32-4.24 2.5 2.5 0 0 1 4.44-1.54Z"/><path d="M14.5 2A2.5 2.5 0 0 0 12 4.5v15a2.5 2.5 0 0 0 4.96.44 2.5 2.5 0 0 0 2.96-3.08 3 3 0 0 0 .34-5.58 2.5 2.5 0 0 0-1.32-4.24 2.5 2.5 0 0 0-4.44-1.54Z"/></svg>',
13
+ search:
14
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>',
15
+ plus:
16
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="M12 5v14"/></svg>',
17
+ refresh:
18
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16"/><path d="M8 16H3v5"/></svg>',
19
+ close:
20
+ '<svg viewBox="0 0 24 24" width="15" height="15" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>',
21
+ trash:
22
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/></svg>',
23
+ restore:
24
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/></svg>',
25
+ sliders:
26
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="4" x2="4" y1="21" y2="14"/><line x1="4" x2="4" y1="10" y2="3"/><line x1="12" x2="12" y1="21" y2="12"/><line x1="12" x2="12" y1="8" y2="3"/><line x1="20" x2="20" y1="21" y2="16"/><line x1="20" x2="20" y1="12" y2="3"/><line x1="2" x2="6" y1="14" y2="14"/><line x1="10" x2="14" y1="8" y2="8"/><line x1="18" x2="22" y1="16" y2="16"/></svg>',
27
+ cpu:
28
+ '<svg viewBox="0 0 24 24" width="15" height="15" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round"><rect width="16" height="16" x="4" y="4" rx="2"/><rect width="6" height="6" x="9" y="9" rx="1"/><path d="M15 2v2M15 20v2M2 15h2M2 9h2M20 15h2M20 9h2M9 2v2M9 20v2"/></svg>',
29
+ cloud:
30
+ '<svg viewBox="0 0 24 24" width="15" height="15" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></svg>',
31
+ check:
32
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.3" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>',
33
+ bolt:
34
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></svg>',
35
+ image:
36
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="18" height="18" x="3" y="3" rx="3"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"/></svg>',
37
+ upload:
38
+ '<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" x2="12" y1="3" y2="15"/></svg>',
39
+ sun:
40
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41"/></svg>',
41
+ moon:
42
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/></svg>',
43
+ download:
44
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" x2="12" y1="15" y2="3"/></svg>',
45
+ play:
46
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round"><polygon points="6 3 20 12 6 21 6 3"/></svg>',
47
+ copy:
48
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>',
49
+ external:
50
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 3h6v6"/><path d="M10 14 21 3"/><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></svg>',
51
+ chevron:
52
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2.1" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>',
53
+ };
54
+
55
+ const NOISE_URI =
56
+ "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='1'/%3E%3C/svg%3E";
57
+
58
+ function esc(s) {
59
+ return String(s ?? "")
60
+ .replace(/&/g, "&amp;")
61
+ .replace(/</g, "&lt;")
62
+ .replace(/>/g, "&gt;")
63
+ .replace(/"/g, "&quot;");
64
+ }
65
+
66
+ function highlightRedacted(text) {
67
+ return esc(text).replace(/\[REDACTED\]/g, '<span class="zm-redact">[REDACTED]</span>');
68
+ }
69
+
70
+ function formatBytes(n) {
71
+ const v = Number(n || 0);
72
+ if (!Number.isFinite(v) || v <= 0) return "";
73
+ if (v >= 1024 ** 3) return `${(v / 1024 ** 3).toFixed(2)} GB`;
74
+ if (v >= 1024 ** 2) return `${(v / 1024 ** 2).toFixed(0)} MB`;
75
+ return `${Math.round(v / 1024)} KB`;
76
+ }
77
+
78
+ function readCachedTheme() {
79
+ try {
80
+ const t = localStorage.getItem(THEME_CACHE_KEY);
81
+ return t === "dark" ? "dark" : "light";
82
+ } catch {
83
+ return "light";
84
+ }
85
+ }
86
+
87
+ function writeCachedTheme(theme) {
88
+ try {
89
+ localStorage.setItem(THEME_CACHE_KEY, theme === "dark" ? "dark" : "light");
90
+ } catch {}
91
+ }
92
+
93
+ async function api(path, opts) {
94
+ const res = await fetch(`${API_BASE}${path}`, {
95
+ headers: opts?.body ? { "Content-Type": "application/json" } : undefined,
96
+ ...opts,
97
+ });
98
+ const data = await res.json().catch(() => ({ ok: false, error: `HTTP ${res.status}` }));
99
+ if (!res.ok || data?.ok === false) {
100
+ throw new Error(data?.error || data?.result?.message || `HTTP ${res.status}`);
101
+ }
102
+ return data;
103
+ }
104
+
105
+ function fileToDataUrl(file) {
106
+ return new Promise((resolve, reject) => {
107
+ const reader = new FileReader();
108
+ reader.onload = () => resolve(String(reader.result || ""));
109
+ reader.onerror = () => reject(new Error("读取图片失败"));
110
+ reader.readAsDataURL(file);
111
+ });
112
+ }
113
+
114
+ async function prepareImageDataUrl(file) {
115
+ if (!file) throw new Error("未选择图片");
116
+ const allowed = new Set(["image/png", "image/jpeg", "image/webp", "image/gif", "image/avif"]);
117
+ if (file.type && !allowed.has(file.type.toLowerCase())) {
118
+ throw new Error("仅支持 PNG / JPG / WebP / GIF / AVIF 图片");
119
+ }
120
+ if (file.type === "image/gif") {
121
+ if (file.size > 5.8 * 1024 * 1024) throw new Error("GIF 超过 5.8 MB,请压缩后上传");
122
+ return fileToDataUrl(file);
123
+ }
124
+ const rawUrl = await fileToDataUrl(file);
125
+ const img = await new Promise((resolve, reject) => {
126
+ const i = new Image();
127
+ i.onload = () => resolve(i);
128
+ i.onerror = () => reject(new Error("图片解码失败"));
129
+ i.src = rawUrl;
130
+ });
131
+ const maxSide = 1920;
132
+ const w = img.naturalWidth || img.width;
133
+ const h = img.naturalHeight || img.height;
134
+ const needsResize = Math.max(w, h) > maxSide;
135
+ if (!needsResize && file.size <= 1.8 * 1024 * 1024 && allowed.has((file.type || "").toLowerCase())) {
136
+ return rawUrl;
137
+ }
138
+ const scale = needsResize ? maxSide / Math.max(w, h) : 1;
139
+ const cw = Math.max(1, Math.round(w * scale));
140
+ const ch = Math.max(1, Math.round(h * scale));
141
+ const canvas = document.createElement("canvas");
142
+ canvas.width = cw;
143
+ canvas.height = ch;
144
+ const ctx2d = canvas.getContext("2d");
145
+ if (!ctx2d) return rawUrl;
146
+ ctx2d.imageSmoothingEnabled = true;
147
+ ctx2d.imageSmoothingQuality = "high";
148
+ ctx2d.drawImage(img, 0, 0, cw, ch);
149
+ let out = canvas.toDataURL("image/webp", 0.88);
150
+ if (!out.startsWith("data:image/webp")) out = canvas.toDataURL("image/jpeg", 0.88);
151
+ return out;
152
+ }
153
+
154
+ const host = document.createElement("div");
155
+ host.id = "dsh-zeromem-root";
156
+ host.dataset.zmRev = "c9f48efabf95";
157
+ host.style.position = "fixed";
158
+ host.style.zIndex = "2147483000";
159
+ host.style.inset = "0";
160
+ host.style.pointerEvents = "none";
161
+ const shadow = host.attachShadow({ mode: "open" });
162
+
163
+ shadow.innerHTML = `
164
+ <style>
165
+ :host {
166
+ all: initial;
167
+ font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
168
+ -webkit-font-smoothing: antialiased;
169
+ text-rendering: optimizeLegibility;
170
+
171
+ --zm-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
172
+ --zm-glider-ease: cubic-bezier(0.32, 1.22, 0.54, 1);
173
+ --zm-ease: cubic-bezier(0.22, 1, 0.36, 1);
174
+
175
+ /* Default Light Porcelain Liquid Glass */
176
+ --zm-text: #0f172a;
177
+ --zm-text-2: #334155;
178
+ --zm-muted: #64748b;
179
+ --zm-accent: #4f46e5;
180
+ --zm-accent-grad: linear-gradient(135deg, #3b82f6 0%, #6366f1 100%);
181
+ --zm-accent-soft: rgba(79, 70, 229, 0.11);
182
+ --zm-accent-border: rgba(99, 102, 241, 0.36);
183
+
184
+ --zm-pill-bg: rgba(255, 255, 255, 0.84);
185
+ --zm-pill-border: rgba(255, 255, 255, 0.92);
186
+ --zm-pill-shadow: 0 10px 30px -6px rgba(15, 23, 42, 0.14), 0 2px 8px rgba(15, 23, 42, 0.06), inset 0 1px 0 rgba(255, 255, 255, 0.96);
187
+
188
+ --zm-drawer-border: rgba(255, 255, 255, 0.84);
189
+ --zm-drawer-shadow: 0 28px 72px -14px rgba(15, 23, 42, 0.18), 0 8px 24px -6px rgba(15, 23, 42, 0.08), inset 0 1px 0 rgba(255, 255, 255, 0.95);
190
+ --zm-backdrop-bg: rgba(15, 23, 42, 0.14);
191
+
192
+ --zm-veil-r: 255; --zm-veil-g: 255; --zm-veil-b: 255;
193
+ --zm-veil-r2: 244; --zm-veil-g2: 248; --zm-veil-b2: 255;
194
+
195
+ --zm-surface: rgba(255, 255, 255, 0.58);
196
+ --zm-surface-strong: rgba(255, 255, 255, 0.82);
197
+ --zm-surface-hover: rgba(255, 255, 255, 0.90);
198
+ --zm-surface-border: rgba(15, 23, 42, 0.085);
199
+ --zm-surface-highlight: rgba(255, 255, 255, 0.92);
200
+ --zm-surface-outer: rgba(15, 23, 42, 0.075);
201
+ --zm-divider: rgba(15, 23, 42, 0.075);
202
+
203
+ --zm-input-bg: rgba(255, 255, 255, 0.76);
204
+ --zm-input-border: rgba(15, 23, 42, 0.12);
205
+ --zm-input-focus: rgba(79, 70, 229, 0.48);
206
+
207
+ --zm-tag-bg: rgba(79, 70, 229, 0.09);
208
+ --zm-tag-text: #4338ca;
209
+ --zm-tag-border: rgba(99, 102, 241, 0.18);
210
+ --zm-rev-bg: rgba(16, 185, 129, 0.13);
211
+ --zm-rev-text: #047857;
212
+ --zm-rev-border: rgba(16, 185, 129, 0.25);
213
+ --zm-exp-bg: rgba(245, 158, 11, 0.14);
214
+ --zm-exp-text: #b45309;
215
+ --zm-exp-border: rgba(245, 158, 11, 0.28);
216
+
217
+ --zm-danger: #e11d48;
218
+ --zm-danger-soft: rgba(244, 63, 94, 0.12);
219
+ --zm-danger-border: rgba(244, 63, 94, 0.35);
220
+
221
+ --zm-toast-ok-bg: rgba(236, 253, 245, 0.92);
222
+ --zm-toast-ok-border: rgba(16, 185, 129, 0.36);
223
+ --zm-toast-ok-text: #065f46;
224
+ --zm-toast-err-bg: rgba(255, 241, 242, 0.94);
225
+ --zm-toast-err-border: rgba(244, 63, 94, 0.36);
226
+ --zm-toast-err-text: #9f1239;
227
+
228
+ --zm-ripple-color: rgba(79, 70, 229, 0.18);
229
+ --zm-blur: 30px;
230
+ --zm-tint-hi: 0.56;
231
+ --zm-tint-lo: 0.44;
232
+ --zm-scroll-thumb: rgba(100, 116, 139, 0.28);
233
+
234
+ color: var(--zm-text);
235
+ }
236
+
237
+ :host([data-theme="dark"]) {
238
+ --zm-text: #f1f5f9;
239
+ --zm-text-2: #cbd5e1;
240
+ --zm-muted: #94a3b8;
241
+ --zm-accent: #818cf8;
242
+ --zm-accent-grad: linear-gradient(135deg, #38bdf8 0%, #6366f1 100%);
243
+ --zm-accent-soft: rgba(56, 189, 248, 0.16);
244
+ --zm-accent-border: rgba(99, 102, 241, 0.48);
245
+
246
+ --zm-pill-bg: rgba(15, 23, 42, 0.78);
247
+ --zm-pill-border: rgba(255, 255, 255, 0.16);
248
+ --zm-pill-shadow: 0 12px 32px rgba(2, 6, 23, 0.52), inset 0 1px 0 rgba(255, 255, 255, 0.14);
249
+
250
+ --zm-drawer-border: rgba(255, 255, 255, 0.14);
251
+ --zm-drawer-shadow: 0 28px 72px rgba(0, 0, 0, 0.62), inset 0 1px 0 rgba(255, 255, 255, 0.14);
252
+ --zm-backdrop-bg: rgba(2, 6, 23, 0.42);
253
+
254
+ --zm-veil-r: 13; --zm-veil-g: 20; --zm-veil-b: 36;
255
+ --zm-veil-r2: 18; --zm-veil-g2: 27; --zm-veil-b2: 48;
256
+
257
+ --zm-surface: rgba(20, 31, 52, 0.56);
258
+ --zm-surface-strong: rgba(24, 37, 62, 0.80);
259
+ --zm-surface-hover: rgba(30, 46, 76, 0.82);
260
+ --zm-surface-border: rgba(255, 255, 255, 0.14);
261
+ --zm-surface-highlight: rgba(255, 255, 255, 0.12);
262
+ --zm-surface-outer: rgba(0, 0, 0, 0.22);
263
+ --zm-divider: rgba(255, 255, 255, 0.08);
264
+
265
+ --zm-input-bg: rgba(11, 18, 34, 0.64);
266
+ --zm-input-border: rgba(148, 163, 184, 0.24);
267
+ --zm-input-focus: rgba(56, 189, 248, 0.55);
268
+
269
+ --zm-tag-bg: rgba(56, 189, 248, 0.14);
270
+ --zm-tag-text: #7dd3fc;
271
+ --zm-tag-border: rgba(56, 189, 248, 0.24);
272
+ --zm-rev-bg: rgba(16, 185, 129, 0.16);
273
+ --zm-rev-text: #6ee7b7;
274
+ --zm-rev-border: rgba(16, 185, 129, 0.3);
275
+ --zm-exp-bg: rgba(245, 158, 11, 0.18);
276
+ --zm-exp-text: #fcd34d;
277
+ --zm-exp-border: rgba(245, 158, 11, 0.32);
278
+
279
+ --zm-danger: #fb7185;
280
+ --zm-danger-soft: rgba(244, 63, 94, 0.18);
281
+ --zm-danger-border: rgba(251, 113, 133, 0.42);
282
+
283
+ --zm-toast-ok-bg: rgba(6, 78, 59, 0.86);
284
+ --zm-toast-ok-border: rgba(16, 185, 129, 0.42);
285
+ --zm-toast-ok-text: #a7f3d0;
286
+ --zm-toast-err-bg: rgba(76, 5, 25, 0.88);
287
+ --zm-toast-err-border: rgba(244, 63, 94, 0.42);
288
+ --zm-toast-err-text: #fecdd3;
289
+
290
+ --zm-ripple-color: rgba(129, 140, 248, 0.28);
291
+ --zm-scroll-thumb: rgba(148, 163, 184, 0.28);
292
+ }
293
+
294
+ * { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
295
+
296
+ @keyframes zm-spin {
297
+ from { transform: translate(-50%, -50%) rotate(0deg); }
298
+ to { transform: translate(-50%, -50%) rotate(360deg); }
299
+ }
300
+ @keyframes zm-breathe {
301
+ 0%, 100% { opacity: 0.42; transform: scale(0.99); }
302
+ 50% { opacity: 0.78; transform: scale(1.035); }
303
+ }
304
+ @keyframes zm-pulse-dot {
305
+ 0%, 100% { transform: scale(1); opacity: 1; }
306
+ 50% { transform: scale(1.26); opacity: 0.72; }
307
+ }
308
+ @keyframes zm-ripple-wave {
309
+ 0% { transform: translate(-50%, -50%) scale(0); opacity: 0.55; }
310
+ 100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
311
+ }
312
+ @keyframes zm-pane-in {
313
+ from { opacity: 0; transform: translateY(6px); }
314
+ to { opacity: 1; transform: translateY(0); }
315
+ }
316
+ @keyframes zm-shimmer {
317
+ from { background-position: 200% 0; }
318
+ to { background-position: -200% 0; }
319
+ }
320
+ @keyframes zm-bin-bounce {
321
+ 0% { transform: scale(1); }
322
+ 32% { transform: scale(1.18, 0.86); }
323
+ 62% { transform: scale(0.94, 1.08); }
324
+ 84% { transform: scale(1.04, 0.98); }
325
+ 100% { transform: scale(1); }
326
+ }
327
+ .zm-tab.bin-hit { animation: zm-bin-bounce 0.42s var(--zm-spring); }
328
+ .zm-card.zm-collapsing {
329
+ pointer-events: none;
330
+ overflow: hidden;
331
+ transition: height 0.26s var(--zm-ease), padding 0.26s var(--zm-ease), margin 0.26s var(--zm-ease), opacity 0.12s ease, border-width 0.26s ease;
332
+ }
333
+
334
+ .zm-ripple {
335
+ position: absolute;
336
+ border-radius: 50%;
337
+ pointer-events: none;
338
+ background: var(--zm-ripple-color);
339
+ animation: zm-ripple-wave 0.44s var(--zm-ease) forwards;
340
+ z-index: 4;
341
+ }
342
+ .zm-icon-btn.primary .zm-ripple { background: rgba(255, 255, 255, 0.34); }
343
+
344
+ /* ===== Floating Pill + Luminous Processing Halo ===== */
345
+ .zm-pill-shell {
346
+ position: fixed; right: 20px; bottom: 88px; pointer-events: auto;
347
+ border-radius: 999px; padding: 2.5px;
348
+ transition: transform 0.26s var(--zm-spring);
349
+ }
350
+ .zm-pill-shell:hover { transform: translateY(-2px) scale(1.015); }
351
+ .zm-pill-shell:active { transform: translateY(0) scale(0.955); transition-duration: 0.08s; }
352
+
353
+ .zm-pill-aura, .zm-pill-ring {
354
+ position: absolute; border-radius: 999px; pointer-events: none;
355
+ opacity: 0; overflow: hidden; transition: opacity 0.32s ease;
356
+ }
357
+ .zm-pill-aura { inset: -6px; filter: blur(12px); }
358
+ .zm-pill-ring { inset: 0; }
359
+ .zm-pill-aura::before, .zm-pill-ring::before {
360
+ content: ""; position: absolute; left: 50%; top: 50%; width: 240px; height: 240px;
361
+ background: conic-gradient(from 0deg, #38bdf8, #6366f1, #a855f7, #f472b6, #06b6d4, #38bdf8);
362
+ animation: zm-spin 2.0s linear infinite;
363
+ }
364
+ .zm-pill-shell.is-busy .zm-pill-ring { opacity: 1; }
365
+ .zm-pill-shell.is-busy .zm-pill-aura { opacity: 0.65; animation: zm-breathe 2.2s ease-in-out infinite; }
366
+
367
+ .zm-pill {
368
+ position: relative; z-index: 1; overflow: hidden;
369
+ display: inline-flex; align-items: center; gap: 7px;
370
+ height: 36px; padding: 0 13px 0 11px; border-radius: 999px;
371
+ background: var(--zm-pill-bg); color: var(--zm-text);
372
+ border: 1px solid var(--zm-pill-border); box-shadow: var(--zm-pill-shadow);
373
+ backdrop-filter: blur(20px) saturate(185%); -webkit-backdrop-filter: blur(20px) saturate(185%);
374
+ cursor: pointer; user-select: none; font-size: 12.5px; font-weight: 600;
375
+ transition: background 0.2s ease, border-color 0.2s ease;
376
+ }
377
+ .zm-pill-shell.is-busy .zm-pill { border-color: transparent; }
378
+ .zm-pill-icon { display: inline-flex; color: var(--zm-accent); }
379
+ .zm-pill-shell.is-busy .zm-pill-icon { animation: zm-pulse-dot 1.6s ease-in-out infinite; }
380
+
381
+ .zm-dot { width: 7.5px; height: 7.5px; border-radius: 50%; flex: none; background: #94a3b8; transition: background 0.2s, box-shadow 0.2s; }
382
+ .zm-dot.ok { background: #10b981; box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.18); }
383
+ .zm-dot.api { background: #3b82f6; box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2); }
384
+ .zm-dot.warn { background: #f59e0b; box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.2); }
385
+ .zm-dot.err { background: #f43f5e; box-shadow: 0 0 0 3px rgba(244, 63, 94, 0.2); }
386
+ .zm-dot.busy { background: #6366f1; box-shadow: 0 0 10px rgba(99, 102, 241, 0.72); animation: zm-pulse-dot 1.2s ease-in-out infinite; }
387
+
388
+ .zm-pill-cnt { padding: 1px 7px; border-radius: 999px; background: var(--zm-accent-soft); color: var(--zm-accent); font-size: 11px; font-weight: 700; min-width: 22px; text-align: center; }
389
+ .zm-pill-label { max-width: 82px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
390
+
391
+ /* ===== Backdrop & Frosted Glass Drawer ===== */
392
+ .zm-backdrop {
393
+ position: fixed; inset: 0; background: var(--zm-backdrop-bg);
394
+ backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
395
+ pointer-events: none; opacity: 0; transition: opacity 0.24s ease;
396
+ }
397
+ .zm-backdrop.open { opacity: 1; pointer-events: auto; }
398
+
399
+ .zm-drawer {
400
+ position: fixed; top: 14px; right: 14px; bottom: 14px;
401
+ width: min(462px, calc(100vw - 24px)); border-radius: 22px;
402
+ border: 1px solid var(--zm-drawer-border); box-shadow: var(--zm-drawer-shadow);
403
+ pointer-events: none; opacity: 0;
404
+ transform: translateX(26px) scale(0.98);
405
+ transition: transform 0.3s var(--zm-ease), opacity 0.22s ease;
406
+ display: flex; flex-direction: column; overflow: hidden; isolation: isolate;
407
+ }
408
+ .zm-drawer.open { opacity: 1; transform: translateX(0) scale(1); pointer-events: auto; }
409
+
410
+ .zm-bg-layer {
411
+ position: absolute; inset: -28px; z-index: 0; pointer-events: none;
412
+ background:
413
+ radial-gradient(circle at 12% 8%, rgba(125, 211, 252, 0.74) 0%, transparent 48%),
414
+ radial-gradient(circle at 90% 14%, rgba(196, 181, 253, 0.70) 0%, transparent 46%),
415
+ radial-gradient(circle at 86% 86%, rgba(253, 186, 116, 0.48) 0%, transparent 48%),
416
+ radial-gradient(circle at 14% 88%, rgba(110, 231, 183, 0.48) 0%, transparent 46%),
417
+ linear-gradient(148deg, #f5f9ff 0%, #eef4ff 48%, #f5f0ff 100%);
418
+ background-size: cover; background-position: center; opacity: 0.80;
419
+ transition: opacity 0.35s ease, filter 0.2s ease;
420
+ }
421
+ :host([data-theme="dark"]) .zm-bg-layer {
422
+ background:
423
+ radial-gradient(circle at 14% 12%, rgba(56, 189, 248, 0.32) 0%, transparent 46%),
424
+ radial-gradient(circle at 86% 18%, rgba(129, 140, 248, 0.32) 0%, transparent 46%),
425
+ radial-gradient(circle at 80% 88%, rgba(236, 72, 153, 0.18) 0%, transparent 44%),
426
+ radial-gradient(circle at 18% 82%, rgba(16, 185, 129, 0.18) 0%, transparent 44%),
427
+ linear-gradient(155deg, #0b1120 0%, #0f172a 52%, #131c36 100%);
428
+ background-size: cover; background-position: center; opacity: 0.86;
429
+ }
430
+ .zm-bg-layer.has-custom { opacity: 1; filter: blur(var(--zm-blur)) saturate(1.45) contrast(1.04); transform: scale(1.06); }
431
+
432
+ .zm-glass-veil {
433
+ position: absolute; inset: 0; z-index: 1; pointer-events: none;
434
+ background:
435
+ radial-gradient(120% 55% at 15% 0%, rgba(255, 255, 255, 0.52) 0%, transparent 62%),
436
+ linear-gradient(160deg, rgba(var(--zm-veil-r), var(--zm-veil-g), var(--zm-veil-b), var(--zm-tint-hi)) 0%, rgba(var(--zm-veil-r2), var(--zm-veil-g2), var(--zm-veil-b2), var(--zm-tint-lo)) 100%);
437
+ backdrop-filter: blur(var(--zm-blur)) saturate(185%) brightness(1.03);
438
+ -webkit-backdrop-filter: blur(var(--zm-blur)) saturate(185%) brightness(1.03);
439
+ }
440
+ :host([data-theme="dark"]) .zm-glass-veil {
441
+ background:
442
+ radial-gradient(120% 50% at 15% 0%, rgba(255, 255, 255, 0.08) 0%, transparent 58%),
443
+ linear-gradient(160deg, rgba(var(--zm-veil-r), var(--zm-veil-g), var(--zm-veil-b), var(--zm-tint-hi)) 0%, rgba(var(--zm-veil-r2), var(--zm-veil-g2), var(--zm-veil-b2), var(--zm-tint-lo)) 100%);
444
+ backdrop-filter: blur(var(--zm-blur)) saturate(165%) brightness(0.96);
445
+ -webkit-backdrop-filter: blur(var(--zm-blur)) saturate(165%) brightness(0.96);
446
+ }
447
+ .zm-glass-veil::after {
448
+ content: ""; position: absolute; inset: 0;
449
+ background-image: url("${NOISE_URI}"); background-size: 160px 160px;
450
+ opacity: 0.032; mix-blend-mode: overlay;
451
+ }
452
+
453
+ .zm-drawer-inner { position: relative; z-index: 2; display: flex; flex-direction: column; height: 100%; min-height: 0; }
454
+
455
+ /* Header */
456
+ .zm-head {
457
+ display: flex; align-items: center; justify-content: space-between; gap: 10px;
458
+ padding: 12px 15px 11px; border-bottom: 1px solid var(--zm-divider);
459
+ }
460
+ .zm-brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 auto; }
461
+ .zm-brand-orb {
462
+ position: relative; width: 34px; height: 34px; border-radius: 50%; padding: 2px;
463
+ flex: none; display: flex; align-items: center; justify-content: center;
464
+ }
465
+ .zm-orb-ring, .zm-orb-aura {
466
+ position: absolute; border-radius: 50%; overflow: hidden; opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
467
+ }
468
+ .zm-orb-ring { inset: 0; }
469
+ .zm-orb-aura { inset: -5px; filter: blur(8px); }
470
+ .zm-orb-ring::before, .zm-orb-aura::before {
471
+ content: ""; position: absolute; left: 50%; top: 50%; width: 62px; height: 62px;
472
+ background: conic-gradient(from 0deg, #38bdf8, #6366f1, #a855f7, #f472b6, #06b6d4, #38bdf8);
473
+ animation: zm-spin 1.7s linear infinite;
474
+ }
475
+ .zm-brand-orb.is-busy .zm-orb-ring { opacity: 1; }
476
+ .zm-brand-orb.is-busy .zm-orb-aura { opacity: 0.72; animation: zm-breathe 2s ease-in-out infinite; }
477
+ .zm-orb-core {
478
+ position: relative; z-index: 1; width: 100%; height: 100%; border-radius: 50%;
479
+ background: var(--zm-surface-strong); border: 1px solid var(--zm-surface-border);
480
+ color: var(--zm-accent); display: flex; align-items: center; justify-content: center;
481
+ box-shadow: 0 2px 8px var(--zm-surface-outer), inset 0 1px 0 var(--zm-surface-highlight);
482
+ }
483
+ .zm-brand-orb.is-busy .zm-orb-core { border-color: transparent; }
484
+
485
+ .zm-brand-meta { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; min-width: 0; flex: 1 1 auto; }
486
+ .zm-title { font-size: 14px; font-weight: 700; line-height: 1.1; color: var(--zm-text); letter-spacing: -0.01em; white-space: nowrap; }
487
+ .zm-mode-badge {
488
+ display: inline-flex; align-items: center; gap: 5px; padding: 1.5px 8px; border-radius: 999px;
489
+ font-size: 10.5px; font-weight: 600; line-height: 1.3;
490
+ background: var(--zm-surface); border: 1px solid var(--zm-surface-border); color: var(--zm-text-2);
491
+ max-width: 100%; overflow: hidden;
492
+ box-shadow: 0 1px 3px var(--zm-surface-outer), inset 0 1px 0 var(--zm-surface-highlight);
493
+ }
494
+ #zmModeText { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
495
+ .zm-head-actions { display: flex; align-items: center; gap: 5px; flex: none; }
496
+
497
+ /* Spring Tactile Buttons */
498
+ .zm-icon-btn {
499
+ position: relative; overflow: hidden;
500
+ display: inline-flex; align-items: center; justify-content: center; gap: 5px;
501
+ height: 32px; min-width: 32px; padding: 0 11px; border-radius: 10px;
502
+ border: 1px solid var(--zm-surface-border); background: var(--zm-surface); color: var(--zm-text-2);
503
+ cursor: pointer; font-size: 12px; font-weight: 600; line-height: 1; white-space: nowrap; flex-shrink: 0;
504
+ backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
505
+ box-shadow: 0 1px 4px var(--zm-surface-outer), inset 0 1px 0 var(--zm-surface-highlight);
506
+ transition: transform 0.24s var(--zm-spring), background 0.16s ease, border-color 0.16s ease, color 0.16s ease, box-shadow 0.18s ease;
507
+ }
508
+ .zm-icon-btn svg { flex: none; display: block; }
509
+ .zm-icon-btn:hover { background: var(--zm-surface-hover); border-color: var(--zm-accent-border); color: var(--zm-text); transform: translateY(-1px); }
510
+ .zm-icon-btn:active { transform: scale(0.945); transition-duration: 0.08s; box-shadow: 0 0 2px var(--zm-surface-outer); }
511
+ .zm-icon-btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
512
+ .zm-icon-btn.square { padding: 0; width: 32px; min-width: 32px; }
513
+ .zm-head-actions .zm-icon-btn { height: 30px; min-width: 30px; padding: 0 10px; border-radius: 9px; }
514
+ .zm-head-actions .zm-icon-btn.square { width: 30px; padding: 0; }
515
+ .zm-icon-btn.primary {
516
+ background: var(--zm-accent-grad); border-color: transparent; color: #ffffff;
517
+ box-shadow: 0 4px 14px -3px rgba(79, 70, 229, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.35);
518
+ }
519
+ .zm-icon-btn.primary:hover { filter: brightness(1.06); color: #ffffff; }
520
+ .zm-icon-btn.danger:hover, .zm-icon-btn.confirming {
521
+ background: var(--zm-danger-soft); border-color: var(--zm-danger-border); color: var(--zm-danger);
522
+ }
523
+ .zm-icon-btn.confirming { width: auto !important; padding: 0 9px !important; }
524
+ .zm-icon-btn.has-dot::after {
525
+ content: ""; position: absolute; top: 4px; right: 4px; width: 6px; height: 6px; border-radius: 50%;
526
+ background: var(--zm-accent); box-shadow: 0 0 0 2px var(--zm-surface-strong); z-index: 2;
527
+ }
528
+
529
+ /* Segmented Glass Tabs with Spring Glider */
530
+ .zm-tabs-wrap { padding: 9px 15px 6px; }
531
+ .zm-tabs {
532
+ position: relative; display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
533
+ padding: 4px; border-radius: 13px;
534
+ background: rgba(148, 163, 184, 0.14); border: 1px solid var(--zm-divider);
535
+ }
536
+ .zm-tab-glider {
537
+ position: absolute; top: 4px; left: 4px; bottom: 4px;
538
+ width: calc((100% - 16px) / 3); border-radius: 9.5px;
539
+ background: var(--zm-surface-strong); border: 1px solid var(--zm-surface-border);
540
+ box-shadow: 0 2px 10px var(--zm-surface-outer), inset 0 1px 0 var(--zm-surface-highlight);
541
+ transform: translateX(calc(var(--zm-tab-idx, 0) * (100% + 4px)));
542
+ transition: transform 0.32s var(--zm-glider-ease);
543
+ pointer-events: none; z-index: 0;
544
+ }
545
+ .zm-tab {
546
+ position: relative; z-index: 1; overflow: hidden;
547
+ display: inline-flex; align-items: center; justify-content: center; gap: 5px;
548
+ height: 31px; padding: 0 8px; border-radius: 9.5px; border: none; background: transparent;
549
+ color: var(--zm-muted); font-size: 12px; font-weight: 600; white-space: nowrap; cursor: pointer;
550
+ transition: color 0.18s ease, transform 0.22s var(--zm-spring);
551
+ }
552
+ .zm-tab:hover { color: var(--zm-text); }
553
+ .zm-tab:active { transform: scale(0.955); transition-duration: 0.08s; }
554
+ .zm-tab.active { color: var(--zm-text); }
555
+ .zm-tab-cnt {
556
+ font-size: 10.5px; font-weight: 700; color: var(--zm-accent); background: var(--zm-accent-soft);
557
+ padding: 0 6px; height: 17px; line-height: 17px; border-radius: 999px;
558
+ }
559
+
560
+ /* Scrollable Body */
561
+ .zm-body { flex: 1; overflow-y: auto; scrollbar-gutter: stable; padding: 8px 15px 16px; display: flex; flex-direction: column; gap: 10px; }
562
+ .zm-body::-webkit-scrollbar { width: 5px; }
563
+ .zm-body::-webkit-scrollbar-track { background: transparent; }
564
+ .zm-body::-webkit-scrollbar-thumb { background: var(--zm-scroll-thumb); border-radius: 999px; }
565
+
566
+ .zm-pane { display: none; flex-direction: column; gap: 9px; }
567
+ .zm-pane.active { display: flex; animation: zm-pane-in 0.22s var(--zm-ease); }
568
+
569
+ .zm-bar { display: flex; gap: 7px; align-items: center; min-width: 0; }
570
+ .zm-bar > .zm-input { flex: 1 1 auto; min-width: 0; width: auto; }
571
+ .zm-bar > .zm-search-wrap { flex: 1 1 auto; min-width: 0; }
572
+ .zm-bar > .zm-select { flex: 0 0 auto; }
573
+ .zm-bar > .zm-ns-dd { flex: 0 0 128px; width: 128px; }
574
+ .zm-bar > .zm-switch { flex: 0 0 auto; white-space: nowrap; height: 34px; }
575
+ .zm-bar > .zm-icon-btn { height: 34px; }
576
+ .zm-bar > .zm-icon-btn.square { width: 34px; min-width: 34px; }
577
+
578
+ /* Fixed-Width Frosted Glass Dropdown (zero layout shift on selection) */
579
+ .zm-ns-dd { position: relative; }
580
+ .zm-ns-trigger {
581
+ position: relative; overflow: hidden; width: 100%; height: 34px;
582
+ display: inline-flex; align-items: center; justify-content: space-between; gap: 6px;
583
+ padding: 0 9px 0 11px; border-radius: 10px;
584
+ background: var(--zm-input-bg); color: var(--zm-text);
585
+ border: 1px solid var(--zm-input-border); font-size: 12px; font-weight: 600;
586
+ cursor: pointer; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
587
+ transition: border-color 0.16s ease, background 0.16s ease, transform 0.22s var(--zm-spring), box-shadow 0.16s ease;
588
+ }
589
+ .zm-ns-trigger:hover { background: var(--zm-surface-strong); border-color: var(--zm-accent-border); }
590
+ .zm-ns-trigger:active { transform: scale(0.965); transition-duration: 0.08s; }
591
+ .zm-ns-dd.open .zm-ns-trigger {
592
+ border-color: var(--zm-input-focus); background: var(--zm-surface-strong);
593
+ box-shadow: 0 0 0 3px var(--zm-accent-soft);
594
+ }
595
+ .zm-ns-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; text-align: left; }
596
+ .zm-ns-chev { display: inline-flex; flex: none; color: var(--zm-muted); transition: transform 0.22s var(--zm-ease); }
597
+ .zm-ns-dd.open .zm-ns-chev { transform: rotate(180deg); color: var(--zm-accent); }
598
+
599
+ .zm-ns-menu {
600
+ position: absolute; top: calc(100% + 6px); right: 0; min-width: 172px; max-width: 240px; max-height: 220px;
601
+ overflow-y: auto; padding: 5px; border-radius: 13px; z-index: 30;
602
+ background: var(--zm-surface-strong); border: 1px solid var(--zm-surface-border);
603
+ box-shadow: 0 16px 36px -8px rgba(15, 23, 42, 0.22), 0 4px 12px rgba(15, 23, 42, 0.08), inset 0 1px 0 var(--zm-surface-highlight);
604
+ backdrop-filter: blur(24px) saturate(185%); -webkit-backdrop-filter: blur(24px) saturate(185%);
605
+ display: flex; flex-direction: column; gap: 2px;
606
+ opacity: 0; pointer-events: none; transform-origin: top right;
607
+ transform: translateY(-6px) scale(0.96);
608
+ transition: opacity 0.16s ease, transform 0.22s var(--zm-spring);
609
+ }
610
+ .zm-ns-dd.open .zm-ns-menu { opacity: 1; pointer-events: auto; transform: translateY(0) scale(1); }
611
+ .zm-ns-opt {
612
+ position: relative; overflow: hidden; width: 100%; border: none; background: transparent;
613
+ display: flex; align-items: center; justify-content: space-between; gap: 8px;
614
+ padding: 7px 9px; border-radius: 8.5px; color: var(--zm-text-2);
615
+ font-size: 12px; font-weight: 600; text-align: left; cursor: pointer;
616
+ transition: background 0.14s ease, color 0.14s ease, transform 0.18s var(--zm-spring);
617
+ }
618
+ .zm-ns-opt:hover { background: var(--zm-accent-soft); color: var(--zm-text); }
619
+ .zm-ns-opt:active { transform: scale(0.975); transition-duration: 0.07s; }
620
+ .zm-ns-opt.active { background: var(--zm-accent-soft); color: var(--zm-accent); }
621
+ .zm-ns-opt-txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
622
+ .zm-ns-opt-chk { flex: none; display: inline-flex; opacity: 0; color: var(--zm-accent); }
623
+ .zm-ns-opt.active .zm-ns-opt-chk { opacity: 1; }
624
+
625
+ .zm-search-wrap { position: relative; flex: 1 1 auto; min-width: 0; display: flex; align-items: center; }
626
+ .zm-search-ic { position: absolute; left: 10px; color: var(--zm-muted); display: inline-flex; pointer-events: none; }
627
+ .zm-search-wrap .zm-input { padding-left: 31px; }
628
+
629
+ .zm-input, .zm-select, .zm-textarea {
630
+ width: 100%; background: var(--zm-input-bg); color: var(--zm-text);
631
+ border: 1px solid var(--zm-input-border); border-radius: 10px; padding: 0 11px;
632
+ font-size: 12.5px; outline: none; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
633
+ transition: border-color 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
634
+ }
635
+ .zm-input, .zm-select { height: 34px; line-height: 32px; }
636
+ .zm-input::placeholder, .zm-textarea::placeholder { color: var(--zm-muted); }
637
+ .zm-input:focus, .zm-select:focus, .zm-textarea:focus {
638
+ border-color: var(--zm-input-focus); background: var(--zm-surface-strong); box-shadow: 0 0 0 3px var(--zm-accent-soft);
639
+ }
640
+ .zm-select { width: auto; max-width: 136px; cursor: pointer; }
641
+ .zm-textarea { min-height: 86px; padding: 9px 11px; resize: vertical; line-height: 1.5; }
642
+ .zm-meta-row { display: flex; align-items: center; justify-content: space-between; font-size: 11px; color: var(--zm-muted); padding: 0 2px; }
643
+
644
+ /* Smooth Accordion Quick Add */
645
+ .zm-add-wrap {
646
+ display: grid; grid-template-rows: 0fr; opacity: 0; margin-top: -10px;
647
+ transition: grid-template-rows 0.28s var(--zm-ease), opacity 0.2s ease, margin-top 0.24s ease;
648
+ }
649
+ .zm-add-wrap.open { grid-template-rows: 1fr; opacity: 1; margin-top: 0; }
650
+ .zm-add-clip { overflow: hidden; }
651
+ .zm-add-box {
652
+ background: var(--zm-surface-strong); border: 1px solid var(--zm-accent-border);
653
+ border-radius: 14px; padding: 10px 11px; display: flex; flex-direction: column; gap: 8px;
654
+ box-shadow: 0 6px 20px -6px rgba(79, 70, 229, 0.16);
655
+ }
656
+
657
+ /* Secondary Frosted Cards + Smooth Evidence Expand */
658
+ .zm-card {
659
+ background: var(--zm-surface); border: 1px solid var(--zm-surface-border);
660
+ border-radius: 14px; padding: 11px 12px; display: flex; flex-direction: column; gap: 6px;
661
+ backdrop-filter: blur(14px) saturate(150%); -webkit-backdrop-filter: blur(14px) saturate(150%);
662
+ box-shadow: 0 3px 14px -4px var(--zm-surface-outer), inset 0 1px 0 var(--zm-surface-highlight);
663
+ transition: transform 0.24s var(--zm-spring), background 0.18s ease, border-color 0.18s ease, box-shadow 0.2s ease;
664
+ }
665
+ .zm-card:hover { background: var(--zm-surface-hover); border-color: var(--zm-accent-border); transform: translateY(-1px); }
666
+ .zm-card-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
667
+ .zm-tags { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; }
668
+ .zm-tag {
669
+ font-size: 10.5px; font-weight: 600; padding: 1.5px 7px; border-radius: 999px; white-space: nowrap;
670
+ background: var(--zm-tag-bg); color: var(--zm-tag-text); border: 1px solid var(--zm-tag-border);
671
+ }
672
+ .zm-tag.rev { background: var(--zm-rev-bg); color: var(--zm-rev-text); border-color: var(--zm-rev-border); }
673
+ .zm-tag.exp { background: var(--zm-exp-bg); color: var(--zm-exp-text); border-color: var(--zm-exp-border); }
674
+
675
+ .zm-fact-text {
676
+ font-size: 12.8px; line-height: 1.56; color: var(--zm-text); word-break: break-word; cursor: pointer;
677
+ border-radius: 6px; transition: opacity 0.14s ease;
678
+ }
679
+ .zm-fact-text:active { opacity: 0.78; }
680
+
681
+ .zm-evidence-wrap {
682
+ display: grid; grid-template-rows: 0fr; opacity: 0; margin-top: 0;
683
+ transition: grid-template-rows 0.26s var(--zm-ease), opacity 0.2s ease, margin-top 0.22s ease;
684
+ }
685
+ .zm-evidence-wrap.open { grid-template-rows: 1fr; opacity: 1; margin-top: 3px; }
686
+ .zm-evidence-clip { overflow: hidden; }
687
+ .zm-evidence {
688
+ padding: 8px 10px; border-radius: 10px; background: var(--zm-input-bg);
689
+ border: 1px solid var(--zm-divider); font-size: 11.5px; color: var(--zm-text-2); line-height: 1.5;
690
+ }
691
+ .zm-ev-item { margin-bottom: 5px; }
692
+ .zm-ev-item:last-child { margin-bottom: 0; }
693
+ .zm-ev-id { color: var(--zm-accent); font-weight: 700; margin-right: 5px; }
694
+ .zm-redact {
695
+ background: var(--zm-exp-bg); color: var(--zm-exp-text); border: 1px solid var(--zm-exp-border);
696
+ padding: 0 4px; border-radius: 5px; font-weight: 700;
697
+ }
698
+
699
+ /* Minimal Appearance Card (Theme + Wallpaper Upload ONLY) */
700
+ .zm-glass-section {
701
+ background: var(--zm-surface); border: 1px solid var(--zm-surface-border);
702
+ border-radius: 15px; padding: 11px 12px; display: flex; flex-direction: column; gap: 9px;
703
+ backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
704
+ box-shadow: 0 3px 14px -4px var(--zm-surface-outer), inset 0 1px 0 var(--zm-surface-highlight);
705
+ }
706
+ .zm-section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
707
+ .zm-section-title { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; color: var(--zm-text); }
708
+
709
+ .zm-theme-seg {
710
+ position: relative; display: grid; grid-template-columns: 1fr 1fr; padding: 3px; border-radius: 10px;
711
+ background: rgba(148, 163, 184, 0.16); border: 1px solid var(--zm-divider); gap: 2px; min-width: 104px;
712
+ }
713
+ .zm-theme-glider {
714
+ position: absolute; top: 3px; left: 3px; bottom: 3px; width: calc((100% - 8px) / 2);
715
+ border-radius: 7.5px; background: var(--zm-surface-strong); border: 1px solid var(--zm-surface-border);
716
+ box-shadow: 0 1px 5px var(--zm-surface-outer);
717
+ transform: translateX(calc(var(--zm-theme-idx, 0) * (100% + 2px)));
718
+ transition: transform 0.3s var(--zm-glider-ease); pointer-events: none; z-index: 0;
719
+ }
720
+ .zm-theme-opt {
721
+ position: relative; z-index: 1; overflow: hidden;
722
+ display: inline-flex; align-items: center; justify-content: center; gap: 4px;
723
+ height: 24px; padding: 0 9px; border-radius: 7.5px; border: none; background: transparent;
724
+ color: var(--zm-muted); font-size: 11px; font-weight: 600; white-space: nowrap; cursor: pointer;
725
+ transition: color 0.18s ease, transform 0.2s var(--zm-spring);
726
+ }
727
+ .zm-theme-opt:active { transform: scale(0.93); transition-duration: 0.08s; }
728
+ .zm-theme-opt.active { color: var(--zm-text); }
729
+
730
+ .zm-bg-drop {
731
+ position: relative; height: 64px; border-radius: 12px;
732
+ border: 1.5px dashed var(--zm-input-border); background: var(--zm-input-bg);
733
+ overflow: hidden; display: flex; align-items: center; justify-content: space-between;
734
+ padding: 8px 10px; gap: 8px; cursor: pointer;
735
+ transition: border-color 0.18s ease, transform 0.24s var(--zm-spring), background 0.18s ease;
736
+ }
737
+ .zm-bg-drop:hover, .zm-bg-drop.dragover { border-color: var(--zm-accent); background: var(--zm-surface-hover); }
738
+ .zm-bg-drop:active { transform: scale(0.985); transition-duration: 0.08s; }
739
+ .zm-bg-preview { position: absolute; inset: 0; background-size: cover; background-position: center; opacity: 0.48; pointer-events: none; }
740
+ .zm-bg-drop-info { position: relative; z-index: 1; display: flex; align-items: center; gap: 9px; min-width: 0; }
741
+ .zm-bg-badge {
742
+ width: 34px; height: 34px; border-radius: 10px; background: var(--zm-surface-strong);
743
+ border: 1px solid var(--zm-surface-border); color: var(--zm-accent);
744
+ display: flex; align-items: center; justify-content: center; flex: none;
745
+ }
746
+ .zm-bg-drop-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
747
+ .zm-bg-drop-title { font-size: 12px; font-weight: 700; color: var(--zm-text); }
748
+ .zm-bg-drop-sub { font-size: 10.5px; color: var(--zm-muted); }
749
+ .zm-bg-drop-actions { position: relative; z-index: 1; display: flex; align-items: center; gap: 5px; flex: none; }
750
+
751
+ /* Auto-Detect Local Setup Banner & Card */
752
+ .zm-setup-banner, .zm-setup-card {
753
+ background: var(--zm-surface-strong); border: 1px solid var(--zm-accent-border);
754
+ border-radius: 13px; padding: 10px 11px; display: none; flex-direction: column; gap: 8px;
755
+ box-shadow: 0 4px 16px -6px rgba(79, 70, 229, 0.2), inset 0 1px 0 var(--zm-surface-highlight);
756
+ }
757
+ .zm-setup-banner.show, .zm-setup-card.show { display: flex; animation: zm-pane-in 0.22s var(--zm-ease); }
758
+ .zm-setup-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
759
+ .zm-setup-title { font-size: 12px; font-weight: 700; color: var(--zm-text); display: inline-flex; align-items: center; gap: 6px; }
760
+ .zm-setup-sub { font-size: 11px; color: var(--zm-muted); line-height: 1.45; }
761
+ .zm-setup-btns { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
762
+ .zm-prog-track {
763
+ width: 100%; height: 6px; border-radius: 999px; background: var(--zm-accent-soft); overflow: hidden;
764
+ }
765
+ .zm-prog-fill {
766
+ height: 100%; width: 0%; border-radius: 999px;
767
+ background: linear-gradient(90deg, #38bdf8, #6366f1, #a855f7, #38bdf8);
768
+ background-size: 200% 100%; animation: zm-shimmer 2.2s linear infinite;
769
+ transition: width 0.26s var(--zm-ease);
770
+ }
771
+
772
+ /* Provider Switch Cards (clean, no subtitle) */
773
+ .zm-provider-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
774
+ .zm-pcard {
775
+ position: relative; overflow: hidden; padding: 11px 12px; border-radius: 13px;
776
+ border: 1px solid var(--zm-surface-border); background: var(--zm-surface);
777
+ cursor: pointer; color: var(--zm-text-2);
778
+ display: flex; align-items: center; justify-content: center;
779
+ backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
780
+ transition: transform 0.24s var(--zm-spring), background 0.16s ease, border-color 0.16s ease, box-shadow 0.18s ease;
781
+ }
782
+ .zm-pcard:hover { background: var(--zm-surface-hover); transform: translateY(-1px); }
783
+ .zm-pcard:active { transform: scale(0.96); transition-duration: 0.08s; }
784
+ .zm-pcard.active {
785
+ border-color: var(--zm-accent-border); background: var(--zm-surface-strong); color: var(--zm-text);
786
+ box-shadow: 0 4px 16px -4px rgba(79, 70, 229, 0.22), inset 0 0 0 1px var(--zm-accent-soft);
787
+ }
788
+ .zm-pcard-h { display: flex; align-items: center; gap: 7px; font-size: 12.8px; font-weight: 700; white-space: nowrap; }
789
+ .zm-pcard.active .zm-pcard-h svg { color: var(--zm-accent); }
790
+
791
+ /* Dedicated Local Model One-Click Install Option Row */
792
+ .zm-install-opt {
793
+ display: flex; align-items: center; justify-content: space-between; gap: 8px;
794
+ padding: 9px 11px; border-radius: 13px;
795
+ background: var(--zm-surface-strong); border: 1px solid var(--zm-accent-border);
796
+ box-shadow: 0 3px 12px -4px rgba(79, 70, 229, 0.16), inset 0 1px 0 var(--zm-surface-highlight);
797
+ }
798
+ .zm-install-meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
799
+ .zm-install-title { font-size: 12px; font-weight: 700; color: var(--zm-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
800
+ .zm-install-author { font-size: 10.5px; font-weight: 600; color: var(--zm-accent); }
801
+
802
+ /* Frosted Glass Confirm Modal inside Drawer */
803
+ .zm-modal-mask {
804
+ position: absolute; inset: 0; z-index: 40;
805
+ background: rgba(15, 23, 42, 0.24);
806
+ backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
807
+ display: flex; align-items: center; justify-content: center; padding: 18px;
808
+ opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
809
+ }
810
+ .zm-modal-mask.open { opacity: 1; pointer-events: auto; }
811
+ .zm-modal {
812
+ width: 100%; max-width: 368px; border-radius: 18px; padding: 15px 15px 14px;
813
+ background: var(--zm-surface-strong); border: 1px solid var(--zm-surface-border);
814
+ box-shadow: 0 24px 56px -12px rgba(15, 23, 42, 0.28), inset 0 1px 0 var(--zm-surface-highlight);
815
+ backdrop-filter: blur(26px) saturate(185%); -webkit-backdrop-filter: blur(26px) saturate(185%);
816
+ display: flex; flex-direction: column; gap: 11px;
817
+ transform: scale(0.93) translateY(8px);
818
+ transition: transform 0.28s var(--zm-spring);
819
+ }
820
+ .zm-modal-mask.open .zm-modal { transform: scale(1) translateY(0); }
821
+ .zm-modal-title { font-size: 14px; font-weight: 700; color: var(--zm-text); display: flex; align-items: center; gap: 7px; }
822
+ .zm-modal-sub { font-size: 11.5px; color: var(--zm-text-2); line-height: 1.5; }
823
+ .zm-ver-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
824
+ .zm-ver-card {
825
+ position: relative; overflow: hidden; padding: 9px 10px; border-radius: 12px;
826
+ border: 1px solid var(--zm-surface-border); background: var(--zm-surface);
827
+ color: var(--zm-text-2); text-align: left; cursor: pointer;
828
+ display: flex; flex-direction: column; gap: 2px;
829
+ transition: transform 0.22s var(--zm-spring), border-color 0.16s ease, background 0.16s ease;
830
+ }
831
+ .zm-ver-card:hover { background: var(--zm-surface-hover); }
832
+ .zm-ver-card:active { transform: scale(0.96); transition-duration: 0.08s; }
833
+ .zm-ver-card.active {
834
+ border-color: var(--zm-accent-border); background: var(--zm-accent-soft); color: var(--zm-text);
835
+ box-shadow: inset 0 0 0 1px var(--zm-accent-border);
836
+ }
837
+ .zm-ver-name { font-size: 12px; font-weight: 700; }
838
+ .zm-ver-size { font-size: 10.5px; color: var(--zm-muted); }
839
+ .zm-modal-note {
840
+ font-size: 11px; color: var(--zm-muted); line-height: 1.45;
841
+ padding: 7px 9px; border-radius: 10px; background: var(--zm-input-bg); border: 1px solid var(--zm-divider);
842
+ }
843
+ .zm-modal-foot { display: flex; align-items: center; justify-content: flex-end; gap: 7px; flex-wrap: wrap; }
844
+
845
+ .zm-field { display: flex; flex-direction: column; gap: 4px; }
846
+ .zm-label { font-size: 11px; font-weight: 600; color: var(--zm-muted); display: flex; justify-content: space-between; align-items: center; }
847
+ .zm-presets { display: flex; flex-wrap: wrap; gap: 5px; }
848
+ .zm-chip {
849
+ position: relative; overflow: hidden; padding: 4px 10px; border-radius: 999px;
850
+ border: 1px solid var(--zm-surface-border); background: var(--zm-surface); color: var(--zm-text-2);
851
+ font-size: 11px; font-weight: 600; white-space: nowrap; cursor: pointer;
852
+ transition: transform 0.22s var(--zm-spring), border-color 0.14s ease, background 0.14s ease, color 0.14s ease;
853
+ }
854
+ .zm-chip:hover { border-color: var(--zm-accent-border); background: var(--zm-surface-strong); color: var(--zm-accent); transform: translateY(-1px); }
855
+ .zm-chip.active { border-color: var(--zm-accent-border); background: var(--zm-accent-soft); color: var(--zm-accent); }
856
+ .zm-chip:active { transform: scale(0.93); transition-duration: 0.08s; }
857
+ .zm-custom-hint { font-size: 10.5px; color: var(--zm-muted); line-height: 1.45; display: none; }
858
+ .zm-custom-hint.show { display: block; animation: zm-pane-in 0.18s var(--zm-ease); }
859
+
860
+ .zm-switches { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
861
+ .zm-switch {
862
+ display: flex; align-items: center; justify-content: space-between; gap: 6px;
863
+ padding: 7.5px 10px; border-radius: 11px; background: var(--zm-surface);
864
+ border: 1px solid var(--zm-surface-border); font-size: 11.5px; font-weight: 600;
865
+ color: var(--zm-text-2); cursor: pointer;
866
+ transition: background 0.14s ease, transform 0.2s var(--zm-spring);
867
+ }
868
+ .zm-switch:hover { background: var(--zm-surface-hover); color: var(--zm-text); }
869
+ .zm-switch:active { transform: scale(0.97); transition-duration: 0.08s; }
870
+ .zm-switch input { accent-color: var(--zm-accent); cursor: pointer; }
871
+
872
+ .zm-toast {
873
+ padding: 8px 11px; border-radius: 11px; font-size: 11.5px; font-weight: 600; line-height: 1.45;
874
+ display: none; backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
875
+ }
876
+ .zm-toast.ok { display: block; animation: zm-pane-in 0.18s var(--zm-ease); background: var(--zm-toast-ok-bg); border: 1px solid var(--zm-toast-ok-border); color: var(--zm-toast-ok-text); }
877
+ .zm-toast.err { display: block; animation: zm-pane-in 0.18s var(--zm-ease); background: var(--zm-toast-err-bg); border: 1px solid var(--zm-toast-err-border); color: var(--zm-toast-err-text); }
878
+
879
+ .zm-empty {
880
+ text-align: center; padding: 36px 14px; color: var(--zm-muted); font-size: 12px; line-height: 1.65;
881
+ border-radius: 14px; background: var(--zm-surface); border: 1px dashed var(--zm-input-border);
882
+ }
883
+
884
+ details.zm-help {
885
+ background: var(--zm-surface); border: 1px solid var(--zm-surface-border);
886
+ border-radius: 12px; padding: 8px 11px; font-size: 11px; color: var(--zm-muted);
887
+ }
888
+ details.zm-help summary { cursor: pointer; color: var(--zm-text-2); font-weight: 600; }
889
+ details.zm-help code { background: var(--zm-accent-soft); padding: 1px 5px; border-radius: 5px; color: var(--zm-accent); font-size: 10.5px; }
890
+
891
+ @media (prefers-reduced-motion: reduce) {
892
+ .zm-pill-ring::before, .zm-pill-aura::before, .zm-orb-ring::before, .zm-orb-aura::before { animation-duration: 12s; }
893
+ .zm-ripple { display: none; }
894
+ }
895
+ </style>
896
+
897
+ <!-- Floating Draggable Glass Pill with Processing Halo -->
898
+ <div class="zm-pill-shell" id="zmPillShell">
899
+ <div class="zm-pill-aura" aria-hidden="true"></div>
900
+ <div class="zm-pill-ring" aria-hidden="true"></div>
901
+ <button class="zm-pill" id="zmPill" type="button" aria-label="打开 ZeroMem 记忆工作台" title="ZeroMem 记忆工作台(可拖动)">
902
+ <span class="zm-dot" id="zmPillDot"></span>
903
+ <span class="zm-pill-icon">${ICONS.brain}</span>
904
+ <span class="zm-pill-label" id="zmPillLabel">零忆</span>
905
+ <span class="zm-pill-cnt" id="zmPillCnt">0</span>
906
+ </button>
907
+ </div>
908
+
909
+ <div class="zm-backdrop" id="zmBackdrop"></div>
910
+
911
+ <!-- Embedded Frosted Glass Drawer -->
912
+ <aside class="zm-drawer" id="zmDrawer" role="dialog" aria-label="ZeroMem 记忆工作台">
913
+ <div class="zm-bg-layer" id="zmBgLayer" aria-hidden="true"></div>
914
+ <div class="zm-glass-veil" aria-hidden="true"></div>
915
+
916
+ <div class="zm-drawer-inner">
917
+ <header class="zm-head">
918
+ <div class="zm-brand">
919
+ <div class="zm-brand-orb" id="zmBrandOrb" aria-hidden="true">
920
+ <div class="zm-orb-aura"></div>
921
+ <div class="zm-orb-ring"></div>
922
+ <div class="zm-orb-core">${ICONS.brain}</div>
923
+ </div>
924
+ <div class="zm-brand-meta">
925
+ <span class="zm-title">ZeroMem</span>
926
+ <span class="zm-mode-badge" id="zmModeBadge" title="当前后端状态">
927
+ <span class="zm-dot" id="zmHeadDot"></span>
928
+ <span id="zmModeText">加载中</span>
929
+ </span>
930
+ </div>
931
+ </div>
932
+
933
+ <div class="zm-head-actions">
934
+ <button class="zm-icon-btn primary" id="zmAddToggle" type="button" aria-label="写入记忆" title="手动写入一条记忆">${ICONS.plus}<span>写入</span></button>
935
+ <button class="zm-icon-btn square" id="zmBgQuickBtn" type="button" aria-label="上传背景图" title="上传自定义背景图">${ICONS.image}</button>
936
+ <button class="zm-icon-btn square" id="zmThemeBtn" type="button" aria-label="切换亮暗主题" title="切换亮色 / 暗色">${ICONS.moon}</button>
937
+ <button class="zm-icon-btn square" id="zmRefreshBtn" type="button" aria-label="刷新" title="刷新状态与列表">${ICONS.refresh}</button>
938
+ <button class="zm-icon-btn square" id="zmCloseBtn" type="button" aria-label="关闭" title="关闭 (Esc)">${ICONS.close}</button>
939
+ </div>
940
+ </header>
941
+
942
+ <input id="zmBgFileInput" type="file" accept="image/png,image/jpeg,image/webp,image/gif,image/avif" style="display:none;" />
943
+
944
+ <div class="zm-tabs-wrap">
945
+ <nav class="zm-tabs" id="zmTabsNav" role="tablist">
946
+ <span class="zm-tab-glider" aria-hidden="true"></span>
947
+ <button class="zm-tab active" data-tab="memories" data-idx="0" type="button" role="tab" aria-selected="true">${ICONS.search}<span>记忆</span><span class="zm-tab-cnt" id="zmTabMemCnt">0</span></button>
948
+ <button class="zm-tab" data-tab="recycle" data-idx="1" type="button" role="tab" aria-selected="false">${ICONS.restore}<span>回收站</span><span class="zm-tab-cnt" id="zmTabRecCnt">0</span></button>
949
+ <button class="zm-tab" data-tab="settings" data-idx="2" type="button" role="tab" aria-selected="false">${ICONS.sliders}<span>设置</span></button>
950
+ </nav>
951
+ </div>
952
+
953
+ <div class="zm-body" id="zmBody">
954
+ <div class="zm-toast" id="zmToast"></div>
955
+
956
+ <!-- Smooth Accordion Quick Add -->
957
+ <div class="zm-add-wrap" id="zmAddWrap">
958
+ <div class="zm-add-clip">
959
+ <section class="zm-add-box" id="zmAddBox">
960
+ <textarea class="zm-textarea" id="zmAddText" placeholder="输入决议、配置或笔记(敏感密钥自动脱敏为 [REDACTED])..."></textarea>
961
+ <div class="zm-bar">
962
+ <input class="zm-input" id="zmAddNs" placeholder="空间名(默认 default)" />
963
+ <label class="zm-switch" style="padding:0 10px;" title="入库前 1 轮原文对照审校">
964
+ <span>审校</span>
965
+ <input type="checkbox" id="zmAddVerify" checked />
966
+ </label>
967
+ <button class="zm-icon-btn primary" id="zmAddSubmit" type="button">${ICONS.check}<span>入库</span></button>
968
+ </div>
969
+ </section>
970
+ </div>
971
+ </div>
972
+
973
+ <!-- Tab 1: Memories -->
974
+ <section id="zmPaneMemories" class="zm-pane active">
975
+ <div class="zm-setup-banner" id="zmTopSetupBanner"></div>
976
+ <div class="zm-bar">
977
+ <div class="zm-search-wrap">
978
+ <span class="zm-search-ic">${ICONS.search}</span>
979
+ <input class="zm-input" id="zmSearchInput" placeholder="检索事实与原文..." />
980
+ </div>
981
+ <div class="zm-ns-dd" id="zmNsDd">
982
+ <button class="zm-ns-trigger" id="zmNsTrigger" type="button" aria-haspopup="listbox" aria-expanded="false" title="全部空间">
983
+ <span class="zm-ns-label" id="zmNsLabel">全部空间</span>
984
+ <span class="zm-ns-chev">${ICONS.chevron}</span>
985
+ </button>
986
+ <div class="zm-ns-menu" id="zmNsMenu" role="listbox" aria-label="空间筛选"></div>
987
+ </div>
988
+ </div>
989
+ <div class="zm-meta-row">
990
+ <span id="zmSearchMeta">就绪</span>
991
+ <span id="zmStatsMeta"></span>
992
+ </div>
993
+ <div id="zmList" style="display:flex;flex-direction:column;gap:8px;"></div>
994
+ </section>
995
+
996
+ <!-- Tab 2: Recycle Bin -->
997
+ <section id="zmPaneRecycle" class="zm-pane">
998
+ <div class="zm-meta-row">
999
+ <span>软淘汰保留原文与 SHA-256 指纹</span>
1000
+ <button class="zm-icon-btn" id="zmEvictExpiredBtn" type="button" title="将已过期的 until: 记忆移入回收站">${ICONS.trash}<span>清过期</span></button>
1001
+ </div>
1002
+ <div id="zmRecycleList" style="display:flex;flex-direction:column;gap:8px;"></div>
1003
+ </section>
1004
+
1005
+ <!-- Tab 3: Settings -->
1006
+ <section id="zmPaneSettings" class="zm-pane">
1007
+ <!-- Minimal Appearance Card: ONLY Light/Dark + Wallpaper Upload -->
1008
+ <div class="zm-glass-section">
1009
+ <div class="zm-section-head">
1010
+ <span class="zm-section-title">${ICONS.image}<span>外观</span></span>
1011
+ <div class="zm-theme-seg" id="zmThemeSeg" role="group" aria-label="主题模式">
1012
+ <span class="zm-theme-glider" aria-hidden="true"></span>
1013
+ <button class="zm-theme-opt active" id="zmThemeLightBtn" type="button" title="亮色模式">${ICONS.sun}<span>亮</span></button>
1014
+ <button class="zm-theme-opt" id="zmThemeDarkBtn" type="button" title="暗色模式">${ICONS.moon}<span>暗</span></button>
1015
+ </div>
1016
+ </div>
1017
+
1018
+ <div class="zm-bg-drop" id="zmBgDrop" role="button" tabindex="0" aria-label="上传或拖拽自定义背景图" title="点击选择或拖入背景图片">
1019
+ <div class="zm-bg-preview" id="zmBgPreview"></div>
1020
+ <div class="zm-bg-drop-info">
1021
+ <div class="zm-bg-badge">${ICONS.upload}</div>
1022
+ <div class="zm-bg-drop-text">
1023
+ <span class="zm-bg-drop-title" id="zmBgDropTitle">上传背景图</span>
1024
+ <span class="zm-bg-drop-sub" id="zmBgDropSub">点击或拖入 · 自动毛玻璃</span>
1025
+ </div>
1026
+ </div>
1027
+ <div class="zm-bg-drop-actions">
1028
+ <button class="zm-icon-btn square danger" id="zmClearBgBtn" type="button" aria-label="清除背景图" title="恢复默认毛玻璃" style="display:none;">${ICONS.trash}</button>
1029
+ </div>
1030
+ </div>
1031
+ </div>
1032
+
1033
+ <!-- Dual Backend Switch -->
1034
+ <div class="zm-provider-grid">
1035
+ <button class="zm-pcard" id="zmPickOllama" type="button">
1036
+ <div class="zm-pcard-h">${ICONS.cpu}<span>本地 Ollama</span></div>
1037
+ </button>
1038
+ <button class="zm-pcard" id="zmPickApi" type="button">
1039
+ <div class="zm-pcard-h">${ICONS.cloud}<span>云端 API</span></div>
1040
+ </button>
1041
+ </div>
1042
+
1043
+ <!-- Local Ollama Auto-Detect & Pull Progress Card -->
1044
+ <div class="zm-setup-card" id="zmLocalSetupCard"></div>
1045
+
1046
+ <!-- Local Ollama Fields -->
1047
+ <div id="zmOllamaFields" style="display:flex;flex-direction:column;gap:8px;">
1048
+ <div class="zm-install-opt">
1049
+ <div class="zm-install-meta">
1050
+ <span class="zm-install-title">一键安装开源专项小模型</span>
1051
+ <span class="zm-install-author">作者 @B站IM榴莲</span>
1052
+ </div>
1053
+ <button class="zm-icon-btn primary" id="zmOpenInstallModalBtn" type="button">${ICONS.download}<span>一键安装</span></button>
1054
+ </div>
1055
+ <div class="zm-field">
1056
+ <label class="zm-label"><span>Ollama 地址(免代理直连)</span></label>
1057
+ <input class="zm-input" id="zmOllamaUrl" placeholder="http://127.0.0.1:11434" />
1058
+ </div>
1059
+ <div class="zm-field">
1060
+ <label class="zm-label"><span>模型名</span><span id="zmOllamaAvail"></span></label>
1061
+ <input class="zm-input" id="zmOllamaModel" list="zmOllamaModelList" placeholder="留空自动探测 V2" />
1062
+ <datalist id="zmOllamaModelList"></datalist>
1063
+ </div>
1064
+ <div class="zm-field">
1065
+ <label class="zm-label"><span>上下文 (num_ctx)</span></label>
1066
+ <select class="zm-select" id="zmNumCtx" style="max-width:none;width:100%;">
1067
+ <option value="4096">4096(低显存)</option>
1068
+ <option value="8192">8192(标准推荐)</option>
1069
+ <option value="16384">16384(大显存/常驻)</option>
1070
+ </select>
1071
+ </div>
1072
+ </div>
1073
+
1074
+ <!-- OpenAI-compatible API Fields -->
1075
+ <div id="zmApiFields" style="display:none;flex-direction:column;gap:8px;">
1076
+ <div class="zm-presets" id="zmPresets"></div>
1077
+ <div class="zm-custom-hint" id="zmCustomApiHint">支持任意 OpenAI 兼容网关(/v1 或完整 /chat/completions),本地回环地址可留空 Key</div>
1078
+ <div class="zm-field">
1079
+ <label class="zm-label"><span>API Base URL</span></label>
1080
+ <input class="zm-input" id="zmApiBaseUrl" placeholder="https://api.deepseek.com/v1" />
1081
+ </div>
1082
+ <div class="zm-field">
1083
+ <label class="zm-label"><span>模型名</span></label>
1084
+ <input class="zm-input" id="zmApiModel" placeholder="deepseek-chat" />
1085
+ </div>
1086
+ <div class="zm-field">
1087
+ <label class="zm-label">
1088
+ <span>API Key(本机保存,不回显)</span>
1089
+ <span id="zmApiKeyHint"></span>
1090
+ </label>
1091
+ <div class="zm-bar">
1092
+ <input class="zm-input" id="zmApiKey" type="password" placeholder="输入新 Key(留空保持不变)" autocomplete="off" />
1093
+ <button class="zm-icon-btn square danger" id="zmClearKeyBtn" type="button" aria-label="清除已存 Key" title="清除已存 Key">${ICONS.trash}</button>
1094
+ </div>
1095
+ </div>
1096
+ </div>
1097
+
1098
+ <!-- Behavior Switches -->
1099
+ <div class="zm-switches">
1100
+ <label class="zm-switch" title="发消息前毫秒级 FTS5 自动注入相关历史"><span>自动召回</span><input type="checkbox" id="zmCfgAutoRecall" /></label>
1101
+ <label class="zm-switch" title="对话轮次结束后后台异步抽取沉淀"><span>自动沉淀</span><input type="checkbox" id="zmCfgAutoRemember" /></label>
1102
+ <label class="zm-switch" title="入库前运行 1 轮原文对照审校"><span>写前审校</span><input type="checkbox" id="zmCfgVerify" /></label>
1103
+ <label class="zm-switch" title="带 until:YYYY-MM-DD 过期记忆自动后移"><span>时效后移</span><input type="checkbox" id="zmCfgUntil" /></label>
1104
+ <label class="zm-switch" title="检索按头名 0.35 相对分数动态保留 2-8 条"><span>动态 2-8</span><input type="checkbox" id="zmCfgAdaptive" /></label>
1105
+ <label class="zm-switch" title="按当前工作目录自动隔离命名空间"><span>按项目分库</span><input type="checkbox" id="zmCfgWsNs" /></label>
1106
+ </div>
1107
+
1108
+ <div class="zm-bar" style="justify-content:space-between;">
1109
+ <button class="zm-icon-btn" id="zmTestBtn" type="button">${ICONS.bolt}<span>测试连接</span></button>
1110
+ <button class="zm-icon-btn primary" id="zmSaveCfgBtn" type="button">${ICONS.check}<span>保存配置</span></button>
1111
+ </div>
1112
+
1113
+ <details class="zm-help">
1114
+ <summary>数据路径与脱敏说明</summary>
1115
+ <div style="margin-top:6px;line-height:1.6;">
1116
+ <div>• 数据路径:<code id="zmDbPathText">~/.zeromem/memory.db</code></div>
1117
+ <div>• 隐私保护:云端 API 模式下常见密钥在离开本机前自动替换为 <code>[REDACTED]</code>。</div>
1118
+ </div>
1119
+ </details>
1120
+ </section>
1121
+ </div>
1122
+ </div>
1123
+
1124
+ <!-- Frosted Glass Confirm Modal for One-Click Open-Source Model Install -->
1125
+ <div class="zm-modal-mask" id="zmInstallModal" role="dialog" aria-modal="true" aria-label="确认安装开源专项小模型">
1126
+ <div class="zm-modal">
1127
+ <div class="zm-modal-title">${ICONS.download}<span>确认安装开源专项小模型</span></div>
1128
+ <div class="zm-modal-sub">
1129
+ 作者 <strong>@B站IM榴莲</strong> · 开源仓库 <code>IMLIULIAN/qwen3.5-4b-memory-v2</code>
1130
+ </div>
1131
+ <div class="zm-ver-grid" id="zmInstallVerGrid">
1132
+ <button class="zm-ver-card active" data-model="hf.co/IMLIULIAN/qwen3.5-4b-memory-v2:Q8_0" type="button">
1133
+ <span class="zm-ver-name">Q8_0 标准版</span>
1134
+ <span class="zm-ver-size">约 4.5 GB · 推荐精度</span>
1135
+ </button>
1136
+ <button class="zm-ver-card" data-model="hf.co/IMLIULIAN/qwen3.5-4b-memory-v2:Q4_K_M" type="button">
1137
+ <span class="zm-ver-name">Q4_K_M 轻量版</span>
1138
+ <span class="zm-ver-size">约 2.7 GB · 低显存</span>
1139
+ </button>
1140
+ </div>
1141
+ <div class="zm-modal-note" id="zmInstallModalNote">确认后将通过本机 Ollama 下载模型,期间显示实时进度并可随时取消。</div>
1142
+ <div class="zm-modal-foot" id="zmInstallModalFoot">
1143
+ <button class="zm-icon-btn" id="zmInstallCancelBtn" type="button">取消</button>
1144
+ <button class="zm-icon-btn primary" id="zmInstallConfirmBtn" type="button">${ICONS.check}<span>确认安装</span></button>
1145
+ </div>
1146
+ </div>
1147
+ </div>
1148
+ </aside>
1149
+ `;
1150
+
1151
+ const $ = (id) => shadow.getElementById(id);
1152
+ const state = {
1153
+ open: false,
1154
+ tab: "memories",
1155
+ config: null,
1156
+ providerDraft: "ollama",
1157
+ clearApiKeyFlag: false,
1158
+ searchTimer: null,
1159
+ selectedNs: "",
1160
+ namespaces: [],
1161
+ selectedInstallModel: "hf.co/IMLIULIAN/qwen3.5-4b-memory-v2:Q8_0",
1162
+ activePresetId: "deepseek",
1163
+ customApiBaseUrl: "",
1164
+ customApiModel: "",
1165
+ theme: readCachedTheme(),
1166
+ bgUrl: "",
1167
+ hasCustomBg: false,
1168
+ lastProviderStatus: null,
1169
+ lastModeLabel: "加载中",
1170
+ serverActivity: { busy: false, phase: "idle", label: "" },
1171
+ setupState: { active: false, status: "idle", percent: null },
1172
+ uiBusyCount: 0,
1173
+ uiBusyLabel: "",
1174
+ evicting: false,
1175
+ eventSource: null,
1176
+ sseRetryTimer: null,
1177
+ };
1178
+
1179
+ // Tactile glass ripple on pointerdown across interactive elements
1180
+ shadow.addEventListener("pointerdown", (e) => {
1181
+ const target = e.target?.closest?.(".zm-icon-btn, .zm-tab, .zm-pcard, .zm-chip, .zm-theme-opt, .zm-bg-drop, .zm-pill, .zm-ns-trigger, .zm-ns-opt, .zm-ver-card");
1182
+ if (!target || target.disabled) return;
1183
+ const rect = target.getBoundingClientRect();
1184
+ const size = Math.max(rect.width, rect.height) * 2.1;
1185
+ const ripple = document.createElement("span");
1186
+ ripple.className = "zm-ripple";
1187
+ ripple.style.width = `${size}px`;
1188
+ ripple.style.height = `${size}px`;
1189
+ ripple.style.left = `${e.clientX - rect.left}px`;
1190
+ ripple.style.top = `${e.clientY - rect.top}px`;
1191
+ target.appendChild(ripple);
1192
+ ripple.addEventListener("animationend", () => ripple.remove(), { once: true });
1193
+ setTimeout(() => ripple.remove(), 520);
1194
+ });
1195
+
1196
+ function showToast(msg, ok = true) {
1197
+ const el = $("zmToast");
1198
+ el.textContent = msg;
1199
+ el.className = `zm-toast ${ok ? "ok" : "err"}`;
1200
+ clearTimeout(el._t);
1201
+ el._t = setTimeout(() => {
1202
+ el.className = "zm-toast";
1203
+ }, 4200);
1204
+ }
1205
+
1206
+ function applyAppearance() {
1207
+ const theme = state.theme === "dark" ? "dark" : "light";
1208
+ host.setAttribute("data-theme", theme);
1209
+ $("zmThemeSeg").style.setProperty("--zm-theme-idx", theme === "dark" ? "1" : "0");
1210
+
1211
+ // Curated golden frosted-glass defaults (30px blur; 50% frost on aurora / 62% on custom wallpaper)
1212
+ const tint = state.hasCustomBg ? 62 : 50;
1213
+ host.style.setProperty("--zm-blur", "30px");
1214
+ host.style.setProperty("--zm-tint-hi", Math.min(0.92, (tint + 6) / 100).toFixed(2));
1215
+ host.style.setProperty("--zm-tint-lo", Math.max(0.28, (tint - 6) / 100).toFixed(2));
1216
+
1217
+ const themeBtn = $("zmThemeBtn");
1218
+ themeBtn.innerHTML = theme === "dark" ? ICONS.sun : ICONS.moon;
1219
+ themeBtn.title = theme === "dark" ? "切换到亮色模式" : "切换到暗色模式";
1220
+ $("zmThemeLightBtn").classList.toggle("active", theme === "light");
1221
+ $("zmThemeDarkBtn").classList.toggle("active", theme === "dark");
1222
+
1223
+ const bgLayer = $("zmBgLayer");
1224
+ const preview = $("zmBgPreview");
1225
+ const clearBtn = $("zmClearBgBtn");
1226
+ const quickBtn = $("zmBgQuickBtn");
1227
+
1228
+ if (state.hasCustomBg && state.bgUrl) {
1229
+ const safeCssUrl = `url("${state.bgUrl.replace(/"/g, "%22")}")`;
1230
+ bgLayer.style.backgroundImage = safeCssUrl;
1231
+ bgLayer.classList.add("has-custom");
1232
+ preview.style.backgroundImage = safeCssUrl;
1233
+ clearBtn.style.display = "inline-flex";
1234
+ quickBtn.classList.add("has-dot");
1235
+ $("zmBgDropTitle").textContent = "更换背景图";
1236
+ $("zmBgDropSub").textContent = "已应用毛玻璃壁纸";
1237
+ } else {
1238
+ bgLayer.style.backgroundImage = "";
1239
+ bgLayer.classList.remove("has-custom");
1240
+ preview.style.backgroundImage = "";
1241
+ clearBtn.style.display = "none";
1242
+ quickBtn.classList.remove("has-dot");
1243
+ $("zmBgDropTitle").textContent = "上传背景图";
1244
+ $("zmBgDropSub").textContent = "点击或拖入 · 自动毛玻璃";
1245
+ }
1246
+ writeCachedTheme(theme);
1247
+ }
1248
+
1249
+ function applyStatusDot(el, pStatus, busy) {
1250
+ el.className = "zm-dot";
1251
+ if (busy) {
1252
+ el.classList.add("busy");
1253
+ return;
1254
+ }
1255
+ if (!pStatus) return;
1256
+ if (pStatus.ok) {
1257
+ el.classList.add(pStatus.provider === "openai-compatible" ? "api" : "ok");
1258
+ } else if (["missing_model", "missing_key", "ollama_stopped"].includes(pStatus.state)) {
1259
+ el.classList.add("warn");
1260
+ } else {
1261
+ el.classList.add("err");
1262
+ }
1263
+ }
1264
+
1265
+ function renderBusyHalo() {
1266
+ const busy = Boolean(state.serverActivity?.busy || state.uiBusyCount > 0 || state.setupState?.active);
1267
+ const label =
1268
+ state.uiBusyCount > 0
1269
+ ? state.uiBusyLabel || "处理中"
1270
+ : state.serverActivity?.busy
1271
+ ? state.serverActivity.label || "处理中"
1272
+ : state.setupState?.active
1273
+ ? "安装中"
1274
+ : "";
1275
+
1276
+ $("zmPillShell").classList.toggle("is-busy", busy);
1277
+ $("zmBrandOrb").classList.toggle("is-busy", busy);
1278
+ $("zmPillLabel").textContent = busy ? label : "零忆";
1279
+ $("zmModeText").textContent = busy ? label : state.lastModeLabel;
1280
+ applyStatusDot($("zmPillDot"), state.lastProviderStatus, busy);
1281
+ applyStatusDot($("zmHeadDot"), state.lastProviderStatus, busy);
1282
+ }
1283
+
1284
+ async function withUiBusy(label, fn, minMs = 360) {
1285
+ const started = Date.now();
1286
+ state.uiBusyCount += 1;
1287
+ state.uiBusyLabel = label || "处理中";
1288
+ renderBusyHalo();
1289
+ try {
1290
+ return await fn();
1291
+ } finally {
1292
+ const remain = Math.max(0, minMs - (Date.now() - started));
1293
+ const done = () => {
1294
+ state.uiBusyCount = Math.max(0, state.uiBusyCount - 1);
1295
+ if (state.uiBusyCount === 0) state.uiBusyLabel = "";
1296
+ renderBusyHalo();
1297
+ };
1298
+ if (remain > 0) setTimeout(done, remain);
1299
+ else done();
1300
+ }
1301
+ }
1302
+
1303
+ async function triggerStartOllama() {
1304
+ await withUiBusy("启动 Ollama", async () => {
1305
+ try {
1306
+ const res = await api("/local-setup", {
1307
+ method: "POST",
1308
+ body: JSON.stringify({ action: "start-ollama" }),
1309
+ });
1310
+ showToast(res.result?.message || "本地 Ollama 已启动");
1311
+ await refreshStatus(false);
1312
+ } catch (err) {
1313
+ showToast(`启动失败:${err.message}`, false);
1314
+ }
1315
+ });
1316
+ }
1317
+
1318
+ async function triggerPullModel(modelId) {
1319
+ try {
1320
+ const res = await api("/local-setup", {
1321
+ method: "POST",
1322
+ body: JSON.stringify({ action: "pull-model", model: modelId }),
1323
+ });
1324
+ if (res.setup) state.setupState = res.setup;
1325
+ renderBusyHalo();
1326
+ renderLocalSetupGuides();
1327
+ showToast("已开始后台拉取本地 V2 模型");
1328
+ } catch (err) {
1329
+ showToast(`拉取启动失败:${err.message}`, false);
1330
+ }
1331
+ }
1332
+
1333
+ async function triggerCancelPull() {
1334
+ try {
1335
+ await api("/local-setup", {
1336
+ method: "POST",
1337
+ body: JSON.stringify({ action: "cancel" }),
1338
+ });
1339
+ showToast("已取消模型下载");
1340
+ } catch (err) {
1341
+ showToast(`取消失败:${err.message}`, false);
1342
+ }
1343
+ }
1344
+
1345
+ function renderLocalSetupGuides() {
1346
+ const ps = state.lastProviderStatus || {};
1347
+ const setup = state.setupState || {};
1348
+ const card = $("zmLocalSetupCard");
1349
+ const banner = $("zmTopSetupBanner");
1350
+ const isOllamaMode = (state.config?.provider || state.providerDraft) !== "openai-compatible";
1351
+
1352
+ // Active pull progress takes priority in both card and top banner
1353
+ if (setup.active && setup.action === "pull-model") {
1354
+ const pct = Number.isFinite(setup.percent) ? setup.percent : 12;
1355
+ const sizeStr = setup.total > 0 ? `${formatBytes(setup.completed)} / ${formatBytes(setup.total)}` : setup.status || "准备中";
1356
+ const html = `
1357
+ <div class="zm-setup-row">
1358
+ <span class="zm-setup-title">${ICONS.download}<span>正在下载本地模型 · ${pct}%</span></span>
1359
+ <button class="zm-icon-btn danger" data-setup-act="cancel" type="button">${ICONS.close}<span>取消</span></button>
1360
+ </div>
1361
+ <div class="zm-prog-track"><div class="zm-prog-fill" style="width:${Math.max(4, pct)}%;"></div></div>
1362
+ <div class="zm-setup-sub">${esc(setup.model)} · ${esc(sizeStr)}</div>
1363
+ `;
1364
+ card.innerHTML = html;
1365
+ card.classList.add("show");
1366
+ banner.innerHTML = html;
1367
+ banner.classList.add("show");
1368
+ bindSetupButtons(card);
1369
+ bindSetupButtons(banner);
1370
+ return;
1371
+ }
1372
+
1373
+ // Only show install/start prompts when local Ollama is selected and not ready
1374
+ if (!isOllamaMode || ps.ok || !ps.state || ps.state === "unchecked") {
1375
+ card.classList.remove("show");
1376
+ banner.classList.remove("show");
1377
+ return;
1378
+ }
1379
+
1380
+ const models = Array.isArray(ps.recommended_models) && ps.recommended_models.length
1381
+ ? ps.recommended_models
1382
+ : [
1383
+ { id: "hf.co/IMLIULIAN/qwen3.5-4b-memory-v2:Q8_0", shortLabel: "一键下载 V2 (Q8)", sizeHint: "4.5 GB" },
1384
+ { id: "hf.co/IMLIULIAN/qwen3.5-4b-memory-v2:Q4_K_M", shortLabel: "轻量 Q4", sizeHint: "2.7 GB" },
1385
+ ];
1386
+
1387
+ if (ps.state === "missing_model") {
1388
+ const m0 = models[0];
1389
+ const m1 = models[1];
1390
+ const html = `
1391
+ <div class="zm-setup-row">
1392
+ <span class="zm-setup-title">${ICONS.cpu}<span>Ollama 已就绪 · 一键安装零忆 V2</span></span>
1393
+ </div>
1394
+ <div class="zm-setup-sub">自动检测到尚未下载零忆专用模型,可一键拉取(免费离线)</div>
1395
+ <div class="zm-setup-btns">
1396
+ <button class="zm-icon-btn primary" data-setup-act="pull" data-model="${esc(m0.id)}" type="button">${ICONS.download}<span>${esc(m0.shortLabel)} · ${esc(m0.sizeHint)}</span></button>
1397
+ ${m1 ? `<button class="zm-icon-btn" data-setup-act="pull" data-model="${esc(m1.id)}" type="button">${ICONS.download}<span>${esc(m1.shortLabel)}</span></button>` : ""}
1398
+ </div>
1399
+ `;
1400
+ card.innerHTML = html;
1401
+ card.classList.add("show");
1402
+ banner.innerHTML = `
1403
+ <div class="zm-setup-row">
1404
+ <span class="zm-setup-title">${ICONS.cpu}<span>Ollama 已就绪,尚未安装 V2 模型</span></span>
1405
+ <button class="zm-icon-btn primary" data-setup-act="pull" data-model="${esc(m0.id)}" type="button">${ICONS.download}<span>一键下载</span></button>
1406
+ </div>
1407
+ `;
1408
+ banner.classList.add("show");
1409
+ } else if (ps.state === "ollama_stopped") {
1410
+ const html = `
1411
+ <div class="zm-setup-row">
1412
+ <span class="zm-setup-title">${ICONS.cpu}<span>已检测到本机 Ollama(未启动)</span></span>
1413
+ <button class="zm-icon-btn primary" data-setup-act="start" type="button">${ICONS.play}<span>一键启动</span></button>
1414
+ </div>
1415
+ <div class="zm-setup-sub">后台静默拉起 Ollama 服务后即可自动检测或一键拉取 V2 模型</div>
1416
+ `;
1417
+ card.innerHTML = html;
1418
+ card.classList.add("show");
1419
+ banner.innerHTML = html;
1420
+ banner.classList.add("show");
1421
+ } else if (ps.state === "ollama_not_installed") {
1422
+ const guide = ps.install_guide || { command: "winget install Ollama.Ollama", downloadUrl: "https://ollama.com/download" };
1423
+ const html = `
1424
+ <div class="zm-setup-row">
1425
+ <span class="zm-setup-title">${ICONS.cpu}<span>未检测到本机 Ollama</span></span>
1426
+ </div>
1427
+ <div class="zm-setup-sub">安装 Ollama 后重启检测即可一键拉取免费本地模型,或直接改用云端 API</div>
1428
+ <div class="zm-setup-btns">
1429
+ <button class="zm-icon-btn primary" data-setup-act="open-url" data-url="${esc(guide.downloadUrl)}" type="button">${ICONS.external}<span>下载 Ollama</span></button>
1430
+ <button class="zm-icon-btn" data-setup-act="copy-cmd" data-cmd="${esc(guide.command)}" type="button">${ICONS.copy}<span>复制安装命令</span></button>
1431
+ <button class="zm-icon-btn" data-setup-act="switch-api" type="button">${ICONS.cloud}<span>切云端 API</span></button>
1432
+ </div>
1433
+ `;
1434
+ card.innerHTML = html;
1435
+ card.classList.add("show");
1436
+ banner.innerHTML = `
1437
+ <div class="zm-setup-row">
1438
+ <span class="zm-setup-title">${ICONS.cpu}<span>未检测到 Ollama</span></span>
1439
+ <div class="zm-setup-btns">
1440
+ <button class="zm-icon-btn primary" data-setup-act="go-settings" type="button">${ICONS.sliders}<span>一键向导</span></button>
1441
+ </div>
1442
+ </div>
1443
+ `;
1444
+ banner.classList.add("show");
1445
+ } else {
1446
+ card.classList.remove("show");
1447
+ banner.classList.remove("show");
1448
+ return;
1449
+ }
1450
+
1451
+ bindSetupButtons(card);
1452
+ bindSetupButtons(banner);
1453
+ }
1454
+
1455
+ function bindSetupButtons(rootEl) {
1456
+ rootEl.querySelectorAll("[data-setup-act]").forEach((btn) => {
1457
+ btn.addEventListener("click", async () => {
1458
+ const act = btn.getAttribute("data-setup-act");
1459
+ if (act === "start") {
1460
+ await triggerStartOllama();
1461
+ } else if (act === "pull") {
1462
+ const picked = btn.getAttribute("data-model") || state.selectedInstallModel;
1463
+ state.selectedInstallModel = picked;
1464
+ $("zmInstallVerGrid")
1465
+ .querySelectorAll(".zm-ver-card")
1466
+ .forEach((c) => c.classList.toggle("active", c.getAttribute("data-model") === picked));
1467
+ openInstallModal();
1468
+ } else if (act === "cancel") {
1469
+ await triggerCancelPull();
1470
+ } else if (act === "open-url") {
1471
+ window.open(btn.getAttribute("data-url") || "https://ollama.com/download", "_blank", "noopener,noreferrer");
1472
+ } else if (act === "copy-cmd") {
1473
+ const cmd = btn.getAttribute("data-cmd") || "";
1474
+ try {
1475
+ await navigator.clipboard.writeText(cmd);
1476
+ showToast(`已复制:${cmd}`);
1477
+ } catch {
1478
+ showToast(`请手动运行:${cmd}`);
1479
+ }
1480
+ } else if (act === "switch-api") {
1481
+ switchTab("settings");
1482
+ state.providerDraft = "openai-compatible";
1483
+ renderProviderFields();
1484
+ $("zmApiKey").focus();
1485
+ } else if (act === "go-settings") {
1486
+ switchTab("settings");
1487
+ }
1488
+ });
1489
+ });
1490
+ }
1491
+
1492
+ function connectEvents() {
1493
+ if (typeof EventSource !== "function") return;
1494
+ if (state.eventSource) {
1495
+ try {
1496
+ state.eventSource.close();
1497
+ } catch {}
1498
+ }
1499
+ try {
1500
+ const es = new EventSource(`${API_BASE}/events`);
1501
+ state.eventSource = es;
1502
+ es.addEventListener("activity", (ev) => {
1503
+ try {
1504
+ const payload = JSON.parse(ev.data || "{}");
1505
+ if (payload.activity) state.serverActivity = payload.activity;
1506
+ if (payload.setup) {
1507
+ const wasActive = state.setupState?.active;
1508
+ state.setupState = payload.setup;
1509
+ if (wasActive && !payload.setup.active) {
1510
+ if (payload.setup.status === "success") {
1511
+ showToast("本地 V2 模型下载完成,已自动就绪");
1512
+ refreshStatus(false);
1513
+ } else if (payload.setup.status === "error" && payload.setup.error) {
1514
+ showToast(`下载失败:${payload.setup.error}`, false);
1515
+ refreshStatus(false);
1516
+ }
1517
+ }
1518
+ }
1519
+ renderBusyHalo();
1520
+ renderLocalSetupGuides();
1521
+ if (payload.stats_dirty && !state.evicting) {
1522
+ refreshStatus(true);
1523
+ if (state.open && state.tab === "memories") loadMemories();
1524
+ if (state.open && state.tab === "recycle") loadRecycleBin();
1525
+ }
1526
+ } catch {}
1527
+ });
1528
+ es.onerror = () => {
1529
+ try {
1530
+ es.close();
1531
+ } catch {}
1532
+ state.eventSource = null;
1533
+ clearTimeout(state.sseRetryTimer);
1534
+ state.sseRetryTimer = setTimeout(connectEvents, 8000);
1535
+ };
1536
+ } catch {
1537
+ // fail-open
1538
+ }
1539
+ }
1540
+
1541
+ function renderProviderFields() {
1542
+ const isApi = state.providerDraft === "openai-compatible";
1543
+ $("zmPickOllama").classList.toggle("active", !isApi);
1544
+ $("zmPickApi").classList.toggle("active", isApi);
1545
+ $("zmOllamaFields").style.display = isApi ? "none" : "flex";
1546
+ $("zmApiFields").style.display = isApi ? "flex" : "none";
1547
+ renderLocalSetupGuides();
1548
+ }
1549
+
1550
+ function detectPresetId(baseUrl, presets) {
1551
+ const norm = String(baseUrl || "").trim().replace(/\/+$/, "").toLowerCase();
1552
+ for (const p of presets || []) {
1553
+ if (p.isCustom || p.id === "custom") continue;
1554
+ if (String(p.baseUrl || "").trim().replace(/\/+$/, "").toLowerCase() === norm) {
1555
+ return p.id;
1556
+ }
1557
+ }
1558
+ return "custom";
1559
+ }
1560
+
1561
+ function updateApiPresetUi(presetId) {
1562
+ state.activePresetId = presetId || "custom";
1563
+ const isCustom = state.activePresetId === "custom";
1564
+ $("zmPresets")
1565
+ .querySelectorAll(".zm-chip")
1566
+ .forEach((btn) => {
1567
+ btn.classList.toggle("active", btn.getAttribute("data-pid") === state.activePresetId);
1568
+ });
1569
+ $("zmCustomApiHint").classList.toggle("show", isCustom);
1570
+ $("zmApiBaseUrl").placeholder = isCustom ? "https://your-gateway.example/v1" : "https://api.deepseek.com/v1";
1571
+ $("zmApiModel").placeholder = isCustom ? "输入自定义模型 ID(如 qwen-plus / gpt-4.1-mini)" : "deepseek-chat";
1572
+ }
1573
+
1574
+ function populateConfigForm(cfg, probe) {
1575
+ state.config = cfg;
1576
+ state.providerDraft = cfg.provider || "ollama";
1577
+ state.clearApiKeyFlag = false;
1578
+ state.customApiBaseUrl = cfg.customApiBaseUrl || state.customApiBaseUrl || "";
1579
+ state.customApiModel = cfg.customApiModel || state.customApiModel || "";
1580
+ renderProviderFields();
1581
+
1582
+ if (cfg.uiTheme === "dark" || cfg.uiTheme === "light") state.theme = cfg.uiTheme;
1583
+ state.hasCustomBg = Boolean(cfg.uiHasBackground && cfg.uiBackgroundUrl);
1584
+ state.bgUrl = state.hasCustomBg ? cfg.uiBackgroundUrl : "";
1585
+ applyAppearance();
1586
+
1587
+ $("zmOllamaUrl").value = cfg.ollamaUrl || "http://127.0.0.1:11434";
1588
+ $("zmOllamaModel").value = cfg.ollamaModel || "";
1589
+ $("zmNumCtx").value = String(cfg.numCtx || 8192);
1590
+ $("zmApiBaseUrl").value = cfg.apiBaseUrl || "https://api.deepseek.com/v1";
1591
+ $("zmApiModel").value = cfg.apiModel || "deepseek-chat";
1592
+ $("zmApiKey").value = "";
1593
+ $("zmApiKeyHint").textContent = cfg.apiKeySet ? `已配置 ${cfg.apiKeyMasked}` : "未配置";
1594
+
1595
+ $("zmCfgAutoRecall").checked = Boolean(cfg.autoRecall);
1596
+ $("zmCfgAutoRemember").checked = Boolean(cfg.autoRemember);
1597
+ $("zmCfgVerify").checked = Boolean(cfg.verify);
1598
+ $("zmCfgUntil").checked = Boolean(cfg.untilExpiry);
1599
+ $("zmCfgAdaptive").checked = Boolean(cfg.adaptive);
1600
+ $("zmCfgWsNs").checked = cfg.namespaceMode !== "fixed";
1601
+ $("zmAddVerify").checked = Boolean(cfg.verify);
1602
+ $("zmDbPathText").textContent = cfg.dbPath || "~/.zeromem/memory.db";
1603
+
1604
+ const rawPresets = Array.isArray(cfg.presets) && cfg.presets.length ? [...cfg.presets] : [];
1605
+ if (!rawPresets.some((p) => p.id === "custom" || p.isCustom)) {
1606
+ rawPresets.push({ id: "custom", label: "自定义 API", baseUrl: "", model: "", isCustom: true });
1607
+ }
1608
+ const presetBox = $("zmPresets");
1609
+ presetBox.innerHTML = "";
1610
+ for (const p of rawPresets) {
1611
+ const btn = document.createElement("button");
1612
+ btn.type = "button";
1613
+ btn.className = "zm-chip";
1614
+ btn.setAttribute("data-pid", p.id);
1615
+ btn.textContent = p.label;
1616
+ btn.onclick = () => {
1617
+ if (p.id === "custom" || p.isCustom) {
1618
+ updateApiPresetUi("custom");
1619
+ const curMatch = detectPresetId($("zmApiBaseUrl").value, rawPresets);
1620
+ if (curMatch !== "custom") {
1621
+ $("zmApiBaseUrl").value = state.customApiBaseUrl || "";
1622
+ $("zmApiModel").value = state.customApiModel || "";
1623
+ }
1624
+ $("zmApiBaseUrl").focus();
1625
+ } else {
1626
+ if (state.activePresetId === "custom") {
1627
+ const curU = $("zmApiBaseUrl").value.trim();
1628
+ const curM = $("zmApiModel").value.trim();
1629
+ if (curU) state.customApiBaseUrl = curU;
1630
+ if (curM) state.customApiModel = curM;
1631
+ }
1632
+ $("zmApiBaseUrl").value = p.baseUrl;
1633
+ $("zmApiModel").value = p.model;
1634
+ updateApiPresetUi(p.id);
1635
+ }
1636
+ };
1637
+ presetBox.appendChild(btn);
1638
+ }
1639
+ const matched = detectPresetId($("zmApiBaseUrl").value, rawPresets);
1640
+ if (matched === "custom") {
1641
+ state.customApiBaseUrl = $("zmApiBaseUrl").value.trim();
1642
+ state.customApiModel = $("zmApiModel").value.trim();
1643
+ }
1644
+ updateApiPresetUi(matched);
1645
+
1646
+ const dl = $("zmOllamaModelList");
1647
+ dl.innerHTML = "";
1648
+ const models = probe?.available_models || [];
1649
+ $("zmOllamaAvail").textContent = models.length ? `已发现 ${models.length} 个` : "";
1650
+ for (const m of models) {
1651
+ const opt = document.createElement("option");
1652
+ opt.value = m;
1653
+ dl.appendChild(opt);
1654
+ }
1655
+ }
1656
+
1657
+ async function refreshStatus(quick = false) {
1658
+ try {
1659
+ const data = await api(`/status${quick ? "?quick=1" : ""}`);
1660
+ const st = data.stats || {};
1661
+ const ps = data.provider_status || {};
1662
+ const cfg = data.config || {};
1663
+
1664
+ $("zmPillCnt").textContent = String(st.facts || 0);
1665
+ $("zmTabMemCnt").textContent = String(st.facts || 0);
1666
+ $("zmTabRecCnt").textContent = String(st.evicted_records || 0);
1667
+ $("zmStatsMeta").textContent = `文档 ${st.documents || 0} · 纠错 ${st.revised_facts || 0}`;
1668
+
1669
+ if (!quick || !state.lastProviderStatus) state.lastProviderStatus = ps;
1670
+ state.lastModeLabel =
1671
+ cfg.provider === "openai-compatible"
1672
+ ? `API · ${cfg.apiModel || "未设"}`
1673
+ : `本地 · ${ps.model || cfg.ollamaModel || "V2"}`;
1674
+ $("zmModeBadge").title = ps.message || state.lastModeLabel;
1675
+ if (data.activity) state.serverActivity = data.activity;
1676
+ if (data.setup) state.setupState = data.setup;
1677
+ renderBusyHalo();
1678
+
1679
+ const nextNamespaces = Array.isArray(st.namespaces) ? st.namespaces : [];
1680
+ state.namespaces = nextNamespaces;
1681
+ if (state.selectedNs && !nextNamespaces.includes(state.selectedNs)) {
1682
+ state.selectedNs = "";
1683
+ }
1684
+ renderNamespaceMenu();
1685
+
1686
+ if (!quick || !state.config) {
1687
+ populateConfigForm(cfg, ps);
1688
+ }
1689
+ renderLocalSetupGuides();
1690
+ } catch (err) {
1691
+ state.lastProviderStatus = { ok: false, state: "offline" };
1692
+ state.lastModeLabel = "未连接";
1693
+ renderBusyHalo();
1694
+ if (state.open) showToast(`状态获取失败:${err.message}`, false);
1695
+ }
1696
+ }
1697
+
1698
+ function bindTwoStepConfirm(btn, onConfirm) {
1699
+ let armed = false;
1700
+ let timer = null;
1701
+ const origHtml = btn.innerHTML;
1702
+ const origTitle = btn.title;
1703
+ btn.addEventListener("click", async (e) => {
1704
+ e.stopPropagation();
1705
+ if (!armed) {
1706
+ armed = true;
1707
+ btn.classList.add("confirming");
1708
+ btn.innerHTML = `${ICONS.check}<span>确认</span>`;
1709
+ btn.title = "再次点击确认操作";
1710
+ clearTimeout(timer);
1711
+ timer = setTimeout(() => {
1712
+ armed = false;
1713
+ btn.classList.remove("confirming");
1714
+ btn.innerHTML = origHtml;
1715
+ btn.title = origTitle;
1716
+ }, 3000);
1717
+ return;
1718
+ }
1719
+ clearTimeout(timer);
1720
+ armed = false;
1721
+ btn.classList.remove("confirming");
1722
+ btn.innerHTML = origHtml;
1723
+ btn.title = origTitle;
1724
+ await onConfirm();
1725
+ });
1726
+ }
1727
+
1728
+ function setNsMenuOpen(flag) {
1729
+ const dd = $("zmNsDd");
1730
+ const open = Boolean(flag);
1731
+ dd.classList.toggle("open", open);
1732
+ $("zmNsTrigger").setAttribute("aria-expanded", open ? "true" : "false");
1733
+ }
1734
+
1735
+ function renderNamespaceMenu() {
1736
+ const labelText = state.selectedNs || "全部空间";
1737
+ $("zmNsLabel").textContent = labelText;
1738
+ $("zmNsTrigger").title = labelText;
1739
+
1740
+ const menu = $("zmNsMenu");
1741
+ const opts = [{ value: "", label: "全部空间" }, ...state.namespaces.map((ns) => ({ value: ns, label: ns }))];
1742
+ menu.innerHTML = opts
1743
+ .map(
1744
+ (o) => `
1745
+ <button class="zm-ns-opt ${o.value === state.selectedNs ? "active" : ""}" data-ns="${esc(o.value)}" type="button" role="option" aria-selected="${o.value === state.selectedNs ? "true" : "false"}" title="${esc(o.label)}">
1746
+ <span class="zm-ns-opt-txt">${esc(o.label)}</span>
1747
+ <span class="zm-ns-opt-chk">${ICONS.check}</span>
1748
+ </button>
1749
+ `
1750
+ )
1751
+ .join("");
1752
+
1753
+ menu.querySelectorAll(".zm-ns-opt").forEach((btn) => {
1754
+ btn.addEventListener("click", (e) => {
1755
+ e.stopPropagation();
1756
+ const nextVal = btn.getAttribute("data-ns") || "";
1757
+ const changed = nextVal !== state.selectedNs;
1758
+ state.selectedNs = nextVal;
1759
+ renderNamespaceMenu();
1760
+ setNsMenuOpen(false);
1761
+ if (changed) loadMemories();
1762
+ });
1763
+ });
1764
+ }
1765
+
1766
+ /**
1767
+ * High-craft 3D textured crumpled-paper deletion animation (zero external deps):
1768
+ * 1. Snapshots the real card content (tags + wrapped text) onto an offscreen paper texture canvas.
1769
+ * 2. Phase 1 (0..350ms): In-place hand-crumple — creases form first, corners fold inward,
1770
+ * affine-textured 3D triangles deform into a sharp faceted paper ball with directional lighting + crease AO.
1771
+ * 3. Phase 2 (350..460ms): Secondary palm squeeze + elastic settle pause (DOM slot begins collapsing smoothly).
1772
+ * 4. Phase 3 (460..790ms): Parabolic tumble-toss with subtle arc trail into Recycle Bin tab -> Q-bounce & flecks.
1773
+ */
1774
+ function renderCardPaperTexture(cardEl, width, height, isDark) {
1775
+ const tex = document.createElement("canvas");
1776
+ const scale = 1.5;
1777
+ tex.width = Math.max(64, Math.round(width * scale));
1778
+ tex.height = Math.max(48, Math.round(height * scale));
1779
+ const tctx = tex.getContext("2d");
1780
+ if (!tctx) return null;
1781
+ tctx.scale(scale, scale);
1782
+
1783
+ // Paper sheet base with subtle fibrous warmth
1784
+ tctx.fillStyle = isDark ? "#1e2b45" : "#fbfcff";
1785
+ tctx.fillRect(0, 0, width, height);
1786
+ const sheen = tctx.createLinearGradient(0, 0, width, height);
1787
+ sheen.addColorStop(0, isDark ? "rgba(255,255,255,0.06)" : "rgba(255,255,255,0.85)");
1788
+ sheen.addColorStop(1, isDark ? "rgba(9,14,28,0.22)" : "rgba(226,232,248,0.45)");
1789
+ tctx.fillStyle = sheen;
1790
+ tctx.fillRect(0, 0, width, height);
1791
+
1792
+ // Subtle paper border
1793
+ tctx.strokeStyle = isDark ? "rgba(148,163,184,0.28)" : "rgba(148,163,184,0.34)";
1794
+ tctx.lineWidth = 1.2;
1795
+ tctx.strokeRect(0.6, 0.6, width - 1.2, height - 1.2);
1796
+
1797
+ // Render real tag pills from card DOM
1798
+ let tx = 12;
1799
+ const ty = 11;
1800
+ tctx.font = '600 10.5px -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif';
1801
+ tctx.textBaseline = "middle";
1802
+ const tags = [...cardEl.querySelectorAll(".zm-tag")].slice(0, 4);
1803
+ for (const tagEl of tags) {
1804
+ const label = (tagEl.textContent || "").trim().slice(0, 22);
1805
+ if (!label) continue;
1806
+ const tw = Math.min(130, tctx.measureText(label).width + 14);
1807
+ if (tx + tw > width - 36) break;
1808
+ const isRev = tagEl.classList.contains("rev");
1809
+ const isExp = tagEl.classList.contains("exp");
1810
+ tctx.fillStyle = isRev
1811
+ ? isDark ? "rgba(16,185,129,0.28)" : "rgba(16,185,129,0.18)"
1812
+ : isExp
1813
+ ? isDark ? "rgba(245,158,11,0.28)" : "rgba(245,158,11,0.20)"
1814
+ : isDark ? "rgba(99,102,241,0.28)" : "rgba(79,70,229,0.14)";
1815
+ tctx.beginPath();
1816
+ tctx.roundRect ? tctx.roundRect(tx, ty, tw, 17, 8.5) : tctx.rect(tx, ty, tw, 17);
1817
+ tctx.fill();
1818
+ tctx.fillStyle = isRev ? (isDark ? "#6ee7b7" : "#047857") : isExp ? (isDark ? "#fcd34d" : "#b45309") : (isDark ? "#a5b4fc" : "#4338ca");
1819
+ tctx.fillText(label, tx + 7, ty + 8.8, tw - 10);
1820
+ tx += tw + 5;
1821
+ }
1822
+
1823
+ // Render real memory text wrapped across lines
1824
+ const rawText = (cardEl.querySelector(".zm-fact-text")?.textContent || "").trim();
1825
+ tctx.fillStyle = isDark ? "#e2e8f0" : "#1e293b";
1826
+ tctx.font = '400 12.5px -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif';
1827
+ tctx.textBaseline = "top";
1828
+ const maxW = Math.max(60, width - 24);
1829
+ let line = "";
1830
+ let y = 36;
1831
+ const lineH = 19;
1832
+ for (let i = 0; i < rawText.length && y < height - 10; i++) {
1833
+ const ch = rawText[i];
1834
+ const test = line + ch;
1835
+ if (ch === "\n" || tctx.measureText(test).width > maxW) {
1836
+ tctx.fillText(line, 12, y);
1837
+ line = ch === "\n" ? "" : ch;
1838
+ y += lineH;
1839
+ } else {
1840
+ line = test;
1841
+ }
1842
+ }
1843
+ if (line && y < height - 8) tctx.fillText(line, 12, y);
1844
+ return { tex, scale };
1845
+ }
1846
+
1847
+ function drawTexturedTriangle(ctx, img, x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2) {
1848
+ const denom = u0 * (v1 - v2) - v0 * (u1 - u2) + (u1 * v2 - u2 * v1);
1849
+ if (Math.abs(denom) < 1e-4) return;
1850
+ // Slight 0.35px outward expansion prevents subpixel hairline seams between adjacent facets
1851
+ const cx = (x0 + x1 + x2) / 3;
1852
+ const cy = (y0 + y1 + y2) / 3;
1853
+ const ex = (x, y) => {
1854
+ const dx = x - cx, dy = y - cy;
1855
+ const l = Math.hypot(dx, dy) || 1;
1856
+ return [x + (dx / l) * 0.45, y + (dy / l) * 0.45];
1857
+ };
1858
+ const [px0, py0] = ex(x0, y0);
1859
+ const [px1, py1] = ex(x1, y1);
1860
+ const [px2, py2] = ex(x2, y2);
1861
+
1862
+ ctx.save();
1863
+ ctx.beginPath();
1864
+ ctx.moveTo(px0, py0);
1865
+ ctx.lineTo(px1, py1);
1866
+ ctx.lineTo(px2, py2);
1867
+ ctx.closePath();
1868
+ ctx.clip();
1869
+
1870
+ const m11 = (x0 * (v1 - v2) - v0 * (x1 - x2) + (x1 * v2 - x2 * v1)) / denom;
1871
+ const m12 = (u0 * (x1 - x2) - x0 * (u1 - u2) + (u1 * x2 - u2 * x1)) / denom;
1872
+ const m13 = (u0 * (v1 * x2 - v2 * x1) - v0 * (u1 * x2 - u2 * x1) + x0 * (u1 * v2 - u2 * v1)) / denom;
1873
+ const m21 = (y0 * (v1 - v2) - v0 * (y1 - y2) + (y1 * v2 - y2 * v1)) / denom;
1874
+ const m22 = (u0 * (y1 - y2) - y0 * (u1 - u2) + (u1 * y2 - u2 * y1)) / denom;
1875
+ const m23 = (u0 * (v1 * y2 - v2 * y1) - v0 * (u1 * y2 - u2 * y1) + y0 * (u1 * v2 - u2 * v1)) / denom;
1876
+
1877
+ ctx.transform(m11, m21, m12, m22, m13, m23);
1878
+ ctx.drawImage(img, 0, 0);
1879
+ ctx.restore();
1880
+ }
1881
+
1882
+ function playPaperCrumpleToTrash(cardEl) {
1883
+ return new Promise((resolve) => {
1884
+ if (!cardEl || window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches) {
1885
+ if (cardEl) cardEl.remove();
1886
+ resolve();
1887
+ return;
1888
+ }
1889
+ const rect = cardEl.getBoundingClientRect();
1890
+ if (rect.width < 20 || rect.height < 12) {
1891
+ cardEl.remove();
1892
+ resolve();
1893
+ return;
1894
+ }
1895
+
1896
+ const binTab = shadow.querySelector('.zm-tab[data-tab="recycle"]');
1897
+ const binRect = binTab ? binTab.getBoundingClientRect() : { left: rect.left + rect.width * 0.5, top: 80, width: 80, height: 30 };
1898
+ const startCx = rect.left + rect.width * 0.5;
1899
+ const startCy = rect.top + rect.height * 0.5;
1900
+ const targetCx = binRect.left + binRect.width * 0.5;
1901
+ const targetCy = binRect.top + binRect.height * 0.5;
1902
+
1903
+ const isDark = state.theme === "dark";
1904
+ const snap = renderCardPaperTexture(cardEl, rect.width, rect.height, isDark);
1905
+
1906
+ // Keep DOM slot occupied until the crumpled paper ball clears this card's slot, preventing lower cards from sliding underneath it
1907
+ const origH = rect.height;
1908
+ const parentStyle = cardEl.parentElement ? getComputedStyle(cardEl.parentElement) : null;
1909
+ const listGap = Math.max(0, parseFloat(parentStyle?.rowGap || parentStyle?.gap || "8") || 8);
1910
+ const hasNext = Boolean(cardEl.nextElementSibling);
1911
+ const hasPrev = Boolean(cardEl.previousElementSibling);
1912
+ cardEl.style.visibility = "hidden";
1913
+ cardEl.style.pointerEvents = "none";
1914
+ cardEl.style.backdropFilter = "none";
1915
+ cardEl.style.webkitBackdropFilter = "none";
1916
+ cardEl.style.boxShadow = "none";
1917
+ let collapseStarted = false;
1918
+ const startCollapse = () => {
1919
+ if (collapseStarted) return;
1920
+ collapseStarted = true;
1921
+ cardEl.style.height = `${origH}px`;
1922
+ cardEl.style.marginTop = "0px";
1923
+ cardEl.style.marginBottom = "0px";
1924
+ void cardEl.offsetHeight;
1925
+ cardEl.classList.add("zm-collapsing");
1926
+ requestAnimationFrame(() => {
1927
+ cardEl.style.opacity = "0";
1928
+ cardEl.style.height = "0px";
1929
+ cardEl.style.paddingTop = "0px";
1930
+ cardEl.style.paddingBottom = "0px";
1931
+ cardEl.style.borderWidth = "0px";
1932
+ if (hasNext) cardEl.style.marginBottom = `-${listGap}px`;
1933
+ else if (hasPrev) cardEl.style.marginTop = `-${listGap}px`;
1934
+ });
1935
+ };
1936
+
1937
+ const dpr = Math.min(window.devicePixelRatio || 1, 2);
1938
+ const canvas = document.createElement("canvas");
1939
+ canvas.width = Math.round(window.innerWidth * dpr);
1940
+ canvas.height = Math.round(window.innerHeight * dpr);
1941
+ canvas.style.cssText = "position:fixed;inset:0;width:100vw;height:100vh;pointer-events:none;z-index:2147483005;";
1942
+ shadow.appendChild(canvas);
1943
+ const ctx = canvas.getContext("2d");
1944
+ if (!ctx) {
1945
+ canvas.remove();
1946
+ cardEl.remove();
1947
+ resolve();
1948
+ return;
1949
+ }
1950
+ ctx.scale(dpr, dpr);
1951
+
1952
+ const COLS = 9;
1953
+ const ROWS = 5;
1954
+ const ballR = Math.max(22, Math.min(34, rect.height * 0.34, rect.width * 0.14));
1955
+ const seed = (((Math.round(rect.top * 11 + rect.height * 17) ^ 0x494d ^ 0x4c4c) % 997) + 997) % 997;
1956
+ const rand = (k) => {
1957
+ const x = Math.sin(seed * 12.9898 + k * 78.233) * 43758.5453;
1958
+ return x - Math.floor(x);
1959
+ };
1960
+
1961
+ // Build flat sheet -> mid-crumple creased fold -> jagged polyhedral 3D paper ball
1962
+ const verts = [];
1963
+ for (let r = 0; r <= ROWS; r++) {
1964
+ for (let c = 0; c <= COLS; c++) {
1965
+ const u = c / COLS;
1966
+ const v = r / ROWS;
1967
+ const fx = (u - 0.5) * rect.width;
1968
+ const fy = (v - 0.5) * rect.height;
1969
+ const idx = r * (COLS + 1) + c;
1970
+
1971
+ // Mid-stage sharp origami-like creases before full ball compression (tucked inward within card bounds)
1972
+ const zig = ((r + c) % 2 === 0 ? 1 : -1) * (9 + rand(idx * 5 + 1) * 15);
1973
+ const mx = fx * 0.56 + (rand(idx * 5 + 2) - 0.5) * 14;
1974
+ const my = fy * 0.54 + (rand(idx * 5 + 3) - 0.5) * 10;
1975
+ const mz = zig + Math.sin(u * 9.4 + v * 7.2) * 12;
1976
+
1977
+ // Final irregular polyhedral paper ball (distinct faceted ridges & valleys)
1978
+ const dx = u - 0.5;
1979
+ const dy = v - 0.5;
1980
+ const radial = Math.min(1, Math.hypot(dx * 2, dy * 2));
1981
+ const ang = Math.atan2(dy, dx) + (rand(idx * 5 + 4) - 0.5) * 0.65;
1982
+ const polar = radial * Math.PI * 0.96 + (rand(idx * 5 + 5) - 0.5) * 0.28;
1983
+ const ridge =
1984
+ 0.66 +
1985
+ 0.34 * Math.abs(Math.sin(u * 14.5 + v * 10.2 + seed)) +
1986
+ 0.22 * Math.abs(Math.cos(v * 12.7 - u * 8.9)) +
1987
+ (rand(idx * 7 + 2) - 0.5) * 0.24;
1988
+ const rr = ballR * ridge;
1989
+ const bx = rr * Math.sin(polar) * Math.cos(ang);
1990
+ const by = rr * Math.sin(polar) * Math.sin(ang) * 0.92;
1991
+ const bz = rr * Math.cos(polar);
1992
+
1993
+ // Corners fold ahead of center
1994
+ const delay = Math.max(0, (1.15 - radial) * 0.22);
1995
+ verts.push({
1996
+ fx, fy, mx, my, mz, bx, by, bz, delay,
1997
+ ridge,
1998
+ tu: snap ? u * snap.tex.width : 0,
1999
+ tv: snap ? v * snap.tex.height : 0,
2000
+ });
2001
+ }
2002
+ }
2003
+
2004
+ const tris = [];
2005
+ for (let r = 0; r < ROWS; r++) {
2006
+ for (let c = 0; c < COLS; c++) {
2007
+ const i00 = r * (COLS + 1) + c;
2008
+ const i10 = i00 + 1;
2009
+ const i01 = i00 + (COLS + 1);
2010
+ const i11 = i01 + 1;
2011
+ // Alternate diagonal orientation like real criss-cross paper creases
2012
+ if ((r + c) % 2 === 0) {
2013
+ tris.push({ a: i00, b: i10, c: i11 });
2014
+ tris.push({ a: i00, b: i11, c: i01 });
2015
+ } else {
2016
+ tris.push({ a: i00, b: i10, c: i01 });
2017
+ tris.push({ a: i10, b: i11, c: i01 });
2018
+ }
2019
+ }
2020
+ }
2021
+
2022
+ const flecks = Array.from({ length: 8 }, (_, i) => {
2023
+ const ang = (Math.PI * 2 * i) / 8 + (rand(500 + i) - 0.5) * 0.45;
2024
+ const spd = 26 + rand(600 + i) * 34;
2025
+ return { vx: Math.cos(ang) * spd, vy: Math.sin(ang) * spd - 16, size: 3.5 + rand(700 + i) * 3.5, rot: rand(800 + i) * 6 };
2026
+ });
2027
+
2028
+ const T_CRUMPLE = 320;
2029
+ const T_PAUSE = 420;
2030
+ const T_TOSS = 740;
2031
+ const T_END = 940;
2032
+ const tStart = performance.now();
2033
+ let binHitTriggered = false;
2034
+ const trail = [];
2035
+
2036
+ // Crisp studio top-left key light
2037
+ const lx = -0.46, ly = -0.60, lz = 0.65;
2038
+
2039
+ function easeOutCubic(x) { return 1 - Math.pow(1 - x, 3); }
2040
+
2041
+ function renderFrame(now) {
2042
+ const elapsed = now - tStart;
2043
+ ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
2044
+
2045
+ if (elapsed <= T_TOSS) {
2046
+ let cx = startCx;
2047
+ let cy = startCy;
2048
+ let scale = 1;
2049
+ let rx = 0, ry = 0, rz = 0;
2050
+ let alpha = 1;
2051
+ let shadowAlpha = 0.18;
2052
+ let shadowDropY = Math.min(ballR * 0.72, Math.max(8, rect.height * 0.5 - 12));
2053
+
2054
+ if (elapsed < T_CRUMPLE) {
2055
+ const p = elapsed / T_CRUMPLE;
2056
+ // Energetic double-grip crumple tremor (contained strictly inside card slot)
2057
+ const shake = Math.sin(p * Math.PI * 6) * (1 - p) * 2.8;
2058
+ cx += shake;
2059
+ cy += Math.cos(p * Math.PI * 5) * (1 - p) * 1.4;
2060
+ rx = p * p * 0.42;
2061
+ ry = p * p * 0.58;
2062
+ rz = Math.sin(p * Math.PI) * 0.06;
2063
+ shadowAlpha = 0.11 + 0.14 * p;
2064
+ } else if (elapsed < T_PAUSE) {
2065
+ // Secondary palm squeeze -> elastic pop settle (still cleanly inside own slot)
2066
+ const p = (elapsed - T_CRUMPLE) / (T_PAUSE - T_CRUMPLE);
2067
+ if (p < 0.45) {
2068
+ const sp = p / 0.45;
2069
+ scale = 1 - Math.sin(sp * Math.PI) * 0.11; // crisp squeeze inward
2070
+ } else {
2071
+ const bp = (p - 0.45) / 0.55;
2072
+ scale = 1 + Math.sin(bp * Math.PI) * 0.07; // light elastic rebound
2073
+ }
2074
+ rx = 0.42 + p * 0.16;
2075
+ ry = 0.58 + p * 0.24;
2076
+ rz = -0.05 * Math.sin(p * Math.PI);
2077
+ shadowAlpha = 0.24;
2078
+ } else {
2079
+ // Snappy wrist-flick parabolic toss with 3D spin into Recycle Bin
2080
+ const p = Math.min(1, (elapsed - T_PAUSE) / (T_TOSS - T_PAUSE));
2081
+ const ep = 1 - Math.pow(1 - p, 1.9);
2082
+ const ctrlX = (startCx + targetCx) * 0.5 + (targetCx >= startCx ? 38 : -38);
2083
+ const ctrlY = Math.min(startCy, targetCy) - 68;
2084
+ const omt = 1 - ep;
2085
+ cx = omt * omt * startCx + 2 * omt * ep * ctrlX + ep * ep * targetCx;
2086
+ cy = omt * omt * startCy + 2 * omt * ep * ctrlY + ep * ep * targetCy;
2087
+ scale = 1 - ep * 0.66;
2088
+ rx = 0.58 + ep * 4.8;
2089
+ ry = 0.82 + ep * 5.6;
2090
+ rz = ep * 4.2;
2091
+ alpha = p > 0.84 ? Math.max(0, 1 - (p - 0.84) / 0.16) : 1;
2092
+ // Fade ground shadow out quickly on liftoff so it never casts over cards below
2093
+ shadowAlpha = 0.18 * Math.max(0, 1 - p * 3.5);
2094
+ shadowDropY = Math.min(ballR * 0.78, Math.max(8, rect.height * 0.5 - 12));
2095
+
2096
+ // Only collapse the empty card slot once the paper ball has physically cleared above rect.top
2097
+ if (cy + ballR * scale * 1.2 <= rect.top || elapsed >= T_PAUSE + 100) {
2098
+ startCollapse();
2099
+ }
2100
+
2101
+ if (p > 0.08) {
2102
+ trail.push({ x: cx, y: cy, r: Math.max(3, ballR * 0.34 * scale) });
2103
+ if (trail.length > 6) trail.shift();
2104
+ }
2105
+ }
2106
+
2107
+ // Subtle motion arc trail during toss
2108
+ if (trail.length > 2) {
2109
+ ctx.save();
2110
+ ctx.lineCap = "round";
2111
+ for (let i = 1; i < trail.length; i++) {
2112
+ const frac = i / trail.length;
2113
+ const fade = frac * frac;
2114
+ ctx.strokeStyle = isDark
2115
+ ? `rgba(129, 140, 248, ${(fade * 0.22 * alpha).toFixed(3)})`
2116
+ : `rgba(99, 102, 241, ${(fade * 0.18 * alpha).toFixed(3)})`;
2117
+ ctx.lineWidth = trail[i].r * frac * 0.68;
2118
+ ctx.beginPath();
2119
+ ctx.moveTo(trail[i - 1].x, trail[i - 1].y);
2120
+ ctx.lineTo(trail[i].x, trail[i].y);
2121
+ ctx.stroke();
2122
+ }
2123
+ ctx.restore();
2124
+ }
2125
+
2126
+ // Compact drop shadow kept inside card slot (never spills onto lower cards)
2127
+ if (shadowAlpha > 0.01) {
2128
+ ctx.save();
2129
+ ctx.globalAlpha = shadowAlpha * alpha;
2130
+ const sw = (elapsed < T_CRUMPLE ? rect.width * (0.40 - 0.26 * (elapsed / T_CRUMPLE)) : ballR * 1.08) * scale;
2131
+ const sh = Math.max(7, Math.min(12, ballR * 0.34 * scale));
2132
+ const maxDrop = Math.max(6, rect.height * 0.5 - sh - 3);
2133
+ const sx = cx + 3;
2134
+ const sy = cy + Math.min(shadowDropY, maxDrop) * scale;
2135
+ const grad = ctx.createRadialGradient(sx, sy, 2, sx, sy, sw);
2136
+ grad.addColorStop(0, isDark ? "rgba(0,0,0,0.62)" : "rgba(15,23,42,0.28)");
2137
+ grad.addColorStop(0.55, isDark ? "rgba(0,0,0,0.22)" : "rgba(15,23,42,0.09)");
2138
+ grad.addColorStop(1, "rgba(15,23,42,0)");
2139
+ ctx.fillStyle = grad;
2140
+ ctx.beginPath();
2141
+ ctx.ellipse(sx, sy, sw, sh, 0, 0, Math.PI * 2);
2142
+ ctx.fill();
2143
+ ctx.restore();
2144
+ }
2145
+
2146
+ const cosX = Math.cos(rx), sinX = Math.sin(rx);
2147
+ const cosY = Math.cos(ry), sinY = Math.sin(ry);
2148
+ const cosZ = Math.cos(rz), sinZ = Math.sin(rz);
2149
+
2150
+ // Project vertices via 2-stage spline: flat -> creased fold -> polyhedral ball
2151
+ const proj = new Array(verts.length);
2152
+ const persp = 540;
2153
+ const rawP = Math.min(1, elapsed / T_CRUMPLE);
2154
+ for (let i = 0; i < verts.length; i++) {
2155
+ const vt = verts[i];
2156
+ const lp = Math.max(0, Math.min(1, (rawP - vt.delay) / Math.max(0.24, 1 - vt.delay)));
2157
+ const e = easeOutCubic(lp);
2158
+ // Quadratic Bezier interpolation through creased midpoint (mx, my, mz)
2159
+ const omt = 1 - e;
2160
+ let x = omt * omt * vt.fx + 2 * omt * e * vt.mx + e * e * vt.bx;
2161
+ let y = omt * omt * vt.fy + 2 * omt * e * vt.my + e * e * vt.by;
2162
+ let z = 2 * omt * e * vt.mz + e * e * vt.bz;
2163
+
2164
+ let x1 = x * cosY + z * sinY;
2165
+ let z1 = -x * sinY + z * cosY;
2166
+ let y2 = y * cosX - z1 * sinX;
2167
+ let z2 = y * sinX + z1 * cosX;
2168
+ let x3 = x1 * cosZ - y2 * sinZ;
2169
+ let y3 = x1 * sinZ + y2 * cosZ;
2170
+
2171
+ const pf = (persp / (persp - z2 * scale)) * scale;
2172
+ proj[i] = { x: cx + x3 * pf, y: cy + y3 * pf, x3, y3, z3: z2, ridge: vt.ridge, tu: vt.tu, tv: vt.tv };
2173
+ }
2174
+
2175
+ // Painter's depth sort (back-to-front)
2176
+ const drawList = [];
2177
+ for (let i = 0; i < tris.length; i++) {
2178
+ const t = tris[i];
2179
+ const pa = proj[t.a], pb = proj[t.b], pc = proj[t.c];
2180
+ drawList.push({ pa, pb, pc, depth: (pa.z3 + pb.z3 + pc.z3) / 3 });
2181
+ }
2182
+ drawList.sort((m, n) => m.depth - n.depth);
2183
+
2184
+ ctx.save();
2185
+ ctx.globalAlpha = alpha;
2186
+ const crumpleStrength = Math.min(1, elapsed / (T_CRUMPLE * 0.85));
2187
+ for (let i = 0; i < drawList.length; i++) {
2188
+ const { pa, pb, pc } = drawList[i];
2189
+ const ux = pb.x3 - pa.x3, uy = pb.y3 - pa.y3, uz = pb.z3 - pa.z3;
2190
+ const vx = pc.x3 - pa.x3, vy = pc.y3 - pa.y3, vz = pc.z3 - pa.z3;
2191
+ let nx = uy * vz - uz * vy;
2192
+ let ny = uz * vx - ux * vz;
2193
+ let nz = ux * vy - uy * vx;
2194
+ const len = Math.hypot(nx, ny, nz) || 1;
2195
+ nx /= len; ny /= len; nz /= len;
2196
+ if (nz < 0) { nx = -nx; ny = -ny; nz = -nz; }
2197
+
2198
+ // 1. Draw real affine-mapped card texture on this 3D paper facet
2199
+ if (snap) {
2200
+ drawTexturedTriangle(
2201
+ ctx, snap.tex,
2202
+ pa.x, pa.y, pb.x, pb.y, pc.x, pc.y,
2203
+ pa.tu, pa.tv, pb.tu, pb.tv, pc.tu, pc.tv
2204
+ );
2205
+ } else {
2206
+ ctx.fillStyle = isDark ? "#233250" : "#f8fafc";
2207
+ ctx.beginPath();
2208
+ ctx.moveTo(pa.x, pa.y); ctx.lineTo(pb.x, pb.y); ctx.lineTo(pc.x, pc.y);
2209
+ ctx.closePath();
2210
+ ctx.fill();
2211
+ }
2212
+
2213
+ // 2. Overlay crisp directional flat-shading + valley Ambient Occlusion (creates iconic 3D paper creases)
2214
+ const diff = Math.max(0, nx * lx + ny * ly + nz * lz);
2215
+ const avgRidge = (pa.ridge + pb.ridge + pc.ridge) / 3;
2216
+ const aoValley = Math.max(0, 1.02 - avgRidge) * 0.58; // deeper shadows inside fold valleys
2217
+ const darkness = Math.min(0.68, ((1 - diff) * 0.44 + aoValley) * crumpleStrength);
2218
+ const highlight = Math.max(0, (diff - 0.68) * 0.62 * crumpleStrength);
2219
+
2220
+ ctx.beginPath();
2221
+ ctx.moveTo(pa.x, pa.y); ctx.lineTo(pb.x, pb.y); ctx.lineTo(pc.x, pc.y);
2222
+ ctx.closePath();
2223
+ if (darkness > 0.015) {
2224
+ ctx.fillStyle = isDark
2225
+ ? `rgba(4, 8, 20, ${Math.min(0.76, darkness * 1.15).toFixed(3)})`
2226
+ : `rgba(24, 34, 62, ${darkness.toFixed(3)})`;
2227
+ ctx.fill();
2228
+ }
2229
+ if (highlight > 0.015) {
2230
+ ctx.fillStyle = `rgba(255, 255, 255, ${highlight.toFixed(3)})`;
2231
+ ctx.fill();
2232
+ }
2233
+ // Delicate paper crease edge
2234
+ if (crumpleStrength > 0.08) {
2235
+ ctx.strokeStyle = isDark
2236
+ ? `rgba(255,255,255,${(0.06 * crumpleStrength).toFixed(3)})`
2237
+ : `rgba(15,23,42,${(0.08 * crumpleStrength).toFixed(3)})`;
2238
+ ctx.lineWidth = 0.55;
2239
+ ctx.stroke();
2240
+ }
2241
+ }
2242
+ ctx.restore();
2243
+ } else {
2244
+ startCollapse();
2245
+ }
2246
+
2247
+ if (elapsed >= T_TOSS - 25 && !binHitTriggered) {
2248
+ binHitTriggered = true;
2249
+ if (binTab) {
2250
+ binTab.classList.remove("bin-hit");
2251
+ void binTab.offsetWidth;
2252
+ binTab.classList.add("bin-hit");
2253
+ setTimeout(() => binTab.classList.remove("bin-hit"), 460);
2254
+ }
2255
+ }
2256
+ if (elapsed >= T_TOSS - 15 && elapsed <= T_END) {
2257
+ const fp = (elapsed - (T_TOSS - 15)) / (T_END - (T_TOSS - 15));
2258
+ ctx.save();
2259
+ ctx.globalAlpha = Math.max(0, 1 - fp);
2260
+ for (let i = 0; i < flecks.length; i++) {
2261
+ const fl = flecks[i];
2262
+ const fx = targetCx + fl.vx * fp;
2263
+ const fy = targetCy + fl.vy * fp + 24 * fp * fp;
2264
+ ctx.save();
2265
+ ctx.translate(fx, fy);
2266
+ ctx.rotate(fl.rot + fp * 5.0);
2267
+ ctx.fillStyle = i % 3 === 0 ? "#6366f1" : (isDark ? "#cbd5e1" : "#f1f5f9");
2268
+ ctx.strokeStyle = isDark ? "rgba(255,255,255,0.2)" : "rgba(15,23,42,0.18)";
2269
+ ctx.lineWidth = 0.5;
2270
+ ctx.fillRect(-fl.size * 0.5, -fl.size * 0.35, fl.size, fl.size * 0.7);
2271
+ ctx.strokeRect(-fl.size * 0.5, -fl.size * 0.35, fl.size, fl.size * 0.7);
2272
+ ctx.restore();
2273
+ }
2274
+ ctx.restore();
2275
+ }
2276
+
2277
+ if (elapsed < T_END) {
2278
+ requestAnimationFrame(renderFrame);
2279
+ } else {
2280
+ canvas.remove();
2281
+ cardEl.remove();
2282
+ resolve();
2283
+ }
2284
+ }
2285
+
2286
+ requestAnimationFrame(renderFrame);
2287
+ });
2288
+ }
2289
+
2290
+ async function loadMemories() {
2291
+ const q = $("zmSearchInput").value.trim();
2292
+ const ns = state.selectedNs;
2293
+ const qs = new URLSearchParams({ q, limit: "50" });
2294
+ if (ns) qs.set("ns", ns);
2295
+ try {
2296
+ const data = await api(`/memories?${qs.toString()}`);
2297
+ const list = $("zmList");
2298
+ $("zmSearchMeta").textContent = q
2299
+ ? `命中 ${data.count} 条 · ${data.elapsed_ms}ms`
2300
+ : `最新 ${data.count} 条`;
2301
+
2302
+ if (!data.items || !data.items.length) {
2303
+ list.innerHTML = '<div class="zm-empty">暂无匹配记忆<br/>点右上角「写入」或在对话中自然沉淀</div>';
2304
+ return;
2305
+ }
2306
+
2307
+ list.innerHTML = data.items
2308
+ .map((item) => {
2309
+ const evHtml = (item.evidence || [])
2310
+ .map((ev) => `<div class="zm-ev-item"><span class="zm-ev-id">${esc(ev.id)}</span>${highlightRedacted(ev.text)}</div>`)
2311
+ .join("");
2312
+ return `
2313
+ <article class="zm-card">
2314
+ <div class="zm-card-top">
2315
+ <div class="zm-tags">
2316
+ <span class="zm-tag">#${item.fact_id}</span>
2317
+ <span class="zm-tag">${esc(item.namespace)}</span>
2318
+ ${item.action === "revise" ? '<span class="zm-tag rev">已纠错</span>' : ""}
2319
+ ${item.until_expiry ? `<span class="zm-tag exp">${esc(item.until_expiry.label)}</span>` : ""}
2320
+ </div>
2321
+ <button class="zm-icon-btn square danger zm-evict-btn" data-fid="${item.fact_id}" type="button" aria-label="移入回收站" title="软淘汰至回收站(可恢复)">${ICONS.trash}</button>
2322
+ </div>
2323
+ <div class="zm-fact-text" data-toggle-ev="1" title="点击展开 / 收起原文">${highlightRedacted(item.text)}</div>
2324
+ <div class="zm-evidence-wrap">
2325
+ <div class="zm-evidence-clip">
2326
+ <div class="zm-evidence">${evHtml || '<span style="color:var(--zm-muted)">无绑定原文片段</span>'}</div>
2327
+ </div>
2328
+ </div>
2329
+ </article>
2330
+ `;
2331
+ })
2332
+ .join("");
2333
+
2334
+ list.querySelectorAll("[data-toggle-ev]").forEach((el) => {
2335
+ el.addEventListener("click", () => {
2336
+ el.nextElementSibling?.classList.toggle("open");
2337
+ });
2338
+ });
2339
+
2340
+ list.querySelectorAll(".zm-evict-btn").forEach((btn) => {
2341
+ const fid = Number(btn.getAttribute("data-fid"));
2342
+ const cardEl = btn.closest(".zm-card");
2343
+ bindTwoStepConfirm(btn, async () => {
2344
+ await withUiBusy("移入回收站", async () => {
2345
+ try {
2346
+ state.evicting = true;
2347
+ await api("/evict", {
2348
+ method: "POST",
2349
+ body: JSON.stringify({ fact_id: fid, reason: "web-ui", force: true }),
2350
+ });
2351
+ await playPaperCrumpleToTrash(cardEl);
2352
+ await refreshStatus(true);
2353
+ if (!list.querySelector(".zm-card")) {
2354
+ await loadMemories();
2355
+ } else {
2356
+ const remain = list.querySelectorAll(".zm-card").length;
2357
+ $("zmSearchMeta").textContent = $("zmSearchInput").value.trim() ? `命中 ${remain} 条` : `最新 ${remain} 条`;
2358
+ }
2359
+ } catch (err) {
2360
+ showToast(`操作失败:${err.message}`, false);
2361
+ } finally {
2362
+ state.evicting = false;
2363
+ }
2364
+ });
2365
+ });
2366
+ });
2367
+ } catch (err) {
2368
+ showToast(`检索失败:${err.message}`, false);
2369
+ }
2370
+ }
2371
+
2372
+ async function loadRecycleBin() {
2373
+ try {
2374
+ const data = await api("/evicted");
2375
+ const box = $("zmRecycleList");
2376
+ if (!data.batches || !data.batches.length) {
2377
+ box.innerHTML = '<div class="zm-empty">回收站为空</div>';
2378
+ return;
2379
+ }
2380
+ box.innerHTML = data.batches
2381
+ .map(
2382
+ (b) => `
2383
+ <article class="zm-card">
2384
+ <div class="zm-card-top">
2385
+ <div class="zm-tags">
2386
+ <span class="zm-tag">${esc(b.batch_id)}</span>
2387
+ <span class="zm-tag">${b.record_count} 项</span>
2388
+ <span class="zm-tag">${esc(b.reason)}</span>
2389
+ </div>
2390
+ <button class="zm-icon-btn primary zm-restore-btn" data-bid="${esc(b.batch_id)}" type="button" title="校验 SHA-256 并原子恢复">${ICONS.restore}<span>恢复</span></button>
2391
+ </div>
2392
+ <div style="font-size:11px;color:var(--zm-muted);">${esc((b.evicted_at || "").replace("T", " ").slice(0, 19))} · ${esc((b.namespaces || []).join(", "))}</div>
2393
+ ${(b.samples || []).map((s) => `<div style="font-size:11.5px;color:var(--zm-text-2);">• ${esc(s.preview)}</div>`).join("")}
2394
+ </article>
2395
+ `
2396
+ )
2397
+ .join("");
2398
+
2399
+ box.querySelectorAll(".zm-restore-btn").forEach((btn) => {
2400
+ btn.addEventListener("click", async () => {
2401
+ const bid = btn.getAttribute("data-bid");
2402
+ await withUiBusy("恢复中", async () => {
2403
+ try {
2404
+ const res = await api("/restore", {
2405
+ method: "POST",
2406
+ body: JSON.stringify({ batch_id: bid }),
2407
+ });
2408
+ showToast(`指纹通过,已恢复 ${res.result.restored_facts} 条记忆`);
2409
+ await Promise.all([refreshStatus(true), loadRecycleBin()]);
2410
+ } catch (err) {
2411
+ showToast(`恢复失败:${err.message}`, false);
2412
+ }
2413
+ });
2414
+ });
2415
+ });
2416
+ } catch (err) {
2417
+ showToast(`加载回收站失败:${err.message}`, false);
2418
+ }
2419
+ }
2420
+
2421
+ function readDraftConfig() {
2422
+ const apiBaseUrl = $("zmApiBaseUrl").value.trim();
2423
+ const apiModel = $("zmApiModel").value.trim();
2424
+ if (state.activePresetId === "custom" && apiBaseUrl) {
2425
+ state.customApiBaseUrl = apiBaseUrl;
2426
+ state.customApiModel = apiModel;
2427
+ }
2428
+ return {
2429
+ provider: state.providerDraft,
2430
+ ollamaUrl: $("zmOllamaUrl").value.trim(),
2431
+ ollamaModel: $("zmOllamaModel").value.trim(),
2432
+ numCtx: Number($("zmNumCtx").value || 8192),
2433
+ apiBaseUrl,
2434
+ apiModel,
2435
+ customApiBaseUrl: state.customApiBaseUrl || "",
2436
+ customApiModel: state.customApiModel || "",
2437
+ apiKey: $("zmApiKey").value.trim(),
2438
+ clearApiKey: state.clearApiKeyFlag,
2439
+ autoRecall: $("zmCfgAutoRecall").checked,
2440
+ autoRemember: $("zmCfgAutoRemember").checked,
2441
+ verify: $("zmCfgVerify").checked,
2442
+ untilExpiry: $("zmCfgUntil").checked,
2443
+ adaptive: $("zmCfgAdaptive").checked,
2444
+ namespaceMode: $("zmCfgWsNs").checked ? "workspace" : "fixed",
2445
+ uiTheme: state.theme,
2446
+ };
2447
+ }
2448
+
2449
+ function openInstallModal() {
2450
+ const ps = state.lastProviderStatus || {};
2451
+ const note = $("zmInstallModalNote");
2452
+ const confirmBtn = $("zmInstallConfirmBtn");
2453
+ if (ps.state === "ollama_not_installed") {
2454
+ const guide = ps.install_guide || { command: "winget install Ollama.Ollama", downloadUrl: "https://ollama.com/download" };
2455
+ note.innerHTML = `未检测到本机 Ollama,请先安装 Ollama(命令:<code>${esc(guide.command)}</code>)后再确认下载。`;
2456
+ confirmBtn.innerHTML = `${ICONS.external}<span>前往下载 Ollama</span>`;
2457
+ confirmBtn.dataset.mode = "open-ollama-url";
2458
+ confirmBtn.dataset.url = guide.downloadUrl;
2459
+ } else if (ps.state === "ollama_stopped") {
2460
+ note.textContent = "检测到本机 Ollama 尚未启动:确认后将先自动后台启动 Ollama,再拉取开源专项小模型。";
2461
+ confirmBtn.innerHTML = `${ICONS.check}<span>确认启动并安装</span>`;
2462
+ confirmBtn.dataset.mode = "start-and-pull";
2463
+ } else {
2464
+ note.textContent = "确认后将通过本机 Ollama 下载开源专项小模型(作者 @B站IM榴莲),期间可实时查看进度并随时取消。";
2465
+ confirmBtn.innerHTML = `${ICONS.check}<span>确认安装</span>`;
2466
+ confirmBtn.dataset.mode = "pull";
2467
+ }
2468
+ $("zmInstallModal").classList.add("open");
2469
+ }
2470
+
2471
+ function closeInstallModal() {
2472
+ $("zmInstallModal").classList.remove("open");
2473
+ }
2474
+
2475
+ async function handleWallpaperFile(file) {
2476
+ if (!file) return;
2477
+ await withUiBusy("换壁纸", async () => {
2478
+ try {
2479
+ const dataUrl = await prepareImageDataUrl(file);
2480
+ state.hasCustomBg = true;
2481
+ state.bgUrl = dataUrl;
2482
+ applyAppearance();
2483
+
2484
+ const res = await api("/background", {
2485
+ method: "POST",
2486
+ body: JSON.stringify({ dataUrl }),
2487
+ });
2488
+ if (res.config?.uiBackgroundUrl) {
2489
+ state.hasCustomBg = true;
2490
+ state.bgUrl = res.config.uiBackgroundUrl;
2491
+ applyAppearance();
2492
+ }
2493
+ showToast("背景图已更新并应用毛玻璃");
2494
+ } catch (err) {
2495
+ showToast(`背景图设置失败:${err.message}`, false);
2496
+ }
2497
+ });
2498
+ }
2499
+
2500
+ function switchTab(tab) {
2501
+ state.tab = tab;
2502
+ const idxMap = { memories: 0, recycle: 1, settings: 2 };
2503
+ $("zmTabsNav").style.setProperty("--zm-tab-idx", String(idxMap[tab] ?? 0));
2504
+ shadow.querySelectorAll(".zm-tab").forEach((btn) => {
2505
+ const active = btn.getAttribute("data-tab") === tab;
2506
+ btn.classList.toggle("active", active);
2507
+ btn.setAttribute("aria-selected", active ? "true" : "false");
2508
+ });
2509
+ $("zmPaneMemories").classList.toggle("active", tab === "memories");
2510
+ $("zmPaneRecycle").classList.toggle("active", tab === "recycle");
2511
+ $("zmPaneSettings").classList.toggle("active", tab === "settings");
2512
+ if (tab === "memories") loadMemories();
2513
+ if (tab === "recycle") loadRecycleBin();
2514
+ if (tab === "settings") refreshStatus(false);
2515
+ }
2516
+
2517
+ function setOpen(flag) {
2518
+ state.open = Boolean(flag);
2519
+ $("zmDrawer").classList.toggle("open", state.open);
2520
+ $("zmBackdrop").classList.toggle("open", state.open);
2521
+ if (state.open) {
2522
+ refreshStatus(false);
2523
+ if (state.tab === "memories") loadMemories();
2524
+ if (state.tab === "recycle") loadRecycleBin();
2525
+ }
2526
+ }
2527
+
2528
+ // Draggable pill shell with click detection
2529
+ (function initPillDrag() {
2530
+ const shell = $("zmPillShell");
2531
+ const pill = $("zmPill");
2532
+ try {
2533
+ const saved = JSON.parse(localStorage.getItem(POS_KEY) || "null");
2534
+ if (saved && typeof saved.right === "number" && typeof saved.bottom === "number") {
2535
+ shell.style.right = `${Math.max(8, Math.min(window.innerWidth - 104, saved.right))}px`;
2536
+ shell.style.bottom = `${Math.max(8, Math.min(window.innerHeight - 52, saved.bottom))}px`;
2537
+ }
2538
+ } catch {}
2539
+
2540
+ let dragging = false;
2541
+ let moved = false;
2542
+ let startX = 0;
2543
+ let startY = 0;
2544
+ let startRight = 20;
2545
+ let startBottom = 88;
2546
+
2547
+ pill.addEventListener("pointerdown", (e) => {
2548
+ if (e.button !== 0) return;
2549
+ dragging = true;
2550
+ moved = false;
2551
+ startX = e.clientX;
2552
+ startY = e.clientY;
2553
+ const rect = shell.getBoundingClientRect();
2554
+ startRight = window.innerWidth - rect.right;
2555
+ startBottom = window.innerHeight - rect.bottom;
2556
+ pill.setPointerCapture(e.pointerId);
2557
+ });
2558
+
2559
+ pill.addEventListener("pointermove", (e) => {
2560
+ if (!dragging) return;
2561
+ const dx = e.clientX - startX;
2562
+ const dy = e.clientY - startY;
2563
+ if (Math.abs(dx) > 4 || Math.abs(dy) > 4) moved = true;
2564
+ if (!moved) return;
2565
+ const r = Math.max(8, Math.min(window.innerWidth - 104, startRight - dx));
2566
+ const b = Math.max(8, Math.min(window.innerHeight - 52, startBottom - dy));
2567
+ shell.style.right = `${r}px`;
2568
+ shell.style.bottom = `${b}px`;
2569
+ });
2570
+
2571
+ pill.addEventListener("pointerup", () => {
2572
+ if (!dragging) return;
2573
+ dragging = false;
2574
+ if (moved) {
2575
+ try {
2576
+ localStorage.setItem(
2577
+ POS_KEY,
2578
+ JSON.stringify({ right: parseFloat(shell.style.right) || 20, bottom: parseFloat(shell.style.bottom) || 88 })
2579
+ );
2580
+ } catch {}
2581
+ } else {
2582
+ setOpen(!state.open);
2583
+ }
2584
+ });
2585
+ })();
2586
+
2587
+ $("zmBackdrop").addEventListener("click", () => setOpen(false));
2588
+ $("zmCloseBtn").addEventListener("click", () => setOpen(false));
2589
+ $("zmRefreshBtn").addEventListener("click", () => {
2590
+ withUiBusy("刷新中", async () => {
2591
+ await refreshStatus(false);
2592
+ if (state.tab === "memories") await loadMemories();
2593
+ if (state.tab === "recycle") await loadRecycleBin();
2594
+ });
2595
+ });
2596
+
2597
+ async function setTheme(next) {
2598
+ state.theme = next === "dark" ? "dark" : "light";
2599
+ applyAppearance();
2600
+ try {
2601
+ await api("/config", { method: "POST", body: JSON.stringify({ uiTheme: state.theme }) });
2602
+ } catch {
2603
+ // fail-open
2604
+ }
2605
+ }
2606
+ $("zmThemeBtn").addEventListener("click", () => setTheme(state.theme === "dark" ? "light" : "dark"));
2607
+ $("zmThemeLightBtn").addEventListener("click", () => setTheme("light"));
2608
+ $("zmThemeDarkBtn").addEventListener("click", () => setTheme("dark"));
2609
+
2610
+ const fileInput = $("zmBgFileInput");
2611
+ const openFilePicker = () => {
2612
+ fileInput.value = "";
2613
+ fileInput.click();
2614
+ };
2615
+ $("zmBgQuickBtn").addEventListener("click", openFilePicker);
2616
+ $("zmBgDrop").addEventListener("click", (e) => {
2617
+ if (e.target.closest("button")) return;
2618
+ openFilePicker();
2619
+ });
2620
+ $("zmBgDrop").addEventListener("keydown", (e) => {
2621
+ if (e.key === "Enter" || e.key === " ") {
2622
+ e.preventDefault();
2623
+ openFilePicker();
2624
+ }
2625
+ });
2626
+ fileInput.addEventListener("change", () => {
2627
+ const file = fileInput.files?.[0];
2628
+ if (file) handleWallpaperFile(file);
2629
+ });
2630
+
2631
+ const bgDrop = $("zmBgDrop");
2632
+ bgDrop.addEventListener("dragover", (e) => {
2633
+ e.preventDefault();
2634
+ bgDrop.classList.add("dragover");
2635
+ });
2636
+ bgDrop.addEventListener("dragleave", () => bgDrop.classList.remove("dragover"));
2637
+ bgDrop.addEventListener("drop", (e) => {
2638
+ e.preventDefault();
2639
+ bgDrop.classList.remove("dragover");
2640
+ const file = e.dataTransfer?.files?.[0];
2641
+ if (file) handleWallpaperFile(file);
2642
+ });
2643
+
2644
+ $("zmClearBgBtn").addEventListener("click", async (e) => {
2645
+ e.stopPropagation();
2646
+ await withUiBusy("重置壁纸", async () => {
2647
+ try {
2648
+ await api("/background", { method: "DELETE" });
2649
+ state.hasCustomBg = false;
2650
+ state.bgUrl = "";
2651
+ applyAppearance();
2652
+ showToast("已恢复默认毛玻璃");
2653
+ } catch (err) {
2654
+ showToast(`清除失败:${err.message}`, false);
2655
+ }
2656
+ });
2657
+ });
2658
+
2659
+ window.addEventListener("keydown", (e) => {
2660
+ if (e.key === "Escape") {
2661
+ if ($("zmInstallModal").classList.contains("open")) {
2662
+ closeInstallModal();
2663
+ return;
2664
+ }
2665
+ if ($("zmNsDd").classList.contains("open")) {
2666
+ setNsMenuOpen(false);
2667
+ return;
2668
+ }
2669
+ if (state.open) setOpen(false);
2670
+ }
2671
+ });
2672
+
2673
+ $("zmOpenInstallModalBtn").addEventListener("click", () => {
2674
+ openInstallModal();
2675
+ });
2676
+ $("zmInstallCancelBtn").addEventListener("click", closeInstallModal);
2677
+ $("zmInstallModal").addEventListener("click", (e) => {
2678
+ if (e.target === $("zmInstallModal")) closeInstallModal();
2679
+ });
2680
+ $("zmInstallVerGrid").querySelectorAll(".zm-ver-card").forEach((card) => {
2681
+ card.addEventListener("click", () => {
2682
+ state.selectedInstallModel = card.getAttribute("data-model") || state.selectedInstallModel;
2683
+ $("zmInstallVerGrid").querySelectorAll(".zm-ver-card").forEach((c) => {
2684
+ c.classList.toggle("active", c === card);
2685
+ });
2686
+ });
2687
+ });
2688
+ $("zmInstallConfirmBtn").addEventListener("click", async () => {
2689
+ const mode = $("zmInstallConfirmBtn").dataset.mode || "pull";
2690
+ if (mode === "open-ollama-url") {
2691
+ window.open($("zmInstallConfirmBtn").dataset.url || "https://ollama.com/download", "_blank", "noopener,noreferrer");
2692
+ closeInstallModal();
2693
+ return;
2694
+ }
2695
+ closeInstallModal();
2696
+ if (mode === "start-and-pull") {
2697
+ await triggerStartOllama();
2698
+ }
2699
+ await triggerPullModel(state.selectedInstallModel);
2700
+ });
2701
+
2702
+ $("zmApiBaseUrl").addEventListener("input", () => {
2703
+ const matched = detectPresetId($("zmApiBaseUrl").value, state.config?.presets || []);
2704
+ updateApiPresetUi(matched);
2705
+ if (matched === "custom") {
2706
+ state.customApiBaseUrl = $("zmApiBaseUrl").value.trim();
2707
+ }
2708
+ });
2709
+ $("zmApiModel").addEventListener("input", () => {
2710
+ if (state.activePresetId === "custom") {
2711
+ state.customApiModel = $("zmApiModel").value.trim();
2712
+ }
2713
+ });
2714
+
2715
+ shadow.addEventListener("click", (e) => {
2716
+ if (!e.target?.closest?.("#zmNsDd")) setNsMenuOpen(false);
2717
+ });
2718
+
2719
+ $("zmNsTrigger").addEventListener("click", (e) => {
2720
+ e.stopPropagation();
2721
+ setNsMenuOpen(!$("zmNsDd").classList.contains("open"));
2722
+ });
2723
+
2724
+ shadow.querySelectorAll(".zm-tab").forEach((btn) => {
2725
+ btn.addEventListener("click", () => switchTab(btn.getAttribute("data-tab")));
2726
+ });
2727
+
2728
+ $("zmSearchInput").addEventListener("input", () => {
2729
+ clearTimeout(state.searchTimer);
2730
+ state.searchTimer = setTimeout(loadMemories, 150);
2731
+ });
2732
+
2733
+ $("zmAddToggle").addEventListener("click", () => {
2734
+ const wrap = $("zmAddWrap");
2735
+ const open = wrap.classList.toggle("open");
2736
+ if (open) setTimeout(() => $("zmAddText").focus(), 140);
2737
+ });
2738
+
2739
+ $("zmAddSubmit").addEventListener("click", async () => {
2740
+ const content = $("zmAddText").value.trim();
2741
+ if (!content) {
2742
+ showToast("请先输入内容", false);
2743
+ return;
2744
+ }
2745
+ const btn = $("zmAddSubmit");
2746
+ btn.disabled = true;
2747
+ await withUiBusy("沉淀中", async () => {
2748
+ try {
2749
+ const res = await api("/remember", {
2750
+ method: "POST",
2751
+ body: JSON.stringify({
2752
+ content,
2753
+ namespace: $("zmAddNs").value.trim() || undefined,
2754
+ verify: $("zmAddVerify").checked,
2755
+ }),
2756
+ });
2757
+ const r = res.result;
2758
+ showToast(
2759
+ r.deduplicated
2760
+ ? `已去重(${r.doc_id})`
2761
+ : `入库 ${r.saved} 条事实(纠错 ${r.revised},拦截 ${r.dropped})`
2762
+ );
2763
+ $("zmAddText").value = "";
2764
+ $("zmAddWrap").classList.remove("open");
2765
+ await Promise.all([refreshStatus(true), loadMemories()]);
2766
+ } catch (err) {
2767
+ showToast(`写入失败:${err.message}`, false);
2768
+ } finally {
2769
+ btn.disabled = false;
2770
+ }
2771
+ });
2772
+ });
2773
+
2774
+ bindTwoStepConfirm($("zmEvictExpiredBtn"), async () => {
2775
+ await withUiBusy("清理中", async () => {
2776
+ try {
2777
+ const res = await api("/evict", {
2778
+ method: "POST",
2779
+ body: JSON.stringify({ expired: true, reason: "expired-cleanup", force: true }),
2780
+ });
2781
+ showToast(
2782
+ res.result.total_records
2783
+ ? `已清理 ${res.result.total_records} 条过期记忆(${res.result.batch_id})`
2784
+ : "当前无已过期记忆"
2785
+ );
2786
+ await Promise.all([refreshStatus(true), loadRecycleBin()]);
2787
+ } catch (err) {
2788
+ showToast(`清理失败:${err.message}`, false);
2789
+ }
2790
+ });
2791
+ });
2792
+
2793
+ $("zmPickOllama").addEventListener("click", () => {
2794
+ state.providerDraft = "ollama";
2795
+ renderProviderFields();
2796
+ });
2797
+ $("zmPickApi").addEventListener("click", () => {
2798
+ state.providerDraft = "openai-compatible";
2799
+ renderProviderFields();
2800
+ });
2801
+
2802
+ $("zmClearKeyBtn").addEventListener("click", () => {
2803
+ state.clearApiKeyFlag = true;
2804
+ $("zmApiKey").value = "";
2805
+ $("zmApiKeyHint").textContent = "保存后清除";
2806
+ });
2807
+
2808
+ $("zmTestBtn").addEventListener("click", async () => {
2809
+ const btn = $("zmTestBtn");
2810
+ btn.disabled = true;
2811
+ await withUiBusy("检测中", async () => {
2812
+ try {
2813
+ const draft = readDraftConfig();
2814
+ const data = await api("/test-provider", {
2815
+ method: "POST",
2816
+ body: JSON.stringify({ ...draft, liveChat: draft.provider === "openai-compatible" }),
2817
+ });
2818
+ const p = data.probe || {};
2819
+ state.lastProviderStatus = p;
2820
+ renderBusyHalo();
2821
+ renderLocalSetupGuides();
2822
+ showToast(p.message || (p.ok ? "连接正常" : "连接失败"), Boolean(p.ok));
2823
+ if (p.available_models?.length && draft.provider === "ollama") {
2824
+ const dl = $("zmOllamaModelList");
2825
+ dl.innerHTML = "";
2826
+ for (const m of p.available_models) {
2827
+ const opt = document.createElement("option");
2828
+ opt.value = m;
2829
+ dl.appendChild(opt);
2830
+ }
2831
+ $("zmOllamaAvail").textContent = `已发现 ${p.available_models.length} 个`;
2832
+ }
2833
+ } catch (err) {
2834
+ showToast(`测试失败:${err.message}`, false);
2835
+ } finally {
2836
+ btn.disabled = false;
2837
+ }
2838
+ });
2839
+ });
2840
+
2841
+ $("zmSaveCfgBtn").addEventListener("click", async () => {
2842
+ const btn = $("zmSaveCfgBtn");
2843
+ btn.disabled = true;
2844
+ await withUiBusy("保存中", async () => {
2845
+ try {
2846
+ const data = await api("/config", {
2847
+ method: "POST",
2848
+ body: JSON.stringify(readDraftConfig()),
2849
+ });
2850
+ populateConfigForm(data.config, null);
2851
+ showToast("配置已保存并立即生效");
2852
+ await refreshStatus(false);
2853
+ } catch (err) {
2854
+ showToast(`保存失败:${err.message}`, false);
2855
+ } finally {
2856
+ btn.disabled = false;
2857
+ }
2858
+ });
2859
+ });
2860
+
2861
+ function mount() {
2862
+ if (!document.body) {
2863
+ document.addEventListener("DOMContentLoaded", mount, { once: true });
2864
+ return;
2865
+ }
2866
+ applyAppearance();
2867
+ renderNamespaceMenu();
2868
+ if (!document.getElementById("dsh-zeromem-root")) {
2869
+ document.body.appendChild(host);
2870
+ }
2871
+ connectEvents();
2872
+ // Run automatic detection immediately on mount so first-time users see one-click setup guidance
2873
+ refreshStatus(false);
2874
+ setInterval(() => {
2875
+ if (!state.open && !state.setupState?.active) refreshStatus(true);
2876
+ }, 30_000);
2877
+ }
2878
+
2879
+ mount();
2880
+ })();