.table-wrapper {
    position: relative;
    overflow-x: auto;
    overflow-y: visible; /* CRITICAL: Ensure vertical overflow is visible for posters */
    background: #fff;
    border-radius: 12px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border-color);
    margin-bottom: 2rem;
    scrollbar-width: none;
    -ms-overflow-style: none;
    min-height: 500px; /* Increased slightly to ensure enough space for posters in single-movie mode */
}

.table-wrapper::-webkit-scrollbar {
    display: none;
}

/* Sticky horizontal scrollbar (replaces the hidden native one) */
.sticky-scrollbar-container {
    position: sticky;
    bottom: 0;
    left: 0;
    right: 0;
    height: 20px;
    overflow-x: auto;
    overflow-y: hidden;
    z-index: 200;
    background: #f5f5f5;
    border-top: 1px solid var(--border-color);
    scrollbar-width: thin;
    scrollbar-color: #667eea #e9ecef;
    scroll-behavior: auto;
}

.sticky-scrollbar-container::-webkit-scrollbar {
    height: 12px;
}

.sticky-scrollbar-container::-webkit-scrollbar-track {
    background: #e9ecef;
}

.sticky-scrollbar-container::-webkit-scrollbar-thumb {
    background: var(--bg-gradient);
    border-radius: 6px;
    border: 2px solid #e9ecef;
}

.sticky-scrollbar-content {
    height: 1px;
}

table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
}

/* Sticky Headers */
thead {
    background: #f8f9fa;
    position: relative;
    z-index: 200; /* Stacking context for headers */
}

/* Base th: sticky to top */
th {
    padding: 0.625rem 0.5rem;
    text-align: left;
    font-weight: 600;
    color: #495057;
    font-size: 0.85em;
    max-width: 8.5em;
    word-wrap: break-word;
    line-height: 1.3;
    vertical-align: top;
    background: #f8f9fa;
    transition: background-color 0.35s ease;
    /* position: sticky removed to avoid conflicts with JS sticky header */
    z-index: 100; /* Relative to thead */
    border-bottom: 0.125rem solid #dee2e6;
    box-shadow: 0 1px 0 rgba(0,0,0,0.05);
}

td {
    padding: 1rem 0.5rem;
    vertical-align: top;
    border-bottom: 1px solid var(--border-color);
}

/* Movie column (first column) */
.movie-col {
    width: 22rem;
    min-width: 22rem;
    max-width: 22rem;
    position: sticky;
    left: 0;
    white-space: nowrap;
    user-select: none;
    transition: background-color 0.2s;
    text-align: center;
    vertical-align: middle;
    font-family: 'Afacad', sans-serif;
}

/* thead first cell (Corner intersection) */
th.movie-col.corner-cell {
    z-index: 210 !important; /* Highest priority: above both top-sticky and left-sticky */
    background: #f8f9fa !important;
    padding: 0 !important;
    border-bottom: 0.125rem solid #dee2e6 !important;
    border-right: 0.125rem solid #e9ecef !important;
    height: 56px;
}

.corner-content {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    padding-left: 0.625rem;
    height: 100%;
    width: 100%;
}

.corner-label {
    font-size: 0.75rem;
    font-weight: 700;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* tbody first cells */
td.movie-col {
    color: #212529;
    background-color: white; /* Base background to mask scrolling content */
    border-right: 0.125rem solid #e9ecef;
    padding: 0.75rem 0.625rem;
    white-space: normal;
    word-wrap: break-word;
    z-index: 50; /* Below thead (200), but above regular rating cells */
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.05);
    font-size: 1.0em;
    text-align: left;
    font-weight: 600;
    transition: all 0.25s ease;
    overflow: visible;
}

/* Movie content wrapper - flexbox layout for thumbnail + text */
.movie-content-wrapper {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    position: relative;
}

/* Poster container - holds both thumbnail and full-size */
.movie-poster-container {
    position: relative;
    flex-shrink: 0;
    width: 48px;
    height: 72px; /* 2:3 aspect ratio */
}

