/* ==========================================
   PROFHAIR CRM - Mobile & PWA Styles v1.0
   Полная мобильная адаптивность
   ========================================== */

/* === PWA Standalone Mode === */
@media all and (display-mode: standalone) {
  body {
    -webkit-user-select: none;
    user-select: none;
  }
  /* Убираем padding-top для standalone, т.к. нет браузерной строки */
  .pwa-standalone-fix {
    padding-top: env(safe-area-inset-top) !important;
  }
}

/* === Safe Area для iPhone с notch === */
@supports (padding: max(0px)) {
  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-bottom: env(safe-area-inset-bottom);
  }
  
  /* Фиксированный хедер с учётом notch */
  header.mobile-header {
    padding-top: max(0.5rem, env(safe-area-inset-top));
    height: calc(3.5rem + env(safe-area-inset-top));
  }
  
  /* Нижние элементы с учётом home indicator */
  .bottom-fixed {
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }
}

/* === Базовые мобильные стили === */
* {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

/* Плавный скролл */
html {
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

/* Запрещаем двойной тап зум на iOS */
body {
  touch-action: manipulation;
}

/* === Мобильные таблицы (до 768px) === */
@media (max-width: 768px) {
  /* Горизонтальная прокрутка таблиц */
  .table-container,
  .overflow-x-auto,
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100vw;
  }

  /* Компактные ячейки */
  table td, table th {
    padding: 0.5rem 0.625rem !important;
    font-size: 0.8125rem !important;
    white-space: nowrap;
  }

  /* Карточный вид для таблиц (опционально) */
  .mobile-cards table {
    border: 0;
  }
  .mobile-cards thead {
    display: none;
  }
  .mobile-cards tr {
    display: block;
    margin-bottom: 0.75rem;
    border-radius: 0.75rem;
    background: rgba(30, 41, 59, 0.8);
    padding: 1rem;
    border: 1px solid rgba(51, 65, 85, 0.5);
  }
  .mobile-cards td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.375rem 0 !important;
    border-bottom: 1px solid rgba(51, 65, 85, 0.3);
    white-space: normal !important;
  }
  .mobile-cards td:last-child {
    border-bottom: none;
  }
  .mobile-cards td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 0.75rem;
    color: #94a3b8;
    text-transform: uppercase;
    margin-right: 1rem;
    flex-shrink: 0;
  }

  /* === Сетка карточек === */
  .grid-cols-2, .grid-cols-3, .grid-cols-4, .grid-cols-5,
  .md\:grid-cols-2, .md\:grid-cols-3, .md\:grid-cols-4, .md\:grid-cols-5,
  .lg\:grid-cols-2, .lg\:grid-cols-3, .lg\:grid-cols-4, .lg\:grid-cols-5 {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Одноколоночная для очень узких экранов */
  @media (max-width: 400px) {
    .grid-cols-2, .grid-cols-3, .grid-cols-4, .grid-cols-5,
    .md\:grid-cols-2, .md\:grid-cols-3, .md\:grid-cols-4, .md\:grid-cols-5,
    .lg\:grid-cols-2, .lg\:grid-cols-3, .lg\:grid-cols-4, .lg\:grid-cols-5 {
      grid-template-columns: 1fr !important;
    }
  }

  /* === Компактные карточки статистики === */
  .stat-card, [class*="rounded-2xl"][class*="bg-slate"] {
    padding: 0.875rem !important;
  }
  .stat-card h3, .stat-card .text-3xl, .stat-card .text-2xl {
    font-size: 1.375rem !important;
  }
  .stat-card p, .stat-card .text-xs {
    font-size: 0.6875rem !important;
  }

  /* === Кнопки === */
  button, .btn, a[class*="bg-amber"], a[class*="bg-blue"],
  a[class*="bg-green"], a[class*="bg-red"],
  [class*="rounded"][class*="px-"][class*="py-"] {
    min-height: 44px; /* Apple minimum touch target */
    min-width: 44px;
  }

  /* Группировка кнопок */
  .button-group, .flex.gap-2, .flex.gap-3 {
    flex-wrap: wrap;
  }

  /* === Формы === */
  input[type="text"], input[type="email"], input[type="tel"],
  input[type="number"], input[type="password"], input[type="search"],
  input[type="date"], select, textarea {
    font-size: 16px !important; /* Предотвращает зум на iOS */
    padding: 0.625rem 0.875rem !important;
    border-radius: 0.625rem !important;
    min-height: 44px;
  }

  /* === Модальные окна === */
  .modal, [class*="fixed"][class*="inset-0"][class*="z-50"] {
    align-items: flex-end !important;
  }
  .modal-content, [class*="fixed"][class*="inset-0"][class*="z-50"] > div {
    border-radius: 1.25rem 1.25rem 0 0 !important;
    max-height: 90vh !important;
    overflow-y: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
  }

  /* === Заголовки === */
  h1, .text-3xl { font-size: 1.5rem !important; }
  h2, .text-2xl { font-size: 1.25rem !important; }
  h3, .text-xl { font-size: 1.125rem !important; }

  /* === Spacing === */
  .p-8 { padding: 1rem !important; }
  .p-6 { padding: 0.875rem !important; }
  .px-8 { padding-left: 1rem !important; padding-right: 1rem !important; }
  .py-8 { padding-top: 1rem !important; padding-bottom: 1rem !important; }
  .gap-8 { gap: 1rem !important; }
  .gap-6 { gap: 0.75rem !important; }
  .mb-8 { margin-bottom: 1rem !important; }
  .mb-6 { margin-bottom: 0.75rem !important; }

  /* === Flex layouts === */
  .flex-col-mobile {
    flex-direction: column !important;
  }
  
  .md\:flex-row {
    flex-direction: column !important;
  }

  /* === Основной контент === */
  main {
    padding: 0.75rem !important;
    padding-top: 4rem !important;
  }

  /* === Скрываем ненужное на мобильных === */
  .hide-mobile, .desktop-only {
    display: none !important;
  }

  /* === Текст === */
  .text-sm { font-size: 0.8125rem !important; }
  
  /* Обрезка длинного текста */
  .truncate-mobile {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 150px;
  }
}

