/* Browse-table cell clamping, overflow affordance, and scroll shadows.
 *
 * Hand-authored plain CSS, deliberately: `embed.css` is a checked-in Tailwind
 * v4 build and there is no Tailwind build step left in-tree, so a new utility
 * class written in a Maud template would silently resolve to nothing. Anything
 * this feature needs that the frozen bundle doesn't already contain lives here.
 *
 * Loaded by `browse::framework::render::render_table`, which emits a <link> to this
 * file next to the <browse-cells> module script -- the same self-contained
 * shape `<category-panel>` uses. Repeat tables on one page re-request the same
 * URL and the browser dedupes it.
 *
 * `:root[data-browse-cells-ready]` is the progressive-enhancement gate: the
 * module script stamps it on <html> when the element upgrades, and every rule
 * that changes what the reader sees hangs off it. Without the script the table
 * renders exactly as it did before this feature -- fully wrapped, nothing
 * clipped. Clamping text behind a "…" that can never open would be strictly
 * worse than not clamping. See `browse-cells.js` for why the attribute has to
 * live on <html> rather than on the element.
 */

browse-cells {
  display: block;
}

.browse-row-action {
  cursor: pointer;
}

.browse-row-action:hover {
  background-image: linear-gradient(
    hsl(var(--primary, 280 50% 50%) / 0.05),
    hsl(var(--primary, 280 50% 50%) / 0.05)
  );
}

.browse-row-action-cell {
  position: relative;
}

/* Native keyboard/assistive-tech entry point for the row action. It stays
   visually hidden until focused, then overlays the first cell without adding
   a navigation-only column. */
.browse-row-control {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.browse-row-control:focus {
  z-index: 20;
  top: 0.25rem;
  left: 0.25rem;
  width: auto;
  height: auto;
  padding: 0.25rem 0.5rem;
  margin: 0;
  overflow: visible;
  clip: auto;
  border: 1px solid hsl(var(--primary, 280 50% 50%));
  border-radius: 0.25rem;
  outline: 2px solid hsl(var(--primary, 280 50% 50%));
  outline-offset: 2px;
  background: hsl(var(--surface, 0 0% 100%));
  color: hsl(var(--primary, 280 50% 50%));
  font-size: 0.75rem;
  line-height: 1rem;
  text-decoration: none;
}

/* Existing-request count badge on the visit create actions, and the by-type
   tooltip it reveals. These classes are hand-authored because the frozen
   Tailwind bundle does not contain this pill size or the focus-visible
   utilities this interaction needs. */
.visit-request-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 0.3125rem;
  border-radius: 9999px;
  background-color: hsl(var(--muted, 280 30% 90%));
  color: hsl(var(--muted-foreground, 280 10% 40%));
  font-size: 0.6875rem;
  font-weight: 600;
  line-height: 1;
  cursor: default;
}

.visit-request-count:focus-visible,
.visit-request-action-trigger:focus-visible {
  outline: 2px solid hsl(var(--primary, 280 50% 50%));
  outline-offset: 2px;
}

/* `position: fixed` and placed by the badge's own handler from its viewport
   rect -- see `render_existing_request_badge` for why an absolutely positioned
   tooltip cannot escape `.js-browse-scroller`'s overflow clip. The handler shows
   it by writing `display` inline, so a morph patch that drops that attribute
   falls back to hidden here. */
.visit-request-count-tip {
  display: none;
  position: fixed;
  z-index: 60;
  align-items: center;
  gap: 0.25rem;
  padding: 0.25rem;
  border: 1px solid hsl(var(--border, 280 30% 85%));
  border-radius: 0.375rem;
  background-color: hsl(var(--popover, 280 50% 98%));
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
  white-space: nowrap;
  pointer-events: none;
}

/* Lines a clamped text cell may occupy. Fixed, not user-settable -- see the
   note in `browse-cells.js` on why the density toggle was dropped. */
:root {
  --browse-cell-lines: 2;
}

/* The table's floor sum. Desktop only: the mobile table keeps its own
   `min-w-[720px]` and its auto layout. */
@media (min-width: 768px) {
  .browse-table {
    min-width: var(--browse-table-min-width, 0);
  }
}

/* A header label may wrap between words but must never fragment within one.
   The server floors each column at its widest header word, but that floor is
   an *estimate* (see `browse::framework::text_metrics`); this is the backstop that
   turns an under-estimate into a visible overflow instead of a silent
   one-character-per-line header. */
.browse-th {
  overflow-wrap: normal;
  word-break: normal;
  hyphens: none;
}

/* Everything below is desktop-only, matching the `md:` breakpoint the table
 * already switches layout at (`table-fixed` above, `table-auto` +
 * `whitespace-nowrap` below). The mobile table keeps its existing behaviour
 * untouched.
 */
