/* ═══════════════════════════════════════════════════════════════════════════════
   CRM UX Enhancements - Mobile Optimization
   Phase 1-5: Accessibility, Performance, Touch Targets, Forms, UI
   Based on comprehensive UX audit (2026-11-04)
   ═══════════════════════════════════════════════════════════════════════════════ */

/* ─────────────────────────────────────────────────────────────────────────────
   PHASE 1: ACCESSIBILITY
   ───────────────────────────────────────────────────────────────────────────── */

/* Global focus styles for keyboard navigation */
@media (max-width: 768px) {
  /* Focus indicators */
  button:focus-visible,
  .btn:focus-visible,
  .bt:focus-visible,
  a:focus-visible,
  input:focus-visible,
  select:focus-visible,
  textarea:focus-visible {
    outline: 3px solid rgba(239, 75, 26, 0.6) !important;
    outline-offset: 2px !important;
    box-shadow: 0 0 0 4px rgba(239, 75, 26, 0.2) !important;
  }

  /* Skip link for screen readers */
  .ptf-skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--pri, #ef4b1a);
    color: #fff;
    padding: 8px 16px;
    text-decoration: none;
    z-index: 99999;
    border-radius: 0 8px 8px 0;
    font-weight: 800;
  }

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

  /* Live region for dynamic content */
  .ptf-live-region {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
  }

  /* Improve contrast for error messages */
  .lerr, .error-message {
    color: #dc2626 !important;
    background: #fef2f2 !important;
    border: 1px solid #dc2626 !important;
    padding: 10px 14px !important;
    border-radius: 10px !important;
    font-weight: 800 !important;
  }

  /* Improve contrast for success messages */
  .success-message {
    color: #059669 !important;
    background: #ecfdf5 !important;
    border: 1px solid #059669 !important;
    padding: 10px 14px !important;
    border-radius: 10px !important;
    font-weight: 800 !important;
  }
}

/* ─────────────────────────────────────────────────────────────────────────────
   PHASE 2: TOUCH TARGETS
   ───────────────────────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
  /* Ensure minimum touch target size (44×44px) */
  button,
  .btn,
  .bt,
  .bt-o,
  .btn-l,
  .ba,
  input[type="button"],
  input[type="submit"],
  input[type="reset"],
  a.btn,
  a.bt {
    min-height: 46px !important;
    min-width: 44px !important;
    padding: 12px 16px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  /* Form inputs - 16px font to prevent iOS zoom */
  select,
  textarea,
  input[type="text"],
  input[type="number"],
  input[type="password"],
  input[type="email"],
  input[type="date"],
  input[type="search"],
  input[type="tel"],
  input[type="url"] {
    min-height: 46px !important;
    font-size: 16px !important; /* Prevents iOS auto-zoom */
    padding: 12px 14px !important;
    border-radius: 12px !important;
  }

  /* Icon buttons */
  .icon-btn,
  .tb-icons button,
  #tbIcons button {
    min-height: 44px !important;
    min-width: 44px !important;
    padding: 10px !important;
  }

  /* Table action buttons */
  .tb2 .ba,
  .tb2 button {
    min-height: 40px !important;
    padding: 8px 12px !important;
  }

  /* Bottom nav items */
  .mnv-item,
  #mnvBar button {
    min-height: 56px !important;
    min-width: 56px !important;
    padding: 8px !important;
  }

  /* Close buttons */
  .close-btn,
  .md-close,
  [aria-label="بستن"] {
    min-height: 44px !important;
    min-width: 44px !important;
  }
}

/* ─────────────────────────────────────────────────────────────────────────────
   PHASE 3: PERFORMANCE - SKELETON LOADING
   ───────────────────────────────────────────────────────────────────────────── */

/* Skeleton loading animation */
.ptf-skeleton {
  background: linear-gradient(
    90deg,
    rgba(0, 0, 0, 0.06) 25%,
    rgba(0, 0, 0, 0.1) 50%,
    rgba(0, 0, 0, 0.06) 75%
  );
  background-size: 200% 100%;
  animation: ptf-shimmer 1.5s infinite;
  border-radius: 8px;
}

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

/* Skeleton variants */
.ptf-skeleton-text {
  height: 16px;
  margin: 8px 0;
}

.ptf-skeleton-title {
  height: 24px;
  width: 60%;
  margin: 12px 0;
}

.ptf-skeleton-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
}

.ptf-skeleton-card {
  height: 120px;
  margin: 12px 0;
}

/* Loading state for cards */
.ptf-card.is-loading {
  position: relative;
  overflow: hidden;
}

.ptf-card.is-loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    transparent 25%,
    rgba(255, 255, 255, 0.3) 50%,
    transparent 75%
  );
  background-size: 200% 100%;
  animation: ptf-shimmer 1.5s infinite;
}

