/* ===== MobileGallery v4.0 (новый, mg4-префикс) ===== */

.mg4-overlay {
    position: fixed;
    inset: 0;
    z-index: 99999;
    background: rgba(0, 0, 0, 0.92);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity .3s ease, visibility 0s linear .3s;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.mg4-overlay.active {
    opacity: 1;
    visibility: visible;
    transition: opacity .3s ease, visibility 0s linear 0s;
}

.mg4-overlay:not(.active) {
    pointer-events: none;
}

.mg4-image-wrap {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    pointer-events: none;
}

.mg4-image {
    max-width: calc(100vw - 40px);
    max-height: calc(100vh - 160px);
    object-fit: contain;
    opacity: 0;
    pointer-events: auto;
    touch-action: none;
    will-change: transform;
    transform: scale(0.95);
    transition: opacity .25s ease, transform .25s ease;
}

.mg4-image.loaded {
   opacity: 1;
}

.mg4-image.mg4-image-zoomed {
    cursor: grab;
    max-width: none;
    max-height: none;
    width: auto;
    height: auto;
    transition: transform .05s linear;
}

.mg4-image.mg4-image-zoomed:active {
    cursor: grabbing;
}

.mg4-image.mg4-image-zoomed.loaded {
    transform: scale(1); /* переопределяем scale(0.95), но transform от JS перезапишет */
}

.mg4-spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: #fff;
    border-radius: 50%;
    animation: mg4-spin .8s linear infinite;
    display: none;
    z-index: 2;
}

.mg4-spinner.active {
    display: block;
}

@keyframes mg4-spin {
    to { transform: rotate(360deg); }
}

.mg4-error {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: #fff;
    display: none;
    padding: 24px;
    max-width: 320px;
    z-index: 3;
}

.mg4-error.active {
    display: block;
}

.mg4-error-icon {
    font-size: 48px;
    margin-bottom: 12px;
    opacity: .7;
}

.mg4-error-text {
    font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    margin-bottom: 16px;
    opacity: .85;
}

.mg4-error-retry {
    background: rgba(255, 255, 255, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: #fff;
    padding: 8px 20px;
    border-radius: 6px;
    font: 13px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    cursor: pointer;
    transition: background .15s ease;
}

.mg4-error-retry:hover {
    background: rgba(255, 255, 255, 0.25);
}

.mg4-caption {
    position: absolute;
    left: 50%;
    bottom: 84px;
    transform: translateX(-50%);
    max-width: calc(100vw - 40px);
    padding: 8px 18px;
    background: rgba(20, 20, 20, 0.75);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    font: 13px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
    pointer-events: none;
    z-index: 4;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: center;
}

@media (max-width: 600px) {
    .mg4-caption {
        bottom: 76px;
        max-width: calc(100vw - 24px);
        padding: 6px 14px;
        font-size: 12px;
        border-radius: 8px;
    }
}

.mg4-toolbar {
    position: absolute;
    left: 50%;
    bottom: 20px;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(20, 20, 20, 0.7);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: 8px 12px;
    border-radius: 12px;
    z-index: 5;
    pointer-events: auto;
}

.mg4-btn {
    background: transparent;
    border: 0;
    color: #fff;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background .15s ease;
    padding: 0;
    touch-action: manipulation;
}

.mg4-btn:hover {
    background: rgba(255, 255, 255, 0.15);
}

.mg4-btn:active {
    background: rgba(255, 255, 255, 0.25);
}

.mg4-btn svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: #fff;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    display: block;
}

.mg4-btn.mg4-nav[disabled] {
    opacity: 0.3;
    pointer-events: none;
}

.mg4-counter {
    color: #fff;
    font: 13px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    min-width: 44px;
    text-align: center;
    opacity: 0.85;
    user-select: none;
}

@media (max-width: 600px) {
    .mg4-toolbar {
        bottom: 12px;
        gap: 4px;
        padding: 6px 8px;
    }
    .mg4-btn {
        width: 44px;
        height: 44px;
    }
    .mg4-caption {
        bottom: 68px;
        font-size: 12px;
        padding: 6px 12px;
    }
    .mg4-image {
        max-width: calc(100vw - 16px);
        max-height: calc(100vh - 150px);
    }
}

.mg4-image-frame {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #dddddd;
    padding: 8px;
    border-radius: 12px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
    max-width: calc(100vw - 40px);
    max-height: calc(100vh - 160px);
    transition: transform .15s ease;
}

.mg4-image-frame.mg4-frame-zoomed {
    /* В зуме фрейм теряет фон и padding — картинка «выходит» наружу */
    background: transparent;
    padding: 0;
    border-radius: 0;
    box-shadow: none;
    max-width: none;
    max-height: none;
}

.mg4-image-frame.mg4-frame-zoomed .mg4-image {
    max-width: none;
    max-height: none;
}

@media (max-width: 600px) {
    .mg4-image-frame {
        max-width: calc(100vw - 16px);
        max-height: calc(100vh - 150px);
        padding: 6px;
        border-radius: 10px;
    }
}