:root {
    --bg: #f4efe6;
    --panel: #fff9ef;
    --panel-strong: #f7e5be;
    --ink: #2f2418;
    --muted: #77644e;
    --board: #8d6e63;
    --accent: #2ecc71;
    --danger: #e74c3c;
    --line: #dfcfb7;
}

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

body {
    min-height: 100vh;
    background:
        radial-gradient(circle at top, rgba(255, 255, 255, 0.65), transparent 35%),
        linear-gradient(180deg, #fbf6ed 0%, #f1e5d2 100%);
    color: var(--ink);
    font-family: 'Segoe UI', sans-serif;
}

button {
    border: none;
    border-radius: 14px;
    cursor: pointer;
    font-weight: 800;
    transition: transform 140ms ease, box-shadow 140ms ease, background 140ms ease;
}

button:hover {
    transform: translateY(-1px);
}

button:active {
    transform: translateY(1px);
}

.app-shell {
    width: 100%;
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}

.screen {
    display: none;
    width: 100%;
}

.screen-active {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.loading-screen,
.menu-screen,
.game-screen {
    justify-content: center;
}

.loading-card,
.menu-card {
    width: min(100%, 720px);
    background: rgba(255, 249, 239, 0.92);
    border: 1px solid var(--line);
    border-radius: 28px;
    box-shadow: 0 20px 60px rgba(93, 63, 34, 0.15);
    padding: 32px;
}

.loading-card {
    text-align: center;
}

.loading-label,
.eyebrow {
    text-transform: uppercase;
    letter-spacing: 0.24em;
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 12px;
}

.loading-card h1,
.menu-card h1 {
    font-size: clamp(42px, 7vw, 72px);
    line-height: 1;
    margin-bottom: 14px;
}

#loadingStatus,
.menu-status {
    color: var(--muted);
    font-size: 16px;
}

.loading-bar {
    margin-top: 28px;
    height: 14px;
    border-radius: 999px;
    background: #eadcc5;
    overflow: hidden;
}

.loading-bar-fill {
    width: 38%;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, #e7b45b 0%, #f0c060 60%, #f8d893 100%);
    animation: loadPulse 1.2s ease-in-out infinite;
}

.menu-actions {
    display: flex;
    gap: 14px;
    margin-top: 28px;
    flex-wrap: wrap;
}

.menu-btn {
    min-width: 160px;
    padding: 14px 22px;
    background: white;
    color: var(--ink);
    box-shadow: 0 5px 0 #d8c2a2;
}

.menu-btn-primary {
    background: #f0c060;
    box-shadow: 0 5px 0 #d49b2e;
}

.menu-panel {
    margin-top: 26px;
    background: #fffdf8;
    border: 1px solid var(--line);
    border-radius: 22px;
    padding: 22px;
    max-height: min(62vh, 520px);
    overflow: hidden;
}

.menu-panel.hidden {
    display: none;
}

.panel-header h2 {
    font-size: 26px;
    margin-bottom: 6px;
}

.panel-header p {
    color: var(--muted);
}

.controls {
    margin-top: 18px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(68px, 1fr));
    gap: 10px;
    max-height: min(46vh, 360px);
    overflow-y: auto;
    padding-right: 6px;
    align-content: start;
}

.controls::-webkit-scrollbar {
    width: 10px;
}

.controls::-webkit-scrollbar-track {
    background: #f1e6d2;
    border-radius: 999px;
}

.controls::-webkit-scrollbar-thumb {
    background: #d3b98b;
    border-radius: 999px;
}

.controls::-webkit-scrollbar-thumb:hover {
    background: #c59f5a;
}

.lvl-btn {
    min-height: 64px;
    background: white;
    color: var(--ink);
    box-shadow: 0 4px 0 #d9ccb7;
    font-size: 18px;
}

.lvl-btn.active {
    background: var(--accent);
    color: white;
    box-shadow: 0 4px 0 #219653;
}

.lvl-btn:disabled {
    background: #e6ddd0;
    color: #9b8b78;
    box-shadow: 0 4px 0 #c9bba8;
    cursor: not-allowed;
}

.lvl-btn:disabled:hover {
    transform: none;
}

.settings-content {
    margin-top: 18px;
    display: grid;
    gap: 12px;
    color: var(--muted);
    line-height: 1.5;
}

.status-card {
    background: #f8f2e8;
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 14px 16px;
}

.status-card strong {
    color: var(--ink);
}

.game-screen {
    width: min(100%, 560px);
}

.ui-header {
    text-align: center;
    margin-bottom: 14px;
}

.ui-header h2 {
    font-size: clamp(30px, 4vw, 44px);
    margin-bottom: 8px;
}

.game-wrapper {
    padding: 18px;
    border-radius: 28px;
    background: rgba(255, 255, 255, 0.6);
    border: 1px solid rgba(223, 207, 183, 0.8);
    box-shadow: 0 16px 40px rgba(93, 63, 34, 0.12);
}

.game-wrapper.wall-shake {
    animation: wallShake 180ms ease-in-out;
}

canvas {
    background: transparent;
    cursor: pointer;
    display: block;
}

.hint-box {
    margin-top: 18px;
    font-size: 14px;
    color: #27ae60;
    background: #eafaf1;
    padding: 12px 20px;
    border-radius: 20px;
    border: 1px dashed #27ae60;
    text-align: center;
    width: 100%;
}

.game-actions {
    display: flex;
    gap: 14px;
    margin-top: 18px;
}

.btn-secondary,
.btn-reset {
    flex: 1;
    padding: 14px 18px;
    color: white;
}

.btn-secondary {
    background: #6c5ce7;
    box-shadow: 0 4px 0 #4b3eb7;
}

.btn-reset {
    background: var(--danger);
    box-shadow: 0 4px 0 #c0392b;
}

#gamePopup {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 30px;
    border-radius: 20px;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
    text-align: center;
    z-index: 100;
    min-width: 280px;
    border: 1px solid rgba(255, 214, 102, 0.45);
    overflow: hidden;
}

