/* ============================================================================
   Мобильная версия и точечные фиксы переполнения (задача 46).
   Подключается после assets/app.css; десктопные правила не переопределяет,
   кроме безопасных общих ограничений ширины и исправления поля пароля.
   ========================================================================== */

/* A. Общее переполнение и переносы */
*, *::before, *::after { box-sizing: border-box; }
html, body { max-width: 100%; overflow-x: hidden; }
@supports (overflow: clip) {
  html, body { overflow-x: clip; }
}
html { width: 100%; }
body { width: 100%; }
img, svg, video { max-width: 100%; }

.app-main, .site-header, .app-footer, .header-inner,
.brand, .brand-text, .main-nav, .app-session, .session-user,
.catalog-layout, .catalog-filters, .catalog-results, .catalog-search, .search-field,
.page-toolbar, .toolbar-actions, .catalog-toolbar, .catalog-row, .catalog-main, .catalog-side,
.part-grid, .gallery, .part-info, .part-title-actions, .price-box,
.edit-page, .form-row-2, .form-row-3, .repeat-row, .field, .form-actions, .form-actions .btn-group,
.photo-grid, .photo-card, .table-wrap, .app-footer .container, .modal, .modal-body,
.auth-page, .auth-wrap, .auth-card { min-width: 0; }

.container > *,
.header-inner > *,
.page-toolbar > *,
.toolbar-actions > *,
.catalog-search > *,
.filters-toggle-row > *,
.catalog-toolbar > *,
.catalog-layout > *,
.catalog-row > *,
.part-grid > *,
.part-title-actions > *,
.part-actions > *,
.form-row-2 > *,
.form-row-3 > *,
.repeat-row > *,
.form-actions > *,
.app-footer .container > * {
  min-width: 0;
  max-width: 100%;
}

input, select, textarea { max-width: 100%; }
table { max-width: 100%; }
pre, code, .mono { overflow-wrap: anywhere; }

.field > input,
.field > select,
.field > textarea,
.form-input,
.filter-select,
.sort-label select,
.role-select,
.catalog-search input,
.auth-form input { width: 100%; max-width: 100%; box-sizing: border-box; }

.catalog-title, .catalog-name-en, .catalog-meta, .catalog-models, .catalog-oem,
.catalog-sub, .part-title, .part-title-en, .oem-code, .oem-alts, .alias-chip,
.breadcrumbs, .breadcrumbs *, .source-line, .notes-box, .form-help, .form-alert,
.edit-section, .field, .data-table th, .data-table td, .audit-diff,
.auth-sub, .auth-hint, .auth-footer, .session-user, .session-role, .session-name,
.app-title, .app-sub, .toast, .modal, .part-view, .card-block {
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Заголовки и подписи не рвём посреди слова: «anywhere» переносит даже короткие
   слова при нехватке места (визуально «Применимост-ь»), поэтому здесь — только
   break-word для действительно длинных слов, а обычный перенос — по пробелам. */
.card-block h1, .card-block h2, .card-block h3, .card-block h4, .card-block .card-title,
.edit-section h1, .edit-section h2, .edit-section h3, .edit-section h4,
.part-view h1, .part-view h2, .part-view h3, .part-view h4,
.auth-card h1, .auth-card h2, .form-section-title, .card-head,
h2, h3, h4 {
  overflow-wrap: break-word;
  word-break: normal;
  hyphens: auto;
}

/* C. Вход: карточка по ширине экрана, аккуратный глаз */
.auth-card {
  width: 100%;
  max-width: 420px;
  min-width: 0;
  box-sizing: border-box;
}
.auth-wrap { width: 100%; max-width: 100%; min-width: 0; }
.auth-footer { flex-wrap: wrap; }
.password-field { position: relative; display: block; }
.password-field input {
  width: 100%;
  max-width: 100%;
  padding-right: 44px;
  box-sizing: border-box;
}
.password-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  line-height: 1;
  z-index: 2;
}
.password-toggle:hover { background: var(--surface-2); color: var(--accent); }
.password-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* B. Мобильное меню */
.mobile-menu-toggle { display: none; }
.session-login { display: none; }
@media (max-width: 760px) {
  .site-header.mobile-menu-open .session-login { display: inline; }
}

