/* ============================================================================
   NEVOD — мобильно-планшетный слой поверх battle.css.
   Подключается после battle.css и ничего не ломает на десктопе (>=1181px):
   все изменения поведения спрятаны в медиа-запросах.
   ========================================================================= */

/* --- 1. Общие улучшения: тач-таргеты, фокус, плавность ------------------- */

:root {
  --sheet-peek: 54px;
  --sheet-height: 82vh;
  --tap-min: 44px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

button,
.btn,
.map-chip,
.n-chip {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
.btn:focus-visible,
.map-chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- 2. Панель: ручка и внутренние вкладки (структура для мобильного) --- */

.stat-pill .stat-label-mobile {
  display: none; /* подписи в шапке нужны только на телефоне */
}

.sheet-handle {
  display: none;
}

.sheet-tabs {
  display: none;
}

.sheet-scrim {
  position: fixed;
  inset: 0;
  background: rgba(3, 6, 12, 0.55);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
  z-index: 120;
}

body.sheet-open .sheet-scrim {
  opacity: 1;
  pointer-events: auto;
}

/* Секция «Цели» — список треков оператора (данные из window.trackObjects) */
.section-targets .target-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px 2px 6px;
}

.target-card {
  display: grid;
  grid-template-columns: 34px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-soft);
  background: var(--panel-card);
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.1s ease, background 0.15s ease;
}

.target-card:hover {
  border-color: var(--border);
  background: rgba(255, 255, 255, 0.06);
}

.target-card:active {
  transform: scale(0.99);
}

.target-card.is-fresh {
  border-color: rgba(34, 197, 94, 0.45);
}

.target-glyph {
  width: 34px;
  height: 34px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 17px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border-soft);
}

.target-main {
  min-width: 0;
}

