:root {
    /* Core palette — purple base */
    --bg-color: #0a0118;
    --bg-deep: #050010;
    --card-bg: #160a2e;
    --card-glass: rgba(30, 16, 60, 0.55);
    --text-color: #f4f0ff;
    --text-muted: #b9a8e6;

    --accent-purple: #c084fc;
    --accent-violet: #9d5cff;
    --accent-magenta: #e879f9;
    --accent-blue: #8b7dff;
    --accent-cyan: #38bdf8;

    --correct: #22c55e;
    --correct-glow: rgba(34, 197, 94, 0.55);
    --present: #eab308;
    --present-glow: rgba(234, 179, 8, 0.5);
    --absent: #2b2440;
    --border: #2a1f52;

    --grad-purple: linear-gradient(135deg, #9d5cff 0%, #c084fc 50%, #e879f9 100%);
    --tile-gap: clamp(4px, 1.4vw, 8px);
    --radius: 14px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
}

body {
    font-family: 'Space Grotesk', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: stretch;
    overflow: hidden;
    position: relative;
}

/* Ambient aurora background */
.bg-aurora {
    position: fixed;
    inset: 0;
    z-index: -1;
    background:
        radial-gradient(45% 40% at 18% 12%, rgba(157, 92, 255, 0.30), transparent 70%),
        radial-gradient(40% 35% at 85% 8%, rgba(232, 121, 249, 0.20), transparent 70%),
        radial-gradient(55% 50% at 50% 110%, rgba(139, 125, 255, 0.22), transparent 70%),
        var(--bg-deep);
    animation: auroraDrift 18s ease-in-out infinite alternate;
}

@keyframes auroraDrift {
    0%   { transform: scale(1) translateY(0); filter: hue-rotate(0deg); }
    100% { transform: scale(1.12) translateY(-2%); filter: hue-rotate(18deg); }
}

.container {
    width: 100%;
    max-width: 460px;
    padding: calc(12px + var(--safe-top)) 14px calc(10px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
    height: 100dvh;
    margin: 0 auto;
    gap: 6px;
}

/* ===== Header / Profile ===== */
.app-header {
    display: flex;
    align-items: center;
    min-height: 56px;
    flex: 0 0 auto;
}

#user-profile {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 12px;
    padding: 8px 12px;
    background: var(--card-glass);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    animation: fadeSlide 0.5s ease both;
}

#user-profile.hidden { display: none; }

.profile-main {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.avatar-container {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    padding: 2px;
    background: var(--grad-purple);
    overflow: hidden;
    flex-shrink: 0;
    box-shadow: 0 0 14px rgba(192, 132, 252, 0.45);
}

#user-avatar {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    background: var(--card-bg);
}

.user-info { min-width: 0; }

.user-info h3 {
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 120px;
}

.user-info span {
    font-size: 11px;
    color: var(--accent-purple);
    opacity: 0.85;
}

.profile-stats {
    display: flex;
    gap: 14px;
    flex-shrink: 0;
}

.mini-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.mini-stat span {
    font-size: 15px;
    font-weight: 700;
    color: #fff;
    line-height: 1.1;
}

.mini-stat label {
    font-size: 8px;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 1px;
    margin-top: 2px;
}

/* ===== Controls ===== */
.controls-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex: 0 0 auto;
    padding: 0 2px;
}

.brand {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 3px;
    text-transform: uppercase;
    background: var(--grad-purple);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

#lang-select {
    background: var(--card-glass);
    border: 1px solid var(--border);
    color: var(--text-color);
    padding: 6px 10px;
    font-size: 12px;
    border-radius: 8px;
    font-family: inherit;
    cursor: pointer;
    transition: border-color 0.25s, box-shadow 0.25s;
    backdrop-filter: blur(8px);
}

#lang-select:focus {
    outline: none;
    border-color: var(--accent-purple);
    box-shadow: 0 0 0 3px rgba(192, 132, 252, 0.18);
}

/* ===== Board ===== */
.play-area {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2px 0;
}

#game-board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    gap: var(--tile-gap);
    width: min(90vw, calc(48dvh * 5 / 6), 330px);
    aspect-ratio: 5 / 6;
    margin: auto;
}

.row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: var(--tile-gap);
}

.tile {
    width: 100%;
    aspect-ratio: 1;
    border: 2px solid var(--border);
    background: rgba(22, 10, 46, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(22px, 7vw, 32px);
    font-weight: 700;
    text-transform: uppercase;
    border-radius: 10px;
    color: #fff;
    user-select: none;
}

.tile.filled {
    border-color: var(--accent-violet);
    background: rgba(157, 92, 255, 0.12);
}

.tile.pop {
    animation: pop 0.12s ease-in-out;
}

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

.tile.reveal {
    animation: flip 0.55s ease forwards;
}

.tile.correct {
    background-color: var(--correct);
    border-color: var(--correct);
    box-shadow: 0 0 18px var(--correct-glow);
}

.tile.present {
    background-color: var(--present);
    border-color: var(--present);
    box-shadow: 0 0 18px var(--present-glow);
}

.tile.absent {
    background-color: var(--absent);
    border-color: var(--absent);
}

.tile.win-bounce {
    animation: bounce 0.6s ease;
}

/* ===== Status ===== */
#status-container {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 0 0 auto;
    min-height: 46px;
    margin: 2px auto;
    width: 100%;
    max-width: 330px;
    background: var(--card-glass);
    padding: 8px 14px;
    border-radius: 12px;
    border: 1px solid var(--border);
    backdrop-filter: blur(8px);
}

