/* ============================================================
   core-ui.css — styles for the shared frontend primitives
   (skip link, focus policy, skeletons, progress, dialogs,
   sortable headers, the global loading bar).

   Everything here is paired with a module in static/js/core/.
   Nothing in here is page-specific; if a rule only applies to
   one page it belongs in css/pages/.

   Colour comes from --clr-*, the token family base/tokens.css and
   base/nord-theme.css actually define. This file used to reach for a
   --color-* family that exists nowhere in the codebase; because every
   one of those calls carried a hardcoded fallback, nothing looked
   broken — the rules just quietly ignored the theme. The muted text
   fallback was #6b7280, the precise value tokens.css had replaced for
   failing 4.5:1 contrast, so .progress-text, .modal-body and
   .empty-state were reintroducing a fixed accessibility bug through
   the back door. A fallback that never fails is a fallback nobody
   notices is wrong.
   ============================================================ */

/* ── Screen-reader-only ───────────────────────────────────────
   Deliberately not `display:none` or `visibility:hidden` — both
   remove the node from the accessibility tree, which is the exact
   opposite of what a live region needs. */
.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: 0;
  overflow: hidden;
  /* clip-path alone is sufficient; the legacy `clip: rect()` hack needed a
     -1px margin to pull the element off its own box, and that is the only
     reason the negative was ever there. */
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ── Skip to content (WCAG 2.4.1) ─────────────────────────────
   Off-screen until focused, then the first thing a keyboard user
   sees. It must be visible when focused, not merely present. */
.skip-link {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  background: var(--clr-primary);
  color: #fff;
  font-weight: 600;
  text-decoration: none;
}
.skip-link:focus {
  /* Restored to a real, visible box on focus. Clip-based hiding beats a
     fixed off-screen offset: it cannot be defeated by a taller header, a
     zoom level, or a browser that scrolls the focused element into view. */
  position: fixed;
  top: 0;
  left: 8px;
  z-index: 10000;
  width: auto;
  height: auto;
  padding: 12px 20px;
  overflow: visible;
  clip-path: none;
  border-radius: 0 0 6px 6px;
}

/* ── Focus policy ─────────────────────────────────────────────
   A visible focus indicator is WCAG 2.4.7. Outlines are only
   suppressed for pointer users, never globally — the pattern
   `*:focus { outline: none }` is the single most common
   accessibility regression in a CSS file. */
:focus-visible {
  outline: 3px solid var(--clr-primary);
  outline-offset: 2px;
  border-radius: 2px;
}
body:not(.user-is-tabbing) :focus:not(:focus-visible) {
  outline: none;
}

/* ── Reduced motion (WCAG 2.3.3) ──────────────────────────────
   Vestibular disorders are not a stylistic preference. Motion is
   removed rather than shortened; `0.01ms` instead of `0` keeps
   transitionend listeners firing so nothing hangs waiting on one. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ── Global loading bar ───────────────────────────────────────── */
.global-loading {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 0;
  background: var(--clr-primary);
  z-index: 9999;
  opacity: 0;
  transition: width 400ms ease, opacity 200ms ease;
  pointer-events: none;
}
.global-loading.is-active {
  width: 92%;
  opacity: 1;
  /* Stops at 92% on purpose: a bar that reaches 100% and waits reads as
     "finished but broken", which is worse feedback than one still moving. */
  transition: width 8s cubic-bezier(0.1, 0.6, 0.3, 1), opacity 200ms ease;
}

/* ── Skeletons ────────────────────────────────────────────────
   Shape as well as delay, so the layout does not jump when data
   arrives — a perceived-performance win and a CLS one. */
.skeleton-bar {
  display: block;
  height: 0.85em;
  margin: 0.35em 0;
  border-radius: 4px;
  background: linear-gradient(90deg,
    var(--clr-surface-alt) 25%,
    var(--clr-bg) 50%,
    var(--clr-surface-alt) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s ease-in-out infinite;
}
.skeleton-row td {
  padding: 12px;
}
@keyframes skeleton-shimmer {
  /* Sweeps the full 200% background-size in one direction. The previous
     200% -> -200% travelled twice the distance for the same visible effect. */
  0% { background-position: 200% 0; }
  100% { background-position: 0 0; }
}

/* ── Button loading state ─────────────────────────────────────── */
.btn[aria-busy="true"] {
  cursor: progress;
  opacity: 0.75;
  /* Centres the spinner against the label instead of nudging it below the
     baseline with a negative vertical-align. */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
}
.btn-spinner {
  display: inline-block;
  width: 1em;
  height: 1em;
  flex-shrink: 0;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Progress ─────────────────────────────────────────────────── */
.progress {
  height: 8px;
  border-radius: 4px;
  background: var(--clr-surface-alt);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0;
  background: var(--clr-primary);
  transition: width 200ms ease;
}
.progress.is-error .progress-fill {
  background: var(--clr-danger);
}
.progress-text {
  display: block;
  margin-top: 4px;
  font-size: 0.85rem;
  color: var(--clr-text-muted);
}

/* ── Dialogs ──────────────────────────────────────────────────
   `.scroll-locked` is ref-counted in JS so a nested dialog closing
   cannot restore page scrolling while an outer one is still open. */
body.scroll-locked {
  overflow: hidden;
}
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(15, 23, 42, 0.55);
}
.modal-overlay.is-open {
  display: flex;
}
.modal {
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 24px;
  border-radius: 10px;
  background: var(--clr-surface);
  box-shadow: 0 20px 45px rgba(15, 23, 42, 0.25);
}
.modal-title { margin: 0 0 8px; font-size: 1.15rem; }
.modal-body { margin: 0 0 20px; color: var(--clr-text-muted); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* ── Alerts ───────────────────────────────────────────────────── */
.alert { display: flex; align-items: flex-start; gap: 8px; }
.alert.is-dismissing {
  opacity: 0;
  transform: translateX(20px);
  transition: opacity 300ms ease, transform 300ms ease;
}
.alert-close {
  margin-left: auto;
  padding: 0 0 0 8px;
  background: none;
  border: none;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.7;
  /* 24px is below the 44px touch target WCAG 2.5.5 asks for; the padding
     brings the hit area up without changing the glyph size. */
  min-width: 44px;
  min-height: 44px;
}
.alert-close:hover { opacity: 1; }

/* ── Sortable table headers ───────────────────────────────────
   A <button> rather than a click handler on the <th>: the old
   version was unreachable by keyboard, which made every table on
   the platform sortable by mouse only. */
.th-sort {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 0;
  background: none;
  border: none;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.th-sort:hover { text-decoration: underline; }
th[aria-sort="ascending"] .sort-indicator::after { content: " ▲"; }
th[aria-sort="descending"] .sort-indicator::after { content: " ▼"; }

/* ── Form errors ──────────────────────────────────────────────
   The message carries an icon glyph as well as colour: WCAG 1.4.1
   (Use of Colour) forbids colour as the only means of conveying
   information, and a red border alone is exactly that. */
.field-error {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--clr-danger);
}
.field-error::before {
  content: "⚠ ";
}
[aria-invalid="true"] {
  border-color: var(--clr-danger) !important;
}

/* ── Empty states ─────────────────────────────────────────────── */
.empty-state {
  padding: 32px 16px;
  text-align: center;
  color: var(--clr-text-muted);
}
