/* ==========================================================================
   Park Tree Attendance - Comprehensive Mobile & Responsive Engine (v3.1)
   Optimized for iOS Safari, Android Chrome, Foldables, Tablets & Desktops
   ========================================================================== */

/* 1. Global Touch, Viewport & Safe-Area Defaults */
html {
  -webkit-text-size-adjust: 100%;
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}

body {
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* 2. Form Controls & Inputs - Prevent iOS Auto-Zoom */
@media (max-width: 768px) {
  input, select, textarea, button {
    font-size: 16px !important; /* Prevents iOS auto-zoom on focus */
  }
  
  input, select {
    min-height: 44px; /* Standard Apple/Android touch target */
  }

  .field, .search input {
    height: 44px;
    font-size: 16px !important;
  }
}

/* 3. Toast Notifications on Mobile */
@media (max-width: 768px) {
  .toast {
    bottom: calc(76px + env(safe-area-inset-bottom, 0px)) !important;
    left: 14px !important;
    right: 14px !important;
    max-width: calc(100% - 28px) !important;
    transform: translateY(20px) !important;
    text-align: center;
    border-radius: 12px;
    padding: 12px 16px;
    font-size: 0.85rem;
    line-height: 1.4;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  }
  
  .toast.show {
    transform: translateY(0) !important;
  }
}

/* 4. Top Header & Hamburger Button */
#mobileMenu {
  display: none;
}

@media (max-width: 768px) {
  #mobileMenu {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--forest);
    font-size: 1.25rem;
    cursor: pointer;
    margin-right: 10px;
    flex-shrink: 0;
    transition: background 0.15s ease, transform 0.1s ease;
  }

  #mobileMenu:active {
    background: var(--forest-3);
    transform: scale(0.94);
  }

  .top-header {
    height: 70px !important;
    padding: 0 14px !important;
    gap: 8px;
  }

  .top-header > div:first-child {
    display: flex;
    align-items: center;
    min-width: 0;
    flex: 1;
  }

  .top-header h1 {
    font-size: 1.15rem !important;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .top-header p {
    display: none; /* Keep header compact on mobile */
  }

  .header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }

  .header-actions .button {
    padding: 8px 12px !important;
    font-size: 0.78rem !important;
    min-height: 38px;
    white-space: nowrap;
  }
}

/* 5. Mobile Navigation Drawer & Backdrop */
.sidebar-backdrop {
  display: none;
}

.sidebar-close-btn {
  display: none;
}

@media (max-width: 768px) {
  /* Slide-in Drawer */
  .sidebar {
    display: flex !important;
    position: fixed !important;
    top: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    width: min(300px, 86vw) !important;
    z-index: 100 !important;
    transform: translateX(-105%) !important;
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.28s ease !important;
    box-shadow: none !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    padding-top: max(20px, env(safe-area-inset-top, 20px)) !important;
    padding-bottom: max(24px, env(safe-area-inset-bottom, 24px)) !important;
  }

  .sidebar.open {
    transform: translateX(0) !important;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.6) !important;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(4, 30, 24, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 95;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }

  .sidebar-backdrop.show {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-close-btn {
    display: flex !important;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 14px;
    right: 14px;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.18);
    border: none;
    color: #fff;
    font-size: 1.1rem;
    cursor: pointer;
    z-index: 10;
  }

  .sidebar-close-btn:active {
    background: rgba(255, 255, 255, 0.35);
  }

  .side-nav {
    margin-top: 10px;
    flex: 1;
    overflow-y: auto;
  }

  .side-nav .nav-item {
    min-height: 42px;
    padding: 10px 12px;
    font-size: 0.88rem;
  }
}

/* 6. Bottom Navigation Bar on Mobile */
@media (max-width: 768px) {
  body {
    padding-bottom: calc(72px + env(safe-area-inset-bottom, 0px)) !important;
  }

  .mobile-nav {
    display: grid !important;
    grid-template-columns: repeat(5, 1fr) !important;
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    height: calc(64px + env(safe-area-inset-bottom, 0px)) !important;
    padding-bottom: env(safe-area-inset-bottom, 0px) !important;
    background: rgba(255, 255, 255, 0.96) !important;
    backdrop-filter: blur(12px) !important;
    -webkit-backdrop-filter: blur(12px) !important;
    border-top: 1px solid var(--line) !important;
    z-index: 50 !important;
    box-shadow: 0 -4px 18px rgba(0, 0, 0, 0.05);
  }

  .mobile-nav button {
    border: none !important;
    background: transparent !important;
    color: var(--muted) !important;
    font-size: 0.65rem !important;
    font-weight: 700 !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 3px !important;
    padding: 4px 2px !important;
    cursor: pointer;
    touch-action: manipulation;
    transition: color 0.15s ease;
  }

  .mobile-nav button span {
    font-size: 1.25rem !important;
    line-height: 1;
  }

  .mobile-nav button.active {
    color: var(--forest) !important;
  }

  .mobile-nav button.active span {
    transform: scale(1.08);
  }
}

