/* ============================================
   primitives.css — design system base components

   Visual chrome for the primitives in components/primitives/index.jsx. Every
   selector here references MD3 system tokens (`var(--md-sys-*)`) or xmesh
   extensions (`var(--xm-*)`) only — never primitives, never raw colors.
   Theme flipping (dark/light) happens automatically through the token layer.

   Naming: the `ds-` prefix scopes these primitives so they don't collide
   with the legacy class names that still live in styles.css and
   chat-shell.jsx (e.g. `.text-input`, `.icon-btn`, `.chip`). The legacy
   classes keep working; new code reaches for `<Input />`, `<IconButton />`
   etc. instead.
   ============================================ */


/* ---------- Icon ----------
   Just a marker class; sizing is handled inline via width/height on the
   svg so it stays predictable regardless of the surrounding font-size. */
.ds-icon { display: inline-block; vertical-align: middle; flex-shrink: 0; }


/* ---------- Spinner ---------- */
.ds-spinner {
  display: inline-block;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: ds-spin 0.7s linear infinite;
  opacity: 0.85;
  vertical-align: middle;
}
@keyframes ds-spin { to { transform: rotate(360deg); } }


/* ---------- Label / Hint / Field ---------- */
.ds-label {
  display: inline-flex;
  align-items: center;
  gap: var(--s-1-5);
  font-size: 13px;
  font-weight: 600;
  color: var(--md-sys-color-inverse-on-surface);
  line-height: 1.4;
}
.ds-label__req {
  color: var(--md-sys-color-inverse-on-surface);
  font-weight: 700;
  margin-left: var(--s-0-5);
}

.ds-hint {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.5;
}
.ds-hint--muted   { color: var(--xm-color-inverse-on-surface-muted); }

.ds-field { display: flex; flex-direction: column; gap: var(--s-1-5); }
.ds-field__control { display: flex; flex-direction: column; gap: var(--s-1-5); }


/* ---------- Input / Textarea ----------
   `ds-input` is the WRAPPER that owns the border + affix layout. The inner
   <input>/<textarea> is borderless and inherits color so left/right adornments
   sit flush. Two tones: `card` for light surfaces, `on-canvas` for the dark
   sidebar/canvas surface. */
.ds-input {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 100%;
  border: 1.5px solid var(--md-sys-color-outline);
  border-radius: var(--md-sys-shape-corner-button);
  background: var(--md-sys-color-inverse-surface);
  color: var(--md-sys-color-inverse-on-surface);
  transition:
    border-color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    box-shadow var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    background var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
}
.ds-input--ta { align-items: flex-start; }
.ds-input:focus-within {
  border-color: var(--md-sys-color-primary);
  box-shadow: var(--xm-state-focus-ring);
}
.ds-input.is-invalid,
.ds-input.is-invalid:focus-within {
  border-color: var(--md-sys-color-inverse-on-surface);
  border-width: 2px;
  box-shadow: var(--xm-state-focus-ring);
}

.ds-input__el {
  flex: 1;
  min-width: 0;
  appearance: none;
  background: transparent;
  border: none;
  outline: none;
  font: inherit;
  font-size: 13.5px;
  color: inherit;
  padding: 9px var(--s-3);
}
.ds-input__el::placeholder { color: var(--xm-color-inverse-on-surface-muted); }
.ds-input__el.mono { font-family: var(--xm-typescale-mono-font); letter-spacing: 0; }

.ds-input__el--ta {
  resize: vertical;
  min-height: 78px;
  max-height: 220px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  padding: var(--s-2-5) var(--s-3);
}

/* Affix slots — left/right buttons or icons inside the input chrome.
   Padding is removed from the side that has an affix so the inner element
   sits flush. */
