/* Shared Telegram Mini App adaptation layer. */
:root {
    --tma-bg: #0f0f0f;
    --tma-secondary-bg: #181818;
    --tma-surface: #1f1f1f;
    --tma-text: #f4f4f4;
    --tma-hint: #9a9a9a;
    --tma-link: #3390ec;
    --tma-button: #3390ec;
    --tma-button-text: #ffffff;
    --tma-line: rgba(127, 127, 127, 0.24);
    --tma-safe-bottom: env(safe-area-inset-bottom, 0px);
    --tma-bottom-tabs-height: 72px;
}

body.is-telegram {
    --bg: var(--tma-bg);
    --bg-soft: var(--tma-secondary-bg);
    --bg-elevated: var(--tma-surface);
    --bg-glass: var(--tma-surface);
    --text: var(--tma-text);
    --text-muted: var(--tma-hint);
    --text-soft: var(--tma-hint);
    --accent: var(--tma-button);
    --accent-2: var(--tma-link);
    --line: var(--tma-line);
    --line-strong: color-mix(in srgb, var(--tma-button) 42%, transparent);
    --aa-bg: var(--tma-bg);
    --aa-bg-soft: var(--tma-secondary-bg);
    --aa-bg-elevated: var(--tma-surface);
    --aa-bg-glass: var(--tma-surface);
    --aa-surface-1: var(--tma-surface);
    --aa-surface-2: color-mix(in srgb, var(--tma-surface) 88%, var(--tma-text) 12%);
    --aa-line: var(--tma-line);
    --aa-line-soft: var(--tma-line);
    --aa-line-strong: color-mix(in srgb, var(--tma-button) 44%, transparent);
    --aa-text: var(--tma-text);
    --aa-text-muted: var(--tma-hint);
    --aa-text-soft: color-mix(in srgb, var(--tma-hint) 86%, var(--tma-bg) 14%);
    --aa-accent: var(--tma-button);
    --aa-accent-2: var(--tma-link);
    --ac-bg: var(--tma-bg);
    --ac-bg-soft: var(--tma-secondary-bg);
    --ac-surface: var(--tma-surface);
    --ac-surface-2: color-mix(in srgb, var(--tma-surface) 88%, var(--tma-text) 12%);
    --ac-line: var(--tma-line);
    --ac-line-strong: color-mix(in srgb, var(--tma-button) 44%, transparent);
    --ac-text: var(--tma-text);
    --ac-text-muted: var(--tma-hint);
    --ac-text-soft: color-mix(in srgb, var(--tma-hint) 86%, var(--tma-bg) 14%);
    --ac-accent: var(--tma-button);
    --ac-accent-2: var(--tma-link);
    /* Перекрываем токены приложения (--color-*) на тёмную палитру Telegram:
       в браузере они светлые (tokens.css), а внутри Telegram — нативно-тёмные. */
    --color-bg: var(--tma-bg);
    --color-bg-soft: var(--tma-secondary-bg);
    --color-surface: var(--tma-surface);
    --color-surface-2: var(--tma-secondary-bg);
    --color-surface-3: color-mix(in srgb, var(--tma-surface) 86%, var(--tma-text) 14%);
    --color-line: var(--tma-line);
    --color-line-strong: var(--tma-line);
    --color-text: var(--tma-text);
    --color-text-muted: var(--tma-hint);
    --color-text-soft: var(--tma-hint);
    background: var(--tma-bg) !important;
    color: var(--tma-text) !important;
}

.app-shell,
.ff-shell,
.aa-shell,
.ac-shell {
    min-height: 100dvh;
}

.app-topbar,
.ff-topbar,
.aa-topbar,
.ac-topbar {
    position: relative;
    z-index: 8;
}

body.is-telegram .ff-sidebar,
body.is-telegram .aa-sidebar,
body.is-telegram .ac-sidebar,
body.is-telegram .ff-topbar,
body.is-telegram .aa-topbar,
body.is-telegram .ac-topbar,
body.is-telegram .ff-card,
body.is-telegram .aa-card,
body.is-telegram .ac-card,
body.is-telegram .ff-alert,
body.is-telegram .aa-alert,
body.is-telegram .ac-alert,
body.is-telegram .pp-panel,
body.is-telegram .pp-card {
    background: var(--tma-surface) !important;
    background-image: none !important;
    border-color: var(--tma-line) !important;
    color: var(--tma-text) !important;
    box-shadow: none !important;
}

