:root {
    --bg-color: #0f172a; /* Slate 900 */
    --panel-bg: rgba(30, 41, 59, 0.7); /* Slate 800 with opacity */
    --panel-border: rgba(255, 255, 255, 0.1);
    --primary: #3b82f6; /* Blue 500 */
    --primary-hover: #2563eb;
    --secondary: #64748b;
    --secondary-hover: #475569;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --grid-bg: #1e293b;
    --cell-border: rgba(255, 255, 255, 0.05);
    --maze-border: #ef4444; /* Red border exactly as requested */
    --command-bg: rgba(59, 130, 246, 0.2);
    --command-border: rgba(59, 130, 246, 0.5);
    --cell-size: min(5vw, 40px); /* Responsive cell size */
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Outfit', sans-serif;
}

body {
    background: linear-gradient(135deg, var(--bg-color), #020617);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
    overflow-y: auto;
}

.game-container {
    width: 95%;
    max-width: 1200px;
    min-height: 90vh;
    height: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Glassmorphism Classes */
.glass-header, .glass-panel {
    background: var(--panel-bg);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--panel-border);
    border-radius: 1rem;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
}

/* Header */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    gap: 1rem;
    flex-wrap: wrap;
}

.title h1 {
    font-size: 1.8rem;
    font-weight: 800;
    background: linear-gradient(to right, #60a5fa, #c084fc);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.title p {
    font-size: 0.9rem;
    color: var(--text-muted);
}

.stats {
    display: flex;
    gap: 1.5rem;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.stat-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(0,0,0,0.2);
    padding: 0.35rem 0.7rem;
    border-radius: 0.5rem;
}

.stat-label {
    font-size: 0.68rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.stat-value {
    font-size: 1.05rem;
    font-weight: 800;
    color: #fff;
    line-height: 1;
    vertical-align: middle;
}

.stat-value-inline {
    display: inline-flex;
    align-items: baseline;
    gap: 0.1rem;
}

.stat-unit {
    font-size: 0.72em;
    line-height: 1;
    opacity: 0.9;
}

/* Main Content */
.main-content {
    display: flex;
    flex: 1;
    gap: 1.5rem;
    min-height: 0; /* Important for flex child scrolling */
    min-width: 0;
}

.left-panel {
    flex: 1;
    min-width: 300px;
    max-width: 350px;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    overflow-y: auto;
}

.right-panel {
    flex: 2;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 2rem;
    min-width: 0;
    overflow: hidden;
}

.panel-section h2 {
    font-size: 1.2rem;
    margin-bottom: 1rem;
    color: var(--text-main);
    border-bottom: 1px solid var(--panel-border);
    padding-bottom: 0.5rem;
}

.flex-grow {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}

.sequence-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--panel-border);
    margin-bottom: 0.5rem;
    padding-bottom: 0.5rem;
}

.sequence-header h2 {
    border: none;
    margin: 0;
    padding: 0;
}

.sequence-tools {
    display: flex;
    gap: 0.35rem;
    margin-bottom: 0.6rem;
    flex-wrap: wrap;
}

.seq-tool-btn {
    border: 1px solid rgba(148, 163, 184, 0.4);
    background: rgba(30, 41, 59, 0.8);
    color: var(--text-main);
    border-radius: 0.35rem;
    padding: 0.2rem 0.5rem;
    font-size: 0.75rem;
    cursor: pointer;
}

.seq-tool-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

/* Command Banks & Sequence */
.command-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    min-width: 0;
}

.cmd-btn {
    background: var(--command-bg);
    border: 1px solid var(--command-border);
    color: white;
    font-size: 1.5rem;
    padding: 0.5rem;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 3.5rem;
    height: 3.5rem;
    position: relative;
}

.cmd-icon {
    line-height: 1;
}

.cmd-count-badge {
    position: absolute;
    right: 0.22rem;
    bottom: 0.16rem;
    font-size: 0.68rem;
    font-weight: 700;
    color: #e2e8f0;
    background: rgba(15, 23, 42, 0.82);
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 0.35rem;
    padding: 0.02rem 0.22rem;
    line-height: 1.1;
}

