/* ==========================================================================
   Hastama Global UX Infrastructure
   --------------------------------------------------------------------------
   Central design tokens, skeleton system, page loader, button loading,
   empty states, error states, and reduced-motion support.
   
   This file should be included AFTER existing page-specific CSS but BEFORE
   page-specific JS. It provides reusable patterns that enhance — never
   replace — the existing Hastama design.
   
   Usage:
     <link rel="stylesheet" href="/static/css/hastama-ux.css">
   
   Rules:
     - Only contains utility/global patterns
     - Never overrides existing page styles blindly
     - Uses existing Hastama design language
     - Fully RTL-compatible
     - Respects prefers-reduced-motion
     - Works with dark mode
   ========================================================================== */

/* ──────────────────────────────────────────────────────────────────────────
   1) DESIGN TOKENS
   ────────────────────────────────────────────────────────────────────────── */
:root {
    /* Spacing scale */
    --h-ux-space-xs: 4px;
    --h-ux-space-sm: 8px;
    --h-ux-space-md: 16px;
    --h-ux-space-lg: 24px;
    --h-ux-space-xl: 32px;

    /* Radii */
    --h-ux-radius-sm: 8px;
    --h-ux-radius-md: 12px;
    --h-ux-radius-lg: 16px;
    --h-ux-radius-pill: 999px;

    /* Transitions */
    --h-ux-transition-fast: 150ms ease;
    --h-ux-transition-normal: 250ms ease;
    --h-ux-transition-slow: 400ms ease;

    /* Skeleton */
    --h-ux-skeleton-base: #e8edf2;
    --h-ux-skeleton-shine: #f5f7fa;

    /* Focus ring */
    --h-ux-focus-ring: 0 0 0 3px rgba(47, 124, 246, 0.35);

    /* Status colors (used by skeletons, states) */
    --h-ux-success: #10b981;
    --h-ux-warning: #f59e0b;
    --h-ux-error: #ef4444;
    --h-ux-info: #3b82f6;
}

body.dark-mode {
    --h-ux-skeleton-base: #1e293b;
    --h-ux-skeleton-shine: #334155;
    --h-ux-focus-ring: 0 0 0 3px rgba(94, 167, 255, 0.35);
}

/* ──────────────────────────────────────────────────────────────────────────
   2) SKELETON SYSTEM
   ──────────────────────────────────────────────────────────────────────────
   Structural placeholders that match final layout geometry.
   Apply .h-ux-skeleton to any element to show it as a placeholder.
   ------------------------------------------------------------------------ */

@keyframes h-ux-shimmer {
    0%   { background-position: -400px 0; }
    100% { background-position: 400px 0; }
}

.h-ux-skeleton,
.h-ux-skeleton * {
    color: transparent !important;
    pointer-events: none;
    user-select: none;
}

.h-ux-skeleton {
    background: var(--h-ux-skeleton-base);
    border-radius: var(--h-ux-radius-sm);
    position: relative;
    overflow: hidden;
    min-height: 14px;
}

.h-ux-skeleton::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
        90deg,
        transparent 0%,
        var(--h-ux-skeleton-shine) 40%,
        var(--h-ux-skeleton-shine) 60%,
        transparent 100%
    );
    background-size: 800px 100%;
    animation: h-ux-shimmer 1.6s infinite linear;
}

/* Skeleton shape variants */
.h-ux-skeleton--text {
    height: 14px;
    width: 100%;
    border-radius: 4px;
}

.h-ux-skeleton--text-sm {
    height: 10px;
    width: 60%;
    border-radius: 3px;
}

.h-ux-skeleton--heading {
    height: 22px;
    width: 50%;
    border-radius: 6px;
}

.h-ux-skeleton--circle {
    border-radius: 50%;
    width: 48px;
    height: 48px;
}

.h-ux-skeleton--avatar {
    border-radius: 50%;
    width: 40px;
    height: 40px;
    flex-shrink: 0;
}

