/**
 * @file
 * Replacement styles for table drag.
 *
 * Replaces core's tabledrag.module.css.
 *
 * @see tabledrag.js
 */

body.drag {
  cursor: move;
}
body.drag-y {
  cursor: row-resize;
}

.draggable-table {
  --table-row--dragging-bg-color: #fe7;
  --table-row--last-dragged-bg-color: #ffb;
  --tabledrag-handle-icon-size: 17px;

  /**
  * Reduce the spacing of draggable table cells.
  */
  & td:first-child ~ td,
  & th:first-child ~ th {
    padding-inline-start: 0;
  }
}

/* The block region's title row in table. */
.region-title {
  font-weight: bold;
}

/* Empty region message row in table. */
.region-message {
  color: var(--color-gray-600);

  &.region-populated {
    /* If the region is populated, we shouldn't display the empty message. */
    display: none;
  }
}

/**
 * Remove border-bottom from abbr element. Class is duplicated in the selector
 * to increase weight to be able to win normalize.css selectors.
 */
.tabledrag-changed.tabledrag-changed {
  border-bottom: none;
}

.tabledrag-changed {
  /* Don't display the abbreviation of 'add-new' table rows. */
  @nest .add-new & {
    display: none;
  }

  @nest .draggable & {
    position: relative;
    inset-inline-start: calc(var(--space-xs) * -1);
  }

  @nest .tabledrag-cell--only-drag & {
    width: var(--space-l);
    min-width: var(--space-l);
  }
}

/**
 * Draggable row state colors.
 */
.draggable.drag,
.draggable.drag:focus {
  background-color: var(--table-row--dragging-bg-color);
}

.draggable.drag-previous {
  background-color: var(--table-row--last-dragged-bg-color);
}

/* Auto width for weight selects and number inputs. */
.draggable td .form-element--type-select[name$="][_weight]"], /* Multiple field */
.draggable td .term-weight, /* Taxonomy term list */
.draggable td .field-weight /* Field UI table */ {
  width: auto;
}

/**
 * Handle styles.
 */
.tabledrag-handle {
  position: relative;
  z-index: 1;
  overflow: visible;
  cursor: move;
  text-align: center;
  vertical-align: text-top;

  &::after {
    display: inline-block;
    width: var(--tabledrag-handle-icon-size);
    height: var(--tabledrag-handle-icon-size);
    margin-inline-start: calc(var(--space-m) * -1);
    padding: var(--space-xs) var(--space-m);
    content: "";
    transition: transform 0.1s ease-in-out 0s;
    background: url(../../images/icons/000f33/tabledrag-handle.svg) no-repeat center;

    @media (forced-colors: active) {
      background: linktext;
      mask-image: url(../../images/icons/000f33/tabledrag-handle.svg);
      mask-repeat: no-repeat;
      mask-position: center;
    }
  }
}

/* Change icon and cursor if only vertical drag is allowed. */
.tabledrag-handle.tabledrag-handle-y {
  cursor: row-resize;
}

.tabledrag-handle.tabledrag-handle-y::after {
  background-image: url(../../images/icons/currentColor/tabledrag-handle-y.svg);
  background-size: 16px 16px;
}

.tabledrag-handle::after,
.tabledrag-disabled .tabledrag-handle.tabledrag-handle.tabledrag-handle::after {
  transform: scale(1);
}

.tabledrag-handle:is(:hover, :focus)::after,
.draggable.drag .tabledrag-handle::after {
  transform: scale(1.25);
}

.tabledrag-handle:focus {
  outline: none !important;
  box-shadow: none !important;

  &::before {
    display: block;
    width: calc(var(--space-m) + (var(--space-xs) * 2)); /* Same as height. */
    height: calc(var(--space-m) + (var(--space-xs) * 2)); /* Hande svg height + its vertical padding */
    margin: 0 calc(var(--space-xs) * -1) calc((var(--space-m) + (var(--space-xs) * 2)) * -1); /* Bottom: handle height as negative value. */
    content: "";
    border-radius: var(--base-border-radius);
    outline: var(--outline-size) dotted transparent;
    box-shadow: 0 0 0 var(--focus-border-size) var(--color-focus);
  }
}