/* Mini thumbnail - always visible */
.movie-poster-thumb {
    width: 48px;
    height: 72px;
    object-fit: cover;
    border-radius: 4px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
    display: block;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

/* Full-size poster - hidden by default, appears on hover with delay */
.movie-poster-full {
    position: absolute;
    top: 0;
    left: 100%; /* Position to the right of thumbnail */
    margin-left: 12px; /* Space from thumbnail */
    width: 200px;
    height: auto;
    object-fit: cover;
    border-radius: 8px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
    opacity: 0;
    visibility: hidden;
    z-index: 300; /* Above everything */
    background: white;
    padding: 4px;
    pointer-events: none; /* Don't block mouse events */
    transition: opacity 0.2s ease, visibility 0s linear 0.2s; /* Instant hide when transition ends */
}

/* Show full poster on hover after delay */
.movie-poster-container:hover .movie-poster-full {
    opacity: 1;
    visibility: visible;
    transition-delay: 0.6s, 0.6s; /* Delay before appearing */
}

/* Subtle scale on thumbnail hover */
.movie-poster-container:hover .movie-poster-thumb {
    transform: scale(1.05);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
}

/* Movie info section - title and meta */
.movie-info {
    flex: 1;
    min-width: 0; /* Allow text to shrink */
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}

/* Hover effects */
th.movie-col:hover {
    background: #e9ecef;
    cursor: pointer;
}

td.movie-col:hover {
    background: linear-gradient(135deg, #e8d5ff 0%, #d4bbff 100%) !important;
    cursor: pointer;
    font-weight: 600;
    z-index: 60; /* Below thead (200) */
    overflow: visible !important;
}

/* Ensure poster full-size appears above the hovered cell and row doesn't clip */
tbody tr:hover {
    z-index: 70;
    position: relative; /* Create stacking context for the row */
}

tbody tr:hover td.movie-col {
    z-index: 80;
    overflow: visible !important;
}

td.movie-col:hover .movie-poster-full {
    z-index: 310; /* Above the hovered cell */
}

/* Zebra striping */
tbody tr {
    border-bottom: 0.0625rem dashed #666;
    transition: background-color 0.15s;
}

tbody tr:nth-child(even) td {
    background-color: #fcfcfc;
}

tbody tr td:nth-child(even) {
    background-color: #f8faff;
}

/* Intersection of even row and even column */
tbody tr:nth-child(even) td:nth-child(even) {
    background-color: #f0f4ff;
}

tbody tr:hover td {
    background-color: #f0ebff !important;
}

tbody tr:hover td.movie-col {
    background: linear-gradient(135deg, #e8d5ff 0%, #d4bbff 100%) !important;
}

/* Movie title in table */
.movie-title-main {
    font-weight: 600;
    line-height: 1.3;
}

/* Critic header details */
.critic-name {
    white-space: normal;
    word-wrap: break-word;
    line-height: 1.2;
}

.critic-media {
    display: block;
    font-size: 0.7em;
    font-weight: normal;
    color: #6c757d;
    margin-top: 0.25rem;
}

.critic-bottom-row {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 0.35rem;
    padding-top: 0.25rem;
    border-top: 1px solid #e9ecef;
    gap: 0.25rem;
}

.review-count {
    display: inline-block;
    font-size: 0.7em;
    font-weight: normal;
    color: #999;
    margin-left: 0.35rem;
    opacity: 0.9;
}

th .review-count {
    display: inline-block;
    font-size: 0.7em;
    margin-top: 0;
    margin-left: 0;
    padding-top: 0;
    border-top: none;
    color: #999;
    text-align: center;
}

/* Rating Cells */
.rating-cell {
    text-align: center;
    width: 7.5em;
    min-width: 7.5em;
    cursor: pointer;
    transition: background-color 0.2s ease;
    padding: 0.5rem 0.2rem;
    vertical-align: middle;
}

th.rating-cell {
    vertical-align: bottom;
}

th.rating-cell:hover {
    background: rgba(139, 92, 246, 0.2);
    cursor: pointer;
}

.rating-link {
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    color: inherit;
    min-height: 2.5rem;
    height: 100%;
}

.no-rating {
    color: #adb5bd;
    font-size: 0.85em;
    font-style: italic;
}

/* Pilli rajautuu gradientilla eikä reunuksella: sävy syvenee alaspäin, joten
   alareuna piirtyy portaan omasta väristä. Kullalla tämä lukee metallina. */
.rating-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.35em 0.5em;
    border-radius: 1em;
    font-weight: 500;
    line-height: 1;
    text-decoration: none;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    box-shadow: 0 1px 2px rgba(24, 16, 4, 0.14);
}

/* Ei brightness-suodinta: se pesisi kirkkaan kullan lähes valkoiseksi */
.rating-pill:hover {
    transform: scale(1.08);
    box-shadow: 0 3px 8px rgba(24, 16, 4, 0.24);
}

.rating-pill .rating-stars {
    color: inherit;
    font-size: 0.9em;
    letter-spacing: 0.05rem;
    display: inline-block;
}

/* Tähdet ovat aina valkoisia. Väri toistetaan :hover/:focus-tiloissa, koska
   pilli on <a> ja base.css:n yleinen a:hover (0,1,1) voittaisi muuten
   .rating-pillin (0,1,0) ja värjäisi tähdet violetiksi. */
.rating-pill,
.rating-pill:hover,
.rating-pill:focus,
.rating-pill:visited { color: var(--star-ink); }

/* Kolme vaaleinta porrasta: ääriviiva antaa valkoiselle tähdelle reunan.
   Kahdella tummalla portaalla sitä ei tarvita — tumma ääriviiva tummalla
   pohjalla ei näkyisi, ja se vain paksuntaisi merkkiä. */
.stars-3 .rating-stars,
.stars-4 .rating-stars,
.stars-5 .rating-stars {
    text-shadow:
        0 0 1px var(--star-outline),
        0 0 2px var(--star-outline),
        0 1px 1px rgba(69, 26, 3, 0.55);
}

.stars-1 { background: linear-gradient(180deg, var(--star-1-top), var(--star-1) 55%, var(--star-1-bot)); }
.stars-2 { background: linear-gradient(180deg, var(--star-2-top), var(--star-2) 55%, var(--star-2-bot)); }
.stars-3 { background: linear-gradient(180deg, var(--star-3-top), var(--star-3) 55%, var(--star-3-bot)); }
.stars-4 { background: linear-gradient(180deg, var(--star-4-top), var(--star-4) 55%, var(--star-4-bot)); }
.stars-5 { background: linear-gradient(180deg, var(--star-5-top), var(--star-5) 55%, var(--star-5-bot)); }

.movie-review-count {
    font-size: 0.75em;
    color: #888;
    margin-top: 0;
    font-weight: 500;
    opacity: 0.9;
    white-space: nowrap;
}

/* Sparkline Styles */
.movie-meta-row {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: nowrap;
    gap: 12px;
    margin-top: 6px;
}

#review-container-desktop .movie-meta-row .movie-link,
#review-container-desktop .movie-links .movie-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.18rem 0.45rem;
    border-radius: 0.375rem;
    text-decoration: none;
    font-size: 0.72em;
    font-weight: 500;
    line-height: 1.15;
    border: 1px solid transparent;
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

#review-container-desktop .movie-meta-row .nytleffaan-link,
#review-container-desktop .movie-links .nytleffaan-link {
    background: #eefaf2;
    color: #1f7a40;
    border-color: #bfe8ce;
}

#review-container-desktop .movie-meta-row .letterboxd-link,
#review-container-desktop .movie-links .letterboxd-link {
    background: #f3efff;
    color: #5b3aa8;
    border-color: #d4c7f7;
}

