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
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""HTML rendering for the Fraud Pattern Co-Pilot demo.

The unique panel for Fraud is a two-distribution scoreboard: 5 stage
bars + 4 type bars side-by-side, with stage and type recommendation
pills above. Visual vocabulary matches dispute and collections.
"""

from __future__ import annotations

import html
from typing import Iterable

import numpy as np

from encoder.src.data.synthetic_fraud_pattern import (
    NUM_STAGES,
    NUM_TYPES,
    STAGE_NAMES,
    TYPE_NAMES,
)
from encoder.src.demo.copilot_inference_fraud_pattern import (
    STAGE_COLORS,
    TYPE_COLORS,
    FraudPatternCastMember,
    FraudPatternResult,
)


_PAGE_BG = "#fafafa"
_INK = "#171717"
_INK_DIM = "#525252"
_BORDER = "rgba(0,0,0,0.08)"
_CARD_BG = "#ffffff"


def render_header() -> str:
    return f"""
    <div style="text-align: center; margin-bottom: 22px;">
        <div style="
            font-size: 11px;
            color: {_INK_DIM};
            font-family: 'JetBrains Mono', ui-monospace, monospace;
            text-transform: uppercase;
            letter-spacing: 0.08em;
            margin-bottom: 6px;
        ">
            Liquid AI &middot; LFM2.5-350M backbone &middot; encoder + LoRA
        </div>
        <h1 style="
            margin: 0;
            font-size: 28px;
            font-weight: 700;
            color: {_INK};
            letter-spacing: -0.02em;
        ">
            Fraud Pattern Co-Pilot
        </h1>
    </div>
    """


def render_cast_strip(
    cast: list[FraudPatternCastMember],
    selected_idx: int,
) -> str:
    cards: list[str] = []
    for i, m in enumerate(cast):
        is_sel = i == selected_idx
        accent = STAGE_COLORS[m.stage_label]
        border = accent if is_sel else _BORDER
        bg = "#ffffff" if not is_sel else _hex_with_alpha(accent, 0.06)
        cards.append(f"""
        <div style="
            flex: 1 1 0;
            min-width: 0;
            padding: 12px 14px;
            background: {bg};
            border: 1.5px solid {border};
            border-radius: 10px;
        ">
            <div style="
                font-size: 13px;
                font-weight: 600;
                color: {_INK};
                margin-bottom: 4px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            ">{html.escape(m.display_name)}</div>
            <div style="
                font-size: 11px;
                color: {_INK_DIM};
                font-family: 'JetBrains Mono', ui-monospace, monospace;
                text-transform: uppercase;
                letter-spacing: 0.04em;
            ">
                <span style="color: {accent};">●</span>
                {html.escape(m.stage_label_name)} / {html.escape(m.type_label_name)}
            </div>
        </div>
        """)
    return f"""
    <div style="display: flex; gap: 10px; margin-bottom: 18px;">
        {''.join(cards)}
    </div>
    """


def render_context(member: FraudPatternCastMember) -> str:
    accent = STAGE_COLORS[member.stage_label]
    return f"""
    <div style="
        background: {_CARD_BG};
        border: 1px solid {_BORDER};
        border-left: 4px solid {accent};
        border-radius: 12px;
        padding: 22px 26px;
        margin-bottom: 18px;
    ">
        <div style="
            font-size: 11px;
            color: {_INK_DIM};
            font-family: 'JetBrains Mono', ui-monospace, monospace;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 10px;
        ">
            Fraud queue context &middot; pattern:
            <span style="color: {_INK}; font-weight: 600;">{html.escape(member.pattern)}</span>
        </div>
        <div style="
            font-size: 18px;
            color: {_INK};
            line-height: 1.5;
            font-style: italic;
        ">
            &ldquo;{html.escape(member.context_text)}&rdquo;
        </div>
        <div style="
            font-size: 13px;
            color: {_INK_DIM};
            margin-top: 12px;
            line-height: 1.5;
        ">
            {html.escape(member.description)}
        </div>
    </div>
    """


def render_timeline(
    flagged_idx: int,
    top_k_positions: Iterable[int] | None = None,
    attribution_probs: Iterable[float] | None = None,
    num_positions: int = 64,
) -> str:
    top_k_set: set[int] = set()
    if top_k_positions is not None:
        top_k_set = {int(i) for i in top_k_positions}
    probs = list(attribution_probs) if attribution_probs is not None else None

    dots: list[str] = []
    for i in range(num_positions):
        if i == flagged_idx:
            dots.append(_dot_flagged(i))
        elif i in top_k_set:
            alpha = float(probs[i]) if probs is not None else 1.0
            dots.append(_dot_attributed(i, alpha))
        else:
            faint = float(probs[i]) * 0.4 if probs is not None else 0.0
            dots.append(_dot_neutral(i, faint))

    return f"""
    <div style="
        background: {_CARD_BG};
        border: 1px solid {_BORDER};
        border-radius: 12px;
        padding: 20px 22px;
        margin-bottom: 18px;
    ">
        <div style="
            font-size: 11px;
            color: {_INK_DIM};
            font-family: 'JetBrains Mono', ui-monospace, monospace;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 14px;
            display: flex;
            justify-content: space-between;
        ">
            <span>64-transaction history &middot; oldest → newest</span>
            <span>
                <span style="color: #ef4444;">★</span> flagged &nbsp;
                <span style="color: #f59e0b;">●</span> contributed
            </span>
        </div>
        <div style="
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 2px;
        ">
            {''.join(dots)}
        </div>
    </div>
    """


def _dot_flagged(i: int) -> str:
    return f"""
    <div title="position {i} &middot; flagged" style="
        width: 14px; height: 14px;
        color: #ef4444;
        font-size: 18px;
        line-height: 14px;
        text-align: center;
        font-weight: 700;
    ">★</div>
    """


def _dot_attributed(i: int, alpha: float) -> str:
    glow = max(0.4, min(1.0, alpha))
    return f"""
    <div title="position {i} &middot; contributed ({alpha:.2f})" style="
        width: 12px; height: 12px;
        border-radius: 50%;
        background: rgba(245, 158, 11, {glow});
        box-shadow: 0 0 8px rgba(245, 158, 11, {glow * 0.7});
    "></div>
    """


def _dot_neutral(i: int, faint: float = 0.0) -> str:
    bg = f"rgba(245, 158, 11, {faint:.2f})" if faint > 0.0 else "rgba(0,0,0,0.18)"
    return f"""
    <div title="position {i}" style="
        width: 8px; height: 8px;
        border-radius: 50%;
        background: {bg};
    "></div>
    """


def render_two_dist_scoreboard(result: FraudPatternResult | None) -> str:
    """Stage 5-bar + type 4-bar side-by-side scoreboard."""
    if result is None:
        return _scoreboard_placeholder()

    stage_rows = _bars_for(
        probs=result.stage_probs,
        names=STAGE_NAMES,
        colors=STAGE_COLORS,
        winning_idx=result.predicted_stage,
    )
    type_rows = _bars_for(
        probs=result.type_probs,
        names=TYPE_NAMES,
        colors=TYPE_COLORS,
        winning_idx=result.predicted_type,
    )

    stage_color = STAGE_COLORS[result.predicted_stage]
    type_color = TYPE_COLORS[result.predicted_type]

    return f"""
    <div style="
        background: {_CARD_BG};
        border: 1px solid {_BORDER};
        border-radius: 12px;
        padding: 22px 26px;
        height: 100%;
        box-sizing: border-box;
    ">
        <div style="
            display: flex;
            justify-content: space-between;
            align-items: baseline;
            margin-bottom: 16px;
        ">
            <div style="
                font-size: 11px;
                color: {_INK_DIM};
                font-family: 'JetBrains Mono', ui-monospace, monospace;
                text-transform: uppercase;
                letter-spacing: 0.05em;
            ">
                Pattern verdict
            </div>
            <div style="display: flex; gap: 8px;">
                <div style="
                    display: inline-block;
                    padding: 4px 12px;
                    background: {_hex_with_alpha(stage_color, 0.12)};
                    color: {stage_color};
                    font-size: 12px;
                    font-weight: 700;
                    border-radius: 999px;
                    text-transform: uppercase;
                    letter-spacing: 0.04em;
                ">
                    stage: {html.escape(STAGE_NAMES[result.predicted_stage])}
                    &middot; {result.stage_probs[result.predicted_stage]:.2f}
                </div>
                <div style="
                    display: inline-block;
                    padding: 4px 12px;
                    background: {_hex_with_alpha(type_color, 0.12)};
                    color: {type_color};
                    font-size: 12px;
                    font-weight: 700;
                    border-radius: 999px;
                    text-transform: uppercase;
                    letter-spacing: 0.04em;
                ">
                    type: {html.escape(TYPE_NAMES[result.predicted_type])}
                    &middot; {result.type_probs[result.predicted_type]:.2f}
                </div>
            </div>
        </div>

        <div style="display: flex; gap: 20px;">
            <div style="flex: 1;">
                <div style="
                    font-size: 10px;
                    color: {_INK_DIM};
                    font-family: 'JetBrains Mono', ui-monospace, monospace;
                    text-transform: uppercase;
                    margin-bottom: 8px;
                    letter-spacing: 0.04em;
                ">Stage distribution</div>
                {''.join(stage_rows)}
            </div>
            <div style="flex: 1;">
                <div style="
                    font-size: 10px;
                    color: {_INK_DIM};
                    font-family: 'JetBrains Mono', ui-monospace, monospace;
                    text-transform: uppercase;
                    margin-bottom: 8px;
                    letter-spacing: 0.04em;
                ">Type distribution</div>
                {''.join(type_rows)}
            </div>
        </div>
    </div>
    """


def _bars_for(
    probs: np.ndarray,
    names: list[str],
    colors: dict[int, str],
    winning_idx: int,
) -> list[str]:
    rows: list[str] = []
    for i, (name, p) in enumerate(zip(names, probs, strict=True)):
        accent = colors[i]
        is_win = i == winning_idx
        bar_w = max(2, int(float(p) * 100))
        rows.append(f"""
        <div style="
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 5px 0;
            border-bottom: 1px solid {_BORDER};
        ">
            <div style="
                width: 95px;
                font-size: 11px;
                font-weight: {600 if is_win else 500};
                color: {_INK if is_win else _INK_DIM};
                font-family: 'JetBrains Mono', ui-monospace, monospace;
            ">
                {'★ ' if is_win else '&nbsp;&nbsp; '}{html.escape(name)}
            </div>
            <div style="
                flex: 1;
                height: 10px;
                background: rgba(0,0,0,0.05);
                border-radius: 5px;
                position: relative;
                overflow: hidden;
            ">
                <div style="
                    position: absolute;
                    left: 0; top: 0; bottom: 0;
                    width: {bar_w}%;
                    background: {accent};
                    border-radius: 5px;
                "></div>
            </div>
            <div style="
                width: 36px;
                text-align: right;
                font-size: 11px;
                font-weight: 600;
                color: {accent};
                font-family: 'JetBrains Mono', ui-monospace, monospace;
            ">
                {float(p):.2f}
            </div>
        </div>
        """)
    return rows


def _scoreboard_placeholder() -> str:
    return f"""
    <div style="
        background: {_CARD_BG};
        border: 1px solid {_BORDER};
        border-radius: 12px;
        padding: 22px 26px;
        height: 100%;
        box-sizing: border-box;
        color: {_INK_DIM};
    ">
        <div style="
            font-size: 11px;
            font-family: 'JetBrains Mono', ui-monospace, monospace;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 16px;
        ">
            Pattern verdict
        </div>
        <div style="font-size: 14px; color: {_INK_DIM};">
            Click <b>Analyze</b> to see pattern stage + type distributions.
        </div>
    </div>
    """


def render_reasoning(text: str | None) -> str:
    if text is None:
        body = f"""
        <div style="font-size: 14px; color: {_INK_DIM}; font-style: italic;">
            Reasoning will appear here once the model has read the customer's history.
        </div>
        """
    else:
        body = f"""
        <div style="
            font-size: 14px;
            color: {_INK};
            line-height: 1.6;
            white-space: pre-wrap;
            font-family: ui-monospace, 'JetBrains Mono', monospace;
        ">{html.escape(text)}<span style="
            display: inline-block;
            width: 7px;
            height: 14px;
            background: {_INK};
            margin-left: 2px;
            vertical-align: text-bottom;
            animation: copilot-blink 1s infinite;
        "></span></div>
        <style>
            @keyframes copilot-blink {{
                0%, 49% {{ opacity: 1; }}
                50%, 100% {{ opacity: 0; }}
            }}
        </style>
        """
    return f"""
    <div style="
        background: {_CARD_BG};
        border: 1px solid {_BORDER};
        border-radius: 12px;
        padding: 22px 26px;
        height: 100%;
        box-sizing: border-box;
    ">
        <div style="
            font-size: 11px;
            color: {_INK_DIM};
            font-family: 'JetBrains Mono', ui-monospace, monospace;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 16px;
        ">
            Reasoning
        </div>
        {body}
    </div>
    """


def _hex_with_alpha(hex_color: str, alpha: float) -> str:
    h = hex_color.lstrip("#")
    if len(h) != 6:
        return hex_color
    r, g, b = int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16)
    return f"rgba({r}, {g}, {b}, {alpha:.3f})"