// Color palette — every value resolves through the app's theme tokens so
// Multi-EPG looks like part of IPTVnator instead of a separate broadcast
// utility. Previously hard-coded #0a0a0f / #12121a / #1a1a24 made the grid
// switch to near-black while the rest of the app sat on --app-content-bg,
// which the UX audit called out as the screen "visually detaching" from
// the rest of the app.
$bg-deep: var(--app-content-bg);
$bg-surface: var(--app-widget-bg);
$bg-elevated: var(--app-widget-header-bg);
$bg-hover: var(--app-card-hover-bg);
$border-subtle: var(--app-widget-header-border);
$border-medium: var(--app-separator);

// Accent colors — unified with the rest of the app:
// cyan stays on the "now" indicator only; blue is the primary; the old
// $8b5cf6 purple and $00d4aa teal are dropped per the UX audit.
$accent-primary: #5cd6ff; // Cyan — reserved for "now" indicator
$accent-now-glow: rgba(92, 214, 255, 0.45); // Cyan glow for the now-line
$accent-secondary: var(--app-selection-color); // Blue — programs
$accent-tertiary: var(--app-selection-color); // Blue — highlights
$accent-warning: #fbbf24;

// Text colors — derived from app heading/body so light and dark themes
// stay in sync without a separate `.light-theme` override at the bottom
// (which was duplicating responsibility with the same token chain).
$text-primary: var(--app-heading-color);
$text-secondary: var(--app-body-color);
$text-muted: var(--app-muted-color);

// Gradients
$gradient-program: linear-gradient(
    135deg,
    rgba(79, 142, 255, 0.15) 0%,
    rgba(79, 142, 255, 0.05) 100%
);
$gradient-program-hover: linear-gradient(
    135deg,
    rgba(79, 142, 255, 0.3) 0%,
    rgba(79, 142, 255, 0.15) 100%
);
$gradient-header: linear-gradient(
    180deg,
    rgba(92, 214, 255, 0.08) 0%,
    transparent 100%
);

// Base container
.parent {
    display: flex;
    background: $bg-deep;
    color: $text-primary;
    font-family: Roboto, 'Helvetica Neue', sans-serif;
    position: relative;
    overflow-y: auto;
    height: calc(100vh - 56px);

    // Custom scrollbar for vertical scroll
    &::-webkit-scrollbar {
        width: 8px;
    }

    &::-webkit-scrollbar-track {
        background: $bg-surface;
    }

    &::-webkit-scrollbar-thumb {
        background: rgba(47, 123, 255, 0.4);
        border-radius: 4px;

        &:hover {
            background: rgba(47, 123, 255, 0.6);
        }
    }
}

// Navigation toolbar - Frosted glass effect
#epg-navigation {
    display: flex;
    align-items: center;
    padding: 12px 16px 12px 80px; // Left padding for macOS traffic lights
    background: rgba(18, 18, 26, 0.85);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid $border-medium;
    gap: 8px;
    position: relative;
    z-index: 100;
    -webkit-app-region: drag;

    // Subtle glow at bottom
    &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        height: 1px;
        background: linear-gradient(
            90deg,
            transparent 0%,
            rgba(92, 214, 255, 0.3) 20%,
            rgba(47, 123, 255, 0.3) 80%,
            transparent 100%
        );
    }

    // Button styling - exclude from drag region
    button {
        font-family: Roboto, 'Helvetica Neue', sans-serif;
        font-weight: 500;
        letter-spacing: 0.02em;
        transition: all 0.2s ease;
        -webkit-app-region: no-drag;

        &:not([disabled]):hover {
            background: rgba(47, 123, 255, 0.15);
        }

        &[disabled] {
            opacity: 0.3;
        }

        mat-icon {
            font-size: 20px;
        }
    }
}

