/* ==========================================================================
   RESPONSIVE STYLES - Mobile & Tablet Adaptation
   Bulk Hub - Адаптивность для мобильных устройств
   Подключается ПОСЛЕ style.css для переопределения стилей
   ========================================================================== */

/* --- iOS Safari: Фикс белой полоски снизу --- */
html {
  /* Используем 100dvh для корректной высоты на iOS Safari */
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  background: #1B1A17; /* Тот же цвет что и body, чтобы не просвечивало белое */
}

body {
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  background: #1B1A17;
}

/* --- Базовые правила адаптивности --- */
img,
video,
svg {
  max-width: 100%;
  height: auto;
}

/* Улучшенный touch-target размер для мобильных устройств */
.is-touch .btn-primary,
.is-touch .btn-secondary,
.is-touch .sidebar__link {
  min-height: 44px; /* Рекомендуемый минимум для touch (Apple HIG) */
}

/* Плавные переходы для адаптивных изменений */
#sidebar,
#main,
.main-shell {
  transition: width 0.2s ease, margin 0.2s ease, padding 0.2s ease;
}

/* --- CSS Custom Properties для адаптива --- */
:root {
  --sidebar-width-mobile: 100%;
  --main-padding-mobile: 16px;
  --main-padding-tablet: 24px;
}

/* ==========================================================================
   TABLET BREAKPOINT (769px - 1024px)
   ========================================================================== */
@media (min-width: 769px) and (max-width: 1024px) {
  :root {
    --sidebar-width: 220px;
  }

  #sidebar {
    width: var(--sidebar-width);
    padding: 20px 16px;
  }

  #main {
    margin-left: var(--sidebar-width);
  }

  .main-shell {
    padding: 24px 20px 36px;
  }

  .sidebar__text {
    font-size: 13px;
  }

  .sidebar__icon {
    width: 20px;
    height: 20px;
  }

  /* Уменьшаем крупные шрифты на планшете */
  .social-activity__total-value {
    font-size: 44px;
  }

  .stake-tab__value {
    font-size: 32px;
  }

  .page-header h1 {
    font-size: 24px;
  }

  /* Карта */
  .map-stage {
    min-height: 500px;
  }

  /* Location panel на карте */
  .location-panel {
    width: min(280px, 85%);
    padding: 14px;
  }

  .location-panel__header h2 {
    font-size: 18px;
  }
}

/* ==========================================================================
   MOBILE BREAKPOINT - УЛУЧШЕННЫЙ (0 - 768px)
   Переопределяет существующий @media (max-width: 640px) из style.css
   ========================================================================== */
