:root {
    /* Apple-Inspired Modern Design System */
    --bg-primary: #fafafa;
    /* Soft white everywhere */
    --bg-secondary: #ffffff;
    --bg-elevated: #ffffff;

    --text-primary: #1d1d1f;
    --text-secondary: #86868b;
    --text-tertiary: #a1a1a6;

    --accent: #0071e3;
    --accent-hover: #0077ed;

    --separator: rgba(0, 0, 0, 0.08);
    --shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 24px rgba(0, 0, 0, 0.08);

    --radius-sm: 12px;
    --radius-md: 16px;
    --radius-lg: 20px;
    --radius-xl: 24px;

    /* ============================================ */
    /* TELEGRAM VIEWPORT VARIABLES (with fallbacks) */
    /* These are dynamically set by JavaScript from Telegram WebApp API */
    /* ============================================ */
    --tg-viewport-height: 100dvh;
    --tg-viewport-stable-height: 100dvh;
    --tg-safe-area-inset-top: env(safe-area-inset-top, 0px);
    --tg-safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);

    /* Dynamic spacing based on safe areas */
    /* Use max() to ensure minimum spacing even if safe area is 0 */
    --spacing-top: max(24px, var(--tg-safe-area-inset-top));
    --spacing-bottom: max(24px, var(--tg-safe-area-inset-bottom));
}

[data-theme="dark"] {
    --bg-primary: #0f0f0f;
    /* Softer dark gray instead of pure black */
    --bg-secondary: #1d1d1f;
    --bg-elevated: #2d2d2f;

    --text-primary: #f5f5f7;
    --text-secondary: #86868b;
    --text-tertiary: #6e6e73;

    --accent: #0a84ff;
    --accent-hover: #409cff;

    --separator: rgba(255, 255, 255, 0.1);
    --shadow-sm: 0 2px 16px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 32px rgba(0, 0, 0, 0.6);
}

* {
    margin: 0;
    padding: 0;
    /* CRITICAL: Include padding in element size calculations */
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", system-ui, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    /* CRITICAL FIX: Use min-height (not height) to allow content to expand when needed */
    /* With box-sizing: border-box, padding is included in this height */
    min-height: var(--tg-viewport-stable-height, 100dvh);
    display: flex;
    justify-content: center;
    align-items: center;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
    transition: background-color 0.3s ease, color 0.3s ease;

    /* App-like behavior */
    overscroll-behavior-y: none;
    /* Prevent rubber-banding */
    user-select: none;
    /* Prevent text selection */
    -webkit-user-select: none;

    /* Safe Area Insets - use Telegram-provided values with env() fallback */
    /* With box-sizing: border-box, these paddings are INSIDE the height above */
    padding-top: var(--tg-safe-area-inset-top);
    padding-bottom: var(--tg-safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
}


/* Override body background for test/pause screens in dark theme to avoid distraction */
[data-theme="dark"] body:has(#screen-test.active),
[data-theme="dark"] body:has(#screen-pause.active) {
    background-color: #fafafa;
    /* Match test screen background to eliminate distraction */
}

/* Pause screen - match test screen styling */
#screen-pause {
    background-color: #fafafa;
    /* Soft white like test screen */
    border-radius: var(--radius-xl);
    /* Adaptive padding: extra space at top for breathing room */
    padding: calc(var(--spacing-top) + 16px) 24px var(--spacing-bottom) 24px;
    margin: 0 auto;
    position: relative;
}

/* Use dark text on pause screen since background is always light */
#screen-pause h2,
#screen-pause p {
    color: #1d1d1f;
    /* Dark text */
}




.app-container {
    width: 100%;
    max-width: 500px;
    /* CRITICAL FIX: Remove duplicate min-height to prevent double-stacking */
    /* Body already handles viewport height, container just needs to fill it */
    height: 100%;
    /* Dynamic padding that adapts to safe areas */
    padding: var(--spacing-top) 24px var(--spacing-bottom) 24px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

/* Screens */
.screen {
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    opacity: 0;
    transform: scale(0.98);
    transition: opacity 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94),
        transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.screen.active {
    display: flex;
    opacity: 1;
    transform: scale(1);
}

/* Special background for test screen to ensure accurate color perception */
#screen-test {
    background-color: #fafafa;
    /* Soft white - optimal for color accuracy */
    border-radius: var(--radius-xl);
    /* Adaptive padding: extra space at top for breathing room */
    padding: calc(var(--spacing-top) + 16px) 24px var(--spacing-bottom) 24px;
    margin: 0 auto;
    position: relative;
}

