@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&display=swap');

:root {
    /* Material 3 Royal Amethyst Palette (Light) */
    --primary: #6750a4;
    --on-primary: #ffffff;
    --primary-container: #eaddff;
    --on-primary-container: #21005d;
    
    --secondary: #625b71;
    --secondary-container: #e8def8;
    
    --tertiary: #7d5260;
    --neutral: #939094;
    --surface: #fef7ff;
    --outline: #79747e;
    
    --bg-color: #fbf8ff;
    --panel-bg: #ffffff;
    --text-main: #1c1b1f;
    --text-muted: #49454f;

    /* Logic Colors */
    --cell-empty: #ffffff;
    --cell-border: #cac4d0;
    --cell-wall: #1d1b20;
    --cell-start: #3f51b5; /* Indigo */
    --cell-goal: #e91e63;  /* Pink */
    --cell-visited: #f3e5f5;
    --cell-search: #e1bee7;
    --cell-path: #ffd600; /* Vibrant Gold */
    --cell-weight: #7d5260; /* Tertiary for weights */

    --shadow-sm: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
    --shadow-md: 0 4px 12px rgba(103, 80, 164, 0.15);
    --shadow-lg: 0 10px 20px rgba(0,0,0,0.08);
    
    --radius-md: 12px;
    --radius-lg: 28px;
    
    --transition: all 0.3s cubic-bezier(0.2, 0, 0, 1);
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.5;
    min-height: 100vh;
    padding: 1.5rem;
    display: flex;
    justify-content: center;
}

.container {
    max-width: 1600px;
    width: 100%;
    display: flex;
    flex-direction: column;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding: 0 1rem;
}

header h1 {
    font-size: 2.2rem;
    font-weight: 700;
    color: var(--primary);
    letter-spacing: -0.02em;
}

.learn-group {
    display: flex;
    gap: 0.75rem;
}

.learn-btn {
    background: var(--secondary-container);
    color: var(--on-primary-container);
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}

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

/* --- Layout --- */
.main-content {
    display: flex;
    gap: 1.5rem;
    flex: 1;
    align-items: stretch;
}

/* --- Grid Area (Left) --- */
.grid-container {
    flex: 1;
    background: var(--panel-bg);
    border-radius: var(--radius-lg);
    padding: 1.5rem; /* Reduced padding slightly */
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: var(--shadow-md);
    border: 1px solid rgba(103, 80, 164, 0.05);
    overflow: hidden;
    max-height: calc(100vh - 180px); /* Hard constraint */
}

#grid {
    display: grid;
    grid-template-columns: repeat(var(--cols, 20), 1fr);
    grid-template-rows: repeat(var(--rows, 15), 1fr);
    width: 100%;
    height: 100%; /* Use height 100% to fill constrained container */
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: var(--cols) / var(--rows);
    object-fit: contain; /* For browser logic */
    gap: 1px;
    background: var(--cell-border);
    border: 2px solid var(--cell-border);
    border-radius: 8px;
    user-select: none;
}

.cell {
    background: var(--cell-empty);
    transition: background 0.15s ease, transform 0.1s ease;
    cursor: crosshair;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 600;
    color: var(--primary);
}

.cell:hover {
    background: var(--primary-container);
    z-index: 5;
    transform: scale(1.1);
    box-shadow: var(--shadow-sm);
    border-radius: 4px;
}

.cell.wall { background: var(--cell-wall); animation: pop 0.2s ease; }
.cell.start { background: var(--cell-start); border-radius: 4px; box-shadow: 0 0 12px rgba(63, 81, 181, 0.4); }
.cell.goal { background: var(--cell-goal); border-radius: 4px; box-shadow: 0 0 12px rgba(233, 30, 99, 0.4); }
.cell.weight { 
    background: var(--cell-weight); 
    opacity: 0.6; 
    background-image: radial-gradient(circle, #fff 10%, transparent 11%);
    background-size: 4px 4px;
}
.cell.visited { background: var(--cell-visited); animation: visitedAnim 0.4s forwards; }
.cell.visited-search { background: var(--cell-search); transform: scale(0.6); border-radius: 50%; }
.cell.path { background: var(--cell-path); z-index: 10; animation: pathAnim 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards; box-shadow: var(--shadow-sm); }

/* --- Controls Panel (Right) --- */
.controls-panel {
    flex: 0 0 360px;
    background: var(--panel-bg);
    border-radius: var(--radius-lg);
    padding: 2rem;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    overflow-y: auto;
}

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

.control-group h3 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--secondary);
    font-weight: 700;
    border-bottom: 2px solid var(--secondary-container);
    padding-bottom: 0.4rem;
}

/* Inputs */
.input-row { display: flex; gap: 0.75rem; }
label { font-size: 0.8rem; font-weight: 600; color: var(--text-muted); flex: 1; display: flex; flex-direction: column; gap: 0.4rem; }
input[type="number"], select {
    padding: 0.7rem;
    border: 1px solid var(--outline);
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.9rem;
    background: var(--surface);
}