.target-title {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.target-sub {
  margin-top: 3px;
  font-size: 10.5px;
  color: var(--dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.target-meta {
  text-align: right;
  font-size: 10.5px;
  color: var(--dim-strong);
  line-height: 1.35;
}

.target-meta .t-dist {
  font-weight: 600;
  color: var(--text);
}

.target-age-fresh {
  color: var(--green);
}

.target-age-stale {
  color: var(--warn);
}

.section-targets .empty-state {
  font-size: 11px;
}

/* --- 3. Планшет 701–1180px: компактнее и крупнее для пальца ------------- */

@media (max-width: 1180px) {
  :root {
    --sidebar-width: clamp(214px, 25vw, 262px);
    --tap-min: 42px;
  }

  .section-header {
    padding-top: 2px;
  }

  .n-chip {
    font-size: 10px;
    padding: 3px 7px;
  }

  .map-chip,
  .btn {
    min-height: 34px;
  }

  .quick-card {
    max-width: min(380px, 46vw);
  }
}

/* --- 4. Телефон <=700px: нижний лист с вкладками ------------------------- */

@media (max-width: 700px) {
  :root {
    --sheet-peek: 54px;
    --quick-card-safe-bottom: calc(var(--sheet-peek) + 14px + var(--safe-bottom));
  }

  body {
    overscroll-behavior-y: none;
  }

  /* Шапка: крупнее цифры, ничего мельче 10px */
  #header {
    min-height: 54px;
    padding: 8px 10px calc(8px) 10px;
    top: calc(6px + var(--safe-top));
    left: calc(6px + var(--safe-left));
    right: calc(6px + var(--safe-right));
    border-radius: 16px;
    gap: 8px;
  }

  .logo {
    font-size: 13px;
    letter-spacing: 0.02em;
    white-space: nowrap;
  }

  .stats {
    gap: 6px;
    font-size: 11px;
  }

  .stat-pill {
    height: 34px;
    min-width: 46px;
    width: auto;
    padding: 0 9px;
    gap: 4px;
    border-radius: 12px;
  }

  #ws-status {
    width: auto;
    min-width: 46px;
  }

  .stat-pill .stat-value {
    font-size: 13px;
  }

  .stat-pill .stat-label-mobile {
    font-size: 9px;
    color: var(--dim);
    display: block;
    line-height: 1;
  }

  .theme-toggle {
    width: 34px;
    height: 34px;
    min-width: 34px;
  }

  /* Панель-лист */
  #panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    height: var(--sheet-height);
    max-height: calc(100vh - 70px);
    width: 100%;
    padding: 0 12px calc(14px + var(--safe-bottom));
    border-radius: 22px 22px 0 0;
    border-right: none;
    border-left: none;
    transform: translateY(calc(100% - var(--sheet-peek) - var(--safe-bottom)));
    transition: transform 0.26s cubic-bezier(0.22, 0.61, 0.36, 1);
    z-index: 140;
  }

  #panel.hidden {
    transform: translateY(calc(100% - var(--sheet-peek) - var(--safe-bottom)));
  }

  #panel.open {
    transform: translateY(0);
  }

  #panel.dragging {
    transition: none;
  }

  /* Ручка листа */
  .sheet-handle {
    display: block;
    position: sticky;
    top: 0;
    z-index: 3;
    margin: 0 -12px;
    padding: 8px 14px 6px;
    background: linear-gradient(180deg, rgba(10, 14, 20, 0.98), rgba(10, 14, 20, 0.86));
    border-bottom: 1px solid var(--border-soft);
    border-radius: 22px 22px 0 0;
    touch-action: none;
    cursor: grab;
  }

  .sheet-grip {
    width: 44px;
    height: 4px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.28);
    margin: 0 auto 7px;
  }

  .sheet-handle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }

  .sheet-title {
    font-size: 12.5px;
    font-weight: 650;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--dim-strong);
  }

  .sheet-title .sheet-badge {
    display: inline-block;
    margin-left: 7px;
    padding: 1px 7px;
    border-radius: 999px;
    font-size: 10px;
    background: rgba(251, 146, 60, 0.16);
    color: var(--accent);
    border: 1px solid rgba(251, 146, 60, 0.3);
  }

  .sheet-close {
    width: 32px;
    height: 32px;
    border-radius: 10px;
    border: 1px solid var(--border-soft);
    background: rgba(255, 255, 255, 0.05);
    color: var(--dim-strong);
    font-size: 15px;
    line-height: 1;
  }

  /* Вкладки листа */
  .sheet-tabs {
    display: flex;
    gap: 6px;
    position: sticky;
    top: 52px;
    z-index: 3;
    margin: 0 -12px 6px;
    padding: 6px 12px 8px;
    background: linear-gradient(180deg, rgba(10, 14, 20, 0.96), rgba(10, 14, 20, 0.86));
    overflow-x: auto;
    scrollbar-width: none;
  }

  .sheet-tabs::-webkit-scrollbar {
    display: none;
  }

  .sheet-tab {
    flex: 0 0 auto;
    min-height: 34px;
    padding: 0 12px;
    border-radius: 999px;
    border: 1px solid var(--border-soft);
    background: rgba(255, 255, 255, 0.04);
    color: var(--dim-strong);
    font-size: 12px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }

  .sheet-tab.active {
    background: rgba(251, 146, 60, 0.16);
    border-color: rgba(251, 146, 60, 0.42);
    color: var(--text);
  }

  .sheet-tab .tab-count {
    font-size: 10px;
    padding: 0 6px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.1);
  }

  /* В листе видна только активная вкладка */
  #panel .section.tab-hidden {
    display: none !important;
  }

  #panel .section {
    max-height: none;
  }

  /* Секции внутри листа не должны быть самостоятельными карточками */
  #panel .section-events,
  #panel .section-nodes,
  #panel .section-targets,
  #panel .section-map-tools {
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 2px 0 6px;
    margin: 0;
    width: 100%;
  }

  #panel .section-map-tools {
    position: static !important;
    left: auto !important;
    right: auto !important;
    top: auto !important;
    bottom: auto !important;
    max-height: none !important;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  #panel .map-tools-card {
    width: 100%;
  }

  .map-chip,
  .btn {
    min-height: var(--tap-min);
    font-size: 12.5px;
    padding: 0 12px;
  }

  /* Карточки узлов и целей — палец должен попадать */
  .node-card,
  .target-card {
    min-height: 56px;
  }

  /* Быстрая карточка узла на телефоне — компактная, не на весь экран */
  .quick-card {
    left: calc(8px + var(--safe-left));
    right: calc(8px + var(--safe-right));
    bottom: calc(var(--sheet-peek) + 10px + var(--safe-bottom));
    max-height: 46vh;
    overflow-y: auto;
    border-radius: 16px;
  }

  .quick-card--panel {
    position: static;
    inset: auto;
    width: 100%;
    max-height: none;
    margin: 0;
  }

  /* Фильтры карты на телефоне уходят в лист — прячем отдельный блок */
  body:not(.sheet-open) .section-map-tools {
    display: none !important;
  }

  /* Подписи и модальные окна */
  .modal-box,
  #modal-box {
    width: calc(100vw - 20px);
    max-width: none;
    max-height: 82vh;
    border-radius: 18px;
  }

  .diag-grid,
  .coord-form-grid {
    grid-template-columns: 1fr;
  }

  #event-log {
    display: none;
  }
}

