
    ji%              	          S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  S SK
Jr  \R                  R                  SS5      r\(       d  \" S5        \R                  R                  SS	5      r\R"                  " \S
9rSrS\S\4S jrS\S\4S jrS\S\S\S\4S jrSS\S\S\S\4S jjr\S:X  a	  \" S5        gg)    N)Path)BeautifulSoup)async_playwrightOPENAI_API_KEY zJWarning: OPENAI_API_KEY not found. Asset generation will use placeholders.GEMINI_API_KEY'AIzaSyCT_rx0NusUJWoqtT7uxHAKEfHo129SJb8)api_keya  ROLE: You are the Chief Creative Officer (CCO) at an Awwwards-winning digital agency. Your job is to elevate functional B2B/B2C landing pages into breathtaking, immersive experiences.

CONTEXT: You are reviewing a generated website draft. You are provided with:
1. A high-res full-page screenshot of the rendered page.
2. The raw HTML/Tailwind CSS string.

CRITERIA FOR PREMIUM POLISH:
1. Spatial Rhythm & Glassmorphism: Ensure blur layers (`backdrop-filter`) have correct opacity, gradients have dramatic contrast, and typography features massive scale differences.
2. Kinematics (Motion): Static pages are dead. Identify elements requiring scroll-triggered fade-ups, staggered reveals, or parallax depth.
3. Rich Media: Identify sterile color blocks (like the Hero) that require cinematic looping background videos, authentic lifestyle photography, or bespoke 3D assets.

YOUR TASK: 
Do NOT rewrite the HTML. Output a strict JSON object mapping structural enhancements, requested rich media, and specific CSS/attribute injections. 

EXPECTED JSON SCHEMA:
{
  "visual_score": 85, 
  "critique_summary": "Hero feels flat. Needs cinematic background video and a stagger-fade-up on the typography.",
  "asset_requests": [
    {
      "target_selector": "#hero-section", 
      "type": "video_background | image_background | 3d_floating_asset", 
      "search_query": "cinematic dark moody corporate architecture loop",
      "overlay_class": "bg-black/60 backdrop-blur-sm"
    }
  ],
  "dom_mutations": [
    {
      "target_selector": "h1.main-title", 
      "action": "add_classes", 
      "value": "opacity-0 translate-y-10 data-animate-reveal delay-200"
    }
  ],
  "injected_styles": "/* Complex @keyframes or custom CSS variables to append to <style> */"
}
html_contentreturnc                   #    [        5        ISh  vN nUR                  R                  SS9I Sh  vN nUR                  SSS.S9I Sh  vN nUR	                  U SS	9I Sh  vN   UR                  S
5      I Sh  vN   UR                  SS9I Sh  vN nUR                  5       I Sh  vN   UsSSS5      ISh  vN   $  N N N{ Ne NN N9 N# N! , ISh  vN  (       d  f       g= f7f)z@Uses headless browser to render the DOM for Multimodal Critique.NT)headlessi  i  )widthheight)viewportnetworkidle)
wait_untili  )	full_page)r   chromiumlaunchnew_pageset_contentwait_for_timeout
screenshotclose)r   pbrowserpager   s        0e:\genesis-system\scripts\visual_polish_agent.pyrender_dom_to_imager    8   s     !!Q

))4)88%%/M%NN|FFF##D)))??T?::
mmo "!!8NF): "!!!s   C.CC.CCCCC$C
%C=C>CCC,C-C2C.>C?C.CC
CCCCC.C+CC+'C.asset_requestc           	         #    [        SU S    SU R                  SS5       S35        U S   S:X  a  gU S   S	:X  a  g
U S   S:X  a  gg7f)z/Mock implementation for the Media Buyer Engine.z    [Media Buyer] Sourcing typez for query: 'search_queryr   'video_backgroundzJhttps://videos.pexels.com/video-files/853889/853889-hd_1920_1080_25fps.mp4image_backgroundz]https://images.unsplash.com/photo-1584622650111-993a426fbf0a?auto=format&fit=crop&q=80&w=20003d_floating_assetz5https://ui-avatars.com/api/?name=3D&background=random)printget)r!   s    r   fetch_rich_mediar+   D   sl     	'f(='>mML]L]^lnpLqKrrs
tu V 22[	v	"4	4n	v	"5	5Fs   AApatchassetsc           	        #    [        U S5      nUR                  S/ 5       H  n UR                  US   5      nU(       a  UR                  S/ 5      nSU;  a
  USS/-   US'   US   S:X  aH  S	X$S       S
UR                  SS5       S3n[        US5      R                  nUR	                  SU5        M  US   S:X  aH  SX$S       SUR                  SS5       S3n	[        U	S5      R                  n
UR	                  SU
5        M  M  M     UR                  S/ 5       Hk  n UR                  US   5      nU HM  nUS   S:X  d  M  UR                  S/ 5      nUS   R                  5       n[        [        UU-   5      5      US'   MO     Mm     UR                  S5      (       aZ  UR                  S5      nUS   Ul        UR                  (       a  UR                  R                  U5        OUR	                  SU5        [        U5      $ ! [
         a-  n[        SUR                  SS5       SU 35         SnAGM  SnAff = f! [
         a-  n[        SUR                  SS5       SU 35         SnAGMV  SnAff = f7f)zGSafe DOM manipulation. BeautifulSoup naturally auto-closes broken tags.lxmlasset_requeststarget_selectorclassrelativezoverflow-hiddenr#   r&   z<div class="absolute inset-0 z-0 overflow-hidden"><video autoplay loop muted playsinline class="w-full h-full object-cover opacity-60"><source src="z8" type="video/mp4"></video><div class="absolute inset-0 overlay_classzbg-black/60z! mix-blend-multiply"></div></div>zhtml.parserr   r'   z<<div class="absolute inset-0 z-0 overflow-hidden"><img src="zM" class="w-full h-full object-cover opacity-60"><div class="absolute inset-0 z-    [AST Warning] Failed media injection for unknownz: Ndom_mutationsactionadd_classesvaluez,    [AST Warning] Failed class mutation for injected_stylesstyle)r   r*   
select_onedivinsert	Exceptionr)   selectsplitlistsetnew_tagstringheadappendstr)r   r,   r-   soupreqtargetexisting_classes
video_html	video_tagimg_htmlimg_tagemutationelementselexistingnew_classes	style_tags                     r   apply_ast_healing_and_mutationsrX   R   s    v.D yy)2.	p__S):%;<F#)::gr#: %55&6*FW9X&XF7Ov;"44 $x  y  DU  @V  yW  xX  XP  QT  QX  QX  Yh  jw  Qx  Py  yZ  "[J -j- H L LIMM!Y/[$66!]^dize{^|]}  ~K  LO  LS  LS  Tc  er  Ls  Kt  tU   VH+HmDHHGMM!W- 7  /* IIor2
	t{{8,=#>?HH%6!vvgr2H"*7"3"9"9";K"&s8k+A'B"CBwK  3 yy"##LL)	 !23	99IIY'KK9% t97  	pA#''J[]fBgAhhjkljmnoo	p  	t@N_ajAk@llnopnqrss	tsn   "I:BH1I:3AHI:  I AI A?I:
H=!H81I:8H==I: 
I7
!I2+I:2I77I:initial_htmlmax_iterationstarget_scorec                   #    U n[        U5       GH  n[        SUS-    SU S35        [        S5        [        U5      I S h  vN n[        S5        SSKJn  S	U S
3n [
        R                  R                  SUR                  SUR                  R                  USS9UR                  R                  US9/S9/UR                  [        SS9S9n[        R                  " UR                   5      n	U	R#                  SS5      n
[        SU
 S35        [        SU	R#                  S5       35        X:  a  [        S5          U$ U	R#                  S/ 5       Vs0 s H  oS   [%        U5      _M     nn0 nUR'                  5        H  u  pUI S h  vN X'   M     [        S5        [)        X9U5      I S h  vN nGM     U$  GNzs  snf  N8 N! [*         a  n[        SU 35         S nA  U$ S nAff = f7f)Nz'
--- [AIVPL] Starting Polish Iteration    /z ---z  [Capturing Render Reality...]z9  [Requesting Multimodal Critique from Gemini 3.1 Pro...]r   )typesz|Review the provided HTML and screenshot against the system instructions.
        
        RAW HTML:
        ```html
        z
        ```
        zgemini-3.1-pro-previewuserz	image/png)data	mime_type)text)rolepartszapplication/json)system_instructionresponse_mime_type)modelcontentsconfigvisual_scorez  Visual Score: z/100z  Critique: critique_summaryu6   ✨ Awwwards-Level aesthetics achieved. Breaking loop.r0   r1   z  [Applying AST Mutations...]z0  [Loop Error] Failed during critique/mutation: )ranger)   r    google.genair_   clientmodelsgenerate_contentContentPart
from_bytes	from_textGenerateContentConfigSYSTEM_PROMPT_CRITICjsonloadsrc   r*   r+   itemsrX   r?   )rY   rZ   r[   current_html	iterationscreenshot_bytesr_   promptresponse
patch_jsonscorerJ   asset_tasksfetched_assetsselectortaskrQ   s                    r   visual_polish_loopr      s'    L>*	8QqHXX\]^ 	/0!4\!BB 	IJ 	'	 
 		$	}}55.MMv

--3C{-[

,,&,96M  22';'9 3  6 H HMM2JNN>15E$UG401L0B!C DEF$NO   U_TbTbcsuwTxyTxS013CC3HHTxKyN"-"3"3"515:( #6 12!@[i!jjLk +v m CR z ,6 k 	DQCHI		sy   A G/F<G/CG>G/GF?-GG$G0G1G5G/?GG
G,G' G/'G,,G/__main__zAIVPL Core Module loaded.)   \   )asynciorx   osiopathlibr   bs4r   playwright.async_apir   rn   genaienvironr*   r   r)   r   Clientro   rw   rH   bytesr    dictr+   rX   intr   __name__     r   <module>r      s      	 	   1   0"5	
VW  02[\	n	-# J
C 
E 
$ 3 1 1D 1RV 1[^ 1f>3 > >WZ >dg >@ z	
%& r   