/* Base elements, controls and shared components */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-body); font-size: var(--fs-base); line-height: 1.55;
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
}
h1, h2, h3, h4 { font-family: var(--font-head); font-weight: 600; line-height: 1.2; margin: 0 0 var(--s3); letter-spacing: -.01em; }
h1 { font-size: var(--fs-2xl); } h2 { font-size: var(--fs-xl); } h3 { font-size: var(--fs-lg); } h4 { font-size: var(--fs-md); }
p { margin: 0 0 var(--s3); }
a { color: var(--primary-text); }
code, kbd, pre, samp { font-family: var(--font-mono); }
kbd { font-size: 11px; padding: 1px 5px; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: var(--r-xs); background: var(--surface); color: var(--text-2); }
img, svg { max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus { outline: none; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: var(--r-xs); }
.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.skip-link { position: absolute; left: 8px; top: -60px; z-index: 1000; background: var(--primary); color: #fff; padding: 8px 12px; border-radius: var(--r-sm); }
.skip-link:focus { top: 8px; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; } }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 0 14px; border-radius: var(--r-sm); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: var(--fs-md); font-weight: 500; cursor: pointer;
  white-space: nowrap; transition: background .12s, border-color .12s, color .12s; text-decoration: none;
}
.btn:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.signal { background: var(--signal); border-color: var(--signal); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-ghost { color: var(--danger-text); }
.btn.danger-ghost:hover { background: var(--danger-weak); border-color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { min-height: 30px; padding: 0 10px; font-size: var(--fs-sm); }
.btn.block { width: 100%; }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: var(--r-sm);
  border: 1px solid transparent; background: transparent; color: var(--text-2); cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.sm { width: 28px; height: 28px; }
.icon-btn[aria-pressed="true"] { color: var(--primary-text); background: var(--primary-weak); }
.icon { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon.sm { width: 15px; height: 15px; }

/* ---------- forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: var(--s4); min-width: 0; }
.field > label, .field > .label { font-size: var(--fs-sm); font-weight: 600; color: var(--text); }
.field .hint { font-size: var(--fs-xs); color: var(--text-3); }
.field .err { font-size: var(--fs-xs); color: var(--danger-text); }
.input, select.input, textarea.input {
  width: 100%; min-height: 38px; padding: 8px 11px; border-radius: var(--r-sm); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font-size: var(--fs-md);
}
textarea.input { min-height: 90px; resize: vertical; line-height: 1.5; }
.input:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
.input.mono, textarea.mono { font-family: var(--font-mono); font-size: 13px; }
.input[aria-invalid="true"] { border-color: var(--danger); }
.check { display: flex; align-items: flex-start; gap: 10px; font-size: var(--fs-md); cursor: pointer; }
.check input { margin-top: 3px; width: 16px; height: 16px; accent-color: var(--primary); flex: none; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 var(--s4); }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0 var(--s4); }
@media (max-width: 640px) { .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); } }
.row { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: var(--s3); }
.muted { color: var(--text-3); }
.small { font-size: var(--fs-sm); }
.xs { font-size: var(--fs-xs); }
.mono { font-family: var(--font-mono); }
.nowrap { white-space: nowrap; }
.grow { flex: 1 1 auto; min-width: 0; }
.spacer { flex: 1; }

/* switch */
.switch { position: relative; display: inline-flex; align-items: center; gap: 10px; cursor: pointer; font-size: var(--fs-md); }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { width: 36px; height: 20px; border-radius: 999px; background: var(--border-strong); position: relative; transition: background .15s; flex: none; }
.switch .track::after { content: ""; position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s var(--ease); box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.switch input:checked + .track { background: var(--primary); }
.switch input:checked + .track::after { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--focus); outline-offset: 2px; }

/* segmented control */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--r-sm); padding: 2px; background: var(--surface-2); gap: 2px; }
.seg button { border: 0; background: transparent; padding: 4px 10px; border-radius: 4px; font-size: var(--fs-sm); cursor: pointer; color: var(--text-2); }
.seg button[aria-pressed="true"], .seg button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1); font-weight: 500; }

