:host {
    width: 100%;
    display: flex;
    min-width: 0;
    min-height: 0;
}

%downloads-skeleton-shimmer {
    position: relative;
    overflow: hidden;
    background: color-mix(
        in srgb,
        var(--app-widget-header-bg, var(--mat-sys-surface-container-high)) 56%,
        var(--app-body-color, var(--mat-sys-on-surface-variant)) 44%
    );

    &::after {
        content: '';
        position: absolute;
        inset: 0;
        background: linear-gradient(
            100deg,
            transparent 8%,
            color-mix(
                    in srgb,
                    var(--app-heading-color, var(--mat-sys-on-surface)) 18%,
                    transparent
                )
                44%,
            transparent 74%
        );
        transform: translateX(-120%);
        animation: downloads-skeleton-shimmer 1.15s ease-in-out infinite;
    }
}

.downloads-layout {
    display: flex;
    flex: 1;
    width: 100%;
    min-width: 0;
    min-height: 0;
}

.downloads {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    min-width: 0;
    min-height: 0;
    overflow: hidden;

    &__header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 1rem;
        padding: 24px 16px 12px;
        border-bottom: 1px solid var(--mat-sys-outline-variant);

        &-left {
            display: flex;
            align-items: center;
            gap: 16px;

            .settings-group__header-text {
                display: flex;
                align-items: center;
                gap: 0.75rem;

                h1 {
                    margin: 0;
                    font-size: 1.2rem;
                    font-weight: 650;
                    letter-spacing: -0.02em;
                }
            }

            .back-btn {
                margin-right: 0.5rem;
            }
        }

        &-actions {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            flex-wrap: wrap;
            gap: 0.5rem;
        }
    }

    &__badge {
        background: var(--mat-sys-primary);
        color: var(--mat-sys-on-primary);
        padding: 0.125rem 0.5rem;
        border-radius: 12px;
        font-size: 0.75rem;
        font-weight: 600;
    }

    &__folder-inline {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        max-width: min(42vw, 460px);
        padding: 7px 10px;
        background: color-mix(
            in srgb,
            var(--mat-sys-surface-container-high) 86%,
            transparent
        );
        border: 1px solid var(--mat-sys-outline-variant);
        border-radius: 8px;
        box-shadow: inset 0 1px 0
            color-mix(in srgb, var(--mat-sys-on-surface) 8%, transparent);

        mat-icon {
            width: 16px;
            height: 16px;
            font-size: 16px;
            color: var(--mat-sys-primary);
            flex-shrink: 0;
        }
    }

    &__folder-inline-path {
        font-family:
            'JetBrains Mono', 'SFMono-Regular', Menlo, Monaco, Consolas,
            monospace;
        font-size: 0.78rem;
        color: var(--mat-sys-on-surface);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    &__empty {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        flex: 1;
        text-align: center;
        color: var(--mat-sys-on-surface-variant);

        mat-icon {
            font-size: 4rem;
            width: 4rem;
            height: 4rem;
            margin-bottom: 1rem;
            opacity: 0.4;
        }

        p {
            margin: 0 0 0.5rem;
            font-size: 1.05rem;
            font-weight: 520;
            letter-spacing: -0.01em;
            color: var(--mat-sys-on-surface);
        }

        span {
            font-size: 0.875rem;
        }
    }

    &__list-wrapper {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 0 16px;
        container-type: inline-size;

        &--loading {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }
    }

    &__list {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(min(100%, 380px), 1fr));
        gap: 12px;
        padding-bottom: 24px;
        align-content: start;

        &--skeleton {
            pointer-events: none;
        }
    }

    &__loading-label {
        margin: 0;
        padding: 2px 4px 0;
        font-size: 0.86rem;
        font-weight: 600;
        letter-spacing: -0.01em;
        color: var(--mat-sys-on-surface);
    }

    &__skeleton-item {
        display: grid;
        grid-template-columns: 64px minmax(0, 1fr);
        align-items: stretch;
        gap: 1rem;
        min-width: 0;
        min-height: 124px;
        padding: 1rem;
        border-radius: 14px;
        background: color-mix(
            in srgb,
            var(--app-widget-bg, var(--mat-sys-surface-container-high)) 88%,
            var(--mat-sys-surface-container) 12%
        );
        border: 1px solid var(--app-separator, var(--mat-sys-outline-variant));
        box-shadow: var(--app-widget-shadow, none);
    }

    &__skeleton-poster {
        @extend %downloads-skeleton-shimmer;
        width: 64px;
        height: 90px;
        border-radius: 10px;
        border: 1px solid var(--app-separator, var(--mat-sys-outline-variant));
    }

    &__skeleton-info {
        display: flex;
        flex-direction: column;
        gap: 0.55rem;
        min-width: 0;
        min-height: 90px;
    }

    &__skeleton-title {
        @extend %downloads-skeleton-shimmer;
        width: min(70%, 280px);
        height: 13px;
        border-radius: 999px;

        &--short {
            width: min(48%, 190px);
        }
    }

    &__skeleton-meta {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;

        span {
            @extend %downloads-skeleton-shimmer;
            width: 88px;
            height: 20px;
            border-radius: 999px;

            &:last-child {
                width: 132px;
            }
        }
    }

    &__skeleton-progress {
        @extend %downloads-skeleton-shimmer;
        width: 100%;
        height: 6px;
        border-radius: 999px;
        margin-top: 0.05rem;
    }

    &__skeleton-actions {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        gap: 0.35rem;
        margin-top: auto;
        padding-top: 0.65rem;
        border-top: 1px solid
            color-mix(
                in srgb,
                var(--app-separator, var(--mat-sys-outline-variant)) 72%,
                transparent
            );

        span {
            @extend %downloads-skeleton-shimmer;
            width: 32px;
            height: 32px;
            border-radius: 9px;
        }
    }

    &__item {
        display: grid;
        grid-template-columns: 64px minmax(0, 1fr);
        align-items: stretch;
        gap: 1rem;
        padding: 1rem;
        min-width: 0;
        background: var(
            --app-widget-bg,
            var(--mat-sys-surface-container-high)
        );
        border-radius: 14px;
        border: 1px solid var(--app-separator, var(--mat-sys-outline-variant));
        box-shadow: var(--app-widget-shadow, none);
        transition:
            background 0.18s ease,
            border-color 0.18s ease,
            box-shadow 0.2s ease,
            transform 0.2s ease;

        &--clickable {
            cursor: pointer;

            &:focus-visible {
                outline: 2px solid var(--mat-sys-primary);
                outline-offset: 2px;
            }
        }

        &:hover {
            background: var(--app-selection-surface);
            border-color: color-mix(
                in srgb,
                var(--app-selection-border) 72%,
                transparent
            );
        }

        &--unavailable {
            cursor: default;
        }

        &-poster {
            width: 64px;
            height: 90px;
            border-radius: 10px;
            overflow: hidden;
            flex-shrink: 0;
            background: var(
                --app-widget-header-bg,
                var(--mat-sys-surface-container-high)
            );
            display: flex;
            align-items: center;
            justify-content: center;
            border: 1px solid var(--app-separator, var(--mat-sys-outline-variant));

            img {
                width: 100%;
                height: 100%;
                object-fit: cover;
            }

            mat-icon {
                font-size: 2rem;
                width: 2rem;
                height: 2rem;
                color: var(--mat-sys-on-surface-variant);
                opacity: 0.5;
            }
        }

        &-info {
            flex: 1;
            min-width: 0;
            min-height: 90px;
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
        }

        &-title {
            font-size: 1.05rem;
            font-weight: 600;
            color: var(--mat-sys-on-surface);
            display: flex;
            align-items: flex-start;
            gap: 0.5rem;
            min-width: 0;
        }

        &-title-text {
            min-width: 0;
            display: -webkit-box;
            overflow: hidden;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            line-height: 1.3;
        }

        &-meta {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
            font-size: 0.85rem;
            color: var(--mat-sys-on-surface-variant);
        }

        &-status {
            display: flex;
            align-items: center;
            gap: 0.35rem;
            font-weight: 500;
            padding: 2px 8px;
            border-radius: 12px;
            background: var(
                --app-widget-header-bg,
                var(--mat-sys-surface-container-high)
            );

            mat-icon {
                font-size: 1.1rem;
                width: 1.1rem;
                height: 1.1rem;
            }

            &.status-downloading {
                color: #2196f3;
                background: color-mix(in srgb, #2196f3 12%, transparent);
            }

            &.status-completed {
                color: #4caf50;
                background: color-mix(in srgb, #4caf50 12%, transparent);
            }

            &.status-failed {
                color: var(--mat-sys-error);
                background: color-mix(
                    in srgb,
                    var(--mat-sys-error) 12%,
                    transparent
                );
            }

            &.status-queued {
                color: var(--mat-sys-on-surface-variant);
            }
        }

        &-size {
            font-family:
                'JetBrains Mono', 'SFMono-Regular', Menlo, Monaco, Consolas,
                monospace;
            background: transparent;
            padding: 2px 8px;
            border-radius: 12px;
        }

        &-error {
            color: var(--mat-sys-error);
            font-size: 0.8rem;
            background: color-mix(
                in srgb,
                var(--mat-sys-error) 12%,
                transparent
            );
            padding: 2px 8px;
            border-radius: 12px;
        }

        &-actions {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            gap: 0.2rem;
            margin-top: auto;
            padding-top: 0.65rem;
            border-top: 1px solid
                color-mix(
                    in srgb,
                    var(--app-separator, var(--mat-sys-outline-variant)) 72%,
                    transparent
                );

            button {
                width: 32px;
                height: 32px;
                min-width: 32px;
                min-height: 32px;
                padding: 0;
                border-radius: 9px;
                --mdc-icon-button-state-layer-size: 32px;
                transition:
                    background-color 0.15s ease,
                    transform 0.15s ease,
                    opacity 0.15s ease;
                opacity: 0.86;

                &:hover {
                    background: var(--app-selection-surface);
                    transform: translateY(-1px);
                    opacity: 1;
                }
            }

            mat-icon {
                width: 18px;
                height: 18px;
                font-size: 18px;
            }
        }

        mat-progress-bar {
            margin-top: 0.15rem;
            border-radius: 4px;
        }
    }

    &__episode-badge {
        background: var(--mat-sys-primary);
        color: var(--mat-sys-on-primary);
        padding: 0.125rem 0.5rem;
        border-radius: 6px;
        font-size: 0.7rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
}

.sidebar-header h2 {
    margin: 0;
}

@media (max-width: 980px) {
    .downloads {
        &__header {
            align-items: flex-start;
            gap: 12px;
        }

        &__header-actions {
            width: 100%;
            justify-content: flex-start;
        }

        &__folder-inline {
            max-width: 100%;
        }

        &__list {
            grid-template-columns: 1fr;
        }
    }
}

@container (max-width: 760px) {
    .downloads__item {
        align-items: start;
    }

    .downloads__skeleton-item {
        align-items: start;
    }
}

@media (prefers-reduced-motion: reduce) {
    .downloads__skeleton-poster,
    .downloads__skeleton-title,
    .downloads__skeleton-meta span,
    .downloads__skeleton-progress,
    .downloads__skeleton-actions span {
        &::after {
            animation: none;
            transform: none;
        }
    }
}

@keyframes downloads-skeleton-shimmer {
    100% {
        transform: translateX(120%);
    }
}
