File size: 21,390 Bytes
a6857c7
 
 
e86bafa
d54a751
e83084c
349f22c
a6857c7
 
 
 
9d96b4e
 
e86bafa
a6857c7
 
89fd26d
 
033540e
a6857c7
 
1d401c4
d4ce746
02f3743
 
 
 
 
 
 
 
d4ce746
 
 
 
 
 
1d401c4
 
d4ce746
 
1d401c4
 
 
 
 
 
 
 
a6857c7
8a16b17
1d401c4
 
 
 
 
 
 
 
 
 
 
 
e86bafa
 
1d401c4
 
 
 
 
 
e86bafa
1d401c4
 
 
 
 
d4ce746
e86bafa
 
 
 
 
 
9d96b4e
 
e86bafa
 
e83084c
89fd26d
 
033540e
e86bafa
e83084c
 
a6857c7
8a16b17
a64e3d2
a6857c7
 
d4ce746
 
a6857c7
89fd26d
02f3743
 
 
 
 
 
1d401c4
 
 
695443f
1d401c4
 
 
 
9d96b4e
695443f
 
 
 
 
9d96b4e
 
 
 
 
 
 
 
1d401c4
 
 
9d96b4e
1d401c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6857c7
02f3743
1d401c4
8a16b17
a6857c7
e86bafa
8a16b17
e86bafa
 
02f3743
e86bafa
 
 
 
 
 
 
a6857c7
 
e86bafa
a6857c7
02f3743
e86bafa
 
 
 
 
 
 
 
 
 
 
02f3743
e86bafa
 
 
 
 
 
 
 
 
 
 
02f3743
e86bafa
 
 
 
 
 
 
 
 
 
 
8a16b17
e86bafa
 
 
 
d4ce746
 
 
 
89fd26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4ce746
1d401c4
d4ce746
 
 
 
 
 
 
 
1d401c4
d4ce746
 
 
1d401c4
d4ce746
 
 
e86bafa
a6857c7
9d96b4e
a6857c7
9d96b4e
a6857c7
89fd26d
a6857c7
 
 
8a16b17
 
a64e3d2
 
 
8a16b17
a64e3d2
e86bafa
8a16b17
 
 
9d96b4e
 
 
 
 
 
8a16b17
e83084c
9d96b4e
8a16b17
 
 
1dc60a8
 
 
 
 
 
e86bafa
1d401c4
 
d4ce746
a64e3d2
 
 
1d401c4
 
a64e3d2
1d401c4
02f3743
1d401c4
d4ce746
1dc60a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4ce746
1dc60a8
d4ce746
1dc60a8
a64e3d2
a6857c7
8a16b17
 
1dc60a8
a64e3d2
1dc60a8
8a16b17
e83084c
9d96b4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6857c7
695443f
d4ce746
695443f
1d401c4
 
9d96b4e
1d401c4
9d96b4e
d4ce746
 
a6857c7
e86bafa
02f3743
e86bafa
02f3743
a6857c7
033540e
9d96b4e
 
033540e
9d96b4e
033540e
d4ce746
a6857c7
1d401c4
e86bafa
a6857c7
 
e83084c
d54a751
 
 
d4ce746
033540e
 
 
 
 
 
 
d4ce746
 
 
 
 
 
 
 
 
 
 
 
 
 
e83084c
d4ce746
 
 
 
 
 
 
 
e83084c
d4ce746
89fd26d
 
 
 
 
 
e83084c
89fd26d
 
 
 
 
 
 
e83084c
89fd26d
 
 
 
d4ce746
 
a6857c7
d4ce746
 
a6857c7
d4ce746
e83084c
a6857c7
1d401c4
 
 
 
 
 
a6857c7
e83084c
a6857c7
d4ce746
e83084c
d4ce746
 
 
 
 
 
 
 
e83084c
d4ce746
 
 
 
 
 
 
 
 
 
e83084c
d4ce746
695443f
d4ce746
 
 
 
 
 
 
e83084c
d4ce746
 
 
 
 
 
 
 
 
e83084c
d4ce746
 
 
d54a751
e86bafa
d4ce746
e86bafa
d54a751
 
02f3743
 
 
 
 
 
 
 
 
 
e83084c
02f3743
 
 
 
 
 
d54a751
e86bafa
 
89fd26d
e83084c
89fd26d
 
 
 
 
 
d4ce746
576df60
 
e83084c
576df60
 
 
 