/* ---------- badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px; border-radius: var(--r-pill); font-size: 12px; font-weight: 500; background: var(--surface-3); color: var(--text-2); white-space: nowrap; }
.badge.blue { background: var(--primary-weak); color: var(--primary-text); }
.badge.teal { background: var(--signal-weak); color: var(--signal-text); }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); box-shadow: inset 0 0 0 1px var(--warn-border); }
.badge.red { background: var(--danger-weak); color: var(--danger-text); }
.badge .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

/* ---------- surfaces ---------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); padding: var(--s5); }
.panel + .panel { margin-top: var(--s4); }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); margin-bottom: var(--s4); flex-wrap: wrap; }
.panel-head h2, .panel-head h3 { margin: 0; }
.callout { border: 1px solid var(--border); border-left: 3px solid var(--primary); background: var(--primary-weak); border-radius: var(--r-sm); padding: var(--s3) var(--s4); font-size: var(--fs-md); }
.callout.warn { border-color: var(--warn-border); border-left-color: #D99A0B; background: var(--warn-bg); color: var(--warn-text); }
.callout.teal { border-left-color: var(--signal); background: var(--signal-weak); }
.callout.danger { border-left-color: var(--danger); background: var(--danger-weak); color: var(--danger-text); }
.empty { text-align: center; padding: var(--s7) var(--s4); color: var(--text-3); font-size: var(--fs-md); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--r-sm); max-width: 100%; }
table.data { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
table.data th, table.data td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data th { background: var(--surface-2); font-weight: 600; color: var(--text-2); position: sticky; top: 0; z-index: 1; white-space: nowrap; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover td { background: color-mix(in srgb, var(--surface-2) 55%, transparent); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- dialog ---------- */
.dlg-backdrop { position: fixed; inset: 0; background: var(--overlay); display: grid; place-items: center; z-index: 400; padding: var(--s4); animation: fade .12s ease; }
.dlg { background: var(--surface); color: var(--text); border-radius: var(--r-lg); box-shadow: var(--shadow-2); width: min(520px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; border: 1px solid var(--border); animation: pop .16s var(--ease); }
.dlg.wide { width: min(860px, 100%); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); gap: var(--s3); }
.dlg-head h2 { font-size: var(--fs-lg); margin: 0; }
.dlg-body { padding: var(--s5); overflow: auto; }
.dlg-foot { display: flex; justify-content: flex-end; gap: var(--s2); padding: var(--s3) var(--s5) var(--s4); border-top: 1px solid var(--border); flex-wrap: wrap; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.985); } }

/* ---------- drawer (slide-over) ---------- */
.drawer-backdrop { position: fixed; inset: 0; background: var(--overlay); z-index: 300; animation: fade .12s ease; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(620px, 100%); background: var(--surface); z-index: 301; box-shadow: var(--shadow-2); display: flex; flex-direction: column; animation: slidein .18s var(--ease); border-left: 1px solid var(--border); }
.drawer-head { display: flex; align-items: center; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); }
.drawer-head h2 { margin: 0; font-size: var(--fs-lg); }
.drawer-body { flex: 1; overflow: auto; padding: var(--s5); }
@keyframes slidein { from { transform: translateX(24px); opacity: 0; } }

/* ---------- menu ---------- */
.menu { position: fixed; z-index: 500; min-width: 190px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); box-shadow: var(--shadow-2); padding: 4px; animation: pop .1s var(--ease); }
.menu [role="menuitem"], .menu [role="menuitemradio"] { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; border: 0; background: none; border-radius: var(--r-sm); text-align: left; font-size: var(--fs-md); cursor: pointer; color: var(--text); text-decoration: none; }
.menu [role="menuitem"]:hover, .menu [role="menuitem"]:focus-visible, .menu [role="menuitemradio"]:hover, .menu [role="menuitemradio"]:focus-visible { background: var(--surface-2); outline: none; }
.menu .danger { color: var(--danger-text); }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px; }
.menu .menu-label { padding: 6px 10px 2px; font-size: var(--fs-xs); color: var(--text-3); }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: var(--s4); bottom: var(--s4); z-index: 600; display: flex; flex-direction: column; gap: var(--s2); max-width: min(380px, calc(100vw - 32px)); }
.toast { display: flex; gap: 10px; align-items: flex-start; background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--primary); border-radius: var(--r-sm); box-shadow: var(--shadow-2); padding: 10px 12px; font-size: var(--fs-md); animation: pop .16s var(--ease); }
.toast.error { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--signal); }
.toast .grow { word-break: break-word; }

/* progress */
.bar { height: 8px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--primary); border-radius: inherit; transition: width .3s var(--ease); }
.bar.teal > span { background: var(--signal); }
.bar.over > span { background: var(--danger); }
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--border-strong); border-top-color: var(--primary); animation: spin .7s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* tabs */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: var(--s4); overflow-x: auto; }
.tabs [role="tab"] { border: 0; background: none; padding: 8px 12px; font-size: var(--fs-md); color: var(--text-2); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs [role="tab"][aria-selected="true"] { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }

/* stat */
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); padding: var(--s4); min-width: 0; }
.stat .k { font-size: var(--fs-sm); color: var(--text-2); }
.stat .v { font-family: var(--font-head); font-size: var(--fs-xl); font-weight: 600; line-height: 1.2; margin-top: 4px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat .d { font-size: var(--fs-xs); color: var(--text-3); margin-top: 2px; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: var(--s3); }
@media (max-width: 640px) { .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s2); } .stat { padding: var(--s3); } }

/* charts */
.chart { width: 100%; height: auto; display: block; font-family: var(--font-body); }
.chart-fig { min-width: 0; }
.chart text { fill: var(--text-3); font-size: 11px; }
.chart .grid { stroke: var(--chart-grid); stroke-width: 1; }
.legend { display: flex; gap: var(--s4); flex-wrap: wrap; font-size: var(--fs-xs); color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
