/* ============================================
   Design Tokens & Variables
   ============================================ */
:root {
    /* Colors - Glassmorphism Palette (single source of truth) */
    --color-text-primary: rgba(255, 255, 255, 0.94);
    --color-text-secondary: rgba(255, 255, 255, 0.78);
    --color-text-muted: rgba(255, 255, 255, 0.6);
    
    /* Accent colors for interactive elements */
    --color-accent-blue: rgba(47, 107, 255, 1);
    --color-accent-cyan: rgba(34, 211, 238, 1);
    
    /* Surface tokens */
    --color-surface-glass-card: rgba(255, 255, 255, 0.08);
    --color-surface-glass-card-hover: rgba(255, 255, 255, 0.12);
    --color-surface-glass-panel: rgba(255, 255, 255, 0.12);
    --color-surface-glass-button: rgba(255, 255, 255, 0.12);
    --color-surface-glass-button-hover: rgba(255, 255, 255, 0.24);
    --color-surface-glass-button-active: rgba(255, 255, 255, 0.28);
    --color-surface-glass-nav: rgba(255, 255, 255, 0.10);

    --color-border-glass-card: rgba(255, 255, 255, 0.15);
    --color-border-glass-panel: rgba(255, 255, 255, 0.20);
    --color-border-glass-nav: rgba(255, 255, 255, 0.18);

    /* Backward-compatible aliases */
    --glass-card-border: var(--color-border-glass-card);
    --glass-panel-bg: var(--color-surface-glass-panel);
    --glass-panel-border: var(--color-border-glass-panel);
    --glass-button-bg: var(--color-surface-glass-button);
    --glass-button-hover-bg: var(--color-surface-glass-button-hover);
    --glass-button-active-bg: var(--color-surface-glass-button-active);
    --glass-nav-bg: var(--color-surface-glass-nav);
    --glass-nav-border: var(--color-border-glass-nav);
    --glass-card-bg: var(--color-surface-glass-card);
    
    /* Blur Levels */
    --blur-card: 24px;
    --blur-panel: 28px;
    --blur-button: 16px;
    --blur-nav: 20px;
    --blur-sm: 12px;
    
    /* Border Radius */
    --radius-card: 24px;
    --radius-button: 999px;
    --radius-action: 18px;
    --radius-input: 16px;
    --radius-nav: 20px;
    
    /* Shadows */
    --shadow-glass: 0 8px 32px rgba(0, 0, 0, 0.12), 
                     0 2px 8px rgba(0, 0, 0, 0.08),
                     inset 0 1px 1px rgba(255, 255, 255, 0.15);
    --shadow-button: 0 4px 16px rgba(0, 0, 0, 0.15);
    --shadow-button-hover: 0 6px 24px rgba(0, 0, 0, 0.20);
    
    /* Spacing Scale */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 12px;
    --space-lg: 16px;
    --space-xl: 20px;
    --space-2xl: 24px;
    
    /* Typography */
    --font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    --font-size-base: 14px;
    --font-size-lg: 16px;
    --font-size-xl: 18px;
    --font-size-sm: 12px;
    --line-height: 1.45;
    
    /* Telegram WebApp viewport height (set by JavaScript) */
    --tg-viewport-height: 100vh;
    --tg-viewport-stable-height: 100vh;
    --app-viewport-height: 100dvh;
    --tg-keyboard-offset: max(0px, calc(var(--tg-viewport-stable-height) - var(--tg-viewport-height)));
    
    /* Safe Area Insets (Phase 3.1) - для iOS notch и gesture areas */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    
    /* Desktop Layout Tokens (Phase 1) */
    --layout-max-width: 1200px;           /* Максимальная ширина контента на desktop */
    --layout-gutter: 32px;                /* Отступы по бокам на desktop */
    --layout-column-gap: 24px;            /* Промежуток между колонками */
    --layout-aside-width: 280px;          /* Ширина боковой колонки */
    --layout-nav-width: 240px;            /* Ширина боковой навигации */
    
    /* Responsive Breakpoints */
    --breakpoint-tablet: 768px;
    --breakpoint-desktop: 1024px;
}

/* ============================================
   CSS Reset & Base Styles
   ============================================ */
*, *::before, *::after {
    box-sizing: border-box;
}

