/*
 * Components. BEM naming, no prefix — a prefixed class (u-) is a utility,
 * an unprefixed one is a component.
 */

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  gap: var(--s-2);
  align-items: center;
  justify-content: center;
  padding: var(--s-2) var(--s-3);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-md);
  box-shadow: var(--sh-xs);
  color: var(--c-text);
  font-size: var(--t-13);
  font-weight: var(--fw-medium);
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}

.btn:hover {
  background: var(--c-surface-hover);
  border-color: var(--c-border-strong);
  text-decoration: none;
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn--primary {
  background: var(--c-text);
  border-color: var(--c-text);
  color: var(--c-text-invert);
}

.btn--primary:hover {
  background: #000;
  border-color: #000;
}

.btn--ghost {
  background: transparent;
  box-shadow: none;
}

.btn--ghost:hover {
  background: var(--c-surface-hover);
}

.btn--danger {
  background: var(--c-neg);
  border-color: var(--c-neg);
  color: var(--c-text-invert);
}

.btn--danger:hover {
  background: #9c1f18;
  border-color: #9c1f18;
}

.btn--link {
  padding: 0;
  background: none;
  border: 0;
  box-shadow: none;
  color: var(--c-link);
}

.btn--link:hover {
  background: none;
  text-decoration: underline;
}

.btn--block {
  width: 100%;
}

.btn-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
  align-items: center;
}

.btn-group--vertical {
  flex-direction: column;
  align-items: stretch;
}

/* ---------- Forms ---------- */

.form__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
  align-items: center;
  margin-top: var(--s-5);
}

.field {
  margin-bottom: var(--s-4);
}

.field__label {
  display: block;
  margin-bottom: var(--s-2);
  color: var(--c-text-2);
  font-size: var(--t-13);
  font-weight: var(--fw-medium);
}

.field__required {
  color: var(--c-text-muted);
}

.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.field input[type="url"],
.field input[type="tel"],
.field input[type="number"],
.field input[type="search"],
.field select,
.field textarea {
  display: block;
  width: 100%;
  padding: var(--s-2) var(--s-3);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-md);
  color: var(--c-text);
  font-size: var(--t-14);
}

.field input::placeholder,
.field textarea::placeholder {
  color: var(--c-text-muted);
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--c-link);
  box-shadow: var(--sh-focus);
  outline: none;
}

.field textarea {
  min-height: 96px;
  resize: vertical;
}

.field--checkbox,
.field--radio {
  display: flex;
  gap: var(--s-2);
  align-items: flex-start;
}

.field--checkbox input,
.field--radio input {
  margin-top: 3px;
  accent-color: var(--c-text);
}

.field--checkbox .field__label,
.field--radio .field__label {
  margin-bottom: 0;
  color: var(--c-text);
  font-weight: var(--fw-normal);
}

.field__help {
  margin-top: var(--s-2);
  color: var(--c-text-muted);
  font-size: var(--t-12);
}

.field__help ul {
  margin: 0;
  padding-left: var(--s-4);
}

.field__error {
  margin-top: var(--s-2);
  color: var(--c-neg);
  font-size: var(--t-12);
}

.field--invalid input,
.field--invalid select,
.field--invalid textarea {
  border-color: var(--c-neg);
}

/* ---------- Alerts ---------- */

.messages {
  display: grid;
  gap: var(--s-2);
  margin-bottom: var(--s-5);
}

.alert {
  display: flex;
  gap: var(--s-3);
  align-items: flex-start;
  padding: var(--s-3) var(--s-4);
  background: var(--c-neutral-bg);
  border: 1px solid var(--c-neutral-bd);
  border-radius: var(--r-md);
  color: var(--c-text);
  font-size: var(--t-13);
}

.alert__body {
  flex: 1;
  min-width: 0;
}

.alert__body ul {
  margin: 0;
  padding-left: var(--s-4);
}

.alert__close {
  padding: 0 var(--s-1);
  background: none;
  border: 0;
  color: inherit;
  font-size: var(--t-16);
  line-height: 1;
  opacity: 0.5;
  cursor: pointer;
}

.alert__close:hover {
  opacity: 1;
}

.alert--success {
  background: var(--c-pos-bg);
  border-color: var(--c-pos-bd);
  color: var(--c-pos);
}

.alert--error {
  background: var(--c-neg-bg);
  border-color: var(--c-neg-bd);
  color: var(--c-neg);
}

