/* =============================================================================
   GRID-ACCESSIBILITY.CSS
   =============================================================================
   Purpose:
     Accessibility styles for TkDataGrid including screen reader only content,
     focus indicators, high contrast mode support, and reduced motion preferences.

   WCAG 2.1 AA Compliance:
     - Focus visible indicators (2.4.7)
     - Color contrast (1.4.3)
     - Resize text support (1.4.4)
     - High contrast mode (1.4.11)
     - Reduced motion preference (2.3.3)

   Agent: 31 of 32
   Component: Accessibility System
   Last Updated: 2026-01-28
   ============================================================================= */

/* =============================================================================
   SCREEN READER ONLY CONTENT
   ============================================================================= */

/**
 * Visually hidden content that remains accessible to screen readers.
 * Uses the standard accessible-hide technique recommended by WebAIM.
 */
.tk-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;
}

/**
 * Allows visually hidden content to become visible when focused.
 * Useful for skip links and similar accessibility features.
 */
.tk-sr-only-focusable:focus,
.tk-sr-only-focusable:active {
    position: static;
    width: auto;
    height: auto;
    margin: 0;
    overflow: visible;
    clip: auto;
    white-space: normal;
}

/* =============================================================================
   SKIP LINKS
   ============================================================================= */

/**
 * Skip link that becomes visible when focused.
 * Allows keyboard users to bypass navigation.
 */
.tk-skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    padding: 8px 16px;
    background: var(--tk-primary-color, #2563eb);
    color: white;
    font-weight: 600;
    text-decoration: none;
    z-index: 10000;
    transition: top 0.2s ease;
}

.tk-skip-link:focus {
    top: 0;
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: 2px;
}

/* =============================================================================
   FOCUS INDICATORS
   ============================================================================= */

/**
 * Default focus outline for all grid interactive elements.
 * Uses :focus-visible to only show for keyboard navigation (not mouse clicks).
 */
.tk-datagrid *:focus-visible {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: 2px;
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.2);
}

/**
 * Focus styles for grid cells.
 */
.tk-grid-cell:focus-visible,
.tk-grid-cell.active {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: -2px;
    z-index: 1;
    position: relative;
}

/**
 * Focus styles for column headers.
 */
