:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #b91c1c;
  --badge-bg: #fef3c7;
  --badge-text: #92400e;
  --ok-bg: #dcfce7;
  --ok-text: #166534;
  --danger-bg: #fee2e2;
  --danger-text: #991b1b;
  --muted-bg: #f3f4f6;
  --muted-text: #4b5563;
  --info-bg: #dbeafe;
  --info-text: #1e40af;
  /* Chart series, validated as a pair (dataviz validate_palette.js) on white. */
  --series-accounts: #1a73e8;
  --series-revenue: #0f9d58;
  --chart-hover: rgba(17, 24, 39, 0.04);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #2a2f38;
    --accent: #60a5fa;
    --accent-text: #0f1115;
    --danger: #f87171;
    --badge-bg: #422006;
    --badge-text: #fcd34d;
    --ok-bg: #052e16;
    --ok-text: #86efac;
    --danger-bg: #450a0a;
    --danger-text: #fca5a5;
    --muted-bg: #1f2937;
    --muted-text: #d1d5db;
    --info-bg: #172554;
    --info-text: #93c5fd;
    /* Re-stepped and re-validated against the dark card, not flipped. */
    --series-accounts: #4b93f2;
    --series-revenue: #19a562;
    --chart-hover: rgba(255, 255, 255, 0.05);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* One bar: brand left, nav centred, badge + sign-out right. Pinned while the
   content scrolls. Below 860px the nav drops to its own row (see bottom). */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: "brand nav right";
  align-items: center;
  gap: 16px;
  min-height: 56px;
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  /* Saturated blue to a bright green, blended in OKLCH so the middle stays
     vivid rather than the grey teal an sRGB blend passes through. The cerulean
     stop holds the centre, where the nav sits, at 4.5:1 against white. The green
     end is a deliberate trade: a green as vivid as the blue has to be lighter,
     so it is ~3.2:1 there, and the soft text shadow keeps white text crisp on it.
     The first line is the fallback for browsers without `in oklch`; a custom
     property can't carry it, because an invalid value in a var() blanks the
     background instead of falling back. */
  background: linear-gradient(135deg, #0062ff 0%, #007ab8 50%, #00a651 100%);
  background: linear-gradient(135deg in oklch, #0062ff 0%, #007ab8 50%, #00a651 100%);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  color: #ffffff;
}
.brand { grid-area: brand; font-weight: 700; font-size: 17px; white-space: nowrap; }
.brand span { color: rgba(255, 255, 255, 0.85); font-weight: 500; }
.header-right { grid-area: right; justify-self: end; display: flex; align-items: center; gap: 12px; }
.badge {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.5;
  vertical-align: 1px;
  white-space: nowrap;
}
.badge-warn { background: var(--badge-bg); color: var(--badge-text); }
.badge-ok { background: var(--ok-bg); color: var(--ok-text); }
.badge-danger { background: var(--danger-bg); color: var(--danger-text); }
.badge-muted { background: var(--muted-bg); color: var(--muted-text); }
.badge-info { background: var(--info-bg); color: var(--info-text); }
/* A badge that is the whole cell has no text before it to keep clear of. */
td > .badge { margin-left: 0; }
.env-badge {
  /* Dark on the gradient, in both colour schemes. */
  text-shadow: none;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: #0f172a;
  color: #ffffff;
  white-space: nowrap;
}
.link-button {
  background: none;
  border: 0;
  color: #ffffff;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  min-height: 44px;
  padding: 0 4px;
  white-space: nowrap;
}

.topnav {
  grid-area: nav;
  display: flex;
  align-self: stretch;
  gap: 4px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}
.topnav::-webkit-scrollbar { display: none; }
.topnav a {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  white-space: nowrap;
  /* Same width as the active underline, so selecting a tab doesn't shift it. */
  border-bottom: 3px solid transparent;
}
.topnav a:hover { color: #ffffff; }
.topnav a.active { color: #ffffff; border-bottom: 3px solid #ffffff; font-weight: 600; }

@media (max-width: 860px) {
  .topbar {
    grid-template-columns: 1fr auto;
    grid-template-areas: "brand right" "nav nav";
    row-gap: 0;
  }
  .topnav { margin: 0 -12px; }
}

/* Content */
.content {
  max-width: 1440px;
  margin: 0 auto;
  padding: 24px max(16px, env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.content h2 { margin: 0 0 16px; font-size: 20px; }
.content h3 { margin: 28px 0 12px; font-size: 16px; }
.note { margin: 10px 0 0; font-size: 13px; }

/* Monthly chart (chart.js) */
.chart-card {
  margin-top: 24px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.chart-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px 16px;
  margin-bottom: 8px;
}
.chart-head h3 { margin: 0; font-size: 16px; }
.chart-sub { margin: 2px 0 0; font-size: 13px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--text); }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.swatch-accounts { background: var(--series-accounts); }
.swatch-revenue { background: var(--series-revenue); }
.chart-body { position: relative; }
.chart-plot { width: 100%; }
.chart-plot svg { display: block; }
.bar-accounts { fill: var(--series-accounts); }
.bar-revenue { fill: var(--series-revenue); }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-base { stroke: var(--muted); stroke-width: 1; }
.chart-axis { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-month { fill: var(--text); }
.chart-axis-title { fill: var(--muted); font-size: 11px; font-weight: 600; }
.chart-hover { fill: transparent; }
.chart-hover.on { fill: var(--chart-hover); }
.chart-hit { fill: transparent; cursor: default; outline: none; }
.chart-tip {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.5;
  white-space: nowrap;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}
.chart-tip div { display: flex; align-items: center; gap: 6px; }

/* Subheading left, controls right; they wrap under it on narrow screens. */
.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  margin: 28px 0 12px;
}
.section-head h3 { margin: 0; }
.section-controls { display: flex; gap: 8px; margin-left: auto; }

.select {
  font: inherit;
  min-height: 40px;
  max-width: 320px;
  padding: 0 36px 0 12px;
  color: var(--text);
  background-color: var(--surface);
  /* Our own chevron, so it matches in both colour schemes. */
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 17px center, right 12px center;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  border: 1px solid var(--border);
  border-radius: 8px;
  appearance: none;
  cursor: pointer;
}
.select:hover { border-color: var(--muted); }
.muted { color: var(--muted); }
.error { color: var(--danger); }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}
.stat-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 13px; }
/* Status cards carry a detail line, so they share the full row instead. */
.stats-wide { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.stats-wide .stat-value { font-size: 22px; font-weight: 400; }
.stat-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.stat-top .badge { margin-left: 0; }
.stat-detail { margin-top: 4px; font-size: 12px; line-height: 1.4; }
/* Overview's top row: 2BookNow's four cards, then QE Cards' two after a wider
   gap. The bases keep each card 166px wide, the same as the plain grid. */
.stats-split { display: flex; flex-wrap: wrap; gap: 12px 40px; }
.stats-split > .stats { flex: 0 1 calc(4 * 178px - 12px); grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.stats-split > .stats-qecards { flex-basis: calc(2 * 178px - 12px); }
/* Wrapped onto its own row: match the two-column card width above it. */
@media (max-width: 760px) {
  .stats-split > .stats-qecards { flex-basis: 100%; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}

.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; vertical-align: top; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th, td.nowrap { white-space: nowrap; }
td { overflow-wrap: break-word; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
tbody tr:last-child td { border-bottom: 0; }

.table-footer {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
  font-size: 14px;
}
.button-secondary {
  font: inherit;
  font-weight: 600;
  min-height: 44px;
  padding: 0 18px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.button-secondary:hover { border-color: var(--muted); }
.button-secondary:disabled { opacity: 0.6; cursor: default; }

/* Login */
.login-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 16px;
  min-height: 44px;
}
.login-card button {
  font: inherit;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  border-radius: 8px;
  padding: 11px;
  cursor: pointer;
}
.login-card .error { margin: 0; font-size: 14px; }

/* Phones and tablets: smaller chrome, and tables become one card per row with the column
   name beside each value (ui.js sets data-label on every cell). */
@media (max-width: 860px) {
  .brand { font-size: 16px; }
  .content { padding-top: 16px; }
  .content h2 { font-size: 18px; margin-bottom: 12px; }
  .stat { padding: 12px; }
  .stat-value { font-size: 24px; }

  .table-wrap { background: none; border: 0; border-radius: 0; overflow: visible; }
  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }
  tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 4px 0;
    margin-bottom: 10px;
  }
  td {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 12px;
    border-bottom: 0;
    white-space: normal;
    text-align: right;
    overflow-wrap: anywhere;
  }
  td.nowrap { white-space: normal; }
  /* Full-width filter; 16px stops iOS zooming into the control. */
  .section-controls { width: 100%; }
  .select { width: 100%; max-width: none; min-height: 44px; font-size: 16px; }
  /* A blank field is just noise in a card (e.g. Product on a free business). */
  td:empty { display: none; }
  td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 13px;
    text-align: left;
    flex-shrink: 0;
  }
  td:first-child { font-weight: 600; }
}