body.is-telegram .ff-btn--primary,
body.is-telegram .aa-btn--primary,
body.is-telegram .ac-btn,
body.is-telegram .app-bottom-tabs__item.is-active {
    background: var(--tma-button) !important;
    border-color: var(--tma-button) !important;
    color: var(--tma-button-text) !important;
}

body.is-telegram .ff-input,
body.is-telegram .ff-select,
body.is-telegram .ff-textarea,
body.is-telegram .aa-input,
body.is-telegram .aa-select,
body.is-telegram .aa-textarea,
body.is-telegram .ac-input,
body.is-telegram .ac-select,
body.is-telegram .ac-textarea,
body.is-telegram .pp-input {
    background: var(--tma-secondary-bg) !important;
    border-color: var(--tma-line) !important;
    color: var(--tma-text) !important;
}

.app-section,
.ff-card,
.aa-card,
.ac-card,
.pp-panel,
.pp-card {
    max-width: 100%;
}

.app-card,
.ff-card,
.aa-card,
.ac-card,
.ff-meta-card,
.aa-meta-card,
.ac-meta-card,
.pp-panel,
.pp-card {
    border-color: var(--tma-line, currentColor);
}

.app-form,
.ff-form,
.aa-form,
.ac-form,
form[data-manual-ops-form] {
    max-width: 100%;
}

.app-table,
.ff-table,
.aa-table,
.ac-table,
.fin-table,
.fid-table,
.pp-table,
table {
    max-width: 100%;
}

.app-modal,
[class*="modal"],
[data-billing-modal],
[data-picker-modal],
[data-spec-modal],
[data-status-gate-modal] {
    z-index: 8000 !important;
}

.app-bottom-tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 6500;
    display: none;
    gap: 6px;
    align-items: stretch;
    padding: 8px 10px calc(8px + var(--tma-safe-bottom));
    border-top: 1px solid var(--tma-line);
    background: color-mix(in srgb, var(--tma-bg) 92%, transparent);
    backdrop-filter: blur(18px);
    overflow-x: auto;
    scrollbar-width: none;
}

.app-bottom-tabs::-webkit-scrollbar {
    display: none;
}

.app-bottom-tabs__item {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 94px;
    min-height: 48px;
    padding: 8px 12px;
    border: 1px solid var(--tma-line);
    border-radius: 14px;
    background: var(--tma-secondary-bg);
    color: var(--tma-hint);
    font-size: 12px;
    font-weight: 800;
    line-height: 1.15;
    text-align: center;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
}

button.app-bottom-tabs__item {
    font: inherit;
}

.app-bottom-tabs__item.is-active {
    background: var(--tma-button);
    border-color: var(--tma-button);
    color: var(--tma-button-text);
}

.app-bottom-tabs__item--danger {
    color: #ff9b9b;
}

.tma-responsive-table {
    width: 100%;
}

