/* Flow design system — plain CSS, no build step.
 *
 * Tokens first, then components. Status colours are defined ONCE here and read by
 * both the badges (via classes) and the charts (static/js/charts.js reads the
 * --status-* / --series-* custom properties), so a "complete" is the same green
 * everywhere. All text/background pairs below meet WCAG AA (>= 4.5:1).
 */

/* Theme-independent tokens: shape, rhythm, type. */
:root {
  --radius: 8px;
  --radius-sm: 5px;
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* ---------- Colour tokens: light (the default for everyone) ----------
 * The theme is chosen by the person, not by the operating system: static/js/theme-init.js puts
 * data-theme="dark" on <html> before first paint when they picked dark (localStorage "flow-theme").
 * The dark set below defines exactly the same variables (tracker/test_theme.py checks the names
 * and the contrast of every text pair), so every screen follows without per-component dark rules. */
:root {
  color-scheme: light;

  /* Brand */
  --brand: #3b5fc0;            /* buttons, entries series  (white text 5.9:1) */
  --brand-strong: #2f4ea3;     /* hover, links              (7.4:1 on white) */
  --brand-tint: #e8eefb;
  --brand-hover: #2f4ea3;      /* button hover fill (white text 7.7:1) */
  --on-brand: #ffffff;         /* text on a --brand or --brand-hover fill */
  --danger: #b4232f;           /* destructive button fill (white text 6.5:1) */
  --danger-hover: #8f1c26;
  --on-danger: #ffffff;
  --focus: #3b5fc0;            /* focus ring (5.9:1 on white) */
  --selection: #c9d7f7;        /* text selection (text 11.4:1) */

  /* Neutrals */
  --text: #1b1f27;             /* 16.5:1 on white */
  --text-secondary: #4b5261;   /* 7.8:1 */
  --text-muted: #5f6673;       /* 5.8:1 on white, 5.3:1 on page */
  --border: #d9dde4;
  --border-subtle: #e8eaef;
  --input-border: #7d8594;     /* form field outline (>= 3:1 on white and on sunken) */
  --surface: #ffffff;
  --surface-sunken: #f7f8fa;
  --page: #f5f6f8;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);

  /* Status — one mapping, reused by badges and charts.
   *   complete / active       -> green
   *   over-quota / test       -> amber
   *   terminate / inactive / locked out -> red
   *   security term            -> violet (a different thing from a plain terminate)
   *   neutral / pending       -> blue-gray
   * Fill colours validated as a chart palette (adjacent CVD dE >= 11); green and red
   * are never placed next to each other in a chart. */
  --status-complete: #0f8a5f;
  --status-complete-text: #0b6b49;
  --status-complete-tint: #e3f4ec;
  --status-overquota: #d98e04;
  --status-overquota-text: #83540a;
  --status-overquota-tint: #fcefd6;
  --status-terminate: #c8303f;
  --status-terminate-text: #a1202e;
  --status-terminate-tint: #fbe6e8;
  --status-security: #6b4fbb;
  --status-security-text: #4f3a96;
  --status-security-tint: #ece8f8;
  --status-neutral: #5b6b82;
  --status-neutral-text: #3f4b5c;
  --status-neutral-tint: #eceff4;

  --series-entries: #3b5fc0;
  /* Money series: blue / orange / near-black are separable for every common colour-vision type;
   * margin is a line (not a third bar colour) and a loss is red and points left. */
  --series-revenue: #3b5fc0;
  --series-cost: #c77700;
  --series-margin: #1b1f27;
  --series-loss: #c8303f;
  --chart-grid: #e8eaef;
  --chart-axis: #5f6673;
  --chart-tooltip-bg: #1b1f27;
  --chart-tooltip-text: #ffffff;
}

/* ---------- Colour tokens: dark (only when the person chose it) ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;

  --brand: #4a6fd8;
  --brand-strong: #9bb5f5;
  --brand-tint: #1f2a47;
  --brand-hover: #3b5fc0;
  --on-brand: #ffffff;
  --danger: #c13540;
  --danger-hover: #a12a35;
  --on-danger: #ffffff;
  --focus: #9bb5f5;
  --selection: #2f4a8a;

  --text: #e8eaef;
  --text-secondary: #bcc3d0;
  --text-muted: #a0a8b6;
  --border: #353c4a;
  --border-subtle: #2a303b;
  --input-border: #6b7487;
  --surface: #181c24;
  --surface-sunken: #1f242e;
  --page: #0f1217;
  --shadow: none;

  --status-complete: #34c38f;
  --status-complete-text: #6fdcb0;
  --status-complete-tint: #123329;
  --status-overquota: #f0a830;
  --status-overquota-text: #f5c36a;
  --status-overquota-tint: #3a2c10;
  --status-terminate: #ef6a77;
  --status-terminate-text: #f59aa3;
  --status-terminate-tint: #3d1a1f;
  --status-security: #a58df0;
  --status-security-text: #c4b5f7;
  --status-security-tint: #2b2450;
  --status-neutral: #8a9ab3;
  --status-neutral-text: #b7c2d4;
  --status-neutral-tint: #252c38;

  --series-entries: #7b9cf0;
  --series-revenue: #7b9cf0;
  --series-cost: #f0a830;
  --series-margin: #e8eaef;
  --series-loss: #ef6a77;
  --chart-grid: #2a303c;
  --chart-axis: #a0a8b6;
  --chart-tooltip-bg: #e8eaef;
  --chart-tooltip-text: #12151b;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
}
h1, h2, h3 { line-height: 1.25; color: var(--text); }
h1 { font-size: 1.6rem; font-weight: 700; margin: 0 0 var(--space-2); }
h2 { font-size: 1.15rem; font-weight: 650; margin: var(--space-5) 0 var(--space-3); }
p { margin: 0 0 var(--space-3); }
a { color: var(--brand-strong); text-underline-offset: 2px; }
a:hover { color: var(--text); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 3px; }
::selection { background: var(--selection); color: var(--text); }
code, pre, .mono { font-family: var(--font-mono); font-size: 0.85em; }
code { background: var(--surface-sunken); border: 1px solid var(--border-subtle); border-radius: 4px; padding: 0 0.3em; }
.mono { word-break: break-all; }
.muted { color: var(--text-muted); font-size: 0.875rem; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; margin: -1px; padding: 0; border: 0;
}
.skip-link { position: absolute; left: -999px; top: var(--space-2); }
.skip-link:focus { left: var(--space-2); z-index: 10; background: var(--surface); padding: var(--space-2) var(--space-3); }

/* ---------- Layout ---------- */
.container { width: 100%; max-width: 72rem; margin: 0 auto; padding: 0 var(--space-4); }
main.container { padding-top: var(--space-5); padding-bottom: var(--space-6); }
/* Wide layout for table pages: a template opts in with
   {% block container_class %} class="container--wide"{% endblock %} (it lands on <body>, so the top bar lines up too). */
.container--wide .container { max-width: 100rem; }
/* Wide tables: tighter side padding and headers that may wrap, so a many-column report fits the extra width. */
.container--wide table.data th,
.container--wide table.data td { padding-left: 0.5rem; padding-right: 0.5rem; }
.container--wide table.data thead th { white-space: normal; vertical-align: bottom; }
.container--wide table.data td .badge { white-space: normal; }
.container--wide table.data .col-project { min-width: 10rem; }
.narrow { max-width: 26rem; margin: var(--space-6) auto 0; }
.prose { max-width: 42rem; }

/* ---------- Top bar & nav ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar .container {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  min-height: 3.25rem;
  flex-wrap: wrap;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text);
  font-weight: 700;
  font-size: 1.05rem;
  text-decoration: none;
}
.brand-mark {
  width: 1.4rem; height: 1.4rem; border-radius: 6px;
  background: var(--brand);
  display: inline-grid; place-items: center;
  color: var(--on-brand); font-size: 0.8rem;
}
.nav { display: flex; align-items: center; gap: var(--space-1); flex-wrap: wrap; flex: 1; }
.nav a, .nav .link-button {
  display: inline-block;
  padding: 0.35rem 0.65rem;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
}
.nav a:hover, .nav .link-button:hover { background: var(--surface-sunken); color: var(--text); }
.nav a[aria-current="page"] { background: var(--brand-tint); color: var(--brand-strong); font-weight: 600; }
.nav-group { position: relative; }
.nav-group > summary {
  display: inline-block;
  padding: 0.35rem 0.65rem;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.nav-group > summary::-webkit-details-marker { display: none; }
.nav-group > summary::after { content: "▾"; margin-left: 0.3em; font-size: 0.75em; }
.nav-group > summary:hover, .nav-group[open] > summary { background: var(--surface-sunken); color: var(--text); }
.nav-group > summary[aria-current="true"] { background: var(--brand-tint); color: var(--brand-strong); font-weight: 600; }
.nav-menu {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 30;
  min-width: 11rem;
  display: flex;
  flex-direction: column;
  padding: var(--space-1);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}
.nav-menu a { white-space: nowrap; }
.nav-user { margin-left: auto; display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.nav-user .who { color: var(--text-muted); font-size: 0.85rem; }
.inline-form { display: inline; margin: 0; }
.link-button {
  background: none; border: 0; font: inherit; cursor: pointer;
  color: var(--brand-strong); padding: 0; text-decoration: underline;
}
.nav .link-button { text-decoration: none; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 2.25rem;
  padding: 0.4rem 0.9rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--brand);
  color: var(--on-brand);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--brand-hover); color: var(--on-brand); }
.btn--secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn--secondary:hover { background: var(--surface-sunken); color: var(--text); }
.btn--danger { background: var(--danger); color: var(--on-danger); }
.btn--danger:hover { background: var(--danger-hover); color: var(--on-danger); }
.btn--small { min-height: 1.9rem; padding: 0.2rem 0.6rem; font-size: 0.8rem; }
.actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }

/* ---------- Alerts / messages ---------- */
.alert {
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
}
.alert ul { margin: var(--space-1) 0 0; padding-left: 1.2rem; }
.alert--success { border-left-color: var(--status-complete); background: var(--status-complete-tint); }
.alert--error { border-left-color: var(--status-terminate); background: var(--status-terminate-tint); }
.alert--warning { border-left-color: var(--status-overquota); background: var(--status-overquota-tint); }
.alert--info { border-left-color: var(--brand); background: var(--brand-tint); }
.messages { list-style: none; padding: 0; margin: 0 0 var(--space-4); }

/* ---------- Forms ---------- */
.form { max-width: 46rem; }
.fieldset { margin: 0 0 var(--space-4); padding-top: var(--space-2); }
.fieldset legend {
  float: left; width: 100%;
  font-weight: 650; font-size: 1rem;
  padding: var(--space-2) 0 var(--space-1);
}
.fieldset legend + * { clear: both; }
.fieldset-help { color: var(--text-muted); font-size: 0.85rem; margin: 0 0 var(--space-3); }
.field { margin-bottom: var(--space-4); }
.field:last-child { margin-bottom: var(--space-1); }
.field-label { display: block; font-weight: 600; font-size: 0.875rem; margin-bottom: var(--space-1); }
.req { color: var(--status-terminate-text); }
input[type="text"], input[type="url"], input[type="password"], input[type="email"], input[type="search"], input[type="number"], input[type="tel"], select, textarea {
  width: 100%;
  max-width: 40rem;
  min-height: 2.4rem;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--input-border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* prevents zoom-on-focus on iOS */
}
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
input:disabled { background: var(--surface-sunken); color: var(--text-muted); }
.input-short { max-width: 14rem !important; }
.input-medium { max-width: 24rem !important; }
.field--error input { border-color: var(--status-terminate); }
.field-help { margin: var(--space-1) 0 0; color: var(--text-muted); font-size: 0.8rem; }
.field-errors { list-style: none; margin: var(--space-1) 0 0; padding: 0; color: var(--status-terminate-text); font-size: 0.85rem; font-weight: 500; }
.checkbox { display: inline-flex; align-items: center; gap: var(--space-2); font-weight: 600; font-size: 0.9rem; cursor: pointer; }
.checkbox input { width: 1.1rem; height: 1.1rem; accent-color: var(--brand); margin: 0; }
.form-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-top: var(--space-4); }

