/* ============================================================
   Service STANTRAS CRM — адаптация под телефоны и планшеты.
   Подключается после styles.css и только дополняет его.

   Брейкпоинты:
     телефон  — до 767px   : нижняя навигация, таблицы -> карточки,
                              модалки на весь экран, крупные элементы
     планшет  — 768..1023px: сайдбар со значками (из styles.css),
                              таблицы с горизонтальной прокруткой
   ============================================================ */

:root {
  --mnav-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

[x-cloak] { display: none !important; }

/* Мобильная навигация существует только на телефонах */
.mnav-root { display: none; }

/* Индикатор отсутствия сети (все размеры экрана) */
.offline-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 3000;
  background: #c53030; color: #fff; font-size: 13px; text-align: center;
  padding: calc(8px + var(--safe-t)) 12px 8px;
}
.offline-bar[hidden] { display: none; }

/* ================= ПЛАНШЕТ / УЗКИЙ НОУТБУК ================= */
/* Широкие таблицы прокручиваются внутри рабочей области, а не растягивают страницу
   (на планшетах это иначе приводит к «отдалению» всей страницы). Вид таблиц не меняется. */
@media (min-width: 768px) and (max-width: 1279px) {
  .content { min-width: 0; max-width: 100%; overflow-x: auto; }
}
@media (min-width: 768px) and (max-width: 1023px) {
  .content { padding: 20px; }
  .data-table th, .data-table td { padding: 10px 8px; font-size: 13px; }
  .modal-content, .modal-large, .modal-xlarge, .modal-auto, .modal-xxlarge { max-width: 96vw; min-width: 0; }
  .modal-resizable { min-width: 0; width: 96vw; }
  .charts-grid { grid-template-columns: minmax(0, 1fr); }
  .btn, .form-group input, .form-group select, .form-group textarea { min-height: 42px; }
}