5e98b5a
576df60
 
 
a6857c7
 
 
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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
'use client';

import { useCallback, useEffect, useRef, useState } from 'react';
import type { DockingInteraction } from '@/lib/api';
import { HudPanel, HudLegend } from '@/components/ui';
import { useTheme } from '@/contexts/theme';
import { ArrowCounterClockwise as RotateCcw, ArrowClockwise as RotateCw, Camera, FlipHorizontal, Palette, Cube as Box, Stack as Layers, Minus, Circle, Hexagon } from '@phosphor-icons/react';

interface DockingViewerProps {
  pdbId: string;
  ligandPdb: string;
  pdbUrl?: string;
  pdbUrlFormat?: 'pdb' | 'cif';
  interactions?: DockingInteraction;
  height?: number | string;
  backgroundColor?: string;
  chains?: Array<{ id: string; residue_count: number }>;
  ligands?: Array<{ id: string; chain: string; residue_count: number }>;
  highlightRange?: { start: number; end: number };
}

type RepresentationType = 'cartoon' | 'ball-and-stick' | 'spacefill' | 'gaussian-surface' | 'molecular-surface' | 'putty' | 'ribbon';
type ColorScheme = 'spectrum' | 'chain' | 'secondary-structure' | 'residue-type' | 'bfactor' | 'uniform';
type InteractionLayer = 'hbonds' | 'hydrophobic' | 'pi_stacking' | 'salt_bridges';

const INTERACTION_LAYERS: { key: InteractionLayer; label: string; color: string }[] = [
  { key: 'hbonds', label: 'H-bonds', color: '#4488ff' },
  { key: 'hydrophobic', label: 'Hydrophobic', color: '#ff8844' },
  { key: 'pi_stacking', label: 'Pi-stacking', color: '#ff44ff' },
  { key: 'salt_bridges', label: 'Salt bridges', color: '#00cccc' },
];

const REP_OPTIONS: { value: RepresentationType; label: string; icon: typeof Box }[] = [
  { value: 'cartoon', label: 'Cartoon', icon: Layers },
  { value: 'ribbon', label: 'Ribbon', icon: Minus },
  { value: 'ball-and-stick', label: 'Ball+Stick', icon: Box },
  { value: 'spacefill', label: 'Spacefill', icon: Circle },
  { value: 'gaussian-surface', label: 'Surface', icon: Hexagon },
  { value: 'putty', label: 'Putty', icon: Minus },
];

const COLOR_MAP: Record<ColorScheme, Record<string, unknown>> = {
  'spectrum': { name: 'spectrum' },
  'chain': { name: 'chain-id' },
  'secondary-structure': { name: 'secondary-structure' },
  'residue-type': { name: 'residue-type' },
  'bfactor': { name: 'bfactor' },
  'uniform': { name: 'uniform', value: 0x66ccff },
};

interface PDBeElement extends HTMLElement {
  viewerInstance?: {
    plugin?: {
      canvas3d?: {
        requestDraw: () => void;
        camera: {
          spin: (on: boolean | string, speed?: number) => void;
          reset: () => void;
        };
      };
      primitives?: {
        clear: () => void;
        add: (primitives: unknown[]) => void;
      };
    };
    visual: {
      update: (options: Record<string, unknown>, fullLoad?: boolean) => Promise<boolean>;
      select: (params: Record<string, unknown>) => Promise<void>;
      reset: (params: Record<string, unknown>) => Promise<void>;
      toggleSpin: (spin?: boolean) => Promise<void>;
      focus: (selection: unknown[]) => Promise<void>;
    };
    clear: () => Promise<void>;
    load: (params: Record<string, unknown>, fullLoad?: boolean) => Promise<void>;
    deleteStructure: (id?: string | number) => Promise<void>;
    events: {
      loadComplete: { subscribe: (cb: (success: boolean) => void) => void };
    };
  };
}

