/* Reset and base styles */
* {
    box-sizing: border-box;
}

body {
    font-family: 'Helvetica Neue', Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #121213;
    color: #ffffff;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height for mobile browsers */
    display: flex;
    flex-direction: column;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Safe area support for devices with notches */
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
}

#app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height */
    max-width: 500px;
    width: 100%;
    margin: 0 auto;
    padding: 0 16px;
    /* Prevent overscroll bounce on iOS */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    box-sizing: border-box;
}

/* Main content - uses flexible height */
main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Allow flex shrinking */
    padding: 8px 0;
    box-sizing: border-box;
}

/* Game container - flexible height */
#game-container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
    margin-bottom: 8px;
    min-height: 0;
}

/* Game board */
#game-board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    padding: 8px;
    width: min(90vw, 500px);
    height: auto;
    max-width: 90vw;
    aspect-ratio: 5/6; /* Maintain proper proportions */
}

.tile {
    aspect-ratio: 1; /* Keep tiles square */
    border: 2px solid #3a3a3c;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(1.4rem, 4vw, 2rem);
    font-weight: bold;
    text-transform: uppercase;
    background-color: #121213;
    color: #ffffff;
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
    /* Enhanced touch support */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    /* Minimum touch target size */
    min-width: 50px;
    min-height: 50px;
    /* Prevent text selection */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.tile:hover {
    border-color: #565758;
}

.tile.focused {
    border-color: #ffffff;
    box-shadow: 0 0 0 1px #ffffff;
}

.tile.filled {
    border-color: #565758;
}

/* Tile states */
.tile.correct {
    background-color: #538d4e;
    border-color: #538d4e;
    color: #ffffff;
}

.tile.present {
    background-color: #b59f3b;
    border-color: #b59f3b;
    color: #ffffff;
}

.tile.absent {
    background-color: #3a3a3c;
    border-color: #3a3a3c;
    color: #ffffff;
}

/* Row input restriction states (requirement 8.4) */
.tile.current-row {
    border-color: #565758;
    cursor: pointer;
}

.tile.current-row:hover {
    border-color: #818384;
}

.tile.submitted-row {
    cursor: default;
    opacity: 0.95;
}

.tile.unreachable-row {
    cursor: default;
    opacity: 0.6;
    border-color: #2a2a2c;
}

.tile.unreachable-row:hover {
    border-color: #2a2a2c; /* No hover effect for unreachable rows */
}

/* Game status - flexible height */
#game-status {
    text-align: center;
    margin: 8px 0;
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 60px;
}

#message-display {
    font-size: 1.1rem;
    font-weight: bold;
    margin-bottom: 10px;
    min-height: 1.5em;
    transition: all 0.3s ease;
}

#message-display.error-message {
    color: #ff6b6b;
    background-color: rgba(255, 107, 107, 0.1);
    padding: 8px 16px;
    border-radius: 4px;
    border: 1px solid rgba(255, 107, 107, 0.3);
}

#new-game-btn {
    background-color: #538d4e;
    color: #ffffff;
    border: none;
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: bold;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s ease;
    /* Enhanced touch support */
    min-width: 44px;
    min-height: 44px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

#new-game-btn:hover {
    background-color: #6aaa64;
}

/* Virtual keyboard - flexible height */
#keyboard {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex: 0 0 auto;
    padding-bottom: 8px;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    justify-content: center;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: 6px;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.key {
    min-width: 40px;
    min-height: 44px; /* Ensure minimum touch target */
    height: 44px; /* Fixed height for consistent appearance */
    width: 40px;
    border: none;
    border-radius: 4px;
    background-color: #818384;
    color: #ffffff;
    font-size: 0.8rem;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
    transition: all 0.1s ease;
    user-select: none;
    touch-action: manipulation;
    /* Enhanced touch support */
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    /* Prevent zoom on double tap */
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.key:hover {
    background-color: #9ca3a4;
}

.key:active {
    transform: scale(0.95);
}

.key.letter-key {
    flex: 0 0 40px;
    max-width: 40px;
}

.key.action-key {
    padding: 0 6px;
    font-size: 0.6rem;
    width: auto;
    min-width: 40px;
}

/* Bottom row with Enter button */
.bottom-action-row {
    display: flex;
    gap: 6px;
}

.bottom-action-row .key.action-key {
    flex: 1;
    min-width: 0;
    max-width: none;
}

/* Special styling for ENTER button */
#submit-key {
    background-color: #538d4e !important;
    color: #ffffff !important;
    font-weight: bold;
}

