/* Norsava shared components: buttons, fields, selects, segmented controls, switches, chips, badges, pills,
   cards, KPI tiles, tables, tabs, toasts, dialogs, popovers, tooltips, skeletons, empty states.
   Built only on /ds/tokens.css. Flat: 1 px lines, radius 12 for cards and 8 for controls, no gradients,
   no glows; --shadow-layer only on layers (menus, popovers, dialogs, toasts, tooltips).
   Red only on .btn--primary, always with a white 19 px 800 label or an icon alone. */

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; scroll-padding-top: calc(var(--pb-h, 0px) + 16px); }
body { margin: 0; background: var(--page); color: var(--text); font: 500 var(--fs-base)/var(--lh) var(--font); -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }
h1, h2, h3, h4, p, figure, blockquote { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }
button, input, select, textarea { font: inherit; color: inherit; letter-spacing: inherit; }
button { cursor: pointer; }
a { color: inherit; }
img, svg { display: block; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
::selection { background: var(--info); color: var(--page); }
.vh { position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.mono { font-variant-numeric: tabular-nums; }
.skip { position: fixed; left: 16px; top: 12px; z-index: 100; padding: 10px 14px; background: var(--panel); color: var(--text); border: 1px solid var(--line-strong); border-radius: var(--r-md); font-weight: 700; text-decoration: none; transform: translateY(-200%); transition: transform var(--t-fast) var(--ease); }
.skip:focus { transform: none; }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: var(--h-control); padding: 0 16px; border: 1px solid transparent; border-radius: var(--r-md); background: none; color: var(--text); font-size: var(--fs-md); font-weight: 700; line-height: 1; text-decoration: none; white-space: nowrap; user-select: none; transition: background-color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), color var(--t-fast) var(--ease); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn--primary { height: var(--h-control-lg); padding: 0 24px; background: var(--red); color: var(--on-red); font-size: 19px; font-weight: 800; letter-spacing: -.005em; }
.btn--primary:hover:not(:disabled) { background: var(--red-hover); }
.btn--secondary { border-color: var(--line-strong); }
.btn--secondary:hover:not(:disabled) { background: var(--panel-2); }
.btn--ghost { color: var(--muted); }
.btn--ghost:hover:not(:disabled) { color: var(--text); background: var(--panel-2); }
.btn--icon { width: var(--h-control); padding: 0; }
.btn--sm { height: 32px; padding: 0 10px; gap: 6px; font-size: var(--fs-sm); }
.btn--sm.btn--icon { width: 32px; padding: 0; }
.btn--block { width: 100%; }
.btn--rev { flex-direction: row-reverse; }
.btn--strong { border-color: var(--text); }
.btn[aria-pressed="true"] { background: var(--panel-2); color: var(--text); border-color: var(--line-strong); }
.btn.is-on { border-color: var(--text); }

/* ---------- fields ---------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-l { font-size: var(--fs-sm); font-weight: 700; }
.field-hint { font-size: var(--fs-sm); color: var(--muted); }
.field-err { display: flex; align-items: flex-start; gap: 6px; font-size: var(--fs-sm); font-weight: 600; color: var(--error); }
.field-err svg { margin-top: 2px; }
.input { width: 100%; height: var(--h-control); padding: 0 12px; background: var(--field); color: var(--text); border: 1px solid var(--line-strong); border-radius: var(--r-md); font-size: var(--fs-base); font-weight: 500; transition: border-color var(--t-fast) var(--ease); }
.input::placeholder { color: var(--muted); opacity: 1; }
.input:hover { border-color: var(--muted); }
.input:focus-visible { border-color: var(--text); }
.input[aria-invalid="true"] { border-color: var(--error); }
.input:disabled { opacity: .6; cursor: not-allowed; }
input[type="date"].input { padding-right: 8px; }
.input-wrap { position: relative; }
.input-wrap .input { padding-left: 40px; }
.input-ic { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.select { position: relative; }
.select-i { appearance: none; -webkit-appearance: none; padding-right: 36px; cursor: pointer; font-size: var(--fs-md); font-weight: 600; }
.select-i option { background: var(--panel); color: var(--text); }
.select-ic { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }

/* Segmented control. The picked option is inverted, so it reads at a glance in both themes. */
.seg { display: inline-flex; align-items: center; gap: 2px; padding: 3px; background: var(--field); border: 1px solid var(--line-strong); border-radius: var(--r-md); }
.seg-b { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 32px; padding: 0 12px; border: 0; border-radius: var(--r-sm); background: none; color: var(--muted); font-size: var(--fs-sm); font-weight: 700; white-space: nowrap; transition: background-color var(--t-fast) var(--ease), color var(--t-fast) var(--ease); }
.seg-b:hover { color: var(--text); }
.seg-b[aria-checked="true"] { background: var(--text); color: var(--page); }
.seg-b:focus-visible { outline-offset: 1px; }
.seg--sm .seg-b { height: 26px; padding: 0 10px; }
.seg--block { display: flex; }
.seg--block .seg-b { flex: 1; padding: 0 6px; }

/* Switch: the whole label is the target. */
.switch { display: inline-flex; align-items: center; gap: 10px; min-height: var(--h-control); padding: 0 6px 0 2px; border: 0; border-radius: var(--r-md); background: none; color: var(--text); font-size: var(--fs-sm); font-weight: 700; text-align: left; }
.switch-t { position: relative; flex: none; width: 36px; height: 20px; border-radius: var(--r-pill); border: 1px solid var(--line-strong); background: var(--field); transition: background-color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease); }
.switch-k { position: absolute; top: 3px; left: 3px; width: 12px; height: 12px; border-radius: 50%; background: var(--muted); transition: transform var(--t-med) var(--ease), background-color var(--t-fast) var(--ease); }
.switch[aria-checked="true"] .switch-t { background: var(--text); border-color: var(--text); }
.switch[aria-checked="true"] .switch-k { transform: translateX(16px); background: var(--page); }

