/* The overlay is positioned like the other games': fixed over the page, dark in both themes through
   game-dark-surface, with the answer screen (game-answer-screen.css) covering it when a gate is open. */
.mahjong-overlay {
    position: fixed;
    inset: 0;
    z-index: 1050;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary, #0f172a);
    color: var(--text-primary);
    padding-bottom: env(safe-area-inset-bottom);
}

html:has(.mahjong-overlay) {
    overscroll-behavior: none;
}

.mahjong-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
    padding-top: calc(0.75rem + env(safe-area-inset-top));
    font-variant-numeric: tabular-nums;
}

.mahjong-score {
    font-size: 1.35rem;
    font-weight: 800;
    color: #fde047;
    transform-origin: left center;
    display: inline-block;
    animation: mahjong-score-bump 420ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes mahjong-score-bump {
    0%   { transform: scale(1); }
    35%  { transform: scale(1.2); text-shadow: 0 0 14px rgba(253, 224, 71, 0.7); }
    100% { transform: scale(1); }
}

.mahjong-moves {
    margin-left: auto;
    opacity: 0.9;
}

.mahjong-close-button {
    background: none;
    border: none;
    color: inherit;
    min-width: 2.75rem;
    min-height: 2.75rem;
}

.mahjong-close-button:disabled {
    opacity: 0.5;
    cursor: default;
}

.mahjong-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    padding: 0 1rem 0.5rem;
    font-variant-numeric: tabular-nums;
}

.mahjong-status {
    display: flex;
    gap: 1rem;
    opacity: 0.9;
}

