:root {
  color-scheme: dark;

  /* Surfaces */
  --bg: #0a0a0d;
  --surface: #16161b;
  --surface-2: #1e1e25;
  --surface-hover: #24242c;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);

  /* Ink */
  --text-primary: #f5f5f7;
  --text-secondary: #a6a6b3;
  --text-muted: #6f6f7c;

  /* Accent (validated categorical slot 1) */
  --accent: #3987e5;
  --accent-strong: #6da7ec;
  --accent-soft: rgba(57, 135, 229, 0.14);
  --accent-ink: #eaf2fd;

  /* Secondary series (validated slot 4) */
  --accent-2: #c98500;
  --accent-2-soft: rgba(201, 133, 0, 0.16);

  /* Status */
  --success: #0ca30c;
  --success-soft: rgba(12, 163, 12, 0.14);
  --critical: #e66767;
  --critical-soft: rgba(230, 103, 103, 0.14);
  --warning-border: #c98500;
  --warning-bg: rgba(201, 133, 0, 0.12);

  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 7px;
  --focus-ring: #6da7ec;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body.auth-pending .app-shell {
  visibility: hidden;
}

a {
  color: var(--accent-strong);
}

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--surface);
  color: var(--text-primary);
  padding: 12px 16px;
  border-radius: 0 0 8px 0;
  z-index: 100;
}

.skip-link:focus {
  left: 0;
}

:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

button,
.btn {
  font-family: inherit;
  font-size: 0.95rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  padding: 8px 16px;
  cursor: pointer;
}

button:hover {
  background: var(--surface-hover);
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.primary:hover {
  background: var(--accent-strong);
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ---------- App shell: sidebar + main ---------- */

.app-shell {
  display: flex;
  min-height: 100vh;
  align-items: stretch;
}

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 22px;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 0.8rem;
  letter-spacing: -0.02em;
  flex-shrink: 0;
}

.brand-name {
  font-weight: 600;
  font-size: 0.98rem;
  letter-spacing: -0.01em;
}

.sidebar-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  text-decoration: none;
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 0.92rem;
}

.sidebar-nav a svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: 0.85;
}

.sidebar-nav a:hover {
  background: var(--surface-2);
  color: var(--text-primary);
}

.sidebar-nav a[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent-ink);
}

.sidebar-nav a[aria-current="page"] svg {
  opacity: 1;
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  min-width: 0;
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent-2);
  color: #1a1206;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.8rem;
  flex-shrink: 0;
}

