:root {
    --bg: #f8fafc;
    --container-bg: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --border: #cbd5e1;
    --success: #10b981;
    --counter-bg: #f1f5f9;
    --toolbar-bg: #f8fafc;
    --accent: #6366f1;
}

body.dark-theme {
    --bg: #0f172a;
    --container-bg: #1e293b;
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --primary: #818cf8;
    --primary-hover: #a5b4fc;
    --border: #334155;
    --counter-bg: #0f172a;
    --toolbar-bg: #1e293b;
    --success: #34d399;
}

/* --- Global Mobile Overflow Fix --- */
html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    overflow-x: hidden; /* Prevents side-scrolling */
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: var(--bg);
    color: var(--text-main);
    transition: background-color 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* --- Navigation --- */
nav {
    width: 100%;
    background: var(--container-bg);
    border-bottom: 1px solid var(--border);
    padding: 15px 0;
    display: flex;
    justify-content: center;
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-container {
    max-width: 850px;
    width: 90%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo {
    font-weight: 800;
    color: var(--primary);
    text-decoration: none;
    font-size: 1.2rem;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 20px;
}

/* --- Main Layout --- */
.container {
    max-width: 850px;
    width: 90%;
    background: var(--container-bg);
    padding: 40px;
    border-radius: 16px;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    margin: 40px 10px; /* Reduced side margin for mobile safety */
    position: relative;
    box-sizing: border-box; /* Crucial: keeps padding inside the width */
}

.theme-toggle {
    background: var(--border);
    border: none;
    padding: 6px 12px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 12px;
    color: var(--text-main);
    font-weight: 600;
}

header {
    text-align: center;
    margin-bottom: 30px;
}

h1 {
    margin-bottom: 10px;
    font-size: 2rem;
}

.description {
    color: var(--text-muted);
    margin-bottom: 25px;
}

/* --- Updated Toolbar (Mobile Overflow Fix) --- */
.toolbar {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 15px;
    background: var(--toolbar-bg);
    padding: 12px;
    border-radius: 12px;
    border: 1px solid var(--border);
    box-sizing: border-box;
}

.find-replace-row {
    display: flex;
    gap: 8px;
    width: 100%;
}

.toolbar input {
    flex: 1;
    min-width: 0;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--container-bg);
    color: var(--text-main);
    font-size: 14px;
}

.toolbar-group {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    width: 100%;
}

.toolbar button {
    flex: 1;
    padding: 8px 12px;
    background: var(--border);
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    color: var(--text-main);
    font-size: 12px;
    white-space: nowrap;
    transition: background 0.2s;
    text-align: center;
}

.toolbar button:hover {
    background: var(--primary);
    color: white;
}

.toolbar button.active {
    background: var(--success);
    color: white;
}

/* --- Editor --- */
.editor-wrapper {
    position: relative;
}

textarea {
    width: 100%;
    height: 320px;
    padding: 20px;
    border: 2px solid var(--border);
    border-radius: 12px;
    font-size: 16px;
    background: transparent;
    color: var(--text-main);
    box-sizing: border-box;
    resize: vertical;
    line-height: 1.6;
}

textarea:focus {
    outline: none;
    border-color: var(--primary);
}

.counter-bar {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    background: var(--counter-bg);
    padding: 10px 15px;
    border-radius: 8px;
    flex-wrap: wrap;
    gap: 10px;
}

.stats-left {
    display: flex;
    gap: 15px;
}

.counter-bar b {
    color: var(--primary);
}

.readability-tag {
    padding: 2px 8px;
    border-radius: 4px;
    background: var(--border);
    color: var(--text-main);
    font-size: 11px;
    text-transform: uppercase;
}

/* --- Action Area (Mobile Optimized) --- */
.action-area {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap; /* Allows wrapping on small screens */
    gap: 12px;
    margin-top: 25px;
    justify-content: center;
    align-items: center;
    width: 100%;
    box-sizing: border-box;
}

.secondary-actions {
    display: flex;
    gap: 12px;
    flex: 1.2;
    min-width: 0; /* Prevents overflow */
}

.main-btn {
    padding: 12px 20px;
    border: none;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    white-space: nowrap;
}

.btn-purify {
    background-color: var(--primary);
    color: white;
    flex: 1;
    min-width: 150px; /* Ensures button is clickable and visible */
}

.btn-purify:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
}

.btn-copy, .btn-clear {
    flex: 1;
    background-color: var(--counter-bg);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.btn-copy:hover {
    background-color: var(--border);
}

.btn-clear:hover {
    background-color: #fee2e2;
    color: #dc2626;
    border-color: #fecaca;
}

.dark-theme .btn-clear:hover {
    background-color: #450a0a;
    color: #f87171;
    border-color: #7f1d1d;
}

#statusMessage {
    height: 24px;
    text-align: center;
    color: var(--success);
    margin-top: 15px;
    font-weight: 600;
    transition: opacity 0.3s;
}

/* --- Footer --- */
footer {
    margin-top: 40px;
    padding: 30px 20px;
    border-top: 1px solid var(--border);
    font-size: 0.95rem;
    color: var(--text-muted);
    text-align: center;
    width: 100%;
    max-width: 850px;
    box-sizing: border-box;
}

footer a {
    text-decoration: none;
    color: var(--text-muted);
    transition: color 0.2s;
}

footer a:hover {
    color: var(--primary);
}

/* --- Responsive Layout --- */
@media (max-width: 700px) {
    .container {
        padding: 20px;
        margin: 20px 10px;
        width: calc(100% - 20px); /* Keeps it within screen bounds */
    }

    .action-area {
        flex-direction: column;
    }
    
    .secondary-actions {
        width: 100%;
    }

    .main-btn, .btn-purify, .btn-copy, .btn-clear {
        width: 100%;
    }

    /* Stacks inputs on very small screens */
    .find-replace-row {
        flex-direction: column;
    }
}
