/* Component library (design 4). Every rule reads tokens only. Documented with live examples on #/styleguide.
   Old class names (.badge, .pill, .tabs, .grid, .tile, .toast, .banner, .chip, .field, .btn) are kept and restyled here, so no view changes. */

/* ---------- Controls: inputs, select, textarea ---------- */
input, select, textarea, button, .btn { font: inherit; font-size: var(--fs-sm); color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-md); padding: .5rem .75rem; min-height: var(--ctl-h); }
input:not([type="checkbox"]):not([type="radio"]):not([type="file"]), select, textarea { box-shadow: var(--sh-1); }
input:hover:not(:disabled), select:hover:not(:disabled), textarea:hover:not(:disabled) { border-color: var(--text-2); }
input:focus-visible, select:focus-visible, textarea:focus-visible { border-color: var(--focus); }
input[type="file"] { padding: .4rem .5rem; max-width: 100%; }
input[type="file"]::file-selector-button { font: inherit; font-size: var(--fs-sm); margin-right: var(--sp-3); padding: .3rem .75rem; border: 1px solid var(--border-strong); border-radius: var(--r-sm); background: var(--surface-2); color: var(--text); cursor: pointer; }
textarea { width: 100%; min-height: 6rem; line-height: var(--lh-body); resize: vertical; }
select { max-width: 100%; contain: paint; height: max(var(--ctl-h), 2.5em); padding-right: 2rem; }
input[type="search"] { -webkit-appearance: none; appearance: none; }
:disabled, [aria-disabled="true"] { opacity: .55; cursor: not-allowed; }
[aria-invalid="true"] { border-color: var(--err); box-shadow: 0 0 0 1px var(--err); }
input[type="checkbox"], input[type="radio"] { min-height: 0; width: 1.25rem; height: 1.25rem; padding: 0; accent-color: var(--accent); flex: none; }
input[type="checkbox"], input[type="radio"] { box-shadow: none; }
input[type="checkbox"]:not(.switch) { min-height: 24px; width: 24px; height: 24px; }
/* switch: <input type="checkbox" role="switch" class="switch">; the knob moves with transform only */
input.switch { appearance: none; -webkit-appearance: none; position: relative; width: 2.5rem; height: 1.5rem; min-height: 0; border-radius: var(--r-full); background: var(--neutral-soft); border: 1px solid var(--border-strong); cursor: pointer; padding: 0; }
input.switch::before { content: ""; position: absolute; top: 2px; left: 2px; width: calc(1.5rem - 8px); height: calc(1.5rem - 8px); border-radius: 50%; background: var(--text-2); transition: transform var(--dur-2) var(--ease-out); }
input.switch:checked { background: var(--accent); border-color: var(--accent); }
input.switch:checked::before { transform: translateX(1rem); background: var(--accent-fg); }

/* ---------- Field: label, hint, inline error ---------- */
label { font-weight: var(--fw-medium); font-size: var(--fs-sm); color: var(--text); }
.field { display: flex; flex-direction: column; gap: var(--sp-1); margin-bottom: var(--sp-4); }
.field.check { flex-direction: row; align-items: center; gap: var(--sp-2); }
.field.check label { font-weight: var(--fw-regular); }
.field .hint, .hint { color: var(--text-2); font-size: var(--fs-sm); margin: 0; }
.field .error, .field-error { color: var(--err); font-size: var(--fs-sm); font-weight: var(--fw-medium); margin: 0; display: flex; gap: var(--sp-1); align-items: flex-start; }
fieldset { border: 1px solid var(--border); border-radius: var(--r-md); margin: 0 0 var(--sp-4); padding: var(--sp-3) var(--sp-4) var(--sp-2); background: var(--surface); }
legend { font-weight: var(--fw-semibold); font-size: var(--fs-sm); padding: 0 var(--sp-2); }

