<?php
declare(strict_types=1);
$options=getopt('', ['target::']);
$root=rtrim((string)($options['target']??dirname(__DIR__)), "\\/");
function p(string $root,string $rel):string{return $root.DIRECTORY_SEPARATOR.str_replace(['/', '\\'],DIRECTORY_SEPARATOR,$rel);} 
function readf(string $path):string{$v=@file_get_contents($path);return $v===false?'':$v;}
$checks=[];$fails=[];
$ok=static function(bool $value,string $code)use(&$checks,&$fails):void{$checks[]=$code;if(!$value)$fails[]=$code;};
$required=[
 'app/Views/layouts/main.php','app/Views/diar_workspace/portal.php','app/Views/diar_workspace/module.php','app/Views/diar_command_center/cases.php',
 'public/assets/css/diar-premium-stabilization.css','public/assets/js/diar-premium-stabilization.js'
];
foreach($required as $rel){$ok(is_file(p($root,$rel)),'FILE_'.strtoupper(preg_replace('/[^A-Z0-9]+/i','_',$rel)));}
$layout=readf(p($root,'app/Views/layouts/main.php'));
$portal=readf(p($root,'app/Views/diar_workspace/portal.php'));
$module=readf(p($root,'app/Views/diar_workspace/module.php'));
$cases=readf(p($root,'app/Views/diar_command_center/cases.php'));
$css=readf(p($root,'public/assets/css/diar-premium-stabilization.css'));
$js=readf(p($root,'public/assets/js/diar-premium-stabilization.js'));
$ok(substr_count($layout,'DIAR_6B_2_R3_1_1_STABILIZATION_CSS')===1,'LAYOUT_CSS_MARKER');
$ok(substr_count($layout,'DIAR_6B_2_R3_1_1_STABILIZATION_JS')===1,'LAYOUT_JS_MARKER');
$ok(strpos($layout,'native-executive-sidebar.css')<strpos($layout,'diar-premium-stabilization.css'),'CSS_AFTER_NATIVE');
$ok(strpos($layout,'native-executive-sidebar.js')<strpos($layout,'diar-premium-stabilization.js'),'JS_AFTER_NATIVE');
$ok(strpos($portal,'diar-access-summary-card')!==false,'PORTAL_COMPACT_ACCESS');
$ok(strpos($portal,'diar-access-matrix-card')===false,'PORTAL_NO_FULL_MATRIX');
$ok(strpos($portal,'Independent hospital sources')!==false,'PORTAL_NONTECH_SOURCE_LABEL');
$ok(strpos($module,'DIAR-6B.2-R3.1')===false,'MODULE_NO_RELEASE_LABEL');
$ok(strpos($module,'DIAR-6B.6')===false&&strpos($module,'DIAR-6B.8')===false,'MODULE_NO_PHASE_CODES');
$ok(strpos($module,'diar-module-status')!==false,'MODULE_USER_STATUS');
$ok(strpos($cases,'DIAR-6B.2-R3.1')===false,'CASES_NO_RELEASE_LABEL');
$ok(strpos($cases,'DIAR-6B.3')===false,'CASES_NO_FUTURE_PHASE_CODE');
foreach([
 'position:fixed!important','overflow-y:auto!important','scrollbar-gutter:stable!important','native-sidebar-height-compact','native-sidebar-height-ultra',
 'overflow-x:clip!important','diar-premium-module-grid','grid-template-columns:repeat(2,minmax(0,1fr))','executive-kpi-strip','cases-table-wrap',
 'overflow-x:auto!important','@media (max-width:760px)','prefers-reduced-motion'
] as $needle){$ok(strpos($css,$needle)!==false,'CSS_'.strtoupper(preg_replace('/[^A-Z0-9]+/i','_',substr($needle,0,35))));}
$ok(strpos($css,'data-native-sidebar-page="diar-portal"')!==false,'PORTAL_TARGETED');
$ok(strpos($css,'data-native-sidebar-page="diar-executive-overview"')!==false,'EXEC_TARGETED');
$ok(strpos($css,'data-native-sidebar-page="diar-cases"')!==false,'CASES_TARGETED');
$ok(strpos($js,'sessionStorage')!==false,'JS_SCROLL_PERSISTENCE');
$ok(strpos($js,'scrollIntoView')!==false,'JS_ACTIVE_VISIBLE');
$ok(strpos($js,'data-diar-sidebar-stability')!==false,'JS_READY_CONTRACT');
$ok(strpos($js,'classList.remove')===false,'JS_NO_DENSITY_CLASS_THRASH');
$ok(strpos($css,'body[data-native-sidebar-page^="diar-"]')!==false,'DIAR_SCOPE_ONLY');
if($fails!==[]){fwrite(STDERR,'DIAR_6B_2_R3_1_1_STATIC_QA|FAIL|CHECKS='.count($checks).'|FAILED='.implode(',',$fails).PHP_EOL);exit(1);} 
echo 'DIAR_6B_2_R3_1_1_STATIC_QA|PASS|CHECKS='.count($checks).'|SIDEBAR_STABLE=PASS|OVERFLOW=PASS|HEADER=PASS|PORTAL=PASS|EXECUTIVE=PASS|CASES=PASS|RESPONSIVE=PASS'.PHP_EOL;
