/* @kira/components — standalone styles.
 *
 * Every colour is read from the inherited --kl-* roles, so a host that
 * already defines them keeps its palette. The :where() fallbacks below
 * have zero specificity: anything a host sets on :root, body or .dark
 * wins, in any import order.
 *
 * Theme: [data-kira-theme="light"|"dark"] on <html> or any ancestor.
 * With no attribute the light defaults apply at :root and inherit down.
 * Order matters here and only here -- dark before explicit light, so a
 * light subtree can re-enter inside a dark one.
 *
 * Font: never set here. Buttons and segments use font-family: inherit, so
 * they render in the host's face. The count/code badge reads --kl-font-mono
 * with the same stack Kira uses, so a host that loads IBM Plex Mono (as Kira
 * does) gets the design's figures; one that does not gets its platform mono.
 * Nothing is downloaded by this file either way.
 * No @font-face, no @import, so importing this file downloads nothing.
 *
 * Sizes follow the host's pointer: touch-capable devices get 44px targets.
 * The query is `any-pointer: coarse`, not `pointer: coarse`, so an iPad with
 * a trackpad or Pencil attached (fine primary, coarse available) still gets
 * the touch floor. A pure mouse/trackpad desktop stays at the drawing's
 * pointer sizes; a touch laptop gains 44px targets, which is the floor the
 * HIG asks for and the deliberate cost of not sniffing the UA. The old
 * [data-device] showcase hook stays absent.
 */

/* Dark first, explicit light last, so a light subtree re-enters inside dark.
 * The :root default skips a dark <html>, so it never fights the dark rule. */
:where([data-kira-theme="dark"]) {
  --kl-surface: #191919;
  --kl-sunken: #1e1e1e;
  --kl-raised: #202020;
  --kl-selected: #2a2a2a;
  --kl-border: #2e2e2e;
  --kl-border-strong: #3a3a3a;
  --kl-text: #e6e6e4;
  --kl-text-2: #a8a7a2;
  --kl-muted: #93928d;
  --kl-faint: #8b8a85;
  --kl-accent: #5aa3d4;
  --kl-running: #d9a445;
  --kl-blocked: #ef8c55;
  --kl-done: #57c398;
  --kl-failed: #ef6f68;
  --kl-queued: #97a0b0;
  --kl-purple: #b9a6ff;
  --kl-font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
}

:where(:root:not([data-kira-theme="dark"])) {
  --kl-surface: #fbfbfa;
  --kl-sunken: #f5f5f3;
  --kl-raised: #ffffff;
  --kl-selected: #ececea;
  --kl-border: #e6e6e3;
  --kl-border-strong: #d9d8d4;
  --kl-text: #37352f;
  --kl-text-2: #585753;
  --kl-muted: #6a6967;
  --kl-faint: #6e6d69;
  --kl-accent: #1a66b3;
  --kl-running: #85600f;
  --kl-blocked: #a84a12;
  --kl-done: #3a7454;
  --kl-failed: #bc3b37;
  --kl-queued: #5a6372;
  --kl-purple: #6b4fc4;
  --kl-font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
}

/* Viewport insets every anchored surface and modal keeps clear of. A host with
 * sticky chrome sets --kira-overlay-inset-top to that chrome's height on the
 * element the surface inherits from (usually :root); the surface then treats
 * that band as unavailable, so it flips rather than sliding under the bar. */
:where(:root) {
  --kira-overlay-inset-top: 8px;
  --kira-overlay-inset-bottom: 8px;
  --kira-overlay-inset-inline: 8px;
  /* Motion tokens — new tokens for the Task Thread waves. The VALUES reuse
   * the package's existing hard-coded set, so nothing here is a new raw
   * duration or easing; components consume them from Wave 1 on.
   * Reduced motion is handled per component (motion sits inside
   * @media (prefers-reduced-motion: no-preference)); there is no global
   * override rule. See docs/task-thread-map.md. */
  --kl-dur-fast: 140ms;
  --kl-dur-base: 160ms;
  --kl-dur-slow: 220ms;
  --kl-dur-slower: 420ms;
  --kl-dur-loop: 1600ms;
  --kl-ease: cubic-bezier(0.32, 0.72, 0, 1);
  --kl-ease-std: cubic-bezier(0.4, 0, 0.2, 1);
}

:where([data-kira-theme="light"]) {
  --kl-surface: #fbfbfa;
  --kl-sunken: #f5f5f3;
  --kl-raised: #ffffff;
  --kl-selected: #ececea;
  --kl-border: #e6e6e3;
  --kl-border-strong: #d9d8d4;
  --kl-text: #37352f;
  --kl-text-2: #585753;
  --kl-muted: #6a6967;
  --kl-faint: #6e6d69;
  --kl-accent: #1a66b3;
  --kl-running: #85600f;
  --kl-blocked: #a84a12;
  --kl-done: #3a7454;
  --kl-failed: #bc3b37;
  --kl-queued: #5a6372;
  --kl-purple: #6b4fc4;
  --kl-font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
}

/* --kl-faint is the 4th text step (the export's --ink4): timestamps, mono
 * meta, de-emphasised labels. It is the faintest role that still clears AA
 * (4.5:1) on the content surfaces in both themes. It fails AA on
 * --kl-selected (4.38 light / 4.15 dark) and must not be used there — use
 * --kl-muted or stronger (docs/task-thread-map.md). */

/* Button — 36 high, radius 6, 15/600 pointer; 44 high, radius 10 touch. */
@layer components {
  .kbtn {
    --kbtn-h: 36px;
    --kbtn-w: auto;
    --kbtn-px: 16px;
    --kbtn-r: 6px;
    --kbtn-fs: 15px;
    --kbtn-fw: 600;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-sizing: border-box;
    margin: 0;
    border: 1px solid transparent;
    font-family: inherit;
    font-style: normal;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    /* A label that cannot fit its container wraps instead of pushing the page
     * sideways (iOS/iPadOS never scroll the page horizontally). Short labels
     * never reach this path: an auto-width flex item still sizes to max-content. */
    white-space: normal;
    max-width: 100%;
    overflow-wrap: anywhere;
    cursor: pointer;
    min-height: var(--kbtn-h);
    width: var(--kbtn-w);
    padding: 0 var(--kbtn-px);
    border-radius: var(--kbtn-r);
    font-size: var(--kbtn-fs);
    font-weight: var(--kbtn-fw);
    -webkit-tap-highlight-color: transparent;
  }

  .kbtn-sm { --kbtn-h: 32px; --kbtn-px: 12px; --kbtn-fs: 13px; --kbtn-fw: 550; }
  .kbtn-lg { --kbtn-h: 40px; --kbtn-px: 24px; --kbtn-r: 10px; }
  .kbtn-icon { --kbtn-w: 36px; --kbtn-px: 0px; }
  .kbtn-pill { --kbtn-px: 16px; --kbtn-r: 999px; --kbtn-fs: 13px; --kbtn-fw: 550; }
  .kbtn-icon-sm { --kbtn-h: 32px; --kbtn-w: 32px; --kbtn-px: 0px; }

  @media (any-pointer: coarse) {
    .kbtn { --kbtn-h: 44px; --kbtn-r: 10px; }
    .kbtn.kbtn-sm { --kbtn-h: 36px; --kbtn-r: 6px; }
    .kbtn.kbtn-lg { --kbtn-h: 48px; }
    .kbtn.kbtn-icon { --kbtn-w: 44px; }
    .kbtn.kbtn-pill { --kbtn-r: 999px; }
    .kbtn.kbtn-icon-sm { --kbtn-h: 36px; --kbtn-w: 36px; --kbtn-r: 6px; }

    /* sm and icon-sm keep their visual box and sit in a 44px hit area. */
    .kbtn.kbtn-sm,
    .kbtn.kbtn-icon-sm { position: relative; }
    .kbtn.kbtn-sm::after,
    .kbtn.kbtn-icon-sm::after {
      content: "";
      position: absolute;
      inset: calc((var(--kbtn-h) - 44px) / 2);
    }

    /* A link-style button is a control in a row, not prose inside a sentence, so
     * WCAG 2.5.8's inline exception does not cover it: it is 30px tall and gets
     * a real 44px box on touch. */
    .kbtn.kbtn-link { min-height: 44px; }
  }

  .kbtn-default { background: var(--kl-text); color: var(--kl-surface); border-color: var(--kl-text); }
  .kbtn-secondary { background: var(--kl-raised); color: var(--kl-text); border-color: var(--kl-border); }
  .kbtn-outline { background: transparent; color: var(--kl-text); border-color: var(--kl-border); }
  .kbtn-ghost { background: transparent; color: var(--kl-text-2); border-color: transparent; }
  .kbtn-destructive { background: var(--kl-failed); color: var(--kl-surface); border-color: var(--kl-failed); }
  .kbtn-link {
    background: transparent;
    color: var(--kl-accent);
    border-color: transparent;
    text-decoration: underline;
    text-underline-offset: 3px;
    padding: 0;
  }

  .kbtn:hover:not(:disabled, [aria-busy="true"]) { filter: brightness(0.96); }
  .kbtn:active:not(:disabled, [aria-busy="true"]) { transform: translateY(1px); filter: brightness(0.94); }
  .kbtn:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }
  .kbtn:disabled { opacity: 0.5; cursor: not-allowed; }
  .kbtn[aria-busy="true"] { opacity: 0.9; }
  .kbtn [data-kbtn-spinner] {
    width: 14px;
    height: 14px;
    box-sizing: border-box;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    display: inline-block;
  }
  .kbtn svg { pointer-events: none; flex-shrink: 0; }
}