#review-container-desktop .movie-meta-row .nytleffaan-link:hover,
#review-container-desktop .movie-meta-row .nytleffaan-link:focus,
#review-container-desktop .movie-links .nytleffaan-link:hover,
#review-container-desktop .movie-links .nytleffaan-link:focus {
    background: #e1f4e8;
    color: #186636;
}

#review-container-desktop .movie-meta-row .letterboxd-link:hover,
#review-container-desktop .movie-meta-row .letterboxd-link:focus,
#review-container-desktop .movie-links .letterboxd-link:hover,
#review-container-desktop .movie-links .letterboxd-link:focus {
    background: #ebe4ff;
    color: #4c2e97;
}

/* Arvosanajakauma. Palkin KORKEUS kertoo muodon: onko mielipide jakautunut vai
   yksimielinen. Korkeus on suhteutettu suurimpaan palkkiin, joten muoto on
   vertailukelpoinen elokuvien välillä riippumatta arvostelujen määrästä —
   määrä näkyy erillisestä laskurista.

   Palkit ovat yksivärisiä tarkoituksella: arvosanan kertoo palkin paikka, ja
   väriasteikko on jo samalla rivillä arvosanapilleissä. Harmaa lokero palkin
   takana pitää kaikki viisi arvosanaa näkyvissä, joten myös tyhjät erottuvat. */
.movie-sparkline {
    display: flex;
    align-items: flex-end;
    gap: 2px;
    height: 22px;
    padding: 0;
    background: transparent;
    border-bottom: 1px solid var(--spark-axis);
    flex-shrink: 0;
    margin-bottom: 2px; /* Pieni säätö tekstin linjaan */
}

.spark-bar-container {
    width: 6px;
    height: 100%;
    display: flex;
    align-items: flex-end;
    background: var(--spark-track);
    border-radius: 1px;
}

.spark-bar {
    width: 100%;
    border-radius: 1px 1px 0 0; /* Vain yläkulmat pyöristetty */
    background: linear-gradient(to top, var(--spark-bar-bot), var(--spark-bar-top));
    transition: height 0.3s ease;
}

/* Tyhjä arvosana jättää lokeron kokonaan näkyviin — harmaa kertoo puuttumisen */
.spark-bar--empty {
    opacity: 0;
}

.movie-sparkline:hover .spark-bar {
    filter: brightness(1.15);
}



/* Tablet / small laptop: narrower columns so the table needs less sideways travel */
@media (min-width: 769px) and (max-width: 1024px) {
    .movie-col {
        width: 14rem;
        min-width: 14rem;
        max-width: 14rem;
    }

    .rating-cell {
        width: 4.5em;
        min-width: 4.5em;
    }

    .critic-media {
        display: none;
    }
}

@media (max-width: 768px) {
    .table-wrapper {
        display: none !important;
    }

    .sticky-scrollbar-container {
        display: none !important;
    }
}
