@use 'sass:map';
@use '../../config';
@use '../../functions' as *;
@use '../../schemas/' as *;
@use '../../../utils/map' as *;
@use '../../../color/functions' as *;
@use '../../../elevations/' as *;

////
/// @package theming
/// @group themes
/// @access public
/// @author <a href="https://github.com/simeonoff" target="_blank">Simeon Simeonoff</a>
/// @author <a href="https://github.com/desig9stein" target="_blank">Marin Popov</a>
////

/// Grid Theme
///
/// PRIMARY TOKENS:
/// - `$background` — Controls the overall background color of the grid, including header and content.
/// - `$foreground` — Controls the overall foreground color of the grid, including text and icons.
/// - `$accent-color` — Controls the accent color used for interactive elements in the grid.
///
/// @param {Map} $schema [$light-material-schema] - The schema used as basis for styling the component.
/// @param {Color} $background [null] - The background color of the grid.
/// @param {Color} $foreground [null] - The foreground color of the grid.
/// @param {Color} $accent-color [null] - The accent color used for interactive elements in the grid.
/// @param {Color} $header-background [null] - The table header background color - derives header-text-color, header-border-color, and many more.
/// @param {Color} $header-text-color [null] - The table header text color. Auto-derived from header-background.
/// @param {String} $header-border-width [null] - The border width used for header borders.
/// @param {String} $header-border-style [null] - The border style used for header borders.
/// @param {Color} $header-border-color [null] - The color used for header borders. Auto-derived from header-background.
/// @param {Color} $header-selected-background [null] - The table header background color when selected. Auto-derived from header-background.
/// @param {Color} $header-selected-text-color [null] - The table header text color when selected. Auto-derived from header-selected-background.
/// @param {Color} $sorted-header-icon-color [null] - The sort icon color when sorted. Auto-derived from header-background.
/// @param {color} $sortable-header-icon-hover-color [null] - The icon color on hover when sortable. Auto-derived from sorted-header-icon-color.
/// @param {Color} $content-background [null] - The table body background color - derives content-text-color, row backgrounds, cell backgrounds, borders.
/// @param {Color} $content-text-color [null] - The table body text color. Auto-derived from content-background.
/// @param {Color} $ghost-header-text-color [null] - The dragged header text color. Auto-derived from ghost-header-background.
/// @param {Color} $ghost-header-icon-color [null] - The dragged header icon color. Auto-derived from ghost-header-background.
/// @param {Color} $ghost-header-background [null] - The dragged header background color - derives ghost-header-text-color, ghost-header-icon-color.
/// @param {Color} $row-odd-background [null] - The background color of odd rows. Auto-derived from content-background.
/// @param {Color} $row-even-background [null] - The background color of even rows. Auto-derived from content-background.
/// @param {Color} $row-odd-text-color [null] - The text color of odd rows. Auto-derived from row-odd-background.
/// @param {Color} $row-even-text-color [null] - The text color of even rows. Auto-derived from row-even-background.
/// @param {Color} $row-selected-background [null] - The selected row background color. Auto-derived from content-background.
/// @param {Color} $row-selected-hover-background [null] - The selected row hover background color. Auto-derived from row-selected-background.
/// @param {Color} $row-selected-text-color [null] - The selected row text color. Auto-derived from row-selected-background.
/// @param {Color} $row-selected-hover-text-color [null] - The selected row hover text color. Auto-derived from row-selected-hover-background.
/// @param {Color} $row-hover-background [null] - The hover row background color. Auto-derived from content-background.
/// @param {Color} $row-hover-text-color [null] - The hover row text color. Auto-derived from row-hover-background.
/// @param {Color} $row-border-color [null] - The row bottom border color. Auto-derived from content-background.
/// @param {String} $pinned-border-width [null] - The border width of the pinned border.
/// @param {String} $pinned-border-style [null] - The CSS border style of the pinned border.
/// @param {Color} $pinned-border-color [null] - The color of the pinned border. Auto-derived from content-background.
/// @param {Color} $cell-active-border-color [null] - The border color for the active cell. Auto-derived from content-background.
/// @param {Color} $cell-selected-background [null] - The selected cell background color. Auto-derived from content-background.
/// @param {Color} $cell-selected-text-color [null] - The selected cell text color. Auto-derived from cell-selected-background.
/// @param {Color} $cell-editing-background [null] - The background of the cell being edited. Auto-derived from content-background.
/// @param {Color} $cell-editing-foreground [null] - The cell text color in edit mode. Auto-derived from cell-editing-background.
/// @param {Color} $cell-editing-focus-foreground [null] - The cell text color in edit mode on focus. Auto-derived from cell-editing-background.
/// @param {Color} $cell-edited-value-color [null] - The text color of an edited cell.
/// @param {Color} $cell-new-color [null] - The text color of a new cell.
/// @param {Color} $cell-disabled-color [null] - The text color of a disabled cell.
/// @param {Color} $cell-selected-within-background [null] - The background of selected cell in selected row. Auto-derived from row-selected-background.
/// @param {Color} $cell-selected-within-text-color [null] - The color of selected cell in selected row. Auto-derived from cell-selected-within-background.
/// @param {Color} $edit-mode-color [null] - The color around the row/cell in edit mode. Auto-derived from content-background.
/// @param {Color} $edited-row-indicator [null] - The edited row indicator line color.
/// @param {Color} $resize-line-color [null] - The table header resize line color.
/// @param {Color} $drop-indicator-color [null] - The color of the column drag indicator line.
/// @param {Color} $grouparea-background [null] - The grid group area background color - derives grouparea-color, drop-area-background. Auto-derived from header-background.
/// @param {Color} $grouparea-color [null] - The grid group area color. Auto-derived from grouparea-background.
/// @param {Color} $group-row-background [null] - The grid group row background color - derives expand-icon-color, group-row-selected-background, group-label-text, group-count-background. Auto-derived from header-background.
/// @param {Color} $group-row-selected-background [null] - The group row selected background. Auto-derived from group-row-background.
/// @param {Color} $group-label-column-name-text [null] - The grid group row column name text color.
/// @param {Color} $group-label-icon [null] - The grid group row icon color.
/// @param {Color} $group-label-text [null] - The grid group row text color. Auto-derived from group-row-background or group-row-selected-background.
/// @param {Color} $expand-all-icon-color [null] - The header expand all icon color. Auto-derived from header-background.
/// @param {Color} $expand-all-icon-hover-color [null] - The header expand all icon hover color. Auto-derived from header-background.
/// @param {Color} $expand-icon-color [null] - The grid row expand icon color. Auto-derived from group-row-background or group-row-selected-background.
/// @param {Color} $expand-icon-hover-color [null] - The grid row expand icon hover color. Auto-derived from expand-icon-color.
/// @param {Color} $active-expand-icon-color [null] - The active expand icon color.
/// @param {Color} $active-expand-icon-hover-color [null] - The active expand icon hover color.
/// @param {Color} $group-count-background [null] - The group row count badge background. Auto-derived from group-row-background or group-row-selected-background.
/// @param {Color} $group-count-text-color [null] - The group row count badge text color. Auto-derived from group-count-background.
/// @param {Color} $drop-area-text-color [null] - The drop area text color. Auto-derived from drop-area-background.
/// @param {Color} $drop-area-icon-color [null] - The drop area icon color. Auto-derived from drop-area-background.
/// @param {Color} $drop-area-background [null] - The drop area background color. Auto-derived from grouparea-background.
/// @param {Color} $drop-area-on-drop-background [null] - The drop area background on drop. Auto-derived from drop-area-background.
/// @param {Color} $filtering-header-background [null] - The filtered column header background. Auto-derived from header-background.
/// @param {Color} $filtering-header-text-color [null] - The filtered column header text color. Auto-derived from filtering-header-background.
/// @param {Color} $filtering-row-background [null] - The filtering row background. Auto-derived from header-background.
/// @param {Color} $filtering-row-text-color [null] - The filtering row text color. Auto-derived from filtering-row-background.
/// @param {Color} $filtering-dialog-background [null] - The background color of the advanced filtering dialog.
/// @param {Color} $excel-filtering-header-foreground [null] - The excel filtering header text color. Auto-derived from filtering-row-background.
/// @param {Color} $excel-filtering-subheader-foreground [null] - The excel filtering subheader text color. Auto-derived from filtering-row-background.
/// @param {Color} $excel-filtering-actions-foreground [null] - The excel filtering actions text color. Auto-derived from filtering-row-background.
/// @param {Color} $excel-filtering-actions-hover-foreground [null] - The excel filtering actions hover text color.
/// @param {Color} $excel-filtering-actions-disabled-foreground [null] - The excel filtering actions disabled text color. Auto-derived from filtering-row-background.
/// @param {Color} $excel-filtering-border-color [null] - The border color used in the excel style filter. Auto-derived from foreground and background.
/// @param {Color} $tree-filtered-text-color [null] - Grouping row background color on focus.
/// @param {Color} $summaries-patch-background [null] - The leading summaries patch background.
/// @param {Color} $row-highlight [null] - The grid row highlight indication color.
/// @param {List} $grid-shadow [null] - The shadow of the grid.
/// @param {List} $drag-shadow [null] - The shadow for movable elements.
/// @param {color} $row-ghost-background [null] - The dragged row background color.
/// @param {color} $row-drag-color [null] - The row drag handle color.
/// @param {Color} $grid-border-color [null] - The color of the grid border.
/// @param {List} $drop-area-border-radius [null] - The border radius for column drop area.
/// @param {List} $active-state-border-style [null] - The border style used for row active state and cell active state.
/// @param {Color} $body-column-border-color [null] - The border color used for the body column.
/// @param {Color} $body-column-hover-border-color [null] - The border color used for the body column when in hovered row.
/// @param {Color} $body-column-hover-selected-border-color [null] - The border color used for the body column when in hovered + selected row.
/// @param {Color} $body-column-selected-border-color [null] - The border color used for the body column when in selected row.
/// @requires $light-material-schema
/// @example scss - Change the header background color
///   $my-grid-theme: grid-theme($header-background: black);
///   // Pass the theme to the css-vars() mixin
///   @include css-vars($my-grid-theme);
@function grid-theme(
    $schema: $light-material-schema,

    $background: null,
    $foreground: null,
    $accent-color: null,

    $header-background: null,
    $header-text-color: null,
    $header-border-width: null,
    $header-border-style: null,
    $header-border-color: null,

    $header-selected-background: null,
    $header-selected-text-color: null,

    $sorted-header-icon-color: null,

    $content-background: null,
    $content-text-color: null,

    $ghost-header-text-color: null,
    $ghost-header-icon-color: null,
    $ghost-header-background: null,

    $row-odd-background: null,
    $row-even-background: null,
    $row-odd-text-color: null,
    $row-even-text-color: null,
    $row-selected-background: null,
    $row-selected-hover-background: null,
    $row-selected-text-color: null,
    $row-selected-hover-text-color: null,
    $row-hover-background: null,
    $row-hover-text-color: null,
    $row-border-color: null,

    $pinned-border-width: null,
    $pinned-border-style: null,
    $pinned-border-color: null,

    $cell-active-border-color: null,
    $cell-selected-background: null,
    $cell-selected-text-color: null,
    $cell-editing-background: null,
    $cell-editing-foreground: null,
    $cell-editing-focus-foreground: null,
    $cell-edited-value-color: null,
    $cell-new-color: null,
    $cell-disabled-color: null,

    $edit-mode-color: null,
    $edited-row-indicator: null,

    $resize-line-color: null,
    $drop-indicator-color: null,

    $grouparea-background: null,
    $grouparea-color: null,

    $group-row-background: null,
    $group-row-selected-background: null,
    $group-label-column-name-text: null,
    $group-label-icon: null,
    $group-label-text: null,

    $expand-all-icon-color: null,
    $expand-all-icon-hover-color: null,

    $expand-icon-color: null,
    $expand-icon-hover-color: null,

    $active-expand-icon-color: null,
    $active-expand-icon-hover-color: null,

    $group-count-background: null,
    $group-count-text-color: null,

    $drop-area-text-color: null,
    $drop-area-icon-color: null,
    $drop-area-background: null,
    $drop-area-on-drop-background: null,

    $filtering-header-background: null,
    $filtering-header-text-color: null,
    $filtering-row-background: null,
    $filtering-row-text-color: null,
    $filtering-dialog-background: null,
    $tree-filtered-text-color: null,
    $tree-selected-filtered-row-text-color: null,
    $tree-selected-filtered-cell-text-color: null,

    $excel-filtering-header-foreground: null,
    $excel-filtering-subheader-foreground: null,
    $excel-filtering-actions-foreground: null,
    $excel-filtering-actions-icon-color: null,
    $excel-filtering-actions-hover-foreground: null,
    $excel-filtering-actions-hover-icon-color: null,
    $excel-filtering-actions-selected-icon-color: null,
    $excel-filtering-actions-hover-background: null,
    $excel-filtering-actions-disabled-foreground: null,
    $excel-filtering-border-color: null,

    $row-highlight: null,
    $grid-shadow: null,
    $drag-shadow: null,
    $row-ghost-background: null,
    $row-ghost-border-color: null,
    $row-drag-color: null,
    $drop-area-border-radius: null,
    $grid-border-color: null,
    $sortable-header-icon-hover-color: null,
    $summaries-patch-background: null,

    $cell-selected-within-background: null,
    $cell-selected-within-text-color: null,

    $body-column-border-color-odd: null,
    $body-column-border-color-even: null,
    $body-column-hover-border-color: null,
    $body-column-hover-selected-border-color: null,
    $body-column-selected-border-color: null,
    $active-state-border-style: null
) {
    $selector: (
        #{config.element-prefix() + '-' + 'grid'},
        #{config.element-prefix() + '-' + 'hierarchical-grid'},
        #{config.element-prefix() + '-' + 'tree-grid'},
        #{config.element-prefix() + '-' + 'pivot-grid'},
        #{config.element-prefix() + '-' + 'advanced-filtering-dialog'},
        #{config.element-prefix() + '-' + 'grid-excel-style-filtering'},
        '.igx-excel-filter__secondary'
    );
    $grid-schema: ();

    @if map.has-key($schema, 'grid') {
        $grid-schema: map.get($schema, 'grid');
    } @else {
        $grid-schema: $schema;
    }

    $theme: digest-schema($grid-schema);
    $variant: map.get($theme, '_meta', 'theme');

    @if not($ghost-header-icon-color) and $ghost-header-background {
        $ghost-header-icon-color: hsl(from adaptive-contrast(var(--ghost-header-background)) h s l / 0.07);
    }

    @if not($ghost-header-text-color) and $ghost-header-background {
        $ghost-header-text-color: adaptive-contrast(var(--ghost-header-background));
    }

    @if not($header-text-color) and $header-background {
        $header-text-color: adaptive-contrast(var(--header-background));
    }

    @if not($header-selected-background) and $header-background {
        $header-selected-background: dynamic-shade(var(--header-background));
    }

    @if not($header-selected-text-color) and $header-selected-background {
        $header-selected-text-color: adaptive-contrast(var(--header-selected-background));
    }

    @if not($header-border-color) and $header-background {
        $header-border-color: hsl(from adaptive-contrast(var(--header-background)) h s l / 0.24);
    }

    @if not($content-text-color) and $content-background {
        $content-text-color: adaptive-contrast(var(--content-background));
    }

    @if not($row-odd-background) and $content-background {
        $row-odd-background: hsl(from var(--content-background) h calc(s - 10) l);
    }

    @if not($row-odd-text-color) and $row-odd-background {
        $row-odd-text-color: adaptive-contrast(var(--row-odd-background));
    }

    @if not($row-even-background) and $content-background {
        $row-even-background: hsl(from var(--content-background) h calc(s + 10) l);
    }

    @if not($row-even-text-color) and $row-even-background {
        $row-even-text-color: adaptive-contrast(var(--row-even-background));
    }

    @if not($row-hover-background) and $content-background {
        $row-hover-background: dynamic-shade(var(--content-background), $offset: 7);
    }

    @if not($row-hover-text-color) and $row-hover-background {
        $row-hover-text-color: adaptive-contrast(var(--row-hover-background));
    }

    @if not($cell-selected-background) and $content-background {
        $cell-selected-background: dynamic-shade(var(--content-background));
    }

    @if not($cell-selected-text-color) and $cell-selected-background {
        $cell-selected-text-color: adaptive-contrast(var(--cell-selected-background));
    }

    @if not($row-selected-background) and $content-background {
        $row-selected-background: dynamic-shade(var(--content-background));
    }

    @if not($row-selected-text-color) and $row-selected-background {
        $row-selected-text-color: adaptive-contrast(var(--row-selected-background));
    }

    @if not($row-selected-hover-background) and $row-selected-background {
        $row-selected-hover-background: dynamic-shade(var(--row-selected-background), $offset: 7);
    }

    @if not($row-selected-hover-text-color) and $row-selected-hover-background {
        $row-selected-hover-text-color: adaptive-contrast(var(--row-selected-hover-background));
    }

    @if not($row-border-color) and $content-background {
        $row-border-color: hsl(from adaptive-contrast(var(--content-background)) h s l / 0.08);
    }

    @if not($cell-selected-within-background) and $row-selected-background {
        $cell-selected-within-background: dynamic-shade(var(--row-selected-background), $offset: 7);
    }

    @if not($cell-selected-within-text-color) and $cell-selected-within-background {
        $cell-selected-within-text-color: adaptive-contrast(var(--cell-selected-within-background));
    }

    @if not($cell-editing-background) and $content-background {
        $cell-editing-background: var(--content-background);
    }

    @if not($cell-editing-foreground) and $cell-editing-background {
        $cell-editing-foreground: adaptive-contrast(var(--cell-editing-background));
    }

    @if not($cell-editing-focus-foreground) and $cell-editing-background {
        $cell-editing-focus-foreground: adaptive-contrast(var(--cell-editing-background));
    }

    @if not($cell-active-border-color) and $content-background {
        $cell-active-border-color: hsl(from adaptive-contrast(var(--content-background)) h s l / 0.5);
    }

    @if not($pinned-border-color) and $content-background {
        $pinned-border-color: hsl(from adaptive-contrast(var(--content-background)) h s l / 0.08);
    }

    @if not($group-row-background) and $header-background {
        $group-row-background: $header-background;
    }

    @if not($expand-icon-color) and $group-row-background {
        $expand-icon-color: adaptive-contrast(var(--group-row-background));
    }

    @if not($group-row-selected-background) and $group-row-background {
        $group-row-selected-background: dynamic-shade(var(--group-row-background), $offset: 10);
    }

    @if not($group-label-text) and $group-row-selected-background {
        $group-label-text: adaptive-contrast(var(--group-row-selected-background));
    }

    @if not($expand-icon-color) and $group-row-selected-background {
        $expand-icon-color: adaptive-contrast(var(--group-row-selected-background));
    }

    @if not($expand-icon-hover-color) and $expand-icon-color {
        $expand-icon-hover-color: var(--expand-icon-color);
    }

    @if not($group-count-background) and $group-row-selected-background {
        $group-count-background: adaptive-contrast(var(--group-row-selected-background));
    }

    @if not($expand-all-icon-color) and $header-background {
        $expand-all-icon-color: hsl(from adaptive-contrast(var(--header-background)) h s l / 0.87);
    }

    @if not($expand-all-icon-hover-color) and $header-background {
        $expand-all-icon-hover-color: adaptive-contrast(var(--header-background));
    }

    @if not($sorted-header-icon-color) and $header-background {
        $sorted-header-icon-color: hsl(from adaptive-contrast(var(--header-background)) h s l / 0.8);
    }

    @if not($sortable-header-icon-hover-color) and $sorted-header-icon-color {
        $sortable-header-icon-hover-color: hsl(from var(--sorted-header-icon-color) h s l / 1);
    }

    @if not($edit-mode-color) and $content-background {
        $edit-mode-color: hsl(from adaptive-contrast(var(--content-background)) h s l / 0.5);
    }

    @if not($group-label-text) and $group-row-background {
        $group-label-text: adaptive-contrast(var(--group-row-background));
    }

    @if not($group-count-background) and $group-row-background {
        $group-count-background: adaptive-contrast(var(--group-row-background));
    }

    @if not($group-count-text-color) and $group-count-background {
        $group-count-text-color: adaptive-contrast(var(--group-count-background));
    }

    @if not($grouparea-background) and $header-background {
        $grouparea-background: var(--header-background);
    }

    @if not($grouparea-color) and $grouparea-background {
        $grouparea-color: hsl(from adaptive-contrast(var(--grouparea-background)) h s l / 0.8);
    }

    @if not($grouparea-color) and $header-background {
        $grouparea-color: hsl(from adaptive-contrast(var(--header-background)) h s l / 0.8);
    }

    @if not($drop-area-background) and $grouparea-background {
        $drop-area-background: dynamic-shade(var(--grouparea-background));
    }

    @if not($drop-area-on-drop-background) and $drop-area-background {
        $drop-area-on-drop-background: $drop-area-background;
    }

    @if not($drop-area-text-color) and $drop-area-background {
        $drop-area-text-color: adaptive-contrast(var(--drop-area-background));
    }

    @if not($drop-area-icon-color) and $drop-area-background {
        $drop-area-icon-color: adaptive-contrast(var(--drop-area-background));
    }

    @if not($filtering-header-background) and $header-background {
        $filtering-header-background: color-mix(in hsl, var(--header-background), #fff 5%);
    }

    @if not($filtering-header-text-color) and $filtering-header-background {
        $filtering-header-text-color: adaptive-contrast(var(--filtering-header-background));
    }

    @if not($filtering-row-background) and $header-background {
        $filtering-row-background: color-mix(in hsl, var(--header-background), #fff 5%);
    }

    @if not($filtering-row-text-color) and $filtering-row-background {
        $filtering-row-text-color: adaptive-contrast(var(--filtering-row-background));
    }

    @if not($excel-filtering-header-foreground) and $filtering-row-background {
        $excel-filtering-header-foreground: adaptive-contrast(var(--filtering-row-background));
    }

    @if not($excel-filtering-subheader-foreground) and $filtering-row-background {
        $excel-filtering-subheader-foreground: adaptive-contrast(var(--filtering-row-background));
    }

    @if not($excel-filtering-actions-foreground) and $filtering-row-background {
        $excel-filtering-actions-foreground: hsl(from adaptive-contrast(var(--filtering-row-background)) h s l / 0.8);
    }

    @if not($excel-filtering-actions-disabled-foreground) and $filtering-row-background {
        $excel-filtering-actions-disabled-foreground: hsl(
            from adaptive-contrast(var(--filtering-row-background)) h s l / 0.5
        );
    }

    @if not($grid-shadow) {
        $grid-elevation: map.get($grid-schema, 'grid-elevation');
        $grid-shadow: elevation($grid-elevation);
    }

    @if not($drag-shadow) {
        $drag-elevation: map.get($grid-schema, 'drag-elevation');
        $drag-shadow: elevation($drag-elevation);
    }

    @if not($drop-area-border-radius) {
        $drop-area-border-radius: map.get($theme, 'drop-area-border-radius');
    }

    @if $row-odd-background and not($body-column-border-color-odd) {
        $body-column-border-color-odd: $row-odd-background;
    }

    @if $row-even-background and not($body-column-border-color-even) {
        $body-column-border-color-even: $row-even-background;
    }

    @if $row-hover-background and not($body-column-hover-border-color) {
        $body-column-hover-border-color: $row-hover-background;
    }

    @if $row-selected-background and not($body-column-hover-selected-border-color) {
        $body-column-hover-selected-border-color: $row-selected-background;
    }

    @if $row-selected-hover-background and not($body-column-selected-border-color) {
        $body-column-selected-border-color: $row-selected-hover-background;
    }

    // Start of Theme Builder Logic

    @if not($foreground) and $background {
        $foreground: adaptive-contrast(var(--background));
    }

    @if $variant == 'fluent' {
        @if not($grid-border-color) and $background and $foreground {
            $grid-border-color: color-mix(in srgb, var(--foreground) 12%, var(--background));
        }
    }

    // header

    @if not($header-background) and $foreground and $background {
        $header-background: color-mix(in srgb, var(--foreground) 6%, var(--background));
    }

    @if not($header-text-color) and $foreground {
        $header-text-color: var(--foreground);
    }

    @if not($header-selected-background) and $accent-color and $background {
        $header-selected-background: color-mix(in srgb, var(--accent-color) 12%, var(--background));
    }

    @if not($header-selected-text-color) and $foreground {
        $header-selected-text-color: var(--foreground);
    }

    @if not($ghost-header-background) and $header-background {
        $ghost-header-background: var(--header-background);
    }

    @if not($ghost-header-text-color) and $foreground {
        $ghost-header-text-color: var(--foreground);
    }

    @if not($ghost-header-icon-color) and $foreground {
        $ghost-header-icon-color: var(--foreground);
    }

    @if not($header-border-color) and $foreground and $background {
        $header-border-color: hsl(from color-mix(in srgb, var(--foreground) 20%, var(--background)) h s l / 0.38);
    }

    @if not($sorted-header-icon-color) and $accent-color {
        $sorted-header-icon-color: hsl(from var(--accent-color) h s l / 0.7);
    }

    @if not($sortable-header-icon-hover-color) and $accent-color {
        $sortable-header-icon-hover-color: var(--accent-color);
    }

    // row and content

    @if not($content-background) and $background {
        $content-background: var(--background);
    }

    @if not($content-text-color) and $foreground {
        $content-text-color: var(--foreground);
    }

    @if not($row-odd-background) and $background {
        $row-odd-background: var(--background);
    }

    @if not($row-hover-background) and $accent-color and $background {
        $row-hover-background: color-mix(in srgb, var(--accent-color) 8%, var(--background));
    }

    @if not($row-selected-background) and $accent-color and $background {
        $row-selected-background: color-mix(in srgb, var(--accent-color) 12%, var(--background));
    }

    @if not($row-selected-hover-background) and $accent-color and $background {
        $row-selected-hover-background: color-mix(in srgb, var(--accent-color) 16%, var(--background));
    }

    @if not($row-even-background) and $background {
        $row-even-background: var(--background);
    }

    @if not($row-odd-text-color) and $foreground {
        $row-odd-text-color: var(--foreground);
    }

    @if not($row-even-text-color) and $foreground {
        $row-even-text-color: var(--foreground);
    }

    @if not($row-hover-text-color) and $foreground {
        $row-hover-text-color: var(--foreground);
    }

    @if not($row-selected-text-color) and $foreground {
        $row-selected-text-color: var(--foreground);
    }

    @if not($row-selected-hover-text-color) and $foreground {
        $row-selected-hover-text-color: var(--foreground);
    }

    @if not($row-border-color) and $foreground and $background {
        $row-border-color: hsl(from color-mix(in srgb, var(--foreground) 16%, var(--background)) h s l / 0.38);
    }

    @if not($pinned-border-color) and $foreground and $background {
        $pinned-border-color: color-mix(in srgb, var(--foreground) 20%, var(--background));
    }

    @if not($edit-mode-color) and $accent-color {
        $edit-mode-color: var(--accent-color);
    }

    @if not($edited-row-indicator) and $foreground and $background {
        $edited-row-indicator: color-mix(in srgb, var(--foreground) 16%, var(--background));
    }

    @if not($row-highlight) and $accent-color {
        $row-highlight: var(--accent-color);
    }

    @if not($row-ghost-background) and $background {
        $row-ghost-background: var(--background);
    }

    @if not($row-ghost-border-color) and $foreground and $background {
        $row-ghost-border-color: hsl(from color-mix(in srgb, var(--foreground) 16%, var(--background)) h s l / 0.38);
    }

    @if not($row-drag-color) and $foreground {
        $row-drag-color: var(--foreground);
    }

    // cell

    @if not($cell-selected-background) and $accent-color and $background {
        $cell-selected-background: color-mix(in srgb, var(--accent-color) 24%, var(--background));
    }

    @if not($cell-selected-text-color) and $foreground {
        $cell-selected-text-color: var(--foreground);
    }

    @if not($cell-editing-background) and $background {
        $cell-editing-background: var(--background);
    }

    @if not($cell-editing-foreground) and $foreground {
        $cell-editing-foreground: var(--foreground);
    }

    @if not($cell-editing-focus-foreground) and $foreground {
        $cell-editing-focus-foreground: var(--foreground);
    }

    @if not($cell-edited-value-color) and $foreground and $background {
        $cell-edited-value-color: color-mix(in srgb, var(--foreground) 80%, var(--background));
    }

    @if not($cell-new-color) and $foreground {
        $cell-new-color: var(--foreground);
    }

    @if not($cell-active-border-color) and $accent-color {
        $cell-active-border-color: var(--accent-color);
    }

    @if not($cell-selected-within-background) and $accent-color and $background {
        $cell-selected-within-background: color-mix(in srgb, var(--accent-color) 24%, var(--background));
    }

    @if not($cell-selected-within-text-color) and $foreground {
        $cell-selected-within-text-color: var(--foreground);
    }

    @if not($cell-disabled-color) and $foreground and $background {
        $cell-disabled-color: color-mix(in srgb, var(--foreground) 50%, var(--background));
    }

    // filtering

    @if not($filtering-dialog-background) and $background {
        $filtering-dialog-background: var(--background);
    }

    @if not($filtering-header-background) and $background {
        $filtering-header-background: var(--background);
    }

    @if not($filtering-header-text-color) and $foreground {
        $filtering-header-text-color: var(--foreground);
    }

    @if not($filtering-row-background) and $background {
        $filtering-row-background: var(--background);
    }

    @if not($filtering-row-text-color) and $foreground {
        $filtering-row-text-color: var(--foreground);
    }

    // excel filtering

    @if not($excel-filtering-header-foreground) and $foreground {
        $excel-filtering-header-foreground: var(--foreground);
    }

    @if not($excel-filtering-subheader-foreground) and $foreground {
        $excel-filtering-subheader-foreground: var(--foreground);
    }

    @if not($excel-filtering-actions-foreground) and $foreground {
        $excel-filtering-actions-foreground: var(--foreground);
    }

    @if not($excel-filtering-actions-icon-color) and $foreground {
        $excel-filtering-actions-icon-color: var(--foreground);
    }

    @if not($excel-filtering-actions-hover-foreground) and $foreground {
        $excel-filtering-actions-hover-foreground: var(--foreground);
    }

    @if not($excel-filtering-actions-hover-icon-color) and $foreground {
        $excel-filtering-actions-hover-icon-color: var(--foreground);
    }

    @if not($excel-filtering-actions-selected-icon-color) and $accent-color {
        $excel-filtering-actions-selected-icon-color: var(--accent-color);
    }

    @if not($excel-filtering-actions-hover-background) and $accent-color and $background {
        $excel-filtering-actions-hover-background: color-mix(in srgb, var(--accent-color) 8%, var(--background));
    }

    @if not($excel-filtering-actions-disabled-foreground) and $foreground and $background {
        $excel-filtering-actions-disabled-foreground: color-mix(in srgb, var(--foreground) 50%, var(--background));
    }

    @if not($excel-filtering-border-color) and $foreground and $background {
        $excel-filtering-border-color: color-mix(in srgb, var(--foreground) 16%, var(--background));
    }

    // resizing

    @if not($resize-line-color) and $accent-color {
        $resize-line-color: var(--accent-color);
    }

    // grouping

    @if not($grouparea-background) and $foreground and $background {
        $grouparea-background: color-mix(in srgb, var(--foreground) 6%, var(--background));
    }

    @if not($grouparea-color) and $foreground and $background {
        $grouparea-color: color-mix(in srgb, var(--foreground) 60%, var(--background));
    }

    @if not($group-row-background) and $foreground and $background {
        $group-row-background: color-mix(in srgb, var(--foreground) 6%, var(--background));
    }

    @if not($group-row-selected-background) and $foreground and $background {
        $group-row-selected-background: color-mix(in srgb, var(--foreground) 6%, var(--background));
    }

    @if not($group-label-column-name-text) and $accent-color {
        $group-label-column-name-text: var(--accent-color);
    }

    @if not($group-label-icon) and $accent-color {
        $group-label-icon: var(--accent-color);
    }

    @if not($group-label-text) and $foreground and $background {
        $group-label-text: color-mix(in srgb, var(--foreground) 80%, var(--background));
    }

    @if not($group-count-background) and $foreground and $background {
        $group-count-background: color-mix(in srgb, var(--foreground) 12%, var(--background));
    }

    @if not($group-count-text-color) and $foreground {
        $group-count-text-color: var(--foreground);
    }

    @if not($drop-area-text-color) and $foreground and $background {
        $drop-area-text-color: color-mix(in srgb, var(--foreground) 80%, var(--background));
    }

    @if not($drop-area-icon-color) and $foreground and $background {
        $drop-area-icon-color: color-mix(in srgb, var(--foreground) 80%, var(--background));
    }

    @if not($drop-area-background) and $background {
        $drop-area-background: var(--background);
    }

    @if not($drop-area-on-drop-background) and $background {
        $drop-area-on-drop-background: var(--background);
    }

    // summaries

    @if not($summaries-patch-background) and $foreground and $background {
        $summaries-patch-background: color-mix(in srgb, var(--foreground) 12%, var(--background));
    }

    // other

    @if not($drop-indicator-color) and $accent-color {
        $drop-indicator-color: var(--accent-color);
    }

    @if not($expand-all-icon-color) and $foreground and $background {
        $expand-all-icon-color: color-mix(in srgb, var(--foreground) 70%, var(--background));
    }

    @if not($expand-all-icon-hover-color) and $foreground and $background {
        $expand-all-icon-hover-color: color-mix(in srgb, var(--foreground) 80%, var(--background));
    }

    @if not($expand-icon-color) and $foreground and $background {
        $expand-icon-color: color-mix(in srgb, var(--foreground) 60%, var(--background));
    }

    @if not($expand-icon-hover-color) and $foreground and $background {
        $expand-icon-hover-color: color-mix(in srgb, var(--foreground) 70%, var(--background));
    }

    @return extend(
        $theme,
        (
            selector: $selector,

            background: $background,
            foreground: $foreground,
            accent-color: $accent-color,

            header-background: $header-background,
            header-text-color: $header-text-color,
            header-selected-background: $header-selected-background,
            header-selected-text-color: $header-selected-text-color,
            header-border-width: $header-border-width,
            header-border-style: $header-border-style,
            header-border-color: $header-border-color,

            sorted-header-icon-color: $sorted-header-icon-color,

            ghost-header-text-color: $ghost-header-text-color,
            ghost-header-icon-color: $ghost-header-icon-color,
            ghost-header-background: $ghost-header-background,

            content-background: $content-background,
            content-text-color: $content-text-color,

            row-odd-background: $row-odd-background,
            row-even-background: $row-even-background,
            row-odd-text-color: $row-odd-text-color,
            row-even-text-color: $row-even-text-color,
            row-selected-background: $row-selected-background,
            row-selected-hover-background: $row-selected-hover-background,
            row-selected-text-color: $row-selected-text-color,
            row-selected-hover-text-color: $row-selected-hover-text-color,
            row-hover-background: $row-hover-background,
            row-hover-text-color: $row-hover-text-color,
            row-border-color: $row-border-color,

            pinned-border-width: $pinned-border-width,
            pinned-border-style: $pinned-border-style,
            pinned-border-color: $pinned-border-color,

            cell-active-border-color: $cell-active-border-color,
            cell-selected-background: $cell-selected-background,
            cell-editing-background: $cell-editing-background,
            cell-editing-foreground: $cell-editing-foreground,
            cell-editing-focus-foreground: $cell-editing-focus-foreground,
            cell-selected-text-color: $cell-selected-text-color,

            edit-mode-color: $edit-mode-color,
            edited-row-indicator: $edited-row-indicator,
            cell-edited-value-color: $cell-edited-value-color,
            cell-new-color: $cell-new-color,

            cell-disabled-color: $cell-disabled-color,

            resize-line-color: $resize-line-color,

            drop-indicator-color: $drop-indicator-color,

            grouparea-background: $grouparea-background,
            grouparea-color: $grouparea-color,

            group-label-column-name-text: $group-label-column-name-text,
            group-label-icon: $group-label-icon,
            group-label-text: $group-label-text,

            expand-all-icon-color: $expand-all-icon-color,
            expand-all-icon-hover-color: $expand-all-icon-hover-color,

            expand-icon-color: $expand-icon-color,
            expand-icon-hover-color: $expand-icon-hover-color,
            active-expand-icon-color: $active-expand-icon-color,
            active-expand-icon-hover-color: $active-expand-icon-hover-color,

            group-count-background: $group-count-background,
            group-count-text-color: $group-count-text-color,

            group-row-background: $group-row-background,
            group-row-selected-background: $group-row-selected-background,

            drop-area-text-color: $drop-area-text-color,
            drop-area-icon-color: $drop-area-icon-color,
            drop-area-on-drop-background: $drop-area-on-drop-background,
            drop-area-background: $drop-area-background,

            filtering-header-background: $filtering-header-background,
            filtering-header-text-color: $filtering-header-text-color,
            filtering-row-background: $filtering-row-background,
            filtering-row-text-color: $filtering-row-text-color,
            filtering-dialog-background: $filtering-dialog-background,

            excel-filtering-header-foreground: $excel-filtering-header-foreground,
            excel-filtering-subheader-foreground: $excel-filtering-subheader-foreground,
            excel-filtering-actions-foreground: $excel-filtering-actions-foreground,
            excel-filtering-actions-icon-color: $excel-filtering-actions-icon-color,
            excel-filtering-actions-hover-foreground: $excel-filtering-actions-hover-foreground,
            excel-filtering-actions-hover-icon-color: $excel-filtering-actions-hover-icon-color,
            excel-filtering-actions-selected-icon-color: $excel-filtering-actions-selected-icon-color,
            excel-filtering-actions-hover-background: $excel-filtering-actions-hover-background,
            excel-filtering-actions-disabled-foreground: $excel-filtering-actions-disabled-foreground,
            excel-filtering-border-color: $excel-filtering-border-color,

            tree-filtered-text-color: $tree-filtered-text-color,
            tree-selected-filtered-row-text-color: $tree-selected-filtered-row-text-color,
            tree-selected-filtered-cell-text-color: $tree-selected-filtered-cell-text-color,

            row-highlight: $row-highlight,
            grid-elevation: $grid-shadow,
            drag-elevation: $drag-shadow,
            row-ghost-background: $row-ghost-background,
            row-ghost-border-color: $row-ghost-border-color,
            row-drag-color: $row-drag-color,
            drop-area-border-radius: $drop-area-border-radius,
            sortable-header-icon-hover-color: $sortable-header-icon-hover-color,
            grid-border-color: $grid-border-color,
            summaries-patch-background: $summaries-patch-background,

            cell-selected-within-background: $cell-selected-within-background,
            cell-selected-within-text-color: $cell-selected-within-text-color,
            active-state-border-style: $active-state-border-style,

            body-column-border-color-odd: $body-column-border-color-odd,
            body-column-border-color-even: $body-column-border-color-even,
            body-column-hover-border-color: $body-column-hover-border-color,
            body-column-hover-selected-border-color: $body-column-hover-selected-border-color,
            body-column-selected-border-color: $body-column-selected-border-color,
        )
    );
}
