/**
 * HANLAH (All-Ages) - Stylesheet
 * 
 * Defines the theming engine and handles responsive 
 * scaling for mobile, tablet, and TV displays.
 */

/* ==========================================================================
   1. CSS VARIABLES (THEMING)
   ========================================================================== */

:root {
    /* DEFAULT THEME: Gamified Sunset (Kid-friendly) */
    --bg-color: #ffcc80;
    --text-color: #2b1d1a;
    --correct-color: #2e7d32; /* Darker green for WCAG AA compliance (4.54:1 contrast) */
    --correct-text: #ffffff;
    --present-color: #fbc02d;
    --present-text: #2b1d1a;  /* Dark text for WCAG AAA compliance (6.55:1 contrast) */
    --absent-color: #5d4037;
    --absent-text: #ffffff;
    --tile-border: #2b1d1a;
    --tile-bg: #fff8e1;
    --key-bg: #ffe0b2;
    --key-text: #2b1d1a;
    --primary-btn: #d84315;
    --primary-btn-hover: #bf360c;
}

/* REALISTIC THEME OVERRIDES */
body[data-theme="realistic"] {
    --bg-color: #fdf6e3;
    --text-color: #3e2723;
    --correct-color: #8b9467;
    --correct-text: #3e2723; /* Dark text for WCAG AA compliance (4.91:1 contrast) */
    --present-color: #d4a373;
    --present-text: #3e2723; /* Dark text for WCAG AA compliance (4.79:1 contrast) */
    --absent-color: #a8a29e;
    --absent-text: #3e2723;  /* Dark text for WCAG AA compliance (5.53:1 contrast) */
    --tile-border: #d4ccb6;
    --tile-bg: #fffdf7;
    --key-bg: #e6dfcc;
    --key-text: #3e2723;
    --primary-btn: #795548;
    --primary-btn-hover: #5d4037;
}

/* GAMIFIED THEME (Explicit for switching back) */
body[data-theme="gamified"] {
    /* Inherits from :root */
}

/* SPACE THEME OVERRIDES */
body[data-theme="space"] {
    --bg-color: #0a0b10;
    --text-color: #e0e0ff;
    --correct-color: #00e676;
    --correct-text: #0a0b10;
    --present-color: #ffd600;
    --present-text: #0a0b10;
    --absent-color: #37474f;
    --absent-text: #e0e0ff;
    --tile-border: #5f5fc4;
    --tile-bg: #1a1b26;
    --key-bg: #24283b;
    --key-text: #e0e0ff;
    --primary-btn: #6200ea;
    --primary-btn-hover: #4a00b0;
}

/* FOREST THEME OVERRIDES */
body[data-theme="forest"] {
    --bg-color: #e8f5e9;
    --text-color: #273c1c;
    --correct-color: #388e3c;
    --correct-text: #ffffff;
    --present-color: #ffb300;
    --present-text: #273c1c;
    --absent-color: #8d6e63;
    --absent-text: #ffffff;
    --tile-border: #273c1c;
    --tile-bg: #f1f8e9;
    --key-bg: #c8e6c9;
    --key-text: #273c1c;
    --primary-btn: #2e7d32;
    --primary-btn-hover: #1b5e20;
}

/* CYBERPUNK THEME OVERRIDES */
body[data-theme="cyberpunk"] {
    --bg-color: #0c0f1d;
    --text-color: #00f0ff;
    --correct-color: #00f0ff;
    --correct-text: #0c0f1d;
    --present-color: #ff007f;
    --present-text: #0c0f1d;
    --absent-color: #3b3e52;
    --absent-text: #00f0ff;
    --tile-border: #00f0ff;
    --tile-bg: #151a30;
    --key-bg: #1e2540;
    --key-text: #00f0ff;
    --primary-btn: #ff007f;
    --primary-btn-hover: #d8006b;
}

/* ==========================================================================
   2. BASE LAYOUT & TYPOGRAPHY
   ========================================================================== */

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Nunito', sans-serif;
    background-color: var(--bg-color);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
    color: var(--text-color);
    height: 100vh;
    height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    user-select: none;
    touch-action: manipulation;
    transition: background-color 0.5s ease;
}