* {
    margin: 0;
    padding: 0;
}

html {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Prevent zoom and resize */
    touch-action: pan-y;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
    /* Lock viewport height */
    height: 100%;
    overflow-x: hidden;
    overflow-y: hidden;
}

body {
    margin: 0;
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    line-height: var(--line-height);
    color: var(--color-text-primary);
    background: #0a0e1a; /* Fallback dark background */
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    overflow: hidden;
    /* Prevent pull-to-refresh and overscroll */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    /* Prevent text selection during gestures */
    -webkit-user-select: none;
    user-select: none;
    /* Disable zoom on iOS */
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

/* ============================================
   Gradient Layers
   ============================================ */
.gradient-backdrop {
    position: fixed;
    inset: 0;
    z-index: 0;
    overflow: hidden;
    pointer-events: none;
}

.gradient-layer {
    position: absolute;
    inset: 0;
    opacity: 0;
    transition: opacity 720ms cubic-bezier(0.4, 0, 0.2, 1);
    will-change: opacity;
    /* Add compositor layer to prevent repaint flickering */
    transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

.gradient-layer.is-active {
    opacity: 1;
}

/* ============================================
   Procedural Noise Filter (Optional Enhancement)
   ============================================ */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    pointer-events: none;
    opacity: 0.08;
    filter: url(#noise);
}

/* Remove old gradient background */
body::after {
    display: none;
}

.app-container::before,
.app-container::after {
    display: none;
}

/* ============================================
   Typography
   ============================================ */
h1, h2, h3, h4, h5, h6 {
    margin: 0;
    font-weight: 600;
    line-height: 1.3;
}

h1 {
    font-size: var(--font-size-xl);
}

h2 {
    font-size: var(--font-size-lg);
    margin-bottom: var(--space-lg);
}

p {
    margin: 0 0 var(--space-md) 0;
}

p:last-child {
    margin-bottom: 0;
}

/* ============================================
   Focus Styles (Accessibility)
   ============================================ */
*:focus-visible {
    outline: 2px solid var(--color-accent-blue);
    outline-offset: 2px;
}

/* ============================================
   App Container
   ============================================ */
.app-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: var(--app-viewport-height);
    max-width: 480px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Prevent pull-to-refresh */
    overscroll-behavior-y: none;
    -webkit-overflow-scrolling: touch;
}

/* Desktop layout: expand max-width and center content */
@media (min-width: 768px) {
    .app-container {
        max-width: var(--layout-max-width);
        /* Phase 2.1: Account for side navigation */
        padding-left: var(--layout-nav-width);
    }
}

/* ============================================
   Main Content Area
   ============================================ */
#content {
    flex: 1;
    /* Phase 3.1: Используем safe-area для всех сторон */
    padding-top: calc(var(--space-xl) + var(--safe-area-top));
    padding-right: calc(var(--space-xl) + var(--safe-area-right));
    padding-left: calc(var(--space-xl) + var(--safe-area-left));
    padding-bottom: calc(72px + var(--safe-area-bottom) + var(--space-xl)); /* Space for bottom nav + safe-area */
    position: relative;
    z-index: 1;
    overflow-y: auto;
    overflow-x: hidden;
    /* Prevent pull-to-refresh on content scroll */
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    /* Enable text selection for content */
    -webkit-user-select: text;
    user-select: text;
}

#content.content--chat-live {
    padding-top: 0;
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
    padding-bottom: 0;
    overflow: hidden;
}

/* Desktop: optimize padding and remove bottom nav space */
@media (min-width: 768px) {
    #content {
        padding: var(--layout-gutter);
        padding-bottom: var(--layout-gutter); /* No bottom nav on desktop */
    }
}

#content.is-transitioning {
    pointer-events: none;
}

#content.is-transitioning > * {
    animation: content-blend-in 520ms cubic-bezier(0.22, 0.61, 0.36, 1) both;
    will-change: opacity, transform, filter;
}

@keyframes content-blend-in {
    0% {
        opacity: 0;
        transform: translateY(10px) scale(0.98);
        filter: blur(6px);
    }
    60% {
        opacity: 1;
        filter: blur(0);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
        filter: blur(0);
    }
}

@media (prefers-reduced-motion: reduce) {
    #content.is-transitioning > * {
        animation: none;
    }
}
