:root {
    /* Color Palette */
    --bg-base: #0f1115;
    --bg-surface: rgba(22, 26, 33, 0.7);
    --bg-surface-solid: #161a21;
    --bg-hover: rgba(56, 189, 248, 0.1);

    --border-light: rgba(56, 189, 248, 0.15);
    --border-subtle: rgba(255, 255, 255, 0.05);

    --text-main: #f8fafc;
    --text-muted: #94a3b8;

    --accent: #38bdf8;
    --accent-hover: #0ea5e9;
    --primary: #8b5cf6;
    --primary-hover: #7c3aed;
    --error: #ef4444;

    /* Highlight Colors Array */
    --hl-0: rgba(56, 189, 248, 0.4);
    --hl-1: rgba(167, 139, 250, 0.4);
    --hl-2: rgba(251, 146, 60, 0.4);
    --hl-3: rgba(52, 211, 153, 0.4);

    --hl-group: rgba(255, 255, 255, 0.2);

    --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
    --font-mono: 'Fira Code', 'Courier New', Courier, monospace;

    --radius-sm: 6px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-color: var(--bg-base);
    color: var(--text-main);
    font-family: var(--font-ui);
    line-height: 1.5;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Utils */
.glass-panel {
    background: var(--bg-surface);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border-light);
}

.font-mono {
    font-family: var(--font-mono);
}

.text-sm {
    font-size: 0.85rem;
}

.text-muted {
    color: var(--text-muted);
}

.text-accent {
    color: var(--accent);
}

.text-error {
    color: var(--error);
}

.hidden {
    display: none !important;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border-radius: var(--radius-sm);
    border: none;
    font-weight: 500;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font-ui);
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* Layout */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.app-header {
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    flex-shrink: 0;
    border-bottom: 1px solid var(--border-light);
    z-index: 10;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 1.2rem;
    font-weight: 500;
}

.logo strong {
    font-weight: 800;
    color: #FFF;
}

.header-actions {
    display: flex;
    gap: 0.75rem;
}

/* Workspace */
.workspace {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 1.5rem;
    gap: 1.5rem;
}

/* Editor Panel */
.editor-panel {
    border-radius: var(--radius-lg);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

/* Regex Builder */
.regex-builder {
    display: flex;
    align-items: stretch;
    padding: 1rem 1.5rem;
    background: rgba(0, 0, 0, 0.4);
    border-bottom: 1px solid var(--border-subtle);
}

.slash-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 600;
    padding: 0 0.5rem;
    font-family: var(--font-mono);
}

.regex-input-container {
    flex: 1;
    display: flex;
}

.regex-input {
    width: 100%;
    background: transparent;
    border: none;
    color: var(--accent);
    font-size: 1.25rem;
    outline: none;
    padding: 0.5rem;
}

.regex-input::placeholder {
    color: rgba(56, 189, 248, 0.3);
}

/* Flags */
.flags-container {
    display: flex;
    gap: 0.25rem;
    align-items: center;
    padding-left: 0.5rem;
}

.flag-toggle {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 1rem;
}

.flag-toggle.active {
    background: rgba(139, 92, 246, 0.2);
    color: var(--primary);
    border-color: rgba(139, 92, 246, 0.4);
}

.flag-toggle:hover:not(.active) {
    background: rgba(255, 255, 255, 0.05);
    color: #FFF;
}

/* Explanation Bar */
.explanation-bar {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.75rem 1.5rem;
    background: rgba(255, 255, 255, 0.02);
    border-bottom: 1px solid var(--border-subtle);
}

.explanation-icon {
    color: var(--accent);
    margin-top: 2px;
}

/* Mode Bar */
.mode-bar {
    display: flex;
    align-items: center;
    padding: 0.75rem 1.5rem;
    gap: 1.5rem;
}

.mode-toggles {
    display: flex;
    background: rgba(0, 0, 0, 0.3);
    border-radius: var(--radius-sm);
    padding: 4px;
    border: 1px solid var(--border-subtle);
}

.mode-radio {
    display: -webkit-box;
    display: flex;
    position: relative;
    cursor: pointer;
}

.mode-radio input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
}