.user-email-text {
  font-size: 0.82rem;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-signout {
  display: flex;
  align-items: center;
  gap: 9px;
  justify-content: flex-start;
  width: 100%;
  background: transparent;
  border-color: var(--border);
  color: var(--text-secondary);
  font-size: 0.88rem;
}

.sidebar-signout svg {
  width: 16px;
  height: 16px;
}

.sidebar-signout:hover {
  background: var(--surface-2);
  color: var(--text-primary);
}

.main-col {
  flex: 1;
  min-width: 0;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 32px 28px 48px;
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 4px;
  letter-spacing: -0.01em;
}

.page-subtitle {
  color: var(--text-muted);
  margin: 0 0 24px;
  font-size: 0.9rem;
}

.status-region {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: var(--warning-bg);
  border: 1px solid var(--warning-border);
  margin-bottom: 20px;
  font-size: 0.9rem;
}

.status-region[hidden] {
  display: none;
}

/* ---------- KPI cards ---------- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 14px;
  margin-bottom: 32px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-color: var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
}

.kpi-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.kpi-card h3 {
  margin: 0;
  font-size: 0.74rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

/* Plain colored text with an arrow, not a filled pill -- e.g. "▲ 2.4% vs
   last month". `neutral` is used for changes that aren't inherently
   good/bad news (job count, contract value). */
.kpi-delta {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 0.78rem;
  font-weight: 700;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.kpi-delta.good {
  color: var(--success);
}

.kpi-delta.bad {
  color: var(--critical);
}

.kpi-delta.neutral {
  color: var(--text-muted);
  font-weight: 600;
}

.kpi-card p.kpi-value {
  margin: 0;
  font-size: clamp(1.3rem, 1.05rem + 1vw, 1.7rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.kpi-card p.kpi-sub {
  margin: 6px 0 0;
  font-size: 0.82rem;
  color: var(--text-secondary);
}

.kpi-card-body {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
}

.kpi-card-body .sparkline-wrap {
  flex-shrink: 0;
}

.kpi-card.negative p.kpi-value {
  color: var(--critical);
}

.kpi-card.positive p.kpi-value {
  color: var(--success);
}

section.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px 24px;
  margin-bottom: 24px;
}

section.panel h2 {
  margin-top: 0;
  font-size: 1.05rem;
  letter-spacing: -0.005em;
}

/* ---------- Segmented control ---------- */

.segmented {
  display: inline-flex;
  padding: 3px;
  background: var(--surface-2);
  border-radius: 999px;
  gap: 2px;
  margin-bottom: 16px;
}

.segmented button {
  border: none;
  background: transparent;
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 0.86rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.segmented button:hover {
  background: transparent;
  color: var(--text-primary);
}

.segmented button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text-primary);
  box-shadow: 0 1px 0 var(--border-strong);
}

/* ---------- Charts ---------- */

.viz-svg {
  width: 100%;
  height: auto;
}

.chart-gridline {
  stroke: var(--border);
  stroke-width: 1;
}

.chart-axis-label {
  fill: var(--text-muted);
  font-size: 11px;
}

.chart-line {
  fill: none;
  stroke-width: 2.5;
}

.chart-area {
  stroke: none;
}

.chart-overlay {
  fill: transparent;
  cursor: crosshair;
}

.chart-overlay:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.chart-guide {
  stroke: var(--border-strong);
  stroke-width: 1;
  stroke-dasharray: 3 3;
  opacity: 0;
  pointer-events: none;
  transition: opacity 100ms ease;
}

.chart-hover-dot {
  stroke: var(--surface);
  stroke-width: 2;
  opacity: 0;
  pointer-events: none;
  transition: opacity 100ms ease;
}

.chart-hover-pill {
  opacity: 0;
  pointer-events: none;
  transition: opacity 100ms ease;
}

.chart-pill-bg {
  fill: var(--surface-2);
  stroke: var(--border-strong);
  stroke-width: 1;
}

.chart-pill-text {
  fill: var(--text-primary);
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------- Sparklines & gauge ---------- */

.sparkline-svg {
  width: 92px;
  height: 30px;
  display: block;
}

.sparkline-path {
  fill: none;
  stroke-width: 2;
}

.gauge-svg {
  width: 148px;
  height: 148px;
  display: block;
}

.gauge-track {
  stroke: var(--surface-2);
}

.gauge-arc {
  transition: stroke-dasharray 300ms ease;
}

.gauge-pct {
  fill: var(--text-primary);
  font-size: 26px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.gauge-of {
  fill: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.gauge-panel {
  display: flex;
  align-items: center;
  gap: 28px;
  flex-wrap: wrap;
}

.gauge-details p.kpi-value {
  font-size: 2.1rem;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.chart-legend {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 10px 0 0;
  margin: 0;
  font-size: 0.84rem;
  color: var(--text-secondary);
}

.chart-legend-swatch {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 2px;
  margin-right: 6px;
  vertical-align: middle;
}

.chart-data-table {
  margin-top: 16px;
}

.chart-data-table summary {
  cursor: pointer;
  color: var(--accent-strong);
  font-size: 0.86rem;
}

.chart-tooltip {
  position: absolute;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text-primary);
  padding: 8px 12px;
  border-radius: var(--radius-md);
  font-size: 0.8rem;
  pointer-events: none;
  z-index: 50;
  max-width: 240px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.chart-tooltip .tt-label {
  color: var(--text-muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  display: block;
  margin-bottom: 2px;
}

.chart-tooltip .tt-value {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------- Tables ---------- */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

caption {
  text-align: left;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}

th,
td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

th button.sort-btn {
  border: none;
  background: none;
  padding: 0;
  font: inherit;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.76rem;
  color: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

tbody tr:hover {
  background: var(--surface-2);
}

.table-scroll {
  overflow-x: auto;
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.badge.personal {
  background: var(--warning-bg);
  color: #e3ac3d;
  border: 1px solid var(--warning-border);
}

.negative-amount {
  color: var(--critical);
}

.positive-amount {
  color: var(--success);
}

/* ---------- Auth pages (login / reset-password) ---------- */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  max-width: 380px;
  width: 100%;
}

.login-card h1 {
  font-size: 1.3rem;
}

.field {
  margin: 16px 0;
}

.field label {
  display: block;
  font-size: 0.86rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-secondary);
}

.field input {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 1rem;
}

.field input:focus {
  border-color: var(--accent);
}

.footer-note {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: 24px;
}

details.tranche-group {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 12px;
  padding: 4px 18px;
  background: var(--surface);
}

details.tranche-group summary {
  cursor: pointer;
  padding: 14px 0;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

@media (max-width: 860px) {
  .app-shell {
    flex-direction: column;
  }

  .sidebar {
    position: static;
    width: 100%;
    height: auto;
    flex-direction: row;
    align-items: center;
    padding: 12px 16px;
    gap: 16px;
  }

  .sidebar-brand {
    padding: 0;
  }

  .sidebar-nav ul {
    flex-direction: row;
  }

  .sidebar-nav a span.nav-label {
    display: none;
  }

  .sidebar-footer {
    margin-top: 0;
    margin-left: auto;
    padding-top: 0;
    border-top: none;
    flex-direction: row;
    align-items: center;
  }

  .user-email-text {
    display: none;
  }

  .sidebar-signout span {
    display: none;
  }

  main {
    padding: 24px 16px 40px;
  }
}
