GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Websites and apps / globalgrid2050 / testcode/red-beach-blind-full-20261003-a4c91e

Testcode — Red Beach Blind Full A4c91e Page

A web page in GlobalGrid2050 that builds index (`buildIndex`), sets meta (`setMeta`) and draws equipment (`drawEquipment`) (units: kV, MVA), covering transformers, 11kV, 33kV and cables. Published at https://globalgrid2050.com/testcode/red-beach-blind-full-20261003-a4c91e/.

584 lines of HTML from globalgrid2050, in 1 file. Topic: Websites and apps. Section 1981 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Testcode — Red Beach Blind Full A4c91e Page render · globalgrid2050 / testcode/red-beach-blind-full-20261003-a4c91e/index.html lines 1–584

Testcode — Red Beach Blind Full A4c91e Page

A web page in GlobalGrid2050 that builds index (`buildIndex`), sets meta (`setMeta`) and draws equipment (`drawEquipment`) (units: kV, MVA), covering transformers, 11kV, 33kV and cables. Published at https://globalgrid2050.com/testcode/red-beach-blind-full-20261003-a4c91e/.

renderglobalgrid2050 / testcode/red-beach-blind-full-20261003-a4c91e/index.html · lines 1–584 of 584 · version 2026-10-03 · Entry point · pattern clearSelection (shared by 2 files)

