/* The Flipbird design system. Class names start xb- so they never meet
 * the editor's Tailwind utilities or a host's own styles.
 *
 * Nothing here needs an inline style attribute or an inline script, so
 * the stylesheet works under a Content-Security-Policy of
 * style-src 'self'. Component widths set from script (pane widths,
 * popover positions) go through the CSSOM, which CSP allows. */

@import "./tokens.css";
@import "./when.css";
@import "./charts.css";

@layer xb-base;

/* ───────────────────────────── base ───────────────────────────── */

/* Element defaults sit in a layer of their own, declared before any
 * other, so that anything layered after them wins: a host's reset, and
 * the editor's Tailwind utilities, which would otherwise lose to these
 * unlayered h2 and a rules inside the embedded editor. */
@layer xb-base {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  body {
    margin: 0;
    background: var(--xb-bg);
    color: var(--xb-text);
    font-family: var(--xb-font-sans);
    font-size: var(--xb-text-base);
    line-height: var(--xb-leading);
    -webkit-font-smoothing: antialiased;
  }

  h1, h2, h3, h4, p {
    margin: 0;
  }

  h1, h2, h3, h4 { font-family: var(--xb-font-display); letter-spacing: -0.01em; }
  h1 { font-size: var(--xb-text-2xl); font-weight: 800; line-height: 1.1; letter-spacing: -0.025em; }
  h2 { font-size: var(--xb-text-lg); font-weight: 700; line-height: 1.25; }
  h3 { font-size: var(--xb-text-base); font-weight: 700; line-height: 1.3; }
  h4 { font-size: var(--xb-text-md); font-weight: 700; }

  a {
    color: var(--xb-accent);
    text-underline-offset: 2px;
  }

  a:hover {
    color: var(--xb-accent-hover);
  }

  code, kbd, pre, .xb-mono {
    font-family: var(--xb-font-mono);
    font-size: 0.92em;
  }

  :focus-visible {
    outline: 2px solid var(--xb-focus);
    outline-offset: 2px;
  }

  ::selection {
    background: var(--xb-accent-soft);
  }
}

.xb-visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.xb-skip-link {
  position: absolute;
  left: var(--xb-space-1);
  top: -100px;
  z-index: calc(var(--xb-z-toast) + 1);
  padding: var(--xb-space-1) var(--xb-space-2);
  background: var(--xb-accent);
  color: var(--xb-accent-contrast);
  border-radius: var(--xb-radius);
  font-weight: 600;
  text-decoration: none;
}

.xb-skip-link:focus {
  top: var(--xb-space-1);
  color: var(--xb-accent-contrast);
}

/* ─────────────────────────── layout helpers ─────────────────────────── */

.xb-stack {
  display: flex;
  flex-direction: column;
  gap: var(--xb-gap, var(--xb-space-2));
  min-width: 0;
}

.xb-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--xb-gap, var(--xb-space-1));
  min-width: 0;
}

.xb-row[data-justify="between"] { justify-content: space-between; }
.xb-row[data-justify="end"] { justify-content: flex-end; }
.xb-gap-half { --xb-gap: var(--xb-space-half); }
.xb-gap-1 { --xb-gap: var(--xb-space-1); }
.xb-gap-2 { --xb-gap: var(--xb-space-2); }
.xb-gap-3 { --xb-gap: var(--xb-space-3); }
.xb-grow { flex: 1 1 auto; min-width: 0; }
.xb-muted { color: var(--xb-text-muted); }
.xb-small { font-size: var(--xb-text-sm); }
.xb-truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

.xb-page {
  display: flex;
  flex-direction: column;
  gap: var(--xb-space-3);
  width: 100%;
  max-width: var(--xb-content-w);
  margin-inline: auto;
  padding: var(--xb-space-4) var(--xb-space-4) var(--xb-space-8);
  min-width: 0;
}

/* A working surface (a table that fills the screen, the editor) takes
 * the full width instead of the reading column. */
.xb-page[data-wide] { max-width: none; }

@media (max-width: 899px) {
  .xb-page { padding: var(--xb-space-3) var(--xb-space-3) var(--xb-space-6); }
}

@media (max-width: 599px) {
  .xb-page {
    padding: var(--xb-space-2) var(--xb-space-2) var(--xb-space-5);
    gap: var(--xb-space-2);
  }
}

@layer xb-base {
  @media (max-width: 599px) {
    h1 { font-size: var(--xb-text-xl); }
  }
}

.xb-page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--xb-space-2);
}

.xb-page-header p {
  color: var(--xb-text-muted);
  margin-top: var(--xb-space-half);
  font-size: var(--xb-text-base);
  max-width: 68ch;
}

/* A page's own title is the display size: it says where you are. */
.xb-page-header h1 {
  font-size: clamp(var(--xb-text-xl), 1.6vw + 14px, var(--xb-text-3xl));
  letter-spacing: -0.03em;
  overflow-wrap: anywhere;
}

/* A thumbnail or illustration leads the title on a wide screen. */
.xb-page-header-lead { display: flex; align-items: center; gap: var(--xb-space-2); min-width: 0; flex: 1 1 320px; }
.xb-page-header-lead > .xb-stack { flex: 1 1 auto; min-width: 0; }
.xb-page-header-art { flex: none; }

@media (max-width: 599px) {
  .xb-page-header-art { display: none; }
}

/* On a phone the header's actions take a row of their own, full width,
 * so the primary action is a large target under the title. */
@media (max-width: 599px) {
  .xb-page-header > .xb-row { width: 100%; }
  .xb-page-header > .xb-row > .xb-btn { flex: 1 1 auto; }
}

.xb-eyebrow {
  color: var(--xb-accent);
  font-size: var(--xb-text-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.xb-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
  gap: var(--xb-space-3);
}

@media (max-width: 599px) {
  .xb-grid { gap: var(--xb-space-2); }
}

/* ───────────────────────── form layout ───────────────────────── */

/* A settings form reads as sections: what the section is for on the
 * left, its fields on the right. Under 900px the intro sits on top. */
.xb-form-section {
  display: grid;
  grid-template-columns: minmax(200px, 300px) minmax(0, 1fr);
  gap: var(--xb-space-2) var(--xb-space-5);
  padding-block: var(--xb-space-4);
  border-top: 1px solid var(--xb-border);
  min-width: 0;
}

.xb-form-section:first-child { border-top: 0; padding-top: 0; }
.xb-form-section-intro { display: flex; flex-direction: column; gap: var(--xb-space-half); min-width: 0; }
.xb-form-section-intro h2 { font-size: var(--xb-text-lg); }
.xb-form-section-intro p { color: var(--xb-text-muted); font-size: var(--xb-text-md); max-width: 44ch; }
.xb-form-section-body { display: flex; flex-direction: column; gap: var(--xb-space-3); min-width: 0; }

@media (max-width: 899px) {
  .xb-form-section { grid-template-columns: minmax(0, 1fr); padding-block: var(--xb-space-3); }
}

/* Fields side by side where there is room, one column on a phone.
 * data-span="full" keeps a field (a textarea, a long URL) on its own row. */
.xb-field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
  gap: var(--xb-space-2) var(--xb-space-3);
  align-items: start;
  min-width: 0;
}

.xb-field-grid[data-cols="2"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.xb-field-grid[data-cols="3"] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.xb-field-grid > [data-span="full"] { grid-column: 1 / -1; }

@media (max-width: 599px) {
  .xb-field-grid[data-cols] { grid-template-columns: minmax(0, 1fr); }
}

/* The save row of a long form. On a phone it sticks to the bottom of
 * the screen, so Save is always a thumb away. */
.xb-form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--xb-space-1);
  padding-top: var(--xb-space-2);
}

