/**
 * EDS (Equinor Design System) Theme for @toolbox-web/grid
 *
 * Styles the toolbox grid to match Equinor's design system. Shared by
 * Angular consumers (cargo-tracker-apps via `@ngx-stoui/core/toolbox-grid.css`)
 * and React consumers (planning Roma app).
 *
 * Both `<tbw-grid>` (web component) and `<div data-tbw-grid>` are supported
 * so React apps that don't import the web component can still opt in to the
 * theme by stamping the attribute on a wrapper.
 *
 * Supports light/dark modes via the light-dark() CSS function. Consumers that
 * don't support dark mode (e.g. Roma) can opt out with `color-scheme: light only`
 * on their root.
 *
 * In-cell editor coexistence:
 *   - Angular: Material form fields (`.mat-mdc-*`) — chrome is stripped so the
 *     surrounding `.tbw-editor-host` paints the visual boundary.
 *   - React/EDS: native EDS `<DatePicker>` / `<Autocomplete>` wrapped with
 *     `.tbw-eds-date-editor` / `.tbw-eds-autocomplete-editor` marker classes.
 *
 * Reference: https://eds.equinor.com/
 * Grid Theming: https://toolboxjs.com/?path=/docs/grid-theming--docs
 * Requires @toolbox-web/grid v1.3.1+
 *
 * Usage:
 *   Angular: "@ngx-stoui/core/toolbox-grid.css" in project.json styles array
 *   React:   import the compiled css, or @use this partial in a Sass entry
 *
 * Sections:
 *   1. Theme Variables
 *   2. Grid Styles (cells, editors, Material stripping, anchors, Typography reset)
 *   3. Pinned cells & sticky group rows
 *   4. Filter Panel
 *   5. Context Menu
 *   6. Overlay Panel
 *   7. EDS in-cell editors (DatePicker / Autocomplete)
 *   8. Empty State Overlay
 *   9. Opt-ins (compact mode, thin header)
 *  10. Dark Mode
 */

/* ================================================================
 * 1. Theme Variables
 *
 * Shared by tbw-grid and body-level panels (.tbw-filter-panel).
 * Internal aliases (--_*) reduce repetition of EDS token + fallback pairs.
 *
 * NOTE: As of v1.3.1, the grid copies theme classes to filter panels
 * rendered in document.body, so these variables cascade properly.
 * ================================================================ */