.mahjong-combo {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.mahjong-combo-meter {
    display: flex;
    gap: 3px;
}

/* An empty step is drawn as a ring, since a fill close to the backdrop does not show how far is left. */
.mahjong-combo-step {
    width: 1.1rem;
    height: 0.55rem;
    border-radius: 999px;
    background: transparent;
    box-shadow: inset 0 0 0 2px #94a3b8;
    transition: background-color 200ms ease-out;
}

.mahjong-combo-step.is-filled {
    background: #2dd4bf;
    box-shadow: none;
}

/* The meter has just paid out a free help: it flashes once before it starts again. */
.mahjong-combo.is-full .mahjong-combo-meter {
    animation: mahjong-combo-flash 700ms ease-out;
}

.mahjong-free-helps {
    padding: 0.1rem 0.6rem;
    border-radius: 999px;
    background: rgba(45, 212, 191, 0.18);
    color: #5eead4;
    font-weight: 700;
}

@keyframes mahjong-combo-flash {
    0%   { transform: scale(1); filter: drop-shadow(0 0 0 rgba(45, 212, 191, 0)); }
    40%  { transform: scale(1.25); filter: drop-shadow(0 0 10px rgba(45, 212, 191, 0.9)); }
    100% { transform: scale(1); filter: drop-shadow(0 0 0 rgba(45, 212, 191, 0)); }
}

.mahjong-board-frame {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.75rem 1rem 1rem;
    overflow: auto;
}

/* Sized by whichever runs out first, the width or the height the phone has. A tile is three by four, so a
   half-tile unit is 1.5 by 2 and the board's ratio follows from its spans. Room is left top and left for the
   layers, which lift up and to the left. */
.mahjong-board {
    position: relative;
    width: min(
        100%,
        calc(var(--mahjong-columns) * 1.8rem),
        calc((100dvh - 15rem) * var(--mahjong-columns) * 3 / (var(--mahjong-rows) * 4)));
    aspect-ratio: calc(var(--mahjong-columns) * 3) / calc(var(--mahjong-rows) * 4);
    margin-top: 1rem;
    margin-left: 1rem;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    touch-action: manipulation;
}

/* Each layer lifts the tile up and to the left and throws a longer shadow, which is what reads as height. */
.mahjong-tile {
    --mahjong-lift-x: calc(var(--mahjong-layer, 0) * -0.28rem);
    --mahjong-lift-y: calc(var(--mahjong-layer, 0) * -0.34rem);
    position: absolute;
    width: calc(200% / var(--mahjong-columns));
    height: calc(200% / var(--mahjong-rows));
    /* app.css gives every button 44 px at least on a phone, which would push a tile over its neighbour. */
    min-width: 0;
    min-height: 0;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #a8a29e;
    border-radius: 0.35rem;
    background: linear-gradient(145deg, #fffdf6 0%, #f3ead3 100%);
    box-shadow:
        0.14rem 0.18rem 0 #a8936a,
        calc(0.25rem + var(--mahjong-layer, 0) * 0.08rem) calc(0.35rem + var(--mahjong-layer, 0) * 0.1rem) 0.5rem rgba(0, 0, 0, 0.45);
    transform: translate(var(--mahjong-lift-x), var(--mahjong-lift-y));
    transition: transform 140ms ease-out, filter 160ms ease-out;
    cursor: pointer;
}

.mahjong-symbol {
    width: 78%;
    height: 78%;
}

/* A locked tile is shown, so the player can plan, but toned down so what can be taken stands out. */
.mahjong-tile.is-locked {
    filter: brightness(0.68) saturate(0.75);
    cursor: default;
}

.mahjong-tile:disabled:not(.is-locked) {
    cursor: default;
}

/* A picked tile is marked by its border and lift, so the outline stays the keyboard's own. */
.mahjong-tile.is-selected {
    background: linear-gradient(145deg, #fef9c3 0%, #fde68a 100%);
    border: 3px solid #a16207;
    transform: translate(var(--mahjong-lift-x), calc(var(--mahjong-lift-y) - 0.3rem)) scale(1.06);
}

.mahjong-tile.is-hinted {
    animation: mahjong-hint 1.1s ease-in-out infinite;
}

.mahjong-tile.is-shaking {
    animation: mahjong-shake 420ms ease-in-out;
}

.mahjong-tile.is-shaking-again {
    animation: mahjong-shake-again 420ms ease-in-out;
}

.mahjong-tile.is-vanishing {
    z-index: 50;
    pointer-events: none;
    animation: mahjong-vanish 320ms ease-in forwards;
}

@keyframes mahjong-hint {
    0%, 100% { box-shadow: 0.14rem 0.18rem 0 #a8936a, 0 0 0 0 rgba(45, 212, 191, 0); }
    50%      { box-shadow: 0.14rem 0.18rem 0 #a8936a, 0 0 0.9rem 0.3rem rgba(45, 212, 191, 0.95); }
}

@keyframes mahjong-shake {
    0%, 100% { transform: translate(var(--mahjong-lift-x), var(--mahjong-lift-y)); }
    20%      { transform: translate(calc(var(--mahjong-lift-x) - 5px), var(--mahjong-lift-y)) rotate(-3deg); }
    40%      { transform: translate(calc(var(--mahjong-lift-x) + 5px), var(--mahjong-lift-y)) rotate(3deg); }
    60%      { transform: translate(calc(var(--mahjong-lift-x) - 4px), var(--mahjong-lift-y)) rotate(-2deg); }
    80%      { transform: translate(calc(var(--mahjong-lift-x) + 3px), var(--mahjong-lift-y)) rotate(1deg); }
}

@keyframes mahjong-shake-again {
    0%, 100% { transform: translate(var(--mahjong-lift-x), var(--mahjong-lift-y)); }
    20%      { transform: translate(calc(var(--mahjong-lift-x) - 5px), var(--mahjong-lift-y)) rotate(-3deg); }
    40%      { transform: translate(calc(var(--mahjong-lift-x) + 5px), var(--mahjong-lift-y)) rotate(3deg); }
    60%      { transform: translate(calc(var(--mahjong-lift-x) - 4px), var(--mahjong-lift-y)) rotate(-2deg); }
    80%      { transform: translate(calc(var(--mahjong-lift-x) + 3px), var(--mahjong-lift-y)) rotate(1deg); }
}

@keyframes mahjong-vanish {
    0%   { opacity: 1; transform: translate(var(--mahjong-lift-x), var(--mahjong-lift-y)) scale(1); }
    40%  { opacity: 1; transform: translate(var(--mahjong-lift-x), calc(var(--mahjong-lift-y) - 0.4rem)) scale(1.12); filter: brightness(1.3); }
    100% { opacity: 0; transform: translate(var(--mahjong-lift-x), calc(var(--mahjong-lift-y) - 1rem)) scale(0.6); }
}

.mahjong-helps {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    padding: 0 1rem 1rem;
}

.mahjong-help-button {
    min-height: 2.75rem;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.mahjong-help-cost {
    min-width: 1.4rem;
    padding: 0 0.4rem;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.15);
    font-size: 0.85rem;
    font-weight: 700;
}

/* Over the dimmed preview board, as three buttons in a column. */
.mahjong-layouts {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 1rem;
    /* The panel covers the header too, and the close button must stay reachable through it. */
    pointer-events: none;
}

.mahjong-layout-button {
    pointer-events: auto;
    width: min(100%, 18rem);
    padding: 0.9rem 1rem;
    border-radius: 0.75rem;
    background: #1e293b;
    border: 1px solid #94a3b8;
    color: var(--text-primary);
    font-size: 1.15rem;
    font-weight: 800;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}

.mahjong-layout-button:hover,
.mahjong-layout-button:focus-visible {
    background: #334155;
    color: var(--text-primary);
}

.mahjong-layout-size {
    font-size: 0.9rem;
    font-weight: 600;
    opacity: 0.85;
}

.mahjong-toolbar[inert],
.mahjong-board-frame[inert],
.mahjong-helps[inert] {
    opacity: 0.45;
    filter: blur(1.5px);
}

.mahjong-panel {
    padding: 0 1rem 1rem;
    text-align: center;
}

.mahjong-error {
    color: var(--color-danger-text, #fca5a5);
}

.mahjong-result {
    position: absolute;
    inset: 0;
    z-index: 60;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: rgba(15, 23, 42, 0.82);
    backdrop-filter: blur(2px);
    animation: mahjong-fade-in 260ms ease-out;
}

.mahjong-result-card {
    text-align: center;
    max-width: 22rem;
    width: 100%;
}

.mahjong-result-title {
    margin: 0 0 0.25rem;
    font-size: 1.25rem;
    font-weight: 800;
}

.mahjong-stars {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin: 0.5rem 0 1rem;
    font-size: 2.5rem;
    color: #64748b;
}

.mahjong-stars .is-earned {
    color: #fde047;
    animation: mahjong-star-pop 480ms cubic-bezier(0.34, 1.56, 0.64, 1) both;
    animation-delay: calc(var(--star-order) * 180ms);
}

.mahjong-result-record {
    display: inline-block;
    margin: 0 0 0.5rem;
    padding: 0.2rem 0.8rem;
    border-radius: 999px;
    background: rgba(253, 224, 71, 0.18);
    color: #fde047;
    font-weight: 800;
}

.mahjong-result-score {
    margin: 0;
    font-size: 5rem;
    line-height: 1;
    font-weight: 900;
    color: #fde047;
    font-variant-numeric: tabular-nums;
}

.mahjong-result-label {
    margin: 0.25rem 0 1.75rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    opacity: 0.8;
}

.mahjong-result-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.mahjong-result-actions .btn {
    padding: 0.9rem 1rem;
    font-size: 1.05rem;
    font-weight: 700;
    border-radius: 0.75rem;
}

@keyframes mahjong-star-pop {
    0%   { transform: scale(0) rotate(-30deg); opacity: 0; }
    100% { transform: scale(1) rotate(0); opacity: 1; }
}

@keyframes mahjong-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* After every state rule, so a picked or hinted tile still shows where the keyboard is. */
.mahjong-board .mahjong-tile:focus-visible {
    outline: 3px solid var(--focus-ring, #93c5fd);
    outline-offset: 3px;
}

.mahjong-gate-error {
    padding: 0 1rem;
    text-align: center;
}

@media (prefers-reduced-motion: reduce) {
    .mahjong-score,
    .mahjong-tile,
    .mahjong-tile.is-hinted,
    .mahjong-tile.is-shaking,
    .mahjong-tile.is-shaking-again,
    .mahjong-combo.is-full .mahjong-combo-meter,
    .mahjong-stars .is-earned,
    .mahjong-result {
        animation: none;
        transition: none;
    }

    /* Without the pulse the hint still has to show, so it is drawn as a steady ring. */
    .mahjong-tile.is-hinted {
        box-shadow: 0.14rem 0.18rem 0 #a8936a, 0 0 0 3px #2dd4bf;
    }

    .mahjong-tile.is-vanishing {
        display: none;
    }
}
