:root {
    --color-bg-grad-start: #667eea;
    --color-bg-grad-end: #764ba2;
    --color-surface: #ffffff;
    --color-text: #212121;
    --color-muted: #616161;
    --color-accent: #3F51B5;
    --color-success: #66BB6A;
    --color-success-2: #66BB6A;
    --color-warn: #FFA726;
    --color-warn-2: #FFA726;
    --color-danger: #EF5350;
    --radius-lg: 20px;
    --radius-md: 12px;
    --radius-sm: 6px;
    --shadow-lg: 0 20px 40px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 4px 15px rgba(0, 0, 0, 0.15);
    --grid-gap: 6px;
    --path-stroke-color: #3F51B5;
    --path-stroke-width: 27px;
}

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

body {
    font-family: 'Inter', 'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, var(--color-bg-grad-start) 0%, var(--color-bg-grad-end) 100%);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 20px;
    overscroll-behavior: none; /* avoid pull-to-refresh flickers */
}

.game-container {
    background: rgba(255, 255, 255, 0.95);
    border-radius: var(--radius-lg);
    padding: 30px;
    box-shadow: var(--shadow-lg);
    max-width: 600px;
    width: 100%;
    margin: 0 auto; /* center on small screens */
    text-align: center;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

h1 {
    color: var(--color-text);
    margin-bottom: 20px;
    font-size: 40px;
    font-weight: 700;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.game-info {
    margin-bottom: 30px;
}

.game-info p {
    color: #666;
    font-size: 1.1em;
    margin-bottom: 15px;
}

.controls {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.button-group {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
    width: 100%;
}

#clearBtn {
    background: linear-gradient(135deg, #ffd43b, #fab005);
    color: #495057;
    border: none;
    padding: 12px 20px;
    border-radius: 25px;
    cursor: pointer;
    font-size: 1em;
    font-weight: bold;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(250, 176, 5, 0.3);
}

#clearBtn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(250, 176, 5, 0.4);
    background: linear-gradient(135deg, #fab005, #f08c00);
}

#newPuzzleBtn { background: #4CAF50; color: #fff; border: none; padding: 12px 20px; border-radius: 25px; cursor: pointer; font-size: 1em; font-weight: bold; transition: all 0.3s ease; box-shadow: 0 4px 15px rgba(76, 175, 80, 0.3); }
#newPuzzleBtn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(76,175,80,0.4); }
#resetBtn { background: #FFB300; color: #212121; border: none; padding: 12px 20px; border-radius: 25px; cursor: pointer; font-size: 1em; font-weight: bold; transition: all 0.3s ease; box-shadow: 0 2px 4px rgba(0,0,0,0.2); }
#resetBtn:hover { transform: translateY(-2px); box-shadow: 0 4px 6px rgba(0,0,0,0.25); }
#debugBtn { background: #AB47BC; color: #fff; border: none; padding: 12px 20px; border-radius: 25px; cursor: pointer; font-size: 1em; font-weight: bold; transition: all 0.3s ease; box-shadow: 0 2px 4px rgba(0,0,0,0.2); }
#debugBtn:hover { transform: translateY(-2px); box-shadow: 0 4px 6px rgba(0,0,0,0.25); }

#debugBtn.active {
    background: linear-gradient(135deg, #e74c3c, #c0392b);
    box-shadow: 0 4px 15px rgba(231, 76, 60, 0.4);
}

#debugBtn.active:hover {
    box-shadow: 0 6px 20px rgba(231, 76, 60, 0.5);
}

.status {
    background: color-mix(in srgb, var(--color-accent) 12%, transparent);
    padding: 8px 16px;
    border-radius: 20px;
    border: 2px solid color-mix(in srgb, var(--color-accent) 30%, transparent);
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
}

#cellsVisited, #checkpointProgress, #backtrackCount {
    color: var(--color-accent);
    font-weight: bold;
}

.grid-container {
    display: flex;
    justify-content: center;
    margin-bottom: 30px;
    position: relative;
}

.grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    grid-template-rows: repeat(6, 1fr);
    gap: var(--grid-gap);
    background: #ddd;
    padding: 10px;
    border-radius: 15px;
    box-shadow: inset 0 4px 8px rgba(0, 0, 0, 0.1);
    touch-action: none; /* enable drag gestures without scroll */
    position: relative;
    z-index: 1;
    width: min(90vw, 500px);
    aspect-ratio: 1 / 1;
}