// Search container - exclude from drag region
.search-container {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
    -webkit-app-region: no-drag;

    .channel-search {
        display: flex;
        align-items: center;
        gap: 8px;
        width: 200px;
        height: 36px;
        padding: 0 12px;
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid $border-subtle;
        border-radius: 8px;
        transition: all 0.2s ease;

        &:focus-within {
            border-color: rgba(47, 123, 255, 0.5);
            background: rgba(255, 255, 255, 0.08);
        }

        .search-icon {
            font-size: 18px;
            width: 18px;
            height: 18px;
            color: $text-muted;
            flex-shrink: 0;
        }

        input {
            flex: 1;
            background: transparent;
            border: none;
            outline: none;
            font-size: 13px;
            font-family: Roboto, 'Helvetica Neue', sans-serif;
            color: $text-primary;
            min-width: 0;

            &::placeholder {
                color: $text-muted;
            }
        }

        .clear-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 24px;
            height: 24px;
            padding: 0;
            background: transparent;
            border: none;
            border-radius: 50%;
            cursor: pointer;
            color: $text-muted;
            transition: all 0.15s ease;
            flex-shrink: 0;

            &:hover {
                background: rgba(255, 255, 255, 0.1);
                color: $text-primary;
            }

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

    .filter-count {
        font-size: 11px;
        color: $text-muted;
        font-family: 'JetBrains Mono', monospace;
        white-space: nowrap;
    }

    &.expanded {
        .channel-search {
            animation: slideIn 0.2s ease-out;
        }
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        width: 0;
        transform: translateX(20px);
    }
    to {
        opacity: 1;
        width: 200px;
        transform: translateX(0);
    }
}

// Program search container
.program-search-container {
    display: flex;
    align-items: center;
    gap: 8px;
    position: relative;
    -webkit-app-region: no-drag;

    .program-search-input {
        display: flex;
        align-items: center;
        gap: 8px;
        width: 220px;
        height: 36px;
        padding: 0 12px;
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid $border-subtle;
        border-radius: 8px;
        transition: all 0.2s ease;

        &:focus-within {
            border-color: rgba(47, 123, 255, 0.5);
            background: rgba(255, 255, 255, 0.08);
        }

        .search-icon {
            font-size: 18px;
            width: 18px;
            height: 18px;
            color: $accent-secondary;
            flex-shrink: 0;
        }

        input {
            flex: 1;
            background: transparent;
            border: none;
            outline: none;
            font-size: 13px;
            font-family: Roboto, 'Helvetica Neue', sans-serif;
            color: $text-primary;
            min-width: 0;

            &::placeholder {
                color: $text-muted;
            }
        }

        .clear-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 24px;
            height: 24px;
            padding: 0;
            background: transparent;
            border: none;
            border-radius: 50%;
            cursor: pointer;
            color: $text-muted;
            transition: all 0.15s ease;
            flex-shrink: 0;

            &:hover {
                background: rgba(255, 255, 255, 0.1);
                color: $text-primary;
            }

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

        .search-spinner {
            width: 16px;
            height: 16px;
            border: 2px solid $border-subtle;
            border-top-color: $accent-secondary;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            flex-shrink: 0;
        }
    }

    .program-search-results {
        position: fixed;
        top: 60px;
        right: 16px;
        width: 320px;
        max-height: 400px;
        overflow-y: auto;
        background: $bg-elevated;
        border: 1px solid $border-medium;
        border-radius: 8px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
        z-index: 10000;

        &::-webkit-scrollbar {
            width: 6px;
        }

        &::-webkit-scrollbar-track {
            background: transparent;
        }

        &::-webkit-scrollbar-thumb {
            background: rgba(47, 123, 255, 0.3);
            border-radius: 3px;
        }

        .search-result-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            padding: 12px 16px;
            background: transparent;
            border: none;
            border-bottom: 1px solid $border-subtle;
            cursor: pointer;
            text-align: left;
            transition: background 0.15s ease;

            &:last-child {
                border-bottom: none;
            }

            &:hover {
                background: $bg-hover;

                .result-arrow {
                    opacity: 1;
                    transform: translateX(0);
                }
            }

            .result-info {
                display: flex;
                flex-direction: column;
                gap: 4px;
                overflow: hidden;
            }

            .result-title {
                font-size: 13px;
                font-weight: 500;
                color: $text-primary;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }

            .result-meta {
                display: flex;
                gap: 8px;
                font-size: 11px;
                color: $text-muted;
            }

            .result-time {
                color: $accent-primary;
                font-family: 'JetBrains Mono', monospace;
            }

            .result-date {
                color: $text-muted;
            }

            .result-arrow {
                font-size: 18px;
                width: 18px;
                height: 18px;
                color: $accent-secondary;
                opacity: 0;
                transform: translateX(-4px);
                transition: all 0.15s ease;
            }
        }
    }

    .program-search-empty {
        position: fixed;
        top: 60px;
        right: 16px;
        width: 220px;
        padding: 24px 16px;
        background: $bg-elevated;
        border: 1px solid $border-medium;
        border-radius: 8px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
        z-index: 10000;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8px;
        color: $text-muted;

        mat-icon {
            font-size: 24px;
            width: 24px;
            height: 24px;
            opacity: 0.5;
        }

        span {
            font-size: 12px;
        }
    }

    &.open {
        .program-search-input {
            animation: slideIn 0.2s ease-out;
        }
    }
}

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

@keyframes highlightPulse {
    0%, 100% {
        filter: drop-shadow(0 0 8px rgba(92, 214, 255, 0.6));
    }
    50% {
        filter: drop-shadow(0 0 20px rgba(92, 214, 255, 0.9))
                drop-shadow(0 0 40px rgba(92, 214, 255, 0.4));
    }
}

