.media-gallery {
    width: 100%;
    margin: 1rem 0;
}

.media-gallery.is-empty {
    display: none;
}

.media-gallery.is-grid {
    display: block;
    flex: none;
    min-height: 0;
    margin: 1rem 0;
}

.media-gallery.is-single {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    margin: 0;
}

.media-gallery-hero {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    width: 100%;
}

.media-gallery-hero-media {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: min(60vh, calc(100vh - 260px));
    width: 100%;
    background: #111;
    border-radius: 12px;
    overflow: hidden;
}

.media-gallery-hero-image,
.media-gallery-hero-video {
    display: block;
    width: 100%;
    height: 100%;
    max-height: calc(100vh - 240px);
    object-fit: contain;
    background: #111;
}

.media-gallery-hero-video {
    pointer-events: auto;
}

.media-gallery-hero-meta {
    flex-shrink: 0;
    padding: 12px 4px 0;
}

.media-gallery.is-viewonly-card {
    margin-top: 1rem;
}

.media-gallery-viewonly-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    width: 100%;
    padding: 1.5rem 1.25rem;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.35);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
    text-align: center;
}

.media-gallery-viewonly-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    color: rgba(255, 255, 255, 0.85);
}

.media-gallery-viewonly-icon i {
    font-size: 28px;
    line-height: 1;
}

.media-gallery-viewonly-meta {
    width: 100%;
}

.media-gallery-viewonly-meta .media-gallery-name {
    display: block;
    font-size: 1rem;
    font-weight: 600;
    word-break: break-word;
}

.media-gallery-viewonly-meta .media-gallery-size {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.85rem;
    opacity: 0.75;
}

.media-gallery-viewonly-preview-btn {
    margin: 0;
    min-width: 180px;
}

.tab-window .tab#tab-files.active {
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    height: calc(100vh - 130px);
    box-sizing: border-box;
}

.tab-window .tab#tab-viewonly-files.active {
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    height: calc(100vh - 130px);
    box-sizing: border-box;
}

.tab-window .tab#tab-files.active .media-gallery.is-single {
    flex: 1;
    min-height: 0;
    overflow: visible;
}

.tab-window .tab#tab-files.active > h1,
.tab-window .tab#tab-files.active > .media-gallery-toolbar {
    flex-shrink: 0;
}

.tab-window .tab#tab-viewonly-files.active > h1 {
    flex-shrink: 0;
}

@media (max-width: 768px) {
    .tab-window .tab#tab-files.active {
        /* tab-window margin-top 50px + tab margins 70px top / 25px bottom */
        height: calc(100dvh - 145px);
        max-height: calc(100dvh - 145px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    .tab-window .tab#tab-viewonly-files.active {
        height: calc(100dvh - 145px);
        max-height: calc(100dvh - 145px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    .tab-window .tab#tab-files.active .media-gallery.is-single {
        flex: none;
    }

    .tab-window .tab#tab-files.active .media-gallery.is-single .media-gallery-hero,
    .tab-window .tab#tab-files.active .media-gallery.is-single .media-gallery-hero-media {
        flex: none;
        min-height: 0;
    }

    .tab-window .tab#tab-files.active .media-gallery-hero-media {
        overflow: visible;
    }

    .tab-window .tab#tab-files.active .media-gallery-hero-image,
    .tab-window .tab#tab-files.active .media-gallery-hero-video {
        width: 100%;
        height: auto;
        max-height: calc(100dvh - 340px);
        object-fit: contain;
    }
}

.media-gallery-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    margin: 0 0 1rem;
    padding: 12px 14px;
    background: #f5f5f5;
    border-radius: 12px;
}

.media-gallery-select-all {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    font-size: 0.9rem;
    font-weight: 600;
    color: #363636;
    cursor: pointer;
    user-select: none;
}

.media-gallery-select-all input {
    width: 16px;
    height: 16px;
    margin: 0;
    cursor: pointer;
}

.media-gallery-selected-count {
    font-size: 0.85rem;
    color: #746e6e;
    font-weight: 600;
}

.media-gallery-toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-left: auto;
}

.media-gallery-toolbar .button {
    margin: 0;
}

.media-gallery-item-wrap,
.media-gallery-hero {
    position: relative;
}

.media-gallery-item-wrap {
    display: block;
    min-width: 0;
    overflow: hidden;
    aspect-ratio: 1 / 1;
}

.media-gallery-item-select {
    position: absolute;
    top: 8px;
    left: 8px;
    z-index: 3;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin: 0;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    cursor: pointer;
}

.media-gallery-item-select input {
    width: 16px;
    height: 16px;
    margin: 0;
    cursor: pointer;
}

.media-gallery-download-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 3;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.92);
    color: #363636;
    text-decoration: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.media-gallery-download-btn:hover,
.media-gallery-download-btn:focus {
    background: #485fc7;
    color: #fff;
    transform: scale(1.05);
    outline: none;
}

