:root {
    color-scheme: light;
    font-family: Nunito, "Trebuchet MS", system-ui, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

*, *::before, *::after { box-sizing: border-box; }

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
    background: #dff7ff;
}

body {
    min-height: 100dvh;
    user-select: none;
    -webkit-user-select: none;
}

button,
select,
a { font: inherit; }

.puzzle-game {
    position: relative;
    isolation: isolate;
    width: 100%;
    height: 100dvh;
    overflow: hidden;
    background:
        radial-gradient(circle at 11% 18%, rgba(255,255,255,.95) 0 4.5%, transparent 4.8%),
        radial-gradient(circle at 17% 18%, rgba(255,255,255,.78) 0 3.6%, transparent 3.9%),
        radial-gradient(circle at 88% 23%, rgba(255,255,255,.76) 0 5%, transparent 5.3%),
        linear-gradient(155deg, #c9f5ff 0%, #e8f9ff 43%, #f1e8ff 100%);
}

.puzzle-game::before,
.puzzle-game::after {
    content: '';
    position: absolute;
    z-index: -1;
    border-radius: 50%;
    pointer-events: none;
}

.puzzle-game::before {
    right: -120px;
    bottom: -150px;
    width: 430px;
    height: 430px;
    background: rgba(239, 1, 127, .15);
    box-shadow: -95px -28px 0 rgba(147, 72, 255, .09);
}

.puzzle-game::after {
    left: -100px;
    bottom: -120px;
    width: 310px;
    height: 310px;
    background: rgba(34, 191, 163, .15);
    box-shadow: 70px -25px 0 rgba(0, 133, 222, .08);
}

.puzzle-topbar {
    position: absolute;
    inset: 0 0 auto;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: max(12px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 10px max(14px, env(safe-area-inset-left));
    pointer-events: none;
}

.puzzle-brand {
    position: absolute;
    top: max(17px, calc(env(safe-area-inset-top) + 5px));
    left: 50%;
    z-index: 29;
    width: 108px;
    transform: translateX(-50%);
    pointer-events: none;
}

.puzzle-brand img {
    display: block;
    width: 100%;
    height: auto;
    opacity: .82;
}

.puzzle-icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    border: 2px solid rgba(255,255,255,.9);
    border-radius: 18px;
    background: rgba(255,255,255,.84);
    color: #4f3b80;
    font-size: 27px;
    font-weight: 900;
    line-height: 1;
    text-decoration: none;
    box-shadow: 0 8px 22px rgba(58, 45, 96, .13);
    backdrop-filter: blur(8px);
    pointer-events: auto;
}

.puzzle-icon-button:active { transform: scale(.94); }
.puzzle-icon-button:focus-visible { outline: 4px solid #27345c; outline-offset: 3px; }
.puzzle-icon-button--empty { visibility: hidden; }

.puzzle-panel {
    display: flex;
    flex-direction: column;
    width: min(100%, 940px);
    height: 100dvh;
    margin: 0 auto;
    padding: max(76px, calc(env(safe-area-inset-top) + 66px)) 16px max(12px, env(safe-area-inset-bottom));
}

.puzzle-heading {
    position: relative;
    z-index: 3;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 38px;
    margin-bottom: 8px;
    text-align: center;
}

.puzzle-kicker {
    margin: 0 0 2px;
    color: #ef017f;
    font-size: 10px;
    font-weight: 900;
    letter-spacing: .12em;
    text-transform: uppercase;
}

.puzzle-heading h1 {
    margin: 0;
    color: #493777;
    font-size: clamp(21px, 4vw, 29px);
    font-weight: 900;
    letter-spacing: -.045em;
    line-height: 1;
    text-shadow: 0 2px 0 rgba(255,255,255,.86);
}

.puzzle-moves {
    position: absolute;
    right: 0;
    display: inline-flex;
    align-items: center;
    min-height: 36px;
    padding: 6px 11px;
    border: 2px solid rgba(255,255,255,.88);
    border-radius: 14px;
    background: rgba(255,255,255,.72);
    color: #74688b;
    font-size: 11px;
    font-weight: 800;
    box-shadow: 0 8px 18px rgba(56,42,94,.09);
    backdrop-filter: blur(8px);
}

.puzzle-moves strong {
    margin-left: 5px;
    color: #5b40c9;
    font-size: 18px;
}

.puzzle-controls {
    position: relative;
    z-index: 4;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 7px;
    margin: 0 auto 10px;
    padding: 6px;
    border: 2px solid rgba(255,255,255,.82);
    border-radius: 18px;
    background: rgba(255,255,255,.62);
    box-shadow: 0 10px 25px rgba(53,39,91,.09);
    backdrop-filter: blur(10px);
}

.puzzle-controls label {
    padding-left: 7px;
    color: #6d627f;
    font-size: 11px;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: .05em;
}

.puzzle-controls select,
.puzzle-controls button {
    min-height: 36px;
    padding: 7px 11px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 900;
}

.puzzle-controls select {
    border: 1px solid #ded7f2;
    background: #fff;
    color: #493d66;
}

.puzzle-controls button {
    border: 1px solid #5b40c9;
    background: #5b40c9;
    color: #fff;
    cursor: pointer;
    box-shadow: 0 5px 12px rgba(91,64,201,.19);
}

.puzzle-controls button:active { transform: scale(.96); }
.puzzle-controls button:focus-visible,
.puzzle-controls select:focus-visible { outline: 3px solid #ef017f; outline-offset: 2px; }
.puzzle-controls button:disabled { opacity: .55; cursor: wait; }
.puzzle-controls .puzzle-button--quiet { border-color: #d9d0ee; background: #fff; color: #5b40c9; box-shadow: none; }

.puzzle-stage {
    position: relative;
    z-index: 2;
    display: grid;
    flex: 1 1 auto;
    min-height: 0;
    place-items: center;
}

.puzzle-board {
    --grid-size: 3;
    display: grid;
    grid-template-columns: repeat(var(--grid-size), 1fr);
    grid-template-rows: repeat(var(--grid-size), 1fr);
    gap: clamp(2px, .7vmin, 7px);
    width: min(100%, 65dvh, 650px);
    aspect-ratio: 1;
    padding: clamp(7px, 1.2vmin, 12px);
    border: 3px solid rgba(255,255,255,.9);
    border-radius: clamp(18px, 3vw, 30px);
    background: rgba(255,255,255,.64);
    box-shadow: 0 22px 50px rgba(57,42,98,.18), inset 0 0 0 1px rgba(93,69,158,.08);
    backdrop-filter: blur(9px);
}

.puzzle-piece {
    position: relative;
    overflow: hidden;
    min-width: 0;
    min-height: 0;
    padding: 0;
    border: 0;
    border-radius: clamp(5px, 1vw, 12px);
    background-repeat: no-repeat;
    cursor: pointer;
    box-shadow: 0 3px 9px rgba(34,24,68,.22);
    transition: transform .12s ease, box-shadow .12s ease;
}

.puzzle-piece:hover { transform: scale(1.018); box-shadow: 0 5px 13px rgba(34,24,68,.28); }
.puzzle-piece:focus-visible { outline: 4px solid #ef017f; outline-offset: -2px; }

.puzzle-piece::before {
    content: attr(data-number);
    position: absolute;
    z-index: 2;
    top: clamp(3px, .7vw, 7px);
    left: clamp(3px, .7vw, 7px);
    display: grid;
    place-items: center;
    min-width: clamp(18px, 3vw, 27px);
    height: clamp(18px, 3vw, 27px);
    padding: 0 4px;
    border: 2px solid rgba(255,255,255,.9);
    border-radius: 999px;
    background: linear-gradient(145deg, #55427d, #2d2348);
    color: #fff;
    font-size: clamp(10px, 1.5vw, 13px);
    font-weight: 900;
    line-height: 1;
    text-shadow: 0 1px 2px rgba(0,0,0,.3);
    box-shadow: 0 2px 7px rgba(20,13,43,.35);
    pointer-events: none;
}

.puzzle-piece.is-empty {
    background: linear-gradient(145deg, rgba(255,255,255,.5), rgba(205,196,229,.5)) !important;
    cursor: default;
    box-shadow: inset 0 2px 7px rgba(54,39,92,.12);
}

.puzzle-piece.is-empty::before { display: none; }
.puzzle-piece.is-empty:hover { transform: none; }
.puzzle-piece.is-celebrating { animation: puzzle-piece-pop .5s ease both; }

.puzzle-loading {
    position: absolute;
    z-index: 5;
    display: none;
    padding: 13px 18px;
    border-radius: 999px;
    background: rgba(55,39,91,.9);
    color: #fff;
    font-weight: 900;
    box-shadow: 0 10px 25px rgba(38,25,75,.24);
}

.puzzle-loading.is-visible { display: block; }

.puzzle-status {
    position: relative;
    z-index: 3;
    align-self: center;
    min-height: 35px;
    margin: 9px auto 0;
    padding: 8px 15px;
    border: 2px solid rgba(255,255,255,.82);
    border-radius: 999px;
    background: rgba(255,255,255,.65);
    color: #655879;
    text-align: center;
    font-size: 12px;
    font-weight: 800;
    box-shadow: 0 8px 20px rgba(48,34,85,.08);
    backdrop-filter: blur(8px);
}

.puzzle-status.is-success { background: rgba(232,255,242,.88); color: #128052; font-size: 14px; }
.puzzle-status.is-error { background: rgba(255,235,242,.88); color: #b4234c; }

.puzzle-confetti {
    position: fixed;
    inset: 0;
    z-index: 60;
    overflow: hidden;
    pointer-events: none;
}

.puzzle-confetti i {
    position: absolute;
    top: -25px;
    left: var(--confetti-left);
    width: var(--confetti-width);
    height: var(--confetti-height);
    border-radius: 2px;
    background: var(--confetti-color);
    opacity: 0;
    animation: puzzle-confetti-fall var(--confetti-duration) cubic-bezier(.18,.72,.3,1) var(--confetti-delay) forwards;
}

@keyframes puzzle-piece-pop { 50% { transform: scale(1.08) rotate(1deg); } }
@keyframes puzzle-confetti-fall {
    0% { opacity: 1; transform: translate3d(0, -5vh, 0) rotate(0); }
    100% { opacity: .95; transform: translate3d(var(--confetti-drift), 108vh, 0) rotate(var(--confetti-rotate)); }
}

@media (max-width: 640px) {
    .puzzle-panel { padding: max(73px, calc(env(safe-area-inset-top) + 63px)) 8px max(8px, env(safe-area-inset-bottom)); }
    .puzzle-heading { justify-content: flex-start; min-height: 34px; padding-left: 6px; text-align: left; }
    .puzzle-kicker { display: none; }
    .puzzle-heading h1 { font-size: 21px; }
    .puzzle-moves { right: 5px; min-height: 32px; padding: 4px 8px; }
    .puzzle-controls { gap: 5px; width: 100%; margin-bottom: 7px; padding: 5px; }
    .puzzle-controls label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
    .puzzle-controls select,
    .puzzle-controls button { flex: 1 1 auto; min-height: 34px; padding: 6px 7px; font-size: 11px; }
    .puzzle-board { width: min(100%, calc(100dvh - 210px)); min-width: 0; border-radius: 19px; }
    .puzzle-status { min-height: 31px; margin-top: 6px; padding: 6px 11px; font-size: 11px; }
}

@media (max-height: 620px) and (orientation: landscape) {
    .puzzle-panel { width: min(100%, 760px); padding-top: 64px; }
    .puzzle-heading { position: absolute; top: 13px; left: 76px; right: 76px; }
    .puzzle-controls { position: absolute; top: 58px; left: 8px; width: 190px; flex-direction: column; }
    .puzzle-controls label { position: static; width: auto; height: auto; clip: auto; }
    .puzzle-controls select,
    .puzzle-controls button { width: 100%; }
    .puzzle-stage { margin-left: 198px; }
    .puzzle-board { width: min(calc(100dvh - 82px), calc(100vw - 230px)); }
    .puzzle-status { position: absolute; right: 10px; bottom: 8px; }
}

@media (prefers-reduced-motion: reduce) {
    .puzzle-piece { transition: none; }
    .puzzle-piece.is-celebrating { animation: none; }
    .puzzle-confetti { display: none; }
}