.cmd-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.cmd-btn:hover {
    background: var(--primary);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}

.cmd-btn:active {
    transform: translateY(0);
}

.sequence-box {
    background: rgba(0,0,0,0.3);
    border-radius: 0.5rem;
    padding: 1rem;
    flex-grow: 1;
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: 0.5rem;
    overflow-y: auto;
    overflow-x: hidden;
    border: 1px inset var(--panel-border);
}

.seq-item {
    font-size: 1.5rem;
    width: 2.5rem;
    height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255,255,255,0.1);
    border-radius: 0.4rem;
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    transition: all 0.2s;
    cursor: pointer;
    position: relative;
}

.seq-item:hover {
    background: rgba(255,255,255,0.16);
    transform: scale(0.98);
}

.seq-item.selected {
    border: 1px solid rgba(34, 211, 238, 0.8);
    background: rgba(34, 211, 238, 0.2);
}

.seq-item.cursor-anchor::after {
    content: '';
    position: absolute;
    left: 15%;
    bottom: -0.34rem;
    width: 70%;
    height: 0.22rem;
    border-radius: 999px;
    background: #67e8f9;
    animation: seqCursorBlink 1s steps(1, end) infinite;
}

.seq-item.executing {
    background: var(--primary);
    box-shadow: 0 0 15px var(--primary);
    transform: scale(1.1);
}

@keyframes seqCursorBlink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0.2; }
}

/* Buttons */
.controls {
    display: flex;
    gap: 1rem;
    margin-top: auto;
    flex-wrap: wrap;
}

.btn {
    flex: 1;
    padding: 0.8rem;
    border: none;
    border-radius: 0.5rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    min-width: 0;
}

.primary-btn {
    background: var(--primary);
    color: white;
}

.primary-btn:hover:not(:disabled) {
    background: var(--primary-hover);
}

.secondary-btn {
    background: var(--secondary);
    color: white;
}

.secondary-btn:hover:not(:disabled) {
    background: var(--secondary-hover);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Maze Grid */
.maze-grid {
    display: grid;
    grid-template-columns: repeat(var(--grid-width, 10), var(--cell-size));
    grid-template-rows: repeat(var(--grid-height, 10), var(--cell-size));
    background-color: var(--map-bg, var(--grid-bg));
    /* Border with same thickness as cell size as requested */
    border: var(--cell-size) solid var(--map-border, var(--maze-border));
    border-radius: 0.5rem;
    position: relative;
    box-sizing: border-box;
    width: fit-content;
    max-width: 100%;
    max-height: 100%;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
    /* For glow effect around the border */
    outline: 2px solid var(--map-outline, rgba(239, 68, 68, 0.5));
    outline-offset: 2px;
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    border: 1px solid var(--cell-border);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: calc(var(--cell-size) * 0.6);
    position: relative;
    box-sizing: border-box;
}

.cell.path {
    background-color: transparent;
}

.cell.wall {
    background-color: #334155;
    background-image: repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(0,0,0,0.1) 10px, rgba(0,0,0,0.1) 20px);
}

.edge-cell {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    box-sizing: border-box;
    border: 1px dashed var(--edge-border-color, rgba(148, 163, 184, 0.55));
    background: linear-gradient(
        135deg,
        var(--edge-bg-start, rgba(15, 23, 42, 0.45)),
        var(--edge-bg-end, rgba(30, 41, 59, 0.7))
    );
    pointer-events: none;
    z-index: 2;
}

.entity-asset-host {
    display: flex;
    justify-content: center;
    align-items: center;
}

.entity-asset {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}

.entity-asset-image {
    object-fit: contain;
}

.goal .entity-asset-image,
.wall .entity-asset-image,
.edge-cell .entity-asset-image {
    width: 84%;
    height: 84%;
}

.player .entity-asset-image {
    width: 86%;
    height: 86%;
}

