/*
 * Cairn component styles (the classes rendered by web/lib/ui.js).
 * Tokens only: no literal colors. State classes use the is-* / has-* prefix.
 */

/* ---------------------------------------------------------------------------
 * Icons and spinner
 * ------------------------------------------------------------------------- */
.icon {
  display: inline-block;
  flex: none;
  vertical-align: -0.15em;
}

.spinner {
  display: inline-block;
  flex: none;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  color: var(--accent);
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  50% {
    opacity: 0.35;
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

@keyframes rise-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateX(24px);
  }
}

/* ---------------------------------------------------------------------------
 * Buttons
 * ------------------------------------------------------------------------- */
.btn {
  position: relative;
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: var(--control-height);
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: var(--text-md);
  font-weight: 500;
  line-height: 1.3;
  white-space: nowrap;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--duration-fast) ease,
    border-color var(--duration-fast) ease,
    color var(--duration-fast) ease;
}

.btn:hover {
  text-decoration: none;
}

.btn-secondary {
  border-color: var(--border-strong);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.btn-secondary:hover:where(:not(:disabled, [aria-disabled="true"])) {
  background: var(--surface-2);
}

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

.btn-primary:hover:where(:not(:disabled, [aria-disabled="true"])) {
  border-color: var(--accent-hover);
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}

.btn-ghost:hover:where(:not(:disabled, [aria-disabled="true"])) {
  background: var(--surface-3);
  color: var(--text);
}

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

.btn-danger:hover:where(:not(:disabled, [aria-disabled="true"])) {
  border-color: var(--danger-hover);
  background: var(--danger-hover);
}

.btn:disabled,
.btn[aria-disabled="true"] {
  box-shadow: none;
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-sm {
  gap: 4px;
  height: var(--control-height-sm);
  padding: 0 8px;
  border-radius: calc(var(--radius) - 2px);
  font-size: var(--text-sm);
}

.btn-icon-only {
  width: var(--control-height);
  padding: 0;
}

.btn-sm.btn-icon-only {
  width: var(--control-height-sm);
}

.btn-label {
  min-width: 0;
}

.btn.is-loading {
  cursor: progress;
}

.btn.is-loading > :not(.btn-spinner) {
  opacity: 0;
}

.btn-spinner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}

.btn-spinner .spinner {
  color: inherit;
}

/* ---------------------------------------------------------------------------
 * Fields and text inputs (the control look itself comes from base.css)
 * ------------------------------------------------------------------------- */
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field-label {
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: 500;
}

.field-required {
  margin-left: 2px;
  color: var(--danger);
}

.field-hint {
  color: var(--text-faint);
  font-size: var(--text-xs);
}

.field-error {
  color: var(--danger);
  font-size: var(--text-xs);
}

.field-error:empty {
  display: none;
}

.field-control {
  min-width: 0;
}

.input,
.select,
.textarea {
  width: 100%;
}

.input-sm {
  min-height: var(--control-height-sm);
  padding: 2px 8px;
  font-size: var(--text-sm);
}

.input-wrap {
  position: relative;
  display: flex;
  min-width: 0;
}

.input-wrap > .input {
  padding-left: 32px;
}

.input-icon {
  position: absolute;
  top: 50%;
  left: 10px;
  display: flex;
  color: var(--text-faint);
  transform: translateY(-50%);
  pointer-events: none;
}

.search-input {
  position: relative;
  display: flex;
  width: 100%;
  min-width: 0;
}

.search-input > .input {
  padding-right: 30px;
  padding-left: 32px;
}

.search-clear {
  position: absolute;
  top: 50%;
  right: 6px;
  display: grid;
  width: 20px;
  height: 20px;
  place-items: center;
  border-radius: var(--radius-sm);
  color: var(--text-faint);
  transform: translateY(-50%);
}

.search-clear:hover {
  background: var(--surface-3);
  color: var(--text);
}

/* ---------------------------------------------------------------------------
 * Select and multi-select
 * ------------------------------------------------------------------------- */
.select-wrap {
  position: relative;
  display: flex;
  min-width: 0;
}

.select {
  padding-right: 30px;
  text-overflow: ellipsis;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

.select-arrow {
  position: absolute;
  top: 50%;
  right: 8px;
  display: flex;
  color: var(--text-faint);
  transform: translateY(-50%);
  pointer-events: none;
}

.multi-select {
  position: relative;
  display: flex;
  align-items: center;
  min-height: var(--control-height);
  padding: 5px 30px 5px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-md);
  line-height: 20px;
  text-align: left;
}

.multi-select:hover {
  border-color: var(--text-faint);
}

.multi-select[aria-expanded="true"] {
  border-color: var(--focus);
}

.multi-select[aria-invalid="true"] {
  border-color: var(--danger);
}

.multi-select:disabled {
  border-color: var(--border);
  background: var(--surface-2);
  color: var(--text-faint);
}

.multi-select-value {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.multi-select-value.is-placeholder {
  color: var(--text-faint);
}

.multi-select-panel {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 220px;
  padding: 8px;
}

.multi-select-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
}

.multi-select-count {
  padding-left: 6px;
  color: var(--text-faint);
  font-size: var(--text-xs);
}

/* ---------------------------------------------------------------------------
 * Checkbox, radio, switch
 * ------------------------------------------------------------------------- */
.check-input,
.radio-input,
.switch-input {
  flex: none;
  margin: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition:
    background-color var(--duration-fast) ease,
    border-color var(--duration-fast) ease;
}

.check-input,
.radio-input {
  display: inline-grid;
  width: 16px;
  height: 16px;
  place-content: center;
  border: 1px solid var(--border-strong);
  background: var(--surface);
}

.check-input {
  border-radius: var(--radius-sm);
}

.radio-input {
  border-radius: 50%;
}

.check-input:hover,
.radio-input:hover {
  border-color: var(--text-faint);
}

.check-input::before {
  width: 10px;
  height: 10px;
  background: var(--accent-text);
  clip-path: polygon(10% 55%, 40% 85%, 92% 25%, 82% 15%, 40% 63%, 20% 44%);
  content: "";
  transform: scale(0);
  transition: transform var(--duration-fast) var(--ease);
}

.radio-input::before {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-text);
  content: "";
  transform: scale(0);
  transition: transform var(--duration-fast) var(--ease);
}

