/* Epsteindle CSS - Rewritten for Clarity and Correctness */

/* ------------------- */
/* --- THEME VARIABLES --- */
/* ------------------- */

:root {
    /* Base Colors (Shared) */
    --correct-bg: #538d4e;
    --present-bg: #b59f3b;

    /* Dark Theme (Default) */
    --bg-color: #121213;
    --text-color: #ffffff;
    --tile-border-default: #3a3a3c;
    --tile-border-filled: #565758;
    --absent-bg: #3a3a3c;
    --key-bg: #818384;
    --key-text: #ffffff;
    --modal-bg: #121213;
    --modal-border: #3a3a3c;
    --popup-bg: #121213;
    --popup-text: #ffffff;
}

@media (prefers-color-scheme: light) {
    :root {
        /* Light Theme Overrides */
        --bg-color: #ffffff;
        --text-color: #1a1a1b;
        --tile-border-default: #8B8B8B; /* User request */
        --tile-border-filled: #565758;
        --absent-bg: #787c7e;
        --key-bg: #d3d6da; /* User request: light grey */
        --key-text: #1a1a1b; /* User request: black */
        --modal-bg: #ffffff;
        --modal-border: #d3d6da;
        --popup-bg: #e0e0e0; /* User request: light grey */
        --popup-text: #1a1a1b;
    }
}

/* ------------------- */
/* --- BASE LAYOUT --- */
/* ------------------- */

body {
    font-family: 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    overflow: hidden;
}

#game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    height: calc(100% - 20px);
    max-width: 500px;
    margin: 0 auto;
    position: relative; /* For message container positioning */
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    border-bottom: 1px solid var(--tile-border-default);
    width: 100%;
    padding-bottom: 10px;
    margin-bottom: 20px;
}

.header-buttons {
    flex: 1;
    display: flex;
}

.header-buttons:first-child {
    justify-content: flex-start;
}

.header-buttons:last-child {
    justify-content: flex-end;
}

.header-icon {
    background: none;
    border: none;
    color: var(--text-color);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 5px 10px;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.header-icon:hover {
    background-color: var(--key-bg);
}

.hidden {
    display: none !important;
}

h1 {
    flex: 2;
    font-size: 2.5rem;
    margin: 0;
    letter-spacing: 0.1em;
    font-weight: bold;
}

/* ------------------- */
/* --- BOARD & TILES --- */
/* ------------------- */

#board-container {
    display: flex;
    flex-direction: column;
    gap: 5px;
    justify-content: center;
    align-items: center;
    margin-bottom: 10px;
    max-width: 350px;
}

.row {
    display: flex;
    gap: 5px;
    justify-content: center;
}

.tile {
    width: 62px;
    height: 62px;
    border: 2px solid var(--tile-border-default);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2.2rem;
    font-weight: bold;
    text-transform: uppercase;
    box-sizing: border-box;
    color: var(--text-color);
}

.tile.filled {
    border-color: var(--tile-border-filled);
    animation: pop 0.1s;
}

/* Tile States */
.tile.correct {
    background-color: var(--correct-bg);
    border-color: var(--correct-bg);
    color: #ffffff;
}
.tile.present {
    background-color: var(--present-bg);
    border-color: var(--present-bg);
    color: #ffffff;
}
.tile.absent {
    background-color: var(--absent-bg);
    border-color: var(--absent-bg);
    color: #ffffff;
}

/* Add this to ensure .tile.flip triggers the flip animation */
.tile.flip {
    animation: flip 0.65s ease-in-out;
    /* Ensure transform-origin for 3D effect */
    transform-origin: center;
    backface-visibility: hidden;
}

/* ------------------- */
/* --- KEYBOARD --- */
/* ------------------- */

#keyboard-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 10px 0;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    margin-bottom: 8px;
    width: 100%;
}

.key {
    font-family: inherit;
    font-weight: bold;
    border: 0;
    padding: 0 12px;
    margin: 0 3px;
    height: 58px;
    min-width: 43px;
    border-radius: 4px;
    cursor: pointer;
    user-select: none;
    display: flex;
    justify-content: center;
    align-items: center;
    text-transform: uppercase;
    -webkit-tap-highlight-color: rgba(0,0,0,0.3);
    transition: background-color 0.1s;
    background-color: var(--key-bg);
    color: var(--key-text);
}

.key.large {
    min-width: 65px;
}

.key:hover {
    filter: brightness(1.2);
}