.ds-input__affix {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--xm-color-inverse-on-surface-muted);
  flex-shrink: 0;
}
.ds-input__affix--left  { padding-left: var(--s-2-5);  }
.ds-input__affix--right { padding-right: var(--s-1-5);  }
.ds-input__affix--ta    { align-self: flex-start; padding-top: var(--s-1-5); }
.ds-input__affix > button {
  appearance: none;
  border: none;
  background: transparent;
  color: inherit;
  width: 28px; height: 28px;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ds-input__affix > button:hover { background: var(--md-sys-color-outline-variant); }

/* on-canvas tone — for the dark sidebar/canvas */
.ds-input--on-canvas {
  background: var(--md-sys-color-surface-container-lowest);
  border-color: var(--md-sys-color-outline);
  color: var(--md-sys-color-on-surface);
}
.ds-input--on-canvas .ds-input__el::placeholder { color: var(--xm-color-on-surface-soft); }
.ds-input--on-canvas:focus-within { border-color: var(--md-sys-color-primary); box-shadow: none; }


/* ---------- SearchInput ----------
   Visual sibling of `.detail-search` in styles.css but reusable. The default
   `card` tone matches the inspector's bordered card; `on-canvas` matches the
   sidebar. */
.ds-search {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  width: 100%;
  padding: var(--s-1-5) var(--s-2-5);
  border-radius: 8px;
  border: 1px solid var(--md-sys-color-outline-variant);
  background: var(--md-sys-color-inverse-surface);
  color: var(--xm-color-inverse-on-surface-muted);
  transition:
    border-color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    box-shadow var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
}
.ds-search:focus-within {
  border-color: var(--md-sys-color-primary);
  box-shadow: var(--xm-state-focus-ring);
}
.ds-search__icon { display: inline-flex; align-items: center; }
.ds-search__input {
  flex: 1;
  min-width: 0;
  appearance: none;
  border: none;
  outline: none;
  background: transparent;
  font-size: 13px;
  color: var(--md-sys-color-inverse-on-surface);
}
.ds-search__input::placeholder { color: var(--xm-color-inverse-on-surface-muted); }
.ds-search__clear {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--xm-color-inverse-on-surface-muted);
  cursor: pointer;
  padding: var(--s-0-5) var(--s-1);
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ds-search__clear:hover { color: var(--md-sys-color-inverse-on-surface); background: var(--md-sys-color-outline-variant); }

.ds-search--on-canvas {
  background: var(--md-sys-color-surface-container-lowest);
  border-color: var(--md-sys-color-outline);
  color: var(--xm-color-on-surface-soft);
}
.ds-search--on-canvas .ds-search__input { color: var(--md-sys-color-on-surface); }
.ds-search--on-canvas .ds-search__input::placeholder { color: var(--xm-color-on-surface-soft); }
.ds-search--on-canvas:focus-within { border-color: var(--md-sys-color-primary); box-shadow: none; }


/* ---------- Link ---------- */
.ds-link {
  color: var(--xm-color-primary-pressed);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  text-decoration-skip-ink: auto;
  transition:
    color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    text-decoration-thickness var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
}
.ds-link:hover { color: var(--md-sys-color-on-primary); text-decoration-thickness: 2px; }
.ds-link:focus-visible { outline: 1px solid var(--md-sys-color-primary); outline-offset: 2px; border-radius: 2px; }


/* ---------- IconButton ---------- */
.ds-icon-btn {
  appearance: none;
  border: none;
  background: transparent;
  cursor: pointer;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--xm-color-inverse-on-surface-muted);
  transition:
    background var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    border-color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
  flex-shrink: 0;
}
.ds-icon-btn--sm { width: 24px; height: 24px; border-radius: 6px; }
.ds-icon-btn--md { width: 32px; height: 32px; }
.ds-icon-btn--lg { width: 34px; height: 34px; }

.ds-icon-btn--ghost:hover { background: var(--md-sys-color-outline-variant); color: var(--md-sys-color-inverse-on-surface); }

.ds-icon-btn--raised {
  border: 1px solid var(--md-sys-color-outline);
  background: var(--md-sys-color-surface-container-low);
  color: var(--md-sys-color-on-surface);
}
.ds-icon-btn--raised:hover {
  background: var(--md-sys-color-surface-container-highest);
  color: var(--md-sys-color-on-surface);
}

.ds-icon-btn--soft-danger { color: var(--xm-color-on-surface-soft); }
.ds-icon-btn--soft-danger:hover { background: var(--md-sys-color-outline-variant); color: var(--md-sys-color-inverse-on-surface); }

.ds-icon-btn[disabled] { cursor: not-allowed; opacity: 0.6; }
.ds-icon-btn:focus-visible { outline: 1px solid var(--md-sys-color-primary); outline-offset: 2px; }


/* ---------- Switch ---------- */
.ds-switch {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2-5);
  cursor: pointer;
  user-select: none;
}
.ds-switch input { display: none; }
.ds-switch__track {
  width: 38px; height: 22px;
  border-radius: 999px;
  background: var(--md-sys-color-outline);
  position: relative;
  transition: background var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-standard);
  flex-shrink: 0;
}
.ds-switch__thumb {
  position: absolute;
  top: 2px; left: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--md-sys-color-inverse-surface);
  box-shadow: var(--md-sys-elevation-level1);
  transition: transform var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-standard);
}
.ds-switch input:checked + .ds-switch__track { background: var(--md-sys-color-primary); }
.ds-switch input:checked + .ds-switch__track .ds-switch__thumb { transform: translateX(16px); }
.ds-switch__label {
  font-size: 13px;
  font-weight: 500;
  color: var(--md-sys-color-inverse-on-surface);
}
.ds-switch:focus-within .ds-switch__track { box-shadow: 0 0 0 3px var(--md-sys-color-primary-container); }


