:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --surface-hover: #f0f3f8;
  --border: #e4e6eb;
  --border-strong: #d2d6de;
  --text: #0f1419;
  --text-2: #4b5260;
  --muted: #7a818e;
  --grid: #eceef2;
  --accent: #2a78d6;
  --accent-soft: #e6f0fc;
  --accent-ink: #1c5cab;
  --series-1: #2a78d6;
  --series-1-soft: rgba(42, 120, 214, 0.14);
  --series-2: #eb6834;
  --good: #0a7d0a;
  --bad: #c62f2f;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --shadow-pop: 0 12px 32px rgba(16, 24, 40, 0.14), 0 2px 6px rgba(16, 24, 40, 0.06);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1d2128;
    --surface-hover: #222731;
    --border: #272c35;
    --border-strong: #353b47;
    --text: #f3f4f6;
    --text-2: #b7bcc6;
    --muted: #868d9a;
    --grid: #232831;
    --accent: #3987e5;
    --accent-soft: rgba(57, 135, 229, 0.16);
    --accent-ink: #86b6ef;
    --series-1: #3987e5;
    --series-1-soft: rgba(57, 135, 229, 0.22);
    --series-2: #d95926;
    --good: #3fbf3f;
    --bad: #ef6b6b;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-pop: 0 12px 32px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115;
  --surface: #171a20;
  --surface-2: #1d2128;
  --surface-hover: #222731;
  --border: #272c35;
  --border-strong: #353b47;
  --text: #f3f4f6;
  --text-2: #b7bcc6;
  --muted: #868d9a;
  --grid: #232831;
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.16);
  --accent-ink: #86b6ef;
  --series-1: #3987e5;
  --series-1-soft: rgba(57, 135, 229, 0.22);
  --series-2: #d95926;
  --good: #3fbf3f;
  --bad: #ef6b6b;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-pop: 0 12px 32px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-feature-settings: "tnum" 1, "cv11" 1;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 17px; }
h2 { font-size: 15px; }
button { font: inherit; color: inherit; cursor: pointer; }
svg:where(:not(.chart *)) { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 28px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand p { margin: 1px 0 0; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
  background: linear-gradient(135deg, #2a78d6, #1c5cab); color: #fff;
}
.brand-mark svg { width: 22px; height: 22px; stroke-width: 3; }
.topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-2);
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.i-moon { display: none; }
:root[data-theme="dark"] .i-sun { display: none; }
:root[data-theme="dark"] .i-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: none; }
  :root:not([data-theme="light"]) .i-moon { display: block; }
}

/* ---------- Filters ---------- */
.filters {
  position: sticky; top: 0; z-index: 20;
  padding: 12px 28px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
}
.filter-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.dropdown { position: relative; }
.filter-btn {
  display: flex; align-items: center; gap: 8px; height: 38px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
  box-shadow: var(--shadow); max-width: 340px;
}
.filter-btn:hover { border-color: var(--border-strong); }
.filter-btn[aria-expanded="true"] { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.filter-btn > svg:first-child { color: var(--muted); width: 16px; height: 16px; }
.filter-label { color: var(--muted); font-size: 12.5px; }
.filter-value { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.filter-btn.has-value { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); background: var(--accent-soft); }
.chev { width: 14px; height: 14px; color: var(--muted); }
.ghost-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 12px;
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px; color: var(--text-2);
  font-weight: 500;
}
.ghost-btn svg { width: 16px; height: 16px; }
.ghost-btn:hover { background: var(--surface-hover); color: var(--text); }
.primary-btn {
  height: 36px; padding: 0 14px; border: 0; border-radius: 9px; background: var(--accent); color: #fff; font-weight: 600;
}
.primary-btn:hover { filter: brightness(1.08); }
.compare-note { margin-left: auto; }