/* === Планшетные стили (769px - 1024px) === */
@media (min-width: 769px) and (max-width: 1024px) {
  .grid-cols-4, .lg\:grid-cols-4, .grid-cols-5, .lg\:grid-cols-5 {
    grid-template-columns: repeat(3, 1fr) !important;
  }
  
  main {
    padding: 1.5rem !important;
  }
}

/* === Touch-friendly элементы === */
.touch-target {
  min-height: 44px;
  min-width: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* === Pull to refresh индикатор === */
.pull-indicator {
  position: fixed;
  top: -60px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f59e0b;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: top 0.3s ease;
  z-index: 9999;
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
}
.pull-indicator.active {
  top: 20px;
}
.pull-indicator svg {
  width: 20px;
  height: 20px;
  color: white;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* === Skeleton loading для быстрой загрузки === */
.skeleton {
  background: linear-gradient(90deg, #1e293b 25%, #334155 50%, #1e293b 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 0.5rem;
}

@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

/* === PWA Install Banner === */
.pwa-install-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(135deg, #1e293b, #0f172a);
  border-top: 1px solid rgba(245, 158, 11, 0.3);
  padding: 1rem 1.25rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
  z-index: 9998;
  transform: translateY(100%);
  transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
}
.pwa-install-banner.show {
  transform: translateY(0);
}
.pwa-install-banner .install-btn {
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: white;
  font-weight: 600;
  padding: 0.75rem 1.5rem;
  border-radius: 0.75rem;
  border: none;
  cursor: pointer;
  font-size: 0.875rem;
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
  transition: all 0.2s;
}
.pwa-install-banner .install-btn:active {
  transform: scale(0.97);
}
.pwa-install-banner .dismiss-btn {
  color: #94a3b8;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5rem;
  font-size: 0.8125rem;
}

/* === Анимации переходов между страницами === */
.page-transition {
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* === Обновление доступно баннер === */
.update-banner {
  position: fixed;
  top: env(safe-area-inset-top, 0);
  left: 0.5rem;
  right: 0.5rem;
  background: linear-gradient(135deg, #059669, #047857);
  color: white;
  padding: 0.875rem 1.25rem;
  border-radius: 0 0 1rem 1rem;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.875rem;
  font-weight: 500;
  box-shadow: 0 4px 20px rgba(5, 150, 105, 0.3);
  transform: translateY(-100%);
  transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.update-banner.show {
  transform: translateY(0);
}

/* === Offline indicator === */
.offline-indicator {
  position: fixed;
  top: env(safe-area-inset-top, 0);
  left: 0;
  right: 0;
  background: #ef4444;
  color: white;
  text-align: center;
  padding: 0.375rem;
  font-size: 0.75rem;
  font-weight: 600;
  z-index: 10001;
  transform: translateY(-100%);
  transition: transform 0.3s ease;
}
.offline-indicator.show {
  transform: translateY(0);
}

/* === Адаптивные конкретные страницы === */

/* Дашборд */
@media (max-width: 768px) {
  .dashboard-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.75rem !important;
  }
  .dashboard-chart {
    height: 200px !important;
  }
}

/* Страница студентов */
@media (max-width: 768px) {
  .student-avatar {
    width: 2rem !important;
    height: 2rem !important;
  }
  .student-info {
    font-size: 0.8125rem !important;
  }
}

/* Финансы */
@media (max-width: 768px) {
  .finance-card {
    padding: 0.75rem !important;
  }
  .finance-amount {
    font-size: 1.25rem !important;
  }
}

/* Группы */
@media (max-width: 768px) {
  .group-card {
    padding: 0.875rem !important;
  }
  .attendance-grid {
    gap: 0.25rem !important;
  }
  .attendance-cell {
    width: 1.75rem !important;
    height: 1.75rem !important;
    font-size: 0.625rem !important;
  }
}

/* === Печать: скрываем PWA элементы === */
@media print {
  .pwa-install-banner,
  .offline-indicator,
  .update-banner,
  .pull-indicator,
  header.mobile-header,
  #sidebar-overlay {
    display: none !important;
  }
}