.xb-form-actions > .xb-form-actions-note { margin-right: auto; color: var(--xb-text-muted); font-size: var(--xb-text-sm); }

@media (max-width: 599px) {
  .xb-form-actions[data-sticky] {
    /* A floating bar inside whatever holds the form, so it never spills
     * past a card's padding. */
    position: sticky;
    bottom: calc(var(--xb-space-1) + env(safe-area-inset-bottom));
    z-index: var(--xb-z-sticky);
    margin-top: var(--xb-space-2);
    padding: var(--xb-space-1);
    border: 1px solid var(--xb-border);
    border-radius: var(--xb-radius-xl);
    background: var(--xb-surface);
    box-shadow: var(--xb-shadow);
  }
  .xb-form-actions[data-sticky] > .xb-btn { flex: 1 1 auto; }
}

/* A row of key figures above a page's detail. */
.xb-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr));
  gap: var(--xb-space-2);
}

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

.xb-stats { margin: 0; }
.xb-stat dd { margin: 0; }
.xb-stat-label { display: flex; align-items: center; gap: var(--xb-space-1); color: var(--xb-text-muted); font-size: var(--xb-text-sm); font-weight: 600; min-width: 0; }
.xb-stat-icon {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 28px;
  height: 28px;
  border-radius: var(--xb-radius-pill);
  background: var(--xb-accent-soft);
  color: var(--xb-accent);
}
.xb-stat-icon > .xb-icon { width: 16px; height: 16px; }
.xb-stat-value { font-family: var(--xb-font-display); font-size: var(--xb-text-2xl); font-weight: 800; letter-spacing: -0.02em; line-height: 1.1; font-variant-numeric: tabular-nums; }
.xb-stat-note { color: var(--xb-text-muted); font-size: var(--xb-text-sm); }

@media (max-width: 599px) {
  .xb-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--xb-space-1); }
  .xb-stat { padding: var(--xb-space-1) var(--xb-space-2); }
  .xb-stat-value { font-size: var(--xb-text-xl); }
}

/* ───────────────────────────── brand ───────────────────────────── */

.xb-logo {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--xb-text);
  text-decoration: none;
}

.xb-logo-mark { flex: none; width: var(--xb-logo-size, 32px); height: var(--xb-logo-size, 32px); }
.xb-logo[data-size="sm"] { --xb-logo-size: 24px; gap: 8px; }
.xb-logo[data-size="lg"] { --xb-logo-size: 48px; gap: 12px; }
.xb-logo-mark .crest-back { fill: var(--xb-brand-ribbon-sh); }
.xb-logo-mark .crest-front { fill: var(--xb-brand-ribbon); }
.xb-logo-mark .head { fill: var(--xb-brand-body); }
.xb-logo-mark .eye-ring { fill: var(--xb-brand-down); }
.xb-logo-mark .eye { fill: var(--xb-brand-ink); }
.xb-logo-mark .bill-lower { fill: var(--xb-brand-bill-sh); }
.xb-logo-mark .bill-upper { fill: var(--xb-brand-bill); }
.xb-logo-mark .nostril { fill: var(--xb-brand-nostril); }

.xb-logo-word {
  font-family: var(--xb-font-display);
  font-size: calc(var(--xb-logo-size, 32px) * 0.68);
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1;
  white-space: nowrap;
}

.xb-logo-word > span { color: var(--xb-accent); }

.xb-mascot { display: block; flex: none; }
.xb-mascot img { display: block; width: 100%; height: auto; }

/* MascotFlight: Flip crosses side-on, turns into the page at 1 s (58%
 * of the 1.7 s approach) and flies away towards the perspective origin. Perspective and depth are
 * in px, so the shrink is the same at any size: scale = 420 / (420 +
 * depth), with depth = 1680 × (1 − e^(−t / 3 s)) — half his size in a
 * second while he is near, a fifth after a few, and no smaller. The two
 * views share a frame, and cross-fade mid-turn. */
.xb-flight {
  position: relative;
  flex: none;
  perspective: 420px;
  perspective-origin: 50% 40%;
  pointer-events: none;
}

.xb-flight-away,
.xb-flight-in { transform-style: preserve-3d; }
.xb-flight-side,
.xb-flight-rear { transform-origin: 50% 56%; }
.xb-flight-rear { position: absolute; inset: 0; display: none; }

@media (prefers-reduced-motion: no-preference) {
  .xb-flight { animation: xb-fade 250ms ease-out both; }
  .xb-flight-in { animation: xb-flight-in 1.7s linear both; }
  .xb-flight-side { animation: xb-flight-side 1.7s linear both; }
  .xb-flight-rear { display: block; animation: xb-flight-rear 1.7s linear both; }
  .xb-flight-away { animation: xb-flight-away 40s linear 0.99s both; }
}

@keyframes xb-flight-in {
  from { transform: translate(-85%, 10%); animation-timing-function: cubic-bezier(0.3, 0.4, 0.6, 1); }
  58% { transform: translate(0, -2%); animation-timing-function: ease-out; }
  to { transform: translate(8%, -6%); }
}

@keyframes xb-flight-side {
  58% { transform: none; animation-timing-function: ease-in; }
  73% { opacity: 1; }
  79%, to { transform: rotateY(64deg) rotateX(22deg) rotateZ(-6deg); opacity: 0; }
}

@keyframes xb-flight-rear {
  from, 73% { transform: rotateY(-40deg) rotateZ(-18deg); opacity: 0; animation-timing-function: ease-out; }
  79% { opacity: 1; }
  to { transform: none; opacity: 1; }
}

@keyframes xb-flight-away {
  1.25% { transform: translateZ(-258px); }
  2.5% { transform: translateZ(-476px); }
  3.75% { transform: translateZ(-661px); }
  5% { transform: translateZ(-817px); }
  7.5% { transform: translateZ(-1062px); }
  10% { transform: translateZ(-1237px); }
  15% { transform: translateZ(-1453px); }
  20% { transform: translateZ(-1563px); }
  30% { transform: translateZ(-1649px); }
  to { transform: translateZ(-1680px); }
}

/* ────────────────────────────── card ────────────────────────────── */

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

.xb-card-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--xb-space-1);
  min-height: 56px;
  padding: var(--xb-space-2) var(--xb-space-3) 0;
}

.xb-card-header h2,
.xb-card-header h3 {
  font-size: var(--xb-text-lg);
}

.xb-card-title { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1 1 16rem; }
.xb-card-title p { color: var(--xb-text-muted); font-size: var(--xb-text-md); max-width: 64ch; }

.xb-card-body {
  padding: var(--xb-space-2) var(--xb-space-3) var(--xb-space-3);
}

/* A flush body (a table, a list) runs to the card's edges, under a rule. */
.xb-card-header + .xb-card-body[data-flush] { margin-top: var(--xb-space-2); border-top: 1px solid var(--xb-border); }
.xb-card-body[data-flush] { overflow: hidden; border-radius: 0 0 var(--xb-radius-xl) var(--xb-radius-xl); }
.xb-card-body[data-flush] > .xb-table-wrap { border: 0; border-radius: 0; box-shadow: none; }

