/* ==================== Дизайн-токены ==================== */
:root {
    /* --- Цвета: палитра PostgreSQL (глубокий слоновий синий + терминальные акценты) --- */
    --bg: #0d1321;
    --bg-soft: #131b2e;
    --surface: #182238;
    --surface-2: #202c47;
    --border: #2b3a5c;
    --border-light: #3c4f78;
    --text: #e7edf6;
    --text-muted: #8fa0bc;
    --primary: #336791;                /* PostgreSQL blue */
    --primary-hover: #5b9bd5;          /* светлый акцент: текст/иконки на тёмном */
    --primary-strong: #4a8bc2;         /* фон сплошных кнопок */
    --primary-strong-hover: #336791;   /* ховер темнее */
    --primary-soft: rgba(var(--primary-rgb), 0.16);
    --success: #2dd4a7;
    --danger: #ef5350;
    --danger-text: #ff8a80;
    --danger-hover: #e53935;
    --warning: #ffb74d;
    --placeholder: #5a6c8f;
    --code-text: #c3e88d;              /* терминально-зелёный для SQL-результатов */
    --skeleton: #202c47;
    --skeleton-highlight: rgba(255, 255, 255, 0.07);
    --overlay: rgba(5, 9, 18, 0.72);
    --overlay-strong: rgba(5, 9, 18, 0.86);
    --header-bg: #131b2e;

    /* --- Каналы цветов для rgba() (бренд меняется в одном месте) --- */
    --primary-rgb: 51, 103, 145;
    --violet-rgb: 91, 155, 213;
    --danger-rgb: 239, 83, 80;
    --warning-rgb: 255, 183, 77;

    --bg-image: none;

    /* --- Серии графиков (читаются и в CSS, и в canvas через getComputedStyle) --- */
    --chart-1: #5b9bd5;
    --chart-2: #ffb74d;
    --chart-3: #2dd4a7;

    /* --- Шкала отступов (2px-сетка) --- */
    --space-3xs: 2px;
    --space-2xs: 4px;
    --space-xs: 6px;
    --space-sm: 8px;
    --space-md: 10px;
    --space-lg: 12px;
    --space-xl: 14px;
    --space-2xl: 16px;
    --space-3xl: 18px;
    --space-4xl: 20px;
    --space-5xl: 24px;
    --space-6xl: 32px;
    --space-7xl: 40px;

    /* --- Шкала типографики (интерфейсный текст; заголовки — в rem) --- */
    --text-2xs: 11px;
    --text-xs: 12px;
    --text-sm: 13px;
    --text-md: 14px;
    --text-base: 15px;

    /* --- Радиусы --- */
    --radius-2xs: 4px;
    --radius-xs: 6px;
    --radius-sm: 8px;
    --radius-md: 10px;
    --radius: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;

    /* --- Прочее --- */
    --ring: 0 0 0 3px rgba(var(--primary-rgb), 0.28);
    /* Единый ритм: ЛЮБОЕ расстояние между блоками = --gap-block, внутренние
       отступы карточек/панелей/модалок = --pad-block. Расстояния держим в окне
       5–15px (чтобы раскладка не «скакала»), а воздух возвращаем внутренними
       отступами и полями страницы. */
    --gap-block: 14px;
    --pad-block: 22px;
    --row-zebra: rgba(255, 255, 255, 0.022); /* чередование строк в таблице результата */
    --stagger: 35ms;                   /* шаг каскадного появления карточек */
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
    --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.25);
    --font: 'IBM Plex Sans', 'Segoe UI', system-ui, sans-serif;
    --mono: 'IBM Plex Mono', 'Consolas', monospace;
}

/* ==================== Светлая тема ==================== */
body[data-theme="light"] {
    --bg: #eef2f7;
    --bg-soft: #ffffff;
    --surface: #ffffff;
    --surface-2: #e6ecf4;
    --border: #d3dde9;
    --border-light: #b9c8db;
    --text: #17263c;
    --text-muted: #5a6b84;
    --primary: #336791;
    --primary-hover: #2b5a80;          /* на светлом акцент темнее фона */
    --primary-strong: #336791;
    --primary-strong-hover: #2b5a80;
    --primary-soft: rgba(var(--primary-rgb), 0.12);
    /* Насыщенные линии графиков на белом фоне */
    --chart-1: #1f6fb2;
    --chart-2: #b25f00;
    --chart-3: #0f7a5c;
    --danger-text: #c62828;
    --row-zebra: rgba(23, 38, 60, 0.035);
    --placeholder: #93a2b8;
    --code-text: #2a4a2f;
    --skeleton: #e2e9f2;
    --skeleton-highlight: rgba(255, 255, 255, 0.65);
    --overlay: rgba(170, 184, 202, 0.5);
    --overlay-strong: rgba(170, 184, 202, 0.66);
    --bg-image: none;
    --shadow: 0 8px 32px rgba(30, 41, 66, 0.12);
    --shadow-sm: 0 2px 10px rgba(30, 41, 66, 0.07);
    --header-bg: #ffffff;
}

/* ==================== База ==================== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: var(--font);
    background: var(--bg);
    background-image: var(--bg-image);
    background-attachment: fixed;
    color: var(--text);
    min-height: 100vh;
    font-size: var(--text-base);
    line-height: 1.5;
    transition: background-color 0.2s ease, color 0.2s ease;
}

h1, h3 {
    font-weight: 700;
    letter-spacing: -0.02em;
}

/* Числовые значения не «прыгают» при обновлении данных */
.stat-value,
.timing-info span span,
.dash-table .num,
.result-rows,
.watch-updated,
.dash-updated {
    font-variant-numeric: tabular-nums;
}

/* ==================== Иконки ====================
   Единый набор: viewBox 24×24, stroke=currentColor, скруглённые концы.
   Размер задаётся классом, цвет наследуется от текста кнопки/статуса.
   Динамические иконки (тосты, удаление запроса) строит svgIcon() в script.js. */
.icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

