/* --- Global Style & Variables --- */
:root {
    /* Light Theme Palette (Default) */
    --primary-color: #6366F1;
    --primary-hover: #818CF8;
    --bg-color: #F9FAFB;        /* Light gray */
    --panel-bg: #FFFFFF;      /* White */
    --border-color: #E5E7EB;    /* Light gray border */
    --text-color: #1F2937;      /* Dark gray text */
    --text-light: #6B7280;     /* Muted gray for light text */
    --white: #FFFFFF;
    --black: #000000;
    --font-family: 'Poppins', sans-serif;
}

/* Dark Theme Palette */
body.dark-theme {
    --bg-color: #111827;
    --panel-bg: #1F2937;
    --border-color: #374151;
    --text-color: #E5E7EB;
    --text-light: #9CA3AF;
}

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

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    transition: background-color 0.3s, color 0.3s;
}

/* --- Header & Navigation --- */
header {
    width: 100%;
    background-color: var(--panel-bg);
    border-bottom: 2px solid var(--border-color);
    transition: background-color 0.3s, border-bottom 0.3s;
}

.main-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 2rem;
    height: 60px;
    max-width: 1600px;
    margin: 0 auto;
}

.logo {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-color);
    text-decoration: none;
}

.nav-links {
    list-style: none;
    display: flex;
    gap: 1.5rem;
    flex-grow: 1; 
    justify-content: center;
}

.nav-link {
    text-decoration: none;
    color: var(--text-light);
    font-weight: 500;
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    transition: all 0.2s ease-in-out;
}

.nav-link:hover {
    color: var(--primary-color);
}

.nav-link.active {
    color: var(--primary-color);
    background-color: var(--bg-color);
}

#theme-toggle-btn {
    background-color: var(--border-color);
    color: var(--text-color);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    font-size: 1.25rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}
#theme-toggle-btn:hover {
    background-color: var(--primary-color);
    color: var(--white);
}

/* --- Main App Layout --- */
#app-container {
    flex-grow: 1;
    display: grid;
    grid-template-columns: 280px 1fr 320px;
    grid-template-rows: 1fr;
    gap: 1.5rem;
    padding: 1.5rem;
}

/* --- Reusable Components --- */
.panel {
    background-color: var(--panel-bg);
    border: 2px solid var(--border-color);
    border-radius: 12px;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    overflow-y: auto;
    transition: background-color 0.3s, border 0.3s;
}

.panel-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-color);
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--border-color);
}

.panel-subtitle {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-light);
}

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

.control-group label {
    font-weight: 500;
    color: var(--text-light);
    font-size: 0.9rem;
}

input[type="range"] {
    width: 100%;
    cursor: pointer;
}

input[type="number"],
input[type="text"],
select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 2px solid var(--border-color);
    background-color: var(--bg-color);
    color: var(--text-color);
    border-radius: 6px;
    font-family: var(--font-family);
    font-size: 0.9rem;
}

.btn {
    padding: 0.6rem 1rem;
    border: none;
    border-radius: 6px;
    font-family: var(--font-family);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-primary {
    background-color: var(--primary-color);
    color: var(--white);
}
.btn-primary:hover {
    background-color: var(--primary-hover);
}
.btn-primary:disabled {
    background-color: var(--border-color);
    color: var(--text-light);
    cursor: not-allowed;
}

.btn-secondary {
    background-color: var(--border-color);
    color: var(--text-color);
}
.btn-secondary:hover {
    background-color: var(--primary-hover);
    color: var(--white);
}

.button-group {
    display: flex;
    gap: 0.75rem;
}
.button-group > .btn {
    flex-grow: 1;
}

.info-box {
    background-color: var(--bg-color);
    border-radius: 8px;
    padding: 1rem;
    border: 2px solid var(--border-color);
}

.info-box h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.info-box p {
    font-size: 0.9rem;
    color: var(--text-light);
    line-height: 1.5;
}

pre, code {
    font-family: 'Courier New', Courier, monospace;
    background-color: var(--bg-color);
    padding: 0.25rem;
    border-radius: 4px;
    font-size: 0.9rem;
}

.algo-description-box {
    background-color: var(--bg-color);
    border: 2px solid var(--border-color);
    border-radius: 6px;
    padding: 0.75rem;
    font-size: 0.85rem;
    color: var(--text-light);
    line-height: 1.5;
}

/* --- Pseudocode Box Fix --- */
pre {
    white-space: pre-wrap;    /* Respects newlines, but wraps long lines */
    overflow-wrap: break-word; /* Breaks words if they are too long */
}

/* --- Bar Visualization (Sorting/Searching) --- */
#visualization-area {
    width: 100%;
    flex-grow: 1;
    display: flex;
    align-items: flex-end;
    min-height: 200px;
}
.bar-container {
    display: flex;
    flex-direction: row;
    align-items: flex-end;
    justify-content: center;
    width: 100%;
    height: 100%;
    gap: 2px;
}
.bar {
    flex-grow: 1;
    background-color: var(--primary-color);
    transition: height 0.3s ease, background-color 0.3s ease;
    position: relative;
    display: flex;
    justify-content: center;
}
.bar-label {
    position: absolute;
    bottom: -20px;
    font-size: 0.8rem;
    color: var(--text-light);
}
.hidden { display: none !important; }

