/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    touch-action: manipulation;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #2C3E50;
    overflow: hidden;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Game container */
#game-container {
    width: 100%;
    max-width: 500px;
    height: 100vh;
    max-height: 800px;
    background: white;
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}

/* Logo in header */
.header-logo {
    height: 70px;
    width: auto;
    object-fit: contain;
    position: absolute;
    top: -35px;
    left: 0;
    filter: drop-shadow(0 2px 8px rgba(0,0,0,0.3));
}

.game-logo-menu {
    max-width: 200px;
    height: auto;
    margin-bottom: 20px;
}

/* Header */
#game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    background: #f8f9fa;
    border-bottom: 1px solid #e0e0e0;
    min-height: 70px;
    position: relative;
}

.header-left {
    display: flex;
    align-items: center;
    flex: 0 0 auto;
    position: relative;
    z-index: 21;
}

.header-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    flex: 1;
    margin-left: 120px;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 0 0 auto;
}

.level-info, .move-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

#level-number {
    font-weight: bold;
    font-size: 16px;
}

#mode-indicator {
    font-size: 11px;
    background: #4A90E2;
    color: white;
    padding: 2px 6px;
    border-radius: 10px;
}

#move-counter, #par-indicator {
    font-size: 13px;
}

#restart-btn, #menu-btn {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    padding: 5px;
    transition: transform 0.2s;
}

#restart-btn:active, #menu-btn:active {
    transform: scale(0.9);
}

/* Main game board */
#game-board {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    position: relative;
    background-image: url('images/background.jpeg');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

/* Game grid */
.game-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 3px;
    width: 100%;
    max-width: 400px;
    aspect-ratio: 1;
    background: rgba(224, 224, 224, 0.95);
    padding: 3px;
    border-radius: 8px;
    position: relative;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* Grid cells */
.cell {
    background: white;
    border-radius: 4px;
    position: relative;
}

.cell.wall {
    background-image: url('images/wall.svg');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

.cell.goal {
    background-image: url('images/goal.svg');
    background-size: 70%;
    background-position: center;
    background-repeat: no-repeat;
    background-color: #FFF8DC;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(0.95); }
}

/* Game pieces */
#game-pieces {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.game-piece {
    position: absolute;
    transition: transform 120ms ease-out;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
}

.player {
    background-image: url('images/player.svg');
    background-size: 80%;
    background-position: center;
    background-repeat: no-repeat;
    background-color: transparent;
    z-index: 10;
}

.patroller {
    background-size: 85%;
    background-position: center;
    background-repeat: no-repeat;
    background-color: transparent;
    z-index: 5;
}

.patroller.horizontal {
    background-image: url('images/patroller-horizontal.svg');
}

.patroller.vertical {
    background-image: url('images/patroller-vertical..svg');
}

/* Preview overlay */
#preview-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 8;
}

.preview-line {
    position: absolute;
    background: rgba(74, 144, 226, 0.3);
    border: 2px dashed rgba(74, 144, 226, 0.5);
    border-radius: 4px;
    transition: opacity 0.2s;
}

/* Footer */
#game-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    background: #f8f9fa;
    border-top: 1px solid #e0e0e0;
    min-height: 60px;
}

.stars {
    display: flex;
    gap: 5px;
}

.star {
    width: 24px;
    height: 24px;
    transition: transform 0.3s;
}

.star.filled {
    content: url('images/star-filled.svg');
}

.timer {
    font-size: 16px;
    font-weight: bold;
}

/* Overlays */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    transition: opacity 0.3s;
}

.overlay.hidden {
    display: none;
}

.overlay-content {
    background: white;
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    max-width: 90%;
    width: 350px;
    animation: slideIn 0.3s;
}

@keyframes slideIn {
    from {
        transform: translateY(-30px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.overlay-content h1, .overlay-content h2 {
    margin-bottom: 20px;
    color: #2C3E50;
}

.menu-buttons, .victory-buttons, .defeat-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
}

.menu-btn {
    padding: 12px 24px;
    font-size: 16px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    background: #e0e0e0;
    color: #2C3E50;
}

.menu-btn.primary {
    background: #4A90E2;
    color: white;
}

.menu-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.menu-btn:active {
    transform: translateY(0);
}

.menu-stats, .victory-stats {
    margin-top: 20px;
    color: #666;
}

.stars.large {
    margin: 20px 0;
    justify-content: center;
}

.stars.large .star {
    width: 36px;
    height: 36px;
}

/* Responsive adjustments */
@media (max-width: 500px) {
    #game-container {
        max-width: 100%;
        height: 100vh;
        max-height: none;
        border-radius: 0;
    }

    .game-grid {
        max-width: calc(100vw - 40px);
        max-height: calc(100vw - 40px);
    }
}

@media (min-height: 700px) {
    #game-header, #game-footer {
        min-height: 80px;
        padding: 20px;
    }
}

