:root {
    --primary: #8b5cf6;
    --primary-hover: #7c3aed;
    --bg-main: #0f172a;
    --bg-panel: #1e293b;
    --bg-surface: #334155;

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

    --item-base: #3b82f6;
    --item-selected: #f59e0b;

    --radius: 8px;
    --font-ui: 'Inter', system-ui, sans-serif;
    --font-code: 'JetBrains Mono', monospace;
}

[data-theme="light"] {
    --bg-main: #f8fafc;
    --bg-panel: #ffffff;
    --bg-surface: #e2e8f0;

    --text-main: #0f172a;
    --text-muted: #64748b;
    --border: #cbd5e1;
}

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

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

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background-color: var(--bg-panel);
    border-bottom: 1px solid var(--border);
}

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

.logo h1 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-main);
}

.badge {
    background: rgba(139, 92, 246, 0.2);
    color: var(--primary);
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
}

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

.btn-icon {
    background: transparent;
    color: var(--text-muted);
    border: none;
    padding: 0.5rem;
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 0.2s;
}

.btn-icon:hover {
    color: var(--primary);
    background-color: var(--bg-surface);
}

main {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* Sidebar Controls */
.controls-panel {
    width: 380px;
    background-color: var(--bg-panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg-panel);
    z-index: 10;
}

.tab {
    flex: 1;
    padding: 1rem;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-weight: 600;
    font-family: var(--font-ui);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: 0.2s;
}

.tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-content {
    display: none;
    padding: 1.5rem;
}

.tab-content.active {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.control-group label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    font-family: var(--font-code);
}

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

.radio-group.wrap {
    flex-wrap: wrap;
}

.radio-group label {
    flex: 1;
    text-align: center;
    position: relative;
    cursor: pointer;
}

.radio-group.wrap label {
    flex: 1 1 30%;
}

.radio-group input[type="radio"] {
    position: absolute;
    opacity: 0;
}

.radio-group span {
    display: block;
    padding: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-main);
    border-radius: 4px;
    transition: 0.2s;
    font-family: var(--font-code);
    user-select: none;
}

.radio-group input[type="radio"]:checked+span {
    background: var(--bg-surface);
    color: var(--primary);
    font-weight: 600;
}

.radio-group span:hover {
    background: rgba(255, 255, 255, 0.05);
}

[data-theme="light"] .radio-group input[type="radio"]:checked+span {
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

input[type="range"] {
    width: 100%;
    accent-color: var(--primary);
}

.text-input {
    width: 100%;
    background: var(--bg-main);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 0.6rem;
    border-radius: 4px;
    font-family: var(--font-code);
    font-size: 0.85rem;
    outline: none;
}

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

.selected-indicator {
    padding: 0.75rem;
    background: rgba(245, 158, 11, 0.1);
    border: 1px dashed var(--item-selected);
    border-radius: var(--radius);
    text-align: center;
    color: var(--item-selected);
    font-size: 0.85rem;
    font-weight: 600;
}

.children-manager {
    border-top: 1px solid var(--border);
    padding-top: 1.5rem;
    margin-top: 1.5rem;
}

.children-manager p {
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.btn-group {
    display: flex;
    gap: 0.5rem;
}

.btn {
    flex: 1;
    padding: 0.6rem;
    border-radius: 4px;
    border: none;
    font-family: var(--font-ui);
    font-size: 0.85rem;
    cursor: pointer;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    gap: 0.4rem;
    font-weight: 500;
}

.btn-secondary {
    background: var(--bg-surface);
    color: var(--text-main);
}

.btn-secondary:hover {
    filter: brightness(1.2);
}

.btn-danger {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.2);
}

.mt-auto {
    margin-top: auto;
}

/* Preview Area */
.preview-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 1.5rem;
    position: relative;
    background: repeating-linear-gradient(45deg,
            rgba(255, 255, 255, 0.02) 0px,
            rgba(255, 255, 255, 0.02) 20px,
            transparent 20px,
            transparent 40px);
}

.preview-header {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-muted);
    padding-bottom: 1rem;
}

kbd {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    padding: 0.1rem 0.3rem;
    border-radius: 4px;
    font-family: var(--font-code);
    font-size: 0.75rem;
}

.canvas-container {
    flex: 1;
    position: relative;
    display: flex;
    border: 2px dashed rgba(139, 92, 246, 0.5);
    border-radius: var(--radius);
    padding: 2rem;
    background: var(--bg-panel);
    overflow: auto;
}

/* Flex Container mapped directly to JS controls */
.flex-container {
    display: flex;
    width: 100%;
    min-height: 100%;
    background: rgba(139, 92, 246, 0.05);
    border: 1px solid rgba(139, 92, 246, 0.2);
    border-radius: 4px;
    position: relative;
    transition: all 0.3s ease;
}

.flex-item {
    background: var(--item-base);
    color: #fff;
    padding: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-code);
    font-weight: 600;
    font-size: 1.25rem;
    border-radius: 4px;
    cursor: pointer;
    user-select: none;
    min-width: 60px;
    min-height: 60px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.flex-item:hover {
    filter: brightness(1.1);
    transform: scale(1.02);
}

.flex-item.selected {
    background: var(--item-selected);
    outline: 3px solid #fff;
    outline-offset: 2px;
    z-index: 10;
}

[data-theme="light"] .flex-item.selected {
    outline-color: var(--bg-main);
}

/* Direction Arrows */
.dir-arrow {
    position: absolute;
    font-family: var(--font-code);
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--primary);
    pointer-events: none;
    transition: all 0.3s;
    opacity: 0.7;
}

.main-axis {
    top: 0.5rem;
    left: 0.5rem;
}

.cross-axis {
    top: 2rem;
    left: 0.5rem;
    writing-mode: vertical-lr;
    transform: rotate(180deg);
    text-orientation: mixed;
}

/* Code Preview */
.code-preview {
    margin-top: 1.5rem;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    position: relative;
}

.code-preview pre {
    font-family: var(--font-code);
    font-size: 0.85rem;
    color: #38bdf8;
    overflow-x: auto;
    white-space: pre-wrap;
}

/* Toast */
.toast {
    position: fixed;
    bottom: -100px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text-main);
    color: var(--bg-main);
    padding: 0.75rem 1.5rem;
    border-radius: 30px;
    font-weight: 500;
    transition: 0.3s;
    opacity: 0;
    z-index: 1000;
}

.toast.show {
    bottom: 30px;
    opacity: 1;
}