/* ===== CSS Variables & Reset ===== */
:root {
    --bg-primary: #0a0a0f;
    --bg-secondary: #11111b;
    --bg-card: rgba(22, 22, 35, 0.75);
    --bg-card-hover: rgba(30, 30, 48, 0.85);
    --border-subtle: rgba(99, 102, 241, 0.12);
    --border-accent: rgba(129, 140, 248, 0.25);
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --accent-indigo: #818cf8;
    --accent-purple: #a78bfa;
    --accent-violet: #c084fc;
    --accent-pink: #f472b6;
    --accent-cyan: #22d3ee;
    --accent-emerald: #34d399;
    --error-red: #f87171;
    --error-bg: rgba(248, 113, 113, 0.08);
    --success-green: #4ade80;
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.5);
    --shadow-glow: 0 0 40px rgba(129,140,248,0.08);
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);

    /* Depth palette for recursion blocks */
    --depth-0: #818cf8;
    --depth-1: #a78bfa;
    --depth-2: #c084fc;
    --depth-3: #e879f9;
    --depth-4: #f472b6;
    --depth-5: #fb7185;
    --depth-6: #f97316;
    --depth-7: #fbbf24;
    --depth-8: #34d399;
    --depth-9: #22d3ee;
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

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

/* ===== Animated Background ===== */
.bg-grid {
    position: fixed;
    inset: 0;
    background-image:
        linear-gradient(rgba(129,140,248,0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(129,140,248,0.03) 1px, transparent 1px);
    background-size: 40px 40px;
    z-index: 0;
    pointer-events: none;
}

.bg-glow {
    position: fixed;
    border-radius: 50%;
    filter: blur(100px);
    opacity: 0.12;
    z-index: 0;
    pointer-events: none;
    animation: glowFloat 20s ease-in-out infinite alternate;
}
.bg-glow-1 {
    width: 500px; height: 500px;
    background: var(--accent-indigo);
    top: -100px; left: -100px;
}
.bg-glow-2 {
    width: 400px; height: 400px;
    background: var(--accent-violet);
    bottom: -80px; right: -80px;
    animation-delay: -7s;
}
.bg-glow-3 {
    width: 300px; height: 300px;
    background: var(--accent-cyan);
    top: 40%; left: 50%;
    animation-delay: -14s;
    opacity: 0.06;
}

@keyframes glowFloat {
    0%   { transform: translate(0, 0) scale(1); }
    50%  { transform: translate(30px, -20px) scale(1.1); }
    100% { transform: translate(-20px, 30px) scale(0.95); }
}

/* ===== Header ===== */
#app-header {
    position: relative;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    background: rgba(10, 10, 15, 0.8);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border-subtle);
}

.header-left { display: flex; align-items: center; gap: 12px; }

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
}
.logo h1 {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}
.logo .accent {
    background: linear-gradient(135deg, var(--accent-indigo), var(--accent-violet));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* Language Toggle */
.lang-toggle {
    position: relative;
    display: flex;
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    padding: 3px;
    gap: 2px;
}
.lang-btn {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 18px;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-family: var(--font-sans);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: var(--radius-md);
    transition: color var(--transition);
}
.lang-btn.active { color: var(--text-primary); }
.lang-btn:hover:not(.active) { color: var(--text-primary); }

.toggle-slider {
    position: absolute;
    z-index: 1;
    top: 3px; left: 3px;
    width: calc(50% - 4px);
    height: calc(100% - 6px);
    background: linear-gradient(135deg, rgba(129,140,248,0.15), rgba(192,132,252,0.15));
    border: 1px solid var(--border-accent);
    border-radius: var(--radius-md);
    transition: transform var(--transition);
}
.toggle-slider.java {
    transform: translateX(calc(100% + 2px));
}

.lang-icon { font-size: 1rem; }

/* Step Badge */
.step-badge {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 14px;
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    font-size: 0.8rem;
    font-family: var(--font-mono);
}
.step-label {
    color: var(--text-muted);
    font-family: var(--font-sans);
    font-weight: 500;
    margin-right: 4px;
}
.step-count {
    color: var(--accent-indigo);
    font-weight: 600;
    min-width: 20px;
    text-align: right;
    transition: color var(--transition);
}
.step-count.warning { color: var(--accent-pink); }
.step-count.danger { color: var(--error-red); }
.step-limit { color: var(--text-muted); }

/* ===== Error Banner ===== */
.error-banner {
    position: relative;
    z-index: 10;
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 10px 24px;
    background: var(--error-bg);
    border-bottom: 1px solid rgba(248,113,113,0.2);
    animation: slideDown 0.3s ease;
}
.error-banner.visible { display: flex; }

.error-content {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--error-red);
    font-size: 0.9rem;
    font-weight: 500;
}
.error-close {
    background: none;
    border: none;
    color: var(--error-red);
    font-size: 1.3rem;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity var(--transition);
    line-height: 1;
}
.error-close:hover { opacity: 1; }