#counting-array-container {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    margin-bottom: 1rem;
}
.count-cell {
    background-color: var(--panel-bg);
    border: 1px solid var(--border-color);
    padding: 2px;
    min-width: 30px;
    text-align: center;
    border-radius: 4px;
}
.count-index { font-size: 0.7rem; color: var(--text-light); }
.count-value { font-size: 0.9rem; font-weight: 600; display: block; }

/* --- Stack/Queue Visualization --- */
#stack-queue-container {
    display: flex;
    flex-direction: column-reverse;
    align-items: center;
    justify-content: flex-start;
    gap: 5px;
    width: 150px;
    height: 100%;
    padding-bottom: 1rem;
    border: 2px solid var(--border-color);
    border-top: none;
    border-radius: 0 0 8px 8px;
    position: relative;
}
.stack-item {
    width: 100px;
    height: 40px;
    background-color: var(--primary-color);
    color: white;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    transition: all 0.3s ease-in-out;
}
.stack-item.push-animation { animation: push 0.4s ease-out; }
.stack-item.pop-animation { animation: pop 0.4s ease-in forwards; }
@keyframes push {
    from { opacity: 0; transform: translateY(-50px); }
    to { opacity: 1; transform: translateY(0); }
}
@keyframes pop {
    from { opacity: 1; transform: translateY(0); }
    to { opacity: 0; transform: translateY(-50px); }
}
#stack-top-pointer {
    position: absolute;
    left: -70px;
    font-weight: 600;
    color: var(--primary-color);
    transition: top 0.3s ease-in-out;
    opacity: 0;
}
#stack-top-pointer.visible { opacity: 1; }
.stack-item.peek-animation { animation: peek 0.6s ease-in-out; }
#stack-queue-container.error { animation: shake 0.5s ease-in-out; }
@keyframes peek {
    0%, 100% { transform: scale(1); background-color: var(--primary-color); }
    50% { transform: scale(1.1); background-color: #F59E0B; }
}
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
    20%, 40%, 60%, 80% { transform: translateX(5px); }
}

/* --- Queue Visualization --- */
#queue-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: 5px;
    height: 60px;
    width: 100%;
    padding: 0 1rem;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    position: relative;
}
.queue-item {
    width: 50px;
    height: 50px;
    flex-shrink: 0;
    background-color: var(--primary-color);
    color: white;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    transition: all 0.3s ease-in-out;
}
.queue-pointer {
    position: absolute;
    top: -30px;
    font-weight: 600;
    color: var(--primary-color);
    transition: left 0.3s ease-in-out;
    opacity: 0;
}
.queue-pointer.visible { opacity: 1; }
.queue-item.enqueue-animation { animation: enqueue 0.4s ease-out; }
.queue-item.dequeue-animation { animation: dequeue 0.4s ease-in forwards; }
@keyframes enqueue {
    from { opacity: 0; transform: scale(0.5); }
    to { opacity: 1; transform: scale(1); }
}
@keyframes dequeue {
    from { opacity: 1; transform: translateX(0); }
    to { opacity: 0; transform: translateX(-50px); }
}

/* --- Circular Queue --- */
#queue-circular-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 5px;
    height: 60px;
    width: 100%;
    padding: 0 1rem;
    position: relative;
}
.queue-cell {
    width: 50px;
    height: 50px;
    flex-shrink: 0;
    background-color: var(--bg-color);
    border: 2px dashed var(--border-color);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
}
.queue-cell .cell-value {
    font-weight: 600;
    font-size: 1rem;
    color: var(--text-color);
}
.queue-cell .cell-index {
    position: absolute;
    bottom: -20px;
    font-size: 0.8rem;
    color: var(--text-light);
}
.queue-cell.filled {
    background-color: var(--primary-color);
    border-style: solid;
}
.queue-cell.filled .cell-value { color: white; }

