/* RoamingPigs Interactive Scorecards v3.0
 * Conditionally loaded on pages with [data-scorecard] elements
 * Features: Micro-interactions, animated scores, sharing, benchmarks
 */

/* =================================================================
   NO-JS GRACEFUL DEGRADATION
   Table remains readable, interactive scoring disabled
   ================================================================= */

/* Fallback message - shown only when JS disabled */
.widget-noscript {
    display: none;
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    background: rgba(245, 158, 11, 0.1);
    border: 1px dashed #f59e0b;
    border-radius: 6px;
    font-size: 0.85rem;
    color: #92400e;
}

[data-theme="dark"] .widget-noscript {
    background: rgba(245, 158, 11, 0.15);
    color: #fbbf24;
}

html.no-js .widget-noscript {
    display: block;
}

/* Hide JS-only elements when no JS */
html.no-js .scorecard-score-box,
html.no-js .scorecard-interpretation,
html.no-js .scorecard-results-card,
html.no-js .scorecard-gauge {
    display: none;
}

/* Table cells are still clickable-looking, make them static */
html.no-js [data-scorecard] .scorecard-option {
    cursor: default;
}

html.no-js [data-scorecard] .scorecard-option:hover {
    background: inherit;
    box-shadow: none;
}

/* =================================================================
   SCORECARD CONTAINER
   ================================================================= */
[data-scorecard] {
    margin: 2.5rem 0;
    padding: 1.5rem;
    background: var(--bg-secondary);
    border-radius: 12px;
    border: 1px solid var(--border-color);
}

[data-scorecard] h3 {
    margin: 0 0 0.5rem 0;
    font-size: 1.25rem;
    color: var(--text-primary);
}

[data-scorecard] > p:first-of-type {
    margin: 0 0 1.25rem 0;
    color: var(--text-muted);
    font-size: 0.95rem;
}

/* =================================================================
   SCORE DISPLAY BOX - Always Visible Above Table
   ================================================================= */
.scorecard-score-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1.25rem;
    padding: 1rem 1.25rem;
    margin-bottom: 1.25rem;
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-primary) 100%);
    border-radius: 10px;
    border: 2px solid var(--border-color);
    position: relative;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    flex-wrap: wrap;
}

.scorecard-score-box.has-score {
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Completion celebration state */
.scorecard-score-box.complete {
    border-color: #22c55e;
    box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.15);
    animation: scorecard-complete-pulse 0.6s ease;
}

@keyframes scorecard-complete-pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.01); }
    100% { transform: scale(1); }
}

/* =================================================================
   SCORE VALUE (Left side)
   ================================================================= */
.scorecard-score-main {
    display: flex;
    align-items: baseline;
    gap: 0.25rem;
    padding-right: 1.25rem;
    border-right: 1px solid var(--border-color);
}

.scorecard-score-value {
    font-size: 2rem;
    font-weight: 800;
    line-height: 1;
    color: var(--text-muted);
    transition: color 0.3s ease, transform 0.2s ease;
    font-variant-numeric: tabular-nums;
}

.scorecard-score-max {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-muted);
}

.scorecard-score-box.has-score .scorecard-score-value {
    color: var(--accent-primary);
}

.scorecard-score-box.complete .scorecard-score-value {
    color: #15803d; /* Green-700: 4.5:1 on white */
}

/* Score bump animation */
.scorecard-score-value.bump {
    animation: scorecard-bump 0.3s ease;
}