.check-input:checked,
.check-input:indeterminate,
.radio-input:checked {
  border-color: var(--accent);
  background: var(--accent);
}

.check-input:checked::before,
.radio-input:checked::before {
  transform: scale(1);
}

.check-input:indeterminate::before {
  clip-path: polygon(12% 40%, 88% 40%, 88% 60%, 12% 60%);
  transform: scale(1);
}

.switch-input {
  position: relative;
  width: 30px;
  height: 18px;
  border-radius: var(--radius-full);
  background: var(--border-strong);
}

.switch-input::before {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  content: "";
  transition: transform var(--duration) var(--ease);
}

.switch-input:checked {
  background: var(--accent);
}

.switch-input:checked::before {
  background: var(--accent-text);
  transform: translateX(12px);
}

.check-input:disabled,
.radio-input:disabled,
.switch-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.check,
.radio,
.switch {
  display: inline-flex;
  align-items: flex-start;
  gap: 8px;
  line-height: 20px;
  cursor: pointer;
}

.check > input,
.radio > input {
  margin-top: 2px;
}

.switch > input {
  margin-top: 1px;
}

.check-body {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.check-label {
  overflow-wrap: anywhere;
}

.check-hint {
  color: var(--text-faint);
  font-size: var(--text-xs);
  line-height: 1.4;
}

.radio-group,
.radio-options {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.radio-options.is-inline {
  flex-flow: row wrap;
  gap: 6px 16px;
}

/* ---------------------------------------------------------------------------
 * Check list
 * ------------------------------------------------------------------------- */
.check-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.check-list-filter {
  flex: none;
}

.check-list-items {
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.check-list-item {
  display: flex;
  flex: none;
  align-items: center;
  gap: 8px;
  min-height: 28px;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.check-list-item:hover {
  background: var(--surface-2);
}

.check-list-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.check-list-count {
  color: var(--text-faint);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

.check-list-empty {
  padding: 8px 6px;
  color: var(--text-faint);
  font-size: var(--text-sm);
}

/* ---------------------------------------------------------------------------
 * Tag input
 * ------------------------------------------------------------------------- */
.tag-input {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  min-height: var(--control-height);
  padding: 4px 6px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: text;
}

.tag-input:hover {
  border-color: var(--text-faint);
}

.tag-input:focus-within {
  border-color: var(--focus);
  outline: 2px solid var(--focus);
  outline-offset: -1px;
}

.field.has-error .tag-input {
  border-color: var(--danger);
}

.tag-input.is-disabled {
  border-color: var(--border);
  background: var(--surface-2);
  cursor: not-allowed;
}

.tag-input-field,
.tag-input-field:hover,
.tag-input-field:focus {
  flex: 1 1 72px;
  min-width: 56px;
  min-height: 22px;
  padding: 0 4px;
  border: 0;
  border-radius: 0;
  outline: none;
  background: transparent;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  max-width: 100%;
  height: 22px;
  padding: 0 2px 0 8px;
  border-radius: var(--radius-sm);
  background: var(--surface-3);
  color: var(--text);
  font-size: var(--text-sm);
}

.tag-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tag-remove {
  display: grid;
  flex: none;
  width: 18px;
  height: 18px;
  place-items: center;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
}

.tag-remove:hover {
  background: var(--border-strong);
  color: var(--text);
}

.suggestions {
  padding: 4px;
}

.suggestion-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.suggestion {
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  overflow-wrap: anywhere;
  cursor: pointer;
}

.suggestion:hover,
.suggestion.is-active {
  background: var(--accent-soft);
}

/* ---------------------------------------------------------------------------
 * Popover and menu
 * ------------------------------------------------------------------------- */
.popover {
  position: fixed;
  top: 0;
  left: 0;
  z-index: var(--z-dropdown);
  max-width: calc(100vw - 16px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  background: var(--surface);
  box-shadow: var(--shadow-md);
  animation: fade-in var(--duration-fast) ease;
}

.menu {
  min-width: 184px;
  padding: 4px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 30px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: var(--text-md);
  text-align: left;
}

.menu-item .icon {
  color: var(--text-faint);
}

.menu-item:hover,
.menu-item:focus {
  background: var(--surface-2);
}

.menu-item:focus-visible {
  outline-offset: -2px;
}

.menu-item.is-danger,
.menu-item.is-danger .icon {
  color: var(--danger);
}

.menu-item.is-danger:hover,
.menu-item.is-danger:focus {
  background: var(--danger-soft);
}

.menu-item:disabled {
  background: none;
  color: var(--text-faint);
  opacity: 0.6;
  cursor: not-allowed;
}

.menu-item-label {
  flex: 1 1 auto;
  min-width: 0;
}

.menu-item-hint {
  color: var(--text-faint);
  font-size: var(--text-xs);
}

.menu-divider {
  height: 1px;
  margin: 4px 0;
  background: var(--border);
}

/* ---------------------------------------------------------------------------
 * Modal and drawer
 * ------------------------------------------------------------------------- */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  background: var(--overlay);
  animation: fade-in var(--duration) ease;
}

.overlay-modal {
  align-items: flex-start;
  justify-content: center;
  padding: 9vh var(--space-4) var(--space-6);
  overflow-y: auto;
  z-index: var(--z-modal);
}

.overlay-drawer {
  justify-content: flex-end;
  z-index: var(--z-drawer);
}

.modal {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: calc(100vh - 9vh - var(--space-6));
  max-height: calc(100dvh - 9vh - var(--space-6));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  outline: none;
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  animation: rise-in var(--duration) var(--ease);
}

.modal-sm {
  max-width: 440px;
}

.modal-md {
  max-width: 600px;
}

.modal-lg {
  max-width: 860px;
}

.drawer {
  display: flex;
  flex-direction: column;
  max-width: 100vw;
  height: 100%;
  border-left: 1px solid var(--border);
  outline: none;
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  animation: slide-in var(--duration) var(--ease);
}

.modal-header,
.drawer-header {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 12px 12px 12px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-title,
.drawer-title {
  min-width: 0;
  font-size: var(--text-lg);
  font-weight: 600;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}

.modal-body,
.drawer-body {
  flex: 1 1 auto;
  min-height: 0;
  padding: var(--space-5);
  overflow-y: auto;
}

.modal-footer,
.drawer-footer {
  display: flex;
  flex: none;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

.dialog-message {
  color: var(--text-muted);
  line-height: 1.55;
  overflow-wrap: anywhere;
  white-space: pre-line;
}

@media (max-width: 640px) {
  .overlay-modal {
    padding: var(--space-4) var(--space-3);
  }

  .modal {
    max-height: calc(100vh - 2 * var(--space-4));
    max-height: calc(100dvh - 2 * var(--space-4));
  }

  .drawer {
    width: 100% !important;
  }
}

/* ---------------------------------------------------------------------------
 * Toasts
 * ------------------------------------------------------------------------- */
/*
 * Bottom center, not a corner: a drawer keeps its action buttons in the bottom
 * right corner, and a toast there would cover "Save" for as long as it shows.
 */
.toast-region {
  position: fixed;
  bottom: var(--space-4);
  left: 50%;
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: min(380px, calc(100vw - 2 * var(--space-4)));
  transform: translateX(-50%);
  pointer-events: none;
}

.toast {
  --toast-tone: var(--info);

  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 8px 10px 12px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--toast-tone);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-md);
  animation: rise-in var(--duration) var(--ease);
  pointer-events: auto;
}

.toast-success {
  --toast-tone: var(--success);
}

.toast-error {
  --toast-tone: var(--danger);
}

.toast-info {
  --toast-tone: var(--info);
}

.toast-icon {
  display: flex;
  flex: none;
  padding-top: 1px;
  color: var(--toast-tone);
}

.toast-message {
  flex: 1 1 auto;
  min-width: 0;
  overflow-wrap: anywhere;
}

.toast-close {
  display: grid;
  flex: none;
  width: 22px;
  height: 22px;
  place-items: center;
  border-radius: var(--radius-sm);
  color: var(--text-faint);
}

.toast-close:hover {
  background: var(--surface-3);
  color: var(--text);
}

/* ---------------------------------------------------------------------------
 * Page header, card, toolbar
 * ------------------------------------------------------------------------- */
.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-3) var(--space-4);
  margin-bottom: var(--space-5);
}

.page-header-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.page-header-back {
  display: inline-flex;
  align-items: center;
  align-self: flex-start;
  gap: 4px;
  margin-bottom: 4px;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.page-header-back:hover {
  color: var(--text);
  text-decoration: none;
}

.page-title {
  font-size: var(--text-xl);
  letter-spacing: -0.015em;
  overflow-wrap: anywhere;
}

.page-subtitle {
  color: var(--text-muted);
}

.page-header-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.card {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

/* The class used on its own (without the Card component's body) still gets padding.
   :where() keeps the specificity of a single class, so page styles can override it. */
.card:where(:not(:has(> .card-body))) {
  padding: var(--space-4);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 12px var(--space-4);
  border-bottom: 1px solid var(--border);
}

.card-heading {
  min-width: 0;
}

.card-title {
  font-size: var(--text-md);
  font-weight: 600;
  letter-spacing: 0;
}

.card-subtitle {
  margin-top: 2px;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.card-actions {
  display: flex;
  flex: none;
  align-items: center;
  gap: var(--space-2);
}

.card-body {
  padding: var(--space-4);
}

.card-body.is-flush {
  padding: 0;
}

.card-body.is-flush > .table-wrap,
.card-body.is-flush > .async > .table-wrap {
  border: 0;
  border-radius: 0 0 var(--radius) var(--radius);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  margin-bottom: var(--space-3);
}

.toolbar > .search-input {
  flex: 0 1 280px;
}

/* ---------------------------------------------------------------------------
 * Tabs
 * ------------------------------------------------------------------------- */
.tabs {
  display: flex;
  gap: var(--space-1);
  margin-bottom: var(--space-4);
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 6px;
  margin-bottom: -1px;
  padding: 8px 10px;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-weight: 500;
  white-space: nowrap;
}

.tab:hover {
  color: var(--text);
}

.tab:focus-visible {
  outline-offset: -2px;
}

.tab.is-active {
  border-bottom-color: var(--accent);
  color: var(--accent);
}

.tab-label {
  max-width: 28ch;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab-count {
  padding: 0 6px;
  border-radius: var(--radius-full);
  background: var(--surface-3);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  line-height: 18px;
}

.tab.is-active .tab-count {
  background: var(--accent-soft);
  color: var(--accent);
}

/* ---------------------------------------------------------------------------
 * Key / value list, stat tile, badge, avatar
 * ------------------------------------------------------------------------- */
.kv {
  display: grid;
  grid-template-columns: minmax(96px, max-content) minmax(0, 1fr);
  gap: var(--space-2) var(--space-4);
  margin: 0;
}

.kv-row {
  display: contents;
}

.kv-label {
  color: var(--text-muted);
  font-size: var(--text-sm);
  line-height: 20px;
}

.kv-value {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  padding: 14px var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.stat-label {
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.stat-value {
  font-size: var(--text-2xl);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.stat-accent .stat-value {
  color: var(--accent);
}

.stat-success .stat-value {
  color: var(--success);
}

.stat-warning .stat-value {
  color: var(--warning);
}

.stat-danger .stat-value {
  color: var(--danger);
}

.stat-info .stat-value {
  color: var(--info);
}

.stat-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  color: var(--text-faint);
  font-size: var(--text-xs);
}

.stat-hint:empty {
  display: none;
}

.stat-delta {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  color: var(--text-muted);
  font-weight: 600;
}

.stat-delta.is-up {
  color: var(--success);
}

.stat-delta.is-down {
  color: var(--danger);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  height: 20px;
  padding: 0 8px;
  border-radius: var(--radius-full);
  background: var(--surface-3);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 500;
  line-height: 1;
  white-space: nowrap;
}

.badge-neutral {
  background: var(--surface-3);
  color: var(--text-muted);
}

.badge-accent {
  background: var(--accent-soft);
  color: var(--accent);
}

.badge-success {
  background: var(--success-soft);
  color: var(--success);
}

.badge-warning {
  background: var(--warning-soft);
  color: var(--warning);
}

.badge-danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.badge-info {
  background: var(--info-soft);
  color: var(--info);
}

.badge-dot {
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.avatar {
  display: inline-grid;
  flex: none;
  width: 28px;
  height: 28px;
  place-items: center;
  border-radius: 50%;
  font-size: 11px;
  background: var(--surface-3);
  color: var(--text-muted);
  font-weight: 600;
  line-height: 1;
  user-select: none;
}

.avatar-0 {
  background: var(--accent-soft);
  color: var(--accent);
}

.avatar-1 {
  background: var(--info-soft);
  color: var(--info);
}

.avatar-2 {
  background: var(--success-soft);
  color: var(--success);
}

.avatar-3 {
  background: var(--warning-soft);
  color: var(--warning);
}

.avatar-4 {
  background: var(--danger-soft);
  color: var(--danger);
}

/* The sixth color is the neutral one. */
.avatar-5 {
  background: var(--surface-3);
  color: var(--text-muted);
}

/* ---------------------------------------------------------------------------
 * Empty, loading and error states
 * ------------------------------------------------------------------------- */
.async {
  display: contents;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 40px var(--space-6);
  text-align: center;
}

.empty-state-icon {
  display: grid;
  width: 44px;
  height: 44px;
  margin-bottom: 6px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-muted);
}

.empty-state-title {
  font-size: var(--text-lg);
}

.empty-state-text {
  max-width: 46ch;
  color: var(--text-muted);
}

.empty-state-action {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--space-2);
  margin-top: 10px;
}

.loading-block {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px var(--space-6);
  color: var(--text-muted);
}

.error-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 32px var(--space-6);
  text-align: center;
}

.error-block-icon {
  color: var(--danger);
}

.error-block-message {
  max-width: 52ch;
  color: var(--text-muted);
  overflow-wrap: anywhere;
}

/* ---------------------------------------------------------------------------
 * Data table and pagination
 * ------------------------------------------------------------------------- */
.table-wrap {
  position: relative;
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.table-scroll {
  max-height: var(--table-max-height, none);
  overflow: auto;
}

.table {
  width: 100%;
}

.table th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 8px 12px;
  background: var(--surface-2);
  /* A shadow, not a border: collapsed borders do not travel with a sticky cell. */
  box-shadow: inset 0 -1px 0 var(--border);
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
}

.table td {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  vertical-align: middle;
}

.table tbody tr:first-child td {
  border-top: 0;
}

.table tbody tr {
  transition: background-color var(--duration-fast) ease;
}

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

.table tbody tr.is-selected {
  background: var(--accent-soft);
}

.table tbody tr.is-clickable {
  cursor: pointer;
}

.table .is-right {
  text-align: right;
}

.table .is-center {
  text-align: center;
}

.table th.table-select,
.table td.table-select {
  width: 36px;
  padding-right: 0;
  line-height: 0;
}

.table-sort {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: inherit;
  font-weight: 600;
}

.table th.is-right .table-sort {
  flex-direction: row-reverse;
}

.table-sort:hover,
.table-sort.is-sorted {
  color: var(--text);
}

.table-sort .icon {
  color: var(--text-faint);
}

.table-sort.is-sorted .icon {
  color: var(--accent);
}

.table-dense .table th {
  padding: 6px 10px;
}

.table-dense .table td {
  padding: 6px 10px;
}

.table-empty {
  padding: 32px var(--space-4);
  border-top: 1px solid var(--border);
  text-align: center;
}

.table-wrap.is-loading tbody {
  opacity: 0.45;
  pointer-events: none;
}

.table-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}

.table-wrap.is-loading.is-empty .table-loading {
  position: static;
  padding: 40px 0;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  margin-top: var(--space-3);
}

.pagination-range {
  margin-right: 6px;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------------------
 * Motion preferences: a frozen spinner looks broken, so it fades instead.
 * ------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation-name: pulse !important;
    animation-duration: 1.4s !important;
    animation-iteration-count: infinite !important;
    animation-timing-function: ease-in-out !important;
  }
}