@media (max-width: 768px) {
  /* --- Основной layout --- */
  body {
    overflow: auto;
    overflow-x: hidden; /* Предотвращаем горизонтальный скролл */
  }

  #app {
    flex-direction: column;
    min-height: 100dvh; /* Динамическая высота для iOS Safari */
    min-height: -webkit-fill-available; /* Fallback для старых Safari */
  }

  /* --- Sidebar становится верхней навигацией --- */
  #sidebar {
    position: sticky;
    top: 0;
    left: 0;
    right: 0;
    bottom: auto;
    width: 100%;
    height: auto;
    min-height: 56px; /* Фиксированная минимальная высота */
    flex-direction: row;
    align-items: center;
    flex-wrap: nowrap; /* КРИТИЧНО: НЕ переносим, используем скролл */
    gap: 6px;
    padding: 10px 12px;
    border-right: none;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    z-index: 100;
    overflow-x: auto; /* Горизонтальный скролл если не влезает */
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }

  /* Скрываем скроллбар на мобильном sidebar */
  #sidebar::-webkit-scrollbar {
    display: none;
  }
  #sidebar {
    -ms-overflow-style: none;
    scrollbar-width: none;
  }

  /* --- Main content на мобилке --- */
  #main {
    margin-left: 0;
    width: 100%;
    min-height: calc(100dvh - 70px); /* Минус высота sidebar, dvh для iOS */
    min-height: calc(-webkit-fill-available - 70px); /* Fallback для старых Safari */
  }

  .main-shell {
    padding: var(--main-padding-mobile);
    padding-bottom: 32px;
  }

  /* --- Sidebar элементы на мобилке --- */
  .sidebar__brand {
    flex-shrink: 0;
    flex-grow: 0;
    width: auto; /* Авто-ширина под логотип */
    min-width: 0;
    overflow: visible;
  }

  .sidebar__brand-logo {
    height: 32px; /* Увеличенный логотип на мобиле */
    max-height: 36px;
    width: auto;
    object-fit: contain;
  }

  .sidebar__nav-group {
    flex-direction: row;
    gap: 2px;
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
  }

  .sidebar__label {
    display: none; /* Скрываем "Main" label на мобилке */
  }

  .sidebar__links {
    flex-direction: row;
    gap: 2px;
    flex-wrap: nowrap; /* НЕ переносим */
    display: flex;
    align-items: center;
  }

  .sidebar__link {
    padding: 8px;
    border-radius: 8px;
    flex-shrink: 0; /* НЕ сжимаем ссылки */
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
  }

  /* На мобиле <= 768px скрываем текст, только иконки */
  .sidebar__text {
    display: none;
  }

  .sidebar__icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
  }

  .sidebar__profile {
    margin-top: 0;
    padding-top: 0;
    border-top: none;
    flex-shrink: 0;
    margin-left: auto; /* Прижимаем Profile к правому краю */
  }
  
  .sidebar__profile .sidebar__link {
    padding: 8px;
  }

  /* --- Типография на мобилке --- */
  .page-header h1 {
    font-size: 22px;
  }

  .social-activity__total-value {
    font-size: 36px;
  }

  .stake-tab__value {
    font-size: 28px;
  }

  .social-activity__hero h2 {
    font-size: 24px;
  }

  /* --- Карточки и гриды на мобилке (одна колонка) --- */
  .card-grid,
  .profile-grid,
  .social-activity__metrics,
  .stake-tabs,
  .contributions__leaderboards {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .social-activity__leaderboard-grid {
    grid-template-columns: 1fr;
  }

  /* --- Карта глобуса на мобилке --- */
  .page-shell--map {
    height: auto;
    min-height: calc(100dvh - 56px); /* Минус высота header */
    display: flex;
    flex-direction: column;
    padding-bottom: 70px; /* Место для свёрнутого location-panel */
  }

  .map-stage {
    min-height: 280px;
    flex: 1;
    padding: 8px;
    display: flex;
    flex-direction: column;
  }

  .map-stage__inner {
    border-radius: 12px;
    flex: 1;
    min-height: 250px;
    position: relative;
    overflow: hidden;
  }
  
  /* Критично для корректной работы глобуса на мобильных */
  .globe-container {
    position: absolute !important;
    inset: 0 !important;
    width: 100% !important;
    height: 100% !important;
  }
  
  .globe-container canvas {
    display: block !important;
    width: 100% !important;
    height: 100% !important;
    touch-action: manipulation; /* Tap без 300ms задержки + drag */
  }
  
  .labels-container {
    position: absolute !important;
    inset: 0 !important;
    pointer-events: none;
  }

  /* Location panel на мобилке - компактный снизу */
  .location-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    max-width: none;
    max-height: 45vh; /* Максимум 45% экрана */
    margin: 0;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0));
    border-radius: 16px 16px 0 0;
    z-index: 50;
    overflow-y: auto;
    transform: translateY(0);
    transition: transform 0.3s ease, max-height 0.3s ease;
  }
  
  /* Когда panel свёрнут (добавляется JS-ом) */
  .location-panel.is-collapsed {
    max-height: 60px;
    overflow: hidden;
  }
  
  .location-panel.is-collapsed .location-panel__header .muted,
  .location-panel.is-collapsed .location-panel__status,
  .location-panel.is-collapsed #location-form,
  .location-panel.is-collapsed .location-panel__change {
    display: none;
  }

  .location-panel__header {
    cursor: pointer; /* Кликабельный для сворачивания */
  }
  
  .location-panel__header h2 {
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  
  /* Индикатор сворачивания */
  .location-panel__header h2::after {
    content: '▼';
    font-size: 10px;
    opacity: 0.5;
    transition: transform 0.2s ease;
  }
  
  .location-panel.is-collapsed .location-panel__header h2::after {
    transform: rotate(180deg);
  }
  
  /* Search autocomplete на мобиле */
  .location-search__results {
    position: fixed;
    bottom: 70px; /* Над свёрнутым location-panel */
    left: 8px;
    right: 8px;
    top: auto;
    max-height: 50vh;
    border-radius: 12px;
    z-index: 200;
  }
  
  .location-search__item {
    padding: 12px 14px;
    min-height: 48px; /* Touch-friendly */
  }
  
  .location-panel__divider {
    display: none; /* Скрываем на мобиле */
  }
  
  /* Скрываем поиск когда panel свёрнут */
  .location-panel.is-collapsed .location-search {
    display: none;
  }

  /* --- Stake transactions на мобилке --- */
  .stake-transaction {
    grid-template-columns: 1fr;
    gap: 8px;
    text-align: left;
  }

  .stake-transaction__amount {
    text-align: left;
  }

  .stake-tab {
    min-height: auto;
    padding: 14px;
  }

  /* --- Формы на мобилке --- */
  .form-group input {
    font-size: 16px; /* Предотвращает авто-zoom на iOS */
    padding: 14px 12px;
  }

  .btn-primary,
  .btn-secondary {
    padding: 14px 16px;
    font-size: 15px;
  }

  /* --- Модалки на мобилке --- */
  .modal-content {
    margin: 12px;
    padding: 20px;
    max-width: calc(100vw - 24px);
  }

  .login-modal__panel {
    width: calc(100% - 24px);
    max-width: 400px;
    padding: 24px 20px;
  }

  /* --- Search controls на мобилке --- */
  .social-activity__search-controls,
  .contributions__posts-search {
    grid-template-columns: 1fr;
  }

  .contributions__posts-search .form-group {
    min-width: 100%;
  }

  .contributions__posts-search button {
    width: 100%;
  }
}

