// ============================================================
// Design Tokens - CSS Custom Properties
// ------------------------------------------------------------
// Every token is overridable: unlayered `:root { --token: ... }` in your
// own CSS beats these (they live in the `theme` layer). Scales come from
// the Sass maps in abstracts/tokens.
// ------------------------------------------------------------
// Two rules:
// 1. `:root` holds raw values (palette, scales, sizes).
// 2. The "derived" rule holds tokens computed from other tokens (accent
//    tints, glows, transitions). It also matches every element that carries
//    a theming attribute, so `data-theme`, `data-contrast`, `data-motion`
//    re-derive correctly on any subtree, not just <html>. `data-glow` is the
//    hook for a local glow: `<div data-glow style="--glow-intensity: 0.4">`.
// ------------------------------------------------------------
// Colour scheme: Cybercore is dark-only by design (neon on void). There is
// no light palette, and `prefers-color-scheme: light` is ignored on
// purpose: `color-scheme: dark` keeps native controls dark too. Retint with
// data-theme / data-contrast instead.
// ============================================================

@use "sass:list";
@use "sass:map";
@use "../abstracts/tokens";

$derived-scope: ":root, [data-theme], [data-contrast], [data-glow], [data-motion]";

// Box/text shadow glow. Blur radii scale with --glow-intensity
// (0 = off, 1 = default), so one token dims every glow.
@function -glow($color, $layers) {
  $shadows: ();

  @each $size, $alpha in $layers {
    $tint: $color;

    @if $alpha != 100% {
      $tint: color-mix(in srgb, #{$color} #{$alpha}, transparent);
    }

    $shadows: list.append(
      $shadows,
      0 0 calc(#{$size} * var(--glow-intensity)) $tint,
      $separator: comma
    );
  }

  @return $shadows;
}

// A palette step: the hand-tuned anchor when there is one, otherwise the
// hue's 500 mixed toward white or black (see tokens.$shade-mix).
@function -shade($hue, $anchors, $step) {
  @if map.has-key($anchors, $step) {
    @return map.get($anchors, $step);
  }

  $mix: map.get(tokens.$shade-mix, $step);

  @return color-mix(in oklab, var(--cyber-#{$hue}-500) list.nth($mix, 2), #{list.nth($mix, 1)});
}

$glow-box: (20px 100%, 40px 50%);
$glow-text: (10px 100%, 20px 60%);

@layer theme {
  :root {
    // --------------------------------------------------------
    // Palette: neon hues (100-900) and neutrals
    // --------------------------------------------------------

    @each $hue, $anchors in tokens.$palette-anchors {
      @each $step in tokens.$shade-steps {
        --cyber-#{$hue}-#{$step}: #{-shade($hue, $anchors, $step)};
      }
    }

    @each $step, $value in tokens.$void {
      --cyber-void-#{$step}: #{$value};
    }

    @each $step, $value in tokens.$chrome {
      --cyber-chrome-#{$step}: #{$value};
    }

    // --------------------------------------------------------
    // Semantic colours: surfaces, text, borders, status
    // --------------------------------------------------------

    --color-bg-primary: var(--cyber-void-500);
    --color-bg-secondary: var(--cyber-void-400);
    --color-bg-tertiary: var(--cyber-void-300);
    --color-bg-elevated: var(--cyber-void-200);
    --color-bg-default: var(--color-bg-primary);
    --color-bg-inset: var(--cyber-void-700);
    --color-bg-overlay: color-mix(in srgb, var(--cyber-void-900) 80%, transparent);
    --color-text-primary: var(--cyber-chrome-100);
    --color-text-secondary: var(--cyber-chrome-300);
    --color-text-muted: var(--cyber-chrome-500);
    --color-text-placeholder: var(--cyber-chrome-400);
    --color-text-disabled: var(--cyber-chrome-600);
    --color-text-on-accent: var(--cyber-void-900);
    --color-border-default: var(--cyber-void-100);
    --color-border-subtle: var(--cyber-void-200);
    --color-border-strong: var(--cyber-chrome-600);
    --color-success: var(--cyber-green-500);
    --color-warning: var(--cyber-yellow-500);
    --color-danger: var(--cyber-magenta-500);
    --color-info: var(--cyber-cyan-500);
    --color-success-soft: color-mix(in srgb, var(--color-success) 15%, transparent);
    --color-warning-soft: color-mix(in srgb, var(--color-warning) 15%, transparent);
    --color-danger-soft: color-mix(in srgb, var(--color-danger) 15%, transparent);
    --color-info-soft: color-mix(in srgb, var(--color-info) 15%, transparent);

    // Accent anchors. data-theme swaps these three; everything accent-ish
    // (tints, glows, focus ring, links, selection) derives from them.
    --color-accent: var(--cyber-cyan-500);
    --color-accent-light: var(--cyber-cyan-300);
    --color-accent-dark: var(--cyber-cyan-700);
    --color-secondary: var(--cyber-magenta-500);

    // --------------------------------------------------------
    // Typography
    // Fonts are not bundled. Self-host woff2 with font-display: swap
    // (see the font-face mixin in abstracts) or rely on the fallbacks.
    // --------------------------------------------------------

    --font-display: "Rajdhani", "Orbitron", system-ui, sans-serif;
    --font-orbitron: "Orbitron", "Rajdhani", system-ui, sans-serif;
    --font-body: "Exo 2", system-ui, -apple-system, "Segoe UI", roboto, sans-serif;
    --font-mono:
      "JetBrains Mono", ui-monospace, "SFMono-Regular", "Cascadia Code", menlo, consolas, monospace;

    @each $name, $value in tokens.$type-scale {
      --text-#{$name}: #{$value};
    }

    @each $name, $value in tokens.$font-weights {
      --font-weight-#{$name}: #{$value};
    }

    @each $name, $value in tokens.$leading {
      --leading-#{$name}: #{$value};
    }

    @each $name, $value in tokens.$tracking {
      --tracking-#{$name}: #{$value};
    }

    // --------------------------------------------------------
    // Spacing, shape, sizing
    // --------------------------------------------------------

    @each $name, $value in tokens.$spacing {
      --space-#{$name}: #{$value};
    }

    @each $name, $value in tokens.$radii {
      --radius-#{$name}: #{$value};
    }

    --radius-cyber: 0 12px 0 12px;

    @each $name, $value in tokens.$clip-sizes {
      --clip-#{$name}: #{$value};
    }

    --border-thin: 1px;
    --border-medium: 2px;
    --border-thick: 3px;

    @each $name, $value in tokens.$control-heights {
      --control-height-#{$name}: #{$value};
    }

    --focus-ring-width: 2px;
    --focus-ring-offset: 3px;

    // --------------------------------------------------------
    // Stacking
    // --------------------------------------------------------

    @each $name, $value in tokens.$z-index {
      --z-#{$name}: #{$value};
    }

    // --------------------------------------------------------
    // Motion (durations collapse to 0ms under reduced motion)
    // --------------------------------------------------------

    @each $name, $value in tokens.$durations {
      --duration-#{$name}: #{$value};
    }

    @each $name, $value in tokens.$easings {
      --ease-#{$name}: #{$value};
    }

    // --------------------------------------------------------
    // Effects
    // --------------------------------------------------------

    --glow-intensity: 1;
    --noise-opacity: 0.03;
    --scanline-opacity: 0.04;
  }

  #{$derived-scope} {
    // Accent tints
    --color-accent-soft: color-mix(in srgb, var(--color-accent) 15%, transparent);
    --color-accent-muted: color-mix(in srgb, var(--color-accent) 35%, transparent);
    --color-border-accent: var(--color-accent);
    --color-link: var(--color-accent);
    --color-link-hover: var(--color-accent-light);
    --color-selection-bg: color-mix(in srgb, var(--color-accent) 35%, transparent);
    --focus-ring-color: var(--color-accent-light);

    // Glows
    --glow-cyan: #{-glow(
        var(--cyber-cyan-500),
        list.append($glow-box, 80px 25%, $separator: comma)
      )};
    --glow-magenta: #{-glow(var(--cyber-magenta-500), $glow-box)};
    --glow-yellow: #{-glow(var(--cyber-yellow-500), $glow-box)};
    --glow-green: #{-glow(var(--cyber-green-500), $glow-box)};
    --glow-accent: #{-glow(var(--color-accent), $glow-box)};
    --glow-text-cyan: #{-glow(var(--cyber-cyan-500), $glow-text)};
    --glow-text-magenta: #{-glow(var(--cyber-magenta-500), $glow-text)};
    --glow-text-yellow: #{-glow(var(--cyber-yellow-500), $glow-text)};
    --glow-text-green: #{-glow(var(--cyber-green-500), $glow-text)};
    --glow-text-accent: #{-glow(var(--color-accent), $glow-text)};

    // Motion shorthands
    --duration-base: var(--duration-normal);
    --transition-fast: var(--duration-fast) ease;
    --transition-base: var(--duration-normal) ease;
    --transition-slow: var(--duration-slow) ease;
  }
}
