/*
 * The shared entity picker: an anchored popover on desktop, a modal bottom sheet
 * on mobile. Markup in app/views/shared/_entity_picker.html.erb, behaviour in
 * app/javascript/controllers/entity_picker_controller.js.
 *
 * Extracted from the comment composer, which is one of its two hosts; the other
 * is the setlist song picker. Non-colour values (radii, type sizes, spacing) come
 * from design 1a in docs/plans/comments-ui-handoff/ verbatim.
 *
 * Colours are mapped onto the app's theme variables below rather than
 * transplanting the design's own token set, so the picker sits correctly beside
 * the rest of the UI in both themes. The mapping is on :root rather than on a
 * host class because a picker can be mounted anywhere, and because custom
 * properties inherit down the DOM tree - which the dialog is still part of, even
 * when the browser is painting it in the top layer.
 *
 * --ep-primary follows --accent-color, which is user-configurable, so the picker
 * picks up whatever accent the member has chosen.
 */

:root {
  --ep-surface: var(--input-bg);
  --ep-raised: var(--dropdown-bg);
  --ep-control: var(--bg-color);
  --ep-hover: var(--hover-bg);
  --ep-border: var(--border-color);
  --ep-text: var(--text-color);
  --ep-primary: var(--accent-color);
  --ep-on-primary: var(--accent-contrast);
  --ep-shadow: var(--dropdown-shadow);
  --ep-danger: #d53e4f;
}

/* Muted text tiers. The app has no variables for these, so they are opacity
   steps on the resolved text colour, which works in both themes. */
.ep-row-shortcut,
.ep-result-meta,
.ep-picker-footer,
.ep-panel-label {
  color: var(--ep-text);
  opacity: 0.6;
}

.ep-result-subtitle {
  color: var(--ep-text);
  opacity: 0.7;
}

.ep-icons {
  display: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* --- Popover ------------------------------------------------------------- */

/*
 * Containing block for the no-Popover-API fallback, which positions the panel
 * absolutely. Hosts that already establish one (the composer's .cc-footer) do
 * not need it.
 *
 * inline-block so the box is the trigger's box rather than the full width of
 * whatever the trigger sits in. The right-start fallback below places the panel
 * at left: 100%, which is only the trigger's right edge if this shrinks to fit.
 */
.ep-anchor {
  position: relative;
  display: inline-block;
}

/*
 * The popover is a <dialog>. Visibility is the browser's job via the `open`
 * attribute, so there is no [hidden] rule here to be out-specified by a :has()
 * selector - [hidden] scores (0,2,0) and would lose to the (0,3,0) width rule
 * below, leaving the panel stuck open. Everything else overrides the UA dialog
 * defaults (border, padding, margin: auto, background, max-width/height).
 *
 * These absolute-position values are the fallback path only, for browsers
 * without the Popover API. When shown with showPopover() the element is in the
 * top layer and positioned from JavaScript - see :popover-open below.
 */
.ep-popover {
  position: absolute;
  bottom: 46px;
  left: 0;
  inset-inline-end: auto;
  z-index: 30;
  width: 268px;
  max-width: calc(100vw - 32px);
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: visible;
  color: var(--ep-text);
  background-color: var(--ep-raised);
  border: 1px solid var(--ep-border);
  border-radius: 12px;
  box-shadow: 0 18px 40px var(--ep-shadow);
  animation: ep-pop-in 0.14s ease-out;
}

/*
 * Top-layer popover (desktop). Positioned against the viewport by the controller
 * rather than by the offsets above, because the top layer is outside the normal
 * flow. This is what lets the panel escape .collapsible-content's overflow:
 * hidden, any backdrop-filter ancestor's containing block, and the fragmentation
 * of .setlist-sets--horizontal's CSS columns.
 */
.ep-popover:popover-open {
  position: fixed;
  inset: auto;
  margin: 0;
}

/* The picker is wider than the type list. */
.ep-popover:has(.ep-picker:not([hidden])) {
  width: 400px;
  overflow: hidden;
}

/*
 * Beside the trigger rather than above it, for a labelled button in the middle
 * of a page. Fallback path only, as above: the top-layer popover is placed by
 * placeBeside() in the controller, which also flips and clamps against the
 * viewport - neither of which CSS can do here.
 *
 * calc() rather than a margin, because :popover-open resets inset but not
 * margin, so a margin here would also shift the JavaScript-placed panel.
 *
 * This must stay above the bottom-sheet block at the end of the file. Both
 * selectors score (0,2,0), so the sheet only wins on source order.
 */
.ep-popover[data-placement="right-start"] {
  top: 0;
  bottom: auto;
  left: calc(100% + 8px);
}

@keyframes ep-pop-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

.ep-sheet-handle {
  display: none;
}

.ep-types {
  padding: 6px;
}

.ep-panel-label {
  padding: 8px 10px 6px;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* --- Rows (shared by the type list and the results) ---------------------- */

.ep-row {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 9px 10px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--ep-text);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}

.ep-row:hover,
.ep-row.is-highlighted {
  background-color: var(--ep-hover);
}

.ep-row[hidden] {
  display: none;
}

.ep-row-icon,
.ep-row-icon svg {
  width: 17px;
  height: 17px;
  flex: none;
  display: block;
}

.ep-row-label {
  flex: 1;
  font-size: 14px;
}

.ep-row-shortcut {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

/* --- Picker -------------------------------------------------------------- */

.ep-picker[hidden] {
  display: none;
}

.ep-picker-header {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--ep-border);
}

.ep-icon-button {
  display: flex;
  padding: 0;
  border: none;
  background: none;
  color: var(--ep-text);
  opacity: 0.65;
  cursor: pointer;
}

.ep-icon-button:hover {
  opacity: 1;
}

.ep-picker-type-icon,
.ep-picker-type-icon svg {
  width: 15px;
  height: 15px;
  flex: none;
  display: block;
}

.ep-search {
  flex: 1;
  min-width: 0;
  padding: 0;
  border: none;
  background: none;
  outline: none;
  color: var(--ep-text);
  font-family: inherit;
  font-size: 14px;
}

.ep-results {
  max-height: 250px;
  overflow-y: auto;
  padding: 6px;
}

/*
 * Two distinct selected states, and they must not be confused.
 *
 * Staged (.is-selected, still enabled): the row has been picked but not yet
 * committed, and tapping it again unstages it. It reads as active.
 *
 * Inert (:disabled): selecting was one-way and there is nothing further to do
 * with this row - the composer's already-attached results. It recedes.
 */
.ep-result-row.is-selected:not(:disabled) {
  background-color: var(--ep-hover);
}

.ep-result-row:disabled {
  opacity: 0.45;
  cursor: default;
}

.ep-result-check {
  display: flex;
  flex: none;
  color: var(--ep-primary);
}

.ep-result-check svg {
  width: 16px;
  height: 16px;
  display: block;
}

.ep-result-thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex: none;
  overflow: hidden; /* clips artwork to the rounded corner */
  border-radius: 6px;
  background-color: var(--ep-control);
}

