:root {
  --header-padding: 12px; /* <-- ИЗМЕНЕНИЕ: Уменьшено для лёгкого подъёма */
  --touch-target: 44px;
  --border-radius: 12px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
}

body {
  background-color: #0f172a;
  color: #ffffff;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  margin: 0;
  padding: 0; /* <-- ФИКС: Убраны лишние margins/paddings */
  min-height: 100vh;
  font-size: clamp(14px, 4vw, 16px);
  line-height: 1.5;
  scroll-behavior: smooth;
  width: 100%; /* <-- ФИКС: Strict full width */
  max-width: 100vw;
  overflow-x: hidden; /* Prevent horizontal scroll/motion */
  box-sizing: border-box; /* Include paddings in width */
  /* <-- ФИКС: Safe areas (TG vars + fallback 0px) */
  padding-top: var(--tg-safe-area-inset-top, 0px);
  padding-bottom: var(--tg-content-safe-area-inset-bottom, 0px);
  padding-left: max(var(--tg-safe-area-inset-left, 0px), var(--tg-content-safe-area-inset-left, 0px));
  padding-right: max(var(--tg-safe-area-inset-right, 0px), var(--tg-content-safe-area-inset-right, 0px));
  overscroll-behavior: none;
}

.smooth-scroll {
  /* Уже в body */
}

/* ----- HEADER / NAVIGATION ----- */
.global-nav {
  z-index: 100;
  background-color: #0f172a;
  border-bottom: 1px solid #334155;
  padding: var(--header-padding) 4px; /* <-- ИЗМЕНЕНИЕ: Меньше bottom для подъёма */
  display: flex;
  align-items: center; /* <-- ФИКС: Center vertical для подъёма элементов */
  transition: background-color 0.3s ease;
  /* <-- ФИКС: Safe areas для header (TG + system) */
  padding-top: max(var(--tg-content-safe-area-inset-top, 0px), var(--tg-safe-area-inset-top, 0px));
  padding-left: max(var(--tg-safe-area-inset-left, 0px), var(--tg-content-safe-area-inset-left, 0px));
  padding-right: max(var(--tg-safe-area-inset-right, 0px), var(--tg-content-safe-area-inset-right, 0px));
  /* Sticky только на десктопе */
  position: sticky;
  top: 0;
  width: 100%; /* Full width */
  left: 0;
  right: 0;
  box-sizing: border-box;
  margin: 0; /* No margins */
}

.nav-content {
  flex: 1;
  max-width: 1440px; /* Десктоп max-width сохранён */
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-start; /* <-- ФИКС: Start для компактности/подъёма */
  gap: 2px; /* <-- ИЗМЕНЕНИЕ: Ещё меньше gap для подъёма */
  padding: 0 clamp(8px, 4vw, 16px); /* <-- ФИКС: Адаптивные боковые паддинги, full-width на мобиле */
  width: 100%;
  max-width: 100%; /* <-- ФИКС: No constraint on mobile */
  box-sizing: border-box;
}

.nav-top {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  flex-wrap: wrap;
}

.logo {
  font-size: clamp(16px, 5vw, 20px);
  font-weight: 700;
  line-height: 1;
  display: flex;
  align-items: center;
  padding: 4px 8px;
  transition: color 0.2s ease;
  flex-shrink: 0;
}

/* Search wrapper for full-width */
.search-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  position: relative;
}

#market-search {
  flex: 1;
  min-width: 120px;
  max-width: 100%;
  padding: clamp(6px, 2vw, 8px) clamp(12px, 3vw, 16px);
  background-color: #1e293b;
  border: 1px solid #334155;
  border-radius: var(--border-radius);
  color: #ffffff;
  font-size: inherit;
  line-height: 1.3;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  box-sizing: border-box;
}

#market-search:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
  outline: none;
}

#search-button {
  flex-shrink: 0;
  background-color: #3b82f6;
  color: #ffffff;
  border: none;
  border-radius: var(--border-radius);
  padding: clamp(6px, 2vw, 8px) clamp(12px, 3vw, 16px);
  font-size: inherit;
  cursor: pointer;
  min-height: var(--touch-target);
  transition: background-color 0.2s ease, transform 0.1s ease;
  box-shadow: var(--shadow-sm);
  white-space: nowrap;
  flex: 0 0 auto;
  box-sizing: border-box;
}

