File size: 13,847 Bytes
8a169a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f5369e
 
 
 
 
8a169a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f5369e
 
 
 
 
 
 
 
 
 
 
8a169a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f5369e
 
 
 
 
 
8a169a0
 
 
5f5369e
 
 
 
 
 
 
 
 
 
 
 
8a169a0
 
1
2
3
4
5
6
7
8
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
"""Dark engineering control-room theme for Murphy Unified dashboard.

Provides CSS, JS, colour constants, Plotly template, and helper functions
shared by every tab.
"""

from __future__ import annotations

import plotly.graph_objects as go
import plotly.io as pio

# ── Colour palette ──────────────────────────────────────────────────────────
BG = "#1a1e24"
SURFACE = "#21262e"
SURFACE2 = "#272d37"
BORDER = "#333c48"
BORDER_BRIGHT = "#445060"
ACCENT = "#4a9eca"
ACCENT2 = "#4fc98a"
WARN = "#d4922a"
DANGER = "#c94a4a"
TEXT = "#b8c8d8"
TEXT_DIM = "#6a7e92"
TEXT_BRIGHT = "#d8e8f0"

TRACE_COLORS: list[str] = [ACCENT, ACCENT2, WARN, DANGER, "#7a8fca", "#ca7a9e"]

# ── Named semantic colors for tab-level use (avoid hardcoded hex in tabs) ──
ENERGY_POS = ACCENT2       # positive energy terms in bar charts
ENERGY_NEG = DANGER        # negative energy terms
VALVE_ICV = ACCENT         # ICV traces
VALVE_DCV = DANGER         # DCV traces

# ── Google Fonts ────────────────────────────────────────────────────────────
FONTS_HTML = (
    '<link rel="stylesheet" href="https://fonts.googleapis.com/css2?'
    "family=JetBrains+Mono:wght@400;500;600&"
    'family=DM+Sans:wght@400;500;600;700&display=swap">'
)

