:root {
    /* Background & Texture */
    --bg-gradient: linear-gradient(135deg, #090916 0%, #17112E 50%, #1B0F2E 100%);
    --bg-color: #0c0b1a; /* Fallback */
    --text-color: #ffffff;
    
    /* Tiles & Glassmorphism */
    --glass-bg: rgba(255, 255, 255, 0.03);
    --glass-border: rgba(255, 255, 255, 0.1);
    --tile-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
    
    /* Semantic Colors */
    --color-correct: #00e5ff; /* Cyan/Neon Blue */
    --color-correct-glow: 0 0 15px rgba(0, 229, 255, 0.5);
    
    --color-present: #f20089; /* Bright Neon Magenta */
    --color-present-glow: 0 0 15px rgba(242, 0, 137, 0.5);
    
    --color-absent: rgba(255, 255, 255, 0.02); /* Very dark glass */
    --color-absent-border: rgba(255, 255, 255, 0.05);
    
    /* Keyboard keys */
    --key-bg: rgba(255, 255, 255, 0.1);
    --key-bg-hover: rgba(255, 255, 255, 0.2);
    --key-bg-active: rgba(255, 255, 255, 0.3);
    
    --board-gap: 8px;
    --tile-size: 62px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    /* Use 100dvh (dynamic viewport height) to account for mobile browser UI correctly */
    height: 100dvh;
    /* Aggressively prevent overscroll bouncing and scrolling on iOS */
    overflow: hidden;
    position: fixed; 
}

body {
    background: var(--bg-gradient);
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: 'Outfit', sans-serif;
    display: flex;
    justify-content: center;
    touch-action: none; /* Disables all browser pinch-zoom and double-tap behaviors */
}

.app-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 500px;
    height: 100%;
    position: relative;
}

header {
    width: 100%;
    padding: 10px 15px;
    border-bottom: 1px solid var(--glass-border);
    background: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    /* Flex limits to prevent tall headers */
    min-height: 60px;
}

header h1 {
    font-size: clamp(1.8rem, 5vw, 2.2rem);
    font-weight: 800;
    letter-spacing: 3px;
    text-transform: uppercase;
    background: linear-gradient(90deg, #00e5ff, #f20089);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 0 20px rgba(0, 229, 255, 0.3);
    margin: 0;
}

.settings-btn {
    position: absolute;
    right: 15px;
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px;
    border-radius: 50%;
    transition: all 0.2s ease;
}

.settings-btn:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.1);
    transform: rotate(45deg);
}

.settings-btn:active {
    transform: rotate(45deg) scale(0.9);
}

main {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    justify-content: center;
    align-items: center;
    width: 100%;
    padding: 10px;
    /* Prevent the board from pushing the keyboard off screen */
    min-height: 0; 
}

/* Board Layout */
.board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    grid-gap: var(--board-gap);
    padding: 5px;
    width: 100%;
    max-width: 350px;
    /* Limit the height to the available space to avoid pushing the keyboard down */
    max-height: calc(100dvh - 280px - env(safe-area-inset-bottom));
    aspect-ratio: 5 / 6;
}

.row {
    display: grid;
    grid-template-columns: repeat(var(--cols, 5), 1fr);
    grid-gap: var(--board-gap);
}

.tile {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(1.5rem, 5vh, 2.2rem);
    font-weight: 700;
    text-transform: uppercase;
    
    /* Glassmorphism base */
    background: var(--glass-bg);
    border: 2px solid var(--glass-border);
    border-radius: 8px; /* Tighter border radius for smaller screens */
    box-shadow: var(--tile-shadow);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275), 
                background-color 0.3s, border-color 0.3s, box-shadow 0.3s;
}

.tile[data-state="active"] {
    border-color: rgba(255, 255, 255, 0.4);
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.1);
    animation: pop 0.15s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

/* Tile Flip Animations & Colors */
.tile.flip {
    animation: flip 0.6s cubic-bezier(0.455, 0.030, 0.515, 0.955) forwards;
}

@keyframes flip {
    0% { transform: rotateX(0); }
    50% { transform: rotateX(90deg); }
    100% { transform: rotateX(0); }
}

.tile.correct { 
    background-color: rgba(0, 229, 255, 0.15); 
    border-color: var(--color-correct); 
    color: var(--text-color);
    box-shadow: var(--color-correct-glow);
}
.tile.present { 
    background-color: rgba(242, 0, 137, 0.15); 
    border-color: var(--color-present); 
    box-shadow: var(--color-present-glow);
}
.tile.absent { 
    background-color: var(--color-absent); 
    border-color: var(--color-absent-border); 
    color: rgba(255, 255, 255, 0.4);
}

