/* ============================================================
   PAGES — process & utility section mimics (interactive P&ID)
   ============================================================ */
const RX = React.createElement;
function gbox(x, y, w, h, label, key, col = "rgba(122,160,209,0.22)") {
  return RX("g", { key },
    RX("rect", { x, y, width: w, height: h, rx: 8, fill: "rgba(13,31,60,0.45)", stroke: col, strokeWidth: 1.2, strokeDasharray: "4 4" }),
    RX("text", { x: x + 10, y: y + 16, fill: "#647a98", style: { font: "700 9px var(--font-mono)", letterSpacing: "0.1em" } }, label)
  );
}
function gtxt(x, y, s, key, size = 9, col = "#647a98", anchor = "start") {
  return RX("text", { key, x, y, fill: col, textAnchor: anchor, style: { font: `600 ${size}px var(--font-mono)` } }, s);
}
function PanelWrap({ title, tag, icon = "factory", children, legend }) {
  return (
    <div className="panel" style={{ marginBottom: 14 }}>
      <div className="panel-h"><Icon n={icon} size={15} /><div className="pt">{title}{tag && <small>{tag}</small>}</div>{legend && <div className="right">{legend}</div>}</div>
      <div className="panel-b" style={{ padding: "10px 6px" }}>{children}</div>
    </div>
  );
}
function MiniLegend() {
  const { t } = useApp();
  return <div className="legend">
    <span className="lg"><span className="d" style={{ background: "#1e9e5a" }}></span>{t("st_open")}/{t("st_run")}</span>
    <span className="lg"><span className="d" style={{ background: "#5b6b7d" }}></span>{t("st_closed")}</span>
    <span className="lg"><span className="d" style={{ background: "#e8a317" }}></span>{t("st_warn")}</span>
    <span className="lg"><span className="d" style={{ background: "#d33a2c" }}></span>{t("st_alarm")}</span>
  </div>;
}
const D = id => window.devById(id);

/* ===== RECEPTION =========================================== */
function ReceptionPage() {
  const { t } = useApp();
  const W = 1140, H = 320;
  return (
    <div className="page">
      <PageHead titleKey="nav_reception" sub={t("reception_hall") + " · " + t("feed_hopper")} tag="AREA 10" />
      <PanelWrap title={t("nav_reception")} tag="P&ID 10" icon="inbox" legend={<MiniLegend />}>
        <Mimic w={W} h={H} svg={[
          gbox(36, 56, 250, 210, "RECEPTION HALL · 10", "b1"),
          gbox(900, 90, 210, 130, "TO FERMENTATION · 20", "b2", "rgba(242,148,31,0.3)"),
          ...pipe("M 160 232 V 250 H 880", "#7BA0D1", true, "rp1"),
          ...pipe("M 470 250 V 210", "#7BA0D1", true, "rp2"),
          ...pipe("M 600 250 V 200", "#7BA0D1", false, "rp3"),
          RX("g", { key: "hop" }, RX("path", { d: "M 120 110 H 200 L 185 200 H 135 Z", fill: "rgba(13,31,60,0.7)", stroke: "rgba(122,160,209,0.35)", strokeWidth: 1.4 }),
            RX("rect", { x: 137, y: 200 - 70 * D("10LT01").v / 100, width: 46, height: 70 * D("10LT01").v / 100, fill: "#3D8BE0", opacity: 0.3 })),
          gtxt(900, 145, "→ " + window.N_TUN + "× " + t("tunnel").toUpperCase(), "tx1", 10, "#caa05a"),
        ]}>
          <ISym d={D("10QT01")} x={250} y={92} />
          <ISym d={D("10LT01")} x={250} y={170} />
          <ISym d={D("10WT01")} x={90} y={290} />
          <MSym d={D("10MC01")} x={360} y={250} />
          <MSym d={D("10MX01")} x={470} y={210} />
          <MSym d={D("10MP01")} x={600} y={200} />
          <MSym d={D("10MP02")} x={680} y={250} />
          <VSym d={D("10EV02")} x={740} y={250} />
          <VSym d={D("10EV01")} x={840} y={250} />
        </Mimic>
      </PanelWrap>
      <DeviceTable devs={window.devByArea("rec")} />
    </div>
  );
}

