import os
import re

css_file = r'd:\NIKKI\lhy\lhy.css'
html_file = r'd:\NIKKI\lhy\lhy.html'

with open(css_file, 'r', encoding='utf-8') as f:
    css = f.read()

# 1. Add variable and replace table backgrounds
var_def = ":root {\n    --table-bg-color: rgba(255, 255, 255, 1);\n}\n"
if ":root" not in css:
    css = var_def + css

css = css.replace("background-color: #ffffff;", "background-color: var(--table-bg-color);")
css = css.replace("background-color: #fff;", "background-color: var(--table-bg-color);")

# 2. Add new classes
new_classes = """
.plan-header-cell {
    vertical-align: top;
    position: relative;
}
.plan-header-flex {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 12px;
    margin-top: 8px;
}
.plan-header-flex .plan-title {
    margin-bottom: 0 !important;
}
.plan-icon-wrapper {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background-color: #eff6ff;
    color: #2563eb;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}
.plan-divider {
    height: 1px;
    background-color: #e2e8f0;
    width: 60%;
    margin: 0 auto 20px auto;
}
.plan-desc {
    color: #64748b;
    font-size: 0.85rem;
    margin-bottom: 24px;
    min-height: 40px;
    line-height: 1.4;
    font-weight: normal;
}
"""
if ".plan-header-cell" not in css:
    css += new_classes

with open(css_file, 'w', encoding='utf-8') as f:
    f.write(css)

with open(html_file, 'r', encoding='utf-8') as f:
    html = f.read()

# Replace inline styles in HTML
html = html.replace('<th style="vertical-align: top;">', '<th class="plan-header-cell">')
html = html.replace('<th style="vertical-align: top; position: relative;">', '<th class="plan-header-cell">')
html = html.replace('<div style="display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 12px; margin-top: 8px;">', '<div class="plan-header-flex">')
html = html.replace('<div style="width: 36px; height: 36px; border-radius: 50%; background-color: #eff6ff; color: #2563eb; display: flex; align-items: center; justify-content: center; font-size: 1rem;">', '<div class="plan-icon-wrapper">')
html = html.replace('<div class="plan-title" style="margin-bottom: 0;">', '<div class="plan-title">')
html = html.replace('<div style="height: 1px; background-color: #e2e8f0; width: 60%; margin: 0 auto 20px auto;"></div>', '<div class="plan-divider"></div>')
html = html.replace('<p style="color: #64748b; font-size: 0.85rem; margin-bottom: 24px; min-height: 40px; line-height: 1.4; font-weight: normal;">', '<p class="plan-desc">')

with open(html_file, 'w', encoding='utf-8') as f:
    f.write(html)