.check { position: relative; display: inline-flex; align-items: center; gap: 10px; min-height: 24px; font-size: var(--fs-md); font-weight: 600; cursor: pointer; }
.check-i { position: absolute; left: 0; top: 50%; width: 20px; height: 20px; margin: -10px 0 0; opacity: 0; cursor: pointer; }
.check-b { display: grid; place-items: center; flex: none; width: 20px; height: 20px; border: 1px solid var(--line-strong); border-radius: 5px; background: var(--field); color: transparent; transition: background-color var(--t-fast) var(--ease); }
.check-i:checked + .check-b { background: var(--text); border-color: var(--text); color: var(--page); }
.check-i:focus-visible + .check-b { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- chips, badges, pills, tags ---------- */
.chip { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 8px 0 12px; border: 1px solid var(--line-strong); border-radius: var(--r-pill); background: var(--panel); color: var(--text); font-size: var(--fs-sm); font-weight: 700; transition: background-color var(--t-fast) var(--ease); }
.chip svg { color: var(--muted); }
.chip:hover { background: var(--panel-2); }
.chip:hover svg { color: var(--text); }
.badge { display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 7px; border: 1px solid var(--line-strong); border-radius: var(--r-sm); color: var(--muted); font-size: var(--fs-xs); font-weight: 700; line-height: 1; white-space: nowrap; }
.badge--info { color: var(--info); border-color: var(--info); }
.badge--success { color: var(--success); border-color: var(--success); }
.badge--warning { color: var(--warning); border-color: var(--warning); }
.badge--error { color: var(--error); border-color: var(--error); }
.badge--demo { height: 26px; padding: 0 9px; font-size: var(--fs-sm); cursor: help; }
.pill { display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 10px 0 8px; border: 1px solid var(--line); border-radius: var(--r-pill); background: var(--panel-2); color: var(--text); font-size: var(--fs-xs); font-weight: 700; line-height: 1; white-space: nowrap; }
.pill svg { color: var(--muted); }
.pill-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); }
.pill--info { border-color: var(--info); }
.pill--info svg, .pill--info .pill-dot { color: var(--info); background: none; }
.pill--warning { border-color: var(--warning); }
.pill--warning svg { color: var(--warning); }
.pill--success { border-color: var(--success); }
.pill--success svg { color: var(--success); }
.tag { display: inline-flex; align-items: center; height: 20px; padding: 0 5px; border: 1px solid var(--line-strong); border-radius: 4px; color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .06em; line-height: 1; }
.meter { position: relative; display: inline-block; flex: none; width: 64px; height: 6px; border-radius: 3px; background: var(--panel-2); box-shadow: inset 0 0 0 1px var(--line); overflow: hidden; vertical-align: middle; }
.meter-f { position: absolute; inset: 0 auto 0 0; border-radius: 3px; background: var(--c1); }
.rating { display: inline-flex; align-items: center; gap: 3px; color: var(--text); font-size: var(--fs-sm); font-weight: 700; font-variant-numeric: tabular-nums; }
.rating svg { color: var(--warning); fill: currentColor; }

