/**
 * AGR.UI Components - UI Enhancements for AURORA
 * TAG: SPEC-UI-001-COMPONENTS
 *
 * Design-TAG: Enhanced UI components with accessibility and responsiveness
 * Function-TAG: Provides toast notifications, modals, validation states, loading indicators
 * Test-TAG: Manually tested for WCAG 2.1 AA compliance and responsive behavior
 *
 * WCAG 2.1 AA Compliance:
 * - Color contrast ratios >= 4.5:1 for normal text
 * - Color contrast ratios >= 3:1 for large text and UI components
 * - Focus indicators visible on all interactive elements
 * - ARIA attributes properly configured
 * - Keyboard navigation fully functional
 */

/* ============================================
   DARK MODE THEME
   ============================================ */

/* Dark mode color variables */
:root,
[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #f5f7fb;
    --bg-tertiary: #e9ecef;
    --text-primary: #0b1f33;
    --text-secondary: #5f6c7b;
    --text-tertiary: #8b95a5;
    --border-color: #d7e0ea;
    --border-color-light: #e8eeF5;
    --shadow-color: rgba(15, 32, 50, 0.08);
    --overlay-color: rgba(15, 32, 50, 0.4);
}

[data-theme="dark"] {
    --bg-primary: #1a202c;
    --bg-secondary: #2d3748;
    --bg-tertiary: #4a5568;
    --text-primary: #f7fafc;
    --text-secondary: #cbd5e0;
    --text-tertiary: #a0aec0;
    --border-color: #4a5568;
    --border-color-light: #2d3748;
    --shadow-color: rgba(0, 0, 0, 0.3);
    --overlay-color: rgba(0, 0, 0, 0.6);

    --primary-color: #63b3ed;
    --primary-dark: #4299e1;
    --success-color: #48bb78;
    --warning-color: #ecc94b;
    --danger-color: #fc8181;
}

/* Smooth theme transitions */
*:not(.agr-modal):not(.agr-modal-backdrop):not(.agr-toast) {
    transition: background-color 0.3s ease,
                color 0.3s ease,
                border-color 0.3s ease,
                box-shadow 0.3s ease;
}

/* Skip navigation link (keyboard only) */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

.skip-to-content {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--primary-color);
    color: white;
    padding: 8px 16px;
    z-index: 10000;
    text-decoration: none;
    font-weight: 600;
}

.skip-to-content:focus {
    top: 0;
}

/* ============================================
   TOAST NOTIFICATIONS
   ============================================ */

.agr-toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 9999;
    max-width: 400px;
    width: 100%;
    pointer-events: none;
}

.agr-toast {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem;
    margin-bottom: 0.75rem;
    border-radius: 0.5rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    pointer-events: auto;
    opacity: 0;
    transform: translateX(100%);
    transition: opacity 0.2s ease, transform 0.2s ease;
    position: relative;
    overflow: hidden;
}

.agr-toast-show {
    opacity: 1;
    transform: translateX(0);
}

.agr-toast-hide {
    opacity: 0;
    transform: translateX(100%);
}

/* Toast types */
.agr-toast-success {
    background: #d1fae5;
    color: #065f46;
    border-left: 4px solid #059669;
}

[data-theme="dark"] .agr-toast-success {
    background: #064e3b;
    color: #d1fae5;
    border-left-color: #10b981;
}

.agr-toast-error {
    background: #fee2e2;
    color: #991b1b;
    border-left: 4px solid #dc2626;
}

[data-theme="dark"] .agr-toast-error {
    background: #7f1d1d;
    color: #fee2e2;
    border-left-color: #f87171;
}

.agr-toast-warning {
    background: #fef3c7;
    color: #92400e;
    border-left: 4px solid #f59e0b;
}

[data-theme="dark"] .agr-toast-warning {
    background: #78350f;
    color: #fef3c7;
    border-left-color: #fbbf24;
}

.agr-toast-info {
    background: #dbeafe;
    color: #1e40af;
    border-left: 4px solid #2563eb;
}

[data-theme="dark"] .agr-toast-info {
    background: #1e3a8a;
    color: #dbeafe;
    border-left-color: #60a5fa;
}

.agr-toast-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
}

.agr-toast-close {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: transparent;
    border: none;
    color: inherit;
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    padding: 0.25rem;
    opacity: 0.6;
    transition: opacity 0.2s;
}

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

/* ============================================
   MODAL DIALOGS
   ============================================ */

.agr-modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--overlay-color);
    z-index: 9998;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.agr-modal-backdrop-show {
    opacity: 1;
}

.agr-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9998;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    pointer-events: none;
}