@media (min-width: 768px) {
  .browse-cell {
    position: relative;
  }

  :root[data-browse-cells-ready] .browse-cell-content {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: var(--browse-cell-lines);
    line-clamp: var(--browse-cell-lines);
    overflow: hidden;
  }

  /* The badge pill is its own clip box -- one line, ellipsis, never the
     multi-line box above. It carries Tailwind's `truncate`, so only the
     `display` from the generic rule needs undoing. */
  :root[data-browse-cells-ready] .browse-cell--badge > .browse-cell-content {
    display: inline-block;
  }

  /* The overflow affordance. Absolutely positioned so it is pure overlay: it
     contributes nothing to the cell's content box and therefore cannot feed
     back into column width -- the determinism invariant holds. Hidden until
     `browse-cells.js` measures the cell as actually clipped. */
  .browse-cell-more {
    position: absolute;
    right: 0.125rem;
    bottom: 0.125rem;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0 0.25rem;
    height: 1rem;
    border: 0;
    border-radius: 0.1875rem;
    background: hsl(var(--muted, 280 30% 94%));
    color: hsl(var(--muted-foreground, 280 10% 45%));
    font: inherit;
    font-size: 0.6875rem;
    line-height: 1;
    letter-spacing: 0.05em;
    cursor: pointer;
  }

  :root[data-browse-cells-ready] .browse-cell[data-overflowing] > .browse-cell-more {
    display: inline-flex;
  }

  .browse-cell-more:hover,
  .browse-cell-more:focus-visible {
    background: hsl(var(--primary, 280 50% 50%));
    color: #fff;
  }
}

/* ---------------------------------------------------------------------------
 * Horizontal-scroll edge affordance
 *
 * A table whose columns are all at their floor overflows into this scroller,
 * so it needs to advertise that there is more to the side. Drawn as gradient
 * overlays rather than the classic `background-attachment: local` trick,
 * because that technique needs an opaque background to mask against and the
 * table card is translucent (`bg-white/50` over a blurred backdrop).
 *
 * The gradients hang off `.browse-scroll-frame` -- the scroller's parent, which
 * does not scroll -- and never off the scroller itself. An absolutely
 * positioned box inside a scroll container is laid out against the scrolled
 * content, so `right: 0` resolves to where the right edge sat at `scrollLeft:
 * 0` and the overlay then travels left with the columns instead of staying at
 * the edge. The frame's box is the visible box at every scroll offset.
 *
 * `browse-cells.js` owns the two boundary attributes.
 * ------------------------------------------------------------------------ */
.browse-scroll-frame {
  position: relative;
}

.browse-scroll-frame::before,
.browse-scroll-frame::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1.5rem;
  pointer-events: none;
  opacity: 0;
  transition: opacity 150ms ease;
}

.browse-scroll-frame::before {
  left: 0;
  background: linear-gradient(to right, rgb(0 0 0 / 0.12), transparent);
}

.browse-scroll-frame::after {
  right: 0;
  background: linear-gradient(to left, rgb(0 0 0 / 0.12), transparent);
}

.browse-scroll-frame[data-overflow-start]::before,
.browse-scroll-frame[data-overflow-end]::after {
  opacity: 1;
}

/* ---------------------------------------------------------------------------
 * Full-value dialog
 *
 * One <dialog> per <browse-cells> host, created and populated on demand by
 * the module script -- emitting one per cell would mean hundreds of dead
 * elements per page. Native <dialog> carries ESC-to-close, the backdrop, and
 * focus containment, so none of that is reimplemented here.
 * ------------------------------------------------------------------------ */
.browse-cell-dialog {
  /* The UA centres a modal <dialog> with `margin: auto` against `inset: 0`,
     but the Tailwind preflight in `embed.css` zeroes `margin` on every
     element, which strands the dialog in the top-left corner. Restate the
     whole UA centring here rather than just the margin. */
  position: fixed;
  inset: 0;
  width: fit-content;
  height: fit-content;
  margin: auto;
  max-width: min(48rem, 90vw);
  max-height: 80vh;
  padding: 0;
  border: 1px solid hsl(var(--border, 280 30% 90%));
  border-radius: 0.5rem;
  background: hsl(var(--surface, 0 0% 100%));
  color: hsl(var(--foreground, 280 10% 10%));
}

.browse-cell-dialog::backdrop {
  background: rgb(0 0 0 / 0.4);
}

/* ---------------------------------------------------------------------------
 * Picklist filter popup
 *
 * The filter panel's multiselect options open in a native modal <dialog>, so
 * the top layer lifts them out of the browse card, whose `backdrop-blur`
 * would otherwise trap a `fixed` overlay. Same preflight-margin restatement
 * as the full-value dialog above.
 * ------------------------------------------------------------------------ */
.browse-picklist-dialog {
  position: fixed;
  inset: 0;
  width: min(42rem, calc(100vw - 2rem));
  height: fit-content;
  margin: auto;
  max-height: 85vh;
  padding: 0;
  overflow: hidden;
  border: 1px solid hsl(var(--border, 280 30% 90%));
  border-radius: 0.75rem;
  background: hsl(var(--surface, 0 0% 100%));
  color: hsl(var(--foreground, 280 10% 10%));
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
}

.browse-picklist-dialog::backdrop {
  background: rgb(0 0 0 / 0.5);
}

.browse-cell-dialog-head {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  justify-content: space-between;
  padding: 0.625rem 0.875rem;
  border-bottom: 1px solid hsl(var(--border, 280 30% 90%));
}

.browse-cell-dialog-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: hsl(var(--muted-foreground, 280 10% 45%));
}

.browse-cell-dialog-actions {
  display: flex;
  gap: 0.375rem;
}

.browse-cell-dialog-btn {
  padding: 0.25rem 0.625rem;
  border: 1px solid hsl(var(--border, 280 30% 90%));
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
}

.browse-cell-dialog-btn:hover {
  border-color: hsl(var(--primary, 280 50% 50%));
  color: hsl(var(--primary, 280 50% 50%));
}

/* `pre-wrap` rather than plain text: long free-text values (error logs,
   addresses) carry meaningful line breaks that the clamped cell swallows. */
.browse-cell-dialog-body {
  overflow: auto;
  max-height: calc(80vh - 3.25rem);
  padding: 0.875rem;
  font-size: 0.8125rem;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
