/* ============================================================================
   kx-tokens.css — the colour names the app's CSS uses, mapped to the active theme.

   docs/agent-reference/ui-standards.md told pages to use names like
   --dxbl-text-color, --dxbl-border, --dxbl-bg, --dxbl-warning … but DevExpress 26.1
   defines NONE of them. Measured 2026-10-13: ~6,000 uses across the app; where a
   page gave no fallback the declaration was dropped (≈880 borders never drawn,
   ≈250 primary-colour backgrounds blank). This file defines them once, from the
   Bootstrap variables every DevExpress and Bootstrap theme declares, so every
   page follows the theme the user picked.

   Pairing rule (why some names are NOT defined here): blazing-dark declares a dark
   page (--bs-body-bg / --bs-body-color) but keeps LIGHT-theme values for
   --bs-secondary-bg, --bs-tertiary-bg, --bs-*-bg-subtle and dark values for
   --bs-*-text-emphasis. So a page colour is only ever mapped to another page
   colour, and a "subtle" background only to its own "emphasis" text — mixing the
   groups gives white-on-pale or dark-on-dark there. Names whose every use already
   has a sensible fallback (gray-100/200, hover/selection backgrounds, shadow,
   secondary button backgrounds, *-50 tints) are left undefined on purpose.

   Also defined: readable TEXT versions of the status colours (--kx-text-*) and the
   label colour for each status FILL (--kx-fill-text-*). Plain --bs-warning /
   --bs-info / --bs-success as text on a white page is 1.6–3:1 — unreadable.
   ========================================================================== */

:root {
    /* Page / surface */
    --dxbl-bg: var(--bs-body-bg);
    --dxbl-body-bg: var(--bs-body-bg);
    --dxbl-surface-bg: var(--bs-body-bg);
    --dxbl-panel-bg: var(--bs-body-bg);
    --dxbl-card-bg: var(--bs-body-bg);
    --dxbl-edit-bg: var(--bs-body-bg);

    /* Text */
    --dxbl-text-color: var(--bs-body-color);
    --dxbl-body-color: var(--bs-body-color);
    --dxbl-text: var(--bs-body-color);
    --dxbl-fg: var(--bs-body-color);
    --dxbl-panel-color: var(--bs-body-color);
    --dxbl-gray-700: var(--bs-body-color);
    --dxbl-gray-800: var(--bs-body-color);
    --dxbl-secondary-text-color: var(--bs-secondary-color);
    --dxbl-text-secondary-color: var(--bs-secondary-color);
    --dxbl-secondary-text: var(--bs-secondary-color);
    --dxbl-text-muted: var(--bs-secondary-color);
    --dxbl-gray-600: var(--bs-secondary-color);

    /* Borders / shape / type */
    --dxbl-border: var(--bs-border-color);
    --dxbl-border-color: var(--bs-border-color);
    --dxbl-input-border-color: var(--bs-border-color);
    --dxbl-gray-300: var(--bs-border-color);
    --dxbl-border-radius: var(--bs-border-radius);
    --dxbl-font-family-monospace: var(--bs-font-monospace);

    /* Status colours (fills, borders, dots). For TEXT use --kx-text-* below. */
    --dxbl-primary: var(--bs-primary);
    --dxbl-success: var(--bs-success);
    --dxbl-info: var(--bs-info);
    --dxbl-warning: var(--bs-warning);
    --dxbl-danger: var(--bs-danger);
    --dxbl-btn-primary-bg: var(--bs-primary);
    --dxbl-btn-success-bg: var(--bs-success);
    --dxbl-btn-info-bg: var(--bs-info);
    --dxbl-btn-warning-bg: var(--bs-warning);
    --dxbl-btn-danger-bg: var(--bs-danger);
    --dxbl-btn-primary-color: var(--kx-fill-text-primary);
    --dxbl-btn-primary-text: var(--kx-fill-text-primary);

    /* Status "subtle" boxes — background, border and text from the same group. */
    --dxbl-success-bg: var(--bs-success-bg-subtle);
    --dxbl-success-color: var(--bs-success-text-emphasis);
    --dxbl-danger-bg: var(--bs-danger-bg-subtle);
    --dxbl-danger-border-color: var(--bs-danger-border-subtle);
    --dxbl-danger-color: var(--bs-danger-text-emphasis);

    /* Readable status TEXT on the page (fallback; see the @supports block below):
       the status colour pulled toward the page's own text colour. */
    --kx-text-primary: color-mix(in srgb, var(--bs-primary) 50%, var(--bs-body-color));
    --kx-text-success: color-mix(in srgb, var(--bs-success) 60%, var(--bs-body-color));
    --kx-text-info: color-mix(in srgb, var(--bs-info) 45%, var(--bs-body-color));
    --kx-text-warning: color-mix(in srgb, var(--bs-warning) 45%, var(--bs-body-color));
    --kx-text-danger: color-mix(in srgb, var(--bs-danger) 50%, var(--bs-body-color));

    /* Lightness window for coloured text and outline-button labels: at most 0.52
       on a light page, at least 0.8 on a dark one (OKLCH lightness); chroma
       capped on dark pages only (0.4 = no cap). */
    --kx-label-l-min: 0;
    --kx-label-l-max: 0.52;
    --kx-label-c-max: 0.4;

    /* Label on a solid status FILL. Fallback: Bootstrap's own choice per colour. */
    --kx-fill-text-primary: var(--bs-white);
    --kx-fill-text-success: var(--bs-white);
    --kx-fill-text-danger: var(--bs-white);
    --kx-fill-text-info: rgb(var(--bs-black-rgb));
    --kx-fill-text-warning: rgb(var(--bs-black-rgb));
}