/* ===== FERMENTATION TUNNELS ================================ */
function TunnelLane({ idx }) {
  const { t, open } = useApp();
  const n = String(idx).padStart(2, "0");
  const meta = D(`20TUN${n}`), T = D(`20TT${n}`), A = D(`20AT${n}`), P = D(`20PT${n}`);
  const phaseColor = { run: ["run", "#1e9e5a"], start: ["warn", "#e8a317"], empty: ["idle", "#5b6b7d"] }[meta.phase];
  const phaseLbl = { run: t("st_running"), start: t("st_filling"), empty: t("st_emptying") }[meta.phase];
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 14, padding: "8px 14px", borderBottom: "1px solid var(--hud-line-soft)" }}>
      <div style={{ width: 116, display: "flex", flexDirection: "column", gap: 4 }}>
        <span className="mono" style={{ fontWeight: 700, color: "#fff", fontSize: 14 }}>TF-0{n}</span>
        <span className="chip" style={{ background: phaseColor[1] + "22", color: phaseColor[1], alignSelf: "flex-start" }}><span className="d" style={{ background: phaseColor[1] }}></span>{phaseLbl}</span>
      </div>
      <div style={{ width: 64, fontSize: 11, color: "var(--hud-text-3)" }} className="mono">{t("hrt").slice(0, 3)} {meta.day}d</div>
      <div style={{ flex: 1, minWidth: 120 }}>
        <div style={{ height: 18, borderRadius: 4, background: "#13284a", border: "1px solid var(--hud-line)", overflow: "hidden", position: "relative" }}>
          <div style={{ height: "100%", width: meta.fill + "%", background: "linear-gradient(90deg," + phaseColor[1] + "44," + phaseColor[1] + "99)", transition: "width .4s" }}></div>
          <span className="mono" style={{ position: "absolute", right: 6, top: 1, fontSize: 11, color: "#dfeaf7" }}>{meta.fill}%</span>
        </div>
      </div>
      <ISym d={T} inline />
      <ISym d={A} inline />
      <ISym d={P} inline />
      <div style={{ width: 1, height: 36, background: "var(--hud-line)" }}></div>
      <div style={{ display: "flex", flexDirection: "column", alignItems: "center" }}><VSym d={D(`20GV${n}`)} inline lbl="GAS" /></div>
      <div style={{ display: "flex", flexDirection: "column", alignItems: "center" }}><VSym d={D(`20AV${n}`)} inline lbl="AIR" /></div>
      <div style={{ display: "flex", flexDirection: "column", alignItems: "center" }}><VSym d={D(`20SV${n}`)} inline lbl="H₂O" /></div>
    </div>
  );
}
function TunnelsPage() {
  const { t } = useApp();
  const tuns = window.devByArea("tun").filter(d => d.kind === "tunnel");
  const active = tuns.filter(x => x.phase === "run").length;
  const avgCh4 = window.MODEL.DEVICES.filter(d => /^20AT/.test(d.id)).reduce((s, d) => s + d.v, 0) / window.N_TUN;
  const avgT = window.MODEL.DEVICES.filter(d => /^20TT/.test(d.id)).reduce((s, d) => s + d.v, 0) / window.N_TUN;
  return (
    <div className="page">
      <PageHead titleKey="nav_tunnels" sub={t("digestion") + " · " + window.N_TUN + "× " + t("tunnel")} tag="AREA 20" />
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 12, marginBottom: 14 }}>
        <KPI icon="tunnel" label={t("active_tunnels")} value={active + " / " + window.N_TUN} accent />
        <KPI icon="gauge" label={"CH₄ ⌀"} value={avgCh4} dp={1} unit="%" />
        <KPI icon="thermo" label={t("digester_temp") + " ⌀"} value={avgT} dp={1} unit="°C" />
        <KPI icon="droplets" label={t("hrt")} value="28" unit="d" sub={<>{t("olr")} 3.2</>} />
      </div>
      <div className="panel">
        <div className="panel-h"><Icon n="tunnel" size={15} /><div className="pt">{t("nav_tunnels")}<small>20-TF-001…007</small></div>
          <div className="right"><div className="legend">
            <span className="lg"><span className="d" style={{ background: "#1e9e5a" }}></span>{t("st_running")}</span>
            <span className="lg"><span className="d" style={{ background: "#e8a317" }}></span>{t("st_filling")}</span>
            <span className="lg"><span className="d" style={{ background: "#5b6b7d" }}></span>{t("st_emptying")}</span>
          </div></div>
        </div>
        <div style={{ display: "flex", padding: "6px 14px", borderBottom: "1px solid var(--hud-line)", gap: 14 }}>
          <span className="eyebrow" style={{ width: 116 }}>{t("tunnel")}</span>
          <span className="eyebrow" style={{ width: 64 }}>{t("hrt").slice(0, 3)}</span>
          <span className="eyebrow" style={{ flex: 1 }}>{t("holder_fill")}</span>
          <span className="eyebrow" style={{ width: 156 }}>T · CH₄ · P</span>
          <span className="eyebrow" style={{ width: 200, textAlign: "right" }}>{t("ev_valve")} (EV)</span>
        </div>
        {tuns.map(tn => <TunnelLane key={tn.idx} idx={tn.idx} />)}
      </div>
    </div>
  );
}