/* Touch feedback */
.cell:active {
    transform: scale(0.95);
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
}

/* High contrast mode */
@media (prefers-contrast: high) {
    .cell.wall {
        background: black;
        border: 2px solid white;
    }

    .player {
        border: 3px solid black;
    }

    .patroller {
        border: 3px solid black;
    }
}

/* Tilt tutorial styles */
.tutorial-content {
    text-align: center;
    padding: 30px;
}

.phone-animation {
    position: relative;
    width: 120px;
    height: 180px;
    margin: 30px auto;
    animation: tiltDemo 4s infinite ease-in-out;
}

.phone-icon {
    font-size: 80px;
    line-height: 1;
}

.tilt-arrows {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 150%;
    height: 150%;
    pointer-events: none;
}

.tilt-arrows .arrow {
    position: absolute;
    font-size: 24px;
    color: #4A90E2;
    font-weight: bold;
    opacity: 0;
    animation: arrowPulse 4s infinite;
}

.arrow.up {
    top: -20px;
    left: 50%;
    transform: translateX(-50%);
    animation-delay: 0s;
}

.arrow.down {
    bottom: -20px;
    left: 50%;
    transform: translateX(-50%);
    animation-delay: 1s;
}

.arrow.left {
    left: -30px;
    top: 50%;
    transform: translateY(-50%);
    animation-delay: 2s;
}

.arrow.right {
    right: -30px;
    top: 50%;
    transform: translateY(-50%);
    animation-delay: 3s;
}

@keyframes tiltDemo {
    0%, 100% { transform: rotate(0deg); }
    12.5% { transform: rotateX(-20deg); }
    25% { transform: rotate(0deg); }
    37.5% { transform: rotateX(20deg); }
    50% { transform: rotate(0deg); }
    62.5% { transform: rotateY(-20deg); }
    75% { transform: rotate(0deg); }
    87.5% { transform: rotateY(20deg); }
}

@keyframes arrowPulse {
    0%, 75%, 100% { opacity: 0; }
    12.5%, 25% { opacity: 1; }
}

.tutorial-note {
    font-size: 14px;
    color: #666;
    margin-top: 10px;
}

.tutorial-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
}

/* Calibration feedback animation */
@keyframes fadeInOut {
    0% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
    50% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
}

/* Highscore input styles */
.highscore-stats {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    margin: 15px 0;
    text-align: left;
}

.highscore-stats p {
    margin: 5px 0;
    font-weight: bold;
}

.highscore-input {
    margin: 20px 0;
}

.highscore-input label {
    display: block;
    margin-bottom: 8px;
    font-weight: bold;
    color: #2C3E50;
}

.highscore-input input {
    width: 100%;
    padding: 12px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 16px;
    text-align: center;
    transition: border-color 0.2s;
}

.highscore-input input:focus {
    outline: none;
    border-color: #4A90E2;
    box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}

.highscore-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

.highscore-buttons .menu-btn {
    flex: 1;
}

/* Highscore result styles */
.highscore-result {
    background: linear-gradient(135deg, #4A90E2, #357ABD);
    color: white;
    padding: 20px;
    border-radius: 8px;
    margin: 15px 0;
    text-align: center;
}

.highscore-result p {
    font-size: 18px;
    font-weight: bold;
    margin: 0;
}

#highscore-rank {
    font-size: 24px;
    color: #FFD700;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}

.leaderboard {
    margin: 20px 0;
}

.leaderboard h3 {
    margin-bottom: 15px;
    color: #2C3E50;
    text-align: center;
}

.leaderboard-list {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 300px;
    overflow-y: auto;
}

.leaderboard-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    margin: 5px 0;
    background: #f8f9fa;
    border-radius: 6px;
    border-left: 4px solid #e0e0e0;
    transition: all 0.2s;
}

.leaderboard-list li.top-three {
    background: linear-gradient(135deg, #FFD700, #FFA500);
    color: white;
    border-left-color: #FF6B35;
    font-weight: bold;
}

.leaderboard-list li:hover {
    transform: translateX(5px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.leaderboard-list .rank {
    font-weight: bold;
    min-width: 30px;
}

.leaderboard-list .name {
    flex: 1;
    margin: 0 10px;
    text-align: left;
}

.leaderboard-list .level {
    color: #666;
    font-size: 14px;
    min-width: 60px;
    text-align: center;
}

.leaderboard-list .time {
    color: #666;
    font-size: 14px;
    min-width: 50px;
    text-align: right;
    font-family: monospace;
}

.highscore-result-buttons {
    margin-top: 20px;
}