@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;500;700&display=swap');
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap");
@import url("https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v30.1.0/dist/font-face.css");

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    --font-family: 'Vazirmatn', Tahoma, Arial, sans-serif;
    --color-primary: #10b981;
    --color-primary-dark: #059669;
    --color-secondary: #64748b;
    --color-bg: #f8fafc;
    --color-bg-muted: #f1f5f9;
    --color-surface: #fff;
    --color-border: #e5e7eb;
    --color-text: #1e293b;
    --color-muted: #64748b;
    --color-success: #22c55e;
    --color-warning: #f59e42;
    --color-error: #ef4444;
    --color-info: #0ea5e9;
    --radius: 12px;
    --shadow: 0 2px 8px 0 rgba(30, 41, 59, 0.06);

    --background: 0 0% 100%;
    --foreground: 222.2 84% 4.9%;

    --card: 0 0% 100%;
    --card-foreground: 222.2 84% 4.9%;

    --popover: 0 0% 100%;
    --popover-foreground: 222.2 84% 4.9%;

    --primary: 160 84% 39%;
    --primary-foreground: 210 40% 98%;

    --secondary: 210 40% 96.1%;
    --secondary-foreground: 222.2 47.4% 11.2%;

    --muted: 210 40% 96.1%;
    --muted-foreground: 215.4 16.3% 46.9%;

    --accent: 210 40% 96.1%;
    --accent-foreground: 222.2 47.4% 11.2%;

    --destructive: 0 84.2% 60.2%;
    --destructive-foreground: 210 40% 98%;

    --border: 214.3 31.8% 91.4%;
    --input: 214.3 31.8% 91.4%;
    --ring: 222.2 84% 4.9%;

    --gold: 41 74% 52%;
    --gold-foreground: 210 40% 98%;
    
    --navy: 221 83% 24%;
    --navy-foreground: 210 40% 98%;
    
    --emerald: 160 84% 39%;
    --emerald-foreground: 210 40% 98%;

    --background: 0 0% 100%;
    --foreground: 0 0% 3.9%;
    --card: 0 0% 100%;
    --card-foreground: 0 0% 3.9%;
    --popover: 0 0% 100%;
    --popover-foreground: 0 0% 3.9%;
    --primary: 0 0% 9%;
    --primary-foreground: 0 0% 98%;
    --secondary: 0 0% 96.1%;
    --secondary-foreground: 0 0% 9%;
    --muted: 0 0% 96.1%;
    --muted-foreground: 0 0% 45.1%;
    --accent: 0 0% 96.1%;
    --accent-foreground: 0 0% 9%;
    --destructive: 0 84.2% 60.2%;
    --destructive-foreground: 0 0% 98%;
    --border: 0 0% 89.8%;
    --input: 0 0% 89.8%;
    --ring: 0 0% 3.9%;
    --chart-1: 12 76% 61%;
    --chart-2: 173 58% 39%;
    --chart-3: 197 37% 24%;
    --chart-4: 43 74% 66%;
    --chart-5: 27 87% 67%;
    --radius: 0.5rem;
    
    /* Persian font variables */
    --font-family: 'Vazir', 'Inter', system-ui, -apple-system, sans-serif;
  }

  .dark {
    --background: 222.2 84% 4.9%;
    --foreground: 210 40% 98%;

    --card: 222.2 84% 4.9%;
    --card-foreground: 210 40% 98%;

    --popover: 222.2 84% 4.9%;
    --popover-foreground: 210 40% 98%;

    --primary: 160 84% 49%;
    --primary-foreground: 222.2 47.4% 11.2%;

    --secondary: 217.2 32.6% 17.5%;
    --secondary-foreground: 210 40% 98%;

    --muted: 217.2 32.6% 17.5%;
    --muted-foreground: 215 20.2% 65.1%;

    --accent: 217.2 32.6% 17.5%;
    --accent-foreground: 210 40% 98%;

    --destructive: 0 62.8% 30.6%;
    --destructive-foreground: 210 40% 98%;

    --border: 217.2 32.6% 17.5%;
    --input: 217.2 32.6% 17.5%;
    --ring: 212.7 26.8% 83.9%;

    --background: 0 0% 3.9%;
    --foreground: 0 0% 98%;
    --card: 0 0% 3.9%;
    --card-foreground: 0 0% 98%;
    --popover: 0 0% 3.9%;
    --popover-foreground: 0 0% 98%;
    --primary: 0 0% 98%;
    --primary-foreground: 0 0% 9%;
    --secondary: 0 0% 14.9%;
    --secondary-foreground: 0 0% 98%;
    --muted: 0 0% 14.9%;
    --muted-foreground: 0 0% 63.9%;
    --accent: 0 0% 14.9%;
    --accent-foreground: 0 0% 98%;
    --destructive: 0 62.8% 30.6%;
    --destructive-foreground: 0 0% 98%;
    --border: 0 0% 14.9%;
    --input: 0 0% 14.9%;
    --ring: 0 0% 83.1%;
    --chart-1: 220 70% 50%;
    --chart-2: 160 60% 45%;
    --chart-3: 30 80% 55%;
    --chart-4: 280 65% 60%;
    --chart-5: 340 75% 55%;
  }
  
  /* Optimized Vazir font loading */
  @font-face {
    font-family: 'Vazir';
    src: url('https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v30.1.0/dist/Vazir-Regular.woff2') format('woff2');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
    unicode-range: U+0600-06FF, U+200C-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
  }
  
  @font-face {
    font-family: 'Vazir';
    src: url('https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v30.1.0/dist/Vazir-Medium.woff2') format('woff2');
    font-weight: 500;
    font-style: normal;
    font-display: swap;
    unicode-range: U+0600-06FF, U+200C-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
  }
  
  @font-face {
    font-family: 'Vazir';
    src: url('https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v30.1.0/dist/Vazir-Bold.woff2') format('woff2');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
    unicode-range: U+0600-06FF, U+200C-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
  }
}

