:host {
  /* Square colors */
  --square-color-dark: hsl(145deg 32% 44%);
  --square-color-light: hsl(51deg 24% 84%);

  /* Square colors when in :hover state. */
  --square-color-dark-hover: hsl(144deg 75% 44%);
  --square-color-light-hover: hsl(52deg 98% 70%);

  /* Square colors when the square is actively being interacted with (moved). */
  --square-color-dark-active: hsl(142deg 77% 43%);
  --square-color-light-active: hsl(50deg 95% 64%);

  /* Color of outline when square is actively being interacted with (moved). */
  --outline-color-dark-active: hsl(138deg 85% 53% / 95%);
  --outline-color-light-active: hsl(66deg 97% 72% / 95%);

  /* Color of outline when square has focus. */
  --outline-color-focus: hsl(30deg 94% 55% / 90%);

  /* Outline size properties. */
  --outline-blur-radius: 3px;
  --outline-spread-radius: 4px;

  /* Label font properties. */
  --coords-font-size: 0.7rem;
  --coords-font-family: sans-serif;

  /* Width of wrapping gutter when showing coordinates outside board. */
  --outer-gutter-width: 4%;

  /* Width of inner border surrounding board. */
  --inner-border-width: 1px;

  /* Padding applied to coordinates when shown inside the board. */
  --coords-inside-coord-padding-left: 0.5%;
  --coords-inside-coord-padding-right: 0.5%;

  /* Square marker colors when the square is an eligible move target. */
  --move-target-marker-color-dark-square: hsl(144deg 64% 9% / 90%);
  --move-target-marker-color-light-square: hsl(144deg 64% 9% / 90%);

  /* Radius of marker on move-target square. */
  --move-target-marker-radius: 24%;

  /* Radius of marker on move-target square that is occupied. */
  --move-target-marker-radius-occupied: 82%;

  /* Opacity of ghost piece shown while dragging. */
  --ghost-piece-opacity: 0.35;

  /* z-index of piece while dragging. */
  --piece-drag-z-index: 9999;

  /* Amount to scale up a piece when doing a coarse (touch) drag. */
  --piece-drag-coarse-scale: 2.4;

  /* Padding applied around piece. */
  --piece-padding: 3%;

  /* Arrow colors. */
  --arrow-color-primary: hsl(40deg 100% 50% / 80%);
  --arrow-color-secondary: hsl(7deg 93% 61% / 80%);

  /* Change from default `display` attribute, which is inline. */
  display: block;
}

:host([hidden]) {
  /* Ensure that 'hidden' attribute is respected despite display override. */
  display: none;
}

.board {
  width: 100%;
  box-sizing: border-box;
  border: var(--inner-border-width) solid
    var(--inner-border-color, var(--square-color-dark));
  border-collapse: collapse;
  table-layout: fixed;

  /* Prevent native selection action on any of the children. */
  user-select: none;
}

.board > tr > td {
  position: relative;

  /* Width of table cell is implicitly 12.5% because row will have
     exactly 8 columns and `table-layout` is fixed. Padding of 12.5% * width
     ensures square aspect ratio. */
  padding: 12.5% 0 0;
}

[data-square] {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: var(--p-square-color);
  color: var(--p-label-color);
  font-family: var(--coords-font-family);
  font-size: var(--coords-font-size);
  inset: 0;
}

[data-square]:focus {
  box-shadow: inset 0 0 var(--outline-blur-radius) var(--outline-spread-radius)
    var(--outline-color-focus);
  outline: none;
}

[data-square].marked-target {
  /* Add pixel offset to boundary to reduce jaggedness. */
  background: radial-gradient(
    var(--p-move-target-marker-color) var(--move-target-marker-radius),
    var(--p-square-color) calc(var(--move-target-marker-radius) + 1px)
  );
}

[data-square].moveable {
  /* Disable default touch actions like drag if square has a moveable piece. */
  touch-action: none;
}

[data-square].has-piece.marked-target,
[data-square].has-content.marked-target {
  /* Wide circle with transparent interior if square is occupied. */
  background: radial-gradient(
    var(--p-square-color) var(--move-target-marker-radius-occupied),
    var(--p-move-target-marker-color)
      calc(var(--move-target-marker-radius-occupied) + 1px)
  );
}

[data-square].move-start {
  --p-square-color: var(--p-square-color-active);
}

[data-square].move-start:not(:focus) {
  /* Add additional outline to move start square when actively moving. */
  box-shadow: inset 0 0 var(--outline-blur-radius) var(--outline-spread-radius)
    var(--p-outline-color-active);
}

@media (hover: hover) {
  /* Hover styles only shown on devices with mouse pointers.
     Prevent iOS Safari from persisting a hover style. */
  [data-square]:is(.moveable, .move-target):hover {
    --p-square-color: var(--p-square-color-hover);
  }
}

/* Artificial .hover class is applied during drag and keyboard moves. */
[data-square].hover {
  --p-square-color: var(--p-square-color-hover);
}

table:not(.dragging) [data-square]:is(.moveable, .move-start, .move-target) {
  cursor: pointer;
}

table.dragging {
  cursor: grab;
}

