:root {
    --sky-deep: #1a7fb5;
    --sky-mid: #3db4e6;
    --sky-light: #7ed6f5;
    --shell: #f4fbff;
    --shell-edge: #b8e4f5;
    --ink: #1a3a4a;
    --sand: #fffdf8;
    --pop-red: #ff5a5f;
    --pop-orange: #ff9f1c;
    --pop-yellow: #ffd166;
    --pop-green: #7bd389;
    --pop-cyan: #2ec4b6;
    --pop-blue: #4cc9f0;
    --pop-indigo: #5b8def;
    --pop-purple: #9b5de5;
    --pop-magenta: #f15bb5;
    --accent: #ff6b35;
    --star: #ffd166;
    --shadow: rgba(26, 58, 74, 0.22);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

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

html,
body {
    min-height: 100%;
}

body {
    font-family: "Fredoka", "Trebuchet MS", sans-serif;
    color: var(--ink);
    background: linear-gradient(165deg, var(--sky-deep) 0%, var(--sky-mid) 45%, var(--sky-light) 100%);
    min-height: 100vh;
    min-height: 100dvh;
    overflow: hidden;
    padding:
        calc(10px + var(--safe-top))
        calc(10px + var(--safe-right))
        calc(10px + var(--safe-bottom))
        calc(10px + var(--safe-left));
}

.bg-blobs {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    background-image:
        radial-gradient(circle at 10% 15%, rgba(255, 209, 102, 0.28) 0 9%, transparent 10%),
        radial-gradient(circle at 88% 18%, rgba(241, 91, 181, 0.2) 0 11%, transparent 12%),
        radial-gradient(circle at 72% 82%, rgba(255, 253, 248, 0.16) 0 13%, transparent 14%),
        radial-gradient(circle at 18% 78%, rgba(46, 196, 182, 0.22) 0 10%, transparent 11%);
    animation: drift 16s ease-in-out infinite alternate;
}

@keyframes drift {
    from { transform: translate3d(0, 0, 0) scale(1); }
    to { transform: translate3d(-1.2%, 1.4%, 0) scale(1.05); }
}

.top-bar,
.game-shell {
    position: relative;
    z-index: 1;
}

.top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    max-width: 920px;
    margin: 0 auto 10px;
}

.home-link,
.score-badge,
.mode-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    padding: 0.45rem 0.9rem;
    border-radius: 12px;
    background: rgba(255, 253, 248, 0.94);
    color: var(--ink);
    font-weight: 600;
    text-decoration: none;
    box-shadow: 0 6px 16px var(--shadow);
}

.mode-badge {
    background: var(--pop-yellow);
}

.mode-badge.medium {
    background: var(--pop-orange);
    color: #3a2208;
}

.mode-badge.hard {
    background: var(--pop-red);
    color: #fff;
}

.game-shell {
    max-width: 920px;
    height: calc(100dvh - 72px - var(--safe-top) - var(--safe-bottom));
    margin: 0 auto;
    position: relative;
}

.start-screen,
.win-overlay {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 1rem;
}

.start-screen {
    text-align: center;
    align-content: center;
    gap: 0.6rem;
    animation: rise-in 0.55s ease-out both;
}

.brand {
    font-size: clamp(2.4rem, 8vw, 4rem);
    font-weight: 700;
    color: var(--sand);
    letter-spacing: 0.02em;
    text-shadow: 0 4px 0 rgba(26, 58, 74, 0.22);
    animation: brand-pop 1.1s ease-out both;
}

.start-screen h1,
.win-message {
    font-size: clamp(1.4rem, 4.5vw, 2rem);
    color: var(--sand);
}

.start-copy,
.win-sub,
.hint {
    color: rgba(255, 253, 248, 0.94);
    font-size: clamp(0.98rem, 3.2vw, 1.2rem);
    max-width: 34ch;
    margin: 0 auto;
    line-height: 1.45;
}

.level-picker {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    width: min(100%, 520px);
    margin: 0.6rem auto 1rem;
}

.level-btn {
    appearance: none;
    border: 3px solid transparent;
    cursor: pointer;
    font: inherit;
    text-align: center;
    padding: 0.75rem 0.5rem;
    border-radius: 16px;
    background: rgba(255, 253, 248, 0.92);
    color: var(--ink);
    box-shadow: 0 6px 14px var(--shadow);
    transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
    display: grid;
    gap: 0.15rem;
}

.level-btn:hover {
    transform: translateY(-2px);
}

.level-btn.selected {
    border-color: var(--pop-yellow);
    box-shadow: 0 0 0 3px rgba(255, 209, 102, 0.45), 0 8px 18px var(--shadow);
    transform: translateY(-2px);
}

.level-num {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--sky-deep);
}

.level-name {
    font-weight: 700;
    font-size: 1rem;
}

.level-hint {
    font-size: 0.78rem;
    opacity: 0.75;
    font-weight: 500;
}

