/* ==========================================================================
   ChessPuzzler UX tokens  (puzzle-ux-refresh)
   Spec: see design SPEC.md. Loaded globally from _Layout.cshtml before site.css.

   --t-*   theme-derived colours. _ThemeEditor.cshtml sets the 7 base vars live
           on document.documentElement whenever a theme colour changes; the
           values below are fallbacks (= proposed Classic Theme, id -5).
   --ux-*  theme-independent design tokens (radii, shadow, motion, space, z).

   Rules: style new UI from these vars only - never hard-code Classic colours.
   Text on --t-surface* is always light (--t-on-surface). Text placed directly
   on the page background uses --t-on-bg (auto black/white).
   ========================================================================== */

:root {
    /* ---- Theme base (set live by _ThemeEditor *Change functions) ---- */
    --t-bg: rgb(38, 30, 29);            /* page background            (Background)   */
    --t-dark: rgb(184, 132, 104);       /* dark squares               (Dark Squares) */
    --t-light: rgb(238, 216, 188);      /* light squares              (Light Squares)*/
    --t-accent-1: #006252;              /* "Panels"  - control bar, buttons, panels   */
    --t-accent-2: #62BD9E;              /* "Highlights" - icons, progress, glow, hl    */
    --t-accent-3: #5f3f2f;              /* "Banners" - win/result banner, mate-in tile */
    --t-accent-4: rgb(224, 102, 82);    /* "Moves"   - legal move markers/captures     */

    /* ---- Theme derived ---- */
    --t-bg-lift: color-mix(in oklab, var(--t-bg), white 7%);
    --t-bg-deep: color-mix(in oklab, var(--t-bg), black 40%);

    --t-surface: color-mix(in oklab, var(--t-accent-1) 86%, var(--t-bg));          /* solid panel */
    --t-surface-strong: color-mix(in oklab, var(--t-accent-1), black 28%);          /* pressed/inset, panel headers */
    --t-surface-raised: color-mix(in oklab, var(--t-accent-1), white 9%);           /* hover tiles, cards inside panels */
    --t-surface-glass: color-mix(in srgb, var(--t-surface) 82%, transparent);       /* panels over effects (pair with blur) */
    --t-surface-sunken: color-mix(in srgb, black 22%, transparent);                 /* wells inside a surface */

    --t-glow: color-mix(in srgb, var(--t-accent-2) 55%, transparent);
    --t-glow-soft: color-mix(in srgb, var(--t-accent-2) 22%, transparent);
    --t-glow-faint: color-mix(in srgb, var(--t-accent-2) 10%, transparent);

    --t-board-rim: color-mix(in oklab, var(--t-dark), black 45%);                   /* 1px ring around board */

    --t-hl-select: color-mix(in srgb, var(--t-accent-2) 48%, transparent);          /* selected piece square */
    --t-hl-last: color-mix(in srgb, color-mix(in oklab, var(--t-accent-2), white 30%) 40%, transparent); /* last-move from/to squares (lifted so it glows on dark squares instead of going khaki) */
    --t-move: var(--t-accent-4);                                                    /* legal move dot / capture ring */
    --t-move-dot: color-mix(in srgb, var(--t-accent-4) 72%, transparent);

    --t-good: var(--t-accent-2);
    --t-bad: color-mix(in oklab, #E5484D 88%, var(--t-accent-4));

    /* Text */
    --t-on-surface: #fff;
    --t-on-surface-muted: rgb(255 255 255 / 0.68);
    --t-on-surface-faint: rgb(255 255 255 / 0.42);
    --t-line: rgb(255 255 255 / 0.12);                                              /* hairlines on surfaces */
    --t-line-strong: rgb(255 255 255 / 0.22);
    --t-on-bg: #fff;            /* overridden below with auto contrast where supported */
    --t-on-bg-muted: rgb(255 255 255 / 0.62);
    --t-on-accent-2: #0d1f1a;

    /* ---- Radii ---- */
    --ux-r-xs: 6px;
    --ux-r-sm: 10px;
    --ux-r-md: 14px;
    --ux-r-lg: 20px;
    --ux-r-xl: 28px;
    --ux-r-pill: 999px;
    --ux-r-board: 14px;

    /* ---- Spacing (4px grid) ---- */
    --ux-s-1: 4px;
    --ux-s-2: 8px;
    --ux-s-3: 12px;
    --ux-s-4: 16px;
    --ux-s-5: 24px;
    --ux-s-6: 32px;
    --ux-s-7: 48px;
    --ux-s-8: 64px;

    /* ---- Elevation ---- */
    --ux-shadow-1: 0 1px 2px rgb(0 0 0 / 0.22), 0 2px 6px rgb(0 0 0 / 0.16);
    --ux-shadow-2: 0 2px 4px rgb(0 0 0 / 0.20), 0 10px 24px -4px rgb(0 0 0 / 0.32);
    --ux-shadow-3: 0 4px 8px rgb(0 0 0 / 0.22), 0 24px 48px -12px rgb(0 0 0 / 0.50);
    --ux-inset-top: inset 0 1px 0 rgb(255 255 255 / 0.10);                          /* top sheen on surfaces */
    --ux-shadow-board:
        0 0 0 1px var(--t-board-rim),
        0 2px 3px rgb(0 0 0 / 0.30),
        0 14px 30px -8px rgb(0 0 0 / 0.55),
        0 44px 90px -24px rgb(0 0 0 / 0.65),
        0 0 140px -30px var(--t-glow-soft);

    /* ---- Motion ---- */
    --ux-ease-out: cubic-bezier(0.22, 1, 0.36, 1);        /* default for anything entering / moving */
    --ux-ease-in: cubic-bezier(0.55, 0, 1, 0.45);         /* exits only */
    --ux-ease-in-out: cubic-bezier(0.65, 0, 0.35, 1);     /* position swaps, sheet drag settle */
    --ux-ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);  /* playful overshoot: pops, chips, toggles */
    --ux-ease-emph: cubic-bezier(0.2, 0, 0, 1);           /* panels / sheets */

    --ux-d-instant: 90ms;
    --ux-d-fast: 140ms;
    --ux-d-base: 220ms;
    --ux-d-move: 240ms;     /* piece glide */
    --ux-d-slow: 360ms;
    --ux-d-slower: 520ms;
    --ux-d-celebrate: 900ms;

    /* ---- Z layers (navbar is 10000 in site.css) ---- */
    --ux-z-effects: 0;
    --ux-z-content: 1;
    --ux-z-board-fx: 500;       /* celebration/flash overlays above pieces (100), below dragged piece (1000) */
    --ux-z-panel: 1100;         /* desktop slideouts (theme, stats) */
    --ux-z-sheet-backdrop: 10400;
    --ux-z-sheet: 10500;
    --ux-z-toast: 10600;

    /* ---- Glass ---- */
    --ux-blur: 14px;

    /* ---- Type ---- */
    --ux-font: Hamlin, system-ui, sans-serif;
    --ux-font-bold: HamlinBold, Hamlin, system-ui, sans-serif;
    --ux-fs-xs: 12px;
    --ux-fs-sm: 14px;
    --ux-fs-md: 16px;
    --ux-fs-lg: 20px;
    --ux-fs-xl: 26px;
    --ux-fs-2xl: 32px;
}

