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

@import "../base/media-queries.pcss.css";

/*
 * When mouse is pressed, scrollbar disappears. This prevents the horizontal
 * layout shift from occurring
 */
html {
  scrollbar-gutter: stable;
}

body {
  &.drag {
    cursor: move;
  }

  &.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;

  /*
   * We remove all padding and hard-code the height of the <td> elements to
   * ensure that the "tree" icons span full height. You can replicate this in
   * the UI by clicking and holding down the drag icon. The tree icons will
   * become visible underneath.
   */
  :is(th, td) {
    height: 60px;

    html[data-gin-layout-density="medium"] & {
      height: 50px;
    }

    html[data-gin-layout-density="small"] & {
      height: 40px;
    }

    .layer-wrapper {
      margin-block: var(--gin-spacing-density-s);
    }

    .draggable-table {
      margin-block: 0;
    }

    :is(th, td) {
      height: auto;
    }
  }

  /**
  * Reduce the spacing of draggable table cells.
  */
  & :is(th, td):first-child ~ :is(th, td) {
    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. */
  .add-new & {
    display: none;
  }

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

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

/**
 * Draggable row state colors.
 */

.draggable {
  &.region-title,
  &.region-title:focus {
    background-color: var(--table-row--dragging-bg-color);
  }

  &.region-title-previous {
    background-color: var(--table-row--last-dragged-bg-color);
  }

  &.drag-previous {
    background-color: var(--gin-bg-layer2);
    box-shadow: inset 6px 0 0 0 var(--gin-color-warning);

    &:dir(rtl) {
      box-shadow: inset -6px 0 0 0 var(--gin-color-warning);
    }

    &::after {
      position: absolute;
      width: 6px;
      height: 100%;
      content: "";
    }

    @media (forced-colors: active) {
      position: relative;

      &::after {
        border-inline-start: 6px solid linktext;
        inset-inline-start: 0;
      }
    }
  }

  &.drag,
  &.drag:focus {
    background-color: var(--gin-bg-layer2);

    .gin--dark-mode & {
      background-color: var(--gin-color-primary-light-hover);
    }
  }

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

  a.tabledrag-handle {
    float: none;
    vertical-align: middle;
  }
}

/**
 * Handle styles.
 */
.tabledrag-handle {
  position: relative;
  z-index: 1;
  display: flex;
  overflow: visible;
  place-content: center;
  box-sizing: border-box;
  height: 44px;
  cursor: move;
  text-align: center;
  vertical-align: text-top;
  margin-inline: calc(-1 * var(--space-s)) var(--space-s);
  padding-inline: var(--space-s);

  &::after {
    display: inline-block;
    box-sizing: border-box;
    width: var(--tabledrag-handle-icon-size);
    height: var(--tabledrag-handle-icon-size);
    padding: var(--space-xs);
    content: "";
    transition: transform 0.1s ease-in-out 0s;
    background-color: var(--gin-color-text-light);
    mask-image: var(--admin-icon-drag);
    mask-repeat: no-repeat;
    mask-position: center center;
    mask-size: 16px 16px;

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

      @supports (mask-composite: source-over) {
        background-color: var(--gin-color-text-light);
      }
    }
  }

  &:focus {
    outline-offset: calc(-1 * var(--outline-size, 0px));
  }

  &:hover {
    &::after {
      transform: scale(1);

      @media (forced-colors: none) {
        background-color: var(--gin-color-primary);
      }
    }
  }
}

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

  &::after {
    mask-image: var(--admin-icon-drag-y);
  }
}

.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);
}

/* 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;
  line-height: calc(28rem / 16);

  & + table,
  & + .tableresponsive-toggle-columns + table {
    margin-block-start: 0;
  }

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

  #field-display-overview,
  #field-display-overview-wrapper,
  .field--widget-entity-reference-paragraphs,
  .field--type-link,
  .field--type-smartdate {
    .gin-layer-wrapper > & {
      .action-link {
        pointer-events: all;
        background-color: var(--gin-bg-layer);
      }
      @media (--admin-medium) {
        position: relative;
        z-index: 1;
        margin-block-end: calc(var(--gin-spacing-xxl) * -1);
        pointer-events: none;
      }
    }
  }

  .action-link {
    margin-block-end: var(--gin-spacing-xs);

    .action-link {
      margin-block-end: 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;
  }

  .table-file-multiple-widget & {
    display: flex;
  }
}

.tabledrag-cell-content__item {
  display: flex;
  align-items: center;
  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;

  .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%;
  }

  [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;
  }
}

/* @todo do these styles belong here? They were migrated from Gin. */
.field-plugin-settings-edit-wrapper,
.field-plugin-settings-edit-wrapper + a {
  margin-block: var(--gin-spacing-m);
}

.field-plugin-settings-edit-wrapper + a {
  margin-block-start: var(--gin-spacing-s);
}

/* Correctly align bulk action checkboxes when used in combination with tabledrag */
table:has(.tabledrag-cell-content__item .form-checkbox) th.select-all .form-checkbox {
  margin-inline-start: calc(var(--tabledrag-handle-icon-size, calc(17rem / 16)) + var(--gin-spacing-m));
}
