/**
 * PWA + safe-area + мобильный UX (notch, standalone, обновление).
 */

:root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --app-header-height: var(--header-height, 80px);
}

html {
    overflow-x: hidden;
    overflow-y: scroll;
    touch-action: pan-x pan-y;
    -ms-touch-action: pan-x pan-y;
}

body {
    overflow-x: hidden;
    overflow-y: visible;
    max-width: 100vw;
    touch-action: pan-x pan-y;
    -ms-touch-action: pan-x pan-y;
    overscroll-behavior-x: none;
}

/* Фиксированный хедер: safe-area + вертикальное центрирование (не прилипать к верху) */
.header {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    padding-top: var(--safe-top);
    padding-bottom: 0;
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
    height: calc(var(--header-height, 80px) + var(--safe-top));
    min-height: calc(var(--header-height, 80px) + var(--safe-top));
    overflow: visible;
}

.header > .container {
    width: 100%;
    display: flex;
    align-items: center;
}

.header .navbar {
    width: 100%;
    align-items: center;
}

body.has-fixed-header {
    padding-top: calc(var(--header-height, 80px) + var(--safe-top));
}

/* Standalone: как нативное приложение */
html.pwa-standalone body,
body.pwa-standalone {
    overscroll-behavior-y: none;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

html.pwa-standalone input,
html.pwa-standalone textarea,
html.pwa-standalone [contenteditable],
body.pwa-standalone input,
body.pwa-standalone textarea {
    user-select: text;
}

html.pwa-standalone .header {
    background: rgba(255, 255, 255, 0.92);
    overflow: visible;
}

/* PWA кабинет: лого слева, аватар справа */
html.pwa-standalone .header-dashboard .navbar-brand,
body.pwa-standalone .header-dashboard .navbar-brand {
    flex: 1;
    min-width: 0;
}

html.pwa-standalone .dashboard-nav-toggle,
body.pwa-standalone .dashboard-nav-toggle {
    display: none !important;
}

html.pwa-standalone .dashboard-bottom-nav,
body.pwa-standalone .dashboard-bottom-nav {
    padding-bottom: max(0.4rem, env(safe-area-inset-bottom, 0px));
    min-height: 3.5rem;
}

html.pwa-standalone .bottom-nav-item.is-active .bottom-nav-icon,
body.pwa-standalone .bottom-nav-item.is-active .bottom-nav-icon {
    color: var(--primary-600, #0284c7);
}

/* Модалки: dvh + safe-area */
.modal.active {
    padding: max(0.75rem, var(--safe-top)) max(0.75rem, var(--safe-right)) max(0.75rem, var(--safe-bottom)) max(0.75rem, var(--safe-left));
    align-items: flex-start;
    justify-content: center;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal-content {
    max-height: min(90dvh, calc(100dvh - var(--safe-top) - var(--safe-bottom) - 1.5rem));
    max-width: min(440px, calc(100vw - var(--safe-left) - var(--safe-right) - 1.5rem));
    margin-top: max(0.5rem, calc(var(--safe-top) * 0.25));
    margin-bottom: max(0.5rem, var(--safe-bottom));
}

.modal-close {
    top: max(0.75rem, calc(var(--safe-top) * 0.15));
    right: max(0.75rem, var(--safe-right));
}

@media (max-width: 767px) {
    .modal.active {
        padding: max(0.5rem, var(--safe-top)) max(0.5rem, var(--safe-right)) max(0.5rem, var(--safe-bottom)) max(0.5rem, var(--safe-left));
    }

    .modal-content {
        width: 100%;
        max-width: 100%;
        border-radius: var(--radius-lg);
        padding: 1.25rem;
        max-height: calc(100dvh - var(--safe-top) - var(--safe-bottom) - 1rem);
    }
}

/* Плашка обновления PWA */
.pwa-update-banner {
    position: fixed;
    inset: 0;
    z-index: 10050;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 1rem;
    padding-bottom: max(1rem, var(--safe-bottom));
    padding-left: max(1rem, var(--safe-left));
    padding-right: max(1rem, var(--safe-right));
    background: rgba(15, 23, 42, 0.45);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.pwa-update-banner.is-visible {
    opacity: 1;
    pointer-events: auto;
}

.pwa-update-card {
    position: relative;
    width: 100%;
    max-width: 420px;
    background: #fff;
    border-radius: 1rem;
    padding: 1.25rem 1.25rem 1.1rem;
    box-shadow: 0 20px 40px rgba(15, 23, 42, 0.2);
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.85rem;
    transform: translateY(12px);
    transition: transform 0.28s ease;
}

.pwa-update-banner.is-visible .pwa-update-card {
    transform: translateY(0);
}

.pwa-update-icon {
    width: 2.75rem;
    height: 2.75rem;
    border-radius: 0.75rem;
    background: var(--primary-50, #f0f9ff);
    color: var(--primary-600, #0284c7);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.15rem;
}

.pwa-update-title {
    margin: 0 0 0.35rem;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--gray-900, #111827);
}

.pwa-update-text {
    margin: 0 0 0.75rem;
    font-size: 0.9rem;
    line-height: 1.45;
    color: var(--gray-600, #4b5563);
}

.pwa-update-howto {
    margin: 0 0 0.75rem;
    padding: 0.65rem 0.75rem;
    background: var(--gray-50, #f9fafb);
    border-radius: 0.5rem;
    font-size: 0.82rem;
    line-height: 1.4;
    color: var(--gray-700, #374151);
}

.pwa-update-howto p {
    margin: 0 0 0.35rem;
}

.pwa-update-howto p:last-child {
    margin-bottom: 0;
}

.pwa-update-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem;
}

.pwa-update-actions .btn {
    min-height: 2.5rem;
    font-size: 0.875rem;
}

.pwa-update-close {
    position: absolute;
    top: 0.65rem;
    right: 0.65rem;
    width: 2rem;
    height: 2rem;
    border: none;
    background: transparent;
    color: var(--gray-400);
    cursor: pointer;
    border-radius: 0.5rem;
}

.pwa-update-close:hover {
    background: var(--gray-100);
    color: var(--gray-700);
}

.pwa-install-chip {
    position: fixed;
    left: 50%;
    bottom: max(1rem, var(--safe-bottom));
    transform: translateX(-50%) translateY(120%);
    z-index: 900;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.7rem 1.1rem;
    border: none;
    border-radius: 999px;
    background: var(--primary-600, #0284c7);
    color: #fff;
    font-weight: 600;
    font-size: 0.875rem;
    box-shadow: 0 10px 24px rgba(2, 132, 199, 0.35);
    cursor: pointer;
    transition: transform 0.3s ease, opacity 0.3s ease;
    opacity: 0;
}

.pwa-install-chip.is-visible {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

body.dashboard-page .pwa-install-chip {
    bottom: max(5rem, calc(4.5rem + var(--safe-bottom)));
}

@media (min-width: 768px) {
    .pwa-install-chip {
        left: auto;
        right: max(1.25rem, var(--safe-right));
        transform: translateY(120%);
    }
    .pwa-install-chip.is-visible {
        transform: translateY(0);
    }
}