@media (max-width: 599px) {
  .xb-card { border-radius: var(--xb-radius-lg); }
  .xb-card-header { padding: var(--xb-space-2) var(--xb-space-2) 0; }
  .xb-card-body { padding: var(--xb-space-2); }
  .xb-card-body[data-flush] { border-radius: 0 0 var(--xb-radius-lg) var(--xb-radius-lg); }
}

.xb-card-body[data-flush] {
  padding: 0;
}

/* ───────────────────────────── buttons ───────────────────────────── */

.xb-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--xb-space-1);
  height: var(--xb-control-h);
  padding: 0 18px;
  border: 1px solid var(--xb-border-strong);
  border-radius: var(--xb-radius-pill);
  background: var(--xb-surface);
  color: var(--xb-text);
  font: inherit;
  font-size: var(--xb-text-md);
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
  text-decoration: none;
  cursor: pointer;
  transition: background-color var(--xb-duration) var(--xb-ease), border-color var(--xb-duration) var(--xb-ease), box-shadow var(--xb-duration) var(--xb-ease), transform var(--xb-duration) var(--xb-ease);
}

.xb-btn:hover { background: var(--xb-surface-hover); color: var(--xb-text); }
.xb-btn:active:not(:disabled) { transform: translateY(1px); }
.xb-btn:disabled,
.xb-btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }

.xb-btn[data-variant="primary"] {
  background: var(--xb-accent);
  border-color: var(--xb-accent);
  color: var(--xb-accent-contrast);
  box-shadow: var(--xb-shadow-sm);
}
.xb-btn[data-variant="primary"]:hover:not(:disabled) { background: var(--xb-accent-hover); border-color: var(--xb-accent-hover); color: var(--xb-accent-contrast); }

.xb-btn[data-variant="danger"] {
  background: var(--xb-danger);
  border-color: var(--xb-danger);
  color: var(--xb-danger-contrast);
}
.xb-btn[data-variant="danger"]:hover:not(:disabled) { filter: brightness(1.08); }

.xb-btn[data-variant="ghost"] {
  background: transparent;
  border-color: transparent;
}
.xb-btn[data-variant="ghost"]:hover:not(:disabled) { background: var(--xb-surface-hover); }

.xb-btn[data-size="sm"] {
  height: var(--xb-control-h-sm);
  padding: 0 12px;
  font-size: var(--xb-text-sm);
}

.xb-btn[data-icon-only] {
  width: var(--xb-control-h);
  padding: 0;
}
.xb-btn[data-icon-only][data-size="sm"] { width: var(--xb-control-h-sm); }

.xb-btn[data-block] { width: 100%; }

.xb-icon {
  flex: none;
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ────────────────────────────── fields ────────────────────────────── */

.xb-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.xb-label {
  font-size: var(--xb-text-md);
  font-weight: 600;
}

.xb-hint {
  color: var(--xb-text-muted);
  font-size: var(--xb-text-sm);
}

.xb-error {
  color: var(--xb-danger);
  font-size: var(--xb-text-sm);
}

.xb-input,
.xb-select,
.xb-textarea {
  width: 100%;
  min-width: 0;
  height: calc(var(--xb-control-h) + 4px);
  padding: 0 12px;
  border: 1px solid var(--xb-control-border);
  border-radius: var(--xb-radius);
  background: var(--xb-surface);
  color: var(--xb-text);
  font: inherit;
  font-size: var(--xb-text-base);
  transition: border-color var(--xb-duration) var(--xb-ease), box-shadow var(--xb-duration) var(--xb-ease);
}

/* 16px text in a field stops iOS zooming the page when it takes focus. */
@media (pointer: coarse) {
  .xb-input, .xb-select, .xb-textarea { font-size: 16px; }
}

.xb-input:hover:not(:disabled, :focus),
.xb-select:hover:not(:disabled, :focus),
.xb-textarea:hover:not(:disabled, :focus) { border-color: var(--xb-text-muted); }

.xb-input:disabled,
.xb-select:disabled,
.xb-textarea:disabled { background: var(--xb-surface-2); color: var(--xb-text-muted); cursor: not-allowed; }

.xb-textarea {
  height: auto;
  min-height: calc(var(--xb-control-h) * 3);
  padding: 10px 12px;
  resize: vertical;
}

/* The native arrow is painted on the border edge, and padding does not
 * move it. Drop it and draw our own, one step in from the right. */
.xb-select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: calc(var(--xb-space-1) * 4);
}

.xb-select-wrap {
  position: relative;
  display: block;
  min-width: 0;
}

.xb-select-wrap > .xb-icon {
  position: absolute;
  top: 50%;
  right: 12px;
  transform: translateY(-50%);
  pointer-events: none;
  color: var(--xb-text-muted);
}

.xb-select-wrap:has(select:disabled) > .xb-icon { opacity: 0.45; }

.xb-input::placeholder,
.xb-textarea::placeholder { color: var(--xb-text-muted); opacity: 0.8; }

.xb-input:focus-visible,
.xb-select:focus-visible,
.xb-textarea:focus-visible {
  outline: 2px solid var(--xb-focus);
  outline-offset: 1px;
  border-color: var(--xb-focus);
}

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

.xb-input[data-code] {
  font-family: var(--xb-font-mono);
  letter-spacing: 0.3em;
  font-size: var(--xb-text-lg);
}

.xb-check {
  display: inline-flex;
  align-items: flex-start;
  gap: var(--xb-space-1);
  font-size: var(--xb-text-md);
  cursor: pointer;
}

.xb-check input {
  width: 16px;
  height: 16px;
  margin: 2px 0 0;
  accent-color: var(--xb-accent);
}

.xb-switch {
  display: inline-flex;
  align-items: center;
  gap: var(--xb-space-1);
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  font-size: var(--xb-text-md);
  cursor: pointer;
}

.xb-switch-track {
  position: relative;
  flex: none;
  width: 36px;
  height: 20px;
  border-radius: 10px;
  background: var(--xb-control-border);
  transition: background-color var(--xb-duration) var(--xb-ease);
}

.xb-switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--xb-surface);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
  transition: transform var(--xb-duration) var(--xb-ease);
}

.xb-switch[aria-checked="true"] .xb-switch-track { background: var(--xb-accent); }
.xb-switch[aria-checked="true"] .xb-switch-track::after { transform: translateX(16px); background: var(--xb-accent-contrast); }
.xb-switch:disabled { opacity: 0.55; cursor: not-allowed; }

/* A finger gets a 40px row to tap for a checkbox, radio or switch, and a
 * bigger box and track to aim at. */
@media (pointer: coarse) {
  .xb-check {
    min-height: var(--xb-control-h);
    padding: 10px 0;
  }
  .xb-check input {
    width: 20px;
    height: 20px;
    margin: 0;
  }
  .xb-switch { min-height: var(--xb-control-h); }
  /* Each row is a 40px target already; the fieldset's gap would only
   * spread a list of them out. */
  .xb-fieldset > .xb-check + .xb-check { margin-top: calc(-1 * var(--xb-space-1)); }
  .xb-switch-track { width: 44px; height: 26px; border-radius: 13px; }
  .xb-switch-track::after { width: 22px; height: 22px; }
  .xb-switch[aria-checked="true"] .xb-switch-track::after { transform: translateX(18px); }
}