.h-ux-skeleton--btn {
    height: 42px;
    width: 120px;
    border-radius: var(--h-ux-radius-sm);
}

.h-ux-skeleton--card {
    border-radius: var(--h-ux-radius-md);
    padding: var(--h-ux-space-lg);
    min-height: 100px;
}

.h-ux-skeleton--row {
    display: flex;
    align-items: center;
    gap: var(--h-ux-space-md);
    padding: var(--h-ux-space-md);
    border-radius: var(--h-ux-radius-sm);
}

.h-ux-skeleton--input {
    height: 42px;
    width: 100%;
    border-radius: var(--h-ux-radius-sm);
}

/* Skeleton table rows */
.h-ux-skeleton-table-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
    gap: var(--h-ux-space-md);
    padding: var(--h-ux-space-sm) var(--h-ux-space-md);
    border-bottom: 1px solid transparent;
}

.h-ux-skeleton-table-row > * {
    height: 14px;
    border-radius: 4px;
}

/* Reduced motion: disable shimmer */
@media (prefers-reduced-motion: reduce) {
    .h-ux-skeleton::after {
        animation: none;
        background: none;
    }
}

/* ──────────────────────────────────────────────────────────────────────────
   3) PAGE LOADER / BRAND LOADER
   ──────────────────────────────────────────────────────────────────────────
   A minimal global loading overlay for initial page load.
   Show on <body> with class="h-ux-page-loading", remove when ready.
   ------------------------------------------------------------------------ */

.h-ux-page-loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 9000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #f8fafc;
    opacity: 1;
    transition: opacity var(--h-ux-transition-normal);
    pointer-events: auto;
}

body.dark-mode .h-ux-page-loading-overlay {
    background: #0f172a;
}

.h-ux-page-loading-overlay.h-ux-page-loading--exit {
    opacity: 0;
    pointer-events: none;
}

.h-ux-page-loading-logo {
    width: 56px;
    height: 56px;
    margin-bottom: var(--h-ux-space-md);
    animation: h-ux-pulse 1.4s ease-in-out infinite;
}

.h-ux-page-loading-text {
    font-size: 14px;
    color: #64748b;
    font-weight: 600;
}

@keyframes h-ux-pulse {
    0%, 100% { opacity: 0.5; transform: scale(0.96); }
    50%      { opacity: 1;   transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
    .h-ux-page-loading-logo { animation: none; opacity: 1; }
}

/* ──────────────────────────────────────────────────────────────────────────
   4) BUTTON LOADING SYSTEM
   ──────────────────────────────────────────────────────────────────────────
   Add .h-ux-loading to any button to show loading state.
   The button is disabled and shows a spinner + loading text.
   Original text is stored in data-h-ux-orig-text.
   ------------------------------------------------------------------------ */

.h-ux-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-family: 'Vazir', Tahoma, sans-serif;
    font-weight: 700;
    cursor: pointer;
    border: none;
    outline: none;
    transition:
        background var(--h-ux-transition-fast),
        box-shadow var(--h-ux-transition-fast),
        transform var(--h-ux-transition-fast),
        opacity var(--h-ux-transition-fast);
    -webkit-tap-highlight-color: transparent;
}

.h-ux-btn:focus-visible {
    box-shadow: var(--h-ux-focus-ring);
}

.h-ux-btn:disabled {
    cursor: not-allowed;
    opacity: 0.7;
}

/* Loading state */
.h-ux-btn.h-ux-loading {
    pointer-events: none;
    min-width: 120px;
}

.h-ux-btn.h-ux-loading .h-ux-btn-text {
    visibility: hidden;
}

.h-ux-btn .h-ux-btn-spinner {
    display: none;
    width: 18px;
    height: 18px;
    border: 2.5px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: h-ux-spin 0.7s linear infinite;
    position: absolute;
}

.h-ux-btn.h-ux-loading .h-ux-btn-spinner {
    display: block;
}

/* Success state */
.h-ux-btn.h-ux-success {
    background: var(--h-ux-success) !important;
    color: #fff !important;
}

