:root {
    --bg-color: #0f172a;
    --grid-bg: #1e293b;
    --cell-border: #334155;
    --wall-color: #0f172a;
    --start-color: #22c55e;
    --end-color: #ef4444;
    --visited-color: #3b82f6;
    --visited-secondary: #60a5fa;
    --path-color: #facc15;
    --text-color: #f8fafc;
    --btn-bg: #2563eb;
    --btn-hover: #1d4ed8;
}

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: system-ui, -apple-system, sans-serif;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

header {
    padding: 1rem;
    background-color: #1e293b;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
    z-index: 10;
}

h1 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 600;
}

.instructions {
    background: #0f172a;
    padding: 8px 15px;
    border-radius: 8px;
    font-size: 0.85rem;
    border: 1px solid var(--cell-border);
    max-width: 800px;
    text-align: center;
}

.instructions p {
    margin: 0;
    color: #94a3b8;
}

.instructions strong {
    color: var(--path-color);
}

.controls {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
}

button {
    background-color: var(--btn-bg);
    color: white;
    border: none;
    padding: 0.6rem 1.2rem;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s ease;
    font-size: 0.9rem;
}

button:hover:not(:disabled) {
    background-color: var(--btn-hover);
    transform: translateY(-1px);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

button.secondary {
    background-color: #475569;
}

.legend {
    display: flex;
    gap: 15px;
    font-size: 0.85rem;
    margin-top: 5px;
    flex-wrap: wrap;
    justify-content: center;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
}

.box {
    width: 16px;
    height: 16px;
    border-radius: 3px;
}

.grid-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    overflow: auto;
    position: relative;
}

#grid {
    display: grid;
    border: 1px solid var(--cell-border);
    background-color: var(--grid-bg);
    user-select: none;
    touch-action: none;
}

.cell {
    width: 25px;
    height: 25px;
    border: 1px solid var(--cell-border);
    box-sizing: border-box;
    background-color: transparent;
}

.cell.start {
    background-color: var(--start-color);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z'/%3E%3C/svg%3E");
    background-size: 70%;
    background-repeat: no-repeat;
    background-position: center;
    animation: pop 0.3s ease-out;
}

.cell.end {
    background-color: var(--end-color);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z'/%3E%3C/svg%3E");
    background-size: 60%;
    background-repeat: no-repeat;
    background-position: center;
    animation: pop 0.3s ease-out;
}

.cell.wall {
    background-color: #475569;
    border-color: #334155;
    animation: wallAnimation 0.2s ease-out forwards;
}

.cell.visited {
    animation: visitedAnimation 1s ease-out forwards;
}

.cell.path {
    background-color: var(--path-color);
    border-color: #eab308;
    animation: pathAnimation 0.5s ease-out forwards;
    z-index: 2;
}

@keyframes wallAnimation {
    0% { transform: scale(0.3); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

@keyframes visitedAnimation {
    0% { transform: scale(0.3); background-color: #312e81; border-radius: 50%; }
    50% { background-color: var(--visited-secondary); }
    100% { transform: scale(1); background-color: var(--visited-color); }
}

@keyframes pathAnimation {
    0% { transform: scale(0.5); background-color: #fef08a; }
    100% { transform: scale(1); background-color: var(--path-color); }
}

@keyframes pop {
    0% { transform: scale(0.5); }
    80% { transform: scale(1.2); }
    100% { transform: scale(1); }
}

@media (max-width: 600px) {
    h1 { font-size: 1.2rem; }
    .cell { width: 20px; height: 20px; }
    .instructions { font-size: 0.7rem; }
}