/* Badge — status pill 12/600, tag 13/550, count mono 12, code mono 13 x 44. */
@layer components {
  .kbadge {
    --kbadge-fs: 12px;
    --kbadge-fw: 600;
    --kbadge-pad: 4px 12px;
    --kbadge-r: 999px;
    --kbadge-mono: inherit;
    display: inline-block;
    box-sizing: border-box;
    margin: 0;
    border: 0;
    padding: var(--kbadge-pad);
    border-radius: var(--kbadge-r);
    font-family: var(--kbadge-mono);
    font-size: var(--kbadge-fs);
    font-weight: var(--kbadge-fw);
    font-variant-numeric: tabular-nums;
    font-style: normal;
    line-height: normal;
    letter-spacing: normal;
    text-transform: none;
    text-align: left;
    white-space: nowrap;
    vertical-align: baseline;
    background: transparent;
    color: inherit;
  }

  .kbadge-tag { --kbadge-fs: 13px; --kbadge-fw: 550; font-variant-numeric: normal; }
  .kbadge-count {
    --kbadge-fs: 12px;
    --kbadge-fw: 400;
    --kbadge-pad: 0;
    --kbadge-r: 0;
    --kbadge-mono: var(--kl-font-mono);
    font-variant-numeric: normal;
    background: transparent;
  }
  .kbadge-code {
    --kbadge-fs: 13px;
    --kbadge-fw: 400;
    --kbadge-pad: 4px 0;
    --kbadge-r: 6px;
    --kbadge-mono: var(--kl-font-mono);
    min-width: 44px;
    text-align: center;
    font-variant-numeric: normal;
    background: var(--kl-selected);
  }

  /* The drawing's touch size for the two device-sized kinds. An explicit
   * size class has higher specificity, so a pinned size is never overridden. */
  @media (any-pointer: coarse) {
    .kbadge.kbadge-status,
    .kbadge.kbadge-count { --kbadge-fs: 12.5px; }
  }

  .kbadge.kbadge-sm { --kbadge-fs: 12px; }
  .kbadge.kbadge-md { --kbadge-fs: 12.5px; }
  .kbadge.kbadge-xl { --kbadge-fs: 13px; }

  /* A tone tints the pill only; count and code keep their own finish. */
  .kbadge.kbadge-count { background: transparent; }
  .kbadge.kbadge-code { background: var(--kl-selected); }

  /* Toned pills: the neutral tint first (parses everywhere), then the OKLCH
   * mix. Safari below 16.4 rejects `color-mix(in oklch, …)` and keeps the
   * neutral tint, so a toned badge is never a bare transparent box there.
   * The tone still lives in the text colour in both cases. */
  .kbadge-default { color: var(--kl-muted); background: var(--kl-selected); }
  .kbadge-secondary { color: var(--kl-text-2); background: var(--kl-selected); }
  .kbadge-success { color: var(--kl-done); background: var(--kl-selected); background: color-mix(in oklch, var(--kl-done) 12%, transparent); }
  .kbadge-warning { color: var(--kl-running); background: var(--kl-selected); background: color-mix(in oklch, var(--kl-running) 12%, transparent); }
  .kbadge-danger,
  .kbadge-destructive { color: var(--kl-failed); background: var(--kl-selected); background: color-mix(in oklch, var(--kl-failed) 12%, transparent); }
  .kbadge-info { color: var(--kl-queued); background: var(--kl-selected); background: color-mix(in oklch, var(--kl-queued) 12%, transparent); }
  .kbadge-purple { color: var(--kl-purple); background: var(--kl-selected); background: color-mix(in oklch, var(--kl-purple) 12%, transparent); }
  .kbadge-outline {
    color: var(--kl-text-2);
    background: transparent;
    box-shadow: inset 0 0 0 1px var(--kl-border);
  }

  .kbadge-code { background: var(--kl-selected); }
}

/* Segmented control — track on --sunken, raised selected chip. */
@layer components {
  .kseg-track {
    display: flex;
    gap: 2px;
    padding: 2px;
    /* border-box so max-width: 100% bounds the drawn track, not just its
     * content; content-box let the 2px padding + 1px border overshoot. */
    box-sizing: border-box;
    width: fit-content;
    /* Fit the viewport, then scroll the track itself rather than the page:
     * the contained-scroller pattern iPhone/iPad use for wide segmented bars. */
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    background: var(--kl-sunken);
    border: 1px solid var(--kl-border);
    border-radius: 10px;
  }
  .kseg-seg {
    --kseg-h: 32px;
    min-height: var(--kseg-h);
    padding: 0 12px;
    box-sizing: border-box;
    margin: 0;
    border: 1px solid transparent;
    border-radius: 6px;
    font-family: inherit;
    font-style: normal;
    font-size: 15px;
    font-weight: 500;
    line-height: normal;
    letter-spacing: normal;
    text-transform: none;
    text-align: center;
    white-space: nowrap;
    vertical-align: baseline;
    cursor: pointer;
    background: transparent;
    color: var(--kl-text-2);
    box-shadow: none;
    -webkit-tap-highlight-color: transparent;
  }
  .kseg-seg[data-state="selected"] {
    font-weight: 600;
    background: var(--kl-raised);
    border-color: var(--kl-border-strong);
    color: var(--kl-text);
    box-shadow: 0 1px 2px rgb(0 0 0 / 0.12);
  }

  /* Same focus treatment as .kbtn: 2px accent ring at 2px offset. The UA
   * default (1px auto, no offset) is invisible against the track tint. */
  .kseg-seg:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }

  .kseg-track-sm { border-radius: 8px; }
  .kseg-seg-sm { --kseg-h: 28px; padding: 0 8px; font-size: 13px; }
  .kseg-seg-icon { display: inline-flex; align-items: center; gap: 6px; }

  @media (any-pointer: coarse) {
    .kseg-seg { --kseg-h: 44px; }
    .kseg-seg-sm { --kseg-h: 44px; }
  }
}

/* ------------------------------------------------------------------------
 * Chip — the board's filter chip, the search's scope chip and the chat
 * header's project chip. Radius 10, 1px --kl-border, label ink --kl-text-2
 * (--kl-text for project).
 *
 *   filter   44 high · 15 / 550 · 0 16 0 12 · gap 8
 *   scope    30 high · 13 / 550 · 0 11    · gap 4
 *   project  34 high · 15 / 550 · 0 8 0 12 · gap 8
 *
 * The scope chip is the only one whose box follows the pointer: 36 high with
 * 8 of padding on touch, as the design's phone column draws it.
 *
 * A chip is a label, not a control: the design draws no hover, pressed or
 * disabled state, so none is invented here. `selected` and `focus` are the two
 * states it does draw.
 * ------------------------------------------------------------------------ */