#mechanicPopup {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(92vw, 360px);
    background: linear-gradient(180deg, #fffdf8 0%, #fff6e7 100%);
    padding: 28px 24px 24px;
    border-radius: 24px;
    box-shadow: 0 20px 70px rgba(69, 48, 21, 0.35);
    text-align: center;
    z-index: 101;
    border: 1px solid rgba(223, 207, 183, 0.9);
}

.mechanic-label {
    text-transform: uppercase;
    letter-spacing: 0.22em;
    font-size: 11px;
    color: var(--muted);
    margin-bottom: 10px;
}

#mechanicTitle {
    font-size: 32px;
    margin-bottom: 8px;
}

#mechanicMsg {
    color: var(--muted);
    font-size: 15px;
    line-height: 1.45;
}

.mechanic-icon-card {
    --mechanic-fill: 0%;
    width: 160px;
    height: 160px;
    margin: 20px auto 14px;
    border-radius: 34px;
    position: relative;
    overflow: hidden;
    background: #fff;
    border: 2px solid #ead9bf;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.7), 0 10px 24px rgba(123, 89, 42, 0.12);
}

.mechanic-icon-fill {
    position: absolute;
    inset: auto 0 0 0;
    height: var(--mechanic-fill);
    background: linear-gradient(180deg, rgba(255,255,255,0.2) 0%, rgba(255,255,255,0) 100%), var(--mechanic-accent, #f4c542);
    transition: height 220ms ease;
}

.mechanic-icon-unfilled {
    position: absolute;
    inset: 0;
    z-index: 2;
    background: #111111;
    clip-path: inset(0 0 var(--mechanic-fill) 0);
    transition: clip-path 220ms ease;
    pointer-events: none;
}

.mechanic-icon-art {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 108px;
    height: 108px;
    transform: translate(-50%, -50%);
    z-index: 1;
    filter: drop-shadow(0 8px 14px rgba(0,0,0,0.16));
}

.mechanic-key .mechanic-icon-art {
    top: auto;
    bottom: 18px;
    width: 116px;
    height: 116px;
    transform: translateX(-50%);
}

.mechanic-frozen .mechanic-icon-art,
.mechanic-sleep .mechanic-icon-art,
.mechanic-bomb .mechanic-icon-art {
    top: auto;
    bottom: 20px;
    width: 112px;
    height: 112px;
    transform: translateX(-50%);
}

.mechanic-sleep .mechanic-icon-art {
    width: 118px;
    height: 118px;
}

.mechanic-svg-base,
.mechanic-svg-fill-clip {
    position: absolute;
    inset: 0;
}

.mechanic-icon-art svg {
    width: 100%;
    height: 100%;
    display: block;
}

.mechanic-svg-fill-clip {
    clip-path: inset(calc(100% - var(--mechanic-fill)) 0 0 0);
    transition: clip-path 220ms ease;
}

.mechanic-percent {
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    z-index: 3;
    padding: 6px 12px;
    border-radius: 999px;
    background: rgba(47, 36, 24, 0.78);
    color: white;
    font-size: 18px;
    font-weight: 800;
    letter-spacing: 0.04em;
}

.mechanic-progress-track {
    width: 100%;
    height: 12px;
    border-radius: 999px;
    background: #ebdcc4;
    overflow: hidden;
}

.mechanic-progress-fill {
    width: 0%;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, #e6b24f 0%, #f1c86b 100%);
    transition: width 220ms ease;
}

.mechanic-key {
    --mechanic-accent: linear-gradient(180deg, #f8d77a 0%, #f4c542 100%);
}

.mechanic-frozen {
    --mechanic-accent: linear-gradient(180deg, #d4f1ff 0%, #89d3ff 100%);
}

.mechanic-sleep {
    --mechanic-accent: linear-gradient(180deg, #d9d5ff 0%, #9c8df5 100%);
}

.mechanic-bomb {
    --mechanic-accent: linear-gradient(180deg, #ffb1a7 0%, #ff6b5f 100%);
}

#overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 90;
}

.popup-btn {
    margin-top: 20px;
    padding: 12px 30px;
    font-size: 18px;
    background: var(--accent);
    color: white;
    border-radius: 12px;
}

#gamePopup.popup-win {
    background:
        radial-gradient(circle at top, rgba(255, 245, 196, 0.95), rgba(255, 255, 255, 0.96) 45%),
        white;
    box-shadow: 0 20px 80px rgba(255, 203, 87, 0.38);
    animation: winPopupFloat 650ms ease-out;
}

#gamePopup.popup-win::before {
    content: '';
    position: absolute;
    inset: -20% 10% auto;
    height: 90px;
    background: radial-gradient(circle, rgba(255, 213, 79, 0.55), transparent 68%);
    pointer-events: none;
}

#overlay.overlay-win {
    background: rgba(82, 52, 17, 0.45);
    animation: overlayGlow 700ms ease-out;
}

#hardLevelNotice {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 120;
    text-align: center;
    background: radial-gradient(circle at center, rgba(140, 15, 24, 0.16), rgba(20, 2, 4, 0.02) 58%, transparent 78%);
    pointer-events: none;
}