@layer base {
  * {
    @apply border-border;
  }
  
  body {
    @apply bg-background text-foreground;
    font-feature-settings: "rlig" 1, "calt" 1;
    direction: rtl;
    min-height: 100%;
    overflow-x: hidden;
    /* Performance optimizations */
    scroll-behavior: smooth;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    /* Prevent font loading flash */
    font-optical-sizing: auto;
  }
  
  /* Import IRANYekan font */
  @font-face {
    font-family: 'IRANYekan';
    src: local('IRANYekan'), local('IRANYekanWebRegular'),
         url('/fonts/iranyekan/iranyekanwebregular.woff2') format('woff2'),
         url('/fonts/iranyekan/iranyekanwebregular.woff') format('woff');
    font-weight: normal;
    font-style: normal;
    font-display: swap;
  }
  
  @font-face {
    font-family: 'IRANYekan';
    src: local('IRANYekan Bold'), local('IRANYekanWebBold'),
         url('/fonts/iranyekan/iranyekanwebbold.woff2') format('woff2'),
         url('/fonts/iranyekan/iranyekanwebbold.woff') format('woff');
    font-weight: bold;
    font-style: normal;
    font-display: swap;
  }
  
  html {
    height: 100%;
    font-family: var(--font-family);
    background: var(--color-bg);
    color: var(--color-text);
    direction: rtl;
    overflow-x: hidden;
  }
  
  /* Admin layout specific styles */
  .admin-layout {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    position: relative;
    /* Ensure proper stacking context */
    isolation: isolate;
  }
  
  .admin-layout main {
    flex: 1;
    overflow-y: auto;
    max-height: calc(100vh - 4rem); /* Accounting for header height */
  }
  
  /* Critical fix for dialogs to be visible */
  [data-state="open"][role="dialog"] {
    z-index: 999999 !important;
    background-color: white !important;
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
  }
  
  [role="dialog"]::backdrop {
    z-index: 99999 !important;
  }
  
  /* Ensure dialog overlays are visible */
  [data-radix-portal] {
    z-index: 999999 !important;
  }

  /* Admin dialog specific fixes */
  .admin-dialog {
    background-color: white !important;
    z-index: 999999 !important;
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    position: fixed !important;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25) !important;
  }

  /* Fix dialog backdrop blur issues */
  body [data-radix-portal],
  body [data-state="open"],
  body [role="dialog"] {
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    filter: none !important;
    -webkit-filter: none !important;
  }

  /* Fix overlay styling */
  body [data-radix-dialog-overlay] {
    background-color: rgba(0, 0, 0, 0.4) !important;
    z-index: 99998 !important;
    backdrop-filter: none !important;
    pointer-events: auto !important;
  }

  /* Fix stacking context issues by resetting isolation */
  body .admin-layout,
  body .admin-layout * {
    isolation: auto !important;
  }

  /* Improve focus visibility for accessibility and touch */
  *:focus-visible {
    @apply outline-2 outline-offset-2 outline-emerald;
  }

  /* Better tap highlighting for mobile */
  button, a, [role="button"] {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
  }

  /* Remove outline on click but keep for keyboard navigation */
  button:focus:not(:focus-visible),
  a:focus:not(:focus-visible),
  [role="button"]:focus:not(:focus-visible) {
    outline: none;
  }
  
  /* Critical rendering optimizations */
  img {
    max-width: 100%;
    height: auto;
  }
  
  /* Optimize for Core Web Vitals */
  .admin-layout {
    contain: layout style paint;
    isolation: isolate;
  }
}

