:root {
    --primary: #8b5cf6;
    --primary-hover: #7c3aed;
    --secondary: #a78bfa;
    --bg-dark: #0f172a;
    --surface-dark: #1e293b;
    --border-dark: #334155;
    --text-primary-dark: #f8fafc;
    --text-secondary-dark: #94a3b8;

    --bg-light: #f8fafc;
    --surface-light: #ffffff;
    --border-light: #e2e8f0;
    --text-primary-light: #0f172a;
    --text-secondary-light: #64748b;

    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --border: var(--border-dark);
    --text-primary: var(--text-primary-dark);
    --text-secondary: var(--text-secondary-dark);

    --error: #ef4444;
    --success: #10b981;
    --radius: 12px;
    --font: 'Inter', system-ui, sans-serif;
    --mono: 'JetBrains Mono', 'Menlo', 'Courier New', monospace;
    --transition: 0.2s ease;
}

body.light-theme {
    --bg: var(--bg-light);
    --surface: var(--surface-light);
    --border: var(--border-light);
    --text-primary: var(--text-primary-light);
    --text-secondary: var(--text-secondary-light);
}

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

body {
    background-color: var(--bg);
    color: var(--text-primary);
    font-family: var(--font);
    line-height: 1.5;
    transition: background-color var(--transition), color var(--transition);
}

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

/* Base UI */
.app {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--primary);
}

.logo h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    color: var(--text-primary);
}

/* Buttons & Inputs */
.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 8px;
    transition: var(--transition);
}

.icon-btn:hover {
    background: rgba(139, 92, 246, 0.1);
    color: var(--primary);
}

body.light-theme .sun-icon {
    display: none;
}

body.dark-theme .moon-icon {
    display: none;
}

.btn {
    padding: 0.75rem 1.25rem;
    border-radius: 8px;
    font-weight: 500;
    font-size: 0.875rem;
    cursor: pointer;
    font-family: var(--font);
    transition: var(--transition);
    border: none;
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--primary);
    color: var(--primary);
}

.btn-outline:hover {
    background: rgba(139, 92, 246, 0.1);
}

.btn-full {
    width: 100%;
}

.mt-2 {
    margin-top: 0.5rem;
}

.mt-3 {
    margin-top: 1rem;
}

.hidden {
    display: none !important;
}

.glass-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
}

/* Layout */
.main-layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 2rem;
    flex: 1;
}

@media (max-width: 900px) {
    .main-layout {
        grid-template-columns: 1fr;
    }
}

/* Sidebar */
.sidebar {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    height: fit-content;
}

.sidebar-section h3 {
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 0.75rem;
}

.select-input,
.text-input {
    width: 100%;
    padding: 0.75rem;
    border-radius: 8px;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: 0.875rem;
    outline: none;
    transition: var(--transition);
}

.select-input:focus,
.text-input:focus {
    border-color: var(--primary);
}

/* Toggle Switch */
.toggle-group {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.875rem;
}

.switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--border);
    transition: .4s;
    border-radius: 24px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 4px;
    bottom: 4px;
    background-color: #fff;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--primary);
}

input:checked+.slider:before {
    transform: translateX(20px);
}

/* Content Area */
.content-area {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.editor-panel {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.panel-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
}

/* Regex Input */
.regex-input-wrapper {
    display: flex;
    align-items: center;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0 1rem;
    font-size: 1.125rem;
    transition: var(--transition);
}

.regex-input-wrapper:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.2);
}

.slash,
.flags-display {
    color: var(--text-secondary);
    font-weight: 600;
    user-select: none;
}

#regex-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--primary);
    padding: 1rem 0.5rem;
    font-size: 1.125rem;
    outline: none;
}

.flags-group {
    display: flex;
    gap: 0.25rem;
    background: var(--bg);
    padding: 0.25rem;
    border-radius: 6px;
    border: 1px solid var(--border);
}

.flag-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    width: 28px;
    height: 28px;
    border-radius: 4px;
    cursor: pointer;
    font-family: var(--mono);
    font-size: 0.875rem;
    font-weight: 600;
}

.flag-btn:hover {
    background: rgba(139, 92, 246, 0.1);
}

.flag-btn.active {
    background: var(--primary);
    color: white;
}

.error-msg {
    color: var(--error);
    font-size: 0.875rem;
    margin-top: -0.5rem;
}

/* Rich Textbox Highlighting */
.test-string-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.match-count {
    background: rgba(139, 92, 246, 0.1);
    color: var(--primary);
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.875rem;
    font-weight: 600;
}

.highlighter-wrapper {
    position: relative;
    flex: 1;
    min-height: 200px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}

/* Both layers must exactly overlay */
.highlight-layer,
.text-layer {
    padding: 1rem;
    font-size: 1rem;
    line-height: 1.6;
    letter-spacing: 0;
    word-wrap: break-word;
    white-space: pre-wrap;
    width: 100%;
    height: 100%;
}

.highlighter-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: auto;
    z-index: 1;
}

.text-layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2;
    background: transparent;
    color: var(--text-primary);
    border: none;
    outline: none;
    resize: none;
}

/* Ensure caret is visible but text doesn't hide highlights completely */
.text-layer {
    -webkit-text-fill-color: transparent;
    color: transparent;
    caret-color: var(--text-primary);
}

/* Because text is transparent, the highlight layer needs to render the normal text too */
.highlight-layer {
    color: var(--text-primary);
}

/* Highlight styling */
.match {
    background-color: rgba(139, 92, 246, 0.3);
    border-radius: 2px;
    border-bottom: 2px solid var(--primary);
}

.match.alt {
    background-color: rgba(16, 185, 129, 0.3);
    border-bottom: 2px solid var(--success);
}

.group-0 {
    background-color: rgba(139, 92, 246, 0.4);
}

.group-1 {
    background-color: rgba(14, 165, 233, 0.4);
}

.group-2 {
    background-color: rgba(245, 158, 11, 0.4);
}

.group-3 {
    background-color: rgba(236, 72, 153, 0.4);
}

/* Replace pane */
#replace-input {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text-primary);
    padding: 0.75rem 1rem;
    border-radius: 8px;
    outline: none;
}

#replace-input:focus {
    border-color: var(--primary);
}

.replace-preview-title {
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
}

.result-box {
    background: var(--bg);
    padding: 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    min-height: 80px;
    color: var(--text-primary);
    white-space: pre-wrap;
    word-wrap: break-word;
}

/* Explanation pane */
.explanation-box {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem;
    min-height: 100px;
    font-size: 0.875rem;
}

.exp-item {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border);
}

.exp-item:last-child {
    border-bottom: none;
}

.exp-token {
    background: var(--surface);
    color: var(--primary);
    padding: 0.125rem 0.375rem;
    border-radius: 4px;
    font-family: var(--mono);
    white-space: nowrap;
}

.exp-desc {
    color: var(--text-primary);
    flex: 1;
}

.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--surface);
    color: var(--text-primary);
    padding: 10px 20px;
    border-radius: 20px;
    border: 1px solid var(--border);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 1000;
}

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