.alert--warning {
  background: var(--c-warn-bg);
  border-color: var(--c-warn-bd);
  color: var(--c-warn);
}

.alert--info {
  background: var(--c-info-bg);
  border-color: var(--c-info-bd);
  color: var(--c-info);
}

/* ---------- Badges and pills ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  padding: 1px var(--s-2);
  border-radius: var(--r-pill);
  font-size: var(--t-12);
  white-space: nowrap;
}

.badge--outline {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  color: var(--c-text);
  font-variant-numeric: tabular-nums;
}

/* A listener count that has reached (or exceeded) its all-time peak. */
.badge--peak {
  background: var(--c-pos-bg);
  border-color: var(--c-pos-bd);
  color: var(--c-pos);
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 1px var(--s-2);
  background: var(--c-neutral-bg);
  border: 1px solid var(--c-neutral-bd);
  border-radius: var(--r-sm);
  color: var(--c-text-2);
  font-size: var(--t-12);
  font-weight: var(--fw-medium);
  white-space: nowrap;
}

.pill--at,
.pill--ok {
  background: var(--c-pos-bg);
  border-color: var(--c-pos-bd);
  color: var(--c-pos);
}

.pill--near,
.pill--info {
  background: var(--c-info-bg);
  border-color: var(--c-info-bd);
  color: var(--c-info);
}

.pill--below,
.pill--warn {
  background: var(--c-warn-bg);
  border-color: var(--c-warn-bd);
  color: var(--c-warn);
}

.pill--far {
  background: var(--c-neg-bg);
  border-color: var(--c-neg-bd);
  color: var(--c-neg);
}

/* Signed change: colour only, no fill. */
.delta {
  font-variant-numeric: tabular-nums;
}

.delta--up {
  color: var(--c-pos);
}

.delta--down {
  color: var(--c-neg);
}

.delta--flat {
  color: var(--c-text-muted);
}

/* ---------- Card ---------- */

/* No overflow:hidden here — it would silently kill position:sticky on the
   table header inside. Corner rounding is done on the corner cells. */
.card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-xs);
}

.card__head {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-3);
  align-items: center;
  padding: var(--s-4) var(--s-5);
  border-bottom: 1px solid var(--c-border);
}

.card__title {
  font-size: var(--t-16);
  font-weight: var(--fw-semibold);
}

.card__actions {
  margin-left: auto;
}

.card__body {
  padding: var(--s-5);
}

/* ---------- Toolbar (filter + search row) ---------- */

.toolbar {
  display: flex;
  gap: var(--s-3);
  align-items: center;
  margin-bottom: var(--s-4);
}

/* ---------- Search ---------- */

.search {
  display: flex;
  gap: var(--s-2);
  align-items: center;
  padding: var(--s-2) var(--s-3);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-xs);
}

/* In the toolbar, the filter button anchors the left edge and the search
   bar takes whatever's left, capped and pushed to the right rather than
   stretching full width — the two shouldn't visually compete for space. */
.search--compact {
  flex: 0 1 320px;
  margin-left: auto;
}

.search:focus-within {
  border-color: var(--c-link);
  box-shadow: var(--sh-focus);
}

.search__icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  color: var(--c-text-muted);
}

.search__input {
  flex: 1;
  min-width: 0;
  background: none;
  border: 0;
  color: var(--c-text);
  font-size: var(--t-14);
}

.search__input::placeholder {
  color: var(--c-text-muted);
}

.search__input:focus {
  outline: none;
}

.search__hint {
  flex-shrink: 0;
  padding: 2px var(--s-2);
  background: var(--c-surface-2);
  border: 1px solid var(--c-border);
  border-radius: var(--r-sm);
  color: var(--c-text-muted);
  font-family: var(--f-mono);
  font-size: var(--t-11);
}

/* ---------- Filter dropdown ---------- */

.filter-dropdown {
  position: relative;
  flex-shrink: 0;
}

.filter-dropdown__trigger {
  gap: var(--s-2);
}

.filter-dropdown__icon {
  width: 14px;
  height: 14px;
  color: var(--c-text-muted);
}

.filter-dropdown__badge {
  display: inline-block;
  width: 6px;
  height: 6px;
  background: var(--c-link);
  border-radius: 50%;
}

/* ---------- Listeners filter ---------- */

/* This doubles as the dropdown's popover panel: positioned, capped at
   roughly half the width the standalone card used to be, and hidden until
   the filter trigger opens it. Animation mirrors the drawer's — same
   can't-transition-display:none problem, same fix (see listeners.js). */