tbw-grid,
[data-tbw-grid],
.tbw-filter-panel {
  /* --- EDS Token Aliases (cascade to all children) --- */
  --_bg: var(--eds_ui_background__default, light-dark(#ffffff, #132634));
  --_fg: var(--eds_text_static_icons__default, light-dark(#3d3d3d, #ffffff));
  --_fg-muted: var(
    --eds_text_static_icons__tertiary,
    light-dark(#6f6f6f, #9ca6ac)
  );
  --_border: var(--eds_ui_background__medium, light-dark(#dcdcdc, #3e4f5c));
  --_surface: var(--eds_ui_background__light, light-dark(#f7f7f7, #2e3f4d));
  --_accent: var(
    --eds_interactive_primary__resting,
    light-dark(#007079, #97cace)
  );
  --_accent-fg: var(
    --eds_text_static_icons__primary_white,
    light-dark(#ffffff, #000000)
  );
  --_hover: var(
    --eds_interactive_table__cell__fill_hover,
    light-dark(#eaeaea, #324d62)
  );
  --_focus: var(--eds_interactive_focus, light-dark(#007079, #97cace));
  --_radius: var(--eds_shape_button_border_radius, 4px);

  /* --- Typography --- */
  --tbw-font-family: Equinor, sans-serif;
  --tbw-font-size: var(--sto-base-font-size, 13px);
  --tbw-font-size-header: var(--sto-base-font-size, 13px);
  --tbw-font-weight-header: 700;
  --tbw-aggregation-font-size: 0.9em;

  /* --- Colors --- */
  --tbw-color-bg: var(--_bg);
  --tbw-color-fg: var(--_fg);
  --tbw-color-fg-muted: var(--_fg-muted);
  --tbw-color-panel-bg: var(--_surface);

  /* --- Borders --- */
  --tbw-color-border: var(--_border);
  --tbw-color-border-strong: var(--_border);
  --tbw-row-divider: 1px solid var(--_border);
  --tbw-color-border-cell: transparent;
  --tbw-color-border-header: var(--_border);
  --tbw-border-radius: var(--_radius);
  --tbw-selection-border-style: dashed;
  --tbw-selection-border-width: 1px;

  /* --- Header --- */
  --tbw-color-header-bg: var(
    --eds_interactive_table__header__fill_resting,
    light-dark(#f7f7f7, #2e3f4d)
  );
  --tbw-color-header-fg: var(--_fg);
  --tbw-header-text-transform: none;
  --tbw-header-letter-spacing: normal;

  /* --- Row & Cell States --- */
  --tbw-color-row-alt: var(--_bg);
  --tbw-color-row-hover: var(--_hover);
  --tbw-color-selection: var(
    --eds_interactive_primary__selected_highlight,
    light-dark(#e6faec, #007079)
  );
  --tbw-color-active-row-bg: var(
    --eds_interactive_table__cell__fill_activated,
    light-dark(#e6faec, #243746)
  );
  --tbw-editing-row-outline-width: 1px;

  /* --- Accent --- */
  --tbw-color-accent: var(--_accent);
  --tbw-color-accent-fg: var(--_accent-fg);

  /* --- Dimensions --- */
  --tbw-row-height: var(
    --eds_clickbound_default__base,
    calc(var(--sto-base-font-size, 13px) * 2.923)
  );
  --tbw-header-height: var(
    --eds_clickbound_default__base,
    calc(var(--sto-base-font-size, 13px) * 2.923)
  );
  --tbw-cell-padding: 0 var(--eds_spacing_medium, 8px);
  --tbw-cell-padding-header: 0 var(--eds_spacing_medium, 8px);
  --tbw-button-padding: 0.375rem 0.625rem;

  /* --- Focus ---
     `--tbw-focus-outline` paints the focus ring; the grid applies
     `--tbw-focus-background` as the cell tint when a cell has focus. Use an
     alpha-blended accent so the grid's own `.cell-focus` rule produces the
     soft tint we want — no element-targeting needed. Pinned cells need a
     pre-composited override (see section 3) because alpha over a sticky
     opaque panel-bg would bleed scrolling content through. */
  --tbw-focus-outline: 0.5px dotted var(--_focus);
  --tbw-focus-outline-offset: -1px;
  --tbw-focus-background: rgba(from var(--_accent) r g b / 8%);

  /* --- Resize Handle --- */
  --tbw-resize-handle-color: transparent;
  --tbw-resize-handle-color-hover: var(--_accent);

  /* --- Sort & Filter Display (v1.5.0+) --- */
  --tbw-sort-indicator-color: var(--_fg-muted);
  --tbw-sort-indicator-active-color: var(--_accent);
  --tbw-filter-btn-display: none;
  --tbw-sort-indicator-display: none;

  /* --- Shadows --- */
  --tbw-color-shadow: light-dark(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.3));

  /* --- Filter Panel Variables --- */
  --tbw-filter-panel-bg: var(--_bg);
  --tbw-filter-panel-fg: var(--_fg);
  --tbw-filter-panel-border: var(--_border);
  --tbw-filter-panel-radius: var(--_radius);
  --tbw-filter-panel-shadow: light-dark(
    rgba(0, 0, 0, 0.15),
    rgba(0, 0, 0, 0.4)
  );
  /* Filter inputs: EDS underline style. Strip the grid's default border
     and radius via variables; the underline box-shadow itself (no variable)
     is applied in section 4. */
  --tbw-filter-input-bg: var(--_surface);
  --tbw-filter-input-border: none;
  --tbw-filter-input-radius: 0;
  --tbw-filter-input-focus: var(--_focus);
  --tbw-filter-accent: var(--_accent);
  --tbw-filter-accent-fg: var(--_accent-fg);
  --tbw-filter-hover: var(--_surface);
  --tbw-filter-muted: var(--_fg-muted);
  --tbw-filter-divider: var(--_border);
  --tbw-filter-active-color: var(--_accent);

  /* --- Filter Button (v1.3.1+) --- */
  --tbw-filter-btn-padding: 8px 16px;
  --tbw-filter-btn-font-weight: 500;
  --tbw-filter-btn-min-height: var(--eds_clickbound_default__base, 48px);
  --tbw-filter-search-padding: 6px 8px;
  --tbw-filter-item-height: 36px;

  /* --- Panel Layout --- */
  --tbw-panel-padding: var(--eds_spacing_medium_small, 12px);
  --tbw-panel-gap: var(--eds_spacing_small, 8px);
  --tbw-animation-duration: 150ms;
}

/* ================================================================
 * 2. Grid Styles
 *
 * All grid-scoped rules: cell styling, editor hosts, anchors,
 * Typography reset, and Angular Material overrides.
 *
 * Compact mode and thin-header opt-ins live in section 9.
 * ================================================================ */
tbw-grid,
[data-tbw-grid] {
  /* --- Angular Material overrides for in-grid editors --- */
  --mat-checkbox-state-layer-size: 20px;
  --mdc-filled-text-field-container-color: transparent;
  --mdc-filled-text-field-active-indicator-color: transparent;
  --mdc-filled-text-field-hover-active-indicator-color: transparent;
  --mdc-filled-text-field-focus-active-indicator-color: transparent;
  --mdc-outlined-text-field-outline-color: transparent;
  --mdc-outlined-text-field-focus-outline-color: transparent;
  --mdc-outlined-text-field-hover-outline-color: transparent;

  & .mat-mdc-icon-button.mat-mdc-button-base {
    --mdc-icon-button-state-layer-size: 28px;
    --mat-icon-button-state-layer-size: 28px;
  }

  /* --- Toolbar --- */
  & .tbw-toolbar-content-slot {
    display: flex;
    place-items: center;
  }

  /* --- Filter button icon --- */
  & .tbw-filter-btn .material-icons-outlined {
    font-size: 1.2em;
    width: 1.2em;
    height: 1.2em;
  }

  /* --- Cell defaults --- */
  & .cell {
    line-height: 1em;
  }

  /* Anchor tags rendered inside cells (e.g. deep-link cells) should read
     in the same color as plain text cells. Browser UA / EDS `Typography`
     would otherwise paint them blue/teal. Underline is preserved
     (we only override `color`). */
  & .cell a {
    color: var(--_fg);
  }

  /* --- Numeric alignment --- */
  & .cell[data-type='number'] {
    text-align: right;
  }

  /* --- Data cell styling & editor host ---
     The grid already paints `height: var(--tbw-row-height)` and
     `background: var(--tbw-focus-background)` on focused cells from its own
     base layer, so we only add what the grid doesn't expose as a variable:
     `font-weight` on cells, the sticky-cell focus override, and editor-host
     chrome (which lives on our `.tbw-editor-host` wrapper, not on a built-in
     grid class). */
  & [part='cell'],
  & .data-grid-row .cell {
    font-weight: 500;

    /* Neutralize EDS `<Typography>` when used inside custom cell renderers.
       Typography defaults to its own color, weight, `<p>` margin, and
       a `1.429em` line-height, all of which break the grid's uniform
       row appearance.

       `!important` is required: EDS ships a global rule with an ID
       selector (specificity 1,1,0) that wins over any class-based
       chain. The third-party rule is itself a hardcoded-ID hack;
       overriding with `!important` is the correct counter and stays
       scoped to elements inside a grid cell. */
    & [class*='Typography__StyledTypography'] {
      color: inherit !important;
      font-family: inherit !important;
      font-size: inherit !important;
      font-weight: inherit !important;
      line-height: inherit !important;
      letter-spacing: inherit !important;
    }

    &.editing {
      padding: 2px 1px;
      position: relative;
    }

    /* Pinned cell focus tint.

       The grid's own `.cell-focus` rule already paints
       `--tbw-focus-background` (set in section 1 to an alpha-blended
       accent) on any focused cell. On a sticky cell the rows behind
       would scroll through that translucent tint as the user pans
       horizontally, so we pre-composite the same 8% accent onto the
       pinned-cell `--tbw-color-panel-bg` to keep the result fully opaque.
       This is the only place we still target `.cell-focus` directly
       — `color-mix` needs the panel-bg as a second color, which a
       single variable can't express. */
    &.cell-focus.sticky-left,
    &.cell-focus.sticky-right {
      background: color-mix(
        in srgb,
        var(--_accent) 8%,
        var(--tbw-color-panel-bg)
      );
    }

    /* Editor host — visual boundary for all editor types */
    & .tbw-editor-host {
      border: 1px solid var(--_border);
      background: var(--tbw-color-bg);
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;
      box-sizing: border-box;
      padding: var(--tbw-cell-padding, 0 6px);

      &:has(.mat-form-field-invalid) {
        border-color: var(--tbw-invalid-border-color);
      }

      &:focus-within {
        box-shadow: inset 0 -2px 0 0 var(--_accent);
        outline: none;
      }

      /* Editors that bring their own full-bleed input (e.g. EDS DatePicker /
         Autocomplete) opt out of the host's padding so they can stretch
         to fill the cell. See section 7. */
      &:has(.tbw-eds-date-editor),
      &:has(.tbw-eds-autocomplete-editor) {
        padding: 0;
      }

      /* Framework component hosts: transparent for flex layout.
         :where() for zero specificity so component :host styles can override.
         Excludes form elements and custom editor boxes. */
      &
        > :where(
          :not(input, select, textarea, [contenteditable], .tbw-editor-box)
        ) {
        display: contents;
      }

      & > .tbw-editor-box {
        display: flex;
        align-items: center;
        width: 100%;
        height: 100%;
      }

      /* Simple text/number inputs */
      & > input[type='text'],
      & > input[type='number'] {
        border: none;
        background: transparent;
        font: inherit;
        color: inherit;
        padding: 0 4px;
        outline: none;
        width: 100%;
        height: 100%;
      }

      /* Native select */
      & > select {
        appearance: base-select;
        border: none;
        background: transparent;
        font: inherit;
        color: inherit;
        padding: 0 4px;
        outline: none;
        width: 100%;
        height: 100%;
        cursor: pointer;
        place-items: center;

        &::picker(select) {
          appearance: base-select;
          border: none;
          border-radius: 4px;
          padding: 8px 0;
          overscroll-behavior: contain;
          box-shadow:
            0 2px 4px -1px rgba(0, 0, 0, 0.2),
            0 4px 5px 0 rgba(0, 0, 0, 0.14),
            0 1px 10px 0 rgba(0, 0, 0, 0.12);
        }

        option {
          font: inherit;
          padding: 8px 16px;
          min-height: 36px;
          display: flex;
          align-items: center;
          border-radius: 0;

          &:hover {
            background-color: light-dark(
              rgba(0, 0, 0, 0.04),
              rgba(255, 255, 255, 0.08)
            );
          }

          &:checked {
            background-color: light-dark(
              rgba(0, 112, 121, 0.12),
              rgba(151, 202, 206, 0.2)
            );
            color: inherit;
          }
        }
      }
    }
  }

  /* --- Material form field stripping inside editor hosts ---
     Lower specificity than cell-scoped rules above; strips
     Angular Material chrome so .tbw-editor-host provides visuals. */
  & .tbw-editor-host {
    & .mat-mdc-form-field {
      width: 100%;
      height: 100%;

      & .mat-mdc-text-field-wrapper {
        padding: 0 4px;
        background: transparent;
        height: 100%;
        display: flex;
        align-items: center;
      }

      & .mat-mdc-form-field-flex {
        height: 100%;
        display: flex;
        align-items: center;
      }

      & .mat-mdc-form-field-focus-overlay {
        display: none;
      }

      & .mdc-line-ripple {
        display: none;
      }

      & .mat-mdc-form-field-infix {
        padding: 0;
        min-height: unset;
        width: 100%;
        display: flex;
        align-items: center;
        border: 0;
      }

      & input.mat-mdc-input-element {
        /* `line-height: normal` is browser/font-dependent (≈ 1.2-1.5) which
           inflates the `1lh + 2 * padding` floor used by `.cell.editing`
           and pushes the editing row past `--tbw-row-height`. Locking it
           to 1 keeps the input the height of its font-size so the cell can
           collapse to `--tbw-row-height` in compact mode. */
        height: auto;
        line-height: 1;
      }

      & .mat-mdc-select {
        height: 100%;
        display: flex;
        align-items: center;
      }

      & .mat-mdc-select-trigger {
        height: 100%;
        display: flex;
        align-items: center;
      }

      & .mat-mdc-select-value {
        display: flex;
        align-items: center;
      }
    }

    & .mat-mdc-form-field-subscript-wrapper {
      display: none;
    }

    & .editor-checkbox {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
    }

    & .mat-mdc-option.mdc-list-item {
      background: var(
        --mat-autocomplete-background-color,
        var(--mat-sys-surface-container)
      );
    }
  }

  /* ================================================================
   * 3. Pinned cells & sticky group rows
   * ================================================================ */

  /* Row hover on pinned cells.

     Pinned cells (`.sticky-left` / `.sticky-right`) carry an opaque
     `--tbw-color-panel-bg` so non-sticky content scrolling behind
     them can't bleed through. That opaque fill also masks the row's
     `:hover` tint, making pinned columns visually disconnected from
     the rest of the row. Repaint them with the row-hover color to
     restore continuity. Wrapped in `@media (hover: hover)` to mirror
     the grid's own row-hover rule and avoid sticky tap-state flicker
     on touch devices. */
  @media (hover: hover) {
    & .data-grid-row:hover > .cell.sticky-left,
    & .data-grid-row:hover > .cell.sticky-right {
      background: var(--tbw-color-row-hover);
    }

    /* Hover + focus on a pinned cell: keep the focus tint visible by
       flattening the same 8% accent over the row-hover color. */
    & .data-grid-row:hover > .cell.cell-focus.sticky-left,
    & .data-grid-row:hover > .cell.cell-focus.sticky-right {
      background: color-mix(
        in srgb,
        var(--tbw-color-accent) 8%,
        var(--tbw-color-row-hover)
      );
    }
  }

  /* Sticky group-row chrome.

     The grouping-rows feature renders a full-width banner row for each
     group boundary: a `.data-grid-row.group-row` whose only child is a
     single `.cell.group-full` (spanning `grid-column: 1 / -1`) containing
     `.group-toggle`, `.group-label`, `.group-count`, and optional
     `.group-aggregates`. On a horizontally-scrollable grid the cell is
     as wide as the row, so when the user scrolls right the entire chrome
     slides off the left edge.

     Approach: shrink the cell to its content width and pin it to the left
     with `position: sticky; left: 0`. The chrome children keep their
     inline flow — we never touch them individually, so new chrome
     elements in a future grid version "just work".

     Two things move from the cell up to the row:
       1. Background — the cell no longer spans the row, so the row paints
          the banner tint.
       2. Full-row span override — the cell still has `grid-column: 1 / -1`
          from the grid runtime; `width: max-content !important` shrinks
          the rendered box while leaving the grid-track placement intact.

     Why we override `overflow: hidden` on the cell:
     @toolbox-web/grid sets `overflow: hidden` on every `.cell` to clip
     content. Per the CSS Position spec, that establishes a scroll
     container, which traps the cell's own sticky positioning inside the
     (non-scrolling) cell box. `overflow: visible` lets the sticky context
     bubble up past the row, past `.rows-viewport` (`overflow: clip` —
     non-scrollable), all the way to `.tbw-scroll-area` (the actual
     horizontal scroller) where sticky engages. */
  & .data-grid-row.group-row {
    background: var(
      --tbw-color-header-bg,
      var(--tbw-color-panel-bg, transparent)
    );

    & > .cell.group-full {
      overflow: visible;
      position: sticky;
      left: 0;
      z-index: 2;
      width: max-content !important;
      max-width: 100%;
      background: transparent;
    }

    & .group-count {
      padding: 0 0.5rem;
    }
  }

  /* Sticky aggregation (footer) row.

     The bottom aggregation row (`.tbw-footer > .tbw-aggregation-rows >
     .tbw-aggregation-row > .tbw-aggregation-cell.tbw-aggregation-cell-full`)
     has the same shape as a group row: a full-width cell that spans the
     entire scrollable area (`grid-column: 1 / -1`, width = scrollWidth).
     When the user scrolls horizontally the totals/label slide off the
     left edge.

     Same fix as `.group-row > .cell.group-full`: shrink the cell to its
     content width, pin it to the left with `position: sticky; left: 0`,
     and override the grid's `overflow: hidden` so the sticky context can
     bubble up past the row to the scroll container.

     `.tbw-footer` itself is already `position: sticky; bottom: 0` from
     the grid runtime — we only need to handle horizontal stickiness. */
  & .tbw-aggregation-row > .tbw-aggregation-cell-full {
    overflow: visible;
    position: sticky;
    left: 0;
    z-index: 2;
    width: max-content !important;
    max-width: 100%;
    background: transparent;
  }
}

/* ================================================================
 * 4. Filter Panel Styles
 * ================================================================ */
.tbw-filter-panel {
  max-width: none;
  max-height: none;

  /* EDS underline inputs — the bg/border/radius are stripped via
     `--tbw-filter-input-*` variables in section 1. The underline
     `box-shadow` and height have no variable equivalent. */
  & .tbw-filter-search-input,
  & .tbw-filter-range-input,
  & .tbw-filter-date-input {
    height: var(--tbw-row-height);
    box-shadow: inset 0px -1px 0px 0px var(--_fg-muted);
    outline: 1px solid transparent;
    outline-offset: 0px;
  }

  /* EDS-styled checkboxes */
  & .tbw-filter-checkbox {
    appearance: none;
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border: 2px solid var(--_fg-muted);
    border-radius: 2px;
    background: transparent;
    cursor: pointer;
    position: relative;
    flex-shrink: 0;
    transition:
      background-color 150ms,
      border-color 150ms;

    &:checked {
      background-color: var(--_accent);
      border-color: var(--_accent);
    }

    &:checked::after {
      content: '';
      position: absolute;
      left: 4px;
      top: 1px;
      width: 5px;
      height: 9px;
      border: solid var(--_accent-fg);
      border-width: 0 2px 2px 0;
      transform: rotate(45deg);
    }

    &:indeterminate {
      background-color: var(--_accent);
      border-color: var(--_accent);
    }

    &:indeterminate::after {
      content: '';
      position: absolute;
      left: 2px;
      top: 6px;
      width: 10px;
      height: 2px;
      background: var(--_accent-fg);
    }

    &:hover {
      border-color: var(
        --eds_interactive_primary__hover,
        light-dark(#004f55, #ade2e6)
      );
    }

    &:focus-visible {
      outline: 2px solid var(--_focus);
      outline-offset: 2px;
    }
  }

  & .tbw-filter-value-item {
    gap: 8px;
  }
}

/* Search input with specificity escalation for filter panel context */
.tbw-filter-search-input,
.tbw-filter-panel .tbw-filter-search-input {
  font-size: 1rem;
  font-weight: 400;
  letter-spacing: 0.025em;
  line-height: 1.5em;
  min-height: 36px;
  box-sizing: border-box;
  border-top: 0;
  border-left: 0;
  border-right: 0;
  background: var(--eds_ui_background__light, light-dark(#f7f7f7, #2e3f4d));
}

/* ================================================================
 * 5. Context Menu
 *
 * Appended to document.body (outside tbw-grid).
 * CSS variables are copied from tbw-grid at render time.
 * ================================================================ */
.tbw-context-menu {
  --tbw-context-menu-bg: var(
    --eds_ui_background__default,
    light-dark(#ffffff, #132634)
  );
  --tbw-context-menu-fg: var(
    --eds_text_static_icons__default,
    light-dark(#3d3d3d, #ffffff)
  );
  --tbw-context-menu-border: var(
    --eds_ui_background__medium,
    light-dark(#dcdcdc, #3e4f5c)
  );
  --tbw-context-menu-hover: var(
    --eds_interactive_table__cell__fill_hover,
    light-dark(#eaeaea, #324d62)
  );
  --tbw-context-menu-danger: var(--eds_interactive_danger__resting, #eb0000);
  --tbw-context-menu-muted: var(
    --eds_text_static_icons__tertiary,
    light-dark(#6f6f6f, #9ca6ac)
  );
  --tbw-context-menu-font-size: 0.873rem;
  --tbw-context-menu-font-family: Equinor, sans-serif;
  --tbw-context-menu-icon-size: 1.2rem;
  --tbw-context-menu-item-padding: 8px 16px;
  --tbw-context-menu-item-gap: 8px;
  --tbw-context-menu-radius: var(--eds_shape_button_border_radius, 4px);
  --tbw-context-menu-shadow: 0 2px 8px
    light-dark(rgba(0, 0, 0, 0.12), rgba(0, 0, 0, 0.3));

  & .tbw-context-menu-shortcut kbd {
    background: var(--eds_ui_background__light, light-dark(#f7f7f7, #2e3f4d));
    border: 1px solid
      var(--eds_ui_background__medium, light-dark(#dcdcdc, #3e4f5c));
    border-radius: 4px;
    padding: 2px 6px;
    font-family: 'Equinor Mono', 'Consolas', 'Courier New', monospace;
    font-size: 0.75rem;
    line-height: 1.4;
  }

  & .tbw-context-menu-icon .material-icons {
    font-size: var(--tbw-context-menu-icon-size, 20px);
    line-height: 1;
    vertical-align: middle;
  }

  & .tbw-context-menu-item.danger .tbw-context-menu-icon .material-icons {
    color: inherit;
  }
}

/* ================================================================
 * 6. Overlay Panel (BaseOverlayEditor)
 * ================================================================ */
.tbw-overlay-panel {
  --tbw-overlay-bg: var(
    --eds_ui_background__default,
    light-dark(#ffffff, #132634)
  );
  --tbw-overlay-border: var(
    --eds_ui_background__medium,
    light-dark(#dcdcdc, #3e4f5c)
  );
  --tbw-overlay-shadow: 0 4px 12px
    light-dark(rgba(0, 0, 0, 0.15), rgba(0, 0, 0, 0.4));
  color: var(--eds_text_static_icons__default, light-dark(#3d3d3d, #ffffff));
}

/* ================================================================
 * 7. EDS in-cell editors — DatePicker and Autocomplete
 *
 * Marker classes:
 *   `.tbw-eds-date-editor`         — wrapper around EDS <DatePicker>
 *   `.tbw-eds-autocomplete-editor` — wrapper around EDS <Autocomplete>
 *
 * Used by React consumers that render EDS form components inside grid
 * cell editors (typically inside `<EdsProvider density="compact">`).
 * EDS wraps each field in an InputWrapper with a label and helper
 * area — none of which we want inside a row-height cell. We strip the
 * chrome, hide the label/helper for sighted users (kept for screen
 * readers), and stretch the inner field stack to fill the cell.
 *
 * Popover positioning fix (applies to both):
 * EDS uses the native popover API (`popover="manual"`) + floating-ui
 * to place the calendar / options dropdown. Inline EDS sets
 * `position: absolute` with viewport-anchored top/left from the
 * trigger's getBoundingClientRect. That works in plain DOM, but
 * `tbw-grid` virtualises rows with `transform` which creates a
 * containing block. Once the popover enters the browser top-layer
 * (`.showPopover()`), the spec says its containing block is the
 * viewport — but floating-ui's inset values assume the regular
 * containing-block cascade. The two coordinate systems mismatch and
 * the popover renders off-screen.
 *
 * Forcing `position: fixed` aligns the popover's containing block
 * with floating-ui's viewport math. The inset reset overrides the
 * browser's default `[popover]:popover-open { inset: 0 }` rule that
 * would otherwise stretch the popover full-viewport.
 *
 * Harmless when EDS is absent (Angular consumers don't ship these
 * markers), so safe to include in the shared theme.
 * ================================================================ */

/* Shared chrome-stripping + popover-positioning rules. Applied to both
   editor wrappers via @extend so the rules stay co-located with their
   shared docstring above. */
%tbw-eds-cell-editor {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: stretch;

  /* Hide the visually-rendered EDS label/helper, keep them for a11y. */
  label,
  [class*='HelperText'] {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  /* Calendar / options dropdown popover — see header for rationale. */
  [popover] {
    position: fixed !important;
    right: auto !important;
    bottom: auto !important;
    width: max-content !important;
    margin: 0 !important;
  }
}

.tbw-eds-date-editor {
  @extend %tbw-eds-cell-editor;

  /* Stretch the EDS field stack to fill the cell vertically and strip
     its own background/box-shadow underline since the surrounding
     `.tbw-editor-host:focus-within` already provides the focus indicator.
     `!important` beats EDS's ID-scoped rule (specificity 1,1,0).
     NOTE: do not use `> div` — the calendar popover is also a direct
     child once `popover="manual"` opens it, and forcing it to 100%
     height stretches it to the full viewport and breaks auto-flip. */
  [class*='InputWrapper__Container'],
  [class*='FieldWrapper'] {
    width: 100% !important;
    height: 100% !important;
  }

  [class*='FieldWrapper'] {
    background: transparent !important;
    box-shadow: none !important;
    border: none !important;
    display: flex;
    align-items: center;
  }
}

.tbw-eds-autocomplete-editor {
  @extend %tbw-eds-cell-editor;

  /* Stretch EDS field stack to fill the cell vertically and strip its
     own background/underline since the editor host owns the focus ring. */
  [class*='InputWrapper__Container'],
  [class*='Container'] {
    width: 100% !important;
    height: 100% !important;
  }

  [class*='Container'] {
    background: transparent !important;
    box-shadow: none !important;
    border: none !important;
  }
}

/* ================================================================
 * 8. Empty State Overlay
 *
 * The grid's empty overlay (.tbw-empty-overlay) is absolutely
 * positioned inside .rows-container. When the grid host is sized
 * by content (no explicit height) and there are zero rows, the
 * rows-container collapses to height 0 — leaving the overlay
 * with no space and stacking the column header directly on top
 * of the pinned/aggregation footer.
 *
 * Reserve a sensible minimum height for the rows area whenever
 * the empty overlay is present, so the message has room to render
 * and the header / footer separate visually.
 * ================================================================ */
tbw-grid,
[data-tbw-grid] {
  & .rows-container:has(> .tbw-empty-overlay) {
    min-height: var(--tbw-empty-min-height, 120px);
  }

  & .tbw-empty-overlay {
    min-height: var(--tbw-empty-min-height, 120px);
    color: var(--_fg-muted);
    /* Keep it pinned to the visible viewport */
    position: sticky;
    left: 0;
    top: 0;

    /* Restrict size to the container's visible area */
    width: 100%;
    height: 100%;
    max-width: 100vw; /* Ensures it doesn't exceed screen width */

    /* Center the content */
    display: flex;
    align-items: center;
    justify-content: center;

    /* Ensure it doesn't block interactions if needed */
    pointer-events: none;
  }

  & .tbw-empty-message {
    pointer-events: auto; /* Re-enable clicks for the message itself */
  }
}

/* ================================================================
 * 9. Opt-ins (compact mode, thin header)
 * ================================================================ */

/* Compact mode — triggers when `.eds-compact` is on the grid itself
   (`<tbw-grid class="eds-compact">`) or on any ancestor (e.g. a wrapping
   section that toggles density for everything inside). */
tbw-grid.eds-compact,
[data-tbw-grid].eds-compact,
.eds-compact tbw-grid,
.eds-compact [data-tbw-grid] {
  /* Compact metrics scale with `--sto-base-font-size`, so compact honours the
     sm/m/l accessibility typography classes (`.sto-sm-typography` 10px,
     `.sto-m-typography` 13px default, `.sto-l-typography` 16px). At the 13px
     Regular base this resolves to 11px text / 15px rows (10px→8px/12px,
     16px→14px/18px).

     RUNTIME, NOT BUILD-TIME: every value below is a native CSS function
     (`calc()`, `round()`, `var()`), evaluated by the browser. Because the
     argument contains `var(--sto-base-font-size)`, Sass cannot fold it at
     compile time — it passes the expression straight through — so changing
     the typography class restyles the grid live, with no rebuild.

     WHY `round(..., 1px)` ON THE HEIGHTS: a `.cell`'s intrinsic content
     height is `≈ line-height + ~3.5px`, and the rendered row is
     `max(--tbw-row-height, that floor)`. A fractional row height renders
     sub-pixel rows, so the 1px row dividers land on fractional device rows
     and drop out inconsistently (the old `calc(13px * 1.111) ≈ 14.44px` did
     exactly this). Rounding each proportional height to a whole pixel keeps
     every scale on integer rows, so dividers stay crisp. The multipliers are
     the Regular-base ratios: 11/13 ≈ 0.846 (font), 15/13 ≈ 1.154 (row),
     20/13 ≈ 1.538 (header). */
  --tbw-font-size: calc(round(var(--sto-base-font-size, 13px) * 0.846, 1px));
  --tbw-font-size-header: calc(
    round(var(--sto-base-font-size, 13px) * 0.846, 1px)
  );
  --tbw-row-height: calc(round(var(--sto-base-font-size, 13px) * 1.154, 1px));
  --tbw-header-height: calc(
    round(var(--sto-base-font-size, 13px) * 1.538, 1px)
  );

  /* In-cell Material icons.
     Angular Material renders `<mat-icon>` as `<span class="mat-icon">` at a
     hardcoded 24x24. Since `.data-grid-row` is `display: grid` and the row
     track grows to fit the tallest cell content, a 24px icon would inflate
     every row past `--tbw-row-height`. Constrain in-cell icons to the cell
     font-size so they fit the compact row track.

     Sizing trick: `font-size: 1em` keeps the glyph at the cell font-size
     (13px default), then `width/height: 1em` resolves against the icon's
     own font-size — producing a 13x13 box. Only applied under `.eds-compact`
     so non-compact grids keep Material's standard 24px icons. */
  & .cell .mat-icon,
  & .cell mat-icon {
    font-size: 1em;
    width: 1em;
    height: 1em;
    line-height: 1em;
  }

  /* In-cell editor density.
     Active editors (`.cell.editing > .tbw-editor-host`) host Angular
     Material form-fields. Even with all chrome stripped (see section 2),
     a Material input renders at ~35px tall because of its intrinsic
     padding/min-height — which then ratchets the grid's row-height
     measurement upward and shrinks the visible-row count for every
     other row.

     In compact mode we constrain the editor to the compact row height
     so editing a row no longer changes the row's painted height. */
  & .cell.editing {
    /* Eliminate the `1lh + 2 * padding` floor inherited from the base
       `.cell.editing` rule — the active cell follows row-height only. */
    min-height: var(--tbw-row-height);

    & .tbw-editor-host,
    & .tbw-editor-host .mat-mdc-form-field,
    & .tbw-editor-host .mat-mdc-text-field-wrapper,
    & .tbw-editor-host .mat-mdc-form-field-flex,
    & .tbw-editor-host .mat-mdc-form-field-infix {
      min-height: 0;
      height: 100%;
    }

    & .tbw-editor-host input.mat-mdc-input-element,
    & .tbw-editor-host .mat-mdc-select-trigger {
      height: var(--tbw-row-height);
      min-height: 0;
      padding-top: 0;
      padding-bottom: 0;
      line-height: 1;
    }
  }
}

/* Thin header — for grids that are sub-sections inside a larger page
   (e.g. a field-split table) where a full-height header competes
   visually with surrounding form headings.

   Apply with: <div class="tbw-thin-header"><tbw-grid ... /></div> */
.tbw-thin-header tbw-grid,
.tbw-thin-header [data-tbw-grid] {
  --tbw-header-height: 28px;
  --tbw-font-weight-header: 500;
  --tbw-font-size-header: 0.75rem;
}

/* ================================================================
 * 10. Dark Mode
 * ================================================================ */
body.sto-dark-theme {
  tbw-grid,
  [data-tbw-grid],
  .tbw-filter-panel,
  .tbw-context-menu,
  .tbw-overlay-panel {
    color-scheme: dark;
  }
}