#search-button:hover,
#search-button:active {
  background-color: #2563eb;
  transform: translateY(-1px);
}

/* Filters row for params */
.filters-row {
  display: none; /* По умолчанию скрыто */
  flex-direction: row;
  gap: 8px;
  width: 100%;
}

.desktop-filters { /* <-- ФИКС: Класс для десктопа */
  flex-wrap: nowrap; /* No wrap on desktop */
  align-items: center;
}

#sort-select,
#status-select {
  flex-shrink: 0;
  width: auto;
  min-width: 100px;
  min-height: var(--touch-target);
  max-width: 35%;
  box-sizing: border-box;
}

/* Selects styles */
select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-color: #1e293b;
  color: #ffffff;
  border: 1px solid #334155;
  border-radius: var(--border-radius);
  padding: clamp(4px, 1.5vw, 8px) clamp(8px, 2vw, 32px) clamp(4px, 1.5vw, 8px) clamp(8px, 2vw, 12px);
  font-size: inherit;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  background-image: url("data:image/svg+xml,%3Csvg fill='white' height='20' viewBox='0 0 24 24' width='20' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right clamp(8px, 2vw, 12px) center;
  background-size: clamp(16px, 4vw, 20px);
  box-sizing: border-box;
}

select:hover,
select:focus {
  background-color: #334155;
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* Categories */
.nav-categories {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  white-space: nowrap;
  -ms-overflow-style: none;
  scrollbar-width: none;
  padding-bottom: 2px;
  scroll-snap-type: x mandatory;
  margin: 0; /* <-- ФИКС: Убрал negative margin для стабильности ширины */
  padding: 0 clamp(4px, 2vw, 8px); /* Inline padding вместо margin */
  width: 100%; /* Full width */
  box-sizing: border-box;
}

.nav-categories::-webkit-scrollbar {
  display: none;
}

.category {
  background: none;
  border: none;
  color: #94a3b8;
  font-size: clamp(12px, 3.5vw, 14px);
  cursor: pointer;
  padding: 6px 10px;
  min-height: var(--touch-target);
  border-radius: var(--border-radius);
  transition: color 0.2s ease, background-color 0.2s ease, transform 0.1s ease;
  white-space: nowrap;
  flex-shrink: 0;
  scroll-snap-align: start;
  touch-action: manipulation;
  box-sizing: border-box;
}

.category:hover,
.category:focus,
.category.active {
  color: #ffffff;
  background-color: #334155;
  transform: translateY(-1px);
}

.category:active {
  transform: scale(0.98);
}

/* Markets container */
#markets-container {
  max-width: 1440px; /* Десктоп max-width сохранён */
  margin: 0 auto;
  padding: clamp(8px, 4vw, 16px); /* <-- ФИКС: Адаптивные паддинги */
  width: 100%;
  max-width: 100%; /* <-- ФИКС: Full-width on mobile */
  box-sizing: border-box;
  overflow-x: hidden; /* <-- ФИКС: Prevent motion */
}

#markets-container h2 {
  font-size: clamp(18px, 5vw, 20px);
  font-weight: 700;
  margin: 16px 0;
  padding: 0 8px;
  width: 100%;
}

#markets-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  width: 100%; /* Full width */
  overflow-x: hidden; /* <-- ФИКС: No horizontal */
}

/* Market card */
.market-card {
  background-color: #1e293b;
  border-radius: var(--border-radius);
  padding: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  position: relative;
  touch-action: manipulation; /* Key for mobile tap without delay */
  width: 100%;
  box-sizing: border-box;
}

.market-card:hover,
.market-card:focus-within {
  background-color: #334155;
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.market-card:active {
  transform: scale(0.98);
}

.market-img {
  width: 100%;
  height: 140px;
  object-fit: cover;
  border-radius: var(--border-radius);
  margin-bottom: 12px;
  filter: blur(2px);
  transition: filter 0.3s ease;
}

.market-img.loaded {
  filter: none;
}

.emoji-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  background-color: #334155;
  height: 140px;
  border-radius: var(--border-radius);
  transition: background-color 0.2s ease;
}

.market-card-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.market-title {
  font-size: clamp(14px, 4vw, 16px);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 1.3;
  transition: color 0.2s ease;
}