.icon-sm {
    width: 15px;
    height: 15px;
}

.btn-sm .icon {
    width: 16px;
    height: 16px;
}

/* Клавиша-подсказка (Ctrl+Enter) */
.kbd {
    display: inline-flex;
    align-items: center;
    padding: var(--space-3xs) var(--space-xs);
    border: 1px solid var(--border);
    border-radius: var(--radius-2xs);
    background: var(--surface-2);
    font-family: var(--mono);
    font-size: var(--text-2xs);
    line-height: 1.7;
    color: var(--text-muted);
    white-space: nowrap;
}

.btn-primary .kbd {
    border-color: rgba(255, 255, 255, 0.35);
    background: rgba(255, 255, 255, 0.16);
    color: #fff;
}

/* ==================== Кнопки ==================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    padding: var(--space-md) var(--space-xl);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    font-size: var(--text-md);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--primary-strong);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-strong-hover);
    box-shadow: 0 4px 16px rgba(var(--primary-rgb), 0.35);
}

.btn-secondary {
    background: var(--primary-soft);
    color: var(--primary-hover);
    border-color: rgba(var(--primary-rgb), 0.3);
}

.btn-secondary:hover {
    background: rgba(var(--primary-rgb), 0.25);
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border-color: var(--border);
}

.btn-ghost:hover {
    background: var(--surface-2);
    color: var(--text);
}

.btn-danger {
    background: transparent;
    color: var(--danger-text);
    border-color: rgba(var(--danger-rgb), 0.35);
}

.btn-danger:hover {
    background: rgba(var(--danger-rgb), 0.12);
}

.btn-warning {
    background: transparent;
    color: var(--warning);
    border-color: rgba(var(--warning-rgb), 0.35);
}

.btn-warning:hover {
    background: rgba(var(--warning-rgb), 0.12);
}

.btn-block {
    width: 100%;
}

.btn-sm {
    padding: var(--space-xs) var(--space-lg);
    font-size: var(--text-sm);
}

/* Спиннер на кнопке во время загрузки */
.btn.loading {
    color: transparent !important;
    pointer-events: none;
    position: relative;
}

.btn.loading::after {
    content: '';
    position: absolute;
    width: 15px;
    height: 15px;
    border: 2px solid var(--primary);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ==================== Формы ==================== */
.field {
    margin-bottom: var(--gap-block);
}

.field label {
    display: block;
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: var(--space-xs);
}

.hint {
    margin-top: var(--space-xs);
    font-size: var(--text-sm);
    color: var(--text-muted);
}

input, select, textarea {
    width: 100%;
    padding: var(--space-md) var(--space-xl);
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-family: var(--font);
    font-size: var(--text-md);
    transition: border-color 0.15s, box-shadow 0.15s;
}

input::placeholder, textarea::placeholder {
    color: var(--placeholder);
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: var(--ring);
}

/* Единый видимый фокус для клавиатуры (мышь и тач не подсвечиваются) */
.btn:focus-visible,
.nav-btn:focus-visible,
.theme-toggle:focus-visible,
.modal-close:focus-visible,
.watch-toggle:has(input:focus-visible),
.panel-collapse-btn:focus-visible,
a:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

select {
    cursor: pointer;
}

textarea {
    resize: vertical;
    min-height: 150px;
    font-family: var(--mono);
    font-size: var(--text-md);
    line-height: 1.6;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0 var(--gap-block);
}

/* ==================== Авторизация ==================== */
#auth-page {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: var(--pad-block);
}

.auth-card {
    position: relative;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--border);
    padding: var(--pad-block);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow);
    width: 100%;
    max-width: 420px;
    animation: fadeUp 0.4s ease;
}

/* Тот же бренд-градиент, что у логотипа и шапки — единый акцент на входе */
.auth-card::before {
    content: '';
    position: absolute;
    inset: 0 0 auto 0;
    height: 3px;
    background: linear-gradient(90deg, var(--primary), rgb(var(--violet-rgb)));
}

.auth-logo {
    text-align: center;
    margin-bottom: var(--gap-block);
}

.logo-icon {
    width: 56px;
    height: 56px;
    margin: 0 auto var(--space-xl);
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--primary), rgb(var(--violet-rgb)));
    border-radius: var(--radius-lg);
    color: #fff;
    font-weight: 800;
    font-size: 18px;
    box-shadow: 0 8px 24px rgba(var(--primary-rgb), 0.4);
}

.logo-icon-sm {
    width: 36px;
    height: 36px;
    margin: 0;
    font-size: var(--text-sm);
    border-radius: var(--radius-md);
}

.auth-card h1 {
    font-size: 1.45rem;
    margin-bottom: var(--space-xs);
}

.auth-subtitle {
    color: var(--text-muted);
    font-size: var(--text-md);
}

.auth-card h3 {
    font-size: 1.15rem;
    margin-bottom: var(--gap-block);
}

.auth-switch {
    margin-top: var(--gap-block);
    text-align: center;
    color: var(--text-muted);
    font-size: var(--text-md);
}

.auth-switch a {
    color: var(--primary-hover);
    text-decoration: none;
    font-weight: 600;
}

.auth-switch a:hover {
    text-decoration: underline;
}

/* ==================== Верхняя панель ====================
   Шапка — колонка: строка брендинга + панель активной вкладки.
   Панели теперь часть шапки, поэтому их отдельное закрепление на экране
   и компенсация высоты в потоке страницы больше не нужны. */
#user-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    flex-direction: column;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border);
}

.header-main {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--gap-block);
    padding: var(--space-lg) var(--space-xl);
}

/* Панели выровнены по колонке контента страницы */
.header-panels {
    display: flex;
    flex-direction: column;
    gap: var(--gap-block);
    width: 100%;
    max-width: 920px;
    margin: 0 auto;
    padding: 0 var(--space-xl);
}

.header-panels > [hidden] {
    display: none;
}