.tma-responsive-table-wrap {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.tma-main-action-source {
    position: relative;
}

@media (max-width: 980px) {
    body {
        padding-bottom: calc(var(--tma-bottom-tabs-height) + var(--tma-safe-bottom));
    }

    body[data-tma-role="auth"],
    body[data-tma-role="public"] {
        padding-bottom: var(--tma-safe-bottom);
    }

    body:not([data-tma-role="auth"]):not([data-tma-role="public"]) .app-bottom-tabs {
        display: flex;
    }

    .ff-shell--app,
    .aa-shell--app,
    .ac-shell {
        display: block !important;
    }

    .ff-main,
    .aa-main,
    .ac-main {
        width: 100% !important;
        padding: 12px 12px calc(18px + var(--tma-bottom-tabs-height)) !important;
    }

    .ff-main__inner,
    .aa-main__inner,
    .ac-main__inner {
        max-width: none !important;
        width: 100% !important;
        display: grid;
        gap: 12px;
    }

    .ff-topbar,
    .aa-topbar,
    .ac-topbar {
        display: grid !important;
        grid-template-columns: 1fr !important;
        gap: 10px !important;
        padding: 14px !important;
        border-radius: 16px !important;
    }

    .ff-topbar__actions,
    .aa-topbar__actions,
    .ac-topbar__actions {
        display: flex !important;
        gap: 8px !important;
        width: 100% !important;
        overflow-x: auto;
        padding-bottom: 2px;
    }

    .ff-page-title,
    .aa-page-title,
    .ac-page-title {
        font-size: clamp(24px, 8vw, 34px) !important;
        line-height: 1.02 !important;
    }

    .ff-page-subtitle,
    .aa-page-subtitle,
    .ac-page-subtitle {
        display: none !important;
    }

    .ff-sidebar,
    .aa-sidebar,
    .ac-sidebar {
        width: min(88vw, 340px) !important;
        max-width: 340px !important;
        height: 100dvh !important;
        z-index: 7000 !important;
        padding-bottom: calc(18px + var(--tma-safe-bottom)) !important;
    }

    .ff-card,
    .aa-card,
    .ac-card,
    .pp-panel,
    .pp-card {
        border-radius: 16px !important;
    }

    .ff-card__header,
    .aa-card__header,
    .ac-card__header {
        padding: 14px 14px 0 !important;
    }

    .ff-card__body,
    .aa-card__body,
    .ac-card__body,
    .pp-panel__body,
    .pp-card__body {
        padding: 14px !important;
    }

    .ff-grid-2,
    .ff-grid-3,
    .ff-grid-4,
    .aa-grid-2,
    .aa-grid-3,
    .aa-grid-4,
    .ac-grid-2,
    .ac-grid-3,
    .ac-grid-4,
    .manual-ops-form-grid,
    .manual-ops-summary,
    .aci-form__grid,
    .fnew-manual-grid,
    [class*="grid"] {
        grid-template-columns: 1fr !important;
    }

    .ff-btn,
    .aa-btn,
    .ac-btn,
    .ff-chip,
    .aa-chip,
    .ac-chip {
        min-height: 44px !important;
    }

    .ff-input,
    .ff-select,
    .ff-textarea,
    .aa-input,
    .aa-select,
    .aa-textarea,
    .ac-input,
    .ac-select,
    .ac-textarea,
    .pp-input {
        min-height: 44px !important;
        font-size: 16px !important;
    }

    .ff-smart-select__panel,
    .aa-smart-select__panel.is-floating,
    .ac-smart-select__panel {
        position: fixed !important;
        left: 10px !important;
        right: 10px !important;
        bottom: calc(10px + var(--tma-bottom-tabs-height) + var(--tma-safe-bottom)) !important;
        top: auto !important;
        width: auto !important;
        max-width: none !important;
        max-height: min(58dvh, 460px) !important;
        border-radius: 18px !important;
        z-index: 8500 !important;
    }

    .ff-smart-select__list,
    .aa-smart-select__list,
    .ac-smart-select__list {
        max-height: min(42dvh, 330px) !important;
    }

    .tma-responsive-table thead {
        display: none !important;
    }

    .tma-responsive-table,
    .tma-responsive-table tbody,
    .tma-responsive-table tr,
    .tma-responsive-table td {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
    }

    .tma-responsive-table tr {
        margin: 0 0 10px !important;
        padding: 12px !important;
        border: 1px solid var(--color-line) !important;
        border-radius: 14px !important;
        background: var(--color-surface-2) !important;
    }

    .tma-responsive-table td {
        display: grid !important;
        grid-template-columns: minmax(92px, 0.42fr) minmax(0, 1fr) !important;
        gap: 10px !important;
        padding: 8px 0 !important;
        border: 0 !important;
        text-align: left !important;
        white-space: normal !important;
        overflow-wrap: anywhere !important;
    }

    .tma-responsive-table td::before {
        content: attr(data-label);
        color: var(--color-text-muted);
        font-size: 11px;
        font-weight: 800;
        letter-spacing: 0.08em;
        text-transform: uppercase;
    }

    [class*="modal__dialog"],
    [class*="modal__panel"],
    .ff-modal__panel,
    .aa-modal__panel,
    .ac-modal__panel {
        width: calc(100vw - 20px) !important;
        max-width: none !important;
        max-height: calc(100dvh - 22px - var(--tma-safe-bottom)) !important;
        border-radius: 20px !important;
        overflow: auto !important;
    }
}

@media (min-width: 981px) {
    .app-bottom-tabs {
        display: none !important;
    }

    .ff-main,
    .aa-main,
    .ac-main {
        padding-bottom: 34px;
    }
}