/* Use dark text on test screen since background is always light */
#screen-test h2,
#screen-test p {
    color: #1d1d1f;
    /* Dark text */
}

#screen-test .subtitle {
    color: #86868b;
    /* Medium gray for subtitle */
}

/* Force light theme colors for buttons on test screen */
#screen-test .btn-secondary {
    color: #1d1d1f;
    background-color: #ffffff;
    border-color: rgba(0, 0, 0, 0.08);
}

/* Back button styles moved to end of file to match theme-toggle */



/* Typography - Apple Style */
h1 {
    font-family: "SF Pro Display", -apple-system, sans-serif;
    font-size: 40px;
    font-weight: 700;
    line-height: 1.1;
    letter-spacing: -0.02em;
    margin-bottom: 12px;
    color: var(--text-primary);
    text-align: center;
}

h2 {
    font-family: "SF Pro Display", -apple-system, sans-serif;
    font-size: 22px;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -0.01em;
    margin-bottom: 24px;
    text-align: center;
    color: var(--text-primary);
}

h3 {
    font-family: "SF Pro Display", -apple-system, sans-serif;
    font-size: 17px;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -0.01em;
    margin-bottom: 12px;
    color: var(--text-primary);
}

p {
    font-size: 17px;
    font-weight: 400;
    line-height: 1.5;
    color: var(--text-secondary);
    text-align: center;
    margin-bottom: 24px;
}

.subt.history-label {
    font-size: 16px;
    color: var(--text-primary);
    font-weight: 500;
}

/* Analytics Screen */
.period-selector {
    display: flex;
    justify-content: center;
    gap: 2px !important;
    margin-bottom: 24px;
}

.period-selector.compact {
    margin-bottom: 0;
    margin-top: 12px;
    padding-top: 8px;
    border-top: 1px solid var(--separator);
}

.period-btn {
    background: transparent;
    border: 1px solid transparent;
    /* Responsive micro sizing */
    font-size: 0.65rem;
    /* ~10-11px safe minimum */
    padding: 0.25em 0.5em !important;
    /* Narrower horizontal padding */
    margin: 0 !important;
    /* Reset default margins */
    border-radius: 0.8em;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 0 !important;
    height: auto !important;
    line-height: 1.2;
}

.period-btn:hover {
    color: var(--text-primary);
    background: rgba(0, 0, 0, 0.03);
}

.period-btn.active {
    background: var(--bg-primary);
    color: var(--text-primary);
    border-color: var(--separator);
    box-shadow: var(--shadow-sm);
    font-weight: 600;
}

.stats-cards {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    /* Force 3 columns in one row */
    gap: 8px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--bg-secondary);
    border: 1px solid var(--separator);
    border-radius: var(--radius-md);
    padding: 12px 8px;
    /* Reduced padding */
    text-align: center;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.stat-label {
    font-size: 10px;
    /* Smaller label */
    color: var(--text-secondary);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    line-height: 1.2;
}

.stat-value {
    font-size: 18px;
    /* Smaller value */
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.2;
}

.chart-container {
    background: var(--bg-secondary);
    border: 1px solid var(--separator);
    border-radius: var(--radius-lg);
    padding: 24px;
    margin-bottom: 24px;
    box-shadow: var(--shadow-sm);
}

.chart-container h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.chart-legend {
    display: flex;
    gap: 16px;
    justify-content: center;
    margin-bottom: 16px;
    font-size: 12px;
    color: var(--text-secondary);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

.legend-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
}

.legend-dot.green {
    background-color: rgba(52, 199, 89, 0.8);
}

.legend-dot.yellow {
    background-color: rgba(255, 204, 0, 0.8);
}

.legend-dot.red {
    background-color: rgba(255, 59, 48, 0.8);
}

#anxiety-chart {
    width: 100%;
    height: 300px;
    display: block;
}

.subtitle {
    font-size: 15px;
    color: var(--text-tertiary);
    margin-bottom: 24px;
}

.highlight {
    color: var(--accent);
    font-weight: 600;
}

.description {
    font-size: 17px;
    line-height: 1.6;
    color: var(--text-secondary);
    text-align: center;
    margin-bottom: 32px;
    max-width: 420px;
    margin-left: auto;
    margin-right: auto;
}

