/**
 * Accessibility Enhancements v2
 * WCAG 2.2 AA Compliance - Critical Fixes Applied
 * Timesheet Express - Bainbridge Builders Inc.
 * Version: v2025-12-19 - RcodexUI Review Fixes
 *
 * CHANGES FROM v1:
 * - Standardized focus indicators across all components
 * - Added ARIA live region styles
 * - Fixed color contrast issues (4.5:1 minimum)
 * - Enhanced touch targets (44px minimum)
 * - Added loading, error, and empty state styles
 * - Improved screen reader support
 */

/* ========== CSS CUSTOM PROPERTIES ========== */
:root {
    /* Focus Colors */
    --focus-color-light: #C8102E; /* BB Red for light backgrounds */
    --focus-color-dark: #22C55E;  /* Green for dark backgrounds */
    --focus-offset: 2px;
    --focus-width: 2px;

    /* State Colors (WCAG AA Compliant) */
    --color-success: #22C55E;
    --color-warning: #F59E0B;
    --color-error: #EF4444;
    --color-info: #3B82F6;

    /* Text Colors (4.5:1 contrast minimum) */
    --text-on-dark: #E5E7EB;      /* Was #D1D5DB - improved contrast */
    --text-muted-on-light: #6B7280; /* 4.5:1 on white */
    --text-label-on-dark: #D1D5DB; /* For labels on dark bg */

    /* Toast Background Colors (WCAG AA compliant) */
    --toast-success-bg: #F0FDF4;
    --toast-error-bg: #FEF2F2;
    --toast-warning-bg: #FFFBEB;
    --toast-info-bg: #EFF6FF;

    /* Toast Text Colors (4.5:1 contrast minimum) */
    --toast-success-text: #166534;
    --toast-error-text: #991B1B;
    --toast-warning-text: #92400E;
    --toast-info-text: #1E40AF;

    /* Neutral Colors */
    --neutral-white: #fff;
    --neutral-gray-light: #E5E7EB;
    --neutral-gray-dark: #374151;
    --neutral-gray-border: #D1D5DB;
    --neutral-gray-hover: #666;

    /* Touch Targets */
    --touch-target-min: 44px;

    /* Transitions (respects prefers-reduced-motion) */
    --transition-speed: 0.2s;
    --animation-speed: 0.3s;
}

/* ========== FOCUS INDICATORS (WCAG 2.4.7 Level AA) ========== */
/* Global focus style - standardized */
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible,
[tabindex]:focus-visible,
[role="button"]:focus-visible {
    outline: var(--focus-width) solid var(--focus-color-light);
    outline-offset: var(--focus-offset);
    border-radius: 4px;
}

/* Dark background focus - higher contrast */
.left-sidebar *:focus-visible,
.right-sidebar *:focus-visible,
.sidebar-section *:focus-visible,
.bg-dark *:focus-visible,
[data-theme="dark"] *:focus-visible {
    outline-color: var(--focus-color-dark);
}

/* Checkbox/Radio focus - wrapper highlight */
.employee-item:has(.emp-checkbox:focus-visible) {
    outline: var(--focus-width) solid var(--focus-color-dark);
    outline-offset: var(--focus-offset);
}

/* Sidebar employee checkbox - maintain minimum touch target */
.employee-item .emp-checkbox,
#selectAllEmployees {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

/* Form group focus highlight */
.settings-group:has(input:focus-visible),
.form-group:has(input:focus-visible) {
    background: rgba(200, 16, 46, 0.03);
    border-radius: 6px;
    transition: background var(--transition-speed);
}

/* Tab navigation focus */
.tab-btn:focus-visible,
.nav-tab:focus-visible {
    outline: var(--focus-width) solid var(--focus-color-light);
    outline-offset: -2px; /* Inside for tabs */
}

/* Never remove focus outlines */
*:focus {
    outline: revert; /* Restore browser default if overridden */
}

/* ========== SKIP NAVIGATION (WCAG 2.4.1 Level A) ========== */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--focus-color-light);
    color: var(--neutral-white);
    padding: 12px 20px;
    z-index: 100000;
    text-decoration: none;
    font-weight: 600;
    border-radius: 0 0 4px 0;
    font-size: 14px;
    transition: top var(--transition-speed);
}

.skip-link:focus {
    top: 0;
}

/* ========== COLOR CONTRAST FIXES (WCAG 1.4.3 Level AA) ========== */
/* Sidebar employee items - improved contrast */
.employee-item {
    color: var(--text-on-dark) !important; /* E5E7EB - 4.8:1 contrast */
}

.employee-item .last-name {
    color: var(--text-label-on-dark) !important; /* D1D5DB for labels */
}