/* Панель вкладки внутри шапки: без рамки, фона, тени и анимации — панель
   полностью растворяется в шапке, а «всплытие» карточки при смене вкладки
   выглядело бы скачком. Фон снимаем и здесь, и в правиле ниже: там селектор с id,
   и его специфичность перебила бы это правило. */
.header-panels .card {
    border: none;
    background: transparent;
    box-shadow: none;
    animation: none;
}

/* Поля внутри панелей шапки: собственный фон, чтобы читались на фоне шапки */
.header-panels select {
    background: var(--surface);
}

.brand {
    display: flex;
    align-items: center;
    gap: var(--space-md);
}

.brand-name {
    font-weight: 700;
    font-size: var(--text-base);
}

#main-nav {
    display: flex;
    gap: var(--space-xs);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-xs);
}

/* Меню «Ещё» — только в мобильной нижней навигации.
   На ПК пункты меню «распаковываются» в обычные вкладки шапки. */
.nav-more {
    display: contents;
}

.nav-more-btn {
    display: none;
}

.nav-more-menu {
    display: contents;
}

.nav-more-menu[hidden] {
    display: contents;
}

.nav-btn {
    padding: var(--space-sm) var(--space-xl);
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-family: var(--font);
    font-size: var(--text-md);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}

.nav-btn:hover {
    color: var(--text);
}

.nav-btn.active {
    background: var(--primary-strong);
    color: #fff;
}

.user-bar-actions {
    display: flex;
    align-items: center;
    gap: var(--space-md);
}

.welcome-chip {
    font-size: var(--text-md);
    font-weight: 600;
    color: var(--text-muted);
    background: var(--surface);
    border: 1px solid var(--border);
    padding: var(--space-sm) var(--space-xl);
    border-radius: var(--radius-xl);
}

/* ==================== Карточки ==================== */
.page-content {
    max-width: 920px;
    margin: 0 auto;
    padding: var(--space-4xl) var(--space-4xl) 60px;
    display: flex;
    flex-direction: column;
    gap: var(--gap-block);
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--pad-block);
    box-shadow: var(--shadow-sm);
    animation: fadeUp 0.3s ease;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-md);
    margin-bottom: var(--gap-block);
}

.card-header h3 {
    font-size: 1.05rem;
}

/* ==================== Кнопки и результаты ==================== */
.button-row {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: var(--gap-block);
    margin-top: 0;
}

.timing-info {
    display: flex;
    gap: var(--gap-block);
    font-size: var(--text-sm);
    color: var(--text-muted);
}

.timing-info span span {
    font-weight: 700;
    color: var(--primary-hover);
    font-family: var(--mono);
}

#result, #modal-result, .watch-result {
    background: var(--bg-soft);
    border: 1px solid var(--border);
    color: var(--code-text);
    padding: var(--pad-block);
    border-radius: var(--radius-sm);
    overflow-x: auto;
    max-height: 380px;
    font-family: var(--mono);
    font-size: var(--text-sm);
    line-height: 1.6;
    text-align: left;
    white-space: pre;
}

/* Табличный вывод результата запроса */
#result.result-table, #modal-result.result-table, .watch-result.result-table {
    white-space: normal;
    padding: 0;
    overflow: auto;
}

/* Пустое состояние результата: подсказка вместо прочерка */
#result.result-empty {
    white-space: normal;
    padding: 0;
    overflow: hidden;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--pad-block) var(--gap-block);
    font-family: var(--font);
    font-size: var(--text-sm);
    color: var(--text-muted);
    text-align: center;
}

.empty-icon {
    width: 30px;
    height: 30px;
    color: var(--text-muted);
    opacity: 0.75;
}

.empty-title {
    color: var(--text);
    font-weight: 600;
}

.empty-hint {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    flex-wrap: wrap;
    justify-content: center;
    font-size: var(--text-sm);
}

.result-rows {
    padding: var(--space-sm) var(--space-xl);
    font-size: var(--text-xs);
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    font-family: var(--font);
}

.result-grid {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--mono);
    font-size: var(--text-sm);
}

.result-grid th,
.result-grid td {
    padding: var(--space-sm) var(--space-lg);
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
    white-space: nowrap;
    max-width: 340px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.result-grid th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--surface);
    color: var(--text-muted);
    font-weight: 600;
    font-family: var(--font);
    font-size: var(--text-xs);
}

/* Зебра задаётся фоном строки: ховер ячейки (ниже по файлу) её перекрывает */
.result-grid tbody tr:nth-child(even) {
    background: var(--row-zebra);
}

.result-grid tbody tr:hover td {
    background: var(--primary-soft);
}

.result-grid .cell-null {
    color: var(--text-muted);
    font-style: italic;
}

/* ==================== Виджеты мониторинга (под панелью подключения) ==================== */
/* Flex вместо grid: при выключении одного виджета второй занимает всю ширину */
.watch-widgets {
    display: flex;
    flex-wrap: wrap;
    gap: var(--gap-block);
}

.watch-card {
    position: relative;
    overflow: hidden;              /* обрезает статусную полосу по радиусу карточки */
    flex: 1 1 340px;
    min-width: 0;
}

/* Статусная полоса слева: цвет состояния виджета (данные/ошибка/загрузка) */
.watch-card::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: transparent;
    transition: background 0.2s ease;
}

.watch-card[data-state="ok"]::before { background: var(--success); }
.watch-card[data-state="stale"]::before { background: var(--warning); }
.watch-card[data-state="error"]::before { background: var(--danger); }
.watch-card[data-state="loading"]::before { background: var(--primary); }

/* Индикатор-точка: пульсирует во время запроса, иначе показывает состояние */
.watch-status {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--text-muted);
}

.watch-card[data-state="ok"] .watch-status { background: var(--success); }
.watch-card[data-state="stale"] .watch-status { background: var(--warning); }
.watch-card[data-state="error"] .watch-status { background: var(--danger); }

.watch-card[data-state="loading"] .watch-status {
    background: var(--primary);
    animation: watch-pulse 1.1s ease-in-out infinite;
}

