:root {
    --primary-color: #6366f1;
    --secondary-color: #8b5cf6;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --bg-color: #0f172a;
    --surface-color: #1e293b;
    --surface-light: #334155;
    --text-color: #f1f5f9;
    --text-muted: #94a3b8;
    --border-radius: 12px;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4);
    --grid-size: 5;
    --cell-size: 60px;
}

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

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: linear-gradient(135deg, var(--bg-color) 0%, #1e1b4b 100%);
    color: var(--text-color);
    min-height: 100vh;
    padding: 8px;
    overflow-x: hidden;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
}

.container {
    max-width: 600px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

header {
    text-align: center;
    padding: 12px;
    background: var(--surface-color);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    
    h1 {
        font-size: clamp(1.5rem, 5vw, 2rem);
        margin-bottom: 4px;
        background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
    }
    
    .subtitle {
        color: var(--text-muted);
        font-size: clamp(0.8rem, 3vw, 0.95rem);
    }
}

.game-info {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    
    > div {
        flex: 1;
        background: var(--surface-color);
        padding: 8px 12px;
        border-radius: var(--border-radius);
        text-align: center;
        font-size: 0.9rem;
        font-weight: 600;
        box-shadow: var(--shadow);
        
        span {
            color: var(--primary-color);
            font-size: 1.1rem;
        }
    }
}

.grid-container {
    display: grid;
    grid-template-columns: repeat(var(--grid-size), 1fr);
    gap: 4px;
    padding: 12px;
    background: var(--surface-color);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg);
    justify-content: center;
    margin: 0 auto;
    max-width: 100%;
    aspect-ratio: 1;
}