// No results state
.no-results {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    color: $text-muted;
    text-align: center;
    z-index: 100;

    mat-icon {
        font-size: 48px;
        width: 48px;
        height: 48px;
        opacity: 0.5;
    }

    p {
        margin: 0;
        font-size: 14px;
    }

    button {
        color: $accent-primary;
    }
}

// Today's date display
.today-date {
    flex: 1;
    font-family: 'JetBrains Mono', monospace;
    font-size: 14px;
    font-weight: 500;
    color: $accent-primary;
    text-align: center;
    padding: 0 24px;
    letter-spacing: 0.05em;
    text-transform: uppercase;

    // Glowing text effect
    text-shadow: 0 0 20px rgba(92, 214, 255, 0.3);
}

// Channel column — bumped from 140px to 180px per the UX audit so long
// channel names ("13th Street Universal HD", "ARD alpha", etc.) stop being
// truncated to 5 characters.
#channels-column {
    width: 180px;
    flex: none;
    border-right: 1px solid $border-medium;
    background: $bg-surface;
    position: relative;
    z-index: 10;
    min-height: calc(100vh - 6px);

    // Subtle shadow for depth
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.3);

    .channel {
        cursor: pointer;
        transition: all 0.15s ease;

        rect {
            fill: $bg-surface;
            stroke: $border-subtle;
            transition: fill 0.15s ease;
        }

        &:hover rect {
            fill: $bg-elevated;
        }

        &.active rect {
            fill: rgba(47, 123, 255, 0.2);
            stroke: rgba(47, 123, 255, 0.4);
        }

        &.active .channel-name {
            color: $accent-secondary;
            font-weight: 600;
        }
    }

    .channel-name {
        color: $text-primary;
        padding: 8px 12px;
        font-size: 12.5px;
        font-weight: 500;
        display: flex;
        align-items: center;
        gap: 10px;
        height: 100%;
        overflow: hidden;
        letter-spacing: 0.01em;
        line-height: 1.2;

        img {
            // Smaller logo (24px) per audit recommendation so the channel
            // name has room to breathe in the 180px column.
            width: 24px !important;
            height: 24px !important;
            margin-right: 0 !important;
            flex: 0 0 auto;
            border-radius: 4px;
            background: rgba(255, 255, 255, 0.05);
            padding: 2px;
            object-fit: contain;
        }

        span {
            // Allow up to two lines so "13th Street Universal HD" and
            // similar long names wrap instead of disappearing into an
            // ellipsis after 5 characters.
            min-width: 0;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
            word-break: break-word;
        }
    }

    image {
        object-fit: contain;
        background: $bg-elevated;
    }
}

// EPG container
#epg-container {
    width: calc(100vw - 180px);
    overflow-x: scroll;
    overflow-y: clip;
    background: $bg-deep;
    min-height: calc(100vh - 6px);

    // Custom scrollbar
    &::-webkit-scrollbar {
        height: 8px;
    }

    &::-webkit-scrollbar-track {
        background: $bg-surface;
    }

    &::-webkit-scrollbar-thumb {
        background: rgba(47, 123, 255, 0.4);
        border-radius: 4px;

        &:hover {
            background: rgba(47, 123, 255, 0.6);
        }
    }
}

// EPG SVG container
#epg-svg {
    // Time header styling
    g > rect[fill='#000'] {
        fill: $bg-surface !important;
    }

    g > text {
        font-family: 'JetBrains Mono', monospace !important;
        font-size: 11px !important;
        font-weight: 500;
        fill: $text-secondary !important;
        letter-spacing: 0.1em;
    }

    // Hour separator vertical lines
    .hour-separator {
        stroke: $border-subtle;
        stroke-width: 1px;
        pointer-events: none;
    }

    // Program blocks
    .program {
        cursor: pointer;

        rect {
            fill: $bg-elevated;
            stroke: $border-subtle;
            rx: 4;
            ry: 4;
            transition: all 0.15s ease;
        }

        &:hover rect {
            fill: $bg-hover;
            stroke: rgba(47, 123, 255, 0.4);
            filter: drop-shadow(0 0 8px rgba(47, 123, 255, 0.2));
        }

        // Highlighted program (from search)
        &.highlighted rect {
            fill: rgba(92, 214, 255, 0.25);
            stroke: $accent-primary;
            stroke-width: 2px;
            animation: highlightPulse 1s ease-in-out 3;
        }

        &.highlighted .program-title {
            color: $accent-primary;
            font-weight: 600;
        }
    }

    // Active channel row highlighting
    .active-row .program rect {
        fill: rgba(47, 123, 255, 0.15);
        stroke: rgba(47, 123, 255, 0.3);
    }

    .active-row .program:hover rect {
        fill: rgba(47, 123, 255, 0.25);
    }

    .program-title {
        color: $text-primary;
        padding: 6px 10px;
        font-size: 11px;
        font-weight: 400;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        pointer-events: none;
        letter-spacing: 0.01em;
        line-height: 1.4;

        // Subtle gradient text for depth
        display: flex;
        align-items: center;
        height: 100%;
    }

    // Current time line - glowing effect
    line[stroke='red'] {
        stroke: $accent-primary !important;
        stroke-width: 2px;
        filter: drop-shadow(0 0 8px rgba(92, 214, 255, 0.6))
            drop-shadow(0 0 16px rgba(92, 214, 255, 0.3));

        // Animated pulse
        animation: timePulse 2s ease-in-out infinite;
    }

    // Now-line time badge — a small filled pill sitting on top of the time
    // header that reads "HH:MM". Keeps the cyan visual language of the line
    // itself so the relationship is unmistakable.
    .now-label {
        pointer-events: none;

        rect {
            fill: $accent-primary;
            stroke: none;
            filter: drop-shadow(0 0 6px $accent-now-glow);
        }

        text {
            fill: var(--app-shell-bg, #0d0f12);
            font-family: 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo,
                monospace;
            font-size: 10.5px;
            font-weight: 700;
            letter-spacing: 0.04em;
        }
    }

    // Currently-airing program — a thin cyan border on every program block
    // whose time window contains the now-line. Audit recommended this to
    // make "what's airing right now" scannable without finding the line
    // first then tracing it down each row.
    .program.is-now {
        rect {
            stroke: $accent-primary;
            stroke-width: 1.5px;
        }
    }
}