@keyframes watch-pulse {
    50% { opacity: 0.3; transform: scale(0.7); }
}

/* Метка времени подкрашивается, когда данные устарели или запрос упал */
.watch-card[data-state="stale"] .watch-updated { color: var(--warning); }
.watch-card[data-state="error"] .watch-updated { color: var(--danger-text); }

.watch-card .card-header {
    margin-bottom: var(--gap-block);
}

.watch-toggle {
    display: inline-flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-xs) var(--space-lg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-soft);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    user-select: none;
    transition: border-color 0.15s, background 0.15s, color 0.15s;
}

.watch-toggle:hover {
    border-color: var(--border-light);
    color: var(--text);
}

.watch-toggle input {
    width: auto;
    margin: 0;
    padding: 0;
    border: none;
    background: none;
    cursor: pointer;
    accent-color: var(--primary);
}

.watch-toggle input:focus {
    box-shadow: none;
}

/* Кнопка-шапка панели (название + стрелка) для сворачивания на мобильных */
.panel-collapse-btn {
    display: inline-flex;
    align-items: center;
    gap: var(--space-sm);
    padding: 0;
    border: none;
    background: none;
    color: inherit;
    font: inherit;
    font-weight: 700;
    letter-spacing: -0.02em;
    text-align: left;
    cursor: pointer;
}

.panel-collapse-btn:disabled {
    cursor: default;
    opacity: 1;
}

.panel-collapse-btn:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 3px;
    border-radius: var(--radius-2xs);
}

.panel-chevron {
    display: none; /* на ПК стрелка не нужна */
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    transition: transform 0.2s ease;
    transform: rotate(180deg); /* развёрнуто — стрелка вверх */
}

#config-manager.collapsed .panel-chevron,
#dash-source-card.collapsed .panel-chevron {
    transform: rotate(0deg); /* свёрнуто — стрелка вниз */
}

.watch-toggle:has(input:checked) {
    background: var(--primary-soft);
    border-color: var(--primary);
    color: var(--text);
}

.watch-db {
    display: inline-block;
    margin-left: var(--space-xs);
    padding: var(--space-3xs) var(--space-sm);
    border-radius: var(--radius-xs);
    background: var(--primary-soft);
    color: var(--primary-hover);
    font-family: var(--mono);
    font-size: var(--text-xs);
    font-weight: 600;
    vertical-align: middle;
}

.watch-sub {
    margin: 0 0 var(--space-lg);
    font-size: var(--text-sm);
    color: var(--text-muted);
}

.watch-updated {
    font-size: var(--text-xs);
    color: var(--text-muted);
    white-space: nowrap;
}

.watch-result.watch-error {
    color: var(--danger-text);
}

/* ==================== Пользователи ==================== */
#users-list {
    display: flex;
    flex-direction: column;
    gap: var(--gap-block);
}

.user-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-lg);
    padding: var(--pad-block) var(--gap-block);
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color 0.15s;
}

.user-item:hover {
    border-color: var(--border-light);
}

.user-info {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
}

.user-name {
    font-size: var(--text-base);
    font-weight: 600;
}

.user-created {
    font-size: var(--text-sm);
    color: var(--text-muted);
}

.user-actions {
    display: flex;
    gap: var(--gap-block);
}

/* ==================== Виджеты и сохранённые запросы ==================== */
#widgets-list,
#saved-queries-list {
    display: flex;
    flex-direction: column;
    gap: var(--gap-block);
}

.widget-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-lg);
    padding: var(--pad-block) var(--gap-block);
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color 0.15s;
}

.widget-item:hover {
    border-color: var(--border-light);
}

.widget-info {
    flex: 1;
    min-width: 180px;
}

.widget-name {
    font-size: var(--text-base);
    font-weight: 600;
}

.widget-meta {
    font-size: var(--text-sm);
    color: var(--text-muted);
    font-family: var(--mono);
    overflow-wrap: anywhere;
}

.widget-actions {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    flex-shrink: 0;
}

/* Статус проверки запроса в модалке виджета */
.widget-check-status {
    margin-top: var(--gap-block);
    font-size: var(--text-sm);
    font-weight: 500;
    overflow-wrap: anywhere;
}

.widget-check-loading { color: var(--text-muted); }
.widget-check-ok { color: var(--success); }
.widget-check-error { color: var(--danger-text); }

/* Индикатор доступности конфигурации */
.config-avail {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    margin-left: var(--space-xs);
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text-muted);
    vertical-align: middle;
}

.avail-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--text-muted);
}

.config-avail.avail-ok { color: var(--success); }
.config-avail.avail-ok .avail-dot { background: var(--success); }

.config-avail.avail-err { color: var(--danger-text); }
.config-avail.avail-err .avail-dot { background: var(--danger); }

.config-avail.avail-loading { color: var(--primary-hover); }
.config-avail.avail-loading .avail-dot {
    background: var(--primary);
    animation: watch-pulse 1.1s ease-in-out infinite;
}

/* Компактные карточки доступных конфигураций: до 3 в строку (на мобильных — по одной) */
.configs-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--gap-block);
}

.config-card {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
    padding: var(--gap-block) var(--gap-block);
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    min-width: 0;
    transition: border-color 0.15s;
}

.config-card:hover {
    border-color: var(--border-light);
}

.config-card-name {
    font-weight: 600;
    font-size: var(--text-md);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.config-card-meta {
    font-family: var(--mono);
    font-size: var(--text-xs);
    color: var(--text-muted);
    overflow-wrap: anywhere;
}

.config-card .config-avail {
    margin-left: 0;
}

/* ==================== Модальные окна ==================== */
.modal {
    position: fixed;
    z-index: 1000;
    inset: 0;
    background: var(--overlay);
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(4px);
    padding: var(--pad-block);
    padding-left: max(var(--pad-block), env(safe-area-inset-left));
    padding-right: max(var(--pad-block), env(safe-area-inset-right));
    padding-bottom: max(var(--pad-block), env(safe-area-inset-bottom));
}

.modal-content {
    background: var(--surface);
    border: 1px solid var(--border);
    padding: var(--pad-block);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 520px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: var(--shadow);
    animation: fadeUp 0.25s ease;
}

.modal-content-wide {
    max-width: 720px;
}

.modal-content-narrow {
    max-width: 420px;
}

#confirm-message {
    white-space: pre-line;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--gap-block);
}

