/**
 * AI Article Pipeline — Table & Chart Styles
 * v6.1.8
 * 
 * 508 Accessibility: Tables use role="region", aria-label, tabindex for
 * keyboard-accessible horizontal scrolling. Screen readers announce the
 * table caption. Scroll shadows provide visual affordance for sighted users.
 *
 * v6.1.8: Two-layer scroll structure with scroll hint.
 *   .aiap-table-responsive     → position:relative for shadow overlays
 *     .aiap-table-scroll       → overflow-x:auto (actual scroll container)
 *       table.aiap-table
 *     .aiap-scroll-hint        → "Scroll to see more" indicator
 */

/* ============================================
   Responsive wrapper (shadows layer)
   ============================================ */
.aiap-table-responsive {
    position: relative;
    width: 100%;
    margin: 1.5em 0 0.5em;
    max-width: 100% !important;
}

/* Inner scroll container */
.aiap-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: #c4c4c4 transparent;
    outline: none;
}

.aiap-table-scroll::-webkit-scrollbar { height: 6px; }
.aiap-table-scroll::-webkit-scrollbar-track { background: #f0f0f0; border-radius: 3px; }
.aiap-table-scroll::-webkit-scrollbar-thumb { background: #c4c4c4; border-radius: 3px; }
.aiap-table-scroll::-webkit-scrollbar-thumb:hover { background: #999; }

/* Focus ring for keyboard navigation (508) */
.aiap-table-scroll:focus {
    outline: 2px solid #4a90d9;
    outline-offset: 2px;
    border-radius: 2px;
}

/* Backward compat: if old markup has no .aiap-table-scroll, scroll the wrapper directly */
.aiap-table-responsive:not(:has(.aiap-table-scroll)) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Scroll shadow — pseudo-elements on the outer wrapper */
.aiap-table-responsive::before,
.aiap-table-responsive::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 6px; /* don't cover scrollbar */
    width: 28px;
    pointer-events: none;
    z-index: 2;
    opacity: 0;
    transition: opacity 0.25s ease;
}

.aiap-table-responsive::before {
    left: 0;
    background: linear-gradient(to right, rgba(0,0,0,0.08) 0%, rgba(0,0,0,0.03) 40%, transparent 100%);
    border-radius: 8px 0 0 8px;
}

.aiap-table-responsive::after {
    right: 0;
    background: linear-gradient(to left, rgba(0,0,0,0.08) 0%, rgba(0,0,0,0.03) 40%, transparent 100%);
    border-radius: 0 8px 8px 0;
}

/* JS toggles these classes based on scroll position */
.aiap-table-responsive.scroll-left::before,
.aiap-table-responsive.can-scroll-left::before {
    opacity: 1;
}

.aiap-table-responsive.scroll-right::after,
.aiap-table-responsive.can-scroll-right::after {
    opacity: 1;
}

/* ============================================
   "Scroll to see more" hint
   ============================================ */
.aiap-scroll-hint {
    display: none;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 6px 0 2px;
    font-size: 12px;
    color: #888;
    letter-spacing: 0.02em;
    user-select: none;
}

.aiap-scroll-hint svg {
    width: 14px;
    height: 14px;
    opacity: 0.6;
    animation: aiap-nudge 1.8s ease-in-out infinite;
}

@keyframes aiap-nudge {
    0%, 100% { transform: translateX(0); }
    40% { transform: translateX(4px); }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(2px); }
}

/* Show hint only when table overflows */
.aiap-table-responsive.is-scrollable .aiap-scroll-hint {
    display: flex;
}

/* Hide after user scrolls */
.aiap-table-responsive.has-scrolled .aiap-scroll-hint {
    display: none;
}

/* ============================================
   Table base styles
   ============================================ */
.aiap-table {
    width: auto;
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.9em;
    line-height: 1.55;
    margin: 0;
    table-layout: auto;
    border: 1px solid #e2e5e9;
    border-radius: 8px;
    overflow: hidden; /* Clips corners for border-radius */
}

/* Column min-widths to prevent cramping */
.aiap-table th,
.aiap-table td {
    min-width: 120px;
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
}

.aiap-table th:first-child,
.aiap-table td:first-child {
    min-width: 130px;
}

.aiap-table caption {
    caption-side: top;
    text-align: left;
    font-weight: 700;
    font-size: 1em;
    color: #1a1a2e;
    padding: 0 0 0.6em;
    line-height: 1.4;
}

.aiap-table thead th {
    background: #f4f6f8;
    font-weight: 600;
    text-align: left;
    padding: 0.7em 0.9em;
    border-bottom: 2px solid #d0d5dc;
    white-space: nowrap;
    color: #2c3e50;
    font-size: 0.8em;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* First and last th: remove outer border-radius overlap */
.aiap-table thead th:first-child {
    border-top-left-radius: 7px;
}

.aiap-table thead th:last-child {
    border-top-right-radius: 7px;
}

.aiap-table tbody td {
    padding: 0.65em 0.9em;
    border-bottom: 1px solid #eef0f2;
    vertical-align: top;
    color: #333;
}

.aiap-table tbody tr:last-child td {
    border-bottom: none;
}

/* Subtle zebra striping for scanability */
.aiap-table tbody tr:nth-child(even) {
    background: #fafbfc;
}

.aiap-table tbody tr:hover {
    background: #edf2f7;
}

/* First column emphasis — helps scan rows */
.aiap-table tbody td:first-child {
    font-weight: 500;
    color: #1a1a2e;
}

/* Right-align numeric columns (applied via JS or manually) */
.aiap-table td[data-numeric],
.aiap-table th[data-numeric] {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* ============================================
   Source annotation (appears after table)
   ============================================ */
.aiap-table-source {
    font-size: 0.8em;
    color: #666;
    margin: 0.4em 0 1.5em;
    line-height: 1.5;
    font-style: italic;
}

.aiap-table-source a {
    color: #555;
    text-decoration: underline;
    text-decoration-color: #ccc;
    text-underline-offset: 2px;
}

.aiap-table-source a:hover {
    color: #1a1a2e;
    text-decoration-color: #888;
}

/* ============================================
   Mobile refinements
   ============================================ */
@media (max-width: 640px) {
    .aiap-table {
        font-size: 0.85em;
        min-width: 400px;
    }
    
    .aiap-table thead th {
        padding: 0.6em 0.7em;
        font-size: 0.75em;
    }
    
    .aiap-table tbody td {
        padding: 0.55em 0.7em;
    }
    
    .aiap-table caption {
        font-size: 0.9em;
    }
}

/* ============================================
   Toggle bar
   ============================================ */
.aiap-chart-toggle {
    display: flex;
    gap: 2px;
    margin: 1.5em 0 0;
    background: #f1f3f5;
    border-radius: 6px;
    padding: 3px;
    width: fit-content;
}

.aiap-chart-toggle-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 14px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: #555;
    font-size: 0.825em;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
    line-height: 1;
    font-family: inherit;
}

.aiap-chart-toggle-btn:hover {
    color: #222;
    background: rgba(255, 255, 255, 0.5);
}

.aiap-chart-toggle-btn.active {
    background: #fff;
    color: #1a1a2e;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.aiap-chart-toggle-btn svg {
    flex-shrink: 0;
    opacity: 0.7;
}

.aiap-chart-toggle-btn.active svg {
    opacity: 1;
}

/* ============================================
   Chart container
   ============================================ */
.aiap-chart-container {
    margin: 0 0 1.5em;
    padding: 1em;
    background: #fff;
    border: 1px solid #eee;
    border-radius: 6px;
    max-width: 100%;
}

.aiap-chart-container canvas {
    max-height: 400px;
}

/* ============================================
   Dark mode support (respects prefers-color-scheme)
   ============================================ */
@media (prefers-color-scheme: dark) {
    .aiap-table-responsive:focus {
        outline-color: #6aa3e0;
    }

    .aiap-table-responsive::before {
        background: linear-gradient(to right, rgba(0,0,0,0.2), transparent);
    }

    .aiap-table-responsive::after {
        background: linear-gradient(to left, rgba(0,0,0,0.2), transparent);
    }

    .aiap-table {
        border-color: #3a3a4e;
    }

    .aiap-table caption {
        color: #e0e0e0;
    }

    .aiap-table thead th {
        background: #2a2a3e;
        color: #cdd4dc;
        border-bottom-color: #4a4a5e;
    }

    .aiap-table tbody td {
        border-bottom-color: #333;
        color: #d0d0d0;
    }

    .aiap-table tbody tr:nth-child(even) {
        background: #252535;
    }

    .aiap-table tbody tr:hover {
        background: #2a2a3e;
    }

    .aiap-table tbody td:first-child {
        color: #e0e0e0;
    }

    .aiap-table-source {
        color: #999;
    }

    .aiap-table-source a {
        color: #aaa;
        text-decoration-color: #555;
    }

    .aiap-table-source a:hover {
        color: #ddd;
        text-decoration-color: #888;
    }

    .aiap-chart-toggle {
        background: #2a2a3e;
    }

    .aiap-chart-toggle-btn {
        color: #aaa;
    }

    .aiap-chart-toggle-btn:hover {
        color: #ddd;
        background: rgba(255, 255, 255, 0.08);
    }

    .aiap-chart-toggle-btn.active {
        background: #3a3a4e;
        color: #fff;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    }

    .aiap-chart-container {
        background: #1e1e2e;
        border-color: #333;
    }
}

/* ============================================
   Print: always show tables, hide charts
   ============================================ */
@media print {
    .aiap-chart-toggle {
        display: none !important;
    }

    .aiap-chart-container {
        display: none !important;
    }

    .aiap-table-responsive {
        overflow: visible;
        display: block !important;
    }
}