/* ===== PERCOLATE =========================================== */
function PercolatePage() {
  const { t } = useApp();
  const W = 1140, H = 330;
  return (
    <div className="page">
      <PageHead titleKey="nav_percolate" sub={"PR1 · PR2 · " + t("sand_trap")} tag="AREA 30" />
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 12, marginBottom: 14 }}>
        <KPI icon="droplets" label={"PR1 " + t("holder_fill")} value={D("30LT01").v} dp={0} unit="%" sparkId="30LT01" />
        <KPI icon="droplets" label={"PR2 " + t("holder_fill")} value={D("30LT02").v} dp={0} unit="%" sparkId="30LT02" />
        <KPI icon="gauge" label={t("ph")} value={D("30QT01").v} dp={2} sparkId="30QT01" />
        <KPI icon="activity" label={t("flow_rate")} value={D("30FT01").v} dp={1} unit="m³/h" sparkId="30FT01" accent />
      </div>
      <PanelWrap title={t("nav_percolate")} tag="P&ID 30" icon="droplets" legend={<MiniLegend />}>
        <Mimic w={W} h={H} svg={[
          gbox(820, 50, 290, 130, "← FROM TUNNELS · 20", "fb", "rgba(242,148,31,0.3)"),
          RX("g", { key: "pr1" }, ...[Tank({ x: 70, y: 120, w: 150, h: 150, level: D("30LT01").v, color: "#3D8BE0" })], gtxt(70, 112, "PR1 · 30-PR-001", "l1", 10)),
          RX("g", { key: "pr2" }, ...[Tank({ x: 270, y: 150, w: 130, h: 120, level: D("30LT02").v, color: "#3D8BE0" })], gtxt(270, 142, "PR2 · 30-PR-002", "l2", 10)),
          ...pipe("M 220 200 H 270", "#3D8BE0", true, "pe1"),
          ...pipe("M 400 200 H 520 V 150", "#3D8BE0", true, "pe2"),
          ...pipe("M 620 150 H 980 V 180", "#3D8BE0", true, "pe3"),
          ...pipe("M 820 110 H 540 V 200 H 400", "#3D8BE0", true, "pe4"),
          gtxt(840, 105, "→ " + t("tunnel") + " spray", "tx", 9, "#6E97C4"),
        ]}>
          <ISym d={D("30LT01")} x={145} y={100} />
          <ISym d={D("30LT02")} x={335} y={130} />
          <ISym d={D("30TT01")} x={150} y={250} />
          <ISym d={D("30QT01")} x={250} y={250} />
          <MSym d={D("30MX01")} x={145} y={195} />
          <MSym d={D("30MP01")} x={520} y={230} />
          <MSym d={D("30MP02")} x={600} y={230} />
          <MSym d={D("30MP03")} x={680} y={230} />
          <VSym d={D("30EV01")} x={820} y={150} />
          <ISym d={D("30FT01")} x={720} y={150} />
          <VSym d={D("30EV02")} x={460} y={200} />
          <VSym d={D("30EV03")} x={400} y={290} />
        </Mimic>
      </PanelWrap>
      <DeviceTable devs={window.devByArea("per")} />
    </div>
  );
}

