/* Overall PFD plus a passive, source-aware treatment walkthrough. */
(() => {
'use strict';
const stages=[
{id:'intake',name:'Intake and raw-water pumping',short:'Intake',river:'River water enters an intake. A screen intercepts larger debris, and raw-water pumps convey it to pretreatment.',groundwater:'Wells lift groundwater from the aquifer. Influent pumping brings it to the treatment plant.',result:'Raw water reaches the treatment train.'},
{id:'pretreat',name:'Pretreatment / pre-oxidation',short:'Pretreat',river:'Pre-oxidation conditions river water before coagulation. Chemical choice and application depend on source-water quality.',groundwater:'Sodium hypochlorite oxidizes arsenite As(III) to arsenate As(V), preparing it for removal.',result:'Water chemistry is conditioned before coagulation.'},
{id:'rapid',name:'Rapid mix / coagulation',short:'Rapid mix',description:'Ferric chloride is rapidly mixed with the water to prepare fine particles for aggregation and separation.',result:'Coagulant is dispersed through the water.'},
{id:'floc',name:'Flocculation',short:'Flocculation',description:'Gentle mixing and anionic polymer help small particles gather into larger floc.',result:'Larger floc forms for settling.'},
{id:'settle',name:'Sedimentation',short:'Sedimentation',description:'Floc settles below the clarified water. Water leaves from the upper outlet; sludge goes to thickening and drying beds.',result:'Most settleable solids leave the main water stream.'},
{id:'ozone',name:'Ozone contact',short:'Ozone contact',illustrative:true,description:'Ozone contacts the settled water before GAC filtration. This contactor is a retained conceptual addition.',result:'Ozone contact occurs before the filter.'},
{id:'filter',name:'GAC filtration',short:'GAC filtration',description:'GAC traps floc carried over from sedimentation. Nonionic polymer is added before filtration; effluent flows to the pump station.',result:'Filtered water is collected for pumping.'},
{id:'blend',name:'Pumping and blending',short:'Blending',river:'Filtered water is pumped to the finished-water system. Blending requires compatible water; the groundwater bypass applies only to the groundwater route.',groundwater:'Pumped filter effluent blends with groundwater bypass to meet the finished-water arsenic target.',result:'The combined stream proceeds to final treatment.'},
{id:'final',name:'Final chemical addition',short:'Final treatment',description:'Sodium hypochlorite is added for final disinfection. Phosphate is added for corrosion control in the distribution system.',result:'Final chlorine addition precedes chlorine contact.'},
{id:'contact',name:'Chlorine contact',short:'Chlorine contact',illustrative:true,description:'After final chlorination, water follows the contact channel before delivery. This basin is a retained conceptual addition.',result:'Chlorine contact is downstream of final chlorination.'},
{id:'tap',name:'Pumping and delivery',short:'Delivery',river:'Finished water is conveyed through the planned finished-water pumping and distribution system to customers.',groundwater:'Finished water leaves for Canutillo Booster Station and onward through the distribution system to customers.',result:'Water reaches the distribution system.'}
];
const positions={pretreat:[260,140],rapid:[420,140],floc:[580,140],settle:[740,140],ozone:[900,140],filter:[900,350],blend:[740,350],final:[580,350],contact:[420,350],tap:[260,350]};
const esc=t=>String(t).replace(/&/g,'&').replace(/''+esc(label)+'';
// Uploaded vessel artwork plus equipment-specific vector symbols. Text is a caption, not the process block.
function equipment(id,source,x,y,w=126,h=88){
let body='';
const assets={rapid:'./equipment/mixing-tank.png',floc:'./equipment/floc-tank.png',filter:'./equipment/media-filter.png'};
const tank='';
const dose='';
if(assets[id])body='';
else if(id==='intake'&&source==='river')body='';
else if(id==='intake')body='';
else if(id==='pretreat'||id==='final')body=tank+dose+''+(id==='final'?'':'');
else if(id==='settle')body='';
else if(id==='ozone')body=tank+''+[[45,59],[59,58],[74,70],[86,54],[100,68],[119,53],[63,43],[105,42]].map(([a,b])=>'').join('')+'';
else if(id==='contact')body=tank+'';
else if(id==='blend')body='';
else if(id==='tap')body='';
return '';
}
const captions={river:['River intake'],groundwater:['Groundwater wells'],pretreat:['Pre-oxidation'],rapid:['Rapid mix'],floc:['Flocculation'],settle:['Sedimentation'],ozone:['Ozone contact*'],filter:['GAC filter'],blend:['Pumping & blending'],final:['Final chemicals'],contact:['Chlorine contact*'],tap:['Delivery']};
function schematicNode(id,x,y,number,selected,source,sourceId,mobile){
const current=id==='intake'?selected==='intake'&&sourceId===source:selected===id,label=captions[sourceId||id];
return ''+equipment(id,sourceId||source,x,y,mobile?132:126,mobile?86:82)+txt(number,x-(sourceId?40:58),y-36,mobile?10:9,current?'#87621e':'#637d78')+label.map((t,i)=>txt(t,x,y+(mobile?54:53)+i*16,mobile?14:14,current?'#71521d':'#2e5357')).join('')+'';
}
function map(index,source){
const selected=stages[index].id,parts=[];
parts.push('