/* ---------- Copyable URL blocks ---------- */
.url-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-3); }
.url-item .url-label { font-size: 0.8rem; font-weight: 600; color: var(--text-secondary); margin-bottom: var(--space-1); display: flex; gap: var(--space-2); align-items: center; }
.url-row { display: flex; gap: var(--space-2); align-items: stretch; }
.url-row pre {
  flex: 1;
  min-width: 0;
  margin: 0;
  padding: 0.5rem 0.7rem;
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  white-space: pre;
  font-size: 0.8rem;
}
.copy-btn[data-copied]::after { content: " ✓"; }

/* ---------- Definition list (project facts) ---------- */
.facts { display: grid; grid-template-columns: max-content 1fr; gap: var(--space-2) var(--space-4); margin: 0; font-size: 0.9rem; }
.facts dt { color: var(--text-muted); }
.facts dd { margin: 0; min-width: 0; }

.row-warn { background: var(--status-overquota-tint); }
table.data tbody tr.row-warn:hover { background: var(--status-overquota-tint); }

/* ---------- Page header ---------- */
.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}
.page-head h1 { margin: 0; }
.page-head .muted { margin: var(--space-1) 0 0; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-4) var(--space-5);
  min-width: 0;
}
.card > h2:first-child,
.card-title {
  margin: 0 0 var(--space-1);
  font-size: 1rem;
  font-weight: 600;
}
.card-sub { margin: 0 0 var(--space-3); color: var(--text-muted); font-size: 0.85rem; }