.modal-header h3 {
    font-size: 1.15rem;
}

.modal-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: var(--radius-xs);
    transition: color 0.15s, background 0.15s;
}

.modal-close:hover {
    color: var(--text);
    background: var(--surface-2);
}

.modal-content p {
    color: var(--text-muted);
    margin-bottom: var(--gap-block);
}

.modal-buttons {
    margin-top: var(--gap-block);
    display: flex;
    justify-content: flex-end;
    gap: var(--gap-block);
}

.result-details {
    text-align: left;
    margin-bottom: var(--gap-block);
    padding: var(--space-lg) var(--space-xl);
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: var(--text-md);
}

.result-details div {
    margin-bottom: var(--space-xs);
}

.result-details div:last-child {
    margin-bottom: 0;
}

.result-details strong {
    color: var(--text-muted);
    font-weight: 600;
}

/* ==================== Список конфигураций ==================== */
#config-list-container {
    max-height: 320px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--gap-block);
}

.config-list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-md);
    padding: var(--pad-block) var(--gap-block);
    background: var(--bg-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color 0.15s;
}

.config-list-item:hover {
    border-color: var(--border-light);
}

.config-info {
    flex: 1;
    min-width: 180px;
}

.config-list-item .config-name {
    font-weight: 600;
    font-size: var(--text-md);
}

.config-list-item .config-meta {
    font-size: var(--text-xs);
    color: var(--text-muted);
    font-family: var(--mono);
    overflow-wrap: anywhere;
}

.config-list-actions {
    display: flex;
    gap: var(--space-xs);
    flex-shrink: 0;
}

.config-list-actions .btn {
    min-width: 104px;
}

.config-test-status {
    font-size: var(--text-sm);
    margin-top: var(--space-xs);
    font-weight: 500;
    overflow-wrap: anywhere;
}

.config-status-ok {
    color: var(--success);
}

.config-status-err {
    color: var(--danger-text);
}

/* ==================== Дашборд ==================== */
.dash-controls {
    display: flex;
    align-items: center;
    gap: var(--gap-block);
    flex-wrap: wrap;
}

.dash-autorefresh {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    font-size: var(--text-sm);
    color: var(--text-muted);
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
}

.dash-autorefresh input {
    width: auto;
    margin: 0;
    cursor: pointer;
    accent-color: var(--primary);
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: var(--gap-block);
}

/* ===== Графики ===== */
.charts-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--gap-block);
}

.chart-card .card-header {
    margin-bottom: var(--gap-block);
}

.chart-legend {
    display: flex;
    gap: var(--gap-block);
    flex-wrap: wrap;
}

.legend-item {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    font-size: var(--text-xs);
    color: var(--text-muted);
    white-space: nowrap;
}

.legend-item i {
    width: 10px;
    height: 10px;
    border-radius: var(--radius-2xs);
    display: inline-block;
    flex-shrink: 0;
}

/* Свотчи легенды берут те же токены, что и линии canvas */
.swatch-total { background: var(--chart-1); }
.swatch-active { background: var(--chart-2); }
.swatch-idle { background: var(--chart-3); }

.chart-wrap {
    position: relative;
}

.chart-card canvas {
    display: block;
    width: 100%;
    height: 180px;
}

/* Тултип графика при наведении/тапе */
.chart-tooltip {
    position: absolute;
    top: 6px;
    left: 0;
    transform: translateX(-50%);
    pointer-events: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    padding: var(--space-sm) var(--space-md);
    font-size: var(--text-xs);
    line-height: 1.5;
    white-space: nowrap;
    z-index: 5;
}

.chart-tooltip .tt-time {
    color: var(--text-muted);
    font-size: var(--text-2xs);
    margin-bottom: var(--space-xs);
}

.chart-tooltip .tt-row {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
}

.chart-tooltip .tt-row i {
    width: 8px;
    height: 8px;
    border-radius: 2px;
    flex-shrink: 0;
}

.chart-tooltip .tt-row b {
    margin-left: auto;
    padding-left: var(--space-md);
    font-family: var(--mono);
    font-weight: 600;
}

/* Контейнер содержимого дашборда: равномерные отступы между карточками */
#dash-content {
    display: flex;
    flex-direction: column;
    gap: var(--gap-block);
}

/* Панели выбора источника — часть шапки: рамку и фон задаёт .header-panels .card.
   Ширина по колонке шапки (на дашборде при ≥1200px — шире, см. ниже) */
.dash-source-card,
#config-manager {
    width: 100%;
    background: transparent; /* фон панели не отделяет её от шапки */
}

/* Широкие экраны: дашборд использует ширину — слева показатели и график, справа таблицы.
   Вкладка «Запросы» остаётся узкой колонкой: длинный SQL и таблицы читаются лучше. */
@media (min-width: 1200px) {
    #dashboard-page {
        max-width: 1200px;
    }

    /* Панель дашборда тянется по его широкой колонке */
    .header-panels:has(#dash-source-card:not([hidden])) {
        max-width: 1200px;
    }

    #dash-content {
        display: grid;
        /* Две равные половины: «Сессии» + «Топ таблиц» симметричны относительно середины */
        grid-template-columns: repeat(2, minmax(0, 1fr));
        align-items: start;
    }

    /* Плитки показателей — горизонтальная строка на всю ширину дашборда:
       до 6 в ряд, дальше перенос (min = 1/6 ширины ограничивает число колонок,
       auto-fit схлопывает пустые, поэтому при 4 плитках они тянутся на всю строку) */
    #dash-stats-grid {
        grid-column: 1 / -1;
    }

    .stats-grid {
        grid-template-columns: repeat(auto-fit, minmax(max(150px, calc((100% - 5 * var(--gap-block)) / 6 - 1px)), 1fr));
    }

    /* График «Сессии баз данных» — левая половина */
    .charts-grid {
        grid-column: 1;
    }

    /* «Топ таблиц по размеру» — правая половина */
    #dash-content > .card:has(#dash-tables) {
        grid-column: 2;
    }

    /* «Базы данных сервера» — от левого края плиток на всю ширину */
    #dash-content > .card:has(#dash-databases) {
        grid-column: 1 / -1;
    }
}

