:root {
    color-scheme: dark;
    --bg: #100b23;
    --panel: rgba(255, 255, 255, 0.1);
    --panel-strong: rgba(255, 255, 255, 0.16);
    --line: rgba(255, 255, 255, 0.18);
    --text: #fff6ff;
    --muted: #d7cbe8;
    --rose: #ff79b0;
    --violet: #a77cff;
    --teal: #8ce7ff;
    --gold: #ffd166;
    --shadow: 0 22px 70px rgba(2, 1, 16, 0.42);
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    background: var(--bg);
}

body {
    height: 100svh;
    margin: 0;
    color: var(--text);
    font-family: "Manrope", system-ui, sans-serif;
    background:
        linear-gradient(180deg, rgba(16, 11, 35, 0.1), rgba(16, 11, 35, 0.92)),
        url("../assets/backgrounds/enchanted-night.svg") center / cover fixed;
    overflow: hidden;
    touch-action: manipulation;
}

button {
    font: inherit;
    color: inherit;
    border: 0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.app-shell {
    width: min(100%, 520px);
    height: 100svh;
    min-height: 0;
    margin: 0 auto;
    padding: max(10px, env(safe-area-inset-top)) 14px calc(72px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.top-panel,
.level-strip,
.board-wrap,
.task-card,
.surprise-card,
.shop-item,
.stat-card,
.photo-card,
.generator,
.modal-panel {
    border: 1px solid var(--line);
    background: linear-gradient(145deg, rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0.07));
    box-shadow: var(--shadow);
    backdrop-filter: blur(18px);
    border-radius: 8px;
}

.top-panel {
    display: grid;
    grid-template-columns: 1fr auto 36px;
    align-items: center;
    gap: 10px;
    padding: 8px;
}

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

.avatar {
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    flex: 0 0 auto;
    border-radius: 8px;
    color: #2b1230;
    background: linear-gradient(135deg, var(--rose), var(--gold));
    box-shadow: 0 0 28px rgba(255, 121, 176, 0.38);
}

.profile strong,
.profile span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.profile span {
    margin-top: 2px;
    color: var(--muted);
    font-size: 12px;
}

.top-stats {
    display: flex;
    gap: 8px;
}

.top-stats div {
    min-width: 66px;
    padding: 6px 8px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
}

.top-stats span,
.level-strip span,
.level-strip strong {
    display: block;
    color: var(--muted);
    font-size: 11px;
}

.top-stats strong {
    display: block;
    margin-top: 1px;
    font-size: 18px;
}

.icon-button {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.11);
    font-size: 24px;
}

.level-strip {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 10px;
    margin: 8px 0 8px;
    padding: 8px 10px;
}

.xp-track,
.progress-line {
    height: 8px;
    overflow: hidden;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.13);
}

.xp-track i,
.progress-line span {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, var(--rose), var(--gold), var(--teal));
    transition: width 0.35s ease;
}

.screen {
    display: none;
    min-height: 0;
    overflow: auto;
    animation: rise 0.28s ease both;
    scrollbar-width: none;
}

.screen.active {
    display: block;
}

.screen.active:not([data-screen="play"]) {
    flex: 1 1 auto;
    overflow: auto;
    padding-bottom: 12px;
}

.screen::-webkit-scrollbar {
    display: none;
}

.screen[data-screen="play"].active {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    overflow: hidden;
}

.play-layout {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: 8px;
    height: 100%;
    min-height: 0;
}

.board-wrap {
    position: relative;
    min-height: 0;
    padding: 6px;
    display: grid;
    align-items: start;
    justify-items: center;
    overflow: hidden;
    box-shadow: var(--shadow), inset 0 -26px 40px rgba(12, 7, 28, 0.18);
}

.board-help {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 3;
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    color: var(--text);
    background: rgba(255, 255, 255, 0.12);
    border: 1px solid var(--line);
    font-weight: 900;
}

.board {
    display: grid;
    grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
    grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
    gap: 5px;
    width: min(100%, 420px);
    height: 490px;
    min-height: 0;
    margin: 0 auto auto;
    aspect-ratio: 6 / 7;
    align-content: stretch;
    overflow: hidden;
}

.cell {
    position: relative;
    display: grid;
    place-items: center;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    border-radius: 8px;
    background:
        linear-gradient(145deg, rgba(255, 255, 255, 0.11), rgba(255, 255, 255, 0.04)),
        radial-gradient(circle at 30% 20%, rgba(255, 121, 176, 0.16), transparent 52%);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.cell.pulse {
    animation: glow 0.62s ease;
}

.tile {
    position: relative;
    display: grid;
    place-items: center;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    padding: 2px;
    border-radius: 8px;
    background:
        radial-gradient(circle at 50% 28%, rgba(255, 231, 250, 0.24), transparent 48%),
        linear-gradient(145deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.06));
    box-shadow: inset 0 1px rgba(255, 255, 255, 0.28), 0 10px 24px rgba(0, 0, 0, 0.22);
    user-select: none;
    touch-action: none;
    transition: transform 0.18s ease, filter 0.18s ease;
}