/* Auto-contrast text for content sitting directly on the page background
   (e.g. Light Mode theme has a white bg). Black/white chosen by OKLCH lightness. */
@supports (color: oklch(from red l c h)) {
    :root {
        --t-on-bg: oklch(from var(--t-bg) clamp(0.18, calc((0.64 - l) * 1000), 0.98) 0 0);
        --t-on-bg-muted: oklch(from var(--t-bg) clamp(0.18, calc((0.64 - l) * 1000), 0.98) 0 0 / 0.62);
        --t-on-accent-2: oklch(from var(--t-accent-2) clamp(0.16, calc((0.70 - l) * 1000), 0.99) 0.02 h);

        /* Text/hairlines on panels flip to a deep ink tinted with the panel's own hue when the
           panel colour is light (e.g. Light Mode's sky-blue dock), so ANY user theme stays readable.
           Dark panels keep pure white. Threshold ~ OKLab L .60 (the WCAG black/white crossover). */
        --t-on-surface: oklch(from var(--t-surface) clamp(0.2, calc((0.6 - l) * 1000), 1) calc(clamp(0, (l - 0.6) * 1000, 1) * min(c, 0.05)) h);
        --t-on-surface-muted: oklch(from var(--t-surface) clamp(0.2, calc((0.6 - l) * 1000), 1) calc(clamp(0, (l - 0.6) * 1000, 1) * min(c, 0.05)) h / 0.72);
        --t-on-surface-faint: oklch(from var(--t-surface) clamp(0.2, calc((0.6 - l) * 1000), 1) calc(clamp(0, (l - 0.6) * 1000, 1) * min(c, 0.05)) h / 0.46);
        --t-line: oklch(from var(--t-surface) clamp(0.2, calc((0.6 - l) * 1000), 1) 0 0 / 0.12);
        --t-line-strong: oklch(from var(--t-surface) clamp(0.2, calc((0.6 - l) * 1000), 1) 0 0 / 0.22);
    }
}