#game-container {
    width: 100%;
    max-width: 500px;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* ==========================================================================
   3. THEME BACKGROUNDS
   ========================================================================== */

/* Realistic Theme Backgrounds */
body[data-theme="realistic"] {
    background-image: linear-gradient(rgba(253, 246, 227, 0.8), rgba(253, 246, 227, 0.8)), url('../images/cafe-bg.jpg');
}
@media (orientation: landscape) { body[data-theme="realistic"] { background-image: linear-gradient(rgba(253, 246, 227, 0.8), rgba(253, 246, 227, 0.8)), url('../images/cafe-bg-desktop.jpg'); } }
@media (orientation: portrait) { body[data-theme="realistic"] { background-image: linear-gradient(rgba(253, 246, 227, 0.8), rgba(253, 246, 227, 0.8)), url('../images/cafe-bg-mobile.jpg'); } }

/* Gamified Theme Backgrounds */
body[data-theme="gamified"] {
    background-image: linear-gradient(rgba(255, 204, 128, 0.7), rgba(255, 204, 128, 0.7)), url('../images/cafe-sunset-bg-desktop.jpg');
}
@media (orientation: landscape) { body[data-theme="gamified"] { background-image: linear-gradient(rgba(255, 204, 128, 0.7), rgba(255, 204, 128, 0.7)), url('../images/cafe-sunset-bg-desktop.jpg'); } }
@media (orientation: portrait) { body[data-theme="gamified"] { background-image: linear-gradient(rgba(255, 204, 128, 0.7), rgba(255, 204, 128, 0.7)), url('../images/cafe-sunset-bg-mobile.jpg'); } }

/* Space Theme Backgrounds */
body[data-theme="space"] {
    background-image: linear-gradient(rgba(10, 11, 16, 0.75), rgba(10, 11, 16, 0.75)), url('../images/space-bg-desktop.png');
}
@media (orientation: landscape) { body[data-theme="space"] { background-image: linear-gradient(rgba(10, 11, 16, 0.75), rgba(10, 11, 16, 0.75)), url('../images/space-bg-desktop.png'); } }
@media (orientation: portrait) { body[data-theme="space"] { background-image: linear-gradient(rgba(10, 11, 16, 0.75), rgba(10, 11, 16, 0.75)), url('../images/space-bg-mobile.png'); } }

/* Forest Theme Backgrounds */
body[data-theme="forest"] {
    background-image: linear-gradient(rgba(232, 245, 233, 0.75), rgba(232, 245, 233, 0.75)), url('../images/forest-bg-desktop.png');
}
@media (orientation: landscape) { body[data-theme="forest"] { background-image: linear-gradient(rgba(232, 245, 233, 0.75), rgba(232, 245, 233, 0.75)), url('../images/forest-bg-desktop.png'); } }
@media (orientation: portrait) { body[data-theme="forest"] { background-image: linear-gradient(rgba(232, 245, 233, 0.75), rgba(232, 245, 233, 0.75)), url('../images/forest-bg-mobile.png'); } }

/* Cyberpunk Theme Backgrounds */
body[data-theme="cyberpunk"] {
    background-image: linear-gradient(rgba(12, 15, 29, 0.75), rgba(12, 15, 29, 0.75)), url('../images/cyberpunk-bg-desktop.png');
}
@media (orientation: landscape) { body[data-theme="cyberpunk"] { background-image: linear-gradient(rgba(12, 15, 29, 0.75), rgba(12, 15, 29, 0.75)), url('../images/cyberpunk-bg-desktop.png'); } }
@media (orientation: portrait) { body[data-theme="cyberpunk"] { background-image: linear-gradient(rgba(12, 15, 29, 0.75), rgba(12, 15, 29, 0.75)), url('../images/cyberpunk-bg-mobile.png'); } }

/* ==========================================================================
   4. SCREENS & NAVIGATION
   ========================================================================== */

