/* =====================================================================
   VWS UI — components
   Uses only tokens from tokens.css. No inline styles are needed by any
   component, so pages can run under a strict Content-Security-Policy
   (style-src 'self'; script-src 'self').
   ===================================================================== */

/* ---------- Base ------------------------------------------------------ */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: var(--lh-normal);
  color: var(--text);
  background-color: var(--bg);
  background-image:
    radial-gradient(1200px 600px at 12% -10%, var(--bg-glow-1), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, var(--bg-glow-2), transparent 60%);
  background-attachment: fixed;
  font-feature-settings: "cv11", "ss01", "tnum";
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; line-height: var(--lh-tight); font-weight: var(--fw-semibold); }
h1 { font-size: var(--fs-xl); }
h2 { font-size: var(--fs-lg); }
h3 { font-size: var(--fs-md); }
p { margin: 0; }
code, .mono { font-family: var(--font-mono); font-size: 0.92em; }
:focus { outline: none; }
:focus-visible { box-shadow: var(--focus-ring); border-radius: var(--radius-sm); }
::selection { background: var(--accent-soft); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.muted { color: var(--text-2); }
.dim { color: var(--text-3); }
.num { font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }

/* scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--surface-3) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 10px; border: 3px solid transparent; background-clip: content-box; }

/* icons (SVG sprite: <svg class="icon"><use href="assets/icons.svg#i-name"/></svg>) */
.icon { width: 16px; height: 16px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; vertical-align: -3px; }
.icon-sm { width: 14px; height: 14px; }
.icon-lg { width: 20px; height: 20px; }

/* ---------- App shell ------------------------------------------------- */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: var(--topbar-h) minmax(0, 1fr) auto;
  grid-template-areas: "brand top" "side main" "side foot";
  min-height: 100vh;
}
.app-brand {
  grid-area: brand;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 0 var(--sp-4);
  background: var(--surface-0);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
.logo-mark {
  width: 30px; height: 30px; border-radius: var(--radius-md);
  background: var(--brand-gradient);
  display: grid; place-items: center;
  color: #fff; font-weight: var(--fw-bold); font-size: var(--fs-xs); letter-spacing: 0.04em;
  box-shadow: 0 0 0 1px rgba(255,255,255,.12) inset, 0 6px 18px color-mix(in oklab, var(--accent) 35%, transparent);
}
.logo-text { display: flex; flex-direction: column; line-height: 1.1; }
.logo-text strong { font-size: var(--fs-md); letter-spacing: 0.01em; }
.logo-text span { font-size: var(--fs-2xs); color: var(--text-3); text-transform: uppercase; letter-spacing: var(--tracking-caps); }

.topbar {
  grid-area: top;
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 0 var(--sp-5);
  background: var(--glass);
  backdrop-filter: blur(14px) saturate(140%);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
  border-bottom: 1px solid var(--border);
}
.topbar .spacer { flex: 1; }

.sidebar {
  grid-area: side;
  display: flex; flex-direction: column;
  background: var(--surface-0);
  border-right: 1px solid var(--border);
  padding: var(--sp-3) var(--sp-2);
  overflow-y: auto;
}
.main { --main-pt: var(--sp-5); --main-px: var(--sp-6); grid-area: main; padding: var(--main-pt) var(--main-px) var(--sp-8); min-width: 0; }
.statusbar {
  grid-area: foot;
  display: flex; align-items: center; gap: var(--sp-4);
  padding: 0 var(--sp-6); height: 30px;
  font-size: var(--fs-xs); color: var(--text-3);
  border-top: 1px solid var(--border);
  background: var(--surface-0);
}
.statusbar .spacer { flex: 1; }

/* sidebar navigation with live sub-metrics */
.nav-section { margin: var(--sp-3) var(--sp-2) var(--sp-1); font-size: var(--fs-2xs); text-transform: uppercase; letter-spacing: var(--tracking-caps); color: var(--text-3); }
.nav-item {
  display: grid; grid-template-columns: 20px 1fr auto; align-items: center; column-gap: var(--sp-2);
  padding: 7px var(--sp-2); border-radius: var(--radius-md);
  color: var(--text-2); cursor: pointer; position: relative;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.nav-item:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-item .nav-label { font-weight: var(--fw-medium); color: inherit; }
.nav-item .nav-sub { grid-column: 2 / 4; font-size: var(--fs-2xs); color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-item[aria-current="page"] { background: var(--accent-soft); color: var(--text); }
.nav-item[aria-current="page"]::before {
  content: ""; position: absolute; left: -8px; top: 8px; bottom: 8px; width: 3px; border-radius: 3px;
  background: var(--accent); box-shadow: 0 0 12px var(--accent);
}
.nav-item[aria-current="page"] .icon { color: var(--accent-text); }
.sidebar-foot { margin-top: auto; padding: var(--sp-3) var(--sp-2) var(--sp-1); }

/* page header */
.page-head { display: flex; align-items: flex-end; gap: var(--sp-4); margin-bottom: var(--sp-5); flex-wrap: wrap; }
.page-head .titles { display: flex; flex-direction: column; gap: 2px; }
.page-head .titles p { color: var(--text-2); font-size: var(--fs-sm); }
.page-head .actions { margin-left: auto; display: flex; gap: var(--sp-2); flex-wrap: wrap; }
.breadcrumb { font-size: var(--fs-xs); color: var(--text-3); display: flex; gap: 6px; align-items: center; }

/* ---------- Grid tabs (one tab per grid) ----------------------------- */
.grid-tabs { display: flex; gap: 2px; align-items: stretch; height: 100%; overflow-x: auto; scrollbar-width: none; }
.grid-tab {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: 0 var(--sp-3); border: 0; background: none; cursor: pointer;
  color: var(--text-2); font: inherit; font-weight: var(--fw-medium); white-space: nowrap;
  position: relative;
  transition: color var(--dur-fast) var(--ease);
}
.grid-tab:hover { color: var(--text); }
.grid-tab .grid-tab-count { font-size: var(--fs-2xs); color: var(--text-3); background: var(--surface-2); border-radius: var(--radius-pill); padding: 1px 7px; }
.grid-tab[aria-selected="true"] { color: var(--text); }
.grid-tab[aria-selected="true"]::after {
  content: ""; position: absolute; left: var(--sp-3); right: var(--sp-3); bottom: -1px; height: 2px; border-radius: 2px;
  background: var(--brand-gradient);
}
.grid-tab .tag { font-size: var(--fs-2xs); color: var(--text-3); text-transform: uppercase; letter-spacing: var(--tracking-caps); }

/* ---------- Cards ----------------------------------------------------- */
.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1), inset 0 1px 0 var(--highlight-edge);
  min-width: 0;
}
.card-head {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
}
.card-head h2, .card-head h3 { font-size: var(--fs-md); }
.card-head .sub { color: var(--text-3); font-size: var(--fs-xs); }
.card-head .tools { margin-left: auto; display: flex; align-items: center; gap: var(--sp-2); }
.card-body { padding: var(--sp-4); }
.card-body.flush { padding: 0; }
.card-foot { padding: var(--sp-2) var(--sp-4); border-top: 1px solid var(--border); font-size: var(--fs-xs); color: var(--text-3); display: flex; align-items: center; gap: var(--sp-3); }

/* layout helpers */
.grid { display: grid; gap: var(--sp-4); }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.cols-auto { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.span-2 { grid-column: span 2; }
.stack { display: flex; flex-direction: column; gap: var(--sp-4); }
.row { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); }
.mt-4 { margin-top: var(--sp-4); } .mt-6 { margin-top: var(--sp-6); } .mt-8 { margin-top: var(--sp-8); }

/* ---------- KPI / metric card ---------------------------------------- */
.kpi { padding: var(--sp-4); display: flex; flex-direction: column; gap: 6px; position: relative; overflow: hidden; }
.kpi-top { display: flex; align-items: center; gap: var(--sp-2); color: var(--text-2); font-size: var(--fs-sm); }
.kpi-top .icon { color: var(--text-3); }
.kpi-top .kpi-aside { margin-left: auto; color: var(--text-3); font-size: var(--fs-xs); }
.kpi-value { font-size: var(--fs-2xl); font-weight: var(--fw-semibold); letter-spacing: -0.01em; font-variant-numeric: tabular-nums; line-height: 1.1; }
.kpi-value .unit { font-size: var(--fs-sm); font-weight: var(--fw-medium); color: var(--text-2); margin-left: 4px; }
.kpi-meta { font-size: var(--fs-xs); color: var(--text-3); display: flex; gap: var(--sp-3); }
.kpi .spark { position: absolute; right: var(--sp-3); bottom: var(--sp-3); width: 96px; height: 30px; }
.trend-up { color: var(--ok); } .trend-down { color: var(--err); }

/* sparkline svg */
.spark { overflow: visible; }
.spark .line { fill: none; stroke: var(--accent-2); stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.spark .area { fill: var(--accent-2-soft); stroke: none; }
.spark.accent .line { stroke: var(--accent); } .spark.accent .area { fill: var(--accent-soft); }

/* ---------- Status bulbs --------------------------------------------- */
.bulb {
  --c: var(--stale);
  width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block;
  background: radial-gradient(circle at 35% 30%, color-mix(in oklab, var(--c) 55%, white) 0, var(--c) 55%);
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--c) 18%, transparent), 0 0 10px color-mix(in oklab, var(--c) 70%, transparent);
}
.bulb.ok { --c: var(--ok); }
.bulb.warn { --c: var(--warn); }
.bulb.err { --c: var(--err); animation: bulb-alert 1.2s var(--ease) infinite; }
.bulb.stale { --c: var(--stale); box-shadow: 0 0 0 3px var(--stale-soft); }
.bulb.lg { width: 14px; height: 14px; }
.bulb.pulse { animation: bulb-pulse 2.4s var(--ease) infinite; }
@keyframes bulb-pulse { 0%, 100% { box-shadow: 0 0 0 3px color-mix(in oklab, var(--c) 18%, transparent), 0 0 8px color-mix(in oklab, var(--c) 60%, transparent); } 50% { box-shadow: 0 0 0 5px color-mix(in oklab, var(--c) 6%, transparent), 0 0 14px var(--c); } }
@keyframes bulb-alert { 50% { box-shadow: 0 0 0 6px color-mix(in oklab, var(--c) 10%, transparent), 0 0 18px var(--c); } }