/* ===== BIOGAS STORAGE & TREATMENT ========================== */
function BiogasPage() {
  const { t } = useApp();
  const W = 1140, H = 360;
  const fill = D("40LT01").v;
  return (
    <div className="page">
      <PageHead titleKey="nav_biogas" sub={t("gas_holder") + " · " + t("desulph") + " · " + t("flare")} tag="AREA 40" />
      <div style={{ display: "grid", gridTemplateColumns: "repeat(5,1fr)", gap: 12, marginBottom: 14 }}>
        <KPI icon="database" label={t("holder_fill")} value={fill} dp={0} unit="%" accent sparkId="40LT01" />
        <KPI icon="gauge" label={t("ch4")} value={D("40AT01").v} dp={1} unit="%" sparkId="40AT01" />
        <KPI icon="activity" label={t("h2s")} value={D("40AT03").v} dp={0} unit="ppm" sparkId="40AT03" />
        <KPI icon="flame" label={t("gas_flow")} value={D("40FT01").v} dp={0} unit="Nm³/h" sparkId="40FT01" />
        <KPI icon="gauge" label={t("gas_press")} value={D("40PT01").v} dp={1} unit="mbar" sparkId="40PT01" />
      </div>
      <PanelWrap title={t("nav_biogas")} tag="P&ID 40" icon="flame" legend={<MiniLegend />}>
        <Mimic w={W} h={H} svg={[
          gbox(28, 60, 150, 120, "← TUNNELS 20", "fb", "rgba(242,148,31,0.3)"),
          // gas holder dome
          RX("path", { key: "domeb", d: "M 250 230 A 130 130 0 0 1 510 230 Z", fill: "rgba(13,31,60,0.6)", stroke: "rgba(122,160,209,0.4)", strokeWidth: 1.5 }),
          RX("clipPath", { key: "dc", id: "domeclip" }, RX("path", { d: "M 250 230 A 130 130 0 0 1 510 230 Z" })),
          RX("rect", { key: "domef", x: 250, y: 230 - 128 * fill / 100, width: 260, height: 128 * fill / 100, fill: "#2A9DE0", opacity: 0.3, clipPath: "url(#domeclip)" }),
          RX("line", { key: "doml", x1: 250, y1: 230 - 128 * fill / 100, x2: 510, y2: 230 - 128 * fill / 100, stroke: "#2A9DE0", strokeWidth: 1.5, opacity: 0.7, clipPath: "url(#domeclip)" }),
          gtxt(330, 250, t("gas_holder").toUpperCase() + " · 40-GH-001", "gl", 10, "#8fb4e6"),
          // pipes
          ...pipe("M 178 150 H 230 V 220", "#F2941F", true, "g1", true),
          ...pipe("M 510 200 H 620", "#F2941F", true, "g2", true),
          ...pipe("M 700 200 H 1000", "#F2941F", true, "g3", true),
          ...pipe("M 760 200 V 90", "#F2941F", false, "g4", true),
          gbox(940, 150, 170, 110, "TO CHP · 50", "tc", "rgba(242,148,31,0.3)"),
          gtxt(940, 80, t("flare").toUpperCase(), "fl", 9, "#f08478"),
          RX("path", { key: "flame", d: "M 760 78 q -6 -10 0 -18 q 4 6 8 0 q 4 10 -8 18", fill: "#f08478", opacity: 0.8 }),
        ]}>
          <VSym d={D("40EV01")} x={230} y={200} lbl="IN" />
          <VSym d={D("40EV02")} x={560} y={200} lbl="OUT" />
          <ISym d={D("40LT01")} x={380} y={120} />
          <ISym d={D("40PT01")} x={470} y={150} />
          <MSym d={D("40MB01")} x={650} y={200} />
          <VSym d={D("40EV04")} x={650} y={140} lbl="O₂" />
          <ISym d={D("40FT01")} x={840} y={200} />
          <VSym d={D("40EV03")} x={760} y={130} lbl="FLR" />
          {/* analyzer cluster */}
          <ISym d={D("40AT01")} x={760} y={280} />
          <ISym d={D("40AT02")} x={830} y={280} />
          <ISym d={D("40AT03")} x={900} y={280} />
          <ISym d={D("40AT04")} x={970} y={280} />
          {/* condensate */}
          <MSym d={D("40MP01")} x={460} y={300} />
          <VSym d={D("40EV05")} x={380} y={300} lbl="DRN" />
        </Mimic>
      </PanelWrap>
      <DeviceTable devs={window.devByArea("gas")} />
    </div>
  );
}