@layer components {
  .kchip {
    --kchip-h: 44px;
    --kchip-px: 12px;
    --kchip-px-end: 16px;
    --kchip-gap: 8px;
    --kchip-fs: 15px;
    display: inline-flex;
    align-items: center;
    box-sizing: border-box;
    gap: var(--kchip-gap);
    margin: 0;
    min-height: var(--kchip-h);
    padding: 0 var(--kchip-px-end) 0 var(--kchip-px);
    border: 1px solid var(--kl-border);
    border-radius: 10px;
    font-family: inherit;
    font-style: normal;
    font-size: var(--kchip-fs);
    font-weight: 550;
    /* The design leaves the line box natural (20px at 15/550); line-height: 1
     * would shrink the drawn box. */
    line-height: normal;
    letter-spacing: normal;
    text-transform: none;
    white-space: nowrap;
    background: transparent;
    color: var(--kl-text-2);
  }

  .kchip-scope {
    --kchip-h: 30px;
    --kchip-px: 11px;
    --kchip-px-end: 11px;
    --kchip-gap: 4px;
    --kchip-fs: 13px;
  }
  /* 36 high on touch so the phone column's drawing holds; the 44px tap area
   * belongs to whatever button holds the chip. */
  @media (any-pointer: coarse) {
    .kchip-scope { --kchip-h: 36px; --kchip-px: 8px; --kchip-px-end: 8px; }
  }

  .kchip-project {
    --kchip-h: 34px;
    --kchip-px: 12px;
    --kchip-px-end: 8px;
    color: var(--kl-text);
  }

  .kchip-selected {
    color: var(--kl-surface);
    background: var(--kl-text);
    border-color: var(--kl-text);
  }
  .kchip-focus {
    outline: 2px solid var(--kl-accent);
    outline-offset: 2px;
  }
}

/* ------------------------------------------------------------------------
 * Avatar — one box per kind, no sizes: the round person chip is 24 and the
 * square project tile 22, both 13 / 650 on --kl-selected inside a 1px
 * --kl-border ring. Person uses --kl-text, project the second ink.
 *
 * Hover, focus and pressed belong to the button that holds the avatar, so
 * none is drawn here.
 * ------------------------------------------------------------------------ */
@layer components {
  .kavatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    flex-shrink: 0;
    user-select: none;
    border: 1px solid var(--kl-border);
    background: var(--kl-selected);
    font-size: 13px;
    font-weight: 650;
    /* Natural line box (17px at 13/650), as the design draws it. */
    line-height: normal;
    color: var(--kl-text);
  }
  .kavatar-person { width: 24px; height: 24px; border-radius: 50%; }
  .kavatar-project { width: 22px; height: 22px; border-radius: 6px; color: var(--kl-text-2); }
}

/* ------------------------------------------------------------------------
 * Dialog — Kira's confirm card, as a real modal <dialog>.
 *
 * Isolated block: nothing below is referenced by Button/Badge/SegmentedControl
 * and nothing above is modified for it. Every value is the live, approved
 * Kira modal (frontend .kdialog + .kdialog-title + .kdialog-actions), which
 * the doc's Dialog spec restates with older numbers (radius 18/padding 20/
 * title 17) — the rendered card wins, as asked.
 *
 *   card     width min(440px, 100% − 40px) · radius 22 · padding 24
 *            1px --kl-border · --kl-raised · 0 24 70 rgb(15 15 15 / .25)
 *   title    18 / 650, line-height normal
 *   body     15 / 1.55, --kl-text-2
 *   actions  flex gap 8 · each 46 high · radius 14 · 15 / 550 (600 destructive)
 *   scrim    rgb(15 15 15 / .32) light, rgb(0 0 0 / .55) dark
 *
 * Why <dialog> and not a div: showModal() puts the card in the top layer,
 * makes the rest of the document inert, gives Escape for free and keeps the
 * closed element display:none — so a closed dialog costs no layout, no paint
 * and no listeners. There is no scroll lock to write: the document is inert.
 * ------------------------------------------------------------------------ */
@layer components {
  .kdialog {
    /* :where() so a host's own dialog rules still win at equal specificity. */
    box-sizing: border-box;
    width: min(440px, 100% - 40px);
    max-width: 440px;
    /* The UA centres with margin:auto; keep that, drop its padding/border. */
    margin: auto;
    padding: 24px;
    overflow: auto;
    overscroll-behavior: contain;
    border: 1px solid var(--kl-border);
    border-radius: 22px;
    background: var(--kl-raised);
    box-shadow: 0 24px 70px rgb(15 15 15 / 0.25);
    color: var(--kl-text);
    font-family: inherit;
    font-size: 15px;
    font-weight: 400;
    /* normal, as the live card: the doc's 1.55 is the body copy's, below. */
    line-height: normal;
    letter-spacing: normal;
    text-align: left;
  }

  /* Pinned to the inset band only while modal. A `<dialog>` that is merely
   * `open` (or shown with show()) is in normal flow, where the viewport pin
   * and the dvh cap would stretch it to the screen instead of its content. */
  .kdialog:modal {
    /* The viewport minus the host's declared insets, so a tall card scrolls
     * inside itself rather than bleeding off the screen: the same roles every
     * anchored surface reserves. 100dvh, not 100vh, so a phone's dynamic
     * toolbar is counted. */
    max-height: calc(100dvh - var(--kira-overlay-inset-top, 8px) - var(--kira-overlay-inset-bottom, 8px));
    /* Centre inside the inset band, not the raw viewport: with a host's sticky
     * chrome declared, `inset: 0` would centre the card partly under the bar. */
    position: fixed;
    inset: var(--kira-overlay-inset-top, 8px) var(--kira-overlay-inset-inline, 8px)
      var(--kira-overlay-inset-bottom, 8px);
  }

  .kdialog::backdrop {
    background: rgb(15 15 15 / 0.32);
  }
  /* Dark is declared second so it wins over the light default above. */
  :where([data-kira-theme="dark"]) .kdialog::backdrop {
    background: rgb(0 0 0 / 0.55);
  }

  .kdialog-title {
    margin: 0;
    padding: 0;
    font-size: 18px;
    font-weight: 650;
    line-height: normal;
    letter-spacing: normal;
    color: var(--kl-text);
  }

  .kdialog-desc {
    margin: 8px 0 0;
    padding: 0;
    font-size: 15px;
    font-weight: 400;
    line-height: 1.55;
    color: var(--kl-text-2);
  }

  .kdialog-actions {
    display: flex;
    gap: 8px;
    margin-top: 16px;
  }

  .kdialog-action {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 46px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 14px;
    font-family: inherit;
    font-size: 15px;
    font-weight: 550;
    line-height: normal;
    letter-spacing: normal;
    color: var(--kl-text);
    background: var(--kl-selected);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .kdialog-action:hover { filter: brightness(0.96); }
  .kdialog-action:active { filter: brightness(0.92); }
  .kdialog-action:focus-visible,
  .kdialog:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }
  .kdialog-action:disabled { opacity: 0.5; cursor: not-allowed; }
  .kdialog-action-destructive {
    font-weight: 600;
    /* Same ink rule as .kbtn-destructive: --kl-surface, not #fff. In light the
     * two are visually identical (#fbfbfa); in dark --kl-surface is the dark
     * ink, which is what keeps white-on-#ef6f68 from failing at ~2.5:1. */
    color: var(--kl-surface);
    background: var(--kl-failed);
  }

  /* One small entrance, transform + opacity only, as the library's motion
   * rule asks. @starting-style is what makes an appearing <dialog> animate at
   * all; browsers without it simply appear. Reduced motion gets no motion. */
  @media (prefers-reduced-motion: no-preference) {
    .kdialog[open] {
      animation: kdialog-in 160ms cubic-bezier(0.32, 0.72, 0, 1);
    }
    @starting-style {
      .kdialog[open] { opacity: 0; transform: translateY(8px) scale(0.98); }
    }
    @keyframes kdialog-in {
      from { opacity: 0; transform: translateY(8px) scale(0.98); }
    }
  }

  /* A 44px touch floor for the action row, matching .kbtn's own rule. */
  @media (any-pointer: coarse) {
    .kdialog-action { min-height: 48px; }
  }
}