.player {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: calc(var(--cell-size) * 0.7);
    transition: transform 0.25s cubic-bezier(0.1, 0.9, 0.2, 1);
    z-index: 10;
}

.player-inner {
    display: inline-block;
    will-change: transform, opacity, filter;
}

.player-inner.anim-zoom-out {
    animation-name: playerFallVoid;
    animation-timing-function: cubic-bezier(0.18, 0.8, 0.2, 1);
    animation-fill-mode: forwards;
}

.player-inner.anim-crouch {
    animation-name: playerCrouch;
    animation-timing-function: ease-in-out;
    animation-fill-mode: both;
}

.player-inner.anim-jump {
    animation-name: playerJump;
    animation-timing-function: cubic-bezier(0.2, 0.9, 0.2, 1);
    animation-fill-mode: both;
}

/* Trail System */
:root {
    --trail-color: #3b82f6;
    --trail-glow: rgba(59, 130, 246, 0.6);
}

.maze-grid.trail-success {
    outline-color: rgba(74, 222, 128, 0.8);
}

.maze-grid.trail-failure {
    outline-color: rgba(239, 68, 68, 0.8);
}

.cell.trail-cell {
    background-image:
        linear-gradient(var(--trail-color), var(--trail-color)),
        linear-gradient(var(--trail-color), var(--trail-color)),
        linear-gradient(var(--trail-color), var(--trail-color)),
        linear-gradient(var(--trail-color), var(--trail-color));
    background-size:
        16% calc(var(--trail-up, 0) * 50%),
        16% calc(var(--trail-down, 0) * 50%),
        calc(var(--trail-left, 0) * 50%) 16%,
        calc(var(--trail-right, 0) * 50%) 16%;
    background-position:
        center top,
        center bottom,
        left center,
        right center;
    background-repeat: no-repeat;
    transition: background-size 0.18s ease-out;
}

.cell.trail-node::after {
    content: '';
    position: absolute;
    width: 34%;
    height: 34%;
    border-radius: 999px;
    background: var(--trail-color);
    box-shadow: 0 0 12px var(--trail-glow);
    z-index: 1;
}

.cell.trail-animate::after {
    animation: trailPulse 0.22s ease-out;
}

.failure-marker {
    animation: failurePulse 0.4s ease-out;
}

