### **TASK**

**Objective:** Create a Python-based web application using the Flask framework that functions as a sophisticated CSS merger. The primary goal is to intelligently merge CSS rules from duplicated selectors while perfectly preserving all other parts of the stylesheet.

**Core Functional Requirements:**

1.  **Scope-Aware Merging (Critical):** The application must understand CSS scope. Selectors should only be considered duplicates if they exist within the same context. The global scope is one context, and each `@-rule` block (like `@media`, `@supports`, `@keyframes`, or custom ones like `@include`) defines a new, distinct, nested scope. For example, `.foo` inside `@media (min-width: 768px)` is separate from `.foo` in any other scope. The merging logic must be applied recursively within each scope.

2.  **Property Merging Logic:** When duplicate selectors within the same scope are merged, their properties must be combined. The definition from the *last-appearing block* in the original text must take precedence for any given property, reflecting standard CSS cascading behavior.

3.  **Perfect Preservation of Untouched Code:** This is a strict requirement. Any part of the CSS that is *not* directly involved in a merge must be preserved **exactly as it is**. This includes:
    *   All `@-rules` and their content (the merging logic is applied *inside* them, but the `@-rule` itself is preserved).
    *   Any standard CSS rule (`selector {}`) that is unique within its scope.
    *   All comments, especially those outside of rule blocks (e.g., section headers).
    *   All original line breaks, blank lines, and whitespace between rules.
    *   The application **must not** reformat, "beautify," or otherwise alter any code it is not explicitly merging.

4.  **Formatting of Newly Merged Blocks:** Only the rule blocks that are newly generated from a merge should be formatted. The formatting must be precise:
    *   The selector (e.g., `.my-class`, `#my-id`) starts at the beginning of its line within the scope's indentation.
    *   A single space separates the selector from the opening brace `{`.
    *   Properties and any collected internal comments are on new lines, indented with two spaces.
    *   The block is closed with a `}` on a new line, at the same indentation level as the selector.
    *   If a merge results in an empty rule, it should be formatted on a single line: `.selector {}`.

5.  **Changelog Feature:**
    *   The application must have a "Changelog" section.
    *   This section should only display the final, newly formatted CSS rules that were the result of a merge.
    *   If multiple rules were merged, a thin horizontal line should separate them visually.
    *   If no duplicates were found, the changelog should display a message like: "No duplicate selectors found to merge."

**Web Interface and Technology:**

*   **Backend:** You are free to choose the best Python libraries and implementation strategy to achieve the functional requirements reliably. A robust, dedicated CSS parsing library is highly recommended over manual parsing with regular expressions to handle the complexities of CSS structure and scopes. The application must be built using the **Flask framework**.
*   **Frontend:** The application must use the **exact HTML template provided below without modification**. The template handles the layout (input/output columns, changelog), a modern dark-mode theme, and includes client-side JavaScript for a "Copy All" button. Your Flask backend should render this template, populating the `{{ input_css }}`, `{{ merged_css }}`, and `{{ changelog_list }}` variables.

**Provided HTML Template (`templates/index.html`):**