/* ================= ТЕЛЕФОН ================= */
@media (max-width: 767px) {
  html { -webkit-text-size-adjust: 100%; }
  body { overflow-x: hidden; }

  /* --- каркас --- */
  .app-layout { display: block; }
  .sidebar { display: none !important; }
  .content {
    margin-left: 0 !important;
    padding: 14px 14px calc(var(--mnav-h) + var(--safe-b) + 90px);
    min-height: 100vh; min-height: 100dvh;
    max-width: 100vw; overflow-x: hidden;
  }
  h1 { font-size: 20px; line-height: 1.25; }
  .page-header { flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
  .page-header p { font-size: 13px; }

  /* Главная кнопка раздела («+ Новая заявка») -> плавающая, под большим пальцем */
  .page-header > .btn-primary:not(.btn-small) {
    position: fixed; right: 16px; bottom: calc(var(--mnav-h) + var(--safe-b) + 14px); z-index: 900;
    height: 50px; padding: 0 20px; border-radius: 28px;
    font-size: 15px; font-weight: 600; white-space: nowrap;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
  }

  /* --- нижняя навигация --- */
  .mnav-root { display: block; }
  .mnav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 950;
    height: calc(var(--mnav-h) + var(--safe-b));
    padding-bottom: var(--safe-b);
    display: flex; align-items: stretch;
    background: #fff; border-top: 1px solid var(--gray-200);
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.06);
  }
  .mnav-item {
    flex: 1 1 0; min-width: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    color: var(--gray-500); text-decoration: none; font-size: 11px; font-weight: 500;
    -webkit-tap-highlight-color: transparent; user-select: none;
  }
  .mnav-item.active { color: var(--primary); }
  .mnav-icon { font-size: 22px; line-height: 1; }
  .mnav-item.active .mnav-icon { transform: translateY(-1px); }
  .mnav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

  /* лист «Ещё» */
  .more-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 960; }
  .more-sheet {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 970;
    background: #fff; border-radius: 18px 18px 0 0;
    max-height: calc(100vh - 40px); max-height: calc(100dvh - 40px);
    overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding-bottom: calc(12px + var(--safe-b));
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.2);
  }
  .more-anim { transition: transform 0.22s ease, opacity 0.22s ease; }
  .more-hidden { transform: translateY(40px); opacity: 0; }
  .more-shown { transform: translateY(0); opacity: 1; }
  .more-head {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 16px 18px; color: #fff; background: var(--sidebar-bg); border-radius: 18px 18px 0 0;
  }
  .more-user { display: flex; align-items: center; gap: 12px; min-width: 0; }
  .more-avatar {
    width: 40px; height: 40px; border-radius: 10px; flex: 0 0 auto;
    background: rgba(255, 255, 255, 0.12); display: flex; align-items: center; justify-content: center; font-size: 20px;
  }
  .more-avatar img { width: 28px; height: 28px; object-fit: contain; }
  .more-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .more-sub { font-size: 12px; opacity: 0.7; margin-top: 2px; }
  .more-close { background: none; border: none; color: #fff; font-size: 28px; line-height: 1; width: 44px; height: 44px; cursor: pointer; }
  .more-list { padding: 8px 8px 4px; }
  .more-item {
    display: flex; align-items: center; gap: 14px; min-height: 50px; padding: 0 12px; border-radius: 10px;
    color: var(--gray-700); text-decoration: none; font-size: 16px; -webkit-tap-highlight-color: transparent;
  }
  .more-item:active { background: var(--gray-100); }
  .more-item.active { background: var(--gray-50); color: var(--primary); font-weight: 600; }
  .more-icon { font-size: 22px; width: 30px; text-align: center; }
  .more-logout { display: block; width: calc(100% - 32px); margin: 8px 16px 0; min-height: 46px; font-size: 15px; }

  /* --- элементы управления: удобно пальцем, без автозума iOS (шрифт >= 16px) --- */
  .btn { min-height: 44px; padding: 10px 16px; font-size: 15px; }
  .btn-small { min-height: 38px; padding: 6px 12px; font-size: 14px; }
  .form-group input, .form-group select, .form-group textarea,
  .search-input, .filter-select, input[type="date"], input[type="datetime-local"] {
    font-size: 16px; min-height: 44px; width: 100%;
  }
  .form-group textarea { min-height: 88px; }
  .form-grid, .form-row { grid-template-columns: 1fr; gap: 12px; }
  .filters { flex-direction: column; gap: 10px; margin-bottom: 14px; }
  .search-input { max-width: none; }

  /* --- таблицы -> карточки --- */
  .data-table, .data-table tbody, .data-table tfoot, .data-table tr, .data-table td { display: block; width: 100%; }
  .data-table { background: transparent; box-shadow: none; border-radius: 0; }
  .data-table thead { display: none; }
  .data-table tbody tr {
    background: #fff; border-radius: 12px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    margin-bottom: 10px; padding: 4px 14px;
  }
  .data-table tbody tr:hover td { background: transparent; }
  .data-table td {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    padding: 9px 0; min-height: 40px; border-bottom: 1px solid var(--gray-100);
    text-align: right; font-size: 14px; line-height: 1.35; word-break: break-word;
  }
  .data-table td:last-child { border-bottom: 0; }
  .data-table td[data-label]::before {
    content: attr(data-label); flex: 0 0 36%; text-align: left;
    font-size: 11px; font-weight: 600; letter-spacing: 0.03em; text-transform: uppercase; color: var(--gray-500);
  }
  .data-table td:not([data-label]) { justify-content: flex-end; }
  .data-table td:first-child[data-label] { font-weight: 600; font-size: 15px; }
  .data-table td.actions { gap: 10px; padding: 10px 0 8px; overflow: visible; }
  .data-table td.actions .btn { min-width: 46px; min-height: 42px; font-size: 17px; padding: 6px 10px; }
  .data-table td.empty-state { display: block; text-align: center; padding: 28px 10px; background: #fff; border-radius: 12px; min-height: 0; }
  .data-table td input, .data-table td select { font-size: 16px; min-height: 40px; max-width: 62%; }
  .data-table td input[type="number"] { max-width: 110px; }
  /* итоговые строки (tfoot) */
  .data-table tfoot tr {
    display: flex; justify-content: space-between; align-items: center; gap: 10px;
    padding: 6px 14px; background: #fff; border-radius: 10px; margin-bottom: 6px;
  }
  .data-table tfoot td { display: block; width: auto; border: 0; padding: 6px 0; min-height: 0; text-align: right; }
  .data-table tfoot td:empty { display: none; }
  .data-table tfoot td[colspan] { flex: 1; text-align: left !important; }

  /* --- модальные окна: во весь экран, шапка и кнопки закреплены --- */
  .modal { padding: 0; align-items: stretch; justify-content: stretch; }
  .modal-content, .modal-large, .modal-xlarge, .modal-auto, .modal-xxlarge, .modal-resizable {
    width: 100% !important; max-width: none !important; min-width: 0 !important;
    height: 100%; height: 100dvh; max-height: none !important; min-height: 0 !important;
    border-radius: 0; resize: none !important;
    display: flex; flex-direction: column;
  }
  .modal-content > form { display: flex; flex-direction: column; flex: 1 0 auto; }
  .modal-content > form > .modal-footer, .modal-content > .modal-footer { margin-top: auto; }
  .modal-content > .modal-header, .modal-content > .modal-tabs, .modal-content > .modal-footer, .modal-content > form > .modal-footer { flex-shrink: 0; }
  .modal-header { padding: 12px 14px; padding-top: calc(12px + var(--safe-t)); gap: 10px; }
  .modal-header h2 { font-size: 17px; line-height: 1.3; }
  .modal-close { width: 44px; height: 44px; font-size: 30px; flex: 0 0 auto; margin: -8px -10px -8px 0; }
  .modal-content > form, .tab-content { padding: 14px; }
  .modal-content > form > .form-group { margin-bottom: 12px; }
  .modal-footer {
    padding: 10px 12px calc(10px + var(--safe-b)); gap: 8px; flex-wrap: wrap;
  }
  .modal-footer > div { display: flex; gap: 8px; flex: 1 1 auto; }
  .modal-footer .btn { flex: 1 1 auto; min-height: 46px; padding: 10px 8px; font-size: 15px; white-space: nowrap; }
  .modal-tabs { padding: 0 6px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tab-btn { flex: 1 0 auto; padding: 12px 10px; font-size: 14px; white-space: nowrap; }
  .parts-section { padding: 10px; }
  .parts-header { flex-wrap: wrap; gap: 8px; }
  .parts-header h3 { font-size: 15px; }
  .parts-search-results { max-height: none; }
  .perm-tree { max-height: none; }
  .perm-tree input[type="checkbox"], .perm-tree input[type="radio"], .form-group input[type="checkbox"], .form-group input[type="radio"] {
    width: 20px; height: 20px; min-height: 0; flex: 0 0 auto; margin: 0 8px 0 0;
  }
  .perm-group-header, .perm-node-label { min-height: 40px; }
  .perm-node-label { font-size: 14px; }
  .perm-node-label.perm-child { font-size: 13px; }

  /* --- панель управления --- */
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 18px; }
  .stat-card { padding: 12px; gap: 10px; min-width: 0; }
  .stat-icon { width: 36px; height: 36px; font-size: 17px; border-radius: 8px; flex: 0 0 auto; }
  .stat-info { min-width: 0; }
  .stat-value { font-size: 17px; line-height: 1.2; overflow-wrap: anywhere; }
  .stat-label { font-size: 12px; line-height: 1.25; }
  .dashboard-columns { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .recent-item { gap: 10px; }
  .recent-item-info { min-width: 0; }
  .recent-item-title, .recent-item-subtitle { overflow: hidden; text-overflow: ellipsis; }

  /* --- отчёты --- */
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 16px; }
  .kpi-card { padding: 12px 14px; min-width: 0; }
  .kpi-value { font-size: 20px; overflow: hidden; text-overflow: ellipsis; }
  .charts-grid { grid-template-columns: minmax(0, 1fr); gap: 14px; margin-bottom: 16px; }
  .chart-card { padding: 14px 12px; min-width: 0; overflow: hidden; }
  .chart-card canvas { max-width: 100% !important; }
  .date-filter { gap: 6px; width: 100%; }
  .date-filter .btn { flex: 1 1 auto; padding: 8px 6px; }
  .date-filter input[type="date"] { flex: 1 1 42%; width: auto; min-width: 0; }
  .date-filter-sep { flex: 0 0 auto; }
  .reports-grid { grid-template-columns: minmax(0, 1fr); }

  /* --- прочие разделы --- */
  .templates-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .template-card { padding: 18px 14px; }
  .company-form-container { padding: 16px 14px; }
  .company-form-container form { padding: 0; }
  .color-row { flex-wrap: wrap; }
  .empty-state { padding: 28px 12px; }

  /* --- вход --- */
  .login-screen { padding: 16px; align-items: flex-start; padding-top: max(40px, 12vh); }
  .login-box { padding: 26px 20px; }

  /* --- уведомления --- */
  .toast { top: calc(12px + var(--safe-t)); left: 12px; right: 12px; max-width: none; text-align: center; font-size: 14px; }
}

/* Очень узкие телефоны (iPhone SE, 320-360px) */
@media (max-width: 360px) {
  .mnav-label { font-size: 10px; }
  .data-table td[data-label]::before { flex-basis: 40%; }
  .stat-value { font-size: 16px; }
}

/* Печать: мобильную навигацию не печатать */
@media print {
  .mnav-root, .offline-bar, .page-header > .btn-primary { display: none !important; }
}