/* ---------- Button: primary (.btn), secondary (default <button>), ghost, danger, sm, icon, loading ---------- */
button, .btn { cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2); font-weight: var(--fw-medium); text-decoration: none; line-height: 1.2; background: var(--surface); box-shadow: var(--sh-1); }
button:hover:not(:disabled), .btn.secondary:hover { background: var(--surface-2); border-color: var(--text-2); }
button:active:not(:disabled) { background: var(--surface-sunken); }
.btn, .btn-primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
a.btn:hover, button.btn:hover:not(:disabled), .btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--accent-fg); }
button.btn:active:not(:disabled) { background: var(--accent-hover); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-ghost, .btn.ghost { background: transparent; border-color: transparent; box-shadow: none; color: var(--text); }
.btn-ghost:hover:not(:disabled), .btn.ghost:hover:not(:disabled) { background: var(--surface-2); border-color: transparent; color: var(--text); }
.btn-danger, .btn.danger { background: var(--err); border-color: var(--err); color: var(--surface); }
.btn-danger:hover:not(:disabled), .btn.danger:hover:not(:disabled) { background: var(--err); border-color: var(--err); filter: brightness(.92); color: var(--surface); }
.btn-sm, .btn.sm { min-height: 2.25rem; padding: .25rem .6rem; font-size: var(--fs-xs); }
.btn-icon, .btn.icon { width: var(--ctl-h); padding: 0; }
.btn-icon .ic, .btn.icon .ic { width: 1.25rem; height: 1.25rem; }
button[aria-busy="true"], .btn.secondary[aria-busy="true"], .btn.loading { position: relative; color: transparent; pointer-events: none; }
button[aria-busy="true"]::after, .btn.loading::after { content: ""; position: absolute; width: 1rem; height: 1rem; border: 2px solid var(--accent-fg); border-top-color: transparent; border-radius: 50%; animation: pc-spin 700ms linear infinite; }
.btn.secondary[aria-busy="true"]::after { border-color: var(--text); border-top-color: transparent; }
@keyframes pc-spin { to { transform: rotate(360deg); } }

/* ---------- Card ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); box-shadow: var(--sh-1); padding: var(--sp-4) var(--sp-5); }
.card > :first-child { margin-top: 0; } .card > :last-child { margin-bottom: 0; }
.card h2, .card h3 { margin-top: 0; }
.card.flat { box-shadow: none; }
.card.interactive { }
.card.interactive:hover { box-shadow: var(--sh-2); border-color: var(--border-strong); }

/* ---------- Tabs (links or role=tab buttons) ---------- */
.tabs { display: flex; flex-wrap: wrap; gap: var(--sp-1); margin: var(--sp-2) 0 var(--sp-4); border-bottom: 1px solid var(--border); }
.tabs a, .tabs [role="tab"] { display: inline-flex; align-items: center; gap: var(--sp-2); min-height: var(--hit); padding: .5rem var(--sp-3); margin-bottom: -1px; border: 0; border-bottom: 2px solid transparent; border-radius: var(--r-sm) var(--r-sm) 0 0; background: transparent; box-shadow: none; color: var(--text-2); text-decoration: none; font: inherit; font-size: var(--fs-sm); font-weight: var(--fw-medium); cursor: pointer; }
.tabs a:hover, .tabs [role="tab"]:hover { color: var(--text); background: var(--surface-2); }
.tabs a[aria-current="page"], .tabs [role="tab"][aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); font-weight: var(--fw-semibold); background: transparent; }
.tabs .count { font-size: var(--fs-xs); background: var(--neutral-soft); color: var(--text-2); border-radius: var(--r-full); padding: 0 .45rem; font-variant-numeric: tabular-nums; }

/* ---------- Segmented control (buttons with aria-pressed, or radios) ---------- */
.segmented { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-sunken); border: 1px solid var(--border); border-radius: var(--r-md); }
.segmented button { min-height: calc(var(--ctl-h) - 8px); border: 0; box-shadow: none; background: transparent; color: var(--text-2); border-radius: var(--r-sm); padding: .25rem var(--sp-3); }
.segmented button:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--sh-1), 0 0 0 1px var(--border-strong); font-weight: var(--fw-semibold); }