.dash-updated {
    font-size: var(--text-xs);
    color: var(--text-muted);
    white-space: nowrap;
}

/* ===== Скелетоны (плейсхолдеры загрузки) ===== */
.skeleton {
    position: relative;
    overflow: hidden;
    background: var(--skeleton);
    border-radius: var(--radius-xs);
}

.skeleton::after {
    content: '';
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background: linear-gradient(90deg, transparent, var(--skeleton-highlight), transparent);
    animation: shimmer 1.4s infinite;
}

@keyframes shimmer {
    100% { transform: translateX(100%); }
}

.sk-value { height: 22px; width: 65%; margin-bottom: 10px; }
.sk-label { height: 10px; width: 45%; margin-bottom: 8px; }
.sk-sub { height: 10px; width: 80%; }

.sk-table-row {
    display: flex;
    justify-content: space-between;
    gap: var(--gap-block);
    padding: var(--space-md) 0;
    border-bottom: 1px solid var(--border);
}

.sk-table-row:last-child { border-bottom: none; }

.sk-table-row .skeleton { height: 13px; }

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--pad-block) var(--gap-block);
    box-shadow: var(--shadow-sm);
    animation: fadeUp 0.3s ease;
}

.stat-value {
    font-family: var(--mono);
    font-size: 1.35rem;
    font-weight: 700;
    line-height: 1.2;
    overflow-wrap: anywhere;
}

.stat-label {
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-top: var(--space-xs);
    font-weight: 600;
}

.stat-sub {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: var(--space-xs);
    overflow-wrap: anywhere;
}

.meter-bar {
    height: 6px;
    border-radius: var(--radius-2xs);
    background: var(--surface-2);
    margin-top: var(--space-md);
    overflow: hidden;
}

.meter-bar-fill {
    height: 100%;
    background: var(--primary);
    border-radius: var(--radius-2xs);
    transition: width 0.3s ease;
}

.dash-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-md);
    padding: var(--pad-block) var(--gap-block);
    text-align: center;
    color: var(--text-muted);
}

.dash-empty p {
    max-width: 420px;
}

.dash-table-wrap {
    overflow-x: auto;
}

.dash-table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--text-md);
}

.dash-table th {
    text-align: left;
    color: var(--text-muted);
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    white-space: nowrap;
}

.dash-table td {
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}

.dash-table tbody tr:last-child td {
    border-bottom: none;
}

.dash-table .num {
    font-family: var(--mono);
    text-align: right;
    white-space: nowrap;
}

.dash-table .mono-cell {
    font-family: var(--mono);
    font-size: var(--text-sm);
    overflow-wrap: anywhere;
}

.dash-table .bar-cell {
    width: 120px;
    vertical-align: middle;
}

.dash-table .row-current td {
    background: var(--primary-soft);
}

.current-badge {
    display: inline-block;
    font-size: var(--text-2xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--primary-hover);
    border: 1px solid rgba(var(--primary-rgb), 0.35);
    border-radius: var(--radius-md);
    padding: var(--space-3xs) var(--space-sm);
    margin-left: var(--space-xs);
    vertical-align: 1px;
}

/* ==================== Тосты ==================== */
#toast-container {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom));
    right: calc(20px + env(safe-area-inset-right));
    z-index: 3000;
    display: flex;
    flex-direction: column;
    gap: var(--gap-block);
    max-width: 360px;
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: var(--space-md);
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--primary);
    color: var(--text);
    padding: var(--pad-block) var(--gap-block);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    font-size: var(--text-md);
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.25s ease, transform 0.25s ease;
    overflow-wrap: anywhere;
}

/* Иконка статуса тоста: цвет дублирует полосу слева */
.toast-icon {
    display: inline-flex;
    flex-shrink: 0;
    margin-top: 1px;
    color: var(--primary);
}

.toast-icon .icon {
    width: 18px;
    height: 18px;
}

.toast-text {
    flex: 1;
    min-width: 0;
}

.toast-error .toast-icon {
    color: var(--danger-text);
}

.toast-success .toast-icon {
    color: var(--success);
}

.toast-visible {
    opacity: 1;
    transform: translateY(0);
}

.toast-error {
    border-left-color: var(--danger);
}

.toast-success {
    border-left-color: var(--success);
}

.toast-info {
    border-left-color: var(--primary);
}

/* ==================== Автологаут ==================== */
.idle-overlay {
    position: fixed;
    z-index: 2000;
    inset: 0;
    background: var(--overlay-strong);
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(6px);
    padding: var(--pad-block);
    padding-bottom: max(var(--space-4xl), env(safe-area-inset-bottom));
}

.idle-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: var(--pad-block);
    text-align: center;
    max-width: 380px;
    width: 100%;
    box-shadow: var(--shadow);
    animation: fadeUp 0.3s ease;
}

.idle-icon {
    display: flex;
    justify-content: center;
    color: var(--warning);
    margin-bottom: var(--space-lg);
}

.idle-icon svg {
    width: 40px;
    height: 40px;
}

.idle-card h3 {
    font-size: 1.2rem;
    margin-bottom: var(--space-sm);
}

.idle-card p {
    color: var(--text-muted);
    font-size: var(--text-md);
    margin-bottom: var(--gap-block);
}

.idle-countdown {
    font-family: var(--mono);
    font-size: 52px;
    font-weight: 700;
    color: var(--warning);
    margin-bottom: var(--gap-block);
    line-height: 1;
}

