/* Админка на телефоне (Влад, 21.10: «под телефон оптимизируй админку фулл»). Loaded last.
   ≤ 900px: one column, the menu behind a button (full-screen sheet), the search under the bar.
   ≤ 640px: table rows become cards «КОЛОНКА / значение» (labels come from admin-shell.js), forms stack. */

.nav-toggle { display: none; }

@media (max-width: 900px) {
    /* ---------- bar: mark + name on the left, the menu button on the right ---------- */
    .sidebar {
        position: sticky; top: 0; inset: auto; z-index: 30; width: auto; height: 56px; padding: 0 6px 0 14px; gap: 0;
        flex-direction: row; align-items: center; overflow: visible; border: 0;
        background: color-mix(in srgb, var(--sidebar) 90%, transparent); -webkit-backdrop-filter: blur(18px); backdrop-filter: blur(18px);
        box-shadow: 0 1px 0 var(--hairline);
    }
    .sidebar .brand { flex: 1; min-width: 0; height: 56px; padding: 0; gap: 10px; }
    .sidebar .brand span:last-child { display: block; }
    .sidebar .brand .brand-mark { width: 26px; height: 28px; flex-basis: 26px; }
    .nav-toggle {
        display: grid; place-items: center; width: 44px; height: 44px; border: 0; border-radius: 10px;
        background: transparent; color: var(--text); font-size: 18px; cursor: pointer;
    }
    .nav-toggle:hover, .nav-toggle:focus-visible { background: var(--tile-hover); outline: none; }
    .nav-toggle-close, body.nav-open .nav-toggle-open { display: none; }
    body.nav-open .nav-toggle-close { display: inline; }

    /* ---------- the menu: a sheet under the bar, the account and «Выйти» at the bottom ---------- */
    .sidebar nav, .sidebar .sidebar-foot { display: none; }
    body.nav-open { overflow: hidden; }
    body.nav-open .sidebar nav {
        display: block; position: fixed; top: 56px; left: 0; right: 0; bottom: 64px; z-index: 29; margin: 0;
        padding: 6px 12px 18px; overflow-y: auto; overscroll-behavior: contain; background: var(--sidebar);
    }
    body.nav-open .sidebar .sidebar-foot {
        display: flex; align-items: center; gap: 10px; position: fixed; left: 0; right: 0; bottom: 0; z-index: 29; height: 64px;
        padding: 0 14px calc(env(safe-area-inset-bottom) / 2) 18px; background: var(--sidebar); box-shadow: 0 -1px 0 var(--hairline);
    }
    /* a blurred bar would hold the fixed sheet inside its 56px: solid while the menu is open */
    body.nav-open .sidebar { background: var(--sidebar); -webkit-backdrop-filter: none; backdrop-filter: none; }
    body.nav-open .sidebar-foot .who { display: block; }
    body.nav-open .sidebar-foot > .ava { display: inline-grid; }
    body.nav-open .sidebar-foot .icon-button { width: 44px; height: 44px; font-size: 16px; }
    .sidebar nav a { min-height: 46px; padding: 0 12px; font-size: 15px; gap: 12px; }
    .sidebar nav a .nav-ic { width: 20px; flex-basis: 20px; font-size: 14px; }
    .sidebar nav a .nav-ic .pmark { width: 18px; height: 19px; }
    .sidebar .nav-group, .sidebar .nav-more > summary { padding: 16px 12px 6px; font-size: 10.5px; }
    .sidebar .nav-more > summary { min-height: 40px; display: flex; align-items: flex-end; }

    /* ---------- search and page ---------- */
    .topbar { position: static; margin-left: 0; height: auto; padding: 10px 14px; box-shadow: none; background: transparent; -webkit-backdrop-filter: none; backdrop-filter: none; }
    .top-search { max-width: none; }
    .top-search kbd { display: none; }
    .top-search input { height: 44px; padding-right: 14px; }
    /* 16px stops the phone from zooming into a field */
    input, select, textarea, .top-search input { font-size: 16px; }
    .shell { margin-left: 0; padding: 8px 14px 48px; min-height: 0; max-width: none; }

    .page-header { flex-direction: column; align-items: stretch; gap: 12px; margin-bottom: 16px; }
    .page-header h1 { font-size: 20px; overflow-wrap: anywhere; }
    .page-header > :last-child:not(:first-child) { justify-content: flex-start; }
    .page-header .actions, .page-header .toolbar, .page-header form { flex-wrap: wrap; }

    .panel { padding: 16px; margin-bottom: 14px; }
    .table-wrap, .panel .table-wrap { margin: 4px -16px -16px; }
    .panel.tight .table-wrap { margin: 0 -16px -14px; }
    .detail-grid, .grant-grid, .compare-grid { grid-template-columns: minmax(0, 1fr); }
    .detail-grid > *, .grant-grid > *, .compare-grid > *, .mod-groups > *, .stats > *, .tiles > * { min-width: 0; }

    /* tab rows scroll sideways instead of growing into a block */
    .segmented, .card-tabs { display: flex; flex-wrap: nowrap; max-width: 100%; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
    .segmented::-webkit-scrollbar { display: none; }
    .segmented a, .segmented button { flex: none; min-height: 34px; padding: 0 13px; font-size: 13px; }

    /* account card head: photo + name, buttons on their own row */
    .account-head { grid-template-columns: auto minmax(0, 1fr); gap: 12px 14px; }
    .account-head > .ava.lg { width: 56px; height: 56px; margin-top: 2px; }
    .account-head > .actions { grid-column: 1 / -1; flex-wrap: wrap; justify-content: flex-start; }
    .account-head .badge, .kv .badge { white-space: normal; max-width: 100%; }
    .account-head-facts .mono { overflow-wrap: anywhere; }

    .signal { grid-template-columns: minmax(0, 1fr); }
    .signal-actions { grid-column: 1 / -1; flex-direction: row; flex-wrap: wrap; }
    .automation-row { gap: 12px; }
    .automation-row > div { flex: 1; }
    .settings-save .button { width: 100%; }
    pre, code { white-space: pre-wrap; overflow-wrap: anywhere; }
    img, video, canvas, svg { max-width: 100%; }
    .sidebar svg { max-width: none; }
}

@media (max-width: 640px) {
    .stats, .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
    .stats article { padding: 12px 14px; min-height: 0; }
    .stats b { font-size: 21px; }
    .account-block-plaque { flex-wrap: wrap; }
    .account-block-text { flex-basis: calc(100% - 30px); }
    .account-block-plaque form { margin-left: 25px; }

    /* ---------- table rows as cards ---------- */
    table.m-cards, table.m-cards > tbody, table.m-cards > tbody > tr, table.m-cards > tbody > tr > td { display: block; width: auto; }
    table.m-cards > thead { display: none; }
    table.m-cards > tbody > tr {
        display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 14px; padding: 12px 16px;
        border-top: 1px solid var(--hairline);
    }
    table.m-cards > tbody > tr:first-child { border-top: 0; }
    table.m-cards > tbody > tr > td, table.m-cards > tbody > tr > th {
        padding: 0; border: 0; min-width: 0; text-align: left; white-space: normal; overflow-wrap: break-word;
    }
    table.m-cards > tbody > tr > td[data-label]::before {
        content: attr(data-label); display: block; margin-bottom: 1px; color: var(--muted);
        font-size: 10px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase;
    }
    /* the first cell is what the row is about: full width, no label */
    table.m-cards > tbody > tr > td:first-child { grid-column: 1 / -1; font-size: 14px; }
    table.m-cards > tbody > tr > td:first-child::before { display: none; }
    table.m-cards > tbody > tr > td.m-wide, table.m-cards > tbody > tr > td.actions-cell,
    table.m-cards > tbody > tr > td:has(form), table.m-cards > tbody > tr > td:has(.button) { grid-column: 1 / -1; }
    table.m-cards > tbody > tr > td.m-empty { display: none; }
    table.m-cards td.actions-cell form.actions, table.m-cards td.actions-cell form.actions:has(input:not([type="hidden"])) { width: auto; }
    table.m-cards > tfoot { display: block; }

    /* ---------- forms ---------- */
    .filter-row, .button-row, .block-options, .uc-grid { grid-template-columns: minmax(0, 1fr); }
    .toolbar > *, .search-row > * { flex: 1 1 auto; }
    .toolbar input:not([type="checkbox"]):not([type="radio"]), .toolbar select { min-width: 0; flex: 1 1 140px; }
    .automation-row { align-items: flex-start; }
    .sign-in-confirm > .button { margin-top: 0; width: 100%; }
    .sign-in-confirm .confirm-field { max-width: none; width: 100%; }
    .confirm-field input, .w-220 { width: 100%; max-width: none; }
    .flash { font-size: 13px; }
}

@media (max-width: 900px) {
    .panel-head { flex-wrap: wrap; gap: 8px 12px; }
    .automation-row { flex-wrap: wrap; }
    .automation-row > div { flex: 1 1 220px; min-width: 0; }
}
@media (max-width: 640px) {
    /* a text field takes its own row in a filter bar; selects and buttons share the next one */
    .toolbar input:not([type="checkbox"]):not([type="radio"]):not([type="number"]):not([type="hidden"]),
    .search-row input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]) { flex: 1 1 100%; }
}
@media (max-width: 900px) {
    select, input, textarea { max-width: 100%; }
    .form-row > *, .actions > * { min-width: 0; }
}
@media (max-width: 640px) {
    /* filter forms (Заказы, Ключи 7.1 …): the search field on its own row, selects two by two */
    form.actions > input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="number"]):not([type="date"]):not([type="datetime-local"]) { flex: 1 1 100%; }
    form.actions > select { flex: 1 1 140px; }
    table.m-cards td form.actions > input:not([type="hidden"]) { flex: 1 1 140px; }
}