/* ---------- Badge, evidence chip, status chip ---------- */
.badge { display: inline-flex; align-items: center; gap: .3em; padding: .05rem .5rem; border: 1px solid var(--border); border-radius: var(--r-full); font-size: var(--fs-xs); font-weight: var(--fw-medium); line-height: 1.5; background: var(--neutral-soft); color: var(--text-2); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.err { background: var(--err-soft); color: var(--err); border-color: transparent; }
.badge.info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.badge.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.mock { border-style: dashed; border-color: var(--warn); color: var(--warn); background: var(--warn-soft); font-weight: var(--fw-semibold); }
.badge.yours { border-width: 2px; font-weight: var(--fw-bold); }
.badge.sourced { border-style: dashed; }
/* evidence chip: the exact word (READ, MOCK, pcvf ...) stays in the DOM; it is shown quietly, in the mono face */
.badge.ev, .ev-chip { font-family: var(--font-mono); font-size: var(--fs-xs); letter-spacing: .02em; background: transparent; color: var(--text-2); border-color: var(--border-strong); cursor: help; }
.pill { display: inline-flex; align-items: center; gap: .4rem; min-height: var(--chip-h); padding: 0 .75rem; border: 1px solid var(--border-strong); border-radius: var(--r-full); font-size: var(--fs-sm); font-weight: var(--fw-semibold); text-decoration: none; color: var(--text); background: var(--surface); }
.pill:hover { background: var(--surface-2); color: var(--text); }
.pill.connected { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.pill.offline { border-color: var(--warn); border-style: dashed; color: var(--warn); background: var(--warn-soft); }
.pill svg { fill: currentColor; stroke: currentColor; stroke-width: 1.5; }
.pill svg .hollow { fill: none; }
.chip { display: inline-flex; align-items: center; gap: var(--sp-1); border: 1px solid var(--border-strong); border-radius: var(--r-full); padding: .15rem .8rem; background: var(--surface); color: var(--text); font: inherit; font-size: var(--fs-sm); cursor: pointer; min-height: 2rem; box-shadow: none; }
.chip:hover:not(:disabled) { background: var(--surface-2); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); border-width: 2px; font-weight: var(--fw-semibold); }
.chip[aria-pressed="true"]::before { content: "\2713"; font-weight: var(--fw-bold); }

/* ---------- Table ---------- */
.grid, .table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.grid th, .table th { text-align: left; font-size: var(--fs-xs); font-weight: var(--fw-semibold); letter-spacing: .04em; text-transform: uppercase; color: var(--text-2); background: var(--surface-2); padding: var(--sp-2) var(--sp-3); border-bottom: 1px solid var(--border-strong); }
.grid td, .table td { padding: var(--sp-2) var(--sp-3); border-bottom: 1px solid var(--border); vertical-align: middle; background: var(--surface); }
.grid tbody tr:hover td, .table tbody tr:hover td { background: var(--surface-2); }
.grid tbody tr[aria-selected="true"] td, .table tbody tr[aria-selected="true"] td { background: var(--accent-soft); }
.grid th:first-child, .table th:first-child { border-top-left-radius: var(--r-md); }
.grid th:last-child, .table th:last-child { border-top-right-radius: var(--r-md); }
.grid caption, .table caption { text-align: left; }
.grid .r, .table .r, td.num, th.num { text-align: right; }
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--r-md); background: var(--surface); }
.table-wrap .table th { position: sticky; top: 0; z-index: 1; }
.grid.sticky th { position: sticky; top: var(--topbar-h); z-index: var(--z-sticky); }
.grid th button.sort, .table th button.sort { border: 0; background: none; box-shadow: none; font: inherit; font-weight: var(--fw-semibold); text-transform: inherit; letter-spacing: inherit; color: inherit; padding: .25rem; gap: var(--sp-1); }
.grid th[aria-sort] button.sort, .table th[aria-sort] { color: var(--text); }
.table.dense td, .table.dense th { padding: .15rem var(--sp-2); }

/* ---------- Tooltip and popover ---------- */
.tip { position: relative; display: inline-flex; }
.tip > [role="tooltip"] { position: absolute; z-index: var(--z-popover); left: 50%; bottom: calc(100% + 6px); transform: translateX(-50%); width: max-content; max-width: 16rem; padding: .35rem .6rem; border-radius: var(--r-sm); background: var(--text); color: var(--bg); font-size: var(--fs-xs); line-height: 1.4; box-shadow: var(--sh-2); opacity: 0; visibility: hidden; pointer-events: none; }
.tip:hover > [role="tooltip"], .tip:focus-within > [role="tooltip"] { opacity: 1; visibility: visible; }
.popover { position: absolute; z-index: var(--z-popover); min-width: 12rem; max-width: min(22rem, 92vw); padding: var(--sp-3) var(--sp-4); background: var(--surface); color: var(--text); border: 1px solid var(--border-strong); border-radius: var(--r-lg); box-shadow: var(--sh-3); font-size: var(--fs-sm); }
.popover h4 { margin: 0 0 var(--sp-1); font-size: var(--fs-sm); }
.popover p { margin: 0; color: var(--text-2); }