/* 7. Dialogs & Modals on Mobile */
@media (max-width: 768px) {
  .modal {
    width: calc(100% - 20px) !important;
    max-width: 520px !important;
    margin: auto !important;
    max-height: calc(100vh - 40px) !important;
    border-radius: 20px !important;
    padding: 0 !important;
    overflow: hidden !important;
    border: none !important;
    box-shadow: 0 25px 70px rgba(0, 0, 0, 0.35) !important;
  }

  .modal-head {
    padding: 16px 18px !important;
    position: sticky !important;
    top: 0 !important;
    background: #fff !important;
    z-index: 10 !important;
    border-bottom: 1px solid var(--line) !important;
  }

  .modal-head h2 {
    font-size: 1.15rem !important;
  }

  .modal-head p {
    font-size: 0.74rem !important;
  }

  .modal-body {
    padding: 16px 18px !important;
    max-height: calc(100vh - 140px) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }

  .modal-actions {
    flex-direction: column-reverse !important;
    gap: 8px !important;
    width: 100% !important;
    margin-top: 14px !important;
  }

  .modal-actions .button {
    width: 100% !important;
    min-height: 44px !important;
    justify-content: center !important;
  }
}

/* 8. Data Tables & Horizontal Scrolling */
.table-wrap {
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch !important;
  border-radius: 12px;
}

@media (max-width: 768px) {
  .data-table, table {
    font-size: 0.78rem !important;
  }

  th, td {
    padding: 10px 8px !important;
  }

  .table-wrap::-webkit-scrollbar {
    height: 6px;
  }

  .table-wrap::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 4px;
  }
}

/* 9. WhatsApp Gateway & Settings Cards on Mobile */
@media (max-width: 680px) {
  #waQrContainer {
    flex-direction: column !important;
    text-align: center !important;
    gap: 16px !important;
    padding: 14px !important;
  }

  #waQrImageWrapper {
    margin: 0 auto !important;
    width: 190px !important;
    height: 190px !important;
  }

  #waStatusTitle {
    text-align: center !important;
    font-size: 1rem !important;
  }

  #waQrContainer ol {
    text-align: left !important;
    padding-left: 20px !important;
    font-size: 0.78rem !important;
  }

  #vpsGatewayCard .button {
    width: 100% !important;
    justify-content: center !important;
    min-height: 38px;
  }

  #vpsGatewayCard > div:first-child > div:last-child {
    width: 100% !important;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  .settings-grid {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }

  .settings-card {
    padding: 16px 14px !important;
  }
}

/* 10. Salary & Payroll Page on Mobile */
@media (max-width: 768px) {
  .staff-salary-widget {
    padding: 14px !important;
  }

  .staff-salary-widget > div {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 14px !important;
  }

  .staff-salary-widget button {
    width: 100% !important;
    justify-content: center !important;
  }

  .modal.modal-salary-detail {
    width: calc(100% - 16px) !important;
    max-width: 100% !important;
    margin: 8px auto !important;
  }

  .salary-detail-table-wrap {
    max-height: 52vh !important;
  }
}

/* 11. Live Camera & Selfie Check-in on Mobile */
@media (max-width: 768px) {
  .selfie-camera-wrap {
    max-width: 100% !important;
    margin: 8px auto 12px auto !important;
    border-radius: 16px !important;
  }

  .selfie-camera-wrap .camera-live {
    max-height: 50vh !important;
    border-radius: 16px !important;
  }

  .face-status-badge {
    bottom: 10px !important;
    font-size: 0.74rem !important;
    padding: 6px 12px !important;
  }

  .check-actions {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
  }

  .check-actions .button {
    min-height: 46px !important;
    font-size: 0.92rem !important;
    font-weight: 700 !important;
  }

  .summary-row {
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 6px !important;
  }

  .summary-box {
    padding: 10px 8px !important;
  }

  .summary-box span {
    font-size: 0.65rem !important;
  }

  .summary-box b {
    font-size: 0.95rem !important;
  }
}

@media (max-width: 380px) {
  .check-actions {
    grid-template-columns: 1fr !important;
  }

  .summary-row {
    grid-template-columns: 1fr !important;
  }
}

/* 12. Cards, Roster & Filter Bar Responsiveness */
@media (max-width: 768px) {
  .filter-bar {
    overflow-x: auto !important;
    flex-wrap: nowrap !important;
    padding-bottom: 6px !important;
    -webkit-overflow-scrolling: touch !important;
  }

  .filter-bar .filter-pill {
    white-space: nowrap !important;
    flex-shrink: 0 !important;
    min-height: 38px;
  }

  .employee-card {
    padding: 16px 14px !important;
  }

  .employee-details {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
  }

  .card-actions {
    display: flex !important;
    gap: 8px !important;
    margin-top: 14px !important;
  }

  .card-actions .button {
    flex: 1 !important;
    justify-content: center !important;
    min-height: 40px !important;
  }

  .leave-card {
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 12px !important;
    padding: 16px 14px !important;
  }

  .leave-meta {
    width: 100% !important;
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
  }

  .leave-card > div:last-child {
    width: 100% !important;
    display: flex !important;
    gap: 8px !important;
  }

  .leave-card > div:last-child .button {
    flex: 1 !important;
    justify-content: center !important;
  }

  .shift-card, .report-card {
    padding: 16px 14px !important;
  }

  .shift-card .button, .report-card .button {
    width: 100% !important;
    justify-content: center !important;
    min-height: 42px !important;
  }

  .quick-item {
    padding: 10px 12px !important;
  }

  .activity-item {
    padding: 8px 0 !important;
  }
}