.mobile-menu-backdrop {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(16, 22, 28, 0.42);
}
.mobile-menu-backdrop[hidden] { display: none !important; }

/* D. Мобильные фильтры каталога */
.filters-mobile-actions { display: none; }
.filters-backdrop { display: none; }
#mobileFilterBadge { margin-left: 4px; }
@media (max-width: 980px) {
  .catalog-filters { display: none; }
  .catalog-filters.is-open {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    z-index: 90;
    max-height: 84vh;
    max-height: 84dvh;
    overflow-y: auto;
    margin: 0;
    padding: 16px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    border-radius: 16px 16px 0 0;
    border: 1px solid var(--border);
    border-bottom: 0;
    background: var(--card);
    box-shadow: 0 -12px 40px rgba(16, 22, 28, 0.28);
  }
  .catalog-filters .filters-head {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--card);
    padding-bottom: 8px;
  }
  .filters-mobile-actions {
    display: flex;
    gap: 8px;
    position: sticky;
    bottom: 0;
    z-index: 2;
    margin-top: 12px;
    padding: 10px 0 2px;
    border-top: 1px solid var(--border-soft);
    background: var(--card);
  }
  .filters-mobile-actions .btn { flex: 1 1 0; min-height: 44px; justify-content: center; }
  .filters-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 85;
    background: rgba(16, 22, 28, 0.42);
  }
  body.filters-open { overflow: hidden; }
}

/* Подсказки к горизонтальному скроллу таблиц */
.mobile-scroll-hint {
  display: none;
  margin: 8px 0 0;
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--muted);
}