/* ---------- surfaces ---------- */
.card { --pad: 24px; min-width: 0; padding: 20px var(--pad) 24px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-lg); scroll-margin-top: calc(var(--pb-h, 0px) + 16px); }
.card-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 12px 16px; margin-bottom: 18px; }
.card-titles { flex: 1 1 220px; min-width: 0; }
.card-title { font-size: var(--fs-base); font-weight: 800; line-height: 1.35; letter-spacing: -.005em; outline: none; }
.card-desc { margin-top: 2px; color: var(--muted); font-size: var(--fs-sm); }
.card-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

/* KPI tile. Value 32 px 800, tabular. The delta carries an arrow and a sign, never colour alone. */
.kpi { position: relative; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; width: 100%; min-width: 0; padding: 18px 20px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-lg); color: var(--text); text-align: left; }
.kpi--btn { transition: border-color var(--t-fast) var(--ease), background-color var(--t-fast) var(--ease); }
.kpi--btn:hover { border-color: var(--line-strong); background: var(--panel-2); }
.kpi-label { color: var(--muted); font-size: var(--fs-sm); font-weight: 700; }
.kpi--btn .kpi-label { padding-right: 24px; }
.kpi-value { margin-top: 2px; font-size: var(--fs-kpi); font-weight: 800; line-height: 1.15; letter-spacing: -.02em; font-variant-numeric: tabular-nums; white-space: nowrap; }
.kpi-unit { color: var(--muted); font-size: var(--fs-md); font-weight: 700; letter-spacing: 0; }
.kpi-none { color: var(--muted); font-size: var(--fs-lg); font-weight: 700; letter-spacing: 0; }
.kpi-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 8px; margin-top: 2px; font-size: var(--fs-sm); font-weight: 700; }
.kpi-prev { color: var(--muted); font-weight: 600; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--muted); font-size: var(--fs-sm); }
.kpi-go { position: absolute; top: 18px; right: 16px; color: var(--muted); transition: transform var(--t-fast) var(--ease), color var(--t-fast) var(--ease); }
.kpi--btn:hover .kpi-go { color: var(--text); transform: translateX(2px); }
.kpi--skel { gap: 10px; }
.delta { display: inline-flex; align-items: center; gap: 3px; font-variant-numeric: tabular-nums; }
.delta--good { color: var(--success); }
.delta--bad { color: var(--error); }
.delta--neutral { color: var(--text); }
.delta--none { color: var(--muted); font-weight: 600; }