fieldset.xb-fieldset {
  display: flex;
  flex-direction: column;
  gap: var(--xb-space-1);
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

fieldset.xb-fieldset legend {
  padding: 0;
  margin-bottom: var(--xb-space-half);
  font-size: var(--xb-text-md);
  font-weight: 600;
}

.xb-form {
  display: flex;
  flex-direction: column;
  gap: var(--xb-space-3);
}

/* ─────────────────────────── feedback ─────────────────────────── */

.xb-banner {
  display: flex;
  align-items: flex-start;
  gap: var(--xb-space-1);
  padding: 12px var(--xb-space-2);
  border: 1px solid var(--xb-border);
  border-left-width: 4px;
  border-radius: var(--xb-radius-lg);
  background: var(--xb-surface);
  font-size: var(--xb-text-md);
}

.xb-banner .xb-icon { margin-top: 2px; }
.xb-banner-title { font-weight: 600; }
.xb-banner[data-tone="info"] { border-left-color: var(--xb-info); background: var(--xb-info-soft); }
.xb-banner[data-tone="info"] > .xb-icon { color: var(--xb-info); }
.xb-banner[data-tone="success"] { border-left-color: var(--xb-success); background: var(--xb-success-soft); }
.xb-banner[data-tone="success"] > .xb-icon { color: var(--xb-success); }
.xb-banner[data-tone="warning"] { border-left-color: var(--xb-warning); background: var(--xb-warning-soft); }
.xb-banner[data-tone="warning"] > .xb-icon { color: var(--xb-warning); }
.xb-banner[data-tone="danger"] { border-left-color: var(--xb-danger); background: var(--xb-danger-soft); }
.xb-banner[data-tone="danger"] > .xb-icon { color: var(--xb-danger); }

.xb-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--xb-space-half);
  height: 22px;
  padding: 0 9px;
  border: 1px solid transparent;
  border-radius: var(--xb-radius-pill);
  background: var(--xb-surface-2);
  color: var(--xb-text-muted);
  font-size: var(--xb-text-xs);
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.xb-badge[data-tone="accent"] { color: var(--xb-accent); background: var(--xb-accent-soft); }
.xb-badge[data-tone="success"] { color: var(--xb-success); background: var(--xb-success-soft); }
.xb-badge[data-tone="warning"] { color: var(--xb-warning); background: var(--xb-warning-soft); }
.xb-badge[data-tone="danger"] { color: var(--xb-danger); background: var(--xb-danger-soft); }

.xb-kbd {
  display: inline-block;
  min-width: 20px;
  padding: 0 5px;
  border: 1px solid var(--xb-border-strong);
  border-bottom-width: 2px;
  border-radius: var(--xb-radius-sm);
  background: var(--xb-surface-2);
  color: var(--xb-text);
  font-family: var(--xb-font-mono);
  font-size: var(--xb-text-xs);
  line-height: 18px;
  text-align: center;
}

.xb-spinner {
  display: inline-flex;
  align-items: center;
  gap: var(--xb-space-1);
  color: var(--xb-text-muted);
  font-size: var(--xb-text-md);
}

.xb-spinner::before {
  content: "";
  width: 14px;
  height: 14px;
  border: 2px solid var(--xb-border-strong);
  border-top-color: var(--xb-accent);
  border-radius: 50%;
  animation: xb-spin 700ms linear infinite;
}

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

@media (prefers-reduced-motion: reduce) {
  .xb-spinner::before { animation: none; border-top-color: var(--xb-border-strong); border-right-color: var(--xb-accent); }
}

.xb-skeleton {
  display: block;
  height: 12px;
  border-radius: var(--xb-radius-sm);
  background: linear-gradient(90deg, var(--xb-surface-hover), var(--xb-surface-2), var(--xb-surface-hover));
  background-size: 200% 100%;
  animation: xb-shimmer 1.4s linear infinite;
}

@keyframes xb-shimmer { to { background-position: -200% 0; } }

@media (prefers-reduced-motion: reduce) {
  .xb-skeleton { animation: none; }
}

/* An empty state wraps its words whatever it sits in: a DataTable's empty
 * row is a cell, and table cells do not wrap (.xb-table td), which left
 * the sentence on one line running out of its centred box. */
.xb-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--xb-space-1);
  padding: var(--xb-space-5) var(--xb-space-2);
  text-align: center;
  white-space: normal;
  color: var(--xb-text-muted);
  font-size: var(--xb-text-md);
}

.xb-empty > .xb-icon {
  width: 44px;
  height: 44px;
  padding: 10px;
  margin-bottom: var(--xb-space-half);
  border-radius: 50%;
  background: var(--xb-accent-soft);
  color: var(--xb-accent);
}
.xb-empty > .xb-mascot { margin-bottom: var(--xb-space-1); }
.xb-empty h1,
.xb-empty h2,
.xb-empty h3 { color: var(--xb-text); font-size: var(--xb-text-lg); }
.xb-empty p { max-width: 44ch; }
.xb-empty > .xb-btn, .xb-empty > .xb-row { margin-top: var(--xb-space-1); }

.xb-empty[data-kind="unavailable"] {
  border: 1px dashed var(--xb-border-strong);
  border-radius: var(--xb-radius-lg);
}

.xb-progress {
  width: 100%;
  height: 6px;
  appearance: none;
  border: 0;
  border-radius: 3px;
  background: var(--xb-surface-hover);
  overflow: hidden;
}

.xb-progress::-webkit-progress-bar { background: var(--xb-surface-hover); }
.xb-progress::-webkit-progress-value { background: var(--xb-accent); border-radius: 3px; }
.xb-progress::-moz-progress-bar { background: var(--xb-accent); }

/* ────────────────────────────── toasts ────────────────────────────── */

.xb-toasts {
  position: fixed;
  right: var(--xb-space-2);
  bottom: var(--xb-space-2);
  z-index: var(--xb-z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--xb-space-1);
  width: min(360px, calc(100vw - var(--xb-space-4)));
  margin: 0;
  padding: 0;
  list-style: none;
  pointer-events: none;
}

.xb-toast {
  display: flex;
  align-items: flex-start;
  gap: var(--xb-space-1);
  padding: var(--xb-space-1) var(--xb-space-1) var(--xb-space-1) var(--xb-space-2);
  border: 1px solid var(--xb-border);
  border-radius: var(--xb-radius-lg);
  background: var(--xb-surface);
  box-shadow: var(--xb-shadow);
  font-size: var(--xb-text-md);
  pointer-events: auto;
  animation: xb-rise var(--xb-duration) var(--xb-ease);
}

.xb-toast[data-tone="danger"] { border-left: 4px solid var(--xb-danger); }
.xb-toast[data-tone="success"] { border-left: 4px solid var(--xb-success); }

/* On a phone toasts sit at the bottom edge, full width, above the home bar. */
@media (max-width: 599px) {
  .xb-toasts { right: var(--xb-space-1); left: var(--xb-space-1); bottom: calc(var(--xb-space-1) + env(safe-area-inset-bottom)); width: auto; }
}
.xb-toast-body { flex: 1; min-width: 0; padding-top: 5px; overflow-wrap: anywhere; }

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

/* ─────────────── modal surfaces: dialog, drawer, overlay ─────────────── */

/* All three are a native <dialog> shown with showModal(): the browser
 * puts it in the top layer, makes the rest of the page inert, traps
 * Tab inside it and closes it on Escape. */

dialog.xb-dialog,
dialog.xb-drawer,
dialog.xb-overlay {
  padding: 0;
  border: 1px solid var(--xb-border);
  background: var(--xb-surface);
  color: var(--xb-text);
  box-shadow: var(--xb-shadow);
  max-width: 100vw;
  max-height: 100dvh;
}

