/* =============================================================================
   PRIMITIVES — generic building blocks. Use these before inventing styles.
   Colour comes ONLY from tokens, so every primitive works in both themes.
   Naming: legacy flat names (.btn, .chip, .icon-btn) + BEM for the rest.
   ========================================================================== */
@layer primitives {

/* ── Utilities ─────────────────────────────────────────────────────────── */
.eyebrow { font: var(--weight-regular) var(--text-xs)/1 var(--font-mono); text-transform: uppercase; letter-spacing: var(--tracking-eyebrow); color: var(--text-muted); }
.mono { font-family: var(--font-mono); }
.muted { color: var(--text-secondary); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.surface { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); }
.glass { background: rgba(var(--bg-rgb), .8); backdrop-filter: blur(var(--blur-md)); -webkit-backdrop-filter: blur(var(--blur-md)); }
.dotgrid { background-image: radial-gradient(circle, var(--dot) 1px, transparent 1px); background-size: 24px 24px; }
.stack { display: flex; flex-direction: column; gap: var(--stack-gap); }
.row { display: flex; align-items: center; gap: var(--stack-gap); flex-wrap: wrap; }

/* ── Icons ─────────────────────────────────────────────────────────────── */
.icon { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.icon-sm { width: 14px; height: 14px; }
.icon-xs { width: 12px; height: 12px; }
.icon-lg { width: 20px; height: 20px; }

/* ── Buttons ───────────────────────────────────────────────────────────── */
.btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  height: var(--control-h-md); padding: 0 var(--space-4); flex-shrink: 0;
  border: 1px solid transparent; border-radius: var(--radius-control); background: transparent; color: var(--text-primary);
  font-family: inherit; font-size: var(--text-base); font-weight: var(--weight-medium); line-height: 1; white-space: nowrap;
  text-decoration: none; user-select: none; -webkit-user-select: none; cursor: pointer;
  transition: background-color var(--duration-base) var(--ease-soft), border-color var(--duration-base) var(--ease-soft), color var(--duration-base) var(--ease-soft), transform var(--duration-base) var(--ease-soft), opacity var(--duration-base);
}
.btn:active { transform: scale(.97); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .35; cursor: not-allowed; pointer-events: none; }
.btn-primary { background: var(--accent); color: var(--on-accent); box-shadow: var(--shadow-glow); }
.btn-primary:hover { opacity: .9; }
.btn-secondary { background: var(--surface-raised); border-color: var(--border); }
.btn-secondary:hover { background: var(--surface-hover); border-color: var(--border-hover); }
.btn-outline { border-color: var(--border); }
.btn-outline:hover { background: var(--accent-subtle); border-color: var(--border-hover); }
.btn-ghost { color: var(--text-secondary); }
.btn-ghost:hover { background: var(--accent-subtle); color: var(--text-primary); }
.btn-danger { background: var(--danger-subtle); border-color: var(--danger-border); color: var(--danger); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 22%, transparent); }
.btn-sm { height: var(--control-h-sm); padding: 0 var(--space-3); font-size: var(--text-sm); gap: var(--space-1-5); }
.btn-lg { height: var(--control-h-lg); padding: 0 var(--space-6); font-size: var(--text-md); gap: var(--space-2-5); }
.btn-pill { border-radius: var(--radius-pill); }
.btn-icon { width: var(--control-h-md); padding: 0; }
.btn-icon.btn-sm { width: var(--control-h-sm); }
.btn-icon.btn-lg { width: var(--control-h-lg); }
.btn-block { width: 100%; }
.btn.is-loading { color: transparent; pointer-events: none; }
.btn.is-loading::after { content: ""; position: absolute; width: 14px; height: 14px; border-radius: 50%; border: 2px solid currentColor; border-top-color: transparent; color: var(--text-primary); animation: spin .7s linear infinite; }
.btn-primary.is-loading::after { color: var(--on-accent); }
a.btn:hover { text-decoration: none; }

.icon-btn {
  display: inline-grid; place-items: center; flex-shrink: 0; width: 28px; height: 28px; padding: 0;
  border: 0; border-radius: var(--radius-md); background: transparent; color: var(--text-muted); cursor: pointer;
  transition: background-color var(--duration-base) var(--ease-soft), color var(--duration-base) var(--ease-soft);
}
.icon-btn:hover { background: var(--accent-subtle); color: var(--text-primary); }

