/**
 * TrackStage Overlay Layers — RC-01.04
 * Consumes --ts-z-* tokens defined in trackstage-pwa.css.
 *
 * Hierarchy:
 *   content 1–100 | sticky 500 | dropdown 1000 | fab 1100 |
 *   popover 1200 | drawer 1500 | modal 2000 | toast 3000 | loading 9999
 */

/* Portal host — always on top of page chrome, below splash/loading */
#ts-overlay-root {
    position: relative;
    z-index: var(--ts-z-modal);
    pointer-events: none;
}

#ts-overlay-root > * {
    pointer-events: auto;
}

/* App shell layering */
.ts-app-shell .ts-shell-bg {
    position: absolute;
    inset: 0;
    z-index: 0;
    pointer-events: none;
}

.ts-app-shell .ts-shell-frame {
    position: relative;
    z-index: var(--ts-z-content);
}

.ts-app-shell .ts-sidebar {
    position: relative;
    z-index: var(--ts-z-content-raised);
}

.ts-app-shell .ts-shell-column {
    position: relative;
    z-index: var(--ts-z-content);
    min-width: 0;
}

/* Sticky header — above main content stacking contexts (cards/transforms) */
.ts-app-shell .ts-topbar {
    position: sticky;
    top: 0;
    z-index: var(--ts-z-sticky);
    /* Keep blur without trapping dropdowns below main content */
    isolation: isolate;
}

/* Support session: keep the banner in flow and scroll the shell beneath it. */
.ts-app-shell.ts-has-support-banner {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
}

.ts-app-shell.ts-has-support-banner [data-ts-support-banner] {
    position: relative;
    z-index: calc(var(--ts-z-sticky) + 20);
    flex: 0 0 auto;
}

.ts-app-shell.ts-has-support-banner > .ts-shell-scroll {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
}

.ts-app-shell .ts-main {
    position: relative;
    z-index: var(--ts-z-content);
}

/* Dropdown / user menu panels (in-flow or portaled) */
.ts-layer-dropdown,
.ts-user-menu-panel,
[data-ts-floating-menu-panel] {
    z-index: var(--ts-z-dropdown) !important;
}

[data-ts-floating-menu-panel] {
    position: absolute;
    right: 0;
    margin-top: 0.5rem;
}

[data-ts-floating-menu-panel][data-ts-ported="1"] {
    position: fixed;
    margin-top: 0;
}

[data-ts-floating-menu-panel][hidden] {
    display: none !important;
}

.ts-user-menu {
    position: relative;
}

.ts-user-menu-panel {
    position: absolute;
    right: 0;
    top: calc(100% + 0.5rem);
    width: 14rem;
    overflow: hidden;
    border-radius: 0.75rem;
    border: 1px solid rgba(51, 65, 85, 0.9);
    background: rgba(10, 15, 28, 0.98);
    padding: 0.375rem 0;
    box-shadow:
        0 20px 40px rgba(0, 0, 0, 0.55),
        0 0 0 1px rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.ts-user-menu-panel[data-ts-ported="1"] {
    position: fixed;
    top: auto;
    right: auto;
}

.ts-user-menu-panel[hidden] {
    display: none !important;
}

/* Autocomplete / combobox results */
.ts-layer-popover,
.ts-autocomplete-list {
    z-index: var(--ts-z-popover) !important;
}

/* Allow search results to escape cards */
.ts-overlay-host {
    position: relative;
    overflow: visible;
}

.ts-card-clip {
    overflow: hidden;
    border-radius: 0.75rem;
}

/* Mobile chrome */
.ts-mobile-bottom-nav {
    z-index: var(--ts-z-sticky) !important;
}

.ts-scan-fab {
    z-index: var(--ts-z-fab) !important;
}

.ts-install-banner {
    z-index: var(--ts-z-popover) !important;
}

.ts-mobile-drawer {
    z-index: var(--ts-z-drawer) !important;
}

/* Modals */
.ts-layer-modal,
.ts-modal-root {
    z-index: var(--ts-z-modal) !important;
}

/* Toast / flash (RC-01.08) */
.ts-layer-toast,
.ts-toast-host {
    z-index: var(--ts-z-toast) !important;
}

.ts-toast-host {
    position: fixed;
    right: 1rem;
    bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
    left: 1rem;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    pointer-events: none;
}

@media (min-width: 640px) {
    .ts-toast-host {
        left: auto;
        width: min(22rem, calc(100vw - 2rem));
        align-items: flex-end;
    }
}

.ts-toast {
    pointer-events: auto;
    border-radius: 0.75rem;
    border: 1px solid rgba(51, 65, 85, 0.9);
    background: rgba(10, 15, 28, 0.96);
    color: #e2e8f0;
    padding: 0.75rem 1rem;
    font-size: 0.875rem;
    line-height: 1.35;
    box-shadow: 0 16px 32px rgba(0, 0, 0, 0.45);
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

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

.ts-toast-success {
    border-color: rgba(34, 197, 94, 0.35);
    color: #86efac;
    background: rgba(6, 78, 59, 0.55);
}

.ts-toast-info {
    border-color: rgba(59, 130, 246, 0.35);
    color: #93c5fd;
    background: rgba(30, 58, 138, 0.45);
}

.ts-toast-warning {
    border-color: rgba(245, 158, 11, 0.4);
    color: #fde68a;
    background: rgba(120, 53, 15, 0.5);
}

.ts-toast-error {
    border-color: rgba(239, 68, 68, 0.4);
    color: #fecaca;
    background: rgba(127, 29, 29, 0.5);
}

/* Full-screen busy overlay for form posts */
.ts-busy-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--ts-z-loading);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    background: rgba(8, 17, 29, 0.72);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.ts-busy-overlay[hidden] {
    display: none !important;
}

.ts-busy-spinner {
    width: 2.25rem;
    height: 2.25rem;
    border-radius: 9999px;
    border: 2px solid rgba(148, 163, 184, 0.25);
    border-top-color: #3b82f6;
    animation: ts-spin 0.7s linear infinite;
}

.ts-busy-label {
    margin: 0;
    font-size: 0.875rem;
    color: #cbd5e1;
}

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

html.ts-busy,
html.ts-busy body {
    cursor: wait;
}

/* Splash / loading stays above everything operational */
#ts-pwa-splash {
    z-index: var(--ts-z-loading) !important;
}

/*
 * Card entrance: do not leave a permanent transform stacking context.
 * `animation-fill-mode: both` + transform: translate3d(0,0,0) trapped overlays.
 */
.ts-card-enter {
    animation: ts-card-in 0.22s ease both;
}

@keyframes ts-card-in {
    from {
        opacity: 0;
        transform: translate3d(0, 10px, 0);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

/* Avoid will-change creating idle stacking contexts on shortcuts */
.ts-mobile-shortcut {
    will-change: auto;
}

.ts-mobile-shortcut:active {
    will-change: transform;
}