#submit-key:hover {
    background-color: #6aaa64 !important;
}

#submit-key:active {
    background-color: #4a7c47 !important;
}

/* Key states */
.key.correct {
    background-color: #538d4e;
}

.key.present {
    background-color: #b59f3b;
}

.key.absent {
    background-color: #3a3a3c;
}

/* Touch feedback effects */
.tile:active,
.key:active,
#new-game-btn:active {
    transform: scale(0.95);
    transition: transform 0.1s ease;
}

/* Prevent text selection on touch devices */
.tile,
.key,
#new-game-btn {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Enhanced responsive design */

/* iPhone XR and similar devices */
@media (max-width: 414px) and (max-height: 896px) {
    #app {
        padding: 0 8px;
        max-width: 100%;
        width: 100%;
        margin: 0 auto;
    }

    #game-container {
        flex: 1;
    }

    #game-board {
        gap: 3px;
        padding: 6px;
        width: min(92vw, 450px);
        max-width: 92vw;
    }

    .tile {
        font-size: clamp(1.2rem, 3.5vw, 1.6rem);
        border-width: 2px;
    }

    #game-status {
        flex: 0 0 auto;
        min-height: 60px;
    }

    #keyboard {
        flex: 0 0 auto;
        max-width: 100%;
        padding: 0 8px;
        width: 100%;
    }

    .keyboard-row {
        gap: 4px;
        justify-content: center;
        width: 100%;
        max-width: 100%;
    }

    .key {
        min-width: 32px;
        height: 40px;
        width: 32px;
        font-size: 0.7rem;
        flex: 0 0 32px;
        max-width: 32px;
    }

    .key.letter-key {
        max-width: 32px;
        min-width: 32px;
    }

    .key.action-key {
        padding: 0 4px;
        font-size: 0.5rem;
        min-width: 32px;
        width: auto;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }
}

/* Extra small mobile devices (portrait) */
@media (max-width: 320px) {
    #app {
        padding: 0 4px;
        max-width: 100%;
        margin: 0 auto;
    }

    #game-container {
        flex: 1;
    }

    #game-board {
        gap: 2px;
        padding: 4px;
        width: min(90vw, 350px);
        max-width: 90vw;
    }

    .tile {
        font-size: clamp(1rem, 3vw, 1.3rem);
        border-width: 2px;
    }

    #game-status {
        flex: 0 0 auto;
        min-height: 50px;
    }

    #keyboard {
        flex: 0 0 auto;
        max-width: 100%;
        padding: 0 4px;
    }

    .key {
        min-width: 28px;
        height: 36px;
        width: 28px;
        font-size: 0.5rem;
        flex: 0 0 28px;
    }

    .key.letter-key {
        max-width: 28px;
        min-width: 28px;
    }

    .key.action-key {
        padding: 0 3px;
        font-size: 0.35rem;
        min-width: 28px;
        width: auto;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }

    .keyboard-row {
        gap: 2px;
        justify-content: center;
        max-width: 100%;
    }
}