dialog.xb-dialog::backdrop,
dialog.xb-drawer::backdrop,
dialog.xb-overlay::backdrop {
  background: var(--xb-backdrop);
}

dialog.xb-dialog[open],
dialog.xb-drawer[open],
dialog.xb-overlay[open] {
  display: flex;
  flex-direction: column;
}

.xb-surface-header {
  display: flex;
  align-items: center;
  gap: var(--xb-space-1);
  min-height: 60px;
  padding: var(--xb-space-1) var(--xb-space-2) var(--xb-space-1) var(--xb-space-3);
  border-bottom: 1px solid var(--xb-border);
}

.xb-surface-header h2 {
  flex: 1;
  min-width: 0;
  font-size: var(--xb-text-xl);
  overflow-wrap: anywhere;
}

.xb-surface-form {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

.xb-surface-body {
  flex: 1 1 auto;
  min-height: 0;
  padding: var(--xb-space-3);
  overflow: auto;
  overscroll-behavior: contain;
}

.xb-surface-body[data-flush] { padding: 0; }
.xb-surface-body > p:first-child:not(:only-child) { margin-bottom: var(--xb-space-2); }

.xb-surface-footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--xb-space-1);
  padding: 12px var(--xb-space-3) calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--xb-border);
  background: var(--xb-surface);
}

@media (max-width: 599px) {
  .xb-surface-header { padding-left: var(--xb-space-2); }
  .xb-surface-body { padding: var(--xb-space-2); }
  .xb-surface-footer { padding-inline: var(--xb-space-2); }
  .xb-surface-footer > .xb-btn { flex: 1 1 auto; }
}

dialog.xb-dialog {
  width: min(480px, calc(100vw - var(--xb-space-4)));
  margin: auto;
  border-radius: var(--xb-radius-xl);
}

/* A dialog on a phone is a bottom sheet: thumb-reachable, full width. */
@media (max-width: 599px) {
  dialog.xb-dialog {
    width: 100vw;
    margin: auto 0 0;
    border-radius: var(--xb-radius-xl) var(--xb-radius-xl) 0 0;
    border-width: 1px 0 0;
  }
}

dialog.xb-dialog .xb-surface-body p + p { margin-top: var(--xb-space-1); }

dialog.xb-drawer {
  position: fixed;
  inset: 0 0 0 auto;
  width: min(var(--xb-drawer-w), 100vw);
  height: 100dvh;
  margin: 0;
  border-width: 0 0 0 1px;
}

dialog.xb-drawer[data-side="left"] {
  inset: 0 auto 0 0;
  width: min(300px, 86vw);
  border-width: 0 1px 0 0;
  border-radius: 0 var(--xb-radius-xl) var(--xb-radius-xl) 0;
}

dialog.xb-overlay {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  margin: 0;
  border: 0;
}

/* A form drawer takes the whole screen on a phone. */
@media (max-width: 599px) {
  dialog.xb-drawer:not([data-side="left"]) {
    width: 100vw;
    border-width: 0;
  }
}

@media (prefers-reduced-motion: no-preference) {
  dialog.xb-dialog[open] { animation: xb-pop var(--xb-duration) var(--xb-ease); }
  dialog.xb-drawer[open] { animation: xb-slide-in var(--xb-duration) var(--xb-ease); }
  dialog.xb-drawer[data-side="left"][open] { animation-name: xb-slide-in-left; }
  dialog.xb-overlay[open] { animation: xb-fade var(--xb-duration) var(--xb-ease); }
}

@keyframes xb-pop { from { opacity: 0; transform: scale(0.97); } }
@keyframes xb-fade { from { opacity: 0; } }
@keyframes xb-slide-in { from { transform: translateX(24px); opacity: 0; } }
@keyframes xb-slide-in-left { from { transform: translateX(-24px); opacity: 0; } }

/* ───────────────────────── popover and menu ───────────────────────── */

.xb-popover {
  position: fixed;
  inset: auto;
  margin: 0;
  padding: var(--xb-space-half);
  min-width: 160px;
  max-width: calc(100vw - var(--xb-space-2));
  overflow: auto;
  border: 1px solid var(--xb-border);
  border-radius: var(--xb-radius-lg);
  background: var(--xb-surface);
  color: var(--xb-text);
  box-shadow: var(--xb-shadow);
  font-size: var(--xb-text-md);
}

.xb-popover[data-padded] { padding: var(--xb-space-2); }

/* Hidden until placed, so it never flashes at the corner of the page. */
.xb-popover:popover-open:not([data-placement]) { visibility: hidden; }

/* An info tip: a quiet (i), and a few sentences in its popover. */
.xb-info-tip { color: var(--xb-text-muted); }
.xb-info-tip:hover:not(:disabled), .xb-info-tip[aria-expanded="true"] { color: var(--xb-text); }
.xb-info-tip-body { max-width: 22rem; font-size: var(--xb-text-sm); line-height: var(--xb-leading); }
.xb-info-tip-body:focus { outline: none; }
.xb-info-tip-body > * { margin: 0; }
.xb-info-tip-body > * + * { margin-top: var(--xb-space-1); }
.xb-info-tip-body ul { padding-left: 1.1rem; display: grid; gap: 6px; }

.xb-menu {
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
  list-style: none;
}