.grid { display: grid; gap: var(--space-4); }
.grid > * { min-width: 0; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 22rem), 1fr)); }
.section { margin-bottom: var(--space-5); }

/* ---------- KPI tiles ---------- */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 9.5rem), 1fr));
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-3) var(--space-4);
  border-top: 3px solid var(--kpi-accent, var(--border));
}
.kpi-label { color: var(--text-secondary); font-size: 0.8rem; font-weight: 500; }
.kpi-value { font-size: 1.6rem; font-weight: 650; line-height: 1.2; margin-top: var(--space-1); }
.kpi-meta { color: var(--text-muted); font-size: 0.78rem; margin-top: 2px; }
.kpi--complete { --kpi-accent: var(--status-complete); }
.kpi--terminate { --kpi-accent: var(--status-terminate); }
.kpi--security { --kpi-accent: var(--status-security); }
.kpi--overquota { --kpi-accent: var(--status-overquota); }
.kpi--brand { --kpi-accent: var(--brand); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3em;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.5;
  white-space: nowrap;
  background: var(--status-neutral-tint);
  color: var(--status-neutral-text);
}
.badge::before {
  content: "";
  width: 0.45em;
  height: 0.45em;
  border-radius: 50%;
  background: currentColor;
}
.badge--complete, .badge--active { background: var(--status-complete-tint); color: var(--status-complete-text); }
.badge--overquota, .badge--test { background: var(--status-overquota-tint); color: var(--status-overquota-text); }
.badge--terminate, .badge--inactive, .badge--locked { background: var(--status-terminate-tint); color: var(--status-terminate-text); }
.badge--security { background: var(--status-security-tint); color: var(--status-security-text); }
.badge--live { background: var(--brand-tint); color: var(--brand-strong); }
.checklist { list-style: none; margin: 0 0 var(--space-3); padding: 0; display: grid; gap: var(--space-2); }
.checklist li { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--space-1) var(--space-3); }
.checklist-meta { font-size: 0.85rem; }
#id_devices { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); }
#id_devices > div { display: inline-flex; }
.badges { display: inline-flex; flex-wrap: wrap; gap: var(--space-1); }

