/* Shared admin UI primitives. Token-driven (tokens.css).

   Components owned here:
     - .btn (+ .is-primary / .is-ghost / .is-link / .is-danger)
     - .toast (managed by Components.toast() in components.js)
     - .banner (+ .is-info / .is-warn / .is-err)
     - .popover-confirm (managed by Components.confirmPopover())
     - .empty
     - .loading-row (skeleton)
     - .segmented-control
     - .field (+ .field-label / .field-hint / .field-error)
     - .kpi-stat

   See /_demo/components.html for a live demo of each. */

/* ── Buttons ──────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--tw-radius-sm);
  font-size: 13px;
  font-weight: 600;
  font-family: var(--tw-font-sans);
  letter-spacing: 0.1px;
  background: var(--tw-card);
  color: var(--tw-text);
  border: 1px solid var(--tw-border);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--tw-card-highest); border-color: var(--tw-border-strong); }
.btn:focus-visible { outline: 2px solid var(--tw-focus); outline-offset: 2px; }
.btn[disabled],
.btn.is-disabled { opacity: 0.4; cursor: not-allowed; }

.btn.is-primary {
  background: var(--tw-accent);
  color: var(--tw-on-accent);
  border-color: var(--tw-accent);
}
.btn.is-primary:hover { background: var(--tw-accent-hover); border-color: var(--tw-accent-hover); }
.btn.is-primary:active { background: var(--tw-accent-strong); border-color: var(--tw-accent-strong); }

.btn.is-ghost {
  background: transparent;
  color: var(--tw-muted);
  border-color: var(--tw-border);
}
.btn.is-ghost:hover { color: var(--tw-text); border-color: var(--tw-border-strong); background: var(--tw-card); }

.btn.is-ghost.is-danger { color: var(--tw-muted); }
.btn.is-ghost.is-danger:hover {
  color: var(--tw-red);
  border-color: var(--tw-red);
  background: transparent;
}

.btn.is-link {
  background: transparent;
  border-color: transparent;
  color: var(--tw-accent);
  padding: 4px 6px;
}
.btn.is-link:hover { text-decoration: underline; background: transparent; border-color: transparent; }

.btn.is-sm { padding: 4px 10px; font-size: 11px; }
.btn.is-lg { padding: 12px 22px; font-size: 14px; }

/* ── Toast ────────────────────────────────────────────────────── */

#components-toast-root {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  max-width: min(360px, calc(100vw - 32px));
}

.toast {
  pointer-events: auto;
  background: var(--tw-card);
  color: var(--tw-text);
  border: 1px solid var(--tw-border);
  border-left: 3px solid var(--tw-accent);
  border-radius: var(--tw-radius-sm);
  padding: 10px 14px;
  font-size: 12px;
  line-height: 1.45;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  transform: translateX(120%);
  opacity: 0;
  transition: transform 240ms cubic-bezier(0.2, 0, 0, 1), opacity 240ms ease;
}
.toast.is-visible { transform: translateX(0); opacity: 1; }
.toast.is-leaving { transform: translateX(120%); opacity: 0; }
.toast.is-info { border-left-color: var(--tw-blue); }
.toast.is-warn { border-left-color: var(--tw-yellow); }
.toast.is-err  { border-left-color: var(--tw-red); }
.toast.is-success { border-left-color: var(--tw-green); }
.toast-text { flex: 1; min-width: 0; word-break: break-word; }
.toast-close {
  background: transparent;
  border: none;
  color: var(--tw-muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0;
}
.toast-close:hover { color: var(--tw-text); }

@media (prefers-reduced-motion: reduce) {
  .toast { transition: opacity 80ms ease; transform: none; }
  .toast.is-leaving { transform: none; }
}

/* ── Banner ───────────────────────────────────────────────────── */

.banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--tw-radius-sm);
  border: 1px solid var(--tw-border);
  background: var(--tw-card);
  color: var(--tw-text);
  font-size: 12px;
  margin-bottom: 12px;
}
.banner.is-info  { background: var(--tw-blue-soft);   border-color: var(--tw-blue);   color: var(--tw-blue); }
.banner.is-warn  { background: var(--tw-yellow-soft); border-color: var(--tw-yellow); color: var(--tw-yellow); }
.banner.is-err   { background: var(--tw-red-soft);    border-color: var(--tw-red);    color: var(--tw-red); }
.banner.is-success { background: var(--tw-green-soft); border-color: var(--tw-green); color: var(--tw-green); }
.banner-text { flex: 1; min-width: 0; }
.banner-dismiss {
  background: transparent;
  border: none;
  color: inherit;
  cursor: pointer;
  opacity: 0.7;
  font-size: 14px;
  padding: 0 4px;
}
.banner-dismiss:hover { opacity: 1; }

/* ── Popover-confirm ──────────────────────────────────────────── */

.popover-confirm {
  position: fixed;
  z-index: 900;
  width: 240px;
  background: var(--tw-card-hover);
  color: var(--tw-text);
  border: 1px solid var(--tw-border-strong);
  border-radius: var(--tw-radius-sm);
  padding: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  font-size: 13px;
  line-height: 1.5;
}
.popover-confirm[hidden] { display: none; }
.popover-confirm-msg {
  margin: 0 0 10px;
  color: var(--tw-text);
}
.popover-confirm-hint {
  margin: 0 0 10px;
  color: var(--tw-muted);
  font-size: 11px;
}
.popover-confirm-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}

/* ── Empty state ──────────────────────────────────────────────── */

