/* The label of a button that only shows its icon, under it while it's hovered or focused with the keyboard. Inside
   .tooltips-above it goes over the button instead, for bars along the bottom of the screen */
:where([data-tooltip]) {
  position: relative;
}

[data-tooltip] {
  &::after {
    content: attr(data-tooltip);
    position: absolute;
    top: var(--tooltip-top, calc(100% + var(--space-1)));
    bottom: var(--tooltip-bottom, auto);
    left: 50%;
    z-index: 30;
    display: var(--tooltip-display, block);
    translate: -50% 0;

    padding: var(--space-1) var(--space-2);
    border-radius: var(--space-1);

    background-color: var(--text-color);
    color: var(--bg-color);
    font-size: 0.75rem;
    font-weight: normal;
    line-height: 1;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;

    transition: opacity 0.15s ease-out,
                box-shadow 0.15s ease-out;
  }

  &:focus-visible::after {
    opacity: 1;
  }

  /* Only on hover, so a tap on a touch screen doesn't leave it showing */
  @media (hover: hover) {
    &:hover::after {
      opacity: 1;
      box-shadow: 0 0 12px 2px var(--bg-color);
    }
  }

  /* Without hover it only shows with keyboard focus, so it isn't drawn otherwise. Safari on iPhone briefly shows one
     added to an element already on the page, such as a tile marked gone, even while it's see-through */
  @media (hover: none) {
    &:not(:focus-visible)::after {
      display: none;
    }
  }

  /* At the left edge of the screen, it lines up with the button's left side instead of spilling off */
  &.tooltip-start::after {
    left: 0;
    translate: none;
  }
}

/* A button with its label beside its icon needs none, unless a bar hides the label for lack of room and sets
   --tooltip-display again */
:where([data-tooltip]:has(> span:not(.icon))) {
  --tooltip-display: none;
}

.tooltips-above {
  --tooltip-top: auto;
  --tooltip-bottom: calc(100% + var(--space-1));
}

/* Dialogs opened from such a bar have theirs below again */
dialog {
  --tooltip-top: initial;
  --tooltip-bottom: initial;
}