/* ---------- Pill ---------- */
.ds-pill {
  appearance: none;
  display: inline-flex;
  align-items: center;
  gap: var(--s-1-5);
  border: 1px solid transparent;
  background: transparent;
  color: var(--md-sys-color-on-surface);
  border-radius: var(--md-sys-shape-corner-full);
  cursor: pointer;
  font: inherit;
  line-height: 1;
  white-space: nowrap;
  transition:
    background var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    border-color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
}
.ds-pill--sm { font-size: 11.5px; padding: 5px var(--s-2-5); }
.ds-pill--md { font-size: 13px;   padding: var(--s-2) var(--s-3-5); }

.ds-pill--neutral {
  border-color: var(--md-sys-color-outline);
  background: var(--md-sys-color-surface-container-lowest);
  color: var(--md-sys-color-on-surface);
}
.ds-pill--neutral:hover {
  background: var(--md-sys-color-surface-container-low);
  border-color: var(--md-sys-color-outline);
}
.ds-pill--neutral.is-selected,
.ds-pill--accent {
  border-color: var(--xm-color-primary-pressed);
  background: var(--md-sys-color-primary-container);
  color: var(--md-sys-color-on-primary);
}
.ds-pill--accent:hover { background: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary); }

.ds-pill[disabled] { cursor: not-allowed; opacity: 0.55; }
.ds-pill:focus-visible { outline: 1px solid var(--md-sys-color-primary); outline-offset: 2px; }


/* ---------- Badge ----------
   Variant `soft` is the default tinted background, `solid` fills with the
   tone color. `method` and `ext` use the existing method/ext token sets. */
.ds-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--s-1);
  font-size: 10px;
  font-weight: 600;
  padding: var(--s-0-5) 7px;
  border-radius: 4px;
  letter-spacing: 0.04em;
  flex-shrink: 0;
  line-height: 1.4;
  font-feature-settings: "tnum";
}
.ds-badge--sm { font-size: 9.5px; padding: var(--s-0-5) 5px; letter-spacing: 0.05em; }
.ds-badge--md { /* default */ }
.ds-badge--lg { font-size: 11.5px; padding: 3px var(--s-2); }

.ds-badge.mono { font-family: var(--xm-typescale-mono-font); }

/* soft variant */
.ds-badge--soft.ds-badge--neutral { background: var(--md-sys-color-outline-variant); color: var(--xm-color-inverse-on-surface-muted); }
.ds-badge--soft.ds-badge--accent  { background: var(--md-sys-color-primary-container);  color: var(--md-sys-color-on-primary); }

/* solid variant */
.ds-badge--solid.ds-badge--neutral { background: var(--xm-color-inverse-on-surface-muted); color: var(--md-sys-color-inverse-surface); }
.ds-badge--solid.ds-badge--accent  { background: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary); }

/* method variant — children carry the lowercased method name; consumers
   pass `tone` matching: get / post / put / delete / patch */
.ds-badge--method { font-family: var(--xm-typescale-mono-font); text-transform: uppercase; }
.ds-badge--method.ds-badge--get    { background: var(--xm-method-get-bg);    color: var(--xm-method-get-ink); }
.ds-badge--method.ds-badge--post   { background: var(--xm-method-post-bg);   color: var(--xm-method-post-ink); }
.ds-badge--method.ds-badge--put    { background: var(--xm-method-put-bg);    color: var(--xm-method-put-ink); }
.ds-badge--method.ds-badge--delete { background: var(--xm-method-delete-bg); color: var(--xm-method-delete-ink); }
.ds-badge--method.ds-badge--patch  { background: var(--xm-method-patch-bg);  color: var(--xm-method-patch-ink); }

/* ext variant */
.ds-badge--ext { font-family: var(--xm-typescale-mono-font); text-transform: uppercase; font-size: 9.5px; padding: 3px var(--s-1-5); letter-spacing: 0.05em; font-weight: 700; }
.ds-badge--ext.ds-badge--yml,
.ds-badge--ext.ds-badge--yaml { background: var(--xm-ext-yml-bg);  color: var(--xm-ext-yml-ink); }
.ds-badge--ext.ds-badge--json { background: var(--xm-ext-json-bg); color: var(--xm-ext-json-ink); }


