* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    /* Prevent text selection on mobile */
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    /* Prevent tap highlight */
    -webkit-tap-highlight-color: transparent;
    /* Prevent callout menu on long press */
    -webkit-touch-callout: none;
}

.hidden {
    display: none !important;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    min-height: 100vh;
    min-height: -webkit-fill-available; /* iOS Safari fix */
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    /* Prevent overscroll/bounce on mobile */
    overscroll-behavior: none;
    /* Prevent pinch zoom */
    touch-action: pan-x pan-y;
    /* Support for safe areas (notches) */
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

html {
    /* iOS Safari height fix */
    height: -webkit-fill-available;
    /* Prevent overscroll/bounce */
    overscroll-behavior: none;
}

#game-container {
    position: relative;
    width: 1000px;
    height: 750px;
}

#canvas-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    /* Prevent all touch gestures on canvas */
    touch-action: none;
}

#canvas-container canvas {
    display: block;
    background: rgba(50, 60, 80, 0.5);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(10px);
    /* Ensure canvas respects container size */
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
}

#ui-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#ui-overlay > * {
    pointer-events: auto;
}

#top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 30px;
}

#game-title {
    color: white;
    font-size: 28px;
    font-weight: 600;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

#top-bar-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.icon-btn {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    outline: none;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 60px;
    min-height: 44px; /* Minimum touch target size */
    justify-content: center;
    /* Prevent text selection in buttons */
    -webkit-user-select: none;
    user-select: none;
}

.icon-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
}

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

#how-to-play-btn {
    min-width: 40px;
}

#how-to-play-btn .help-icon {
    font-size: 24px;
    font-weight: 700;
    line-height: 1;
}

#undo-btn .arrow-icon {
    font-size: 22px;
    line-height: 1;
}

#undo-btn #undo-count {
    font-size: 16px;
    font-weight: 600;
}

#language-switcher {
    position: fixed;
    top: 20px;
    right: 20px;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    outline: none;
    transition: all 0.3s ease;
    z-index: 1000;
    backdrop-filter: blur(10px);
}

#language-switcher:hover {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
}

#language-switcher option {
    background: #1a1a2e;
    color: white;
}

#controls {
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 15px;
}

#difficulty-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255, 255, 255, 0.2);
    padding: 10px 16px;
    border-radius: 12px;
    backdrop-filter: blur(10px);
}

#difficulty-controls label {
    color: white;
    font-size: 14px;
    font-weight: 500;
}

#difficulty-selector {
    background: rgba(255, 255, 255, 0.3);
    color: white;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    outline: none;
    transition: all 0.3s ease;
}

#difficulty-selector:hover {
    background: rgba(255, 255, 255, 0.4);
    border-color: rgba(255, 255, 255, 0.5);
}

#difficulty-selector option {
    background: #1a1a2e;
    color: white;
}

#new-level-btn,
#restart-btn {
    background: white;
    color: #1a1a2e;
    border: none;
    border-radius: 12px;
    padding: 14px 28px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
    white-space: nowrap;
    min-height: 48px; /* Comfortable touch target */
    /* Prevent text selection in buttons */
    -webkit-user-select: none;
    user-select: none;
}

#new-level-btn:hover,
#restart-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

#new-level-btn:active,
#restart-btn:active {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

#win-message {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.95);
    color: #1a1a2e;
    padding: 30px 50px;
    border-radius: 16px;
    font-size: 24px;
    font-weight: 600;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
    animation: popIn 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

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

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

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

.modal.hidden {
    display: none;
}

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

.modal-content {
    background: linear-gradient(135deg, #ffffff 0%, #f5f5f5 100%);
    border-radius: 20px;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    animation: slideUp 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

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

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 24px 30px;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    color: white;
    border-bottom: 3px solid rgba(255, 255, 255, 0.1);
}

.modal-header h2 {
    margin: 0;
    font-size: 28px;
    font-weight: 700;
}

.close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.3);
    color: white;
    font-size: 32px;
    line-height: 1;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.close-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
    transform: rotate(90deg);
}

.modal-body {
    padding: 30px;
    overflow-y: auto;
    max-height: calc(80vh - 100px);
    color: #1a1a2e;
}

.modal-body .description {
    font-size: 16px;
    line-height: 1.6;
    color: #555;
    margin-bottom: 24px;
    padding: 16px;
    background: rgba(33, 150, 243, 0.1);
    border-left: 4px solid #2196F3;
    border-radius: 8px;
}

.modal-body .section {
    margin-bottom: 24px;
}

.modal-body h3 {
    color: #1a1a2e;
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 12px;
    display: flex;
    align-items: center;
}

.modal-body h3::before {
    content: "▸";
    color: #2196F3;
    font-size: 24px;
    margin-right: 8px;
}

.modal-body ol,
.modal-body ul {
    margin-left: 20px;
    line-height: 1.8;
}

.modal-body li {
    margin-bottom: 10px;
    color: #333;
    font-size: 15px;
}

.modal-body ol li {
    padding-left: 8px;
}

.modal-body ul li {
    list-style-type: none;
    position: relative;
    padding-left: 24px;
}

.modal-body ul li::before {
    content: "✓";
    position: absolute;
    left: 0;
    color: #4CAF50;
    font-weight: bold;
    font-size: 16px;
}