.screen {
    display: none;
    flex-direction: column;
    width: 100%;
    flex-grow: 1;
    padding: 20px;
}

.screen.active {
    display: flex;
}

#start-screen {
    justify-content: center;
    align-items: center;
    text-align: center;
    position: relative;
}

/* Theme & Settings Selectors */
.theme-selector-container, .theme-selector-container-small, .settings-container {
    background-color: rgba(255, 255, 255, 0.4);
    padding: 5px 12px;
    border-radius: 20px;
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    gap: 8px;
}

.theme-selector-container { position: absolute; top: 20px; right: 20px; }
.settings-container {
    position: absolute;
    top: 20px;
    left: 20px;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--text-color);
}
.settings-container label, .settings-container input { cursor: pointer; }
.theme-selector-container-small { padding: 2px 8px; border-radius: 15px; }

.theme-select {
    background: none;
    border: none;
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--text-color);
    cursor: pointer;
    outline: none;
}

#theme-select-game { font-size: 0.75rem; }

.icon-btn {
    background: none;
    border: none;
    font-family: inherit;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-color);
    cursor: pointer;
    outline: none;
    transition: transform 0.1s ease, opacity 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px;
}

.icon-btn:hover {
    transform: scale(1.15);
    opacity: 0.8;
}

.icon-btn:active {
    transform: scale(0.95);
}

/* Typography */
.logo {
    font-size: 4rem;
    font-weight: 900;
    color: var(--primary-btn);
    margin-bottom: 0.5rem;
    letter-spacing: 2px;
}

.subtitle {
    font-size: 1.2rem;
    margin-bottom: 2rem;
    opacity: 0.8;
}

/* Buttons */
.difficulty-options {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 100%;
    max-width: 300px;
}

.diff-btn {
    padding: 15px 30px;
    font-size: 1.2rem;
    font-weight: 700;
    border: none;
    border-radius: 12px;
    background-color: var(--primary-btn);
    color: white;
    cursor: pointer;
    transition: transform 0.1s, background-color 0.2s;
}

.diff-btn:hover { background-color: var(--primary-btn-hover); transform: scale(1.02); }
.diff-btn:active { transform: scale(0.98); }

/* ==========================================================================
   5. GAME BOARD & TILES
   ========================================================================== */

#game-screen { justify-content: space-between; }

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--tile-border);
    padding-bottom: 10px;
}

.game-title { font-size: 2rem; font-weight: 900; color: var(--primary-btn); }

#board-container {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px 0;
}

#board { display: grid; gap: 5px; }

.tile {
    width: 55px;
    height: 55px;
    border: 1px solid var(--tile-border);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    font-weight: 700;
    text-transform: uppercase;
    background-color: var(--tile-bg);
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    transition: transform 0.1s, background-color 0.2s, box-shadow 0.2s;
}

.tile[data-state="toggled"] {
    border-color: var(--text-color);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    animation: pop 0.1s;
}

.tile.correct { background-color: var(--correct-color); border-color: var(--correct-color); color: var(--correct-text); box-shadow: inset 0 2px 4px rgba(0,0,0,0.1); }
.tile.present { background-color: var(--present-color); border-color: var(--present-color); color: var(--present-text); box-shadow: inset 0 2px 4px rgba(0,0,0,0.1); }
.tile.absent { background-color: var(--absent-color); border-color: var(--absent-color); color: var(--absent-text); box-shadow: inset 0 2px 4px rgba(0,0,0,0.1); }

/* ==========================================================================
   6. VIRTUAL KEYBOARD
   ========================================================================== */

#keyboard {
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: 6px;
    margin-bottom: 8px;
    width: 100%;
}

.key {
    background-color: var(--key-bg);
    color: var(--key-text);
    border: 1px solid var(--tile-border);
    border-radius: 6px;
    padding: 15px 0;
    font-size: 1.1rem;
    font-weight: 700;
    cursor: pointer;
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    max-width: 45px;
    text-transform: uppercase;
    transition: transform 0.05s, opacity 0.05s, background-color 0.2s;
    box-shadow: 0 2px 0 var(--tile-border);
}