/* Dark themes = the ones whose page is dark: Bootstrap's data-bs-theme="dark"
   (default-dark) or a dark theme stylesheet loaded (blazing-dark, cyborg, solar,
   superhero — theme-controller.js swaps the <link>, it does not flag these dark). */
:root:is([data-bs-theme="dark"],
         :has(link[href*="blazing-dark"], link[href*="/themes/cyborg/"], link[href*="/themes/solar/"], link[href*="/themes/superhero/"])) {
    --kx-label-l-min: 0.8;
    --kx-label-l-max: 1;
    /* very saturated colours (blazing-dark's red) lose lightness when clipped to
       the screen's gamut, so their intensity is capped too */
    --kx-label-c-max: 0.18;
}

/* Where supported, coloured text keeps the theme's exact hue and only moves its
   lightness into the window above (a colour that already reads is untouched), and
   a fill's label is black or white from the fill itself (OKLCH lightness
   above 0.6 → black) — right in every theme, e.g. office-white's orange primary. */
@supports (color: oklch(from red l c h)) {
    :root {
        --kx-text-primary: oklch(from var(--bs-primary) clamp(var(--kx-label-l-min), l, var(--kx-label-l-max)) min(c, var(--kx-label-c-max)) h);
        --kx-text-success: oklch(from var(--bs-success) clamp(var(--kx-label-l-min), l, var(--kx-label-l-max)) min(c, var(--kx-label-c-max)) h);
        --kx-text-info: oklch(from var(--bs-info) clamp(var(--kx-label-l-min), l, var(--kx-label-l-max)) min(c, var(--kx-label-c-max)) h);
        --kx-text-warning: oklch(from var(--bs-warning) clamp(var(--kx-label-l-min), l, var(--kx-label-l-max)) min(c, var(--kx-label-c-max)) h);
        --kx-text-danger: oklch(from var(--bs-danger) clamp(var(--kx-label-l-min), l, var(--kx-label-l-max)) min(c, var(--kx-label-c-max)) h);
        --kx-fill-text-primary: oklch(from var(--bs-primary) clamp(0, (0.6 - l) * 1000, 1) 0 0);
        --kx-fill-text-success: oklch(from var(--bs-success) clamp(0, (0.6 - l) * 1000, 1) 0 0);
        --kx-fill-text-info: oklch(from var(--bs-info) clamp(0, (0.6 - l) * 1000, 1) 0 0);
        --kx-fill-text-warning: oklch(from var(--bs-warning) clamp(0, (0.6 - l) * 1000, 1) 0 0);
        --kx-fill-text-danger: oklch(from var(--bs-danger) clamp(0, (0.6 - l) * 1000, 1) 0 0);
    }
}

/* Bootstrap colour utilities used in markup (text-warning icons, text-success
   amounts, badge bg-danger …): same readable colours as above. Bootstrap marks its
   own utilities !important, so these must be too. A badge keeps an explicit
   text-* class (e.g. "bg-warning text-dark") when one is given. */
.text-primary { color: var(--kx-text-primary) !important; }
.text-success { color: var(--kx-text-success) !important; }
.text-info    { color: var(--kx-text-info) !important; }
.text-warning { color: var(--kx-text-warning) !important; }
.text-danger  { color: var(--kx-text-danger) !important; }

.badge.bg-primary:not([class*="text-"]) { color: var(--kx-fill-text-primary); }
.badge.bg-success:not([class*="text-"]) { color: var(--kx-fill-text-success); }
.badge.bg-info:not([class*="text-"])    { color: var(--kx-fill-text-info); }
.badge.bg-warning:not([class*="text-"]) { color: var(--kx-fill-text-warning); }
.badge.bg-danger:not([class*="text-"])  { color: var(--kx-fill-text-danger); }
