:root {
  color-scheme: light;
  --page:          #f9f9f7;
  --surface-1:     #fcfcfb;
  --text-primary:  #0b0b0b;
  --text-secondary:#52514e;
  --text-muted:    #898781;
  --grid:          #e1e0d9;
  --baseline:      #c3c2b7;
  --border:        rgba(11, 11, 11, 0.10);
  --series-1:      #2a78d6;
  --series-1-soft: rgba(42, 120, 214, 0.12);
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page:          #0d0d0d;
    --surface-1:     #1a1a19;
    --text-primary:  #ffffff;
    --text-secondary:#c3c2b7;
    --text-muted:    #898781;
    --grid:          #2c2c2a;
    --baseline:      #383835;
    --border:        rgba(255, 255, 255, 0.10);
    --series-1:      #3987e5;
    --series-1-soft: rgba(57, 135, 229, 0.18);
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
}
.topbar h1 { margin: 0; font-size: 18px; font-weight: 650; }
.dataset-info { margin: 2px 0 0; color: var(--text-muted); font-size: 12.5px; }
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.append-toggle { color: var(--text-secondary); font-size: 12.5px; display: flex; align-items: center; gap: 6px; }

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 24px 48px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---- controls ---- */
.btn {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
}
.btn:hover { border-color: var(--baseline); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--series-1); border-color: var(--series-1); color: #fff; font-weight: 550; }
.btn-primary:hover { filter: brightness(1.07); }

.filters {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  flex-wrap: wrap;
}
.filter-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-secondary);
}
.filter-caption { font-size: 12px; }
.filter-search { flex: 1 1 200px; }
.filter-search > input {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  width: 100%;
}

/* ---- multi-select dropdown ---- */
.msel { position: relative; }
.msel-btn {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 28px 7px 10px;
  min-width: 170px;
  max-width: 280px;
  width: 100%;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
}
.msel-btn::after {
  content: "";
  position: absolute;
  right: 11px;
  top: 50%;
  width: 7px;
  height: 7px;
  border-right: 1.5px solid var(--text-muted);
  border-bottom: 1.5px solid var(--text-muted);
  transform: translateY(-70%) rotate(45deg);
}
.msel-btn:hover { border-color: var(--baseline); }
.msel-btn.has-selection { border-color: var(--series-1); }
.msel-pop[hidden] { display: none; }
.msel-pop {
  position: absolute;
  z-index: 15;
  top: calc(100% + 4px);
  left: 0;
  min-width: 240px;
  max-width: 320px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.msel-search {
  font: inherit;
  font-size: 12.5px;
  color: var(--text-primary);
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  width: 100%;
}
.msel-list {
  max-height: 260px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.msel-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 6px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-primary);
  cursor: pointer;
}
.msel-opt:hover { background: var(--series-1-soft); }
.msel-opt input { accent-color: var(--series-1); margin: 0; flex-shrink: 0; }
.msel-opt span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msel-empty { padding: 8px 6px; font-size: 12.5px; color: var(--text-muted); }
.msel-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid var(--grid);
  padding-top: 6px;
  font-size: 12px;
}
.msel-actions button {
  font: inherit;
  font-size: 12px;
  background: none;
  border: none;
  color: var(--series-1);
  cursor: pointer;
  padding: 2px 4px;
}
.msel-actions button:disabled { color: var(--text-muted); cursor: default; }
.msel-count { color: var(--text-muted); }

/* ---- KPI tiles ---- */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-label { font-size: 12.5px; color: var(--text-secondary); }
.stat-value { font-size: 30px; font-weight: 600; letter-spacing: -0.01em; }

/* ---- cards ---- */
.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
}
.card-header {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.card-header h2 { margin: 0 auto 0 0; font-size: 15px; font-weight: 650; }
.seg-group { display: flex; align-items: center; gap: 8px; }
.seg-caption { font-size: 12px; color: var(--text-muted); }
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.seg button {
  font: inherit;
  font-size: 12.5px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 6px 11px;
  cursor: pointer;
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active {
  background: var(--series-1-soft);
  color: var(--text-primary);
  font-weight: 600;
}

/* ---- breakdown chart (horizontal bars) ---- */
.chart { display: flex; flex-direction: column; gap: 6px; }
.chart.loading, .table-wrap.loading, .kpi-row.loading { opacity: 0.45; transition: opacity 0.15s; }
.bar-row {
  display: grid;
  grid-template-columns: minmax(120px, 260px) 1fr 76px;
  align-items: center;
  gap: 10px;
  min-height: 26px;
  padding: 0 4px;
  border-radius: 6px;
  cursor: pointer;
}
.bar-row:hover, .bar-row:focus-visible { background: var(--series-1-soft); outline: none; }
.bar-label {
  font-size: 12.5px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: right;
}
.bar-track { position: relative; height: 18px; border-left: 1px solid var(--baseline); }
.bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  min-width: 2px;
  background: var(--series-1);
  border-radius: 0 4px 4px 0;
}
.bar-row:hover .bar-fill { filter: brightness(1.12); }
.bar-value {
  font-size: 12.5px;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  text-align: left;
}
.chart-note { margin: 10px 0 0; font-size: 12px; color: var(--text-muted); }
.empty-state {
  padding: 32px 0;
  text-align: center;
  color: var(--text-muted);
}

/* ---- table ---- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--grid); }
th {
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 12px;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--baseline);
}
th:hover { color: var(--text-primary); }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.url-cell { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.url-cell a { color: var(--series-1); text-decoration: none; }
td.url-cell a:hover { text-decoration: underline; }
td { color: var(--text-primary); }
td.dim { color: var(--text-secondary); }

.pager {
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: flex-end;
  margin-top: 12px;
  font-size: 12.5px;
  color: var(--text-secondary);
}

/* ---- tooltip & toast ---- */
.tooltip {
  position: fixed;
  z-index: 10;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  padding: 8px 12px;
  font-size: 12.5px;
  max-width: 320px;
}
.tooltip .tt-title { font-weight: 600; margin-bottom: 4px; overflow-wrap: anywhere; }
.tooltip .tt-row { display: flex; justify-content: space-between; gap: 16px; }
.tooltip .tt-row .v { font-weight: 600; font-variant-numeric: tabular-nums; }
.tooltip .tt-row .k { color: var(--text-secondary); }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--page);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 13px;
  z-index: 20;
}
