/**
 * Responsive Design System for Dakhla Farm Management
 * Mobile-first approach with breakpoints for tablet and desktop
 * 
 * Breakpoints:
 * - Mobile: 320px - 767px
 * - Tablet: 768px - 1023px
 * - Desktop: 1024px+
 */

/* ============================================================================
   BASE MOBILE STYLES (320px+)
   ============================================================================ */

/* Viewport Meta Tag Required:
   <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0">
*/

/* Ensure proper box-sizing */
*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Prevent horizontal scroll on mobile */
html,
body {
  overflow-x: hidden;
  width: 100%;
}

/* ============================================================================
   SIDEBAR RESPONSIVE BEHAVIOR
   ============================================================================ */

/* Mobile: Sidebar is hidden by default, slides in with overlay */
@media (max-width: 767px) {
  /* Sidebar container */
  #sidebar,
  .sidebar {
    position: fixed !important;
    left: -280px !important; /* Hidden off-screen */
    top: 0 !important;
    bottom: 0 !important;
    width: 280px !important;
    z-index: 9999 !important;
    transition: left 0.3s ease !important;
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.3) !important;
  }

  /* Sidebar visible state */
  #sidebar.mobile-visible,
  .sidebar.mobile-visible {
    left: 0 !important;
  }

  /* Overlay for mobile sidebar */
  .sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 9998;
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  .sidebar-overlay.active {
    display: block;
    opacity: 1;
  }

  /* Main content takes full width on mobile */
  .main-content,
  main {
    margin-left: 0 !important;
    width: 100% !important;
    padding: 60px 16px 16px 16px !important; /* Top padding for mobile header */
  }

  /* Mobile header with hamburger button */
  .mobile-header {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    background-color: #2D5A3D;
    color: white;
    padding: 0 16px;
    z-index: 9997;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .mobile-header-title {
    font-size: 18px;
    font-weight: 600;
    color: white;
  }

  .mobile-menu-button {
    background: none;
    border: none;
    color: white;
    padding: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .mobile-menu-button svg {
    width: 24px;
    height: 24px;
  }

  /* Hide desktop sidebar toggle button on mobile */
  #sidebarToggle,
  #sidebarExpandBtn {
    display: none !important;
  }
}

/* Tablet and Desktop: Normal sidebar behavior */
@media (min-width: 768px) {
  .mobile-header {
    display: none !important;
  }

  .sidebar-overlay {
    display: none !important;
  }

  /* Sidebar normal position */
  #sidebar,
  .sidebar {
    position: fixed !important;
    left: 0 !important;
  }

  /* Main content with sidebar margin */
  .main-content,
  main {
    margin-left: 280px !important;
  }

  /* When sidebar is collapsed on desktop */
  body.sidebar-collapsed .main-content,
  body.sidebar-collapsed main {
    margin-left: 0 !important;
  }
}

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

/* Mobile: Horizontal scroll for tables */
@media (max-width: 767px) {
  .table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -16px; /* Extend to screen edges */
    padding: 0 16px;
  }

  table {
    min-width: 600px; /* Ensure table doesn't shrink too much */
    font-size: 14px;
  }

  table th,
  table td {
    padding: 8px 12px !important;
    white-space: nowrap;
  }

  /* Alternative: Card layout for tables */
  .table-card-mobile {
    display: block;
  }

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

  .table-card-mobile tbody,
  .table-card-mobile tr {
    display: block;
    margin-bottom: 16px;
  }

  .table-card-mobile tr {
    background: white;
    border-radius: 8px;
    padding: 16px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }

  .table-card-mobile td {
    display: flex;
    justify-content: space-between;
    padding: 8px 0 !important;
    border-bottom: 1px solid #E8E5E0;
  }

  .table-card-mobile td:last-child {
    border-bottom: none;
  }

  .table-card-mobile td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #374151;
  }
}

/* ============================================================================
   FORMS & MODALS RESPONSIVE
   ============================================================================ */