.key:active { transform: translateY(2px); box-shadow: none; opacity: 0.9; }
.key.large { max-width: 65px; font-size: 0.9rem; }

/* Keyboard State Highlights */
.key.correct { background-color: var(--correct-color); color: var(--correct-text); border-color: var(--correct-color); box-shadow: 0 2px 0 #2e7d32; }
.key.present { background-color: var(--present-color); color: var(--present-text); border-color: var(--present-color); box-shadow: 0 2px 0 #f9a825; }
.key.absent { background-color: var(--absent-color); color: var(--absent-text); border-color: var(--absent-color); box-shadow: 0 2px 0 #3e2723; }

/* ==========================================================================
   7. MOBILE & UI OVERLAYS
   ========================================================================== */

#active-input { position: fixed; top: -100px; left: 0; width: 0; height: 0; opacity: 0; pointer-events: none; }

#mobile-input-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 24px;
    margin: 5px 0;
}

.mobile-hint {
    font-size: 0.85rem;
    opacity: 0.85;
    letter-spacing: 0.5px;
    color: var(--text-color);
}

@media (pointer: coarse) {
    .mobile-hint {
        display: none; /* Hide on touch screens */
    }
}

footer {
    padding: 10px;
    text-align: center;
    font-size: 0.8rem;
    opacity: 0.85;
    margin-top: auto;
}

.modal {
    display: none;
    position: fixed;
    z-index: 100;
    inset: 0;
    background-color: rgba(0,0,0,0.5);
    justify-content: center;
    align-items: center;
}

.modal.active { display: flex; }

.modal-content {
    background-color: white;
    padding: 30px;
    border-radius: 20px;
    text-align: center;
    max-width: 90%;
    width: 350px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2);
    color: #333;
}

#play-again-btn {
    padding: 12px 24px;
    font-size: 1.1rem;
    font-weight: 700;
    background-color: var(--correct-color);
    color: white;
    border: none;
    border-radius: 10px;
    cursor: pointer;
}

.toast {
    position: absolute;
    top: 15%;
    left: 50%;
    transform: translateX(-50%);
    background-color: var(--text-color);
    color: white;
    padding: 10px 20px;
    border-radius: 5px;
    font-weight: 700;
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}

.toast.show { opacity: 1; }

/* ==========================================================================
   8. ANIMATIONS & RESPONSIVE OVERRIDES
   ========================================================================== */

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

@keyframes shake {
    0% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    50% { transform: translateX(5px); }
    75% { transform: translateX(-5px); }
    100% { transform: translateX(0); }
}

.tile.shake { animation: shake 0.2s ease-in-out 2; }

.tile.flip { transition: transform 0.4s; transform: rotateX(90deg); }

/* MOBILE (Portrait) */
@media (max-width: 600px) {
    body { align-items: center; }
    #keyboard { width: 100%; padding: 0 5px; }
    .key { max-width: none; height: 45px; padding: 0; font-size: 1rem; }
    .key.large { flex: 1.5; font-size: 0.8rem; }
    #board-container { flex-shrink: 1; min-height: 0; padding: 5px 0; }
    .tile {
        width: clamp(40px, 12vw, 60px);
        height: clamp(40px, 12vw, 60px);
        font-size: clamp(1.4rem, 6vw, 2rem);
    }
    #board { gap: 4px; }
}

/* TV & LARGE SCREENS */
@media (min-width: 1000px) and (min-height: 700px) {
    #game-container { max-width: 800px; }
    .logo { font-size: 6rem; }
    .diff-btn { padding: 25px 50px; font-size: 2rem; border-radius: 20px; }
    .game-title { font-size: 3rem; }
    .tile { width: 80px; height: 80px; font-size: 3rem; border-radius: 12px; }
    .key { max-width: 70px; height: 70px; font-size: 1.8rem; border-radius: 8px; }
    .modal-content { width: 500px; padding: 50px; }
}