# ── CSS ─────────────────────────────────────────────────────────────────────
CSS = """
/* ── Murphy Unified β€” dark engineering theme ─────────────────────────── */
:root {
    --bg: #1a1e24; --surface: #21262e; --surface2: #272d37;
    --border: #333c48; --border-bright: #445060;
    --accent: #4a9eca; --accent2: #4fc98a; --warn: #d4922a; --danger: #c94a4a;
    --text: #b8c8d8; --text-dim: #6a7e92; --text-bright: #d8e8f0;
    --mono: 'JetBrains Mono', monospace;
    --body: 'DM Sans', sans-serif;
}

/* ── Plotly modebar β€” always visible on hover (issue #13) ──────────── */
.js-plotly-plot .modebar { opacity: 0.7 !important; }
.js-plotly-plot:hover .modebar { opacity: 1 !important; }
.js-plotly-plot .modebar-btn { font-size: 16px; }

/* ── body / dark-mode override ──────────────────────────────────────── */
body, .dark {
    background: var(--bg) !important;
    color: var(--text) !important;
    font-family: var(--body) !important;
}
.gradio-container {
    background: var(--bg) !important;
    max-width: 100% !important;
}
.block, .gr-block, .gr-box, .gr-panel {
    background: var(--surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: 4px !important;
}
input, textarea, select, .gr-input, .gr-text-input {
    background: var(--surface2) !important;
    color: var(--text) !important;
    border: 1px solid var(--border) !important;
    border-radius: 4px !important;
    font-family: var(--mono) !important;
}
button, .gr-button {
    background: var(--surface2) !important;
    color: var(--text) !important;
    border: 1px solid var(--border) !important;
    border-radius: 4px !important;
    font-family: var(--body) !important;
}
button.primary, .gr-button.primary {
    background: var(--accent) !important;
    color: var(--bg) !important;
    border-color: var(--accent) !important;
}

/* ── Tab navigation ─────────────────────────────────────────────────── */
.tabs .tab-nav button, .tab-nav button {
    font-family: var(--mono) !important;
    font-size: 10px !important;
    text-transform: uppercase !important;
    letter-spacing: 2px !important;
    color: var(--text-dim) !important;
    background: var(--surface) !important;
    border: 1px solid var(--border) !important;
    border-radius: 4px !important;
    padding: 8px 16px !important;
}
.tabs .tab-nav button.selected, .tab-nav button.selected {
    background: var(--surface2) !important;
    color: var(--accent) !important;
    border-color: var(--border-bright) !important;
    border-bottom: 2px solid var(--accent) !important;
}

/* ── Headings ───────────────────────────────────────────────────────── */
h1, h2, h3, h4, h5, h6 {
    font-family: var(--body) !important;
    font-weight: 700 !important;
    text-transform: uppercase !important;
    letter-spacing: 2px !important;
    color: var(--text-dim) !important;
}

/* ── Tables ──────────────────────────────────────────────────────────── */
table {
    background: var(--surface) !important;
    border-collapse: collapse !important;
}
thead, th {
    background: var(--surface2) !important;
    font-family: var(--body) !important;
    font-weight: 600 !important;
    text-transform: uppercase !important;
    color: var(--text-dim) !important;
    border: 1px solid var(--border) !important;
}
td {
    background: var(--surface) !important;
    color: var(--text) !important;
    border: 1px solid var(--border) !important;
    font-family: var(--mono) !important;
    font-size: 12px !important;
}

/* ── Accordion ──────────────────────────────────────────────────────── */
.gr-accordion .label-wrap, .accordion .label-wrap {
    font-family: var(--body) !important;
    font-weight: 600 !important;
    text-transform: uppercase !important;
    letter-spacing: 2px !important;
    color: var(--text-dim) !important;
}

/* ── Sliders ────────────────────────────────────────────────────────── */
.gr-slider label, label span {
    font-family: var(--body) !important;
    color: var(--text-dim) !important;
}

/* ── Dropdown / listbox ─────────────────────────────────────────────── */
.gr-dropdown, .gr-listbox, ul.options {
    background: var(--surface2) !important;
    color: var(--text) !important;
    border: 1px solid var(--border) !important;
    border-radius: 4px !important;
}
.gr-dropdown li:hover, .gr-listbox li:hover, ul.options li:hover {
    background: var(--surface) !important;
}

/* ── Footer hidden ──────────────────────────────────────────────────── */
footer { display: none !important; }

/* ── Metric cards (LEFT accent bar) ─────────────────────────────────── */
.metric-row {
    display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0;
}
.metric-card {
    flex: 1; min-width: 140px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: 4px;
    padding: 8px 12px;
}
.metric-card.green  { border-left-color: var(--accent2); }
.metric-card.amber  { border-left-color: var(--warn); }
.metric-card.red    { border-left-color: var(--danger); }
.metric-val {
    font-family: var(--mono);
    font-size: 18px;
    color: var(--text-bright);
    font-variant-numeric: tabular-nums;
}
.metric-label {
    font-family: var(--body);
    font-size: 10px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 2px;
    font-weight: 600;
}

/* ── Status bar ─────────────────────────────────────────────────────── */
.status-bar {
    font-family: var(--mono);
    font-size: 10px;
    color: var(--text-dim);
    border-top: 1px solid var(--border);
    margin-top: 8px;
    padding-top: 6px;
}

/* ── Checkboxes / radio ─────────────────────────────────────────────── */
input[type="checkbox"], input[type="radio"] {
    accent-color: var(--accent) !important;
    width: 16px !important;
    height: 16px !important;
    appearance: auto !important;
    -webkit-appearance: auto !important;
    opacity: 1 !important;
    position: relative !important;
    pointer-events: auto !important;
}
input[type="checkbox"]:checked, input[type="radio"]:checked {
    background: var(--accent) !important;
    border-color: var(--accent) !important;
}
/* Force native rendering over Gradio's custom styling */
.checkbox-item input, label input[type="checkbox"], label input[type="radio"] {
    appearance: auto !important;
    -webkit-appearance: auto !important;
    opacity: 1 !important;
}
/* Selected item label highlight */
.checkbox-item.selected, label:has(input:checked) {
    border-color: var(--accent) !important;
    background: rgba(74, 158, 202, 0.15) !important;
    color: var(--text-bright) !important;
}

/* ── Status pills ───────────────────────────────────────────────────── */
.pill {
    display: inline-flex;
    font-family: var(--mono);
    font-size: 10px;
    border-radius: 20px;
    padding: 2px 10px;
    align-items: center;
}
.pill.nominal {
    background: rgba(79, 201, 138, 0.15);
    color: var(--accent2);
}
.pill.warn {
    background: rgba(212, 146, 42, 0.15);
    color: var(--warn);
}
.pill.danger {
    background: rgba(201, 74, 74, 0.15);
    color: var(--danger);
}
"""

