/*
 * Responsible for: how the Map looks and moves: the coloured rectangles, their light-up under the
 * pointer, the pop-in with its white flash, the rectangles held back until their turn in a live reading,
 * the zoom, and the small box that follows the pointer.
 *
 * Must never: hold a raw colour or a made-up size (values come from tokens.css; the few pixel sizes
 * here are geometry), animate anything but transform and opacity, or let a state change move the
 * things around the Map: it only ever fills the box it is given.
 */

@layer kit {
  .ia-map {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 0;
    overflow: hidden;
    contain: layout paint;
    border-radius: var(--r-md);
    background: var(--sunken);
    color: var(--pop);
    font-family: var(--sans);
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  .ia-map__stage,
  .ia-map__layer {
    position: absolute;
    inset: 0;
  }

  .ia-map__layer {
    transform-origin: 0 0;
  }

  .ia-map__layer.is-moving {
    will-change: transform, opacity;
  }

  .ia-map__layer.is-outgoing {
    pointer-events: none;
  }

  /* While a layer zooms its names are put away: stretched text reads as a glitch, not as motion. */
  .ia-map__label,
  .ia-map__strip {
    transition: opacity var(--fast) var(--ease);
  }

  .ia-map__layer.is-moving .ia-map__label,
  .ia-map__layer.is-moving .ia-map__strip {
    opacity: 0;
  }

  .ia-map__tile,
  .ia-map__inner {
    position: absolute;
    box-sizing: border-box;
    overflow: hidden;
    border-radius: var(--r-sm);
    background: var(--c);
  }

  .ia-map__tile {
    margin: 0;
    padding: 0;
    border: 0;
    appearance: none;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
  }

  /*
   * A folder that shows rectangles inside it is a shade darker than they are, so the folder reads
   * as one container and the rectangles keep the folder's true colour (white text stays readable).
   */
  .ia-map__block {
    background: color-mix(in srgb, var(--c) 76%, var(--ink));
  }

  .ia-map__tile--in,
  .ia-map__inner {
    border-radius: 2px;
  }

  /* Light-up under the pointer (::after) and the white flash of a new rectangle (::before). */
  .ia-map__tile::before,
  .ia-map__inner::before,
  .ia-map__tile::after {
    content: "";
    position: absolute;
    inset: 0;
    background: var(--pop);
    opacity: 0;
    pointer-events: none;
  }

  .ia-map__tile::after {
    z-index: 2;
    transition: opacity var(--fast) var(--ease);
  }

  .ia-map__tile::before,
  .ia-map__inner::before {
    z-index: 3;
  }

  @media (hover: hover) {
    .ia-map__tile:hover::after {
      opacity: 0.22;
    }
  }

  .ia-map__tile:active::after {
    opacity: 0.32;
  }

  .ia-map__tile:focus-visible {
    z-index: 4;
    outline: 2px solid var(--pop);
    outline-offset: -2px;
    box-shadow: inset 0 0 0 4px var(--ink);
  }

  /*
   * A category pointed at under the Map: its rectangle (and everything inside it) keeps its full colour and is
   * outlined, the others are quieted. Colour, opacity and outline only: no size or place changes.
   */
  .ia-map__layer .ia-map__tile {
    transition: opacity var(--fast) var(--ease);
  }

  .ia-map__layer.has-pick > .ia-map__tile:not(.is-in-pick) {
    opacity: var(--quieted);
  }

  .ia-map__layer.has-pick > .ia-map__tile.is-in-pick {
    z-index: 4;
  }

  .ia-map__layer.has-pick .ia-map__tile.is-picked {
    outline: 2px solid var(--pop);
    outline-offset: -2px;
    box-shadow: inset 0 0 0 3px var(--ink);
  }

  .ia-map__label {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    padding: var(--s-1) var(--s-1) 0 var(--s-2);
    pointer-events: none;
  }

  .ia-map__name,
  .ia-map__count {
    display: block;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: var(--t-xs);
    line-height: 1.3;
  }

  .ia-map__name {
    font-weight: 600;
  }

  .ia-map__label--big .ia-map__name {
    font-size: var(--t-md);
  }

  .ia-map__label--big .ia-map__count {
    font-size: var(--t-sm);
  }

  .ia-map__strip {
    position: absolute;
    inset: 0 0 auto;
    display: flex;
    align-items: center;
    gap: var(--s-2);
    padding: 0 var(--s-2);
    pointer-events: none;
  }

  .ia-map__strip .ia-map__name {
    flex: 1 1 auto;
    font-size: var(--t-sm);
  }

  .ia-map__strip .ia-map__count {
    flex: 0 0 auto;
    font-size: var(--t-sm);
  }

  .ia-map__empty {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    margin: 0;
    color: var(--muted);
    font-size: var(--t-sm);
  }

  /* The pop-in: one pre-made animation per rectangle, started by its own delay. */
  .ia-map--pop .ia-map__unit {
    animation: ia-map-pop var(--move) var(--spring) both;
    animation-delay: var(--d, 0ms);
  }

  .ia-map--pop .ia-map__flash::before {
    animation: ia-map-flash var(--slow) var(--ease) both;
    animation-delay: var(--d, 0ms);
  }

  .ia-map--pop .ia-map__block {
    animation: ia-map-fade var(--move) var(--ease) both;
    animation-delay: var(--d, 0ms);
  }

  /* A rectangle of a reading that is still under way, drawn in its place until its turn: it shows nothing and takes no press. */
  .ia-map__wait {
    opacity: 0;
    pointer-events: none;
  }

  @keyframes ia-map-pop {
    from {
      opacity: 0;
      transform: scale(0.6);
    }

    to {
      opacity: 1;
      transform: none;
    }
  }

  @keyframes ia-map-fade {
    from {
      opacity: 0;
    }

    to {
      opacity: 1;
    }
  }

  @keyframes ia-map-flash {
    from {
      opacity: 0.92;
    }

    to {
      opacity: 0;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .ia-map--pop .ia-map__unit,
    .ia-map--pop .ia-map__flash::before,
    .ia-map--pop .ia-map__block {
      animation: none;
    }
  }

  /* The small box that follows the mouse. */
  .ia-map__tip {
    position: absolute;
    left: 0;
    top: 0;
    z-index: var(--z-popover);
    display: flex;
    flex-direction: column;
    gap: 2px;
    max-width: 16em;
    padding: var(--s-2) var(--s-3);
    border-radius: var(--r-sm);
    background: var(--ink);
    color: var(--pop);
    box-shadow: var(--shadow-2);
    pointer-events: none;
    opacity: 0;
    transition: opacity var(--fast) var(--ease);
  }

  .ia-map__tip.is-on {
    opacity: 1;
  }

  .ia-map__tip-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: var(--t-sm);
    font-weight: 600;
  }

  .ia-map__tip-line {
    font-size: var(--t-xs);
    opacity: 0.8;
    white-space: nowrap;
  }
}
