/**
 * RSL Star Rating Component
 * Version: 1.0.0
 *
 * A fully accessible star rating component supporting:
 * - Input mode (user selects rating)
 * - Display mode (readonly aggregate ratings)
 * - Filter mode (FilterBus integration for filtering)
 *
 * WCAG 2.1 AA Compliant:
 * - Full keyboard navigation (arrow keys, Home/End)
 * - Screen reader announcements
 * - Visible focus indicators
 * - Respects prefers-reduced-motion
 */

/* ==========================================================================
   CSS Variables
   ========================================================================== */

:root {
    /* Colors */
    --rsl-star-filled: #ffc107;
    --rsl-star-empty: #d0d0d0;
    --rsl-star-hover: #ffca28;
    --rsl-star-focus: #1976d2;
    --rsl-star-disabled: #9e9e9e;

    /* Sizes */
    --rsl-star-size-sm: 16px;
    --rsl-star-size-md: 24px;
    --rsl-star-size-lg: 32px;
    --rsl-star-size-xl: 48px;

    /* Gaps */
    --rsl-star-gap-sm: 2px;
    --rsl-star-gap-md: 4px;
    --rsl-star-gap-lg: 6px;
    --rsl-star-gap-xl: 8px;

    /* Animation */
    --rsl-star-transition: 150ms ease;
}

/* ==========================================================================
   Base Container
   ========================================================================== */

.rsl-star-rating {
    display: inline-flex;
    align-items: center;
    gap: var(--rsl-star-gap-md);
    position: relative;
    font-family: inherit;
}

/* Focus outline for the entire component when using keyboard */
.rsl-star-rating:focus {
    outline: none;
}

.rsl-star-rating:focus-visible {
    outline: 2px solid var(--rsl-star-focus);
    outline-offset: 4px;
    border-radius: 4px;
}

/* ==========================================================================
   Stars Container
   ========================================================================== */

.rsl-star-rating-stars {
    display: inline-flex;
    align-items: center;
    gap: var(--rsl-star-gap-md);
}

/* Size variants - stars container gap */
.rsl-star-rating[data-star-size="small"] .rsl-star-rating-stars {
    gap: var(--rsl-star-gap-sm);
}

.rsl-star-rating[data-star-size="large"] .rsl-star-rating-stars {
    gap: var(--rsl-star-gap-lg);
}

.rsl-star-rating[data-star-size="xl"] .rsl-star-rating-stars {
    gap: var(--rsl-star-gap-xl);
}

/* ==========================================================================
   Individual Star
   ========================================================================== */

.rsl-star {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--rsl-star-size-md);
    height: var(--rsl-star-size-md);
    cursor: pointer;
    transition: transform var(--rsl-star-transition), color var(--rsl-star-transition);
    color: var(--rsl-star-empty);
    flex-shrink: 0;
}

.rsl-star svg {
    width: 100%;
    height: 100%;
    fill: currentColor;
    transition: fill var(--rsl-star-transition);
}

/* Size variants */
.rsl-star-rating[data-star-size="small"] .rsl-star {
    width: var(--rsl-star-size-sm);
    height: var(--rsl-star-size-sm);
}

.rsl-star-rating[data-star-size="large"] .rsl-star {
    width: var(--rsl-star-size-lg);
    height: var(--rsl-star-size-lg);
}

.rsl-star-rating[data-star-size="xl"] .rsl-star {
    width: var(--rsl-star-size-xl);
    height: var(--rsl-star-size-xl);
}

/* ==========================================================================
   Star States
   ========================================================================== */

/* Filled state */
.rsl-star.rsl-star-filled {
    color: var(--rsl-star-filled);
}

/* Half-filled state - uses overlay with clipped SVG */
.rsl-star.rsl-star-half {
    color: var(--rsl-star-empty);
}

/* Hide overlay by default - only show for half stars */
.rsl-star .rsl-star-half-overlay {
    display: none;
}

/* Half star using overlay technique - clips the filled SVG to 50% width */
.rsl-star.rsl-star-half .rsl-star-half-overlay {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 100%;
    overflow: hidden;
    color: var(--rsl-star-filled);
}

.rsl-star.rsl-star-half .rsl-star-half-overlay svg {
    width: var(--rsl-star-size-md);
    height: var(--rsl-star-size-md);
}

.rsl-star-rating[data-star-size="small"] .rsl-star.rsl-star-half .rsl-star-half-overlay svg {
    width: var(--rsl-star-size-sm);
    height: var(--rsl-star-size-sm);
}