// Pulse animation for time line
@keyframes timePulse {
    0%,
    100% {
        opacity: 1;
        filter: drop-shadow(0 0 8px rgba(92, 214, 255, 0.6))
            drop-shadow(0 0 16px rgba(92, 214, 255, 0.3));
    }
    50% {
        opacity: 0.8;
        filter: drop-shadow(0 0 12px rgba(92, 214, 255, 0.8))
            drop-shadow(0 0 24px rgba(92, 214, 255, 0.4));
    }
}

// Legacy classes cleanup
.program-item {
    cursor: pointer;
}

rect {
    stroke: $border-subtle;
    cursor: pointer;
}

.channel > rect {
    stroke: $border-subtle;
}

.program-name,
.channel-name {
    -webkit-line-clamp: 2;
    height: 32px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    padding: 6px 8px;
}

#current-time-line {
    stroke-width: 2px;
    stroke: $accent-primary;
}

// Dark theme override (already dark, but ensure consistency)
::ng-deep {
    .dark-theme {
        #epg-navigation {
            background: rgba(18, 18, 26, 0.85);
        }
    }

    // Light theme adjustments
    .light-theme {
        #epg-navigation {
            background: rgba(255, 255, 255, 0.9);
            border-bottom-color: rgba(0, 0, 0, 0.1);

            .today-date {
                color: #059669;
                text-shadow: none;
            }
        }

        .parent {
            background: #f8fafc;

            &::before {
                background-image:
                    linear-gradient(rgba(0, 0, 0, 0.03) 1px, transparent 1px),
                    linear-gradient(
                        90deg,
                        rgba(0, 0, 0, 0.03) 1px,
                        transparent 1px
                    );
            }
        }

        #channels-column {
            background: #ffffff;
            border-right-color: rgba(0, 0, 0, 0.1);
            box-shadow: 4px 0 20px rgba(0, 0, 0, 0.05);

            .channel rect {
                fill: #ffffff;
                stroke: rgba(0, 0, 0, 0.08);
            }

            .channel:hover rect {
                fill: #f1f5f9;
            }

            .channel-name {
                color: #1e293b;
            }
        }

        #epg-container {
            background: #f8fafc;
        }

        #epg-svg {
            g > rect[fill='#000'] {
                fill: #e2e8f0 !important;
            }

            g > text {
                fill: #64748b !important;
            }

            .program {
                rect {
                    fill: #ffffff;
                    stroke: rgba(0, 0, 0, 0.08);
                }

                &:hover rect {
                    fill: #f1f5f9;
                    stroke: rgba(47, 123, 255, 0.4);
                }
            }

            .program-title {
                color: #1e293b;
            }

            line[stroke='red'] {
                stroke: $accent-primary !important;
                filter: drop-shadow(0 0 6px $accent-now-glow);
            }
        }
    }
}

// Responsive adjustments
@media (max-width: 768px) {
    #epg-navigation {
        padding-left: 70px; // Slightly reduced for smaller screens
    }

    #channels-column {
        width: 140px;

        .channel-name {
            font-size: 11.5px;
            padding: 6px 8px;
            gap: 8px;

            img {
                width: 20px !important;
                height: 20px !important;
            }
        }
    }

    #epg-container {
        width: calc(100vw - 140px);
    }

    .today-date {
        font-size: 12px;
        padding: 0 12px;
    }
}