.market-options {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: calc(2 * 22px + 6px);
  overflow: hidden;
  position: relative;
}

.market-options::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 12px;
  background: linear-gradient(to bottom, rgba(30,41,59,0), rgba(30,41,59,1));
  pointer-events: none;
}

.market-option-row {
  display: flex;
  justify-content: space-between;
  font-size: clamp(12px, 3.5vw, 14px);
  transition: color 0.2s ease;
}

.market-option-label {
  color: #94a3b8;
}

.market-option-pct {
  font-weight: 500;
  color: #3b82f6;
}

.market-meta {
  display: flex;
  justify-content: space-between;
  font-size: clamp(12px, 3.5vw, 14px);
  color: #94a3b8;
  gap: 8px;
}

/* Snackbar, loading, error — без изменений */
.snackbar {
  background-color: #1e293b;
  color: #ffffff;
  padding: 16px;
  border-radius: var(--border-radius);
  text-align: center;
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  min-width: 280px;
  max-width: 90vw;
  box-shadow: var(--shadow-md);
  transition: opacity 0.3s ease, transform 0.3s ease;
  animation: slideUp 0.3s ease-out;
  /* <-- ФИКС: Учитываем bottom safe area */
  bottom: max(20px, var(--tg-content-safe-area-inset-bottom, 0px));
}

@keyframes slideUp {
  from { transform: translateX(-50%) translateY(100%); opacity: 0; }
  to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

.loading {
  text-align: center;
  color: #94a3b8;
  font-size: 16px;
  margin: 32px 0;
  padding: 16px;
  width: 100%;
}

.error {
  color: #ef4444;
  text-align: center;
  padding: 20px;
  background: #1e293b;
  border-radius: var(--border-radius);
  margin: 10px;
  border-left: 4px solid #ef4444;
  width: 100%;
  box-sizing: border-box;
}

/* ----- MOBILE ADAPTATION (<768px) ----- */
@media (max-width: 767px) { /* <-- ИЗМЕНЕНИЕ: Поднял breakpoint для лучшей адаптации landscape */
  .global-nav {
    position: static; /* Scroll with page */
    border-bottom: none; /* No border on mobile */
    padding: 4px; /* <-- ФИКС: Ultra-compact bottom для подъёма */
    align-items: center; /* Center для подъёма */
    width: 100%; /* <-- ФИКС: Strict full width */
    left: 0;
    right: 0;
    margin: 0; /* No offsets */
  }

  .nav-content {
    padding: 0 clamp(4px, 2vw, 12px); /* Minimal боковые, full-width */
    gap: 2px; /* <-- ИЗМЕНЕНИЕ: Ещё меньше для подъёма */
    max-width: none; /* No max on mobile */
    width: 100%;
    justify-content: flex-start; /* Start для компактности */
  }

  .nav-top {
    justify-content: center; /* Center logo */
    gap: 0;
    flex-direction: column; /* Stack for centering */
    align-items: center;
    width: 100%;
  }

  .logo {
    order: -1; /* Logo first, centered */
    margin-bottom: 2px; /* <-- ИЗМЕНЕНИЕ: Ещё меньше для подъёма */
    padding: 2px;
  }

  #search-container {
    width: 100%;
    gap: 0;
  }

  .search-wrapper {
    width: 100%;
    gap: 4px; /* Small gap between input/button */
  }

  #market-search {
    border-radius: 20px; /* More rounded for mobile */
    padding: 10px 16px; /* Native-like */
    width: 100%; /* Full in wrapper */
  }

  #search-button {
    border-radius: 20px;
    padding: 10px 12px;
    font-size: 14px;
    min-width: 60px; /* Compact button */
    flex-shrink: 0;
  }

  .filters-row {
    display: flex; /* Show under search */
    gap: 8px;
    margin-top: 2px; /* <-- ИЗМЕНЕНИЕ: Ещё меньше top для подъёма */
    width: 100%;
  }

  #sort-select,
  #status-select {
    flex: 1; /* Equal width in row */
    min-width: auto;
    max-width: none;
    font-size: 14px;
    padding: 8px 20px 8px 8px; /* Compact */
    border-radius: 8px; /* Smaller radius */
    width: calc(50% - 4px); /* Even split */
  }

  .nav-categories {
    gap: 2px;
    margin: 1px 0 0; /* <-- ИЗМЕНЕНИЕ: Минимальный top для подъёма */
    padding: 0 clamp(2px, 1vw, 4px); /* Inline padding */
    width: 100%;
  }

  .category {
    padding: 6px 8px;
    font-size: 12px;
    border-radius: 8px;
    min-height: 36px; /* Still touch-friendly */
  }

  #markets-container {
    padding: clamp(4px, 2vw, 8px); /* Minimal on mobile */
    max-width: none;
    width: 100%;
    overflow-x: hidden; /* <-- ФИКС: No motion */
  }

  #markets-grid {
    grid-template-columns: 1fr;
    gap: 12px;
    width: 100%;
    overflow-x: hidden;
  }

  .market-card {
    padding: 12px;
    border-radius: 8px;
    width: 100%;
  }

  .market-img,
  .emoji-fallback {
    height: 120px;
    border-radius: 8px;
  }

  .market-title {
    -webkit-line-clamp: 1;
    font-size: 14px;
  }

  .market-options {
    max-height: 40px;
  }

  .snackbar {
    max-width: 95vw;
    padding: 12px;
    left: 50%;
    transform: translateX(-50%);
    /* Bottom safe area уже в body */
  }
}