/* service tile: bulb + name + detail */
.services { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: var(--sp-2); }
.service {
  display: grid; grid-template-columns: auto 1fr; column-gap: var(--sp-3); row-gap: 1px; align-items: center;
  padding: var(--sp-3); border-radius: var(--radius-md);
  background: var(--surface-2); border: 1px solid var(--border);
  cursor: pointer; transition: border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.service:hover { border-color: var(--border-strong); }
.service .bulb { grid-row: span 2; }
.service .name { font-weight: var(--fw-medium); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.service .detail { font-size: var(--fs-xs); color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.service.is-err { border-color: color-mix(in oklab, var(--err) 45%, transparent); background: linear-gradient(0deg, var(--err-soft), var(--err-soft)), var(--surface-2); }
.service.is-warn { border-color: color-mix(in oklab, var(--warn) 40%, transparent); }

/* big grid status banner */
.grid-status {
  display: flex; align-items: center; gap: var(--sp-4); padding: var(--sp-4) var(--sp-5);
  border-radius: var(--radius-lg); border: 1px solid var(--border);
  background: linear-gradient(100deg, var(--ok-soft), transparent 55%), var(--surface-1);
}
.grid-status.is-offline { background: linear-gradient(100deg, var(--err-soft), transparent 55%), var(--surface-1); }
.grid-status.is-limited { background: linear-gradient(100deg, var(--warn-soft), transparent 55%), var(--surface-1); }
.grid-status .gs-title { font-size: var(--fs-lg); font-weight: var(--fw-semibold); }
.grid-status .gs-sub { font-size: var(--fs-xs); color: var(--text-2); }
.grid-status .gs-stats { margin-left: auto; display: flex; gap: var(--sp-6); }
.gs-stat { display: flex; flex-direction: column; align-items: flex-end; }
.gs-stat b { font-size: var(--fs-xl); font-variant-numeric: tabular-nums; font-weight: var(--fw-semibold); }
.gs-stat span { font-size: var(--fs-2xs); color: var(--text-3); text-transform: uppercase; letter-spacing: var(--tracking-caps); }

/* live indicator in header */
.live { display: inline-flex; align-items: center; gap: var(--sp-2); white-space: nowrap; font: inherit; font-size: var(--fs-xs); cursor: pointer; color: var(--text-2); padding: 4px 10px; border-radius: var(--radius-pill); background: var(--surface-2); border: 1px solid var(--border); }
.live .bulb { width: 8px; height: 8px; }
.live.is-paused .bulb { --c: var(--stale); animation: none; }

/* ---------- Buttons --------------------------------------------------- */
.btn {
  --btn-bg: var(--surface-2); --btn-fg: var(--text); --btn-bd: var(--border);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: var(--control-h); padding: 0 var(--sp-3);
  font: inherit; font-weight: var(--fw-medium); font-size: var(--fs-sm); white-space: nowrap;
  color: var(--btn-fg); background: var(--btn-bg); border: 1px solid var(--btn-bd); border-radius: var(--radius-md);
  cursor: pointer; user-select: none; text-decoration: none;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.btn:hover { --btn-bg: var(--surface-3); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.45; pointer-events: none; }
.btn-primary { --btn-bg: var(--accent); --btn-fg: var(--on-accent); --btn-bd: transparent; box-shadow: 0 4px 14px color-mix(in oklab, var(--accent) 35%, transparent), inset 0 1px 0 rgba(255,255,255,.18); }
.btn-primary:hover { --btn-bg: var(--accent-hover); }
.btn-ghost { --btn-bg: transparent; --btn-bd: transparent; --btn-fg: var(--text-2); }
.btn-ghost:hover { --btn-bg: var(--surface-2); --btn-fg: var(--text); }
.btn-danger { --btn-bg: var(--err-soft); --btn-fg: var(--err); --btn-bd: color-mix(in oklab, var(--err) 35%, transparent); }
.btn-danger:hover { --btn-bg: var(--err); --btn-fg: #fff; }
.btn-danger-solid { --btn-bg: var(--err); --btn-fg: #fff; --btn-bd: transparent; }
.btn-sm { height: var(--control-h-sm); padding: 0 var(--sp-2); font-size: var(--fs-xs); border-radius: var(--radius-sm); }
.btn-lg { height: var(--control-h-lg); padding: 0 var(--sp-5); font-size: var(--fs-md); }
.btn-icon { width: var(--control-h); padding: 0; }
.btn-icon.btn-sm { width: var(--control-h-sm); }
.btn-group { display: inline-flex; }
.btn-group .btn { border-radius: 0; }
.btn-group .btn:first-child { border-radius: var(--radius-md) 0 0 var(--radius-md); }
.btn-group .btn:last-child { border-radius: 0 var(--radius-md) var(--radius-md) 0; }
.btn-group .btn + .btn { margin-left: -1px; }
.kbd { font-family: var(--font-mono); font-size: var(--fs-2xs); padding: 1px 5px; border-radius: var(--radius-xs); border: 1px solid var(--border-strong); border-bottom-width: 2px; color: var(--text-2); background: var(--surface-1); }

/* ---------- Segmented control / pill tabs ---------------------------- */
.page-head .actions, .table-tools > * { max-width: 100%; min-width: 0; }
.segmented { display: inline-flex; max-width: 100%; min-width: 0; overflow-x: auto; scrollbar-width: none; padding: 3px; gap: 2px; background: var(--field); border: 1px solid var(--border); border-radius: var(--radius-md); }
.segmented button {
  display: inline-flex; align-items: center; gap: 6px; flex: none; white-space: nowrap;
  height: calc(var(--control-h) - 8px); padding: 0 var(--sp-3);
  border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-2);
  font: inherit; font-size: var(--fs-sm); font-weight: var(--fw-medium); cursor: pointer;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-selected="true"], .segmented button[aria-pressed="true"] { background: var(--surface-3); color: var(--text); box-shadow: var(--shadow-1); }
.segmented .count { font-size: var(--fs-2xs); color: var(--text-3); }
.segmented button[aria-selected="true"] .count { color: var(--accent-text); }

/* ---------- Chips / badges ------------------------------------------- */
.chip {
  --c: var(--text-2);
  display: inline-flex; align-items: center; gap: 5px;
  height: 20px; padding: 0 8px; border-radius: var(--radius-pill);
  font-size: var(--fs-2xs); font-weight: var(--fw-semibold); letter-spacing: 0.02em; white-space: nowrap;
  color: var(--c); background: color-mix(in oklab, var(--c) 14%, transparent);
  border: 1px solid color-mix(in oklab, var(--c) 22%, transparent);
}
.chip .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.chip.ok { --c: var(--ok); } .chip.warn { --c: var(--warn); } .chip.err { --c: var(--err); }
.chip.info { --c: var(--info); } .chip.accent { --c: var(--accent-text); } .chip.cyan { --c: var(--accent-2); }
.chip.stale { --c: var(--stale); }
.chip.square { border-radius: var(--radius-xs); }
.badge-count { min-width: 18px; height: 18px; padding: 0 5px; border-radius: var(--radius-pill); background: var(--err); color: #fff; font-size: var(--fs-2xs); font-weight: var(--fw-bold); display: inline-grid; place-items: center; }

/* ---------- Forms ----------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .label { font-size: var(--fs-xs); font-weight: var(--fw-medium); color: var(--text-2); }
.field .hint { font-size: var(--fs-xs); color: var(--text-3); }
.field .error-text { font-size: var(--fs-xs); color: var(--err); }
.input, .select, .textarea {
  width: 100%; height: var(--control-h); padding: 0 var(--sp-3);
  font: inherit; font-size: var(--fs-sm); color: var(--text);
  background: var(--field); border: 1px solid var(--border-strong); border-radius: var(--radius-md);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.textarea { height: auto; min-height: 84px; padding: var(--sp-2) var(--sp-3); resize: vertical; }
.input::placeholder, .textarea::placeholder { color: var(--text-3); }
.input:hover, .select:hover, .textarea:hover { border-color: color-mix(in oklab, var(--text-3) 60%, transparent); }
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input[aria-invalid="true"] { border-color: var(--err); box-shadow: 0 0 0 3px var(--err-soft); }
.select { appearance: none; padding-right: 32px; background-image: linear-gradient(45deg, transparent 50%, var(--text-3) 50%), linear-gradient(135deg, var(--text-3) 50%, transparent 50%); background-position: calc(100% - 16px) 52%, calc(100% - 11px) 52%; background-size: 5px 5px; background-repeat: no-repeat; }
.input-wrap { position: relative; display: flex; align-items: center; }
.input-wrap > .icon { position: absolute; left: 10px; color: var(--text-3); pointer-events: none; }
.input-wrap > .input { padding-left: 32px; }
.input-wrap > .kbd { position: absolute; right: 8px; }
.search { max-width: 360px; }

.check, .radio { display: inline-flex; align-items: center; gap: var(--sp-2); cursor: pointer; font-size: var(--fs-sm); }
.check input, .radio input { appearance: none; width: 16px; height: 16px; margin: 0; border: 1.5px solid var(--border-strong); background: var(--field); display: grid; place-items: center; cursor: pointer; transition: all var(--dur-fast) var(--ease); }
.check input { border-radius: var(--radius-xs); }
.radio input { border-radius: 50%; }
.check input:checked, .radio input:checked { background: var(--accent); border-color: var(--accent); }
.check input:checked::after { content: ""; width: 8px; height: 4px; border: 2px solid var(--on-accent); border-top: 0; border-right: 0; transform: rotate(-45deg) translate(1px, -1px); }
.radio input:checked::after { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--on-accent); }

.switch { display: inline-flex; align-items: center; gap: var(--sp-2); cursor: pointer; font-size: var(--fs-sm); }
.switch input { appearance: none; width: 34px; height: 20px; margin: 0; border-radius: var(--radius-pill); background: var(--surface-3); border: 1px solid var(--border-strong); position: relative; cursor: pointer; transition: background var(--dur) var(--ease); }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--text-2); transition: transform var(--dur) var(--ease), background var(--dur) var(--ease); }
.switch input:checked { background: var(--accent); border-color: transparent; }
.switch input:checked::after { transform: translateX(14px); background: var(--on-accent); }

/* ---------- Data table ------------------------------------------------ */
.table-tools { display: flex; align-items: center; gap: var(--sp-2); padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.table-tools .spacer { flex: 1; }
.table-wrap { overflow: auto; max-height: var(--table-max-h, 520px); }
.table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--fs-sm); }
.table th {
  position: sticky; top: 0; z-index: 1;
  height: 34px; padding: 0 var(--sp-3); text-align: left; white-space: nowrap;
  font-size: var(--fs-2xs); font-weight: var(--fw-semibold); text-transform: uppercase; letter-spacing: var(--tracking-caps);
  color: var(--text-3); background: var(--surface-1); border-bottom: 1px solid var(--border);
}
.table th.sortable { cursor: pointer; user-select: none; }
.table th.sortable:hover { color: var(--text); }
.table th .sort-ind { display: inline-block; width: 10px; margin-left: 4px; opacity: 0; transition: opacity var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease); }
.table th.sortable:hover .sort-ind { opacity: .5; }
.table th[aria-sort="ascending"], .table th[aria-sort="descending"] { color: var(--accent-text); }
.table th[aria-sort="ascending"] .sort-ind, .table th[aria-sort="descending"] .sort-ind { opacity: 1; }
.table th[aria-sort="descending"] .sort-ind { transform: rotate(180deg); }
.table td { height: var(--row-h); padding: 0 var(--sp-3); border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th.num, .table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.table tbody tr { transition: background var(--dur-fast) var(--ease); }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr[aria-selected="true"] { background: var(--accent-softer); box-shadow: inset 3px 0 0 var(--accent); }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr.is-new { animation: row-new 1.6s var(--ease); }
.table tbody tr.is-changed td.changed { animation: cell-flash 1.4s var(--ease); }
.table tbody tr.is-leaving { opacity: 0; transition: opacity var(--dur-slow) var(--ease); }
@keyframes row-new { from { background: var(--accent-2-soft); } to { background: transparent; } }
@keyframes cell-flash { from { background: var(--accent-soft); } to { background: transparent; } }
.cell-user { display: flex; align-items: center; gap: var(--sp-2); }
.avatar { width: 24px; height: 24px; border-radius: 50%; flex: none; display: grid; place-items: center; font-size: 10px; font-weight: var(--fw-bold); color: #fff; background: var(--brand-gradient); }
.avatar.hg { background: linear-gradient(135deg, var(--accent-2), var(--info)); }
.cell-user .who { display: flex; flex-direction: column; line-height: 1.15; }
.cell-user .who small { color: var(--text-3); font-size: var(--fs-2xs); }
.link-cell { color: var(--accent-text); cursor: pointer; }
.link-cell:hover { text-decoration: underline; }
.table .row-actions { opacity: 0; transition: opacity var(--dur-fast) var(--ease); text-align: right; }
.table tbody tr:hover .row-actions, .table tbody tr:focus-within .row-actions { opacity: 1; }
.table-empty { padding: var(--sp-10) var(--sp-4); text-align: center; color: var(--text-3); }

/* ---------- Usage bars ----------------------------------------------- */
.meter { height: 6px; border-radius: var(--radius-pill); background: var(--surface-3); overflow: hidden; }
.meter > span { display: block; height: 100%; width: 0; border-radius: inherit; background: var(--brand-gradient); transition: width var(--dur-slow) var(--ease); }
.meter.warn > span { background: var(--warn); } .meter.err > span { background: var(--err); }
.meter-row { display: grid; grid-template-columns: 110px 1fr 56px; align-items: center; gap: var(--sp-3); font-size: var(--fs-xs); }
.meter-row .val { text-align: right; color: var(--text-2); font-variant-numeric: tabular-nums; }
.segbar { display: flex; height: 8px; border-radius: var(--radius-pill); overflow: hidden; gap: 2px; background: var(--surface-3); }
.segbar > span { display: block; height: 100%; width: 0; }
.legend { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px var(--sp-4); font-size: var(--fs-xs); color: var(--text-2); margin-top: var(--sp-3); }
.legend span { display: flex; align-items: center; gap: 6px; }
.legend i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.legend b { margin-left: auto; font-weight: var(--fw-medium); color: var(--text); font-variant-numeric: tabular-nums; }
.c1 { background: var(--accent); } .c2 { background: var(--accent-2); } .c3 { background: var(--ok); }
.c4 { background: var(--warn); } .c5 { background: #f472b6; } .c6 { background: var(--info); } .c7 { background: var(--stale); }

/* ---------- Menus (context menu, dropdown) --------------------------- */
.menu {
  position: fixed; z-index: 100; min-width: 220px; padding: 5px;
  background: var(--glass); backdrop-filter: blur(16px) saturate(150%); -webkit-backdrop-filter: blur(16px) saturate(150%);
  border: 1px solid var(--border-strong); border-radius: var(--radius-lg); box-shadow: var(--shadow-3);
  opacity: 0; transform: scale(.97); transform-origin: top left; pointer-events: none;
  transition: opacity var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.menu.is-open { opacity: 1; transform: none; pointer-events: auto; }
.menu-title { padding: 6px 10px 4px; font-size: var(--fs-2xs); color: var(--text-3); text-transform: uppercase; letter-spacing: var(--tracking-caps); }
.menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  height: 32px; padding: 0 10px; border: 0; border-radius: var(--radius-sm); background: none;
  font: inherit; font-size: var(--fs-sm); color: var(--text); text-align: left; cursor: pointer;
}
.menu-item .icon { color: var(--text-2); }
.menu-item .kbd, .menu-item .hint { margin-left: auto; }
.menu-item .hint { font-size: var(--fs-xs); color: var(--text-3); }
.menu-item:hover, .menu-item:focus-visible { background: var(--accent-soft); box-shadow: none; }
.menu-item:hover .icon { color: var(--accent-text); }
.menu-item.danger { color: var(--err); } .menu-item.danger .icon { color: var(--err); }
.menu-item.danger:hover { background: var(--err-soft); }
.menu-item[aria-disabled="true"] { opacity: .4; pointer-events: none; }
.menu-sep { height: 1px; margin: 5px 4px; background: var(--border); }

/* ---------- Detail drawer -------------------------------------------- */
.drawer-scrim { position: fixed; inset: 0; z-index: 60; background: var(--scrim); opacity: 0; pointer-events: none; transition: opacity var(--dur) var(--ease); }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 61; width: min(var(--detail-w), 100vw);
  display: flex; flex-direction: column;
  background: var(--surface-1); border-left: 1px solid var(--border-strong); box-shadow: var(--shadow-3);
  transform: translateX(102%); transition: transform var(--dur-slow) var(--ease);
}
.drawer.is-open { transform: none; }
.drawer.is-open + .drawer-scrim, .drawer-scrim.is-open { opacity: 1; pointer-events: auto; }
.drawer-head { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-4); border-bottom: 1px solid var(--border); }
.drawer-body { padding: var(--sp-4); overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: var(--sp-4); }
.drawer-foot { padding: var(--sp-3) var(--sp-4); border-top: 1px solid var(--border); display: flex; gap: var(--sp-2); justify-content: flex-end; }
.maptile { width: 100%; aspect-ratio: 1; border-radius: var(--radius-md); border: 1px solid var(--border); display: block; object-fit: cover; background: var(--field); }

/* key / value list */
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 8px var(--sp-3); font-size: var(--fs-sm); }
.kv dt { color: var(--text-3); }
.kv dd { margin: 0; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

/* copy-to-clipboard value */
.copy {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 2px 6px 2px 8px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--field); color: var(--text); font-family: var(--font-mono); font-size: var(--fs-xs);
  cursor: pointer; transition: border-color var(--dur-fast) var(--ease);
}
.copy span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.copy .icon { color: var(--text-3); }
.copy:hover { border-color: var(--accent); }
.copy.is-copied { border-color: var(--ok); }
.copy.is-copied .icon { color: var(--ok); }

/* ---------- Modal / confirm sheet ------------------------------------ */
.modal-backdrop { position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; padding: var(--sp-4); background: var(--scrim); backdrop-filter: blur(3px); opacity: 0; pointer-events: none; transition: opacity var(--dur) var(--ease); }
.modal-backdrop.is-open { opacity: 1; pointer-events: auto; }
.modal { width: min(460px, 100%); background: var(--surface-1); border: 1px solid var(--border-strong); border-radius: var(--radius-xl); box-shadow: var(--shadow-3); transform: translateY(8px) scale(.98); transition: transform var(--dur) var(--ease); }
.modal-backdrop.is-open .modal { transform: none; }
.modal-head { display: flex; gap: var(--sp-3); padding: var(--sp-5) var(--sp-5) var(--sp-3); }
.modal-head .modal-icon { width: 36px; height: 36px; border-radius: var(--radius-md); display: grid; place-items: center; flex: none; background: var(--err-soft); color: var(--err); }
.modal-head p { color: var(--text-2); font-size: var(--fs-sm); margin-top: 4px; }
.modal-head .modal-icon.is-neutral { background: var(--accent-soft); color: var(--accent-text); }
.modal { max-height: calc(100vh - 32px); overflow-y: auto; }
.modal-body { padding: 0 var(--sp-5) var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-3); }
.modal-foot { padding: var(--sp-3) var(--sp-5); border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: var(--sp-2); }

/* ---------- Command palette ------------------------------------------ */
.palette { position: fixed; inset: 0; z-index: 90; display: flex; justify-content: center; align-items: flex-start; padding-top: 12vh; background: var(--scrim); opacity: 0; pointer-events: none; transition: opacity var(--dur-fast) var(--ease); }
.palette.is-open { opacity: 1; pointer-events: auto; }
.palette-box { width: min(620px, calc(100vw - 32px)); background: var(--glass); backdrop-filter: blur(20px) saturate(150%); -webkit-backdrop-filter: blur(20px) saturate(150%); border: 1px solid var(--border-strong); border-radius: var(--radius-xl); box-shadow: var(--shadow-3); overflow: hidden; }
.palette-input { display: flex; align-items: center; gap: var(--sp-3); padding: 0 var(--sp-4); border-bottom: 1px solid var(--border); }
.palette-input input { flex: 1; height: 52px; border: 0; background: none; font: inherit; font-size: var(--fs-lg); color: var(--text); outline: none; }
.palette-list { max-height: 360px; overflow-y: auto; padding: 6px; }
.palette-list .menu-item { height: 38px; }
.palette-list .menu-item[aria-selected="true"] { background: var(--accent-soft); }

/* ---------- Toasts ---------------------------------------------------- */
.toasts { position: fixed; right: var(--sp-5); bottom: calc(30px + var(--sp-4)); z-index: 95; display: flex; flex-direction: column; gap: var(--sp-2); pointer-events: none; }
.toast {
  pointer-events: auto; display: flex; align-items: center; gap: var(--sp-3);
  min-width: 280px; max-width: 420px; padding: var(--sp-3) var(--sp-4);
  background: var(--glass); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border-strong); border-radius: var(--radius-lg); box-shadow: var(--shadow-3);
  font-size: var(--fs-sm); animation: toast-in var(--dur) var(--ease);
}
.toast .icon { color: var(--ok); }
.toast.err .icon { color: var(--err); } .toast.warn .icon { color: var(--warn); }
.toast .btn { margin-left: auto; }
.toast.is-leaving { opacity: 0; transform: translateX(16px); transition: all var(--dur) var(--ease); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Tooltip (data-tip attribute) ----------------------------- */
[data-tip] { position: relative; }
[data-tip]:hover::after, [data-tip]:focus-visible::after {
  content: attr(data-tip); position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%);
  padding: 5px 8px; border-radius: var(--radius-sm); white-space: nowrap; pointer-events: none; z-index: 120;
  font-size: var(--fs-xs); font-weight: var(--fw-medium); color: var(--text); background: var(--surface-3); border: 1px solid var(--border-strong); box-shadow: var(--shadow-2);
}

/* Placement: below the element (automatic in the top bar, where "above"
   would be off-screen) and right-aligned near the right edge. */
[data-tip-pos="bottom"]:hover::after, [data-tip-pos="bottom"]:focus-visible::after,
.topbar [data-tip]:hover::after, .topbar [data-tip]:focus-visible::after { bottom: auto; top: calc(100% + 8px); }
[data-tip-align="end"]:hover::after, [data-tip-align="end"]:focus-visible::after { left: auto; right: 0; transform: none; }

/* ---------- Sticky notices (problems across all grids) --------------- */
.notices {
  position: sticky; top: var(--topbar-h); z-index: 20;
  display: flex; flex-direction: column; gap: 6px;
  margin: calc(-1 * var(--main-pt)) calc(-1 * var(--main-px)) var(--sp-5);
  padding: var(--sp-3) var(--main-px);
  background: color-mix(in oklab, var(--bg) 82%, transparent);
  backdrop-filter: blur(12px) saturate(140%); -webkit-backdrop-filter: blur(12px) saturate(140%);
  border-bottom: 1px solid var(--border);
}
.notices[hidden] { display: none; }
.notice {
  --c: var(--warn);
  display: grid; grid-template-columns: auto auto minmax(0, 1fr) auto auto; align-items: center; gap: var(--sp-3);
  min-height: 38px; padding: 4px 6px 4px 12px; border-radius: var(--radius-md);
  border: 1px solid color-mix(in oklab, var(--c) 35%, transparent);
  background: linear-gradient(90deg, color-mix(in oklab, var(--c) 15%, transparent), color-mix(in oklab, var(--c) 4%, transparent) 60%), var(--surface-1);
  font-size: var(--fs-sm);
  animation: toast-in var(--dur) var(--ease);
}
.notice.err { --c: var(--err); } .notice.info { --c: var(--info); }
.notice.task { --c: var(--accent); }   /* work waiting: new orders, tickets needing a reply */
.notice > .icon { color: var(--c); }
.notice.err > .icon { animation: bulb-alert 1.2s var(--ease) infinite; border-radius: 50%; }
.notice .n-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.notice .n-text b { font-weight: var(--fw-semibold); margin-right: 6px; }
.notice .n-text span { color: var(--text-2); }
.notice .n-time { font-size: var(--fs-xs); color: var(--text-3); white-space: nowrap; }
.notice .n-actions { display: flex; gap: 2px; }
.notice.is-leaving { opacity: 0; transform: translateY(-4px); transition: all var(--dur) var(--ease); }
.notices-more { align-self: flex-start; }

/* default-grid star */
.grid-tab .star { color: var(--warn); width: 12px; height: 12px; fill: currentColor; stroke: none; }
.btn.is-default { --btn-fg: var(--warn); }
.btn.is-default .icon { fill: currentColor; }

/* ---------- Alerts / callouts ---------------------------------------- */
.callout { --c: var(--info); display: flex; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); border-radius: var(--radius-md); border: 1px solid color-mix(in oklab, var(--c) 30%, transparent); background: color-mix(in oklab, var(--c) 9%, transparent); font-size: var(--fs-sm); }
.callout > .icon { color: var(--c); margin-top: 2px; }
.callout strong { display: block; }
.callout.ok { --c: var(--ok); } .callout.warn { --c: var(--warn); } .callout.err { --c: var(--err); }

/* ---------- Skeleton / empty ----------------------------------------- */
.skeleton { border-radius: var(--radius-sm); background: linear-gradient(90deg, var(--surface-2) 0%, var(--surface-3) 50%, var(--surface-2) 100%); background-size: 200% 100%; animation: shimmer 1.4s linear infinite; height: 12px; }
@keyframes shimmer { to { background-position: -200% 0; } }
.empty { display: flex; flex-direction: column; align-items: center; gap: var(--sp-2); padding: var(--sp-10) var(--sp-4); color: var(--text-3); text-align: center; }
.empty .icon { width: 28px; height: 28px; color: var(--text-3); }
.empty strong { color: var(--text); font-weight: var(--fw-semibold); }

/* ---------- Avatar menu / user chip ---------------------------------- */
.user-chip { display: flex; align-items: center; gap: var(--sp-2); padding: 3px 10px 3px 3px; border-radius: var(--radius-pill); border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; font-size: var(--fs-sm); color: var(--text); }
.user-chip .avatar { width: 26px; height: 26px; }

/* ---------- Responsive ----------------------------------------------- */
@media (max-width: 1100px) {
  .cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "brand" "top" "main" "foot"; grid-template-rows: auto auto 1fr auto; }
  .app-brand { border-right: 0; height: var(--topbar-h); }
  .sidebar { display: none; }
  .sidebar.is-open { display: flex; position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w); z-index: 70; box-shadow: var(--shadow-3); }
  .main { --main-pt: var(--sp-4); --main-px: var(--sp-4); }
  .notice { grid-template-columns: auto minmax(0, 1fr) auto; }
  .notice .n-grid, .notice .n-time { display: none; }
  .notice .n-text { white-space: normal; }
  .cols-2, .cols-3, .cols-4 { grid-template-columns: minmax(0, 1fr); }
  .span-2 { grid-column: auto; }
  .grid-status { flex-wrap: wrap; }
  .grid-status .gs-stats { margin-left: 0; width: 100%; justify-content: space-between; }
  .statusbar { padding: 0 var(--sp-4); }
  .statusbar .hide-sm { display: none; }
  .topbar .hide-sm { display: none; }
}
.show-sm { display: none; }
@media (max-width: 860px) { .show-sm { display: inline-flex; } }