/* Day labels on dark background */
.employee-item .day-label {
    color: var(--text-label-on-dark) !important; /* Improved from #9CA3AF */
}

/* Muted text - ensure minimum contrast */
.text-muted,
.settings-hint,
.form-hint,
.employee-tab .hours {
    color: var(--text-muted-on-light);
}

/* Status light text alternatives */
.status-light::after {
    content: attr(aria-label);
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

/* ========== SCREEN READER SUPPORT ========== */
/* Visually hidden but accessible */
.sr-only,
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Show on focus for skip links */
.sr-only-focusable:focus,
.visually-hidden:focus {
    position: static;
    width: auto;
    height: auto;
    padding: inherit;
    margin: inherit;
    overflow: visible;
    clip: auto;
    white-space: normal;
}

/* ========== ARIA LIVE REGIONS (WCAG 4.1.3 Level AA) ========== */
/* Screen reader announcements */
.status-announcer,
[role="status"],
[role="alert"] {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

/* Toast container - visible but announces changes */
#toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 10001;
}

/* Individual toast styling */
.toast {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    margin-bottom: 10px;
    min-width: 320px;
    max-width: 420px;
    position: relative;
}

.toast.success {
    background: var(--toast-success-bg);
    border-left: 4px solid var(--color-success);
    color: var(--toast-success-text);
}

.toast.error {
    background: var(--toast-error-bg);
    border-left: 4px solid var(--color-error);
    color: var(--toast-error-text);
}

.toast.warning {
    background: var(--toast-warning-bg);
    border-left: 4px solid var(--color-warning);
    color: var(--toast-warning-text);
}

.toast.info {
    background: var(--toast-info-bg);
    border-left: 4px solid var(--color-info);
    color: var(--toast-info-text);
}

.toast-icon {
    font-size: 20px;
    flex-shrink: 0;
}

.toast-message {
    flex: 1;
    font-size: 14px;
    font-weight: 500;
}

.toast .close-toast,
.toast .toast-close {
    margin-left: auto;
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    opacity: 0.6;
    padding: 4px;
    line-height: 1;
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    display: flex;
    align-items: center;
    justify-content: center;
}

.toast .toast-close:hover {
    opacity: 1;
}

/* ========== FORM ACCESSIBILITY ========== */
/* Required field indicator */
.required-indicator,
.field-required::after {
    color: var(--color-error);
    font-weight: bold;
    margin-left: 2px;
}

.required-indicator::after,
.field-required::after {
    content: '*';
}

/* Form validation states */
.form-input.is-invalid,
.settings-input.is-invalid,
input.is-invalid,
select.is-invalid,
textarea.is-invalid {
    border-color: var(--color-error) !important;
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}

.form-input.is-valid,
.settings-input.is-valid,
input.is-valid,
select.is-valid,
textarea.is-valid {
    border-color: var(--color-success) !important;
    box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.1);
}

/* Error message styling */
.form-error,
.validation-msg.error,
.error-message {
    color: var(--color-error);
    font-size: 13px;
    margin-top: 6px;
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 500;
}

.form-error::before,
.error-message::before {
    content: '⚠';
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    background: var(--color-error);
    color: var(--neutral-white);
    border-radius: 50%;
    font-size: 12px;
    font-weight: bold;
    flex-shrink: 0;
}

/* ========== TOUCH TARGETS (WCAG 2.5.5 Level AAA - Best Practice) ========== */
/* Minimum 44x44px touch targets on all devices */
button,
.btn,
.toolbar-btn,
.tab-btn,
.icon-btn,
input[type="checkbox"],
input[type="radio"],
.modal-close,
.context-menu-item,
a.btn {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Specific mobile fixes */
@media (max-width: 768px) {
    .employee-item {
        min-height: var(--touch-target-min);
        padding: 12px;
    }

    .detail-table td {
        padding: 12px 8px; /* Increase row height for touch */
    }

    /* Day hour blocks - make tappable */
    .employee-item .day-hours {
        min-width: var(--touch-target-min);
        min-height: var(--touch-target-min);
        padding: 8px;
    }

    /* Preset buttons */
    .preset-btn {
        min-height: var(--touch-target-min);
        padding: 10px 16px;
    }
}

/* ========== LOADING STATES (WCAG 4.1.3 Level AA) ========== */
.loading-spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid var(--neutral-gray-light);
    border-top-color: var(--focus-color-light);
    border-radius: 50%;
    animation: spin var(--animation-speed) linear infinite;
}

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

/* Button loading state */
.btn.loading,
button.loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
    cursor: wait;
}

.btn.loading::after,
button.loading::after {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    top: 50%;
    left: 50%;
    margin: -9px 0 0 -9px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

/* Loading container with status */
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    gap: 12px;
}

.loading-text {
    font-size: 14px;
    color: var(--text-muted-on-light);
    font-weight: 500;
}