/* Larger screens — alternative one-line layout (ИЗМЕНЕНИЕ: min-width 768px, для "stretched" ориентации) */
@media (min-width: 768px) {
  body {
    /* Reset safe areas on desktop (не нужны) */
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  .global-nav {
    padding: var(--header-padding) 0 !important; /* Original padding, но меньше */
    padding-top: var(--header-padding) !important;
    padding-left: 0 !important;
    padding-right: 0 !important;
    align-items: center; /* <-- ИЗМЕНЕНИЕ: Center для выравнивания в линию */
    position: sticky;
    top: 0;
    width: 100%;
  }

  .nav-content {
    padding: 0 16px; /* Original */
    max-width: 1440px;
    gap: 4px; /* <-- ИЗМЕНЕНИЕ: Меньше gap для подъёма */
    justify-content: flex-start; /* <-- ИЗМЕНЕНИЕ: Start для компактного выравнивания */
  }

  /* Однострочный лейаут: logo | input | button | filters */
  .nav-top {
    flex-direction: row; /* <-- ИЗМЕНЕНИЕ: Row для одной линии */
    align-items: center;
    justify-content: flex-start; /* Start для левого выравнивания */
    gap: 16px; /* Больше gap для воздуха */
    width: 100%;
    flex-wrap: nowrap; /* No wrap! */
  }

  .logo {
    flex-shrink: 0;
    margin: 0; /* No extra */
  }

  #search-container {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px; /* Маленький gap между частями */
    flex: 1; /* Занимает остаток */
    min-width: 0; /* Allow shrink if needed */
  }

  .search-wrapper {
    display: flex;
    flex-direction: row;
    gap: 8px;
    flex: 3; /* <-- ИЗМЕНЕНИЕ: Больше flex для длинного input */
    min-width: 450px; /* <-- ИЗМЕНЕНИЕ: ДЛИННЕЕ!!! */
    max-width: none;
  }

  #market-search {
    flex: 1;
    min-width: 400px; /* <-- ИЗМЕНЕНИЕ: Ещё длиннее input */
    max-width: none;
    padding: 8px 16px; /* Чуть больше для десктопа */
  }

  #search-button {
    flex: 0 0 80px; /* Фиксированная ширина, компактная */
    padding: 8px 12px;
    white-space: nowrap;
  }

  .filters-row.desktop-filters {
    display: flex !important;
    flex-direction: row;
    gap: 8px;
    width: auto;
    margin: 0; /* No margin, inline */
    flex: 1; /* Делит пространство с search */
    max-width: 40%; /* Не жрёт слишком много */
  }

  #sort-select,
  #status-select {
    flex: 1; /* Equal */
    min-width: 120px; /* Компактно */
    max-width: none;
    padding: 8px 12px; /* Стандартно */
  }

  .nav-categories {
    margin: 4px 0 0; /* Лёгкий top после nav-top */
    padding: 0;
    gap: 8px; /* Original */
    flex-wrap: wrap; /* Allow wrap if too many */
  }

  #markets-container {
    padding: 16px;
    max-width: 1440px;
  }
}