#timer-display {
    font-size: 19px;
    font-weight: 700;
    color: var(--accent-purple);
    font-variant-numeric: tabular-nums;
    letter-spacing: 1px;
    text-shadow: 0 0 12px rgba(192, 132, 252, 0.4);
}

#timer-display.urgent {
    color: var(--accent-magenta);
    animation: pulse 1s ease-in-out infinite;
}

#timer-display.hidden { display: none; }

#game-summary {
    font-size: 14px;
    font-weight: 600;
    color: #fff;
    text-align: center;
    width: 100%;
}

#game-summary.hidden { display: none; }

/* ===== Keyboard ===== */
#keyboard {
    display: flex;
    flex-direction: column;
    gap: clamp(5px, 1.4vw, 8px);
    flex: 0 0 auto;
    padding-bottom: 6px;
}

.kb-row {
    display: flex;
    justify-content: center;
    gap: clamp(4px, 1.2vw, 6px);
}

.key {
    background: rgba(42, 31, 82, 0.85);
    border: 1px solid rgba(157, 92, 255, 0.18);
    color: #fff;
    padding: clamp(11px, 3.4vw, 16px) 0;
    min-width: 0;
    font-size: clamp(13px, 3.6vw, 17px);
    flex: 1;
    border-radius: 8px;
    font-weight: 700;
    cursor: pointer;
    text-transform: uppercase;
    transition: transform 0.08s, background 0.2s, box-shadow 0.2s;
}

.key.large {
    flex: 1.6;
    font-size: clamp(10px, 3vw, 13px);
}

.key:active {
    transform: scale(0.92);
    background: var(--accent-violet);
}

.key.correct { background-color: var(--correct); border-color: var(--correct); }
.key.present { background-color: var(--present); border-color: var(--present); }
.key.absent  { background-color: var(--absent); border-color: var(--absent); opacity: 0.65; }

/* ===== Animations ===== */
@keyframes pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.08); }
    100% { transform: scale(1); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-6px); }
    40%, 80% { transform: translateX(6px); }
}

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

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    40% { transform: translateY(-22px); }
    70% { transform: translateY(-8px); }
}

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

@keyframes fadeSlide {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ===== Loading ===== */
.loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 400;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 18px;
    background: var(--bg-deep);
    transition: opacity 0.5s ease;
}

.loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.loader-ring {
    width: 54px;
    height: 54px;
    border-radius: 50%;
    border: 3px solid rgba(192, 132, 252, 0.18);
    border-top-color: var(--accent-purple);
    animation: spin 0.8s linear infinite;
}

.loader-text {
    font-size: 13px;
    letter-spacing: 6px;
    font-weight: 700;
    background: var(--grad-purple);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Tutorial RPG Styles (shared with tutorial.html — do not remove) ===== */
#dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
    transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

#dialog-overlay.at-bottom {
    align-items: flex-end;
    padding-bottom: 80px;
}

.dialog-container {
    position: relative;
    width: 90%;
    max-width: 550px;
    display: flex;
    flex-direction: column;
    pointer-events: auto;
}

.mascot-frame {
    position: absolute;
    top: -160px;
    left: -20px;
    width: 220px;
    height: 220px;
    flex-shrink: 0;
    overflow: visible;
    background: transparent;
    animation: bobbing 3s ease-in-out infinite;
    pointer-events: none;
    z-index: 10;
}

.mascot-frame img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 0 20px rgba(192, 132, 252, 0.6));
}

.speaker-name {
    position: absolute;
    top: -40px;
    right: 5px;
    margin: 0;
    color: var(--accent-purple);
    font-size: 20px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 3px;
    text-shadow: 0 0 15px rgba(192, 132, 252, 0.8);
}

.dialog-box {
    background: rgba(15, 5, 33, 0.98);
    border: 2px solid var(--accent-purple);
    border-radius: 20px;
    padding: 30px 35px;
    box-shadow: 0 0 50px rgba(192, 132, 252, 0.3);
    backdrop-filter: blur(20px);
    width: 100%;
    position: relative;
}

#dialog-text {
    margin: 0;
    font-size: 18px;
    line-height: 1.7;
    color: white;
    min-height: 3.5em;
}

#dialog-options {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    justify-content: center;
}

.opt-btn {
    background: rgba(15, 5, 33, 0.95);
    border: 2px solid var(--accent-purple);
    color: white;
    padding: 14px 28px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 5px 20px rgba(0,0,0,0.4);
}