export function DockingViewer({
  pdbId,
  ligandPdb,
  pdbUrl,
  pdbUrlFormat,
  interactions,
  height = 480,
  backgroundColor,
  chains = [],
  ligands = [],
  highlightRange,
}: DockingViewerProps) {
  const { theme } = useTheme();
  const sceneBg = backgroundColor ?? (theme === 'light' ? '#FFFFFF' : '#0B0C14');
  const containerRef = useRef<HTMLDivElement>(null);
  const viewerRef = useRef<PDBeElement | null>(null);
  const initRef = useRef(false);
  const [status, setStatus] = useState<'loading' | 'ready' | 'error'>('loading');
  const [error, setError] = useState<string | null>(null);
  const [representation, setRepresentation] = useState<RepresentationType>('cartoon');
  const [colorScheme, setColorScheme] = useState<ColorScheme>('spectrum');
  const [spinning, setSpinning] = useState(false);
  const [selectedChain, setSelectedChain] = useState('');
  const [visibleInteractions, setVisibleInteractions] = useState<Record<InteractionLayer, boolean>>({
    hbonds: true,
    hydrophobic: true,
    pi_stacking: true,
    salt_bridges: true,
  });
  const representationRef = useRef<RepresentationType>('cartoon');
  const colorSchemeRef = useRef<ColorScheme>('spectrum');

  const applyVisualization = useCallback(async (rep: RepresentationType, color: ColorScheme) => {
    const viewer = viewerRef.current?.viewerInstance;
    if (!viewer) return;

    try {
      const options: Record<string, unknown> = {
        visualStyle: {
          polymer: { type: rep, color: { name: COLOR_MAP[color].name } },
          het: 'ball-and-stick',
          water: 'spacefill',
        },
      };
      if (pdbUrl) {
        options.customDataUrl = pdbUrl;
        options.customDataFormat = pdbUrlFormat || 'pdb';
      } else {
        options.moleculeId = pdbId.toLowerCase();
      }
      await viewer.visual.update(options, true);
    } catch {
      // Best-effort
    }
  }, [pdbId, pdbUrl, pdbUrlFormat]);

  const loadLigand = useCallback(async () => {
    const viewer = viewerRef.current?.viewerInstance;
    if (!viewer || !ligandPdb) return;

    try {
      const blob = new Blob([ligandPdb], { type: 'text/plain' });
      const url = URL.createObjectURL(blob);

      await viewer.load({
        url,
        format: 'pdb',
        id: 'ligand',
      }, false);

      URL.revokeObjectURL(url);
    } catch {
      // Best-effort
    }
  }, [ligandPdb]);

  const drawInteractions = useCallback((inter: DockingInteraction, visible: Record<InteractionLayer, boolean>) => {
    const plugin = viewerRef.current?.viewerInstance?.plugin;
    if (!plugin?.primitives) return;

    try {
      plugin.primitives.clear();
      const primitives: unknown[] = [];

      for (const hb of visible.hbonds ? inter.hbonds || [] : []) {
        if (hb.ligand_coords && hb.protein_coords) {
          primitives.push({
            kind: 'cylinder',
            start: { x: hb.ligand_coords[0], y: hb.ligand_coords[1], z: hb.ligand_coords[2] },
            end: { x: hb.protein_coords[0], y: hb.protein_coords[1], z: hb.protein_coords[2] },
            color: 0x4488ff,
            radius: 0.15,
          });
        }
      }

      for (const hp of visible.hydrophobic ? inter.hydrophobic || [] : []) {
        if (hp.ligand_coords && hp.protein_coords) {
          primitives.push({
            kind: 'cylinder',
            start: { x: hp.ligand_coords[0], y: hp.ligand_coords[1], z: hp.ligand_coords[2] },
            end: { x: hp.protein_coords[0], y: hp.protein_coords[1], z: hp.protein_coords[2] },
            color: 0xff8844,
            radius: 0.12,
          });
        }
      }

      for (const ps of visible.pi_stacking ? inter.pi_stacking || [] : []) {
        if (ps.ring_centroid && ps.ligand_centroid) {
          primitives.push({
            kind: 'cylinder',
            start: { x: ps.ring_centroid[0], y: ps.ring_centroid[1], z: ps.ring_centroid[2] },
            end: { x: ps.ligand_centroid[0], y: ps.ligand_centroid[1], z: ps.ligand_centroid[2] },
            color: 0xff44ff,
            radius: 0.18,
          });
        }
      }

      for (const sb of visible.salt_bridges ? inter.salt_bridges || [] : []) {
        if (sb.ligand_coords && sb.protein_coords) {
          primitives.push({
            kind: 'cylinder',
            start: { x: sb.ligand_coords[0], y: sb.ligand_coords[1], z: sb.ligand_coords[2] },
            end: { x: sb.protein_coords[0], y: sb.protein_coords[1], z: sb.protein_coords[2] },
            color: 0x00cccc,
            radius: 0.2,
          });
        }
      }

      if (primitives.length > 0) {
        plugin.primitives.add(primitives);
      }
      plugin.canvas3d?.requestDraw();
    } catch {
      // Best-effort
    }
  }, []);

  const selectChain = useCallback(async (chain: string) => {
    const viewer = viewerRef.current?.viewerInstance;
    if (!viewer) return;
    try {
      if (!chain) {
        await viewer.visual.reset({ camera: false, theme: false });
        return;
      }
      await viewer.visual.select({
        data: [{ auth_asym_id: chain, color: '#5eead4' }],
        nonSelectedColor: '#334155',
        keepRepresentations: true,
      });
      await viewer.visual.focus([{ auth_asym_id: chain }]);
    } catch {
      // The PDBe API can differ between deposited structures; preserve the canvas if a selection is unsupported.
    }
  }, []);

  const focusContacts = useCallback(async () => {
    const viewer = viewerRef.current?.viewerInstance;
    if (!viewer || !interactions) return;
    const residues = [
      ...interactions.hbonds,
      ...interactions.hydrophobic,
      ...interactions.pi_stacking,
      ...interactions.salt_bridges,
    ].map(contact => ({ auth_asym_id: contact.protein_chain, auth_seq_id: contact.protein_residue_seq }));
    if (!residues.length) return;
    try {
      await viewer.visual.select({ data: residues, keepRepresentations: true });
      await viewer.visual.focus(residues);
    } catch {
      // Best-effort viewer enhancement; the tabular fingerprint remains available.
    }
  }, [interactions]);

  const handleScreenshot = useCallback(() => {
    const canvas = viewerRef.current?.querySelector('canvas');
    if (!canvas) return;
    const link = document.createElement('a');
    link.download = `docking-${pdbId}-${Date.now()}.png`;
    link.href = canvas.toDataURL('image/png');
    link.click();
  }, [pdbId]);

  const handleResetCamera = useCallback(() => {
    viewerRef.current?.viewerInstance?.visual?.reset({ camera: true });
  }, []);

  const handleFlipView = useCallback(() => {
    const cam = viewerRef.current?.viewerInstance?.plugin?.canvas3d?.camera;
    if (!cam) return;
    cam.spin('x');
    setTimeout(() => cam.spin(false), 1500);
  }, []);

  // Initialize viewer ONCE per pdbId / pdbUrl
  useEffect(() => {
    if (!pdbId && !pdbUrl) {
      setStatus('error');
      setError('Missing structure identifier');
      return;
    }

    const container = containerRef.current;
    if (!container) return;
    if (initRef.current) return;
    initRef.current = true;

    let cancelled = false;
    let checkInterval: ReturnType<typeof setInterval> | null = null;

    container.innerHTML = '';

    const el = document.createElement('pdbe-molstar') as PDBeElement;
    if (pdbUrl) {
      el.setAttribute('custom-data-url', pdbUrl);
      el.setAttribute('custom-data-format', pdbUrlFormat || 'pdb');
    } else {
      el.setAttribute('molecule-id', pdbId.toLowerCase());
    }
    el.setAttribute('hide-controls', '');
    el.setAttribute('background-color', sceneBg);
    el.id = `pdbe-docking-${(pdbId || pdbUrl || 'structure').toLowerCase().replace(/[^a-z0-9]/gi, '')}`;
    container.appendChild(el);
    viewerRef.current = el;

    let cancelled_after_event = false;

    const onModelsLoaded = async () => {
      if (cancelled || cancelled_after_event) return;
      cancelled_after_event = true;
      if (checkInterval) clearInterval(checkInterval);

      const viewer = el.viewerInstance;
      if (!viewer) {
        if (!cancelled) setStatus('ready');
        return;
      }

      // Load ligand as separate structure
      await loadLigand();

      // Draw interaction lines
      if (interactions) drawInteractions(interactions, visibleInteractions);

      if (!cancelled) setStatus('ready');
    };

    el.addEventListener('molstar-models-loaded', onModelsLoaded);

    let attempts = 0;
    checkInterval = setInterval(() => {
      if (cancelled || cancelled_after_event) {
        if (checkInterval) clearInterval(checkInterval);
        return;
      }
      const hasCanvas = el.querySelector('canvas');
      if (hasCanvas) {
        onModelsLoaded();
        return;
      }
      attempts++;
      if (attempts > 100) {
        if (checkInterval) clearInterval(checkInterval);
        if (!cancelled) setStatus('ready');
      }
    }, 300);

    return () => {
      cancelled = true;
      cancelled_after_event = true;
      if (checkInterval) clearInterval(checkInterval);
      el.removeEventListener('molstar-models-loaded', onModelsLoaded);
    };
  }, [pdbId, pdbUrl, ligandPdb, sceneBg]); // eslint-disable-line react-hooks/exhaustive-deps

  // Highlight a residue range (deep-linked from motif scan on the structure page).
  // Uses PDBe Molstar's visual.select range format so the residues actually get
  // colored and focused on the canvas.
  const applyHighlight = useCallback(async () => {
    const viewer = viewerRef.current?.viewerInstance;
    if (!viewer || !highlightRange) return;
    const { start, end } = highlightRange;
    if (!Number.isFinite(start) || !Number.isFinite(end) || end < start) return;
    try {
      await viewer.visual.select({
        data: [{ start_residue_number: start, end_residue_number: end, color: '#fbbf24', focus: true }],
        nonSelectedColor: '#1e2436',
        keepRepresentations: true,
      });
    } catch {
      // Best-effort viewer enhancement; the tabular residue data remains available.
    }
  }, [highlightRange]);

  // Representation + color changes
  useEffect(() => {
    if (status === 'ready' && (representation !== representationRef.current || colorScheme !== colorSchemeRef.current)) {
      representationRef.current = representation;
      colorSchemeRef.current = colorScheme;
      void applyVisualization(representation, colorScheme).then(() => applyHighlight());
    }
  }, [representation, colorScheme, status, applyVisualization, applyHighlight]);

  // Interaction updates
  useEffect(() => {
    if (status === 'ready' && interactions) {
      drawInteractions(interactions, visibleInteractions);
    }
  }, [interactions, status, drawInteractions, visibleInteractions]);

  useEffect(() => {
    if (status === 'ready') {
      void applyHighlight();
    }
  }, [status, applyHighlight]);

  // Spin
  useEffect(() => {
    viewerRef.current?.viewerInstance?.visual?.toggleSpin(spinning);
  }, [spinning, status]);

  return (
    <div className="relative z-20 w-full overflow-hidden rounded-xl border border-glass-border bg-viewer">
      {/* HUD toolbar — spatial chrome for the viewer */}
      <HudPanel className="flex flex-wrap items-center gap-2 border-b border-glass-border px-3 py-2">
        <span className="mr-2 font-mono text-xs text-text-muted">{pdbId}</span>

        {highlightRange && (
          <span className="flex items-center gap-1.5 rounded border border-amber-400/40 bg-amber-400/10 px-2 py-1 text-[10px] text-amber-300">
            <span className="inline-block w-2 h-2 rounded-full bg-amber-400" />
            Highlight {highlightRange.start}–{highlightRange.end}
          </span>
        )}

        {/* Representation */}
        <div className="flex items-center gap-1">
          {REP_OPTIONS.map(opt => {
            const Icon = opt.icon;
            return (
              <button
                key={opt.value}
                type="button"
                title={opt.label}
                onClick={() => setRepresentation(opt.value)}
                disabled={status !== 'ready'}
                className={`p-1.5 rounded text-[10px] transition disabled:opacity-40 ${
                  representation === opt.value
                    ? 'bg-accent-cyan/20 text-accent-cyan'
                    : 'text-hud-text/50 hover:text-hud-text/80 hover:bg-hud/5'
                }`}
              >
                <Icon className="w-3.5 h-3.5" />
              </button>
            );
          })}
        </div>

        <div className="w-px h-5 bg-hud/10" />

        {chains.length > 0 && (
          <select
            value={selectedChain}
            onChange={(event) => { const chain = event.target.value; setSelectedChain(chain); selectChain(chain); }}
            disabled={status !== 'ready'}
            title="Focus protein chain"
            className="rounded border border-hud-line/10 bg-hud/30 px-2 py-1 text-[10px] text-hud-text/80 disabled:opacity-40"
          >
            <option value="">All chains</option>
            {chains.map(chain => <option key={chain.id} value={chain.id}>Chain {chain.id} · {chain.residue_count} residues</option>)}
          </select>
        )}

        {interactions && (
          <button type="button" onClick={focusContacts} disabled={status !== 'ready'} className="rounded border border-hud-line/10 px-2 py-1 text-[10px] text-hud-text/60 hover:bg-hud/5 hover:text-hud-text disabled:opacity-40">
            Focus pocket
          </button>
        )}

        {/* Color scheme */}
        <div className="relative">
          <select
            value={colorScheme}
            onChange={(e) => setColorScheme(e.target.value as ColorScheme)}
            disabled={status !== 'ready'}
            title="Color scheme"
            className="appearance-none rounded border border-hud-line/10 bg-hud/30 pl-6 pr-2 py-1 text-[10px] text-hud-text/80 disabled:opacity-40 cursor-pointer"
          >
            <option value="spectrum">Spectrum</option>
            <option value="chain">Chain</option>
            <option value="secondary-structure">Sec. Structure</option>
            <option value="residue-type">Residue Type</option>
            <option value="bfactor">B-factor</option>
            <option value="uniform">Uniform</option>
          </select>
          <Palette className="absolute left-1.5 top-1/2 -translate-y-1/2 w-3 h-3 text-hud-text/40 pointer-events-none" />
        </div>

        <div className="w-px h-5 bg-hud/10" />

        {/* Action buttons */}
        <button
          type="button"
          title="Toggle spin"
          onClick={() => setSpinning(s => !s)}
          disabled={status !== 'ready'}
          className={`p-1.5 rounded transition disabled:opacity-40 ${
            spinning ? 'text-accent-cyan bg-accent-cyan/10' : 'text-hud-text/50 hover:text-hud-text/80 hover:bg-hud/5'
          }`}
        >
          <RotateCcw className="w-3.5 h-3.5" />
        </button>

        <button
          type="button"
          title="Reset camera"
          onClick={handleResetCamera}
          disabled={status !== 'ready'}
          className="p-1.5 rounded text-hud-text/50 hover:text-hud-text/80 hover:bg-hud/5 transition disabled:opacity-40"
        >
          <RotateCw className="w-3.5 h-3.5" />
        </button>

        <button
          type="button"
          title="Flip view (X-axis)"
          onClick={handleFlipView}
          disabled={status !== 'ready'}
          className="p-1.5 rounded text-hud-text/50 hover:text-hud-text/80 hover:bg-hud/5 transition disabled:opacity-40"
        >
          <FlipHorizontal className="w-3.5 h-3.5" />
        </button>

        <button
          type="button"
          title="Screenshot"
          onClick={handleScreenshot}
          disabled={status !== 'ready'}
          className="p-1.5 rounded text-hud-text/50 hover:text-hud-text/80 hover:bg-hud/5 transition disabled:opacity-40"
        >
          <Camera className="w-3.5 h-3.5" />
        </button>
      </HudPanel>

      {/* Interaction legend */}
      {status === 'ready' && interactions && (
        <HudLegend className="flex flex-wrap items-center gap-2 border-b border-glass-border">
          <span className="mr-1 text-[10px] text-text-muted">Contacts</span>
          {INTERACTION_LAYERS.map(({ key, label, color }) => {
            const count = interactions[key]?.length || 0;
            if (!count) return null;
            const active = visibleInteractions[key];
            return (
              <button
                key={key}
                type="button"
                aria-pressed={active}
                onClick={() => setVisibleInteractions(current => ({ ...current, [key]: !current[key] }))}
                className={`flex items-center gap-1 rounded px-1.5 py-1 transition ${active ? 'bg-hud/10 text-hud-text/90' : 'text-hud-text/35 line-through'}`}
              >
                <span className="inline-block w-3 h-0.5" style={{ backgroundColor: color }} />
                {label} ({count})
              </button>
            );
          })}
        </HudLegend>
      )}

      {(chains.length > 0 || ligands.length > 0) && (
        <div className="flex flex-wrap gap-x-4 gap-y-1 border-b border-glass-border bg-viewer/60 px-3 py-2 text-[10px] text-text-muted">
          {chains.length > 0 && <span>{chains.length} protein chain{chains.length === 1 ? '' : 's'}</span>}
          {ligands.length > 0 && <span>Deposited ligands: {ligands.map(ligand => `${ligand.id}:${ligand.chain}`).join(', ')}</span>}
          {ligandPdb && <span className="text-accent-cyan">Docked ligand loaded</span>}
        </div>
      )}

      {/* Canvas */}
      <div ref={containerRef} style={{ height, minHeight: height, position: 'relative' }} />
      {status === 'loading' && (
        <div className="flex items-center justify-center py-4 text-sm text-hud-text/60">
          Loading structure...
        </div>
      )}
      {status === 'error' && (
        <div className="flex items-center justify-center py-4 px-6 text-center text-sm text-error">
          {error}
        </div>
      )}
    </div>
  );
}