.idle-card .btn {
    width: 100%;
    margin-bottom: var(--gap-block);
}

/* ==================== Анимации ==================== */
@keyframes fadeUp {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Каскадное появление карточек вместо одновременного «мигания» страницы.
   Дочерние карточки виджетов продолжают ту же последовательность. */
.page-content > *:nth-child(1) { animation-delay: 0ms; }
.page-content > *:nth-child(2) { animation-delay: calc(var(--stagger) * 1); }
.page-content > *:nth-child(3) { animation-delay: calc(var(--stagger) * 2); }
.page-content > *:nth-child(4) { animation-delay: calc(var(--stagger) * 3); }
.page-content > *:nth-child(5) { animation-delay: calc(var(--stagger) * 4); }
.page-content > *:nth-child(n+6) { animation-delay: calc(var(--stagger) * 5); }

.watch-widgets > .watch-card:nth-child(1) { animation-delay: calc(var(--stagger) * 1); }
.watch-widgets > .watch-card:nth-child(2) { animation-delay: calc(var(--stagger) * 3); }

/* Уважение к системной настройке «меньше анимаций» */
@keyframes navMenuUp {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-delay: 0s !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        transition-delay: 0s !important;
    }
}

/* ==================== Скроллбар ==================== */
::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-track {
    background: var(--bg);
}

::-webkit-scrollbar-thumb {
    background: var(--border-light);
    border-radius: 5px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--border-light);
}

/* ==================== Переключатель темы ==================== */
.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.15s;
}

.theme-toggle:hover {
    background: var(--surface-2);
    color: var(--text);
    border-color: var(--border-light);
}

.theme-toggle svg {
    width: 18px;
    height: 18px;
}

body[data-theme="light"] .icon-sun {
    display: none;
}

body:not([data-theme="light"]) .icon-moon {
    display: none;
}

.theme-toggle-fixed {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 500;
}