@layer components {
  .container-custom {
    @apply max-w-7xl mx-auto px-4 sm:px-6 lg:px-8;
  }

  /* Admin card component - Critical for admin pages */
  .admin-card {
    @apply bg-white rounded-lg border shadow-sm p-4 sm:p-6;
    contain: content;
  }

  .admin-card-header {
    @apply flex items-center justify-between mb-4 sm:mb-6;
  }

  .admin-card-title {
    @apply text-lg sm:text-xl font-semibold text-gray-900;
  }

  /* Critical navigation styles */
  .mobile-menu-btn {
    @apply lg:hidden p-2 rounded-md hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-emerald touch-manipulation;
    transform: translateZ(0); /* Force hardware acceleration */
  }

  .nav-item-responsive {
    @apply flex items-center px-3 py-2 sm:py-2.5 text-sm lg:text-base rounded-lg transition-colors touch-manipulation;
    will-change: transform;
  }

  .nav-item-responsive:hover {
    @apply bg-gray-100;
  }

  .nav-item-responsive.active {
    @apply bg-emerald text-white;
  }

  /* Performance optimized stats cards */
  .stats-card {
    @apply bg-white rounded-lg border p-4 sm:p-6 hover:shadow-md transition-shadow;
    contain: layout style paint;
    will-change: box-shadow;
  }

  .stats-card-icon {
    @apply h-8 w-8 sm:h-10 sm:w-10 rounded-full flex items-center justify-center;
  }

  .stats-card-value {
    @apply text-2xl sm:text-3xl font-bold text-gray-900;
  }

  .stats-card-label {
    @apply text-sm text-gray-600;
  }
}

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-family);
  min-height: 100vh;
  line-height: 1.7;
  overflow-x: hidden;
}

h1, h2, h3, h4, h5, h6 {
  font-weight: 700;
  color: var(--color-text);
  margin-top: 0;
}

a {
  color: var(--color-primary);
  text-decoration: none;
  transition: color 0.2s;
}
a:hover {
  color: var(--color-primary-dark);
}

input, button, textarea, select {
  font-family: inherit;
}

::-webkit-scrollbar {
  width: 8px;
  background: var(--color-bg-muted);
}
::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 8px;
}

/* Utility classes for backgrounds, borders, and surfaces */
.bg-surface {
  background: var(--color-surface);
}
.bg-muted {
  background: var(--color-bg-muted);
}
.text-muted {
  color: var(--color-muted);
}
.shadow {
  box-shadow: var(--shadow);
}
.rounded {
  border-radius: var(--radius);
}