```html
<!DOCTYPE html>
<html lang="en"> <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Rule Merger - Dark Mode</title>
    <style>
        :root {
            --bg-color: #1e1e1e;
            --primary-text-color: #e0e0e0;
            --secondary-text-color: #b0b0b0;
            --accent-color: #007acc;
            --accent-hover-color: #005fa3;
            --border-color: #3a3a3a; 
            --input-bg-color: #2c2c2c;
            --pre-bg-color: #252525;
            --card-bg-color: #282828;
            --button-text-color: #ffffff;
            --success-color: #28a745;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: var(--bg-color);
            color: var(--primary-text-color);
            line-height: 1.6;
        }

        .main-container {
            max-width: 1400px;
            margin: 20px auto;
            padding: 25px;
            background-color: var(--card-bg-color);
            border-radius: 12px;
            box-shadow: 0 8px 16px rgba(0,0,0,0.2);
        }

        h1 {
            text-align: center;
            color: var(--primary-text-color);
            margin-bottom: 20px;
            font-weight: 600;
        }

        .columns-container {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            gap: 25px;
            margin-bottom: 20px;
        }

        .column {
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .column-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 8px;
            margin-bottom: 10px;
        }

        h2 {
            margin-top: 0;
            margin-bottom: 0;
            color: var(--secondary-text-color);
            font-size: 1.2em;
            font-weight: 500;
        }

        textarea, #merged_css_output {
            width: calc(100% - 30px);
            height: 400px;
            padding: 15px;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
            font-size: 14px;
            color: var(--primary-text-color);
            margin-bottom: 15px;
            overflow-y: auto;
        }

        textarea {
            background-color: var(--input-bg-color);
            resize: vertical;
        }

        #merged_css_output {
            background-color: var(--pre-bg-color);
            white-space: pre-wrap;
            word-wrap: break-word;
			margin: 0;
        }
        
        #changelog_output_container {
            background-color: var(--pre-bg-color);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            padding: 5px 15px 15px 15px;
            margin-top: 10px;
            height: auto;
            overflow-y: auto;
            font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
            font-size: 14px;
            color: var(--primary-text-color);
        }

        .changelog-entry {
            white-space: pre-wrap;
            word-wrap: break-word;
            margin: 0;
            padding-top: 10px;
        }

        hr.changelog-separator {
            border: none;
            height: 1px;
            background-color: var(--border-color);
            margin-top: 15px;
            margin-bottom: 5px;
        }


        .action-button {
            background-color: var(--accent-color);
            color: var(--button-text-color);
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: background-color 0.2s ease;
        }
        .action-button:hover {
            background-color: var(--accent-hover-color);
        }

        .submit-button {
            padding: 12px 25px;
            font-size: 16px;
            display: block;
            width: fit-content;
            margin: 0 auto;
        }

        #copy_merged_css_button {
            font-size: 0.9em;
            padding: 6px 12px;
        }

        @media (max-width: 768px) {
            .columns-container {
                flex-direction: column;
            }
            textarea, #merged_css_output {
                height: 300px;
            }
            #changelog_output_container {
                height: auto;
            }
            h1 {
                font-size: 1.8em;
            }
        }
    </style>
</head>
<body>
    <div class="main-container">
        <h1>CSS Rule Merger</h1>

        <form method="POST" action="/">
            <div class="columns-container">
                <div class="column">
                    <div class="column-header">
                        <h2>Input CSS:</h2>
                    </div>
                    <textarea name="input_css" placeholder="Paste your CSS here...">{{ input_css }}</textarea>
                </div>
                <div class="column">
                    <div class="column-header">
                        <h2>Merged CSS:</h2>
                        <button type="button" id="copy_merged_css_button" class="action-button">Copy All</button>
                    </div>
                    <pre id="merged_css_output">{{ merged_css }}</pre>
                </div>
            </div>
            <input type="submit" value="Merge CSS" class="action-button submit-button">
        </form>

        <div class="column-header" style="margin-top: 20px;">
             <h2>Changelog:</h2>
        </div>
        <div id="changelog_output_container">
            {% if changelog_list %}
                {% for entry in changelog_list %}
                    <pre class="changelog-entry">{{ entry }}</pre>
                    {% if not loop.last %}
                        <hr class="changelog-separator">
                    {% endif %}
                {% endfor %}
            {% else %}
                <pre class="changelog-entry">No changes to display.</pre> 
            {% endif %}
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const copyButton = document.getElementById('copy_merged_css_button');
            const mergedCssOutput = document.getElementById('merged_css_output');

            if (copyButton && mergedCssOutput) {
                copyButton.addEventListener('click', function() {
                    const textToCopy = mergedCssOutput.textContent || mergedCssOutput.innerText;
                    
                    if (!textToCopy.trim()) {
                        copyButton.textContent = 'Nothing to Copy';
                        setTimeout(() => {
                            copyButton.textContent = 'Copy All';
                        }, 2000);
                        return;
                    }

                    navigator.clipboard.writeText(textToCopy).then(function() {
                        copyButton.textContent = 'Copied!';
                        copyButton.style.backgroundColor = 'var(--success-color)';
                        setTimeout(function() {
                            copyButton.textContent = 'Copy All';
                            copyButton.style.backgroundColor = 'var(--accent-color)';
                        }, 2000);
                    }).catch(function(err) {
                        copyButton.textContent = 'Copy Failed';
                        console.error('Error copying text: ', err);
                        setTimeout(function() {
                            copyButton.textContent = 'Copy All';
                        }, 2000);
                    });
                });
            }
        });
    </script>
</body>
</html>
```