* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: #0f1419;
  color: #e6e9ef;
  font-size: 14px;
  line-height: 1.5;
}

.hidden { display: none !important; }
.muted { color: #8892a0; }
.small { font-size: 12px; }
.error { color: #ff6b6b; min-height: 18px; margin: 6px 0 0; font-size: 13px; }
.empty-state { padding: 32px; text-align: center; color: #8892a0; font-size: 13px; }

/* Custom scrollbars (dark) */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: #0d1117; }
::-webkit-scrollbar-thumb { background: #30363d; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #484f58; }

/* ========= Login ========= */
.auth-view {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: radial-gradient(circle at 30% 20%, #1a2332 0%, #0a0e14 70%);
  padding: 20px;
}
.auth-card {
  background: #161b22;
  padding: 40px;
  border-radius: 12px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  border: 1px solid #21262d;
}
.auth-card h1 { margin: 0 0 8px; font-size: 28px; }
.auth-card form { display: flex; flex-direction: column; }
.auth-card label { margin: 14px 0 6px; font-size: 13px; color: #8892a0; }
.auth-card input { padding: 10px 12px; border-radius: 6px; border: 1px solid #30363d;
  background: #0d1117; color: #e6e9ef; font-size: 14px; }
.auth-card input:focus { outline: none; border-color: #1f6feb; }
.auth-card button { margin-top: 24px; padding: 12px;
  background: #1f6feb; color: white; border: none; border-radius: 6px;
  font-size: 15px; cursor: pointer; font-weight: 600; }
.auth-card button:hover { background: #388bfd; }

/* ========= App layout ========= */
#app-view { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px; flex-shrink: 0;
  background: #0d1117; border-right: 1px solid #21262d;
  padding: 20px 14px; display: flex; flex-direction: column; gap: 16px;
  position: sticky; top: 0; height: 100vh;
  overflow-y: auto;
}
.brand { font-size: 18px; font-weight: 700; padding: 4px 6px; }

.app-switcher { display: flex; flex-direction: column; gap: 6px; padding: 0 6px; }
.app-switcher label { font-size: 11px; color: #8892a0; text-transform: uppercase; letter-spacing: 0.5px; }
.app-switcher select { padding: 8px; background: #161b22; color: #e6e9ef;
  border: 1px solid #30363d; border-radius: 6px; font-size: 13px; cursor: pointer; }
.app-switcher select:focus { outline: none; border-color: #1f6feb; }

.ghost-btn {
  background: transparent; border: 1px solid #30363d; color: #c9d1d9;
  padding: 6px 10px; border-radius: 6px; font-size: 12px; cursor: pointer;
  transition: background 0.12s ease;
}
.ghost-btn:hover { background: #161b22; }

nav { display: flex; flex-direction: column; gap: 2px; }
.nav-btn {
  text-align: left; padding: 10px 12px;
  background: transparent; color: #c9d1d9; border: none; border-radius: 6px;
  cursor: pointer; font-size: 14px;
  transition: background 0.12s ease, color 0.12s ease;
}
.nav-btn:hover { background: #161b22; }
.nav-btn.active { background: #1f6feb22; color: #58a6ff; font-weight: 600; }
.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding: 6px; font-size: 12px; }
.sidebar-footer #user-email {
  color: #8892a0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

main { flex: 1; min-width: 0; padding: 24px 32px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px; gap: 12px; flex-wrap: wrap;
}
.topbar h2 { margin: 0; font-size: 22px; }
.filters { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.filters label { color: #8892a0; font-size: 12px; }
.filters select { padding: 6px 10px; background: #161b22; color: #e6e9ef;
  border: 1px solid #30363d; border-radius: 6px; cursor: pointer; }

.custom-range {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 8px;
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 6px;
}
.custom-range input[type="date"] {
  padding: 4px 8px;
  background: #161b22;
  color: #e6e9ef;
  border: 1px solid #30363d;
  border-radius: 4px;
  font-size: 12px;
  font-family: inherit;
  /* Show the calendar picker icon in light colors on the dark theme. */
  color-scheme: dark;
}
.custom-range input[type="date"]:focus { outline: none; border-color: #1f6feb; }
.ghost-btn.primary {
  background: #1f6feb; color: #fff; border-color: #1f6feb; font-weight: 600;
}
.ghost-btn.primary:hover { background: #388bfd; border-color: #388bfd; }

.live-pill {
  background: #161b22; padding: 6px 12px; border-radius: 20px;
  border: 1px solid #30363d; color: #8892a0; font-size: 12px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px;
}
.live-pill.has-live { color: #3fb950; }
.live-pill.has-live::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: #3fb950; box-shadow: 0 0 6px #3fb950;
  animation: pulse 1.6s ease-in-out infinite;
}
.live-pill::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%; background: #484f58;
}
@keyframes pulse { 50% { opacity: 0.4; } }

.tab { display: none; }
.tab.active { display: block; }

/* ========= KPI cards ========= */
.kpi-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px; margin-bottom: 20px;
}
.kpi {
  background: #161b22; border: 1px solid #21262d; border-radius: 10px;
  padding: 16px; display: flex; flex-direction: column; gap: 6px;
  min-width: 0;
}
.kpi-label { font-size: 11px; color: #8892a0; text-transform: uppercase; letter-spacing: 0.5px; }
.kpi-value {
  font-size: 22px; font-weight: 700; color: #58a6ff;
  font-variant-numeric: tabular-nums;
  word-break: break-all;
}

.card {
  background: #161b22; border: 1px solid #21262d; border-radius: 10px;
  padding: 18px; margin-bottom: 18px;
}
.card h3 {
  margin: 0 0 14px; font-size: 16px; font-weight: 600;
  display: flex; align-items: baseline; gap: 8px;
}
.card p { margin: 0 0 14px; }

/* ========= Charts =========
   Wrapping every canvas in `.chart-wrap` with an explicit pixel height fixes
   the Chart.js "infinite growth" bug — without a sized parent + maintainAspectRatio:false
   the canvas keeps expanding on each render. */
.chart-wrap {
  position: relative;
  height: 320px;
  width: 100%;
  margin-bottom: 16px;
}
.chart-wrap.tall { height: 380px; }

/* ========= Tab toolbar (search + filter dropdowns above each table) ========= */
.tab-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 14px;
}
.filter-input, .filter-select {
  padding: 8px 12px;
  background: #0d1117;
  border: 1px solid #30363d;
  color: #e6e9ef;
  border-radius: 6px;
  font-size: 13px;
}
.filter-input::placeholder { color: #6e7681; }
.filter-input:focus, .filter-select:focus { outline: none; border-color: #1f6feb; }
.filter-input { flex: 1; min-width: 220px; max-width: 460px; }
.filter-select { min-width: 150px; cursor: pointer; }
.filter-count {
  margin-left: auto;
  color: #8892a0;
  font-size: 12px;
  white-space: nowrap;
}
.no-results-row td { text-align: center; color: #8892a0; padding: 24px !important; }

/* ========= Pagination ========= */
.pagination-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.pagination-bar .page-info {
  margin-right: auto;
  color: #8892a0;
  font-size: 12px;
  padding: 0 4px;
}
.pager-btn {
  background: #161b22;
  border: 1px solid #30363d;
  color: #c9d1d9;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  min-width: 36px;
  transition: background 0.12s ease;
}
.pager-btn:hover:not(:disabled) { background: #21262d; border-color: #484f58; }
.pager-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.pagination-bar .page-size {
  padding: 6px 10px;
  background: #161b22;
  border: 1px solid #30363d;
  color: #e6e9ef;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  margin-left: 6px;
}
.pagination-bar .page-size:focus { outline: none; border-color: #1f6feb; }

/* ========= Tables ========= */
.table-wrap {
  overflow: auto;
  max-height: 65vh;
  border-radius: 8px;
  border: 1px solid #21262d;
  background: #0d1117;
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td {
  padding: 10px 12px; text-align: left;
  border-bottom: 1px solid #21262d;
  vertical-align: middle;
}
td { color: #c9d1d9; }
th {
  color: #8892a0; font-weight: 600;
  text-transform: uppercase; font-size: 11px; letter-spacing: 0.4px;
  background: #0d1117; position: sticky; top: 0; z-index: 1;
  white-space: nowrap;
}
tbody tr:hover { background: #1a2030; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: #1f2a3d; }
.ads-count { color: #f778ba; }
td code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px; color: #c9d1d9;
}

/* ========= Modal ========= */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
  backdrop-filter: blur(2px);
}
.modal-card {
  background: #161b22; border: 1px solid #30363d; border-radius: 10px;
  width: 880px; max-width: 100%; max-height: 85vh;
  padding: 22px; overflow: hidden;
  display: flex; flex-direction: column; gap: 14px;
}
.apps-modal-card { width: 1180px; }
.apps-table-wrap { flex: 1; min-height: 0; }
#apps-table th:nth-child(1), #apps-table td:nth-child(1) { min-width: 200px; white-space: nowrap; }
#apps-table th:nth-child(2), #apps-table td:nth-child(2) { min-width: 240px; white-space: nowrap; }
#apps-table th:nth-child(4), #apps-table td:nth-child(4) { white-space: nowrap; }
#apps-table th:nth-child(5), #apps-table td:nth-child(5) { white-space: nowrap; }
.modal-header { display: flex; align-items: center; justify-content: space-between; }
.modal-header h3 { margin: 0; font-size: 18px; }
.close-btn {
  background: transparent; border: none; color: #c9d1d9;
  font-size: 26px; cursor: pointer; line-height: 1;
  padding: 4px 10px; border-radius: 6px;
}
.close-btn:hover { background: #21262d; color: #fff; }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form input {
  flex: 1; min-width: 180px; padding: 9px 12px;
  background: #0d1117; border: 1px solid #30363d; color: #e6e9ef; border-radius: 6px;
  font-size: 13px;
}
.inline-form input:focus { outline: none; border-color: #1f6feb; }
.inline-form button {
  padding: 9px 18px; background: #1f6feb; color: white;
  border: none; border-radius: 6px; cursor: pointer; font-weight: 600;
  white-space: nowrap;
}
.inline-form button:hover { background: #388bfd; }

/* ========= Apps table inside modal ========= */
.api-key-cell {
  display: flex; align-items: center; gap: 6px;
  max-width: 360px;
}
code.api-key {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  background: #0d1117; padding: 5px 8px; border-radius: 4px;
  font-size: 11px; color: #79c0ff;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1; min-width: 0;
  border: 1px solid #21262d;
}
.actions {
  display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap;
}
button.copy-btn, button.regen-btn, button.danger-btn {
  background: transparent; border: 1px solid #30363d; color: #c9d1d9;
  padding: 5px 10px; border-radius: 4px; font-size: 12px; cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease;
}
button.copy-btn:hover, button.regen-btn:hover { background: #21262d; }
button.danger-btn { color: #ff6b6b; border-color: #4a1f1f; }
button.danger-btn:hover { background: #2d1212; }

/* ========= User detail modal ========= */
.user-modal-card { width: 920px; }
.user-modal-body { overflow-y: auto; padding-right: 4px; }
.user-modal-body h4 {
  margin: 22px 0 10px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #8892a0;
  font-weight: 600;
}
.user-info {
  background: #0d1117;
  border: 1px solid #21262d;
  border-radius: 8px;
  padding: 12px 16px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 24px;
}
.user-info-row {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px; padding: 4px 0; font-size: 13px;
  border-bottom: 1px dashed #21262d;
}
.user-info-row:last-child,
.user-info-row:nth-last-child(2) { border-bottom: none; }
.user-info-row > span:first-child {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.4px;
}
.user-info-row > span:last-child { text-align: right; min-width: 0; word-break: break-all; }

.kpi-grid.kpi-mini { margin: 16px 0 0; gap: 10px; }
.kpi-grid.kpi-mini .kpi { padding: 12px; }
.kpi-grid.kpi-mini .kpi-value { font-size: 18px; }

.event-badge {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 11px; font-weight: 600; font-family: ui-monospace, monospace;
  background: #21262d; color: #c9d1d9;
}
.event-badge.event-screen_view { background: #0e2a3a; color: #58a6ff; }
.event-badge.event-ad_shown    { background: #2a1530; color: #f778ba; }
.event-badge.event-app_open    { background: #0e2a14; color: #3fb950; }
.event-badge.event-app_close   { background: #2a1014; color: #ff6b6b; }

@media (max-width: 700px) {
  .user-info { grid-template-columns: 1fr; }
}

/* ========= Status pills ========= */
.status-active, .status-inactive, .status-uninstalled {
  display: inline-block; padding: 3px 10px; border-radius: 12px;
  font-size: 11px; font-weight: 600;
}
.status-active     { color: #3fb950; background: #0e2a14; border: 1px solid #1d4022; }
.status-inactive   { color: #8892a0; background: #161b22; border: 1px solid #30363d; }
.status-uninstalled{ color: #ff6b6b; background: #2a1014; border: 1px solid #4a1f1f; }

/* ========= Responsive ========= */
@media (max-width: 900px) {
  #app-view { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: static;
    flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px;
    padding: 12px;
  }
  .sidebar nav { flex-direction: row; flex-wrap: wrap; flex: 1 1 100%; }
  .nav-btn { flex: 1 1 auto; text-align: center; padding: 8px 12px; }
  .sidebar-footer { flex-direction: row; margin-top: 0; flex: 1 1 100%; justify-content: flex-end; }
  main { padding: 16px; }
  .kpi-value { font-size: 18px; }
  .chart-wrap { height: 260px; }
}