.rsl-star-rating[data-star-size="large"] .rsl-star.rsl-star-half .rsl-star-half-overlay svg {
    width: var(--rsl-star-size-lg);
    height: var(--rsl-star-size-lg);
}

.rsl-star-rating[data-star-size="xl"] .rsl-star.rsl-star-half .rsl-star-half-overlay svg {
    width: var(--rsl-star-size-xl);
    height: var(--rsl-star-size-xl);
}

/* Hover state (interactive mode only) */
.rsl-star-rating:not([data-star-readonly="true"]) .rsl-star:hover {
    transform: scale(1.15);
}

/* Hover preview - stars up to and including hovered star get hover color */
.rsl-star-rating:not([data-star-readonly="true"]) .rsl-star.rsl-star-hover-preview {
    color: var(--rsl-star-hover);
}

/* ==========================================================================
   Readonly Mode
   ========================================================================== */

.rsl-star-rating[data-star-readonly="true"] .rsl-star {
    cursor: default;
}

.rsl-star-rating[data-star-readonly="true"]:focus-visible {
    outline: none;
}

/* ==========================================================================
   Disabled State
   ========================================================================== */

.rsl-star-rating.rsl-star-rating-disabled,
.rsl-star-rating[disabled] {
    opacity: 0.5;
    pointer-events: none;
}

.rsl-star-rating.rsl-star-rating-disabled .rsl-star,
.rsl-star-rating[disabled] .rsl-star {
    cursor: not-allowed;
    color: var(--rsl-star-disabled);
}

.rsl-star-rating.rsl-star-rating-disabled .rsl-star.rsl-star-filled,
.rsl-star-rating[disabled] .rsl-star.rsl-star-filled {
    color: var(--rsl-star-disabled);
}

/* ==========================================================================
   Value Display
   ========================================================================== */

.rsl-star-rating-value {
    font-size: 0.875rem;
    font-weight: 600;
    color: #333;
    margin-left: 0.5rem;
    white-space: nowrap;
}

.rsl-star-rating[data-star-size="small"] .rsl-star-rating-value {
    font-size: 0.75rem;
}

.rsl-star-rating[data-star-size="large"] .rsl-star-rating-value {
    font-size: 1rem;
}

.rsl-star-rating[data-star-size="xl"] .rsl-star-rating-value {
    font-size: 1.25rem;
}

/* Optional count display (e.g., "4.5 (128 reviews)") */
.rsl-star-rating-count {
    font-size: 0.8rem;
    font-weight: 400;
    color: #666;
    margin-left: 0.25rem;
}

/* ==========================================================================
   Clear Button
   ========================================================================== */

.rsl-star-rating-clear {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    margin-left: 0.5rem;
    padding: 0;
    border: none;
    background: transparent;
    color: #999;
    cursor: pointer;
    border-radius: 50%;
    transition: background-color var(--rsl-star-transition), color var(--rsl-star-transition);
    opacity: 0;
    visibility: hidden;
}

.rsl-star-rating:hover .rsl-star-rating-clear,
.rsl-star-rating:focus-within .rsl-star-rating-clear {
    opacity: 1;
    visibility: visible;
}

.rsl-star-rating-clear:hover {
    background-color: rgba(0, 0, 0, 0.1);
    color: #666;
}

.rsl-star-rating-clear:focus {
    outline: 2px solid var(--rsl-star-focus);
    outline-offset: 2px;
}

.rsl-star-rating-clear svg {
    width: 12px;
    height: 12px;
}

/* Hide clear button when readonly or disabled */
.rsl-star-rating[data-star-readonly="true"] .rsl-star-rating-clear,
.rsl-star-rating.rsl-star-rating-disabled .rsl-star-rating-clear {
    display: none;
}

/* ==========================================================================
   Icon Variants
   ========================================================================== */

/* Heart icon */
.rsl-star-rating[data-star-icon="heart"] {
    --rsl-star-filled: #e91e63;
    --rsl-star-hover: #f06292;
}

/* Thumb icon */
.rsl-star-rating[data-star-icon="thumb"] {
    --rsl-star-filled: #4caf50;
    --rsl-star-hover: #81c784;
}

/* ==========================================================================
   Custom Colors via Data Attributes
   ========================================================================== */

/* These are applied via JavaScript when data attributes are present */
.rsl-star-rating[style*="--star-color"] .rsl-star.rsl-star-filled {
    color: var(--star-color);
}

