:root {
  /* Theme tokens (default: dark). Override via html[data-theme="light"]. */
  --t-bg: #0b0f14;
  --t-surface: #0f172a;
  --t-card: rgba(255, 255, 255, 0.06);
  --t-card2: rgba(255, 255, 255, 0.03);
  --t-text: #f9fafb;
  --t-muted: #9ca3af;
  --t-border: rgba(255, 255, 255, 0.12);
  --t-border-strong: rgba(255, 255, 255, 0.20);

  --t-primary: #ef4444; /* red */
  --t-primary-contrast: #ffffff;
  --t-primary-weak: rgba(239, 68, 68, 0.18);
  /* Accent blue for non-destructive actions + links. */
  --t-accent: #2563eb;
  --t-accent-contrast: #ffffff;
  --t-accent-weak: rgba(37, 99, 235, 0.16);

  --t-link: #60a5fa;

  --t-control-bg: rgba(255, 255, 255, 0.06);
  --t-control-bg-hover: rgba(255, 255, 255, 0.10);
  --t-focus-ring: rgba(37, 99, 235, 0.30);

  --radius: 14px;
  --radius-sm: 10px;

  --shadow: 0 18px 60px rgba(0, 0, 0, 0.50);
  --shadow-sm: 0 10px 30px rgba(0, 0, 0, 0.40);

  /* Legacy aliases used by older templates (keep as stable API). */
  --bg: var(--t-bg);
  --surface: var(--t-surface);
  --card: var(--t-card);
  --card2: var(--t-card2);
  --text: var(--t-text);
  --muted: var(--t-muted);
  --border: var(--t-border);
  --border-strong: var(--t-border-strong);
  --primary: var(--t-primary);
  --primary-contrast: var(--t-primary-contrast);
  --primary-weak: var(--t-primary-weak);
  --accent: var(--t-accent);
  --accent-contrast: var(--t-accent-contrast);
  --accent-weak: var(--t-accent-weak);
  --link: var(--t-link);

  --control-bg: var(--t-control-bg);
  --control-bg-hover: var(--t-control-bg-hover);
  --focus-ring: var(--t-focus-ring);
}

/* Ensure native controls (select dropdowns, scrollbars, form menus) follow the active theme.
  This prevents the classic “white dropdown + white text” issue in dark mode. */
html { color-scheme: dark; }
html[data-theme="light"] { color-scheme: light; }

html[data-theme="light"] {
  --t-bg: #f8fafc;
  --t-surface: #ffffff;
  --t-card: #ffffff;
  --t-card2: #ffffff;
  --t-text: #111827;
  --t-muted: #6b7280;
  --t-border: rgba(17, 24, 39, 0.12);
  --t-border-strong: rgba(17, 24, 39, 0.20);

  --t-primary: #dc2626;
  --t-primary-contrast: #ffffff;
  --t-primary-weak: rgba(220, 38, 38, 0.10);
  --t-accent: #2563eb;
  --t-accent-contrast: #ffffff;
  --t-accent-weak: rgba(37, 99, 235, 0.10);

  --t-link: #2563eb;

  --t-control-bg: rgba(17, 24, 39, 0.03);
  --t-control-bg-hover: rgba(17, 24, 39, 0.06);
  --t-focus-ring: rgba(37, 99, 235, 0.22);

  --shadow: 0 16px 55px rgba(17, 24, 39, 0.10);
  --shadow-sm: 0 10px 30px rgba(17, 24, 39, 0.10);
}