@keyframes scorecard-bump {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

/* =================================================================
   PROGRESS SECTION (Right side)
   ================================================================= */
.scorecard-score-meta {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 0.5rem;
    min-height: 100%;
}

.scorecard-progress {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.scorecard-progress-text {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    white-space: nowrap;
    min-width: 90px;
}

.scorecard-progress-bar {
    flex: 1;
    max-width: 140px;
    height: 6px;
    background: var(--border-color);
    border-radius: 3px;
    overflow: hidden;
}

.scorecard-progress-fill {
    height: 100%;
    background: var(--accent-primary);
    border-radius: 3px;
    transition: width 0.3s ease, background-color 0.3s ease;
    width: 0%;
}

.scorecard-score-box.complete .scorecard-progress-fill {
    background: #22c55e;
}

/* =================================================================
   ACTION BUTTONS
   ================================================================= */
.scorecard-actions {
    display: flex;
    gap: 0.4rem;
    align-items: center;
}

.scorecard-reset,
.scorecard-copy,
.scorecard-share {
    padding: 0.3rem 0.6rem;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.75rem;
    color: var(--text-muted);
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    white-space: nowrap;
}

.scorecard-reset span,
.scorecard-copy span,
.scorecard-share span {
    font-size: 0.8rem;
}

.scorecard-reset:hover,
.scorecard-copy:hover,
.scorecard-share:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-color: var(--text-muted);
}

.scorecard-copy.copied {
    background: #22c55e;
    color: white;
    border-color: #22c55e;
}

/* =================================================================
   RESULT GAUGE (Always visible, fills on completion)
   ================================================================= */
.scorecard-gauge {
    flex-basis: 100%;
    width: 100%;
    height: 8px;
    margin-top: 0.5rem;
    background: var(--border-color);
    border-radius: 4px;
    overflow: hidden;
    position: relative;
}

@keyframes gauge-reveal {
    from {
        opacity: 0;
        transform: scaleX(0.8);
    }
    to {
        opacity: 1;
        transform: scaleX(1);
    }
}

.scorecard-gauge-fill {
    height: 100%;
    border-radius: 5px;
    transition: width 0.5s ease-out, background-color 0.3s ease;
    position: relative;
}

.scorecard-gauge-marker {
    position: absolute;
    top: -3px;
    width: 2px;
    height: 16px;
    background: var(--text-primary);
    opacity: 0.4;
    transition: left 0.5s ease-out;
    border-radius: 1px;
}

/* Gauge color based on assessment result */
.scorecard-gauge-fill.level-pending {
    background: var(--text-muted);
    opacity: 0.5;
}

.scorecard-gauge-fill.level-good {
    background: #22c55e;
}

.scorecard-gauge-fill.level-medium {
    background: #eab308;
}

.scorecard-gauge-fill.level-bad {
    background: #ef4444;
}

/* =================================================================
   TABLE STYLING
   ================================================================= */
[data-scorecard] table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    background: var(--bg-primary);
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border-color);
}

[data-scorecard] thead th {
    background: var(--bg-secondary);
    padding: 0.875rem 0.75rem;
    text-align: left;
    font-weight: 600;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.025em;
    border-bottom: 2px solid var(--border-color);
}

[data-scorecard] thead th:first-child {
    color: var(--text-primary);
}

[data-scorecard] tbody td {
    padding: 0.875rem 0.75rem;
    border-bottom: 1px solid var(--border-color);
    vertical-align: middle;
}

[data-scorecard] tbody tr:last-child td {
    border-bottom: none;
}

[data-scorecard] tbody td:first-child {
    font-weight: 600;
    min-width: 140px;
    color: var(--text-primary);
    position: relative;
}

/* Row completion checkmark */
[data-scorecard] tbody tr.has-selection td:first-child::after {
    content: "\2713";
    position: absolute;
    right: 4px;
    top: 50%;
    transform: translateY(-50%);
    color: #15803d; /* Green-700: 4.5:1 on white */
    font-size: 0.9rem;
    font-weight: bold;
    opacity: 0;
    animation: scorecard-checkmark 0.3s ease forwards;
}

@keyframes scorecard-checkmark {
    0% { opacity: 0; transform: translateY(-50%) scale(0.5); }
    100% { opacity: 1; transform: translateY(-50%) scale(1); }
}

/* Unscored row highlight - subtle pulse */
[data-scorecard] tbody tr:not(.has-selection) {
    position: relative;
}

[data-scorecard].has-started tbody tr:not(.has-selection) {
    background: rgba(59, 130, 246, 0.02);
}

[data-scorecard].has-started tbody tr:not(.has-selection) td:first-child {
    color: var(--accent-primary);
}

/* Score Column Headers with Colors
   Default polarity: higher = worse (risk assessment style)
   Use data-polarity="positive" for higher = better scorecards
*/

/* Default (negative polarity): Low=Green, Mid=Yellow, High=Red */
/* All colors WCAG AA compliant (4.5:1+ on white) */
[data-scorecard] thead th:nth-child(2) {
    color: #15803d; /* Green-700 */
}
[data-scorecard] thead th:nth-child(3) {
    color: #854d0e; /* Amber-800 */
}
[data-scorecard] thead th:nth-child(4) {
    color: #dc2626; /* Red-600 */
}

/* Positive polarity: Low=Red, Mid=Yellow, High=Green */
[data-scorecard][data-polarity="positive"] thead th:nth-child(2) {
    color: #dc2626; /* Red-600 */
}
[data-scorecard][data-polarity="positive"] thead th:nth-child(3) {
    color: #854d0e; /* Amber-800 */
}
[data-scorecard][data-polarity="positive"] thead th:nth-child(4) {
    color: #15803d; /* Green-700 */
}