.listeners-filter {
  position: absolute;
  top: calc(100% + var(--s-2));
  left: 0;
  z-index: var(--z-drawer);
  width: max(320px, 50vw);
  max-width: 520px;
  padding: var(--s-4) var(--s-5) var(--s-5);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-lg);
  opacity: 0;
  transform: translateY(-4px);
  transition:
    opacity var(--drawer-duration) var(--drawer-ease),
    transform var(--drawer-duration) var(--drawer-ease);
}

.listeners-filter[hidden] {
  display: none;
}

.listeners-filter--open {
  opacity: 1;
  transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .listeners-filter {
    transition-duration: 1ms;
  }
}

.listeners-filter__group--divider {
  padding-top: var(--s-5);
  margin-top: var(--s-5);
  border-top: 1px solid var(--c-border);
}

.listeners-filter__label {
  margin-bottom: var(--s-4);
  color: var(--c-text-2);
  font-size: var(--t-13);
  font-weight: var(--fw-medium);
}

.listeners-filter__histogram {
  display: flex;
  align-items: flex-end;
  height: 40px;
  gap: 2px;
}

.listeners-filter__bar {
  display: flex;
  flex: 1;
  align-items: flex-end;
  min-width: 0;
  height: 100%;
}

.listeners-filter__bar-fill {
  width: 100%;
  min-height: 2px;
  background: var(--c-neutral-bd);
  border-radius: 1px 1px 0 0;
  transition: background var(--drawer-duration) var(--drawer-ease);
}

.listeners-filter__bar-fill--selected {
  background: var(--c-text-2);
}

/* Two native range inputs stacked on top of each other — the standard
   dual-thumb pattern without a library. The static track/range divs behind
   them are what's actually visible; the inputs themselves stay invisible
   except for their thumbs, which is why pointer-events is scoped to just
   the ::-webkit-slider-thumb / ::-moz-range-thumb pseudo-elements below. */
.range-slider {
  position: relative;
  height: 20px;
  margin-top: var(--s-3);
}

.range-slider__track {
  position: absolute;
  top: 50%;
  right: 0;
  left: 0;
  height: 4px;
  background: var(--c-neutral-bd);
  border-radius: var(--r-pill);
  transform: translateY(-50%);
}

.range-slider__range {
  position: absolute;
  top: 50%;
  height: 4px;
  background: var(--c-text);
  border-radius: var(--r-pill);
  transform: translateY(-50%);
}

.range-slider__input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  background: none;
  pointer-events: none;
  appearance: none;
}

.range-slider__input::-webkit-slider-runnable-track {
  height: 100%;
  background: transparent;
  appearance: none;
}

.range-slider__input::-moz-range-track {
  height: 100%;
  background: transparent;
  border: 0;
}

.range-slider__input::-webkit-slider-thumb {
  width: 18px;
  height: 18px;
  pointer-events: auto;
  cursor: pointer;
  background: var(--c-surface);
  border: 2px solid var(--c-text);
  border-radius: 50%;
  box-shadow: var(--sh-sm);
  appearance: none;
}

.range-slider__input::-moz-range-thumb {
  width: 18px;
  height: 18px;
  pointer-events: auto;
  cursor: pointer;
  background: var(--c-surface);
  border: 2px solid var(--c-text);
  border-radius: 50%;
  box-shadow: var(--sh-sm);
}

.range-slider__input:focus-visible::-webkit-slider-thumb {
  outline: 2px solid var(--c-link);
  outline-offset: 1px;
}

.range-slider__input:focus-visible::-moz-range-thumb {
  outline: 2px solid var(--c-link);
  outline-offset: 1px;
}

.listeners-filter__fields {
  display: flex;
  gap: var(--s-4);
  margin-top: var(--s-5);
}

.listeners-filter__fields .field {
  flex: 1;
  min-width: 0;
  margin-bottom: 0;
}

.listeners-filter .btn--block {
  margin-top: var(--s-4);
}

/* ---------- Data table ---------- */

.data-table {
  width: 100%;

  /* separate, not collapse: with collapse the sticky header's bottom border
     belongs to the grid and scrolls away with the first row. */
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--t-13);
}

.data-table th,
.data-table td {
  padding: var(--s-3) var(--s-4);
  text-align: left;
  vertical-align: middle;
}