/* ── Chips / badges ────────────────────────────────────────────────────── */
.chip {
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; height: 20px; padding: 0 var(--space-2);
  border: 1px solid var(--border); border-radius: var(--radius-badge); background: var(--surface-raised); color: var(--text-secondary);
  font: var(--weight-medium) var(--text-2xs)/1 var(--font-mono); white-space: nowrap; letter-spacing: 0;
}
.chip-accent { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.chip-outline { background: transparent; }
.chip-green, .chip-success { background: var(--success-subtle); border-color: var(--success-border); color: var(--success); }
.chip-amber, .chip-warning { background: var(--warning-subtle); border-color: var(--warning-border); color: var(--warning); }
.chip-red, .chip-danger { background: var(--danger-subtle); border-color: var(--danger-border); color: var(--danger); }
.chip-brand, .chip-info { background: var(--info-subtle); border-color: var(--info-border); color: var(--info); }
.chip-violet { background: var(--violet-subtle); border-color: var(--violet-border); color: var(--violet); }
.chip-dot { width: 6px; height: 6px; margin-right: var(--space-1-5); border-radius: var(--radius-full); background: currentColor; }
.chip-pill { border-radius: var(--radius-pill); }

.kbd {
  display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 var(--space-1-5);
  border: 1px solid var(--border); border-radius: var(--radius-badge); background: var(--surface-raised); color: var(--text-muted);
  font: var(--weight-regular) var(--text-2xs)/1 var(--font-mono);
}

/* ── Avatars ───────────────────────────────────────────────────────────── */
.avatar {
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; width: 28px; height: 28px;
  border-radius: var(--radius-full); border: 2px solid var(--surface); font-size: var(--text-2xs); font-weight: var(--weight-semibold); line-height: 1;
  background: var(--hue, var(--surface-raised)); color: var(--bg);
}
.avatar--info   { --hue: var(--hue-1); } .avatar--violet { --hue: var(--hue-2); } .avatar--success { --hue: var(--hue-3); }
.avatar--warning { --hue: var(--hue-4); } .avatar--danger { --hue: var(--hue-5); }
.avatar--lg { width: 40px; height: 40px; font-size: var(--text-sm); }
.avatars { display: flex; align-items: center; }
.avatars .avatar + .avatar { margin-left: calc(var(--space-2) * -1); }

/* ── Form controls ─────────────────────────────────────────────────────── */
.label { display: block; margin-bottom: var(--space-1-5); font-size: var(--text-sm); font-weight: var(--weight-medium); color: var(--text-secondary); }
.hint { margin-top: var(--space-1-5); font-size: var(--text-xs); color: var(--text-muted); }
.hint--error { color: var(--danger); }
.input, .select, .textarea {
  width: 100%; height: var(--field-h); padding: 0 var(--field-px);
  border: 1px solid var(--border); border-radius: var(--radius-control); background: var(--surface-raised); color: var(--text-primary);
  font-size: var(--text-base); line-height: 1.2;
  transition: border-color var(--duration-base) var(--ease-soft), background-color var(--duration-base) var(--ease-soft), box-shadow var(--duration-base);
}
.input::placeholder, .textarea::placeholder { color: var(--text-muted); }
.input:hover, .select:hover, .textarea:hover { border-color: var(--border-hover); }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--border-hover); box-shadow: 0 0 0 3px var(--accent-subtle); }
.input:disabled, .select:disabled, .textarea:disabled { opacity: .45; cursor: not-allowed; }
.input[aria-invalid="true"] { border-color: var(--danger-border); }
.input[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px var(--danger-subtle); }
.textarea { height: auto; min-height: 88px; padding: var(--space-2-5) var(--space-3); resize: vertical; line-height: var(--leading-normal); }
.select { appearance: none; padding-right: var(--space-8); cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23949494' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right var(--space-2-5) center; background-size: 14px; }
.input-group { position: relative; }
.input-group > .icon { position: absolute; left: var(--space-2-5); top: 50%; translate: 0 -50%; color: var(--text-muted); pointer-events: none; }
.input-group > .input { padding-left: var(--space-8); }
.input-group > .kbd { position: absolute; right: var(--space-2); top: 50%; translate: 0 -50%; pointer-events: none; }

.check { display: inline-flex; align-items: center; gap: var(--space-2-5); font-size: var(--text-base); color: var(--text-primary); cursor: pointer; }
.check input { appearance: none; flex: none; width: 16px; height: 16px; margin: 0; border: 1px solid var(--border-hover); border-radius: var(--radius-xs); background: var(--surface-raised); display: grid; place-items: center; cursor: pointer; transition: background-color var(--duration-fast), border-color var(--duration-fast); }
.check input[type="radio"] { border-radius: var(--radius-full); }
.check input:checked { background: var(--accent); border-color: var(--accent); }
.check input:checked::after { content: ""; width: 8px; height: 5px; border: solid var(--on-accent); border-width: 0 0 2px 2px; rotate: -45deg; translate: 0 -1px; }
.check input[type="radio"]:checked::after { width: 6px; height: 6px; border: 0; border-radius: 50%; background: var(--on-accent); rotate: 0deg; translate: 0 0; }
.check input:disabled { opacity: .4; cursor: not-allowed; }

.switch { position: relative; display: inline-block; flex: none; width: 34px; height: 20px; }
.switch input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.switch__track { position: absolute; inset: 0; border: 1px solid var(--border-hover); border-radius: var(--radius-full); background: var(--surface-raised); pointer-events: none; transition: background-color var(--duration-base) var(--ease-soft), border-color var(--duration-base); }
.switch__track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--text-secondary); transition: transform var(--duration-base) var(--ease-soft), background-color var(--duration-base); }
.switch input:checked + .switch__track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .switch__track::after { transform: translateX(14px); background: var(--on-accent); }
.switch input:focus-visible + .switch__track { outline: 2px solid var(--focus-ring); outline-offset: 2px; }
.switch input:disabled { cursor: not-allowed; } .switch input:disabled + .switch__track { opacity: .4; }

