/* VISH — доп. мобильная оптимизация (десктоп не трогаем) */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
}

/* min-height 100dvh на #app-page давал полосу снизу и сдвиг вверх на iOS — только для логина */

#login-page .login-wrap {
  min-height: var(--vish-vh, 100dvh);
  min-height: -webkit-fill-available;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}

#login-page .login-card {
  padding: 28px 22px;
  max-width: min(380px, 100%);
}

.app-toast {
  bottom: max(24px, env(safe-area-inset-bottom));
}

@media (max-width: 768px) {
  /* ── Мобильная оболочка VISH (все вкладки + чат) ── */

  /* Шапка: safe-area сверху без «съедания» иконок */
  .nav {
    box-sizing: border-box;
    height: calc(52px + env(safe-area-inset-top, 0px));
    min-height: calc(52px + env(safe-area-inset-top, 0px));
    padding-top: env(safe-area-inset-top, 0px);
    padding-left: 10px;
    padding-right: 10px;
    align-items: center;
  }

  .nav-links {
    top: calc(52px + env(safe-area-inset-top, 0px));
  }

  .nav-burger,
  .nav .theme-btn,
  .nav .btn-logout {
    flex-shrink: 0;
  }

  html:has(body.vish-app) {
    height: var(--vish-vh, 100%);
    min-height: var(--vish-vh, 100vh);
    overflow: hidden;
    background: var(--bg);
  }

  body.vish-app {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 100vw;
    height: var(--vish-vh, 100%);
    min-height: var(--vish-vh, 100vh);
    min-height: -webkit-fill-available;
    max-height: none;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: manipulation;
    background: var(--bg);
  }

  body.vish-app #app-page {
    display: flex !important;
    flex-direction: column;
    flex: 1 1 0;
    min-height: 0;
    width: 100%;
    max-width: 100vw;
    overflow: hidden;
    box-sizing: border-box;
    background: var(--bg);
  }

  /* Иконка на главном экране: реальная высота окна, без чёрной полосы снизу */
  html.vish-standalone:has(body.vish-app) {
    height: var(--vish-vh, 100vh);
    min-height: var(--vish-vh, 100vh);
  }

  html.vish-standalone body.vish-app {
    height: var(--vish-vh, 100vh);
    min-height: var(--vish-vh, 100vh);
    bottom: 0;
  }

  body.vish-app .section.active {
    flex: 1 1 0;
    min-height: 0;
    display: flex !important;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    width: 100%;
    margin: 0 auto;
    animation: none;
    padding: 12px 10px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }

  /* Чат на весь экран под шапкой */
  body.vish-app #section-chat.active {
    padding: 0 !important;
    overflow: hidden;
  }

  body.vish-app #section-chat > .page-header {
    display: none !important;
    height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden;
    border: 0;
  }

  body.vish-app #section-chat-host,
  body.vish-app .messenger-shell--page {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  body.vish-app .tg-app {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    overflow: hidden;
    width: 100%;
  }

  body.vish-app .tg-app:not(.mobile-room-open) .tg-sidebar {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  body.vish-app .tg-app.mobile-room-open .tg-pane {
    flex: 1 1 0;
    min-height: 0;
    display: flex !important;
    flex-direction: column;
    overflow: hidden;
    width: 100%;
  }

  body.vish-app .tg-topbar {
    flex-shrink: 0;
  }

  body.vish-app .tg-messages {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  body.vish-app .tg-compose {
    flex-shrink: 0;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--tg-border);
    background: var(--tg-compose);
  }

  body.vish-app .tg-compose-inner {
    align-items: center;
    border-radius: 20px;
    padding: 6px 8px;
  }

  body.vish-app .tg-compose-inner.is-multiline {
    align-items: flex-end;
    border-radius: 16px;
  }

  body.vish-app .tg-input {
    font-size: 16px;
    min-height: 22px;
    max-height: 200px;
    padding: 8px 6px;
  }

  /* ── Мессенджер: список чатов и поиск на всю ширину ── */
  .messenger-shell .tg-app {
    width: 100%;
    max-width: 100%;
  }

  .messenger-shell .tg-sidebar,
  .messenger-shell--drawer .tg-sidebar,
  body.vish-app .tg-app:not(.mobile-room-open) .tg-sidebar {
    flex: 1 1 auto !important;
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
  }

  .messenger-shell .tg-sidebar-top {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    box-sizing: border-box;
    padding: 10px 12px;
  }

  .messenger-shell .tg-search-wrap {
    flex: 1 1 0;
    min-width: 0;
    width: auto;
  }

  .messenger-shell .tg-search {
    width: 100%;
    box-sizing: border-box;
    font-size: 16px;
    padding: 10px 14px 10px 40px;
    min-height: 44px;
  }

  .messenger-shell .tg-icon-btn {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
  }

  body.vish-app #section-chat.active .tg-splitter {
    display: none !important;
  }

  /* Виджет чата (FAB): на весь экран, без узкой колонки справа */
  :root {
    --chat-drawer-w: 100%;
  }

  body.chat-drawer-open #app-page {
    margin-right: 0 !important;
    margin-left: 0 !important;
  }

  .chat-drawer-panel {
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    height: var(--vish-vh, 100vh) !important;
    max-height: var(--vish-vh, 100vh);
    padding-top: env(safe-area-inset-top, 0px);
    box-sizing: border-box;
    border-left: none;
  }

  .chat-drawer-bar {
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    min-height: 48px;
    box-sizing: border-box;
  }

  .chat-drawer-title {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 16px;
  }

  .chat-drawer-actions {
    flex: 0 0 auto;
    width: auto !important;
    display: flex;
    gap: 8px;
    justify-content: flex-end;
  }

  .chat-drawer-btn {
    flex: 0 0 auto !important;
    max-width: none !important;
    padding: 8px 12px;
    font-size: 13px;
  }

  .messenger-shell--drawer {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    width: 100%;
  }

  .messenger-shell--drawer .tg-app {
    flex: 1 1 0;
    min-height: 0;
    width: 100%;
    height: auto;
  }

  .messenger-shell--drawer .tg-sidebar-top {
    padding: 10px 12px;
  }

  .messenger-shell--drawer .tg-compose {
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  }

  .messenger-shell--drawer .tg-input-wrap {
    flex: 1 1 0 !important;
    min-width: 0 !important;
    width: auto !important;
    max-width: none !important;
  }

  .messenger-shell--drawer .tg-compose-inner {
    align-items: center;
    width: 100%;
  }

  #section-products .page-header {
    margin-bottom: 12px;
    padding-top: 2px;
  }

  #section-products .page-title {
    font-size: 18px;
    margin-bottom: 2px;
  }

  #section-products .page-sub {
    font-size: 11px;
    line-height: 1.35;
  }

  #section-products .shop-grid {
    margin-bottom: 12px;
  }

  /* Потребность · Приоритет — карточки на телефоне */
  #section-demand .page-header {
    margin-bottom: 10px;
  }

  #section-demand .page-title {
    font-size: 20px;
  }

  #section-demand .page-sub {
    font-size: 12px;
    line-height: 1.35;
  }

  #section-demand .priority-panel {
    margin: 0;
    border-radius: 12px;
    border: none;
    background: transparent;
  }

  #section-demand .pri-toolbar {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 0 0 10px;
    border-bottom: none;
    background: transparent;
  }

  #section-demand .pri-toolbar-filters {
    width: 100%;
    gap: 6px;
  }

  #section-demand .pri-select,
  #section-demand .pri-sort-mobile {
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
    min-height: 40px;
    font-size: 16px;
    display: block;
  }

  #section-demand .pri-abc-chips {
    width: 100%;
    justify-content: space-between;
  }

  #section-demand .pri-chip {
    flex: 1;
    min-height: 40px;
    font-size: 12px;
  }

  #section-demand .pri-search-wrap {
    width: 100%;
    min-height: 44px;
    height: auto;
    padding: 8px 10px;
  }

  #section-demand .pri-search-input {
    font-size: 16px;
  }

  #section-demand .pri-search-clear {
    min-width: 36px;
    min-height: 36px;
  }

  #section-demand .pri-toolbar-actions {
    width: 100%;
    gap: 8px;
  }

  #section-demand .pri-btn {
    flex: 1;
    min-height: 44px;
    font-size: 14px;
  }

  #section-demand .priority-summary {
    padding: 8px 10px;
    border-radius: 10px;
    margin-bottom: 8px;
    gap: 4px 10px;
    font-size: 11px;
  }

  #section-demand .priority-table-wrap {
    display: none !important;
  }

  #section-demand .pri-mobile-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 0 0 8px;
  }

  #section-demand .pri-m-card {
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 12px;
    -webkit-tap-highlight-color: transparent;
  }

  #section-demand .pri-m-card-click {
    cursor: pointer;
  }

  #section-demand .pri-m-card-click:active {
    transform: scale(0.995);
    background: rgba(124, 111, 247, 0.08);
  }

  #section-demand .pri-m-head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
  }

  #section-demand .pri-m-rank {
    font-size: 12px;
    font-weight: 700;
    color: var(--text2);
    min-width: 28px;
  }

  #section-demand .pri-m-q {
    margin-left: auto;
    font-size: 13px;
    font-weight: 800;
    color: #c4b5fd;
  }

  #section-demand .pri-m-q-hot {
    color: #e9d5ff;
  }

  #section-demand .pri-m-body {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
  }

  #section-demand .pri-m-photo {
    width: 52px;
    height: 52px;
    border-radius: 10px;
    object-fit: cover;
    flex-shrink: 0;
    border: 1px solid var(--border);
    background: var(--bg2);
  }

  #section-demand .pri-m-photo-ph {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
  }

  #section-demand .pri-m-info {
    flex: 1;
    min-width: 0;
  }

  #section-demand .pri-m-name {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  #section-demand .pri-m-art {
    font-size: 10px;
    font-family: ui-monospace, monospace;
    margin-top: 3px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #section-demand .pri-m-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
  }

  #section-demand .pri-m-stat {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 8px;
    text-align: center;
  }

  #section-demand .pri-m-stat span {
    display: block;
    font-size: 9px;
    color: var(--text3);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 2px;
  }

  #section-demand .pri-m-stat b {
    font-size: 12px;
    font-variant-numeric: tabular-nums;
  }

  #section-demand .pri-m-empty {
    text-align: center;
    padding: 28px 16px;
    color: var(--text3);
    font-size: 13px;
  }

  #section-demand .priority-legend-details {
    border-top: none;
    padding: 4px 0 0;
    font-size: 11px;
  }

  .form-input,
  .search-inp,
  .sales-select,
  .tg-settings-field,
  .prompt-input {
    font-size: 16px;
  }

  .nav-burger,
  .theme-btn,
  .page-btn,
  .filter-btn,
  .pdp-close,
  .chat-fab {
    min-width: 44px;
    min-height: 44px;
  }

  .filter-btn {
    min-height: 40px;
    padding: 8px 14px;
  }

  .page-btn {
    width: 44px;
    height: 44px;
  }

  .table-scroll {
    max-height: none;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  /* Товары / склад: карточки вместо широкой таблицы */
  .products-table-desktop,
  .warehouse-table-desktop {
    display: none !important;
  }

  .products-mobile-list,
  .warehouse-mobile-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 10px 12px 12px;
    max-height: none;
    overflow: visible;
  }

  .prod-mobile-card {
    background: var(--bg3);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 12px;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
    -webkit-tap-highlight-color: transparent;
  }

  .prod-mobile-card.active {
    border-color: rgba(124, 111, 247, 0.55);
    background: rgba(124, 111, 247, 0.1);
  }

  .prod-mobile-card:active {
    transform: scale(0.995);
  }

  .pmc-top {
    display: flex;
    gap: 12px;
    align-items: flex-start;
    margin-bottom: 10px;
  }

  .pmc-photo,
  .pmc-photo-ph {
    width: 56px;
    height: 56px;
    border-radius: 12px;
    flex-shrink: 0;
    object-fit: cover;
    border: 1px solid var(--border);
  }

  .pmc-photo-ph {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    background: var(--bg2);
  }

  .pmc-main {
    flex: 1;
    min-width: 0;
  }

  .pmc-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .pmc-meta {
    font-size: 11px;
    color: var(--text2);
    margin-top: 4px;
    line-height: 1.4;
  }

  .pmc-barcode {
    font-family: ui-monospace, monospace;
    font-size: 11px;
    color: var(--text3);
    margin-top: 4px;
    word-break: break-all;
  }

  .pmc-arts {
    font-size: 11px;
    color: var(--text2);
    margin-top: 6px;
    line-height: 1.45;
    word-break: break-word;
  }

  .pmc-sizes {
    font-size: 11px;
    color: var(--text3);
    margin-top: 4px;
    line-height: 1.4;
  }

  .pmc-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }

  .pmc-stats--wh {
    grid-template-columns: repeat(3, 1fr);
  }

  .pmc-stat {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 6px;
    text-align: center;
  }

  .pmc-stat--wide {
    grid-column: 1 / -1;
    text-align: left;
    padding: 8px 10px;
  }

  .pmc-l {
    display: block;
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text2);
    margin-bottom: 4px;
  }

  .pmc-v {
    font-size: 16px;
    font-weight: 700;
    color: var(--text);
    line-height: 1.1;
  }

  .pmc-extra {
    margin-top: 8px;
    font-size: 11px;
    color: var(--text2);
    line-height: 1.4;
  }

  .wh-mobile-card .pmc-extra:last-child {
    margin-bottom: 0;
  }

  #products-table-wrap,
  .table-container {
    border-radius: 12px;
  }

  .table-toolbar.sticky-toolbar {
    position: sticky;
    top: 0;
    z-index: 5;
  }

  body.product-dock-right #app-page,
  body.product-dock-left #app-page,
  body.chat-drawer-open.product-dock-right #app-page,
  body.chat-drawer-open.product-dock-left #app-page {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  .product-side-drawer {
    width: 100% !important;
    max-width: 100% !important;
  }

  .product-side-drawer.dock-right,
  .product-side-drawer.dock-left {
    border: none;
  }

  .pdp-hdr {
    padding-top: max(10px, env(safe-area-inset-top));
  }

  .pdp-body {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .users-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .user-card {
    padding: 14px;
  }

  .upload-grid {
    grid-template-columns: 1fr !important;
  }

  .upload-card {
    padding: 16px;
  }

  .sales-select {
    width: 100%;
    margin-bottom: 12px;
  }

  .admin-layout {
    flex-direction: column;
  }

  .admin-panel {
    width: 100% !important;
  }

  .manage-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    padding-bottom: 4px;
  }

  .manage-tab-btn {
    flex-shrink: 0;
    min-height: 44px;
  }

  .chat-access-layout {
    grid-template-columns: 1fr !important;
  }

  .chat-access-users {
    grid-template-columns: 1fr;
  }

  .soon-page {
    padding: 40px 20px;
  }

  .modal-overlay {
    padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
    align-items: flex-end;
  }

  #prod-modal .modal {
    max-height: 92dvh;
    border-radius: 16px 16px 0 0;
  }

  .tg-compose-input {
    font-size: 16px;
  }

  .nav-admin-link {
    font-size: 11px;
    padding: 6px 8px;
  }
}

@media (min-width: 769px) {
  .products-mobile-list,
  .warehouse-mobile-list {
    display: none !important;
  }
}

@media (max-width: 480px) {
  #section-demand .pri-m-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  #section-demand .pri-toolbar-filters .pri-select {
    flex: 1 1 100%;
  }

  .pmc-stats,
  .pmc-stats--wh {
    grid-template-columns: repeat(2, 1fr);
  }
  .toolbar-title {
    font-size: 13px;
  }

  .filter-group {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .pagination .page-btns {
    flex-wrap: wrap;
    justify-content: center;
  }

  .wh-1c-cell,
  .wh-1c-th {
    min-width: 72px;
  }
}

@media (hover: none) and (pointer: coarse) {
  tbody tr:hover {
    background: inherit;
  }

  .shop-card:hover {
    transform: none;
  }

  tbody tr:active {
    background: var(--bg3);
  }

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