/* Small mobile devices (portrait) */
@media (min-width: 321px) and (max-width: 480px) {
    #app {
        padding: 0 8px;
        max-width: 100%;
        margin: 0 auto;
    }

    #game-board {
        gap: 3px;
        padding: 6px;
        width: min(88vw, 400px);
        max-width: 88vw;
    }

    .tile {
        font-size: clamp(1rem, 3vw, 1.4rem);
        border-width: 2px;
    }

    #keyboard {
        max-width: 100%;
        padding: 0 8px;
    }

    .key {
        min-width: 34px;
        height: 42px;
        width: 34px;
        font-size: 0.6rem;
        flex: 0 0 34px;
    }

    .key.letter-key {
        max-width: 34px;
        min-width: 34px;
    }

    .key.action-key {
        padding: 0 4px;
        font-size: 0.45rem;
        min-width: 34px;
        width: auto;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }

    .keyboard-row {
        gap: 3px;
        justify-content: center;
        max-width: 100%;
    }
}

/* Medium mobile devices and small tablets (portrait) */
@media (min-width: 481px) and (max-width: 768px) {
    #game-board {
        gap: 4px;
        padding: 8px;
        width: min(85vw, 480px);
        max-width: 85vw;
    }

    .tile {
        font-size: clamp(1.2rem, 3.5vw, 1.6rem);
        border-width: 2px;
    }

    .key {
        min-width: 38px;
        height: 46px;
        width: 38px;
        font-size: 0.8rem;
        flex: 0 0 38px;
    }

    .key.letter-key {
        max-width: 38px;
    }

    .key.action-key {
        padding: 0 6px;
        font-size: 0.6rem;
        min-width: 38px;
        width: auto;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }

    .keyboard-row {
        gap: 5px;
    }
}

/* Mobile landscape orientation */
@media (max-width: 768px) and (orientation: landscape) and (max-height: 500px) {
    body {
        overflow: hidden;
    }

    #app {
        padding: 0 8px;
        max-width: none;
        margin: 0 auto;
    }

    main {
        flex-direction: row;
        align-items: stretch;
        gap: 16px;
        padding: 8px 0;
    }

    #game-container {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 0;
    }

    #game-board {
        gap: 3px;
        padding: 8px;
        margin: 0 auto;
        width: auto;
        height: 80vh;
        max-height: 80vh;
    }

    .tile {
        font-size: 0.9rem;
    }

    #game-status {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 10;
        background-color: rgba(18, 18, 19, 0.95);
        padding: 16px;
        border-radius: 8px;
        border: 1px solid #3a3a3c;
        min-width: 200px;
    }

    #keyboard {
        flex: 1;
        margin-top: 0;
        padding-bottom: 0;
        justify-content: center;
        max-width: 300px;
        padding: 0 8px;
    }

    .key {
        min-width: 24px;
        height: 32px;
        font-size: 0.6rem;
        flex: 1;
    }

    .key.letter-key {
        max-width: 28px;
        min-width: 24px;
    }

    .key.action-key {
        padding: 0 6px;
        font-size: 0.5rem;
        min-width: 32px;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }

    .keyboard-row {
        gap: 2px;
        justify-content: center;
        max-width: 100%;
    }
}

/* Extra small landscape (very narrow screens) */
@media (max-width: 568px) and (orientation: landscape) {
    main {
        gap: 12px;
    }

    #game-board {
        gap: 2px;
        padding: 3px;
    }

    .tile {
        width: 28px;
        height: 28px;
        font-size: 0.8rem;
    }

    .key {
        min-width: 44px;
        height: 38px;
        font-size: 0.6rem;
    }

    .key.letter-key {
        max-width: 28px;
    }

    .key.action-key {
        padding: 0 6px;
        font-size: 0.5rem;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }

    .keyboard-row {
        gap: 2px;
    }
}

/* Tablets (portrait) */
@media (min-width: 769px) and (max-width: 1024px) {
    #app {
        max-width: 600px;
        padding: 0 20px;
    }

    .tile {
        font-size: 1.6rem;
    }

    #game-board {
        gap: 4px;
        padding: 10px;
    }

    .key {
        min-width: 50px;
        height: 52px;
        font-size: 1rem;
    }

    .key.letter-key {
        max-width: 50px;
    }

    .key.action-key {
        padding: 0 16px;
        font-size: 0.9rem;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }

    .keyboard-row {
        gap: 8px;
    }
}

