/* --- Sidebar Width Variable (Overridden by sidebar_resize.js) --- */
:root {
    --sidebar-width: 420px;
}

/* --- Ultra-wide Sidebar Preview --- */
body.view-grid-active #desktopPreviewSidebar,
.view-grid-active #desktopPreviewSidebar {
    display: none !important;
}

/* --- Sidebar Layout --- */
#desktopPreviewSidebar {
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1), margin-right 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

/* [NEW] Force hide sidebar on small screens to prevent layout issues at bottom */
@media (max-width: 768px) {
    #desktopPreviewSidebar,
    .sidebar-toggle-btn {
        display: none !important;
    }
    #sidebarResizer {
        display: none !important;
        pointer-events: none !important;
    }
    body {
        padding-right: 0 !important;
    }
}

/* Sidebar Toggle Tab Button - Refined Design */
.sidebar-toggle-btn {
    position: absolute;
    left: -16px !important;
    /* Slightly closer for a tighter look */
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 80px;
    background: rgba(15, 23, 42, 0.8) !important;
    backdrop-filter: blur(12px) saturate(180%);
    -webkit-backdrop-filter: blur(12px) saturate(180%);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-right: none;
    border-radius: 12px 0 0 12px;
    cursor: pointer;
    display: flex !important;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.7) !important;
    z-index: 2000 !important;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    outline: none;
    padding: 0;
    box-shadow: -4px 0 15px rgba(0, 0, 0, 0.3);
}

.sidebar-toggle-btn:hover {
    background: var(--accent-color, #6366f1) !important;
    color: white !important;
    width: 22px;
    /* Subtle expansion on hover */
    left: -22px !important;
}

.sidebar-toggle-btn:active {
    transform: translateY(-50%) scale(0.95);
}

.toggle-icon {
    font-size: 14px;
    font-weight: 900;
    user-select: none;
    transition: transform 0.3s;
    letter-spacing: -1px;
}

/* Hidden State Logic */
body.sidebar-hidden #desktopPreviewSidebar {
    /* Translate full width to hide but keep button visible (button is at left: -32px) */
    transform: translateX(100%) !important;
}

@media (min-width: 1400px) {
    /* Grid view allows full width, list view remains constrained */
    body:not(.view-grid-active).sidebar-hidden .container {
        margin-right: auto !important;
        max-width: 1400px !important;
    }
}

/* Ensure inner content is clipped since parent is now overflow:visible */
.sidebar-header,
.sidebar-content {
    overflow-x: hidden;
    padding: 0 !important;
    /* Global side padding removal */
}