.agr-modal-content {
    position: relative;
    background: var(--bg-primary);
    border-radius: 0.75rem;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    pointer-events: auto;
    opacity: 0;
    transform: scale(0.95);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.agr-modal-show .agr-modal-content {
    opacity: 1;
    transform: scale(1);
}

.agr-modal-hide .agr-modal-content {
    opacity: 0;
    transform: scale(0.95);
}

.agr-modal-header {
    padding: 1.5rem 1.5rem 1rem;
    border-bottom: 1px solid var(--border-color);
}

.agr-modal-title {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
}

.agr-modal-body {
    padding: 1.5rem;
    color: var(--text-primary);
    line-height: 1.6;
}

.agr-modal-footer {
    padding: 1rem 1.5rem 1.5rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
}

.agr-modal-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.25rem;
    border-radius: 0.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s, color 0.2s;
}

.agr-modal-close:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

/* ============================================
   FORM VALIDATION
   ============================================ */

.is-valid {
    border-color: var(--success-color) !important;
}

.is-valid:focus {
    box-shadow: 0 0 0 3px rgba(31, 138, 112, 0.1) !important;
}

.is-invalid {
    border-color: var(--danger-color) !important;
}

.is-invalid:focus {
    box-shadow: 0 0 0 3px rgba(184, 50, 47, 0.1) !important;
}

.is-pending {
    border-color: var(--warning-color) !important;
}

.invalid-feedback {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.875rem;
    color: var(--danger-color);
}

.valid-feedback {
    display: none;
    margin-top: 0.25rem;
    font-size: 0.875rem;
    color: var(--success-color);
    align-items: center;
    gap: 0.25rem;
}

.valid-feedback svg {
    width: 16px;
    height: 16px;
}

.validation-error-summary {
    margin-bottom: 1.5rem;
}

.validation-error-summary h3 {
    margin: 0 0 0.5rem 0;
    font-size: 1rem;
    font-weight: 600;
}

.validation-error-list {
    margin: 0;
    padding-left: 1.25rem;
}

.validation-error-list li {
    margin-bottom: 0.25rem;
}

.validation-error-list a {
    color: inherit;
    text-decoration: underline;
}

.validation-error-list a:hover {
    color: var(--text-primary);
}

/* ============================================
   LOADING STATES
   ============================================ */

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

.btn.is-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 1rem;
    height: 1rem;
    margin: -0.5rem 0 0 -0.5rem;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: agr-btn-spinner 0.6s linear infinite;
}

@keyframes agr-btn-spinner {
    to {
        transform: rotate(360deg);
    }
}

/* Loading spinner */
.agr-spinner {
    display: inline-block;
    width: 2rem;
    height: 2rem;
    border: 3px solid var(--border-color);
    border-right-color: var(--primary-color);
    border-radius: 50%;
    animation: agr-spin 0.8s linear infinite;
}

.agr-spinner-sm {
    width: 1rem;
    height: 1rem;
    border-width: 2px;
}

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

/* Loading overlay */
.agr-loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--overlay-color);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(2px);
}

/* Skeleton screens */
.agr-skeleton {
    background: linear-gradient(
        90deg,
        var(--bg-secondary) 0%,
        var(--bg-tertiary) 50%,
        var(--bg-secondary) 100%
    );
    background-size: 200% 100%;
    animation: agr-skeleton-shimmer 1.5s ease-in-out infinite;
    border-radius: 0.25rem;
}