/* Keyboard States */
.key.correct { background-color: var(--correct-bg); color: #ffffff; }
.key.present { background-color: var(--present-bg); color: #ffffff; }
.key.absent { background-color: var(--absent-bg); color: #ffffff; }


/* ------------------- */
/* --- POPUPS & MODALS --- */
/* ------------------- */

#message-container {
    position: absolute;
    top: 110px; /* Position between header and grid */
    left: 50%;
    transform: translateX(-50%);
    background-color: var(--popup-bg);
    color: var(--popup-text);
    font-weight: bold;
    padding: 12px 22px;
    border-radius: 5px;
    z-index: 1000;
    min-width: 180px;
    text-align: center;
    visibility: hidden;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
}

#message-container.show {
    visibility: visible;
    pointer-events: auto;
    opacity: 1;
}

.modal {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0,0,0,0.6);
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.modal:not(.hidden) {
    opacity: 1;
    pointer-events: auto;
    animation: overlay-fade 0.3s ease-out;
}

.modal-content {
    background-color: var(--modal-bg);
    border: 1px solid var(--modal-border);
    padding: 25px;
    border-radius: 8px;
    text-align: center;
    width: 90%;
    max-width: 480px;
    position: relative;
    color: var(--text-color);
    transform: scale(0.8);
    opacity: 0;
    transition: transform 0.3s ease 0.3s, opacity 0.3s ease 0.3s;
}

.modal:not(.hidden) .modal-content {
    transform: scale(1);
    opacity: 1;
}

.close-button {
    color: #aaa;
    position: absolute;
    right: 15px;
    top: 10px;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close-button:hover,
.close-button:focus {
    color: var(--text-color);
    text-decoration: none;
}

#results-grid {
    font-size: 1.2rem;
    line-height: 1.5;
    margin: 20px 0;
    white-space: pre-wrap;
    font-family: 'Courier New', Courier, monospace;
}

#share-button {
    background-color: var(--correct-bg);
    color: white;
    font-size: 1.2rem;
    font-weight: bold;
    border: none;
    padding: 12px 30px;
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    transition: background-color 0.2s;
}

#share-button:hover {
    filter: brightness(1.1);
}

/* ------------------- */
/* --- ANIMATIONS --- */
/* ------------------- */

@keyframes pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-10px); }
    20%, 40%, 60%, 80% { transform: translateX(10px); }
}

@keyframes flip {
    0% {
        transform: rotateX(0deg);
    }
    49% {
        transform: rotateX(90deg);
    }
    50% {
        transform: rotateX(90deg);
        /* Color change happens here, handled by .tile state classes */
    }
    100% {
        transform: rotateX(0deg);
    }
}

@keyframes win-dance {
    0%, 100% { transform: translateY(0); }
    25% { transform: translateY(-15px); }
    50% { transform: translateY(0); }
    75% { transform: translateY(-7px); }
}

/* --- Overlay Fade Animation --- */
@keyframes overlay-fade {
    0% { opacity: 0; }
    100% { opacity: 1; }
}

/* Remove scale transform on modal itself */
.modal {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0,0,0,0.6);
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.modal:not(.hidden) {
    opacity: 1;
    pointer-events: auto;
    animation: overlay-fade 0.3s ease-out;
}

/* Content Pop-in */
.modal-content {
    background-color: var(--modal-bg);
    border: 1px solid var(--modal-border);
    padding: 25px;
    border-radius: 8px;
    text-align: center;
    width: 90%;
    max-width: 480px;
    position: relative;
    color: var(--text-color);
    transform: scale(0.8);
    opacity: 0;
    transition: transform 0.3s ease 0.3s, opacity 0.3s ease 0.3s;
}

.modal:not(.hidden) .modal-content {
    transform: scale(1);
    opacity: 1;
}

.close-button {
    color: #aaa;
    position: absolute;
    right: 15px;
    top: 10px;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close-button:hover,
.close-button:focus {
    color: var(--text-color);
    text-decoration: none;
}

#results-grid {
    font-size: 1.2rem;
    line-height: 1.5;
    margin: 20px 0;
    white-space: pre-wrap;
    font-family: 'Courier New', Courier, monospace;
}

#share-button {
    background-color: var(--correct-bg);
    color: white;
    font-size: 1.2rem;
    font-weight: bold;
    border: none;
    padding: 12px 30px;
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    transition: background-color 0.2s;
}

#share-button:hover {
    filter: brightness(1.1);
}

/* ------------------- */
/* --- RESPONSIVE --- */
/* ------------------- */

@media (max-width: 600px) {
    body {
        height: auto;
        min-height: 100vh;
        overflow: auto;
        padding: 0;
    }
    #game-container {
        max-width: 100vw;
        width: 100vw;
        min-height: 100vh;
        height: auto;
        padding: 0 2vw;
    }
    #board-container {
        gap: 2vw;
        margin-bottom: 2vw;
        max-width: 90vw;
    }
    .row {
        gap: 2vw;
    }
    .tile {
        width: 12vw;
        height: 12vw;
        min-width: 36px;
        min-height: 36px;
        font-size: 6vw;
        padding: 0;
    }
    #keyboard-container {
        margin: 2vw 0;
    }
    .keyboard-row {
        margin-bottom: 2vw;
    }
    .key {
        min-width: 8vw;
        height: 10vw; /* Taller for better touch */
        font-size: 4vw;
        padding: 0 2vw;
    }
    .key.large {
        min-width: 14vw;
    }
    .modal-content {
        width: 98vw;
        max-width: 98vw;
        padding: 2vw;
    }
}

/* Add jump keyframes for final winner tiles */
@keyframes jump {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-12px); } /* A bit higher for more impact */
}

.tile.jump {
    animation: jump 0.4s ease-out; /* Faster and snappier */
}