/* =========================================
   1. VARIABLES & CONTAINER
   ========================================= */
:root {
    --manga-star-gold: #ff9e0b;
    --manga-star-empty: #444; /* Dark gray for contrast */
    --manga-star-focus: #2196F3; /* Blue focus ring */
    --manga-error: #ff4444;
    --manga-text: #eee;
    --manga-bg-hover: rgba(255, 255, 255, 0.05);
}

.manga-rating-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    width: 100%;
    position: relative;
    /* CLS PROTECTION: Matches PHP inline styles */
    min-height: 90px;
    contain: content;
}

/* =========================================
   2. TEXT & META
   ========================================= */
.rating-meta {
    font-size: 0.85rem;
    letter-spacing: 1px;
    font-weight: 600;
    color: #999;
    margin-bottom: 5px;
    text-transform: uppercase;
    text-align: center;
    line-height: 1.4;
}

.rating-text {
    font-size: 0.8rem;
    color: var(--manga-text);
    margin: 5px 0 0 0;
}

/* =========================================
   3. STAR RATING SYSTEM
   ========================================= */
.radio {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 2px; 
    
    /* CRITICAL: Visual 1-5 order vs DOM 5-1 order */
    flex-direction: row-reverse; 
}

/* ACCESSIBILITY: SR-ONLY pattern (Do not use display:none) */
.radio > input {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.radio > label {
    cursor: pointer;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    
    /* WCAG COMPLIANCE: Minimum touch target 44x44px */
    width: 44px; 
    height: 44px; 
    
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    -webkit-tap-highlight-color: transparent;
    border-radius: 50%; /* For focus ring shape */
}

/* THE STAR ICON (Visual size remains 30px inside 44px box) */
.radio > label > svg {
    width: 30px;
    height: 30px;
    fill: var(--manga-star-empty);
    transition: fill 0.2s ease, filter 0.2s ease;
    pointer-events: none;
}

/* =========================================
   4. INTERACTION STATES
   ========================================= */

/* A. KEYBOARD FOCUS (Accessibility) */
.radio > input:focus-visible + label {
    outline: 2px solid var(--manga-star-focus);
    outline-offset: -2px;
    background: var(--manga-bg-hover);
}

/* B. CHECKED STATE */
/* Selects the clicked star AND all subsequent siblings (visually previous) */
.radio > input:checked + label > svg,
.radio > input:checked ~ label > svg {
    fill: var(--manga-star-gold);
}

/* C. HOVER OVERRIDE (Reset) */
/* When hovering the container, gray out "checked" stars so hover takes precedence */
@media (hover: hover) {
    .radio:hover > input:checked + label > svg,
    .radio:hover > input:checked ~ label > svg {
        fill: var(--manga-star-empty);
    }
}

/* D. ACTIVE HOVER */
/* Color the hovered star AND all visual predecessors */
.radio > label:hover > svg,
.radio > label:hover ~ label > svg {
    fill: var(--manga-star-gold) !important;
    filter: drop-shadow(0 0 6px rgba(255, 158, 11, 0.5));
}

/* E. SCALE ANIMATION */
.radio > label:hover {
    transform: scale(1.15);
    z-index: 2;
}

/* =========================================
   5. LOADING & FEEDBACK STATES
   ========================================= */

/* LOADING: Pulsing effect */
#manga-rating-form.is-loading {
    opacity: 0.6;
    pointer-events: none;
    animation: pulse-opacity 1.5s infinite;
}

@keyframes pulse-opacity {
    0% { opacity: 0.6; }
    50% { opacity: 0.3; }
    100% { opacity: 0.6; }
}

