:root {
    --widget-width: 720px;
    --widget-height: 210px;
    --video-size: 210px;
    --background: #000;
    --border: #fff;
    --primary-text: #fff;
    --secondary-text: rgba(255, 255, 255, 0.7);
    --accent: #fff;
    --progress-background: rgba(255, 255, 255, 0.2);
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    background: transparent;
    font-family: Inter, Arial, Helvetica, sans-serif;
}

body {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* -----------------------------
   MAIN WIDGET ANIMATION
------------------------------ */

.now-playing-widget {
    position: relative;
    display: grid;
    grid-template-columns: var(--video-size) 1fr;
    width: var(--widget-width);
    height: var(--widget-height);
    overflow: hidden;
    color: var(--primary-text);
    background: var(--background);
    /*
        Keep the normal top, bottom, and final
        right border on the widget.
    */
    border: 2px solid var(--border);
    border-radius: 6px;
    box-shadow: none;
    opacity: 0;
    visibility: hidden;
    /*
        Hide the entire widget from right to left.
    */
    clip-path: inset(0 100% 0 0);
}

/*
    LEFT BORDER

    This is separate from the widget so it can draw
    before the clipped widget becomes visible.
*/
body::before {
    content: "";
    position: fixed;
    left: calc(50% - (var(--widget-width) / 2));
    top: calc(50% - (var(--widget-height) / 2));
    width: 2px;
    height: var(--widget-height);
    background: var(--border);
    border-radius: 2px;
    opacity: 0;
    transform: scaleY(0);
    transform-origin: center;
    pointer-events: none;
    z-index: 100;
}

/*
    MOVING RIGHT BORDER

    This line travels from the left side to the right
    side while the rectangle expands.
*/
body::after {
    content: "";
    position: fixed;
    left: calc(50% - (var(--widget-width) / 2));
    top: calc(50% - (var(--widget-height) / 2));
    width: 2px;
    height: var(--widget-height);
    background: var(--border);
    border-radius: 2px;
    opacity: 0;
    transform: translateX(0);
    pointer-events: none;
    z-index: 101;
}

/*
    Stage 1:
    Draw the permanent left border.
*/
body.widget-revealing::before {
    animation: drawLeftBorder 0.32s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

/*
    Stage 2:
    Move the temporary right border across the
    leading edge of the expanding rectangle.
*/
body.widget-revealing::after {
    animation: moveRightBorder 0.62s 0.36s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

/*
    Reveal the main rectangle at the same speed
    as the moving right border.
*/
.now-playing-widget.reveal-animation {
    visibility: visible;
    animation: revealWidget 0.62s 0.36s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

/*
    Final resting state.
*/
.now-playing-widget.ready {
    opacity: 1;
    visibility: visible;
    clip-path: inset(0 0 0 0);
}

/*
    Hide the temporary animation lines once the
    reveal has completed.
*/
body.widget-ready::before,
body.widget-ready::after {
    opacity: 0;
}

/* -----------------------------
   CONTENT FADE-IN
------------------------------ */

.video-frame,
.track-information {
    opacity: 0;
}

.now-playing-widget.reveal-animation .video-frame {
    animation: revealContent 0.28s 0.72s ease-out forwards;
}

.now-playing-widget.reveal-animation .track-information {
    animation: revealInformation 0.32s 0.78s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.now-playing-widget.ready .video-frame,
.now-playing-widget.ready .track-information {
    opacity: 1;
    transform: translateX(0);
}

/* -----------------------------
   KEYFRAMES
------------------------------ */

@keyframes drawLeftBorder {
    0% {
        opacity: 1;
        transform: scaleY(0);
    }

    100% {
        opacity: 1;
        transform: scaleY(1);
    }
}

@keyframes moveRightBorder {
    0% {
        opacity: 1;
        transform: translateX(0);
    }

    99% {
        opacity: 1;
        transform: translateX( calc(var(--widget-width) - 2px) );
    }

    100% {
        opacity: 0;
        transform: translateX( calc(var(--widget-width) - 2px) );
    }
}

@keyframes revealWidget {
    0% {
        opacity: 1;
        clip-path: inset(0 100% 0 0);
    }

    100% {
        opacity: 1;
        clip-path: inset(0 0 0 0);
    }
}

@keyframes revealContent {
    0% {
        opacity: 0;
        transform: scale(1.04);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes revealInformation {
    0% {
        opacity: 0;
        transform: translateX(-18px);
    }

    100% {
        opacity: 1;
        transform: translateX(0);
    }
}
/* -----------------------------
   END-OF-SONG ANIMATION
------------------------------ */

/*
    Collapse the rectangle toward its right edge.
*/
.now-playing-widget.hide-animation {
    opacity: 1;
    visibility: visible;
    clip-path: inset(0 0 0 0);
    animation: collapseToRight 0.55s cubic-bezier(0.65, 0, 0.35, 1) forwards;
}

    /*
    Fade the thumbnail and information slightly
    before the rectangle finishes collapsing.
*/
    .now-playing-widget.hide-animation .video-frame,
    .now-playing-widget.hide-animation .track-information {
        animation: hideContent 0.22s ease-in forwards;
    }

/*
    Hide the left entrance line during the outro.
*/
body.widget-hiding::before {
    opacity: 0;
}

/*
    Start the temporary line at the right side.

    It stays there while the rectangle collapses,
    then slides back to the left.
*/
body.widget-hiding::after {
    opacity: 1;
    animation: returnBorderToLeft 1.05s cubic-bezier(0.65, 0, 0.35, 1) forwards;
}

/*
    Once the border reaches the left side, keep it
    visible while the next video begins loading.
*/
body.widget-waiting::after {
    opacity: 1;
    transform: translateX(0);
}

/* -----------------------------
   OUTRO KEYFRAMES
------------------------------ */

@keyframes collapseToRight {
    0% {
        opacity: 1;
        clip-path: inset(0 0 0 0);
    }

    100% {
        opacity: 1;
        /* The separate body::after line draws the moving border. */
        clip-path: inset(0 0 0 100%);
    }
}

@keyframes hideContent {
    0% {
        opacity: 1;
        transform: translateX(0);
    }

    100% {
        opacity: 0;
        transform: translateX(12px);
    }
}

@keyframes returnBorderToLeft {
    /*
        Begin on the right border.
    */
    0% {
        opacity: 1;
        transform: translateX( calc(var(--widget-width) - 2px) );
    }
    /*
        Stay on the right while the rectangle
        collapses into it.
    */
    52% {
        opacity: 1;
        transform: translateX( calc(var(--widget-width) - 2px) );
    }
    /*
        Slide back to the starting left position.
    */
    100% {
        opacity: 1;
        transform: translateX(0);
    }
}

/* -----------------------------
   THUMBNAIL AREA
------------------------------ */

.video-frame {
    position: relative;
    width: var(--video-size);
    height: 100%;
    overflow: hidden;
    flex-shrink: 0;
    background: #000;
    border-right: 2px solid var(--border);
}

#youtube-player,
#youtube-player iframe {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 374px;
    height: 210px;
    transform: translate(-50%, -50%);
    border: 0;
}

.video-thumbnail {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 2;
    pointer-events: none;
    transition: opacity 0.35s ease, transform 0.45s ease;
}

    .video-thumbnail.changing {
        opacity: 0;
        transform: scale(1.05);
    }

.video-shade {
    display: none;
}

/* -----------------------------
   TRACK INFORMATION
------------------------------ */

.track-information {
    min-width: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 26px 30px;
}

.now-playing-label {
    display: flex;
    align-items: center;
    gap: 9px;
    margin-bottom: 13px;
    color: var(--primary-text);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.25em;
}

.music-note {
    color: var(--primary-text);
    font-size: 18px;
    animation: none;
}

.title-container {
    width: 100%;
    overflow: hidden;
}

#track-title {
    width: max-content;
    max-width: 100%;
    margin: 0;
    overflow: hidden;
    color: var(--primary-text);
    font-size: 27px;
    font-weight: 800;
    line-height: 1.15;
    text-overflow: ellipsis;
    white-space: nowrap;
}

    #track-title.marquee {
        max-width: none;
        padding-right: 70px;
        animation: titleScroll 14s linear infinite;
    }

#track-artist {
    margin: 8px 0 25px;
    overflow: hidden;
    color: var(--secondary-text);
    font-size: 16px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* -----------------------------
   PROGRESS BAR
------------------------------ */

.progress-section {
    display: grid;
    grid-template-columns: 42px 1fr 42px;
    align-items: center;
    gap: 11px;
    color: var(--secondary-text);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.progress-track {
    height: 5px;
    overflow: hidden;
    background: var(--progress-background);
    border-radius: 999px;
}

.progress-fill {
    width: 0%;
    height: 100%;
    background: var(--accent);
    border-radius: inherit;
    transition: width 0.2s linear;
}

/* -----------------------------
   ERROR DISPLAY
------------------------------ */

.error-message {
    max-width: 700px;
    padding: 16px 20px;
    color: #ffd6d6;
    background: #000;
    border: 2px solid #fff;
    border-radius: 6px;
}

/* -----------------------------
   TITLE SCROLL
------------------------------ */

@keyframes titleScroll {
    0%, 12% {
        transform: translateX(0);
    }

    88%, 100% {
        transform: translateX(calc(-100% + 440px));
    }
}

.now-playing-widget.hide-animation {
    border-right-color: transparent;
}

    .now-playing-widget.hide-animation .video-frame {
        border-right-color: transparent;
    }

/* -----------------------------
   NEXT-SONG ENTRANCE

   The returned line is already full height. Keep it
   stationary as the left border while the widget
   expands outward from it.
------------------------------ */

/* Keep the full-height returned line visible at left. */
body.widget-song-revealing::before {
    opacity: 1;
    transform: scaleY(1);
}

/* Use the second line as the moving leading edge. */
body.widget-song-revealing::after {
    animation: moveRightBorder 0.62s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

/* Expand the next song's box from the stationary line. */
.now-playing-widget.song-reveal-animation {
    visibility: visible;
    animation: revealWidget 0.62s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

    .now-playing-widget.song-reveal-animation .video-frame {
        animation: nextSongThumbnail 0.28s 0.34s ease-out forwards;
    }

    .now-playing-widget.song-reveal-animation .track-information {
        animation: nextSongInformation 0.34s 0.38s cubic-bezier(0.22, 1, 0.36, 1) forwards;
    }

@keyframes nextSongThumbnail {
    0% {
        opacity: 0;
        transform: scale(1.04);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes nextSongInformation {
    0% {
        opacity: 0;
        transform: translateX(-14px);
    }

    100% {
        opacity: 1;
        transform: translateX(0);
    }
}
