:root {
    --bg-arcade: #0f131f;
    --panel-bg: #161b2e;
    --border-arcade: #2a3454;
    --neon-blue: #38bdf8;
    --neon-pink: #f43f5e;
    --btn-green: #22c55e;
    --btn-blue: #3b82f6;
    --btn-yellow: #eab308;
    --btn-purple: #a855f7;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
}

/* Light theme */
html.light-theme, body.light-theme {
    --bg-arcade: #f1f5f9;
    --panel-bg: #ffffff;
    --border-arcade: #cbd5e1;
    --text-main: #0f131f;
    --text-muted: #64748b;
}
html.light-theme .arcade-cabinet {
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.15), 0 0 30px rgba(56, 189, 248, 0.05);
}
html.light-theme .arcade-input,
html.light-theme .game-panel,
html.light-theme .word-row,
html.light-theme .match-item,
html.light-theme .result-stats-card {
    background: #f8fafc;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent !important;
}

html, body {
    font-family: 'Plus Jakarta Sans', sans-serif;
    background-color: var(--bg-arcade);
    color: var(--text-main);
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100vw;
    min-height: 100vh;
    min-height: 100dvh;
    transition: background-color 0.3s ease, color 0.3s ease;
    -webkit-user-select: none;
    user-select: none;
    overflow: hidden;
    touch-action: none;
}

button, input, select, textarea, .arcade-btn, .match-item, .menu-tile {
    outline: none !important;
    -webkit-appearance: none;
    -webkit-touch-callout: none;
}
button:focus, button:focus-visible, .arcade-btn:focus, .arcade-btn:focus-visible {
    outline: none !important;
    border-color: transparent !important;
}
.arcade-btn.btn-outline:focus { border-color: var(--border-arcade) !important; }