# ── JS β€” force dark mode ───────────────────────────────────────────────────
FORCE_DARK = """
function FORCE_DARK() {
    document.body.classList.add('dark');
    document.documentElement.style.colorScheme = 'dark';

    /* Issue #13: ensure Plotly modebar (download/copy buttons) is visible */
    const observer = new MutationObserver(() => {
        document.querySelectorAll('.js-plotly-plot').forEach(plot => {
            if (plot._fullLayout && !plot._modebarPatched) {
                Plotly.relayout(plot, {'modebar.orientation': 'v'});
                plot._modebarPatched = true;
            }
        });
    });
    observer.observe(document.body, {childList: true, subtree: true});
}
"""

# ── Metric card helper ──────────────────────────────────────────────────────

def metric_html(label: str, value: str, unit: str = "", accent: str = "") -> str:
    """Render a single metric card div.

    Parameters
    ----------
    label : str
        Short uppercase label displayed beneath the value.
    value : str
        Primary numeric / text value.
    unit : str, optional
        Unit suffix appended after *value*.
    accent : str, optional
        Extra CSS class for the card, e.g. ``"green"``, ``"amber"``, ``"red"``.

    Returns
    -------
    str
        HTML string for one ``.metric-card``.
    """
    cls = f"metric-card {accent}" if accent else "metric-card"
    unit_span = f" <span style='font-size:12px;color:{TEXT_DIM}'>{unit}</span>" if unit else ""
    return (
        f'<div class="{cls}">'
        f'  <div class="metric-val">{value}{unit_span}</div>'
        f'  <div class="metric-label">{label}</div>'
        f"</div>"
    )


# ── Font family strings (for inline styles & Plotly) ───────────────────────

FONT_MONO = "JetBrains Mono, monospace"
FONT_BODY = "DM Sans, sans-serif"


def engine_banner(label: str, color: str = ACCENT) -> str:
    """Render a small engine-label banner for the left panel."""
    return (
        f'<div style="font-family:{FONT_MONO};font-size:11px;'
        f'color:{color};letter-spacing:3px;text-transform:uppercase;'
        f'padding:6px 0;border-bottom:1px solid {BORDER};margin-bottom:8px;">'
        f'{label}</div>'
    )


# ── Plotly template ─────────────────────────────────────────────────────────

_axis = dict(
    gridcolor="rgba(40,55,75,0.9)",
    linecolor=BORDER_BRIGHT,
    zeroline=False,
    tickfont=dict(family=FONT_MONO, size=10, color=TEXT_DIM),
)

_template = go.layout.Template(
    layout=go.Layout(
        paper_bgcolor=SURFACE,
        plot_bgcolor=SURFACE,
        font=dict(family=FONT_BODY, color=TEXT),
        title=dict(font=dict(family=FONT_BODY, color=TEXT_DIM)),
        xaxis=_axis,
        yaxis=_axis,
        colorway=TRACE_COLORS,
        margin=dict(l=56, r=16, t=44, b=44),
        modebar=dict(
            bgcolor="rgba(0,0,0,0)",
            color=TEXT_DIM,
            activecolor=ACCENT,
            orientation="v",
        ),
    )
)

# Plotly config applied to all figures β€” ensures modebar with download/copy
# button is visible (issue #13).
PLOTLY_CONFIG = {
    "displayModeBar": True,
    "modeBarButtonsToAdd": ["toImage"],
    "toImageButtonOptions": {
        "format": "png",
        "filename": "murphy_plot",
        "scale": 2,
    },
}

pio.templates["murphy"] = _template
pio.templates.default = "murphy"