.popover {
  display: inline-block;
  max-width: 100%;
}

.popover-trigger {
  cursor: pointer;
}

/* A marked chip's words give way first, so a clipped chip keeps its caret. */
.popover-trigger-words {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* The caret that says a click opens a panel. The empty alt text keeps it
   out of the accessible name. */
.popover-marker::after {
  content: "▾" / "";
  flex: none;
}

.popover-trigger:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* A [popover] starts centred in the viewport (margin: auto, inset: 0) with
   the browser's border and padding; clear those so the anchor places it.
   It opens under its trigger when it fits there, else above when it fits
   there, as it always has. A panel too tall for either side is held to the
   room on one side and scrolls inside itself (popover-held, below). */
.popover-panel {
  --popover-held-floor: 10rem;

  inset: auto;
  margin: var(--space-1) 0;
  max-width: calc(100vw - 2 * var(--space-2));
  max-height: calc(100vh - 2 * var(--space-2));
  overflow: auto;
  overscroll-behavior: contain;
  padding: var(--space-1);
  border: var(--line-width) solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-float);
  position-try-fallbacks:
    flip-block,
    flip-inline,
    flip-block flip-inline,
    --popover-held,
    --popover-held flip-block,
    --popover-held flip-inline,
    --popover-held flip-block flip-inline;
}

/* The last resort for a long panel: position-area makes the room on the
   side it opens its containing block, so 100% less the margins is the room
   between the trigger and the screen's edge. The floor keeps it from
   taking a side with only a sliver of room: that side fails and the other
   is tried. */
@position-try --popover-held {
  max-height: calc(100% - 2 * var(--space-1));
  min-height: var(--popover-held-floor);
}

.popover-panel-end {
  --popover-area: bottom span-left;
  position-area: var(--popover-area);
}

.popover-panel-start {
  --popover-area: bottom span-right;
  position-area: var(--popover-area);
}

/* A panel that is a tool rather than a menu (a picker, a list to read): on
   a phone it leaves its trigger and comes up from the foot of the screen
   as a sheet, the screen's width less a margin, over a backdrop, where a
   thumb reaches it and nothing runs off an edge. With room it hangs from
   its trigger like any other panel. */
.popover-panel-sheet {
  position-area: none;
  inset: auto var(--space-2) var(--space-2);
  margin: 0;
  position-try-fallbacks: none;
}

.popover-panel-sheet::backdrop {
  background: var(--backdrop);
}

@media (min-width: 40rem) {
  .popover-panel-sheet {
    position-area: var(--popover-area);
    inset: auto;
    margin: var(--space-1) 0;
    position-try-fallbacks:
      flip-block,
      flip-inline,
      flip-block flip-inline,
      --popover-held,
      --popover-held flip-block,
      --popover-held flip-inline,
      --popover-held flip-block flip-inline;
  }

  .popover-panel-sheet::backdrop {
    background: transparent;
  }
}
