from pathlib import Path
import json
root=Path(__file__).resolve().parents[1]
schema=(root/'work/survey_schema.json').read_text()
page='''<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Sparsh | Mobile survey concept</title>
<style>
:root{--purple:#482454;--orange:#d55b14;--ink:#252238;--muted:#655f70;--line:#ded6e4;--pale:#f5f1f7}*{box-sizing:border-box}body{margin:0;background:#f2eef4;font:16px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;color:var(--ink)}button,input,select,textarea{font:inherit}button{cursor:pointer}button:focus-visible,input:focus-visible,select:focus-visible,textarea:focus-visible,canvas:focus-visible{outline:3px solid #ce611e;outline-offset:3px}header{padding:32px max(24px,calc((100vw - 1120px)/2));background:var(--purple);color:#fff}header p{max-width:740px;color:#e5dbea;margin:8px 0 0}.eyebrow{font-size:12px;font-weight:700;letter-spacing:2px;color:#ffc392}h1{font-size:30px;line-height:1.2;margin:12px 0}main{max-width:1120px;margin:32px auto;padding:0 24px;display:grid;grid-template-columns:250px minmax(0,430px) 1fr;gap:24px;align-items:start}aside h2{font-size:14px;margin:0 0 14px}nav{display:grid;gap:6px}nav button{border:0;background:transparent;color:var(--muted);text-align:left;padding:10px;border-radius:10px;min-height:44px;font-size:14px}nav button.active{color:var(--purple);background:#e8deed;font-weight:700}nav span{display:inline-block;width:26px;color:var(--purple)}.device{background:#fff;border:1px solid #cfc0d6;border-radius:30px;box-shadow:0 20px 55px #48245412;overflow:hidden}.status{display:flex;justify-content:space-between;padding:14px 22px 0;font-size:12px;font-weight:700}.appbrand{display:flex;align-items:center;justify-content:space-between;padding:17px 22px 12px}.wordmark{font-size:24px;font-weight:750;color:var(--purple)}.appbrand small{font-size:10px;letter-spacing:1px;color:var(--muted)}.progress{height:4px;margin:0 22px;background:#eee7f1}.progress div{height:4px;background:var(--orange)}.content{padding:20px 22px 12px;min-height:410px}.step{font-size:11px;letter-spacing:1.5px;color:var(--purple);font-weight:700}h2{font-size:24px;line-height:1.25;margin:10px 0}p.subtitle{color:var(--muted);font-size:14px;margin:8px 0 22px}.field{margin:0 0 21px}.label,legend{display:block;font-weight:600;font-size:15px;margin:0 0 9px;padding:0}fieldset{border:0;padding:0;min-width:0}input:not([type=checkbox]):not([type=radio]),textarea,select{width:100%;min-height:48px;padding:11px 12px;background:#fbf9fc;border:1px solid var(--line);border-radius:10px;color:var(--ink)}textarea{resize:vertical;min-height:88px}.choice{display:flex;align-items:center;gap:12px;min-height:46px;padding:9px 12px;border:1px solid var(--line);border-radius:10px;margin:7px 0;font-size:15px}.choice:has(input:checked){background:#f0e8f4;border-color:var(--purple)}input[type=checkbox],input[type=radio]{accent-color:var(--purple);width:19px;height:19px;flex-shrink:0}.yesno{display:flex;gap:10px}.yesno .choice{flex:1}.hint{font-size:12px;color:var(--muted);margin:7px 0}.actions{display:flex;gap:10px;border-top:1px solid #eee7f1;padding:18px 22px 22px}.actions button{min-height:48px;border-radius:10px;padding:10px 20px;border:1px solid var(--line);color:var(--purple);background:#fff}.actions .primary{flex:1;background:var(--purple);color:#fff;border-color:var(--purple);font-weight:600}.actions button:disabled{opacity:.4;cursor:default}.note{border-top:3px solid var(--orange);padding-top:12px;font-size:14px;color:var(--muted)}.note h2{font-size:15px;color:var(--ink)}.note p{margin:10px 0 20px}.pill{display:inline-block;padding:4px 10px;border-radius:20px;background:#e8deed;color:var(--purple);font-size:12px}canvas{width:100%;height:130px;border:1px dashed #a38bac;border-radius:10px;touch-action:none;background:#fbf9fc}.clear{background:transparent;border:0;color:var(--purple);padding:8px 0;min-height:44px}.review{border-bottom:1px solid var(--line);padding:14px 0}.reviewhead{display:flex;justify-content:space-between;gap:15px}.reviewhead strong{font-size:15px}.edit{border:0;background:transparent;color:var(--purple);text-decoration:underline;min-height:44px}.review p{font-size:13px;margin:8px 0;overflow-wrap:anywhere}.review small{color:var(--muted)}#notice{font-size:14px;padding:0 22px 16px;color:var(--purple)}[hidden]{display:none!important}.declaration{border-left:3px solid var(--orange);padding:0 0 0 14px;margin:0 0 24px;font-size:15px}.ref{color:var(--muted);font-size:11px;margin-left:6px;font-weight:400}@media(max-width:920px){main{grid-template-columns:220px minmax(0,430px);justify-content:center}.notes{grid-column:1/-1;max-width:680px}}@media(max-width:650px){header{padding:22px}h1{font-size:25px}main{display:block;margin:20px auto;padding:0 12px;max-width:454px}aside{margin:0 10px 16px}nav{grid-template-columns:1fr 1fr}nav button{font-size:12px;padding:6px}aside h2{display:none}.notes{margin:25px 12px}.device{border-radius:22px}.content{padding:18px}.actions{padding:16px 18px}}
</style></head><body>
<header><div class="eyebrow">SENSE INTERNATIONAL INDIA · SPARSH CENTRE, SANAND</div><h1>A clearer way to capture every story.</h1><p>Mobile survey concept · English working copy · All source fields included</p></header>
<main><aside><h2>Survey journey</h2><nav aria-label="Survey sections" id="nav"></nav></aside>
<section class="device" aria-label="Mobile application preview"><div class="status"><span>9:41</span><span>4G · 100%</span></div><div class="appbrand"><span class="wordmark">Sparsh</span><small>FIELD SURVEY</small></div><div class="progress" aria-label="Survey position"><div id="progress"></div></div><div class="content" id="content"></div><div class="actions"><button id="back" type="button">Back</button><button id="next" type="button" class="primary">Continue</button></div><div id="notice" role="status" aria-live="polite"></div></section>
<aside class="notes"><div class="note"><span class="pill">Interactive design review</span><h2>Source-complete</h2><p>40 fields, all disability choices, eight document checks, the declaration and both signatures.</p><h2>Try a follow-up</h2><p>Choose “Other” for disability, or “Yes” for family disability, seizures, treatment or assistive devices.</p><h2>Demonstration only</h2><p>Use fictional information. Entries remain in this open page only and disappear on refresh. There is no server, account, saved draft or real submission.</p><h2>Pending programme decisions</h2><p>Required fields, age/date-of-birth rules, income currency, adult applicability and accessible alternatives to signing still need agreement.</p></div></aside></main>
<script>
const sections=__SCHEMA__;
const declaration="I declare that the information provided above is correct to the best of my knowledge and has been provided with my consent.";
let current=0;const values={};const signatures={};
const content=document.getElementById('content'),nav=document.getElementById('nav'),notice=document.getElementById('notice');
function el(tag,cls,txt){let n=document.createElement(tag);if(cls)n.className=cls;if(txt!==undefined)n.textContent=txt;return n;}
function isVisible(f){if(!f.show)return true;const v=values[f.show[0]];return Array.isArray(v)?v.includes(f.show[1]):v===f.show[1];}
function updateVisibility(){sections.flatMap(s=>s.fields).forEach(f=>{const n=document.getElementById('wrap-'+f.id);if(n)n.hidden=!isVisible(f);});}
function labelFor(f){return f.label;}
function addField(f){let box=el('div','field');box.id='wrap-'+f.id;box.hidden=!isVisible(f);let legend;
if(['multi','radio'].includes(f.kind)){box=el('fieldset','field');box.id='wrap-'+f.id;box.hidden=!isVisible(f);legend=el('legend','label',labelFor(f));}
else legend=el('label','label',labelFor(f));
if(f.kind!=='check'){if(legend.tagName==='LABEL')legend.htmlFor=f.id;box.append(legend);}
const changed=v=>{values[f.id]=v;updateVisibility();};
if(f.kind==='radio'||f.kind==='multi'){
const group=el('div',f.kind==='radio'?'yesno':'');f.options.forEach((o,i)=>{let l=el('label','choice'),inp=el('input');inp.type=f.kind==='multi'?'checkbox':'radio';inp.name=f.id;inp.value=o;inp.id=f.id+'-'+i;inp.checked=f.kind==='multi'?(values[f.id]||[]).includes(o):values[f.id]===o;inp.addEventListener('change',()=>{if(f.kind==='radio')changed(o);else{const set=new Set(values[f.id]||[]);inp.checked?set.add(o):set.delete(o);changed([...set]);}});l.append(inp,document.createTextNode(o));group.append(l);});box.append(group);
}else if(f.kind==='check'){let l=el('label','choice'),inp=el('input');inp.type='checkbox';inp.id=f.id;inp.checked=!!values[f.id];inp.addEventListener('change',()=>changed(inp.checked));l.append(inp,document.createTextNode(f.label));box.append(l);
}else if(f.kind==='signature'){
let cv=el('canvas');cv.id=f.id;cv.width=700;cv.height=260;cv.setAttribute('aria-label',f.label+' drawing area');box.append(cv,el('p','hint','Draw with a finger or pointer. Accessible signing alternatives require programme approval.'));
let clear=el('button','clear','Clear signature');clear.type='button';clear.onclick=()=>{cv.getContext('2d').clearRect(0,0,700,260);delete signatures[f.id];delete values[f.id];};box.append(clear);
requestAnimationFrame(()=>{let ctx=cv.getContext('2d');ctx.strokeStyle='#482454';ctx.lineWidth=3;ctx.lineCap='round';if(signatures[f.id]){let im=new Image();im.onload=()=>ctx.drawImage(im,0,0);im.src=signatures[f.id];}let down=false;function point(e){let r=cv.getBoundingClientRect();return[(e.clientX-r.left)*700/r.width,(e.clientY-r.top)*260/r.height];}cv.onpointerdown=e=>{down=true;cv.setPointerCapture(e.pointerId);ctx.beginPath();ctx.moveTo(...point(e));};cv.onpointermove=e=>{if(!down)return;ctx.lineTo(...point(e));ctx.stroke();values[f.id]='Signature captured';};function end(){if(down){down=false;if(values[f.id])signatures[f.id]=cv.toDataURL();}}cv.onpointerup=end;cv.onpointercancel=end;});
}else{let input=el(f.kind==='select'?'select':f.kind==='textarea'?'textarea':'input');input.id=f.id;
if(f.kind==='select'){input.append(new Option('Select an option',''));f.options.forEach(o=>input.append(new Option(o,o)));}else if(f.kind!=='textarea'){input.type=['date','number','tel'].includes(f.kind)?f.kind:'text';if(f.kind==='number'){input.min='0';input.step=f.id==='F14'?'any':'1';}}input.value=values[f.id]||'';input.oninput=()=>changed(input.value);box.append(input);}
content.append(box);
}
function review(){content.append(el('p','subtitle','Review your entries. Blank fields are marked “Not entered”; required-field rules are pending approval.'));
sections.forEach((s,i)=>{let block=el('section','review'),head=el('div','reviewhead'),edit=el('button','edit','Edit');edit.type='button';edit.onclick=()=>{current=i;render();};head.append(el('strong','',s.title),edit);block.append(head);s.fields.filter(isVisible).forEach(f=>{let v=values[f.id];if(f.kind==='check')v=v?'Available':'Not selected';if(Array.isArray(v))v=v.join(', ');let p=el('p');p.append(el('small','',f.label+': '),document.createTextNode(v||'Not entered'));block.append(p);});content.append(block);});}
function render(){notice.textContent='';content.replaceChildren();nav.replaceChildren();[...sections,{title:'Review & submit'}].forEach((s,i)=>{let b=el('button',i===current?'active':'');b.type='button';b.append(el('span','',String(i+1).padStart(2,'0')),document.createTextNode(s.title));if(i===current)b.setAttribute('aria-current','step');b.onclick=()=>{current=i;render();};nav.append(b);});
document.getElementById('progress').style.width=((current+1)/11*100)+'%';content.append(el('div','step',`STEP ${current+1} OF 11`));let heading=el('h2','',current===10?'Review & submit':sections[current].title);content.append(heading);
if(current<10){content.append(el('p','subtitle',sections[current].subtitle));if(current===9)content.append(el('p','declaration',declaration));sections[current].fields.forEach(addField);}else review();
document.getElementById('back').disabled=current===0;document.getElementById('next').textContent=current===10?'Finish demo':'Continue';}
document.getElementById('back').onclick=()=>{if(current>0){current--;render();}};
document.getElementById('next').onclick=()=>{if(current<10){current++;render();}else{notice.textContent='Demo complete. No information was submitted or saved outside this page.';}};
render();
</script></body></html>'''
(root/'output/mockup/Sparsh_Mobile_Survey.html').write_text(page.replace('__SCHEMA__',schema))
print('Interactive mockup generated')