.data-table thead th {
  /* sticky on th, never thead/tr — the latter are unreliable across engines.
     top offsets by the topbar height, otherwise the header slides under it. */
  position: sticky;
  top: var(--h-topbar);
  z-index: var(--z-thead);
  background: var(--c-surface-2);
  box-shadow: inset 0 -1px 0 var(--c-border);
  color: var(--c-text-muted);
  font-size: var(--t-12);
  font-weight: var(--fw-medium);
  letter-spacing: var(--ls-caps);
  text-transform: uppercase;
  white-space: nowrap;
}

.data-table thead th:first-child {
  border-top-left-radius: var(--r-lg);
}

.data-table thead th:last-child {
  border-top-right-radius: var(--r-lg);
}

.data-table tbody td {
  border-bottom: 1px solid var(--c-border);
}

.data-table tbody tr:hover {
  background: var(--c-surface-hover);
}

.data-table tbody tr:last-child td {
  border-bottom: 0;
}

.data-table__link {
  color: var(--c-text);
  font-weight: var(--fw-medium);
}

.data-table__link:hover {
  text-decoration: underline;
}

.data-table__sortable {
  cursor: pointer;
  user-select: none;
}

.data-table__sortable:hover {
  color: var(--c-text);
}

.data-table__sortable:focus-visible {
  outline: 2px solid var(--c-link);
  outline-offset: -2px;
}

.data-table__sortable[aria-sort] {
  color: var(--c-text);
}

.data-table__sort-icon {
  display: inline-block;
  min-width: 1em;
  margin-left: var(--s-1);
  text-align: center;
}

.data-table__foot td {
  background: var(--c-surface-2);
  border-top: 1px solid var(--c-border);
  border-bottom-right-radius: var(--r-lg);
  border-bottom-left-radius: var(--r-lg);
  color: var(--c-text-muted);
  font-size: var(--t-12);
}

.data-table__empty {
  padding: var(--s-12) var(--s-4);
  color: var(--c-text-muted);
  text-align: center;
}

@media (width <= 760px) {
  .data-table__optional {
    display: none;
  }

  .data-table th,
  .data-table td {
    padding: var(--s-2) var(--s-3);
  }

  /* A table cannot shrink below its min-content width, so on narrow screens
     it would otherwise spill outside the card. Scroll it instead — and drop
     the sticky header here, because any overflow on an ancestor disables
     position:sticky anyway. A pinned header matters far less on a phone. */
  .card--table {
    overflow-x: auto;
  }

  .data-table thead th {
    position: static;
  }
}

/* ---------- Pager ---------- */

.pager {
  display: flex;
  gap: var(--s-3);
  align-items: center;
  justify-content: center;
  margin-top: var(--s-5);
}

.pager__label {
  color: var(--c-text-muted);
  font-size: var(--t-13);
  font-variant-numeric: tabular-nums;
}

/* ---------- Record panel ---------- */

.record__section + .record__section {
  margin-top: var(--s-8);
}

.record__section-title {
  margin-bottom: var(--s-2);
  color: var(--c-text-muted);
  font-size: var(--t-11);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--ls-caps);
  text-transform: uppercase;
}

.record__row {
  display: grid;
  grid-template-columns: 180px 1fr;
  gap: var(--s-4);
  align-items: baseline;
  padding: var(--s-3) 0;
  border-bottom: 1px solid var(--c-border);
}

.record__section .record__row:last-child {
  border-bottom: 0;
}

.record__label {
  color: var(--c-text-muted);
  font-size: var(--t-13);
}

.record__value {
  min-width: 0;
  color: var(--c-text);
  font-size: var(--t-13);
  word-break: break-word;
}

.record__empty {
  color: var(--c-text-muted);
}

.record__chart {
  padding-top: var(--s-4);
}

.record__chart-label {
  margin-bottom: var(--s-2);
  color: var(--c-text-muted);
  font-size: var(--t-13);
}

.record__chart-summary {
  margin-top: var(--s-2);
  color: var(--c-text-2);
  font-size: var(--t-12);
  font-variant-numeric: tabular-nums;
}

.sparkline {
  display: block;
  width: 100%;
  height: 48px;
}