html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(1200px 700px at 10% 10%, rgba(220, 38, 38, 0.18), transparent 55%),
    radial-gradient(900px 600px at 80% 40%, rgba(255, 255, 255, 0.10), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

html[data-theme="light"] body {
  background:
    radial-gradient(1200px 700px at 10% 10%, rgba(185, 28, 28, 0.10), transparent 55%),
    radial-gradient(900px 600px at 80% 40%, rgba(17, 24, 39, 0.06), transparent 55%),
    var(--bg);
}

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Consistent focus rings (keyboard accessible) */
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.container { max-width: 1160px; margin: 0 auto; padding: 16px; }

.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  padding: 12px 0;
  backdrop-filter: blur(12px);
  background: linear-gradient(180deg, rgba(11, 15, 20, 0.92), rgba(11, 15, 20, 0.70));
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

html[data-theme="light"] .appbar {
  background: linear-gradient(180deg, rgba(247, 247, 248, 0.92), rgba(247, 247, 248, 0.70));
  border-bottom-color: rgba(17, 24, 39, 0.08);
}

.appbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.title {
  font-size: 18px;
  font-weight: 900;
  letter-spacing: 0.2px;
}

.subtitle { color: var(--muted); font-size: 12px; font-weight: 700; }

.card {
  background: linear-gradient(180deg, var(--card), var(--card2));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
}

.panel-title {
  margin: 0;
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0.2px;
}

.panel-subtitle {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.toolbar .input {
  min-width: 260px;
  flex: 1 1 280px;
}

.stats-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 10px;
}

.stat-pill {
  border: 1px solid var(--border);
  background: var(--control-bg);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 800;
  color: var(--text);
}

.empty-state {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 12px;
  color: var(--muted);
  font-weight: 650;
  background: linear-gradient(180deg, var(--card), var(--card2));
}

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.btn {
  border: 1px solid var(--border);
  background: var(--control-bg);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.btn:hover { background: var(--control-bg-hover); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

/* Primary actions should be non-destructive (blue). */
.btn-primary {
  border-color: rgba(37, 99, 235, 0.55);
  background: rgba(37, 99, 235, 0.24);
  color: #eff6ff;
}
.btn-primary:hover { filter: brightness(0.98); }

/* Non-destructive accent actions (copy/save/etc). */
.btn-accent,
.btn.btn-accent {
  border-color: rgba(37, 99, 235, 0.55);
  background: rgba(37, 99, 235, 0.14);
  color: #dbeafe;
}
.btn-accent:hover,
.btn.btn-accent:hover { filter: brightness(0.98); }

/* Cautionary actions (bulk recompute/backfill/etc) should be amber, not red. */
.btn-warn,
.btn.btn-warn {
  border-color: rgba(245, 158, 11, 0.55);
  background: rgba(245, 158, 11, 0.18);
  color: #fffbeb;
}
.btn-warn:hover,
.btn.btn-warn:hover { filter: brightness(0.98); }

/* Destructive actions (delete/reset/etc) should be explicitly red. */
.btn-danger,
.btn.btn-danger {
  border-color: rgba(239, 68, 68, 0.50);
  background: var(--primary);
  color: var(--primary-contrast);
}
.btn-danger:hover,
.btn.btn-danger:hover { filter: brightness(0.96); }

.btn-ok {
  border-color: rgba(34, 197, 94, 0.50);
  background: #16a34a;
  color: #ffffff;
}
.btn-ok:hover { filter: brightness(0.96); }

html[data-theme="light"] .btn-primary {
  border-color: rgba(37, 99, 235, 0.40);
  background: rgba(37, 99, 235, 0.12);
  color: #1d4ed8;
}

html[data-theme="light"] .btn-accent,
html[data-theme="light"] .btn.btn-accent {
  border-color: rgba(37, 99, 235, 0.40);
  background: rgba(37, 99, 235, 0.07);
  color: #1d4ed8;
}

html[data-theme="light"] .btn-warn,
html[data-theme="light"] .btn.btn-warn {
  border-color: rgba(245, 158, 11, 0.45);
  background: rgba(245, 158, 11, 0.10);
  color: #92400e;
}

html[data-theme="light"] .btn-ok {
  border-color: rgba(22, 163, 74, 0.40);
}

html[data-theme="light"] .btn-danger,
html[data-theme="light"] .btn.btn-danger {
  border-color: rgba(220, 38, 38, 0.40);
}

.input {
  border: 1px solid var(--border);
  background: var(--control-bg);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-weight: 650;
  min-width: 220px;
  outline: none;
}

select {
  border: 1px solid var(--border);
  background: var(--control-bg);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-weight: 650;
  outline: none;
}

select:focus {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

select option {
  background-color: var(--surface);
  color: var(--text);
}
.input::placeholder { color: var(--muted); opacity: 0.65; }
.input:focus {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.navrow {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: nowrap;
  overflow-x: auto;
  padding: 10px 0;
}
.navrow::-webkit-scrollbar { height: 8px; }
.navrow::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 999px; }

.navrow a {
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--control-bg);
  font-weight: 900;
  color: var(--text);
}
.navrow a:hover { text-decoration: none; border-color: rgba(220, 38, 38, 0.40); }
.navrow .sep { color: var(--muted); font-weight: 900; }

/* Website-style nav for authenticated app routes (replaces bubble/pill nav look). */
.admin-site-nav {
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.admin-site-nav a {
  border: none;
  background: transparent;
  border-radius: 8px;
  padding: 10px 8px;
  font-weight: 750;
  color: var(--muted);
  border-bottom: 2px solid transparent;
}

.admin-site-nav a:hover {
  color: var(--text);
  background: var(--control-bg);
  border-color: transparent;
  text-decoration: none;
}

.admin-site-nav a.is-active,
.admin-site-nav a[aria-current="page"] {
  color: var(--text);
  background: var(--control-bg);
  border-bottom-color: var(--t-primary, var(--primary));
}

.admin-site-nav .sep {
  color: var(--muted);
  opacity: 0.55;
}

.admin-site-nav .nav-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.admin-site-nav .nav-utility-btn {
  border-radius: 8px;
  padding: 8px 12px;
}

@media (max-width: 960px) {
  .admin-site-nav {
    gap: 8px;
  }

  .admin-site-nav .sep {
    display: none;
  }

  .admin-site-nav a {
    padding: 8px 8px;
  }
}

.table-wrap {
  overflow: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: linear-gradient(180deg, var(--card), var(--card2));
  box-shadow: var(--shadow-sm);
}

table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: left;
  padding: 12px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  vertical-align: top;
}
th {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--control-bg);
}
tr:last-child td { border-bottom: none; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--control-bg);
  color: var(--text);
  font-weight: 900;
  font-size: 12px;
}

/* Semantic pills (QC status etc) */
.pill.ok {
  background: #16a34a;
  border-color: #15803d;
  color: #ffffff;
}

.pill.warn {
  background: #f59e0b;
  border-color: #d97706;
  color: #111827;
}

.pill.bad {
  background: #dc2626;
  border-color: #b91c1c;
  color: #ffffff;
}

.muted { color: var(--muted); }

/* Setup dialog status semantics (used by run_setup.js) */
.setup-status[data-kind="warning"] { color: #f59e0b; }
.setup-status[data-kind="error"] { color: var(--primary); }

.grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 900px) { .grid.cols-2 { grid-template-columns: 1fr 1fr; } }

@media (max-width: 720px) {
  .container { padding: 12px; }
  .row { gap: 8px; }
  .input { min-width: 0; flex: 1; }
}