/* ===== COGENERATION (CHP) ================================== */
function ChpPage() {
  const { t } = useApp();
  const W = 1140, H = 320;
  const gen = D("50GG01");
  return (
    <div className="page">
      <PageHead titleKey="nav_chp" sub={t("chp_engine") + " · 600 kWe"} tag="AREA 50" />
      <div style={{ display: "grid", gridTemplateColumns: "repeat(5,1fr)", gap: 12, marginBottom: 14 }}>
        <KPI icon="zap" label={t("el_power")} value={D("50JT01").v} dp={0} unit="kW" accent sparkId="50JT01" />
        <KPI icon="thermo" label={t("th_power")} value={D("50JT02").v} dp={0} unit="kW" />
        <KPI icon="activity" label={t("speed")} value={D("50ST01").v} dp={0} unit="rpm" />
        <KPI icon="gauge" label={"T " + t("co2").slice(0, 3) + "."} value={D("50TT01").v} dp={0} unit="°C" sub={t("temp_winding")} />
        <KPI icon="battery" label={t("op_hours")} value={gen.runH} dp={0} unit="h" />
      </div>
      <PanelWrap title={t("nav_chp")} tag="P&ID 50" icon="zap" legend={<MiniLegend />}>
        <Mimic w={W} h={H} svg={[
          gbox(24, 110, 130, 110, "← BIOGAS 40", "fb", "rgba(242,148,31,0.3)"),
          ...pipe("M 154 165 H 520", "#F2941F", true, "c1", true),
          // genset block
          RX("rect", { key: "gs", x: 520, y: 100, width: 230, height: 150, rx: 8, fill: "rgba(19,49,92,0.6)", stroke: gen.running ? "rgba(30,158,90,0.5)" : "rgba(122,160,209,0.3)", strokeWidth: 1.6 }),
          gtxt(534, 122, "GG265 · " + t("chp_engine").toUpperCase(), "gl", 10, "#8fb4e6"),
          gtxt(534, 240, "JMS 312 · 600 kWe / 636 kWt", "gl2", 9, "#647a98"),
          // outputs
          ...pipe("M 750 150 H 950", "#5fd99a", true, "c2"),
          ...pipe("M 750 200 H 950", "#d33a2c", true, "c3"),
          gbox(950, 110, 165, 50, "→ " + t("grid_export"), "ge", "rgba(95,217,154,0.4)"),
          gbox(950, 175, 165, 50, "→ " + t("nav_heat"), "gh", "rgba(211,58,44,0.4)"),
        ]}>
          <VSym d={D("50EV02")} x={300} y={165} lbl="ESD" />
          <VSym d={D("50EV01")} x={430} y={165} lbl="GAS" />
          <MSym d={gen} x={635} y={175} glyph="G" />
          <ISym d={D("50JT01")} x={860} y={120} />
          <ISym d={D("50JT02")} x={860} y={245} />
          <ISym d={D("50TT01")} x={560} y={290} />
          <ISym d={D("50TT02")} x={640} y={290} />
          <ISym d={D("50TT03")} x={720} y={290} />
          <ISym d={D("50ST01")} x={635} y={120} />
          <VSym d={D("50EV03")} x={800} y={250} lbl="DUMP" />
        </Mimic>
      </PanelWrap>
      <DeviceTable devs={window.devByArea("chp")} />
    </div>
  );
}