What it does
It draws transformers, 11kV, 33kV and cables. Defines 25 functions or classes, including $, fmt, esc, S, add. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage experimental (path word 'testcode'): work its own path marks as experimental (sandbox, prototype, archive or similar words); by this evidence it is not part of a production workflow. Domain terms in the code: transformers, 11kV, 33kV, cables.
Inputs & outputs
In: user-supplied file (file picker); form fields (user input). Out: web page; SVG drawing. Voltage levels named: 33kV, 11kV.
Downstream impact
It is itself an entry point, published at https://globalgrid2050.com/testcode/red-beach-blind-full-20261003-a4c91e/.
What it achieves
It builds index (`buildIndex`), sets meta (`setMeta`) and draws equipment (`drawEquipment`). Red Beach — Procedural Single Line Diagram. Its code works with 11 kV, 33 kV and 25 kA. It responds to click, wheel and pointerdown events.
Why it matters
No other file in the estate was found to import it; it is an entry point.
Application
globalgrid2050 · testcode/red-beach-blind-full-20261003-a4c91e · page
Uses
none found
Used by
none found
Produces
$, fmt, esc, S, add, lineClass, isFutureEquipment, kvOfEquipment, buildIndex, setMeta, render, drawEquipment
Defines here
$, fmt, esc, S, add, lineClass, isFutureEquipment, kvOfEquipment, buildIndex, setMeta, render, drawEquipment, renderInspector, inspectorHome, clearSelection, selectItem, structuralReach, traceSelected, applyVisibility, fit, zoomAt, showInfo, showToast, searchableItems, focusEquipment
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 testcode/red-beach-blind-full-20261003-a4c91e/index.html:1-584 · blob f03c2ffab46d3b766f5dc68a95b3e837e3031de0 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: experimental · users: none · canonical: production_core
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover" />
<title>Red Beach — Procedural Single Line Diagram</title>
<style>
:root{
  --bg:#071018;
  --panel:#0d1822;
  --panel2:#111f2c;
  --text:#eaf2f8;
  --muted:#90a4b5;
  --line:#8ca2b3;
  --kv33:#ffb347;
  --kv11:#5dd5ff;
  --future:#9aa6b2;
  --accent:#8cf0c8;
  --danger:#ff7b7b;
  --grid:rgba(255,255,255,.035);
  --border:rgba(255,255,255,.10);
  --shadow:0 14px 40px rgba(0,0,0,.32);
}
*{box-sizing:border-box}
html,body{height:100%;margin:0;background:var(--bg);color:var(--text);font-family:Inter,ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}
body{overflow:hidden}
button,input{font:inherit}
button{color:var(--text);background:var(--panel2);border:1px solid var(--border);border-radius:9px;padding:8px 10px;cursor:pointer}
button:hover{border-color:rgba(255,255,255,.28)}
button.active{border-color:var(--accent);box-shadow:0 0 0 1px rgba(140,240,200,.22) inset}
button.primary{background:#14382f;border-color:#276c5b}
input[type="search"]{background:#08131c;color:var(--text);border:1px solid var(--border);border-radius:9px;padding:8px 10px;min-width:180px}
.app{display:grid;grid-template-rows:auto 1fr;height:100%}
header{
  display:flex;gap:12px;align-items:center;justify-content:space-between;
  padding:10px 14px;border-bottom:1px solid var(--border);background:rgba(7,16,24,.96);
  backdrop-filter:blur(12px);z-index:20
}
.titlewrap{min-width:230px}
h1{font-size:16px;line-height:1.1;margin:0;font-weight:720;letter-spacing:.02em}
.sub{font-size:11px;color:var(--muted);margin-top:3px}
.controls{display:flex;gap:7px;align-items:center;flex-wrap:wrap;justify-content:flex-end}
.workspace{display:grid;grid-template-columns:minmax(0,1fr) 330px;min-height:0}
.stage{
  position:relative;min-width:0;min-height:0;overflow:hidden;
  background:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg,var(--grid) 1px, transparent 1px),
    radial-gradient(circle at 45% 35%,#10202d 0,#08131c 48%,#050c12 100%);
  background-size:32px 32px,32px 32px,100% 100%;
  touch-action:none
}
svg{width:100%;height:100%;display:block;user-select:none;-webkit-user-select:none}
.world{transform-origin:0 0}
.panel{border-left:1px solid var(--border);background:var(--panel);display:flex;flex-direction:column;min-height:0}
.panel-head{padding:14px 14px 11px;border-bottom:1px solid var(--border)}
.panel-head strong{font-size:13px}
.panel-scroll{overflow:auto;padding:12px 14px 30px}
.card{background:var(--panel2);border:1px solid var(--border);border-radius:12px;padding:11px 12px;margin-bottom:10px}
.card h2{font-size:12px;margin:0 0 8px;color:#fff;letter-spacing:.04em;text-transform:uppercase}
.kv{display:grid;grid-template-columns:112px 1fr;gap:5px 8px;font-size:12px}
.kv div:nth-child(odd){color:var(--muted)}
.mono{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;word-break:break-word}
.pill{display:inline-flex;align-items:center;gap:5px;border:1px solid var(--border);border-radius:999px;padding:4px 7px;font-size:10px;color:#d6e0e8}
.dot{width:8px;height:8px;border-radius:50%;display:inline-block}
.legend{position:absolute;left:12px;bottom:12px;display:flex;gap:7px;flex-wrap:wrap;z-index:10}
.legend .pill{background:rgba(6,14,20,.82);backdrop-filter:blur(8px)}
.notice{position:absolute;top:12px;left:12px;max-width:540px;background:rgba(8,19,28,.88);border:1px solid var(--border);border-radius:10px;padding:8px 10px;font-size:11px;color:#b8c6d0;z-index:10;backdrop-filter:blur(8px);pointer-events:none}
.toast{position:absolute;right:12px;bottom:12px;background:#10212e;border:1px solid var(--border);border-radius:10px;padding:9px 12px;font-size:11px;opacity:0;transform:translateY(6px);transition:.2s;pointer-events:none}
.toast.show{opacity:1;transform:none}
.statusbar{display:flex;gap:8px;align-items:center;color:var(--muted);font-size:10px;margin-top:8px;flex-wrap:wrap}
.statusbar span{border:1px solid var(--border);border-radius:999px;padding:3px 6px}
svg text{fill:#dfe9ef;font-family:ui-sans-serif,system-ui,sans-serif;pointer-events:none}
.enclosure{fill:rgba(255,255,255,.012);stroke:#516474;stroke-width:1.5;stroke-dasharray:16 10;vector-effect:non-scaling-stroke}
.route,.bus,.equipment-stroke{fill:none;stroke-linecap:round;stroke-linejoin:round;vector-effect:non-scaling-stroke}
.route{stroke-width:2.25}
.bus{stroke-width:5}
.solid{}
.dashed{stroke-dasharray:10 8}
.long_dash{stroke-dasharray:18 10}
.kv33{stroke:var(--kv33)}
.kv11{stroke:var(--kv11)}
.unknownkv{stroke:var(--line)}
.future{opacity:.62}
.hit{stroke:transparent;stroke-width:20;fill:none;cursor:pointer;pointer-events:stroke;vector-effect:non-scaling-stroke}
.eqhit{cursor:pointer}
.breaker-body{stroke-width:2.2;fill:none}
.breaker-cross{stroke-width:1.6}
.transformer-circle{fill:rgba(255,255,255,.025);stroke-width:2.2}
.external-cap{stroke-width:2}
.eq-label{font-size:15px;font-weight:650}
.small-label{font-size:11px;fill:#9fb0bd}
.bay-label{font-size:13px;font-weight:700;fill:#cbd7df}
.annotation{font-size:13px;fill:#93a7b6}
.voltage-label{font-size:13px;font-weight:800}
.selected .breaker-body,.selected .breaker-cross,.selected .transformer-circle,.selected .external-cap{stroke:var(--accent)!important;filter:drop-shadow(0 0 5px rgba(140,240,200,.55))}
.highlight{stroke:var(--accent)!important;opacity:1!important;filter:drop-shadow(0 0 4px rgba(140,240,200,.45))}
.trace-overlay{
  fill:none;stroke:var(--accent);stroke-width:5;stroke-linecap:round;stroke-linejoin:round;
  stroke-dasharray:3 16;animation:march 1s linear infinite;vector-effect:non-scaling-stroke;
  filter:drop-shadow(0 0 5px rgba(140,240,200,.45));pointer-events:none
}
@keyframes march{to{stroke-dashoffset:-38}}
.hiddenSystem{opacity:.08;pointer-events:none}
.hiddenFuture{display:none}
.noLabels .human-label{display:none}
.crosshair{pointer-events:none;display:none}
.search-results{position:absolute;right:345px;top:56px;width:310px;max-height:320px;overflow:auto;background:#0a1620;border:1px solid var(--border);border-radius:10px;box-shadow:var(--shadow);z-index:30;display:none}
.search-results.show{display:block}
.search-item{padding:9px 10px;border-bottom:1px solid rgba(255,255,255,.06);cursor:pointer}
.search-item:hover{background:#142332}
.search-item:last-child{border-bottom:0}
.search-item b{font-size:12px}
.search-item div{font-size:10px;color:var(--muted);margin-top:2px}
dialog{background:#0b1721;color:var(--text);border:1px solid var(--border);border-radius:14px;max-width:720px;width:min(92vw,720px);box-shadow:var(--shadow);padding:0}
dialog::backdrop{background:rgba(0,0,0,.66)}
.modal-head{padding:14px 16px;border-bottom:1px solid var(--border);display:flex;justify-content:space-between;align-items:center}
.modal-body{padding:16px;max-height:70vh;overflow:auto;font-size:13px;line-height:1.5}
.modal-body h3{font-size:12px;text-transform:uppercase;letter-spacing:.05em;margin:18px 0 6px}
.modal-body ul{padding-left:19px}
.modal-body code{color:#b7f5df}
input[type=file]{display:none}
@media(max-width:900px){
  body{overflow:auto}
  .app{height:auto;min-height:100%}
  header{align-items:flex-start;flex-direction:column}
  .controls{justify-content:flex-start;width:100%}
  input[type="search"]{flex:1;min-width:140px}
  .workspace{grid-template-columns:1fr;grid-template-rows:68vh auto}
  .panel{border-left:0;border-top:1px solid var(--border)}
  .search-results{right:10px;left:10px;top:108px;width:auto}
  .notice{max-width:calc(100% - 24px)}
}
</style>
</head>
<body>
<div class="app">
  <header>
    <div class="titlewrap">
      <h1 id="title">Procedural SLD</h1>
      <div class="sub" id="subtitle"></div>
      <div class="statusbar" id="statusbar"></div>
    </div>
    <div class="controls">
      <input id="search" type="search" placeholder="Find bay, circuit, transformer…" autocomplete="off" />
      <button id="fitBtn" title="Fit drawing">Fit</button>
      <button id="traceBtn" title="Animate structural connectivity">Trace topology</button>
      <button id="futureBtn" class="active" title="Show/hide future or dashed provisions">Future</button>
      <button id="labelsBtn" class="active" title="Show/hide human-readable labels">Labels</button>
      <button id="infoBtn" title="Source and assumptions">Source / assumptions</button>
      <label><button id="loadBtn" title="Load another compatible JSON">Load JSON</button><input id="fileInput" type="file" accept=".json,application/json"/></label>
    </div>
  </header>

  <div class="workspace">
    <main class="stage" id="stage">
      <div class="notice">Interactive design-topology visualisation. Breaker operating states are unknown; animation shows structural connectivity, <b>not validated power flow</b>.</div>
      <svg id="svg" role="img" aria-label="Procedural single-line diagram">
        <g id="viewport"></g>
      </svg>
      <div class="legend">
        <span class="pill"><span class="dot" style="background:var(--kv33)"></span>33 kV</span>
        <span class="pill"><span class="dot" style="background:var(--kv11)"></span>11 kV</span>
        <span class="pill"><span style="width:16px;border-top:1px dashed #aab5bd;display:inline-block"></span>future / dashed</span>
      </div>
      <div id="toast" class="toast"></div>
      <div id="searchResults" class="search-results"></div>
    </main>

    <aside class="panel">
      <div class="panel-head"><strong>Inspector</strong><div class="sub">Click equipment, busbar or connection.</div></div>
      <div class="panel-scroll" id="inspector"></div>
    </aside>
  </div>
</div>

<dialog id="infoDialog">
  <div class="modal-head"><strong>Source / assumptions</strong><button id="closeInfo">Close</button></div>
  <div class="modal-body" id="infoBody"></div>
</dialog>

<script id="source-json" type="application/json">{"format": "substation_single_line_model", "format_version": "1.0.0", "id": "red_beach_4256_0010_B", "title": "RED BEACH SINGLE LINE DIAGRAM", "description": "Electrical connectivity and schematic layout transcribed from the supplied single-page drawing.", "source": {"filename": "4256-0010 (B).pdf", "drawing_number": "4256-0010", "revision": "B", "s  … [line shortened here: 46,663 characters; full line on GitHub]
<script>
(() => {
'use strict';

const NS='http://www.w3.org/2000/svg';
const $=s=>document.querySelector(s);
const svg=$('#svg'), viewport=$('#viewport'), stage=$('#stage'), inspector=$('#inspector');
const search=$('#search'), results=$('#searchResults'), toast=$('#toast');
let model=JSON.parse($('#source-json').textContent);
let index=null, view=null, drag=null, selected=null, traceOn=false, showFuture=true, showLabels=true;

const fmt=v => v===null||v===undefined ? '—' : typeof v==='object' ? JSON.stringify(v) : String(v);
const esc=s=>String(s??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
const S=(tag,attrs={},text=null)=>{
  const e=document.createElementNS(NS,tag);
  for(const [k,v] of Object.entries(attrs)){
    if(v===null||v===undefined) continue;
    if(k==='class') e.setAttribute('class',v); else e.setAttribute(k,String(v));
  }
  if(text!==null) e.textContent=text;
  return e;
};
function add(parent,tag,attrs,text){const e=S(tag,attrs,text);parent.appendChild(e);return e}
function lineClass(kv,style,extra=''){return `${kv===33?'kv33':kv===11?'kv11':'unknownkv'} ${style||'solid'} ${extra}`.trim()}
function isFutureEquipment(eq,placement){
  const s=(eq?.provision_status||'')+' '+(eq?.drawing_line_style||'')+' '+(placement?.line_style||'');
  return /future|dashed/.test(s);
}
function kvOfEquipment(eq){
  if(!eq) return null;
  if(typeof eq.nominal_voltage_kv==='number') return eq.nominal_voltage_kv;
  if(eq.rated_voltage_kv?.hv) return null;
  return null;
}
function buildIndex(){
  const equipment=new Map(model.equipment.map(x=>[x.id,x]));
  const bays=new Map(model.bays.map(x=>[x.id,x]));
  const nodes=new Map(model.nodes.map(x=>[x.id,x]));
  const connections=new Map(model.connections.map(x=>[x.id,x]));
  const placements=new Map((model.layout?.equipment_placements||[]).map(x=>[x.equipment_id,x]));
  const byEq=new Map(), byNode=new Map();
  for(const c of model.connections){
    if(!byEq.has(c.terminal.equipment_id))byEq.set(c.terminal.equipment_id,[]);
    byEq.get(c.terminal.equipment_id).push(c);
    if(!byNode.has(c.node_id))byNode.set(c.node_id,[]);
    byNode.get(c.node_id).push(c);
  }
  return {equipment,bays,nodes,connections,placements,byEq,byNode};
}
function setMeta(){
  $('#title').textContent=model.title||model.substation?.name||'Single Line Diagram';
  const s=model.source||{};
  $('#subtitle').textContent=[s.drawing_number&&`Drawing ${s.drawing_number}`,s.revision&&`Rev ${s.revision}`,model.substation?.name].filter(Boolean).join(' · ');
  const cov=model.coverage||{};
  const chips=[
    `${model.switchboards?.length||0} switchboards`,
    `${model.bays?.length||0} bays`,
    `${cov.circuit_breaker_count ?? model.equipment.filter(e=>e.type==='circuit_breaker').length} breakers`,
    `${cov.transformer_count ?? model.equipment.filter(e=>e.type==='power_transformer').length} transformers`
  ];
  $('#statusbar').innerHTML=chips.map(x=>`<span>${esc(x)}</span>`).join('');
}
function render(){
  index=buildIndex(); setMeta(); selected=null; traceOn=false; $('#traceBtn').classList.remove('active');
  viewport.replaceChildren();
  const canvas=model.layout?.canvas||{width:1888,height:1334};
  view={x:0,y:0,w:canvas.width,h:canvas.height};
  svg.setAttribute('viewBox',`0 0 ${canvas.width} ${canvas.height}`);

  const gBack=add(viewport,'g',{id:'back'});
  const gRoutes=add(viewport,'g',{id:'routes'});
  const gBus=add(viewport,'g',{id:'buses'});
  const gEq=add(viewport,'g',{id:'equipment'});
  const gLabels=add(viewport,'g',{id:'labels'});
  const gTrace=add(viewport,'g',{id:'trace'});

  for(const enc of model.layout?.enclosures||[]){
    const [x,y,w,h]=enc.bounds;
    add(gBack,'rect',{x,y,width:w,height:h,rx:8,class:`enclosure ${enc.line_style||''}`});
    if(enc.end_labels){
      add(gLabels,'text',{x:x+8,y:y+18,class:'small-label human-label'},enc.end_labels.left||'');
      add(gLabels,'text',{x:x+w-8,y:y+18,'text-anchor':'end',class:'small-label human-label'},enc.end_labels.right||'');
    }
  }

  for(const r of model.layout?.wire_routes||[]){
    const c=index.connections.get(r.connection_id);
    const eq=c ? index.equipment.get(c.terminal.equipment_id) : null;
    const node=c ? index.nodes.get(c.node_id) : null;
    const kv=kvOfEquipment(eq) ?? node?.nominal_voltage_kv ?? null;
    if(!r.points?.length) continue;
    const pts=r.points.map(p=>p.join(',')).join(' ');
    const fut=(r.line_style==='dashed'||isFutureEquipment(eq,index.placements.get(eq?.id)))?' future':'';
    const p=add(gRoutes,'polyline',{points:pts,class:`route ${lineClass(kv,r.line_style)}${fut}`,'data-connection':r.connection_id,'data-node':c?.node_id||'','data-equipment':eq?.id||''});
    const h=add(gRoutes,'polyline',{points:pts,class:'hit','data-pick-kind':'connection','data-pick-id':r.connection_id});
    h.addEventListener('click',ev=>{ev.stopPropagation();selectItem('connection',r.connection_id)});
  }

  for(const b of model.layout?.busbar_segments||[]){
    const eq=index.equipment.get(b.equipment_id), kv=kvOfEquipment(eq);
    const pts=b.points.map(p=>p.join(',')).join(' ');
    const fut=(b.line_style==='dashed'||/future/.test(b.provision_status||''))?' future':'';
    add(gBus,'polyline',{points:pts,class:`bus ${lineClass(kv,b.line_style)}${fut}`,'data-bus-equipment':b.equipment_id,'data-bus-segment':b.id});
    const h=add(gBus,'polyline',{points:pts,class:'hit','data-pick-kind':'equipment','data-pick-id':b.equipment_id});
    h.addEventListener('click',ev=>{ev.stopPropagation();selectItem('equipment',b.equipment_id)});
  }

  for(const p of model.layout?.equipment_placements||[]){
    const eq=index.equipment.get(p.equipment_id); if(!eq) continue;
    if(eq.type==='busbar_section') continue;
    drawEquipment(gEq,gLabels,eq,p);
  }

  for(const bl of model.layout?.bay_labels||[]){
    add(gLabels,'text',{x:bl.position[0],y:bl.position[1],class:'bay-label human-label','text-anchor':'middle'},bl.text);
  }

  for(const a of model.layout?.annotations||[]){
    const lines=String(a.text||'').split('\n');
    const t=add(gLabels,'text',{x:a.position[0],y:a.position[1],class:'annotation human-label'});
    lines.forEach((ln,i)=>add(t,'tspan',{x:a.position[0],dy:i?16:0},ln));
  }

  // explicit voltage labels, derived from switchboard objects, placed beside enclosure bounds
  for(const enc of model.layout?.enclosures||[]){
    const sb=model.switchboards.find(s=>s.id===enc.switchboard_id); if(!sb) continue;
    const [x,y]=enc.bounds;
    add(gLabels,'text',{x:x+12,y:y-12,class:`voltage-label human-label ${sb.nominal_voltage_kv===33?'kv33':'kv11'}`,style:`fill:${sb.nominal_voltage_kv===33?'var(--kv33)':'var(--kv11)'}`},`${sb.nominal_voltage_kv} kV`);
  }

  stage.onclick=()=>clearSelection();
  fit();
  inspectorHome();
  applyVisibility();
}
function drawEquipment(g,gLabels,eq,p){
  const [x,y]=p.position;
  const kv=kvOfEquipment(eq);
  const klass=lineClass(kv,p.line_style, isFutureEquipment(eq,p)?'future':'');
  const group=add(g,'g',{class:`eq eqhit ${isFutureEquipment(eq,p)?'future':''}`,'data-equipment-id':eq.id});
  group.addEventListener('click',ev=>{ev.stopPropagation();selectItem('equipment',eq.id)});

  if(eq.type==='circuit_breaker'){
    const horizontal=p.orientation==='horizontal';
    if(horizontal){
      add(group,'line',{x1:x-15,y1:y,x2:x-6,y2:y,class:`equipment-stroke breaker-body ${klass}`});
      add(group,'line',{x1:x+6,y1:y,x2:x+15,y2:y,class:`equipment-stroke breaker-body ${klass}`});
      add(group,'line',{x1:x-6,y1:y,x2:x+7,y2:y-10,class:`equipment-stroke breaker-body ${klass}`});
      add(group,'line',{x1:x-4,y1:y-7,x2:x+5,y2:y+7,class:`equipment-stroke breaker-cross ${klass}`});
      add(group,'line',{x1:x-4,y1:y+7,x2:x+5,y2:y-7,class:`equipment-stroke breaker-cross ${klass}`});
      add(group,'rect',{x:x-18,y:y-18,width:36,height:36,fill:'transparent'});
    }else{
      add(group,'line',{x1:x,y1:y-15,x2:x,y2:y-6,class:`equipment-stroke breaker-body ${klass}`});
      add(group,'line',{x1:x,y1:y+6,x2:x,y2:y+15,class:`equipment-stroke breaker-body ${klass}`});
      add(group,'line',{x1:x,y1:y+6,x2:x+10,y2:y-7,class:`equipment-stroke breaker-body ${klass}`});
      add(group,'line',{x1:x-7,y1:y-4,x2:x+7,y2:y+5,class:`equipment-stroke breaker-cross ${klass}`});
      add(group,'line',{x1:x+7,y1:y-4,x2:x-7,y2:y+5,class:`equipment-stroke breaker-cross ${klass}`});
      add(group,'rect',{x:x-18,y:y-18,width:36,height:36,fill:'transparent'});
    }
    add(gLabels,'text',{x:x+17,y:y+5,class:'small-label human-label'},eq.source_local_marking||eq.id);
  }
  else if(eq.type==='power_transformer'){
    const centers=p.symbol_circle_centres||[[x,y-24],[x,y+24]], r=p.symbol_circle_radius||32;
    for(const c of centers) add(group,'circle',{cx:c[0],cy:c[1],r,class:`equipment-stroke transformer-circle ${klass}`});
    const lp=p.label_position||[x+r+20,y-28];
    add(gLabels,'text',{x:lp[0],y:lp[1],class:'eq-label human-label'},eq.label||eq.id);
    add(gLabels,'text',{x:lp[0],y:lp[1]+18,class:'small-label human-label'},eq.source_rating_text||'');
    add(group,'rect',{x:x-r-8,y:centers[0][1]-r-8,width:2*r+16,height:(centers[1][1]-centers[0][1])+2*r+16,fill:'transparent'});
  }
  else if(eq.type==='external_circuit'){
    add(group,'line',{x1:x-8,y1:y,x2:x+8,y2:y,class:`equipment-stroke external-cap ${klass}`});
    add(group,'circle',{cx:x,cy:y,r:12,fill:'transparent'});
    const rot=p.label_rotation||0;
    const label=eq.source_label||eq.label||eq.id;
    const tx=x+(rot?-13:14), ty=y+(rot?-10:4);
    add(gLabels,'text',{x:tx,y:ty,class:'eq-label human-label',transform:rot?`rotate(${rot} ${tx} ${ty})`:null,'text-anchor':rot?'start':'start'},label);
    if(eq.via){
      const vy=y+(rot?110:19);
      add(gLabels,'text',{x:tx,y:vy,class:'small-label human-label',transform:rot?`rotate(${rot} ${tx} ${vy})`:null},`via ${eq.via}`);
    }
  }
}
function renderInspector(title,rows,extra=''){
  inspector.innerHTML=`<div class="card"><h2>${esc(title)}</h2><div class="kv">${
    rows.map(([k,v])=>`<div>${esc(k)}</div><div class="${typeof v==='string'&&v.length>30?'mono':''}">${esc(fmt(v))}</div>`).join('')
  }</div>${extra}</div>`;
}
function inspectorHome(){
  const s=model.source||{}, c=model.conventions||{};
  inspector.innerHTML=`
    <div class="card"><h2>${esc(model.substation?.name||'Model')}</h2>
      <div class="kv">
        <div>Drawing</div><div>${esc(s.drawing_number||'—')}</div>
        <div>Revision</div><div>${esc(s.revision||'—')}</div>
        <div>Source</div><div>${esc(s.filename||'—')}</div>
        <div>Verification</div><div>${esc(s.extraction?.verification_status||'—')}</div>
      </div>
    </div>
    <div class="card"><h2>How to use</h2>
      <div style="font-size:12px;line-height:1.55;color:#b9c7d1">
        Drag to pan. Wheel or pinch-style trackpad gesture to zoom. Click a breaker, transformer, external circuit, busbar or route to inspect it. Use <b>Trace topology</b> after selection to animate the structurally connected design graph.
      </div>
    </div>
    <div class="card"><h2>Critical source rule</h2>
      <div style="font-size:12px;line-height:1.55;color:#b9c7d1">${esc(c.operating_state||'Breaker states are not supplied.')}</div>
    </div>`;
}
function clearSelection(){
  selected=null; traceOn=false; $('#traceBtn').classList.remove('active');
  viewport.querySelectorAll('.selected,.highlight').forEach(e=>e.classList.remove('selected','highlight'));
  $('#trace').replaceChildren(); inspectorHome();
}
function selectItem(kind,id){
  selected={kind,id}; traceOn=false; $('#traceBtn').classList.remove('active'); $('#trace').replaceChildren();
  viewport.querySelectorAll('.selected,.highlight').forEach(e=>e.classList.remove('selected','highlight'));
  if(kind==='equipment'){
    const eq=index.equipment.get(id); if(!eq)return;
    viewport.querySelectorAll(`[data-equipment-id="${CSS.escape(id)}"]`).forEach(e=>e.classList.add('selected'));
    viewport.querySelectorAll(`[data-bus-equipment="${CSS.escape(id)}"]`).forEach(e=>e.classList.add('highlight'));
    const p=index.placements.get(id), bay=eq.bay_id?index.bays.get(eq.bay_id):null;
    const rows=[
      ['ID',eq.id],['Type',eq.type],['Bay',eq.bay_id||eq.hv_bay_id||'—'],['Label',eq.label||eq.source_label||'—'],
      ['Voltage',eq.nominal_voltage_kv ? `${eq.nominal_voltage_kv} kV` : eq.rated_voltage_kv ? `${eq.rated_voltage_kv.hv}/${eq.rated_voltage_kv.lv} kV`:'—'],
      ['Rating',eq.rated_current_a ? `${eq.rated_current_a} A` : eq.rated_power_mva ? `${eq.rated_power_mva} MVA`:'—'],
      ['Source ref',eq.source_reference||'—'],['Operating state',eq.operating_state||'—'],['Provision',eq.provision_status||p?.line_style||'—']
    ];
    if(eq.vector_group) rows.push(['Vector group',eq.vector_group]);
    if(eq.via) rows.push(['Via',eq.via]);
    const conns=index.byEq.get(id)||[];
    const extra=`<div style="margin-top:10px;font-size:11px;color:#91a4b4">Nodes: ${conns.map(c=>`<span class="pill mono">${esc(c.node_id)}</span>`).join(' ')||'—'}</div>
      ${bay?`<div style="margin-top:8px;font-size:11px;color:#91a4b4">Bay role: ${esc(bay.role)}</div>`:''}`;
    renderInspector('Equipment',rows,extra);
  }else{
    const c=index.connections.get(id); if(!c)return;
    viewport.querySelectorAll(`[data-connection="${CSS.escape(id)}"]`).forEach(e=>e.classList.add('highlight'));
    const eq=index.equipment.get(c.terminal.equipment_id), node=index.nodes.get(c.node_id);
    renderInspector('Connection',[
      ['ID',c.id],['Equipment',c.terminal.equipment_id],['Terminal',c.terminal.terminal_id],['Node',c.node_id],
      ['Node kind',node?.kind||'—'],['Voltage',node?.nominal_voltage_kv?`${node.nominal_voltage_kv} kV`:'—'],
      ['Equipment type',eq?.type||'—']
    ]);
  }
}
function structuralReach(startEq){
  // Design-topology traversal. It intentionally treats multi-terminal breakers and transformers as graph bridges.
  // This is structural connectivity only because operating states are not provided.
  const eqSeen=new Set(), nodeSeen=new Set(), connSeen=new Set(), q=[];
  if(startEq){eqSeen.add(startEq);q.push({t:'eq',id:startEq})}
  while(q.length){
    const cur=q.shift();
    if(cur.t==='eq'){
      for(const c of index.byEq.get(cur.id)||[]){
        connSeen.add(c.id);
        if(!nodeSeen.has(c.node_id)){nodeSeen.add(c.node_id);q.push({t:'node',id:c.node_id})}
      }
    }else{
      for(const c of index.byNode.get(cur.id)||[]){
        connSeen.add(c.id);
        const eid=c.terminal.equipment_id;
        if(!eqSeen.has(eid)){eqSeen.add(eid);q.push({t:'eq',id:eid})}
      }
    }
  }
  return {eqSeen,nodeSeen,connSeen};
}
function traceSelected(){
  $('#trace').replaceChildren();
  if(!selected){showToast('Select equipment or a connection first.');return}
  let startEq=selected.kind==='equipment'?selected.id:index.connections.get(selected.id)?.terminal.equipment_id;
  if(!startEq)return;
  const reach=structuralReach(startEq);
  for(const r of model.layout?.wire_routes||[]){
    if(!reach.connSeen.has(r.connection_id)||!r.points?.length)continue;
    add($('#trace'),'polyline',{points:r.points.map(p=>p.join(',')).join(' '),class:'trace-overlay'});
  }
  for(const b of model.layout?.busbar_segments||[]){
    if(!reach.eqSeen.has(b.equipment_id))continue;
    add($('#trace'),'polyline',{points:b.points.map(p=>p.join(',')).join(' '),class:'trace-overlay'});
  }
  // Trace through multi-terminal equipment themselves.
  for(const eid of reach.eqSeen){
    viewport.querySelectorAll(`[data-equipment-id="${CSS.escape(eid)}"] .breaker-body, [data-equipment-id="${CSS.escape(eid)}"] .transformer-circle`).forEach(el=>{
      const clone=el.cloneNode(true); clone.setAttribute('class','trace-overlay'); $('#trace').appendChild(clone);
    });
  }
  showToast(`${reach.eqSeen.size} equipment items · ${reach.nodeSeen.size} nodes in design topology`);
}
function applyVisibility(){
  viewport.classList.toggle('noLabels',!showLabels);
  viewport.querySelectorAll('.future').forEach(e=>e.classList.toggle('hiddenFuture',!showFuture));
}
function fit(){
  const c=model.layout?.canvas||{width:1888,height:1334};
  view={x:0,y:0,w:c.width,h:c.height};
  svg.setAttribute('viewBox',`${view.x} ${view.y} ${view.w} ${view.h}`);
}
function zoomAt(clientX,clientY,factor){
  const rect=svg.getBoundingClientRect();
  const px=(clientX-rect.left)/rect.width, py=(clientY-rect.top)/rect.height;
  const wx=view.x+px*view.w, wy=view.y+py*view.h;
  const nw=Math.max(220,Math.min((model.layout?.canvas?.width||1888)*3,view.w*factor));
  const nh=nw*(rect.height/rect.width);
  view.x=wx-px*nw; view.y=wy-py*nh; view.w=nw; view.h=nh;
  svg.setAttribute('viewBox',`${view.x} ${view.y} ${view.w} ${view.h}`);
}
svg.addEventListener('wheel',e=>{e.preventDefault();zoomAt(e.clientX,e.clientY,Math.exp(e.deltaY*0.0012))},{passive:false});
stage.addEventListener('pointerdown',e=>{
  if(e.button!==0)return;
  stage.setPointerCapture(e.pointerId);
  drag={x:e.clientX,y:e.clientY,vx:view.x,vy:view.y,moved:false};
});
stage.addEventListener('pointermove',e=>{
  if(!drag)return;
  const rect=svg.getBoundingClientRect();
  const dx=(e.clientX-drag.x)*view.w/rect.width, dy=(e.clientY-drag.y)*view.h/rect.height;
  if(Math.abs(dx)+Math.abs(dy)>2)drag.moved=true;
  view.x=drag.vx-dx; view.y=drag.vy-dy;
  svg.setAttribute('viewBox',`${view.x} ${view.y} ${view.w} ${view.h}`);
});
stage.addEventListener('pointerup',e=>{drag=null;});

$('#fitBtn').onclick=fit;
$('#traceBtn').onclick=()=>{
  if(!selected){showToast('Select equipment or a connection first.');return}
  traceOn=!traceOn; $('#traceBtn').classList.toggle('active',traceOn);
  if(traceOn) traceSelected(); else $('#trace').replaceChildren();
};
$('#futureBtn').onclick=()=>{showFuture=!showFuture;$('#futureBtn').classList.toggle('active',showFuture);applyVisibility()};
$('#labelsBtn').onclick=()=>{showLabels=!showLabels;$('#labelsBtn').classList.toggle('active',showLabels);applyVisibility()};
$('#infoBtn').onclick=()=>showInfo();
$('#closeInfo').onclick=()=>$('#infoDialog').close();
$('#loadBtn').onclick=()=>$('#fileInput').click();
$('#fileInput').onchange=async e=>{
  const f=e.target.files?.[0]; if(!f)return;
  try{
    const candidate=JSON.parse(await f.text());
    const required=['equipment','nodes','connections','layout'];
    if(required.some(k=>!candidate[k])) throw new Error('JSON is missing required model sections: '+required.join(', '));
    model=candidate; render(); showToast(`Loaded ${f.name}`);
  }catch(err){showToast('Could not load JSON: '+err.message,true)}
  e.target.value='';
};
function showInfo(){
  const s=model.source||{}, c=model.conventions||{}, reviews=model.review_items||[];
  $('#infoBody').innerHTML=`
    <p><b>Directly used from the JSON:</b> equipment, bays, voltage levels, node/terminal connectivity, approximate page coordinates, wire routes, busbar segments, source labels, line styles, switchboard bounds and source review items.</p>
    <p><b>Invented by this viewer:</b> screen colours, typography, inspector layout, pan/zoom controls, search UI, selection highlighting and the animated structural-topology overlay.</p>
    <p><b>Not inferred:</b> live breaker state, actual energisation, power-flow direction, protection behaviour, cable parameters, relay settings, transformer impedance or earthing values.</p>
    <h3>Source</h3>
    <div class="kv">
      <div>File</div><div>${esc(s.filename||'—')}</div>
      <div>Drawing</div><div>${esc(s.drawing_number||'—')}</div>
      <div>Revision</div><div>${esc(s.revision||'—')}</div>
      <div>Extraction</div><div>${esc(s.extraction?.method||'—')}</div>
      <div>Verification</div><div>${esc(s.extraction?.verification_status||'—')}</div>
      <div>Layout basis</div><div>${esc(c.layout||'—')}</div>
    </div>
    <h3>Review items from source model</h3>
    <ul>${reviews.map(r=>`<li><b>${esc(r.id)} · ${esc(r.severity)}</b>: ${esc(r.observed)}<br><span style="color:#92a6b5">${esc(r.handling)}</span></li>`).join('')}</ul>`;
  $('#infoDialog').showModal();
}
function showToast(msg,bad=false){
  toast.textContent=msg; toast.style.borderColor=bad?'var(--danger)':'var(--border)';toast.classList.add('show');
  clearTimeout(showToast.t); showToast.t=setTimeout(()=>toast.classList.remove('show'),2600);
}
function searchableItems(){
  return model.equipment.map(e=>({kind:'equipment',id:e.id,label:e.label||e.source_label||e.source_reference||'',type:e.type}))
    .concat(model.bays.map(b=>({kind:'bay',id:b.id,label:b.label||b.source_reference||'',type:b.role})));
}
search.addEventListener('input',()=>{
  const q=search.value.trim().toLowerCase();
  if(!q){results.classList.remove('show');return}
  const hits=searchableItems().filter(x=>(x.id+' '+x.label+' '+x.type).toLowerCase().includes(q)).slice(0,14);
  results.innerHTML=hits.map((x,i)=>`<div class="search-item" data-i="${i}"><b>${esc(x.id)}</b><div>${esc(x.label||x.type)}</div></div>`).join('');
  results.classList.toggle('show',hits.length>0);
  [...results.children].forEach((el,i)=>el.onclick=()=>{
    const x=hits[i]; results.classList.remove('show');
    if(x.kind==='equipment') focusEquipment(x.id);
    else{
      const bay=index.bays.get(x.id); const eid=bay?.equipment_ids?.find(id=>index.placements.has(id))||bay?.equipment_ids?.[0];
      if(eid)focusEquipment(eid);
    }
  });
});
function focusEquipment(id){
  const p=index.placements.get(id);
  selectItem('equipment',id);
  if(!p)return;
  const rect=svg.getBoundingClientRect(), targetW=Math.min(700,model.layout?.canvas?.width||1888), targetH=targetW*rect.height/rect.width;
  view={x:p.position[0]-targetW/2,y:p.position[1]-targetH/2,w:targetW,h:targetH};
  svg.setAttribute('viewBox',`${view.x} ${view.y} ${view.w} ${view.h}`);
}
document.addEventListener('keydown',e=>{
  if(e.key==='Escape'){results.classList.remove('show');if($('#infoDialog').open)$('#infoDialog').close()}
  if(e.key==='0')fit();
});

render();
})();
</script>
</body>
</html>