.ep-result-thumb svg {
  width: 15px;
  height: 15px;
  display: block;
}

/* Real artwork fills the thumbnail slot; the icon variant stays inset. */
.ep-result-artwork {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 6px;
  object-fit: cover;
}

.ep-result-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.ep-result-title {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ep-result-subtitle {
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ep-result-meta {
  font-size: 12px;
  white-space: nowrap;
}

.ep-results-message {
  padding: 16px 12px;
  font-size: 13px;
  color: var(--ep-text);
  opacity: 0.6;
}

.ep-picker-footer {
  padding: 8px 12px;
  border-top: 1px solid var(--ep-border);
  font-size: 12px;
}

/* --- Commit footer ------------------------------------------------------- */
/* For hosts that stage a selection and submit it themselves. */

.ep-picker-footer--commit {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.ep-commit-error {
  flex: 1;
  min-width: 0;
  color: var(--ep-danger);
  font-size: 12px;
}

.ep-commit-error[hidden] {
  display: none;
}

/* Pushes the button right when there is no error text beside it. */
.ep-picker-footer--commit .ep-commit-error[hidden] + .btn {
  margin-left: auto;
}

/* --- Focus --------------------------------------------------------------- */
/* The prototype does not draw focus rings; production must. */

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

/* --- Mobile: bottom sheet ------------------------------------------------ */
/*
 * Below 640px, or on any coarse pointer, the popover becomes a bottom sheet.
 * Keep this breakpoint in step with MOBILE_QUERY in
 * entity_picker_controller.js.
 */

@media (max-width: 640px), (pointer: coarse) {
  /*
   * Opened with showModal(), so this is in the top layer and its position is
   * resolved against the viewport regardless of any backdrop-filter ancestor
   * (.glass-card, .message-detail and .set-card all have one, which would
   * otherwise make them the containing block and leave the "sheet" floating
   * mid-card).
   *
   * The UA modal defaults that need overriding: inset: 0, margin: auto,
   * max-width: calc(100% - 6px - 2em), max-height: calc(100% - 6px - 2em).
   */
  .ep-popover[open] {
    position: fixed;
    inset: auto 0 0 0;
    width: 100%;
    max-width: none;
    max-height: 78vh;
    margin: 0;
    display: flex;
    flex-direction: column;
    padding: 10px 0 calc(18px + env(safe-area-inset-bottom));
    overflow: hidden;
    border: none;
    border-top: 1px solid var(--ep-border);
    border-radius: 16px 16px 0 0;
    animation: ep-sheet-up 0.2s ease-out;
  }

  /* The scrim. A modal dialog gets this for free - no element required. */
  .ep-popover::backdrop {
    background-color: rgba(10, 11, 14, 0.6);
  }

  /* Neutralise the desktop picker width; the sheet is always full-bleed. */
  .ep-popover:has(.ep-picker:not([hidden])) {
    width: 100%;
    max-width: none;
  }

  @keyframes ep-sheet-up {
    from { transform: translateY(100%); }
    to   { transform: none; }
  }

  .ep-sheet-handle {
    display: block;
    width: 36px;
    height: 4px;
    margin: 0 auto 10px;
    border-radius: 2px;
    background-color: var(--ep-border);
  }

  .ep-types {
    padding: 0;
  }

  .ep-panel-label {
    padding: 0 18px 8px;
  }

  /* 44px minimum touch target. */
  .ep-row {
    gap: 13px;
    padding: 13px 18px;
    border-radius: 0;
  }

  .ep-row-icon,
  .ep-row-icon svg {
    width: 19px;
    height: 19px;
  }

  .ep-row-label {
    font-size: 15px;
  }

  /* Shortcuts are keyboard affordances; no keyboard, no point. */
  .ep-row-shortcut {
    display: none;
  }

  .ep-picker {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .ep-picker-header {
    padding: 0 16px 10px;
  }

  .ep-results {
    max-height: none;
    flex: 1;
    padding: 6px 8px;
  }

  .ep-picker-footer {
    padding: 10px 16px 0;
  }
}

/* --- Reduced motion ------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  .ep-popover {
    animation: none;
  }

  .ep-row {
    transition: none;
  }
}