/* ===== HEAT RECOVERY ======================================= */
function HeatPage() {
  const { t } = useApp();
  const W = 1140, H = 300;
  return (
    <div className="page">
      <PageHead titleKey="nav_heat" sub={t("heat_exch") + " · " + t("digestion")} tag="AREA 60" />
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 12, marginBottom: 14 }}>
        <KPI icon="thermo" label={"↑ " + t("digester_temp").slice(0, 10)} value={D("60TT01").v} dp={1} unit="°C" accent sparkId="60TT01" />
        <KPI icon="thermo" label={"↓ " + t("digester_temp").slice(0, 10)} value={D("60TT02").v} dp={1} unit="°C" />
        <KPI icon="activity" label={t("flow_rate")} value={D("60FT01").v} dp={1} unit="m³/h" sparkId="60FT01" />
        <KPI icon="gauge" label={t("gas_press")} value={D("60PT01").v} dp={2} unit="bar" />
      </div>
      <PanelWrap title={t("nav_heat")} tag="P&ID 60" icon="thermo" legend={<MiniLegend />}>
        <Mimic w={W} h={H} svg={[
          gbox(24, 80, 130, 90, "← CHP 50", "fb", "rgba(211,58,44,0.4)"),
          RX("rect", { key: "hx", x: 300, y: 90, width: 90, height: 130, rx: 6, fill: "rgba(19,49,92,0.6)", stroke: "rgba(122,160,209,0.4)", strokeWidth: 1.5 }),
          gtxt(296, 82, t("heat_exch").toUpperCase() + " 60-HE-001", "hl", 9),
          RX("path", { key: "hxz", d: "M 312 110 h 66 M 312 130 h 66 M 312 150 h 66 M 312 170 h 66 M 312 190 h 66", stroke: "#d33a2c", strokeWidth: 1.4, opacity: 0.5 }),
          ...pipe("M 154 125 H 300", "#d33a2c", true, "h1"),
          ...pipe("M 390 125 H 560 V 160", "#f08478", true, "h2"),
          ...pipe("M 700 160 H 1000", "#f08478", true, "h3"),
          ...pipe("M 1000 220 H 300 V 220", "#6E97C4", true, "h4"),
          gbox(940, 110, 170, 110, "→ TUNNELS 20", "tt", "rgba(242,148,31,0.3)"),
        ]}>
          <ISym d={D("60TT01")} x={470} y={110} />
          <VSym d={D("60EV01")} x={560} y={160} lbl="MIX" />
          <MSym d={D("60MP01")} x={680} y={160} />
          <MSym d={D("60MP02")} x={760} y={160} />
          <ISym d={D("60FT01")} x={850} y={120} />
          <ISym d={D("60PT01")} x={850} y={210} />
          <ISym d={D("60TT02")} x={470} y={250} />
          <VSym d={D("60EV02")} x={250} y={125} lbl="ISO" />
        </Mimic>
      </PanelWrap>
      <DeviceTable devs={window.devByArea("heat")} />
    </div>
  );
}

/* ===== COMPRESSED AIR ====================================== */
function AirPage() {
  const { t } = useApp();
  const W = 1140, H = 300;
  return (
    <div className="page">
      <PageHead titleKey="nav_air" sub={t("air_recv") + " · " + t("compressor")} tag="AREA 70" />
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 12, marginBottom: 14 }}>
        <KPI icon="gauge" label={t("gas_press")} value={D("70PT01").v} dp={2} unit="bar" accent sparkId="70PT01" />
        <KPI icon="thermo" label={t("temp_bearing").slice(0, 10)} value={D("70TT01").v} dp={1} unit="°C" />
        <KPI icon="wind" label={t("flow_rate")} value={D("70FT01").v} dp={1} unit="Nm³/h" sparkId="70FT01" />
        <KPI icon="cpu" label={t("compressor")} value={window.devByArea("air").filter(d => d.kind === "motor" && d.running).length + " / 2"} />
      </div>
      <PanelWrap title={t("nav_air")} tag="P&ID 70" icon="wind" legend={<MiniLegend />}>
        <Mimic w={W} h={H} svg={[
          ...pipe("M 180 130 H 420", "#6E97C4", true, "a1"),
          ...pipe("M 180 200 H 420", "#6E97C4", true, "a2"),
          ...[Tank({ x: 470, y: 90, w: 70, h: 150 })],
          gtxt(466, 82, t("air_recv").toUpperCase() + " 70-AR-001", "al", 9),
          ...pipe("M 540 165 H 640", "#6E97C4", true, "a3"),
          RX("rect", { key: "dry", x: 640, y: 130, width: 80, height: 70, rx: 6, fill: "rgba(19,49,92,0.6)", stroke: "rgba(122,160,209,0.4)" }),
          gtxt(648, 170, "DRYER", "dl", 9, "#8fb4e6"),
          ...pipe("M 720 165 H 1000", "#6E97C4", true, "a4"),
          gbox(940, 120, 170, 90, "→ PLANT EV / INSTR.", "pl", "rgba(122,160,209,0.3)"),
        ]}>
          <MSym d={D("70MC01")} x={120} y={130} />
          <MSym d={D("70MC02")} x={120} y={200} />
          <ISym d={D("70PT01")} x={600} y={110} />
          <ISym d={D("70TT01")} x={680} y={250} />
          <ISym d={D("70FT01")} x={840} y={120} />
          <VSym d={D("70EV01")} x={780} y={165} lbl="DIST" />
          <VSym d={D("70EV02")} x={505} y={250} lbl="PRG" />
        </Mimic>
      </PanelWrap>
      <DeviceTable devs={window.devByArea("air")} />
    </div>
  );
}

