/* ========================================
   MOBILE RESPONSIVENESS
   Otimizado para Safari/iOS
   ======================================== */

/* === iOS SAFE AREAS === */
:root {
  /* Fallback para navegadores que não suportam env() */
  --safe-area-inset-top: 0px;
  --safe-area-inset-bottom: 0px;
  --safe-area-inset-left: 0px;
  --safe-area-inset-right: 0px;
}

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

/* === DYNAMIC VIEWPORT HEIGHT (100dvh) === */
/* Corrige problema do 100vh no iOS com barra de endereço */
html {
  height: 100%;
  height: -webkit-fill-available;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  /* Padding para safe areas do iPhone */
  padding-top: var(--safe-area-inset-top);
  padding-bottom: var(--safe-area-inset-bottom);
  padding-left: var(--safe-area-inset-left);
  padding-right: var(--safe-area-inset-right);
}

/* Tablets e menores */
@media (max-width: 1024px) {
  .grid-cols-4 {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  .sidebar {
    width: 240px;
  }

  .main-content {
    margin-left: 240px;
  }
}

/* Mobile Landscape e menores */
@media (max-width: 768px) {
  /* === LAYOUT === */
  body {
    overflow-x: hidden;
  }

  /* Sidebar colapsável */
  .sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    height: 100vh;
    transition: left 0.3s ease;
    z-index: 1000;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
  }

  .sidebar.open {
    left: 0;
  }

  /* Overlay quando sidebar aberta */
  .sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 999;
  }

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

  /* Main content full width */
  .main-content {
    margin-left: 0 !important;
    padding: 1rem !important;
    width: 100%;
  }

  /* === HEADER === */
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 1rem 0;
  }

  .page-title {
    font-size: 1.5rem !important;
  }

  /* === GRID === */
  .grid {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  .grid-cols-2,
  .grid-cols-3,
  .grid-cols-4 {
    grid-template-columns: 1fr !important;
  }

  /* === CARDS === */
  .card {
    padding: 1rem !important;
    margin-bottom: 1rem;
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 0.75rem;
  }

  .card-title {
    font-size: 1.125rem !important;
  }

  /* === STAT CARDS === */
  .stat-card {
    padding: 1rem !important;
    min-height: auto !important;
  }

  .stat-value {
    font-size: 1.75rem !important;
  }

  .stat-label {
    font-size: 0.75rem !important;
  }

  /* === MODAIS === */
  .modal {
    padding: 0.5rem;
  }

  .modal-content {
    width: calc(100vw - 1rem) !important;
    max-width: none !important;
    margin: 0.5rem !important;
    max-height: calc(100vh - 1rem);
    overflow-y: auto;
  }

  .modal-header {
    padding: 1rem !important;
  }

  .modal-body {
    padding: 1rem !important;
  }

  .modal-footer {
    flex-direction: column-reverse;
    gap: 0.5rem;
    padding: 1rem !important;
  }

  .modal-footer .btn {
    width: 100%;
  }

  /* === FORMULÁRIOS === */
  .form-group {
    margin-bottom: 1rem;
  }

  input,
  select,
  textarea,
  .form-input,
  .form-select,
  .form-textarea {
    font-size: 16px !important; /* Evita zoom automático no iOS */
    -webkit-appearance: none;
    padding: 0.75rem !important;
  }

  /* === BOTÕES === */
  .btn {
    min-height: 44px; /* Apple touch target size */
    padding: 0.75rem 1rem !important;
    font-size: 0.875rem !important;
    width: 100%;
    justify-content: center;
  }

  .btn-group {
    flex-direction: column;
    gap: 0.5rem;
  }

  .btn-group .btn {
    width: 100%;
  }

  /* === CONVERSAÇÕES === */
  .conversation-item {
    padding: 0.75rem !important;
  }

  .conversation-avatar {
    width: 40px !important;
    height: 40px !important;
    font-size: 0.875rem !important;
  }

  .conversation-name {
    font-size: 0.875rem !important;
  }

  .conversation-preview {
    font-size: 0.75rem !important;
  }

  /* === TABELAS === */
  .table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  table {
    font-size: 0.875rem !important;
  }

  /* === KANBAN (CRM) === */
  .kanban-board {
    flex-direction: column !important;
    gap: 1rem !important;
  }

  .kanban-column {
    width: 100% !important;
    min-width: auto !important;
  }

  .lead-card {
    margin-bottom: 0.75rem;
  }

  /* === MENU HAMBURGER === */
  .menu-toggle {
    display: block !important;
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 1001;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.5rem;
    width: 44px;
    height: 44px;
    cursor: pointer;
    transition: all 0.3s;
  }

  .menu-toggle:active {
    transform: scale(0.95);
  }

  .menu-toggle span {
    display: block;
    width: 24px;
    height: 2px;
    background: var(--text-primary);
    margin: 4px 0;
    transition: all 0.3s;
  }

  .menu-toggle.open span:nth-child(1) {
    transform: rotate(45deg) translate(5px, 5px);
  }

  .menu-toggle.open span:nth-child(2) {
    opacity: 0;
  }

  .menu-toggle.open span:nth-child(3) {
    transform: rotate(-45deg) translate(7px, -6px);
  }

  /* === QR CODE === */
  #qr-container img {
    max-width: 100% !important;
    height: auto !important;
  }

  /* === CHARTS === */
  canvas {
    max-width: 100% !important;
    height: auto !important;
  }

  /* === BADGES === */
  .badge {
    font-size: 0.625rem !important;
    padding: 0.25rem 0.5rem !important;
  }

  /* === EMPTY STATES === */
  .empty-state {
    padding: 2rem 1rem !important;
  }

  .empty-state-icon {
    font-size: 2rem !important;
  }

  .empty-state-title {
    font-size: 1rem !important;
  }

  .empty-state-description {
    font-size: 0.875rem !important;
  }

  /* === TRAINING CARDS === */
  .example-card,
  .training-card {
    padding: 1rem !important;
  }

  /* === INSIGHTS === */
  .insight-card {
    padding: 1rem !important;
  }

  /* === SETTINGS === */
  .weekday-checkbox {
    padding: 0.5rem !important;
  }

  .weekday-checkbox span {
    font-size: 0.75rem !important;
  }

  /* === UTILITIES === */
  .mb-lg,
  .mb-xl {
    margin-bottom: 1rem !important;
  }

  .mt-lg,
  .mt-xl {
    margin-top: 1rem !important;
  }

  .p-lg,
  .p-xl {
    padding: 1rem !important;
  }

  /* Hide desktop-only elements */
  .desktop-only {
    display: none !important;
  }

  /* Show mobile-only elements */
  .mobile-only {
    display: block !important;
  }
}