.grid.disabled,
.grid.disabled * {
    pointer-events: none !important;
}

.grid.disabled {
    opacity: 0.6;
    filter: grayscale(0.1);
}

/* Overlay to show daily score when grid is disabled */
.grid-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    z-index: 999;
}
.grid-overlay .overlay-card {
    background: rgba(255,255,255,0.92);
    border: 2px solid #ddd;
    border-radius: 14px;
    padding: 16px 20px;
    font-weight: 700;
}

#pathSvg {
    position: absolute;
    left: 10px; /* updated at runtime to match grid padding */
    top: 10px;  /* updated at runtime to match grid padding */
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 3; /* above tiles to ensure start dot/path is visible */
}

#pathSvg path {
    stroke: var(--path-stroke-color);
    stroke-width: var(--path-stroke-width);
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
    transition: stroke-dasharray 0.2s ease-out;
}

.cell {
    background: #FFFFFF;
    border: 2px solid #E0E0E0;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    font-weight: bold;
    font-size: 0.9em;
    overflow: visible;
    touch-action: none; /* allow smooth pointer-drag without scrolling */
}

.cell:hover {
    background: #e9ecef;
    /* Remove scaling to avoid stacking-context issues that hide path connections */
}

.cell.visited {
    background: #f8f9fa;
    color: #333;
    border: 2px solid #dee2e6;
    box-shadow: none;
}

