<template>
  <div id="app">
    <header class="app-header">
      <div class="container header-container">
        <!-- Логотип -->
        <router-link to="/" class="logo-link">
          <div class="logo">
            <div class="logo-box">
              <img src="./assets/logo/Logo.png" alt="Little logo begushiy bashkir" class="little-logo">
            </div>
            <div class="logo-box">
              <span><i>Бегущий Башкир</i></span>
            </div>
            <div class="logo-box team">
              <span>team</span>
            </div>
          </div>
        </router-link>

        <!-- Бургер-меню для всех устройств -->
        <div class="burger-menu-container">
          <button class="burger-menu" :class="{ 'active': isMobileMenuOpen }" @click="toggleMobileMenu"
            aria-label="Открыть меню">
            <span></span>
            <span></span>
            <span></span>
          </button>

          <!-- Выпадающее меню -->
          <div class="dropdown-menu" :class="{ 'active': isMobileMenuOpen }" ref="dropdownMenu">
            <nav class="dropdown-nav">
              <div class="mobile-menu-header">
                <div class="mobile-logo">
                  <img src="./assets/logo/Logo.png" alt="Little logo begushiy bashkir" class="little-logo">
                  <span>Бегущий Башкир</span>
                </div>
                <button class="close-menu" @click="closeMobileMenu" aria-label="Закрыть меню">
                  ×
                </button>
              </div>
              
              <div class="dropdown-nav-content">
                <router-link to="/" class="dropdown-nav-link" @click="closeMobileMenu">
                  🏠 Главная
                </router-link>
                <router-link to="/about" class="dropdown-nav-link" @click="closeMobileMenu">
                  👥 О нас
                </router-link>
                <router-link to="/achievements" class="dropdown-nav-link" @click="closeMobileMenu">
                  🏆 Достижения
                </router-link>
                <router-link to="/gallery" class="dropdown-nav-link" @click="closeMobileMenu">
                  📸 Галерея
                </router-link>
                <router-link to="/training" class="dropdown-nav-link" @click="closeMobileMenu">
                  📅 Тренировки
                </router-link>
                <router-link to="/news" class="dropdown-nav-link" @click="closeMobileMenu">
                  📰 Новости
                </router-link>
                <router-link to="/members" class="dropdown-nav-link" @click="closeMobileMenu">
                  👥 Участники
                </router-link>
                <router-link to="/reviews" class="dropdown-nav-link" @click="closeMobileMenu">
                  ⭐ Отзывы
                </router-link>
                <div class="dropdown-divider"></div>
                <router-link to="/register" class="dropdown-nav-link accent" @click="closeMobileMenu">
                  📝 Регистрация
                </router-link>
                <router-link to="/login" class="dropdown-nav-link accent" @click="closeMobileMenu">
                  🔐 Войти
                </router-link>
              </div>
            </nav>
          </div>
        </div>
      </div>
    </header>

    <!-- Оверлей для мобильных -->
    <div class="mobile-overlay" :class="{ 'active': isMobileMenuOpen }" @click="closeMobileMenu"></div>

    <main class="main-content" :class="{ 'menu-open': isMobileMenuOpen }">
      <router-view />
    </main>

    <footer class="app-footer">
      <div class="container">
        <p>© 2025 Беговой клуб "Бегущий Башкир". Все права защищены.</p>
        <p>Уфа, Республика Башкортостан</p>
      </div>
    </footer>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      isMobileMenuOpen: false
    }
  },
  methods: {
    toggleMobileMenu() {
      this.isMobileMenuOpen = !this.isMobileMenuOpen
      // Блокируем скролл body когда меню открыто на мобильных
      if (window.innerWidth <= 767) {
        document.body.style.overflow = this.isMobileMenuOpen ? 'hidden' : ''
      }
    },
    closeMobileMenu() {
      this.isMobileMenuOpen = false
      document.body.style.overflow = ''
    },
    handleClickOutside(event) {
      if (!event.target.closest('.burger-menu-container') && 
          !event.target.closest('.dropdown-menu') && 
          this.isMobileMenuOpen) {
        this.closeMobileMenu()
      }
    },
    handleEscapeKey(event) {
      if (event.key === 'Escape' && this.isMobileMenuOpen) {
        this.closeMobileMenu()
      }
    },
    handleResize() {
      // Закрываем мобильное меню при переходе на десктоп
      if (window.innerWidth > 767 && this.isMobileMenuOpen) {
        this.closeMobileMenu()
      }
    },
    handleTouchMove(event) {
      // Обработка свайпа для закрытия меню на мобильных
      if (this.isMobileMenuOpen && window.innerWidth <= 767) {
        const touch = event.touches[0]
        const startX = touch.clientX
        const menu = this.$refs.dropdownMenu
        
        if (menu && startX < window.innerWidth * 0.3) {
          event.preventDefault()
          this.closeMobileMenu()
        }
      }
    }
  },
  mounted() {
    document.addEventListener('click', this.handleClickOutside)
    document.addEventListener('keydown', this.handleEscapeKey)
    document.addEventListener('touchmove', this.handleTouchMove, { passive: false })
    window.addEventListener('resize', this.handleResize)
    
    this.$router.afterEach(() => {
      this.closeMobileMenu()
    })
  },
  beforeUnmount() {
    document.removeEventListener('click', this.handleClickOutside)
    document.removeEventListener('keydown', this.handleEscapeKey)
    document.removeEventListener('touchmove', this.handleTouchMove)
    window.removeEventListener('resize', this.handleResize)
    document.body.style.overflow = '' // Восстанавливаем скролл при размонтировании
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

a {
  text-decoration: none;
  color: inherit;
}

body {
  font-family: 'Arial', sans-serif;
  line-height: 1.6;
  color: #333;
  overflow-x: hidden;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Header Styles */
.app-header {
  background-color: #2e8b56;
  color: white;
  padding: 1rem 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
}

/* Logo Styles */
.logo-link {
  text-decoration: none;
  color: inherit;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
  z-index: 1002;
}

.logo-box {
  display: flex;
  align-items: center;
}

.little-logo {
  width: 2.5rem;
  height: 2.5rem;
  object-fit: contain;
}

.team {
  font-size: 0.8rem;
  opacity: 0.8;
  align-self: flex-end;
  margin-bottom: 0.2rem;
}

/* Burger Menu Container */
.burger-menu-container {
  position: relative;
  z-index: 1001;
}

/* Burger Menu Button */
.burger-menu {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  position: relative;
  transition: transform 0.3s ease;
}

.burger-menu:hover {
  transform: scale(1.1);
}

.burger-menu span {
  display: block;
  height: 3px;
  width: 100%;
  background-color: white;
  border-radius: 3px;
  transition: all 0.3s ease;
  transform-origin: center;
}

.burger-menu.active span:nth-child(1) {
  transform: rotate(45deg) translate(6px, 6px);
}

.burger-menu.active span:nth-child(2) {
  opacity: 0;
}

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

/* Dropdown Menu */
.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  min-width: 280px;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-10px);
  transition: all 0.3s ease;
  border: 1px solid #e9ecef;
  margin-top: 10px;
  max-height: 80vh; /* Ограничиваем высоту на десктопе */
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.dropdown-menu.active {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.dropdown-nav {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.mobile-menu-header {
  display: none; /* Скрываем на десктопе */
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  background: #2e8b57;
  color: white;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.mobile-logo {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  font-weight: bold;
}

.mobile-logo .little-logo {
  width: 2rem;
  height: 2rem;
}

.close-menu {
  display: none; /* Скрываем на десктопе */
  background: none;
  border: none;
  color: white;
  font-size: 2rem;
  cursor: pointer;
  width: 40px;
  height: 40px;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.3s ease;
}

.close-menu:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.dropdown-nav-content {
  flex: 1;
  padding: 1rem 0;
  overflow-y: auto; /* Включаем скролл при необходимости */
}

.dropdown-nav-link {
  display: flex;
  align-items: center;
  padding: 1rem 1.5rem;
  color: #333;
  text-decoration: none;
  transition: all 0.3s ease;
  font-size: 1rem;
  border-left: 3px solid transparent;
  white-space: nowrap;
}

.dropdown-nav-link:hover {
  background-color: #f8fff8;
  color: #2e8b57;
  border-left-color: #2e8b57;
  padding-left: 2rem;
}

.dropdown-nav-link.router-link-active {
  background-color: #f0f8f0;
  color: #2e8b57;
  border-left-color: #2e8b57;
  font-weight: 600;
}

.dropdown-nav-link.accent {
  background-color: #f8fff8;
  color: #2e8b57;
  font-weight: 600;
}

.dropdown-nav-link.accent:hover {
  background-color: #2e8b57;
  color: white;
}

.dropdown-divider {
  height: 1px;
  background: #e9ecef;
  margin: 0.5rem 1.5rem;
}

/* Mobile Overlay */
.mobile-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  z-index: 998;
}

.mobile-overlay.active {
  opacity: 1;
  visibility: visible;
}

/* Main Content */
.main-content {
  min-height: calc(100vh - 140px);
  padding: 2rem 0;
  transition: filter 0.3s ease;
}

.main-content.menu-open {
  filter: blur(2px);
}

/* Footer */
.app-footer {
  background-color: #1a3e23;
  color: white;
  text-align: center;
  padding: 1.5rem 0;
  margin-top: 2rem;
}

.app-footer p {
  margin-bottom: 0.5rem;
}

/* Responsive Design */

/* Desktop (1024px and above) */
@media (min-width: 1024px) {
  .dropdown-menu {
    min-width: 320px;
  }

  .dropdown-nav-link {
    padding: 1.2rem 2rem;
    font-size: 1.1rem;
  }
}

/* Tablet (768px - 1023px) */
@media (max-width: 1023px) and (min-width: 768px) {
  .dropdown-menu {
    min-width: 300px;
  }

  .dropdown-nav-link {
    padding: 1.1rem 1.8rem;
    font-size: 1.05rem;
  }

  .team {
    display: none;
  }
}

/* Mobile (767px and below) */
@media (max-width: 767px) {
  .dropdown-menu {
    position: fixed;
    top: 0;
    right: -100%;
    width: 85%;
    max-width: 320px;
    height: 100vh;
    border-radius: 0;
    margin-top: 0;
    transition: right 0.3s ease;
    box-shadow: -5px 0 25px rgba(0, 0, 0, 0.3);
    max-height: 100vh;
    overflow: hidden;
  }

  .dropdown-menu.active {
    right: 0;
  }

  .mobile-menu-header {
    display: flex;
    flex-shrink: 0;
  }

  .close-menu {
    display: flex;
  }

  .dropdown-nav-content {
    padding: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch; /* Плавный скролл на iOS */
  }

  .dropdown-nav-link {
    padding: 1.4rem 1.5rem;
    font-size: 1.1rem;
    border-left: none;
    border-bottom: 1px solid #f0f0f0;
  }

  .dropdown-nav-link:hover,
  .dropdown-nav-link.router-link-active {
    border-left: none;
    border-bottom: 1px solid #2e8b57;
    padding-left: 1.5rem;
  }

  .logo {
    font-size: 1.1rem;
  }

  .team {
    display: none;
  }

  .little-logo {
    width: 2rem;
    height: 2rem;
  }

  .container {
    padding: 0 15px;
  }

  /* Отключаем скролл body когда меню открыто */
  body.menu-open {
    overflow: hidden;
  }
}

/* Small Mobile (480px and below) */
@media (max-width: 480px) {
  .dropdown-menu {
    width: 90%;
    max-width: none;
  }

  .dropdown-nav-link {
    padding: 1.3rem 1.5rem;
    font-size: 1rem;
  }

  .logo {
    font-size: 1rem;
    gap: 0.3rem;
  }

  .little-logo {
    width: 1.8rem;
    height: 1.8rem;
  }

  .main-content {
    padding: 1rem 0;
  }

  .mobile-menu-header {
    padding: 1rem;
  }

  .mobile-logo {
    font-size: 0.9rem;
  }

  .mobile-logo .little-logo {
    width: 1.6rem;
    height: 1.6rem;
  }
}

/* Очень маленькие экраны (360px и меньше) */
@media (max-width: 360px) {
  .dropdown-menu {
    width: 95%;
  }

  .dropdown-nav-link {
    padding: 1.2rem 1rem;
    font-size: 0.95rem;
  }

  .logo-box:nth-child(2) {
    display: none; /* Скрываем текст логотипа на очень маленьких экранах */
  }
}

/* Large Desktop (1440px and above) */
@media (min-width: 1440px) {
  .dropdown-menu {
    min-width: 350px;
  }

  .dropdown-nav-link {
    padding: 1.3rem 2.2rem;
    font-size: 1.15rem;
  }
}

/* Стили для скроллбара в меню */
.dropdown-nav-content::-webkit-scrollbar {
  width: 4px;
}

.dropdown-nav-content::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.dropdown-nav-content::-webkit-scrollbar-thumb {
  background: #2e8b57;
  border-radius: 2px;
}

.dropdown-nav-content::-webkit-scrollbar-thumb:hover {
  background: #26734a;
}

/* Button Styles */
.btn {
  display: inline-block;
  background-color: #ffd700;
  color: #333;
  padding: 12px 30px;
  border-radius: 5px;
  text-decoration: none;
  font-weight: bold;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s;
  margin: 0.5rem;
}

.btn:hover {
  background-color: #e6c200;
}

.btn-secondary {
  background-color: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background-color: #545b62;
}

/* Page Styles */
.page {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
  text-align: center;
}

.page h1 {
  color: #2e8b57;
  margin-bottom: 1rem;
  font-size: 2.5rem;
}

.page p {
  font-size: 1.1rem;
  margin-bottom: 1.5rem;
  color: #666;
}

/* Анимации */
@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown-menu.active {
  animation: slideIn 0.3s ease;
}

/* Поддержка свайпа */
.dropdown-menu {
  touch-action: pan-y;
}
</style>