@media (min-width: 769px) {

    /* Main Layout Adjustment - Only for List View */
    /* [FIX] Body padding = sidebar width → screen is DIVIDED, not overlapped */
    body:not(.view-grid-active):not(.sidebar-hidden) {
        padding-right: var(--sidebar-width, 420px) !important;
        transition: padding-right 0.15s ease;
    }

    body:not(.view-grid-active).sidebar-hidden {
        padding-right: 0px !important;
    }

    body:not(.view-grid-active) .container {
        max-width: none !important;
        margin-left: 10px !important;
        margin-right: 10px !important;
        width: auto !important;
        transition: max-width 0.3s ease;
    }

    /* Sidebar Styling - Only for List View */
    /* [FIX] Use var(--sidebar-width) for dynamic resizing */
    body:not(.view-grid-active) #desktopPreviewSidebar {
        display: flex;
        flex-direction: column;
        position: fixed;
        right: 0;
        top: 0;
        width: var(--sidebar-width, 420px) !important;
        height: 100vh;
        background: var(--glass-bg);
        backdrop-filter: blur(20px);
        border-left: 1px solid var(--glass-border);
        border-radius: 0;
        padding: 0 !important;
        box-shadow: -5px 0 30px rgba(0, 0, 0, 0.3);
        z-index: 2000;
        overflow: visible !important;
        animation: slideInRight 0.3s ease-out;
        transition: width 0.1s ease;
    }

    /* [NEW] Sidebar Resizer Handle - Drag to resize */
    #sidebarResizer {
        position: absolute;
        left: -6px;
        top: 0;
        width: 12px;
        height: 100%;
        cursor: col-resize;
        z-index: 10001 !important;
        background: transparent;
        transition: background 0.2s;
    }

    #sidebarResizer:hover,
    #sidebarResizer.resizing {
        background: rgba(99, 102, 241, 0.4);
        box-shadow: 0 0 8px rgba(99, 102, 241, 0.5);
    }

    .sidebar-title {
        font-size: 0.95rem;
        font-weight: 800;
        margin-bottom: 0 !important;
        /* Removed bottom margin */
        color: #fff;
        white-space: normal;
        overflow: hidden;
        text-overflow: ellipsis;
        border-bottom: 2px solid rgba(255, 255, 255, 0.1);
        padding: 0px 0px !important;
        /* Matching Title Box padding */
        line-height: 1.3;
    }

    .sidebar-content {
        display: flex !important;
        flex-direction: column !important;
        gap: 0 !important;
        padding: 0 !important;
        /* Forced edge-to-edge inside media query */
    }

    .sidebar-image-box {
        display: flex;
        flex-direction: column;
        gap: 2px;
        /* Tightened from 5px */
    }

    .sidebar-image-box>span {
        display: none !important;
        /* Hiding labels to let images fill space */
    }

    .sidebar-image-box img {
        width: 100% !important;
        height: auto !important;
        min-height: 120px !important;
        max-height: 650px !important;
        object-fit: cover !important;
        /* Fill width completely */
        aspect-ratio: auto !important;
        border-radius: 0 !important;
        /* Sharp borders */
        border: none !important;
        box-shadow: none !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .sidebar-image-box {
        margin: 0 !important;
        padding: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        gap: 0 !important;
        width: 100% !important;
        /* Force full width */
    }

    .sidebar-image-box img.music-cover {
        aspect-ratio: 1 / 1 !important;
        object-fit: contain;
    }

    /* [Fix] Specific rule for Preview Image (Video Thumbnail) */
    #sidebarPreviewImg {
        aspect-ratio: 16 / 9 !important;
        object-fit: cover !important;
        /* Stretch or crop to fill 100% width */
        min-height: auto;
    }

    .sidebar-placeholder {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
        color: var(--text-subtitle);
        font-style: italic;
    }

    .sidebar-content.hidden {
        display: none !important;
    }

    /* HIDE the old floating popup tooltip when sidebar is VISIBLE on wide screens */
    body:not(.sidebar-hidden) .preview-tooltip {
        display: none !important;
        opacity: 0 !important;
        pointer-events: none !important;
    }
}

@keyframes slideInRight {
    from {
        transform: translateX(50px);
        opacity: 0;
    }

    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* --- Only Poster Mode Styling --- */
.sidebar-content.only-poster-mode {
    flex: 1;
    /* Changed from height: 100% */
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    /* Ensure scrollable */
}

.only-poster-mode .sidebar-content {
    gap: 2px !important;
    flex: 1;
    height: auto;
}

/* Hide Preview Box by default in this mode */
.only-poster-mode #sidebarPreviewBox {
    display: none !important;
}

/* Make Poster Box Full Width, Height Flexible */
.only-poster-mode #sidebarPosterBox {
    flex: none;
    height: auto;
}

.only-poster-mode .sidebar-image-box:last-child img {
    max-height: calc(100vh - 100px) !important;
    /* Minimized overhead from 200px */
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: transparent;
    border: none;
    box-shadow: none;
}

/* Fallback: Show preview if directed by JS */
.only-poster-mode.show-preview-fallback #sidebarPreviewBox {
    display: flex !important;
    flex: 1;
    height: 100%;
}

.only-poster-mode.show-preview-fallback #sidebarPreviewBox img {
    max-height: calc(100vh - 200px) !important;
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: transparent;
    border: none;
    box-shadow: none;
}