input[type="range"].range { width: 100%; accent-color: var(--accent); }

/* ── Cards ─────────────────────────────────────────────────────────────── */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); overflow: hidden; transition: border-color var(--duration-slow); }
.card--interactive:hover { border-color: var(--border-hover); }
.card__header { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3-5) var(--card-pad); border-bottom: 1px solid var(--border-subtle); }
.card__title { font-size: var(--text-base); font-weight: var(--weight-medium); letter-spacing: var(--tracking-snug); }
.card__desc { margin-top: var(--space-0-5); font-size: var(--text-sm); color: var(--text-muted); }
.card__body { padding: var(--card-pad); }
.card__footer { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-3) var(--card-pad); border-top: 1px solid var(--border-subtle); background: var(--bg); }
.stat__label { font: var(--weight-regular) var(--text-2xs)/1 var(--font-mono); text-transform: uppercase; letter-spacing: .1em; color: var(--text-muted); }
.stat__value { margin-top: var(--space-2); font-size: var(--text-3xl); font-weight: var(--weight-semibold); letter-spacing: var(--tracking-tight-heading); font-variant-numeric: tabular-nums; }
.stat__delta { margin-left: var(--space-2); font: var(--weight-medium) var(--text-xs)/1 var(--font-mono); }
.stat__delta--up { color: var(--success); } .stat__delta--down { color: var(--danger); }

/* ── Alerts ────────────────────────────────────────────────────────────── */
.alert { display: flex; gap: var(--space-3); padding: var(--space-3) var(--space-4); border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); font-size: var(--text-base); }
.alert > .icon { margin-top: 2px; color: var(--text-secondary); }
.alert__title { font-weight: var(--weight-medium); }
.alert__text { margin-top: var(--space-0-5); color: var(--text-secondary); font-size: var(--text-sm); }
.alert--info    { background: var(--info-subtle);    border-color: var(--info-border); }    .alert--info > .icon    { color: var(--info); }
.alert--success { background: var(--success-subtle); border-color: var(--success-border); } .alert--success > .icon { color: var(--success); }
.alert--warning { background: var(--warning-subtle); border-color: var(--warning-border); } .alert--warning > .icon { color: var(--warning); }
.alert--danger  { background: var(--danger-subtle);  border-color: var(--danger-border); }  .alert--danger > .icon  { color: var(--danger); }