/* Disabled tabledrag handle. */
.tabledrag-disabled {
  & .tabledrag-handle {
    cursor: default;
    opacity: 0.4;

    &.tabledrag-handle::before {
      content: none;
    }
  }
}

/**
 * Enhancements for touch-capable screens.
 */

/**
 * Increase handle size.
 */
.touchevents {
  & .tabledrag-handle::after {
    padding-block: var(--space-s);
  }

  & .draggable .menu-item__link {
    padding-block: var(--space-xs);
  }
}

/**
 * Wrapper of the toggle weight button (styled as a link).
 */
.tabledrag-toggle-weight-wrapper {
  text-align: end;

  /* Hide nested weight toggles as they are redundant. */
  @nest .draggable-table & {
    display: none;
  }
}

/**
 * Keep crowded tabledrag cells vertically centered.
 */
.tabledrag-cell {
  padding-block: 0;
}

/**
 * If the first table cell is empty (like in a multiple field widget table),
 * we can save some space for the following cells.
 * If it isn't empty (Field UI table, taxonomy term overview page), this CSS
 * class won't be added.
 */
.tabledrag-cell--only-drag {
  width: 1px; /* This forces this cell to use the smallest possible width. */
  padding-inline-end: 0;
}

.tabledrag-cell-content {
  display: flex;
  align-items: center;
  height: 100%;

  & > * {
    display: table-cell;
    vertical-align: middle;
  }

  & .tree {
    flex-grow: 1;
  }

  & .tabledrag-handle::after {
    vertical-align: middle;
  }
}

.tabledrag-cell-content__item {
  padding-inline-end: var(--space-xs);

  &:empty {
    display: none;
  }
}

/**
 * Indentation.
 */
.indentation {
  position: relative;
  inset-inline-start: calc(var(--space-xs) * -0.5);
  float: left; /* LTR */
  width: 25px;
  height: 25px;
  background: none !important;
  line-height: 0;

  @nest .tabledrag-cell-content & {
    /* Fixes Safari bug (16.1 at least) where table rows are overly large when
      using indentation (e.g. re-ordering menu items. */
    display: inline-flex;
    float: none;
    overflow: hidden;
    flex-direction: column;
    height: 100%;
  }

  @nest [dir="rtl"] & {
    float: right;
  }
}

/**
 * Tree is the visual representation for the simultaneously moved draggable
 * rows.
 *
 * These rules are styling the inline SVG that is placed inside the .indentation
 * element.
 */
.tree {
  width: 25px;
  height: 25px;
}

.tree__item {
  display: none;
}

/* LTR tree child. */
.tree-child path {
  &:not(.tree__item-child-ltr) {
    display: none;
  }

  &.tree__item-child-ltr {
    display: block;
  }
}

/* RTL tree child. */
[dir="rtl"] {
  & .tree-child path {
    &:not(.tree__item-child-rtl) {
      display: none;
    }
    &.tree__item-child-rtl {
      display: block;
    }
  }

  /* Last RTL tree child. */
  & .tree-child-last path {
    &:not(.tree__item-child-last-rtl) {
      display: none;
    }
    &.tree__item-child-last-rtl {
      display: block;
    }
  }
}

/* Last LTR tree child. */
.tree-child-last path {
  &:not(.tree__item-child-last-ltr) {
    display: none;
  }
  &.tree__item-child-last-ltr {
    display: block;
  }
}

/* Horizontal line. */
.tree-child-horizontal path {
  &:not(.tree__item-horizontal) {
    display: none;
  }

  &.tree__item-horizontal {
    display: block;
  }
}