@keyframes slideDown {
    from { transform: translateY(-100%); opacity: 0; }
    to   { transform: translateY(0); opacity: 1; }
}

/* ===== Main Layout ===== */
#app-main {
    position: relative;
    z-index: 5;
    display: flex;
    height: calc(100vh - 57px);
    gap: 0;
}

.panel {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Panel Header */
.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border-subtle);
    flex-shrink: 0;
}
.panel-header h2 {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}
.panel-actions {
    display: flex;
    gap: 4px;
}
.icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px; height: 30px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
}
.icon-btn:hover {
    background: rgba(129,140,248,0.1);
    color: var(--text-primary);
}

/* ===== Editor Panel ===== */
.editor-panel {
    width: 45%;
    background: rgba(10, 10, 15, 0.6);
    backdrop-filter: blur(10px);
    border-right: 1px solid var(--border-subtle);
}

.editor-wrapper {
    flex: 1;
    overflow: hidden;
    position: relative;
}

/* CodeMirror Overrides */
.editor-wrapper .CodeMirror {
    height: 100%;
    font-family: var(--font-mono);
    font-size: 14px;
    line-height: 1.65;
    padding: 8px 0;
    background: transparent;
}
.CodeMirror-gutters {
    background: transparent !important;
    border-right: 1px solid var(--border-subtle) !important;
}
.CodeMirror-linenumber {
    color: var(--text-muted) !important;
    font-size: 12px !important;
}
.CodeMirror-cursor { border-left-color: var(--accent-indigo) !important; }
.CodeMirror-selected { background: rgba(129,140,248,0.15) !important; }
.CodeMirror-focused .CodeMirror-selected { background: rgba(129,140,248,0.2) !important; }

/* Editor Footer */
.editor-footer {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    border-top: 1px solid var(--border-subtle);
    flex-shrink: 0;
}

.input-group {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 0 12px;
    transition: border-color var(--transition);
}
.input-group:focus-within {
    border-color: var(--accent-indigo);
}
.input-group label {
    font-size: 0.78rem;
    color: var(--text-muted);
    white-space: nowrap;
    font-weight: 500;
}
.input-group input {
    flex: 1;
    background: none;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 0.85rem;
    padding: 8px 0;
}
.input-group input::placeholder {
    color: var(--text-muted);
    opacity: 0.6;
}

/* Run Button */
.run-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 24px;
    border: none;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg, var(--accent-indigo), var(--accent-purple));
    color: white;
    font-family: var(--font-sans);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
    box-shadow: 0 2px 12px rgba(129,140,248,0.25);
    flex-shrink: 0;
}
.run-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 20px rgba(129,140,248,0.4);
}
.run-btn:active {
    transform: translateY(0);
}
.run-btn.running {
    opacity: 0.7;
    pointer-events: none;
}

/* ===== Visualization Panel ===== */
.viz-panel {
    flex: 1;
    background: rgba(10, 10, 15, 0.4);
    backdrop-filter: blur(10px);
    display: flex;
    flex-direction: column;
}

.viz-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    position: relative;
}

/* Scrollbar styling */
.viz-content::-webkit-scrollbar {
    width: 6px;
}
.viz-content::-webkit-scrollbar-track {
    background: transparent;
}
.viz-content::-webkit-scrollbar-thumb {
    background: rgba(129,140,248,0.2);
    border-radius: 3px;
}

/* Placeholder */
.viz-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    text-align: center;
    gap: 12px;
}
.viz-placeholder.hidden { display: none; }
.placeholder-icon {
    opacity: 0.4;
    margin-bottom: 8px;
    animation: float 4s ease-in-out infinite;
}
.viz-placeholder p {
    font-size: 0.95rem;
    max-width: 320px;
    line-height: 1.6;
}
.placeholder-hint {
    font-size: 0.78rem;
    color: var(--text-muted);
    opacity: 0.5;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

/* ===== Call Tree Blocks ===== */
.viz-tree { display: none; }
.viz-tree.active { display: block; }

.call-node {
    margin: 4px 0 4px 0;
    animation: nodeSlideIn 0.35s ease both;
}

.call-block {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px;
    border-radius: var(--radius-md);
    border-left: 3px solid var(--depth-0);
    background: var(--bg-card);
    backdrop-filter: blur(8px);
    cursor: pointer;
    transition: all var(--transition);
    position: relative;
    overflow: hidden;
}
.call-block::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, var(--depth-0), transparent);
    opacity: 0.04;
    pointer-events: none;
}
.call-block:hover {
    background: var(--bg-card-hover);
    transform: translateX(2px);
}