/* ---------- Menu ---------- */
.menu { list-style: none; margin: 0; padding: var(--sp-1); min-width: 12rem; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-lg); box-shadow: var(--sh-3); }
.menu [role="menuitem"], .menu a, .menu button { display: flex; align-items: center; gap: var(--sp-2); width: 100%; min-height: var(--hit); padding: .4rem var(--sp-3); border: 0; border-radius: var(--r-sm); background: transparent; box-shadow: none; color: var(--text); text-decoration: none; text-align: left; font-size: var(--fs-sm); justify-content: flex-start; cursor: pointer; }
.menu [role="menuitem"]:hover, .menu a:hover, .menu button:hover:not(:disabled), .menu [role="menuitem"][aria-selected="true"] { background: var(--surface-2); }
.menu .sep { height: 1px; margin: var(--sp-1) 0; background: var(--border); }
.menu .kbd-hint { margin-left: auto; }

/* ---------- Dialog (native <dialog>; bottom sheet on a phone) ---------- */
dialog.dlg { width: min(32rem, calc(100vw - 2rem)); max-height: calc(100vh - 2rem); padding: 0; border: 1px solid var(--border-strong); border-radius: var(--r-lg); background: var(--surface); color: var(--text); box-shadow: var(--sh-3); overflow: auto; }
dialog.dlg::backdrop { background: rgba(10, 14, 20, .55); }
dialog.dlg[open] { animation: pc-pop var(--dur-3) var(--ease-out); }
.dlg-head { padding: var(--sp-4) var(--sp-5) 0; } .dlg-head h2 { margin: 0; font-size: var(--fs-lg); }
.dlg-body { padding: var(--sp-3) var(--sp-5); color: var(--text-2); }
.dlg-foot { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: var(--sp-2); padding: var(--sp-3) var(--sp-5) var(--sp-4); border-top: 1px solid var(--border); background: var(--surface-2); }
@keyframes pc-pop { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
@media (max-width: 600px) {
  dialog.dlg { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: var(--r-lg) var(--r-lg) 0 0; border-bottom: 0; max-height: 85vh; }
  dialog.dlg[open] { animation-name: pc-sheet; }
  .dlg-foot { flex-direction: column-reverse; } .dlg-foot > * { width: 100%; }
}
@keyframes pc-sheet { from { transform: translateY(100%); } to { transform: none; } }

/* ---------- Toast (stack: #undo-stack in app.css) ---------- */
.toast { background: var(--text); color: var(--bg); border: 0; border-radius: var(--r-md); padding: var(--sp-3) var(--sp-4); box-shadow: var(--sh-3); z-index: var(--z-toast); font-size: var(--fs-sm); animation: pc-toast var(--dur-3) var(--ease-out); }
.toast p { margin: 0 0 var(--sp-2); }
.toast p:last-child { margin: 0; }
.toast button { margin-right: var(--sp-2); min-height: 2.25rem; background: transparent; color: inherit; border-color: currentColor; box-shadow: none; }
.toast button:hover:not(:disabled) { background: rgba(127, 127, 127, .25); color: inherit; border-color: currentColor; }
.toast a { color: inherit; }
.toast :focus-visible { outline-color: var(--bg); box-shadow: 0 0 0 4px var(--text); }
@keyframes pc-toast { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Banner ---------- */
.banner { margin: 0 0 var(--sp-4); padding: var(--sp-3) var(--sp-4); border: 1px solid var(--border); border-left: 4px solid var(--warn); border-radius: var(--r-md); background: var(--warn-soft); color: var(--text); font-size: var(--fs-sm); }
.banner p { margin: var(--sp-1) 0 0; }
.banner.info { border-left-color: var(--info); background: var(--info-soft); }
.banner.ok { border-left-color: var(--ok); background: var(--ok-soft); }
.banner.err { border-left-color: var(--err); background: var(--err-soft); }
.banner .ic { margin-right: var(--sp-2); }

/* ---------- Empty state ---------- */
.empty, .no-matches { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--sp-2); padding: var(--sp-6) var(--sp-5); border: 1px dashed var(--border-strong); border-radius: var(--r-lg); background: var(--surface); margin: var(--sp-4) 0; }
.empty .ic { width: 2.5rem; height: 2.5rem; color: var(--text-3); }
.empty .btn .ic { width: 1.1rem; height: 1.1rem; color: inherit; } /* the 2.5rem muted icon rule above is for the illustration, not for a button inside it (S9c: the Start a run plus was grey and large) */
.empty h2, .empty h3, .no-matches h2 { margin: 0; font-size: var(--fs-lg); }
.empty p, .no-matches p { margin: 0; color: var(--text-2); max-width: 32rem; }
.no-matches { align-items: flex-start; text-align: left; }

/* ---------- Skeleton and progress ---------- */
.skeleton { display: block; height: 1rem; border-radius: var(--r-sm); background: var(--surface-sunken); animation: pc-pulse 1.4s ease-in-out infinite; }
.skeleton.line + .skeleton.line { margin-top: var(--sp-2); }
.skeleton.title { height: 1.75rem; width: 40%; } .skeleton.block { height: 5rem; border-radius: var(--r-md); }
@keyframes pc-pulse { 50% { opacity: .45; } }
progress { -webkit-appearance: none; appearance: none; height: .5rem; min-height: 0; padding: 0; border: 0; border-radius: var(--r-full); background: var(--surface-sunken); box-shadow: none; overflow: hidden; width: 100%; accent-color: var(--accent); }
progress::-webkit-progress-bar { background: var(--surface-sunken); }
progress::-webkit-progress-value { background: var(--accent); border-radius: var(--r-full); }
progress::-moz-progress-bar { background: var(--accent); border-radius: var(--r-full); }

/* ---------- Stepper and breadcrumb ---------- */
.stepper { display: flex; gap: var(--sp-2); list-style: none; margin: 0 0 var(--sp-4); padding: 0; counter-reset: step; }
.stepper li { flex: 1; display: flex; align-items: center; gap: var(--sp-2); font-size: var(--fs-sm); color: var(--text-2); counter-increment: step; padding-top: var(--sp-2); border-top: 3px solid var(--border-strong); }
.stepper li::before { content: counter(step); display: inline-grid; place-items: center; width: 1.5rem; height: 1.5rem; border-radius: 50%; border: 1px solid var(--border-strong); font-size: var(--fs-xs); font-weight: var(--fw-semibold); flex: none; }
.stepper li[aria-current="step"] { color: var(--text); font-weight: var(--fw-semibold); border-top-color: var(--accent); }
.stepper li[aria-current="step"]::before { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.stepper li.done { color: var(--text); border-top-color: var(--ok); }
.stepper li.done::before { content: "\2713"; background: var(--ok-soft); color: var(--ok); border-color: var(--ok); }
.crumbs ol { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-1); list-style: none; margin: 0 0 var(--sp-2); padding: 0; font-size: var(--fs-sm); color: var(--text-2); }
.crumbs li + li::before { content: "/"; margin-right: var(--sp-1); color: var(--text-3); }
.crumbs a { color: var(--text-2); text-decoration: none; } .crumbs a:hover { color: var(--text); text-decoration: underline; }
.crumbs [aria-current="page"] { color: var(--text); font-weight: var(--fw-medium); }

/* ---------- Keyboard hint ---------- */
kbd { display: inline-block; min-width: 1.5em; padding: .05em .4em; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: var(--r-sm); background: var(--surface-2); color: var(--text); font-size: .8em; text-align: center; line-height: 1.4; }

/* ---------- Tile (dashboard counters) ---------- */
.tiles { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr)); gap: var(--sp-3); }
.tile { border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--sp-4); background: var(--surface); box-shadow: var(--sh-1); display: flex; flex-direction: column; gap: var(--sp-1); }
.tile:hover { box-shadow: var(--sh-2); border-color: var(--border-strong); }
.tile a { text-decoration: none; display: flex; flex-direction: column; min-height: 44px; color: var(--text); gap: var(--sp-1); }
.tile .num { font-size: var(--fs-2xl); font-weight: var(--fw-bold); line-height: 1.1; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.tile .lbl { color: var(--text-2); font-size: var(--fs-sm); font-weight: var(--fw-medium); }
.tile a:hover .lbl { color: var(--accent); text-decoration: underline; }
.tile .badge { align-self: flex-start; }

/* ---------- Toolbar and action rows ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: var(--sp-3); align-items: end; margin-bottom: var(--sp-4); }
.actions { display: flex; flex-wrap: wrap; gap: var(--sp-2); margin: var(--sp-4) 0; align-items: center; }