.xb-menu-item {
  display: flex;
  align-items: center;
  gap: var(--xb-space-1);
  width: 100%;
  min-height: var(--xb-control-h);
  padding: 0 10px;
  border: 0;
  border-radius: var(--xb-radius);
  background: none;
  color: var(--xb-text);
  font: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.xb-menu-item:hover,
.xb-menu-item:focus-visible {
  background: var(--xb-surface-hover);
  outline: none;
  color: var(--xb-text);
}

.xb-menu-item:focus-visible { box-shadow: inset 0 0 0 2px var(--xb-focus); }
.xb-menu-item[data-danger] { color: var(--xb-danger); }
.xb-menu-item[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }
.xb-menu-sep { height: 1px; margin: var(--xb-space-half) 0; background: var(--xb-border); }
.xb-menu-label { padding: var(--xb-space-half) var(--xb-space-1); color: var(--xb-text-muted); font-size: var(--xb-text-xs); }

/* ────────────────────────────── tabs ────────────────────────────── */

.xb-tabs {
  display: flex;
  gap: var(--xb-space-half);
  margin: 0;
  padding: 0;
  border-bottom: 1px solid var(--xb-border);
  /* Sideways only. overflow-x: auto would otherwise compute overflow-y
   * to auto as well, and the tab's border plus its -1px margin then
   * draws a vertical scrollbar. */
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  list-style: none;
}

.xb-tab {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: var(--xb-space-half);
  height: max(44px, var(--xb-control-h));
  padding: 0 12px;
  margin-bottom: -1px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: none;
  color: var(--xb-text-muted);
  font: inherit;
  font-size: var(--xb-text-md);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}

/* A strip with more tabs past an edge fades out there (Tabs.tsx marks
 * it), so it plainly scrolls. */
.xb-tabs[data-more] {
  /* The fade, plus an unfaded 1px strip so the bottom border stays whole. */
  mask-image: var(--xb-tabs-fade), linear-gradient(#000, #000);
  mask-size: 100% 100%, 100% 1px;
  mask-position: 0 0, 0 100%;
  mask-repeat: no-repeat;
}
.xb-tabs[data-more="end"] { --xb-tabs-fade: linear-gradient(to right, #000 calc(100% - 32px), transparent); }
.xb-tabs[data-more="start"] { --xb-tabs-fade: linear-gradient(to left, #000 calc(100% - 32px), transparent); }
.xb-tabs[data-more="both"] { --xb-tabs-fade: linear-gradient(to right, transparent, #000 32px, #000 calc(100% - 32px), transparent); }

.xb-tab:hover { color: var(--xb-text); }
.xb-tab:focus-visible { outline-offset: -2px; }

.xb-tab[aria-selected="true"],
.xb-tab[aria-current="page"] {
  color: var(--xb-text);
  border-bottom-color: var(--xb-accent);
}

.xb-tabs[data-size="sm"] .xb-tab {
  height: var(--xb-control-h-sm);
  padding: 0 10px;
  font-size: var(--xb-text-sm);
  border-bottom-width: 2px;
}

.xb-tab .xb-badge { height: 16px; font-size: 10px; }

/* ───────────────────────────── data table ───────────────────────────── */

.xb-table-wrap {
  position: relative;
  container-type: inline-size;
  overflow: auto;
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--xb-border);
  border-radius: var(--xb-radius-lg);
  background: var(--xb-surface);
  box-shadow: var(--xb-shadow-sm);
}

.xb-table-wrap[data-fill] {
  flex: 1 1 auto;
}

.xb-table-wrap:not([data-fill]) {
  max-height: min(70vh, 720px);
}

.xb-stack[data-fill] { min-height: 0; flex: 1 1 auto; }

.xb-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  font-size: var(--xb-text-md);
  font-variant-numeric: tabular-nums;
}

.xb-table td[data-primary] { font-weight: 600; }

.xb-table th,
.xb-table td {
  height: var(--xb-row-h);
  padding: 0 var(--xb-space-1);
  border-bottom: 1px solid var(--xb-border);
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.xb-table th:first-child,
.xb-table td:first-child { padding-left: var(--xb-space-2); }
.xb-table th:last-child,
.xb-table td:last-child { padding-right: var(--xb-space-2); }

.xb-table thead th {
  position: sticky;
  top: 0;
  z-index: var(--xb-z-sticky);
  background: var(--xb-surface-2);
  color: var(--xb-text-muted);
  font-size: var(--xb-text-xs);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.xb-table th[data-align="end"],
.xb-table td[data-align="end"] { text-align: right; }

.xb-table th[data-select],
.xb-table td[data-select] {
  width: 40px;
  padding-right: 0;
}

.xb-table td[data-select] input,
.xb-table th[data-select] input {
  width: 16px;
  height: 16px;
  margin: 0;
  vertical-align: middle;
  accent-color: var(--xb-accent);
}

.xb-sort {
  display: inline-flex;
  align-items: center;
  gap: var(--xb-space-half);
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

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

.xb-table tbody tr[data-row] { cursor: default; }
.xb-table tbody tr[data-row][data-openable] { cursor: pointer; }
.xb-table tbody tr[data-row]:hover td { background: var(--xb-surface-hover); }
.xb-table tbody tr[aria-selected="true"] td { background: var(--xb-selected); }
.xb-table tbody tr[data-row]:focus-visible { outline: none; }
.xb-table tbody tr[data-row]:focus-visible td { box-shadow: inset 0 2px 0 -1px var(--xb-focus), inset 0 -2px 0 -1px var(--xb-focus); }
.xb-table tbody tr[data-row]:focus-visible td:first-child { box-shadow: inset 2px 2px 0 -1px var(--xb-focus), inset 0 -2px 0 -1px var(--xb-focus); }
.xb-table tbody tr[data-spacer] td { height: auto; padding: 0; border: 0; }
.xb-table tbody tr:last-child td { border-bottom: 0; }

/* Lower-priority columns give way as the table's own box narrows, so a
 * table in a narrow pane behaves like one on a phone. */
@container (max-width: 639px) {
  .xb-table [data-priority="2"],
  .xb-table [data-priority="3"] { display: none; }
}

@container (max-width: 959px) {
  .xb-table [data-priority="3"] { display: none; }
}

/* The checkbox takes taps across its whole cell, not only its box. */
.xb-select-hit {
  display: flex;
  align-items: center;
  height: 100%;
  cursor: pointer;
}

@media (pointer: coarse) {
  .xb-table th[data-select],
  .xb-table td[data-select] {
    width: 56px;
    padding: 0 0 0 var(--xb-space-1);
  }
  .xb-select-hit {
    justify-content: center;
    width: 40px;
    min-height: 40px;
  }
  .xb-table td[data-select] input,
  .xb-table th[data-select] input {
    width: 20px;
    height: 20px;
  }
  .xb-sort {
    min-width: 40px;
    min-height: 40px;
  }
}

/* Stacked: a narrow table (DataTable's stackBelow) lays each row out as
 * a card, every column under its header, so a phone hides nothing.
 * Only the select-all box and the sort buttons stay in the header. */
.xb-table-wrap[data-layout="stack"] {
  overflow-x: hidden;
}

.xb-table-wrap[data-layout="stack"]:not([data-fill]) {
  max-height: none;
  overflow: visible;
}

.xb-table-wrap[data-layout="stack"] .xb-table,
.xb-table-wrap[data-layout="stack"] .xb-table thead,
.xb-table-wrap[data-layout="stack"] .xb-table tbody {
  display: block;
}

.xb-table-wrap[data-layout="stack"] .xb-table thead {
  position: sticky;
  top: 0;
  z-index: var(--xb-z-sticky);
  background: var(--xb-surface-2);
  border-bottom: 1px solid var(--xb-border);
}

.xb-table-wrap[data-layout="stack"] .xb-table thead tr {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--xb-space-half) var(--xb-space-1);
  padding: var(--xb-space-half) var(--xb-space-1);
}

.xb-table-wrap[data-layout="stack"] .xb-table thead th {
  position: static;
  display: block;
  width: auto !important;
  height: auto;
  padding: 0;
  border: 0;
  background: none;
  overflow: visible;
}

/* Headers with nothing to press stay for assistive technology only. */
.xb-table-wrap[data-layout="stack"] .xb-table thead th:not([data-select], [data-sortable]) {
  position: absolute;
  width: 1px !important;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.xb-table-wrap[data-layout="stack"] .xb-table thead:not(:has(th[data-select], th[data-sortable])) {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  border: 0;
}

.xb-table-wrap[data-layout="stack"] .xb-table thead th[data-select] {
  padding: 0;
}

.xb-table-wrap[data-layout="stack"] .xb-sort {
  min-height: var(--xb-control-h);
  padding: 0 var(--xb-space-1);
  border-radius: var(--xb-radius);
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr {
  display: block;
}

/* The empty state and the spacer rows are one full-width cell. */
.xb-table-wrap[data-layout="stack"] .xb-table tbody tr:not([data-row]) > td {
  display: block;
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-empty] > td {
  padding: var(--xb-space-1) var(--xb-space-2);
}

/* A card is a grid: the labels share one column, sized to the longest,
 * through subgrid, so the values line up and get the rest. */
.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row] {
  display: grid;
  grid-template-columns: [label] max-content [value] minmax(0, 1fr);
  column-gap: var(--xb-space-2);
  row-gap: 6px;
  padding: 14px var(--xb-space-2);
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row]:has(> td[data-select]) {
  grid-template-columns: [select] 40px [label] max-content [value] minmax(0, 1fr);
  padding-left: var(--xb-space-1);
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row] + tr[data-row] {
  border-top: 1px solid var(--xb-border);
}

.xb-table-wrap[data-layout="stack"] .xb-table td {
  height: auto;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
  text-align: left;
  white-space: normal;
  overflow: visible;
  overflow-wrap: anywhere;
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row] > td:not([data-select]) {
  display: grid;
  grid-column: label / -1;
  grid-template-columns: subgrid;
  align-items: center;
  min-height: 24px;
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row] > td[data-blank] {
  display: none;
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row] > td[data-primary] {
  display: block;
  align-self: center;
  font-size: var(--xb-text-base);
  font-weight: 600;
}

.xb-table-wrap[data-layout="stack"] .xb-table td[data-select] {
  grid-column: select;
  grid-row: 1;
  width: auto;
  padding: 0;
}

.xb-table-wrap[data-layout="stack"] .xb-select-hit {
  justify-content: center;
  min-height: 40px;
}

.xb-table-wrap[data-layout="stack"] .xb-cell-value {
  grid-column: value;
}

/* A ghost button lines up with the values' text, not its own padding. */
.xb-table-wrap[data-layout="stack"] .xb-cell-value > .xb-btn[data-variant="ghost"]:first-child,
.xb-table-wrap[data-layout="stack"] .xb-cell-value > .xb-row > .xb-btn[data-variant="ghost"]:first-child {
  margin-inline-start: calc(-1 * var(--xb-space-1));
}

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

.xb-cell-value {
  min-width: 0;
}

/* What would truncate in a column has the card's width to wrap in. */
.xb-table-wrap[data-layout="stack"] .xb-table td .xb-truncate {
  white-space: normal;
  overflow: visible;
}

.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row]:hover { background: var(--xb-surface-hover); }
.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[aria-selected="true"] { background: var(--xb-selected); }
.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row]:focus-visible { box-shadow: inset 0 0 0 2px var(--xb-focus); }
.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-row] td:first-child { box-shadow: none; }
.xb-table-wrap[data-layout="stack"] .xb-table tbody tr[data-end] { height: 1px; }

.xb-bulkbar {
  /* Never squeezed by a filling table below it. */
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--xb-space-1);
  min-height: var(--xb-space-6);
  padding: var(--xb-space-1) var(--xb-space-2);
  border: 1px solid var(--xb-accent);
  border-radius: var(--xb-radius-pill);
  background: var(--xb-accent-soft);
  font-size: var(--xb-text-md);
}

.xb-bulkbar-count { font-weight: 600; margin-right: auto; }

/* ─────────────────────────────── tree ─────────────────────────────── */

.xb-tree,
.xb-tree ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.xb-tree-row {
  display: flex;
  align-items: center;
  gap: 6px;
  height: var(--xb-row-h);
  padding-right: var(--xb-space-1);
  padding-left: calc(var(--xb-space-1) + (var(--xb-level) - 1) * var(--xb-space-2));
  border-radius: var(--xb-radius);
  color: var(--xb-text);
  font-size: var(--xb-text-md);
  cursor: pointer;
  user-select: none;
}

/* Indent by level from an attribute, not an inline style, so a tree
 * rendered on the server still passes style-src 'self'. Deeper levels
 * stop indenting at 10. */
.xb-tree-row { --xb-level: 10; }
.xb-tree [data-level="1"] > .xb-tree-row { --xb-level: 1; }
.xb-tree [data-level="2"] > .xb-tree-row { --xb-level: 2; }
.xb-tree [data-level="3"] > .xb-tree-row { --xb-level: 3; }
.xb-tree [data-level="4"] > .xb-tree-row { --xb-level: 4; }
.xb-tree [data-level="5"] > .xb-tree-row { --xb-level: 5; }
.xb-tree [data-level="6"] > .xb-tree-row { --xb-level: 6; }
.xb-tree [data-level="7"] > .xb-tree-row { --xb-level: 7; }
.xb-tree [data-level="8"] > .xb-tree-row { --xb-level: 8; }
.xb-tree [data-level="9"] > .xb-tree-row { --xb-level: 9; }
.xb-tree [data-level="10"] > .xb-tree-row { --xb-level: 10; }

.xb-tree [role="treeitem"]:focus-visible { outline: none; }
.xb-tree [role="treeitem"]:focus-visible > .xb-tree-row { box-shadow: inset 0 0 0 2px var(--xb-focus); }
.xb-tree-row:hover { background: var(--xb-surface-hover); }
.xb-tree [aria-selected="true"] > .xb-tree-row { background: var(--xb-selected); color: var(--xb-text); font-weight: 600; }

.xb-tree-toggle {
  display: inline-flex;
  flex: none;
  width: 16px;
  height: 16px;
  color: var(--xb-text-muted);
  transition: transform var(--xb-duration) var(--xb-ease);
}

/* The chevron fits its 16px box, so nothing spills past the row. */
.xb-tree-toggle > .xb-icon { width: 16px; height: 16px; }
.xb-tree [aria-expanded="true"] > .xb-tree-row .xb-tree-toggle { transform: rotate(90deg); }
.xb-tree-count { margin-left: auto; color: var(--xb-text-muted); font-size: var(--xb-text-xs); }

/* A row's own controls (Tree's `actions`): at its end, shown on the
 * selected row, the row under the pointer and the row with focus, and
 * always under a finger, where there is no hover. */
.xb-tree-actions { display: inline-flex; flex: none; margin-left: auto; opacity: 0; }
.xb-tree-count + .xb-tree-actions { margin-left: 0; }
.xb-tree-row:hover > .xb-tree-actions,
.xb-tree [aria-selected="true"] > .xb-tree-row > .xb-tree-actions,
.xb-tree [role="treeitem"]:focus-visible > .xb-tree-row > .xb-tree-actions,
.xb-tree-actions:focus-within,
.xb-tree-actions:has([aria-expanded="true"]) { opacity: 1; }
/* A menu opened from a row is not the row: not bold when it is selected. */
.xb-tree-actions .xb-popover { font-weight: 400; cursor: auto; user-select: auto; }
@media (pointer: coarse) {
  .xb-tree-actions { opacity: 1; }
}

/* ──────────────────────────── master-detail ──────────────────────────── */

.xb-md {
  container-type: inline-size;
  display: flex;
  min-width: 0;
  min-height: 0;
  flex: 1 1 auto;
}

.xb-md-inner {
  display: grid;
  grid-template-columns: var(--xb-md-w, 320px) 8px minmax(0, 1fr);
  width: 100%;
  min-height: 0;
}

.xb-md-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: auto;
}

.xb-md-sep {
  position: relative;
  cursor: col-resize;
  touch-action: none;
}

.xb-md-sep::after {
  content: "";
  position: absolute;
  inset: 0 3px;
  background: var(--xb-border);
  transition: background-color var(--xb-duration) var(--xb-ease);
}

.xb-md-sep:hover::after,
.xb-md-sep:focus-visible::after { background: var(--xb-accent); }
.xb-md-sep:focus-visible { outline: none; }

.xb-md-back { display: none; }

@container (max-width: 767px) {
  .xb-md-inner { grid-template-columns: minmax(0, 1fr); }
  .xb-md-sep { display: none; }
  .xb-md[data-detail-open="true"] .xb-md-master { display: none; }
  .xb-md[data-detail-open="false"] .xb-md-detail { display: none; }
  .xb-md-back { display: flex; }
}

/* ───────────────────────────── app shell ───────────────────────────── */

@property --xb-sidebar-w {
  syntax: "<length>";
  inherits: true;
  initial-value: 248px;
}

.xb-shell {
  --xb-sidebar-w: 248px;
  display: grid;
  grid-template-columns: var(--xb-sidebar-w) minmax(0, 1fr);
  grid-template-rows: var(--xb-topbar-h) minmax(0, 1fr);
  grid-template-areas: "brand top" "side main";
  min-height: 100dvh;
  /* The sidebar's column stays painted below the sticky sidebar when
   * the page is taller than the viewport. */
  background: linear-gradient(to right, var(--xb-surface-2) calc(var(--xb-sidebar-w) - 1px), var(--xb-border) 0, var(--xb-border) var(--xb-sidebar-w), transparent 0);
}

/* The sidebar slides when the person opens or shuts it, and snaps when
 * a screen asks for it shut (AppShell's collapseNav), so that screen
 * lays out once, at its final width. */
.xb-shell[data-slide] {
  transition: --xb-sidebar-w var(--xb-duration) var(--xb-ease);
}

.xb-shell-brand {
  grid-area: brand;
  /* The bar is one row tall, but its grid area runs the full shell so
   * it can stick to the viewport while the page scrolls. */
  grid-row: 1 / -1;
  position: sticky;
  top: 0;
  z-index: calc(var(--xb-z-sticky) + 1);
  align-self: start;
  display: flex;
  align-items: center;
  gap: var(--xb-space-1);
  height: var(--xb-topbar-h);
  padding: 0 var(--xb-space-2) 0 20px;
  border-right: 1px solid var(--xb-border);
  background: var(--xb-surface-2);
  font-weight: 700;
  min-width: 0;
  overflow: hidden;
}

.xb-shell-top {
  grid-area: top;
  grid-row: 1 / -1;
  position: sticky;
  top: 0;
  z-index: calc(var(--xb-z-sticky) + 1);
  align-self: start;
  display: flex;
  align-items: center;
  gap: var(--xb-space-1);
  height: var(--xb-topbar-h);
  padding: 0 var(--xb-space-3);
  border-bottom: 1px solid var(--xb-border);
  background: color-mix(in srgb, var(--xb-bg) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  min-width: 0;
}

/* The product mark, in the bar, once the brand corner has collapsed.
 * A narrow screen keeps it in the menu drawer instead. */
.xb-shell-top-brand {
  display: none;
  align-items: center;
  min-width: 0;
}

/* Where a screen puts its own controls (TopBarSlot). It takes the room
 * the bar's other contents leave; empty, it is the spacer between them. */
.xb-shell-slot {
  display: flex;
  align-items: center;
  align-self: stretch;
  flex: 1 1 auto;
  min-width: 0;
}

.xb-shell-side {
  grid-area: side;
  position: sticky;
  top: var(--xb-topbar-h);
  align-self: start;
  height: calc(100dvh - var(--xb-topbar-h));
  overflow: hidden auto;
  padding: var(--xb-space-1) 12px var(--xb-space-3);
  border-right: 1px solid var(--xb-border);
  background: var(--xb-surface-2);
}

.xb-shell-main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.xb-shell-menu { display: none; }

/* Desktop only: a click, or a short drag, on the menu's edge snaps it
 * fully open or fully shut. A narrow screen keeps the Menu button and
 * the drawer. */
.xb-shell-grip { display: none; }

@media (min-width: 900px) {
  .xb-shell[data-nav="closed"] .xb-shell-top-brand { display: flex; }
  .xb-shell-grip {
    display: flex;
    align-items: center;
    justify-content: center;
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: start;
    align-self: start;
    position: sticky;
    top: var(--xb-topbar-h);
    height: calc(100dvh - var(--xb-topbar-h));
    z-index: 6;
    width: 16px;
    /* Ride the edge while it animates, and stay fully on screen once shut. */
    margin-left: max(0px, calc(var(--xb-sidebar-w) - 8px));
    cursor: pointer;
    touch-action: none;
  }
  .xb-shell-grip-handle {
    width: 6px;
    height: 44px;
    border-radius: 999px;
    background: var(--xb-surface);
    border: 1px solid var(--xb-control-border);
    box-shadow: 0 1px 2px rgb(0 0 0 / 0.2);
  }
  .xb-shell-grip:hover .xb-shell-grip-handle,
  .xb-shell-grip:focus-visible .xb-shell-grip-handle,
  .xb-shell[data-dragging] .xb-shell-grip-handle {
    background: var(--xb-accent);
    border-color: var(--xb-accent);
  }
  .xb-shell-grip:focus-visible { outline: none; }
  .xb-shell-grip:focus-visible .xb-shell-grip-handle { box-shadow: 0 0 0 2px var(--xb-focus); }
  .xb-shell[data-nav="closed"] .xb-shell-brand,
  .xb-shell[data-nav="closed"] .xb-shell-side {
    padding-inline: 0;
    border-right-color: transparent;
  }
  /* The corner's copy of the mark hides at once; the bar shows its own. */
  .xb-shell[data-nav="closed"] .xb-shell-brand { visibility: hidden; }
  /* The shut sidebar's links hide too, once it has slid shut: in a box
   * no wider than nothing they would still count as on screen. */
  .xb-shell[data-nav="closed"] .xb-shell-side {
    visibility: hidden;
    transition: visibility 0s linear var(--xb-duration);
  }
}

/* A stacked table scrolls with the page, so its header would stick over
 * the shell bar. Hold it just under that bar. A table that scrolls
 * inside itself keeps top: 0, against its own box. */
.xb-shell-main .xb-table-wrap[data-layout="stack"]:not([data-fill]) .xb-table thead {
  top: var(--xb-topbar-h);
}

@media (max-width: 899px) {
  .xb-shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--xb-topbar-h) minmax(0, 1fr);
    grid-template-areas: "top" "main";
    background: none;
    --xb-sidebar-w: 0px;
  }
  .xb-shell-brand,
  .xb-shell-side { display: none; }
  .xb-shell-menu { display: inline-flex; }
  .xb-shell-top { padding: 0 var(--xb-space-1); gap: var(--xb-space-half); }
}

.xb-shell-drawer-nav { padding: var(--xb-space-1) 12px; }

.xb-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.xb-nav-heading {
  margin: var(--xb-space-3) 12px var(--xb-space-half);
  color: var(--xb-text-muted);
  font-size: var(--xb-text-xs);
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.xb-nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  height: max(40px, var(--xb-control-h));
  padding: 0 12px;
  border-radius: var(--xb-radius-pill);
  color: var(--xb-text-muted);
  font-size: var(--xb-text-base);
  font-weight: 600;
  text-decoration: none;
  transition: background-color var(--xb-duration) var(--xb-ease), color var(--xb-duration) var(--xb-ease);
}

.xb-nav-link:hover { background: var(--xb-surface-hover); color: var(--xb-text); }
.xb-nav-link[aria-current="page"] { background: var(--xb-surface); color: var(--xb-text); box-shadow: var(--xb-shadow-sm), inset 0 0 0 1px var(--xb-border); }
.xb-nav-link[aria-current="page"] .xb-icon { color: var(--xb-accent); }

.xb-dl {
  display: grid;
  grid-template-columns: minmax(96px, max-content) minmax(0, 1fr);
  gap: 10px var(--xb-space-3);
  margin: 0;
  font-size: var(--xb-text-md);
}

.xb-dl dd { font-weight: 500; }

.xb-dl-row { display: contents; }
.xb-dl dt { color: var(--xb-text-muted); }
.xb-dl dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

@media (max-width: 399px) {
  .xb-dl { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .xb-dl dd { margin-bottom: var(--xb-space-1); }
}