/***********
 * Wrappers *
 ***********/

.wrapper,
.board-arrows-wrapper {
  position: relative;
}

/***************
 * Coordinates *
 ***************/

.coords {
  position: absolute;
  display: none;
  font-family: var(--coords-font-family);
  font-size: var(--coords-font-size);
  pointer-events: none;
  touch-action: none;
  user-select: none;
}

.coord {
  display: flex;
  box-sizing: border-box;
}

.coords.file > .coord {
  width: 12.5%;
}

.coords.rank {
  flex-direction: column;
}

.coords.rank > .coord {
  height: 12.5%;
}

.wrapper.outside {
  padding: var(--outer-gutter-width);
  background-color: var(--square-color-light);
}

.wrapper.outside > .coords {
  display: flex;
  color: var(--square-color-dark);
}

.wrapper.outside > .coords > .coord {
  align-items: center;
  justify-content: center;
}

.wrapper.outside > .coords.file {
  right: var(--outer-gutter-width);
  bottom: 0;
  left: var(--outer-gutter-width);
  width: calc(100% - 2 * var(--outer-gutter-width));
  height: var(--outer-gutter-width);
}

.wrapper.outside > .coords.rank {
  top: var(--outer-gutter-width);
  bottom: var(--outer-gutter-width);
  left: 0;
  width: var(--outer-gutter-width);
  height: calc(100% - 2 * var(--outer-gutter-width));
}

.wrapper.inside > .coords {
  display: flex;
  width: 100%;
  height: 100%;
  inset: 0;
}

.wrapper.inside > .coords > .coord.light {
  color: var(--square-color-dark);
}

.wrapper.inside > .coords > .coord.dark {
  color: var(--square-color-light);
}

.wrapper.inside > .coords.file > .coord {
  align-items: flex-end;
  justify-content: flex-end;
  padding-right: var(--coords-inside-coord-padding-right);
}

.wrapper.inside > .coords.rank > .coord {
  padding-left: var(--coords-inside-coord-padding-left);
}

/**************************
 * Square color overrides *
 **************************/

[data-square-color="dark"] {
  --p-square-color: var(--square-color-dark);

  /* Label colors for inline labels should be inverse of square color */
  --p-label-color: var(--square-color-light);
  --p-square-color-hover: var(--square-color-dark-hover);
  --p-move-target-marker-color: var(--move-target-marker-color-dark-square);
  --p-square-color-active: var(--square-color-dark-active);
  --p-outline-color-active: var(--outline-color-dark-active);
}

[data-square-color="light"] {
  --p-square-color: var(--square-color-light);
  --p-label-color: var(--square-color-dark);
  --p-square-color-hover: var(--square-color-light-hover);
  --p-move-target-marker-color: var(--move-target-marker-color-light-square);
  --p-square-color-active: var(--square-color-light-active);
  --p-outline-color-active: var(--outline-color-light-active);
}

/****************
 * Piece styles *
 ****************/

[data-square] .piece,
[data-square] .slot {
  /* Always use absolute positioning for pieces and slotted
    content (custom SVGs, custom content etc) */
  position: absolute;
  width: 100%;
  height: 100%;
  inset: 0;
  pointer-events: none;
}

[data-square] .piece {
  z-index: 10;
  box-sizing: border-box;
  padding: var(--piece-padding);

  /* Make background placement respect padding. */
  background-origin: content-box;
  background-repeat: no-repeat;
  background-size: cover;
}

[data-square] .piece.moving {
  z-index: 15;
}

[data-square] .piece.secondary {
  z-index: 5;
  opacity: var(--ghost-piece-opacity);
}

[data-square].move-start .piece:not(.secondary) {
  /* Ensure that the piece appears above most other elements in document. */
  z-index: var(--piece-drag-z-index);
}

@media (pointer: coarse) {
  /* Override scale value for piece when dragging on a  device. */
  [data-square] .piece {
    --p-piece-drag-scale: var(--piece-drag-coarse-scale);
  }
}

/**********
 * Pieces *
 **********/

.bb {
  background-image: url("../pieces/bb.svg");
}

.bk {
  background-image: url("../pieces/bk.svg");
}

.bn {
  background-image: url("../pieces/bn.svg");
}

.bp {
  background-image: url("../pieces/bp.svg");
}

.bq {
  background-image: url("../pieces/bq.svg");
}

.br {
  background-image: url("../pieces/br.svg");
}

.wb {
  background-image: url("../pieces/wb.svg");
}

.wk {
  background-image: url("../pieces/wk.svg");
}

.wn {
  background-image: url("../pieces/wn.svg");
}

.wp {
  background-image: url("../pieces/wp.svg");
}

.wq {
  background-image: url("../pieces/wq.svg");
}

.wr {
  background-image: url("../pieces/wr.svg");
}

/**********
 * Arrows *
 **********/

.arrows {
  position: absolute;
  z-index: 20;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border: var(--inner-border-width) solid transparent;
  inset: 0;
  pointer-events: none;
  touch-action: none;
}

.arrow-primary {
  color: var(--arrow-color-primary);
}

.arrow-secondary {
  color: var(--arrow-color-secondary);
}