/* ==================== Адаптивность ==================== */
@media (max-width: 720px) {

    .header-main {
        flex-wrap: wrap;
        padding: var(--space-md) var(--space-lg);
        padding-left: max(var(--space-lg), env(safe-area-inset-left));
        padding-right: max(var(--space-lg), env(safe-area-inset-right));
        gap: var(--space-md);
    }

    .header-panels {
        padding: 0 var(--space-lg);
        padding-left: max(var(--space-lg), env(safe-area-inset-left));
        padding-right: max(var(--space-lg), env(safe-area-inset-right));
    }

    /* ===== Нижняя навигация (тач-доступная) ===== */
    #main-nav {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: auto;
        z-index: 150;
        justify-content: stretch;
        border-radius: 0;
        border-bottom: none;
        border-left: none;
        border-right: none;
        padding: var(--space-2xs) var(--space-2xs) calc(var(--space-2xs) + env(safe-area-inset-bottom));
        background: var(--header-bg);
        box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.3);
    }

    .nav-btn {
        flex: 1;
        padding: var(--space-lg) var(--space-xs);
        font-size: var(--text-sm);
        white-space: nowrap;
    }

    /* ===== Выпадающее меню «Ещё»: раскрывается вверх ===== */
    .nav-more {
        flex: 1;
        display: flex;
        position: relative;
    }

    .nav-more-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        position: relative;
    }

    .nav-more-menu {
        display: flex;
        flex-direction: column;
    }    .nav-more-btn {
        position: relative;
    }

    /* Точка на кнопке «Ещё», когда активна вкладка из меню */
    .nav-more-dot {
        position: absolute;
        top: 7px;
        right: 12px;
        width: 7px;
        height: 7px;
        border-radius: 50%;
        background: var(--primary-hover);
    }

    .nav-more-menu {
        position: fixed;
        left: var(--space-sm);
        right: var(--space-sm);
        bottom: calc(64px + env(safe-area-inset-bottom));
        display: flex;
        flex-direction: column;
        gap: var(--space-2xs);
        padding: var(--space-sm);
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        box-shadow: 0 -8px 28px rgba(0, 0, 0, 0.35);
        animation: navMenuUp 0.16s ease;
        z-index: 300;
    }

    .nav-more-menu[hidden] {
        display: none;
    }

    .nav-more-item {
        flex: none;
        text-align: right;
        padding-right: var(--space-xl);
    }

    .nav-more-item:hover {
        background: var(--surface-2);
    }

    .nav-more-item.active {
        background: var(--primary-strong);
        color: #fff;
    }

    /* Место под фиксированную нижнюю навигацию */
    .page-content {
        padding: var(--space-4xl) var(--space-lg) calc(84px + env(safe-area-inset-bottom));
        gap: var(--gap-block);
    }

    /* ===== Тосты сверху — не перекрывают нижние кнопки ===== */
    #toast-container {
        top: calc(12px + env(safe-area-inset-top));
        bottom: auto;
        left: 12px;
        right: 12px;
        max-width: none;
    }

    .toast {
        transform: translateY(-8px);
    }

    .toast-visible {
        transform: translateY(0);
    }

    .user-bar-actions {
        margin-left: auto;
    }

    .welcome-chip {
        max-width: 110px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        padding: var(--space-sm) var(--space-md);
    }

    .form-grid {
        grid-template-columns: 1fr;
        gap: 0;
    }

    /* В панелях (часть шапки) selects в одну строку — компактнее */
    .dash-source-card .form-grid,
    #config-manager .form-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0 var(--gap-block);
    }

    .dash-source-card .field label,
    #config-manager .field label {
        font-size: var(--text-xs);
        margin-bottom: var(--space-xs);
    }

    .dash-source-card select,
    #config-manager select {
        padding: var(--space-sm) var(--space-md);
    }

    /* Кнопки панели «Подключение» — в строку, чтобы sticky-панель была компактной */
    #config-manager .button-row {
        flex-direction: row;
        margin-top: 0;
    }

    /* ===== Сворачивание панелей шапки в одну строку ===== */
    .panel-collapse-btn .panel-chevron {
        display: block;
    }

    /* Свёрнуто: остаётся только строка с названием и стрелкой */
    #config-manager.collapsed #config-manager-body,
    #dash-source-card.collapsed #dash-source-body,
    #dash-source-card.collapsed .dash-controls {
        display: none;
    }

    #config-manager.collapsed .card-header,
    #dash-source-card.collapsed .card-header {
        margin-bottom: 0;
    }

    #config-manager .button-row .btn {
        flex: 1;
        width: auto;
        padding: var(--space-md) var(--space-sm);
        font-size: var(--text-sm);
    }

    .card {
        padding: var(--pad-block);
        border-radius: var(--radius-md);
    }

    .card-header h3 {
        font-size: 1rem;
    }

    /* Убираем hover-зависимости и липкость на тач-устройствах */
    .btn:active {
        transform: none;
    }

    /* Кнопки в строках растягиваются на всю ширину */
    .button-row {
        flex-direction: column;
    }

    .button-row .btn {
        width: 100%;
    }

    /* 16px+ не даёт iOS авто-зумить поле при фокусе */
    input, select, textarea {
        font-size: 16px;
        padding: var(--space-lg) var(--space-lg);
    }

    /* ===== Тап-таргеты: не меньше 44px ===== */
    .btn {
        min-height: 44px;
    }

    .watch-toggle {
        padding: var(--space-lg) var(--space-xl);
    }

    .modal-close {
        width: 44px;
        height: 44px;
    }

    /* Клавиатурная подсказка на тач-устройствах не нужна */
    .btn .kbd,
    .empty-kbd-wrap {
        display: none;
    }

    .field label {
        font-size: var(--text-sm);
    }

    .user-item {
        flex-direction: column;
        align-items: stretch;
    }

    .user-actions {
        width: 100%;
    }

    .user-actions .btn {
        flex: 1;
    }

    .widget-item {
        flex-direction: column;
        align-items: stretch;
    }

    .widget-info {
        min-width: 0;
    }

    .widget-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .widget-actions .watch-toggle {
        flex: 1 1 100%;
        justify-content: center;
    }

    .widget-actions .btn {
        flex: 1;
    }

    .modal {
        padding: var(--space-lg);
        padding-left: max(var(--space-lg), env(safe-area-inset-left));
        padding-right: max(var(--space-lg), env(safe-area-inset-right));
        padding-bottom: max(var(--space-lg), env(safe-area-inset-bottom));
        align-items: flex-end;
    }

    .modal-content {
        padding: var(--pad-block);
        max-height: 88dvh;
    }

    .modal-buttons {
        flex-direction: column-reverse;
    }

    .modal-buttons .btn {
        width: 100%;
    }

    .modal-close {
        font-size: 30px;
        padding: var(--space-xs) var(--space-lg);
    }

    /* Кнопки конфигураций: в одну строку на всю ширину карточки */
    .config-list-item {
        align-items: stretch;
        flex-direction: column;
    }

    .config-info {
        min-width: 0;
    }

    .config-list-actions {
        width: 100%;
    }

    .config-list-actions .btn {
        flex: 1;
        min-width: 0;
    }

    /* ===== Дашборд ===== */
    #dash-content {
        gap: var(--gap-block);
    }

    #dash-stats-grid [data-key="connections"] { order: 1; }
    #dash-stats-grid [data-key="dbsize"] { order: 2; }
    #dash-stats-grid [data-key="uptime"] { order: 3; }
    #dash-stats-grid [data-key="version"] { order: 4; }

    .dash-controls {
        width: 100%;
        justify-content: space-between;
    }

    /* Виджеты мониторинга и карточки конфигураций — в одну колонку на узких экранах */
    .watch-card {
        flex-basis: 100%;
    }

    .configs-grid {
        grid-template-columns: 1fr;
    }

    /* Карточка конфигурации: имя и индикатор в одну строку, мета скрыта.
       Если имя шире строки — индикатор переносится на следующую. */
    .config-card {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        gap: var(--space-xs) var(--space-sm);
    }

    .config-card-meta {
        display: none;
    }

    .config-card-name {
        flex: 0 0 auto;
        max-width: 100%;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        overflow-wrap: anywhere;
    }

    .config-card .config-avail {
        flex: 0 0 auto;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--gap-block);
    }

    /* Порядок на мобильных: сессии (график), соединения, размер, аптайм, версия */
    .charts-grid {
        order: -1;
        grid-template-columns: 1fr;
        gap: var(--gap-block);
    }

    .chart-card canvas {
        height: 150px;
    }

    .chart-legend {
        gap: var(--space-md);
    }

    .stat-card {
        padding: var(--pad-block);
    }

    .stat-value {
        font-size: 1.05rem;
    }

    .stat-label {
        font-size: var(--text-2xs);
    }

    .stat-sub {
        font-size: var(--text-2xs);
    }

    /* Полосы-индикаторы заметнее на мобильных */
    .meter-bar {
        height: 8px;
        margin-top: var(--space-sm);
    }

    .meter-bar-fill {
        border-radius: var(--radius-2xs);
    }

    .dash-table th,
    .dash-table td {
        padding: var(--space-sm) var(--space-sm);
    }

    .dash-table {
        font-size: var(--text-sm);
    }

    .dash-table .bar-cell {
        width: 56px;
    }

    #result, #modal-result, .watch-result {
        max-height: 300px;
        font-size: var(--text-xs);
    }

    .auth-card {
        padding: var(--pad-block);
    }

    .theme-toggle {
        width: 40px;
        height: 40px;
    }
}

/* ==================== Узкие экраны ==================== */
@media (max-width: 400px) {
    .stats-grid {
        grid-template-columns: 1fr;
    }

    .nav-btn {
        padding: var(--space-sm) var(--space-xs);
        font-size: var(--text-xs);
        letter-spacing: -0.01em;
    }

    .welcome-chip {
        display: none;
    }

    .dash-table .bar-cell {
        display: none;
    }

    .auth-card h1 {
        font-size: 1.2rem;
    }
}