.opt-btn:hover {
    background: var(--accent-purple);
    box-shadow: 0 0 30px rgba(192, 132, 252, 0.7);
    transform: translateY(-3px);
}

/* Intro Cinematic */
#intro-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: #000;
    z-index: 3000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    cursor: pointer;
    margin: 0;
    padding: 40px;
    opacity: 1;
    transition: opacity 1.5s ease;
}

#intro-text {
    font-size: 24px;
    font-weight: 300;
    color: white;
    max-width: 700px;
    line-height: 1.8;
    opacity: 0;
    transition: opacity 2.5s ease;
    pointer-events: none;
}

#intro-subtext {
    font-size: 14px;
    color: var(--accent-blue);
    margin-top: 40px;
    letter-spacing: 4px;
    text-transform: uppercase;
    opacity: 0;
    transition: opacity 1.5s ease;
    pointer-events: none;
}

#intro-overlay.fade-out {
    opacity: 0;
    pointer-events: none;
}

/* Highlights */
.highlight-board {
    z-index: 500;
    position: relative;
    box-shadow: 0 0 0 9999px rgba(0,0,0,0.85);
    border-radius: 15px;
}

.tile.highlight {
    border-color: var(--correct);
    box-shadow: 0 0 30px rgba(34, 197, 94, 1);
    transform: scale(1.1);
    z-index: 600;
}

.tile.highlight-warn {
    border-color: var(--present);
    box-shadow: 0 0 30px rgba(234, 179, 8, 1);
    transform: scale(1.1);
    z-index: 600;
}

.tile.highlight-error {
    border-color: var(--absent);
    box-shadow: 0 0 30px rgba(63, 63, 70, 1);
    transform: scale(1.1);
    z-index: 600;
}

#fade-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: black;
    opacity: 0;
    pointer-events: none;
    z-index: 2000;
    transition: opacity 1.5s ease;
}

#fade-overlay.active {
    opacity: 1;
}

@keyframes bobbing {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-20px); }
}

/* ===== Modal ===== */
#modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(5, 0, 16, 0.82);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    padding: 20px;
    transition: opacity 0.3s;
}

#modal-overlay.hidden { display: none; }

.modal {
    background: linear-gradient(160deg, #1a0e36 0%, #0f0521 100%);
    border: 1px solid rgba(192, 132, 252, 0.4);
    padding: 32px 28px;
    border-radius: 22px;
    text-align: center;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 24px 60px rgba(0,0,0,0.6), 0 0 30px rgba(192, 132, 252, 0.18);
    animation: modalIn 0.35s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes modalIn {
    from { opacity: 0; transform: translateY(20px) scale(0.94); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

#modal-title {
    font-size: 26px;
    font-weight: 700;
    letter-spacing: 2px;
    margin-bottom: 12px;
}

#modal-message {
    font-size: 15px;
    line-height: 1.6;
    color: var(--text-muted);
    margin-bottom: 6px;
}

#modal-message code {
    background: rgba(192, 132, 252, 0.15);
    color: var(--accent-purple);
    padding: 2px 7px;
    border-radius: 6px;
    font-size: 13px;
}

.stats {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin: 22px 0 4px;
}

.stats.hidden { display: none; }

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-item span {
    font-size: 30px;
    font-weight: 700;
    color: #fff;
}

.stat-item label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-top: 4px;
}

#modal-close, #modal-start-btn {
    border: none;
    color: #fff;
    padding: 13px 28px;
    border-radius: 12px;
    cursor: pointer;
    font-weight: 700;
    font-family: inherit;
    font-size: 15px;
    display: block;
    margin: 18px auto 0;
    width: 100%;
    max-width: 220px;
    transition: transform 0.12s, box-shadow 0.25s;
}

#modal-start-btn {
    background: var(--grad-purple);
    box-shadow: 0 8px 22px rgba(157, 92, 255, 0.4);
    margin-bottom: 4px;
}

#modal-start-btn:active { transform: scale(0.96); }

#modal-start-btn.hidden { display: none; }

#modal-close {
    background: rgba(42, 31, 82, 0.9);
    border: 1px solid var(--border);
}

#modal-close:active { transform: scale(0.96); }

/* ===== Toast ===== */
#message-container {
    position: fixed;
    top: calc(72px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 200;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
}

.toast {
    background: rgba(244, 240, 255, 0.96);
    color: #1a0e36;
    padding: 10px 22px;
    border-radius: 10px;
    font-weight: 700;
    font-size: 14px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.45);
    animation: fadeInOut 2s forwards;
}

@keyframes fadeInOut {
    0% { opacity: 0; transform: translateY(-16px); }
    15% { opacity: 1; transform: translateY(0); }
    85% { opacity: 1; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(-16px); }
}

/* Small height phones */
@media (max-height: 640px) {
    #game-board { width: min(82vw, calc(44dvh * 5 / 6), 300px); }
    .avatar-container { width: 36px; height: 36px; }
}