/* --- Linked List Visualization --- */
#linked-list-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    min-height: 100px;
    flex-wrap: wrap;
    gap: 10px 0;
    width: 100%;
    padding: 2rem 0;
}
.ll-node-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
}
.ll-node-main {
    display: flex;
    align-items: center;
}
.ll-node {
    width: 60px;
    height: 60px;
    background-color: var(--primary-color);
    color: white;
    font-weight: 600;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 3px solid var(--primary-hover);
    position: relative;
    z-index: 2;
    transition: background-color 0.3s ease, transform 0.3s ease;
}
#linked-list-container .ll-node[style*="var(--text-light)"] {
    background-color: var(--border-color) !important;
    color: var(--text-light) !important;
}
.ll-pointer-next, .ll-pointer-prev {
    height: 4px;
    background-color: var(--text-light);
    position: relative;
    z-index: 1;
}
.ll-pointer-next { width: 50px; }
.ll-pointer-prev { width: 50px; margin-bottom: 5px; }
.ll-pointer-next::after {
    content: ''; position: absolute; right: -2px; top: -6px;
    border-top: 8px solid transparent; border-bottom: 8px solid transparent; border-left: 10px solid var(--text-light);
}
.ll-pointer-prev::before {
    content: ''; position: absolute; left: -2px; top: -6px;
    border-top: 8px solid transparent; border-bottom: 8px solid transparent; border-right: 10px solid var(--text-light);
}
.ll-node-container:last-child .ll-pointer-next { display: none; }
.ll-node-container.singly .ll-pointer-prev, .ll-node-container.circular-singly .ll-pointer-prev { display: none; }
.ll-node-container .ll-index {
    position: absolute; top: -25px; font-size: 0.8rem; color: var(--text-light);
}
.ll-head-pointer, .ll-tail-pointer {
    position: absolute; font-weight: 600; color: var(--primary-color);
    transition: all 0.4s ease-in-out; opacity: 0; white-space: nowrap;
}
.ll-head-pointer.visible, .ll-tail-pointer.visible { opacity: 1; }
#ll-circular-svg {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 0;
    pointer-events: none;
}
#ll-circular-svg path {
    stroke: var(--text-light);
    stroke-width: 3;
    fill: none;
    marker-end: url(#arrowhead);
}

/* --- Binary Search Tree --- */
#bst-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: auto;
}
.bst-node {
    position: absolute;
    width: 50px;
    height: 50px;
    background-color: var(--primary-color);
    color: white;
    font-weight: 600;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 3px solid var(--primary-hover);
    z-index: 2;
    left: var(--left-pos);
    top: var(--top-pos);
    transition: all 0.4s ease-in-out;
}
#bst-svg-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    min-width: 500px;
    min-height: 500px;
    z-index: 1;
    pointer-events: none;
}
#bst-svg-container line {
    stroke: var(--text-light);
    stroke-width: 3;
    transition: all 0.4s ease-in-out;
}

/* --- Graph Visualization --- */
#graph-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    cursor: crosshair;
}
.graph-node {
    position: absolute;
    width: 45px;
    height: 45px;
    background-color: var(--primary-color);
    color: white;
    font-weight: 600;
    font-size: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 3px solid var(--primary-hover);
    z-index: 10;
    cursor: pointer;
    transition: all 0.3s ease;
}
.graph-node:hover { transform: scale(1.1); }
.graph-node.selected { border-color: #F59E0B; }
#graph-svg-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 5;
    pointer-events: none;
}
#graph-svg-container line {
    stroke: var(--text-light);
    stroke-width: 3;
}
#graph-svg-container text {
    font-family: var(--font-family);
    font-size: 0.9rem;
    font-weight: 600;
    fill: var(--primary-color);
    text-anchor: middle;
    pointer-events: none;
}
.node-distance {
    position: absolute;
    bottom: -20px;
    font-size: 0.8rem;
    font-weight: 700;
    color: #F87171;
    background-color: var(--panel-bg);
    padding: 1px 4px;
    border-radius: 4px;
}
#graph-svg-container line.path {
    stroke: #F59E0B;
    stroke-width: 4;
}

/* --- Responsive Design --- */
@media (max-width: 1200px) {
    #app-container {
        grid-template-columns: 240px 1fr 280px;
        padding: 1rem;
        gap: 1rem;
    }
}
@media (max-width: 992px) {
    #app-container {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
    }
    #visualization-panel { min-height: 40vh; }
    .panel { padding: 1rem; }
    .main-nav {
        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-rows: auto auto;
        height: auto;
        padding: 0.5rem 1rem;
    }
    .logo {
        grid-column: 1 / 2;
        grid-row: 1 / 2;
    }
    #theme-toggle-btn {
        grid-column: 2 / 3;
        grid-row: 1 / 2;
        justify-self: end;
    }
    .nav-links {
        grid-column: 1 / 3;
        grid-row: 2 / 3;
        width: 100%;
        justify-content: center;
        margin-top: 0.5rem;
        gap: 0.5rem;
    }
    #stack-queue-container { width: 120px; }
    .stack-item { width: 80px; height: 35px; }
}