.radio-label {
    padding: 0.4rem 1rem;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
    border-radius: 4px;
    transition: all 0.2s;
}

.mode-radio input:checked+.radio-label {
    background: var(--accent);
    color: #000;
}

.replace-container {
    flex: 1;
    display: flex;
}

.replace-input {
    width: 100%;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border-subtle);
    color: #FFF;
    padding: 0.5rem 1rem;
    border-radius: var(--radius-sm);
    outline: none;
    font-size: 0.95rem;
}

.replace-input:focus {
    border-color: var(--accent);
}

/* Bottom Split Container */
.bottom-split {
    display: flex;
    gap: 1.5rem;
    flex: 1;
    overflow: hidden;
}

.test-pane,
.results-pane {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-radius: var(--radius-lg);
    position: relative;
}

.pane-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid var(--border-light);
    background: rgba(0, 0, 0, 0.2);
    flex-shrink: 0;
}

.pane-title {
    font-size: 0.9rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.badge {
    background: rgba(255, 255, 255, 0.1);
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
}

/* Editor Background Highlight Logic */
.editor-container {
    position: relative;
    flex: 1;
    overflow: hidden;
    background: var(--bg-base);
}

.highlights,
.test-input {
    font-size: 1rem;
    line-height: 1.6;
    padding: 1rem;
    width: 100%;
    height: 100%;
    margin: 0;
    border: none;
    white-space: pre-wrap;
    word-wrap: break-word;
    outline: none;
    overflow-y: auto;
    overflow-x: hidden;
}

.highlights {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    color: transparent;
    pointer-events: none;
}

.test-input {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    color: #FFF;
    background: transparent;
    caret-color: #FFF;
    resize: none;
}

/* Match Highlights natively positioned underneath text */
.hl-match {
    border-radius: 3px;
}

/* Results Pane */
.results-content {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.result-item {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 0.75rem;
}

.result-header {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
    font-weight: 600;
}

.result-value {
    font-family: var(--font-mono);
    font-size: 0.9rem;
    color: #FFF;
    background: rgba(255, 255, 255, 0.05);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    display: inline-block;
    word-break: break-all;
}

.result-groups {
    margin-top: 0.5rem;
    padding-top: 0.5rem;
    border-top: 1px dashed var(--border-subtle);
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.group-item {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    font-size: 0.8rem;
}

.group-idx {
    color: var(--primary);
    font-weight: 600;
    font-family: var(--font-mono);
}

.group-val {
    color: rgba(255, 255, 255, 0.9);
    font-family: var(--font-mono);
    word-break: break-all;
}

/* Modal Library */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    opacity: 0;
    pointer-events: none;
    transition: 0.2s;
}

.modal-overlay:not(.hidden) {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    width: 600px;
    max-width: 90vw;
    max-height: 80vh;
    border-radius: var(--radius-lg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
    transform: translateY(20px);
    transition: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal-overlay:not(.hidden) .modal-content {
    transform: translateY(0);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-light);
    background: rgba(0, 0, 0, 0.3);
}

.btn-close-library {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
}

.modal-body {
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.library-item {
    padding: 1rem;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    background: rgba(255, 255, 255, 0.02);
    cursor: pointer;
    transition: all 0.2s;
}

.library-item:hover {
    background: rgba(56, 189, 248, 0.1);
    border-color: var(--accent);
}

.lib-title {
    font-weight: 600;
    color: #FFF;
    margin-bottom: 0.25rem;
}

.lib-pattern {
    font-family: var(--font-mono);
    color: var(--accent);
    font-size: 0.85rem;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--primary);
    color: #FFF;
    padding: 0.75rem 1.5rem;
    border-radius: 30px;
    font-weight: 600;
    font-size: 0.95rem;
    box-shadow: 0 10px 25px rgba(139, 92, 246, 0.4);
    z-index: 100;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    opacity: 0;
    pointer-events: none;
    transition: all 0.3s;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

@media (max-width: 1024px) {
    .bottom-split {
        flex-direction: column;
    }
}

@media (max-width: 768px) {
    .workspace {
        padding: 1rem;
        gap: 1rem;
    }

    .mode-bar {
        flex-direction: column;
        align-items: flex-start;
    }

    .replace-container {
        width: 100%;
    }
}