/* RTL fix for tables and flex */
table {
  direction: rtl;
}
.flex-row-reverse {
  flex-direction: row-reverse;
}

/* استایل‌های اسکرول‌بار سفارشی */
.custom-scrollbar::-webkit-scrollbar {
  width: 4px;
}

.custom-scrollbar::-webkit-scrollbar-track {
  background: transparent;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: #e2e8f0;
  border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: #cbd5e1;
}

/* Custom animations for mobile category cards */
@keyframes gentle-float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

.category-card-animate {
  animation: gentle-float 4s ease-in-out infinite;
}

.category-card-animate:nth-child(odd) {
  animation-delay: 0.5s;
}

.category-card-animate:nth-child(3n) {
  animation-delay: 1s;
}

.category-card-animate:nth-child(4n) {
  animation-delay: 1.5s;
}

/* Custom scrollbar for mobile navigation */
.custom-scrollbar::-webkit-scrollbar {
  width: 4px;
}

.custom-scrollbar::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: rgba(16, 185, 129, 0.3);
  border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: rgba(16, 185, 129, 0.5);
}

/* Gradient text effect */
.gradient-text {
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  background-image: linear-gradient(to right, hsl(var(--emerald)), hsl(var(--navy)));
}

/* Shimmer effect animation */
@keyframes shimmer {
  0% {
    background-position: -200% 0;
  }
  100% {
    background-position: 200% 0;
  }
}

.shimmer-effect {
  background: linear-gradient(90deg, 
    rgba(255, 255, 255, 0) 0%, 
    rgba(255, 255, 255, 0.8) 50%, 
    rgba(255, 255, 255, 0) 100%);
  background-size: 200% 100%;
  animation: shimmer 2s infinite;
}