/* ---------- Charts ---------- */
.chart-box { position: relative; width: 100%; height: 260px; }
.chart-box--bars { height: var(--chart-height, 220px); }
.chart-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.chart-scroll > .chart-box { min-width: 30rem; }
.chart-empty {
  display: grid;
  place-items: center;
  height: 100%;
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
}
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-4);
  margin: 0 0 var(--space-3);
  padding: 0;
  list-style: none;
  font-size: 0.8rem;
  color: var(--text-secondary);
}
.legend li { display: inline-flex; align-items: center; gap: 0.4em; }
.swatch { width: 0.75rem; height: 0.75rem; border-radius: 3px; background: var(--swatch); }
.swatch--line { height: 3px; border-radius: 2px; }
.swatch--entries { --swatch: var(--series-entries); }
.swatch--complete { --swatch: var(--status-complete); }
.swatch--overquota { --swatch: var(--status-overquota); }
.swatch--terminate { --swatch: var(--status-terminate); }
.swatch--security { --swatch: var(--status-security); }

details.data-table { margin-top: var(--space-3); font-size: 0.85rem; }
details.data-table summary { cursor: pointer; color: var(--text-secondary); }

/* ---------- Tables ---------- */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card .table-wrap { box-shadow: none; }
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}
table.data th,
table.data td {
  padding: 0.55rem 0.75rem;
  text-align: left;
  vertical-align: middle;
  border-bottom: 1px solid var(--border-subtle);
  border-left: 0;
  border-right: 0;
  border-top: 0;
}
table.data thead th {
  background: var(--surface-sunken);
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-sunken); }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data .nowrap { white-space: nowrap; }
table.data a.row-link { font-weight: 600; }
.kpi-value--sm { font-size: 1.1rem; padding-top: 0.35rem; }

/* ---------- Utilities ---------- */
.mt-2 { margin-top: var(--space-2); }
.study-waves { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-1, 0.25rem); overflow-wrap: anywhere; }
.study-waves li[data-depth="1"] { padding-left: 1.25rem; }
.study-waves li[data-depth="2"] { padding-left: 2.5rem; }
.study-waves li[data-depth="3"] { padding-left: 3.75rem; }
.study-waves li[data-depth="4"], .study-waves li[data-depth="5"], .study-waves li[data-depth="6"] { padding-left: 5rem; }
.mt-5 { margin-top: var(--space-5); }
.mb-0 { margin-bottom: 0; }
.btn--block { width: 100%; }
.narrow h1 { font-size: 1.35rem; }

/* ---------- Small screens ---------- */
/* Below this width the nav is a plain vertical list; each group opens in place. */
@media (max-width: 56rem) {
  .nav { flex-basis: 100%; order: 3; flex-direction: column; align-items: stretch; gap: 0; }
  .nav-group > summary { display: block; }
  .nav-menu { position: static; box-shadow: none; border: 0; border-left: 2px solid var(--border); border-radius: 0; margin: 0 0 var(--space-1) var(--space-3); padding: 0 0 0 var(--space-2); }
  .nav-user { margin-left: 0; padding: var(--space-2) 0.65rem 0; }
  .nav .theme-toggle { justify-content: flex-start; padding: 0 0.65rem; margin-left: -0.65rem; }
  .nav .theme-toggle-label { position: static !important; width: auto; height: auto; overflow: visible; clip: auto; white-space: normal; margin: 0; }
}
@media (max-width: 40rem) {
  body { font-size: 14.5px; }
  .topbar .container { padding-top: var(--space-2); padding-bottom: var(--space-2); gap: var(--space-2); }
  .nav { flex-basis: 100%; order: 3; }
  .card { padding: var(--space-3) var(--space-4); }
  .kpi-value { font-size: 1.35rem; }
  .facts { grid-template-columns: 1fr; gap: 0; }
  .facts dd { margin-bottom: var(--space-2); }
  h1 { font-size: 1.35rem; }
}
table.data .col-project { min-width: 13rem; }
table.data .col-project .mono { word-break: normal; overflow-wrap: anywhere; }

