:root {
    --bg-dark: #0f111a;
    --panel-bg: rgba(25, 28, 41, 0.65);
    --panel-border: rgba(255, 255, 255, 0.08);
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --accent: #ec4899;
    --accent-hover: #db2777;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --error: #ef4444;
    --glass-blur: 24px;
    --wave-color: #38bdf8;
    --playhead-color: #ec4899;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-main);
    height: 100vh;
    margin: 0;
    overflow: hidden;
    position: relative;
}

/* Background Blobs for Visual Interest */
.background-blobs {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: -1;
    overflow: hidden;
    pointer-events: none;
}

.blob {
    position: absolute;
    border-radius: 50%;
    filter: blur(120px);
    opacity: 0.5;
    animation: float 20s infinite ease-in-out alternate;
}

.blob-1 {
    top: -10%;
    right: -5%;
    width: 500px;
    height: 500px;
    background: radial-gradient(circle, var(--primary) 0%, transparent 70%);
}

.blob-2 {
    bottom: -10%;
    left: -10%;
    width: 600px;
    height: 600px;
    background: radial-gradient(circle, var(--accent) 0%, transparent 70%);
    animation-delay: -10s;
}

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

/* Split Layout System */
.split-layout {
    display: flex;
    width: 100vw;
    height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 400px;
    min-width: 320px;
    height: 100vh;
    background: var(--panel-bg);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    border-right: 1px solid var(--panel-border);
    display: flex;
    flex-direction: column;
    padding: 2rem;
    gap: 2rem;
    overflow-y: auto;
    z-index: 10;
    box-shadow: 10px 0 30px -10px rgba(0, 0, 0, 0.5);
}

header {
    text-align: center;
    padding-bottom: 1rem;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
}

.logo i {
    font-size: 2.5rem;
    color: var(--wave-color);
}

h1 {
    font-size: 2.5rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--text-main) 0%, #cbd5e1 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.02em;
}

.subtitle {
    color: var(--text-muted);
    font-weight: 300;
    font-size: 1.1rem;
}

/* Equation Section — full-width bar flush with sidebar edges */
.equation-section {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
    margin-left: -2rem;
    margin-right: -2rem;
    padding: 0.75rem 1.25rem;
    background: rgba(0, 0, 0, 0.35);
    border-top: 1px solid var(--panel-border);
    border-bottom: 1px solid var(--panel-border);
    transition: border-color 0.3s, box-shadow 0.3s;
}

.equation-section:focus-within {
    border-top-color: var(--primary);
    border-bottom-color: var(--primary);
    box-shadow: inset 0 0 0 1px rgba(99, 102, 241, 0.15);
}

.equation-section label {
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--wave-color);
    white-space: nowrap;
    flex-shrink: 0;
}

math-field {
    flex: 1;
    min-width: 0;
    font-size: 1.4rem;
    background: transparent;
    color: var(--text-main);
    border: none;
    outline: none;

    /* MathLive Selection Overrides */
    --selection-background-color: rgba(99, 102, 241, 0.4);
    --selection-color: var(--text-main);

    /* Fix for when editing INSIDE a function like \sin() */
    --contains-highlight-background-color: rgba(255, 255, 255, 0.05);
}
math-field::part(selection) {
    background-color: rgba(99, 102, 241, 0.4) !important;
    color: var(--text-main) !important;
}
math-field::part(virtual-keyboard-toggle),
math-field::part(menu-toggle) {
    color: var(--text-muted);
}

.error-msg {
    color: var(--error);
    font-size: 0.9rem;
    min-height: 1.2rem;
}

.equation-error-row {
    padding: 0 1.25rem;
}

/* Controls */
.controls-section {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    background: rgba(0, 0, 0, 0.2);
    padding: 1.5rem;
    border-radius: 16px;
    border: 1px solid var(--panel-border);
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    flex: 1;
    min-width: 150px;
}

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

input[type="number"] {
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid var(--panel-border);
    color: var(--text-main);
    padding: 0.75rem 1rem;
    border-radius: 8px;
    font-family: 'Outfit', sans-serif;
    font-size: 1rem;
    outline: none;
    transition: border-color 0.3s;
    width: 100%;
}