/* ===== BIOFILTER =========================================== */
function BiofilterPage() {
  const { t } = useApp();
  const W = 1140, H = 300;
  return (
    <div className="page">
      <PageHead titleKey="nav_biofilter" sub={t("biofilter") + " · " + t("environment")} tag="AREA 80" />
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 12, marginBottom: 14 }}>
        <KPI icon="leaf" label={"H₂S " + t("st_ok")} value={D("80QT01").v} dp={1} unit="ppm" accent sparkId="80QT01" />
        <KPI icon="gauge" label={"Δp " + t("biofilter")} value={D("80PT01").v} dp={0} unit="Pa" sparkId="80PT01" />
        <KPI icon="droplets" label={t("temp_winding").slice(0, 4) + " " + t("biofilter").slice(0, 6)} value={D("80MT01").v} dp={1} unit="%" />
        <KPI icon="wind" label={t("fan")} value={window.devByArea("bio").filter(d => d.kind === "motor" && d.running).length + " / 2"} />
      </div>
      <PanelWrap title={t("nav_biofilter")} tag="P&ID 80" icon="leaf" legend={<MiniLegend />}>
        <Mimic w={W} h={H} svg={[
          gbox(24, 90, 120, 110, "← FOUL AIR", "fb", "rgba(122,160,209,0.3)"),
          ...pipe("M 144 145 H 320", "#9aa6b2", true, "bf1"),
          // biofilter bed
          RX("rect", { key: "bed", x: 470, y: 90, width: 260, height: 150, rx: 8, fill: "rgba(30,90,50,0.25)", stroke: "rgba(95,180,120,0.4)", strokeWidth: 1.5 }),
          gtxt(478, 110, t("biofilter").toUpperCase() + " 80-BF-001", "bl", 10, "#7fc99a"),
          RX("path", { key: "media", d: "M 480 220 q 20 -16 40 0 q 20 -16 40 0 q 20 -16 40 0 q 20 -16 40 0 q 20 -16 40 0 q 20 -16 40 0", stroke: "#5fb478", strokeWidth: 2, fill: "none", opacity: 0.5 }),
          ...pipe("M 400 145 H 470", "#9aa6b2", true, "bf2"),
          ...pipe("M 600 90 V 40 H 900", "#cfe0d6", true, "bf3"),
          gbox(880, 20, 130, 50, "→ STACK", "st", "rgba(95,180,120,0.4)"),
        ]}>
          <MSym d={D("80MF01")} x={360} y={130} />
          <MSym d={D("80MF02")} x={360} y={200} />
          <ISym d={D("80PT01")} x={440} y={250} />
          <ISym d={D("80MT01")} x={560} y={160} showVal />
          <ISym d={D("80TT01")} x={650} y={160} />
          <VSym d={D("80EV01")} x={560} y={100} lbl="IRR" />
          <ISym d={D("80QT01")} x={820} y={40} />
          <VSym d={D("80EV02")} x={720} y={145} lbl="BYP" />
        </Mimic>
      </PanelWrap>
      <DeviceTable devs={window.devByArea("bio")} />
    </div>
  );
}

window.PAGES = {
  overview: window.OverviewPage,
  params: window.ParamsPage, alarms: window.AlarmsPage, trends: window.TrendsPage,
  reception: ReceptionPage, tunnels: TunnelsPage, percolate: PercolatePage, biogas: BiogasPage,
  chp: ChpPage, heat: HeatPage, air: AirPage, biofilter: BiofilterPage,
};