.hard-level-flare {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: min(88vw, 520px);
    animation: hardLevelFlicker 1450ms ease-out;
}

.hard-level-text {
    font-size: clamp(38px, 9vw, 72px);
    font-weight: 900;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #ff5447;
    text-shadow:
        0 0 8px rgba(255, 74, 58, 0.55),
        0 0 18px rgba(255, 42, 42, 0.45),
        0 0 38px rgba(255, 8, 8, 0.35);
    margin-bottom: 8px;
}

.hard-level-subtext {
    color: rgba(255, 231, 231, 0.92);
    font-size: 15px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    text-shadow: 0 0 14px rgba(118, 0, 0, 0.35);
}

@keyframes loadPulse {
    0% {
        transform: translateX(-35%);
    }
    50% {
        transform: translateX(95%);
    }
    100% {
        transform: translateX(-35%);
    }
}

@keyframes wallShake {
    0% {
        transform: translateX(0);
    }
    20% {
        transform: translateX(-7px);
    }
    45% {
        transform: translateX(6px);
    }
    70% {
        transform: translateX(-4px);
    }
    100% {
        transform: translateX(0);
    }
}

@keyframes winPopupFloat {
    0% {
        opacity: 0;
        transform: translate(-50%, -44%) scale(0.9);
    }
    60% {
        opacity: 1;
        transform: translate(-50%, -51%) scale(1.03);
    }
    100% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

@keyframes overlayGlow {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

@keyframes hardLevelFlicker {
    0% {
        opacity: 0;
        transform: translate(-50%, calc(-50% - 8px)) scale(0.96);
    }
    10% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
    22% {
        opacity: 0.82;
    }
    32% {
        opacity: 1;
    }
    58% {
        opacity: 0.88;
    }
    72% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
    100% {
        opacity: 0;
        transform: translate(-50%, calc(-50% - 6px)) scale(0.985);
    }
}

@media (max-width: 640px) {
    .app-shell {
        padding: 16px;
    }

    .loading-card,
    .menu-card {
        padding: 24px 20px;
        border-radius: 24px;
    }

    .menu-actions,
    .game-actions {
        flex-direction: column;
    }

    .game-wrapper {
        padding: 12px;
    }

    #mechanicPopup {
        padding: 24px 18px 20px;
    }

    .mechanic-icon-card {
        width: 140px;
        height: 140px;
    }

    .mechanic-icon-glyph {
        font-size: 58px;
    }

    canvas {
        width: 100%;
        height: auto;
    }
}