.cell {
    aspect-ratio: 1;
    background: var(--surface-light);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(1.5rem, 8vw, 2.5rem);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    
    .robot-emoji {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 2;
    }
    
    &.robot {
        background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    }
    
    &.goal {
        background: linear-gradient(135deg, var(--success-color), #059669);
        animation: glow 2s infinite;
    }
    
    &.wall {
        background: linear-gradient(135deg, #475569, #334155);
        box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.4);
    }
    
    &.trap {
        background: linear-gradient(135deg, var(--danger-color), #dc2626);
        animation: danger-pulse 1.5s infinite;
    }
    
    &.visited {
        background: rgba(99, 102, 241, 0.2);
    }
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes glow {
    0%, 100% { box-shadow: 0 0 10px rgba(16, 185, 129, 0.5); }
    50% { box-shadow: 0 0 20px rgba(16, 185, 129, 0.8); }
}

@keyframes danger-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

.instruction-panel {
    background: var(--surface-color);
    border-radius: var(--border-radius);
    padding: 12px;
    box-shadow: var(--shadow);
    
    h3 {
        margin-bottom: 10px;
        font-size: 1rem;
        color: var(--primary-color);
    }
}

.instruction-slots {
    display: flex;
    gap: 6px;
    margin-bottom: 10px;
    flex-wrap: wrap;
    min-height: 60px;
}

.slot {
    width: 55px;
    height: 55px;
    background: var(--surface-light);
    border: 2px dashed var(--text-muted);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8rem;
    transition: all 0.3s ease;
    position: relative;
    
    &.filled {
        border-style: solid;
        border-color: var(--primary-color);
        background: rgba(99, 102, 241, 0.1);
    }
    
    &.drag-over {
        background: rgba(16, 185, 129, 0.3);
        border-color: #10b981;
        border-width: 3px;
        transform: scale(1.05);
        box-shadow: 0 0 20px rgba(16, 185, 129, 0.8);
    }
    
    .remove-btn {
        position: absolute;
        top: -8px;
        right: -8px;
        width: 20px;
        height: 20px;
        background: var(--danger-color);
        color: white;
        border: none;
        border-radius: 50%;
        font-size: 12px;
        cursor: pointer;
        display: none;
        align-items: center;
        justify-content: center;
        
        &:active {
            transform: scale(0.9);
        }
    }
    
    &.filled .remove-btn {
        display: flex;
    }
}

.controls {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.btn {
    flex: 1;
    min-width: 100px;
    padding: 10px 16px;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: var(--shadow);
    touch-action: manipulation;
    
    &:active {
        transform: translateY(2px);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }
    
    &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }
}

.btn-run {
    background: linear-gradient(135deg, var(--success-color), #059669);
    color: white;
    
    &:hover:not(:disabled) {
        filter: brightness(1.1);
    }
}

.btn-reset {
    background: linear-gradient(135deg, var(--warning-color), #d97706);
    color: white;
    
    &:hover {
        filter: brightness(1.1);
    }
}

.btn-clear {
    background: linear-gradient(135deg, #64748b, #475569);
    color: white;
    
    &:hover {
        filter: brightness(1.1);
    }
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    
    &:hover {
        filter: brightness(1.1);
    }
}

.arrow-palette {
    background: var(--surface-color);
    border-radius: var(--border-radius);
    padding: 12px;
    box-shadow: var(--shadow);
    
    h3 {
        margin-bottom: 10px;
        font-size: 1rem;
        color: var(--secondary-color);
    }
    
    .arrows {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
    }
}

.arrow {
    background: var(--surface-light);
    border: 2px solid var(--primary-color);
    border-radius: 8px;
    padding: 12px;
    font-size: 2rem;
    text-align: center;
    cursor: grab;
    transition: all 0.2s ease;
    touch-action: none;
    
    &:active {
        cursor: grabbing;
        transform: scale(1.1);
        box-shadow: var(--shadow-lg);
        z-index: 1000;
    }
    
    &.dragging {
        opacity: 0.5;
    }
}

.message {
    min-height: 32px;
    padding: 8px 16px;
    border-radius: 8px;
    text-align: center;
    font-weight: 600;
    font-size: 0.9rem;
    transition: all 0.3s ease;
    
    &.success {
        background: rgba(16, 185, 129, 0.2);
        border: 2px solid var(--success-color);
        color: var(--success-color);
    }
    
    &.error {
        background: rgba(239, 68, 68, 0.2);
        border: 2px solid var(--danger-color);
        color: var(--danger-color);
    }
    
    &.info {
        background: rgba(99, 102, 241, 0.2);
        border: 2px solid var(--primary-color);
        color: var(--primary-color);
    }
}

.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 20px;
    
    &.show {
        display: flex;
        animation: fadeIn 0.3s ease;
    }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: var(--surface-color);
    border-radius: var(--border-radius);
    padding: 32px;
    max-width: 500px;
    width: 100%;
    box-shadow: var(--shadow-lg);
    animation: slideUp 0.3s ease;
    
    h2 {
        font-size: 2rem;
        margin-bottom: 16px;
        background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
    }
    
    p {
        margin-bottom: 12px;
        font-size: 1.1rem;
        line-height: 1.6;
        
        &.success-message {
            color: var(--success-color);
            font-weight: 600;
            font-size: 1.3rem;
        }
    }
    
    ul {
        list-style: none;
        margin: 20px 0;
        
        li {
            padding: 8px 0;
            font-size: 1.1rem;
        }
    }
    
    .btn {
        width: 100%;
        margin-top: 20px;
    }
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Mobile optimizations */
@media (max-width: 480px) {
    :root {
        --cell-size: 50px;
    }
    
    body {
        padding: 12px;
    }
    
    .container {
        gap: 16px;
    }
    
    header {
        padding: 16px;
    }
    
    .grid-container {
        padding: 12px;
        gap: 3px;
    }
    
    .slot {
        width: 50px;
        height: 50px;
        font-size: 1.8rem;
    }
    
    .arrow {
        padding: 12px;
        font-size: 2rem;
    }
    
    .btn {
        padding: 12px 16px;
        font-size: 0.9rem;
    }
}

/* Landscape mobile */
@media (max-height: 600px) and (orientation: landscape) {
    .container {
        gap: 12px;
    }
    
    header {
        padding: 12px;
        
        h1 {
            font-size: 1.5rem;
        }
    }
    
    .grid-container {
        max-width: 300px;
    }
}

/* Tablet and larger */
@media (min-width: 768px) {
    :root {
        --cell-size: 70px;
    }
    
    body {
        padding: 24px;
    }
    
    .container {
        gap: 24px;
    }
    
    .arrow-palette .arrows {
        grid-template-columns: repeat(4, 1fr);
    }
}
