/* Global overflow prevention */
* {
    box-sizing: border-box;
}

html {
    overflow-x: hidden;
    width: 100%;
    max-width: 100%;
    height: 100%;
}

body {
    font-family: 'Helvetica Neue', Arial, sans-serif;
    touch-action: manipulation;
    letter-spacing: -0.01em;
    overflow-x: hidden;
    width: 100%;
    max-width: 100%;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height */
}

/* Wordle-style tiles */
.tile {
    transition: all 0.3s ease;
    transform-style: preserve-3d;
    position: relative;
    width: 2.5rem;
    height: 2.5rem;
    border: 2px solid #e0e7ff;
    border-radius: 8px;
    background: linear-gradient(135deg, #ffeaa7, #fab1a0);
    flex-shrink: 0;
    overflow: visible;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

@media (min-width: 480px) {
    .tile {
        width: 2.75rem;
        height: 2.75rem;
    }
}

@media (min-width: 640px) {
    .tile {
        width: 3.5rem;
        height: 3.5rem;
    }
}

/* Large screen optimizations */
@media (min-width: 1024px) {
    .tile {
        width: 4rem;
        height: 4rem;
    }
    
    .tile-front, .tile-back {
        font-size: 2rem;
    }
    
    .chain-word {
        margin: 0.375rem 0.75rem;
        padding: 0.75rem 1rem;
        font-size: 1rem;
        letter-spacing: 0.075em;
    }
}

.tile.flipped {
    animation: flip 0.6s ease-in-out;
}

.tile-front, .tile-back {
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    font-weight: bold;
    color: #1a1a1b;
    border-radius: 2px;
}

@media (min-width: 480px) {
    .tile-front, .tile-back {
        font-size: 1.25rem;
    }
}

@media (min-width: 640px) {
    .tile-front, .tile-back {
        font-size: 1.75rem;
    }
}

.tile-back {
    transform: rotateY(180deg);
}

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

/* Timer styling */
#timer-bar-inner {
    transition: width 0.1s linear;
    background: linear-gradient(90deg, #a78bfa, #ec4899, #f97316);
}

.timer-critical {
    background: linear-gradient(90deg, #ef4444, #dc2626, #b91c1c) !important;
    animation: pulse 0.3s infinite;
}

.timer-warning {
    background: linear-gradient(90deg, #fbbf24, #f59e0b, #d97706) !important;
}

.timer-normal {
    background: linear-gradient(90deg, #a78bfa, #ec4899, #f97316) !important;
}

.timer-pulse {
    animation: pulse 0.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

/* Word chain styling */
.chain-word {
    animation: fadeIn 0.5s ease-in-out;
    display: inline-block;
    margin: 0.125rem 0.25rem;
    padding: 0.375rem 0.5rem;
    background: linear-gradient(135deg, #fce7f3, #ddd6fe);
    border: 2px solid #e9d5ff;
    border-radius: 12px;
    font-weight: 600;
    font-size: 0.75rem;
    color: #7c3aed;
    letter-spacing: 0.025em;
    box-shadow: 0 2px 4px rgba(124, 58, 237, 0.1);
}

@media (min-width: 640px) {
    .chain-word {
        margin: 0.25rem 0.5rem;
        padding: 0.5rem 0.75rem;
        font-size: 0.875rem;
        letter-spacing: 0.05em;
    }
}

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

/* Spinner for light theme */
.spinner {
    border: 2px solid #e9d5ff;
    border-radius: 50%;
    border-top: 2px solid #a855f7;
    border-right: 2px solid #ec4899;
    width: 16px;
    height: 16px;
    animation: spin 1s linear infinite;
    display: inline-block;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Difficulty level styling - more vibrant and fun */
.difficulty-beginner { 
    color: #10b981; 
    font-weight: bold;
    text-shadow: 0 0 4px rgba(16, 185, 129, 0.2);
}
.difficulty-intermediate { 
    color: #f59e0b; 
    font-weight: bold;
    text-shadow: 0 0 4px rgba(245, 158, 11, 0.2);
}
.difficulty-advanced { 
    color: #ef4444; 
    font-weight: bold;
    text-shadow: 0 0 4px rgba(239, 68, 68, 0.2);
}
.difficulty-expert { 
    color: #dc2626; 
    font-weight: bold;
    text-shadow: 0 0 4px rgba(220, 38, 38, 0.3);
}
.difficulty-master { 
    color: #a855f7; 
    font-weight: bold;
    text-shadow: 0 0 4px rgba(168, 85, 247, 0.3);
}

/* Wordle tile states */
.tile-correct {
    background: linear-gradient(135deg, #8b5cf6, #a855f7) !important;
    border-color: #8b5cf6 !important;
    color: white !important;
    animation: tileReveal 0.5s ease-in-out;
    box-shadow: 0 4px 8px rgba(139, 92, 246, 0.3);
}

.tile-incorrect {
    background: linear-gradient(135deg, #94a3b8, #cbd5e1) !important;
    border-color: #94a3b8 !important;
    color: white !important;
    animation: tileShake 0.6s ease-in-out;
    opacity: 0.8;
}

.tile-present {
    background: linear-gradient(135deg, #facc15, #fbbf24) !important;
    border-color: #facc15 !important;
    color: white !important;
    animation: tileReveal 0.5s ease-in-out;
    box-shadow: 0 4px 8px rgba(250, 204, 21, 0.3);
}

.tile-filled {
    border-color: #c084fc;
    background: linear-gradient(135deg, #fce7f3, #e0e7ff);
    color: #374151;
    animation: tilePop 0.15s ease-in-out;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

@keyframes tileReveal {
    0% { 
        transform: scale(1) rotateX(0deg);
        background-color: white;
        border-color: #d3d6da;
        color: #1a1a1b;
    }
    50% { 
        transform: scale(1.05) rotateX(90deg);
    }
    100% { 
        transform: scale(1) rotateX(0deg);
    }
}

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

@keyframes tilePop {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* Button states */
.button-loading {
    opacity: 0.7;
    cursor: not-allowed;
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* On-screen keyboard styles */
#keyboard {
    max-width: 500px;
    margin: 0 auto;
    user-select: none;
    will-change: transform;
    transform-origin: bottom center;
}

.keyboard-row {
    display: flex;
    width: 100%;
    margin: 0 auto 6px;
    touch-action: manipulation;
}

.key {
    font-family: inherit;
    font-weight: bold;
    border: 0;
    padding: 0;
    margin: 0 4px 0 0;
    height: 48px;
    border-radius: 8px;
    cursor: pointer;
    user-select: none;
    background: linear-gradient(135deg, #e0e7ff, #fce7f3);
    color: #6b21a8;
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    text-transform: uppercase;
    -webkit-tap-highlight-color: transparent;
    transition: all 100ms ease;
    font-size: 13px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    border: 1px solid #e9d5ff;
}

.key:last-of-type {
    margin: 0;
}

.key.key-wide {
    flex: 1.5;
    font-size: 12px;
}

.half-key-spacer {
    flex: 0.5;
}

.key:active {
    background: linear-gradient(135deg, #c084fc, #f472b6);
    color: #ffffff;
    transform: scale(0.95);
    box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.key-pressed {
    background-color: #878a8c !important;
    color: #ffffff !important;
    transform: scale(0.95);
    animation: keyPress 150ms ease-out;
}

@keyframes keyPress {
    0% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(135, 138, 140, 0.4);
    }
    50% {
        transform: scale(0.95);
        box-shadow: 0 0 0 4px rgba(135, 138, 140, 0.2);
    }
    100% {
        transform: scale(0.95);
        box-shadow: 0 0 0 0 rgba(135, 138, 140, 0);
    }
}

.key[data-state="correct"] {
    background: linear-gradient(135deg, #8b5cf6, #a855f7);
    color: #ffffff;
    box-shadow: 0 2px 4px rgba(139, 92, 246, 0.3);
}

.key[data-state="present"] {
    background: linear-gradient(135deg, #facc15, #fbbf24);
    color: #ffffff;
    box-shadow: 0 2px 4px rgba(250, 204, 21, 0.3);
}

.key[data-state="absent"] {
    background: linear-gradient(135deg, #94a3b8, #cbd5e1);
    color: #ffffff;
    opacity: 0.7;
}

/* Mobile keyboard size adjustments */
@media (max-width: 480px) {
    #keyboard {
        margin: 0 -4px;
        padding: 0 2px;
    }
    
    .keyboard-row {
        margin: 0 auto 3px;
    }
    
    .key {
        height: 40px;
        margin: 0 2px 0 0;
        font-size: 13px;
        border-radius: 4px;
        min-width: 0;
        padding: 0 2px;
    }
    
    .key.key-wide {
        font-size: 10px;
    }
}

/* iPhone SE and smaller screens */
@media (max-width: 375px) {
    .key {
        height: 36px;
        font-size: 11px;
        margin: 0 1px 0 0;
        padding: 0 1px;
    }
    
    .key.key-wide {
        font-size: 8px;
    }
    
    .tile {
        width: 1.5rem !important;
        height: 1.5rem !important;
    }
    
    .tile-front, .tile-back {
        font-size: 0.875rem !important;
    }
    
    #keyboard {
        margin: 0 -2px;
    }
}

/* Ensure keyboard visibility on all devices */
@media (max-height: 700px) {
    #game-container {
        padding-top: 0.25rem !important;
        padding-bottom: 0.25rem !important;
    }
    
    .mb-2 {
        margin-bottom: 0.125rem !important;
    }
    
    #word-chain {
        height: 2.5rem !important;
    }
}

/* Fix for iPhone notch and safe areas */
@supports (padding: env(safe-area-inset-bottom)) {
    body {
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    #input-area {
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    #keyboard {
        margin-bottom: calc(0.5rem + env(safe-area-inset-bottom)) !important;
    }
}

/* Ultra small devices */
@media (max-width: 320px) {
    .tile {
        width: 1.4rem !important;
        height: 1.4rem !important;
    }
    
    .key {
        height: 34px;
        font-size: 10px;
    }
}

/* Mobile-first responsive design */
body {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    min-height: 100vh;
}

/* Touch targets for mobile */
button, .tile {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

/* Ensure proper input handling on mobile */
#word-input {
    position: fixed;
    left: -9999px;
    top: 50%;
    opacity: 0;
    pointer-events: none;
}

/* When input is focused, adjust layout */
#word-input:focus {
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 1px;
    height: 1px;
    opacity: 0.01;
    pointer-events: auto;
}

/* Adjust game container for mobile keyboard */
@supports (-webkit-touch-callout: none) {
    /* iOS Safari */
    #game-container {
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    
    body.keyboard-visible #game-container {
        padding-bottom: calc(50vh + env(safe-area-inset-bottom, 0));
    }
}

/* Android keyboard handling */
@media (max-width: 768px) {
    body.keyboard-visible {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    body.keyboard-visible #game-container {
        min-height: calc(100vh - 60px);
        padding-bottom: 50vh;
    }
    
    body.keyboard-visible #input-area {
        position: relative;
        z-index: 1000;
    }
}

/* Mobile optimizations */
@media (max-width: 640px) {
    html, body {
        font-size: 14px;
        overflow-x: hidden;
        overflow-y: auto;
        width: 100%;
        max-width: 100%;
        min-height: 100vh;
        min-height: 100dvh; /* Dynamic viewport height for iOS */
        position: relative;
    }
    
    #game-container {
        padding: 0.25rem !important;
        gap: 0.25rem !important;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
        overflow-y: auto;
        min-height: calc(100vh - 80px);
        min-height: calc(100dvh - 80px); /* Dynamic viewport height */
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
    
    /* Ensure tiles fit on mobile screens - RESPONSIVE SIZING */
    #input-tiles {
        flex-wrap: nowrap !important;
        width: 100%;
        max-width: 100%;
        justify-content: center;
        gap: 2px;
        overflow: visible;
        box-sizing: border-box;
        padding: 0.25rem 0 !important;
        min-height: 2.5rem !important;
        height: auto !important;
        align-items: center;
    }
    
    /* Make tiles responsive on mobile to fit screen width */
    .tile {
        width: calc(9vw - 2px) !important;
        height: calc(9vw - 2px) !important;
        max-width: 2.25rem !important;
        max-height: 2.25rem !important;
        min-width: 1.5rem !important;
        min-height: 1.5rem !important;
        flex-shrink: 0;
    }
    
    .tile-front, .tile-back {
        font-size: 1.25rem !important;
    }
    
    /* Input section container */
    #word-form {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow: visible !important;
        height: auto !important;
    }
    
    /* Header adjustments for mobile */
    header {
        padding: 0.5rem 0;
        width: 100%;
        max-width: 100%;
        flex-shrink: 0;
    }
    
    header h1 {
        font-size: 1.5rem;
    }
    
    /* Modal improvements for mobile */
    .modal-content {
        margin: 0.5rem;
        max-height: 90vh;
        overflow-y: auto;
    }
    
    /* Prevent horizontal overflow globally */
    * {
        max-width: 100%;
        box-sizing: border-box;
    }
    
    /* Button adjustments */
    button {
        font-size: 14px;
        padding: 0.75rem 1rem;
        width: 100%;
        max-width: 100%;
    }
    
    /* Compact game sections */
    #word-chain {
        height: 2.5rem !important;
    }
    
    /* Make input area always visible */
    #input-area {
        flex-shrink: 0;
        min-height: auto !important;
        position: sticky;
        bottom: 0;
        background: inherit;
        z-index: 10;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    
    /* Mobile keyboard optimization */
    #keyboard {
        margin-bottom: 0.5rem !important;
        position: relative;
        z-index: 20;
    }
    
    .keyboard-row {
        margin-bottom: 4px !important;
    }
    
    .key {
        height: 36px !important;
        margin: 0 2px 0 0 !important;
        font-size: 12px !important;
    }
    
    .key.key-wide {
        font-size: 10px !important;
    }
    
    /* Reduce spacing around elements */
    .mb-2 {
        margin-bottom: 0.25rem !important;
    }
    
    .mb-3 {
        margin-bottom: 0.5rem !important;
    }
    
    .py-1 {
        padding-top: 0.125rem !important;
        padding-bottom: 0.125rem !important;
    }
    
    /* Game stats compact */
    #score, #round-number {
        font-size: 1rem !important;
    }
    
    #lives {
        font-size: 0.875rem !important;
    }
}

/* Fix mobile overflow issues */
@media (max-width: 640px) {
    html {
        overflow-x: hidden;
        overflow-y: auto;
        position: relative;
        -webkit-overflow-scrolling: touch;
    }
    
    body {
        overflow-x: hidden;
        overflow-y: auto;
        position: relative;
        width: 100%;
        -webkit-overflow-scrolling: touch;
    }
    
    #game-container {
        overflow-x: hidden;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* Very small mobile devices */
@media (max-width: 480px) {
    .tile {
        width: 1.75rem !important;
        height: 1.75rem !important;
        border-width: 2px;
        flex-shrink: 0;
        min-width: 1.75rem;
    }
    
    .tile-front, .tile-back {
        font-size: 1.125rem !important;
        font-weight: 700;
    }
    
    #input-tiles {
        gap: 0.0625rem;
        padding: 0.125rem 0 !important;
        margin: 0 auto;
        max-width: calc(100vw - 1rem);
        justify-content: center;
        overflow: visible !important;
        min-height: 2.5rem !important;
        height: 2.5rem !important;
        align-items: center;
        flex-wrap: nowrap !important;
    }
    
    .chain-word {
        font-size: 0.6875rem;
        padding: 0.25rem 0.375rem;
        margin: 0.0625rem 0.125rem;
    }
    
    /* Ensure proper mobile viewport */
    html {
        overflow-x: hidden;
        width: 100vw;
        max-width: 100vw;
    }
    
    /* Additional overflow prevention */
    #word-form {
        overflow: visible !important;
        max-width: 100%;
        min-height: auto !important;
        height: auto !important;
        display: flex;
        flex-direction: column;
    }
    
    /* Override Tailwind height constraints */
    .h-10, .h-12 {
        height: auto !important;
        min-height: 3rem !important;
    }
    
    /* Make game sections more compact */
    .py-2 {
        padding-top: 0.25rem;
        padding-bottom: 0.25rem;
    }
    
    .mb-3 {
        margin-bottom: 0.5rem;
    }
    
    /* Ensure tiles are always fully visible */
    #input-tiles, #input-tiles * {
        overflow: visible !important;
    }
    
    /* Force proper height for all parent containers */
    #game-container {
        min-height: calc(100vh - 80px) !important;
        min-height: calc(100dvh - 80px) !important;
        height: auto !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }
    
    .flex-1, .min-h-0 {
        min-height: auto !important;
        height: auto !important;
    }
    
    /* Aggressive tile visibility fix */
    .tile, .tile-front, .tile-back {
        position: relative !important;
        overflow: visible !important;
        z-index: 1;
    }
}

/* Landscape mobile optimization */
@media (max-height: 600px) and (orientation: landscape) {
    #game-container {
        padding: 0.25rem;
        min-height: calc(100vh - 50px);
        min-height: calc(100dvh - 50px);
        max-height: calc(100vh - 50px);
        max-height: calc(100dvh - 50px);
        overflow-y: auto;
    }
    
    #word-chain {
        height: 2rem !important;
    }
    
    header {
        padding: 0.25rem 0;
    }
    
    header h1 {
        font-size: 1.25rem;
    }
    
    .mb-4 {
        margin-bottom: 0.5rem;
    }
    
    .mb-2 {
        margin-bottom: 0.125rem !important;
    }
    
    .key {
        height: 32px !important;
    }
    
    #keyboard {
        margin-bottom: 0.25rem !important;
    }
    
    .tile {
        width: 1.5rem !important;
        height: 1.5rem !important;
    }
    
    .tile-front, .tile-back {
        font-size: 0.875rem !important;
    }
    
    /* Hide less important elements in landscape */
    header p {
        display: none;
    }
    
    #seed-countdown {
        display: none;
    }
}

/* Keyboard attention animation */
@keyframes keyboardPulse {
    0% {
        box-shadow: 0 0 0 0 rgba(106, 170, 100, 0.7);
        transform: scale(1);
    }
    50% {
        box-shadow: 0 0 0 10px rgba(106, 170, 100, 0);
        transform: scale(1.02);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(106, 170, 100, 0);
        transform: scale(1);
    }
}

.keyboard-attention {
    animation: keyboardPulse 0.8s ease-out 2;
}

/* Start button container - maintain height */
#start-button-container {
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.key-highlight {
    background-color: #6aaa64 !important;
    color: white !important;
    transform: scale(1.1);
    transition: all 0.3s ease;
}

/* Countdown display */
#countdown-display {
    backdrop-filter: blur(8px);
}

#countdown-number {
    animation: countdownPop 1s ease-out;
}

@keyframes countdownPop {
    0% {
        transform: scale(0.5);
        opacity: 0;
    }
    50% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Clean scrollbar for word chain */
#word-chain::-webkit-scrollbar {
    width: 4px;
}

#word-chain::-webkit-scrollbar-track {
    background: #f1f5f9;
}

#word-chain::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 2px;
}

#word-chain::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}