.rsl-star-rating[style*="--star-empty-color"] .rsl-star:not(.rsl-star-filled):not(.rsl-star-half) {
    color: var(--star-empty-color);
}

/* ==========================================================================
   Filter Mode
   ========================================================================== */

.rsl-star-rating-filter {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.rsl-star-rating-filter-option {
    display: inline-flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    background: #fff;
    cursor: pointer;
    transition: all var(--rsl-star-transition);
}

.rsl-star-rating-filter-option:hover {
    border-color: var(--rsl-star-filled);
    background: rgba(255, 193, 7, 0.05);
}

.rsl-star-rating-filter-option:focus {
    outline: 2px solid var(--rsl-star-focus);
    outline-offset: 2px;
}

.rsl-star-rating-filter-option.rsl-star-rating-filter-active {
    border-color: var(--rsl-star-filled);
    background: rgba(255, 193, 7, 0.1);
}

.rsl-star-rating-filter-option input[type="radio"] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.rsl-star-rating-filter-label {
    font-size: 0.875rem;
    color: #666;
}

.rsl-star-rating-filter-count {
    font-size: 0.75rem;
    color: #999;
    margin-left: auto;
}

/* ==========================================================================
   Inline Variant (horizontal filter options)
   ========================================================================== */

.rsl-star-rating-filter.rsl-star-rating-filter-inline {
    flex-direction: row;
    flex-wrap: wrap;
}

.rsl-star-rating-filter.rsl-star-rating-filter-inline .rsl-star-rating-filter-option {
    padding: 0.375rem 0.625rem;
}

/* ==========================================================================
   Form Validation States
   ========================================================================== */

.rsl-star-rating.rsl-star-rating-error {
    --rsl-star-focus: #dc3545;
}

.rsl-star-rating.rsl-star-rating-error::after {
    content: attr(data-star-error);
    display: block;
    position: absolute;
    bottom: -1.5rem;
    left: 0;
    font-size: 0.75rem;
    color: #dc3545;
}

.rsl-star-rating.rsl-star-rating-required .rsl-star-rating-label::after {
    content: ' *';
    color: #dc3545;
}

/* ==========================================================================
   Label
   ========================================================================== */

.rsl-star-rating-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    color: #333;
    margin-bottom: 0.5rem;
}

/* Inline label */
.rsl-star-rating.rsl-star-rating-inline-label {
    flex-wrap: wrap;
}

.rsl-star-rating.rsl-star-rating-inline-label .rsl-star-rating-label {
    margin-bottom: 0;
    margin-right: 0.75rem;
}

/* ==========================================================================
   Screen Reader Only Text
   ========================================================================== */

.rsl-star-rating-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ==========================================================================
   Reduced Motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .rsl-star,
    .rsl-star svg,
    .rsl-star-rating-clear,
    .rsl-star-rating-filter-option {
        transition: none;
    }

    .rsl-star-rating:not([data-star-readonly="true"]) .rsl-star:hover {
        transform: none;
    }
}

/* ==========================================================================
   Dark Mode
   ========================================================================== */

[data-theme="dark"] .rsl-star-rating {
    --rsl-star-empty: #4a4a4a;
    --rsl-star-focus: #64b5f6;
}

[data-theme="dark"] .rsl-star-rating-value {
    color: #e0e0e0;
}

[data-theme="dark"] .rsl-star-rating-count {
    color: #999;
}

[data-theme="dark"] .rsl-star-rating-label {
    color: #e0e0e0;
}

[data-theme="dark"] .rsl-star-rating-clear {
    color: #888;
}

[data-theme="dark"] .rsl-star-rating-clear:hover {
    background-color: rgba(255, 255, 255, 0.1);
    color: #ccc;
}

[data-theme="dark"] .rsl-star-rating-filter-option {
    border-color: #444;
    background: #2a2a2a;
    color: #e0e0e0;
}

[data-theme="dark"] .rsl-star-rating-filter-option:hover {
    border-color: var(--rsl-star-filled);
    background: rgba(255, 193, 7, 0.1);
}

[data-theme="dark"] .rsl-star-rating-filter-option.rsl-star-rating-filter-active {
    border-color: var(--rsl-star-filled);
    background: rgba(255, 193, 7, 0.15);
}

[data-theme="dark"] .rsl-star-rating-filter-label {
    color: #bbb;
}

[data-theme="dark"] .rsl-star-rating-filter-count {
    color: #888;
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
    .rsl-star-rating {
        color-adjust: exact;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .rsl-star-rating-clear {
        display: none;
    }
}