/* ------------------------------------------------------------------------
 * Input — the library's Input section, as a real <input>.
 *
 * Numbers are the approved standalone's (sha 1a62d561…), LIB_PX + LIB_SPECS:
 *
 *   search   100% · 54 tall (52 phone) · pad 0 14 (pad-right 8 phone) · radius 14
 *            · gap 12 · 18px muted icon
 *   field    max 260 · 36 tall (44 touch) · pad 0 12 · radius 10 · gap 8
 *   state    36 tall (44 touch) · pad 0 12 · radius 10
 *   text     15px, 16px on touch (the iOS zoom floor the section states)
 *   box      --kl-raised, 1px --kl-border; placeholder --kl-muted, value --kl-text
 *
 * One deliberate delta from the drawing: the drawing's focus is "caret only,
 * outline none" and its spec table flags that as undecided. A real field keeps
 * the caret and adds the same 2px accent ring .kbtn/.kseg use, so keyboard
 * focus stays visible.
 * ------------------------------------------------------------------------ */
@layer components {
  .kinput-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
  }

  .kinput-label {
    font-family: inherit;
    font-size: 13px;
    font-weight: 550;
    line-height: normal;
    color: var(--kl-text-2);
  }

  .kinput-error {
    margin: 0;
    font-family: inherit;
    font-size: 13px;
    line-height: 1.4;
    color: var(--kl-failed);
  }

  .kinput {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    background: var(--kl-raised);
    border: 1px solid var(--kl-border);
  }
  .kinput-invalid { border-color: var(--kl-failed); }

  .kinput-search {
    gap: 12px;
    height: 54px;
    padding: 0 14px;
    border-radius: 14px;
  }
  .kinput-field {
    gap: 8px;
    max-width: 260px;
    height: 36px;
    padding: 0 12px;
    border-radius: 10px;
  }
  .kinput-state {
    gap: 8px;
    height: 36px;
    padding: 0 12px;
    border-radius: 10px;
  }
  .kinput-icon { flex: none; color: var(--kl-muted); }

  /* The drawing's `$` and `per month`: 15px value ink, 13.5px unit. */
  .kinput-affix {
    flex: none;
    font-family: inherit;
    font-size: 15px;
    line-height: normal;
    white-space: nowrap;
    color: var(--kl-text);
  }
  .kinput-suffix { font-size: 13.5px; color: var(--kl-muted); }

  /* The input itself: no box of its own, the wrapper draws it. The 16px
   * font on touch is the zoom floor, not a style choice. */
  .kinput-text {
    flex: 1;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    font-family: inherit;
    font-size: 15px;
    line-height: normal;
    color: var(--kl-text);
    appearance: none;
  }
  .kinput-text::placeholder { color: var(--kl-muted); opacity: 1; }
  .kinput-text:focus { outline: none; }
  .kinput-text:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }
  .kinput-text:disabled { color: var(--kl-muted); cursor: not-allowed; }
  .kinput:has(.kinput-text:disabled) { opacity: 0.6; }

  @media (any-pointer: coarse) {
    .kinput-field,
    .kinput-state { height: 44px; }
    .kinput-text { font-size: 16px; }
  }
  /* The drawing's phone search is 52 tall with an 8px right pad; iPad keeps
   * 54/14. The split is by viewport, as ChatInput's iPad radius is, because
   * `any-pointer: coarse` alone cannot tell an iPad from an iPhone. */
  @media (any-pointer: coarse) and (max-width: 699px) {
    .kinput-search { height: 52px; padding-right: 8px; }
  }
}

/* ------------------------------------------------------------------------
 * Tabs — the underline nav for page navigation and the setup guide.
 *
 * The drawing (approved export, Tabs section): a row of labels on a 1px
 * --kl-border rule, no track; the selected label carries a 2px --kl-text bar
 * under it, as wide as the word. Values are the export's measured ones:
 *
 *   nav     gap 30 · tab 54 high on a pointer; 50 / 24 on an iPad-sized touch
 *           screen; 46 / 22 on a phone
 *   tab     15 / 500, --kl-muted; hover and selected --kl-text, 600 selected
 *   rule    2px, radius 2, bottom -1px so it lands on the nav's own 1px rule
 *
 * The export keys its three drawings off [data-device], which this package does
 * not have. Touch is one drawing here, so its iPad step is the coarse pointer
 * at a tablet width — the same rule ChatInput uses for its iPad radius.
 * ------------------------------------------------------------------------ */
@layer components {
  .ktabs {
    display: flex;
    gap: var(--ktabs-gap, 30px);
    box-sizing: border-box;
    width: 100%;
    border-bottom: 1px solid var(--kl-border);
    /* More tabs than fit (a phone, a long label) scroll inside the nav, the
     * way a wide segmented track does, instead of being clipped away: every
     * tab stays reachable by touch and by keyboard. The 1px click overshoot of
     * the selected rule is clipped vertically, as the drawing clips it, so no
     * scrollbar appears and the divider on the nav's own border box — outside
     * the scroll box — stays visible at every scroll offset. */
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
  }
  .ktab {
    position: relative;
    flex: none;
    display: flex;
    align-items: center;
    height: var(--ktabs-tab-h, 54px);
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    font-family: inherit;
    font-style: normal;
    font-size: 15px;
    font-weight: 500;
    line-height: normal;
    letter-spacing: normal;
    text-transform: none;
    white-space: nowrap;
    color: var(--kl-muted);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .ktab:hover,
  .ktab[data-state="selected"] { color: var(--kl-text); }
  .ktab[data-state="selected"] { font-weight: 600; }
  /* The nav clips the rule's 1px overshoot, so the ring sits inside the tab:
   * an outside ring would be cut at the nav's top edge. */
  .ktab:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: -2px; }
  /* The rule: a pseudo-element, so the tab's markup is its label. Same ink
   * as the text (`currentColor`), which is what the export draws. */
  .ktab::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: -1px;
    height: 2px;
    border-radius: 2px;
    background: transparent;
  }
  .ktab[data-state="selected"]::after { background: currentColor; }

  .ktabs-panel { margin-top: 16px; }
  .ktabs-panel:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }

  @media (any-pointer: coarse) {
    .ktabs { --ktabs-gap: 22px; --ktabs-tab-h: 46px; }
  }
  /* The drawing's iPad step sits between the pointer and phone ones. */
  @media (any-pointer: coarse) and (min-width: 700px) {
    .ktabs { --ktabs-gap: 24px; --ktabs-tab-h: 50px; }
  }
}

  /* TabNav — the same row of labels, as page navigation (real links), with the
   * selected pill of the segmented control instead of the underline.
   *
   * --ktabnav-h is the link's tap box and the bar's drawn height (a host may
   * pin its own; 44px is the HIG floor). The pill is that height minus 12px, so
   * the fill keeps an equal 6px gap to both edges of its box and never touches
   * a rule above or below it — the box is the target, the pill is the drawing. */
