import os
import webbrowser

# --- CARD DATA ---
CARD_DATA = [
    # BLUE - TIER 1
    {"color": "Blue", "tier": "1", "text": "No pointing. Anyone who points drinks."},
    {"color": "Blue", "tier": "1", "text": "Anyone who laughs at their own joke drinks."},
    {"color": "Blue", "tier": "1", "text": "Everyone must say “cheers” before drinking. If you dont, drink twice."},
    {"color": "Blue", "tier": "1", "text": "Anyone who rolls a 7 drinks."},
    {"color": "Blue", "tier": "1", "text": "Anyone who touches their face drinks."},
    {"color": "Blue", "tier": "1", "text": "Anyone who checks their phone drinks."},
    {"color": "Blue", "tier": "1", "text": "Anyone who says “yeah-nah” drinks."},
    {"color": "Blue", "tier": "1", "text": "Clap before each drink, if you don't add an extra drink."},
    {"color": "Blue", "tier": "1", "text": "Anyone who swears drinks."},
    {"color": "Blue", "tier": "1", "text": "Anyone who stands up drinks."},
    {"color": "Blue", "tier": "1", "text": "Anyone who rolls a double takes a shot."},
    {"color": "Blue", "tier": "1", "text": "Anyone who says “mate” takes a drink."},
    {"color": "Blue", "tier": "1", "text": "Anyone who asks a question drinks."},
    {"color": "Blue", "tier": "1", "text": "Anyone who says “_________________________________” drinks."},
    # BLUE - TIER 2
    {"color": "Blue", "tier": "2", "text": "You can't use anyones name if you do you drink."},
    {"color": "Blue", "tier": "2", "text": "Everyone must speak in an accent. Caught without an accent = SHOT!"},
    {"color": "Blue", "tier": "2", "text": "Anyone who rolls even must take a drink."},
    {"color": "Blue", "tier": "2", "text": "Anyone who forgets to say “thank you” after drinking drinks again."},
    {"color": "Blue", "tier": "2", "text": "Anyone who says “what” drinks twice."},
    {"color": "Blue", "tier": "2", "text": "Anyone who rolls under 5 drinks."},
    {"color": "Blue", "tier": "2", "text": "Anyone who speaks without raising their hand drinks."},
    {"color": "Blue", "tier": "2", "text": "Anyone who forgets to salute before speaking drinks."},
    {"color": "Blue", "tier": "2", "text": "Anyone who laughs must drink."},
    {"color": "Blue", "tier": "2", "text": "Anyone who touches the table drinks."},
    {"color": "Blue", "tier": "2", "text": "Anyone who says “I don’t know” drinks."},
    {"color": "Blue", "tier": "2", "text": "Anyone who rolls a 1 must compliment someone. Snake eyes compliment everyone at the table."},
    {"color": "Blue", "tier": "2", "text": "Anyone who forgets to speak in third person drinks."},
    {"color": "Blue", "tier": "2", "text": "Everyone chooses a word anyone who repeats another player’s word drinks."},
    {"color": "Blue", "tier": "2", "text": "Anyone who says “bro” drinks."},
    # BLUE - TIER 3
    {"color": "Blue", "tier": "3", "text": "Every time someone drinks, they must choose another player to drink with them."},
    {"color": "Blue", "tier": "3", "text": "Anyone who laughs drinks twice."},
    {"color": "Blue", "tier": "3", "text": "Anyone who stands up must finish their drink."},
    {"color": "Blue", "tier": "3", "text": "All drink punishments are doubles; the player of this card is immune to the rules."},
    {"color": "Blue", "tier": "3", "text": "Anyone who rolls doubles forces everyone else to drink."},
    {"color": "Blue", "tier": "3", "text": "Anyone who speaks must end their sentence with “your majesty”."},
    {"color": "Blue", "tier": "3", "text": "Anyone who touches their drink without having to drink, shots."},
    {"color": "Blue", "tier": "3", "text": "Anyone who speaks louder than a whisper drinks."},
    {"color": "Blue", "tier": "3", "text": "Anyone who makes eye contact with the player of this card drinks."},
    {"color": "Blue", "tier": "3", "text": "Anyone who rolls a 3 must swap spaces with someone."},
    {"color": "Blue", "tier": "3", "text": "Anyone who rolls a 10 must finish their drink."},
    {"color": "Blue", "tier": "3", "text": "When passing another player you both drink."},
    # GREEN - TIER 1
    {"color": "Green", "tier": "1", "text": "Flip a coin; call it. Wrong = drink."},
    {"color": "Green", "tier": "1", "text": "Say a tongue twister cleanly."},
    {"color": "Green", "tier": "1", "text": "Do 5 squats or drink."},
    {"color": "Green", "tier": "1", "text": "Choose a number between 1-12. Roll 2 dice and drink the difference."},
    {"color": "Green", "tier": "1", "text": "Touch your toes or drink."},
    {"color": "Green", "tier": "1", "text": "Balance a coin on your forehead for 5 seconds."},
    {"color": "Green", "tier": "1", "text": "Tell a joke; if no one laughs, SHOT."},
    {"color": "Green", "tier": "1", "text": "Do an animal impression. If no one can guess it you drink."},
    {"color": "Green", "tier": "1", "text": "Do a dramatic reading of your last text message."},
    {"color": "Green", "tier": "1", "text": "Do 10 star jumps or drink."},
    {"color": "Green", "tier": "1", "text": "Give someone a Tier 1 challenge card; if they complete it, you take a sip."},
    {"color": "Green", "tier": "1", "text": "Give a challenge card to the player on your right. If they fail it they take a drink. If you cant give them a challenge card you take a drink."},
    {"color": "Green", "tier": "1", "text": "Give someone a challenge card; if they fail, they drink."},
    {"color": "Green", "tier": "1", "text": "Give a challenge card to any player; if they succeed, they move forward 1. They must do the new space. If no card is on the new space they may place one down."},
    # GREEN - TIER 2
    {"color": "Green", "tier": "2", "text": "Stand on one foot until your next turn. If you fail, Shot!"},
    {"color": "Green", "tier": "2", "text": "Do 10 push-ups or drink twice."},
    {"color": "Green", "tier": "2", "text": "Recite a tongue twister three times fast."},
    {"color": "Green", "tier": "2", "text": "Hold a plank until the next player finishes their turn."},
    {"color": "Green", "tier": "2", "text": "Do a 10-second freestyle rap or drink."},
    {"color": "Green", "tier": "2", "text": "Say the alphabet backwards."},
    {"color": "Green", "tier": "2", "text": "Drink without using your hands. If you use your hands, Shot!"},
    {"color": "Green", "tier": "2", "text": "Do a 10-second plank on one arm or drink!"},
    {"color": "Green", "tier": "2", "text": "Do a dramatic monologue about something that's not supposed to be dramatic."},
    {"color": "Green", "tier": "2", "text": "Do a 10-second impression of someone at the table."},
    {"color": "Green", "tier": "2", "text": "Hold a squat for 20 seconds."},
    {"color": "Green", "tier": "2", "text": "Do a 10-second interpretive dance."},
    {"color": "Green", "tier": "2", "text": "Give someone a Tier 2 challenge card; if they fail, they drink twice. If you can't draw cards until you can."},
    {"color": "Green", "tier": "2", "text": "Give someone a challenge card; if they succeed, you drink."},
    {"color": "Green", "tier": "2", "text": "Give a challenge card to any player; if they refuse, they skip their next turn and take a shot."},
    # GREEN - TIER 3
    {"color": "Green", "tier": "3", "text": "Do an impression of a celebrity. Those who cant' guess it take a drink. If no one can guess it you take a shot."},
    {"color": "Green", "tier": "3", "text": "Hold a plank for a full round. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do 20 push-ups or drink twice."},
    {"color": "Green", "tier": "3", "text": "Balance on one foot for a full round. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do a 15-second freestyle rap about another player. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Hold your breath for a minute. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do a 10-second plank with one leg raised. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do 15 burpees. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do a 20-second interpretive dance. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Try to juggle three items for 10 seconds. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do a 10-second handstand. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do a 30-second squat hold. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Do a 20-second dramatic monologue. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Balance your drink on your head for 10 seconds. Fail = SHOT!"},
    {"color": "Green", "tier": "3", "text": "Give someone a Tier 3 challenge card; if they fail, they move back 3 spaces."},
    {"color": "Green", "tier": "3", "text": "Give a challenge card to any player; if they succeed, they steal one of your cards."},
    {"color": "Green", "tier": "3", "text": "Give someone a challenge card; if they refuse, they drink 4."},
    # ORANGE - TIER 1
    {"color": "Orange", "tier": "1", "text": "Pick someone to drink with you."},
    {"color": "Orange", "tier": "1", "text": "Swap drinks with someone for one turn."},
    {"color": "Orange", "tier": "1", "text": "Give someone a compliment or drink."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to move forward 1 space. They must do the new space. If no card on the new space they may place one down."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to move backward 1 space. They must do the new space. If no card on the new space they may place one down."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to take a drink."},
    {"color": "Orange", "tier": "1", "text": "Trade a card with someone."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to do a tiktok dance."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to do a 5-second plank."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to roll the dice; if they roll even, you drink. Odd they drink."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to swap seats with."},
    {"color": "Orange", "tier": "1", "text": "Choose someone they must copy everything you say until your next turn."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to take a drink every time you do until your next turn."},
    {"color": "Orange", "tier": "1", "text": "Choose someone to speak in an accent until their next turn. Fail = Shot"},
    {"color": "Orange", "tier": "1", "text": "Choose someone, everyone else has 30 seconds to make them laugh, if they laugh SHOT! If they don't everyone takes a drink"},
    # ORANGE - TIER 2
    {"color": "Orange", "tier": "2", "text": "Steal a random card from someone."},
    {"color": "Orange", "tier": "2", "text": "Swap positions with any player."},
    {"color": "Orange", "tier": "2", "text": "Nominate someone to do a Tier 1 or 2 challenge on the board."},
    {"color": "Orange", "tier": "2", "text": "Choose someone to drink twice."},
    {"color": "Orange", "tier": "2", "text": "Choose someone to skip their next turn."},
    {"color": "Orange", "tier": "2", "text": "Choose someone to roll; if they roll odd, they drink twice."},
    {"color": "Orange", "tier": "2", "text": "Choose someone to move forward 3 spaces. They must do the new space. If no card on the new space they may place one down."},
    {"color": "Orange", "tier": "2", "text": "Choose someone to move backward 3 spaces. They must do the new space. If no card on the new space they may place one down."},
    {"color": "Orange", "tier": "2", "text": "Choose someone to do a 10-second dance."},
    # ORANGE - TIER 3
    {"color": "Orange", "tier": "3", "text": "Pick a player; they must do the next challenge you land on."},
    {"color": "Orange", "tier": "3", "text": "Move any player forward or backward 5 spaces. They must do the new space. If no card on the new space they may place one down."},
    {"color": "Orange", "tier": "3", "text": "Choose someone to discard a tier 3, a tier 2 and a tier 1 card."},
    {"color": "Orange", "tier": "3", "text": "Choose someone to drink 3 times."},
    {"color": "Orange", "tier": "3", "text": "Choose someone they skip two turns."},
    {"color": "Orange", "tier": "3", "text": "Choose someone to swap positions with you."},
    {"color": "Orange", "tier": "3", "text": "Choose someone to drink every time you drink until your next turn."},
    {"color": "Orange", "tier": "3", "text": "Choose someone to roll; if they roll under 5, they drink 3."},
    {"color": "Orange", "tier": "3", "text": "Choose someone to move to the nearest red space. They must do the challenge."},
    {"color": "Orange", "tier": "3", "text": "Give someone a Tier 3 challenge card; if they fail, they shot 2."},
    {"color": "Orange", "tier": "3", "text": "Give a challenge card to any player; if they succeed, they move forward 3 spaces. They must do the new space. If no card is on the new space they may place one down."},
    {"color": "Orange", "tier": "3", "text": "Give someone a challenge card; if they refuse, they take 2 shots and lose their next two turns."},
    {"color": "Orange", "tier": "3", "text": "Give a challenge card to the player of your choice; if they complete it, you shot 2 times."},
    # RED - TIER 1
    {"color": "Red", "tier": "1", "text": "Take 2 drinks."},
    {"color": "Red", "tier": "1", "text": "Move back 1 space. You must do the new space. If no card on the new space they may place one down."},
    {"color": "Red", "tier": "1", "text": "Skip your next turn."},
    {"color": "Red", "tier": "1", "text": "Give a card to someone."},
    {"color": "Red", "tier": "1", "text": "Take a drink for every Blue rule currently active."},
    {"color": "Red", "tier": "1", "text": "Roll a die; take that many drinks."},
    {"color": "Red", "tier": "1", "text": "Take a drink every time someone rolls even until your next turn."},
    {"color": "Red", "tier": "1", "text": "Move to the nearest Blue space. You may replace that with another blue card from your hand."},
    {"color": "Red", "tier": "1", "text": "Take a drink every time someone says “mate”."},
    {"color": "Red", "tier": "1", "text": "Move back 2 spaces. You must do the new space. If no card on the new space you may place one down."},
    {"color": "Red", "tier": "1", "text": "Take a shot and compliment someone."},
    {"color": "Red", "tier": "1", "text": "Nominate another player. You both take a shot and swap spaces."},
    {"color": "Red", "tier": "1", "text": "Take a shot and roll again."},
    {"color": "Red", "tier": "1", "text": "Take a shot and skip your next roll."},
    {"color": "Red", "tier": "1", "text": "Take a shot and give someone else a shot."},
    {"color": "Red", "tier": "1", "text": "Take a shot and move backward 1 space. You must do the new space. If no card on the new space you may place one down."},
    {"color": "Red", "tier": "1", "text": "Give someone a Tier 1 challenge card from your hand; if you can’t, take 2 drinks."},
    {"color": "Red", "tier": "1", "text": "Choose a player and give them any Green challenge card from your hand; if you can’t, take 2 drinks."},
    {"color": "Red", "tier": "1", "text": "Give a challenge card to the player on your left; if they complete it, you drink."},
    {"color": "Red", "tier": "1", "text": "Give someone a challenge card; if they refuse it, they take 2 shots."},
    # RED - TIER 2
    {"color": "Red", "tier": "2", "text": "Drink 3 times."},
    {"color": "Red", "tier": "2", "text": "Move back 3 spaces. You must do the new space. If no card on the new space you may place one down."},
    {"color": "Red", "tier": "2", "text": "Lose your next two turns."},
    {"color": "Red", "tier": "2", "text": "Discard your highest-tier card."},
    {"color": "Red", "tier": "2", "text": "Move to the nearest Red space. You must do the new space. If no card on the new space you may place one down."},
    {"color": "Red", "tier": "2", "text": "Drink every time someone rolls odd until your next turn."},
    {"color": "Red", "tier": "2", "text": "Move back to the start."},
    {"color": "Red", "tier": "2", "text": "Swap all your cards with someone."},
    {"color": "Red", "tier": "2", "text": "Take 2 drinks and move back 2 spaces. You must do the new space. If no card on the new space you may place one down."},
    {"color": "Red", "tier": "2", "text": "Take 3 drinks and skip your next turn."},
    {"color": "Red", "tier": "2", "text": "Choose another player, you both take 2 drinks."},
    {"color": "Red", "tier": "2", "text": "Move to the nearest Orange space. You must do the new space."},
    {"color": "Red", "tier": "2", "text": "Take 3 drinks and roll again."},
    {"color": "Red", "tier": "2", "text": "Take 3 drinks and swap seats with someone."},
    {"color": "Red", "tier": "2", "text": "Take 3 drinks and discard a random card."},
    {"color": "Red", "tier": "2", "text": "Give someone a Tier 2 challenge card; if they fail it, they take 3 drinks."},
    {"color": "Red", "tier": "2", "text": "Give a challenge card to any player; if they succeed, you move back 2 spaces. You must do the new space. If there is no card down you may put one down."},
    {"color": "Red", "tier": "2", "text": "Give someone a challenge card from your hand; if you have none, reveal your hand, draw 3 cards and drink."},
    {"color": "Red", "tier": "2", "text": "Give a challenge card to the player furthest ahead in the ring."},
    # RED - TIER 3
    {"color": "Red", "tier": "3", "text": "Finish your drink."},
    {"color": "Red", "tier": "3", "text": "Move to the inner ring."},
    {"color": "Red", "tier": "3", "text": "Lose your next three turns."},
    {"color": "Red", "tier": "3", "text": "Discard your entire hand."},
    {"color": "Red", "tier": "3", "text": "Drink every time ANYONE drinks until your next turn."},
    {"color": "Red", "tier": "3", "text": "Drink every time someone rolls doubles."},
    {"color": "Red", "tier": "3", "text": "Drink every time someone laughs."},
    {"color": "Red", "tier": "3", "text": "Take a drink for every 2 spaces someone moves until your next turn."},
    {"color": "Red", "tier": "3", "text": "Move to the nearest Red space and take 3 drinks."},
    {"color": "Red", "tier": "3", "text": "Take 2 shots and skip your next two turns."},
    {"color": "Red", "tier": "3", "text": "Take 2 shots and swap all your cards with someone."},
    {"color": "Red", "tier": "3", "text": "Take 3 drinks and move backward 5 spaces. You must do the new space. If no card on the new space you may place one down."},
    {"color": "Red", "tier": "3", "text": "Take 3 drinks and force someone else to drink 3."},
    {"color": "Red", "tier": "3", "text": "Take a shot and roll again; if you roll even, take another shot and roll again; keep going until you roll odds."},
    {"color": "Red", "tier": "3", "text": "Take 3 drinks and give someone a challenge from your hand. Discard it afterwards"},
    {"color": "Red", "tier": "3", "text": "Drink 3 and choose someone else to lose their next turn."},
    {"color": "Red", "tier": "3", "text": "Give someone a Tier 3 challenge card; if they fail, they finish their drink. If you can't you finish your drink."},
    {"color": "Red", "tier": "3", "text": "Give a challenge card to any player; if they succeed, you lose your next turn."},
    {"color": "Red", "tier": "3", "text": "Give someone a challenge card; if they refuse, they move to the inner ring."},
    {"color": "Red", "tier": "3", "text": "Give a challenge card to the player of your choice; if they complete it, you take 2 shots."},
    # WHITE - TIER 5 (Blanks & Special Rules)
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Play this at anytime. You don't take any drinks or shots until your next turn."},
    {"color": "White", "tier": "5", "text": "Make a Rule as you play this card on a space."},
    {"color": "White", "tier": "5", "text": "Make a Rule as you play this card on a space."},
    {"color": "White", "tier": "5", "text": "Make a Rule as you play this card on a space."},
    {"color": "White", "tier": "5", "text": "Make a Rule as you play this card on a space."},
    {"color": "White", "tier": "5", "text": "Make a Rule as you play this card on a space."}
]

# Style Colors mapping
PALETTE = {
    "Blue":   {"bg": "#F7FAFC", "border": "#2B6CB0", "header": "#1A365D", "text": "#2D3748"},
    "Green":  {"bg": "#F0FDF4", "border": "#10B981", "header": "#047857", "text": "#065F46"},
    "Orange": {"bg": "#FFFAF0", "border": "#DD6B20", "header": "#7B341E", "text": "#7B341E"},
    "Red":    {"bg": "#FFF5F5", "border": "#E53E3E", "header": "#742A2A", "text": "#742A2A"},
    "White":  {"bg": "#FFFFFF", "border": "#CBD5E0", "header": "#4A5568", "text": "#1A202C"}
}

def generate_html():
    html_filename = "drinking_game_cards.html"
    
    html_content = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
    body { margin: 0; padding: 0; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background: #e2e8f0; }
    
    /* A4 Page Formatting Layout Setup */
    .page {
        width: 210mm;
        height: 297mm;
        margin: 0 auto;
        padding-top: 16.5mm; /* Dynamic grid perfect-centering calculation */
        padding-left: 10.5mm;
        background: white;
        box-sizing: border-box;
        page-break-after: always;
        position: relative;
    }
    
    .grid {
        display: grid;
        grid-template-columns: repeat(3, 63mm);
        grid-template-rows: repeat(3, 88mm);
        gap: 0px;
    }

    /* Standard Card Architecture Details */
    .card {
        width: 63mm;
        height: 88mm;
        box-sizing: border-box;
        position: relative;
        overflow: hidden;
        background: #fff;
        border: 0.5px dashed #cbd5e0; /* Cutting guide boundaries */
    }

    .card-inner {
        position: absolute;
        top: 1mm; left: 1mm; right: 1mm; bottom: 1mm;
        border-radius: 3px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
    }

    .card-header {
        height: 16mm;
        padding: 0 4mm;
        display: flex;
        justify-content: space-between;
        align-items: center;
        color: white;
    }

    .card-title { font-size: 11px; font-weight: bold; letter-spacing: 0.5px; }
    .card-tier { font-size: 10px; font-weight: bold; opacity: 0.9; }

    .card-body {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 4mm 5mm;
        text-align: center;
        font-size: 12.5px;
        line-height: 1.4;
        font-weight: 500;
    }

    /* Print Driver Override Styles */
    @media print {
        body { background: none; }
        .page { margin: 0; box-shadow: none; page-break-after: always; }
        html, body { width: 210mm; height: 297mm; }
        * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
    }
</style>
</head>
<body>
"""

    cards_per_page = 9
    for i in range(0, len(CARD_DATA), cards_per_page):
        html_content += '  <div class="page">\n    <div class="grid">\n'
        
        page_cards = CARD_DATA[i:i+cards_per_page]
        for card in page_cards:
            p = PALETTE.get(card["color"], PALETTE["White"])
            html_content += f"""      <div class="card">
        <div class="card-inner" style="background: {p['bg']}; border: 1.5px solid {p['border']};">
          <div class="card-header" style="background: {p['header']};">
            <span class="card-title">{card['color'].upper()} DECK</span>
            <span class="card-tier">TIER {card['tier']}</span>
          </div>
          <div class="card-body" style="color: {p['text']};">
            {card['text']}
          </div>
        </div>
      </div>\n"""
      
        html_content += '    </div>\n  </div>\n'

    html_content += """</body>
</html>"""

    with open(html_filename, "w", encoding="utf-8") as f:
        f.write(html_content)
        
    print(f"Success! Webcard system safely compiled layout to: '{html_filename}'")
    
    # Safely open browser locally
    try:
        url = 'file://' + os.path.abspath(html_filename)
        webbrowser.open(url)
    except Exception:
        pass

if __name__ == "__main__":
    generate_html()