.text-link {
    color: var(--accent);
    font-size: 17px;
    font-weight: 500;
    text-decoration: underline;
    cursor: pointer;
    transition: opacity 0.2s ease;
    display: inline;
}

.text-link:active {
    opacity: 0.7;
}



/* Info Screen & Long Content */
.scroll-content {
    width: 100%;
    max-width: 600px;
    /* Top padding: safe area + header space */
    padding-top: calc(var(--spacing-top) + 24px);
    /* Bottom padding: safe area + extra space */
    padding-bottom: calc(var(--spacing-bottom) + 40px);
    text-align: left;
}

.scroll-content h2 {
    text-align: left;
    margin-bottom: 24px;
}

.scroll-content h3 {
    margin-top: 32px;
    margin-bottom: 16px;
    text-align: left;
    color: var(--text-primary);
}

.scroll-content p {
    text-align: left;
    margin-bottom: 16px;
    font-size: 16px;
    line-height: 1.6;
}

.scroll-content ul {
    list-style: none;
    padding: 0;
    margin: 0 0 24px 0;
}

.scroll-content li {
    font-size: 16px;
    line-height: 1.6;
    color: var(--text-secondary);
    padding: 4px 0 4px 20px;
    position: relative;
    text-align: left;
}

.scroll-content li::before {
    content: "•";
    position: absolute;
    left: 0;
    color: var(--accent);
    font-weight: 600;
}



/* Premium Buttons */
button:not(.theme-toggle) {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 56px;
    padding: 0 40px;
    border-radius: 980px;
    font-size: 17px;
    font-weight: 500;
    letter-spacing: -0.01em;
    border: none;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    width: 100%;
    max-width: 340px;
    margin: 8px auto;
}

.btn-primary {
    background-color: var(--accent);
    color: #ffffff;
    box-shadow: 0 4px 16px rgba(0, 113, 227, 0.25);
    margin-top: 24px;
}

.btn-primary:active {
    transform: scale(0.97);
    box-shadow: 0 2px 8px rgba(0, 113, 227, 0.2);
}

.btn-secondary {
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--separator);
    box-shadow: var(--shadow-sm);
    margin-bottom: 24px;
}

.btn-secondary:active {
    transform: scale(0.97);
    background-color: var(--bg-elevated);
}

/* Color Grid - Perfectly Centered */
.color-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    width: 100%;
    max-width: 380px;
    margin: 32px auto;
    padding: 0;
}

.color-card {
    aspect-ratio: 1 / 2;
    /* Rectangular: width 1, height 2 */
    border-radius: 22px;
    cursor: pointer;
    transition: all 0.35s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    position: relative;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12),
        0 1px 3px rgba(0, 0, 0, 0.08);
    will-change: transform;
}

.color-card::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.15) 0%, transparent 100%);
    pointer-events: none;
}

.color-card:active {
    transform: scale(0.92);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.color-card.selected {
    transform: scale(0);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.35s cubic-bezier(0.4, 0.0, 1, 1),
        opacity 0.35s ease;
}

/* Elegant Loader */
.loader {
    width: 48px;
    height: 48px;
    border: 3px solid var(--separator);
    border-radius: 50%;
    border-top-color: var(--accent);
    animation: spin 0.8s cubic-bezier(0.4, 0.0, 0.2, 1) infinite;
    margin: 0 auto;
}

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

/* Results - Card Style */
.results-content {
    width: 100%;
    max-width: 600px;
    /* Top padding: safe area + minimal space for back button (reduced from 56px) */
    padding-top: calc(var(--spacing-top) + 16px);
    /* Bottom padding: safe area + extra space */
    padding-bottom: calc(var(--spacing-bottom) + 20px);
}

.result-block {
    background: var(--bg-secondary);
    border: 1px solid var(--separator);
    border-radius: var(--radius-xl);
    padding: 32px;
    margin: 0 auto 20px auto;
    box-shadow: var(--shadow-md);
    width: 100%;
}

.result-block p {
    white-space: pre-wrap;
    word-wrap: break-word;
    margin-bottom: 20px;
    line-height: 1.7;
    color: var(--text-primary);
    font-size: 17px;
}

.result-block p:last-child {
    margin-bottom: 0;
}

/* History - Refined Cards */
.history-list {
    width: 100%;
    max-width: 600px;
    /* Bottom padding: safe area + extra space for comfortable scrolling */
    padding-bottom: calc(var(--spacing-bottom) + 76px);
}

.history-item {
    background: var(--bg-secondary);
    border: 1px solid var(--separator);
    border-radius: var(--radius-lg);
    padding: 24px;
    margin: 0 auto 16px auto;
    cursor: pointer;
    transition: all 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    box-shadow: var(--shadow-sm);
    width: 100%;
}

.history-item:active {
    transform: scale(0.98);
    background-color: var(--bg-elevated);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
}

.history-date {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-tertiary);
    margin-bottom: 6px;
    letter-spacing: 0.01em;
    text-transform: uppercase;
}