/* ERROR MESSAGE */
.rating-error {
    color: var(--manga-error);
    font-size: 0.85rem;
    font-weight: 700;
    margin-top: 10px;
    background: rgba(255, 68, 68, 0.1);
    padding: 8px 12px;
    border-radius: 4px;
    border-left: 3px solid var(--manga-error);
    animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes shake {
    10%, 90% { transform: translate3d(-1px, 0, 0); }
    20%, 80% { transform: translate3d(2px, 0, 0); }
    30%, 50%, 70% { transform: translate3d(-4px, 0, 0); }
    40%, 60% { transform: translate3d(4px, 0, 0); }
}

/* SUCCESS FEEDBACK */
.rating-feedback {
    text-align: center;
    color: var(--manga-text);
    width: 100%;
    animation: fadeIn 0.4s ease-out;
}

/* Static stars for success message */
.stars-static {
    display: flex;
    justify-content: center;
    margin-bottom: 8px;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

/* =========================================
   6. WIDGET STYLING
   ========================================= */
.manga-widget-item {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    padding-bottom: 12px;
}

/* Remove border from last item */
.manga-widget-item:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.manga-rank {
    font-size: 1.1rem;
    font-weight: 900;
    color: #666; /* Subtle rank color */
    min-width: 25px;
    text-align: center;
}

/* FIX v2.7.6: Thumbnail container with fixed dimensions */
.manga-thumb {
    display: block;
    flex-shrink: 0;
    width: 50px;
    height: 50px;
    overflow: hidden;
    border-radius: 6px;
    background: #333; /* Placeholder color while loading */
}

/* FIX v2.7.6: Handle any WordPress thumbnail size (typically 150x150)
   Scale down to fit 50x50 container while maintaining aspect ratio */
.manga-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.manga-info {
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-width: 0; /* Allow text truncation */
}

.manga-title {
    font-weight: 700;
    text-decoration: none;
    color: var(--manga-text);
    font-size: 0.95rem;
    line-height: 1.3;
    transition: color 0.2s;
    /* Prevent long titles from breaking layout */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.manga-title:hover {
    color: var(--manga-star-gold);
}

.manga-meta {
    font-size: 0.8rem;
    margin-top: 4px;
    color: #999;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* UTILITY: Screen Reader Text */
.screen-reader-text {
    border: 0;
    clip: rect(1px, 1px, 1px, 1px);
    clip-path: inset(50%);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    word-wrap: normal !important;
}

/* =========================================
   8. TOP RATED GRID LAYOUT (v2.7.6)
   Shortcode: [manga_top_rated_grid]
   ========================================= */

.manga-grid-wrapper {
    width: 100%;
    margin: 30px 0;
}

.manga-grid-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--manga-text);
    margin: 0 0 20px 0;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--manga-star-gold);
}

.manga-grid-empty {
    text-align: center;
    color: #888;
    padding: 40px 20px;
}

.manga-grid {
    display: grid;
    grid-template-columns: repeat(var(--manga-grid-columns, 5), 1fr);
    gap: 20px;
}

/* Responsive grid columns */
@media (max-width: 1200px) {
    .manga-grid {
        grid-template-columns: repeat(min(var(--manga-grid-columns, 5), 4), 1fr);
    }
}

@media (max-width: 900px) {
    .manga-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 15px;
    }
}

@media (max-width: 600px) {
    .manga-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
    }
}

@media (max-width: 380px) {
    .manga-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }
}

.manga-grid-item {
    position: relative;
}

.manga-grid-link {
    display: block;
    text-decoration: none;
    color: inherit;
    transition: transform 0.2s ease;
}

.manga-grid-link:hover {
    transform: translateY(-4px);
}

.manga-grid-link:focus-visible {
    outline: 2px solid var(--manga-star-focus);
    outline-offset: 4px;
    border-radius: 8px;
}

/* Thumbnail container - 150x150 with aspect ratio lock */
.manga-grid-thumb {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    overflow: hidden;
    border-radius: 8px;
    background: #222;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: box-shadow 0.2s ease;
}

.manga-grid-link:hover .manga-grid-thumb {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.manga-grid-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.3s ease;
}

.manga-grid-link:hover .manga-grid-thumb img {
    transform: scale(1.05);
}

/* Placeholder for missing thumbnails */
.manga-grid-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #333 0%, #222 100%);
    color: #555;
}

/* Rank badge - top left */
.manga-grid-rank {
    position: absolute;
    top: 8px;
    left: 8px;
    background: rgba(0, 0, 0, 0.85);
    color: var(--manga-star-gold);
    font-size: 0.75rem;
    font-weight: 900;
    padding: 4px 8px;
    border-radius: 4px;
    z-index: 2;
    backdrop-filter: blur(4px);
}