/* =================================================================
   SCORE OPTION CELLS - Interactive
   ================================================================= */
[data-scorecard] .scorecard-option {
    cursor: pointer;
    transition: all 0.15s ease;
    position: relative;
    user-select: none;
    border-radius: 6px;
    margin: 2px;
    padding: 0.75rem 0.5rem !important;
}

/* Hover preview - show what would happen */
[data-scorecard] .scorecard-option:hover {
    background: var(--bg-secondary);
    box-shadow: 0 0 0 2px var(--accent-primary);
}

[data-scorecard] .scorecard-option:focus {
    outline: none;
    box-shadow: 0 0 0 2px var(--accent-primary);
}

/* Selection state */
[data-scorecard] .scorecard-option.selected {
    background: #2563eb; /* Blue-600 for 4.5:1 contrast */
    color: white;
    font-weight: 600;
    box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
    animation: scorecard-select 0.25s ease;
}

@keyframes scorecard-select {
    0% { transform: scale(1); }
    40% { transform: scale(1.03); }
    100% { transform: scale(1); }
}

/* Row highlighting when has selection */
[data-scorecard] tbody tr.has-selection {
    background: rgba(59, 130, 246, 0.03);
}

/* =================================================================
   INTERPRETATION BOX
   ================================================================= */
.scorecard-interpretation {
    margin-top: 1rem;
    padding: 1rem 1.25rem;
    border-radius: 8px;
    font-size: 0.95rem;
    line-height: 1.6;
    background: var(--bg-secondary);
    border-left: 4px solid var(--border-color);
    transition: all 0.3s ease;
}

.scorecard-interpretation strong {
    display: block;
    margin-bottom: 0.25rem;
    font-size: 1rem;
}

.scorecard-interpretation.info {
    color: var(--text-secondary);
    border-left-color: var(--border-color);
}

.scorecard-interpretation.placeholder {
    opacity: 0.7;
    border-left-style: dashed;
}

.scorecard-interpretation.success {
    color: #15803d;
    background: rgba(34, 197, 94, 0.08);
    border-left-color: #22c55e;
}

.scorecard-interpretation.warning {
    color: #854d0e; /* Amber-800: 6.2:1 on white */
    background: rgba(234, 179, 8, 0.08);
    border-left-color: #eab308;
}

.scorecard-interpretation.danger {
    color: #b91c1c;
    background: rgba(239, 68, 68, 0.08);
    border-left-color: #ef4444;
}

/* =================================================================
   RESULTS CARD - Always visible, updates on completion
   ================================================================= */
.scorecard-results-card {
    display: block;
    margin-top: 1rem;
    padding: 1rem 1.25rem;
    background: var(--bg-secondary);
    border-radius: 8px;
    border: 1px solid var(--border-color);
    transition: all 0.3s ease;
}

.scorecard-results-card.placeholder {
    opacity: 0.7;
    border-style: dashed;
}

.scorecard-results-card:not(.placeholder) {
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-primary) 100%);
    border-style: solid;
    border-color: var(--accent-primary);
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.1);
}

@keyframes scorecard-card-appear {
    0% { opacity: 0; transform: translateY(10px); }
    100% { opacity: 1; transform: translateY(0); }
}

.scorecard-results-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.scorecard-results-title {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
}

.scorecard-results-actions {
    display: flex;
    gap: 0.5rem;
}

.scorecard-results-summary {
    font-size: 0.85rem;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* =================================================================
   CELEBRATION CONFETTI (CSS-only)
   ================================================================= */
.scorecard-confetti {
    position: absolute;
    pointer-events: none;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    overflow: hidden;
    opacity: 0;
    z-index: 10;
}

.scorecard-score-box.celebrating .scorecard-confetti {
    opacity: 1;
}

.scorecard-confetti span {
    position: absolute;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    animation: scorecard-confetti-fall 1s ease forwards;
}

@keyframes scorecard-confetti-fall {
    0% { transform: translateY(-20px) rotate(0deg); opacity: 1; }
    100% { transform: translateY(60px) rotate(360deg); opacity: 0; }
}

/* =================================================================
   TOAST NOTIFICATION
   ================================================================= */
.scorecard-toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #1f2937;
    color: white;
    padding: 0.75rem 1.25rem;
    border-radius: 8px;
    font-size: 0.9rem;
    z-index: 9999;
    transition: transform 0.3s ease;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.scorecard-toast.show {
    transform: translateX(-50%) translateY(0);
}

/* =================================================================
   DARK MODE ADJUSTMENTS
   ================================================================= */
[data-theme="dark"] .scorecard-score-box {
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-primary) 100%);
}

