/* ===== Global App Styles =====
 * Shell-level globals only:
 * CSS variables, document/base styles, header, page shell, bottom nav
 */

/* ===== CSS Variables ===== */
:root {
    color-scheme: dark light;

    /* Surfaces */
    --bg-primary: light-dark(#f2f2f7, #06080c);
    --bg-secondary: light-dark(#e5e5ea, #0d1117);
    --bg-card: light-dark(#fff, #161b22);
    --bg-card-hover: light-dark(#f0f2f5, #1c232d);
    --bg-tertiary: light-dark(#f0f2f5, #1c232d);
    --bg-section: light-dark(#f0f2f5, #0d1117);

    /* Borders */
    --border-subtle: light-dark(rgb(175 184 193 / 80%), rgb(48 54 61 / 80%));
    --border-default: light-dark(#afb8c1, #30363d);
    --border-glow: light-dark(
        color-mix(in srgb, var(--accent-cyan) 20%, transparent),
        color-mix(in srgb, var(--accent-cyan) 30%, transparent)
    );
    --grid-color: light-dark(rgb(175 184 193 / 50%), rgb(48 54 61 / 50%));

    /* Text */
    --text-primary: light-dark(#1f2328, #e6edf3);
    --text-secondary: light-dark(#5c646d, #8b949e);
    --text-muted: light-dark(#6e7781, #848d97);
    --text-tertiary: light-dark(#8b949e, #484f58);

    /* Accents — WCAG AA verified on respective backgrounds */
    --accent-cyan: light-dark(#0369a1, #38bdf8);
    --accent-teal: light-dark(#0f766e, #2dd4bf);
    --accent-green: light-dark(#15803d, #4ade80);
    --accent-amber: light-dark(#8f5b00, #fbbf24);
    --accent-orange: light-dark(#c2410c, #fb923c);
    --accent-orange-dark: light-dark(#9a3412, #ea580c);
    --accent-red: light-dark(#dc2626, #f87171);
    --accent-red-dark: light-dark(#b91c1c, #dc2626);
    --accent-purple: light-dark(#6d28d9, #a78bfa);

    /* --accent-amber and --accent-yellow share the same light value intentionally (dark differs) */
    --accent-yellow: light-dark(#8f5b00, #facc15);
    --accent-indigo: light-dark(#4338ca, #818cf8);

    /* Quality — follow accent values */
    --quality-good: light-dark(#15803d, #4ade80);
    --quality-moderate: light-dark(#EAAA00, #fbbf24);
    --quality-poor: light-dark(#dc2626, #f87171);

    /* MD3 roles (buttons, links, active states, errors) */
    --color-primary: light-dark(#1a60a5, #a4c9ff);
    --color-on-primary: light-dark(#fff, #00315d);
    --color-primary-container: light-dark(#d4e3ff, #004883);
    --color-on-primary-container: light-dark(#001c39, #d4e3ff);
    --color-secondary: light-dark(#3c6a05, #a0d66b);
    --color-on-secondary: light-dark(#fff, #1c3700);
    --color-tertiary: light-dark(#715c00, #ebc315);
    --color-on-tertiary: light-dark(#fff, #3b2f00);
    --color-error: light-dark(#ba1045, #ffb2bb);
    --color-on-error: light-dark(#fff, #670021);
    --color-error-container: light-dark(#ffd9dc, #910032);
    --color-on-error-container: light-dark(#400011, #ffd9dc);

    /* Toast notification colors */
    --toast-success-from: light-dark(#dcfce7, #065f46);
    --toast-success-to: light-dark(#bbf7d0, #047857);
    --toast-success-text: light-dark(#14532d, #d1fae5);
    --toast-error-from: light-dark(#fee2e2, #7f1d1d);
    --toast-error-to: light-dark(#fecaca, #991b1b);
    --toast-error-text: light-dark(#7f1d1d, #fecaca);
    --toast-info-from: light-dark(#dbeafe, #1e3a5f);
    --toast-info-to: light-dark(#bfdbfe, #1e40af);
    --toast-info-text: light-dark(#1e3a5f, #bfdbfe);
    --toast-update-from: light-dark(#ede9fe, #4c1d95);
    --toast-update-to: light-dark(#ddd6fe, #6d28d9);
    --toast-update-text: light-dark(#4c1d95, #ede9fe);
    --toast-warning-from: light-dark(#fef3c7, #78350f);
    --toast-warning-to: light-dark(#fde68a, #92400e);
    --toast-warning-text: light-dark(#78350f, #fef3c7);

    /* Grid line — hidden in light mode (dark-mode aesthetic) */
    --grid-line-color: light-dark(transparent, color-mix(in srgb, #38bdf8 3%, transparent));

    /* Modal backdrop */
    --bg-overlay: light-dark(rgb(0 0 0 / 50%), rgb(0 0 0 / 70%));

    /* Non-color values (shadow type — cannot use light-dark()) */
    --glow-cyan: 0 0 20px color-mix(in srgb, var(--accent-cyan) 30%, transparent);
    --glow-green: 0 0 20px color-mix(in srgb, var(--accent-green) 30%, transparent);
    --glow-amber: 0 0 20px color-mix(in srgb, var(--accent-amber) 30%, transparent);
    --glow-red: 0 0 20px color-mix(in srgb, var(--accent-red) 30%, transparent);
    --shadow-card: none;
    --shadow-dropdown: none;
    --shadow-lg: none;
    --font-display: 'JetBrains Mono', monospace;
    --font-body: 'DM Sans', sans-serif;
}

/* Light theme overrides for non-color values (shadows, glows) */
[data-theme="light"] {
    color-scheme: light;

    --glow-cyan: 0 1px 6px color-mix(in srgb, var(--accent-cyan) 20%, transparent);
    --glow-green: 0 1px 6px color-mix(in srgb, var(--accent-green) 20%, transparent);
    --glow-amber: 0 1px 6px color-mix(in srgb, var(--accent-amber) 20%, transparent);
    --glow-red: 0 1px 6px color-mix(in srgb, var(--accent-red) 20%, transparent);
    --shadow-card: 0 1px 12px rgb(0 0 0 / 10%);
    --shadow-dropdown: 0 4px 12px rgb(0 0 0 / 12%);
    --shadow-lg: 0 10px 25px rgb(0 0 0 / 10%), 0 4px 10px rgb(0 0 0 / 5%);
}

/* Auto light (OS preference, no manual override) */
:root:not([data-theme="dark"], [data-theme="light"]) {
    @media (prefers-color-scheme: light) {
        --glow-cyan: 0 1px 6px color-mix(in srgb, var(--accent-cyan) 20%, transparent);
        --glow-green: 0 1px 6px color-mix(in srgb, var(--accent-green) 20%, transparent);
        --glow-amber: 0 1px 6px color-mix(in srgb, var(--accent-amber) 20%, transparent);
        --glow-red: 0 1px 6px color-mix(in srgb, var(--accent-red) 20%, transparent);
        --shadow-card: 0 1px 12px rgb(0 0 0 / 10%);
        --shadow-dropdown: 0 4px 12px rgb(0 0 0 / 12%);
        --shadow-lg: 0 10px 25px rgb(0 0 0 / 10%), 0 4px 10px rgb(0 0 0 / 5%);
    }
}

[data-theme="dark"] {
    color-scheme: dark;
}

/* ===== Skip Link (Accessibility) ===== */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--accent-cyan);
    color: var(--bg-primary);
    padding: var(--space-2) var(--space-4);
    z-index: 1000;
    border-radius: var(--radius-sm);
    font-weight: var(--font-weight-semibold);
    text-decoration: none;
}

.skip-link:focus {
    top: calc(var(--space-2) + env(safe-area-inset-top, 0px));
    left: var(--space-2);
}

/* ===== Base Styles ===== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/*
 * Viewport height strategy:
 * - Totem uses position: fixed with svh, so it doesn't need html/body sizing
 * - Main app content scrolls naturally, so don't constrain html/body height
 * - Only set min-height to ensure background covers viewport
 */
html {
    min-height: 100%;
}

body {
    font-family: var(--font-body);
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100%;
    line-height: 1.5;
    overflow-x: hidden;
}

body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image:
        linear-gradient(var(--grid-line-color) 1px, transparent 1px),
        linear-gradient(90deg, var(--grid-line-color) 1px, transparent 1px);
    background-size: 50px 50px;
    pointer-events: none;
    z-index: -1;
}

/* ===== Gradient Orbs =====
 * GPU Optimization: blur reduced from 80px to 20px
 * Blur cost scales exponentially with radius - keep under 20px
 * See: https://motion.dev/blog/web-animation-performance-tier-list
 */
.gradient-orb {
    position: fixed;
    border-radius: 50%;
    filter: blur(20px);
    opacity: 0.4;
    pointer-events: none;
    z-index: -1;

    /* No will-change needed - orbs are static (no animation) */
    contain: layout style;
}

.orb-1 {
    width: 400px;
    height: 400px;
    background: radial-gradient(circle, var(--accent-cyan) 0%, transparent 70%);
    top: -100px;
    right: -100px;

    /* Animation disabled by default for GPU performance
     * blur(20px) + continuous animation = expensive
     * Static orbs still provide visual interest without GPU cost */
}

.orb-2 {
    width: 300px;
    height: 300px;
    background: radial-gradient(circle, var(--accent-teal) 0%, transparent 70%);
    bottom: -50px;
    left: -50px;

    /* Animation disabled - see .orb-1 comment */
}

/* Gradient orbs — near-invisible in light mode */
[data-theme="light"] .gradient-orb { opacity: 0.04; }

:root:not([data-theme="dark"], [data-theme="light"]) .gradient-orb {
    @media (prefers-color-scheme: light) { opacity: 0.04; }
}

/* Pause animations when tab is hidden (via Page Visibility API) */
.animations-paused .gradient-orb,
.animations-paused .health-dot {
    animation-play-state: paused !important;
}

/* ===== Header =====
 * GPU Optimization: replaced backdrop-filter with solid background
 * backdrop-filter: blur() causes GPU recompositing on every scroll/repaint
 * See: https://www.f22labs.com/blogs/how-css-properties-affect-website-performance/
 */
.header {
    /* Solid background instead of transparent + backdrop-filter */
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-subtle);
    padding: calc(var(--space-5) + env(safe-area-inset-top, 0px)) var(--space-8) var(--space-5);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;

    /* Removed: backdrop-filter: blur(12px) - high GPU cost */
}

.header-content {
    max-width: clamp(1200px, 92vw, 2200px);
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo {
    display: flex;
    align-items: center;
    gap: var(--space-3);
}

.logo-icon {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    object-fit: contain;
    display: block;
    background: transparent;
    box-shadow: none;
}

.logo-text {
    font-family: var(--font-display);
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-lg);
    letter-spacing: var(--letter-spacing-tight);
}

.logo-text span {
    color: var(--accent-cyan);
}

.header-status {
    display: flex;
    align-items: center;
    gap: var(--space-6);
}

.header-user {
    display: flex;
    align-items: center;
    gap: var(--space-4);
}

.user-info {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--font-size-base);
    color: var(--text-secondary);
}

.user-email {
    font-family: var(--font-display);
}

.user-email-link {
    color: inherit;
    text-decoration: none;
}

.user-email-link:hover {
    text-decoration: underline;
    text-decoration-color: var(--accent-cyan);
    text-underline-offset: 2px;
}

.user-badge {
    font-size: var(--font-size-xs); /* iOS HIG: 11px minimum */
    padding: 0.125rem var(--space-1-5);
    border-radius: var(--radius-full);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    background: color-mix(in srgb, var(--accent-cyan) 15%, transparent);
    color: var(--accent-cyan);
    border: 1px solid color-mix(in srgb, var(--accent-cyan) 30%, transparent);
}

.header-settings-mobile {
    display: none;
}

.header-links {
    display: flex;
    gap: var(--space-2);
}

.header-link {
    min-height: 44px;
    padding: var(--space-1-5) var(--space-3);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: var(--radius-sm);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
}

.header-link:hover {
    background: var(--bg-card);
    color: var(--text-primary);
}

.header-link.active {
    background: var(--bg-card);
    color: var(--accent-cyan);
    box-shadow: inset 0 -2px 0 var(--accent-cyan);
}

.header-link:focus-visible {
    outline: 2px solid var(--accent-cyan);
    outline-offset: 2px;
}

.btn-theme-toggle {
    background: transparent;
    border: 1px solid transparent;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
}

.btn-theme-toggle:hover {
    color: var(--accent-cyan);
}

.btn-theme-toggle svg {
    width: 20px;
    height: 20px;
}

.btn-logout {
    background: var(--bg-secondary);
    border: 1px solid var(--border-subtle);
    color: var(--text-primary);
    cursor: pointer;
}

.btn-logout:hover {
    background: var(--bg-card-hover);
    border-color: var(--border-default);
}

.btn-logout:focus-visible {
    outline: 2px solid var(--accent-cyan);
    outline-offset: 2px;
}

/* ===== System Health Indicator (Admin Only) ===== */
.system-health-indicator {
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
    padding: var(--space-1-5) var(--space-2-5);
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-full);
    text-decoration: none;
    transition: all 0.2s ease;
    cursor: pointer;
}

.system-health-indicator:hover {
    background: var(--bg-card-hover);
    border-color: color-mix(in srgb, var(--accent-cyan) 30%, transparent);
}

.health-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    transition: background 0.3s ease, box-shadow 0.3s ease;
}

/* Consolidated pulse animation using CSS custom properties
 * GPU Optimization: Limited to 3 iterations instead of infinite
 * Animation restarts when state changes (class added/removed)
 * Critical state keeps infinite to ensure visibility
 * Note: box-shadow animation accepted here — element is 8px, limited iterations,
 * and covered by prefers-reduced-motion
 */
@keyframes pulse {
    0%, 100% { box-shadow: 0 0 4px var(--pulse-color); }
    50% { box-shadow: 0 0 12px var(--pulse-color); }
}

.health-dot.healthy {
    --pulse-color: color-mix(in srgb, var(--accent-green) 50%, transparent);

    background: var(--accent-green);
    box-shadow: 0 0 8px color-mix(in srgb, var(--accent-green) 50%, transparent);

    /* Limited iterations - static glow after animation ends */
    animation: pulse 2s ease-in-out 3;
}

.health-dot.warning {
    --pulse-color: color-mix(in srgb, var(--accent-amber) 50%, transparent);

    background: var(--accent-amber);
    box-shadow: 0 0 8px color-mix(in srgb, var(--accent-amber) 50%, transparent);

    /* Warning pulses 5 times to draw more attention */
    animation: pulse 1.5s ease-in-out 5;
}

.health-dot.critical {
    --pulse-color: color-mix(in srgb, var(--accent-red) 50%, transparent);

    background: var(--accent-red);
    box-shadow: 0 0 8px color-mix(in srgb, var(--accent-red) 50%, transparent);

    /* Limited to 10 iterations for GPU performance - static glow remains visible */
    animation: pulse 1s ease-in-out 10;
}

.health-badge {
    font-family: var(--font-display);
    font-size: var(--font-size-xs); /* iOS HIG: 11px minimum */
    font-weight: var(--font-weight-semibold);
    min-width: 16px;
    height: 16px;
    padding: 0 var(--space-1);
    background: var(--accent-red);
    color: var(--color-on-error);
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ===== Main Container ===== */

/* Content offset for fixed header (ID selector for specificity over page styles) */
#pageContent {
    margin-top: calc(4.5rem + env(safe-area-inset-top, 0px));
}

.container {
    max-width: clamp(1200px, 92vw, 2200px);
    margin: 0 auto;
    padding: var(--space-6) var(--space-8) calc(5rem + env(safe-area-inset-bottom, 0px));
}

.spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--border-subtle);
    border-top-color: var(--accent-cyan);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Note: :empty spinner removed - causes layout shift on iOS PWA during SPA navigation.
   Old content stays visible until new page loads (instant swap strategy). */

/* ===== Loading State ===== */
.loading-container {
    display: flex;
    justify-content: center;
    padding-top: 4rem;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border-width: 3px;
}

/* ===== Bottom Navigation ===== */

/* iOS Tab Bar Standards (2024/2025):
   - Total height on iPhone X+: 83px (content + home indicator)
   - Content area: 49pt for icons/labels
   - Home indicator: ~34pt (via safe-area-inset-bottom)
   - Icons: 25-28pt
   - Touch targets: minimum 44x44pt (we use 48x48 for accessibility)
   - Sources: Apple HIG, https://ivomynttinen.com/blog/ios-design-guidelines/
*/

.bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-subtle);
    z-index: 100;

    /* padding-bottom creates space for home indicator, content stays above it */
    padding-bottom: env(safe-area-inset-bottom, 0);
}

.bottom-nav-items {
    display: flex;
    justify-content: space-around;
    align-items: center;

    /* Fixed height for content area - iOS standard is 49pt, we use more for comfort */
    height: 56px;
    max-width: 500px;
    margin: 0 auto;
    padding: 0 8px;
}

.bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);

    /* Ensure 48x48 minimum touch target */
    min-width: 48px;
    min-height: 48px;
    padding: var(--space-1) var(--space-3);
    color: var(--text-muted);
    text-decoration: none;
    border: none;
    background: none;
    cursor: pointer;
    transition: color 0.2s ease;
    font-family: var(--font-body);
    -webkit-tap-highlight-color: transparent;
}

.bottom-nav-item:hover {
    color: var(--text-secondary);
}

.bottom-nav-item.active {
    color: var(--accent-cyan);
}

.bottom-nav-item:focus-visible {
    outline: 2px solid var(--accent-cyan);
    outline-offset: 2px;
}

.bottom-nav-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;  /* M3 spec: 24dp */
    height: 24px;
}

.bottom-nav-icon svg {
    width: 100%;
    height: 100%;
}

.bottom-nav-label {
    font-size: var(--font-size-sm); /* M3 spec: 12sp */
    font-weight: var(--font-weight-medium);
    letter-spacing: 0.02em;
}

/* ===== Focus Scroll Margin (WCAG 2.4.11) ===== */
:target,
:focus {
    scroll-margin-top: calc(5rem + env(safe-area-inset-top, 0px));
}

@media (width <= 768px) {
    :target,
    :focus {
        scroll-margin-bottom: calc(6rem + env(safe-area-inset-bottom, 0px));
    }
}

/* ===== Responsive ===== */
@media (width <= 768px) {
    .header {
        padding: calc(var(--space-3) + env(safe-area-inset-top, 0px)) var(--space-4) var(--space-3);
    }

    .header-links {
        display: none;
    }

    #pageContent {
        margin-top: calc(3.5rem + env(safe-area-inset-top, 0px));
    }

    .container {
        padding: var(--space-4) var(--space-4) calc(6rem + env(safe-area-inset-bottom, 0px));
    }

    .system-health-indicator {
        display: none !important;
    }

    .header-user {
        display: none;
    }

    .header-settings-mobile {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        color: var(--text-secondary);
        text-decoration: none;
        border-radius: var(--radius-sm);
        transition: color 0.2s ease;
    }

    .header-settings-mobile:hover {
        color: var(--accent-cyan);
    }

    .header-settings-mobile:focus-visible {
        outline: 2px solid var(--accent-cyan);
        outline-offset: 2px;
    }

    .header-settings-mobile svg {
        width: 20px;
        height: 20px;
    }

    .bottom-nav {
        display: block;
    }
}

/* ===== Reduced Motion (Accessibility) =====
 * Respects user's OS-level motion preference
 * See: https://web.dev/articles/prefers-reduced-motion
 * WCAG 2.3.3: Animation from Interactions
 */
@media (prefers-reduced-motion: reduce) {
    /* Disable decorative animations */
    .gradient-orb,
    .orb-1,
    .orb-2 {
        animation: none !important;
    }

    /* Health indicators: use transition instead of animation */
    .health-dot.healthy,
    .health-dot.warning,
    .health-dot.critical {
        animation: none !important;

        /* Keep static glow for status indication */
    }

    /* Slow down essential spinners instead of removing */
    .spinner,
    .loading-spinner {
        animation-duration: 2s;
    }

    /* Disable non-essential transitions */
    .header-link,
    .bottom-nav-item,
    .system-health-indicator {
        transition: none !important;
    }

    /* Instant state changes for interactive elements */
    * {
        scroll-behavior: auto !important;
    }
}