.menu {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 30; display: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-pop);
}
.dropdown.open .menu { display: flex; }
.date-menu { gap: 0; }
.presets { display: flex; flex-direction: column; padding: 8px; min-width: 180px; border-right: 1px solid var(--border); }
.presets button {
  text-align: left; padding: 8px 10px; border: 0; background: none; border-radius: 8px; color: var(--text-2);
}
.presets button:hover { background: var(--surface-hover); color: var(--text); }
.presets button.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.custom-range { display: flex; flex-direction: column; gap: 10px; padding: 14px; min-width: 220px; }
.custom-range label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.menu-title { margin: 0; font-weight: 600; font-size: 12.5px; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.04em; }
input[type="date"], input[type="search"] {
  height: 36px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); font: inherit;
}
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.list-menu { flex-direction: column; width: 420px; max-width: calc(100vw - 32px); max-height: 460px; }
.list-menu .list-search { padding: 10px; border-bottom: 1px solid var(--border); }
.list-menu .list-search input { width: 100%; }
.list-menu .list-actions { display: flex; justify-content: space-between; padding: 6px 12px; border-bottom: 1px solid var(--border); }
.list-menu .list-actions button { border: 0; background: none; color: var(--accent); font-size: 12.5px; font-weight: 600; padding: 4px 0; }
.list-items { overflow-y: auto; padding: 6px; }
.list-item {
  display: flex; align-items: flex-start; gap: 10px; padding: 8px; border-radius: 8px; cursor: pointer;
}
.list-item:hover { background: var(--surface-hover); }
.list-item input { margin: 2px 0 0; accent-color: var(--accent); width: 16px; height: 16px; flex: none; }
.list-item .li-name { font-weight: 500; word-break: break-word; }
.list-item .li-meta { font-size: 12px; color: var(--muted); }
.list-group { padding: 8px 8px 4px; font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.list-empty { padding: 20px; text-align: center; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips:not(:empty) { margin-top: 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 6px 4px 10px; max-width: 100%;
  border-radius: 999px; background: var(--accent-soft); color: var(--accent-ink); font-size: 12.5px; font-weight: 500;
}
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip small { color: var(--muted); font-weight: 500; }
.chip button { border: 0; background: none; color: inherit; display: grid; place-items: center; padding: 2px; border-radius: 50%; }
.chip button:hover { background: color-mix(in srgb, var(--accent) 20%, transparent); }
.chip svg { width: 12px; height: 12px; }

/* ---------- Layout ---------- */
.content { padding: 22px 28px 40px; display: flex; flex-direction: column; gap: 16px; max-width: 1600px; margin: 0 auto; transition: opacity 0.2s; }
.content.loading { opacity: 0.55; pointer-events: none; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; min-width: 0;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head p { margin: 2px 0 0; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr); gap: 16px; }
.grid-2.equal { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.chart { width: 100%; height: 320px; }

/* ---------- KPIs ---------- */
.kpis-hero { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.kpis-secondary { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 16px; }
.kpi {
  position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px; display: flex; flex-direction: column; gap: 4px; min-width: 0; overflow: hidden;
}
.kpi-label { display: flex; align-items: center; gap: 8px; color: var(--text-2); font-weight: 500; font-size: 13px; }
.kpi-icon { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent-ink); }
.kpi-icon svg { width: 15px; height: 15px; }
.kpi-value { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpis-hero .kpi-value { font-size: 28px; margin-top: 6px; }
.kpi-foot { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 12.5px; color: var(--muted); }
.delta { display: inline-flex; align-items: center; gap: 2px; font-weight: 600; }
.delta svg { width: 13px; height: 13px; stroke-width: 2.5; }
.delta.good { color: var(--good); }
.delta.bad { color: var(--bad); }
.delta.neutral { color: var(--text-2); }
.kpi-hint { font-size: 11.5px; color: var(--muted); }
.spark { height: 40px; margin-top: 8px; }
.spark svg { width: 100%; height: 40px; overflow: visible; }
.spark path.line { stroke: var(--series-1); stroke-width: 2; fill: none; }
.spark path.area { fill: var(--series-1-soft); stroke: none; }

/* ---------- Metric tabs / segmented ---------- */
.seg { display: inline-flex; padding: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; }
.seg button { border: 0; background: none; padding: 5px 12px; border-radius: 7px; color: var(--text-2); font-weight: 500; font-size: 13px; }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.metric-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.metric-tabs button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 12px;
  font-size: 12.5px; color: var(--text-2); font-weight: 500;
}
.metric-tabs button:hover { border-color: var(--border-strong); color: var(--text); }
.metric-tabs button.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Funnel ---------- */
.funnel { display: flex; flex-direction: column; gap: 4px; }
.f-stage { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 4px 12px; }
.f-name { font-weight: 500; color: var(--text-2); }
.f-value { font-weight: 700; font-size: 18px; text-align: right; }
.f-bar { grid-column: 1 / -1; height: 30px; background: var(--surface-2); border-radius: 6px; overflow: hidden; }
.f-bar i { display: block; height: 100%; border-radius: 6px; background: var(--series-1); min-width: 4px; transition: width 0.5s ease; }
.f-step {
  display: flex; align-items: center; gap: 8px; padding: 8px 0 8px 10px; margin-left: 10px;
  border-left: 2px dashed var(--border-strong); color: var(--muted); font-size: 12.5px;
}
.f-step strong { color: var(--text); font-size: 13px; }
.f-step .f-cost { margin-left: auto; }
.funnel-note { margin: 10px 0 0; }

/* ---------- Table ---------- */
.table-card { padding-bottom: 8px; }
.table-tools { display: flex; gap: 8px; flex-wrap: wrap; }
.search { position: relative; display: flex; align-items: center; }
.search svg { position: absolute; left: 10px; width: 15px; height: 15px; color: var(--muted); }
.search input { padding-left: 32px; width: 240px; }
.table-hint { margin: -4px 0 10px; }
.table-wrap { overflow-x: auto; margin: 0 -20px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; padding-left: 20px; }
th:last-child, td:last-child { padding-right: 20px; }
th {
  position: sticky; top: 0; background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: 12px;
  cursor: pointer; user-select: none;
}
th:hover { color: var(--text); }
th.sorted { color: var(--accent-ink); }
th .arrow { font-size: 10px; margin-left: 3px; }
tbody tr { cursor: pointer; }
tbody tr:hover td { background: var(--surface-hover); }
tfoot td { font-weight: 700; background: var(--surface-2); border-bottom: 0; }
.name-cell { max-width: 380px; min-width: 220px; white-space: normal; }
.name-cell .nm { font-weight: 600; color: var(--text); display: block; }
.name-cell .sub { color: var(--muted); font-size: 11.5px; display: block; }
.share { display: flex; align-items: center; justify-content: flex-end; gap: 8px; }
.share-bar { width: 54px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.share-bar i { display: block; height: 100%; background: var(--series-1); border-radius: 3px; }
.dim { color: var(--muted); }
.pill-meet { display: inline-block; min-width: 22px; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-ink); font-weight: 700; text-align: center; }

.footnote { margin: 4px 2px 0; line-height: 1.6; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow-pop); font-weight: 500;
}

/* ---------- Responsive ---------- */
@media (max-width: 1280px) {
  .kpis-secondary { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 1080px) {
  .grid-2, .grid-2.equal { grid-template-columns: minmax(0, 1fr); }
  .kpis-hero { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .compare-note { margin-left: 0; width: 100%; }
}
@media (max-width: 640px) {
  .topbar, .filters { padding-left: 16px; padding-right: 16px; }
  .content { padding: 16px; }
  .kpis-hero, .kpis-secondary { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpis-hero .kpi-value { font-size: 22px; }
  .kpi-value { font-size: 18px; }
  .kpi { padding: 12px 14px; }
  .dropdown, .filter-btn { width: 100%; max-width: none; }
  .filter-value { margin-left: auto; }
  .menu { left: 0; right: 0; width: auto; }
  .date-menu { flex-direction: column; }
  .presets { border-right: 0; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 1fr 1fr; }
  .search input { width: 100%; }
  .table-tools, .search { width: 100%; }
  .card { padding: 14px 16px; }
  .table-wrap { margin: 0 -16px; }
  .chart { height: 280px; }
}

/* ---------- View tabs ---------- */
.view-tabs { display: flex; gap: 4px; padding: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 11px; }
.view-tabs button { border: 0; background: none; padding: 7px 14px; border-radius: 8px; font-weight: 500; color: var(--text-2); white-space: nowrap; }
.view-tabs button.active { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.view { display: flex; flex-direction: column; gap: 16px; }

/* ---------- Ads panel ---------- */
.ads-summary { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.ads-toolbar { align-items: center; }
.toolbar-group { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.select { display: flex; align-items: center; gap: 8px; }
select {
  height: 36px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); color: var(--text); font: inherit;
}
#adsNote { margin: -4px 0 14px; }
.ads-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); gap: 14px; }
.ad-card {
  border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; background: var(--surface);
  display: flex; flex-direction: column; gap: 10px; min-width: 0; transition: border-color 0.15s, box-shadow 0.15s;
}
.ad-card:hover { border-color: var(--border-strong); box-shadow: var(--shadow-pop); }
.ad-card.inactive { background: var(--surface-2); }
.ad-top { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.ad-name { font-weight: 700; font-size: 14px; word-break: break-word; }
.ad-camp { color: var(--muted); font-size: 12px; word-break: break-word; margin-top: 2px; }
.status { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status.on { color: var(--good); background: color-mix(in srgb, var(--good) 13%, transparent); }
.status.off { color: var(--muted); background: color-mix(in srgb, var(--muted) 15%, transparent); }
.ad-dates { font-size: 12px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.ad-dates svg { width: 14px; height: 14px; color: var(--muted); }
.ad-spark svg { width: 100%; height: 36px; display: block; }
.ad-spark path.line { stroke: var(--series-1); stroke-width: 2; fill: none; }
.ad-spark path.area { fill: var(--series-1-soft); }
.ad-card.inactive .ad-spark path.line { stroke: var(--muted); }
.ad-card.inactive .ad-spark path.area { fill: color-mix(in srgb, var(--muted) 14%, transparent); }
.ad-metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.ad-metrics div { min-width: 0; }
.ad-metrics span { display: block; font-size: 11px; color: var(--muted); }
.ad-metrics b { display: block; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ad-actions { display: flex; justify-content: flex-end; }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 600; font-size: 12.5px; padding: 2px 0; display: inline-flex; align-items: center; gap: 4px; }
.link-btn svg { width: 14px; height: 14px; }
.link-btn:hover { text-decoration: underline; }
.empty { padding: 40px 10px; text-align: center; color: var(--muted); grid-column: 1 / -1; }

@media (max-width: 1280px) { .ads-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; }
  .view-tabs { order: 3; width: 100%; }
  .view-tabs button { flex: 1; }
}
@media (max-width: 640px) {
  .ads-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .toolbar-group, .select, .select select { width: 100%; }
  .select select { flex: 1; }
  .ads-grid { grid-template-columns: minmax(0, 1fr); }
}