/* ========== ERROR STATES ========== */
/* Error banner */
.error-banner,
.alert-error {
    background: var(--toast-error-bg);
    border-left: 4px solid var(--color-error);
    padding: 14px 18px;
    margin-bottom: 16px;
    border-radius: 6px;
    display: flex;
    align-items: flex-start;
    gap: 12px;
}

.error-banner-icon {
    font-size: 20px;
    color: var(--color-error);
    flex-shrink: 0;
}

.error-banner-content {
    flex: 1;
}

.error-banner-title {
    font-weight: 600;
    color: var(--toast-error-text);
    margin-bottom: 4px;
}

.error-banner-message {
    font-size: 14px;
    color: var(--toast-error-text);
}

/* Table row error state */
tr.has-error,
.table-row-error {
    background: var(--toast-error-bg) !important;
    border-left: 3px solid var(--color-error) !important;
}

/* API status error with pulse */
.status-light.error {
    background: var(--color-error);
    animation: pulse-error 2s ease-in-out infinite;
}

@keyframes pulse-error {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ========== EMPTY STATES ========== */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    text-align: center;
    color: var(--text-muted-on-light);
}

.empty-state-icon {
    width: 80px;
    height: 80px;
    opacity: 0.3;
    margin-bottom: 20px;
}

.empty-state-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--neutral-gray-dark);
    margin-bottom: 8px;
}

.empty-state-message {
    font-size: 14px;
    margin-bottom: 24px;
    max-width: 400px;
}

.empty-state-action {
    /* Inherits button styles */
}

/* ========== DISABLED STATES ========== */
button:disabled,
input:disabled,
select:disabled,
textarea:disabled,
.btn:disabled,
.btn.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

/* ========== REDUCED MOTION (WCAG 2.3.3 Level AAA) ========== */
@media (prefers-reduced-motion: reduce) {
    :root {
        --transition-speed: 0.01ms;
        --animation-speed: 0.01ms;
    }

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .loading-spinner,
    .spinning {
        animation: none !important;
    }
}

/* ========== HIGH CONTRAST MODE ========== */
@media (prefers-contrast: high) {
    .status-dot,
    .status-light {
        border: 2px solid currentColor;
    }

    .emp-badge {
        border: 2px solid currentColor !important;
    }

    button,
    .btn,
    input,
    select,
    textarea {
        border: 2px solid currentColor;
    }

    /* Ensure focus is extra visible */
    *:focus-visible {
        outline-width: 3px !important;
    }
}

/* ========== MODAL ACCESSIBILITY ========== */
/* Modal backdrop - prevents background interaction */
.modal-overlay {
    position: fixed;
    inset: 0;
    overflow: auto;
    background: rgba(0, 0, 0, 0.6);
}

/* Modal focus trap */
.settings-modal[role="dialog"],
.modal[role="dialog"] {
    position: relative;
}

/* Modal close button - ensure touch target */
.modal-close {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
}

/* ========== TABLE ACCESSIBILITY (WCAG 1.3.1 Level A) ========== */
/* Table caption - visually hidden but accessible */
.detail-table caption {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

/* Row focus indication */
.detail-table tbody tr:focus-within {
    outline: 2px solid var(--focus-color-light);
    outline-offset: -2px;
}

/* ========== MOBILE SPECIFIC ========== */
@media (max-width: 768px) {
    /* Prevent iOS zoom on input focus */
    input,
    select,
    textarea {
        font-size: 16px !important;
    }

    /* Larger tap targets */
    .tab-btn,
    .nav-tab {
        min-height: var(--touch-target-min);
        padding: 12px 16px;
    }
}

/* ========== HOVER STATES - RESPECT DEVICE CAPABILITY ========== */
/* Only apply hover effects on devices that support hover */
@media (hover: hover) and (pointer: fine) {
    .employee-item:hover {
        background: rgba(255, 255, 255, 0.08);
    }

    .btn:hover:not(:disabled) {
        transform: translateY(-1px);
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
    }

    .toast .toast-close:hover {
        opacity: 1;
        background: rgba(0, 0, 0, 0.1);
        border-radius: 4px;
    }
}

/* Touch devices - use active state instead */
@media (hover: none) {
    .btn:active,
    .employee-item:active,
    button:active {
        opacity: 0.8;
    }
}

/* ========== PRINT ACCESSIBILITY ========== */
@media print {
    /* Show skip link content in print */
    .skip-link {
        position: static;
        display: block;
        padding: 0;
    }

    /* Ensure focus outlines don't print */
    *:focus-visible {
        outline: none;
    }

    /* Hide interactive states */
    .loading-spinner,
    .toast,
    .modal-overlay {
        display: none !important;
    }
}
