:root {
  --navy: #0f172a;
  --navy-soft: #1a2540;
  --bg: #f4f6fb;
  --card: #ffffff;
  --border: #e6e9f2;
  --text: #10172a;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #eaf0ff;
  --good: #16a34a;
  --bad: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 23, 42, 0.04), 0 4px 16px rgba(16, 23, 42, 0.04);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Inter, -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
}

.app {
  display: flex;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 240px;
  flex: 0 0 240px;
  background: var(--navy);
  color: #fff;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, #2563eb, #7c3aed);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.5px;
}

.brand-title { font-weight: 700; font-size: 14.5px; line-height: 1.3; }
.brand-sub { font-size: 11.5px; color: #94a3b8; }

.channel-nav {
  flex: 1;
  overflow-y: auto;
  padding: 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.channel-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  color: #cbd5e1;
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 500;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
}

.channel-item:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }

.channel-item.active {
  background: var(--navy-soft);
  color: #fff;
  box-shadow: inset 3px 0 0 var(--accent);
}

.channel-icon { font-size: 15px; width: 18px; text-align: center; }
.channel-label { flex: 1; }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #475569;
  flex: 0 0 8px;
}
.status-dot.ok { background: var(--good); }
.status-dot.err { background: var(--bad); }
.status-dot.loading { background: #f59e0b; }

.sidebar-footer {
  padding: 14px 16px 18px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.last-updated { font-size: 11px; color: #94a3b8; }

.btn-ghost {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12.5px;
  cursor: pointer;
}
.btn-ghost:hover { background: rgba(255, 255, 255, 0.14); }

/* ---------- Main ---------- */
.main {
  flex: 1;
  min-width: 0;
  padding: 22px 28px 60px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 18px;
}

.topbar-title h1 { margin: 0; font-size: 21px; font-weight: 700; }
.muted { color: var(--muted); font-size: 13px; }

.date-controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

select, input[type="date"] {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text);
}

.custom-date-range { display: flex; align-items: center; gap: 6px; }
.custom-date-range.hidden { display: none; }

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary:hover { background: #1d4ed8; }

.loading-bar {
  height: 3px;
  border-radius: 3px;
  margin-bottom: 14px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  background-size: 200% 100%;
  animation: loadingSweep 1.1s infinite linear;
}
.loading-bar.hidden { display: none; }
@keyframes loadingSweep {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ---------- KPI cards ---------- */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.kpi-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 16px;
  box-shadow: var(--shadow);
}

.kpi-label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px; }
.kpi-value { font-size: 22px; font-weight: 700; margin-top: 6px; }
.kpi-delta { font-size: 12px; margin-top: 6px; font-weight: 600; }
.kpi-delta.up { color: var(--good); }
.kpi-delta.down { color: var(--bad); }
.kpi-delta.flat { color: var(--muted); }

/* ---------- Cards / charts ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px 18px;
}

.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.card-head h2 { font-size: 14.5px; margin: 0; font-weight: 700; }

.charts-row {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 16px;
  margin-bottom: 18px;
}

.chart-card { min-width: 0; }

.chart-canvas-wrap {
  position: relative;
  width: 100%;
  height: 280px;
}
.chart-canvas-wrap canvas {
  width: 100% !important;
  height: 100% !important;
}
.chart-canvas-wrap--sm { height: 220px; }

@media (max-width: 980px) {
  .charts-row { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .chart-canvas-wrap { height: 220px; }
  .chart-canvas-wrap--sm { height: 190px; }
}

/* ---------- Live ad creatives gallery ---------- */
.live-ads-card { margin-bottom: 18px; }
.ad-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 14px;
}
.ad-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--card);
}
.ad-thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.ad-thumb::before {
  content: "🖼️";
  font-size: 26px;
  opacity: 0.3;
  position: absolute;
}
.ad-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: relative;
  z-index: 1;
}
.ad-thumb--broken img { display: none; }
.ad-meta { padding: 8px 10px 10px; }
.ad-name {
  font-size: 12.5px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ad-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
  display: flex;
  align-items: center;
  gap: 5px;
}
.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  display: inline-block;
  flex: 0 0 7px;
}

/* ---------- Table ---------- */
.table-card { margin-bottom: 18px; }
.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  padding: 8px 10px;
  white-space: nowrap;
}
tbody td {
  padding: 10px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--accent-soft); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.campaign-name { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }

/* ---------- Empty state ---------- */
.empty-state {
  background: var(--card);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
}
.empty-state.hidden { display: none; }
.empty-state-icon { font-size: 28px; margin-bottom: 10px; }
.empty-state h2 { color: var(--text); font-size: 16px; margin: 0 0 6px; }
.empty-state a { color: var(--accent); text-decoration: none; font-weight: 600; font-size: 13px; }

/* ---------- Breakdown panel (per channel) ---------- */
.breakdown-card { margin-bottom: 18px; }
.breakdown-controls { display: flex; gap: 8px; }
.breakdown-card canvas { margin-bottom: 14px; }

/* ---------- Explore view ---------- */
.explore-controls { margin-bottom: 16px; }
.explore-row {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  align-items: flex-end;
  margin-bottom: 14px;
}
.explore-row:last-child { margin-bottom: 0; }

.field-block {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 200px;
  flex: 1 1 200px;
}

.field-group-label {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--muted);
}

.filter-input {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 6px 8px;
  font-size: 12px;
  background: var(--bg);
}

.field-block select {
  width: 100%;
}

.metric-picker { flex: 1 1 100%; }

.metric-checklist {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  max-height: 140px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--bg);
}
.metric-checklist label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}
.metric-checklist optgroup-label,
.metric-checklist .group-label {
  flex-basis: 100%;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 6px;
}

.explore-actions { align-items: center; gap: 10px; }

.btn-ghost-light {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn-ghost-light:hover { background: var(--accent-soft); border-color: #c7d7fb; }

.template-row, .saved-row {
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }

.chip {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 999px;
  padding: 6px 13px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.disabled { opacity: 0.4; cursor: not-allowed; }
.chip.disabled:hover { border-color: var(--border); color: var(--text); }
.chip.saved { background: var(--accent-soft); border-color: #c7d7fb; display: flex; align-items: center; gap: 6px; }
.chip.saved .chip-remove { color: var(--muted); font-weight: 700; }
.chip.saved .chip-remove:hover { color: var(--bad); }

#explorePivotTable th.sticky-col, #explorePivotTable td.sticky-col {
  position: sticky;
  left: 0;
  background: var(--card);
  font-weight: 700;
}

.currency-warning {
  background: #fff7ed;
  border: 1px solid #fed7aa;
  color: #9a3412;
  font-size: 12.5px;
  font-weight: 600;
  padding: 9px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
}
.currency-warning.hidden { display: none; }

.hidden { display: none !important; }

@media (max-width: 760px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; flex: none; height: auto; position: relative; }
  .channel-nav { flex-direction: row; overflow-x: auto; }
  .main { padding: 16px; }
}