[data-theme="dark"] .scorecard-interpretation.success {
    color: #4ade80;
    background: rgba(34, 197, 94, 0.12);
}

[data-theme="dark"] .scorecard-interpretation.warning {
    color: #facc15;
    background: rgba(234, 179, 8, 0.12);
}

[data-theme="dark"] .scorecard-interpretation.danger {
    color: #f87171;
    background: rgba(239, 68, 68, 0.12);
}

[data-theme="dark"] [data-scorecard] .scorecard-option.selected {
    background: var(--accent-primary);
}

[data-theme="dark"] .scorecard-toast {
    background: #374151;
}

[data-theme="dark"] .scorecard-reset,
[data-theme="dark"] .scorecard-copy,
[data-theme="dark"] .scorecard-share {
    background: var(--bg-secondary);
}

/* =================================================================
   MOBILE RESPONSIVE
   ================================================================= */
@media (max-width: 768px) {
    [data-scorecard] {
        padding: 1rem;
        margin: 1.5rem 0;
    }

    /* Scrollable table wrapper */
    .scorecard-table-wrapper {
        position: relative;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -0.75rem;
        padding: 0 0.75rem;
    }

    [data-scorecard] table {
        font-size: 0.85rem;
        min-width: 500px; /* Force readable size */
    }

    [data-scorecard] thead th,
    [data-scorecard] tbody td {
        padding: 0.75rem 0.625rem;
        white-space: nowrap;
    }

    [data-scorecard] thead th {
        font-size: 0.75rem;
    }

    /* Allow description text to wrap */
    [data-scorecard] tbody td:not(:first-child) {
        white-space: normal;
        min-width: 100px;
    }

    [data-scorecard] tbody td:first-child {
        min-width: 100px;
        padding-right: 1.5rem; /* Room for checkmark */
        position: sticky;
        left: 0;
        background: var(--bg-primary);
        z-index: 1;
    }

    /* Sticky header for dimension column */
    [data-scorecard] thead th:first-child {
        position: sticky;
        left: 0;
        background: var(--bg-secondary);
        z-index: 2;
    }

    /* Scroll hint gradient */
    .scorecard-table-wrapper::after {
        content: '';
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 20px;
        background: linear-gradient(to right, transparent, var(--bg-secondary));
        pointer-events: none;
        opacity: 0.8;
    }

    /* Stack score box vertically on mobile */
    .scorecard-score-box {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
        padding: 1rem;
    }

    .scorecard-score-main {
        justify-content: center;
        padding-right: 0;
        border-right: none;
        padding-bottom: 0.75rem;
        border-bottom: 1px solid var(--border-color);
    }

    .scorecard-score-value {
        font-size: 1.75rem;
    }

    .scorecard-score-meta {
        align-items: center;
    }

    .scorecard-progress {
        flex-direction: column;
        gap: 0.5rem;
    }

    .scorecard-progress-bar {
        max-width: 160px;
    }

    .scorecard-actions {
        justify-content: center;
    }

    .scorecard-gauge {
        display: block;
        flex-basis: auto;
        width: 100%;
        height: 8px;
        margin-top: 0.75rem;
    }
}

/* Dark mode sticky column fix */
@media (max-width: 768px) {
    [data-theme="dark"] [data-scorecard] tbody td:first-child {
        background: var(--bg-primary);
    }
    [data-theme="dark"] [data-scorecard] thead th:first-child {
        background: var(--bg-secondary);
    }
}

/* =================================================================
   PRINT STYLES
   ================================================================= */
@media print {
    [data-scorecard] {
        background: white;
        border: 1px solid #ccc;
    }

    .scorecard-score-box {
        background: #f5f5f5;
    }

    .scorecard-reset,
    .scorecard-copy,
    .scorecard-share,
    .scorecard-actions {
        display: none;
    }

    [data-scorecard] .scorecard-option {
        cursor: default;
    }

    [data-scorecard] .scorecard-option.selected {
        background: #e5e7eb !important;
        color: inherit !important;
        font-weight: 700;
    }

    .scorecard-gauge,
    .scorecard-confetti {
        display: none !important;
    }
}