.primary-btn {
    appearance: none;
    border: none;
    cursor: pointer;
    font: inherit;
    font-weight: 700;
    font-size: 1.15rem;
    color: var(--ink);
    background: linear-gradient(180deg, var(--pop-yellow) 0%, var(--pop-orange) 100%);
    padding: 0.9rem 1.6rem;
    border-radius: 14px;
    box-shadow: 0 8px 0 #c46d3a, 0 12px 24px var(--shadow);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.primary-btn:hover {
    transform: translateY(-2px);
}

.primary-btn:active {
    transform: translateY(3px);
    box-shadow: 0 3px 0 #c46d3a, 0 8px 16px var(--shadow);
}

.play-area {
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: 28px;
    background: linear-gradient(180deg, #eef9ff 0%, #d5f0fb 100%);
    border: 4px solid rgba(255, 253, 248, 0.55);
    box-shadow:
        inset 0 0 0 3px rgba(184, 228, 245, 0.9),
        0 16px 40px var(--shadow);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 12px 12px 16px;
    touch-action: manipulation;
}

.play-area[hidden],
.start-screen[hidden],
.win-overlay[hidden] {
    display: none !important;
}

.card-slot {
    width: min(100%, 340px);
    margin: 4px auto 10px;
    flex-shrink: 0;
}

.word-card {
    position: relative;
    background: var(--sand);
    border-radius: 20px;
    border: 3px solid #d9eaf3;
    box-shadow: 0 8px 0 #a9cfe0, 0 14px 24px rgba(26, 58, 74, 0.12);
    padding: 1rem 1.1rem 1.15rem;
    text-align: center;
    min-height: 150px;
    display: grid;
    place-items: center;
    gap: 0.35rem;
    animation: card-slide 0.45s cubic-bezier(0.2, 1.2, 0.4, 1) both;
}

.card-picture {
    font-size: clamp(3.2rem, 12vw, 4.4rem);
    line-height: 1;
    filter: drop-shadow(0 4px 0 rgba(26, 58, 74, 0.08));
    animation: picture-bob 2.4s ease-in-out infinite;
}

.card-word {
    font-size: clamp(1.8rem, 7vw, 2.6rem);
    font-weight: 700;
    letter-spacing: 0.18em;
    color: var(--ink);
}

.card-word.hidden-word {
    visibility: hidden;
}

.card-progress {
    display: flex;
    justify-content: center;
    gap: 0.35rem;
    min-height: 1.4rem;
    margin-top: 0.15rem;
}

.progress-slot {
    width: 1.35rem;
    height: 1.35rem;
    border-radius: 50%;
    border: 2px dashed #9bbdcd;
    background: rgba(61, 180, 230, 0.08);
    display: grid;
    place-items: center;
    font-size: 0.75rem;
    font-weight: 700;
    color: transparent;
    transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

.progress-slot.filled {
    background: var(--pop-cyan);
    border-style: solid;
    border-color: #1f9e92;
    color: #fff;
    transform: scale(1.08);
}

.card-count {
    position: absolute;
    top: 10px;
    right: 12px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--pop-green);
    color: #164a2a;
    font-weight: 700;
    font-size: 0.9rem;
    display: grid;
    place-items: center;
    box-shadow: 0 2px 0 #4ea860;
}

.keyboard-shell {
    flex: 1;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    padding: 0 4px;
}

.keyboard {
    display: grid;
    grid-template-rows: repeat(3, 1fr);
    gap: clamp(6px, 1.4vw, 12px);
    width: min(100%, 720px);
    aspect-ratio: 9 / 3.35;
    max-height: 100%;
    padding: clamp(8px, 2vw, 16px);
    border-radius: 22px;
    background: linear-gradient(180deg, #ffffff 0%, #eaf7fd 100%);
    border: 3px solid var(--shell-edge);
    box-shadow: inset 0 2px 8px rgba(255, 255, 255, 0.8), 0 10px 0 #9fc8da;
}

.keyboard-row {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    gap: clamp(4px, 1vw, 10px);
    min-height: 0;
}

.key {
    appearance: none;
    border: none;
    cursor: pointer;
    font: inherit;
    position: relative;
    aspect-ratio: 1;
    border-radius: 50%;
    color: #fff;
    font-weight: 700;
    font-size: clamp(0.85rem, 2.8vw, 1.35rem);
    display: grid;
    place-items: center;
    box-shadow:
        inset 0 -6px 0 rgba(0, 0, 0, 0.18),
        inset 0 4px 6px rgba(255, 255, 255, 0.35),
        0 4px 8px rgba(26, 58, 74, 0.18);
    transition: transform 0.12s ease, filter 0.12s ease, box-shadow 0.12s ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.key::before {
    content: "";
    position: absolute;
    inset: 14% 18% auto;
    height: 28%;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.35);
    pointer-events: none;
}

.key:active,
.key.pressed {
    transform: scale(0.88) translateY(3px);
    filter: brightness(1.35) saturate(1.2);
    box-shadow:
        inset 0 -2px 0 rgba(0, 0, 0, 0.18),
        inset 0 2px 4px rgba(255, 255, 255, 0.25),
        0 2px 4px rgba(26, 58, 74, 0.15),
        0 0 16px 4px rgba(255, 253, 248, 0.85);
}

.key.waiting {
    animation: key-pulse 1s ease-in-out infinite;
    outline: 3px solid rgba(255, 253, 248, 0.95);
    outline-offset: 2px;
    z-index: 2;
    filter: brightness(1.12);
}

.key.correct {
    animation: key-correct 0.35s ease-out;
}

.key.wrong {
    animation: key-wrong 0.35s ease-out;
}

.key.star {
    font-size: clamp(1rem, 3.2vw, 1.55rem);
    color: #5a4208;
}

.hint {
    margin-top: 8px;
    padding: 0.35rem 0.85rem;
    border-radius: 999px;
    background: rgba(26, 58, 74, 0.45);
    white-space: nowrap;
    flex-shrink: 0;
    max-width: 95%;
    text-align: center;
}

.win-overlay {
    z-index: 5;
    background: rgba(26, 58, 74, 0.32);
    backdrop-filter: blur(2px);
}

.win-card {
    text-align: center;
    padding: 1.5rem 1.25rem;
    border-radius: 22px;
    background: rgba(26, 58, 74, 0.78);
    border: 2px solid rgba(255, 253, 248, 0.28);
    animation: rise-in 0.4s ease-out both;
    min-width: min(90vw, 320px);
}

.win-message {
    margin-bottom: 0.35rem;
    animation: celebrate-pop 0.55s cubic-bezier(0.2, 1.4, 0.4, 1) both;
}

.win-sub {
    margin: 0;
    letter-spacing: 0.2em;
    font-weight: 700;
    font-size: 1.4rem;
}

#confetti-canvas {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

@keyframes rise-in {
    from {
        opacity: 0;
        transform: translateY(18px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes brand-pop {
    0% {
        opacity: 0;
        transform: scale(0.86) translateY(12px);
    }
    60% {
        opacity: 1;
        transform: scale(1.04) translateY(0);
    }
    100% {
        transform: scale(1);
    }
}

@keyframes card-slide {
    from {
        opacity: 0;
        transform: translateY(-18px) scale(0.94);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes picture-bob {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
}

@keyframes key-pulse {
    0%, 100% {
        transform: scale(1);
        box-shadow:
            inset 0 -6px 0 rgba(0, 0, 0, 0.18),
            inset 0 4px 6px rgba(255, 255, 255, 0.35),
            0 4px 8px rgba(26, 58, 74, 0.18),
            0 0 0 0 rgba(255, 253, 248, 0.7);
    }
    50% {
        transform: scale(1.08);
        box-shadow:
            inset 0 -6px 0 rgba(0, 0, 0, 0.18),
            inset 0 4px 6px rgba(255, 255, 255, 0.45),
            0 6px 12px rgba(26, 58, 74, 0.22),
            0 0 18px 4px rgba(255, 253, 248, 0.85);
    }
}

@keyframes key-correct {
    0% { transform: scale(0.88); }
    50% { transform: scale(1.12); }
    100% { transform: scale(1); }
}

@keyframes key-wrong {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

@keyframes celebrate-pop {
    0% {
        opacity: 0;
        transform: scale(0.7) rotate(-4deg);
    }
    70% {
        opacity: 1;
        transform: scale(1.08) rotate(2deg);
    }
    100% {
        transform: scale(1) rotate(0);
    }
}

@media (max-width: 640px) {
    .top-bar {
        margin-bottom: 8px;
    }

    .home-link,
    .score-badge,
    .mode-badge {
        min-height: 36px;
        padding: 0.35rem 0.7rem;
        font-size: 0.92rem;
    }

    .game-shell {
        height: calc(100dvh - 64px - var(--safe-top) - var(--safe-bottom));
    }

    .level-picker {
        gap: 8px;
    }

    .level-btn {
        padding: 0.55rem 0.35rem;
        border-radius: 12px;
    }

    .level-hint {
        display: none;
    }

    .word-card {
        min-height: 130px;
        padding: 0.8rem 0.9rem 0.95rem;
    }

    .hint {
        font-size: 0.92rem;
        white-space: normal;
    }

    .primary-btn {
        width: min(100%, 280px);
    }

    .keyboard {
        box-shadow: inset 0 2px 8px rgba(255, 255, 255, 0.8), 0 7px 0 #9fc8da;
    }
}

@media (max-height: 700px) {
    .word-card {
        min-height: 110px;
        padding: 0.65rem 0.8rem 0.75rem;
    }

    .card-picture {
        font-size: clamp(2.4rem, 8vw, 3.2rem);
    }

    .card-word {
        font-size: clamp(1.4rem, 5vw, 1.9rem);
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