/* Mobile Portrait (smartphones) */
@media (max-width: 480px) {
  .page-title {
    font-size: 1.25rem !important;
  }

  .card {
    padding: 0.75rem !important;
  }

  .stat-value {
    font-size: 1.5rem !important;
  }

  .btn {
    padding: 0.625rem 0.875rem !important;
    font-size: 0.8125rem !important;
  }

  .modal-content {
    border-radius: 0 !important;
    max-height: 100vh;
  }

  /* Força altura mínima de campos para iOS */
  input,
  select,
  textarea {
    min-height: 44px !important;
  }
}

/* === TOUCH IMPROVEMENTS === */
@media (hover: none) and (pointer: coarse) {
  /* Aumenta área de toque */
  button,
  a,
  .btn,
  .clickable {
    min-height: 44px;
    min-width: 44px;
  }

  /* Remove hover effects em touch devices */
  .hover-effect:hover {
    transform: none !important;
  }

  /* Melhor feedback de toque */
  button:active,
  .btn:active,
  .clickable:active {
    opacity: 0.7;
    transform: scale(0.98);
  }
}

/* === LANDSCAPE ORIENTATION === */
@media (max-width: 768px) and (orientation: landscape) {
  .modal-content {
    max-height: 90vh;
  }

  .sidebar {
    width: 200px;
  }
}

/* === PRINT === */
@media print {
  .sidebar,
  .menu-toggle,
  .btn,
  .modal {
    display: none !important;
  }

  .main-content {
    margin-left: 0 !important;
  }

  .card {
    break-inside: avoid;
  }
}