.media-gallery-download-btn i {
    font-size: 18px;
    line-height: 1;
}

.tab .files-other .file {
    position: relative;
}

.tab .files-other .media-gallery-item-select {
    top: 12px;
    left: 12px;
}

.media-lightbox-download {
    position: absolute;
    z-index: 4;
    top: 8px;
    left: 8px;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    cursor: pointer;
    line-height: 1;
    transition: background 0.15s ease;
}

.media-lightbox-download i {
    font-size: 20px;
    line-height: 40px;
}

.media-lightbox-download:hover {
    background: rgba(255, 255, 255, 0.22);
}

@media (max-width: 768px) {
    .media-gallery-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .media-gallery-toolbar-actions {
        margin-left: 0;
        width: 100%;
    }

    .media-gallery-toolbar-actions .button {
        flex: 1;
    }
}

.media-gallery-title {
    display: block;
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #746e6e;
    margin-bottom: 0.75rem;
}

.media-gallery-grid {
    display: grid;
    gap: 12px;
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

/* 2 items: always split evenly */
.media-gallery-grid[data-count="2"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

/* 3 items: 2 cols narrow, 3 cols when panel is wide enough */
.media-gallery-grid[data-count="3"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (min-width: 480px) {
    .media-gallery-grid[data-count="3"] {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

/* 4+ items: 2 cols narrow, 3 cols wide */
.media-gallery-grid[data-count="4"],
.media-gallery-grid[data-count="5"],
.media-gallery-grid[data-count="6"],
.media-gallery-grid[data-count="7"],
.media-gallery-grid[data-count="8"],
.media-gallery-grid[data-count="9"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (min-width: 480px) {
    .media-gallery-grid[data-count="4"],
    .media-gallery-grid[data-count="5"],
    .media-gallery-grid[data-count="6"],
    .media-gallery-grid[data-count="7"],
    .media-gallery-grid[data-count="8"],
    .media-gallery-grid[data-count="9"] {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

.media-gallery-grid[data-count]:not([data-count="1"]):not([data-count="2"]):not([data-count="3"]):not([data-count="4"]):not([data-count="5"]):not([data-count="6"]):not([data-count="7"]):not([data-count="8"]):not([data-count="9"]) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

@media (min-width: 480px) {
    .media-gallery-grid[data-count]:not([data-count="1"]):not([data-count="2"]) {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .media-gallery-grid[data-count="2"] {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

.media-gallery-grid > .media-gallery-item-wrap,
.media-gallery-grid > .media-gallery-item {
    min-width: 0;
    aspect-ratio: 1 / 1;
}

.media-gallery-item-wrap > .media-gallery-item {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
}

.media-gallery-item {
    position: relative;
    display: block;
    width: 100%;
    height: 100%;
    aspect-ratio: 1 / 1;
    border: 0;
    padding: 0;
    background: #f5f5f5;
    border-radius: 10px;
    overflow: hidden;
    text-align: left;
    cursor: pointer;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.06);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.media-gallery-item-wrap:hover .media-gallery-item,
.media-gallery-item:hover,
.media-gallery-item:focus {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
    outline: none;
}

.media-gallery-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, rgba(30, 34, 48, 0.85), rgba(52, 58, 78, 0.75));
    color: rgba(255, 255, 255, 0.72);
    font-size: 28px;
}

.media-gallery-placeholder-video {
    background: linear-gradient(135deg, rgba(20, 24, 36, 0.9), rgba(40, 46, 62, 0.82));
}

.media-gallery-item-blurred .media-gallery-placeholder {
    filter: blur(14px);
    transform: scale(1.06);
}

.media-gallery-item-blurred:hover .media-gallery-placeholder {
    filter: blur(10px);
}

.media-gallery-item img,
.media-gallery-item video,
.media-gallery-video {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #e8e8e8;
}

.media-gallery-item-video {
    cursor: pointer;
}

.media-gallery-item-video .media-gallery-video {
    object-fit: cover;
    background: #111;
    pointer-events: none;
}

.media-gallery-play-badge {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 1;
    transform: translate(-50%, -50%);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    pointer-events: none;
}

.media-gallery-play-badge::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 54%;
    transform: translate(-50%, -50%);
    border-style: solid;
    border-width: 9px 0 9px 14px;
    border-color: transparent transparent transparent #fff;
}

.media-gallery-item > .media-gallery-meta {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 2;
    padding: 28px 10px 8px;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.72) 0%, rgba(0, 0, 0, 0.35) 55%, transparent 100%);
    pointer-events: none;
}

.media-gallery-item .media-gallery-meta .media-gallery-name {
    display: block;
    font-size: 12px;
    font-weight: 700;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.media-gallery-item .media-gallery-meta .media-gallery-size {
    display: block;
    font-size: 11px;
    color: rgba(255, 255, 255, 0.85);
    margin-top: 2px;
}

.media-gallery-hero-meta,
.media-gallery-viewonly-meta {
    position: static;
    padding: 0;
    background: none;
    pointer-events: auto;
}

.media-gallery-hero-meta {
    flex-shrink: 0;
    padding: 12px 4px 0;
}

.media-gallery-name {
    display: block;
    font-size: 12px;
    font-weight: 700;
    color: #363636;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.media-gallery-size {
    display: block;
    font-size: 11px;
    color: #746e6e;
    margin-top: 2px;
}

.upload-block .selected-files ul li.has-media-preview {
    height: auto;
    min-height: 56px;
    align-items: flex-start;
    padding-top: 8px;
    padding-bottom: 8px;
}

.upload-block .selected-files ul li .upload-preview-thumb {
    flex: 0 0 48px;
    width: 48px;
    height: 48px;
    aspect-ratio: 1 / 1;
    border-radius: 8px;
    object-fit: cover;
    margin-right: 10px;
    background: #ddd;
    cursor: zoom-in;
    pointer-events: auto;
}

.upload-block .selected-files ul li .upload-preview-video {
    pointer-events: none;
}

.upload-block .selected-files ul li.has-media-preview span.name,
.upload-block .selected-files ul li.has-media-preview span.size {
    width: calc(100% - 68px);
}

.media-lightbox {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: none;
    align-items: center;
    justify-content: center;
}

.media-lightbox.active {
    display: flex;
}

.media-lightbox-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(10, 10, 20, 0.88);
}

.media-lightbox-dialog {
    position: relative;
    z-index: 1;
    width: min(96vw, 1100px);
    max-height: 92vh;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
    padding: 48px 56px 64px;
    box-sizing: border-box;
}

.media-lightbox-dialog[data-watermark]::before,
.media-lightbox-dialog[data-watermark]::after {
    content: attr(data-watermark);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) rotate(-24deg);
    white-space: nowrap;
    font-size: clamp(0.9rem, 2vw, 1.4rem);
    font-weight: 700;
    letter-spacing: 0.08em;
    color: rgba(255, 255, 255, 0.16);
    text-transform: uppercase;
    pointer-events: none;
    z-index: 3;
}

.media-lightbox-dialog[data-watermark]::after {
    transform: translate(-30%, -20%) rotate(-24deg);
    color: rgba(255, 255, 255, 0.1);
}

.media-lightbox-stage {
    position: relative;
    z-index: 2;
    width: 100%;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    min-width: 0;
}

.media-lightbox-image,
.media-lightbox-video {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: calc(92vh - 120px);
    border-radius: 8px;
    background: #111;
    object-fit: contain;
}

.media-lightbox-video {
    width: 100%;
    max-width: 100%;
    pointer-events: auto;
}

.media-lightbox-close,
.media-lightbox-nav {
    position: absolute;
    z-index: 4;
    border: 0;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    cursor: pointer;
    line-height: 1;
    transition: background 0.15s ease;
}

.media-lightbox-close {
    top: 8px;
    right: 8px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    font-size: 28px;
}

.media-lightbox-nav {
    top: 50%;
    transform: translateY(-50%);
    width: 44px;
    height: 64px;
    border-radius: 8px;
    font-size: 34px;
}

.media-lightbox-prev {
    left: 0;
}

.media-lightbox-next {
    right: 0;
}

.media-lightbox-close:hover,
.media-lightbox-nav:hover {
    background: rgba(255, 255, 255, 0.22);
}

.media-lightbox-caption,
.media-lightbox-counter {
    position: relative;
    z-index: 4;
    color: rgba(255, 255, 255, 0.92);
    text-align: center;
}

.media-lightbox-caption {
    margin-top: 12px;
    font-size: 0.95rem;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.media-lightbox-counter {
    margin-top: 6px;
    font-size: 0.8rem;
    opacity: 0.75;
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.media-lightbox-counter.media-lightbox-counter-flash {
    opacity: 1;
    transform: scale(1.08);
}

body.media-lightbox-open {
    overflow: hidden;
}

.viewonly-page .media-gallery-item-image img {
    pointer-events: none;
    -webkit-user-drag: none;
}

.viewonly-page .media-gallery-item-video .media-gallery-video {
    pointer-events: none;
}

.tab .files .file-media-thumb {
    display: block;
    width: 100%;
    max-height: 220px;
    object-fit: cover;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
}

.tab .files .file-media-video {
    display: block;
    width: 100%;
    max-height: 220px;
    background: #111;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
}

@media (max-width: 768px) {
    .media-gallery-grid {
        gap: 8px;
    }

    .media-lightbox-dialog {
        padding: 40px 12px 56px;
        max-height: 100vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .media-lightbox-nav {
        width: 36px;
        height: 52px;
        font-size: 28px;
    }
}