.cell.current {
    background: linear-gradient(135deg, #ff6b6b, var(--color-danger));
    color: white;
    border-color: #cc0000;
    box-shadow: 0 4px 8px color-mix(in srgb, var(--color-danger) 40%, transparent);
}

/* Ensure current styling wins on checkpoint cells as well */
.cell.checkpoint.current {
    background: linear-gradient(135deg, #ff6b6b, var(--color-danger));
    color: white;
    border-color: #cc0000;
}

/* Numbered tile inset inside cell */
.cell .tile {
    position: absolute;
    inset: 2.5%; /* ~95% size */
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #FFFFFF;
    font-weight: 700;
    font-family: 'Inter', 'Roboto', sans-serif;
    line-height: 1;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    z-index: 2; /* above path */
}
.tile-standard { background: #FFA726; }
.tile-constraint { background: #EF5350; }
.tile-start { background: #66BB6A; }
.cell .tile:hover { transform: scale(1.03); box-shadow: 0 4px 6px rgba(0,0,0,0.25); }

.cell.available:not(.checkpoint):not(.current) {
    background: linear-gradient(135deg, var(--color-success-2), var(--color-success));
    border-color: #37b24d;
    box-shadow: 0 2px 4px color-mix(in srgb, var(--color-success-2) 30%, transparent);
}

/* Debug mode styles - hidden by default */
.debug-hidden .cell.available:not(.checkpoint):not(.current) {
    background: #f8f9fa !important;
    border-color: #dee2e6 !important;
    box-shadow: none !important;
}

.debug-hidden .cell .debug-number {
    display: none !important;
}

.debug-number {
    position: absolute;
    top: 2px;
    left: 2px;
    font-size: 0.7em;
    color: #666;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    width: 16px;
    height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

.cell.checkpoint {
    background: linear-gradient(135deg, var(--color-warn-2), var(--color-warn));
    border: 3px solid #fd7e14;
    font-size: 1.2em;
    font-weight: bold;
    color: #495057;
    box-shadow: 0 4px 8px rgba(253, 126, 20, 0.3);
}

.cell.checkpoint.visited {
    background: linear-gradient(135deg, var(--color-success-2), var(--color-success));
    border-color: #37b24d;
    color: white;
}

/* Next-required checkpoint highlighting removed for increased difficulty */

/* Special styling for checkpoints that are also available moves */
.cell.checkpoint.available {
    box-shadow: 0 0 0 3px rgba(81, 207, 102, 0.5);
}

/* Solution path styling for debug mode */
.cell.solution-path {
    box-shadow: 0 0 0 3px rgba(138, 43, 226, 0.8) !important;
    position: relative;
}

.solution-connection {
    position: absolute;
    background: rgba(138, 43, 226, 0.8);
    z-index: 20;
    border-radius: 3px;
    border: 2px solid rgba(138, 43, 226, 1);
    pointer-events: none;
}

.solution-connection.horizontal {
    height: 10px;
    width: 64px; /* Cell width + gap + borders */
    top: 50%;
    transform: translateY(-50%);
}

.solution-connection.vertical {
    width: 10px;
    height: 64px; /* Cell height + gap + borders */
    left: 50%;
    transform: translateX(-50%);
}

.solution-connection.to-right {
    right: -34px; /* Adjusted for better connection */
}

.solution-connection.to-left {
    left: -34px;
}

.solution-connection.to-down {
    bottom: -34px;
}

.solution-connection.to-up {
    top: -34px;
}

/* Checkpoint pulse animation removed */

/* Snake path connection styles */
/* path-connection elements removed (SVG overlay draws the path) */

/* Turn indicators - removed for cleaner design */

/* Path head indicator (current position) - removed for cleaner design */

/* Pulse animation removed */

.instructions {
    background: rgba(102, 126, 234, 0.05);
    border-radius: 15px;
    padding: 20px;
    border: 1px solid rgba(102, 126, 234, 0.2);
}

.instructions h3 {
    color: #667eea;
    margin-bottom: 15px;
    font-size: 1.3em;
}

.instructions ul {
    text-align: left;
    color: #555;
    line-height: 1.6;
}

.instructions li {
    margin-bottom: 5px;
}

.win-message {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: linear-gradient(135deg, #51cf66, #40c057);
    color: white;
    padding: 30px;
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    font-size: 1.5em;
    font-weight: bold;
    z-index: 1000;
    animation: winAnimation 0.5s ease-out;
}

/* Error toast (was inline in JS) */
.toast-error {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #ff6b6b, #ee5a52);
    color: #ffffff;
    padding: 10px 20px;
    border-radius: var(--radius-md);
    font-weight: bold;
    z-index: 1000;
    box-shadow: var(--shadow-md);
}

/* Touch optimizations */
body.is-touch .cell {
    cursor: default;
}
body.is-touch .controls button {
    padding: 14px 22px;
}

#leaders {
    margin-top: 10px;
    text-align: left;
}

#leaders .leaders-header {
    font-weight: 700;
    margin-bottom: 6px;
}

#leaders .leader-row {
    display: grid;
    grid-template-columns: 28px 1fr auto;
    gap: 8px;
    align-items: center;
    padding: 6px 0;
    border-bottom: 1px solid rgba(0,0,0,0.05);
}

#leaders .leader-rank {
    color: #667eea;
    font-weight: 700;
}

#leaders .leader-time {
    font-variant-numeric: tabular-nums;
}

#leaders .leader-bt {
    opacity: 0.8;
}

@keyframes winAnimation {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.5);
    }
    100% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

@media (max-width: 600px) {
    .game-container {
        padding: 20px;
    }
    
    h1 {
        font-size: 28px;
    }
    
    .controls {
        justify-content: center;
    }
}

#dailyBtn {
    background: #64B5F6;
    color: #fff;
    border: none;
    padding: 12px 20px;
    border-radius: 25px;
    cursor: pointer;
    font-size: 1em;
    font-weight: bold;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(77, 171, 247, 0.3);
}
#dailyBtn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(77,171,247,0.4); }