/* ─────────────────────────────────────────────────────────────────────────────
   PHASE 4: FORMS - INLINE VALIDATION
   ───────────────────────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
  /* Form field container */
  .fld, .field {
    position: relative;
    margin-bottom: 16px;
  }

  /* Labels */
  .fld label, .field label {
    display: block;
    font-weight: 800;
    margin-bottom: 6px;
    color: var(--tx, #1e293b);
    font-size: 14px;
  }

  /* Required indicator */
  .fld label em, .field label em,
  .fld label .required, .field label .required {
    color: #dc2626;
    font-style: normal;
    margin-right: 2px;
  }

  /* Invalid input */
  .fld input:invalid:not(:placeholder-shown):not(:focus),
  .field input:invalid:not(:placeholder-shown):not(:focus),
  .fld textarea:invalid:not(:placeholder-shown):not(:focus),
  .field textarea:invalid:not(:placeholder-shown):not(:focus),
  .fld select:invalid:not(:focus),
  .field select:invalid:not(:focus) {
    border-color: #dc2626 !important;
    background: #fef2f2 !important;
  }

  /* Valid input */
  .fld input:valid:not(:placeholder-shown),
  .field input:valid:not(:placeholder-shown),
  .fld textarea:valid:not(:placeholder-shown),
  .field textarea:valid:not(:placeholder-shown) {
    border-color: #059669 !important;
    background: #ecfdf5 !important;
  }

  /* Error message */
  .fld .error, .field .error {
    display: none;
    color: #dc2626;
    font-size: 13px;
    margin-top: 6px;
    font-weight: 700;
    padding: 8px 12px;
    background: #fef2f2;
    border-radius: 8px;
    border-right: 3px solid #dc2626;
  }

  .fld input:invalid:not(:placeholder-shown):not(:focus) ~ .error,
  .field input:invalid:not(:placeholder-shown):not(:focus) ~ .error,
  .fld textarea:invalid:not(:placeholder-shown):not(:focus) ~ .error,
  .field textarea:invalid:not(:placeholder-shown):not(:focus) ~ .error {
    display: block;
  }

  /* Success message */
  .fld .success, .field .success {
    display: none;
    color: #059669;
    font-size: 13px;
    margin-top: 6px;
    font-weight: 700;
    padding: 8px 12px;
    background: #ecfdf5;
    border-radius: 8px;
    border-right: 3px solid #059669;
  }

  .fld input:valid:not(:placeholder-shown) ~ .success,
  .field input:valid:not(:placeholder-shown) ~ .success {
    display: block;
  }

  /* Helper text */
  .fld .helper, .field .helper {
    color: #64748b;
    font-size: 12px;
    margin-top: 4px;
    font-weight: 600;
  }

  /* Submit button loading state */
  button[type="submit"].is-loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
  }

  button[type="submit"].is-loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid #fff;
    border-top-color: transparent;
    border-radius: 50%;
    animation: ptf-spin 0.6s linear infinite;
  }

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

/* ─────────────────────────────────────────────────────────────────────────────
   PHASE 5: UI ENHANCEMENTS
   ───────────────────────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
  /* Empty states */
  .ptf-empty-state {
    text-align: center;
    padding: 48px 20px;
    background: linear-gradient(135deg, rgba(14, 116, 144, 0.05), rgba(15, 39, 68, 0.03));
    border-radius: 16px;
    border: 2px dashed rgba(14, 116, 144, 0.2);
    margin: 20px 0;
  }

  .ptf-empty-state svg {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
    opacity: 0.5;
  }

  .ptf-empty-state h3 {
    font-size: 18px;
    color: #334155;
    margin: 0 0 8px 0;
    font-weight: 900;
  }

  .ptf-empty-state p {
    font-size: 14px;
    color: #64748b;
    margin: 0 0 16px 0;
    line-height: 1.6;
  }

  /* Card hover effects */
  .pn, .ptf-card {
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                box-shadow 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .pn:active, .ptf-card:active {
    transform: scale(0.98);
  }

  /* Button press effect */
  button:active,
  .btn:active,
  .bt:active {
    transform: scale(0.96);
  }

  /* Smooth transitions */
  a, button, .btn, .bt {
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  }

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

  /* Improve card readability */
  .tb2 tr.ptf-card {
    padding: 16px !important;
    margin-bottom: 12px !important;
    background: #fff !important;
    border-radius: 14px !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06) !important;
  }

  /* Card labels */
  .tb2 td[data-mobile-label]::before {
    font-weight: 800 !important;
    color: #475569 !important;
    font-size: 13px !important;
  }

  /* Bottom nav improvements */
  #mnvBar {
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.08) !important;
    border-top: 1px solid rgba(0, 0, 0, 0.06) !important;
  }

  #mnvBar button {
    transition: all 0.2s ease;
  }

  #mnvBar button:active {
    transform: scale(0.92);
  }

  #mnvBar button.active {
    background: linear-gradient(135deg, rgba(239, 75, 26, 0.12), rgba(247, 148, 0, 0.08)) !important;
  }

  /* Modal/Dialog improvements */
  .md-b, .ptf-modal {
    border-radius: 20px !important;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2) !important;
  }

  .md-b .md-h, .ptf-modal-header {
    border-bottom: 1px solid rgba(0, 0, 0, 0.08) !important;
    padding: 16px 20px !important;
  }

  .md-b .md-f, .ptf-modal-footer {
    border-top: 1px solid rgba(0, 0, 0, 0.08) !important;
    padding: 16px 20px !important;
    gap: 10px !important;
  }
}

/* ─────────────────────────────────────────────────────────────────────────────
   UTILITY CLASSES
   ───────────────────────────────────────────────────────────────────────────── */

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

/* Loading spinner */
.ptf-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid rgba(0, 0, 0, 0.1);
  border-top-color: var(--pri, #ef4b1a);
  border-radius: 50%;
  animation: ptf-spin 0.6s linear infinite;
}

/* Badge */
.ptf-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--pri, #ef4b1a);
  color: #fff;
  font-size: 11px;
  font-weight: 900;
}

/* Toast notification */
.ptf-toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: #fff;
  padding: 12px 20px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 99998;
  font-weight: 700;
  font-size: 14px;
  max-width: calc(100vw - 32px);
  animation: ptf-toast-in 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes ptf-toast-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}
