/* ==========================================================
   MOBILE LAYER — подключается ПОСЛЕ style.css
   Ничего не меняет на десктопе: всё внутри media-запросов.
   ========================================================== */

/* Тач-устройства: убираем «залипающий» hover (карточки не подпрыгивают после тапа) */
@media (hover: none) {
    .modern-card:hover,
    .stat-card:hover,
    .btn-primary:hover { transform: none; }
}

/* Горизонтальный скролл для широких графиков (работает на любой ширине) */
.chart-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (max-width: 640px) {

    /* ---------- База ---------- */
    html { font-size: 100%; -webkit-text-size-adjust: 100%; }
    body { overflow-x: hidden; }
    body.nav-open { overflow: hidden; }

    /* ---------- PWA / вырез экрана ---------- */
    .topbar {
        height: calc(56px + env(safe-area-inset-top, 0px));
        padding-top: env(safe-area-inset-top, 0px);
    }
    .topbar-title {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .content { padding-bottom: calc(40px + env(safe-area-inset-bottom, 0px)); }

    .sidebar {
        width: min(84vw, 300px);
        height: 100dvh;
        padding-top: calc(14px + env(safe-area-inset-top, 0px));
        padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    }
    body.nav-collapsed .sidebar { width: min(84vw, 300px); }

    /* ---------- Тап-зоны ≥ 44px; шрифт полей 16px (iOS не зумит при фокусе) ---------- */
    .nav-item, .logout-btn { min-height: 46px; font-size: .92rem; }
    .btn { min-height: 44px; font-size: .9rem; }
    .btn-sm { min-height: 40px; font-size: .84rem; padding: 8px 12px; }
    .icon-btn { width: 40px; height: 40px; }

    .modern-form input,
    .modern-form select,
    .filter-form input,
    .filter-form select,
    .auth-card input {
        font-size: 16px;
        min-height: 46px;
    }
    .modern-form input[type="checkbox"] { min-height: 0; }

    /* ---------- Заголовок страницы ---------- */
    .page-header > .btn,
    .page-header > a.btn { width: 100%; }
    .page-header > div:last-child:not(:first-child) {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }
    .page-header > div:last-child:not(:first-child) > .btn { flex: 1 1 calc(50% - 8px); }

    /* ---------- Карточки со встроенным padding ---------- */
    .card[style*="padding:16px 24px"],
    .card[style*="padding:20px 24px"] { padding: 14px !important; }

    /* ---------- Статистика: 2 колонки, компактнее ---------- */
    .stats-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
    .stat-card,
    .cards-grid .card,
    .cards-grid > a.card {
        flex-direction: row;
        align-items: center;
        gap: 10px;
        padding: 9px 12px;
        min-height: 52px;
    }
    .stat-icon,
    .card-icon,
    .cards-grid .card .icon-circle {
        width: 34px;
        height: 34px;
        flex: 0 0 34px;
    }
    .stat-icon .icon,
    .card-icon .icon,
    .cards-grid .card .icon {
        width: 16px;
        height: 16px;
    }
    .stat-info,
    .cards-grid .card > div {
        display: flex;
        flex-direction: row;
        align-items: baseline;
        gap: 8px;
        min-width: 0;
        flex: 1 1 auto;
    }
    .stat-number,
    .cards-grid .card .stat-number,
    .cards-grid .card strong {
        font-size: 1.08rem;
        line-height: 1;
        flex: 0 0 auto;
    }
    .stat-label,
    .cards-grid .card .stat-label,
    .cards-grid .card span:not(.icon):not(.stat-number) {
        font-size: .71rem;
        line-height: 1.15;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .cards-grid { gap: 8px !important; }
    .cards-grid > .stat-card { flex: 1 1 140px; margin-bottom: 0; }

    /* ---------- "Частые гости" / компактные списки ---------- */
    .guests-grid,
    .frequent-grid,
    .mini-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }
    .guest-card,
    .frequent-card,
    .mini-card {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 10px;
        padding: 8px 12px;
        min-height: 48px;
        border-radius: 12px;
    }
    .guest-card .plate-badge,
    .frequent-card .plate-badge {
        font-size: 15px;
        padding: 3px 6px 4px;
    }
    .guest-card .guest-name,
    .frequent-card .guest-name {
        font-size: .82rem;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .guest-card .guest-count,
    .frequent-card .guest-count {
        margin-left: auto;
        font-size: .78rem;
        opacity: .8;
    }

    /* ---------- Формы фильтров (в шаблонах там inline-стили) ---------- */
    .filter-form > div[style],
    .filter-form div[style*="display:flex"] {
        display: flex !important;
        flex-direction: column;
        align-items: stretch !important;
        flex-wrap: nowrap !important;
        justify-content: flex-start !important;
        gap: 8px !important;
        width: 100%;
        margin-right: 0 !important;
    }
    .filter-form label[style] { margin-bottom: 0 !important; }
    .filter-form select[style],
    .filter-form input[style] {
        width: 100% !important;
        min-width: 0 !important;
    }
    .filter-form .inline-row {
        flex-direction: row !important;
        align-items: stretch !important;
    }
    .filter-form .inline-row > input { flex: 1 1 auto; width: auto !important; }
    .filter-form .inline-row > .btn { flex: none; }

    /* ---------- Таблицы → карточки (класс .table-cards + data-label в <td>) ---------- */
    .table-responsive.table-cards { border: 0; overflow: visible; }
    .table-cards .modern-table,
    .table-cards .modern-table tbody,
    .table-cards .modern-table tr,
    .table-cards .modern-table td { display: block; width: 100%; }
    .table-cards .modern-table { background: transparent; }
    .table-cards .modern-table thead { display: none; }
    .table-cards .modern-table tr {
        margin-bottom: 10px;
        padding: 4px 14px;
        border: 1px solid var(--border);
        border-radius: 12px;
        background: var(--surface-2);
    }
    .table-cards .modern-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 14px;
        padding: 9px 0;
        border-bottom: 1px solid var(--border);
        text-align: right;
        font-size: .86rem;
        overflow-wrap: anywhere;
    }
    .table-cards .modern-table td:last-child { border-bottom: 0; }
    .table-cards .modern-table td::before {
        content: attr(data-label);
        flex: none;
        max-width: 42%;
        color: var(--muted-2);
        font-size: .68rem;
        font-weight: 700;
        letter-spacing: .06em;
        text-transform: uppercase;
        text-align: left;
    }
    .table-cards .modern-table td:not([data-label])::before { display: none; }
    .table-cards .modern-table td:not([data-label]) { justify-content: center; text-align: center; }
    .table-cards .modern-table tbody tr:hover td { background: transparent; }

    /* Длинные значения (сообщение лога) — на всю ширину, подпись сверху */
    .table-cards .modern-table td.td-wide {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
        text-align: left;
    }
    .table-cards .modern-table td.td-wide::before { max-width: none; }

    /* Пустое состояние и строки-«продолжения» (colspan) — обычные блоки */
    .table-cards .modern-table td[colspan] { display: block; text-align: center; }

    /* Доп. строка с телом запроса прилипает к карточке выше */
    .table-cards .modern-table tr:has(+ tr.body-row) {
        margin-bottom: 0;
        border-bottom: 0;
        border-radius: 12px 12px 0 0;
    }
    .table-cards .modern-table tr.body-row {
        padding-top: 0;
        border-top: 0;
        border-radius: 0 0 12px 12px;
    }
    .table-cards .modern-table tr.body-row td[colspan] {
        text-align: left;
        padding: 9px 0 10px;
        border-top: 1px solid var(--border);
        background: transparent !important;
    }

    /* Кнопки действий: подпись не нужна, кнопки в ряд, на всю ширину */
    .table-cards .modern-table td.actions-cell {
        display: flex;
        justify-content: stretch;
        gap: 8px;
        padding-top: 12px;
    }
    .table-cards .modern-table td.actions-cell::before { display: none; }
    .table-cards .actions-cell > form,
    .table-cards .actions-cell > a,
    .table-cards .actions-cell > .wl-copy-wrap { flex: 1 1 auto; min-width: 44px; display: block !important; }
    .table-cards .actions-cell .btn { width: 100%; }
    .table-cards .actions-cell > .barrier-edit { margin-left: 0; }

    /* ---------- Сброс пароля в таблице пользователей ---------- */
    .table-cards .actions-cell > form.reset-form {
        flex: 1 1 100%;
        display: flex !important;
        gap: 8px;
    }
    .reset-form input {
        flex: 1 1 auto;
        min-width: 0;
        min-height: 40px;
        padding: 8px 12px;
        border: 1px solid var(--border);
        border-radius: 8px;
        background: rgba(8, 12, 24, .65);
        color: var(--text);
        font: inherit;
        font-size: 16px;
    }
    .reset-form .btn { flex: none; width: auto; }
    html[data-theme="light"] .reset-form input { background: #fff; }

    /* ---------- Кабинет ---------- */
    .field-row .field-value { white-space: normal; text-align: right; overflow-wrap: anywhere; }
    .checkbox-row { align-items: flex-start; gap: 12px; padding: 10px 0; }
    .checkbox-row input { flex: none; width: 20px !important; height: 20px; margin-top: 2px; }
    .callout code { word-break: break-all; }

    /* ---------- Госномер ---------- */
    .plate-badge { font-size: 17px; padding: 4px 8px 5px; }
    .plate-region { font-size: 15px; min-width: 20px; }

    /* ---------- Модалки — нижняя шторка ---------- */
    .modal-overlay { align-items: flex-end; padding: 0; }
    .modal-card {
        max-width: none;
        margin: 0;
        max-height: 90dvh;
        border-radius: 18px 18px 0 0;
        padding: 22px 18px calc(22px + env(safe-area-inset-bottom, 0px));
    }

    /* Видео — на всю ширину шторки, без лишних отступов карточки */
    .video-modal-card {
        padding: 0;
        max-height: 94dvh;
    }
    .video-modal-header { padding: 16px 18px 10px; }
    .video-stage { aspect-ratio: 4 / 3; }
    .video-modal-footer {
        padding: 12px 18px calc(14px + env(safe-area-inset-bottom, 0px));
    }
    .video-open-status { flex-basis: 100%; order: 3; }
    .video-open-form { flex: 1 1 auto; }
    .video-open-form .btn { width: 100%; }
    .video-modal-footer > .btn-secondary { flex: 1 1 auto; }

    /* ---------- Выпадашка «добавить в другой список» — шторка снизу ---------- */
    .wl-copy-dd {
        position: fixed;
        left: 12px;
        right: 12px;
        top: auto;
        bottom: calc(12px + env(safe-area-inset-bottom, 0px));
        min-width: 0;
        max-height: 60dvh;
        z-index: 400;
    }
    .wl-copy-dd button { padding: 13px 12px; white-space: normal; }

    /* ---------- График ---------- */
    .chart-scroll svg {
        width: var(--cw, 100%) !important;
        max-width: none !important;
    }

    /* ---------- Пагинация ---------- */
    .pager .pager-info { order: -1; flex: 1 1 100%; text-align: center; }

    /* ---------- Логин ---------- */
    .auth-page {
        align-items: flex-start;
        min-height: 100dvh;
        padding-top: max(12vh, calc(env(safe-area-inset-top, 0px) + 24px));
        padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    }
}

/* Совсем узкие экраны */
@media (max-width: 420px) {
    .topbar-user { display: none; }
}
@media (max-width: 380px) {
    .stats-grid { grid-template-columns: 1fr; }
    .page-header > div:last-child:not(:first-child) > .btn { flex: 1 1 100%; }
}
