:host {
    display: block;
    --rail-card-width: var(--cover-rail-width, 172px);
    --rail-gap: var(--cover-gap, 14px);
    --rail-radius: 12px;
}

.rail {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.rail__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 0 2px;
    // Tighter height cap — keeps rail chapter markers compact
    min-height: 24px;
}

.rail__title {
    margin: 0;
    display: inline-flex;
    align-items: baseline;
    gap: 8px;
    font-size: 1.125rem; // 18px — matches Netflix rail-title scale
    font-weight: 600;
    line-height: 1.2;
    letter-spacing: -0.012em;
    color: var(--app-heading-color, #1a1d23);
}

.rail__see-all {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 4px 8px 4px 10px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 500;
    line-height: 1;
    letter-spacing: 0.005em;
    color: var(--app-muted-color, #8891a4);
    text-decoration: none;
    transition:
        color 0.15s ease,
        background-color 0.15s ease;

    mat-icon {
        font-size: 16px;
        width: 16px;
        height: 16px;
        transition: transform 0.15s ease;
    }

    &:hover {
        color: var(--app-selection-color, #2f7bff);
        background: color-mix(
            in srgb,
            var(--app-selection-color, #2f7bff) 12%,
            transparent
        );

        mat-icon {
            transform: translateX(2px);
        }
    }

    &:focus-visible {
        outline: 2px solid var(--app-selection-color, #2f7bff);
        outline-offset: 2px;
    }
}

.rail__viewport {
    position: relative;
    min-width: 0;

    // Soft edge gradients hint at horizontal scroll without any interaction.
    // Anchored to the viewport so they appear/disappear in sync with the
    // chevrons' `.rail__chev--visible` state.
    &::before,
    &::after {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        width: 32px;
        pointer-events: none;
        z-index: 1;
        opacity: 0;
        transition: opacity 0.2s ease;
    }

    &::before {
        left: 0;
        background: linear-gradient(
            to right,
            var(--app-content-bg, #161a22) 0%,
            transparent 100%
        );
    }

    &::after {
        right: 0;
        background: linear-gradient(
            to left,
            var(--app-content-bg, #161a22) 0%,
            transparent 100%
        );
    }

    // Show the correct edge gradient when scroll is possible in that direction
    &:has(.rail__chev--left.rail__chev--visible)::before {
        opacity: 0.9;
    }

    &:has(.rail__chev--right.rail__chev--visible)::after {
        opacity: 0.9;
    }

    &:hover .rail__chev--visible {
        opacity: 1;
        transform: translateY(-50%) scale(1);
    }
}

.rail__track {
    display: flex;
    align-items: flex-start;
    gap: var(--rail-gap);
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    scroll-padding-inline-start: 4px;
    scrollbar-width: none;
    padding: 4px;
    margin: -4px;

    &::-webkit-scrollbar {
        display: none;
    }
}

.rail__card {
    // Pin the card size so long titles never expand the flex item.
    // `min-width: 0` is needed because flex items default to `min-width: auto`
    // (their intrinsic content size), which would otherwise override the
    // flex-basis when the title wraps.
    flex: 0 0 var(--rail-card-width);
    width: var(--rail-card-width);
    max-width: var(--rail-card-width);
    min-width: 0;
    scroll-snap-align: start;
    position: relative;
    display: flex;
    flex-direction: column;
    color: inherit;
    border-radius: var(--rail-radius);
    transition:
        transform 0.2s ease,
        filter 0.2s ease;

    &:hover {
        transform: translateY(-2px);

        .rail__art {
            box-shadow:
                0 12px 22px -12px rgba(0, 0, 0, 0.45),
                0 0 0 1px var(--app-selection-border, rgba(47, 123, 255, 0.4));
        }

        .rail__card-title {
            color: var(--app-selection-color, #2f7bff);
        }
    }
}

.rail__card-link {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
    color: inherit;
    text-decoration: none;
    border-radius: inherit;

    &:focus-visible {
        outline: 2px solid var(--app-selection-color, #2f7bff);
        outline-offset: 3px;
    }
}

.rail__art {
    position: relative;
    overflow: hidden;
    border-radius: var(--rail-radius);
    background: var(--app-widget-bg, #1e2330);
    border: 1px solid var(--app-widget-header-border, rgba(255, 255, 255, 0.05));
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
    transition: box-shadow 0.2s ease;

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

.rail__art-fallback {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background:
        radial-gradient(
            circle at 30% 20%,
            var(--app-selection-surface, rgba(120, 173, 255, 0.18)) 0%,
            transparent 55%
        ),
        var(--app-widget-bg, #1e2330);

    mat-icon {
        font-size: 44px;
        width: 44px;
        height: 44px;
        color: var(--app-selection-color, #78adff);
        opacity: 0.55;
    }
}

// Live TV channel logos are typically small (100–256px square) and look terrible
// when stretched to fill a 2:3 portrait card with `object-fit: cover`. The
// `--live` modifier centers the contained logo over a soft blurred wash sampled
// from the logo itself — same pattern as Apple Podcasts / Plex Live TV tiles.
.rail__art--live {
    img {
        box-sizing: border-box;
        object-fit: contain;
        padding: 26% 18%;
        position: relative;
        z-index: 1;
        filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.35));
    }
}

// Thin "watched-up-to-here" bar pinned to the bottom of the poster art.
// Mirrors the Netflix / Plex resume-card pattern: the bar is the user's
// quick "did I finish this?" cue without needing to read the metadata
// strip below the card.
.rail__art-progress {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 3px;
    background: rgba(0, 0, 0, 0.55);
    z-index: 2;
    pointer-events: none;

    i {
        display: block;
        height: 100%;
        background: var(--app-selection-color, #78adff);
        transition: width 0.4s ease;
    }
}

.rail__art-bg {
    position: absolute;
    inset: 0;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    filter: blur(28px) saturate(1.4);
    transform: scale(1.4);
    opacity: 0.55;
    z-index: 0;
    pointer-events: none;
}

.rail__action-trigger {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 3;
    width: 30px;
    height: 30px;
    padding: 3px;
    border-radius: 50%;
    color: #fff;
    background: transparent;
    border: 0;
    box-shadow: none;
    opacity: 0.92;
    transition:
        opacity 0.15s ease,
        transform 0.15s ease;
    --mdc-icon-button-state-layer-size: 30px;

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

    &:hover,
    &:focus-visible {
        opacity: 1;
        transform: translateY(-1px);
    }
}

.rail__menu-action--separated {
    border-top: 1px solid var(--app-separator);
    margin-top: 4px;
}

.rail__menu-action--danger {
    color: var(--mat-sys-error);

    mat-icon {
        color: var(--mat-sys-error);
    }
}

.rail__meta {
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 0 2px;
    min-width: 0;
}

.rail__card-title {
    font-size: 0.85rem;
    font-weight: 600;
    line-height: 1.3;
    color: var(--app-heading-color, #1a1d23);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: -0.005em;
    transition: color 0.15s ease;
}

.rail__card-meta-row {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}

.rail__card-episode {
    flex: 0 0 auto;
    font-size: 0.66rem;
    font-weight: 700;
    line-height: 1;
    padding: 2px 6px;
    border-radius: 4px;
    background: color-mix(
        in srgb,
        var(--app-selection-color, #2f86ff) 14%,
        transparent
    );
    color: var(--app-selection-color, #2f86ff);
    letter-spacing: 0.02em;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

.rail__card-subtitle {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 0.72rem;
    font-weight: 500;
    line-height: 1.4;
    color: var(--app-muted-color, #8891a4);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: 0.005em;
}

// Circular chevron — streaming-app pattern (Netflix/Apple TV+). High-contrast
// dark glass works against any card color, including the muted fallback tiles
// which previously camouflaged the buttons.
.rail__chev {
    position: absolute;
    top: 50%;
    transform: translateY(-50%) scale(0.96);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.72);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    color: #fff;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition:
        opacity 0.18s ease,
        background-color 0.18s ease,
        transform 0.18s ease,
        box-shadow 0.18s ease;
    z-index: 2;
    box-shadow:
        0 4px 14px -4px rgba(0, 0, 0, 0.55),
        0 0 0 0 rgba(255, 255, 255, 0); // slot for hover ring

    mat-icon {
        font-size: 24px;
        width: 24px;
        height: 24px;
        line-height: 24px;
        // Keep icon crisp over bright content
        filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.5));
    }

    // Peek at rest when scroll is possible — hints the affordance exists
    // without requiring the user to hover first.
    &--visible {
        opacity: 0.5;
        pointer-events: auto;
    }

    &:hover {
        background: rgba(0, 0, 0, 0.88);
        transform: translateY(-50%) scale(1.08);
        box-shadow:
            0 8px 22px -6px rgba(0, 0, 0, 0.65),
            0 0 0 4px rgba(255, 255, 255, 0.06);
    }

    &:active {
        transform: translateY(-50%) scale(0.96);
    }

    &:focus-visible {
        outline: 2px solid var(--app-selection-color, #78adff);
        outline-offset: 3px;
    }

    &--left {
        left: 6px;
    }

    &--right {
        right: 6px;
    }
}

@media (hover: none) {
    .rail__chev {
        display: none;
    }

    .rail__viewport::before,
    .rail__viewport::after {
        display: none;
    }
}

@media (max-width: 720px) {
    :host {
        --rail-card-width: min(var(--cover-rail-width, 172px), 140px);
    }
}

// ── Channel layout (compact horizontal info row) ────────────────────────────
// Used by the "Live now on your favorites" rail. Two stacked rows:
//   1. logo + channel name + current programme title
//   2. LIVE chip + progress bar + time range
// Card is wider but much shorter than the 2:3 poster — TV station logos are
// small (100–256px square) so a poster card wastes most of its height on
// padding. Mirrors the live strip in the v0.22 mockup.
.rail__track[data-layout='channel'] {
    --rail-card-width: var(--channel-rail-width, 280px);
    --rail-gap: 10px;
    scroll-snap-type: x mandatory;
}

.rail__card--channel {
    background: var(--app-widget-bg, #1e2330);
    border: 1px solid
        var(--app-widget-header-border, rgba(255, 255, 255, 0.05));
    border-radius: var(--rail-radius);
    padding: 10px 12px;
    transition:
        transform 0.2s ease,
        border-color 0.2s ease,
        box-shadow 0.2s ease;

    &:hover {
        transform: translateY(-1px);
        border-color: var(
            --app-selection-border,
            rgba(47, 123, 255, 0.4)
        );
        box-shadow:
            0 6px 16px -10px rgba(0, 0, 0, 0.45),
            0 0 0 1px var(--app-selection-border, rgba(47, 123, 255, 0.3));

        .rail__channel-name {
            color: var(--app-selection-color, #2f7bff);
        }
    }
}

.rail__channel-link {
    display: flex;
    flex-direction: column;
    gap: 8px;
    color: inherit;
    text-decoration: none;
    border-radius: inherit;

    &:focus-visible {
        outline: 2px solid var(--app-selection-color, #2f7bff);
        outline-offset: 3px;
    }
}

.rail__channel-row {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.rail__channel-logo {
    flex: 0 0 auto;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: var(--app-content-bg, #161a22);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;

    img {
        max-width: 100%;
        max-height: 100%;
        object-fit: contain;
        // Drop-shadow gives small logos some lift against the card surface
        // without the dark wash from the poster-style live treatment.
        filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.35));
    }

    &--fallback mat-icon {
        font-size: 22px;
        width: 22px;
        height: 22px;
        color: var(--app-selection-color, #78adff);
        opacity: 0.7;
    }
}

.rail__channel-meta {
    min-width: 0;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.rail__channel-name {
    font-size: 0.85rem;
    font-weight: 600;
    line-height: 1.2;
    color: var(--app-heading-color, #1a1d23);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: color 0.15s ease;
}

.rail__channel-now {
    font-size: 0.72rem;
    font-weight: 500;
    line-height: 1.35;
    color: var(--app-muted-color, #8891a4);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-height: 1.35em;
}

.rail__channel-progress-row {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.65rem;
    color: var(--app-muted-color, #8891a4);
}

.rail__channel-live-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-family: var(
        --font-mono,
        ui-monospace,
        'SF Mono',
        'JetBrains Mono',
        Menlo,
        monospace
    );
    font-size: 0.6rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: var(--app-live-color, #ff8f8f);
    text-transform: uppercase;

    &::before {
        content: '';
        width: 5px;
        height: 5px;
        border-radius: 50%;
        background: var(--app-live-color, #ff8f8f);
        box-shadow: 0 0 5px var(--app-live-color, #ff8f8f);
    }
}

.rail__channel-progress {
    position: relative;
    flex: 1;
    height: 3px;
    border-radius: 2px;
    background: color-mix(
        in srgb,
        var(--app-on-surface, #e6e1e5) 12%,
        transparent
    );
    overflow: hidden;

    i {
        display: block;
        height: 100%;
        background: var(--app-live-color, #ff8f8f);
        border-radius: 2px;
        transition: width 0.4s ease;
    }

    &--idle i {
        // No EPG data yet — keep the bar neutral so it doesn't look like
        // "0% watched".
        background: color-mix(
            in srgb,
            var(--app-on-surface, #e6e1e5) 18%,
            transparent
        );
    }
}

.rail__channel-time {
    flex: 0 0 auto;
    font-variant-numeric: tabular-nums;
}
