/* Client Statistics — UI theme.

   Tokens are defined once on :root (dark, the default) and redefined as a set under
   .theme-light. Components reference roles only, never raw hex, so the two themes
   stay in step. The chart surfaces (--surface-1) are the two colours the data-viz
   accent was contrast-validated against; changing them means revalidating.
*/

:root {
  color-scheme: dark;

  --bg:            #0f0f0e;
  --surface-1:     #1a1a19;   /* cards, chart surface */
  --surface-2:     #232322;   /* inputs, raised chips */
  --surface-hover: #2b2b29;

  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #8a8a80;

  --border:        rgba(255, 255, 255, .09);
  --border-strong: rgba(255, 255, 255, .17);

  --accent:        #3987e5;
  --accent-soft:   rgba(57, 135, 229, .16);
  --accent-ink:    #ffffff;
  --danger:        #e66767;

  --grid:          rgba(255, 255, 255, .07);

  /* Status palette: fixed in both themes, and never the only carrier of meaning
     -- every use is paired with a number or a label. */
  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-critical: #d03b3b;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .40);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, .34);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, .46);

  --r-lg: 16px;
  --r-md: 12px;
  --r-sm: 9px;

  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
          "Apple Color Emoji", "Segoe UI Emoji";
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;

  --ease: cubic-bezier(.2, .7, .3, 1);
}