.h-ux-btn.h-ux-success .h-ux-btn-text {
    visibility: hidden;
}

.h-ux-btn .h-ux-btn-success-icon {
    display: none;
}

.h-ux-btn.h-ux-success .h-ux-btn-success-icon {
    display: inline-flex;
}

/* Loading text overlay */
.h-ux-btn .h-ux-btn-loading-text {
    display: none;
    position: absolute;
    inset: 0;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-family: inherit;
    font-weight: inherit;
    font-size: inherit;
    color: inherit;
}

.h-ux-btn.h-ux-loading .h-ux-btn-loading-text {
    display: inline-flex;
}

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

@media (prefers-reduced-motion: reduce) {
    .h-ux-btn .h-ux-btn-spinner { animation-duration: 1.4s; }
}

/* ──────────────────────────────────────────────────────────────────────────
   5) EMPTY STATE
   ──────────────────────────────────────────────────────────────────────────
   Reusable empty state component for tables, lists, search results.
   Usage: <div class="h-ux-empty-state">...</div>
   ------------------------------------------------------------------------ */

.h-ux-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--h-ux-space-xl) var(--h-ux-space-md);
    text-align: center;
    min-height: 140px;
}

.h-ux-empty-state__icon {
    width: 56px;
    height: 56px;
    margin-bottom: var(--h-ux-space-md);
    color: #cbd5e1;
    opacity: 0.7;
}

body.dark-mode .h-ux-empty-state__icon {
    color: #475569;
}

.h-ux-empty-state__title {
    font-size: 16px;
    font-weight: 800;
    color: #334155;
    margin: 0 0 var(--h-ux-space-xs);
}

body.dark-mode .h-ux-empty-state__title {
    color: #e2e8f0;
}

.h-ux-empty-state__desc {
    font-size: 13px;
    color: #64748b;
    margin: 0 0 var(--h-ux-space-md);
    max-width: 320px;
    line-height: 1.7;
}

.h-ux-empty-state__action {
    margin-top: var(--h-ux-space-sm);
}

/* ──────────────────────────────────────────────────────────────────────────
   6) ERROR STATE
   ──────────────────────────────────────────────────────────────────────────
   Inline error state with retry capability.
   Usage: <div class="h-ux-error-state">...</div>
   ------------------------------------------------------------------------ */

.h-ux-error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--h-ux-space-xl) var(--h-ux-space-md);
    text-align: center;
    min-height: 120px;
    border: 1px dashed rgba(239, 68, 68, 0.3);
    border-radius: var(--h-ux-radius-md);
    background: rgba(239, 68, 68, 0.03);
}

.h-ux-error-state__icon {
    width: 40px;
    height: 40px;
    margin-bottom: var(--h-ux-space-sm);
    color: var(--h-ux-error);
    opacity: 0.8;
}

.h-ux-error-state__title {
    font-size: 15px;
    font-weight: 800;
    color: #1e293b;
    margin: 0 0 var(--h-ux-space-xs);
}

body.dark-mode .h-ux-error-state__title {
    color: #f1f5f9;
}

.h-ux-error-state__desc {
    font-size: 13px;
    color: #64748b;
    margin: 0 0 var(--h-ux-space-md);
    line-height: 1.7;
}

.h-ux-error-state__action {
    margin-top: var(--h-ux-space-sm);
}

/* ──────────────────────────────────────────────────────────────────────────
   7) INLINE LOADING INDICATOR
   ──────────────────────────────────────────────────────────────────────────
   Small spinner for inline/section loading without blocking the page.
   ------------------------------------------------------------------------ */

.h-ux-inline-loader {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--h-ux-space-lg);
    gap: var(--h-ux-space-sm);
    color: #64748b;
    font-size: 13px;
    font-weight: 600;
}

.h-ux-inline-loader__spinner {
    width: 20px;
    height: 20px;
    border: 2.5px solid #e2e8f0;
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: h-ux-spin 0.7s linear infinite;
}