/* Toast */
.toast-container {
    position: absolute;
    top: 15%;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: none;
    gap: 10px;
}

.toast {
    background: rgba(255, 255, 255, 0.9);
    color: #000;
    padding: 12px 24px;
    border-radius: 30px;
    font-weight: 700;
    font-size: 1.1rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.4s ease, transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.toast.fade-out {
    opacity: 0;
    transform: translateY(-20px);
}

/* Keyboard */
.keyboard {
    width: 100%;
    /* Add extra bottom padding utilizing the iOS safe area environment variable so it clears the home indicator */
    padding: 5px 8px calc(15px + env(safe-area-inset-bottom)) 8px; 
    display: flex;
    flex-direction: column;
    gap: 6px;
    user-select: none;
    max-width: 500px;
    margin: 0 auto;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: 6px;
}

.key {
    background: var(--key-bg);
    color: var(--text-color);
    font-size: clamp(0.9rem, 1.5vh, 1.1rem);
    font-weight: 600;
    border: 1px solid var(--glass-border);
    border-radius: 6px;
    flex: 1;
    /* Dynamic height calculation to fit on small mobile screens */
    height: clamp(40px, 6vh, 52px);
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

.key.large {
    flex: 1.5;
    font-size: 0.9rem;
}

.key:hover { 
    background: var(--key-bg-hover); 
    transform: translateY(-2px);
}
.key:active { 
    background: var(--key-bg-active); 
    transform: translateY(0) scale(0.95);
}

.key.correct { 
    background-color: var(--color-correct); 
    color: #000;
    border-color: var(--color-correct);
    box-shadow: var(--color-correct-glow);
}
.key.present { 
    background-color: var(--color-present); 
    color: #fff;
    border-color: var(--color-present);
    box-shadow: var(--color-present-glow);
}
.key.absent { 
    background-color: rgba(255, 255, 255, 0.05); 
    color: rgba(255, 255, 255, 0.3);
    border-color: transparent;
}

/* Modal */
.modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.modal.hidden {
    opacity: 0;
    pointer-events: none;
}

.modal-content {
    background: rgba(30, 20, 50, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 0 20px 50px rgba(0,0,0,0.5), inset 0 0 0 1px rgba(255,255,255,0.05);
    transform: translateY(0) scale(1);
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal.hidden .modal-content {
    transform: translateY(30px) scale(0.9);
}

.modal h2 { 
    margin-bottom: 15px; 
    font-size: clamp(2rem, 8vw, 2.5rem);
    background: linear-gradient(90deg, #00e5ff, #f20089);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}
.modal p { 
    margin-bottom: 30px; 
    font-size: clamp(1.1rem, 4vw, 1.4rem); 
    font-weight: 300;
}

.btn {
    background: linear-gradient(135deg, #00e5ff 0%, #00b4d8 100%);
    color: #000;
    border: none;
    padding: 14px 30px;
    font-size: 1.2rem;
    font-weight: 700;
    border-radius: 30px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(0, 229, 255, 0.3);
    font-family: inherit;
}

.btn.small {
    padding: 8px 16px;
    font-size: 0.95rem;
    box-shadow: none;
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 229, 255, 0.5);
}

.btn.small:hover {
    background: rgba(255, 255, 255, 0.2);
    box-shadow: 0 4px 10px rgba(255, 255, 255, 0.1);
}

.btn:active {
    transform: translateY(1px);
}

.settings-content {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 20px;
    min-width: 300px;
}

.close-btn {
    position: absolute;
    top: 15px;
    right: 20px;
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    transition: color 0.2s;
}

.close-btn:hover {
    color: #fff;
}

.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 10px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.setting-row:last-child {
    border-bottom: none;
    justify-content: center;
    padding-top: 20px;
}

.setting-row label {
    font-size: 1.1rem;
    font-weight: 300;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.setting-row select {
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 1.1rem;
    font-family: inherit;
    font-weight: 600;
    outline: none;
    cursor: pointer;
    transition: border-color 0.2s;
}

.setting-row select:focus {
    border-color: #00e5ff;
}

/* Media Query for extremely small height screens (landscape phones/old devices) */
@media (max-height: 700px) {
    .board {
        max-height: calc(100dvh - 240px - env(safe-area-inset-bottom));
    }
    .key {
        height: min(40px, 6vh);
    }
    header {
        min-height: 50px;
        padding: 5px 10px;
    }
    header h1 {
        font-size: 1.6rem;
    }
}