.theme-light {
  color-scheme: light;

  --bg:            #f7f7f5;
  --surface-1:     #fcfcfb;
  --surface-2:     #ffffff;
  --surface-hover: #f0efec;

  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #78776f;

  --border:        rgba(0, 0, 0, .10);
  --border-strong: rgba(0, 0, 0, .22);

  --accent:        #2a78d6;
  --accent-soft:   rgba(42, 120, 214, .12);
  --danger:        #c8322f;

  --grid:          rgba(0, 0, 0, .07);

  --shadow-sm: 0 1px 2px rgba(16, 15, 12, .06);
  --shadow-md: 0 4px 16px rgba(16, 15, 12, .07);
  --shadow-lg: 0 16px 48px rgba(16, 15, 12, .10);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.hidden { display: none !important; }
.muted  { color: var(--text-muted); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------------------------------------------------------------- controls */

input, select, button { font: inherit; }

input[type="text"], input[type="password"], input[type="date"], select {
  width: 100%;
  padding: 9px 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text-primary);
  outline: none;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
input:hover, select:hover { border-color: var(--border-strong); }
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

select {
  appearance: none;
  padding-right: 32px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 16px) 52%, calc(100% - 11px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 9px 15px;
  border-radius: var(--r-sm);
  border: 1px solid transparent;
  font-weight: 560;
  font-size: 13.5px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s var(--ease), border-color .15s var(--ease),
              transform .1s var(--ease), color .15s var(--ease);
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary   { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.theme-light .btn-primary { color: #fff; }

.btn-secondary { background: var(--surface-2); color: var(--text-primary); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-hover); border-color: var(--border-strong); }

.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { color: var(--text-primary); background: var(--surface-2); }

.fullWidth { width: 100%; }

/* ------------------------------------------------------------------- login */

.loginWrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 480px at 50% -10%, var(--accent-soft), transparent 70%),
    var(--bg);
}

.loginCard {
  width: 100%;
  max-width: 370px;
  padding: 32px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
}

.loginMark {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  margin: 0 auto 16px;
  border-radius: 11px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -.03em;
}

.loginTitle {
  margin: 0 0 4px;
  font-size: 19px;
  font-weight: 640;
  letter-spacing: -.02em;
  text-align: center;
}
.loginSub {
  margin: 0 0 24px;
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
}

.field { display: block; margin-bottom: 14px; }
.field .label {
  display: block;
  margin-bottom: 6px;
  font-size: 12.5px;
  font-weight: 520;
  color: var(--text-secondary);
}

.loginError {
  margin: 0 0 14px;
  padding: 9px 11px;
  font-size: 12.5px;
  color: var(--danger);
  background: var(--surface-2);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  border: 1px solid var(--danger);
  border-color: color-mix(in srgb, var(--danger) 30%, transparent);
  border-radius: var(--r-sm);
}

/* ------------------------------------------------------------------- shell */

.app { min-height: 100dvh; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brandMark {
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: -.03em;
  flex-shrink: 0;
}
.title { font-size: 15.5px; font-weight: 620; letter-spacing: -.01em; }
.subtitle { font-size: 12.5px; color: var(--text-muted); }

.topbarRight { display: flex; align-items: center; gap: 8px; }

.themeSwitch {
  display: flex;
  padding: 2px;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
}
.themeBtn {
  padding: 4px 9px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  line-height: 1.3;
  transition: background .15s var(--ease), color .15s var(--ease);
}
.themeBtn:hover { color: var(--text-primary); }
.themeBtn.active { background: var(--surface-hover); color: var(--text-primary); }

.main {
  max-width: 1320px;
  margin: 0 auto;
  padding: 22px 24px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* -------------------------------------------------------------------- card */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}

.cardHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.cardTitle { font-size: 13.5px; font-weight: 620; letter-spacing: -.01em; }
.cardHint  { font-size: 12.5px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* ----------------------------------------------------------------- filters */

.filters { padding: 16px 18px; }

.filterRow {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
}
.filterRow .field { margin: 0; min-width: 210px; }
.filterRow .field.grow { flex: 1 1 240px; }
.actions { display: flex; gap: 8px; }

/* ---------------------------------------------------------- date dropdown */

/* Presets as rows: nobody fights a calendar grid for "last 30 days".
   The custom range sits behind a hairline in the footer. */
.dropdown { position: relative; }

.dropdownTrigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 9px 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text-primary);
  cursor: pointer;
  text-align: left;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
.dropdownTrigger:hover { border-color: var(--border-strong); }
.dropdown.isOpen .dropdownTrigger {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.chev { color: var(--text-muted); font-size: 10px; }

.dropdownPanel {
  position: absolute;
  z-index: 30;
  top: calc(100% + 6px);
  left: 0;
  min-width: 260px;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.dropdownList { padding: 6px; }
.dropdownOption {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  padding: 8px 10px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-primary);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}
/* Hover stays a ghost wash so it never competes with the selection mark. */
.dropdownOption:hover { background: var(--surface-hover); }
.dropdownOption .check { color: var(--accent); font-size: 16px; font-weight: 700; opacity: 0; }
.dropdownOption.selected { font-weight: 600; }
.dropdownOption.selected .check { opacity: 1; }
.dropdownOption .optionTail { display: flex; align-items: center; gap: 10px; }
.dropdownOption .optionHint { color: var(--text-muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.dropdownFooter { padding: 12px; border-top: 1px solid var(--border); }
.customRange { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.customRange .field { margin: 0; }

/* ------------------------------------------------------------------- stats */

.kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

/* Six cards (impressions ... CTR, registrations, FTD) sit better as 3 x 2 than 4 + 2. */
.kpis.kpisWide { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.kpi {
  position: relative;
  padding: 15px 16px 12px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
.kpi:hover { border-color: var(--border-strong); box-shadow: var(--shadow-md); }

.kpiLabel {
  font-size: 12px;
  font-weight: 520;
  color: var(--text-muted);
  letter-spacing: .01em;
}
.kpiValue {
  margin-top: 5px;
  font-size: 27px;
  font-weight: 630;
  letter-spacing: -.025em;
  line-height: 1.15;
  /* Proportional figures: tabular widths look loose at display sizes. */
}
.kpiSub { margin-top: 3px; font-size: 12px; color: var(--text-muted); }
.kpiSpark { display: block; margin-top: 10px; width: 100%; height: 30px; }

/* -------------------------------------------------------------------- grid */

.panels {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

/* ------------------------------------------------------------------- chart */

.chartBody { padding: 8px 10px 14px; position: relative; }
.chartSvg  { display: block; width: 100%; height: 260px; }

.segmented {
  display: flex;
  gap: 2px;
  padding: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
}
.segmentBtn {
  padding: 4px 10px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: 12.5px;
  cursor: pointer;
  transition: background .15s var(--ease), color .15s var(--ease);
}
.segmentBtn:hover { color: var(--text-primary); }
.segmentBtn.active { background: var(--surface-hover); color: var(--text-primary); font-weight: 560; }

.chartEmpty {
  display: grid;
  place-items: center;
  height: 260px;
  color: var(--text-muted);
  font-size: 13px;
}

/* Axis and grid are deliberately recessive. */
.axisLabel {
  font-size: 11px;
  fill: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.gridLine   { stroke: var(--grid); stroke-width: 1; }
.chartLine  { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chartArea  { fill: url(#areaGradient); }
.crosshair  { stroke: var(--border-strong); stroke-width: 1; stroke-dasharray: 3 3; }
.chartDot   { fill: var(--accent); stroke: var(--surface-1); stroke-width: 2; }

.tooltip {
  position: absolute;
  z-index: 5;
  min-width: 116px;
  padding: 8px 10px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-md);
  pointer-events: none;
  transform: translate(-50%, -112%);
  transition: opacity .12s var(--ease);
}
.tooltipDate { font-size: 11.5px; color: var(--text-muted); margin-bottom: 3px; font-variant-numeric: tabular-nums; }
.tooltipRow  { display: flex; align-items: center; gap: 7px; }
.tooltipKey  { width: 12px; height: 2px; border-radius: 1px; background: var(--accent); flex-shrink: 0; }
/* Value leads, label follows. */
.tooltipValue { font-size: 14px; font-weight: 620; letter-spacing: -.01em; }
.tooltipName  { font-size: 11.5px; color: var(--text-muted); }

/* ----------------------------------------------------------------- summary */

.summaryList { padding: 6px 18px 14px; }
.summaryRow {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
}
.summaryRow:last-child { border-bottom: none; }
.summaryKey { font-size: 12.5px; color: var(--text-muted); }
.summaryVal {
  font-size: 13.5px;
  font-weight: 560;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* ------------------------------------------------------------------- table */

.tableWrap { overflow: auto; max-height: 420px; }
.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  min-width: 520px;
}
.table th, .table td {
  padding: 9px 18px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}
/* The campaign name is the only cell allowed to be long; let it truncate. */
.table td:nth-child(2) {
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-1);
  color: var(--text-muted);
  font-size: 11.5px;
  font-weight: 560;
  letter-spacing: .03em;
  text-transform: uppercase;
}
.table td { color: var(--text-secondary); }
.table td:first-child { color: var(--text-primary); font-variant-numeric: tabular-nums; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table tbody tr { transition: background .1s var(--ease); }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: none; }

/* ------------------------------------------------------------------ status */

.status {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-1);
}
.status.isError {
  border-color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 35%, transparent);
}
.status.isError .spinner { display: none; }
.spinner {
  width: 15px; height: 15px;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }
.statusTitle { font-size: 13px; font-weight: 560; }
.statusText  { font-size: 12.5px; color: var(--text-muted); }

/* Refetch keeps the frame: hold the previous render, dimmed. */
.isLoading { opacity: .45; transition: opacity .15s var(--ease); }

/* ------------------------------------------------------------- responsive */

@media (max-width: 1080px) {
  .panels { grid-template-columns: minmax(0, 1fr); }
  .kpis, .kpis.kpisWide { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .gridFilters { grid-template-columns: 1fr 1fr; }
  .gridFilters .field:nth-child(3) { grid-column: span 2; }
  .actions { grid-column: span 2; }
  .actions .btn { flex: 1; }
}

@media (max-width: 620px) {
  .topbar { padding: 12px 16px; }
  .main   { padding: 16px 16px 32px; }
  .kpis, .kpis.kpisWide { grid-template-columns: minmax(0, 1fr); }
  .gridFilters { grid-template-columns: 1fr; }
  .gridFilters .field:nth-child(3), .actions { grid-column: span 1; }
  .subtitle { display: none; }
}

/* --------------------------------------------------------------------- nav */

.nav { display: flex; gap: 2px; padding: 2px; background: var(--surface-2);
       border: 1px solid var(--border); border-radius: var(--r-sm); }
.navBtn {
  padding: 6px 14px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13.5px;
  cursor: pointer;
  transition: background .15s var(--ease), color .15s var(--ease);
}
.navBtn:hover { color: var(--text-primary); }
.navBtn.active { background: var(--surface-hover); color: var(--text-primary); font-weight: 560; }

.btnSm { padding: 5px 11px; font-size: 12.5px; }

/* ------------------------------------------------------------------- meter */

/* Spend against the agreed budget. The bar is never the only signal -- the
   figure beside it always states the numbers. */
.meterCell { min-width: 190px; }
.meterTop {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 5px;
  font-variant-numeric: tabular-nums;
}
.meterPct { font-size: 12px; color: var(--text-muted); }
.meterTrack {
  height: 6px;
  border-radius: 999px;
  background: var(--accent-soft);
  overflow: hidden;
}
.meterFill { height: 100%; border-radius: 999px; background: var(--accent); transition: width .3s var(--ease); }
.meterFill.isWarning  { background: var(--status-warning); }
.meterFill.isCritical { background: var(--status-critical); }
.meterNone { color: var(--text-muted); font-size: 12.5px; }

/* ------------------------------------------------------------------- admin */

.adminGrid {
  display: grid;
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.userList { padding: 6px; max-height: 560px; overflow: auto; }
.userRow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 9px 11px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-primary);
  text-align: left;
  cursor: pointer;
  transition: background .12s var(--ease);
}
.userRow:hover { background: var(--surface-hover); }
.userRow.selected { background: var(--accent-soft); font-weight: 560; }
.userName { font-size: 13.5px; }
.userMeta { font-size: 11.5px; color: var(--text-muted); }
.tag {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 10.5px;
  color: var(--text-muted);
  letter-spacing: .03em;
  text-transform: uppercase;
}

.adminDetailBody { padding: 16px 18px 18px; }
.adminSection { margin-bottom: 22px; }
.adminSection:last-child { margin-bottom: 0; }
.adminSectionTitle {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 10px;
}
.formRow { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.formRow .field { margin: 0; }
.formActions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

.assignList { display: flex; flex-direction: column; gap: 8px; }
.assignRow {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 128px auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface-2);
}
.assignName { font-size: 13.5px; font-weight: 540; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.assignMeta { font-size: 11.5px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.assignRow input { padding: 6px 9px; font-size: 12.5px; }

.checkRow { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.checkRow input { width: auto; }

.inlineNote {
  padding: 9px 11px;
  border-radius: var(--r-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--text-secondary);
}
.inlineNote.isGood { border-color: var(--status-good); color: var(--text-primary); }
.inlineNote.isBad  { border-color: var(--status-critical); color: var(--text-primary); }
.credBox {
  margin-top: 10px;
  padding: 11px 12px;
  border-radius: var(--r-sm);
  border: 1px dashed var(--border-strong);
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.7;
  word-break: break-all;
}

@media (max-width: 1080px) {
  .adminGrid { grid-template-columns: minmax(0, 1fr); }
  .userList { max-height: 300px; }
}
@media (max-width: 620px) {
  .nav { order: 3; width: 100%; }
  .topbar { flex-wrap: wrap; }
  .assignRow { grid-template-columns: minmax(0, 1fr); }
  .filterRow .field { min-width: 100%; }
  .actions, .actions .btn { width: 100%; }
}
