/* --- Global Settings --- */
/* We use variables here so we can easily change colors or sizes later */
:root {
    --cell-size: 60px;          /* Width/Height of one tile */
    --debug-cell-size: 30px;    /* Size of the small debug grids at the bottom */
    --bg-color: #f8fafc;
    --primary: #3b82f6;         /* Blue */
    --primary-dark: #1d4ed8;    /* Dark Blue */
    --success: #10b981;         /* Green */
    --text-main: #334155;
    --shadow-card: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    margin: 0;
    min-height: 100vh;
    overflow-y: auto;

    /* Global Interface: Disable selection to prioritize drag-and-drop interactions */
    -webkit-user-select: none;
    user-select: none;
}

h1 { margin-bottom: 1rem; color: #1e293b; }

/* --- Controls & Layout --- */
.controls-container {
    background: white;
    padding: 1.5rem;
    border-radius: 16px;
    box-shadow: var(--shadow-card);
    width: 100%;
    max-width: 850px;
    margin-bottom: 1rem;
    border: 1px solid #e2e8f0;
    
    /* Layout: Side-by-Side Flexbox */
    display: flex;
    gap: 1.5rem;
    align-items: flex-start;
}

/* Left column: Textarea and Primary Action */
.config-left {
    flex: 1; 
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.config-left label {
    font-weight: 800;
    font-size: 1.1rem;
    color: #1e293b;
    margin-bottom: 0.5rem;
}

textarea {
    width: 100%;
    height: 180px; /* Slightly taller to accommodate manual input */
    font-family: 'Menlo', 'Monaco', monospace; /* Monospace font helps numbers align nicely */
    padding: 1rem;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    resize: vertical;
    box-sizing: border-box;
    background-color: #fcfcfc;
    font-size: 0.95rem;
    line-height: 1.5;

    /* Input Override: Ensure text remains selectable and cursor is visible */
    -webkit-user-select: text;
    user-select: text;
    cursor: text;
    transition: border-color 0.2s;
}

textarea:focus {
    outline: none;
    border-color: var(--primary);
    background-color: #fff;
}

/* Right column: Controls (Dropdown + Random Gen) */
.config-right {
    width: 260px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    gap: 1rem;
    padding-top: 1.8rem; /* Align visual top with text area */
}

.preset-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: #64748b;
    margin-bottom: 0.25rem;
    display: block;
}

/* --- RANDOM GENERATOR STYLES --- */
.random-gen-group {
    background: #f1f5f9;
    padding: 12px;
    border-radius: 12px;
    border: 1px solid #e2e8f0;
}

.random-inputs {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 10px;
}

.random-inputs input {
    width: 60px;
    padding: 6px;
    border-radius: 6px;
    border: 1px solid #cbd5e1;
    text-align: center;
    font-weight: bold;
    font-size: 0.9rem;
}

.btn-random {
    width: 100%;
    font-size: 0.85rem;
    padding: 0.6rem;
    background-color: #475569;
    color: white;
}
.btn-random:hover { background-color: #334155; }

/* --- CUSTOM DROPDOWN STYLING --- */
.custom-dropdown {
    position: relative;
    width: 100%;
    font-size: 0.9rem;
    font-weight: 600;
    color: #334155;
    margin-bottom: 5px;
}

.dropdown-trigger {
    padding: 0.6rem 0.8rem;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    background-color: #f8fafc;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.2s;
}

.dropdown-trigger:hover {
    border-color: #94a3b8; 
    background-color: #f1f5f9;
}

.dropdown-trigger.active {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

.dropdown-arrow {
    font-size: 0.7em;
    color: #64748b;
    transition: transform 0.2s;
}
.dropdown-trigger.active .dropdown-arrow {
    transform: rotate(180deg);
}

/* The Dropdown Menu List */
.dropdown-menu {
    position: absolute;
    top: 105%; /* Just below the trigger */
    left: 0;
    width: 100%;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    z-index: 1000; /* Sit on top of everything */
    display: none; /* Hidden by default */
    overflow: hidden;
    flex-direction: column;
}

.dropdown-menu.show { display: flex; }

.dropdown-item {
    padding: 0.6rem 0.8rem;
    cursor: pointer;
    transition: background 0.1s;
}

.dropdown-item:hover {
    background-color: #eff6ff;
    color: var(--primary);
}

/* --- Buttons --- */
.action-bar {
    margin-top: 1rem;
    display: flex;
    gap: 1rem;
    justify-content: center;
}

button {
    padding: 0.7rem 1.2rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 700;
    font-size: 0.95rem;
    transition: all 0.2s ease;
}

/* Primary Load Button placement styling */
.btn-load {
    width: 100%;
    margin-top: 1rem;
    box-shadow: 0 4px 6px -1px rgba(59, 130, 246, 0.2);
}

.btn-primary { background-color: var(--primary); color: white; }
.btn-primary:hover { background-color: var(--primary-dark); transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }

.btn-undo { background-color: #64748b; color: white; } /* Grey for Undo */
.btn-undo:hover { background-color: #475569; }

.btn-viz { background-color: #8b5cf6; color: white; } /* Purple button for 3D */
.btn-viz:hover { background-color: #7c3aed; }
.btn-viz.active { background-color: #6d28d9; box-shadow: inset 0 2px 4px rgba(0,0,0,0.2); }

.btn-check { background-color: var(--success); color: white; } /* Green button for Check */
.btn-check:hover { background-color: #059669; }

/* --- Score Dashboard --- */
.stats-bar {
    display: flex;
    gap: 2rem;
    background: white;
    padding: 0.75rem 2rem;
    border-radius: 50px;
    box-shadow: var(--shadow-card);
    font-weight: 700;
    color: #475569;
    margin-bottom: 1.5rem;
}

/* --- Result Box (Success/Fail Messages) --- */
.result-box {
    margin-top: 2rem;
    padding: 1.5rem;
    border-radius: 12px;
    text-align: center;
    display: none; /* Hidden until user clicks 'Check configuration' */
    width: 100%;
    max-width: 800px;
    background: white;
    box-shadow: var(--shadow-card);
}
.result-success { border: 2px solid #10b981; background-color: #f0fdf4; }
.result-fail { border: 2px solid #ef4444; background-color: #fef2f2; }

.result-title { font-size: 1.2rem; margin-bottom: 0.5rem; display: block; }

/* --- Debug Grids (The small grids at the bottom) --- */
.debug-container {
    display: flex;
    flex-wrap: wrap;
    gap: 2rem;
    justify-content: center;
    margin-top: 1.5rem;
    border-top: 1px solid #e2e8f0;
    padding-top: 1.5rem;
}

.debug-section {
    display: flex;
    flex-direction: column;
    align-items: center;
}
.debug-section h4 { margin: 0 0 10px 0; font-size: 0.9rem; color: #64748b; text-transform: uppercase; letter-spacing: 0.05em; }

.debug-grid { display: grid; gap: 2px; }
.debug-cell {
    width: var(--debug-cell-size);
    height: var(--debug-cell-size);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.8rem;
    font-weight: bold;
    border-radius: 4px;
    color: white;
}

/* Debug Colors */
.r-yes { background-color: #10b981; } /* Green: Reaches the bottom */
.r-no { background-color: #ef4444; }  /* Red: Trapped water */
.d-val { background-color: #64748b; } /* Grey: Valid distance */
.d-inf { background-color: #ef4444; } /* Red: Unreachable */

/* --- 2D Game Area --- */
.game-area-2d {
    padding: 20px;
    background: #e2e8f0;
    border-radius: 12px;
    margin-bottom: 10px;
}

#grid2D { display: grid; gap: 4px; }

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    background: white;
    border: 1px solid #94a3b8;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.1em;
    font-weight: bold;
    color: #475569;
    cursor: grab; /* Shows the "hand" icon so user knows it's draggable */
    user-select: none;
    transition: transform 0.2s, background-color 0.2s;
    box-shadow: 0 2px 0 #cbd5e1;
}

.cell:active { cursor: grabbing; transform: scale(0.95); }
.cell.dragging { opacity: 0.4; border-color: var(--primary); }
.cell.min-cell { background-color: #d1fae5; border-color: #34d399; color: #065f46; } /* Highlight the lowest number */

/* --- History Log --- */
.history-panel {
    width: 100%;
    max-width: 800px;
    background: white;
    border-radius: 8px;
    padding: 15px;
    margin-top: 20px;
    box-shadow: var(--shadow-card);
}
.history-panel h3 { margin: 0 0 10px 0; font-size: 1rem; border-bottom: 1px solid #eee; padding-bottom: 10px; }

#historyList {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 150px;
    overflow-y: auto;
    font-family: monospace;
}
.history-item {
    padding: 6px 0;
    border-bottom: 1px solid #f1f5f9;
    font-size: 0.9rem;
    display: flex;
    justify-content: space-between;
}

/* --- 3D Visualization Embedded Styles --- */

.view-3d-panel {
    display: none; /* Hidden by default */
    width: 100%;
    max-width: 850px;
    height: 450px; /* Fixed height for the inline view */
    background-color: #0f172a; /* Dark background for better 3D contrast */
    border-radius: 12px;
    margin-top: 20px;
    position: relative;
    box-shadow: inset 0 0 20px rgba(0,0,0,0.5);
    overflow: hidden; /* Important so 3D elements don't spill out */
}

.view-3d-panel.active {
    display: block;
}

.view-instructions {
    position: absolute;
    bottom: 15px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255,255,255,0.6);
    font-size: 0.85rem;
    pointer-events: none;
    background: rgba(0,0,0,0.4);
    padding: 5px 12px;
    border-radius: 20px;
    z-index: 10;
}

/* This container holds the 3D scene */
.scene-container {
    width: 100%;
    height: 100%;
    perspective: 1200px; /* Simulates depth/camera distance */
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: move; /* Indicates you can drag to rotate */

    /* Optimized for interaction; prevents highlighting during rotation */
    -webkit-user-select: none;
    user-select: none;
}

.pivot-point {
    position: relative;
    transform-style: preserve-3d;
    /* Optimized for smooth animation */
    will-change: transform;
}

/* --- 3D Block Styles --- */
/* The "Pillar" is the base footprint on the floor */
.pillar {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    transform-style: preserve-3d;
    background: rgba(0,0,0,0.1);

    /* Prevent selecting pillar text during rotation */
    -webkit-user-select: none;
    user-select: none;
}

.wall {
    position: absolute;
    background-color: inherit;
    pointer-events: none;
}

/* The Top Face (where the number is) */
.wall-cap {
    width: 100%; height: 100%;
    display: flex; align-items: center; justify-content: center;
    font-weight: bold; font-size: 1.2rem; color: #1e293b;
    border: 1px solid rgba(255,255,255,0.3);
    background: inherit;
    filter: brightness(1.2);
    transform: translateZ(var(--pillar-height)); /* Moves this face UP in 3D space */
}

/* These classes construct the sides of the 3D block.
   We rotate divs 90 degrees to make them stand up like walls.
*/
.wall-south {
    left: 0; bottom: 0; width: 100%; height: var(--pillar-height);
    transform-origin: bottom; transform: rotateX(-90deg); filter: brightness(0.7);
}
.wall-north {
    left: 0; top: 0; width: 100%; height: var(--pillar-height);
    transform-origin: top; transform: rotateX(90deg); filter: brightness(0.75);
}
.wall-east {
    right: 0; top: 0; width: var(--pillar-height); height: 100%;
    transform-origin: right; transform: rotateY(90deg); filter: brightness(0.9);
}
.wall-west {
    left: 0; top: 0; width: var(--pillar-height); height: 100%;
    transform-origin: left; transform: rotateY(-90deg); filter: brightness(0.95);
}

/* Responsive */
@media (max-width: 500px) {
    .controls-container { flex-direction: column; }
    .config-right { width: 100%; padding-top: 0; }
}