@media (prefers-reduced-motion: reduce) {
    :root {
        --ux-d-fast: 1ms;
        --ux-d-base: 1ms;
        --ux-d-move: 1ms;
        --ux-d-slow: 1ms;
        --ux-d-slower: 1ms;
        --ux-d-celebrate: 1ms;
    }
}

/* ---- Surface utilities ---- */
.ux-surface {
    background: var(--t-surface);
    color: var(--t-on-surface);
    border-radius: var(--ux-r-lg);
    box-shadow: var(--ux-inset-top), var(--ux-shadow-2);
}

/* Glass: only for panels that sit over background effects. Max ~2 glass panels on screen. */
.ux-glass {
    background: var(--t-surface-glass);
    color: var(--t-on-surface);
    border-radius: var(--ux-r-lg);
    box-shadow: var(--ux-inset-top), 0 0 0 1px var(--t-line), var(--ux-shadow-3);
}

@supports ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
    .ux-glass {
        -webkit-backdrop-filter: blur(var(--ux-blur)) saturate(1.2);
        backdrop-filter: blur(var(--ux-blur)) saturate(1.2);
    }
}

/* ---- Classic Theme background: warm pool of light behind the board + soft vignette.
   Used as Theme.BackgroundClass ("ux-bg-vignette"). Works for any --t-bg. ---- */
body.ux-bg-vignette {
    background-color: var(--t-bg);
}

/* Layers (top to bottom): a fine felt grain (tiny SVG feTurbulence tile, rasterised once, static),
   a warm overhead lamp falling from the top edge, a faint wash of the theme's panel colour low on
   the page, and a vignette into --t-bg-deep. One fixed paint, nothing animates. The pool of light
   that follows the board lives on the puzzle page (puzzler-ux.css, #chess-board-container::before). */
body.ux-bg-vignette::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: -1;
    pointer-events: none;
    background:
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.8' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 .11 -.02'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E") 0 0 / 180px 180px,
        radial-gradient(ellipse 75% 60% at 50% -8%, color-mix(in srgb, var(--t-light) 13%, transparent) 0%, color-mix(in srgb, var(--t-light) 4%, transparent) 45%, transparent 75%),
        radial-gradient(ellipse 90% 45% at 50% 108%, color-mix(in srgb, var(--t-accent-1) 14%, transparent) 0%, transparent 70%),
        radial-gradient(ellipse 115% 90% at 50% 38%, transparent 40%, color-mix(in srgb, var(--t-bg-deep) 95%, transparent) 100%);
}

/* Visually hidden but still in the DOM / accessible (used for the native theme selects kept as JS hooks). */
.ux-visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}