/* Top 3 special styling */
.manga-grid-item:nth-child(1) .manga-grid-rank {
    background: linear-gradient(135deg, #ffd700 0%, #ffaa00 100%);
    color: #000;
}

.manga-grid-item:nth-child(2) .manga-grid-rank {
    background: linear-gradient(135deg, #c0c0c0 0%, #a0a0a0 100%);
    color: #000;
}

.manga-grid-item:nth-child(3) .manga-grid-rank {
    background: linear-gradient(135deg, #cd7f32 0%, #a0522d 100%);
    color: #fff;
}

/* Rating badge - bottom right */
.manga-grid-rating {
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.85);
    color: #fff;
    font-size: 0.8rem;
    font-weight: 700;
    padding: 4px 8px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    gap: 4px;
    z-index: 2;
    backdrop-filter: blur(4px);
}

.manga-grid-star {
    color: var(--manga-star-gold);
    font-size: 0.9rem;
}

.manga-grid-avg {
    color: #fff;
}

/* Title container below thumbnail - CLS Protected */
.manga-grid-item-title {
    /* CLS PROTECTION: Fixed height container */
    height: 52px;
    min-height: 52px;
    margin: 12px 0 0 0;
    padding: 0 4px;
    
    /* Flexbox layout for icon + text */
    display: flex;
    align-items: flex-start;
    justify-content: center;
    gap: 6px;
    
    /* Contain layout shifts */
    contain: layout style;
}

/* Animated star icon */
.manga-grid-title-icon {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    margin-top: 2px;
    fill: var(--manga-star-gold);
    filter: drop-shadow(0 0 3px rgba(255, 158, 11, 0.4));
    transition: transform 0.3s ease, filter 0.3s ease;
}

.manga-grid-link:hover .manga-grid-title-icon {
    transform: rotate(72deg) scale(1.2);
    filter: drop-shadow(0 0 6px rgba(255, 158, 11, 0.7));
}

/* Title text with modern styling */
.manga-grid-title-text {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--manga-text);
    line-height: 1.35;
    text-align: left;
    
    /* Two-line clamp with ellipsis */
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    
    /* Smooth color transition */
    transition: color 0.2s ease;
}

.manga-grid-link:hover .manga-grid-title-text {
    color: var(--manga-star-gold);
}

/* Responsive title adjustments */
@media (max-width: 600px) {
    .manga-grid-item-title {
        height: 48px;
        min-height: 48px;
        margin-top: 10px;
        gap: 5px;
    }
    
    .manga-grid-title-icon {
        width: 14px;
        height: 14px;
    }
    
    .manga-grid-title-text {
        font-size: 0.85rem;
    }
}

@media (max-width: 380px) {
    .manga-grid-item-title {
        height: 44px;
        min-height: 44px;
        margin-top: 8px;
        gap: 4px;
    }
    
    .manga-grid-title-icon {
        width: 12px;
        height: 12px;
        margin-top: 1px;
    }
    
    .manga-grid-title-text {
        font-size: 0.8rem;
        line-height: 1.3;
    }
}

/* =========================================
   9. AUTO-INSERT WRAPPER (v2.7.7)
   ========================================= */

.manga-rating-auto-insert {
    margin: 30px 0;
    padding: 20px 0;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.manga-rating-auto-insert .manga-rating-container {
    margin: 0;
}

/* =========================================
   10. ACCESSIBILITY & PREFERENCES
   ========================================= */

/* High Contrast Mode (Windows) */
@media (forced-colors: active) {
    .radio > input:focus-visible + label { outline: 2px solid Highlight; }
    .radio > label > svg { fill: CanvasText; stroke: CanvasText; stroke-width: 1px; }
    .radio > input:checked + label > svg,
    .radio > input:checked ~ label > svg,
    .radio > label:hover > svg,
    .radio > label:hover ~ label > svg { fill: Highlight; stroke: Highlight; }
}

/* Reduced Motion Preference */
@media (prefers-reduced-motion: reduce) {
    #manga-rating-form.is-loading,
    .rating-error,
    .rating-feedback,
    .radio > label {
        animation: none !important;
        transition: none !important;
        transform: none !important;
    }
}