/**
 * Responsive Mobile & iPad Optimizations
 * نظام إدارة التحصيل - تحسينات الجوال والآيباد
 * Version: 1.0
 */

/* ==========================================================================
   1. BASE MOBILE SETTINGS
   ========================================================================== */

/* Prevent horizontal overflow */
html, body {
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

/* Touch action optimization */
* {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ==========================================================================
   2. TOUCH-FRIENDLY BUTTON SIZES (44x44px minimum)
   ========================================================================== */

@media (max-width: 1024px) {
  /* All buttons minimum touch target */
  button,
  [role="button"],
  .btn,
  input[type="button"],
  input[type="submit"],
  input[type="reset"],
  a.btn {
    min-height: 44px;
    min-width: 44px;
    padding: 10px 16px !important;
  }

  /* Icon-only buttons */
  button:has(> i:only-child),
  .icon-btn {
    width: 44px;
    height: 44px;
    padding: 10px !important;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
}

/* Small action buttons - make them bigger on touch devices */
@media (max-width: 768px) {
  .action-btn,
  .card button,
  button.text-xs,
  button.text-sm {
    min-height: 40px !important;
    padding: 8px 12px !important;
    font-size: 13px !important;
  }

  /* Legal page action buttons */
  .legal-case-card button,
  [class*="bg-indigo-600"],
  [class*="bg-blue-600"],
  [class*="bg-green-600"],
  [class*="bg-purple-600"],
  [class*="bg-orange-600"],
  [class*="bg-yellow-600"] {
    min-height: 38px !important;
    padding: 8px 14px !important;
  }
}

/* ==========================================================================
   3. FORM INPUT SIZING FOR TOUCH
   ========================================================================== */

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

  /* Minimum input height */
  input[type="text"],
  input[type="email"],
  input[type="tel"],
  input[type="number"],
  input[type="password"],
  input[type="search"],
  input[type="url"],
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"],
  select,
  textarea {
    min-height: 48px !important;
    padding: 12px 14px !important;
  }

  /* Larger touch target for checkboxes and radios */
  input[type="checkbox"],
  input[type="radio"] {
    width: 20px !important;
    height: 20px !important;
    margin: 4px;
  }

  /* Checkbox labels */
  label:has(input[type="checkbox"]),
  label:has(input[type="radio"]) {
    min-height: 44px;
    padding: 10px 12px;
    display: flex;
    align-items: center;
    gap: 10px;
  }
}

/* ==========================================================================
   4. RESPONSIVE PADDING
   ========================================================================== */

@media (max-width: 640px) {
  /* Reduce padding on small screens */
  .p-6 { padding: 1rem !important; }
  .p-8 { padding: 1.25rem !important; }
  .px-6 { padding-left: 1rem !important; padding-right: 1rem !important; }
  .py-6 { padding-top: 1rem !important; padding-bottom: 1rem !important; }
  .px-8 { padding-left: 1.25rem !important; padding-right: 1.25rem !important; }

  /* Main content padding */
  main {
    padding: 1rem !important;
  }

  /* Card padding */
  .card, .modal-content, dialog > div {
    padding: 1rem !important;
  }
}

@media (min-width: 641px) and (max-width: 1024px) {
  /* iPad specific padding */
  main {
    padding: 1.5rem !important;
  }
}

/* ==========================================================================
   5. MODAL/DIALOG RESPONSIVENESS
   ========================================================================== */

/* Mobile: Full screen modals */
@media (max-width: 640px) {
  dialog,
  .modal,
  .fixed[class*="bg-black"] > div,
  [id$="Modal"] > div,
  [id$="Dlg"]::backdrop + div {
    max-width: 100vw !important;
    width: 100vw !important;
    max-height: 100vh !important;
    height: 100vh !important;
    margin: 0 !important;
    border-radius: 0 !important;
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
  }

  dialog::backdrop {
    background: rgba(0, 0, 0, 0.7);
  }

  /* Modal header sticky on mobile */
  .modal-header,
  dialog > div > div:first-child {
    position: sticky;
    top: 0;
    z-index: 10;
    border-radius: 0 !important;
  }

  /* Modal content scrollable */
  .modal-content,
  dialog > div > div:not(:first-child) {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 60px);
  }
}

/* Tablet: Responsive max-width */
@media (min-width: 641px) and (max-width: 1024px) {
  dialog,
  .modal > div,
  .fixed[class*="bg-black"] > div {
    max-width: 90vw !important;
    width: 90vw !important;
    max-height: 90vh !important;
  }
}

/* ==========================================================================
   6. GRID RESPONSIVENESS
   ========================================================================== */

@media (max-width: 640px) {
  /* Force single column on mobile */
  .grid[class*="md:grid-cols"],
  .grid[class*="lg:grid-cols"] {
    grid-template-columns: 1fr !important;
  }

  /* Stats grid */
  .stats-grid,
  .grid.md\:grid-cols-4,
  .grid.lg\:grid-cols-4 {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.75rem !important;
  }

  /* Form grids */
  form .grid {
    grid-template-columns: 1fr !important;
  }
}

/* Small tablet (portrait) */
@media (min-width: 641px) and (max-width: 768px) {
  .grid.md\:grid-cols-4,
  .grid.lg\:grid-cols-4 {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ==========================================================================
   7. FLEX RESPONSIVENESS
   ========================================================================== */

@media (max-width: 640px) {
  /* Stack flex items vertically */
  .flex-row:not(.flex-nowrap),
  .md\:flex-row,
  .lg\:flex-row {
    flex-direction: column !important;
  }

  /* Action buttons row */
  .flex.gap-2.flex-wrap,
  .flex.items-center.gap-2 {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  /* Button groups stack on mobile */
  .flex.gap-2 > button,
  .flex.gap-3 > button {
    flex: 1 1 calc(50% - 8px);
    min-width: calc(50% - 8px);
  }
}

/* ==========================================================================
   8. TABLE RESPONSIVENESS
   ========================================================================== */

@media (max-width: 768px) {
  /* Horizontal scroll for tables */
  .table-container,
  .overflow-x-auto,
  table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  table {
    min-width: 600px;
  }

  /* Or card view for tables */
  .table-responsive-cards tbody tr {
    display: block;
    margin-bottom: 1rem;
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    padding: 1rem;
  }

  .table-responsive-cards tbody td {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border: none;
    border-bottom: 1px solid #f3f4f6;
  }

  .table-responsive-cards tbody td:last-child {
    border-bottom: none;
  }

  .table-responsive-cards tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #4b5563;
  }

  .table-responsive-cards thead {
    display: none;
  }
}

/* ==========================================================================
   9. NAVIGATION MOBILE
   ========================================================================== */

@media (max-width: 768px) {
  /* Mobile nav items */
  nav a,
  .nav-link {
    min-height: 48px;
    padding: 12px 16px;
    display: flex;
    align-items: center;
  }

  /* Bottom safe area for notched phones */
  .mobile-bottom-nav {
    padding-bottom: env(safe-area-inset-bottom, 20px);
  }
}

/* ==========================================================================
   10. TYPOGRAPHY MOBILE
   ========================================================================== */

@media (max-width: 640px) {
  /* Headings */
  h1, .text-3xl { font-size: 1.5rem !important; }
  h2, .text-2xl { font-size: 1.25rem !important; }
  h3, .text-xl { font-size: 1.125rem !important; }

  /* Body text */
  body { font-size: 14px; line-height: 1.6; }

  /* Small text readable */
  .text-xs { font-size: 12px !important; }
  .text-sm { font-size: 13px !important; }
}

/* ==========================================================================
   11. CARDS MOBILE
   ========================================================================== */

@media (max-width: 640px) {
  /* Legal case cards */
  .legal-case-card,
  .client-card,
  .debt-card {
    padding: 1rem !important;
  }

  /* Card actions */
  .card-actions,
  .flex.items-center.gap-2.flex-wrap {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #e5e7eb;
  }
}

/* ==========================================================================
   12. SEARCH & FILTERS MOBILE
   ========================================================================== */

@media (max-width: 640px) {
  /* Search container */
  .search-container,
  .filter-container {
    flex-direction: column !important;
    gap: 0.75rem !important;
  }

  /* Search input full width */
  .search-input,
  input[type="search"] {
    width: 100% !important;
  }

  /* Filter buttons row */
  .filter-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .filter-buttons button {
    flex: 1 1 auto;
    min-width: 80px;
  }
}

/* ==========================================================================
   13. STAT CARDS MOBILE
   ========================================================================== */

@media (max-width: 640px) {
  .stat-card {
    padding: 0.75rem !important;
  }

  .stat-card .stat-value,
  .stat-card .text-2xl,
  .stat-card .text-3xl {
    font-size: 1.25rem !important;
  }

  .stat-card .stat-label,
  .stat-card .text-sm {
    font-size: 0.75rem !important;
  }

  .stat-card i {
    font-size: 1.25rem !important;
  }
}

/* ==========================================================================
   14. NOTIFICATION & ALERTS MOBILE
   ========================================================================== */

@media (max-width: 640px) {
  /* Toast notifications */
  .notification,
  .toast,
  .alert {
    width: calc(100vw - 2rem) !important;
    max-width: none !important;
    left: 1rem !important;
    right: 1rem !important;
  }

  /* Notification position */
  .notification-container {
    bottom: env(safe-area-inset-bottom, 20px);
  }
}

/* ==========================================================================
   15. RTL MOBILE FIXES
   ========================================================================== */

@media (max-width: 768px) {
  [dir="rtl"] {
    /* Ensure RTL text alignment on mobile */
    text-align: right;
  }

  [dir="rtl"] .flex-row-reverse {
    flex-direction: row-reverse !important;
  }

  [dir="rtl"] input,
  [dir="rtl"] textarea,
  [dir="rtl"] select {
    text-align: right;
    direction: rtl;
  }
}

/* ==========================================================================
   16. LANDSCAPE ORIENTATION
   ========================================================================== */

@media (max-width: 896px) and (orientation: landscape) {
  /* Reduce heights in landscape */
  .modal-content {
    max-height: 80vh !important;
  }

  /* Compact form in landscape */
  form .grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ==========================================================================
   17. IPAD SPECIFIC OPTIMIZATIONS
   ========================================================================== */

@media (min-width: 768px) and (max-width: 1024px) {
  /* iPad grid */
  .grid.lg\:grid-cols-4 {
    grid-template-columns: repeat(3, 1fr) !important;
  }

  /* iPad modal size */
  dialog,
  .modal > div {
    max-width: 80vw !important;
  }

  /* iPad touch targets slightly smaller than phone */
  button,
  input,
  select {
    min-height: 44px;
  }
}

/* iPad Pro */
@media (min-width: 1024px) and (max-width: 1366px) {
  .grid.lg\:grid-cols-4 {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ==========================================================================
   18. SAFE AREA INSETS (Notched phones)
   ========================================================================== */

@supports (padding: env(safe-area-inset-top)) {
  body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  .fixed.bottom-0 {
    padding-bottom: env(safe-area-inset-bottom);
  }

  .fixed.top-0 {
    padding-top: env(safe-area-inset-top);
  }
}

/* ==========================================================================
   19. PRINT STYLES MOBILE
   ========================================================================== */

@media print {
  /* Reset mobile styles for print */
  * {
    min-height: auto !important;
    padding: initial;
  }

  .no-print {
    display: none !important;
  }
}

/* ==========================================================================
   20. ACCESSIBILITY MOBILE
   ========================================================================== */

/* Focus visible for touch devices */
@media (pointer: coarse) {
  *:focus {
    outline: 3px solid #C8A963 !important;
    outline-offset: 2px;
  }

  /* Larger focus area */
  button:focus,
  a:focus,
  input:focus,
  select:focus,
  textarea:focus {
    box-shadow: 0 0 0 3px rgba(200, 169, 99, 0.3);
  }
}

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