/* kb list - status badge, tabs, VIN, list transitions, loading.
   Mockups: 09-badges-og-menu.html (badge "NY - Prik i tone med kant"), 06-komponenter.html (tabs, VIN, status change),
   08-indlaesning.html (skeleton, progress line, stagger).
   Markup:
     <span class="kb-badge kb-badge--new|warn|ok|err|muted">Tekst</span>
     <div class="kb-tabs" role="tablist"><button class="kb-tab" role="tab">Nye <span class="kb-tab__count">4</span></button> ...</div>   (kb.tabs.init)
     <kb-vin>WVWZZZ3CZLE012345</kb-vin>                                                                        (list.js defines the element)
     loading: .kb-skel (placeholder bar) inside .kb-skel-group (shown only while the container .is-loading),
              .kb-loading-real (hidden while loading), [data-count-to], .kb-grow bars; kb.loading.track(container, work)
   Colour rule: the dot, halo, tint and border use the base status token, the text uses the -ink token (AA as small text).
   Motion lives in no-preference only; reduced motion gets plain state changes. */
@layer kb.components {

  /* ---------- status badge ---------- */
  .kb-badge {
    --kb-badge-c: var(--kb-status-muted);
    --kb-badge-ink: var(--kb-status-muted-ink);
    position: relative;                 /* kb.badge.swap lays its sweep layers inside */
    display: inline-flex;
    align-items: center;
    gap: 6px;
    box-sizing: border-box;
    padding: 4px 9px 4px 8px;
    border-radius: var(--kb-radius);
    font-size: 13px;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    color: var(--kb-badge-ink);
    /* the faint tint sits on the card colour (not on whatever is behind), so the text keeps its contrast
       on hovered and selected rows too */
    background: color-mix(in srgb, var(--kb-badge-c) 6%, var(--kb-card));
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--kb-badge-c) 45%, transparent);
  }
  .kb-badge::before {
    content: "";
    flex: none;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--kb-badge-c);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--kb-badge-c) 18%, transparent);
  }
  .kb-badge--new   { --kb-badge-c: var(--kb-status-new);   --kb-badge-ink: var(--kb-status-new-ink); }
  .kb-badge--warn  { --kb-badge-c: var(--kb-status-warn);  --kb-badge-ink: var(--kb-status-warn-ink); }
  .kb-badge--ok    { --kb-badge-c: var(--kb-status-ok);    --kb-badge-ink: var(--kb-status-ok-ink); }
  .kb-badge--err   { --kb-badge-c: var(--kb-status-err);   --kb-badge-ink: var(--kb-status-err-ink); }
  .kb-badge--muted { --kb-badge-c: var(--kb-status-muted); --kb-badge-ink: var(--kb-status-muted-ink); }

  /* kb.badge.swap: the badge's own paint is hidden while two copies (old, new) are clipped against each other */
  .kb-badge.is-swapping {
    color: transparent;
    background: none;
    box-shadow: none;
  }
  .kb-badge.is-swapping::before { visibility: hidden; }
  .kb-badge__layer {
    position: absolute;
    top: 0;
    left: 0;
    margin: 0;
    pointer-events: none;
  }

  /* ---------- tabs ---------- */
  .kb-tabs {
    position: relative;
    display: flex;
    gap: 4px;
    overflow-x: auto;                   /* 360 px: the tabs scroll instead of wrapping */
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    box-shadow: inset 0 -1px 0 var(--kb-line);
  }
  .kb-tabs::-webkit-scrollbar { display: none; }
  .kb-tab {
    position: relative;
    z-index: 1;
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    box-sizing: border-box;
    min-height: 40px;
    padding: 8px 12px;
    border: 0;
    border-radius: var(--kb-radius);
    background: transparent;
    color: var(--kb-text-muted);
    font: inherit;
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
  }
  .kb-tab:hover { color: var(--kb-text); }
  .kb-tab[aria-selected="true"] { color: var(--kb-text); }
  .kb-tab:focus-visible { outline: 2px solid var(--kb-focus-ring); outline-offset: -2px; }
  .kb-tab__count {
    display: inline-block;              /* transformable for the bump */
    min-width: 1.5em;
    padding: 1px 7px;
    border-radius: var(--kb-radius);
    background: var(--kb-chip);
    color: var(--kb-text);
    font-size: 12px;
    line-height: 1.4;
    text-align: center;
    font-variant-numeric: tabular-nums;
  }
  .kb-tabs__ind {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 3px;
    width: 0;
    border-radius: 2px;
    background: var(--kb-primary);
    pointer-events: none;
  }

  /* ---------- VIN ---------- */
  kb-vin {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-family: var(--kb-font-mono);
    font-size: 13px;
    line-height: 1.4;
    border-radius: var(--kb-radius);
    cursor: copy;
    -webkit-tap-highlight-color: transparent;
  }
  kb-vin:focus-visible { outline: 2px solid var(--kb-focus-ring); outline-offset: 2px; }
  .kb-vin__v { border-bottom: 1px dashed var(--kb-line-strong); }
  .kb-vin__cp {
    position: relative;
    flex: none;
    width: 16px;
    height: 16px;
    color: var(--kb-text-muted);
  }
  kb-vin:hover .kb-vin__cp,
  kb-vin:focus-visible .kb-vin__cp { color: var(--kb-primary); }
  .kb-vin__cp > svg {
    position: absolute;
    inset: 0;
    width: 15px;
    height: 15px;
    margin: auto;
  }
  .kb-vin__cp > .kb-vin__ok { color: var(--kb-status-ok); transform: scale(0); opacity: 0; }
  kb-vin.is-copied .kb-vin__cp > :first-child { transform: scale(0); opacity: 0; }
  kb-vin.is-copied .kb-vin__cp > .kb-vin__ok { transform: scale(1); opacity: 1; }
  .kb-vin__live {                       /* screen-reader announcement, one per page */
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  /* ---------- loading: skeleton, progress line ---------- */
  .kb-skel {
    --kb-skel-base: color-mix(in srgb, var(--kb-text) 8%, var(--kb-card));
    --kb-skel-sheen: light-dark(rgba(255, 255, 255, .8), rgba(255, 255, 255, .08));
    position: relative;
    display: block;
    height: 12px;
    border-radius: 3px;
    background: var(--kb-skel-base);
    overflow: hidden;
  }
  .kb-skel--num { width: 90px; height: 34px; margin: 4px 0 8px; }
  .kb-skel--text { width: 70%; }
  .kb-skel::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: -60px;
    width: 60px;
    background: linear-gradient(90deg, transparent, var(--kb-skel-sheen) 40%, var(--kb-skel-sheen) 60%, transparent);
    transform: skewX(var(--kb-skew));
    display: none;                      /* the wave only runs where motion is welcome */
  }
  /* skeleton groups exist in the markup but show only while the container is loading. Every loading rule is scoped to
     [data-kb-loading] (set by kb.loading.track): .is-loading is also the busy state of .kb-btn and must not match here. */
  .kb-skel-group { display: none; }
  [data-kb-loading].is-loading .kb-skel-group { display: block; }
  [data-kb-loading].is-loading tbody.kb-skel-group { display: table-row-group; }
  [data-kb-loading].is-loading .kb-loading-real { display: none; }
  .kb-skel-group td { white-space: nowrap; }

  /* thin progress line at the top of the container (the container must not use its own ::before) */
  :where([data-kb-loading]) { position: relative; }
  [data-kb-loading].is-loading::before {
    content: "";
    position: absolute;
    z-index: 2;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    pointer-events: none;
    background: color-mix(in srgb, var(--kb-primary) 35%, transparent);   /* reduced motion: a still line */
  }

  /* bars that grow when the data is there (kb.loading plays the motion; this only sets the origin) */
  .kb-grow { transform-origin: 50% 100%; }
  .kb-grow--x { transform-origin: 0 50%; }

  @media (prefers-reduced-motion: no-preference) {
    .kb-tabs__ind.is-ready { transition: transform .35s var(--kb-ease-out), width .35s var(--kb-ease-out); }
    .kb-tab { transition: color .2s; }
    kb-vin .kb-vin__cp { transition: color .2s; }
    .kb-vin__cp > svg { transition: transform .3s var(--kb-ease-spring), opacity .2s; }

    .kb-skel::after {
      display: block;
      animation: kb-skel-sweep 1.65s infinite;
      animation-delay: var(--d, 0s);
    }
    @keyframes kb-skel-sweep {
      0% { left: -60px; animation-timing-function: var(--kb-ease-momentum); }
      85%, 100% { left: calc(100% + 60px); }
    }

    [data-kb-loading].is-loading::before {
      background-image: linear-gradient(90deg, transparent, var(--kb-primary), var(--kb-prog-2), var(--kb-prog-3), transparent);
      background-color: transparent;
      background-repeat: no-repeat;
      background-size: 40% 100%;
      background-position: -66.667% 0;
      animation: kb-fade-in .2s both, kb-prog 1.65s infinite;
    }
    @keyframes kb-prog {
      0% { background-position: -66.667% 0; animation-timing-function: var(--kb-ease-momentum); }
      85%, 100% { background-position: 166.667% 0; }
    }
    @keyframes kb-fade-in { from { opacity: 0; } }
  }

  /* second and third stop of the primary gradient (same as the button; dark exists twice, keep in step) */
  :root,
  [data-theme="light"] { --kb-prog-2: #5840d4; --kb-prog-3: #1086e0; }
  :root[data-theme="dark"],
  [data-theme="dark"] { --kb-prog-2: #6a52e4; --kb-prog-3: #1f95ea; }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme]) { --kb-prog-2: #6a52e4; --kb-prog-3: #1f95ea; }
  }
}
