:root {
    --green-bg: #a6cc63;
    --green-dark: #0f5042;
    --green-mid: #12b18e;
    --green-empty: #44916a;
    --green-filled: #0f5042;
    --orange-correct: #ffaa00;
    --orange-text: #241a00;
    --purple-inventory: #763678;
    --cell-gap: clamp(2px, 0.5vmin, 4px);
    --board-pad: clamp(6px, 1.5vmin, 12px);
    --board-border: clamp(2px, 0.6vmin, 4px);
    --shell-pad: clamp(4px, 1vmin, 10px);
    --board-column-gap: clamp(4px, 1vmin, 8px);
    --level-row: clamp(26px, 5.5vmin, 36px);
    --completion-row: clamp(26px, 5.5vmin, 36px);
    --difficulty-hint-row: clamp(20px, 4.5vmin, 28px);
}

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

body,
html {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    background-color: var(--green-bg);
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    overflow: hidden;
    touch-action: none;
    font-family: "PingFang SC", "Microsoft YaHei", monospace, sans-serif;
    color: var(--green-dark);
}

body {
    display: flex;
    align-items: stretch;
    justify-content: center;
}

.game-shell {
    position: relative;
    container-type: size;
    width: 100%;
    height: 100%;
    padding: var(--shell-pad);
    --board-side: min(
        calc(100cqw - 2 * var(--shell-pad)),
        calc(
            100cqh - var(--level-row) - var(--completion-row) - 2 * var(--board-column-gap) - 2 * var(--shell-pad)
        )
    );
    --cell-size: calc(
        (var(--board-side) - 2 * var(--board-border) - 2 * var(--board-pad) - 4 * var(--cell-gap)) / 5
    );
    --cell-font: calc(var(--cell-size) * 0.44);
}

.play-layout {
    width: 100%;
    height: 100%;
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
}

.side-rail {
    flex: 1 1 0;
    min-width: 0;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.side-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: clamp(6px, 1.5vmin, 10px);
    width: auto;
    max-width: 100%;
}

.inventory-group {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: var(--cell-gap);
}

.board-column {
    flex: 0 0 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--board-column-gap);
}

.level-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    min-height: var(--level-row);
    height: var(--level-row);
    padding: 0 clamp(10px, 2.5vmin, 16px);
    border-radius: 8px;
    background: var(--green-dark);
    color: #fff;
    font-size: clamp(0.6875rem, 2.2vmin, 0.875rem);
    font-weight: 800;
    line-height: 1.2;
    text-align: center;
    white-space: nowrap;
}

.progress-pill {
    display: block;
    width: auto;
    min-width: calc(2 * var(--cell-size) + var(--cell-gap));
    max-width: 100%;
    padding: clamp(4px, 1vmin, 6px) clamp(6px, 1.5vmin, 10px);
    border: none;
    border-radius: 8px;
    background: rgba(15, 80, 66, 0.12);
    color: var(--green-dark);
    font-size: clamp(0.5625rem, 1.6vmin, 0.6875rem);
    font-weight: 800;
    line-height: 1.25;
    text-align: center;
    word-break: break-word;
}

.difficulty-hint {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-sizing: border-box;
    width: auto;
    min-width: calc(2 * var(--cell-size) + var(--cell-gap));
    max-width: 100%;
    min-height: var(--difficulty-hint-row);
    padding: 0 clamp(6px, 1.5vmin, 10px);
    font-size: clamp(0.6875rem, 2.2vmin, 0.8125rem);
    font-weight: 800;
    line-height: 1.25;
    text-align: center;
    color: var(--green-dark);
    white-space: nowrap;
    visibility: hidden;
}

.difficulty-hint.is-visible {
    visibility: visible;
}

.completion-pill {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-sizing: border-box;
    width: auto;
    max-width: calc(100vw - 2 * var(--shell-pad));
    min-height: var(--completion-row);
    padding: clamp(4px, 1vmin, 6px) clamp(8px, 2vmin, 12px);
    border: none;
    background: transparent;
    color: var(--green-dark);
    font-size: clamp(0.5625rem, 1.4vmin, 0.6875rem);
    font-weight: 700;
    line-height: 1.3;
    text-align: center;
    word-break: break-word;
    visibility: hidden;
}

.completion-pill.is-visible {
    visibility: visible;
}

.btn-tool {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border: 2px solid var(--green-mid);
    border-radius: 8px;
    font-family: inherit;
    font-weight: 800;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.btn-tool--side {
    flex-direction: column;
    width: auto;
    min-width: calc(2 * var(--cell-size) + var(--cell-gap));
    max-width: 100%;
    padding: clamp(6px, 1.5vmin, 10px) clamp(8px, 2vmin, 12px);
    font-size: clamp(0.5625rem, 1.6vmin, 0.75rem);
    line-height: 1.2;
}

.btn-tool--side svg {
    width: clamp(14px, 3.5vmin, 16px);
    height: clamp(14px, 3.5vmin, 16px);
    flex-shrink: 0;
}

.btn-tool:active:not(:disabled) {
    transform: translateY(1px);
    box-shadow: none;
}

.btn-tool:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-tool--primary {
    background: var(--green-mid);
    color: #fff;
    border-color: var(--green-mid);
    box-shadow: none;
}

.btn-tool--danger {
    border: none;
    background: rgba(15, 80, 66, 0.12);
    color: var(--green-dark);
}

.btn-tool--danger.btn-tool--side {
    flex-direction: row;
    box-shadow: none;
}

.btn-tool--help {
    position: absolute;
    right: var(--shell-pad);
    bottom: var(--shell-pad);
    z-index: 5;
    flex-direction: row;
    min-width: auto;
    padding: clamp(6px, 1.5vmin, 10px) clamp(10px, 2vmin, 14px);
    font-size: clamp(0.5625rem, 1.6vmin, 0.75rem);
}

.help-modal {
    position: fixed;
    inset: 0;
    z-index: 30;
    display: none;
    align-items: center;
    justify-content: center;
    padding: var(--shell-pad);
}

.help-modal.is-open {
    display: flex;
}

.help-modal__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 80, 66, 0.45);
}