.tk-column-header:focus-visible {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: -2px;
    background-color: var(--tk-header-hover-bg, #f3f4f6);
}

/**
 * Focus styles for row selection checkboxes.
 */
.tk-row-checkbox input:focus-visible {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: 2px;
}

/**
 * Focus styles for buttons.
 */
.tk-datagrid button:focus-visible {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: 2px;
}

/**
 * Focus styles for row expanders.
 */
.tk-row-expander:focus-visible {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: 2px;
    border-radius: 4px;
}

/* =============================================================================
   SELECTED STATE INDICATORS
   ============================================================================= */

/**
 * Visual indicator for selected rows.
 */
.tk-grid-row.selected {
    background-color: var(--tk-selection-bg, #dbeafe);
    border-left: 3px solid var(--tk-primary-color, #2563eb);
}

.tk-grid-row.selected:hover {
    background-color: var(--tk-selection-hover-bg, #bfdbfe);
}

/**
 * Active row indicator.
 */
.tk-grid-row.active {
    background-color: var(--tk-active-bg, #eff6ff);
}

/* =============================================================================
   EDITOR FOCUS STYLES
   ============================================================================= */

/**
 * Focus styles for cell editors.
 */
.tk-cell-editor:focus,
.tk-cell-editor input:focus,
.tk-cell-editor textarea:focus,
.tk-cell-editor select:focus {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: 0;
    border-color: var(--tk-focus-color, #3b82f6);
}

/**
 * Highlighted option in dropdown.
 */
.tk-select-option.highlighted {
    background-color: var(--tk-highlight-bg, #dbeafe);
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: -2px;
}

/* =============================================================================
   HIGH CONTRAST MODE SUPPORT
   ============================================================================= */

/**
 * Windows High Contrast Mode support.
 * Uses forced-colors media query (modern) and -ms-high-contrast (legacy).
 */
@media (forced-colors: active) {
    .tk-datagrid {
        border: 2px solid currentColor;
    }

    .tk-grid-cell:focus-visible,
    .tk-grid-cell.active {
        outline: 3px solid currentColor;
        outline-offset: -3px;
    }

    .tk-column-header {
        border-bottom: 2px solid currentColor;
    }

    .tk-column-header:focus-visible {
        outline: 3px solid currentColor;
    }

    .tk-grid-row.selected {
        background-color: Highlight;
        color: HighlightText;
        border-left-color: currentColor;
    }

    .tk-grid-row:hover {
        background-color: Highlight;
        color: HighlightText;
    }

    .tk-select-option.selected {
        background-color: Highlight;
        color: HighlightText;
    }

    .tk-select-option.highlighted {
        outline: 2px solid currentColor;
    }

    button,
    .tk-btn {
        border: 2px solid currentColor;
    }

    button:focus-visible,
    .tk-btn:focus-visible {
        outline: 3px solid currentColor;
        outline-offset: 2px;
    }

    /* Ensure icons are visible */
    .tk-datagrid i,
    .tk-datagrid .bi {
        forced-color-adjust: auto;
    }
}

/**
 * Legacy high contrast mode support for older browsers.
 */
@media (-ms-high-contrast: active) {
    .tk-datagrid {
        border: 2px solid currentColor;
    }

    .tk-grid-row.selected {
        background-color: Highlight;
        color: HighlightText;
    }
}

/**
 * prefers-contrast: more support.
 */
@media (prefers-contrast: more) {
    .tk-datagrid {
        border: 2px solid var(--tk-border-color, #374151);
    }

    .tk-grid-cell {
        border: 1px solid var(--tk-border-color, #374151);
    }

    .tk-column-header {
        border-bottom: 2px solid var(--tk-border-color, #374151);
        font-weight: 700;
    }

    .tk-grid-cell:focus-visible,
    .tk-column-header:focus-visible {
        outline-width: 3px;
    }

    .tk-grid-row.selected {
        border-left-width: 4px;
    }
}

/* =============================================================================
   REDUCED MOTION SUPPORT
   ============================================================================= */

/**
 * Respect user preference for reduced motion.
 * Disables transitions and animations.
 */
@media (prefers-reduced-motion: reduce) {
    .tk-datagrid,
    .tk-datagrid * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .tk-datagrid-loading-spinner {
        animation: none;
    }

    .tk-skip-link {
        transition: none;
    }
}

/* =============================================================================
   KEYBOARD HELP DIALOG STYLES
   ============================================================================= */

/**
 * Keyboard help dialog overlay.
 */
.tk-keyboard-help-overlay {
    position: fixed;
    inset: 0;
    background-color: var(--tk-grid-overlay-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--tk-grid-z-modal);
}

/**
 * Keyboard help dialog container.
 */
.tk-keyboard-help {
    background: var(--tk-grid-bg);
    color: var(--tk-grid-text-primary);
    border-radius: var(--tk-grid-border-radius);
    box-shadow: var(--tk-grid-shadow-modal);
    max-width: 700px;
    max-height: 80vh;
    width: 90%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/**
 * Keyboard help dialog header.
 */
.tk-keyboard-help-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border-bottom: 1px solid var(--tk-grid-border);
}

.tk-keyboard-help-header h2 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
}

.tk-close-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 8px;
    border-radius: 4px;
    color: var(--tk-grid-text-secondary);
}

.tk-close-btn:hover {
    background-color: var(--tk-grid-bg-alt);
}

/**
 * Keyboard help content area.
 */
.tk-keyboard-help-content {
    padding: 24px;
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 24px;
}

/**
 * Shortcut group section.
 */
.tk-shortcut-group h3 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--tk-grid-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0 0 12px 0;
    display: flex;
    align-items: center;
    gap: 8px;
}

/**
 * Shortcut list.
 */
.tk-shortcut-list {
    margin: 0;
    padding: 0;
}

.tk-shortcut-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid var(--tk-grid-border-light);
}

.tk-shortcut-item:last-child {
    border-bottom: none;
}

.tk-shortcut-item dt {
    flex-shrink: 0;
}

.tk-shortcut-item dd {
    margin: 0;
    color: var(--tk-grid-text-secondary);
    font-size: 0.875rem;
    text-align: right;
}

/**
 * Keyboard key styling.
 */
kbd {
    display: inline-block;
    padding: 2px 6px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.75rem;
    font-weight: 500;
    line-height: 1.25rem;
    color: var(--tk-grid-text-primary);
    background-color: var(--tk-grid-bg-alt);
    border: 1px solid var(--tk-grid-border);
    border-radius: 4px;
    box-shadow: inset 0 -1px 0 var(--tk-grid-border);
}

/**
 * Keyboard help footer.
 */
.tk-keyboard-help-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border-top: 1px solid var(--tk-grid-border);
    background-color: var(--tk-grid-header-bg);
}

.tk-help-tip {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    font-size: 0.875rem;
    color: var(--tk-grid-text-secondary);
}

/* =============================================================================
   BUTTON STYLES
   ============================================================================= */

.tk-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 16px;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: 6px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.tk-btn-primary {
    background-color: var(--tk-primary-color, #2563eb);
    color: white;
}

.tk-btn-primary:hover {
    background-color: var(--tk-primary-hover, #1d4ed8);
}

.tk-btn-primary:focus-visible {
    outline: 2px solid var(--tk-focus-color, #3b82f6);
    outline-offset: 2px;
}

/* =============================================================================
   LOADING AND STATUS INDICATORS
   ============================================================================= */

/**
 * Loading state accessibility.
 */
.tk-datagrid-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px;
    gap: 16px;
}

.tk-datagrid-loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--tk-border-color, #e5e7eb);
    border-top-color: var(--tk-primary-color, #2563eb);
    border-radius: 50%;
    animation: tk-spin 1s linear infinite;
}

@keyframes tk-spin {
    to {
        transform: rotate(360deg);
    }
}

/**
 * Error state accessibility.
 */
.tk-datagrid-error {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px;
    gap: 12px;
    color: var(--tk-error-color, #dc2626);
}

.tk-datagrid-error i {
    font-size: 2rem;
}

.tk-datagrid-retry-btn {
    margin-top: 8px;
    padding: 8px 16px;
    background-color: var(--tk-error-color, #dc2626);
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
}

.tk-datagrid-retry-btn:hover {
    background-color: var(--tk-error-hover, #b91c1c);
}

/**
 * Empty state accessibility.
 */
.tk-datagrid-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px;
    gap: 12px;
    color: var(--tk-text-secondary, #6b7280);
}

.tk-datagrid-empty i {
    font-size: 2.5rem;
    opacity: 0.5;
}

/* =============================================================================
   RESPONSIVE ACCESSIBILITY
   ============================================================================= */

/**
 * Ensure touch targets are at least 44x44px on touch devices.
 */
@media (pointer: coarse) {
    .tk-column-menu-btn,
    .tk-row-expander,
    .tk-row-checkbox {
        min-width: 44px;
        min-height: 44px;
    }

    .tk-select-option {
        min-height: 44px;
    }
}

/* =============================================================================
   PRINT STYLES
   ============================================================================= */

@media print {
    .tk-keyboard-help-overlay,
    .tk-skip-link,
    .tk-sr-only {
        display: none !important;
    }

    .tk-datagrid {
        border: 1px solid #000;
    }

    .tk-grid-row.selected {
        background-color: #f3f4f6 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
