/* ============================================================
   Mobile adaptation · Управленческий стек ХП
   iPhone 14/15 baseline (390×844) · Swiss Design v07.04.2026
   Breakpoint: 767px (ниже — «phone», выше — штатная десктоп-сетка).
   Все токены и цвета совпадают с Figma-моками (node 0Yx4rDyoTCb8uM6HFnSukf).
   ============================================================ */

/* Мобильная «обёртка» (топбар + таббар + бэкдроп) создаётся через mobile.js.
   На десктопе скрываем её намертво — рендерится только на <=767px. */
.mobile-topbar,
.mobile-tabbar,
.mobile-backdrop {
    display: none;
}

/* ─── Переменные для мобильной хромы ─── */
:root {
    --m-topbar-h: 56px;
    --m-tabbar-h: 72px;
    --m-drawer-w: 280px;
}

/* ============================================================
   @media (max-width: 767px) — ВСЯ мобильная логика под одним медиа-запросом.
   ============================================================ */
@media (max-width: 767px) {

    /* ────────────────────────────────────────────────────────
       1. МОБИЛЬНЫЙ ТОПБАР (фиксированный, 56px, тёмный)
       Рендерится JS'ом только на admin/dashboard.
       ──────────────────────────────────────────────────────── */
    .mobile-topbar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: var(--m-topbar-h);
        background: #121212;
        color: #FFFFFF;
        padding: 0 12px;
        z-index: 950;
        box-shadow: 0 1px 0 rgba(255,255,255,0.04);
        font-family: var(--font-mono, var(--font-main, ui-monospace, monospace));
    }
    .mobile-topbar::before {
        content: '';
        position: absolute;
        top: 0; left: 0; right: 0;
        height: 2px;
        background: #DC2626;
    }
    .mobile-topbar .m-topbar-logo {
        font-size: 14px;
        font-weight: 700;
        color: #FFFFFF;
        letter-spacing: -0.3px;
        padding: 0 8px;
    }
    .mobile-topbar .m-topbar-title {
        font-size: 11px;
        color: #A6A6A6;
        letter-spacing: 0.3px;
        flex: 1;
        text-align: left;
        padding-left: 6px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .mobile-topbar .m-burger,
    .mobile-topbar .m-close,
    .mobile-topbar .m-back {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        background: transparent;
        border: 0;
        color: #FFFFFF;
        font-size: 18px;
        font-family: inherit;
        cursor: pointer;
        border-radius: 6px;
    }
    .mobile-topbar .m-burger:active,
    .mobile-topbar .m-close:active,
    .mobile-topbar .m-back:active {
        background: #1E1E1E;
    }
    /* SVG-иконка гамбургера — три полосы через CSS */
    .mobile-topbar .m-burger .m-bars {
        display: inline-block;
        width: 18px;
        height: 12px;
        position: relative;
    }
    .mobile-topbar .m-burger .m-bars::before,
    .mobile-topbar .m-burger .m-bars::after,
    .mobile-topbar .m-burger .m-bars > span {
        content: '';
        position: absolute;
        left: 0; right: 0;
        height: 2px;
        background: #FFFFFF;
        border-radius: 1px;
    }
    .mobile-topbar .m-burger .m-bars::before { top: 0; }
    .mobile-topbar .m-burger .m-bars > span { top: 50%; transform: translateY(-50%); }
    .mobile-topbar .m-burger .m-bars::after { bottom: 0; }

    /* ────────────────────────────────────────────────────────
       2. МОБИЛЬНЫЙ BOTTOM TAB BAR (дашборд)
       5 табов: kpi · опер · прот · ком · стр.
       Цветные accent-bar'ы сверху у активного таба.
       ──────────────────────────────────────────────────────── */
    .mobile-tabbar {
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: var(--m-tabbar-h);
        background: #121212;
        z-index: 950;
        border-top: 1px solid rgba(255,255,255,0.08);
        padding: 0;
        padding-bottom: env(safe-area-inset-bottom, 0);
        font-family: var(--font-mono, var(--font-main, ui-monospace, monospace));
    }
    .mobile-tabbar .m-tab {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 4px;
        padding: 10px 4px 8px;
        background: transparent;
        border: 0;
        color: #8A8A8A;
        font-family: inherit;
        font-size: 10px;
        letter-spacing: 0.3px;
        cursor: pointer;
        position: relative;
        text-decoration: none;
        transition: color 120ms;
    }
    .mobile-tabbar .m-tab:hover { color: #BDBDBD; }
    .mobile-tabbar .m-tab .m-tab-accent {
        position: absolute;
        top: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 28px;
        height: 3px;
        border-radius: 0 0 2px 2px;
        background: transparent;
        transition: background 120ms;
    }
    .mobile-tabbar .m-tab .m-tab-dot {
        width: 6px;
        height: 6px;
        border-radius: 50%;
        background: currentColor;
        opacity: 0.5;
        transition: background 120ms, opacity 120ms;
    }
    .mobile-tabbar .m-tab .m-tab-label {
        font-size: 10px;
        font-weight: 500;
        text-transform: lowercase;
        letter-spacing: 0.3px;
    }
    /* Активный таб — цветной accent сверху + белый лейбл */
    .mobile-tabbar .m-tab.is-active {
        color: #FFFFFF;
    }
    .mobile-tabbar .m-tab.is-active .m-tab-dot {
        opacity: 1;
    }
    /* Цветовая схема табов (Swiss Design · 5 блоков) */
    .mobile-tabbar .m-tab[data-group="kpi"]    .m-tab-dot { background: #0D9488; }
    .mobile-tabbar .m-tab[data-group="oper"]   .m-tab-dot { background: #0D9488; }
    .mobile-tabbar .m-tab[data-group="prot"]   .m-tab-dot { background: #8B5CF6; }
    .mobile-tabbar .m-tab[data-group="com"]    .m-tab-dot { background: #22C55E; }
    .mobile-tabbar .m-tab[data-group="strat"]  .m-tab-dot { background: #F59E0B; }
    .mobile-tabbar .m-tab.is-active[data-group="kpi"]   .m-tab-accent { background: #0D9488; }
    .mobile-tabbar .m-tab.is-active[data-group="oper"]  .m-tab-accent { background: #0D9488; }
    .mobile-tabbar .m-tab.is-active[data-group="prot"]  .m-tab-accent { background: #8B5CF6; }
    .mobile-tabbar .m-tab.is-active[data-group="com"]   .m-tab-accent { background: #22C55E; }
    .mobile-tabbar .m-tab.is-active[data-group="strat"] .m-tab-accent { background: #F59E0B; }

    /* ────────────────────────────────────────────────────────
       3. МОБИЛЬНЫЙ DRAWER (слайдит сайдбар слева)
       На admin и dashboard — один паттерн, переиспользуем
       существующие .sidebar (admin) / .sidebar (dashboard).
       ──────────────────────────────────────────────────────── */
    .mobile-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.5);
        z-index: 980;
        opacity: 0;
        pointer-events: none;
        transition: opacity 200ms;
    }
    body.drawer-open .mobile-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    /* ────────────────────────────────────────────────────────
       4. AUTH (login / reset / reset-confirm)
       Была двухколоночная раскладка → стэк: бренд-шапка 176 + форма.
       ──────────────────────────────────────────────────────── */
    .auth-layout {
        flex-direction: column;
        min-height: 100vh;
    }
    .brand-panel {
        width: 100%;
        min-width: 0;
        min-height: 176px;
        padding: 20px 24px;
    }
    .brand-panel .brand-logo {
        font-size: 40px;
        margin-bottom: 4px;
    }
    .brand-panel .brand-name {
        font-size: 14px;
    }
    .brand-panel .brand-subtitle {
        font-size: 11px;
    }
    .brand-panel .block-indicators {
        margin-top: 16px;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 8px;
    }
    .brand-panel .block-indicator .bar {
        height: 24px;
    }
    .brand-panel .block-indicator .label {
        font-size: 11px;
    }
    .brand-panel .brand-footer {
        display: none;
    }
    /* На reset.html есть инлайновый блок «линий безопасности» — скрываем его
       на моб, оставляя только логотип и subtitle. */
    .brand-panel > div[style*="margin-top: 140px"] {
        display: none;
    }
    .form-panel {
        flex: 1;
        padding: 24px;
        align-items: flex-start;
    }
    .form-container {
        max-width: 100%;
    }
    .form-title {
        font-size: 22px;
    }
    .form-subtitle {
        font-size: 12px;
        margin-bottom: 24px;
    }
    .form-input {
        height: 44px;
        font-size: 14px;
    }
    .btn-primary {
        height: 48px;
    }

    /* ────────────────────────────────────────────────────────
       5. PORTAL (/portal)
       Топбар сжимается, модули в 1 колонку, меньше паддинги.
       ──────────────────────────────────────────────────────── */
    .portal-layout {
        min-height: 100vh;
    }
    .portal-layout > .top-bar {
        height: 56px;
        padding: 0 16px;
    }
    .portal-layout > .top-bar .top-bar-logo {
        font-size: 16px;
        margin-right: 8px;
    }
    .portal-layout > .top-bar .top-bar-label {
        display: none;
    }
    .portal-layout > .top-bar .top-bar-user {
        gap: 12px;
    }
    .portal-layout > .top-bar .top-bar-user .name {
        font-size: 10px;
        max-width: 120px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .portal-content {
        padding: 24px 16px;
    }
    .portal-content .greeting.text-h1 {
        font-size: 20px;
        line-height: 1.3;
    }
    .portal-content .role-info {
        font-size: 12px;
    }
    .portal-content .last-login {
        font-size: 10px;
        margin-bottom: 24px;
    }
    .modules-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    .module-card {
        padding: 16px;
    }
    .module-card .card-title {
        font-size: 15px;
    }
    .module-card .card-desc {
        font-size: 12px;
    }
    .admin-grid {
        grid-template-columns: 1fr 1fr;
        gap: 12px;
    }
    .portal-footer {
        padding: 16px;
        font-size: 10px;
    }

    /* ────────────────────────────────────────────────────────
       6. ADMIN (staff / org / users / prompts / rhythm / transcript)
       Стандартная .admin-layout → column + mobile-topbar.
       ──────────────────────────────────────────────────────── */
    .admin-layout {
        flex-direction: column;
        padding-top: var(--m-topbar-h);
        background: #FAFAFA;
    }
    /* Десктоп-сайдбар прячем, но оставляем в DOM: mobile.js превратит
       его в drawer через body.drawer-open. */
    .admin-layout > .sidebar {
        position: fixed;
        top: 0;
        left: -100%;
        width: var(--m-drawer-w);
        height: 100vh;
        max-width: 82vw;
        padding: 16px 12px 24px;
        z-index: 990;
        transition: left 200ms ease-out;
        overflow-y: auto;
    }
    body.drawer-open .admin-layout > .sidebar {
        left: 0;
        box-shadow: 4px 0 24px rgba(0,0,0,0.25);
    }
    .admin-layout > .sidebar .sidebar-logo {
        padding-top: 8px;
    }
    .admin-layout > .main-content {
        padding: 16px 16px 24px;
        gap: 16px;
    }
    /* Page-header компактнее */
    .admin-layout .page-header {
        gap: 12px;
    }
    .admin-layout .page-header-bar {
        width: 4px;
        height: 36px;
    }
    .admin-layout .page-header-title {
        font-size: 18px;
        letter-spacing: -0.3px;
    }
    .admin-layout .page-header-subtitle {
        font-size: 11px;
        line-height: 1.4;
    }
    /* data-section — header вертикально, внутри таблицы — горизонт-скролл */
    .admin-layout .data-section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
        padding: 12px 16px;
    }
    .admin-layout .data-section {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .admin-layout .data-table {
        font-size: 11px;
        min-width: 540px;
    }
    .admin-layout .data-table th,
    .admin-layout .data-table td {
        padding: 8px 12px;
        white-space: nowrap;
    }
    /* Staff / Org — 2 колонки в 1 (для 767px уже отработано, закрепим) */
    .admin-layout .staff-grid,
    .admin-layout .org-grid {
        grid-template-columns: 1fr;
    }
    .admin-layout .staff-aside,
    .admin-layout .org-aside {
        position: static;
    }
    /* Transcript — монопробел, уменьшенный padding */
    .admin-layout .rhythm-transcript-body {
        font-size: 11px;
        padding: 12px;
        line-height: 1.5;
        overflow-x: auto;
    }

    /* ────────────────────────────────────────────────────────
       7. DASHBOARD (/dashboard — отдельный шаблон, не extends base)
       .layout + .sidebar (fixed, 220) + .main (margin-left: 220)
       → хромой дашборд: топбар 56 + таббар 72 + content между.
       ──────────────────────────────────────────────────────── */
    body.dashboard-mobile {
        padding-bottom: var(--m-tabbar-h);
    }
    body.dashboard-mobile .layout {
        display: block;
    }
    /* Прячем десктоп-сайдбар — откроется как drawer через JS.
       ВАЖНО: `display: flex !important` нужен чтобы перекрыть inline-CSS в
       templates/dashboard/index.html, который на @media (max-width:800px)
       ставит `.sidebar { display: none }` — это скрывает весь drawer после
       слайда. Инцидент 21.04.2026: burger открывал drawer, но он был пуст. */
    body.dashboard-mobile .layout > .sidebar {
        display: flex !important;
        position: fixed;
        top: 0;
        left: -100%;
        width: var(--m-drawer-w);
        max-width: 82vw;
        height: 100vh;
        transition: left 200ms ease-out;
        z-index: 990;
        overflow-y: auto;
    }
    body.dashboard-mobile.drawer-open .layout > .sidebar {
        left: 0;
        box-shadow: 4px 0 24px rgba(0,0,0,0.25);
    }
    /* Main — прижимается к нулю слева, полный ширина */
    body.dashboard-mobile .main {
        margin-left: 0;
        padding: calc(var(--m-topbar-h) + 16px) 14px calc(var(--m-tabbar-h) + 16px);
        min-height: 100vh;
    }
    body.dashboard-mobile .page.active {
        gap: 16px;
    }
    /* Page-header компактнее */
    body.dashboard-mobile .page-header {
        gap: 12px;
    }
    body.dashboard-mobile .page-header-bar {
        height: 36px;
    }
    body.dashboard-mobile .page-header-title {
        font-size: 18px;
    }
    body.dashboard-mobile .page-header-subtitle {
        font-size: 11px;
    }
    /* KPI-row → 2-колоночный грид (2×2 для 4 карточек, 2×3 для 6) */
    body.dashboard-mobile .kpi-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }
    body.dashboard-mobile .kpi-card {
        min-width: 0;
        padding: 12px 14px 12px;
        gap: 2px;
    }
    body.dashboard-mobile .kpi-card::before {
        height: 2px;
    }
    body.dashboard-mobile .kpi-overline {
        font-size: 9px;
    }
    body.dashboard-mobile .kpi-label {
        font-size: 10px;
    }
    body.dashboard-mobile .kpi-value {
        font-size: 18px;
        letter-spacing: -0.3px;
    }
    body.dashboard-mobile .kpi-delta,
    body.dashboard-mobile .kpi-sub,
    body.dashboard-mobile .delta-row {
        font-size: 10px;
    }
    /* Двух- и трёх-колоночные сетки — 1 колонка на моб. */
    body.dashboard-mobile .grid-2,
    body.dashboard-mobile .grid-3 {
        grid-template-columns: 1fr !important;
        gap: 12px;
    }
    /* Фильтры/пилюли — wrap + меньше шрифт */
    body.dashboard-mobile .filter-row {
        gap: 6px;
    }
    body.dashboard-mobile .filter-btn,
    body.dashboard-mobile .pnl-preset,
    body.dashboard-mobile .alerts-domain-btn,
    body.dashboard-mobile .docs-filter-btn {
        font-size: 10px;
        padding: 6px 10px;
    }
    /* Чарты — уменьшаем высоту, внутри .chart-box */
    body.dashboard-mobile .chart-container {
        height: 220px;
    }
    body.dashboard-mobile .chart-box {
        padding: 14px;
    }
    /* Snapshot-banner + большие карточки */
    body.dashboard-mobile .snapshot-banner {
        padding: 14px;
        font-size: 12px;
    }
    /* Data-section + таблицы — scroll-x */
    body.dashboard-mobile .data-section {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    body.dashboard-mobile .data-section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
        padding: 12px 14px;
    }
    body.dashboard-mobile .data-section-title {
        font-size: 12px;
    }
    body.dashboard-mobile .data-table {
        font-size: 11px;
        min-width: 540px;
    }
    body.dashboard-mobile .data-table th,
    body.dashboard-mobile .data-table td {
        padding: 8px 10px;
        white-space: nowrap;
    }
    /* Revenue/expenses/avgcheck KPI-гриды (3×1 форсятся в inline !important) —
       гарантируем, что 3-колоночный !important не ломает 2-кол сетку */
    body.dashboard-mobile #revenue-kpi-cards,
    body.dashboard-mobile #expenses-kpi-cards,
    body.dashboard-mobile #avgcheck-kpi-cards {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* ────────────────────────────────────────────────────────
       8. МЕЛОЧЬ: фокус-видимость, сенсор-таргеты 44×44
       ──────────────────────────────────────────────────────── */
    .btn, .btn-primary, .btn-sm, button[type="submit"] {
        min-height: 44px;
    }
    input[type="email"],
    input[type="password"],
    input[type="text"],
    .form-input {
        min-height: 44px;
    }
}

/* ============================================================
   Очень маленькие экраны (<= 380px) — редкие iPhone SE / mini
   Дополнительно чуть ужимаем KPI-value и падинги.
   ============================================================ */
@media (max-width: 380px) {
    body.dashboard-mobile .kpi-row {
        gap: 8px;
    }
    body.dashboard-mobile .kpi-card {
        padding: 10px 12px;
    }
    body.dashboard-mobile .kpi-value {
        font-size: 16px;
    }
    body.dashboard-mobile .main {
        padding-left: 10px;
        padding-right: 10px;
    }
    .admin-layout > .main-content {
        padding-left: 12px;
        padding-right: 12px;
    }
    .mobile-tabbar .m-tab .m-tab-label {
        font-size: 9px;
    }
}