/* Mobile: Full-screen modals */
@media (max-width: 767px) {
  .modal,
  .modal-content,
  [class*="modal"] {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    height: 100% !important;
    max-width: 100% !important;
    max-height: 100% !important;
    margin: 0 !important;
    border-radius: 0 !important;
    overflow-y: auto !important;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 16px !important;
  }

  /* Form inputs */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  input[type="time"],
  textarea,
  select {
    font-size: 16px !important; /* Prevent zoom on iOS */
    padding: 12px !important;
    width: 100% !important;
  }

  /* Buttons */
  button,
  .btn {
    min-height: 44px !important; /* Touch-friendly size */
    padding: 12px 20px !important;
    font-size: 16px !important;
  }

  /* Form groups */
  .form-group {
    margin-bottom: 16px !important;
  }

  label {
    font-size: 14px !important;
    margin-bottom: 8px !important;
    display: block;
  }
}

/* ============================================================================
   CARDS & GRID LAYOUTS
   ============================================================================ */

/* Mobile: Single column layout */
@media (max-width: 767px) {
  .grid,
  .card-grid,
  [class*="grid-cols"] {
    grid-template-columns: 1fr !important;
    gap: 16px !important;
  }

  .card {
    margin-bottom: 16px;
  }
}

/* Tablet: 2 columns */
@media (min-width: 768px) and (max-width: 1023px) {
  .grid,
  .card-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 20px !important;
  }
}

/* Desktop: 3+ columns */
@media (min-width: 1024px) {
  .grid,
  .card-grid {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)) !important;
    gap: 24px !important;
  }
}

/* ============================================================================
   CHARTS RESPONSIVE
   ============================================================================ */

@media (max-width: 767px) {
  canvas {
    max-width: 100% !important;
    height: auto !important;
  }

  .chart-container {
    position: relative;
    height: 300px !important; /* Fixed height for mobile */
    width: 100% !important;
    overflow: hidden;
  }
}

/* ============================================================================
   MAP RESPONSIVE (Leaflet)
   ============================================================================ */

@media (max-width: 767px) {
  #map {
    height: calc(100vh - 56px) !important; /* Full screen minus mobile header */
    width: 100% !important;
  }

  /* Map controls */
  .leaflet-control-zoom {
    margin-top: 60px !important; /* Below mobile header */
  }

  .leaflet-top.leaflet-right {
    top: 60px !important;
    right: 8px !important;
  }

  .leaflet-top.leaflet-left {
    top: 60px !important;
    left: 8px !important;
  }

  /* Map toolbar buttons */
  .leaflet-bar a {
    width: 40px !important;
    height: 40px !important;
    line-height: 40px !important;
    font-size: 18px !important;
  }

  /* Drawing toolbar */
  .leaflet-draw-toolbar {
    margin-top: 60px !important;
  }
}

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

/* Show/hide based on screen size */
.mobile-only {
  display: none !important;
}

.desktop-only {
  display: block !important;
}

@media (max-width: 767px) {
  .mobile-only {
    display: block !important;
  }

  .desktop-only {
    display: none !important;
  }
}

/* Text sizing */
@media (max-width: 767px) {
  h1 {
    font-size: 24px !important;
  }

  h2 {
    font-size: 20px !important;
  }

  h3 {
    font-size: 18px !important;
  }

  h4 {
    font-size: 16px !important;
  }

  p,
  body {
    font-size: 14px !important;
    line-height: 1.6 !important;
  }
}

/* Spacing utilities */
@media (max-width: 767px) {
  .container {
    padding-left: 16px !important;
    padding-right: 16px !important;
  }

  .section {
    padding: 24px 16px !important;
  }
}

/* ============================================================================
   TOUCH INTERACTIONS
   ============================================================================ */

/* Larger touch targets */
@media (max-width: 767px) {
  a,
  button,
  [role="button"],
  input[type="checkbox"],
  input[type="radio"] {
    min-width: 44px;
    min-height: 44px;
  }

  /* Remove hover effects on touch devices */
  @media (hover: none) {
    *:hover {
      /* Disable hover styles on touch devices */
    }
  }
}

/* ============================================================================
   PRINT STYLES
   ============================================================================ */

@media print {
  #sidebar,
  .sidebar,
  .mobile-header,
  button,
  .no-print {
    display: none !important;
  }

  .main-content,
  main {
    margin-left: 0 !important;
    width: 100% !important;
  }
}
