from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import FancyBboxPatch, FancyArrowPatch
from xml.etree import ElementTree as ET

ROOT=Path(__file__).resolve().parent
plt.rcParams.update({'font.family':'DejaVu Sans','svg.fonttype':'none','font.size':16})
fig,ax=plt.subplots(figsize=(3.6,17.0),dpi=200)
fig.subplots_adjust(0,0,1,1)
fig.patch.set_facecolor('#0b142a');ax.set_facecolor('#0b142a')
ax.set_xlim(0,360);ax.set_ylim(1700,0);ax.axis('off')

def text(x,y,s,size=18,color='#eaf0ff',weight='normal',ha='left'):
 return ax.text(x,y,s,fontsize=size*.72,color=color,weight=weight,ha=ha,va='top',linespacing=1.4)
def box(y,title,body):
 ax.add_patch(FancyBboxPatch((16,y),328,195,boxstyle='round,pad=0,rounding_size=10',facecolor='#172642',edgecolor='#91a9dc',linewidth=1))
 text(30,y+18,title,20,weight='bold');text(30,y+82,body,18)
text(18,25,'INHERITANCE',17,'#c3acff',weight='bold')
text(18,63,'Four observations\nto distinguish.',27,weight='bold')
text(18,152,'A change in one place does not\nestablish what caused a change\nin another.',18)
boxes=[(257,'1  Environmental\n    input','What changed around\nthe organism? Record the\nconditions and their timing.'),(523,'2  Individual\n    response','What changed in that\nindividual? A response is\nnot proof of inheritance.'),(789,'3  Cellular\n    change','What changed in cells?\nSpecify the observable\nand its location.'),(1055,'4  Outcome in\n    descendants','What appears in later\ngenerations? Similarity alone\ndoes not identify its cause.')]
for y,title,body in boxes:box(y,title,body)
for y in [452,718,984]:
 ax.add_patch(FancyArrowPatch((70,y+12),(70,y+58),arrowstyle='-|>',mutation_scale=12,color='#c3acff',linewidth=1.6,linestyle=(0,(4,4))))
 text(96,y+24,'Link to test',18,'#dac9ff')
ax.add_patch(FancyBboxPatch((16,1285),328,248,boxstyle='round,pad=0,rounding_size=10',facecolor='#26233f',edgecolor='#c3acff',linewidth=1))
text(30,1305,'Other explanations\nremain possible',20,weight='bold')
text(30,1370,'• Shared environments can\n  produce similarity.\n• Learning can pass between\n  generations.\n• Existing genetic differences\n  can contribute.',17)
text(18,1560,'Dashed arrows need evidence.\nThey are not an established\ncausal chain.',18,weight='bold')
text(18,1650,'Conceptual diagram\nNo biological protocol',17,'#b7c7e4')
fig.savefig(ROOT/'evidence-chain.svg',facecolor=fig.get_facecolor(),metadata={'Title':'Inheritance: four observations to distinguish','Description':'Four distinct observations with dashed proposed causal links; shared environment, learning and genetic differences are alternatives. No biological procedure.'})
fig.savefig(ROOT/'evidence-chain.png',facecolor=fig.get_facecolor(),dpi=160)
# Accessible SVG metadata, independent of renderer-specific metadata.
p=ROOT/'evidence-chain.svg'; tree=ET.parse(p); root=tree.getroot();ns='{http://www.w3.org/2000/svg}'
root.set('role','img');root.set('aria-labelledby','diagram-title diagram-desc')
t=ET.Element(ns+'title',{'id':'diagram-title'});t.text='Inheritance: four observations to distinguish'
d=ET.Element(ns+'desc',{'id':'diagram-desc'});d.text='Environmental input, individual response, cellular change and outcome in descendants are separate observations. Dashed arrows mark proposed links requiring evidence, not established causation. Shared environment, learning and existing genetic differences are alternative explanations.'
root.insert(0,d);root.insert(0,t);tree.write(p,encoding='utf-8',xml_declaration=True)
print('Saved mobile-first SVG and PNG; logical width360, minimum font17; at330px minimum15.58px.')