input[type="number"]:focus {
    border-color: var(--primary);
}

.input-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

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

.volume-group {
    flex: 1.5;
}

.slider-wrapper {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-top: 0.5rem;
}

.slider-wrapper i {
    color: var(--text-muted);
    font-size: 1.2rem;
}

input[type="range"] {
    flex: 1;
    appearance: none;
    -webkit-appearance: none;
    background: rgba(255, 255, 255, 0.1);
    height: 6px;
    border-radius: 3px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 16px;
    height: 16px;
    background: var(--text-main);
    border-radius: 50%;
    cursor: pointer;
    transition: transform 0.2s;
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

/* Dynamic Variables */
.variables-section {
    display: none; /* Changed by JS */
    flex-wrap: wrap;
    gap: 1.5rem;
    background: rgba(99, 102, 241, 0.1); 
    padding: 1.5rem;
    border-radius: 16px;
    border: 1px dashed var(--primary);
}

.variable-control-group {
    min-width: 280px;
    background: rgba(0, 0, 0, 0.3);
    padding: 1.25rem;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

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

.variable-header label {
    font-weight: 700;
    color: var(--primary);
    font-size: 1.1rem;
}

.variable-number-input {
    width: 70px;
    padding: 0.3rem 0.6rem;
    font-size: 0.9rem;
}

.variable-slider {
    width: 100%;
}

.variable-settings {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
}

.variable-settings label {
    display: flex;
    flex-direction: column;
    color: var(--text-muted);
    font-size: 0.75rem;
    font-weight: 600;
}

.variable-settings input, .variable-controls select, .variable-controls input {
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid var(--panel-border);
    color: var(--text-main);
    padding: 0.3rem;
    border-radius: 6px;
    font-family: inherit;
    font-size: 0.8rem;
    margin-top: 0.2rem;
    transition: border-color 0.2s;
}

.variable-settings input:focus, .variable-controls select:focus, .variable-controls input:focus {
    border-color: var(--primary);
    outline: none;
}

.variable-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    background: rgba(255, 255, 255, 0.03);
    padding: 0.5rem;
    border-radius: 8px;
    margin-top: 0.25rem;
}

.btn-play-var {
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 6px;
    width: 2rem;
    height: 2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    transition: all 0.2s ease;
}

.btn-play-var:hover {
    background: var(--primary-hover);
    transform: scale(1.05);
}

.btn-play-var.playing {
    background: var(--accent);
}

.btn-play-var.playing:hover {
    background: var(--accent-hover);
}


/* Sidebar Resize Handle */
.resize-handle {
    width: 5px;
    height: 100vh;
    cursor: col-resize;
    background: var(--panel-border);
    flex-shrink: 0;
    position: relative;
    z-index: 20;
    transition: background 0.2s;
}

.resize-handle::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 3px;
    height: 40px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.15);
    transition: background 0.2s, height 0.2s;
}

.resize-handle:hover,
.resize-handle.dragging {
    background: var(--primary);
}

.resize-handle:hover::after,
.resize-handle.dragging::after {
    background: rgba(99, 102, 241, 0.6);
    height: 60px;
}

/* Main Visualizer Area */
.main-graph {
    flex: 1;
    position: relative;
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.canvas-wrapper {
    width: 100%;
    flex: 1;
    min-height: 0;
    background: transparent;
    position: relative;
}

canvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: grab;
}

canvas:active {
    cursor: grabbing;
}

.playhead {
    position: absolute;
    top: 0;
    left: 0;
    width: 2px;
    height: 100%;
    background: var(--playhead-color);
    box-shadow: 0 0 10px var(--playhead-color);
    display: none;
    pointer-events: none;
}