/* Mobile navigation drawer: scrim behind the open sidebar */
@media (max-width: 860px) {
  .app.is-nav-open::after { content: ""; position: fixed; inset: 0; z-index: 69; background: var(--scrim); }
  .sidebar.is-open { padding-top: calc(var(--sp-3) + env(safe-area-inset-top, 0px)); }
  .topbar { padding: 0 var(--sp-3); gap: var(--sp-2); }
  .table-tools .search { flex: 1 1 200px; max-width: none; }
  .table-tools > h2 { font-size: var(--fs-md); }
  .page-head .actions { margin-left: 0; }
  .grid-status { padding: var(--sp-4); }
  .gs-stat b { font-size: var(--fs-lg); }
  .toasts { left: var(--sp-3); right: var(--sp-3); }
  .toast { min-width: 0; max-width: none; }
  .modal-foot .btn { flex: 1; }
  .kv { grid-template-columns: 104px minmax(0, 1fr); }
  /* keep the first column (the name) in view while a wide table scrolls sideways */
  .table th:first-child, .table td:first-child { position: sticky; left: 0; z-index: 1; background: var(--surface-1); }
  .table th:first-child { z-index: 2; }
  .table tbody tr:hover td:first-child { background: var(--surface-2); }
}

/* Phones: menus become a bottom action sheet, easy to reach with a thumb */
@media (max-width: 600px) {
  .menu {
    left: 8px !important; right: 8px; top: auto !important;
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    min-width: 0; max-height: 70vh; overflow-y: auto;
    transform: translateY(16px); transform-origin: bottom center;
  }
  .menu.static-menu { position: static; transform: none; }
  .gs-stats { gap: var(--sp-3); }
}

/* Touch screens: no hover, bigger targets */
@media (hover: none) {
  .table .row-actions { opacity: 1; }
  [data-tip]:hover::after { display: none; }
}
@media (pointer: coarse) {
  :root { --control-h: 40px; --control-h-sm: 34px; --row-h: 44px; }
  .menu-item { height: 44px; }
  .nav-item { padding-block: 10px; }
  [data-menu] { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
}