@keyframes agr-skeleton-shimmer {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

.agr-skeleton-text {
    height: 1em;
    margin-bottom: 0.5rem;
}

.agr-skeleton-title {
    height: 1.5em;
    margin-bottom: 1rem;
    width: 60%;
}

.agr-skeleton-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.agr-skeleton-card {
    height: 150px;
    border-radius: 0.5rem;
}

/* Progress bars */
.agr-progress {
    height: 0.5rem;
    background: var(--bg-secondary);
    border-radius: 0.25rem;
    overflow: hidden;
}

.agr-progress-bar {
    height: 100%;
    background: var(--primary-color);
    transition: width 0.3s ease;
}

.agr-progress-bar.indeterminate {
    width: 100%;
    background: linear-gradient(
        90deg,
        transparent,
        var(--primary-color),
        transparent
    );
    background-size: 50% 100%;
    animation: agr-progress-indeterminate 1.5s ease-in-out infinite;
}

@keyframes agr-progress-indeterminate {
    0% {
        background-position: 100% 0;
    }
    100% {
        background-position: -100% 0;
    }
}

/* Circular progress */
.agr-progress-circle {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.agr-progress-circle svg {
    transform: rotate(-90deg);
}

.agr-progress-circle-bg {
    fill: none;
    stroke: var(--bg-secondary);
    stroke-width: 3;
}

.agr-progress-circle-value {
    fill: none;
    stroke: var(--primary-color);
    stroke-width: 3;
    stroke-linecap: round;
    transition: stroke-dashoffset 0.3s ease;
}

.agr-progress-circle-text {
    position: absolute;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
}

/* ============================================
   THEME TOGGLE BUTTON
   ============================================ */

.agr-theme-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.agr-theme-toggle-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.agr-theme-toggle-label {
    font-size: 0.875rem;
}

/* ============================================
   MOBILE NAVIGATION
   ============================================ */

.agr-mobile-nav-toggle {
    display: none;
    background: transparent;
    border: none;
    padding: 0.5rem;
    cursor: pointer;
    border-radius: 0.375rem;
    color: var(--text-primary);
}

.agr-mobile-nav-toggle:hover,
.agr-mobile-nav-toggle:focus {
    background: var(--bg-secondary);
}

.agr-mobile-nav-toggle-icon {
    display: block;
    width: 24px;
    height: 24px;
}

/* ============================================
   RESPONSIVE TABLES
   ============================================ */

@media (max-width: 768px) {
    .agr-table-responsive {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .agr-table-card {
        display: block;
    }

    .agr-table-card thead {
        display: none;
    }

    .agr-table-card tbody,
    .agr-table-card tr,
    .agr-table-card td {
        display: block;
        width: 100%;
    }

    .agr-table-card tr {
        margin-bottom: 1rem;
        border: 1px solid var(--border-color);
        border-radius: 0.5rem;
        padding: 1rem;
        background: var(--bg-primary);
    }

    .agr-table-card td {
        text-align: left !important;
        padding: 0.5rem 0 !important;
        border: none !important;
    }

    .agr-table-card td::before {
        content: attr(data-label);
        font-weight: 600;
        display: inline-block;
        width: 100px;
        color: var(--text-secondary);
    }

    /* Show mobile nav toggle */
    .agr-mobile-nav-toggle {
        display: block;
    }

    /* Hide desktop nav */
    .nav {
        display: none;
    }

    /* Mobile nav drawer */
    .agr-mobile-nav {
        display: block;
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        width: 280px;
        max-width: 85vw;
        background: var(--bg-primary);
        box-shadow: -2px 0 8px rgba(0, 0, 0, 0.1);
        z-index: 9999;
        transform: translateX(100%);
        transition: transform 0.3s ease;
        padding: 1rem;
        overflow-y: auto;
    }

    .agr-mobile-nav.is-open {
        transform: translateX(0);
    }

    .agr-mobile-nav a {
        display: block;
        padding: 0.75rem 1rem;
        color: var(--text-primary);
        text-decoration: none;
        border-radius: 0.375rem;
        margin-bottom: 0.25rem;
        font-weight: 500;
    }

    .agr-mobile-nav a:hover,
    .agr-mobile-nav a:focus {
        background: var(--bg-secondary);
    }

    /* Toast adjustments */
    .agr-toast-container {
        left: 1rem;
        right: 1rem;
        max-width: none;
    }

    /* Modal adjustments */
    .agr-modal-content {
        margin: 1rem;
        max-height: calc(100vh - 2rem);
    }
}

/* ============================================
   ACCESSIBILITY ENHANCEMENTS
   ============================================ */

/* Focus indicators */
*:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --border-color: #000000;
    }

    [data-theme="dark"] {
        --border-color: #ffffff;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Print styles */
@media print {
    .agr-toast-container,
    .agr-modal,
    .agr-modal-backdrop,
    .agr-theme-toggle,
    .agr-mobile-nav-toggle {
        display: none !important;
    }

    .agr-modal-content {
        display: none;
    }
}

/* ============================================
   UTILITY CLASSES
   ============================================ */

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

.visually-hidden-focusable:not(:focus):not(:focus-within) {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

.no-scroll {
    overflow: hidden !important;
}

.z-index-high {
    z-index: 9999;
}

.position-relative {
    position: relative;
}

/* Form Leave Guard Indicator (SPEC-UIX-002 Milestone 4) */
.form-dirty-indicator {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--bg-primary);
    padding: 0.75rem 1rem;
    border-bottom: 2px solid var(--warning-color);
    border-radius: 4px;
    margin-bottom: 1rem;
    text-align: center;
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.form-dirty-indicator .badge {
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.form-dirty-indicator .badge::before {
    content: '⚠';
    font-size: 1rem;
}

/* Body class for global unsaved changes state */
body.has-unsaved-changes::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: var(--warning-color);
    z-index: 9999;
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.7;
    }
}

/* ============================================
   SESSION TIMEOUT NOTICES
   ============================================ */

/* assets/js/session-keepalive.js: the idle-timeout countdown and the
   signed-out notice. Bottom-right, clear of the toasts at the top, and above
   the grant-type section dialog (z-index 2000) so it shows while editing. */
.agr-session-notice-host {
    position: fixed;
    right: 1rem;
    bottom: 1rem;
    z-index: 10000;
    width: calc(100% - 2rem);
    max-width: 420px;
}

.agr-session-notice {
    flex-wrap: wrap;
    margin-bottom: 0;
}

.agr-session-notice-text {
    flex: 1 1 14rem;
}

.agr-session-notice a {
    color: inherit;
    font-weight: 600;
    text-decoration: underline;
}
