:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --border: #e2e6ec;
  --text: #1c2430;
  --muted: #6b7684;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --good: #16a34a;
  --bad: #dc2626;
  --radius: 10px;
}

* { box-sizing: border-box; }

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

.nav-bar {
  display: flex;
  align-items: center;
  gap: 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  flex-wrap: wrap;
}

.nav-brand {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
}

.nav-links {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  flex: 1;
}

.nav-links a {
  color: var(--muted);
  text-decoration: none;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
}

.nav-links a:hover { background: var(--bg); color: var(--text); }
.nav-links a.active { background: var(--accent); color: #fff; }

.logout-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
}
.logout-btn:hover { background: var(--bg); }

.page {
  width: 100%;
  margin: 0;
  padding: 24px;
}

.filter-field.search-field { flex: 1; min-width: 200px; }
.filter-field.search-field input { width: 100%; min-width: 0; }

.filter-bar {
  display: flex;
  gap: 16px;
  align-items: flex-end;
  flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 20px;
}

.filter-field { display: flex; flex-direction: column; gap: 6px; }
.filter-field label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; }
.filter-field select,
.filter-field input {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 14px;
  min-width: 140px;
}

.filter-bar button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.filter-bar button:hover { background: var(--accent-dark); }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.tile {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.tile .tile-label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; }
.tile .tile-value { font-size: 26px; font-weight: 700; margin-top: 6px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 20px;
}

.panel h2 { margin: 0 0 14px; font-size: 15px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; border-radius: 8px; overflow: hidden; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid #e0e7ff; white-space: nowrap; }
th {
  color: #fff;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  position: sticky;
  top: 0;
  background: linear-gradient(135deg, #4f46e5, #2563eb 55%, #0891b2);
  border-bottom: 0;
  text-align: center;
  vertical-align: middle;
}
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { background: linear-gradient(135deg, #4338ca, #1d4ed8 55%, #0e7490); }
th.sortable::after { content: '\2195'; margin-left: 6px; opacity: 0.6; font-size: 11px; }
th.sorted-asc::after { content: '\2191'; opacity: 1; color: #fde047; }
th.sorted-desc::after { content: '\2193'; opacity: 1; color: #fde047; }
tbody tr:nth-child(odd) { background: #ffffff; }
tbody tr:nth-child(even) { background: #eef2ff; }
tbody tr:hover { background: #fef3c7; }
/* Metric-name column (first cell) on the Daily/Monthly pivot tables and the first column elsewhere */
tbody td:first-child { font-weight: 600; color: #3730a3; background: rgba(79, 70, 229, 0.07); }
/* Tint column groups so wide tables are easier to scan */
tbody td:nth-child(3n+2) { color: #0f766e; }
tbody td:nth-child(3n) { color: #b45309; }
.table-scroll { overflow-x: auto; }

.status { color: var(--muted); font-size: 13px; padding: 20px 0; }
.error { color: var(--bad); }

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 320px;
  text-align: center;
}
.login-card h1 { font-size: 18px; margin-bottom: 20px; }
.login-card input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 14px;
  margin-bottom: 12px;
}
.login-card button {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.login-card button:hover { background: var(--accent-dark); }

.chat-wrap { display: flex; flex-direction: column; height: calc(100vh - 160px); background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); }
.chat-messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.chat-msg { max-width: 75%; padding: 10px 14px; border-radius: 12px; font-size: 14px; line-height: 1.4; white-space: pre-wrap; }
.chat-msg.user { align-self: flex-end; background: var(--accent); color: #fff; }
.chat-msg.assistant { align-self: flex-start; background: var(--bg); }
.chat-input-row { display: flex; gap: 10px; padding: 14px; border-top: 1px solid var(--border); }
.chat-input-row input { flex: 1; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); font-size: 14px; }
.chat-input-row button { background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 10px 18px; font-weight: 600; cursor: pointer; }

.filter-bar .download-btn { background: #059669; }
.filter-bar .download-btn:hover { background: #047857; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }

a.trend-link { color: #4f46e5; text-decoration: underline dotted; cursor: pointer; }
a.trend-link:hover { color: #1d4ed8; text-decoration: underline; }
.modal-backdrop { display: none; position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55); align-items: center; justify-content: center; z-index: 1000; padding: 20px; }
.modal-card { background: var(--panel); border-radius: var(--radius); padding: 20px; width: min(1000px, 100%); max-height: 90vh; overflow: auto; box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3); }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.modal-head h3 { margin: 0; font-size: 15px; word-break: break-word; }
.modal-close { background: none; border: 0; font-size: 26px; line-height: 1; cursor: pointer; color: var(--muted); }

.section-title { margin: 0 0 4px; font-size: 18px; }
.section-title span { color: var(--muted); font-weight: 400; font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; margin: 0 0 16px; }
.chart-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 16px; }
.chart-grid .panel { margin: 0; }
.panel-total { float: right; color: var(--text); text-transform: none; letter-spacing: 0; font-size: 16px; font-weight: 700; }
.error-text { color: var(--bad); font-size: 12px; font-weight: 400; }

.chart-subtitle { margin: 18px 0 8px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.ads-totals { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.ads-totals b { color: var(--text); }

/* Loading indicator: spinner + pulsing text */
.status.loading { display: flex; align-items: center; gap: 10px; animation: loading-pulse 1.4s ease-in-out infinite; }
.status.loading::before {
  content: '';
  width: 18px;
  height: 18px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: loading-spin 0.8s linear infinite;
  flex-shrink: 0;
}
@keyframes loading-spin { to { transform: rotate(360deg); } }
@keyframes loading-pulse { 50% { opacity: 0.45; } }
@media (prefers-reduced-motion: reduce) { .status.loading, .status.loading::before { animation-duration: 3s; } }

/* Worldwide totals on the dashboard */
.summary-title { margin: 0 0 4px; font-size: 18px; }
.summary-title span { color: var(--muted); font-weight: 400; font-size: 14px; }
.tiles.summary { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 24px; }

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

.insights-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 420px;
  max-width: 100vw;
  background: var(--panel);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.08);
  display: none;
  flex-direction: column;
  z-index: 1000;
}
.insights-panel.open { display: flex; }
.insights-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.insights-head button {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  cursor: pointer;
  color: var(--muted);
}
.insights-head .insights-close { font-size: 18px; line-height: 1; margin-left: 6px; }
.insights-body { padding: 4px 18px 18px; overflow-y: auto; font-size: 14px; line-height: 1.5; }
.insights-body h3 { font-size: 14px; margin: 18px 0 6px; }
.insights-body ul { margin: 0; padding-left: 20px; }
.insights-body li { margin-bottom: 4px; }
.insights-note { color: var(--muted); }
.insights-error { color: var(--bad); }
