:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-2: #4b5563;
  --muted: #6b7280;
  --accent: #f59e0b;
  --accent-ink: #92400e;
  --dark: #111827;
  --dark-2: #1f2937;
  --spike: #c2410c;
  --drop: #b91c1c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.06);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
code { font-size: 12px; background: #eef0f3; padding: 1px 5px; border-radius: 4px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 12px 24px;
  background: var(--dark);
  color: #f9fafb;
  border-bottom: 3px solid var(--accent);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-name { font-weight: 700; letter-spacing: 0.14em; font-size: 14px; }
.brand-sub { font-size: 12px; color: #9ca3af; margin-top: 2px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.status { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; color: #d1d5db; margin-right: 6px; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: #6b7280; }
.status.ok .dot { background: #22c55e; box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2); }
.status.busy .dot { background: var(--accent); animation: pulse 1s infinite; }
.status.err .dot { background: #ef4444; }
@keyframes pulse { 50% { opacity: 0.3; } }
.auto { font-size: 12px; color: #d1d5db; display: inline-flex; align-items: center; gap: 6px; }
.auto select { background: var(--dark-2); color: #f9fafb; border: 1px solid #374151; border-radius: 6px; padding: 5px 6px; font: inherit; }

.btn {
  display: inline-flex; align-items: center; gap: 7px;
  font: inherit; font-weight: 600; font-size: 13px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid transparent; cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn-primary { background: var(--accent); color: #111827; }
.btn-primary:hover { background: #fbbf24; }
.btn-ghost { background: transparent; color: #f9fafb; border-color: #374151; }
.btn-ghost:hover { background: var(--dark-2); }
.btn-small { padding: 6px 12px; font-size: 12px; background: var(--dark); color: #fff; }
.btn-small:hover { background: var(--dark-2); }
.btn-ghost-dark { background: transparent; color: var(--text-2); border-color: var(--border); width: 100%; justify-content: center; margin-top: 4px; }
.btn-ghost-dark:hover { background: #f9fafb; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 20px; padding: 20px 24px 40px; max-width: 1600px; margin: 0 auto; }
.sidebar { position: sticky; top: 84px; align-self: start; max-height: calc(100vh - 100px); overflow-y: auto; display: flex; flex-direction: column; gap: 14px; padding-right: 2px; }
.content { display: flex; flex-direction: column; gap: 20px; min-width: 0; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.panel h3 { margin: 0 0 12px; font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; font-size: 12px; color: var(--text-2); font-weight: 500; }
.field.inline { flex-direction: row; align-items: center; gap: 6px; flex-wrap: wrap; }
.field.inline select { width: auto; }
.field input[type='text'], .field select {
  font: inherit; font-size: 13px; color: var(--text);
  padding: 7px 9px; border: 1px solid #d1d5db; border-radius: 7px; background: #fff; width: 100%;
}
.field input[type='text']:focus, .field select:focus { outline: 2px solid rgba(245, 158, 11, 0.4); border-color: var(--accent); }
.field.slider span { display: flex; justify-content: space-between; }
.field.slider output { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; }
input[type='range'] { accent-color: #d97706; width: 100%; margin: 0; }
.row { display: flex; align-items: center; justify-content: space-between; }
.link { font-size: 12px; color: var(--accent-ink); text-decoration: none; font-weight: 600; }
.link:hover { text-decoration: underline; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 10px; cursor: pointer; }
.check input { accent-color: #d97706; width: 15px; height: 15px; }
.check.strong { font-weight: 600; margin-bottom: 8px; }
.test { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 4px; }
.test.off .field { opacity: 0.4; pointer-events: none; }

.segmented { display: grid; grid-template-columns: repeat(3, 1fr); background: #f3f4f6; border-radius: 8px; padding: 3px; }
.segmented button, .filter-pills button {
  font: inherit; font-size: 12px; font-weight: 600; border: 0; background: transparent; color: var(--text-2);
  padding: 6px 8px; border-radius: 6px; cursor: pointer;
}
.segmented button.on { background: #fff; color: var(--text); box-shadow: var(--shadow); }
.filter-pills { display: flex; gap: 4px; background: #f3f4f6; border-radius: 8px; padding: 3px; }
.filter-pills button.on { background: #fff; color: var(--text); box-shadow: var(--shadow); }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 14px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.kpi::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent); }
.kpi .label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600; }
.kpi .value { font-size: 24px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.kpi .value small { font-size: 13px; color: var(--muted); font-weight: 500; margin-left: 3px; }
.kpi .hint { font-size: 12px; color: var(--text-2); margin-top: 4px; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 14px; flex-wrap: wrap; }
.card h2 { margin: 0; font-size: 16px; font-weight: 600; }
.muted { color: var(--muted); font-size: 12.5px; margin: 4px 0 0; }
.chart-wrap { position: relative; height: 420px; }
.legend-key { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); align-items: center; }
.legend-key span { display: inline-flex; align-items: center; gap: 6px; }
.tri { width: 0; height: 0; border-left: 6px solid transparent; border-right: 6px solid transparent; display: inline-block; }
.tri.up { border-bottom: 10px solid var(--spike); }
.tri.down { border-top: 10px solid var(--drop); }
.dash { width: 18px; border-top: 2px dashed #1f2a37; display: inline-block; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
table.data th { text-align: right; font-weight: 600; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; background: #fafafa; }
table.data td { text-align: right; padding: 9px 10px; border-bottom: 1px solid #f1f2f4; white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th.l, table.data td.l { text-align: left; }
table.data tbody tr:hover { background: #fffbeb; }
table.data tr.total td { font-weight: 700; background: #f9fafb; border-top: 1px solid var(--border); }
table.data .group { border-left: 1px solid var(--border); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 8px; vertical-align: -1px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.badge.spike { background: #fff1e6; color: var(--spike); }
.badge.drop { background: #fee2e2; color: var(--drop); }
.badge.zero { color: #9ca3af; }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: #f3f4f6; color: var(--text-2); margin-right: 3px; }
.event-tag { font-size: 12px; color: var(--accent-ink); }
.empty { text-align: center !important; color: var(--muted); padding: 24px !important; }

/* ---------- small multiples ---------- */
.multiples { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.mini { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 6px; cursor: pointer; transition: border-color 0.15s, box-shadow 0.15s; }
.mini:hover { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.15); }
.mini.active { border-color: var(--accent); }
.mini-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 13px; font-weight: 600; }
.mini-head .meta { font-size: 11.5px; color: var(--muted); font-weight: 500; }
.mini-chart { position: relative; height: 150px; }

.alert { background: #fef2f2; border: 1px solid #fecaca; color: #991b1b; padding: 12px 16px; border-radius: var(--radius); }
.foot { color: var(--muted); font-size: 12px; text-align: center; padding: 8px 0; }

.overlay { position: fixed; inset: 0; background: rgba(17, 24, 39, 0.45); display: grid; place-items: center; z-index: 50; }
.overlay[hidden] { display: none; }
.overlay-box { background: #fff; padding: 22px 28px; border-radius: 12px; display: flex; gap: 14px; align-items: center; font-weight: 600; box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2); }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid #fde68a; border-top-color: #d97706; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 1200px) { .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 16px; }
  .sidebar { position: static; max-height: none; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .chart-wrap { height: 320px; }
  .topbar { padding: 12px 16px; }
}