.help-modal__panel {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: clamp(8px, 2vmin, 12px);
    width: min(360px, calc(100vw - 2 * var(--shell-pad)));
    max-height: min(80vh, 480px);
    padding: clamp(12px, 3vmin, 20px);
    border-radius: 12px;
    background: var(--green-bg);
    border: 2px solid var(--green-mid);
    box-shadow: 0 8px 24px rgba(15, 80, 66, 0.2);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.help-modal__title {
    margin: 0;
    font-size: clamp(0.875rem, 2.8vmin, 1rem);
    font-weight: 800;
    color: var(--green-dark);
    text-align: center;
}

.help-modal__body {
    display: flex;
    flex-direction: column;
    gap: clamp(10px, 2.5vmin, 14px);
    font-size: clamp(0.6875rem, 2vmin, 0.8125rem);
    line-height: 1.5;
    color: var(--green-dark);
    user-select: text;
    -webkit-user-select: text;
}

.help-modal__section {
    display: flex;
    flex-direction: column;
    gap: clamp(4px, 1vmin, 6px);
}

.help-modal__section-title {
    margin: 0;
    font-size: clamp(0.75rem, 2.2vmin, 0.875rem);
    font-weight: 800;
    color: var(--green-dark);
}

.help-modal__body p {
    margin: 0;
}

.help-modal__close {
    align-self: center;
    min-width: 96px;
    padding: clamp(6px, 1.5vmin, 10px) clamp(16px, 4vmin, 24px);
    font-size: clamp(0.6875rem, 2vmin, 0.8125rem);
}

.victory-overlay {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: none;
    align-items: center;
    justify-content: center;
    padding: var(--shell-pad);
    background: rgba(166, 204, 99, 0.35);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    pointer-events: auto;
}

.victory-overlay.is-visible {
    display: flex;
}

.victory-overlay__content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: clamp(8px, 2vmin, 12px);
    text-align: center;
}

.victory-overlay__line {
    margin: 0;
    font-size: clamp(1rem, 4vmin, 1.375rem);
    font-weight: 800;
    line-height: 1.4;
    color: var(--green-dark);
}

.board-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
}

.game-board {
    display: grid;
    grid-template-columns: repeat(5, var(--cell-size));
    grid-template-rows: repeat(5, var(--cell-size));
    gap: var(--cell-gap);
    width: calc(5 * var(--cell-size) + 4 * var(--cell-gap) + 2 * var(--board-pad) + 2 * var(--board-border));
    height: calc(5 * var(--cell-size) + 4 * var(--cell-gap) + 2 * var(--board-pad) + 2 * var(--board-border));
    max-width: var(--board-side);
    max-height: var(--board-side);
    padding: var(--board-pad);
    border: none;
    border-radius: clamp(10px, 2vmin, 16px);
    background: var(--green-bg);
}

.container__grid-row {
    display: contents;
}

.box {
    min-width: 0;
    min-height: 0;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: var(--cell-font);
    font-weight: 800;
    line-height: 1;
}

.grid {
    border: none;
    border-radius: clamp(3px, 0.8vmin, 6px);
    background-color: var(--green-mid);
    box-shadow: none;
    touch-action: none;
}

.inventory {
    position: relative;
    width: var(--cell-size);
    height: var(--cell-size);
    border: none;
    border-radius: clamp(3px, 0.8vmin, 6px);
    background: var(--purple-inventory);
    flex-shrink: 0;
    touch-action: none;
    cursor: grab;
    box-shadow: none;
    font-size: var(--cell-font);
}

.inventory:active {
    cursor: grabbing;
}

.inventory__digit {
    font-size: inherit;
    font-weight: 800;
}

.inventory__amount {
    position: absolute;
    right: 2px;
    bottom: 1px;
    font-size: calc(var(--cell-font) * 0.42);
    font-weight: 700;
    line-height: 1;
    opacity: 0.85;
}

.text__follow {
    position: fixed;
    font-size: var(--cell-font);
    font-weight: 800;
    color: #fff;
    z-index: 9;
    visibility: hidden;
    pointer-events: none;
}

@supports not (width: 1cqw) {
    .game-shell {
        container-type: normal;
        --board-side: min(
            calc(100vw - 2 * var(--shell-pad)),
            calc(
                100vh - var(--level-row) - var(--completion-row) - 2 * var(--board-column-gap) - 2 * var(--shell-pad)
            )
        );
        --cell-size: calc(
            (var(--board-side) - 2 * var(--board-border) - 2 * var(--board-pad) - 4 * var(--cell-gap)) / 5
        );
        --cell-font: calc(var(--cell-size) * 0.44);
    }
}

@media (max-width: 480px) {
    .game-shell {
        --board-column-gap: 2px;
        --level-row: 18px;
        --completion-row: 18px;
        --difficulty-hint-row: 16px;
        --shell-pad: 4px;
    }

    .level-badge {
        padding: 0 6px;
        font-size: 0.625rem;
        border-radius: 6px;
    }

    .completion-pill {
        padding: 2px 6px;
        font-size: 0.5625rem;
        line-height: 1.2;
    }

    .difficulty-hint {
        font-size: 0.625rem;
    }

    .btn-tool--help {
        padding: 5px 8px;
        font-size: 0.5625rem;
    }
}

@media (max-width: 360px) {
    .btn-tool--side {
        font-size: 0.5625rem;
        padding: 5px 6px;
    }
}