.tile::after {
    content: "";
    position: absolute;
    right: 5px;
    bottom: 5px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--rank, #fff);
    box-shadow: 0 0 9px var(--rank, #fff);
}

.tile:active {
    transform: scale(0.96);
}

.tile img {
    width: 96%;
    height: 96%;
    min-width: 0;
    min-height: 0;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    pointer-events: none;
    filter: saturate(1.1) contrast(1.04);
}

.tile span {
    display: none;
}

.rarity-редкое,
.rarity-легендарное {
    box-shadow: 0 0 26px rgba(255, 209, 102, 0.26), inset 0 1px rgba(255, 255, 255, 0.28);
}

.rarity-обычное {
    --rank: #9fe6ff;
}

.rarity-нежное {
    --rank: #ff95ca;
}

.rarity-редкое {
    --rank: #ffd166;
}

.rarity-легендарное {
    --rank: #d8b4ff;
}

.drag-source {
    opacity: 0.25;
}

.drag-ghost {
    position: fixed;
    z-index: 100;
    width: 72px;
    height: 72px;
    pointer-events: none;
    transform: translate(-50%, -50%) scale(1.08);
}

.generators {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 7px;
    min-height: 72px;
}

.generator {
    display: grid;
    justify-items: center;
    gap: 4px;
    min-width: 0;
    padding: 6px 4px;
}

.generator img {
    width: 42px;
    height: 42px;
    object-fit: contain;
}

.generator span {
    width: 100%;
    overflow: hidden;
    font-size: 10px;
    font-weight: 800;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.generator small {
    color: var(--muted);
    font-size: 9px;
}

.generator.cooling {
    filter: saturate(1.45) brightness(1.16);
    animation: smallPulse 0.65s ease;
}

.section-title {
    margin: 10px 2px 14px;
}

.section-title h1 {
    margin: 0;
    font-family: "Playfair Display", serif;
    font-size: 30px;
    line-height: 1;
}

.section-title p {
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 14px;
}

.task-card,
.surprise-card {
    margin-bottom: 12px;
    padding: 14px;
}

.task-head {
    display: flex;
    gap: 12px;
    align-items: center;
}

.task-head img {
    width: 58px;
    height: 58px;
    flex: 0 0 auto;
}

.task-head h3 {
    margin: 0 0 4px;
    font-size: 17px;
}

.task-head p,
.expedition p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.reward-row {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    margin-top: 10px;
    color: var(--gold);
    font-size: 12px;
    font-weight: 800;
}

.progress-line {
    margin-top: 12px;
}

.stats-grid,
.shop-grid,
.gallery {
    display: grid;
    gap: 10px;
}

.stats-grid {
    grid-template-columns: repeat(2, 1fr);
}

.stat-card {
    padding: 12px;
}

.stat-card strong {
    display: block;
    font-size: 22px;
}

.stat-card span {
    color: var(--muted);
    font-size: 12px;
}

.soft-button,
.primary-button {
    width: 100%;
    min-height: 48px;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--rose), var(--violet));
    box-shadow: 0 16px 32px rgba(255, 121, 176, 0.22);
    font-weight: 900;
}

.soft-button {
    background: linear-gradient(135deg, rgba(255, 121, 176, 0.34), rgba(140, 231, 255, 0.22));
}

.soft-button:disabled {
    cursor: default;
    opacity: 0.62;
}

.danger {
    margin-top: 10px;
    background: rgba(255, 103, 127, 0.24);
}

.surprise-card {
    display: grid;
    gap: 10px;
    text-align: center;
}

.surprise-card img,
.love-modal img {
    width: 100%;
    max-height: 280px;
    object-fit: cover;
    border-radius: 8px;
}

.surprise-card p,
.love-modal p {
    margin: 0;
    color: #fffaff;
    font-size: 16px;
    font-weight: 700;
}

.primary-button {
    margin-bottom: 12px;
}

.upload-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 12px;
}

.upload-row .soft-button {
    min-height: 44px;
    font-size: 12px;
}

.hidden-input {
    display: none;
}

.gallery {
    grid-template-columns: repeat(2, 1fr);
}

.library-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 7px;
    max-height: 230px;
    overflow: auto;
    margin-top: 12px;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.07);
}

.library-grid img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: contain;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.08);
}

.library-grid img.locked-library {
    filter: brightness(0.2) saturate(0.35);
    opacity: 0.62;
}