/* Depth colors */
.call-block[data-depth="0"] { border-left-color: var(--depth-0); }
.call-block[data-depth="0"]::before { background: linear-gradient(90deg, var(--depth-0), transparent); }
.call-block[data-depth="1"] { border-left-color: var(--depth-1); }
.call-block[data-depth="1"]::before { background: linear-gradient(90deg, var(--depth-1), transparent); }
.call-block[data-depth="2"] { border-left-color: var(--depth-2); }
.call-block[data-depth="2"]::before { background: linear-gradient(90deg, var(--depth-2), transparent); }
.call-block[data-depth="3"] { border-left-color: var(--depth-3); }
.call-block[data-depth="3"]::before { background: linear-gradient(90deg, var(--depth-3), transparent); }
.call-block[data-depth="4"] { border-left-color: var(--depth-4); }
.call-block[data-depth="4"]::before { background: linear-gradient(90deg, var(--depth-4), transparent); }
.call-block[data-depth="5"] { border-left-color: var(--depth-5); }
.call-block[data-depth="5"]::before { background: linear-gradient(90deg, var(--depth-5), transparent); }
.call-block[data-depth="6"] { border-left-color: var(--depth-6); }
.call-block[data-depth="6"]::before { background: linear-gradient(90deg, var(--depth-6), transparent); }
.call-block[data-depth="7"] { border-left-color: var(--depth-7); }
.call-block[data-depth="7"]::before { background: linear-gradient(90deg, var(--depth-7), transparent); }
.call-block[data-depth="8"] { border-left-color: var(--depth-8); }
.call-block[data-depth="8"]::before { background: linear-gradient(90deg, var(--depth-8), transparent); }
.call-block[data-depth="9"] { border-left-color: var(--depth-9); }
.call-block[data-depth="9"]::before { background: linear-gradient(90deg, var(--depth-9), transparent); }

.call-order {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--text-muted);
    background: rgba(255,255,255,0.05);
    padding: 2px 7px;
    border-radius: var(--radius-sm);
    min-width: 26px;
    text-align: center;
    flex-shrink: 0;
}

.call-signature {
    font-family: var(--font-mono);
    font-size: 0.82rem;
    color: var(--text-primary);
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.call-signature .fn-name {
    color: var(--accent-indigo);
    font-weight: 600;
}
.call-signature .fn-args {
    color: var(--text-secondary);
}

.call-return {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--accent-emerald);
    font-weight: 500;
    white-space: nowrap;
    flex-shrink: 0;
    opacity: 0;
    animation: fadeIn 0.3s ease forwards;
    animation-delay: 0.2s;
}
.call-return.error-return {
    color: var(--error-red);
}

.call-toggle {
    font-size: 0.75rem;
    color: var(--text-muted);
    flex-shrink: 0;
    transition: transform var(--transition);
    width: 16px;
    text-align: center;
}
.call-toggle.collapsed { transform: rotate(-90deg); }

.call-children {
    margin-left: 24px;
    padding-left: 12px;
    border-left: 1px dashed var(--border-subtle);
}
.call-children.collapsed {
    display: none;
}

@keyframes nodeSlideIn {
    from { opacity: 0; transform: translateX(-12px); }
    to   { opacity: 1; transform: translateX(0); }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* ===== Console Panel ===== */
.console-resizer {
    height: 4px;
    background: transparent;
    cursor: ns-resize;
    flex-shrink: 0;
    transition: background var(--transition);
}
.console-resizer:hover, .console-resizer.active {
    background: var(--accent-indigo);
}

.console-panel {
    flex-shrink: 0;
    min-height: 40px;
    height: 140px; /* Default height, controllable via JS */
    border-top: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
}
.console-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 16px;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    background: rgba(0,0,0,0.2);
}
.console-output {
    flex: 1;
    overflow-y: auto;
    padding: 8px 16px;
    font-family: var(--font-mono);
    font-size: 12.5px; /* Default px font-size to allow direct numeric scaling */
    color: var(--text-secondary);
    line-height: 1.6;
    white-space: pre-wrap;
}
.console-output::-webkit-scrollbar { width: 4px; }
.console-output::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; }

.console-line {
    padding: 1px 0;
}
.console-line.error {
    color: var(--error-red);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    #app-main {
        flex-direction: column;
    }
    .editor-panel {
        width: 100%;
        height: 45%;
        border-right: none;
        border-bottom: 1px solid var(--border-subtle);
    }
    .viz-panel {
        height: 55%;
    }
    .logo h1 { font-size: 1rem; }
    .lang-btn { padding: 5px 12px; font-size: 0.78rem; }
    .step-badge { font-size: 0.72rem; padding: 4px 10px; }
}