/* ARCADE CABINET (DESKTOP) */
.arcade-cabinet {
    background: var(--panel-bg);
    width: 100%;
    max-width: 650px;
    height: 640px;
    max-height: 95dvh;
    border-radius: 32px;
    border: 4px solid var(--border-arcade);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7), 0 0 30px rgba(56, 189, 248, 0.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

/* Mobile (PORTRAIT) */
@media (max-width: 650px) and (orientation: portrait) {
    body { padding: 10px; touch-action: manipulation; }
    .arcade-cabinet {
        width: 100%;
        height: calc(100dvh - 20px);
        max-width: 100%;
        max-height: calc(100dvh - 20px);
        border-radius: 24px;
    }
    .arcade-screen { padding: 16px 16px calc(24px + env(safe-area-inset-bottom)) !important; }
    .screen-title { font-size: 22px; margin-bottom: 4px; }
    .screen-subtitle { margin-bottom: 15px; font-size: 11px; }
    .game-panel { padding: 12px; margin-bottom: 12px; }
    .arcade-question { font-size: 18px; margin: 8px 0; }
    .arcade-btn { padding: 14px; font-size: 13px; margin-bottom: 10px; }
    .menu-grid { gap: 10px; margin-bottom: 12px; }
    .menu-tile { height: 90px; padding: 12px; }
    #g1-options { gap: 8px; margin-bottom: 10px; }
    .nav-buttons { gap: 8px; margin-bottom: 8px; }
}

/* FIX IOS AUTOZOOM */
.arcade-input {
    width: 100%; background: #0b0f19; border: 2px solid var(--border-arcade); padding: 12px 14px;
    border-radius: 12px; color: white; font-family: inherit; font-weight: 700;
    margin-bottom: 10px; outline: none !important; transition: border-color 0.2s;
    font-size: 16px !important;
}

html.light-theme .arcade-input, body.light-theme .arcade-input { color: #0f131f; }
.arcade-input:focus { border-color: var(--neon-blue); }
.arcade-input:disabled { opacity: 0.5; cursor: not-allowed; }

/* CENTER THIRD BUTTON */
#g1-options button:nth-child(3):last-child {
    grid-column: span 2;
}

/* CUSTOM POPUPS */
#custom-alert-overlay, #custom-edit-overlay, #custom-confirm-overlay, #profile-edit-overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(15, 19, 31, 0.85); display: flex; justify-content: center;
    align-items: center; z-index: 1000; backdrop-filter: blur(3px);
}
#custom-alert-overlay { z-index: 9999 !important; }
.custom-alert-box {
    background: var(--panel-bg); border: 2px solid var(--neon-pink); border-radius: 20px;
    padding: 20px; width: 80%; max-width: 320px; text-align: center;
    box-shadow: 0 10px 30px rgba(0,0,0,0.8), 0 0 20px rgba(244, 63, 94, 0.4);
    animation: popIn 0.2s ease-out;
}
#custom-edit-overlay .custom-alert-box, #profile-edit-overlay .custom-alert-box {
    border-color: var(--neon-blue); box-shadow: 0 10px 30px rgba(0,0,0,0.8), 0 0 20px rgba(56, 189, 248, 0.4);
    max-width: 360px; text-align: left;
}
.custom-alert-message { font-size: 14px; font-weight: 800; color: var(--text-main); margin-bottom: 15px; line-height: 1.4; word-break: break-word; }
@keyframes popIn { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* SPINNER */
.arcade-spinner {
    width: 26px; height: 26px; border: 3px solid var(--border-arcade); border-top: 3px solid var(--neon-blue);
    border-radius: 50%; animation: spin 0.8s linear infinite; margin: 0 auto 8px auto;
}
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

.arcade-top-bar { height: 12px; background: #0f131f; display: flex; justify-content: center; align-items: center; border-bottom: 2px solid var(--border-arcade); }
.arcade-led { width: 40px; height: 4px; background: var(--neon-blue); border-radius: 2px; box-shadow: 0 0 8px var(--neon-blue); }

.arcade-screen { padding: 20px 25px; flex: 1; display: flex; flex-direction: column; position: relative; min-height: 0; overflow-y: auto; }
.arcade-screen > div[id^="screen-"]:not(.hidden) { display: flex; flex-direction: column; flex: 1; min-height: 0; }

.screen-title { text-align: center; font-size: 24px; font-weight: 900; text-transform: uppercase; letter-spacing: 1px; color: var(--text-main); margin-bottom: 4px; }
.screen-subtitle { text-align: center; font-size: 12px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; margin-bottom: 20px; }

.arcade-btn {
    width: 100%; padding: 12px; border-radius: 12px; font-family: inherit; font-size: 13px;
    font-weight: 800; text-transform: uppercase; cursor: pointer; border: none;
    box-shadow: 0 4px 0 rgba(0,0,0,0.3); transform: translateY(0);
    transition: transform 0.05s ease, box-shadow 0.05s ease; margin-bottom: 10px;
}

@media (hover: hover) {
    .arcade-btn:active { transform: translateY(3px) !important; box-shadow: 0 1px 0 rgba(0,0,0,0.3) !important; }
    .menu-tile:active { transform: scale(0.95); }
}

@media (hover: none) {
    .arcade-btn:active, .match-item:active, .menu-tile:active { transform: translateY(0) !important; box-shadow: 0 4px 0 rgba(0,0,0,0.3) !important; }
    .menu-tile:active { transform: scale(1) !important; }
}
.is-pressed { transform: translateY(3px) !important; box-shadow: 0 1px 0 rgba(0,0,0,0.3) !important; }
.menu-tile.is-pressed { transform: scale(0.95) !important; }

.arcade-btn:disabled { opacity: 0.6 !important; cursor: not-allowed !important; pointer-events: none !important; transform: translateY(0) !important; box-shadow: 0 4px 0 rgba(0,0,0,0.3) !important; }

.btn-pink { background: var(--neon-pink); color: white; }
.btn-blue { background: var(--btn-blue); color: white; }
.btn-green { background: var(--btn-green); color: white; }
.btn-yellow { background: var(--btn-yellow); color: #1e293b; }
.btn-outline { background: transparent; border: 2px solid var(--border-arcade); color: var(--text-muted); box-shadow: none; }
.btn-outline:disabled { box-shadow: none !important; border-color: var(--border-arcade) !important; }

/* 🔥 ФІКС КОНТЕЙНЕРА ПРОФІЛЮ ТА МОВИ */
#profile-btn-container {
    position: absolute;
    top: 15px;
    right: 20px;
    display: flex;
    gap: 8px;
    z-index: 20;
}

/* Іконка профілю залишається квадратною */
.profile-icon-btn {
    width: 40px;
    height: 40px;
    background: #1e2640;
    border: 2px solid var(--neon-blue);
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-size: 18px;
    color: var(--text-main);
    transition: all 0.2s ease;
    box-shadow: 0 0 12px rgba(56, 189, 248, 0.3);
    user-select: none;
}

/* 🔥 Кнопка мови стала прямокутною з відступами */
.lang-icon-btn {
    width: auto;
    min-width: 50px;
    padding: 0 12px;
    height: 40px;
    background: #1e2640;
    border: 2px solid var(--neon-blue);
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-size: 14px;
    font-weight: 900;
    color: var(--text-main);
    transition: all 0.2s ease;
    box-shadow: 0 0 12px rgba(56, 189, 248, 0.3);
    user-select: none;
    gap: 6px;
}

html.light-theme .profile-icon-btn, body.light-theme .profile-icon-btn,
html.light-theme .lang-icon-btn, body.light-theme .lang-icon-btn {
    background: #e2e8f0;
    color: var(--text-main);
}

.link-text { text-align: center; font-size: 11px; color: var(--text-muted); text-transform: uppercase; font-weight: 700; cursor: pointer; margin-bottom: 15px; }

.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 15px; }
.menu-tile { height: 110px; border-radius: 18px; padding: 14px; display: flex; flex-direction: column; justify-content: space-between; cursor: pointer; position: relative; overflow: hidden; box-shadow: 0 6px 15px rgba(0,0,0,0.3); transition: transform 0.2s; border: 2px solid rgba(255,255,255,0.1); }
.tile-dot { width: 8px; height: 8px; border-radius: 50%; background: white; box-shadow: 0 0 6px white; }
.tile-title { font-size: 12px; font-weight: 900; text-transform: uppercase; color: white; line-height: 1.2; }
.tile-green { background: var(--btn-green); } .tile-blue { background: var(--btn-blue); }
.tile-yellow { background: var(--btn-yellow); color: #1e293b !important; } .tile-yellow .tile-title { color: #1e293b; }
.tile-purple { background: var(--btn-purple); }

.game-panel { background: #0b0f19; border: 2px solid var(--border-arcade); border-radius: 18px; padding: 16px; text-align: center; margin-bottom: 15px; }

.profile-panel { text-align: left; padding: 18px 20px; margin-bottom: 15px; }
.profile-label { font-size: 11px; color: var(--text-muted); font-weight: 700; margin-bottom: 3px; text-transform: uppercase; }
.profile-value { font-size: 16px; font-weight: 800; color: var(--text-main); margin-bottom: 15px; word-break: break-all; }
.profile-value.blue { color: var(--neon-blue); }
.profile-btn { padding: 10px; font-size: 12px; margin-bottom: 15px; }
.profile-words-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; }

.arcade-question { font-size: 20px; font-weight: 800; color: var(--neon-blue); margin: 12px 0 15px 0; line-height: 1.4; }

.words-scroll-box { flex: 1; min-height: 0; overflow-y: auto; margin-bottom: 12px; padding-right: 4px; }
.word-row { background: #0b0f19; border: 2px solid var(--border-arcade); padding: 10px 14px; border-radius: 12px; font-size: 13px; font-weight: 700; margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.word-row-left { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.word-index { color: var(--text-muted); font-size: 11px; min-width: 22px; flex-shrink: 0; }
.word-row-right { display: flex; justify-content: flex-end; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.word-ua-text { color: var(--neon-blue); margin-right: 4px; text-align: right; word-break: normal; overflow-wrap: break-word; line-height: 1.3; }
.word-actions { display: flex; align-items: center; flex-shrink: 0; gap: 6px; }
.action-icon { cursor: pointer; font-size: 13px; opacity: 0.7; padding: 4px; line-height: 1; }
.action-icon:hover { opacity: 1; transform: scale(1.1); }

.result-stats-card { background: linear-gradient(135deg, #0b0f19 0%, #131b2e 100%); border: 2px solid var(--border-arcade); border-radius: 18px; padding: 16px; text-align: center; margin-bottom: 12px; box-shadow: inset 0 0 12px rgba(0, 0, 0, 0.5); }
html.light-theme .result-stats-card, body.light-theme .result-stats-card { background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%); }
.result-stat-row { display: flex; justify-content: space-around; align-items: center; margin-top: 8px; }
.stat-item { display: flex; flex-direction: column; align-items: center; }
.stat-label { font-size: 10px; font-weight: 800; color: var(--text-muted); text-transform: uppercase; margin-bottom: 3px; }
.stat-value { font-size: 16px; font-weight: 900; color: var(--text-main); }
.stat-value.green { color: var(--btn-green); text-shadow: 0 0 8px rgba(34, 197, 94, 0.4); }
.stat-value.pink { color: var(--neon-pink); text-shadow: 0 0 8px rgba(244, 63, 94, 0.4); }
#result-details .word-row { display: flex; flex-direction: column; align-items: flex-start; gap: 5px; padding: 8px 12px; margin-bottom: 8px; background: #0b0f19; border-radius: 12px; font-size: 12px; line-height: 1.3; }
html.light-theme #result-details .word-row, body.light-theme #result-details .word-row { background: #f8fafc; }
.result-ai-feedback { margin-top: 4px; padding-top: 6px; border-top: 1px solid rgba(255, 255, 255, 0.08); color: var(--text-muted); font-size: 11px; width: 100%; }

/* GAME 4 */
.match-columns { display: flex; gap: 12px; margin-bottom: 12px; }
.match-col { flex: 1; display: flex; flex-direction: column; gap: 6px; }
.match-item { background: #161b2e; border: 2px solid var(--border-arcade); padding: 12px 6px; border-radius: 10px; font-size: 12px; font-weight: 700; cursor: pointer; outline: none !important; text-align: center; transition: all 0.2s; user-select: none; }
html.light-theme .match-item, body.light-theme .match-item { background: #ffffff; color: var(--text-main); }
.match-item:hover { border-color: var(--neon-blue); }
.match-item.selected { background: rgba(56, 189, 248, 0.2); border-color: var(--neon-blue); color: var(--neon-blue); }
.match-item.correct { background: rgba(34, 197, 94, 0.2) !important; border-color: var(--btn-green) !important; color: var(--btn-green) !important; }
.match-item.wrong { background: rgba(244, 63, 94, 0.2) !important; border-color: var(--neon-pink) !important; color: var(--neon-pink) !important; }
.match-item.matched { opacity: 0; visibility: hidden; pointer-events: none; transform: scale(0.8); transition: opacity 0.3s ease, transform 0.3s ease; }

::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: #0b0f19; border-radius: 4px; }
::-webkit-scrollbar-thumb { background: var(--border-arcade); border-radius: 4px; }
.hidden { display: none !important; }
.nav-buttons { display: flex; gap: 8px; margin-bottom: 10px; }

/* 🔥 WORD SCRAMBLE AREAS */
.scramble-answer-box {
    display: flex;
    flex-wrap: wrap;
    gap: clamp(4px, 2vw, 8px);
    justify-content: center;
    align-content: center;
    align-items: center;
    min-height: 70px;
    height: auto;
    margin-bottom: 12px;
    padding: 12px 10px;
    background: #0b0f19;
    border: 2px solid var(--border-arcade);
    border-radius: 12px;
    width: 100%;
    box-sizing: border-box;
    flex-shrink: 0 !important;
}

.scramble-letters-box {
    display: flex;
    flex-wrap: wrap;
    gap: clamp(4px, 2vw, 8px);
    justify-content: center;
    align-items: center;
    margin-bottom: 15px;
    min-height: 50px;
    height: auto;
    width: 100%;
    box-sizing: border-box;
    flex-shrink: 0 !important;
}

.letter-btn {
    width: clamp(32px, 10vw, 42px) !important;
    height: clamp(32px, 10vw, 42px) !important;
    padding: 0 !important;
    margin: 0 !important;
    font-size: clamp(14px, 5vw, 18px) !important;
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
    border-radius: 8px !important;
    flex-shrink: 0 !important;
}

html.light-theme .scramble-answer-box, body.light-theme .scramble-answer-box {
    background: #f1f5f9 !important;
    border-color: #cbd5e1 !important;
}

.scramble-answer-box,
.scramble-letters-box {
    height: auto !important;
    max-height: none !important;
    flex: 0 0 auto !important;
    align-content: flex-start !important;
    overflow: visible !important;
}


/* =========================================================
   📱 ЛАНДШАФТНИЙ РЕЖИМ
   ========================================================= */
@media (max-height: 500px) and (orientation: landscape) {
    html, body {
        position: fixed !important;
        top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important;
        padding: 0 !important; margin: 0 !important;
        display: flex !important; justify-content: center !important; align-items: center !important;
        overflow: hidden !important; touch-action: none !important;
    }

    .arcade-cabinet {
        position: relative !important;
        top: auto !important; left: auto !important; transform: none !important;
        width: 100vw !important; height: 100dvh !important;
        max-width: 100% !important; max-height: 100% !important;
        border-radius: 0 !important; border: none !important;
        margin: 0 !important;
        box-shadow: none !important;
    }

    .arcade-top-bar { display: none !important; }

    .arcade-screen {
        padding: 6px 12px calc(6px + env(safe-area-inset-bottom)) !important;
        overflow-y: auto !important;
        display: flex; flex-direction: column; justify-content: flex-start;
    }

    .screen-title { font-size: 14px !important; margin-top: 2px !important; margin-bottom: 2px !important; }
    .screen-subtitle { font-size: 9px !important; margin-bottom: 6px !important; }

    .game-panel { padding: 4px 8px !important; margin-bottom: 6px !important; border-radius: 8px !important; }
    .arcade-question { font-size: 14px !important; margin: 2px 0 !important; }

    .arcade-btn {
        height: 30px !important;
        min-height: 30px !important;
        padding: 0 10px !important;
        font-size: 10px !important;
        margin-bottom: 6px !important;
        border-radius: 8px !important;
        box-shadow: 0 2px 0 rgba(0,0,0,0.3) !important;
        display: flex !important; align-items: center !important; justify-content: center !important;
    }
    .arcade-btn.is-pressed { transform: translateY(2px) !important; box-shadow: 0 0 0 rgba(0,0,0,0.3) !important; }

    .arcade-input {
        height: 30px !important;
        padding: 0 10px !important;
        margin-bottom: 6px !important;
        margin-top: 0 !important;
        border-radius: 8px !important;
        font-size: 11px !important;
        box-sizing: border-box;
    }

    .menu-grid { gap: 6px !important; margin-bottom: 6px !important; grid-template-columns: repeat(4, 1fr) !important; }
    .menu-tile { height: 42px !important; padding: 4px !important; border-radius: 8px !important; justify-content: center !important; gap: 2px !important;}
    .tile-title { font-size: 8px !important; }
    .tile-dot { display: none !important; }

    #g1-options { gap: 4px !important; margin-bottom: 6px !important; display: grid !important; grid-template-columns: 1fr 1fr !important; }
    #g1-options .arcade-btn { margin-bottom: 0 !important; height: auto !important; min-height: 30px !important; }

    .nav-buttons { margin-bottom: 4px !important; gap: 6px !important; }

    /* 🔥 КНОПКИ В ЛАНДШАФТІ */
    #profile-btn-container { top: 4px !important; right: 10px !important; gap: 6px !important; }
    .profile-icon-btn { width: 26px !important; height: 26px !important; font-size: 12px !important; border-radius: 6px !important; }
    .lang-icon-btn {
        height: 26px !important;
        min-width: 40px !important;
        padding: 0 8px !important;
        font-size: 10px !important;
        border-radius: 6px !important;
        gap: 4px !important;
    }

    #screen-dictionary .words-scroll-box { min-height: 40px !important; margin-bottom: 6px !important; }
    #screen-dictionary > div:last-child { margin-top: 0 !important; }
    #screen-dictionary div[style*="margin-bottom"] { margin-bottom: 4px !important; }
    .word-row { padding: 6px 10px !important; margin-bottom: 6px !important; font-size: 11px !important; border-radius: 8px !important; }

    .match-columns { gap: 6px !important; margin-bottom: 6px !important; }
    .match-item { padding: 4px 2px !important; font-size: 9px !important; border-radius: 6px !important; border-width: 1px !important; min-height: 26px !important; display: flex !important; align-items: center !important; justify-content: center !important;}

    .profile-panel { display: grid !important; grid-template-columns: 1fr 1fr !important; gap: 8px !important; padding: 6px 10px !important; margin-bottom: 6px !important; }
    .profile-label { font-size: 8px !important; margin-bottom: 2px !important; }
    .profile-value { font-size: 11px !important; margin-bottom: 4px !important; }
    .profile-btn { margin-bottom: 0 !important; font-size: 9px !important; height: 26px !important; min-height: 26px !important; }
    .profile-words-row { margin-bottom: 4px !important; }

    .scramble-answer-box {
        width: 100% !important;
        min-height: 36px !important;
        height: auto !important;
        max-height: none !important;
        padding: 4px !important;
        margin-bottom: 6px !important;
        gap: 4px !important;
        border-radius: 8px !important;
        border-width: 1px !important;
        box-sizing: border-box !important;
        overflow: visible !important;
        align-content: flex-start !important;
    }
    .scramble-letters-box {
        min-height: 28px !important;
        height: auto !important;
        max-height: none !important;
        overflow: visible !important;
        align-content: flex-start !important;
        margin-bottom: 6px !important;
        gap: 4px !important;
    }
    .letter-btn {
        width: 24px !important;
        height: 24px !important;
        font-size: 12px !important;
        border-radius: 4px !important;
    }
    #g3-feedback { margin: 2px 0 !important; font-size: 10px !important; min-height: 14px !important; }

    .custom-alert-box {
        padding: 10px 14px !important;
        max-height: 95vh !important;
    }
    .custom-alert-box .screen-subtitle {
        margin-bottom: 6px !important;
        font-size: 11px !important;
    }
    #profile-edit-overlay div[style*="font-size: 11px"] {
        font-size: 8px !important;
        margin-bottom: 2px !important;
    }
    #profile-edit-overlay .arcade-input,
    #custom-edit-overlay .arcade-input {
        margin-bottom: 4px !important;
        padding: 4px 8px !important;
        font-size: 10px !important;
        height: 26px !important;
    }
    #profile-edit-overlay .arcade-btn,
    #custom-edit-overlay .arcade-btn {
        margin-bottom: 4px !important;
        padding: 4px !important;
        font-size: 9px !important;
        height: 26px !important;
        min-height: 26px !important;
    }
    #password-change-container {
        padding-top: 4px !important;
        margin-bottom: 4px !important;
    }
}