/* Tablets (landscape) - only for very wide tablets */
@media (min-width: 1200px) and (max-width: 1400px) and (orientation: landscape) {
    #app {
        max-width: 600px;
        padding: 0 20px;
        margin: 0 auto;
    }

    main {
        flex-direction: column;
        align-items: center;
        gap: 20px;
        max-width: 600px;
        margin: 0 auto;
    }

    #game-container {
        flex: 0 0 65vh;
        margin-bottom: 0;
    }

    #keyboard {
        flex: 0 0 30vh;
        margin-top: 0;
        padding-bottom: 0;
        max-width: 500px;
    }
}

/* Desktop and large screens */
@media (min-width: 1025px) {
    #app {
        max-width: 600px;
        padding: 0 20px;
        margin: 0 auto;
    }

    main {
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
        padding: 20px 0;
    }

    #game-container {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #game-board {
        width: min(85vw, 500px);
        max-width: 500px;
    }

    .tile {
        font-size: clamp(1.4rem, 2.5vw, 1.8rem);
        border-width: 2px;
    }

    #game-status {
        flex: 0 0 auto;
        min-height: 80px;
    }

    #keyboard {
        flex: 0 0 auto;
        max-width: 500px;
        width: 100%;
    }

    .key {
        min-width: 50px;
        height: 58px;
        font-size: 1rem;
    }

    .key.letter-key {
        max-width: 50px;
    }

    .key.action-key {
        padding: 0 16px;
    }

    .bottom-action-row .key.action-key {
        flex: 1;
        min-width: 0;
        max-width: none;
    }

    .keyboard-row {
        gap: 8px;
    }

    /* Hover effects only on non-touch devices */
    .tile:hover {
        border-color: #565758;
    }

    .key:hover {
        background-color: #9ca3a4;
    }

    #new-game-btn:hover {
        background-color: #6aaa64;
    }
}

/* High DPI displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .tile,
    .key {
        border-width: 1px;
    }
}

/* Animation keyframes */
@keyframes tile-flip {
    0% {
        transform: rotateX(0deg);
    }
    50% {
        transform: rotateX(90deg);
    }
    100% {
        transform: rotateX(0deg);
    }
}

@keyframes tile-bounce {
    0%, 20%, 53%, 80%, 100% {
        transform: translate3d(0, 0, 0);
    }
    40%, 43% {
        transform: translate3d(0, -8px, 0);
    }
    70% {
        transform: translate3d(0, -4px, 0);
    }
    90% {
        transform: translate3d(0, -2px, 0);
    }
}

@keyframes tile-shake {
    0%, 100% {
        transform: translateX(0);
    }
    10%, 30%, 50%, 70%, 90% {
        transform: translateX(-2px);
    }
    20%, 40%, 60%, 80% {
        transform: translateX(2px);
    }
}

@keyframes key-press {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(0.95);
    }
    100% {
        transform: scale(1);
    }
}

@keyframes focus-pulse {
    0%, 100% {
        box-shadow: 0 0 0 1px #ffffff;
    }
    50% {
        box-shadow: 0 0 0 2px #ffffff, 0 0 8px rgba(255, 255, 255, 0.3);
    }
}

@keyframes celebration-bounce {
    0%, 20%, 53%, 80%, 100% {
        transform: translate3d(0, 0, 0) scale(1);
    }
    40%, 43% {
        transform: translate3d(0, -12px, 0) scale(1.05);
    }
    70% {
        transform: translate3d(0, -6px, 0) scale(1.02);
    }
    90% {
        transform: translate3d(0, -3px, 0) scale(1.01);
    }
}