/* ---------- Vendors ---------- */
.page-head--section { margin: var(--space-5) 0 var(--space-3); }
.page-head--section h2 { margin: 0; }
.universal-card { border-left: 3px solid var(--brand); margin-bottom: var(--space-3); }
.universal-card .card-title { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.vendor-links { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-2); margin-top: var(--space-3); }
.vendor-card { padding: var(--space-3) var(--space-4); }
.vendor-card summary { cursor: pointer; }
.vendor-card[open] summary { margin-bottom: var(--space-3); }
table.data .col-quota { min-width: 9rem; }
.quota-line { font-size: 0.85rem; white-space: nowrap; }
progress.quota {
  display: block;
  width: 100%;
  max-width: 9rem;
  height: 6px;
  margin-top: 4px;
  border: 0;
  border-radius: 3px;
  background: var(--border-subtle);
  accent-color: var(--status-complete);
  appearance: none;
}
progress.quota::-webkit-progress-bar { background: var(--border-subtle); border-radius: 3px; }
progress.quota::-webkit-progress-value { background: var(--status-complete); border-radius: 3px; }
progress.quota::-moz-progress-bar { background: var(--status-complete); border-radius: 3px; }
progress.quota--full::-webkit-progress-value { background: var(--status-terminate); }
progress.quota--full::-moz-progress-bar { background: var(--status-terminate); }
progress.quota--full { accent-color: var(--status-terminate); }
.blocked-note { color: var(--status-terminate-text); font-size: 0.8rem; font-weight: 600; }

/* ---------- Respondents ---------- */
.respondents-card { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; justify-content: space-between; }
.filters { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: flex-end; margin-bottom: var(--space-3); padding: var(--space-3) var(--space-4); }
.filter-field { flex: 1 1 11rem; min-width: 0; }
.filter-field input, .filter-field select { max-width: none; }
.filter-actions { display: flex; gap: var(--space-3); align-items: center; }
.pager { display: flex; gap: var(--space-3); align-items: center; justify-content: center; margin-top: var(--space-4); }
.rid-title { font-size: 1.25rem; word-break: break-all; }
.nowrap { white-space: nowrap; }

/* ---------- Reconcile (client review) ---------- */
.select-bar { align-items: center; margin-bottom: var(--space-2); }
.check-col { width: 2.25rem; }
.check-col input { width: 1.1rem; height: 1.1rem; accent-color: var(--brand); margin: 0; }
input[type="file"] { max-width: 100%; font: inherit; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Reports ---------- */
.presets { display: flex; flex-wrap: wrap; gap: var(--space-2); margin-bottom: var(--space-3); }
.presets a { padding: 0.2rem 0.7rem; border: 1px solid var(--border); border-radius: 999px; background: var(--surface); font-size: 0.85rem; text-decoration: none; }
.presets a[aria-current="true"] { background: var(--brand); border-color: var(--brand); color: var(--on-brand); }
.filter-field--narrow { flex: 0 1 9rem; }
.field-error { color: var(--status-terminate-text); font-weight: 600; margin: 0 0 var(--space-3); }
.quick-reports { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin: 0; padding: 0; list-style: none; }
.leaders { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-2); }
.leaders li { display: grid; grid-template-columns: minmax(6rem, 14rem) minmax(0, 1fr) 4.5rem; gap: var(--space-3); align-items: center; }
.leaders .leader-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaders .leader-value { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
meter.leader-bar { width: 100%; height: 10px; border: 0; background: var(--border-subtle); border-radius: 5px; }
meter.leader-bar::-webkit-meter-bar { background: var(--border-subtle); border: 0; border-radius: 5px; height: 10px; }
meter.leader-bar::-webkit-meter-optimum-value, meter.leader-bar::-webkit-meter-suboptimum-value, meter.leader-bar::-webkit-meter-even-less-good-value { background: var(--status-complete); border-radius: 5px; }
meter.leader-bar::-moz-meter-bar { background: var(--status-complete); border-radius: 5px; }
table.data thead th a { color: inherit; text-decoration: none; }
table.data thead th a:hover { text-decoration: underline; }
table.data tr.row-total th, table.data tr.row-total td { background: var(--surface-sunken); font-weight: 700; text-align: left; }
table.data tr.row-total td.num { text-align: right; }
table.data tfoot td { font-weight: 700; background: var(--surface-sunken); border-top: 2px solid var(--border); border-bottom: 0; }
@media (max-width: 40rem) {
  .leaders li { grid-template-columns: minmax(0, 1fr) 4rem; }
  .leaders li meter { grid-column: 1 / -1; grid-row: 2; }
}
.delivery-url { min-width: 14rem; max-width: 26rem; }
.callbacks-line { margin: var(--space-1) 0 0; font-size: 0.8rem; }

/* Two-factor: recovery codes, setup steps and the setup key */
.recovery-codes { list-style: none; margin: var(--space-2) 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr)); gap: var(--space-1); }
.recovery-codes code { display: block; font-size: 1rem; padding: 0.35em 0.6em; letter-spacing: 0.05em; }
.steps { padding-left: 1.25rem; }
.steps li { margin-bottom: var(--space-1); }
.secret { font-size: 1rem; letter-spacing: 0.08em; word-break: break-all; }