.history-label {
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

/* ========================================
   RESPONSIVE DESIGN - MEDIA QUERIES
   ======================================== */

/* Small phones (< 360px) */
@media (max-width: 359px) {
    .app-container {
        padding: 16px;
    }

    h1 {
        font-size: 32px;
    }

    h2 {
        font-size: 20px;
    }

    p,
    .description {
        font-size: 15px;
    }

    .color-grid {
        gap: 12px;
        max-width: 100%;
    }

    .modal-content {
        padding: 24px;
    }

    button:not(.theme-toggle) {
        min-height: 50px;
        font-size: 16px;
    }
}

/* Standard phones (360px - 480px) */
@media (min-width: 360px) and (max-width: 480px) {
    .app-container {
        padding: 20px;
    }

    .color-grid {
        max-width: 100%;
    }
}

/* Tablets and larger (768px+) */
@media (min-width: 768px) {
    .app-container {
        padding: 40px;
    }

    h1 {
        font-size: 48px;
    }

    h2 {
        font-size: 28px;
    }

    .modal-content {
        max-width: 600px;
        padding: 40px;
    }

    .color-grid {
        gap: 20px;
        max-width: 450px;
    }

    button:not(.theme-toggle) {
        min-height: 60px;
        font-size: 18px;
    }
}

/* Large screens (1024px+) */
@media (min-width: 1024px) {
    .app-container {
        max-width: 600px;
    }

    .modal-content {
        max-width: 700px;
    }
}

/* Landscape orientation for phones */
@media (max-height: 600px) and (orientation: landscape) {
    .app-container {
        min-height: auto;
        padding: 20px;
    }

    #screen-test,
    #screen-pause {
        padding: 20px 24px;
    }

    .color-grid {
        margin: 20px auto;
    }

    button:not(.theme-toggle) {
        min-height: 48px;
    }
}



/* Logo Styles */
.logo-container {
    margin-bottom: 24px;
    display: flex;
    justify-content: center;
    width: 100%;
}

.app-logo {
    width: 120px;
    height: 120px;
    border-radius: 24px;
    object-fit: cover;
    box-shadow: var(--shadow-md);
}

/* ============================================ */
/* ACCESS DENIED SCREEN */
/* ============================================ */

#screen-access-denied {
    padding: calc(var(--spacing-top) + 24px) 24px var(--spacing-bottom) 24px;
}

.glass-card {
    background: var(--bg-secondary);
    border: 1px solid var(--separator);
    border-radius: var(--radius-xl);
    padding: 32px 24px;
    box-shadow: var(--shadow-md);
    text-align: center;
}

.glass-card h2 {
    font-size: 24px;
    margin-bottom: 16px;
    color: var(--text-primary);
}

.glass-card p {
    font-size: 16px;
    line-height: 1.5;
    color: var(--text-secondary);
    margin-bottom: 16px;
}

.glass-card .user-info {
    background: var(--bg-elevated);
    border: 1px solid var(--separator);
    border-radius: var(--radius-md);
    padding: 12px 16px;
    margin: 20px 0;
    font-size: 15px;
}

.glass-card code {
    background: var(--accent);
    color: #ffffff;
    padding: 4px 12px;
    border-radius: 8px;
    font-family: 'SF Mono', 'Monaco', monospace;
    font-size: 16px;
    font-weight: 600;
}

.glass-card .description {
    font-size: 14px;
    color: var(--text-tertiary);
    margin-top: 12px;
    margin-bottom: 0;
}

/* Admin Panel Badges */
.stats-badges {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 20px;
}

.badge {
    display: inline-flex;
    align-items: center;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 600;
    box-shadow: var(--shadow-sm);
    line-height: 1;
}

.badge span {
    margin-left: 6px;
    font-weight: 700;
    opacity: 0.9;
}