@layer components {
  .ktabnav {
    --ktabnav-pill: calc(var(--ktabnav-h, 44px) - 12px);
    display: flex;
    align-items: center;
    height: var(--ktabnav-h, 44px);
    min-height: var(--ktabnav-h, 44px);
    border-bottom: 0;
    gap: 0 var(--ktabs-gap, 30px);
  }
  .ktabnav .ktab {
    /* Own stacking context, so the pill can sit behind the label: an absolutely
     * positioned pseudo-element paints above in-flow text otherwise, and a solid
     * fill would erase the selected label. */
    isolation: isolate;
    height: var(--ktabnav-h, 44px);
    /* The pill's horizontal padding: the fill is wider than the word by 12px
     * each side, and the link's hit box grows with it. */
    padding: 0 12px;
    color: var(--kl-muted);
  }
  /* The pill: an inner box, so it can be shorter than the link without the tap
   * target shrinking. It spans the link's padding box, so the fill is the word
   * plus 12px either side. */
  .ktabnav .ktab::before {
    content: "";
    position: absolute;
    inset: 50% 0 auto;
    height: var(--ktabnav-pill);
    border: 1px solid transparent;
    border-radius: 6px;
    transform: translateY(-50%);
    z-index: -1; /* behind the label; inside the link's own context */
    pointer-events: none;
  }
  /* No underline in this drawing: the pill is the only indicator. */
  .ktabnav .ktab::after { content: none; }
  .ktabnav .ktab:hover::before { background: var(--kl-sunken); }
  .ktabnav .ktab[data-state="selected"] { font-weight: 600; }
  .ktabnav .ktab[data-state="selected"]::before {
    background: var(--kl-raised);
    border-color: var(--kl-border-strong);
    box-shadow: 0 1px 2px rgb(0 0 0 / 0.12);
  }
  /* The ring follows the pill, not the 44px box: an inset ring on the box would
   * draw on top of the bar's rule. */
  .ktabnav .ktab:focus-visible { outline: none; }
  .ktabnav .ktab:focus-visible::before {
    outline: 2px solid var(--kl-accent);
    outline-offset: 2px;
  }
}

/* Menu — a 300px popover, radius 14, padding 8; rows 38/44 with a 12 gap.
 *
 * The design draws the phone variant as a Sheet. This ships the popover at
 * every width instead: it is clamped to the viewport, so a narrow screen gets
 * the same menu, inset rather than resized into a sheet the package does not
 * have. The 52px phone row belongs to the sheet and is not applied here.
 *
 * Placement is CSS anchor positioning, same contract as Popover: the trigger
 * carries a per-instance `anchor-name` and the surface an inline
 * `position-anchor`; this file turns the pair into `position-area` plus a
 * flip/shift/pin ladder. The `--kira-overlay-inset-*` roles are margins, so
 * the browser's fit test treats the host's sticky chrome as unavailable space.
 * Engines without anchor positioning get `left`/`top` from the component's
 * single measured placement, against the same insets. */
@layer components {
  .kmenu { display: inline-flex; }
  /* A word trigger sizes to its label and never breaks mid-word: an icon-only
   * trigger is unchanged: 10 + 16 + 10 = 36 on a pointer, 14 + 16 + 14 = 44 on
   * touch. `max-content` and `nowrap` are both needed — `.kbtn`'s
   * `overflow-wrap: anywhere` makes the button's min-content one character, so
   * a shrinkable flex parent would otherwise stack the label letter by letter. */
  /* `.kmenu-trigger` alone is not enough: the trigger is rendered with
   * `size="icon"`, and `.kbtn-icon` sets the same properties at the same
   * specificity, so whichever comes later wins. It came later, leaving a 44px
   * box whose centred 147px label spilled out over the card edge. The extra
   * class is not enough, because the coarse-pointer rule is `.kbtn.kbtn-icon`;
   * matching on both the button and the trigger classes wins outright. */
  .kmenu .kbtn.kmenu-trigger {
    --kbtn-w: max-content;
    /* Zero padding plus a min-width of the control height is what keeps an
     * icon-only trigger exactly 36/44: 10px of inline padding would add to the
     * icon and land on 38, which is why the padding is handed back below only
     * when the trigger actually carries a label. */
    --kbtn-px: 0px;
    min-width: var(--kbtn-h);
    white-space: nowrap;
    flex: 0 0 auto;
  }
  /* A trigger with anything but a lone icon has a label, and gets its padding
   * back so the label is not flush against the glyph. */
  .kmenu .kbtn.kmenu-trigger:has(> :not(svg)) { --kbtn-px: 10px; }
  @media (any-pointer: coarse) {
    .kmenu .kbtn.kmenu-trigger:has(> :not(svg)) { --kbtn-px: 14px; }
  }

  /* Fixed, because the surface is placed from the trigger's box so it can flip
   * above the trigger and stay inside the viewport, not the page. */
  .kmenu-popover {
    position: fixed;
    inset: auto;
    z-index: 60;
    box-sizing: border-box;
    width: 300px;
    /* The design's 300, clamped to the viewport between the insets: on a 320
     * screen it insets instead of overflowing. */
    max-width: calc(100vw - 2 * var(--kmenu-inset-inline, 8px));
    max-height: calc(100dvh - var(--kmenu-inset-top, 8px) - var(--kmenu-inset-bottom, 8px));
    overflow: auto;
    overscroll-behavior: contain;
    padding: 8px;
    margin: 0;
    border: 1px solid var(--kl-border);
    border-radius: 14px;
    background: var(--kl-raised);
    color: var(--kl-text);
    box-shadow: 0 18px 44px rgba(0, 0, 0, 0.28);
    font-family: inherit;
    --kmenu-inset-top: var(--kira-overlay-inset-top, 8px);
    --kmenu-inset-bottom: var(--kira-overlay-inset-bottom, 8px);
    --kmenu-inset-inline: var(--kira-overlay-inset-inline, 8px);
    --kmenu-gap: 6px;
  }

  /* ---- CSS anchor positioning. Inert where unsupported, so the fallback's
   * left/top stand alone exactly as in Popover. */
  @supports (anchor-name: --a) {
    .kmenu-popover {
      /* The design's right alignment: the panel's inline-end meets the
       * trigger's inline-end, then the flips below take over. */
      position-area: block-end span-inline-end;
      margin-block: var(--kmenu-gap) var(--kmenu-inset-bottom);
      margin-inline: var(--kmenu-inset-inline);
      position-try-fallbacks: --kmenu-near, --kmenu-above, --kmenu-above-near, --kmenu-pin;
      position-visibility: anchors-visible;
    }

    @position-try --kmenu-near {
      position-area: block-end span-inline-start;
    }
    @position-try --kmenu-above {
      position-area: block-start span-inline-end;
      margin-block: var(--kmenu-inset-top) var(--kmenu-gap);
    }
    @position-try --kmenu-above-near {
      position-area: block-start span-inline-start;
      margin-block: var(--kmenu-inset-top) var(--kmenu-gap);
    }
    /* Last resort: pinned inside the inset box and scrolling internally. */
    @position-try --kmenu-pin {
      position-area: none;
      inset: var(--kmenu-inset-top) auto auto var(--kmenu-inset-inline);
      margin: 0;
      width: auto;
      max-width: calc(100vw - 2 * var(--kmenu-inset-inline));
      height: auto;
    }
  }
  .kmenu-row {
    --kmenu-row-h: 38px;
    display: flex;
    align-items: center;
    gap: 12px;
    box-sizing: border-box;
    width: 100%;
    min-height: var(--kmenu-row-h);
    margin: 0;
    padding: 0 12px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--kl-text);
    font-family: inherit;
    font-size: 15px;
    font-weight: 500;
    text-align: left;
    line-height: normal;
    white-space: normal;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .kmenu-row:hover:not(:disabled),
  .kmenu-row:focus-visible { background: var(--kl-selected); }
  .kmenu-row:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: -2px; }
  .kmenu-row:disabled,
  .kmenu-row[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }

  /* The leading glyph: the design's 17px box, secondary ink, never shrinks. */
  .kmenu-icon {
    display: inline-flex;
    flex: none;
    align-items: center;
    color: var(--kl-text-2);
  }

  .kmenu-label { min-width: 0; font-weight: 400; }
  .kmenu-row[data-current] .kmenu-label { font-weight: 600; }
  .kmenu-note { flex: none; font-size: 13px; color: var(--kl-muted); }

  /* The label and its description stack in one column that flexes; a plain
   * row's DOM is label-only and renders identically to before. */
  .kmenu-text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  .kmenu-desc { font-size: 13px; font-weight: 400; color: var(--kl-muted); }

  /* Section heading: text only, never a focus stop (the arrows walk rows). */
  .kmenu-heading {
    padding: 8px 12px 4px;
    font-size: 13px;
    font-weight: 600;
    color: var(--kl-muted);
  }
  .kmenu-group + .kmenu-group { margin-top: 4px; border-top: 1px solid var(--kl-border); }

  /* The check slot is fixed width so unchecked radio rows line up. */
  .kmenu-check {
    flex: none;
    display: inline-flex;
    justify-content: center;
    min-width: 17px;
    color: var(--kl-accent);
  }

  /* The HIG's 44px floor for a finger, on the query the rest of the file uses. */
  @media (any-pointer: coarse) {
    .kmenu-row { --kmenu-row-h: 44px; }
  }
}