/* ── Tabs (segmented underline) ────────────────────────────────────────── */
.tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--border); }
.tabs__tab { height: var(--control-h-md); padding: 0 var(--space-3); margin-bottom: -1px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--text-muted); font-size: var(--text-base); font-weight: var(--weight-medium); cursor: pointer; transition: color var(--duration-base), border-color var(--duration-base); }
.tabs__tab:hover { color: var(--text-primary); }
.tabs__tab[aria-selected="true"] { color: var(--text-primary); border-bottom-color: var(--accent); }
.segmented { display: inline-flex; gap: var(--space-0-5); padding: var(--space-0-5); border: 1px solid var(--border); border-radius: var(--radius-control); background: var(--bg); }
.segmented__item { height: calc(var(--control-h-sm) - 2px); padding: 0 var(--space-3); border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-muted); font-size: var(--text-sm); font-weight: var(--weight-medium); cursor: pointer; transition: all var(--duration-base); }
.segmented__item:hover { color: var(--text-primary); }
.segmented__item[aria-pressed="true"], .segmented__item[aria-selected="true"] { background: var(--surface-raised); color: var(--text-primary); box-shadow: var(--shadow-subtle); }

/* ── Breadcrumb / pagination ───────────────────────────────────────────── */
.breadcrumb { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-base); color: var(--text-muted); line-height: 1; }
.breadcrumb a { text-decoration: none; color: var(--text-secondary); } .breadcrumb a:hover { color: var(--text-primary); }
.breadcrumb [aria-current="page"] { color: var(--text-primary); font-weight: var(--weight-medium); }
.breadcrumb__sep { color: var(--text-subtle); }
.pagination { display: inline-flex; align-items: center; gap: var(--space-1); }
.pagination .btn[aria-current="page"] { background: var(--surface-raised); border-color: var(--border); }

/* ── Table ─────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); }
.table { width: 100%; border-collapse: collapse; font-size: var(--text-base); }
.table th { height: 36px; padding: 0 var(--space-4); text-align: left; background: var(--bg); border-bottom: 1px solid var(--border); font: var(--weight-regular) var(--text-2xs)/1 var(--font-mono); text-transform: uppercase; letter-spacing: .08em; color: var(--text-muted); white-space: nowrap; }
.table td { height: 44px; padding: 0 var(--space-4); border-bottom: 1px solid var(--border-subtle); color: var(--text-secondary); white-space: nowrap; }
.table td:first-child { color: var(--text-primary); font-weight: var(--weight-medium); }
.table tbody tr { transition: background-color var(--duration-fast); }
.table tbody tr:hover { background: var(--accent-subtle); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--font-mono); }

/* ── Menus, popovers, tooltips ─────────────────────────────────────────── */
.menu-wrap { position: relative; display: inline-block; }
.menu { position: absolute; z-index: var(--z-popover); top: calc(100% + var(--space-2)); left: 0; min-width: 190px; display: grid; gap: var(--space-0-5); padding: var(--space-1); border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); box-shadow: var(--shadow-elevated); animation: fade-in .12s var(--ease-soft); }
.menu--end { left: auto; right: 0; }
.menu__item { display: flex; align-items: center; gap: var(--space-2-5); height: var(--control-h-md); padding: 0 var(--space-2-5); border: 0; border-radius: var(--radius-md); background: transparent; color: var(--text-secondary); font-size: var(--text-base); text-align: left; cursor: pointer; }
.menu__item:hover { background: var(--surface-raised); color: var(--text-primary); }
.menu__item--danger { color: var(--danger); } .menu__item--danger:hover { background: var(--danger-subtle); color: var(--danger); }
.menu__item .kbd { margin-left: auto; }
.menu__sep { height: 1px; margin: var(--space-1) calc(var(--space-1) * -1); background: var(--border-subtle); }
.tooltip { position: relative; }
.tooltip::after { content: attr(data-tip); position: absolute; z-index: var(--z-popover); bottom: calc(100% + 6px); left: 50%; translate: -50% 4px; padding: var(--space-1-5) var(--space-2-5); border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface-raised); color: var(--text-primary); box-shadow: var(--shadow-elevated); font-size: var(--text-xs); white-space: nowrap; opacity: 0; pointer-events: none; transition: opacity var(--duration-fast), translate var(--duration-fast) var(--ease-soft); }
.tooltip:hover::after, .tooltip:focus-visible::after { opacity: 1; translate: -50% 0; }