/* Floating Buttons */
.playback-controls.floating-controls {
    position: absolute;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    background: rgba(15, 17, 26, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 0.75rem 1.25rem;
    border-radius: 50px;
    border: 1px solid var(--panel-border);
    box-shadow: 0 15px 30px -5px rgba(0, 0, 0, 0.5);
    z-index: 20;
    transition: bottom 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.main-graph.piano-active .playback-controls.floating-controls {
    bottom: calc(40% + 2rem);
}

/* Zoom Controls */
.zoom-controls {
    position: absolute;
    right: 2rem;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    background: rgba(15, 17, 26, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 0.5rem;
    border-radius: 50px;
    border: 1px solid var(--panel-border);
    box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.5);
    z-index: 20;
}

.btn-icon {
    background: transparent;
    color: var(--text-muted);
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 50%;
    border: none;
    font-size: 1.25rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
}


button {
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    border: none;
    outline: none;
    border-radius: 50px;
    font-weight: 600;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
    color: white;
    padding: 1rem 2.5rem;
    font-size: 1.2rem;
    box-shadow: 0 10px 20px -10px var(--primary);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 15px 25px -10px var(--primary);
    filter: brightness(1.1);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    width: 3.5rem;
    height: 3.5rem;
    border: 1px solid var(--panel-border);
    font-size: 1.5rem;
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
}

.btn-secondary.active {
    color: var(--accent);
    border-color: var(--accent);
    background: rgba(236, 72, 153, 0.1);
}

/* Piano Visualizer Split Screen */
.piano-visual-container {
    display: none;
    flex-direction: column;
    border-top: 1px solid var(--panel-border);
    background: rgba(0, 0, 0, 0.3);
}
.piano-visual-container.active {
    display: flex;
    flex: 0 0 40%;
    min-height: 250px;
}

.piano-telemetry {
    display: flex;
    align-items: center;
    gap: 2rem;
    padding: 0.5rem 1rem;
    font-family: 'Outfit', sans-serif;
    color: var(--primary);
    font-size: 1rem;
    font-weight: 600;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.btn-piano-dynamic {
    margin-left: auto;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    border: 1px solid var(--panel-border);
    border-radius: 50px;
    padding: 0.25rem 0.85rem;
    font-size: 0.85rem;
    font-family: 'Outfit', sans-serif;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    transition: all 0.2s;
}

.btn-piano-dynamic:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
}

.btn-piano-dynamic.dynamic {
    color: var(--accent);
    border-color: var(--accent);
    background: rgba(236, 72, 153, 0.1);
}

.piano-keyboard-ui {
    display: flex;
    flex: 1;
    position: relative;
    padding: 0;
    margin: 0;
    align-items: flex-start;
}

.piano-key {
    transition: background 0.1s, box-shadow 0.1s;
    cursor: pointer;
    user-select: none;
    /* touch-action: none routes mobile touches to pointer events instead of
       letting the browser interpret them as scroll/zoom gestures. */
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
}

.piano-key.white-key {
    flex: 1;
    background: #e2e8f0;
    border: 1px solid #94a3b8;
    border-radius: 0 0 6px 6px;
    position: relative;
    z-index: 1;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 15px;
    color: #1e293b;
    font-weight: 700;
    height: 100%;
}
.piano-key.white-key.active {
    background: #93c5fd;
    box-shadow: inset 0 0 10px rgba(0,0,0,0.2), 0 0 15px rgba(59, 130, 246, 0.6);
}

.piano-key.black-key {
    width: 6%;
    background: #0f172a;
    border-radius: 0 0 4px 4px;
    position: absolute;
    z-index: 2;
    height: 60%;
    transform: translateX(-50%);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding-bottom: 10px;
    color: #f8fafc;
    font-weight: 700;
    font-size: 0.8rem;
    border: 1px solid #334155;
    border-top: none;
}
.piano-key.black-key.active {
    background: #3b82f6;
    box-shadow: inset 0 0 5px rgba(0,0,0,0.5), 0 0 15px rgba(59, 130, 246, 0.8);
    border-color: #60a5fa;
}

@media (max-width: 768px) {
    .split-layout {
        flex-direction: column;
    }
    .sidebar {
        width: 100%;
        height: 50vh;
        border-right: none;
        border-bottom: 1px solid var(--panel-border);
    }
    .main-graph {
        height: 50vh;
    }
}
