/* --- GLOBAL RESET & LAYOUT --- */
:root {
    --header-height: auto;
}

html, body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden; /* Prevent scrolling */
    overscroll-behavior: none; /* Prevent pull-to-refresh */
    font-family: 'Arial', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    touch-action: manipulation; /* Disable double-tap zoom */
    -webkit-user-select: none; /* Disable text selection */
    user-select: none;
}

/* The Main Container fits 100% of the viewport height (dvh handles mobile address bars) */
.game-container {
    height: 100dvh; 
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-sizing: border-box;
}

/* --- HEADER SECTION --- */
/* Flex-shrink: 0 prevents these from squishing. */
.header {
    flex: 0 0 auto;
    text-align: center;
    width: 100%;
    margin-bottom: 5px;
}

.title {
    font-size: 2rem;
    margin: 0 0 5px 0;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

.navigation {
    margin-bottom: 5px;
}

.button-container {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 5px;
}

.nav-button, .share-button, .new-game-button, .game-button {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.4);
    color: white;
    padding: 6px 14px;
    border-radius: 18px;
    text-decoration: none;
    font-weight: bold;
    font-size: 0.9rem;
    cursor: pointer;
    backdrop-filter: blur(5px);
    transition: background 0.2s;
    white-space: nowrap;
}

.nav-button:hover, .game-button:hover {
    background: rgba(255, 255, 255, 0.3);
}

.game-mode-selector {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin: 8px 0;
}

.mode-button {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.4);
    color: rgba(255,255,255,0.6);
    padding: 5px 12px;
    border-radius: 12px;
    font-size: 0.85rem;
    cursor: pointer;
    font-weight: bold;
}

.mode-button.active {
    background: white;
    color: #764ba2;
    border-color: white;
}

/* --- SCOREBOARD --- */
.game-info {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    margin-bottom: 5px;
}

.score-board {
    display: flex;
    gap: 15px;
}

#timerDisplay, #movesDisplay {
    font-size: 1rem;
    font-weight: bold;
    background: rgba(0,0,0,0.2);
    padding: 6px 12px;
    border-radius: 8px;
    font-family: monospace;
}

.message {
    height: 1.4em;
    font-size: 1rem;
    text-align: center;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

/* --- THE GAME BOARD (Flexible Area) --- */
/* This container grows/shrinks to fill remaining space */
.board-container {
    flex: 1 1 auto; 
    display: flex;
    justify-content: center;
    align-items: center; /* Centers the board vertically */
    width: 100%;
    min-height: 0; /* Important: Allows flexbox to shrink content if needed */
    margin: 5px 0;
}

.game-board {
    display: grid;
    /* Grid columns are set by JS */
    gap: 1.5%; /* Relative gap */
    background: rgba(255, 255, 255, 0.15);
    padding: 2%;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.3);
    position: relative;
    
    /* Responsive Magic: 
       Width is whichever is smaller: 90% of width OR 55% of height.
       This keeps it a perfect square that never overflows. */
    width: min(90vw, 55vh); 
    aspect-ratio: 1 / 1;
    height: auto;
}

/* --- TILES --- */
.tile {
    /* Fill the grid cell exactly */
    width: 100%;
    height: 100%;
    background: white;
    border-radius: 15%; /* Relative rounding */
    display: flex;
    justify-content: center;
    align-items: center;
    
    /* Font scales with the viewport size */
    font-size: min(5vh, 6vw);
    font-weight: bold;
    color: #333;
    cursor: pointer;
    box-shadow: 0 2px 3px rgba(0,0,0,0.2);
    transition: transform 0.1s, background-color 0.2s;
}

.tile.empty {
    background: transparent;
    box-shadow: none;
    cursor: default;
}

.tile.correct {
    background: #6aaa64;
    color: white;
    cursor: default;
    box-shadow: inset 0 0 4px rgba(0,0,0,0.1);
    transform: none !important;
}

.tile.selected {
    background: #f1c40f;
    transform: scale(1.05);
    z-index: 10;
    box-shadow: 0 0 10px rgba(241, 196, 15, 0.6);
}

.tile.valid-target {
    background: #fff8dc;
    box-shadow: inset 0 0 0 2px #f1c40f;
    transform: scale(1.02);
}

.tile.dimmed {
    opacity: 0.4;
    transform: scale(0.95);
    cursor: not-allowed;
}

/* --- CONTROLS / FOOTER --- */
.controls-area {
    flex: 0 0 auto;
    padding-bottom: 10px;
    text-align: center;
}

.instruction {
    font-size: 0.85rem;
    opacity: 0.8;
    margin: 0;
}

/* --- OVERLAYS --- */
.pause-overlay, .solved-overlay {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    font-weight: bold;
    color: white;
    z-index: 200;
    border-radius: 12px;
    text-align: center;
    line-height: 1.4;
    pointer-events: all;
}

.pause-overlay {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 100;
}
.pause-overlay.active { opacity: 1; pointer-events: all; }

.solved-overlay {
    animation: fadeIn 0.5s ease;
}

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

/* --- STATS MODAL --- */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.85);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-content {
    background: #2c3e50;
    padding: 1.5rem;
    border-radius: 15px;
    text-align: center;
    width: 85%;
    max-width: 350px;
    border: 1px solid rgba(255,255,255,0.1);
}

.modal-close-button {
    position: absolute;
    top: 10px;
    right: 15px;
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-top: 15px;
}

.stat-item {
    background: rgba(255,255,255,0.1);
    padding: 8px;
    border-radius: 8px;
}

.stat-value { font-size: 1.3rem; font-weight: bold; color: #f1c40f; }
.stat-label { font-size: 0.75rem; opacity: 0.8; }

h3 { margin-top: 15px; margin-bottom: 8px; font-size: 1.1rem; opacity: 0.9; }

/* --- SMALL SCREENS --- */
@media (max-width: 360px) {
    .title { font-size: 1.6rem; }
    .nav-button, .game-button { font-size: 0.8rem; padding: 5px 10px; }
    .game-board { gap: 1%; padding: 1.5%; }
}