:root {
    --primary-color: #2c3e50;
    --secondary-color: #3498db;
    --success-color: #27ae60;
    --danger-color: #e74c3c;
    --background-color: #f5f6fa;
    --wall-color: #34495e;
    --path-color: #ecf0f1;
    --visited-color: #3498db;
    --start-color: #27ae60;
    --end-color: #e74c3c;
    --player-color: #f39c12;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1rem;
    touch-action: manipulation; /* 모바일 확대 방지 */
    overflow-x: hidden;
}

.game-container {
    background: white;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    padding: 2rem;
    max-width: 900px;
    width: 100%;
    display: flex;
    flex-direction: column;
}

header {
    text-align: center;
    margin-bottom: 1.5rem;
    position: relative;
}

header h1 {
    color: var(--primary-color);
    font-size: 2rem;
    font-weight: 700;
}

.settings-btn {
    position: absolute;
    top: 0;
    right: 0;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: var(--secondary-color);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.settings-btn:hover {
    background: #2980b9;
    transform: rotate(90deg);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.settings-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    backdrop-filter: blur(5px);
}

.settings-overlay.hidden {
    display: none;
}

.settings-menu {
    background: white;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    max-width: 500px;
    width: 90%;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideIn 0.3s ease-out;
}

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

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 2px solid var(--background-color);
}

.settings-header h2 {
    color: var(--primary-color);
    font-size: 1.5rem;
    margin: 0;
}

.close-btn {
    width: 35px;
    height: 35px;
    border: none;
    border-radius: 50%;
    background: var(--danger-color);
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    line-height: 1;
}

.close-btn:hover {
    background: #c0392b;
    transform: rotate(90deg);
}

.settings-content {
    padding: 1.5rem;
}

.settings-section {
    margin-bottom: 2rem;
}

.settings-section:last-child {
    margin-bottom: 0;
}

.settings-section h3 {
    color: var(--primary-color);
    font-size: 1.1rem;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--background-color);
}

.game-info {
    display: flex;
    justify-content: space-around;
    margin-bottom: 1rem;
    padding: 1rem;
    background: var(--background-color);
    border-radius: 10px;
}

.info-item {
    font-size: 1rem;
    color: var(--primary-color);
    font-weight: 500;
}

.info-item span:last-child {
    color: var(--secondary-color);
    font-weight: 700;
}

.canvas-wrapper {
    display: flex;
    justify-content: center;
    margin-bottom: 1.5rem;
    background: var(--background-color);
    padding: 1rem;
    border-radius: 10px;
    position: relative;
}

/* 모바일 가로 모드용 조이스틱 */
.joystick-container {
    position: absolute;
    left: 20px;
    bottom: 20px;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}

.joystick-container.hidden {
    display: none;
}

.joystick {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: rgba(52, 152, 219, 0.3);
    border: 3px solid rgba(52, 152, 219, 0.6);
    position: relative;
    touch-action: none;
}

.joystick-handle {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--secondary-color);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    transition: transform 0.1s ease-out;
}

.joystick-handle.active {
    background: #2980b9;
}

/* 모바일 가로 모드용 아이템 버튼 */
.mobile-item-buttons {
    position: absolute;
    right: 20px;
    bottom: 20px;
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.mobile-item-buttons.hidden {
    display: none;
}

.mobile-item-btn {
    width: 80px;
    height: 80px;
    font-size: 1.2rem;
    border: none;
    border-radius: 15px;
    background: var(--secondary-color);
    color: white;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 600;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.mobile-item-btn:hover:not(.disabled) {
    background: #2980b9;
    transform: scale(1.05);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.3);
}

.mobile-item-btn:active:not(.disabled) {
    transform: scale(0.95);
}

.mobile-item-btn.disabled {
    background: #95a5a6;
    cursor: not-allowed;
    opacity: 0.6;
}

#mazeCanvas {
    border: 2px solid var(--primary-color);
    border-radius: 5px;
    background: white;
    max-width: 100%;
    height: auto;
}

.maze-3d {
    width: 100%;
    height: 600px;
    min-height: 400px;
    border: 2px solid var(--primary-color);
    border-radius: 5px;
    background: #000;
    position: relative;
    overflow: hidden;
    cursor: crosshair;
}

.maze-3d.hidden {
    display: none;
}

#mazeCanvas.hidden {
    display: none;
}

.pointer-lock-info {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 1rem 2rem;
    border-radius: 10px;
    z-index: 100;
    text-align: center;
    pointer-events: none;
}

.pointer-lock-info.hidden {
    display: none;
}

.pointer-lock-info p {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
}

/* 나침반 스타일 */
.compass {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 120px;
    height: 120px;
    z-index: 100;
}