/* ── Feedback: progress, spinner, skeleton, empty, toast ───────────────── */
.progress { height: 6px; border-radius: var(--radius-full); background: var(--surface-raised); overflow: hidden; }
.progress__bar { height: 100%; width: var(--value, 0%); border-radius: inherit; background: var(--accent); transition: width var(--duration-slower) var(--ease-soft); }
.progress--success .progress__bar { background: var(--success); } .progress--warning .progress__bar { background: var(--warning); } .progress--danger .progress__bar { background: var(--danger); }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border-hover); border-top-color: var(--text-primary); border-radius: 50%; animation: spin .7s linear infinite; }
.skeleton { display: block; height: 10px; border-radius: var(--radius-full); background: linear-gradient(90deg, var(--surface-raised), var(--surface-hover), var(--surface-raised)); background-size: 200% 100%; animation: shimmer 1.6s ease-in-out infinite; }
.empty { display: flex; flex-direction: column; align-items: center; gap: var(--space-2); padding: var(--space-10) var(--space-6); text-align: center; }
.empty__icon { display: grid; place-items: center; width: 40px; height: 40px; margin-bottom: var(--space-1); border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface-raised); color: var(--text-secondary); }
.empty__title { font-size: var(--text-md); font-weight: var(--weight-medium); }
.empty__text { max-width: 320px; font-size: var(--text-sm); color: var(--text-muted); }
.empty .btn { margin-top: var(--space-2); }
.toast-region { position: fixed; z-index: var(--z-toast); right: var(--space-4); bottom: var(--space-4); display: grid; gap: var(--space-2); width: min(360px, calc(100vw - var(--space-8))); pointer-events: none; }
.toast { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3) var(--space-4); border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface-raised); box-shadow: var(--shadow-elevated); font-size: var(--text-base); pointer-events: auto; animation: toast-in .25s var(--ease-soft); }
.toast .icon { color: var(--success); }

/* ── Dialog (native <dialog>) ──────────────────────────────────────────── */
.dialog { position: fixed; inset: 0; margin: auto; width: calc(100% - var(--space-8)); max-width: 460px; max-height: calc(100dvh - var(--space-12)); padding: 0; overflow: hidden; border: 1px solid var(--border); border-radius: var(--radius-overlay); background: var(--surface); color: var(--text-primary); box-shadow: var(--shadow-modal); }
.dialog[open] { animation: dialog-in var(--duration-slow) var(--ease-soft) both; }
.dialog::backdrop { background: var(--overlay); backdrop-filter: blur(var(--blur-sm)); }
.dialog__inner { display: flex; flex-direction: column; max-height: inherit; }
.dialog__header { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--border-subtle); }
.dialog__title { font-size: var(--text-md); font-weight: var(--weight-medium); letter-spacing: var(--tracking-snug); }
.dialog__body { padding: var(--space-5); overflow-y: auto; }
.dialog__footer { display: flex; justify-content: flex-end; gap: var(--space-2); padding: var(--space-3) var(--space-5); border-top: 1px solid var(--border-subtle); background: var(--bg); }

@keyframes spin { to { transform: rotate(360deg) } }
@keyframes fade-in { from { opacity: 0 } to { opacity: 1 } }
@keyframes pulse-subtle { 0%,100% { opacity: 1 } 50% { opacity: .6 } }
@keyframes shimmer { from { background-position: 200% 0 } to { background-position: -200% 0 } }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px) scale(.98) } to { opacity: 1; transform: none } }
@keyframes dialog-in { from { opacity: 0; transform: translateY(6px) scale(.98) } to { opacity: 1; transform: none } }
}