/* ------------------------------------------------------------------------
 * Toast — Kira's one-row confirmation, ported from the live .ktoast.
 *
 *   row      44 min · padding 0 8 0 16 · gap 12 · radius 14
 *            --kl-selected · 1px --kl-border · 0 14 40 rgb(0 0 0 / .55)
 *            15px --kl-text, one line, ellipsis
 *   mark     16px, decorative
 *   action   draws a .kbtn (size sm) at 32 high, --kl-text on --kl-raised
 *   dismiss  32 × 32 · radius 10 · --kl-muted, 14px X
 *   touch    the X sits in a 44px hit area, as .kbtn-icon-sm does
 *
 * The live drawing is dark only (delta §Toast): --kl-selected with that
 * shadow is right on a dark page and a grey slab on a light one, so light
 * takes the raised surface and a soft shadow. Same stacking rule as above:
 * dark first, explicit light last.
 * ------------------------------------------------------------------------ */
@layer components {
  .ktoast {
    display: flex;
    align-items: center;
    gap: 12px;
    box-sizing: border-box;
    max-width: 100%;
    min-height: 44px;
    margin: 0;
    padding: 0 8px 0 16px;
    border: 1px solid var(--kl-border);
    border-radius: 14px;
    background: var(--kl-selected);
    box-shadow: 0 14px 40px rgb(0 0 0 / 0.55);
    color: var(--kl-text);
    font-family: inherit;
    font-size: 15px;
    font-weight: 400;
    line-height: normal;
    letter-spacing: normal;
    text-align: left;
  }
  :where(:root:not([data-kira-theme="dark"])) .ktoast,
  :where([data-kira-theme="light"]) .ktoast {
    background: var(--kl-raised);
    box-shadow: 0 1px 2px rgb(15 15 15 / 0.05), 0 14px 40px rgb(15 15 15 / 0.18);
  }

  .ktoast-mark {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    color: var(--kl-text);
  }

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

  .ktoast-action-slot { display: inline-flex; flex: none; }

  .ktoast-dismiss {
    display: flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--kl-muted);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .ktoast-dismiss:hover { color: var(--kl-text); background: var(--kl-border); }
  .ktoast-dismiss:active { background: var(--kl-border-strong); }
  .ktoast-dismiss:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }

  /* Touch keeps the 32px drawing and puts a 44px hit area around it. */
  @media (any-pointer: coarse) {
    .ktoast-dismiss { position: relative; }
    .ktoast-dismiss::after {
      content: "";
      position: absolute;
      inset: -6px;
    }
  }

  /* One small entrance, transform + opacity only; reduced motion opts out. */
  @media (prefers-reduced-motion: no-preference) {
    .ktoast { animation: ktoast-in 220ms cubic-bezier(0.32, 0.72, 0, 1); }
    @keyframes ktoast-in {
      from { opacity: 0; transform: translateY(8px); }
    }
  }
}

/* ChatInput — the Composer specimen's field and send disc: card hairline on
 * --kl-raised, radius 16/18/22, text 15px/16px, a 32px disc in a 36/44 target,
 * and the drawing's Off rim. Roles only. */
@layer components {
  .kchat {
    --kchat-r: 16px;
    --kchat-fs: 15px;
    --kchat-tap: 36px;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    border: 1px solid var(--kl-border);
    border-radius: var(--kchat-r);
    background: var(--kl-raised);
    box-shadow: 0 2px 12px rgb(15 15 15 / 0.05);
    color: var(--kl-text);
    font-family: inherit;
  }
  .kchat:focus-within { border-color: var(--kl-border-strong); }
  .kchat:has(.kchat-field:focus-visible) {
    box-shadow: 0 2px 12px rgb(15 15 15 / 0.05), 0 0 0 2px var(--kl-accent);
  }
  /* Native textarea: Enter, IME, undo and autocorrect are the browser's. */
  .kchat-field {
    display: block;
    box-sizing: border-box;
    width: 100%;
    margin: 0;
    padding: 12px 16px 4px;
    /* Grows with the text to about four rows, then scrolls. field-sizing is
     * the browser's own sizing; where it is missing the field stays one row
     * and scrolls, which is the same thing at the other end of the range. */
    field-sizing: content;
    max-height: calc(var(--kchat-fs) * 1.4 * 4 + 16px);
    border: 0;
    outline: none;
    overflow-y: auto;
    resize: none;
    background: transparent;
    color: inherit;
    font: inherit;
    font-size: var(--kchat-fs);
    line-height: 1.4;
  }
  .kchat-field::placeholder { color: var(--kl-muted); }
  .kchat-row {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px 8px;
  }
  .kchat-lead { display: flex; align-items: center; gap: 4px; min-width: 0; }
  .kchat-tail { display: flex; align-items: center; gap: 4px; margin-left: auto; }
  /* A caller's model chip gets the drawing's 32/36px pill; its content is the
   * caller's, so only the box is ours. */
  .kchat-chip {
    display: flex;
    align-items: center;
    gap: 8px;
    height: 32px;
    padding: 0 12px 0 8px;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 500;
    color: var(--kl-text-2);
    white-space: nowrap;
  }
  /* The disc is the drawing; the button is the hit target. */
  .kchat-send {
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    width: var(--kchat-tap);
    height: var(--kchat-tap);
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--kl-surface);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .kchat-send:disabled { cursor: default; }
  .kchat-send:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }
  .kchat-send[aria-busy="true"] { opacity: 0.55; }
  .kchat-disc {
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--kl-selected);
    color: var(--kl-muted);
  }
  .kchat-send[data-ready] .kchat-disc,
  .kchat-send[data-stop] .kchat-disc { background: var(--kl-text); color: var(--kl-surface); }
  .kchat[data-off] { opacity: 0.7; }
  .kchat[data-off] .kchat-disc { background: transparent; border: 1.5px dashed var(--kl-muted); }

  @media (any-pointer: coarse) {
    .kchat { --kchat-fs: 16px; --kchat-tap: 44px; --kchat-r: 22px; }
    .kchat-chip { height: 36px; }
  }
  /* The drawing's iPad radius sits between the pointer and phone steps. */
  @media (any-pointer: coarse) and (min-width: 700px) {
    .kchat { --kchat-r: 18px; }
  }
}

/* ------------------------------------------------------------------------
 * Sheet — Kira's bottom sheet, as a real modal <dialog>.
 *
 * Isolated block: nothing above is modified and nothing below is referenced by
 * the other components. Numbers are the drawn ones from the standalone design
 * of record (LIB:538-541, LIB:1357): 22/22/0/0 corners, elev.sheet, grabber
 * 38 x 5, pinned header with a 44 x 44 close around a 30px chip, padding
 * 0 12, bottom max(16px, safe area), 84% cap at content height.
 *
 * Why <dialog> and not a portal div: showModal() gives the top layer, an inert
 * document, Escape via `cancel`, focus restoration and display:none while
 * closed — the same platform Dialog already uses, with no global state and no
 * scroll-lock bookkeeping. The panel is bottom-anchored with the UA's own
 * margin box overridden, so there is no extra wrapper element.
 * ------------------------------------------------------------------------ */