/* Responsive adjustments */
@media (max-width: 1100px) {
    #game-container {
        width: 90vw;
        height: 67.5vw;
        max-width: 1000px;
        max-height: 750px;
    }
    
    #game-title {
        font-size: 24px;
    }
    
    #top-bar {
        padding: 15px 20px;
    }
}

@media (max-width: 600px) {
    body {
        /* Ensure body fills viewport on mobile */
        min-height: 100dvh; /* Use dvh for dynamic viewport on mobile */
    }

    #game-container {
        /* Better fit for mobile screens */
        width: 100vw;
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for mobile */
        max-width: none;
        max-height: none;
    }

    #game-title {
        font-size: 18px; /* Slightly smaller for very small screens */
    }
    
    #top-bar {
        padding: 10px 15px; /* Reduce padding on small screens */
    }
    
    #top-bar-controls {
        gap: 6px;
    }
    
    /* Move undo button below canvas on mobile */
    #undo-btn {
        position: absolute;
        bottom: 15px;
        left: 15px;
        top: auto;
        padding: 10px 14px;
        font-size: 14px;
        gap: 6px;
        min-width: 70px;
        min-height: 48px;
        z-index: 10;
        /* Make it stand out from other controls */
        background: rgba(255, 255, 255, 0.25);
        border: 2px solid rgba(255, 255, 255, 0.4);
    }
    
    #undo-btn:hover:not(:disabled) {
        background: rgba(255, 255, 255, 0.35);
    }
    
    #undo-btn .arrow-icon {
        font-size: 20px;
    }
    
    #undo-btn #undo-count {
        font-size: 16px;
    }
    
    .icon-btn {
        padding: 8px 10px;
        font-size: 12px;
        gap: 4px;
        min-width: 50px;
        min-height: 44px; /* Maintain touch target size */
    }
    
    #how-to-play-btn {
        min-width: 44px; /* Minimum touch target */
        padding: 8px;
    }
    
    #how-to-play-btn .help-icon {
        font-size: 20px;
    }
    
    #language-switcher {
        top: 10px;
        right: 10px;
        padding: 8px 12px;
        font-size: 12px;
        min-height: 44px; /* Ensure touch target */
    }
    
    #controls {
        flex-direction: column;
        gap: 10px;
        bottom: 15px;
        /* Shift slightly right to balance with undo button on left */
        left: 55%;
        transform: translateX(-50%);
    }
    
    #difficulty-controls {
        padding: 10px 14px;
        min-height: 44px; /* Ensure proper touch height */
    }
    
    #difficulty-controls label {
        font-size: 13px;
    }
    
    #difficulty-selector {
        padding: 8px 10px;
        font-size: 13px;
        min-height: 36px;
    }
    
    #new-level-btn,
    #restart-btn {
        padding: 14px 28px;
        font-size: 15px;
        min-height: 48px; /* Comfortable touch target */
    }
    
    #win-message {
        font-size: 18px;
        padding: 20px 32px;
        max-width: 90%;
    }
    
    /* Modal responsive */
    .modal-content {
        width: 95%;
        max-height: 90vh;
        max-height: 90dvh; /* Dynamic viewport for mobile */
    }
    
    .modal-header {
        padding: 16px 20px;
    }
    
    .modal-header h2 {
        font-size: 20px;
    }
    
    .close-btn {
        width: 44px; /* Proper touch target */
        height: 44px;
        font-size: 28px;
    }
    
    .modal-body {
        padding: 20px;
        max-height: calc(90dvh - 76px);
        /* Enable momentum scrolling on iOS */
        -webkit-overflow-scrolling: touch;
    }
    
    .modal-body .description {
        font-size: 14px;
        padding: 12px;
    }
    
    .modal-body h3 {
        font-size: 18px;
    }
    
    .modal-body li {
        font-size: 14px;
        margin-bottom: 8px;
    }
}

/* Additional optimizations for very small screens (phones in portrait) */
@media (max-width: 380px) {
    #game-title {
        font-size: 16px;
    }
    
    #top-bar {
        padding: 8px 12px;
    }
    
    .icon-btn {
        padding: 8px;
        font-size: 11px;
        min-width: 44px;
    }
    
    #undo-btn {
        bottom: 10px;
        left: 10px;
        padding: 8px 12px;
        min-width: 65px;
        min-height: 44px;
    }
    
    #undo-btn .arrow-icon {
        font-size: 18px;
    }
    
    #undo-btn #undo-count {
        font-size: 14px;
    }
    
    #controls {
        bottom: 10px;
    }
    
    #new-level-btn,
    #restart-btn {
        padding: 12px 20px;
        font-size: 14px;
    }
}

/* Landscape mode optimization for mobile */
@media (max-width: 900px) and (orientation: landscape) {
    #game-container {
        width: 100vw;
        height: 100vh;
        height: 100dvh;
    }
    
    #top-bar {
        padding: 8px 15px;
    }
    
    #game-title {
        font-size: 18px;
    }
    
    #undo-btn {
        bottom: 10px;
        left: 10px;
        padding: 8px 12px;
        min-height: 44px;
    }
    
    #controls {
        bottom: 10px;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        /* In landscape, center the controls since we have more space */
        left: 50%;
        transform: translateX(-50%);
    }
}