.compass.hidden {
    display: none;
}

.compass-ring {
    position: relative;
    width: 100%;
    height: 100%;
    border: 3px solid rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.compass-direction {
    position: absolute;
    font-weight: bold;
    font-size: 16px;
    color: rgba(255, 255, 255, 0.9);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
    user-select: none;
}

#compassN {
    top: 5px;
    left: 50%;
    transform: translateX(-50%);
    color: #e74c3c; /* 빨간색으로 북쪽 강조 */
    font-size: 18px;
}

#compassE {
    right: 5px;
    top: 50%;
    transform: translateY(-50%);
}

#compassS {
    bottom: 5px;
    left: 50%;
    transform: translateX(-50%);
}

#compassW {
    left: 5px;
    top: 50%;
    transform: translateY(-50%);
}

.compass-needle {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 3px;
    height: 40px;
    background: #3498db;
    transform-origin: center bottom;
    transform: translate(-50%, -100%) rotate(0deg);
    box-shadow: 0 0 5px rgba(52, 152, 219, 0.8);
    border-radius: 2px;
}

.compass-needle::before {
    content: '';
    position: absolute;
    top: -8px;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 0;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 10px solid #3498db;
}

.controls {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.control-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

.horizontal-controls {
    display: flex;
    gap: 0.5rem;
}

.control-btn {
    width: 60px;
    height: 60px;
    font-size: 1.5rem;
    border: none;
    border-radius: 10px;
    background: var(--secondary-color);
    color: white;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: bold;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    touch-action: manipulation; /* 모바일 확대 방지 */
    -webkit-tap-highlight-color: transparent; /* 터치 하이라이트 제거 */
}

.control-btn:hover {
    background: #2980b9;
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

.control-btn:active {
    transform: translateY(0);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.maze-size-selector {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.size-label {
    font-size: 1rem;
    color: var(--primary-color);
    font-weight: 500;
}

.size-input {
    width: 80px;
    padding: 0.5rem;
    font-size: 1rem;
    border: 2px solid var(--secondary-color);
    border-radius: 5px;
    text-align: center;
    font-weight: 600;
}

.size-input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.size-note {
    font-size: 0.85rem;
    color: var(--primary-color);
    opacity: 0.7;
}

.texture-selector {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.maze-algorithm-selector {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.texture-label {
    font-size: 0.95rem;
    color: var(--primary-color);
    font-weight: 500;
}

.texture-select {
    padding: 0.5rem 1rem;
    border: 2px solid var(--secondary-color);
    border-radius: 5px;
    background: white;
    color: var(--primary-color);
    font-size: 0.95rem;
    cursor: pointer;
    min-width: 120px;
}

.texture-select:focus {
    outline: none;
    border-color: var(--primary-color);
}

.game-options {
    display: flex;
    justify-content: center;
    padding: 1rem;
    background: var(--background-color);
    border-radius: 10px;
    flex-wrap: wrap;
    gap: 1rem;
}

.option-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1rem;
    color: var(--primary-color);
    cursor: pointer;
}

.option-label input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
}

.action-buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
}

.item-buttons-inline {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    margin-top: 0.5rem;
}

.item-btn {
    width: 60px;
    height: 60px;
    font-size: 1rem;
    border: none;
    border-radius: 10px;
    background: var(--secondary-color);
    color: white;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 600;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.item-btn:hover:not(.disabled) {
    background: #2980b9;
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

.item-btn.disabled {
    background: #95a5a6;
    cursor: not-allowed;
    opacity: 0.6;
}

.action-btn {
    padding: 0.75rem 2rem;
    font-size: 1rem;
    border: none;
    border-radius: 10px;
    background: var(--primary-color);
    color: white;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 600;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.action-btn:hover {
    background: #1a252f;
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

.action-btn:active {
    transform: translateY(0);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.instructions {
    text-align: center;
    padding: 1rem;
    background: var(--background-color);
    border-radius: 10px;
}

.instructions p {
    color: var(--primary-color);
    font-size: 0.9rem;
    margin: 0.5rem 0;
    line-height: 1.5;
}

.instructions p.hidden {
    display: none;
}

.win-message {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 2rem;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    text-align: center;
    z-index: 1000;
    min-width: 300px;
}

.win-message.hidden {
    display: none;
}

.win-message h2 {
    color: var(--success-color);
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

.win-message p {
    color: var(--primary-color);
    margin-bottom: 1.5rem;
    font-size: 1.1rem;
}

/* 설정 메뉴 스크롤바 스타일 */
.settings-menu::-webkit-scrollbar {
    width: 8px;
}

.settings-menu::-webkit-scrollbar-track {
    background: var(--background-color);
    border-radius: 10px;
}

.settings-menu::-webkit-scrollbar-thumb {
    background: var(--secondary-color);
    border-radius: 10px;
}

.settings-menu::-webkit-scrollbar-thumb:hover {
    background: #2980b9;
}

/* 모바일 가로 모드 */
@media (orientation: landscape) and (max-height: 600px) {
    body {
        height: 100vh;
        overflow: hidden;
        padding: 0.5rem;
        align-items: stretch;
    }
    
    .game-container {
        max-width: 100%;
        height: 100%;
        padding: 0.5rem;
        border-radius: 10px;
        overflow: hidden;
        display: flex;
        flex-direction: column;
    }
    
    /* 상단 요소들을 한 줄로 배치 */
    .top-bar {
        margin-bottom: 0.5rem;
        flex-direction: row;
        align-items: center;
        gap: 0.5rem;
    }
    
    .back-link {
        display: inline-block;
        margin: 0;
        font-size: 0.85rem;
        flex-shrink: 0;
    }
    
    header {
        margin-bottom: 0;
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex: 1;
        min-width: 0;
    }
    
    header h1 {
        font-size: 1.2rem;
        margin: 0;
        flex: 1;
    }
    
    .settings-btn {
        width: 32px;
        height: 32px;
        position: static;
    }
    
    .settings-btn svg {
        width: 18px;
        height: 18px;
    }
    
    .game-info {
        margin-bottom: 0.5rem;
        padding: 0.5rem;
        flex-direction: row;
        justify-content: space-around;
        flex-shrink: 0;
        gap: 1rem;
    }
    
    .info-item {
        font-size: 0.9rem;
        margin: 0;
    }
    
    /* 미로 영역이 하단에 가득 차도록 */
    .canvas-wrapper {
        flex: 1;
        margin-bottom: 0;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 0;
        position: relative;
        overflow: hidden;
        width: 100%;
    }
    
    #mazeCanvas {
        width: 100%;
        height: 100%;
        object-fit: contain;
    }
    
    .joystick-container {
        display: flex;
        position: absolute;
        left: 5px;
        bottom: 5px;
        z-index: 100;
    }
    
    .joystick-container.hidden {
        display: none;
    }
    
    .joystick {
        width: 90px;
        height: 90px;
    }
    
    .joystick-handle {
        width: 35px;
        height: 35px;
    }
    
    .mobile-item-buttons {
        display: flex;
        position: absolute;
        right: 5px;
        bottom: 5px;
        gap: 8px;
        z-index: 100;
    }
    
    .mobile-item-buttons.hidden {
        display: none;
    }
    
    .mobile-item-btn {
        width: 65px;
        height: 65px;
        font-size: 0.9rem;
    }
    
    .control-buttons {
        display: none !important;
    }
    
    .controls {
        display: none;
    }
    
    .instructions {
        display: none;
    }
}

/* 모바일 최적화 */
@media (max-width: 768px) {
    .game-container {
        padding: 1rem;
    }

    header h1 {
        font-size: 1.5rem;
    }

    .settings-btn {
        width: 35px;
        height: 35px;
    }

    .settings-btn svg {
        width: 20px;
        height: 20px;
    }

    .control-btn {
        width: 50px;
        height: 50px;
        font-size: 1.2rem;
    }

    .action-buttons {
        flex-direction: column;
    }

    .action-btn {
        width: 100%;
    }

    .game-info {
        flex-direction: column;
        gap: 0.5rem;
        text-align: center;
    }

    .maze-size-selector {
        flex-direction: column;
        gap: 0.75rem;
    }

    .size-input {
        width: 100%;
    }

    .size-note {
        font-size: 0.75rem;
    }

    .maze-3d {
        height: 400px;
        min-height: 300px;
    }

    .settings-menu {
        width: 95%;
        max-height: 95vh;
    }

    .settings-header {
        padding: 1rem;
    }

    .settings-content {
        padding: 1rem;
    }

    .settings-section {
        margin-bottom: 1.5rem;
    }
}

/* 다크모드 지원 */
@media (prefers-color-scheme: dark) {
    :root {
        --background-color: #2c2c2c;
        --primary-color: #ecf0f1;
        --wall-color: #34495e;
        --path-color: #34495e;
    }

    body {
        background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    }

    .game-container {
        background: #2c2c2c;
        color: var(--primary-color);
    }
}

.top-bar {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-shrink: 0;
}

.back-link {
    display: inline-block;
    color: var(--secondary-color);
    text-decoration: none;
    font-weight: 600;
    transition: color 0.3s ease;
    white-space: nowrap;
}

.back-link:hover {
    color: #2980b9;
}