import os
import webbrowser

def generate_rulebook():
    filename = "rulebook.html"
    html = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
    body { font-family: 'Segoe UI', Helvetica, Arial, sans-serif; line-height: 1.6; color: #2d3748; background: #f7fafc; margin: 0; padding: 0; }
    .page { max-width: 800px; margin: 40px auto; background: #fff; padding: 50px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); border-radius: 8px; box-sizing: border-box; }
    h1 { font-size: 28px; text-align: center; color: #1a202c; margin-bottom: 5px; text-transform: uppercase; letter-spacing: 1px; }
    .version { text-align: center; color: #718096; font-size: 14px; margin-bottom: 30px; font-weight: bold; }
    h2 { font-size: 20px; color: #2b6cb0; border-bottom: 2px solid #e2e8f0; padding-bottom: 5px; margin-top: 35px; text-transform: uppercase; }
    h3 { font-size: 16px; color: #4a5568; margin-top: 20px; }
    h4 { font-size: 14px; color: #2d3748; margin-bottom: 5px; margin-top: 15px; }
    p { margin: 0 0 15px 0; font-size: 15px; }
    ul { margin: 0 0 20px 0; padding-left: 20px; font-size: 15px; }
    li { margin-bottom: 8px; }
    .golden-rule { background: #fff5f5; border-left: 4px solid #e53e3e; padding: 15px; margin: 25px 0; border-radius: 4px; }
    .golden-rule h3 { color: #9b2c2c; margin-top: 0; margin-bottom: 5px; text-transform: uppercase; letter-spacing: 0.5px; }
    .golden-rule p { color: #742a2a; margin-bottom: 0; font-style: italic; font-weight: 500; }
    table { width: 100%; border-collapse: collapse; margin: 20px 0; font-size: 14px; }
    th { background: #edf2f7; text-align: left; padding: 10px; font-weight: bold; border-bottom: 2px solid #cbd5e0; }
    td { padding: 12px 10px; border-bottom: 1px solid #e2e8f0; vertical-align: top; }
    .dot { font-size: 16px; vertical-align: middle; margin-right: 4px; }
    @media print {
        body { background: #fff; }
        .page { margin: 0; padding: 20px; box-shadow: none; max-width: 100%; }
        * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
    }
</style>
</head>
<body>
<div class="page">
    <h1>🎲 DECK OF DEATH</h1>
    <div class="version">MASTER RULEBOOK — Version 2.2</div>
    
    <h2>📦 Game Overview</h2>
    <p><strong>Deck of Death</strong> is a competitive, high-chaos tabletop board game that blends strategic board control with intense social, physical, and wild group challenges. Players navigate a dual-ring circular board, seed the track with cards from their hands, complete tasks, and engage in high-stakes duels to rewrite the rules of the game in real-time.</p>
    
    <div class="golden-rule">
        <h3>💀 The Golden Rule</h3>
        <p>"There is no formal winner. The game does not conclude when a specific score is reached; instead, the session ends whenever the group collectively decides they have endured enough madness and utters the phrase: 'Yeah... that's enough chaos.'"</p>
    </div>

    <h2>📦 Components</h2>
    <ul>
        <li><strong>1 Dual-Ring Circular Board:</strong> Featuring an Outer Circle and an Inner Circle (containing exactly 10 spaces), connected by 5–8 distinct Arrow Spaces.</li>
        <li><strong>2 Six-Sided Dice</strong> (2d6)</li>
        <li><strong>Player Tokens</strong> (one per participant)</li>
        <li><strong>A Custom Deck of 190+ Cards</strong></li>
    </ul>

    <h2>🏁 Setup</h2>
    <ul>
        <li>All players place their chosen tokens on the <strong>Start</strong> space.</li>
        <li>Shuffle the main deck and deal a starting hand of <strong>7 cards</strong> to each player.</li>
        <li>Determine turn order based on clockwise seating position around the table.</li>
    </ul>

    <h2>🔄 Turn Structure</h2>
    <p>Each player's turn is broken into five distinct, sequential phases. You must resolve each phase entirely before proceeding to the next.</p>
    
    <h4>1. Draw Phase</h4>
    <p>Count the cards remaining in your hand and draw from the deck until you hold exactly <strong>7 cards</strong>. (Example: If you entered your turn with 3 cards, you must draw 4). <em>Note: There is no hand limit during other players' turns. If you acquire extra cards from duels or card mechanics, you hold them until your own End Phase.</em></p>
    
    <h4>2. Roll Phase</h4>
    <p>Roll both six-sided dice (2d6) and advance your token clockwise along the track by the total value rolled.</p>
    
    <h4>3. Challenge Phase</h4>
    <p>You must interact with and completely resolve the card currently occupying the space where your token lands.</p>
    
    <h4>4. Card Phase</h4>
    <p>You may only place or replace a card on the <strong>exact space your token currently occupies</strong>.</p>
    <ul>
        <li><strong>If the space is empty:</strong> You may place any card from your hand onto the space with no restrictions.</li>
        <li><strong>If the space is occupied:</strong> You must first successfully complete the challenge during the Challenge Phase. Once completed, you may choose whether or not you want to try and replace the existing card with one from your hand.</li>
    </ul>
    
    <h4>5. End Phase</h4>
    <p>You may choose to discard up to <strong>3 unwanted cards</strong> from your hand to the face-up discard pile. If you still hold more than 7 cards at this point, you must discard down to 7.</p>

    <h2>🚶 Movement & Track Rules</h2>
    <ul>
        <li><strong>Clockwise Flow & Laps:</strong> Movement always tracks clockwise around the board. Passing over or landing directly on the <strong>Start</strong> space completes a lap.</li>
        <li><strong>The Lap Penalty:</strong> A lap is officially completed only when a player moves <strong>clockwise</strong> past or lands exactly on the Start space during their Roll Phase. The moment this happens, momentum halts briefly: <strong>Everyone at the table must take a drink.</strong> Moving backward across Start due to a card punishment does not trigger a lap penalty.</li>
        <li><strong>Entering the Inner Circle:</strong> The inner ring contains exactly 10 spaces. Players cannot freely choose to enter it; they are cast into it by landing directly on an <strong>Arrow Space</strong> on the outer ring, or by failing specific card instructions.</li>
    </ul>

    <h2>🃏 The Card System & Board Control</h2>
    <ul>
        <li><strong>Single Occupancy:</strong> Each board space can hold exactly <strong>one card</strong> at a time.</li>
        <li><strong>Cards Over Spaces:</strong> Players always interact with the rules on the <em>card</em>, never the underlying physical board space. Spaces themselves have no ownership; cards belong strictly to the player who placed them.</li>
    </ul>

    <table>
        <thead>
            <tr>
                <th style="width: 25%">Card Type</th>
                <th>Behavioral Rules & Mechanics</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><strong style="color:#2B6CB0"><span class="dot">●</span>Blue</strong><br><small>Persistent Rules</small></td>
                <td>Environmental parameters (e.g., <em>No pointing</em>, <em>Speak in accents</em>). They <strong>remain active constantly</strong> while on the board, <strong>stack infinitely</strong> with no limit, and affect everyone.</td>
            </tr>
            <tr>
                <td><strong style="color:#DD6B20"><span class="dot">●</span>Orange</strong><br><small>Social</small></td>
                <td>Direct player interactions, dynamic voting, group tasks, or psychological prompts.</td>
            </tr>
            <tr>
                <td><strong style="color:#10B981"><span class="dot">●</span>Green</strong><br><small>Physical</small></td>
                <td>Coordination, movement, or endurance checks (e.g., <em>Balance items</em>, <em>Planks</em>, <em>Dance challenges</em>).</td>
            </tr>
            <tr>
                <td><strong style="color:#E53E3E"><span class="dot">●</span>Red</strong><br><small>Chaos</small></td>
                <td>Unpredictable wild cards, heavy drink penalties, spatial displacement, or swift tactical shifts.</td>
            </tr>
            <tr>
                <td><strong style="color:#718096"><span class="dot">●</span>White</strong><br><small>Rare Chaos</small></td>
                <td>Ultra-rare anomalies with zero operational restrictions. Can be played at <strong>any time</strong>.<br>
                • <strong>Immunity Card:</strong> Completely ignore all drinks, challenges, and card effects until your next turn.<br>
                • <strong>Rule Creator Card:</strong> Write a brand new, permanent rule directly onto a space.</td>
            </tr>
        </tbody>
    </table>

    <h4>📈 The Tier System</h4>
    <p>Cards within the Blue, Orange, Green, and Red decks are categorized into hierarchical <strong>Tiers (1 through 3)</strong> reflecting their severity and penalizing power. Higher tiers offer more devastating effects and are harder to cleanly replace.</p>

    <h2>🔁 Card Replacement & Upgrades</h2>
    <p>If you land on an occupied space and successfully complete its challenge, you earn the right to attempt a takeover during your Card Phase:</p>
    <ul>
        <li><strong>1. Free Upgrade (Clean Takeover):</strong> If you decide to replace the card with one of the <strong>exact same color but a higher Tier</strong>, you can replace it immediately with no issues. The old card is discarded, and your new card claims the space.</li>
        <li><strong>2. Conflict Replacement (Hostile Takeover):</strong> If the card you want to play is a <strong>lower Tier</strong> than the one on the board, or if you want to <strong>change the card color</strong> entirely, you must challenge the person who originally placed the card to a minigame duel.</li>
    </ul>

    <h3>⚔️ Minigame Duel System</h3>
    <ul>
        <li><strong>Defender’s Choice:</strong> The player who originally placed the card on the space is the Defender. They hold the ultimate right to select the style of combat. They can choose any quick, agreed-upon game (e.g., <em>Rock-Paper-Scissors</em>, <em>High-Die Roll</em>, <em>Trivia</em>, or a <em>Physical Test</em>).</li>
        <li><strong>Resolution:</strong> The winner of the minigame gets their card placed on the spot. The loser's card goes directly to the discard pile.</li>
        <li><strong>Tie-Breaker:</strong> In the event of a tie during a duel, the duel resets immediately. The Defender may choose a new mini-game or replay the current one until a clear winner is established.</li>
    </ul>

    <h3>🧱 The Full Board Lockout State</h3>
    <p>Once every outer space has been seeded with a card, players can no longer place cards into empty spaces. Any landed card interaction becomes entirely replacement-based, turning the board into a state of continuous, aggressive conflict.</p>

    <h2>⚡ Defend & React System (The Target Rule)</h2>
    <p>If you are explicitly targeted by an offensive or punishing card played or triggered by an opponent, you have one window to defend yourself. <strong>Only one reaction is permitted per challenge.</strong> Once a reaction card is played, the interaction ends immediately.</p>
    <ul>
        <li><strong>❌ Cancel:</strong> If you possess <strong>ANY card of the exact same Tier</strong> (regardless of color), you may discard it to cancel the challenge. The effect ends instantly, and your card goes to the discard pile.</li>
        <li><strong>🔄 Reflect:</strong> If you possess a card that matches the <strong>exact same Tier AND the exact same Color</strong>, you may discard it to reflect the punishment. The entire incoming effect bounces back and strikes the original player who targeted you. They take the full punishment and <em>cannot</em> play a reaction card to defend against the reflection. Both cards are then discarded.</li>
    </ul>

    <h2>🌀 Inner Circle Rules</h2>
    <p>Once sent inside via an outer ring Arrow Space, your token circles these 10 internal spaces indefinitely. Normal turn phases still apply inside, but you cannot return to the main outer board until you achieve one of two escape clauses:</p>
    <ul>
        <li><strong>Method 1 (Luck):</strong> During your Roll Phase, you must roll <strong>Snake Eyes (double 1s)</strong>. If successful, your token breaks containment and returns immediately to the outer ring.</li>
        <li><strong>Method 2 (Endurance):</strong> If you fail to roll Snake Eyes after <strong>5 consecutive turns</strong> inside, you may force your way out on your 6th turn. However, you must pay the baseline penalty: <strong>chug/finish your entire drink on the spot, or take 2 heavy shots</strong>, before advancing back to the Outer Circle.</li>
    </ul>
</div>
</body>
</html>"""
    with open(filename, "w", encoding="utf-8") as f:
        f.write(html)
    webbrowser.open('file://' + os.path.abspath(filename))

def generate_quick_ref():
    filename = "quick_reference.html"
    html = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
    body { font-family: 'Segoe UI', Helvetica, Arial, sans-serif; line-height: 1.5; color: #2d3748; background: #f7fafc; margin: 0; padding: 0; }
    .page { max-width: 750px; margin: 30px auto; background: #fff; padding: 40px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); border-radius: 8px; border: 2px solid #1a202c; box-sizing: border-box; }
    h1 { font-size: 24px; text-align: center; color: #1a202c; margin-top: 0; margin-bottom: 20px; border-bottom: 3px double #1a202c; padding-bottom: 10px; letter-spacing: 1px; }
    .section { margin-bottom: 22px; padding-bottom: 15px; border-bottom: 1px dashed #cbd5e0; }
    .section:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
    h2 { font-size: 15px; color: #2b6cb0; margin: 0 0 8px 0; text-transform: uppercase; letter-spacing: 0.5px; display: flex; align-items: center; }
    ul { margin: 0; padding-left: 18px; font-size: 14px; }
    li { margin-bottom: 5px; }
    li:last-child { margin-bottom: 0; }
    .footer { text-align: center; font-style: italic; font-size: 13px; color: #e53e3e; margin-top: 25px; font-weight: bold; background: #fff5f5; padding: 8px; border-radius: 4px; }
    @media print {
        body { background: #fff; }
        .page { margin: 0; padding: 15px; box-shadow: none; max-width: 100%; border: none; }
        * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
    }
</style>
</head>
<body>
<div class="page">
    <h1>⚡ DECK OF DEATH — QUICK REFERENCE GUIDE</h1>
    
    <div class="section">
        <h2>🔄 TURN PHASES</h2>
        <ul>
            <li><strong>1. DRAW:</strong> Top up your hand back to exactly <strong>7 cards</strong>. <em>(No hand limit during out-of-turn actions).</em></li>
            <li><strong>2. ROLL:</strong> Roll <strong>2d6</strong> and advance clockwise.</li>
            <li><strong>3. CHALLENGE:</strong> Intercept and resolve the card you landed on.</li>
            <li><strong>4. CARD:</strong> Place/Replace a card <strong>ONLY</strong> on your current space. <em>(See Placement Rules below).</em></li>
            <li><strong>5. END:</strong> Discard up to <strong>3 unwanted cards</strong>. Trim hand size back down to 7 if over.</li>
        </ul>
    </div>

    <div class="section">
        <h2>🚶 MOVEMENT & TRACK RULES</h2>
        <ul>
            <li><strong>Laps:</strong> Moving <strong>clockwise</strong> past or landing exactly on <strong>Start</strong> = <strong>EVERYONE AT THE TABLE DRINKS</strong>.</li>
            <li><strong>Going Backward:</strong> Reversing over Start due to card layout penalties does <strong>NOT</strong> trigger a lap drink.</li>
            <li><strong>The Inner Circle:</strong> Landing on an <strong>Arrow Space</strong> sucks your token into the 10-space inner penalty track loop.</li>
        </ul>
    </div>

    <div class="section">
        <h2>🃏 CARD PHASE PLACEMENT RULES</h2>
        <ul>
            <li><strong>Empty Space:</strong> Drop <strong>any card</strong> directly from your hand onto the space immediately. Zero restrictions.</li>
            <li><strong>Occupied Space:</strong> You <strong>MUST</strong> successfully pass the challenge first. If you survive, you choose one option:
                <br>• <strong>Free Upgrade:</strong> Same Color + Higher Tier = Replace the card instantly with no issues.
                <br>• <strong>Conflict Takeover:</strong> Different Color OR Lower Tier = Triggers a <strong>DUEL</strong>.
            </li>
        </ul>
    </div>

    <div class="section">
        <h2>⚔️ MINIGAME DUELS (Conflict Takeovers)</h2>
        <ul>
            <li><strong>The Setup:</strong> Moving player is the Attacker; the player who originally seeded the space is the <strong>Defender</strong>.</li>
            <li><strong>The Choice:</strong> The <strong>Defender</strong> selects any quick, agreed-upon minigame <em>(e.g., Rock-Paper-Scissors, High-Die Roll, Trivia)</em>.</li>
            <li><strong>The Outcome:</strong> The winner's card claims the board space layout. The loser's card is sent directly to the discard pile.</li>
            <li><strong>Ties:</strong> Duel resets immediately. Defender can choose a brand new minigame or replay the tie.</li>
        </ul>
    </div>

    <div class="section">
        <h2>🧱 FULL BOARD LOCKOUT STATE</h2>
        <ul>
            <li>Once <strong>every single outer board space</strong> contains a card, empty placements are locked out permanently.</li>
            <li>Every subsequent space landing triggers a mandatory <strong>Challenge ➔ Upgrade/Conflict Duel</strong> cycle.</li>
        </ul>
    </div>

    <div class="section">
        <h2>⚡ DEFEND & REACT SYSTEM (The Target Rule)</h2>
        <ul>
            <li>If directly targeted by an opponent's card, you have <strong>one window</strong> to react. Max <strong>one reaction per challenge</strong>:</li>
            <li><strong>❌ CANCEL:</strong> Discard <strong>ANY card of the exact same Tier</strong> to completely vaporize the incoming effect.</li>
            <li><strong>🔄 REFLECT:</strong> Discard a card matching the <strong>exact Tier AND Color</strong> to bounce the action directly back onto the attacker. <em>The attacker cannot counter-react to a reflection.</em></li>
        </ul>
    </div>

    <div class="section">
        <h2>🌀 INNER CIRCLE ESCAPE CONDITIONS</h2>
        <ul>
            <li><strong>Method 1 (Luck):</strong> Roll <strong>Snake Eyes (Double 1s)</strong> during your Roll Phase to break containment immediately.</li>
            <li><strong>Method 2 (Endurance):</strong> Spend <strong>5 consecutive turns</strong> trapped inside. On your 6th turn, you can walk out automatically, but you must pay the exit fine: <strong>Finish your entire drink on the spot or take 2 heavy shots.</strong></li>
        </ul>
    </div>

    <div class="footer">
        💀 GAME OVER: When the table collectively agrees: "Yeah... that's enough chaos."
    </div>
</div>
</body>
</html>"""
    with open(filename, "w", encoding="utf-8") as f:
        f.write(html)
    webbrowser.open('file://' + os.path.abspath(filename))

if __name__ == "__main__":
    print("Generating Master Rulebook manual...")
    generate_rulebook()
    print("Generating Quick Reference guide sheet...")
    generate_quick_ref()
    print("Done! Both documents have been opened in your browser window. Press Ctrl+P to save them as clean PDFs.")