/* ==========================================================================
   LARGE MOBILE (401px - 430px) — iPhone 14 Pro Max, iPhone 16 Pro Max
   ========================================================================== */
@media (min-width: 401px) and (max-width: 430px) {
  .map-stage {
    min-height: 320px;
    max-height: 55dvh;
  }
  
  .map-stage__inner {
    min-height: 280px;
  }
  
  .location-panel {
    padding: 14px;
  }
  
  .page-header h1 {
    font-size: 22px;
  }
}

/* ==========================================================================
   MEDIUM MOBILE (393px - 400px) — iPhone 14 Pro, iPhone 15 Pro, iPhone 16 Pro
   ========================================================================== */
@media (min-width: 393px) and (max-width: 400px) {
  .map-stage {
    min-height: 300px;
    max-height: 52dvh;
  }
  
  .map-stage__inner {
    min-height: 260px;
  }
}

/* ==========================================================================
   NARROW MOBILE (0 - 400px) — очень узкие экраны (iPhone SE, Galaxy S8)
   ========================================================================== */
@media (max-width: 400px) {
  .main-shell {
    padding: 12px;
    padding-bottom: 24px;
  }

  #sidebar {
    padding: 8px 10px;
    gap: 4px;
  }

  .sidebar__brand {
    width: auto;
  }
  
  .sidebar__brand-logo {
    height: 28px;
    max-height: 32px;
  }

  .sidebar__link {
    padding: 6px;
  }

  .sidebar__icon {
    width: 18px;
    height: 18px;
  }

  .page-header h1 {
    font-size: 20px;
  }

  .social-activity__total-value {
    font-size: 32px;
  }

  .stake-tab__value {
    font-size: 24px;
  }

  .leaderboard-card {
    padding: 14px;
  }

  .leaderboard-card__header h2,
  .leaderboard-card__network {
    font-size: 16px;
  }

  .x-posts-grid {
    gap: 10px;
  }

  .x-post-card {
    padding: 12px;
  }

  .x-post-card__body {
    font-size: 13px;
  }
}

/* ==========================================================================
   LANDSCAPE MOBILE — горизонтальная ориентация телефона
   ========================================================================== */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  #sidebar {
    padding: 8px 16px;
  }

  .sidebar__brand-logo {
    max-height: 28px;
  }

  .map-stage {
    min-height: 280px;
  }

  .page-shell--map {
    min-height: auto;
  }
}

/* ==========================================================================
   HOVER STATES — отключаем hover эффекты на touch устройствах
   ========================================================================== */
@media (hover: none) {
  .sidebar__link:hover {
    background: transparent;
    transform: none;
  }

  .btn-primary:hover:not(:disabled),
  .btn-secondary:hover {
    transform: none;
  }

  .x-post-card:hover {
    transform: none;
  }
}

/* ==========================================================================
   SAFE AREA (для iPhone с notch, Dynamic Island и home indicator)
   ========================================================================== */
@supports (padding: max(0px)) {
  /* Фикс белой полоски снизу на iOS Safari */
  html, body {
    padding-bottom: env(safe-area-inset-bottom, 0);
    background-color: #1B1A17;
  }
  
  #app {
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  
  @media (max-width: 768px) {
    #sidebar {
      padding-top: max(12px, env(safe-area-inset-top));
      padding-left: max(16px, env(safe-area-inset-left));
      padding-right: max(16px, env(safe-area-inset-right));
    }
    
    .main-shell {
      padding-left: max(16px, env(safe-area-inset-left));
      padding-right: max(16px, env(safe-area-inset-right));
      padding-bottom: max(32px, env(safe-area-inset-bottom));
    }
    
    /* Глобус на мобильных с safe-area */
    .map-stage {
      padding-left: max(8px, env(safe-area-inset-left));
      padding-right: max(8px, env(safe-area-inset-right));
    }
    
    /* Location panel уже имеет padding-bottom с safe-area */
  }
}

/* ==========================================================================
   iOS Safari Specific Fixes
   ========================================================================== */
/* Фикс для iPhone 14 Pro+ с Dynamic Island - дополнительный отступ сверху */
@supports (padding: env(safe-area-inset-top)) {
  @media (max-width: 430px) and (min-height: 800px) {
    #sidebar {
      padding-top: max(12px, calc(env(safe-area-inset-top) + 4px));
    }
  }
}

/* ==========================================================================
   PRINT STYLES
   ========================================================================== */
@media print {
  #sidebar {
    display: none;
  }

  #main {
    margin-left: 0;
  }

  .modal-overlay,
  .login-modal-overlay {
    display: none !important;
  }
}