.empty {
  padding: 32px 16px;
  text-align: center;
  background: var(--tw-card);
  border: 1px dashed var(--tw-border);
  border-radius: var(--tw-radius);
  color: var(--tw-muted);
}
.empty-title {
  color: var(--tw-text);
  font-size: 14px;
  font-weight: 600;
  margin: 0 0 4px;
}
.empty-hint {
  color: var(--tw-muted);
  font-size: 12px;
  margin: 0 0 12px;
}
.empty .btn { margin-top: 4px; }

/* ── Loading skeleton row ─────────────────────────────────────── */

.loading-row {
  display: flex;
  gap: 12px;
  padding: 10px 0;
  align-items: center;
}
.loading-row .skeleton {
  height: 12px;
  background: linear-gradient(90deg,
    var(--tw-card-2) 0%,
    var(--tw-card-hover) 50%,
    var(--tw-card-2) 100%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s ease-in-out infinite;
  border-radius: 4px;
  flex: 1;
}
.loading-row .skeleton.is-sm  { max-width: 80px; }
.loading-row .skeleton.is-md  { max-width: 180px; }
.loading-row .skeleton.is-lg  { max-width: 100%; }
.loading-row .skeleton.is-pill { height: 18px; max-width: 64px; border-radius: 999px; }

@keyframes skeleton-shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
@media (prefers-reduced-motion: reduce) {
  .loading-row .skeleton { animation: none; background: var(--tw-card-2); }
}

/* ── Segmented control ────────────────────────────────────────── */

.segmented-control {
  display: inline-flex;
  background: var(--tw-card);
  border: 1px solid var(--tw-border);
  border-radius: var(--tw-radius-sm);
  padding: 3px;
  gap: 2px;
}
.segmented-control-item {
  padding: 6px 14px;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--tw-muted);
  font-size: 12px;
  font-weight: 600;
  font-family: var(--tw-font-sans);
  cursor: pointer;
  letter-spacing: 0.2px;
  transition: color 0.15s, background 0.15s;
}
.segmented-control-item:hover { color: var(--tw-text); }
.segmented-control-item.is-active {
  background: var(--tw-accent);
  color: var(--tw-on-accent);
}
.segmented-control.is-muted .segmented-control-item.is-active {
  background: var(--tw-card-highest);
  color: var(--tw-text);
}
.segmented-control.is-sm .segmented-control-item { padding: 4px 10px; font-size: 11px; }

/* ── Field ────────────────────────────────────────────────────── */

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}
.field-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--tw-muted-2);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}
.field-control {
  width: 100%;
  padding: 8px 10px;
  background: var(--tw-bg);
  color: var(--tw-text);
  border: 1px solid var(--tw-border);
  border-radius: var(--tw-radius-sm);
  font-family: var(--tw-font-sans);
  font-size: 13px;
  transition: border-color 0.15s, background 0.15s;
}
.field-control:focus,
.field-control:focus-visible {
  outline: none;
  border-color: var(--tw-accent);
  background: var(--tw-card-2);
}
.field-control.is-invalid { border-color: var(--tw-red); }
.field-hint {
  color: var(--tw-muted);
  font-size: 11px;
}
.field-error {
  color: var(--tw-red);
  font-size: 11px;
  min-height: 14px;
}

/* ── KPI stat ─────────────────────────────────────────────────── */

.kpi-stat {
  background: var(--tw-card);
  border: 1px solid var(--tw-border);
  border-radius: var(--tw-radius);
  padding: 14px 18px;
}
.kpi-stat-value {
  font-family: var(--tw-font-mono);
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  color: var(--tw-accent);
  margin-bottom: 4px;
}
.kpi-stat-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--tw-muted-2);
  text-transform: uppercase;
  letter-spacing: 1px;
}
.kpi-stat-delta {
  display: inline-block;
  margin-left: 6px;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  font-family: var(--tw-font-mono);
}
.kpi-stat-delta.is-up { color: var(--tw-green); }
.kpi-stat-delta.is-down { color: var(--tw-red); }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

/* First-paint stagger reveal — opt-in by adding .kpi-grid.is-stagger
   on the host. Each .kpi-stat delays its entry by its index *40ms.
   Skipped under prefers-reduced-motion via the global guard in
   tokens.css. */
@media (prefers-reduced-motion: no-preference) {
  .kpi-grid.is-stagger .kpi-stat {
    opacity: 0;
    transform: translateY(6px);
    animation: kpi-rise var(--tw-duration-default) var(--tw-ease-standard) forwards;
  }
  .kpi-grid.is-stagger .kpi-stat:nth-child(1) { animation-delay: 0ms; }
  .kpi-grid.is-stagger .kpi-stat:nth-child(2) { animation-delay: 40ms; }
  .kpi-grid.is-stagger .kpi-stat:nth-child(3) { animation-delay: 80ms; }
  .kpi-grid.is-stagger .kpi-stat:nth-child(4) { animation-delay: 120ms; }
  .kpi-grid.is-stagger .kpi-stat:nth-child(5) { animation-delay: 160ms; }
  .kpi-grid.is-stagger .kpi-stat:nth-child(6) { animation-delay: 200ms; }
  .kpi-grid.is-stagger .kpi-stat:nth-child(7) { animation-delay: 240ms; }
  .kpi-grid.is-stagger .kpi-stat:nth-child(8) { animation-delay: 280ms; }
}
@keyframes kpi-rise {
  to { opacity: 1; transform: none; }
}

/* ── Hero numeric (DESIGN.md hero-numeric token) ──────────────── */

.hero-numeric {
  font-family: var(--tw-font-mono);
  font-size: 32px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.5px;
  line-height: 1.0;
  color: var(--tw-accent);
}