/* Grid-треки на мобильном не раздуваются min-content'ом длинных полей/карточек */
@media (max-width: 980px) {
  .catalog-layout,
  .part-grid { grid-template-columns: minmax(0, 1fr); }
  .repeat-row--compat { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .form-row-2,
  .form-row-3,
  .form-grid-2,
  .repeat-row,
  .repeat-row--compat,
  .repeat-row--analog { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 520px) {
  .price-box { grid-template-columns: minmax(0, 1fr); }
}

/* --------------------------------------------------------------------------
   Мобильная навигация и компактная шапка ≤760px
   -------------------------------------------------------------------------- */
@media (max-width: 760px) {
  .container { padding-left: 16px; padding-right: 16px; }
  .site-header .header-inner {
    gap: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
    min-height: 0;
    flex-wrap: wrap;
  }
  .site-header .brand { min-width: 0; flex: 0 1 auto; }
  .site-header .brand-text { min-width: 0; }
  .site-header .brand-text strong { font-size: 16px; line-height: 1.15; }
  .site-header .main-nav,
  .site-header .app-session { display: none; }
  .mobile-menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    flex: 0 0 auto;
    min-height: 44px;
    padding: 0 10px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: var(--card);
    color: var(--muted);
    font: inherit;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
  }
  .mobile-menu-toggle:hover { border-color: var(--accent); color: var(--accent); }
  .mobile-menu-toggle svg { width: 18px; height: 18px; flex: 0 0 auto; }
  .site-header .theme-toggle { width: 44px; height: 44px; }
  .site-header.mobile-menu-open .main-nav,
  .site-header.mobile-menu-open .app-session {
    display: flex;
    order: 4;
    width: 100%;
    margin: 0;
  }
  .site-header.mobile-menu-open .main-nav {
    flex-direction: column;
    align-items: stretch;
    gap: 2px;
    border-top: 1px solid var(--border-soft);
    padding-top: 8px;
  }
  .site-header.mobile-menu-open .main-nav a {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    font-size: 15px;
    border-radius: 8px;
  }
  .site-header.mobile-menu-open .app-session {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-top: 8px;
    padding-top: 10px;
    border-top: 1px solid var(--border-soft);
  }
  .site-header.mobile-menu-open .session-user { display: flex; flex-direction: column; gap: 2px; }
  .site-header.mobile-menu-open .session-name { display: none; }
  .site-header.mobile-menu-open .session-role { font-size: 14px; }
  .site-header.mobile-menu-open .session-login { display: inline; }
  .site-header.mobile-menu-open #logoutBtn { width: 100%; min-height: 44px; justify-content: center; }
  body.mobile-menu-open { overflow: hidden; }

  /* C. Вход на телефоне */
  .auth-page { min-height: 100dvh; }
  .auth-top { padding: 12px 0; }
  .auth-top .header-inner { flex-wrap: nowrap; gap: 8px; }
  .auth-brand { display: none; }
  .auth-wrap { padding: 8px 16px 28px; }
  .auth-card { padding: 22px 16px; border-radius: 12px; }
  .auth-title { font-size: 22px; }
  .auth-sub { font-size: 14px; margin-bottom: 18px; }
  .auth-hint { font-size: 14px; }
  .auth-footer { flex-direction: column; align-items: flex-start; gap: 4px; }
  .auth-form .field input { min-height: 44px; font-size: 16px; }
  .auth-form .btn-lg { min-height: 44px; }

  /* Общие тап-зоны и читаемость */
  .app-main { font-size: 14px; }
  .btn, .btn-sm, .page-btn, .chip, .filter-select, .role-select,
  .sort-label select, .field > input, .field > select, .field > textarea,
  .form-input, .linkbtn { min-height: 44px; }
  .btn-sm { height: auto; }
  .theme-toggle { width: 44px; height: 44px; }
  .page-btn { min-width: 44px; height: 44px; }
  .filter-select, .sort-label select, .role-select,
  .field > input, .field > select, .field > textarea,
  .form-input, .auth-form input { font-size: 16px; }

  /* D. Каталог */
  .app-main { padding-top: 16px; }
  .page-toolbar { align-items: stretch; }
  .page-toolbar .toolbar-actions { width: 100%; }
  .page-toolbar .toolbar-actions .btn { width: 100%; min-height: 44px; justify-content: center; }
  .catalog-toolbar { flex-direction: column; align-items: stretch; gap: 8px; }
  .catalog-sort, .sort-label, .sort-label select { width: 100%; max-width: 100%; }
  .catalog-sort { display: flex; }
  .catalog-count { font-size: 14px; }
  .catalog-search { width: 100%; }
  .catalog-search .btn { min-height: 44px; }
  .filters-toggle-row { gap: 8px; align-items: center; }
  .filters-toggle-row #filtersToggle { min-height: 44px; flex: 0 0 auto; }
  .filters-toggle-row .muted { flex: 1 1 auto; min-width: 0; font-size: 13px; }
  .filter-badge { min-width: 22px; height: 22px; font-size: 12px; }
  .catalog-row {
    grid-template-columns: 64px minmax(0, 1fr);
    gap: 10px;
    padding: 12px;
  }
  .catalog-photo { width: 64px; height: 64px; }
  .catalog-title { font-size: 15px; line-height: 1.3; }
  .catalog-title-row { align-items: flex-start; gap: 6px; }
  .catalog-name-en, .catalog-meta, .catalog-models,
  .catalog-stock, .catalog-price.is-missing,
  .part-meta-line, .part-gallery-note, .filters-note { font-size: 14px; }
  .catalog-oem { max-width: 100%; }
  .catalog-side {
    grid-column: 1 / -1;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: 100%;
    min-width: 0;
    text-align: left;
    gap: 6px;
  }
  .catalog-actions { display: flex; flex-direction: column; gap: 6px; width: 100%; }
  .catalog-actions .btn {
    width: 100%;
    min-height: 44px;
    justify-content: center;
  }
  .catalog-actions .btn + .btn { margin-top: 0; }
  .pagination { align-items: stretch; }
  .pagination-buttons { justify-content: flex-start; }
  .empty-actions .btn { min-height: 44px; }

  /* E. Карточка позиции */
  .part-grid { grid-template-columns: 1fr; gap: 16px; }
  .gallery { max-width: 100%; width: 100%; }
  .part-info { width: 100%; }
  .part-title-actions { flex-direction: column; align-items: stretch; gap: 12px; }
  .part-title-actions .btn { width: 100%; min-height: 44px; justify-content: center; }
  .part-title { font-size: 22px; line-height: 1.25; }
  .part-title-en { font-size: 14px; }
  .oem-line { gap: 8px; }
  .oem-code { font-size: 15px; }
  .part-actions { flex-direction: column; align-items: stretch; gap: 8px; }
  .part-actions .btn { width: 100%; min-height: 44px; justify-content: center; }
  .price-box { grid-template-columns: 1fr; }
  .part-view .data-table { min-width: 620px; }
  .part-view .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .mobile-scroll-hint { display: block; }
  .gallery-thumbs { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .photo-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .part-breadcrumbs { font-size: 13px; }
  .part-breadcrumbs .current { min-width: 0; }

  /* F. Редактор: sticky-панель сохранения */
  .edit-page { padding-bottom: 180px; }
  .edit-section { padding: 14px; }
  .page-toolbar .toolbar-actions .btn { width: 100%; }
  .field label, .repeat-row .field label { font-size: 14px; }
  .field > input, .field > select, .field > textarea, .form-input { min-height: 44px; }
  .repeat-row { gap: 8px; padding: 10px; }
  .repeat-row .remove-row { width: 100%; min-height: 44px; justify-self: stretch; }
  .repeat-row--alias .remove-row { justify-self: stretch; }
  .photo-card__caption { min-height: 44px; font-size: 16px; }
  .photo-card__tools .btn { min-height: 44px; }
  .upload-zone { padding: 16px 12px; }
  .form-actions {
    position: sticky;
    bottom: 0;
    z-index: 20;
    margin: 16px 0 0;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    border-radius: 12px;
    box-shadow: 0 -6px 24px rgba(16, 22, 28, 0.18);
  }
  .form-actions .btn-group {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 8px;
  }
  .form-actions .btn-group .btn,
  .form-actions .btn-danger { width: 100%; min-height: 44px; justify-content: center; }

  /* G. Пользователи и аудит: скролл таблиц с подсказкой */
  .admin-narrow { max-width: 100%; }
  .users-toolbar { align-items: stretch; gap: 8px; }
  .users-toolbar .btn { width: 100%; min-height: 44px; }
  .users-table { min-width: 720px; }
  .audit-table { min-width: 760px; }
  .role-select { min-height: 44px; }
  .td-actions .btn { min-height: 44px; }
  .modal { width: 100%; max-width: 100%; max-height: calc(100dvh - 24px); overflow-y: auto; }
  .modal-backdrop { padding: 12px; }
  .photo-modal-box { max-width: 100%; max-height: calc(100dvh - 24px); }
  .photo-modal-img { max-width: 100%; max-height: calc(100dvh - 120px); }
  .modal-close, .photo-modal-close, .toast-close {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
  }
  .gallery-thumb { min-height: 44px; }
  .modal .field > input,
  .modal .field > select { font-size: 16px; }

  /* Скролл-обёртки */
  .table-wrap { -webkit-overflow-scrolling: touch; }
}

/* Очень узкие экраны 360px: шапка без переносов и лишних отступов */
@media (max-width: 400px) {
  .site-header .header-inner { gap: 8px; }
  .site-header .brand-text strong { font-size: 15px; }
  .site-header .brand { gap: 9px; }
  .mobile-menu-toggle { gap: 3px; padding: 0 8px; font-size: 13px; }
  .app-title, .page-head h1 { font-size: 20px; }
  .auth-card { padding: 20px 14px; }
  .catalog-row { padding: 10px; }
  .modal-backdrop { padding: 10px; }
  .part-title { font-size: 20px; }
}