.mini-options {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

.mini-pick {
    display: grid;
    gap: 6px;
    place-items: center;
    padding: 10px 6px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--line);
}

.mini-pick img {
    width: 74px;
    height: 74px;
    object-fit: contain;
}

.mini-pick span {
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
}

.photo-card {
    min-height: 170px;
    overflow: hidden;
    padding: 0;
    text-align: left;
}

.photo-card img {
    display: block;
    width: 100%;
    height: 116px;
    object-fit: cover;
    background: rgba(255, 255, 255, 0.08);
}

.photo-card video,
.love-modal video,
.surprise-card video {
    display: block;
    width: 100%;
    max-height: 280px;
    object-fit: cover;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.08);
}

.photo-card span {
    display: block;
    padding: 9px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    line-height: 1.25;
}

.photo-card:not(.opened) img,
.photo-card:not(.opened) video {
    filter: blur(7px) saturate(0.65) brightness(0.72);
}

.photo-card.missing img {
    display: none;
}

.empty-note {
    grid-column: 1 / -1;
    color: var(--muted);
    padding: 16px;
}

.shop-grid {
    grid-template-columns: 1fr;
}

.shop-item {
    display: grid;
    grid-template-columns: 58px 1fr auto;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px;
    text-align: left;
}

.shop-item img {
    width: 58px;
    height: 58px;
}

.shop-item strong {
    font-size: 15px;
}

.shop-item span {
    color: var(--gold);
    font-size: 12px;
    font-weight: 900;
}

.bottom-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 20;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    width: min(100%, 520px);
    margin: 0 auto;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: rgba(16, 11, 35, 0.82);
    backdrop-filter: blur(18px);
}

.nav-button {
    display: grid;
    gap: 3px;
    place-items: center;
    min-width: 0;
    min-height: 50px;
    border-radius: 8px;
    color: var(--muted);
    background: transparent;
    font-size: 10px;
    font-weight: 800;
}

.nav-button span {
    font-size: 17px;
}

.nav-button.active {
    color: var(--text);
    background: linear-gradient(145deg, rgba(255, 121, 176, 0.28), rgba(140, 231, 255, 0.16));
}

.toast {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    right: max(10px, calc((100vw - 520px) / 2 + 10px));
    left: auto;
    z-index: 60;
    width: min(290px, calc(100% - 20px));
    margin: 0;
    padding: 9px 11px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--text);
    background: rgba(25, 17, 53, 0.94);
    box-shadow: var(--shadow);
    opacity: 0;
    transform: translateY(-12px);
    pointer-events: none;
    transition: 0.22s ease;
    font-size: 12px;
    font-weight: 800;
}

.toast.active {
    opacity: 1;
    transform: translateY(0);
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 70;
    display: none;
    place-items: center;
    padding: 18px;
    background: rgba(10, 6, 23, 0.68);
}

.modal.active {
    display: grid;
}

.modal-panel {
    position: relative;
    width: min(100%, 430px);
    max-height: min(760px, 88vh);
    overflow: auto;
    padding: 18px;
}

.modal-close {
    position: absolute;
    top: 8px;
    right: 8px;
}

.love-modal {
    display: grid;
    gap: 12px;
    padding-top: 28px;
    text-align: center;
}

.love-modal h2 {
    margin: 0;
    font-family: "Playfair Display", serif;
    font-size: 30px;
}

.particle {
    position: fixed;
    z-index: 90;
    color: var(--rose);
    font-size: 18px;
    pointer-events: none;
    animation: particle 0.9s ease-out forwards;
}

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

@keyframes glow {
    0% {
        box-shadow: 0 0 0 rgba(255, 209, 102, 0);
    }
    45% {
        box-shadow: 0 0 28px rgba(255, 209, 102, 0.7), inset 0 0 18px rgba(255, 255, 255, 0.24);
    }
    100% {
        box-shadow: 0 0 0 rgba(255, 209, 102, 0);
    }
}

@keyframes smallPulse {
    0%,
    100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.03);
    }
}

@keyframes particle {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0.4);
    }
    15% {
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: translate(calc(-50% + var(--x)), calc(-50% + var(--y))) scale(1.2);
    }
}

@media (max-width: 380px) {
    .app-shell {
        padding-right: 9px;
        padding-left: 9px;
    }

    .board {
        gap: 4px;
    }

    .top-panel {
        grid-template-columns: 1fr auto;
    }

    .top-stats {
        grid-column: 1 / -1;
        width: 100%;
    }

    .top-stats div {
        flex: 1;
    }

    #closeApp {
        grid-row: 1;
        grid-column: 2;
    }
}

@media (min-width: 720px) {
    .app-shell {
        padding-top: 22px;
    }
}