/* User menu dropdown fix for right-to-left direction */
.user-menu-dropdown {
  position: absolute !important;
  top: 100% !important;
  right: 0 !important;
  left: auto !important;
  margin-top: 0.5rem !important;
  z-index: 9999 !important;
  width: 16rem !important;
  transform-origin: top right !important;
  direction: rtl !important;
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

/* Menu item icons alignment for RTL */
.user-menu-dropdown a,
.user-menu-dropdown button {
  display: flex !important;
  align-items: center !important;
  text-align: right !important;
  justify-content: flex-start !important;
}

.user-menu-dropdown a > svg,
.user-menu-dropdown button > svg {
  margin-right: 0 !important;
  margin-left: 8px !important;
}

/* Prevent any potential overflow issues */
.user-menu-container {
  position: relative !important;
  display: inline-block !important;
}

/* Fix menu positioning for RTL layout */
[dir="rtl"] .dropdown-menu {
  right: 0 !important;
  left: auto !important;
  text-align: right !important;
}

/* Ensure RTL works properly across all browsers */
html[dir="rtl"] body .dropdown-menu,
body[dir="rtl"] .dropdown-menu {
  right: 0 !important;
  left: auto !important;
}

/* Make sure all popup menus are visible */
[data-radix-popper-content-wrapper] {
  z-index: 9999 !important;
}

[data-state="open"][role="dialog"] {
  z-index: 99999 !important;
}

/* Ensure dropdowns are visible over other elements */
.absolute-dropdown {
  z-index: 50 !important;
  visibility: visible !important;
  opacity: 1 !important;
  display: block !important;
}

/* Remove potential isolation issues */
body, body * {
  isolation: auto !important;
}

/* Ensure dropdown portal elements are visible */
[data-radix-portal] {
  z-index: 9999 !important;
  visibility: visible !important;
}

/* RTL Support for Radix Dropdowns */
[data-radix-popper-content-wrapper] {
  direction: rtl !important;
}

.rtl [role="menuitem"] {
  direction: rtl;
  text-align: right;
}

.rtl [data-radix-dropdown-menu-content] {
  right: auto !important;
  left: auto !important;
}

/* Custom responsive utilities */
@layer utilities {
  /* Hardware acceleration utilities */
  .gpu-accelerated {
    transform: translateZ(0);
    backface-visibility: hidden;
    perspective: 1000;
  }
  
  /* Content visibility for better rendering performance */
  .content-visibility-auto {
    content-visibility: auto;
    contain-intrinsic-size: 0 500px;
  }

  /* Touch-friendly sizing */
  .touch-target {
    min-height: 44px;
    min-width: 44px;
  }

  /* Optimized scrollbars */
  .scrollbar-thin {
    scrollbar-width: thin;
    scrollbar-color: rgb(203 213 225) transparent;
  }

  .scrollbar-thin::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }

  .scrollbar-thin::-webkit-scrollbar-track {
    background: transparent;
  }

  .scrollbar-thin::-webkit-scrollbar-thumb {
    background-color: rgb(203 213 225);
    border-radius: 3px;
  }

  .scrollbar-thin::-webkit-scrollbar-thumb:hover {
    background-color: rgb(148 163 184);
  }

  /* Optimized text truncation */
  .line-clamp-1 {
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
  }

  .line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
  }

  .line-clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
  }

  /* Performance optimized responsive spacing */
  .space-y-responsive > :not([hidden]) ~ :not([hidden]) {
    margin-top: 0.75rem;
  }

  @screen sm {
    .space-y-responsive > :not([hidden]) ~ :not([hidden]) {
      margin-top: 1rem;
    }
  }

  @screen md {
    .space-y-responsive > :not([hidden]) ~ :not([hidden]) {
      margin-top: 1.5rem;
    }
  }

  /* Critical path optimizations */
  .critical-above-fold {
    contain: layout style paint;
  }

  /* Mobile-first responsive containers */
  .container-responsive {
    width: 100%;
    padding-left: 1rem;
    padding-right: 1rem;
    margin-left: auto;
    margin-right: auto;
  }

  @screen sm {
    .container-responsive {
      padding-left: 1.5rem;
      padding-right: 1.5rem;
    }
  }

  @screen md {
    .container-responsive {
      padding-left: 2rem;
      padding-right: 2rem;
    }
  }

  @screen lg {
    .container-responsive {
      max-width: 1024px;
    }
  }

  @screen xl {
    .container-responsive {
      max-width: 1280px;
    }
  }

  @screen 2xl {
    .container-responsive {
      max-width: 1536px;
    }
  }

  /* Optimized mobile navigation */
  .mobile-nav-backdrop {
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    will-change: opacity;
  }

  /* Performance optimized form fields */
  .form-field-responsive {
    @apply space-y-1 sm:space-y-2;
    contain: layout;
  }

  .form-field-responsive label {
    @apply text-sm font-medium text-gray-700;
  }

  .form-field-responsive input,
  .form-field-responsive textarea,
  .form-field-responsive select {
    @apply w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald focus:border-emerald;
  }

  /* Hardware accelerated buttons */
  .btn-touch {
    @apply min-h-10 min-w-10 touch-manipulation;
    transform: translateZ(0);
    transition: transform 0.1s ease-in-out;
    will-change: transform;
  }

  .btn-touch:active {
    transform: scale(0.95) translateZ(0);
  }

  /* Optimized loading states */
  .loading-skeleton {
    @apply bg-gray-200 animate-pulse rounded;
    contain: strict;
  }

  /* Performance improvements for reduced motion */
  @media (prefers-reduced-motion: reduce) {
    .animate-spin,
    .animate-ping,
    .animate-pulse,
    .transition-all,
    .transition-transform,
    .transition-colors,
    .transition-opacity {
      animation: none !important;
      transition: none !important;
    }
    
    .btn-touch:active {
      transform: none;
    }
  }

  /* High contrast mode optimizations */
  @media (prefers-contrast: high) {
    .border {
      border-width: 2px;
    }
    
    .shadow-sm {
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
    }
  }
}