/* ---------- Kbd ---------- */
.ds-kbd {
  display: inline-flex;
  align-items: center;
  font-family: var(--xm-typescale-mono-font);
  font-size: 11px;
  padding: var(--s-0-5) var(--s-1-5);
  border-radius: 4px;
  background: var(--md-sys-color-surface-container-high);
  color: var(--xm-color-inverse-on-surface-muted);
  border: 1px solid var(--md-sys-color-outline-variant);
  font-feature-settings: "tnum";
}
.ds-kbd--accent {
  background: color-mix(in oklch, var(--md-sys-color-primary-container) 50%, transparent);
  color: var(--md-sys-color-on-primary);
  border-color: transparent;
}


/* ---------- Divider ---------- */
.ds-divider { height: 1px; width: 100%; }
.ds-divider--card    { background: var(--md-sys-color-outline-variant); }
.ds-divider--canvas  { background: var(--md-sys-color-outline); }
.ds-divider--strong  { background: var(--md-sys-color-outline); }


/* ---------- Dot ---------- */
.ds-dot { display: inline-block; border-radius: 50%; flex-shrink: 0; }
.ds-dot--accent  { background: var(--md-sys-color-primary); }
.ds-dot--muted   { background: var(--xm-color-inverse-on-surface-muted); }


/* ---------- Sep — middot inline metadata divider ----------
   Used between inline metadata fragments, e.g. "Document · MD" or
   "12:30 · 4 KB". The element renders as a styled middot character;
   pass it the literal "·" as children. Opacity is applied so the
   ink reads softer than its surrounding text on either surface. */
.ds-sep { opacity: 0.5; }


/* ---------- TabBar ---------- */
.ds-tabs {
  display: flex;
  gap: var(--s-0-5);
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.ds-tab {
  appearance: none;
  background: transparent;
  border: none;
  padding: var(--s-2) var(--s-3) var(--s-2-5);
  font-size: 13px;
  font-weight: 500;
  color: var(--xm-color-inverse-on-surface-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  display: inline-flex;
  align-items: center;
  gap: var(--s-1-5);
  transition:
    color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard),
    border-color var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-standard);
}
.ds-tab:hover { color: var(--md-sys-color-inverse-on-surface); }
.ds-tab.is-active {
  color: var(--md-sys-color-inverse-on-surface);
  border-bottom-color: var(--xm-color-primary-pressed);
}
.ds-tab__count {
  font-size: 11px;
  background: var(--md-sys-color-outline-variant);
  color: var(--xm-color-inverse-on-surface-muted);
  padding: 1px var(--s-1-5);
  border-radius: 99px;
  font-feature-settings: "tnum";
}
.ds-tab.is-active .ds-tab__count { background: var(--md-sys-color-primary-container); color: var(--md-sys-color-on-primary); }


/* ---------- Drawer / on-canvas overrides (applied where a primitive
   lives on a dark surface — sidebar or settings drawer) ----------
   Mirrors the existing drawer overrides in styles.css for the legacy
   classes, so a `<Field>` or `<Switch>` dropped into the drawer reads
   correctly in the dark theme. */
:root[data-theme="dark"] .drawer .ds-label,
:root[data-theme="dark"] .drawer .ds-hint--muted {
  color: var(--md-sys-color-on-surface);
}
:root[data-theme="dark"] .drawer .ds-input--card {
  background: var(--md-sys-color-surface-container-low);
  border-color: var(--md-sys-color-outline);
  color: var(--md-sys-color-on-surface);
}
:root[data-theme="dark"] .drawer .ds-input--card .ds-input__el::placeholder {
  color: var(--xm-color-on-surface-soft);
}
:root[data-theme="dark"] .drawer .ds-input--card:focus-within {
  border-color: var(--md-sys-color-primary);
  box-shadow: none;
}
:root[data-theme="dark"] .drawer .ds-input__affix > button { color: var(--xm-color-on-surface-soft); }
:root[data-theme="dark"] .drawer .ds-input__affix > button:hover {
  background: var(--md-sys-color-surface-container-highest);
  color: var(--md-sys-color-on-surface);
}
:root[data-theme="dark"] .drawer .ds-switch__track { background: var(--md-sys-color-surface-container-highest); }
:root[data-theme="dark"] .drawer .ds-switch__label { color: var(--md-sys-color-on-surface); }
:root[data-theme="dark"] .drawer .ds-divider--card { background: var(--md-sys-color-outline); }
