* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #4CAF50;
    --primary-hover: #5cb860;
    --primary-contrast: #ffffff;
    /* Theme-aware token for primary color used as TEXT on light backgrounds.
       Defaults to --primary-color (dark theme); overridden in light theme below
       to a darker green that meets WCAG AA contrast on white. */
    --primary-color-on-light-bg: #2e7d32;
    --primary-text: var(--primary-color);
    --danger-color: #f44336;
    --warning-color: #ff9800;
    --info-color: #2196F3;
    --font-mono: 'JetBrains Mono', 'Cascadia Code', 'Fira Code', 'SF Mono', 'Consolas', 'Liberation Mono', monospace;

    /* ── Lumenworks design tokens (additive; active alongside legacy tokens
           during phased migration). Typography + spatial system for the
           studio-console redesign. Channel colors defined in the theme
           blocks below so they can shift with light/dark mode. ──────── */
    --font-display: 'Big Shoulders Display', 'Orbitron', 'Manrope', sans-serif;
    --font-brand:   'Orbitron', sans-serif;
    --font-body:    'Manrope', 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

    --lux-r-sm: 3px;
    --lux-r-md: 6px;
    --lux-r-lg: 10px;
    --lux-r-xl: 14px;

    /* Hairline + bold dividers — thinner than the legacy 1px --border-color
       to get the "silkscreened panel" feel. */
    --lux-hairline: 1px;
    --lux-rule:     2px;

    /* Spacing scale */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 12px;
    --space-lg: 20px;
    --space-xl: 40px;

    /* ── Card grid sizing ──────────────────────────────────────────────
       Tokens for the auto-fill card grids (devices, displays, dashboard
       targets, integrations, autostart). Defaults reproduce the values
       that were inline before tokenization, so this layer is a no-op
       until the card-mode toggle wires `[data-card-mode=…]` overrides.

         · `*-min`        — minmax() column width for the main module
           cards (devices, displays, dashboard targets/scenes).
         · `*-min-narrow` — column width for slimmer dashboard-module
           rows (integrations, autostart).
         · `*-gap` / `*-gap-narrow` — corresponding row/column gap. */
    --card-grid-min:        380px;
    --card-grid-gap:        14px;
    --card-grid-min-narrow: 320px;
    --card-grid-gap-narrow: 12px;

    /* Capture-template / source-card grids (sources, streams, templates,
       color strips) have their own column proportions so they stay
       distinct from device/target cards. */
    --templates-grid-min:   350px;
    --templates-grid-gap:   20px;

    /* Border radius */
    --radius: 8px;
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-pill: 100px;

    /* Animation timing */
    --duration-fast: 0.15s;
    --duration-normal: 0.25s;
    --duration-slow: 0.4s;
    --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
    --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);

    /* Font weights */
    --weight-normal: 400;
    --weight-medium: 500;
    --weight-semibold: 600;
    --weight-bold: 700;

    /* Z-index layers */
    --z-card-elevated: 10;
    --z-sticky: 100;
    --z-dropdown: 200;
    --z-bulk-toolbar: 1000;
    --z-modal: 2000;
    --z-log-overlay: 2100;
    --z-confirm: 2500;
    --z-command-palette: 3000;
    --z-toast: 3500;
    --z-overlay-spinner: 9999;
    --z-modal-top: 9999;        /* drag clones, top-most modals */
    --z-lightbox: 10000;
    --z-tutorial: 10010;        /* tutorial backdrop / fixed overlay */
    --z-tutorial-tooltip: 10012;
    --z-connection: 10020;      /* connection-lost overlay — MUST be the
                                   highest layer so a dropped connection is
                                   never occluded by a tutorial step. */
}

/* ── SVG icon base ── */
.icon {
    display: inline-block;
    width: 1em;
    height: 1em;
    vertical-align: -0.125em;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex-shrink: 0;
}

/* Dark theme (default) */
[data-theme="dark"] {
    --bg-color: #000000;
    --bg-secondary: #0a0b0d;
    --card-bg: #000000;
    --text-color: #e0e0e0;
    --text-primary: #e0e0e0;
    --text-secondary: #999;
    --text-muted: #777;
    --border-color: #404040;
    --display-badge-bg: rgba(0, 0, 0, 0.4);
    --primary-text-color: #66bb6a;
    --success-color: #28a745;
    --shadow-color: rgba(0, 0, 0, 0.3);
    --hover-bg: rgba(255, 255, 255, 0.05);
    --input-bg: #1a1a2e;
    color-scheme: dark;

    /* ── Lumenworks dark palette — page is pure black, cards elevate ── */
    --lux-bg-0:     #000000;
    --lux-bg-1:     #0e1014;
    --lux-bg-2:     #15181d;
    --lux-bg-3:     #1c2027;
    --lux-line:     #232831;
    --lux-line-bold:#2e3440;
    --lux-ink:      #e6ebf2;
    --lux-ink-dim:  #8b95a5;
    --lux-ink-mute: #5b6473;
    --lux-ink-faint:#3a414c;

    /* Channel palette — consistent across tabs for entity types.
       --ch-signal tracks --primary-color so the accent color picker
       propagates through the brand mark, running stripes, transport
       chip, active tabs, etc. Other channels are fixed hues used for
       non-primary entity types. */
    --ch-signal:  var(--primary-color);
    --ch-signal-dim: var(--primary-text-color, var(--primary-color));
    --ch-cyan:    #00d8ff;  /* data / sources / screen */
    --ch-magenta: #ff4ade;  /* audio / FFT */
    --ch-amber:   #ffb800;  /* autostart / pending */
    --ch-coral:   #ff5e5e;  /* offline / error / alarm */
    --ch-violet:  #8b7eff;  /* graph / scenes / automations */

    --lux-signal-glow: 0 0 14px color-mix(in srgb, var(--ch-signal) 40%, transparent);
    --lux-shadow-rack: 0 1px 0 rgba(255, 255, 255, 0.03), 0 8px 24px rgba(0, 0, 0, 0.5);
}