body.dark-mode .h-ux-inline-loader__spinner {
    border-color: #334155;
    border-top-color: #60a5fa;
}

@media (prefers-reduced-motion: reduce) {
    .h-ux-inline-loader__spinner { animation-duration: 1.4s; }
}

/* ──────────────────────────────────────────────────────────────────────────
   8) SECTION REFRESH INDICATOR
   ──────────────────────────────────────────────────────────────────────────
   Tiny indicator that appears during background refresh without
   destroying existing content.
   ------------------------------------------------------------------------ */

.h-ux-refresh-indicator {
    position: absolute;
    top: var(--h-ux-space-sm);
    left: var(--h-ux-space-sm);
    width: 24px;
    height: 24px;
    border: 2px solid rgba(59, 130, 246, 0.2);
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: h-ux-spin 0.8s linear infinite;
    opacity: 0;
    transition: opacity var(--h-ux-transition-fast);
    pointer-events: none;
    z-index: 10;
}

.h-ux-refresh-indicator.h-ux-visible {
    opacity: 1;
}

@media (prefers-reduced-motion: reduce) {
    .h-ux-refresh-indicator { animation-duration: 1.4s; }
}

/* ──────────────────────────────────────────────────────────────────────────
   9) FOCUS MANAGEMENT & ACCESSIBILITY UTILITIES
   ────────────────────────────────────────────────────────────────────────── */

/* Skip to main content link */
.h-ux-skip-link {
    position: fixed;
    top: -100%;
    right: var(--h-ux-space-md);
    z-index: 10000;
    padding: var(--h-ux-space-sm) var(--h-ux-space-md);
    background: #1e293b;
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    border-radius: 0 0 var(--h-ux-radius-sm) var(--h-ux-radius-sm);
    text-decoration: none;
    transition: top var(--h-ux-transition-fast);
}

.h-ux-skip-link:focus {
    top: 0;
    outline: 2px solid var(--h-ux-info);
    outline-offset: 2px;
}

/* Screen reader only */
.h-ux-sr-only {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Live region for announcements */
[aria-live].h-ux-aria-busy {
    /* Visual hint that content is loading - purely for AT */
}

/* ──────────────────────────────────────────────────────────────────────────
   10) RESPONSIVE UTILITIES
   ────────────────────────────────────────────────────────────────────────── */

/* Prevent text overflow */
.h-ux-truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.h-ux-truncate-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Touch-friendly minimum tap targets */
@media (pointer: coarse) {
    .h-ux-btn {
        min-height: 44px;
    }
}

/* ──────────────────────────────────────────────────────────────────────────
   11) FORM FEEDBACK UTILITIES
   ────────────────────────────────────────────────────────────────────────── */

.h-ux-field-error {
    border-color: var(--h-ux-error) !important;
    box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.15) !important;
}

.h-ux-field-success {
    border-color: var(--h-ux-success) !important;
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.15) !important;
}

.h-ux-field-msg {
    font-size: 12px;
    font-weight: 600;
    margin-top: var(--h-ux-space-xs);
    line-height: 1.5;
}

.h-ux-field-msg--error {
    color: var(--h-ux-error);
}

.h-ux-field-msg--success {
    color: var(--h-ux-success);
}

/* ──────────────────────────────────────────────────────────────────────────
   12) MODAL ENHANCEMENTS
   ──────────────────────────────────────────────────────────────────────────
   Transitions and focus management for modal/dialog elements.
   ------------------------------------------------------------------------ */

.h-ux-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.4);
    z-index: 8000;
    opacity: 0;
    transition: opacity var(--h-ux-transition-normal);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.h-ux-modal-backdrop.h-ux-active {
    opacity: 1;
}

/* ──────────────────────────────────────────────────────────────────────────
   13) PAGE TRANSITION
   ──────────────────────────────────────────────────────────────────────────
   Subtle fade-in for page content after initial load.
   Apply .h-ux-page-enter to <main> content area.
   ------------------------------------------------------------------------ */