@layer components {
  .ksheet {
    margin: 0 auto;
    width: 100%;
    max-width: 560px;
    box-sizing: border-box;
    padding: 0;
    overflow: hidden;
    border: 0;
    border-radius: 22px 22px 0 0;
    background: var(--kl-raised);
    box-shadow: 0 -14px 44px rgb(15 15 15 / 0.22);
    color: var(--kl-text);
    font-family: inherit;
    font-size: 15px;
    font-weight: 400;
    line-height: normal;
    text-align: left;
    /* Only a drag writes transform; this is its per-frame consequence. */
    --ksheet-scrim: 1;
    will-change: transform;
  }

  /* Pinned to the bottom edge only while modal. A `<dialog>` that is merely
   * `open` (or shown with show()) keeps its natural in-flow size. */
  .ksheet:modal {
    /* The UA centres a modal dialog; pin it to the bottom edge instead and let
     * the block margins collapse to nothing above it. */
    position: fixed;
    inset: auto 0 0;
    /* 84% of the viewport, never taller than the viewport minus the host's
     * declared insets: the body below scrolls, so a long list moves inside
     * the cap instead of the sheet bleeding off the top. The dialog is fixed,
     * so a percentage is the viewport (no whole-document re-resolve on open);
     * 100dvh is the fallback where the percentage has no parent box. */
    max-height: min(84%, calc(100dvh - var(--kira-overlay-inset-top, 8px)));
  }
  .ksheet::backdrop {
    background: rgb(15 15 15 / 0.32);
    opacity: var(--ksheet-scrim, 1);
  }
  :where([data-kira-theme="dark"]) .ksheet::backdrop {
    background: rgb(0 0 0 / 0.55);
  }

  /* The panel is the dialog's content box; the body flexes inside the cap and
   * is the only scroller, so the header stays put. */
  /* [open] so the UA's `dialog:not([open]) { display: none }` still stands:
   * a closed sheet keeps costing no box, no paint and no listeners. */
  .ksheet[open] {
    display: flex;
    flex-direction: column;
  }
  .ksheet-surface {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-height: 0;
    padding: 0 12px max(16px, env(safe-area-inset-bottom));
    box-sizing: border-box;
  }

  .ksheet-grabber-row {
    display: flex;
    flex: none;
    justify-content: center;
    padding: 8px 0 4px;
    cursor: grab;
    touch-action: none;
  }
  .ksheet-grabber {
    display: block;
    width: 38px;
    height: 5px;
    border-radius: 2px;
    background: var(--kl-border);
  }

  .ksheet-header {
    display: flex;
    flex: none;
    align-items: center;
    padding: 4px 4px 8px 12px;
    touch-action: none;
  }
  .ksheet-title {
    flex: 1;
    min-width: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
    font-size: 18px;
    font-weight: 650;
    line-height: normal;
    letter-spacing: normal;
    color: var(--kl-text);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .ksheet-close {
    display: flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 22px;
    background: transparent;
    color: var(--kl-muted);
    font: inherit;
    cursor: pointer;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
  }
  .ksheet-close-chip {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 14px;
    background: var(--kl-selected);
  }
  .ksheet-close:focus-visible,
  .ksheet:focus-visible { outline: 2px solid var(--kl-accent); outline-offset: 2px; }

  /* The scroller: a long list scrolls inside the 84% cap. */
  .ksheet-body {
    min-height: 0;
    flex: 1 1 auto;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* One entrance, transform + opacity only, as the library's motion rule asks.
   * @starting-style is what lets an appearing <dialog> animate at all; without
   * it (or with reduced motion) the sheet simply appears in place. */
  @media (prefers-reduced-motion: no-preference) {
    .ksheet[open] {
      animation: ksheet-in 420ms cubic-bezier(0.32, 0.72, 0, 1);
    }
    @starting-style {
      .ksheet[open] { opacity: 0; transform: translate3d(0, 100%, 0); }
    }
    @keyframes ksheet-in {
      from { opacity: 0; transform: translate3d(0, 100%, 0); }
    }
  }
}

/* ------------------------------------------------------------------------
 * Popover — a non-modal anchored panel.
 *
 * The box is the design's floating surface: a --kl-raised card, 1px
 * --kl-border, radius 14, an 8px inset, and the same soft shadow family as the
 * dialog one size down.
 *
 * Placement is CSS anchor positioning. The component puts a unique
 * `anchor-name` on the trigger and `position-anchor` on the panel (as an inline
 * style, because the name is per instance); the rules here turn that into a
 * `position-area` per `data-placement`, plus a fallback ladder:
 *
 *   --kpop-near        the same side, shifted to the other inline edge
 *   --kpop-flip        the opposite block side (above instead of below)
 *   --kpop-flip-near   the opposite block side, other inline edge
 *   --kpop-pin         last resort: pinned to the inset box, full width capped,
 *                      scrolling inside itself
 *
 * The viewport insets are --kira-overlay-inset-* (tokens/styles.css) and are
 * applied as a MARGIN on the panel. That is what makes the browser's own
 * fallback test treat sticky chrome the host declared as unavailable space: a
 * panel whose margin box does not fit the viewport falls through to the flip
 * before it is ever allowed to sit under the bar.
 *
 * `max-block-size` caps the panel at the inset box and `overflow: auto` scrolls
 * it internally, so no content can make it leave the screen.
 *
 * Engines without anchor positioning (WebKit 18.2) ignore every rule above and
 * get `left`/`top` from the component's `place()` fallback, against the same
 * insets; the box rules here apply to both paths unchanged.
 * ------------------------------------------------------------------------ */
@layer components {
  .kpop {
    box-sizing: border-box;
    position: fixed;
    /* The design's panel width; a host overrides with its own style prop. */
    width: max-content;
    min-width: 220px;
    max-width: min(360px, calc(100vw - 2 * var(--kpop-inset-inline, 8px)));
    max-height: calc(100dvh - var(--kpop-inset-top, 8px) - var(--kpop-inset-bottom, 8px));
    overflow: auto;
    overscroll-behavior: contain;
    padding: 8px;
    border: 1px solid var(--kl-border);
    border-radius: 14px;
    background: var(--kl-raised);
    box-shadow: 0 16px 44px rgb(15 15 15 / 0.2);
    color: var(--kl-text);
    font-family: inherit;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.4;
    letter-spacing: normal;
    text-align: left;
    /* The UA's [popover] rule sets inset:0 + margin:auto; the position comes
     * from the anchor rules below instead, in both the promoted and the plain
     * path. */
    inset: auto;
    margin: 0;
    /* Touch: never let a hold-and-drag on the panel text-select behind it. */
    -webkit-tap-highlight-color: transparent;
    /* The inset roles, aliased once so the fallback rules can use the same
     * values the component's JS reads. */
    --kpop-inset-top: var(--kira-overlay-inset-top, 8px);
    --kpop-inset-bottom: var(--kira-overlay-inset-bottom, 8px);
    --kpop-inset-inline: var(--kira-overlay-inset-inline, 8px);
    --kpop-gap: 8px;
  }

  /* A touch floor for the panel's own rows, as every other control gets. */
  @media (any-pointer: coarse) {
    .kpop { font-size: 15px; }
  }

  /* ---- CSS anchor positioning ------------------------------------------
   * Every rule below is inert where the engine does not support it: an
   * unknown property and an unparsed value are dropped, leaving the plain
   * fixed box the fallback positions. */
  @supports (anchor-name: --a) {
    .kpop {
      /* Placed against the trigger's named anchor, never the viewport. */
      position-area: block-end span-inline-start;
      /* The outer margins ARE the insets: the browser tests this margin box
       * against the viewport, so a panel that would land under the host's
       * sticky chrome fails the fit and the next fallback is tried. */
      margin-block: var(--kpop-gap) var(--kpop-inset-bottom);
      margin-inline: var(--kpop-inset-inline);
      /* Ordered: the other inline edge first (cheapest move), then the other
       * block side, then both, then the pinned last resort. */
      position-try-fallbacks: --kpop-near, --kpop-flip, --kpop-flip-near, --kpop-pin;
      /* An anchor scrolled out of view hides the panel rather than parking it
       * in a corner with nothing to hang from. */
      position-visibility: anchors-visible;
    }

    /* The eight placements, in the physical terms the props document. */
    .kpop[data-placement="bottom-start"] { --_kpop-area: block-end span-inline-start; --_kpop-area-near: block-end span-inline-end; --_kpop-area-flip: block-start span-inline-start; --_kpop-area-flip-near: block-start span-inline-end; }
    .kpop[data-placement="bottom"] { --_kpop-area: block-end center; --_kpop-area-near: block-end center; --_kpop-area-flip: block-start center; --_kpop-area-flip-near: block-start center; }
    .kpop[data-placement="bottom-end"] { --_kpop-area: block-end span-inline-end; --_kpop-area-near: block-end span-inline-start; --_kpop-area-flip: block-start span-inline-end; --_kpop-area-flip-near: block-start span-inline-start; }
    .kpop[data-placement="top-start"] { --_kpop-area: block-start span-inline-start; --_kpop-area-near: block-start span-inline-end; --_kpop-area-flip: block-end span-inline-start; --_kpop-area-flip-near: block-end span-inline-end; }
    .kpop[data-placement="top"] { --_kpop-area: block-start center; --_kpop-area-near: block-start center; --_kpop-area-flip: block-end center; --_kpop-area-flip-near: block-end center; }
    .kpop[data-placement="top-end"] { --_kpop-area: block-start span-inline-end; --_kpop-area-near: block-start span-inline-start; --_kpop-area-flip: block-end span-inline-end; --_kpop-area-flip-near: block-end span-inline-start; }
    .kpop[data-placement="right"] { --_kpop-area: inline-end center; --_kpop-area-near: inline-start center; --_kpop-area-flip: inline-end center; --_kpop-area-flip-near: inline-start center; }
    .kpop[data-placement="left"] { --_kpop-area: inline-start center; --_kpop-area-near: inline-end center; --_kpop-area-flip: inline-start center; --_kpop-area-flip-near: inline-end center; }

    .kpop {
      position-area: var(--_kpop-area, block-end span-inline-start);
    }

    /* The block-edge distance is the anchor gap; the opposite outer margin is
     * the viewport inset, so the fit test reserves sticky chrome on the side
     * the panel is moving toward. */
    @position-try --kpop-near {
      position-area: var(--_kpop-area-near);
      margin-block: var(--kpop-gap) var(--kpop-inset-bottom);
      margin-inline: var(--kpop-inset-inline);
    }
    @position-try --kpop-flip {
      position-area: var(--_kpop-area-flip);
      margin-block: var(--kpop-inset-top) var(--kpop-gap);
      margin-inline: var(--kpop-inset-inline);
    }
    @position-try --kpop-flip-near {
      position-area: var(--_kpop-area-flip-near);
      margin-block: var(--kpop-inset-top) var(--kpop-gap);
      margin-inline: var(--kpop-inset-inline);
    }
    /* The measured path. The component sets this when the engine has no anchor
     * positioning, or when the trigger sits under a transformed ancestor that
     * Chromium resolves the anchor against wrongly. Dropping position-area and
     * the try list hands the placement to the inline left/top alone. */
    .kpop[data-anchor-position="js"] {
      position-area: none;
      position-try-fallbacks: none;
      position-visibility: always;
      margin: 0;
    }

    /* Pinned: the panel sits at the inset box's inline edge and top, full
     * inline size inside the insets, still scrolling internally. This is the
     * one case that cannot keep the anchor's side — a panel taller than the
     * space around the trigger has no side left to keep. */
    @position-try --kpop-pin {
      position-area: none;
      inset: var(--kpop-inset-top) auto auto var(--kpop-inset-inline);
      margin: 0;
      width: auto;
      max-width: calc(100vw - 2 * var(--kpop-inset-inline));
      height: auto;
    }
  }

  /* One small entrance, transform + opacity only, as the library's motion rule
   * asks; reduced motion gets none. Keyed off the open state the component
   * renders, so the fallback and the top layer animate the same way. */
  @media (prefers-reduced-motion: no-preference) {
    .kpop {
      animation: kpop-in 140ms cubic-bezier(0.32, 0.72, 0, 1);
    }
    @keyframes kpop-in {
      from { opacity: 0; transform: translateY(-4px) scale(0.98); }
    }
  }
}

/* Divider — a full-width 1px hairline with an optional label in a gap.
 *
 * Idle it is a plain rule in --kl-border with the label in --kl-text-2. Busy,
 * two 120px streaks of --kl-text at low alpha run in from the outer ends and
 * land on the label together while the label brightens to --kl-text, then
 * dims; 1.6s a pass.
 *
 * Only transform and opacity move. Each streak is a pseudo-element inside an
 * overflow-hidden half that carries a 120px gradient at its outer edge, so the
 * run is a translate of that half's own width — no layout, no
 * background-position. The bright label is drawn by CSS from a hidden copy of
 * the label's own text, so the DOM holds the label once and the pulse is an
 * opacity swap, not an animated colour. */
@layer components {
  .kdiv {
    --kdiv-pad: 12px;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    min-height: 20px;
    color: var(--kl-text-2);
    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    line-height: 1;
    letter-spacing: normal;
  }
  .kdiv-half {
    position: relative;
    flex: 1 1 0;
    min-width: 0;
    height: 1px;
    overflow: hidden;
    background: var(--kl-border);
  }
  /* The streak: a 120px gradient at the half's outer edge. A half narrower than
   * that clips it, which is what should happen on a phone. */
  .kdiv-half::after {
    content: "";
    position: absolute;
    inset: 0;
    background-image: linear-gradient(90deg, transparent, var(--kl-text-2) 50%, transparent);
    background-repeat: no-repeat;
    background-size: 120px 100%;
    opacity: 0;
  }
  .kdiv-half:first-child::after { background-position: left center; }
  .kdiv-half:last-child::after { background-position: right center; }
  /* A brighter hairline where color-mix is understood; the flat --kl-text-2
   * above is the fallback for engines that reject it. */
  @supports (background: color-mix(in srgb, red 10%, transparent)) {
    .kdiv-half::after {
      background-image: linear-gradient(90deg, transparent, color-mix(in srgb, var(--kl-text) 85%, transparent) 50%, transparent);
    }
  }
  .kdiv-label {
    position: relative;
    flex: none;
    padding: 0 var(--kdiv-pad);
    white-space: nowrap;
  }
  /* The bright copy, exactly over the ink: only its opacity changes. It is
   * generated from the copy's own data-label, so the label is one text node in
   * the DOM. The copy itself is hidden from assistive tech: a generated copy
   * is announced like any other text, and this one says what the label says. */
  .kdiv-label-copy {
    position: absolute;
    inset: 0;
    padding: 0 var(--kdiv-pad);
    color: var(--kl-text);
    pointer-events: none;
  }
  .kdiv-label-copy::after {
    content: attr(data-label);
    opacity: 0;
  }
  @media (prefers-reduced-motion: no-preference) {
    .kdiv-busy .kdiv-half:first-child::after { animation: kdiv-run-l 1600ms cubic-bezier(0.4, 0, 0.2, 1) infinite; }
    .kdiv-busy .kdiv-half:last-child::after { animation: kdiv-run-r 1600ms cubic-bezier(0.4, 0, 0.2, 1) infinite; }
    .kdiv-busy .kdiv-label-copy::after { animation: kdiv-pulse 1600ms cubic-bezier(0.4, 0, 0.2, 1) infinite; }
    /* Fade in at the outer end, run to the label's gap (the half's width minus
     * the streak), fade out; the rest of the cycle the line is a plain rule.
     * Literal keyframes rather than var()s: WebKit does not resolve a custom
     * property inside a keyframe. */
    @keyframes kdiv-run-l {
      0% { opacity: 0; transform: translateX(0); }
      12% { opacity: 1; }
      72% { opacity: 1; transform: translateX(calc(100% - 120px)); }
      84%, 100% { opacity: 0; transform: translateX(calc(100% - 120px)); }
    }
    @keyframes kdiv-run-r {
      0% { opacity: 0; transform: translateX(0); }
      12% { opacity: 1; }
      72% { opacity: 1; transform: translateX(calc(-100% + 120px)); }
      84%, 100% { opacity: 0; transform: translateX(calc(-100% + 120px)); }
    }
    /* Lit as the streaks land, held a moment, then dim. */
    @keyframes kdiv-pulse {
      0%, 58% { opacity: 0; }
      72% { opacity: 1; }
      88%, 100% { opacity: 0; }
    }
  }
}