@keyframes message-slide-in {
    0% {
        opacity: 0;
        transform: translateY(-10px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes button-appear {
    0% {
        opacity: 0;
        transform: scale(0.8);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

/* Animation classes */
.tile.flipping {
    animation: tile-flip 0.6s ease-in-out;
    animation-fill-mode: both;
}

.tile.bouncing {
    animation: tile-bounce 0.6s ease-in-out;
}

.tile.shaking {
    animation: tile-shake 0.5s ease-in-out;
}

.tile.celebrating {
    animation: celebration-bounce 0.8s ease-in-out;
}

.key.pressing {
    animation: key-press 0.15s ease-in-out;
}

.tile.focused {
    border-color: #ffffff;
    animation: focus-pulse 2s ease-in-out infinite;
}

#message-display.sliding-in {
    animation: message-slide-in 0.3s ease-out;
}

#new-game-btn.appearing {
    animation: button-appear 0.4s ease-out;
}

/* Enhanced tile flip animation with perspective */
.tile {
    perspective: 1000px;
    transform-style: preserve-3d;
}

.tile.flipping {
    transform-origin: center center;
}

/* Staggered animation delays for row reveals */
.tile[data-col="0"].flipping { animation-delay: 0ms; }
.tile[data-col="1"].flipping { animation-delay: 100ms; }
.tile[data-col="2"].flipping { animation-delay: 200ms; }
.tile[data-col="3"].flipping { animation-delay: 300ms; }
.tile[data-col="4"].flipping { animation-delay: 400ms; }

/* Celebration animation delays for winning row */
.tile.celebrating[data-col="0"] { animation-delay: 0ms; }
.tile.celebrating[data-col="1"] { animation-delay: 100ms; }
.tile.celebrating[data-col="2"] { animation-delay: 200ms; }
.tile.celebrating[data-col="3"] { animation-delay: 300ms; }
.tile.celebrating[data-col="4"] { animation-delay: 400ms; }

/* Enhanced button feedback */
.key:active {
    transform: scale(0.95);
    transition: transform 0.1s ease;
}

#new-game-btn:active {
    transform: scale(0.95);
    transition: transform 0.1s ease;
}

/* Smooth transitions for state changes */
.tile {
    transition: all 0.2s ease, border-color 0.3s ease;
}

.key {
    transition: all 0.1s ease, background-color 0.3s ease;
}

#message-display {
    transition: all 0.3s ease;
}

/* Performance optimizations */
.tile,
.key,
#new-game-btn {
    will-change: transform;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    transform: translateZ(0); /* Force hardware acceleration */
    -webkit-transform: translateZ(0);
}

.tile.flipping,
.tile.bouncing,
.tile.shaking,
.tile.celebrating {
    will-change: transform, background-color, border-color;
}

/* Optimize animations for mobile performance */
@media (max-width: 768px) {
    .tile.flipping {
        animation-duration: 0.5s; /* Slightly faster on mobile */
    }

    .tile.celebrating {
        animation-duration: 0.6s; /* Shorter celebration on mobile */
    }

    .tile.bouncing {
        animation-duration: 0.4s; /* Quicker bounce on mobile */
    }
}

/* Reduce animation complexity on older devices */
@media (max-width: 480px) {
    @keyframes tile-flip {
        0% {
            transform: scaleY(1);
        }
        50% {
            transform: scaleY(0.8);
        }
        100% {
            transform: scaleY(1);
        }
    }

    @keyframes celebration-bounce {
        0%, 100% {
            transform: scale(1);
        }
        50% {
            transform: scale(1.1);
        }
    }
}

/* Accessibility - Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .tile,
    .key,
    #new-game-btn,
    #message-display {
        transition: none;
        animation: none;
    }

    .tile:active,
    .key:active,
    #new-game-btn:active {
        transform: none;
    }

    .tile.focused {
        animation: none;
        box-shadow: 0 0 0 2px #ffffff;
    }
}

/* Dark mode support (system preference) */
@media (prefers-color-scheme: light) {
    /* Keep dark theme as default for game consistency */
}

/* Focus management for keyboard navigation */
@media (min-width: 1025px) {
    .tile:focus,
    .key:focus,
    #new-game-btn:focus {
        outline: 2px solid #ffffff;
        outline-offset: 2px;
    }
}