/* Tables bleed to the card edges so row lines run edge to edge. */
.tbl-wrap { margin: 0 calc(-1 * var(--pad, 24px)) -8px; overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: var(--fs-md); }
.tbl th, .tbl td { padding: 11px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.tbl thead th { padding-top: 0; padding-bottom: 10px; color: var(--muted); font-size: var(--fs-xs); font-weight: 700; white-space: nowrap; }
.tbl tbody tr:last-child > * { border-bottom: 0; }
.tbl th:first-child, .tbl td:first-child { padding-left: var(--pad, 24px); }
.tbl th:last-child, .tbl td:last-child { padding-right: var(--pad, 24px); }
.tbl tbody th { font-weight: 700; }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl tr.is-link { cursor: pointer; }
.tbl tr.is-link:hover > * { background: var(--panel-2); }
.tbl-open { padding: 0; border: 0; background: none; color: inherit; font: inherit; font-weight: 700; text-align: left; }
.tbl-open:focus-visible { outline-offset: 4px; border-radius: 2px; }
.th-sort { display: inline-flex; align-items: center; gap: 4px; padding: 0; border: 0; background: none; color: inherit; font: inherit; }
.th-sort svg { opacity: .45; }
.th-sort.is-on, .th-sort:hover { color: var(--text); }
.th-sort.is-on svg { opacity: 1; }
.num .th-sort { flex-direction: row-reverse; }
.tbl-empty { padding: 24px 0; color: var(--muted); font-size: var(--fs-md); text-align: center; }
@media (max-width: 719.98px) { .hide-sm { display: none; } }
@media (max-width: 1099.98px) { .hide-md { display: none; } }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tab { position: relative; height: 40px; padding: 0 12px; border: 0; background: none; color: var(--muted); font-size: var(--fs-md); font-weight: 700; white-space: nowrap; }
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--text); }
.tab[aria-selected="true"]::after { content: ''; position: absolute; left: 8px; right: 8px; bottom: -1px; height: 2px; background: var(--text); }
.tab-n { margin-left: 6px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- layers ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 80; display: flex; flex-direction: column; gap: 8px; width: min(400px, calc(100vw - 32px)); }
.toast { display: flex; align-items: flex-start; gap: 10px; padding: 10px 6px 10px 14px; background: var(--panel); border: 1px solid var(--line-strong); border-radius: var(--r-md); box-shadow: var(--shadow-layer); font-size: var(--fs-md); animation: layer-in var(--t-med) var(--ease); transition: opacity var(--t-fast) var(--ease); }
.toast.is-out { opacity: 0; }
.toast-ic { margin-top: 7px; color: var(--muted); }
.toast--success .toast-ic { color: var(--success); }
.toast--error .toast-ic { color: var(--error); }
.toast-m { flex: 1; padding-top: 5px; }
.dialog { width: min(520px, calc(100vw - 32px)); max-height: calc(100vh - 64px); padding: 0; border: 1px solid var(--line-strong); border-radius: var(--r-lg); background: var(--panel); color: var(--text); box-shadow: var(--shadow-layer); }
.dialog::backdrop { background: var(--scrim); }
.dialog[open] { animation: layer-in var(--t-med) var(--ease); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 12px 8px 24px; }
.dialog-title { font-size: var(--fs-lg); font-weight: 800; }
.dialog-body { padding: 8px 24px 20px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 16px 24px; border-top: 1px solid var(--line); }
.pop { position: fixed; z-index: 70; max-width: calc(100vw - 16px); padding: 16px; background: var(--panel); border: 1px solid var(--line-strong); border-radius: var(--r-lg); box-shadow: var(--shadow-layer); animation: layer-in var(--t-fast) var(--ease); }
.pop:focus { outline: none; }
.pop--menu { min-width: 200px; padding: 6px; }
.menu-i { display: flex; align-items: center; gap: 10px; width: 100%; height: 40px; padding: 0 12px; border: 0; border-radius: var(--r-sm); background: none; color: var(--text); font-size: var(--fs-md); font-weight: 600; text-align: left; text-decoration: none; }
.menu-i:hover, .menu-i:focus-visible { background: var(--panel-2); }
.menu-i svg { color: var(--muted); }
.tip { position: fixed; z-index: 90; max-width: 280px; padding: 8px 10px; background: var(--panel); border: 1px solid var(--line-strong); border-radius: var(--r-md); box-shadow: var(--shadow-layer); color: var(--text); font-size: var(--fs-sm); line-height: 1.45; pointer-events: none; }
.tip--hover { pointer-events: auto; }
.tip-t { margin-bottom: 4px; font-weight: 700; }
.tip-r { display: flex; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; }
.tip-r strong { font-weight: 800; }
.tip-r span:last-child { color: var(--muted); font-weight: 600; }
@keyframes layer-in { from { opacity: 0; transform: translateY(-4px); } }

/* ---------- states ---------- */
.skel { display: block; flex: none; max-width: 100%; border-radius: var(--r-sm); background: var(--panel-2); animation: skel 1.4s ease-in-out infinite; }
@keyframes skel { 50% { opacity: .45; } }
.empty { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 48px 24px; text-align: center; }
.empty-ic { display: grid; place-items: center; width: 48px; height: 48px; margin-bottom: 8px; border: 1px solid var(--line); border-radius: var(--r-lg); background: var(--panel-2); color: var(--muted); }
.empty-t { font-size: var(--fs-base); font-weight: 800; }
.empty-x { max-width: 380px; color: var(--muted); font-size: var(--fs-md); }
.empty .btn { margin-top: 12px; }
.empty--sm { padding: 28px 16px; }
.empty--sm .empty-ic { width: 40px; height: 40px; }
.empty--err .empty-ic { color: var(--error); }
.alert { display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px; border: 1px solid var(--error); border-radius: var(--r-md); font-size: var(--fs-md); }
.alert > svg { margin-top: 2px; color: var(--error); }
.alert p + p { margin-top: 2px; color: var(--muted); }

@media (forced-colors: active) {
  .seg-b[aria-checked="true"], .switch[aria-checked="true"] .switch-t, .check-i:checked + .check-b { forced-color-adjust: none; background: Highlight; color: HighlightText; }
}