/* Buttons */
.btn-group { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
button {
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
    border: none;
    border-radius: 12px;
    padding: 0.8rem;
    transition: var(--transition);
}

.primary-btn { background: var(--primary); color: white; box-shadow: var(--shadow-sm); }
.primary-btn:hover:not(:disabled) { background: #4f378b; transform: translateY(-2px); box-shadow: var(--shadow-md); }

.secondary-btn { background: var(--secondary-container); color: var(--on-primary-container); }
.secondary-btn:hover:not(:disabled) { background: var(--primary-container); }

.action-btn {
    background: var(--primary);
    color: white;
    font-size: 1rem;
    padding: 1rem;
    width: 100%;
}

/* Radio Labels */
.radio-group { display: flex; flex-direction: column; gap: 0.5rem; }
.radio-label {
    display: flex;
    align-items: center;
    padding: 0.8rem 1rem;
    background: var(--surface);
    border: 1px solid var(--outline);
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
}
.radio-label.active { background: var(--primary-container); border-color: var(--primary); color: var(--on-primary-container); }
.radio-label input { display: none; }
.swatch { width: 12px; height: 12px; border-radius: 3px; margin-right: 0.8rem; }

/* Analysis Results */
.results-panel { background: var(--surface); padding: 1.25rem; border-radius: 16px; border: 1px dashed var(--primary); }
.results-panel p { display: flex; justify-content: space-between; margin-bottom: 0.6rem; font-size: 0.9rem; color: var(--text-muted); }
.results-panel strong { color: var(--text-main); }

/* --- Modals --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal-content {
    background: white;
    width: 90%;
    max-width: 600px;
    border-radius: var(--radius-lg);
    padding: 2.5rem;
    box-shadow: var(--shadow-lg);
    position: relative;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
}

.close-modal { position: absolute; top: 1.5rem; right: 1.5rem; font-size: 1.5rem; cursor: pointer; color: var(--neutral); }
.modal-content h2 { color: var(--primary); margin-bottom: 1rem; }
.modal-content p { margin-bottom: 1rem; line-height: 1.6; }

/* --- Animations --- */
@keyframes pop { from { transform: scale(0.5); } to { transform: scale(1); } }
@keyframes visitedAnim { 0% { scale: 0.3; border-radius: 50%; background: var(--primary); } 100% { scale: 1; border-radius: 0; background: var(--cell-visited); } }
@keyframes pathAnim { 0% { scale: 0; } 100% { scale: 1; } }

/* Template Buttons */
.template-group .btn-group {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.4rem;
}

.template-btn {
    font-size: 0.7rem;
    padding: 0.4rem;
    background: var(--surface-container-low);
    border: 1px solid var(--outline-variant);
    color: var(--on-surface);
}

.template-btn:hover {
    background: var(--primary-container);
    color: var(--on-primary-container);
    border-color: var(--primary);
}

    /* Academic Modals */
    .modal-content.academic {
        width: 80%;
        max-width: 1000px;
        background: #fff;
        border-radius: 16px;
        padding: 2.5rem;
    }

    .modal-body-layout {
        display: grid;
        grid-template-columns: 1.2fr 1fr;
        gap: 2.5rem;
        align-items: start;
    }

    .theory-section h2 {
        color: var(--primary);
        font-size: 1.8rem;
        margin-bottom: 1rem;
    }

    .theory-section p {
        margin-bottom: 1.2rem;
        line-height: 1.6;
        color: var(--on-surface-variant);
    }

    .theory-section ul {
        padding-left: 1.2rem;
        margin-top: 0.5rem;
    }

    .theory-section li {
        margin-bottom: 0.6rem;
        color: var(--on-surface-variant);
    }

    .theory-img {
        width: 100%;
        border-radius: 12px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.08);
        border: 1px solid var(--outline-variant);
        transition: transform 0.3s ease;
    }

    .theory-img:hover {
        transform: scale(1.02);
    }

    @media (max-width: 900px) {
        .modal-body-layout {
            grid-template-columns: 1fr;
        }
        .modal-content.academic {
            width: 95%;
            padding: 1.5rem;
        }
    }

    /* Modal Adjustments */
    .modal-content {
        position: relative;
        overflow-y: auto;
        max-height: 90vh;
    }

#graph-container {
    cursor: grab;
    height: 100%;
    min-height: 400px;
    background-color: #fff;
    overflow: hidden;
}

#graph-container:active {
    cursor: grabbing;
}

.hidden { display: none !important; }

@media (max-width: 1200px) {
    .main-content { flex-direction: column; }
    .controls-panel { width: 100%; flex: none; order: 2; }
    .grid-container { order: 1; min-height: 400px; }
    header { flex-direction: column; gap: 1rem; text-align: center; }
}

/* Responsive Sidebar */
@media (max-width: 1100px) {
    .main-content {
        flex-direction: column;
    }
    .controls-panel {
        width: 100%;
    }
}