/* Light theme */
[data-theme="light"] {
    --bg-color: #ffffff;
    --bg-secondary: #fafbfc;
    --card-bg: #ffffff;
    --text-color: #333333;
    --text-primary: #333333;
    --text-secondary: #595959;
    --text-muted: #767676;
    --border-color: #e0e0e0;
    --display-badge-bg: rgba(255, 255, 255, 0.85);
    --primary-text-color: #3d8b40;
    --success-color: #2e7d32;
    --shadow-color: rgba(0, 0, 0, 0.12);
    --hover-bg: rgba(0, 0, 0, 0.05);
    --input-bg: #f0f0f0;
    /* WCAG AA: #4CAF50 fails (~3.2:1) on #ffffff. Darken to #2e7d32 (~5.4:1). */
    --primary-color: #2e7d32;
    --primary-hover: #36913a;
    --primary-color-on-light-bg: #2e7d32;
    --primary-text: #2e7d32;
    color-scheme: light;

    /* ── Lumenworks light palette — page is pure white, cards slightly
         off-white so the stripe + hairline border still read against
         the page. WCAG AA tuned. ── */
    --lux-bg-0:     #ffffff;
    --lux-bg-1:     #f6f8fb;
    --lux-bg-2:     #eef1f5;
    --lux-bg-3:     #e4e8ee;
    --lux-line:     #dee3ea;
    --lux-line-bold:#c4ccd6;
    --lux-ink:      #0f1419;
    --lux-ink-dim:  #4c5866;
    --lux-ink-mute: #6b7684;
    --lux-ink-faint:#a5afbc;

    /* --ch-signal tracks --primary-color so the accent picker propagates. */
    --ch-signal:  var(--primary-color);
    --ch-signal-dim: var(--primary-text-color, var(--primary-color));
    --ch-cyan:    #006b88;
    --ch-magenta: #b01a99;
    --ch-amber:   #a56a00;
    --ch-coral:   #d8392e;
    --ch-violet:  #5b4fd0;

    --lux-signal-glow: 0 0 12px color-mix(in srgb, var(--ch-signal) 28%, transparent);
    --lux-shadow-rack: 0 1px 0 rgba(255, 255, 255, 0.6), 0 6px 18px rgba(0, 0, 0, 0.08);
}

/* Default to dark theme */
body {
    background: var(--bg-color);
    color: var(--text-color);
}

html {
    background: var(--bg-color);
    overflow-x: hidden;
    overflow-y: scroll;
    scroll-behavior: smooth;
    scrollbar-gutter: stable;
}

body {
    font-family: var(--font-body, 'Manrope', 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif);
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.55;
    font-feature-settings: "ss01", "cv11";
    -webkit-font-smoothing: antialiased;
}

html.modal-open {
    overflow: hidden; /* scrollbar-gutter: stable keeps the gutter reserved */
}

/* ── Ambient animated background ── */
#bg-anim-canvas,
#bg-effect-canvas {
    display: none;
    position: fixed;
    inset: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

[data-bg-anim="on"] #bg-anim-canvas {
    display: block;
}

[data-bg-anim="on"] body {
    background: transparent;
}

/* Card backgrounds are intentionally stable across the dynamic-bg
   toggle — the shader bleeds through the page background only.
   (Previously a translucent override let the shader show through
   cards too, but it made the same card look different depending on
   whether the user had the WebGL background enabled.) */
/* Blur behind header via pseudo-element — applying backdrop-filter directly
   to header would create a containing block and break position:fixed on
   the .tab-bar nested inside it (mobile bottom nav). */
[data-bg-anim="on"] header {
    background: transparent;
}
[data-bg-anim="on"] header::after {
    content: '';
    position: absolute;
    inset: 0;
    z-index: -1;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    background: color-mix(in srgb, var(--bg-color) 60%, transparent);
}

/* ── Tab indicator (background watermark) ── */
#tab-indicator {
    position: fixed;
    right: -10vw;
    top: 50%;
    transform: translateY(-50%);
    width: 55vw;
    height: 55vw;
    max-width: 600px;
    max-height: 600px;
    pointer-events: none;
    z-index: -1;
    opacity: 0;
    filter: blur(6px);
    color: var(--primary-color);
    transition: opacity 0.5s ease;
}
#tab-indicator svg {
    width: 100%;
    height: 100%;
    opacity: 0.04;
}
#tab-indicator.tab-indicator-visible {
    opacity: 1;
}
[data-theme="light"] #tab-indicator svg {
    opacity: 0.07;
}

.container {
    padding: 20px;
}

/* ── Smooth theme transitions ── */
body,
.card,
.template-card,
.modal-content,
.dashboard-target,
.perf-chart-card,
header {
    transition: background-color var(--duration-normal) ease, color var(--duration-normal) ease, border-color var(--duration-normal) ease;
}

/* ── Respect reduced motion preference ── */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    html { scroll-behavior: auto; }
}

@media print {
    [data-theme] {
        --bg-color: #fff;
        --card-bg: #fff;
        --text-color: #000;
        --text-secondary: #333;
        --border-color: #ccc;
    }
    .tab-bar, .app-header, .scroll-to-top, .toast-container { display: none; }
}