@keyframes trailPulse {
    0% {
        transform: scale(0.6);
        opacity: 0.8;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes failurePulse {
    0% {
        transform: scale(0.5);
        opacity: 0;
    }
    50% {
        opacity: 1;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes playerFallVoid {
    0% {
        transform: translateY(0) scale(1);
        opacity: 1;
        filter: blur(0px);
    }
    100% {
        transform: translateY(35%) scale(0.12);
        opacity: 0.12;
        filter: blur(1.4px);
    }
}

@keyframes playerCrouch {
    0%, 100% {
        transform: translateY(0) scale(1, 1);
    }
    50% {
        transform: translateY(8%) scale(1.08, 0.82);
    }
}

@keyframes playerJump {
    0% {
        transform: translateY(0) scale(1);
    }
    35% {
        transform: translateY(-38%) scale(0.96, 1.06);
    }
    100% {
        transform: translateY(0) scale(1);
    }
}

.goal {
    font-size: calc(var(--cell-size) * 0.7);
    animation: bounce 2s infinite;
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(5px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.modal-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.modal {
    width: 90%;
    max-width: 400px;
    padding: 2rem;
    text-align: center;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.modal h2 {
    font-size: 2rem;
    margin-bottom: 0.5rem;
}

.hidden {
    display: none !important;
}

.mt-3 {
    margin-top: 1.5rem;
}

/* Animations */
@keyframes popIn {
    0% { transform: scale(0); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10%); }
}

/* Responsiveness */
@media (max-width: 768px) {
    body {
        align-items: flex-start;
        justify-content: flex-start;
    }

    .main-content {
        flex-direction: column-reverse; /* Grid on top, controls bottom */
        gap: 1rem;
    }
    
    .left-panel {
        max-width: 100%;
        min-width: 0;
        min-height: 0;
        padding: 1rem;
    }

    .right-panel {
        padding: 1rem;
    }

    header {
        padding: 1rem;
    }

    .title {
        min-width: 0;
        flex: 1 1 100%;
    }

    .title p {
        line-height: 1.35;
        overflow-wrap: anywhere;
    }

    .stats {
        width: 100%;
        justify-content: space-between;
        gap: 0.75rem;
    }

    .stat-box {
        flex: 1 1 0;
        min-width: 0;
        padding: 0.25rem 0.45rem;
    }

    .stat-label {
        font-size: 0.58rem;
        letter-spacing: 0.06em;
    }

    .stat-value {
        font-size: 0.9rem;
    }

    .stat-unit {
        font-size: 0.7em;
    }

    .command-grid {
        justify-content: flex-start;
    }

    .sequence-box {
        padding: 0.75rem;
    }

    .controls {
        gap: 0.75rem;
    }

    .btn {
        flex: 1 1 calc(50% - 0.375rem);
    }
    
    :root {
        --cell-size: min(calc((100vw - 5rem) / 12), 35px);
    }
}

/* Lore Overlay */
.lore-overlay,
.tutorial-overlay {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.82);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 3000;
    padding: 1rem;
}

.lore-modal,
.tutorial-modal {
    width: min(760px, 95vw);
    border-radius: 20px;
    padding: 2rem;
    border: 1px solid var(--panel-border);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

.lore-kicker {
    color: #fbbf24;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 700;
    font-size: 0.8rem;
    margin-bottom: 0.4rem;
}

.lore-header h2,
.tutorial-header h2 {
    font-size: 1.8rem;
    line-height: 1.2;
    margin-bottom: 1rem;
}

.lore-description,
.lore-story,
.lore-objective,
.tutorial-description,
.tutorial-objective {
    color: var(--text-muted);
    line-height: 1.6;
    margin-bottom: 0.7rem;
}

.lore-objective,
.tutorial-objective {
    color: #dbeafe;
    font-weight: 600;
}

.lore-progress {
    height: 8px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 999px;
    overflow: hidden;
    margin: 1.2rem 0;
}

.lore-progress-bar {
    width: 0;
    height: 100%;
    background: linear-gradient(90deg, #3b82f6, #22d3ee);
    transition: width 0.3s ease;
}

.lore-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 0.6rem;
}

.tutorial-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.tutorial-command-badge {
    padding: 0.35rem 0.65rem;
    border-radius: 999px;
    border: 1px solid rgba(34, 211, 238, 0.45);
    background: rgba(34, 211, 238, 0.15);
    color: #67e8f9;
    font-weight: 700;
    font-size: 0.8rem;
}

@media (max-width: 768px) {
    .lore-modal,
    .tutorial-modal {
        padding: 1.4rem;
        width: min(760px, calc(100vw - 2rem));
    }

    .lore-header h2,
    .tutorial-header h2 {
        font-size: 1.35rem;
    }
}

@media (max-width: 768px) and (orientation: portrait) {
    .game-container {
        width: 100%;
        max-width: none;
        min-height: 100vh;
        height: auto;
        gap: 0.75rem;
        padding: 0.35rem 0;
    }

    header {
        padding: 0.75rem 1rem;
    }

    .stats {
        gap: 0.4rem;
    }

    .stat-box {
        padding: 0.2rem 0.35rem;
        border-radius: 0.4rem;
    }

    .stat-label {
        font-size: 0.52rem;
    }

    .stat-value {
        font-size: 0.8rem;
    }

    .stat-unit {
        font-size: 0.68em;
    }

    .main-content {
        gap: 0.75rem;
    }

    .maze-grid {
        justify-self: center;
    }

    .right-panel {
        padding: 0.5rem;
    }

    .left-panel {
        padding: 0.9rem;
    }

    .modal {
        width: calc(100vw - 2rem);
    }
}