.h-ux-page-enter {
    animation: h-ux-fade-in var(--h-ux-transition-slow) ease both;
}

@keyframes h-ux-fade-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
    .h-ux-page-enter {
        animation: none;
        opacity: 1;
    }
}

/* ──────────────────────────────────────────────────────────────────────────
   14) CONFIRM DIALOG
   ──────────────────────────────────────────────────────────────────────────
   Reusable confirm dialog for destructive actions.
   Usage: call window.HastamaConfirm({ title, message, onConfirm })
   HTML is injected dynamically.
   ------------------------------------------------------------------------ */

.h-ux-confirm-overlay {
    position: fixed;
    inset: 0;
    z-index: 8500;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(15, 23, 42, 0.45);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    opacity: 0;
    transition: opacity var(--h-ux-transition-normal);
    padding: var(--h-ux-space-md);
}

.h-ux-confirm-overlay.h-ux-active {
    opacity: 1;
}

.h-ux-confirm-card {
    background: #fff;
    border-radius: var(--h-ux-radius-lg);
    box-shadow: 0 25px 60px rgba(0, 0, 0, 0.15);
    max-width: 400px;
    width: 100%;
    padding: var(--h-ux-space-lg);
    text-align: center;
    transform: scale(0.95);
    transition: transform var(--h-ux-transition-normal);
}

.h-ux-confirm-overlay.h-ux-active .h-ux-confirm-card {
    transform: scale(1);
}

body.dark-mode .h-ux-confirm-card {
    background: #1e293b;
    color: #e2e8f0;
}

.h-ux-confirm-icon {
    width: 48px;
    height: 48px;
    margin: 0 auto var(--h-ux-space-md);
    color: var(--h-ux-warning);
}

.h-ux-confirm-title {
    font-size: 18px;
    font-weight: 800;
    margin: 0 0 var(--h-ux-space-sm);
}

.h-ux-confirm-message {
    font-size: 14px;
    color: #64748b;
    margin: 0 0 var(--h-ux-space-lg);
    line-height: 1.7;
}

body.dark-mode .h-ux-confirm-message {
    color: #94a3b8;
}

.h-ux-confirm-actions {
    display: flex;
    gap: var(--h-ux-space-sm);
    justify-content: center;
}

.h-ux-confirm-cancel {
    padding: 10px 24px;
    border: 1px solid #e2e8f0;
    border-radius: var(--h-ux-radius-sm);
    background: transparent;
    color: #475569;
    font-family: 'Vazir', Tahoma, sans-serif;
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
    transition: background var(--h-ux-transition-fast);
    min-height: 42px;
}

.h-ux-confirm-cancel:hover {
    background: #f1f5f9;
}

body.dark-mode .h-ux-confirm-cancel {
    border-color: #334155;
    color: #94a3b8;
}

body.dark-mode .h-ux-confirm-cancel:hover {
    background: #334155;
}

.h-ux-confirm-ok {
    padding: 10px 24px;
    border: none;
    border-radius: var(--h-ux-radius-sm);
    background: var(--h-ux-error);
    color: #fff;
    font-family: 'Vazir', Tahoma, sans-serif;
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
    transition: filter var(--h-ux-transition-fast);
    min-height: 42px;
}

.h-ux-confirm-ok:hover {
    filter: brightness(1.08);
}

@media (prefers-reduced-motion: reduce) {
    .h-ux-confirm-overlay { transition: none; }
    .h-ux-confirm-card { transition: none; }
}

/* ──────────────────────────────────────────────────────────────────────────
   15) UNIVERSAL REDUCED MOTION
   ────────────────────────────────────────────────────────────────────────── */

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

    /* Allow critical animations */
    .h-ux-page-loading-overlay,
    .h-ux-skeleton::after,
    .h-ux-btn .h-ux-btn-spinner,
    .h-ux-inline-loader__spinner,
    .h-ux-refresh-indicator {
        animation-duration: 1.4s !important;
    }
}