/* --- 5. Светлая тема: ручка и вкладки ----------------------------------- */

:root[data-theme="light"] .sheet-handle,
:root[data-theme="light"] .sheet-tabs {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(246, 249, 254, 0.9));
  border-color: rgba(15, 23, 42, 0.08);
}

:root[data-theme="light"] .sheet-grip {
  background: rgba(15, 23, 42, 0.2);
}

:root[data-theme="light"] .target-card {
  background: rgba(15, 23, 42, 0.03);
  border-color: rgba(15, 23, 42, 0.08);
}

:root[data-theme="light"] .target-glyph {
  background: rgba(15, 23, 42, 0.04);
  border-color: rgba(15, 23, 42, 0.08);
}

/* --- 6. Уважение к настройкам доступности ------------------------------- */

@media (prefers-reduced-motion: reduce) {
  #panel,
  .sheet-scrim,
  .target-card {
    transition: none !important;
  }
}

/* --- 7. Уточнения по шапке и порядку элементов в листе (телефон) --------- */

@media (max-width: 700px) {
  #header {
    flex-wrap: nowrap;
    gap: 6px;
  }

  #header .logo {
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 12.5px;
  }

  #header .stats {
    flex: 0 0 auto;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    gap: 5px;
  }

  #header .stats::-webkit-scrollbar { display: none; }

  .stat-pill {
    height: 32px;
    min-width: 0;
    padding: 0 8px;
    gap: 3px;
  }

  .stat-pill .stat-value { font-size: 12.5px; }

  .stat-pill .stat-label-mobile {
    font-size: 8.5px;
    line-height: 1.05;
  }

  .theme-toggle {
    width: 32px;
    height: 32px;
    min-width: 32px;
  }

  /* Порядок в нижнем листе: ручка -> вкладки -> карточка узла -> секции */
  #panel .sheet-handle { order: 0; }
  #panel .sheet-tabs { order: 1; }
  #panel .quick-card--panel { order: 3; }
  #panel .section { order: 4; }

  /* Карточка узла внутри листа — без собственных отступов */
  #panel .quick-card--panel {
    margin: 0 0 6px;
    padding: 10px;
    border-radius: var(--radius-md);
  }
}

/* --- 8. Финальные правки мобильного листа ------------------------------- */

@media (max-width: 700px) {
  /* Карточка узла в листе не должна занимать весь экран */
  #panel .quick-card--panel {
    max-height: 44vh;
    overflow-y: auto;
  }

  /* Чипы-фильтры чуть компактнее кнопок, но не мелче 38px */
  .map-chip {
    min-height: 38px;
    font-size: 12px;
  }

  .btn {
    min-height: var(--tap-min);
  }
}

/* --- 9. Скорость/курс цели, кнопки экспорта, звук ----------------------- */

.target-actions {
  display: flex;
  gap: 4px;
  margin-left: auto;
}

.btn.btn-xs {
  min-height: 26px;
  padding: 0 8px;
  font-size: 10.5px;
  border-radius: 9px;
}

.t-motion {
  margin-top: 3px;
  font-size: 10.5px;
  color: var(--dim-strong);
  display: flex;
  align-items: center;
  gap: 4px;
}

.course-arrow {
  display: inline-block;
  font-size: 12px;
  line-height: 1;
  color: var(--accent);
  transition: transform 0.4s ease;
}

#sound-toggle {
  font-size: 14px;
}

@media (max-width: 700px) {
  .btn.btn-xs {
    min-height: 30px;
    padding: 0 10px;
    font-size: 11.5px;
  }

  .t-motion {
    font-size: 11px;
  }
}

/* --- 10. Мобильные чипы: карточка цели с движением ---------------------- */

@media (max-width: 700px) {
  .target-card {
    grid-template-columns: 32px 1fr auto;
    gap: 9px;
  }

  .target-glyph {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }
}