/* ---------- Dashboard: period analytics ---------- */
.num { font-variant-numeric: tabular-nums; }
.dash-toolbar { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-2); }
.dash-toolbar .presets { margin-bottom: 0; }
.dash-currency-pick { display: grid; gap: 2px; }
.dash-range { max-width: 60rem; }
.kpis--traffic { grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr)); }
.dash-kpi { display: flex; flex-direction: column; min-height: 8.5rem; }
.dash-kpi .kpi-value { font-size: 1.5rem; }
.kpi-delta { font-size: 0.8rem; font-weight: 600; margin-top: 2px; color: var(--text-secondary); }
.kpi-delta--good { color: var(--status-complete-text); }
.kpi-delta--bad { color: var(--status-terminate-text); }
.spark { width: 100%; height: 1.5rem; margin-top: auto; padding-top: var(--space-1); overflow: visible; }
.spark polyline { fill: none; stroke: var(--series-entries); stroke-width: 1.5; vector-effect: non-scaling-stroke; stroke-linejoin: round; stroke-linecap: round; }
.spark--over_quota polyline { stroke: var(--status-overquota); }
.spark--terminates polyline { stroke: var(--status-terminate); }
.spark--security_terms polyline { stroke: var(--status-security); }
.spark--billable polyline, .spark--conversion polyline { stroke: var(--status-complete); }
.dash-currency { margin-bottom: var(--space-3); }
.dash-currency-title { margin: 0 0 var(--space-2); font-size: 0.95rem; }
.dash-currency .kpis { margin-bottom: var(--space-2); }
.dash-currency .kpi-value { font-size: 1.25rem; }
.neg { color: var(--status-terminate-text); }
.dash-grid { display: grid; gap: var(--space-4); grid-template-columns: repeat(auto-fit, minmax(min(100%, 24rem), 1fr)); align-items: stretch; }
.dash-card { display: flex; flex-direction: column; }
.dash-card .chart-scroll { flex: 1; }
.dash-card .chart-box { height: 280px; }
.swatch--revenue { --swatch: var(--series-revenue); }
.swatch--cost { --swatch: var(--series-cost); }
.swatch--loss { --swatch: var(--series-loss); }
.swatch--text { --swatch: var(--text); }
@media (min-width: 70rem) {
  .dash-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .dash-card--wide { grid-column: span 2; }
}
.attention { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-2); grid-template-columns: repeat(auto-fit, minmax(min(100%, 20rem), 1fr)); }
.attention-item { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--status-overquota); border-radius: var(--radius); padding: var(--space-2) var(--space-3); display: flex; flex-direction: column; gap: 2px; }
.attention-item--bad { border-left-color: var(--status-terminate); }
.attention-count { font-weight: 700; font-size: 1.1rem; }
.attention-label { font-weight: 600; }
.attention-detail { overflow-wrap: anywhere; }
.dash-alltime { margin-top: var(--space-5); }

/* ---------- Theme toggle (static/js/theme.js) ----------
 * Shows the moon in light mode and the sun in dark mode. The button is hidden until the script runs:
 * without JavaScript there is nothing to switch with, and the page stays light. Its text label is
 * read by screen readers everywhere and shown inside the phone menu. */
.theme-toggle {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-width: 44px; min-height: 44px; padding: 0 0.6rem;
  border: 1px solid transparent; border-radius: var(--radius-sm); background: none;
  color: var(--text-secondary); font: inherit; font-size: 0.9rem; font-weight: 500; cursor: pointer;
}
.theme-toggle[hidden] { display: none; }
.theme-toggle:hover { background: var(--surface-sunken); color: var(--text); }
.theme-toggle svg { width: 1.15rem; height: 1.15rem; flex: none; }
.theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
.theme-toggle--bar { margin-left: auto; }

/* ---------- Phone width (390px and up) ----------
 * The menu button only exists when static/js/nav.js runs; without it the navigation stays open.
 * Tables scroll inside .table-wrap, so the page itself never scrolls sideways. Tap targets are
 * at least 44px, forms are one column. */