.sparkline__line {
  fill: none;
  stroke: var(--c-text-muted);
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sparkline__dot {
  fill: var(--c-text-muted);
}

.sparkline--up .sparkline__line {
  stroke: var(--c-pos);
}

.sparkline--up .sparkline__dot {
  fill: var(--c-pos);
}

.sparkline--down .sparkline__line {
  stroke: var(--c-neg);
}

.sparkline--down .sparkline__dot {
  fill: var(--c-neg);
}

@media (width <= 560px) {
  .record__row {
    grid-template-columns: 1fr;
    gap: var(--s-1);
  }
}

/* ---------- Drawer ---------- */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--z-drawer-backdrop);
  background: rgb(16 16 16 / 24%);
  opacity: 0;
  transition: opacity var(--drawer-duration) var(--drawer-ease);
}

.drawer-backdrop[hidden] {
  display: none;
}

.drawer-backdrop--open {
  opacity: 1;
}

/* Floats free of the viewport edges rather than filling the full height,
   so it reads as a panel over the page instead of a mode change. */
.drawer {
  position: fixed;
  top: var(--s-4);
  right: var(--s-4);
  bottom: var(--s-4);
  z-index: var(--z-drawer);
  display: flex;
  flex-direction: column;
  width: min(var(--w-drawer), calc(100vw - var(--s-4) * 2));
  overflow: hidden;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-lg);
  opacity: 0;
  transform: translateX(var(--s-2));
  transition:
    opacity var(--drawer-duration) var(--drawer-ease),
    transform var(--drawer-duration) var(--drawer-ease);
}

.drawer[hidden] {
  display: none;
}

.drawer--open {
  opacity: 1;
  transform: translateX(0);
}

@media (prefers-reduced-motion: reduce) {
  .drawer-backdrop,
  .drawer {
    transition-duration: 1ms;
  }
}

.drawer__head {
  display: flex;
  gap: var(--s-3);
  align-items: center;
  padding: var(--s-4) var(--s-5);
  border-bottom: 1px solid var(--c-border);
}

.drawer__title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  font-size: var(--t-20);
  font-weight: var(--fw-semibold);
  letter-spacing: -0.02em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drawer__actions {
  display: flex;
  gap: var(--s-1);
  align-items: center;
}

.drawer__icon {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  background: none;
  border: 0;
  border-radius: var(--r-sm);
  color: var(--c-text-muted);
  font-size: var(--t-16);
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
}

.drawer__icon:hover {
  background: var(--c-surface-hover);
  color: var(--c-text);
  text-decoration: none;
}

.drawer__body {
  flex: 1;
  padding: var(--s-5);
  overflow-y: auto;
}

/* Rows are focusable buttons-in-disguise; make that obvious on hover. */
.data-table__link {
  background: none;
  border: 0;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.data-table tbody tr[aria-selected="true"] {
  background: var(--c-surface-hover);
}

/* ---------- Auth (entrance layout) ---------- */

.auth {
  width: 100%;
  max-width: var(--w-narrow);
  margin: 0 auto;
  padding: var(--s-16) var(--s-5) var(--s-12);
}

.auth__mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  margin-bottom: var(--s-5);
  background: var(--c-neutral-bg);
  border-radius: var(--r-md);
  color: var(--c-text);
  font-size: var(--t-16);
  font-weight: var(--fw-semibold);
}

.auth h1 {
  font-size: var(--t-30);
  font-weight: var(--fw-semibold);
  line-height: var(--lh-tight);
  letter-spacing: -0.02em;
}

.auth h2 {
  margin-top: var(--s-6);
  font-size: var(--t-16);
  font-weight: var(--fw-semibold);
}

.auth > p {
  margin-top: var(--s-2);
  color: var(--c-text-muted);
  font-size: var(--t-14);
}

.auth form {
  margin-top: var(--s-6);
}

.auth .form__actions .btn {
  width: 100%;
  padding: var(--s-3) var(--s-4);
}

.auth__foot {
  margin-top: var(--s-8);
  color: var(--c-text-muted);
  font-size: var(--t-13);
  text-align: center;
}

/* ---------- Error pages ---------- */

.errorpage {
  max-width: 520px;
  margin: 0 auto;
  padding-block: var(--s-16);
  text-align: center;
}

.errorpage__code {
  color: var(--c-text-muted);
  font-size: var(--t-30);
  font-weight: var(--fw-semibold);
  font-variant-numeric: tabular-nums;
  letter-spacing: var(--ls-caps);
}

.errorpage h1 {
  margin-top: var(--s-2);
  font-size: var(--t-24);
  font-weight: var(--fw-semibold);
  letter-spacing: -0.02em;
}

.errorpage p {
  margin-top: var(--s-3);
  color: var(--c-text-muted);
}

.errorpage .btn {
  margin-top: var(--s-6);
}