.only-poster-mode.show-preview-fallback #sidebarPosterBox {
    display: none !important;
}

/* Poster Overlay Badges - Refined Precision Styling (10% Compact) */
.poster-overlay {
    position: absolute;
    top: 8px;
    left: 8px;
    right: auto !important;
    display: flex !important;
    flex-wrap: wrap;
    align-items: center;
    gap: 5px;
    z-index: 100 !important;
    pointer-events: none;
    width: max-content;
}

.poster-overlay .rating-badge {
    display: inline-flex !important;
    align-items: center !important;
    gap: 4px;
    padding: 1px 3px !important;
    border-radius: 5px !important;
    font-weight: 800 !important;
    font-size: 1.08rem !important;
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.6) !important;
    line-height: normal !important;
    height: auto !important;
    text-transform: none !important; /* Ratings are not all caps */
}

.rating-badge span:first-child {
    font-size: 0.58rem;
    font-weight: 900;
    text-transform: uppercase;
    opacity: 0.9;
}

.imdb-badge {
    background: #f5c518 !important;
    /* Golden Yellow */
    color: #000 !important;
}

.tmdb-badge {
    background: #01b4e4 !important;
    /* Official TMDB Light Blue */
    color: #fff !important;
}

.poster-overlay .hdr-badge {
    padding: 2px 3px !important;
    border-radius: 5px !important;
    font-size: 0.76rem !important;
    font-weight: 800 !important;
    color: var(--hdr-text, #fff) !important;
    background: var(--hdr-10-bg, #3b82f6) !important;
    text-transform: uppercase !important;
    display: inline-flex !important;
    align-items: center !important;
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.6) !important;
    height: auto !important;
    line-height: normal !important;
}

.hdr-badge.hdr-dv, .hdr-badge.dv, .hdr-badge.DV {
    background: var(--hdr-dv-bg, #a855f7) !important;
}

.hdr-badge.hdr-10plus, .hdr-badge.hdr10plus {
    background: var(--hdr-10plus-bg, #f43f5e) !important;
}

.poster-overlay .sub-badge,
.hdr-badge.sub-badge {
    background: #84cc16 !important; /* Vivid lime / light green */
    color: #052e16 !important; /* Dark green / black for high contrast */
    font-weight: 900 !important;
}



/* Music Metadata Styling */
.sidebar-meta-container {
    /* Removed margin to touch edges */
    padding: 10px 12px !important;
    background: rgba(55, 5, 96, 0.823);
    /* Keep user's preferred color */
    border: none !important;
    border-radius: 0 !important;
    /* Made square to fill width */
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 100% !important;
    margin: 0px 0px 10px !important;
}





/* Sidebar Controls Area - Precision Matching */
.sidebar-controls-area {
    padding: 0px 5px !important;
    /* Minimal side padding */
    margin: 0 !important;
    display: flex;
    flex-direction: column;
}

.sidebar-option {
    display: flex !important;
    align-items: center !important;
    gap: 6px;
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.8);
    cursor: pointer;
    margin-left: 10px !important;
    /* Padding-like margin for text only */
}

.sidebar-row-compact {
    display: flex;
    gap: 8px;
    width: 100%;
    margin-top: 2px;
    padding: 0 10px !important;
    /* Add padding only to controls row */
}

.sidebar-input-compact {
    flex: 0 0 80px;
    /* Narrow for Manual ID */
    background: rgba(20, 20, 20, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    color: #94a3b8;
    padding: 8px 10px;
    font-size: 0.85rem;
}

.sidebar-btn-compact {
    flex: 1;
    background: rgba(30, 30, 30, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    color: #e2e8f0;
    padding: 8px 12px;
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
}

#sidebarAutoFillBtn {
    width: fit-content;
    background: rgba(30, 41, 59, 0.6);
    border: 1px solid rgba(99, 102, 241, 0.4);
    color: #818cf8;
    padding: 6px 12px;
    font-size: 0.85rem;
    font-weight: 700;
}

/* Title Highlight Box */
.sidebar-title-highlight-box {
    margin: 0 !important;
    /* Removed margin to touch edges */
    padding: 10px 12px;
    background: rgba(239, 68, 68, 0.2);
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: 0;
    /* Made square to fill width */
    color: #fff;
    font-weight: 700;
    font-size: 0.95rem;
    line-height: 1.4;
}

/* Sidebar Section Title */
.sidebar-section-title {
    font-size: 0.75rem;
    font-weight: 800;
    color: var(--accent-color, #6366f1);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 8px;
    padding-bottom: 4px;
    border-bottom: 2px solid rgba(99, 102, 241, 0.3);
    width: max-content;
}

/* Overview Section */
.sidebar-overview-container {
    margin: 0 !important;
    /* Removed margin to touch edges */
    padding: 12px;
    background: rgba(54, 79, 119, 0.5);
    backdrop-filter: blur(8px);
    border-radius: 0 !important;
    /* Made square to fill width */
    border: none !important;
    border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
    width: 100% !important;
}

.sidebar-overview-content {
    font-size: 0.9rem;
    line-height: 1.6;
    color: #cdd5df;
    word-break: keep-all;
    overflow-wrap: break-word;
    max-height: 400px;
    overflow-y: auto;
}

/* Sidebar Button Grid (Footer) */
.sidebar-button-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    width: 100%;
    padding: 12px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-meta-item strong {
    color: var(--accent-color);
    font-size: 0.75rem;
    text-transform: uppercase;
    margin-right: 0px;
}

.sidebar-meta-bio {
    font-size: 0.9rem;
    color: var(--text-subtitle);
    line-height: 1.5;
    margin-top: 10px;
    background: rgba(36, 112, 243, 0.419);
    /* Light Blue for Bio/Overview */
    border: 1px solid rgba(59, 130, 246, 0.2);
    padding: 5px;
    border-radius: 12px;
    font-style: normal;
    white-space: pre-line;
}

.sidebar-meta-bio::-webkit-scrollbar {
    width: 6px;
}

.sidebar-meta-bio::-webkit-scrollbar-thumb {
    background: var(--accent-color);
    border-radius: 3px;
}

.sidebar-meta-bio a {
    color: var(--accent-color);
    text-decoration: none;
}

/* Sidebar Tracklist Styling */
.sidebar-tracklist-table {
    margin-top: 15px;
    background: rgba(16, 185, 129, 0.08);
    /* Light Green for Tracks */
    border-radius: 12px;
    padding: 12px;
    border: 1px solid rgba(16, 185, 129, 0.2);
}

.sidebar-tracklist-header {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--accent-color);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding-bottom: 5px;
    border-bottom: 1px solid var(--glass-border);
}

.sidebar-tracklist-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.sidebar-tracklist-content::-webkit-scrollbar {
    width: 4px;
}

.sidebar-tracklist-content::-webkit-scrollbar-thumb {
    background: var(--accent-color);
    border-radius: 2px;
}

.track-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 6px;
    border-radius: 4px;
    font-size: 0.85rem;
    color: var(--text-main);
    transition: background 0.2s;
}

.track-row:hover {
    background: rgba(255, 255, 255, 0.05);
}

.track-row.popular-track {
    color: var(--accent-color);
    background: rgba(236, 72, 153, 0.1);
}

.track-row.popular-track .track-num {
    color: var(--accent-color);
    font-weight: bold;
}

.pop-star {
    font-size: 0.75rem;
    color: #ffd700;
    /* Gold */
    margin-left: 6px;
    vertical-align: middle;
    text-shadow: 0 0 5px rgba(255, 215, 0, 0.5);
}

.track-num {
    width: 20px;
    text-align: right;
    color: var(--text-subtitle);
    font-size: 0.75rem;
}

.track-name {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.track-time {
    font-size: 0.75rem;
    color: var(--text-subtitle);
    width: 35px;
    text-align: right;
}