.nav-toggle { display: none; }
@media (max-width: 56rem) {
  .nav-toggle {
    display: inline-flex; align-items: center; margin-left: auto; min-height: 44px; padding: 0 var(--space-4);
    border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface);
    color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
  }
  .nav-toggle[hidden] { display: none; }
  .nav-toggle[aria-expanded="true"] { background: var(--brand-tint); color: var(--brand-strong); }
  .js .nav:not([data-open]) { display: none; }
  .nav { max-height: calc(100vh - 4rem); overflow-y: auto; padding-bottom: var(--space-2); }
}
@media (max-width: 40rem) {
  .container { padding: 0 var(--space-3); }
  main.container { padding-top: var(--space-4); }
  .btn, .btn--small { min-height: 44px; }
  .nav a, .nav .link-button, .nav-group > summary { display: flex; align-items: center; min-height: 44px; }
  .nav-menu a { min-height: 44px; }
  .nav-user { flex-wrap: wrap; gap: var(--space-2) var(--space-3); }
  .nav-user .link-button { padding: 0 0.65rem; }
  input[type="text"], input[type="url"], input[type="password"], input[type="email"], input[type="search"],
  input[type="number"], input[type="tel"], input[type="date"], select { min-height: 44px; max-width: 100%; }
  textarea { max-width: 100%; }
  .input-short, .input-medium { max-width: 100% !important; }
  .checkbox { min-height: 44px; }
  .check-col { width: 3rem; }
  .checkbox { gap: 0; }
  input[type="checkbox"] {
    appearance: none; -webkit-appearance: none; width: 44px; height: 44px; min-width: 44px; margin: 0; padding: 0; flex: none;
    border: 0; background: transparent url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='2.5' y='2.5' width='19' height='19' rx='4' fill='%23ffffff' stroke='%236b7380' stroke-width='2'/%3E%3C/svg%3E") center / 28px no-repeat; cursor: pointer;
  }
  input[type="checkbox"]:checked { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='2.5' y='2.5' width='19' height='19' rx='4' fill='%233b5fc0' stroke='%233b5fc0' stroke-width='2'/%3E%3Cpath d='M7 12.5l3.2 3.2L17 9' fill='none' stroke='white' stroke-width='2.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); }
  :root[data-theme="dark"] input[type="checkbox"]:not(:checked) { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='2.5' y='2.5' width='19' height='19' rx='4' fill='%23181c24' stroke='%23a0a8b6' stroke-width='2'/%3E%3C/svg%3E"); }
  :root[data-theme="dark"] input[type="checkbox"]:checked { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='2.5' y='2.5' width='19' height='19' rx='4' fill='%234a6fd8' stroke='%239bb5f5' stroke-width='2'/%3E%3Cpath d='M7 12.5l3.2 3.2L17 9' fill='none' stroke='white' stroke-width='2.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); }
  input[type="checkbox"]:disabled { opacity: 0.5; }
  input[type="radio"] { width: 1.4rem; height: 1.4rem; margin: 0.65rem; }
  input[type="file"] { min-height: 44px; padding: 0.6rem 0; }
  .brand { min-height: 44px; }
  /* Links keep their place in the text; padding (offset by margin) only enlarges the area you can tap. */
  main a:where(:not(.btn)) { padding: 1rem 0.75rem; margin: 0 -0.75rem; }
  main .presets a, main .pager a { margin: 0; }
  main details > summary { padding-top: 0.9rem; padding-bottom: 0.9rem; }
  .presets a { display: inline-flex; align-items: center; min-height: 44px; padding: 0 var(--space-3); }
  .pager { flex-wrap: wrap; }
  .pager a { display: inline-flex; align-items: center; min-height: 44px; padding: 0 var(--space-3); }
  .filters { flex-direction: column; align-items: stretch; }
  .filter-field, .filter-field--narrow { flex: 1 1 auto; width: 100%; }
  .filter-actions { flex-wrap: wrap; }
  .form-actions .btn, .actions .btn, .filter-actions .btn { flex: 1 1 auto; }
  .page-head { align-items: stretch; }
  .page-head > .actions { width: 100%; }
  .page-head > .actions .btn { flex: 1 1 auto; }
  .dash-toolbar { align-items: stretch; }
  .url-row { flex-wrap: wrap; }
  .url-row pre { flex-basis: 100%; }
  .recovery-codes { grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr)); }
  .delivery-url { min-width: 0; max-width: none; overflow-wrap: anywhere; }
  .chart-scroll > .chart-box { min-width: 26rem; }
  table.data .col-project { min-width: 10rem; }
  table.data a.row-link { display: inline-block; padding: 1rem 0.75rem; margin: 0 -0.75rem; }
  table.data td a { white-space: nowrap; }
  .link-button { min-height: 44px; }
  .facts dd, .mono, .rid-title, .kpi-value { overflow-wrap: anywhere; }
}

/* ---------- One row, one line (Projects list, dashboard table) ----------
 * Nothing wraps: a long name is cut with an ellipsis (the full text is in the cell's title) and the
 * table scrolls sideways inside .table-wrap on a narrow screen instead of growing taller. */
.container--wide table.data.data--lean th,
.container--wide table.data.data--lean td,
table.data.data--lean th,
table.data.data--lean td { white-space: nowrap; }
.container--wide table.data.data--lean td .badge { white-space: nowrap; }
table.data--lean .cell-trunc { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: var(--cell-max, 14rem); }
table.data--lean .cell-trunc--name { --cell-max: 18rem; }
table.data--lean .cell-trunc--company { --cell-max: 12rem; }
table.data--lean .cell-trunc--pm { --cell-max: 9rem; }
table.data--lean thead th a { display: inline-block; }
table.data--lean .row-actions { display: flex; gap: var(--space-3); }
@media (max-width: 40rem) {
  main table.data--lean a:where(:not(.btn)) { padding: 0; margin: 0; }
}
.filter-field--id { flex: 0 1 6rem; }
.filter-field--search { flex: 2 1 14rem; }
.list-bar { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; justify-content: space-between; margin-bottom: var(--space-3); }

/* ---------- Dashboard status tiles ---------- */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr));
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}
.tile {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 3px solid var(--kpi-accent, var(--border));
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-3) var(--space-4);
  color: inherit;
  text-decoration: none;
}
.tile:hover, .tile:focus-visible { background: var(--surface-sunken); }
.tile-label { color: var(--text-secondary); font-size: 0.8rem; font-weight: 600; }
.tile-count { font-size: 2rem; font-weight: 650; line-height: 1.2; font-variant-numeric: tabular-nums; }
.tile-caption { color: var(--text-muted); font-size: 0.78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile--active { --kpi-accent: var(--status-complete); }
.tile--inactive { --kpi-accent: var(--status-terminate); }
.tile--closed { --kpi-accent: var(--brand); }
.tile--on_hold { --kpi-accent: var(--status-overquota); }
.section-head { display: flex; justify-content: space-between; align-items: baseline; gap: var(--space-3); }

/* ---------- Project page: section jump bar and quick stats ---------- */
.jump { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin: 0 0 var(--space-4); padding: 0; list-style: none; font-size: 0.875rem; }

/* ---------- App shell: left sidebar on wide screens ----------
 * Same markup as the top bar. From 56rem up, a signed-in page (the one that has the main navigation) puts
 * the brand, menus and account controls in a fixed column on the left and centres the content next to it.
 * Below 56rem and on signed-out pages (no navigation) the top bar above is unchanged. */
@media (min-width: 56.01rem) {
  :root { --sidebar-w: 15rem; --content-max: 80rem; }
  body:has(#main-nav) {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    align-items: start;
    min-height: 100vh;
  }
  body:has(#main-nav) .topbar {
    grid-column: 1;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    border-bottom: 0;
    border-right: 1px solid var(--border);
  }
  body:has(#main-nav) .topbar .container {
    max-width: none;
    min-height: 100%;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: var(--space-4);
    padding: var(--space-4) var(--space-3);
  }
  body:has(#main-nav) .brand { padding: 0 0.65rem; }
  body:has(#main-nav) .nav { flex-direction: column; flex-wrap: nowrap; align-items: stretch; gap: 2px; }
  body:has(#main-nav) .nav a,
  body:has(#main-nav) .nav-group > summary { display: block; }
  body:has(#main-nav) .nav-group > summary::after { float: right; }
  body:has(#main-nav) .nav-menu {
    position: static;
    min-width: 0;
    box-shadow: none;
    border: 0;
    border-left: 2px solid var(--border);
    border-radius: 0;
    margin: 0 0 var(--space-1) 0.9rem;
    padding: 0 0 0 var(--space-2);
  }
  body:has(#main-nav) .nav-user {
    margin: auto 0 0;
    padding: var(--space-3) 0.65rem 0;
    border-top: 1px solid var(--border-subtle);
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }
  body:has(#main-nav) .nav-user a,
  body:has(#main-nav) .nav-user .link-button { padding-left: 0; padding-right: 0; }
  body:has(#main-nav) .nav .theme-toggle { justify-content: flex-start; padding: 0; margin: 0; }
  body:has(#main-nav) .nav .theme-toggle-label { position: static !important; width: auto; height: auto; overflow: visible; clip: auto; white-space: normal; margin: 0 0 0 var(--space-2); }
  /* The content: a centred column with real margins, never edge to edge (wide table pages get a little more room). */
  body:has(#main-nav) main.container {
    grid-column: 2;
    max-width: var(--content-max);
    padding: var(--space-6) var(--space-6) var(--space-6);
  }
  body.container--wide:has(#main-nav) main.container { max-width: 96rem; }
}

/* ---------- Menu icons ---------- */
.nav-icon { width: 1.05rem; height: 1.05rem; flex: none; vertical-align: -0.2em; margin-right: 0.55rem; opacity: 0.8; }
.nav a[aria-current="page"] .nav-icon,
.nav-group > summary[aria-current="true"] .nav-icon { opacity: 1; }
.nav .link-button { display: inline-flex; align-items: center; }
.nav .link-button .nav-icon { vertical-align: 0; }
.nav-menu .nav-icon { width: 0.95rem; height: 0.95rem; }

/* ---------- Forms: centred, fields side by side ----------
 * The form is a centred column under the page header instead of hugging the left edge, and from 48rem up
 * the fields inside each card sit in two columns. Long fields (text areas, checkboxes, help) span both. */
@media (min-width: 48rem) {
  main > .form, main > form.form { margin-left: auto; margin-right: auto; }
  main > .page-head:has(+ form.form) { max-width: 60rem; margin-left: auto; margin-right: auto; }
  .form { max-width: 60rem; }
  .form .fieldset { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); column-gap: var(--space-5); align-content: start; }
  .form .fieldset > legend, .form .fieldset > .fieldset-help { grid-column: 1 / -1; }
  .form .fieldset > .field:has(textarea),
  .form .fieldset > .field--checkbox,
  .form .fieldset > .field:has(.input-wide, input[type="file"], .checkbox-list) { grid-column: 1 / -1; }
  .form .fieldset .field input, .form .fieldset .field select, .form .fieldset .field textarea { max-width: none; }
  .form .fieldset .field .input-short { max-width: 14rem !important; }
}
