:root {
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "Cascadia Mono", "SFMono-Regular", Consolas,
    "Liberation Mono", Menlo, monospace;

  --ink: #14213a;
  --ink-soft: #33425e;
  --muted: #5c6c86;
  --faint: #8a97ad;
  --paper: #eef2f8;
  --surface: #ffffff;
  --surface-2: #f6f8fc;
  --line: #dde4ef;
  --line-strong: #c7d1e2;

  --accent: #2456d6;
  --accent-ink: #1b429e;
  --accent-wash: #eaf0fe;
  --teal: #0e8f9e;

  --good: #12805c;
  --good-wash: #e4f4ec;
  --info: #2f5fd0;
  --info-wash: #e9effc;
  --warn: #b56a00;
  --warn-wash: #fbf0dd;
  --bad: #c02b2b;
  --bad-wash: #fbe7e7;

  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 16px;
  --r-pill: 999px;

  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;
  --sp-7: 48px;

  --shadow-sm: 0 1px 2px rgba(20, 33, 58, 0.06), 0 1px 3px rgba(20, 33, 58, 0.05);
  --shadow-md: 0 4px 12px rgba(20, 33, 58, 0.08), 0 2px 4px rgba(20, 33, 58, 0.04);
  --shadow-lg: 0 18px 40px rgba(20, 33, 58, 0.12);

  --maxw: 1080px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink);
  background: linear-gradient(180deg, #f2f5fa 0%, var(--paper) 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent-ink);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.15;
  letter-spacing: -0.01em;
  font-weight: 650;
}
h1 {
  font-size: 1.75rem;
}
h2 {
  font-size: 1.2rem;
}
h3 {
  font-size: 1rem;
}
p {
  margin: 0 0 var(--sp-3);
}

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

.eyebrow {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--faint);
  margin: 0 0 var(--sp-2);
}

.muted {
  color: var(--muted);
}

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: saturate(1.2) blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar__inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: var(--sp-3) var(--sp-5);
  display: flex;
  align-items: center;
  gap: var(--sp-5);
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  font-weight: 700;
  letter-spacing: -0.02em;
  font-size: 1.05rem;
  color: var(--ink);
}
.brand:hover {
  text-decoration: none;
}
.brand__mark {
  width: 26px;
  height: 26px;
  flex: none;
}

.nav {
  display: flex;
  gap: var(--sp-1);
  margin-left: var(--sp-3);
}
.nav a {
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-sm);
  color: var(--ink-soft);
  font-size: 0.94rem;
  font-weight: 550;
}
.nav a:hover {
  background: var(--surface-2);
  text-decoration: none;
}
.nav a.is-active {
  background: var(--accent-wash);
  color: var(--accent-ink);
}

.session {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--muted);
}
.session__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--good);
  box-shadow: 0 0 0 3px var(--good-wash);
}

/* Layout */
.container {
  width: 100%;
  max-width: var(--maxw);
  margin: 0 auto;
  padding: var(--sp-6) var(--sp-5) var(--sp-7);
  flex: 1 0 auto;
}
.page-head {
  margin-bottom: var(--sp-5);
}
.page-head p {
  color: var(--muted);
  margin: var(--sp-2) 0 0;
  max-width: 60ch;
}

.footer {
  border-top: 1px solid var(--line);
  color: var(--faint);
  font-size: 0.82rem;
}
.footer__inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: var(--sp-4) var(--sp-5);
  display: flex;
  justify-content: space-between;
  gap: var(--sp-4);
  flex-wrap: wrap;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}
.card__body {
  padding: var(--sp-5);
}
.card__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}

.grid {
  display: grid;
  gap: var(--sp-4);
}
@media (min-width: 720px) {
  .grid--2 {
    grid-template-columns: 1fr 1fr;
  }
  .grid--3 {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  font: inherit;
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1;
  padding: 0.72rem 1.1rem;
  border-radius: var(--r-md);
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.06s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.btn:active {
  transform: translateY(1px);
}
.btn--primary {
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow-sm);
}
.btn--primary:hover {
  background: var(--accent-ink);
  text-decoration: none;
}
.btn--ghost {
  background: var(--surface);
  border-color: var(--line-strong);
  color: var(--ink);
}
.btn--ghost:hover {
  background: var(--surface-2);
  text-decoration: none;
}
.btn--lg {
  padding: 0.85rem 1.4rem;
  font-size: 1rem;
}

/* Forms */
.field {
  display: block;
  margin-bottom: var(--sp-4);
}
.field__label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: var(--sp-2);
}
.field__hint {
  color: var(--muted);
  font-size: 0.82rem;
  margin-top: var(--sp-2);
}
.input {
  width: 100%;
  font: inherit;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--ink);
}
.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-wash);
}

/* Tables */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
.table th {
  text-align: left;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--faint);
  font-weight: 600;
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  white-space: nowrap;
}
.table td {
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
.table tbody tr:last-child td {
  border-bottom: none;
}
.table tbody tr {
  transition: background 0.12s ease;
}
.table tbody tr:hover {
  background: var(--surface-2);
}
.table td.num,
.table th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.cell-id {
  font-family: var(--font-mono);
  font-weight: 600;
}

.pager {
  display: flex;
  gap: var(--sp-3);
  margin-top: var(--sp-4);
}
.pager__spacer {
  flex: 1;
}

/* Badges & chips */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  font-weight: 600;
  padding: 0.28rem 0.6rem;
  border-radius: var(--r-pill);
  border: 1px solid transparent;
  white-space: nowrap;
}
.badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}
.badge--good {
  color: var(--good);
  background: var(--good-wash);
}
.badge--info {
  color: var(--info);
  background: var(--info-wash);
}
.badge--warn {
  color: var(--warn);
  background: var(--warn-wash);
}
.badge--bad {
  color: var(--bad);
  background: var(--bad-wash);
}
.badge--neutral {
  color: var(--muted);
  background: var(--surface-2);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 0.45rem 0.75rem;
  border-radius: var(--r-md);
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 0.86rem;
}
.chip__count {
  font-family: var(--font-mono);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.chip__label {
  color: var(--muted);
}
.chip.is-empty {
  color: var(--faint);
}
.chip.is-empty .chip__count {
  color: var(--faint);
}
.chip--flag {
  border-color: var(--accent-wash);
  background: var(--accent-wash);
  color: var(--accent-ink);
  font-weight: 600;
}

/* Score gauge */
.score-panel {
  display: grid;
  gap: var(--sp-5);
  align-items: center;
}
@media (min-width: 720px) {
  .score-panel {
    grid-template-columns: minmax(240px, 300px) 1fr;
  }
}
.gauge {
  text-align: center;
}
.gauge__svg {
  width: 100%;
  max-width: 280px;
  height: auto;
  display: block;
  margin: 0 auto;
}
.gauge__track {
  fill: none;
  stroke-width: 16;
  stroke-linecap: round;
}
.gauge__needle {
  transition: transform 0.9s cubic-bezier(0.16, 1, 0.3, 1);
  transform-origin: 120px 120px;
}
.gauge__value {
  margin-top: calc(-1 * var(--sp-5));
}
.gauge__num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 3.4rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1;
  color: var(--ink);
}
.gauge__scale {
  color: var(--faint);
  font-size: 1rem;
  font-family: var(--font-mono);
}
.gauge__stage {
  margin-top: var(--sp-3);
  color: var(--muted);
  font-size: 0.86rem;
}
.gauge__stage b {
  color: var(--ink-soft);
}

.score-summary {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}
.score-summary__band {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  flex-wrap: wrap;
}
.score-summary__desc {
  color: var(--muted);
  margin: 0;
}
.meta-row {
  display: flex;
  gap: var(--sp-5);
  flex-wrap: wrap;
  padding-top: var(--sp-4);
  border-top: 1px solid var(--line);
}
.meta {
  min-width: 0;
}
.meta__label {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--faint);
  margin-bottom: 2px;
}
.meta__value {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--ink);
  word-break: break-all;
}

/* Factor breakdown */
.factors {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}
.factor {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--sp-2);
}
.factor__top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
}
.factor__name {
  font-weight: 600;
  text-transform: capitalize;
}
.factor__weight {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--faint);
}
.factor__bar {
  position: relative;
  height: 10px;
  border-radius: var(--r-pill);
  background: var(--surface-2);
  overflow: hidden;
  border: 1px solid var(--line);
}
.factor__fill {
  position: absolute;
  inset: 0 auto 0 0;
  border-radius: var(--r-pill);
  background: linear-gradient(90deg, var(--teal), var(--accent));
}
.factor__nums {
  display: flex;
  gap: var(--sp-4);
  font-family: var(--font-mono);
  font-size: 0.76rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.factor__nums b {
  color: var(--ink);
}

/* Distribution */
.dist {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}
.dist__value {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--ink);
}
.dist__value span {
  font-size: 0.9rem;
  color: var(--faint);
  font-weight: 500;
}
.dist__bar {
  position: relative;
  height: 12px;
  border-radius: var(--r-pill);
  background: var(--surface-2);
  border: 1px solid var(--line);
  overflow: hidden;
}
.dist__fill {
  position: absolute;
  inset: 0 auto 0 0;
  border-radius: var(--r-pill);
}
.dist--income .dist__fill {
  background: linear-gradient(90deg, #7a63e0, #4a34b8);
}
.dist--wealth .dist__fill {
  background: linear-gradient(90deg, #d99a2a, #a86f0c);
}
.dist__scale {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 0.66rem;
  color: var(--faint);
}

/* Upload groups */
.upload-groups {
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}
.upload-group {
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.upload-group[open] {
  padding-bottom: var(--sp-4);
}
.upload-group__summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: var(--sp-4);
  font-weight: 600;
}
.upload-group__summary::-webkit-details-marker {
  display: none;
}
.upload-group__summary::before {
  content: "\25b8";
  color: var(--faint);
  transition: transform 0.15s ease;
}
.upload-group[open] > .upload-group__summary::before {
  transform: rotate(90deg);
}
.upload-group__file {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upload-group__meta {
  font-weight: 500;
  font-size: 0.85rem;
  white-space: nowrap;
}
.upload-group .table-wrap {
  margin: 0 var(--sp-4);
  width: auto;
  border-radius: var(--r-md);
}

.empty {
  padding: var(--sp-6);
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-lg);
  background: var(--surface-2);
}

/* Landing */
.hero {
  padding: var(--sp-7) 0 var(--sp-6);
}
.hero__badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 0.35rem 0.75rem;
  border-radius: var(--r-pill);
  border: 1px solid var(--line);
  background: var(--surface);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: var(--sp-5);
}
.hero h1 {
  font-size: clamp(2rem, 5vw, 3.1rem);
  max-width: 16ch;
}
.hero p {
  font-size: 1.12rem;
  color: var(--muted);
  max-width: 52ch;
  margin-top: var(--sp-4);
}
.hero__cta {
  display: flex;
  gap: var(--sp-3);
  margin-top: var(--sp-5);
  flex-wrap: wrap;
}
.feature-row {
  margin-top: var(--sp-7);
}
.feature h3 {
  margin-bottom: var(--sp-2);
}
.feature p {
  color: var(--muted);
  margin: 0;
  font-size: 0.95rem;
}
.feature__k {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.14em;
  color: var(--teal);
  text-transform: uppercase;
  margin-bottom: var(--sp-3);
}

/* Toasts */
.toast-stack {
  position: fixed;
  top: var(--sp-4);
  right: var(--sp-4);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  width: min(360px, calc(100vw - 2 * var(--sp-4)));
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  padding: 0.8rem 0.9rem;
  border-radius: var(--r-md);
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--muted);
  box-shadow: var(--shadow-lg);
  transform: translateX(120%);
  transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.25s ease;
}
.toast.is-in {
  transform: translateX(0);
}
.toast.is-out {
  transform: translateX(120%);
  opacity: 0;
}
.toast--success {
  border-left-color: var(--good);
}
.toast--error {
  border-left-color: var(--bad);
}
.toast--info {
  border-left-color: var(--accent);
}
.toast__icon {
  flex: none;
  width: 18px;
  height: 18px;
  margin-top: 1px;
}
.toast--success .toast__icon {
  color: var(--good);
}
.toast--error .toast__icon {
  color: var(--bad);
}
.toast--info .toast__icon {
  color: var(--accent);
}
.toast__msg {
  flex: 1;
  font-size: 0.9rem;
  color: var(--ink);
}
.toast__close {
  flex: none;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--faint);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0 2px;
}
.toast__close:hover {
  color: var(--ink);
}

@media (max-width: 620px) {
  .topbar__inner {
    flex-wrap: wrap;
    gap: var(--sp-3);
  }
  .nav {
    order: 3;
    width: 100%;
    margin-left: 0;
  }
  .session {
    margin-left: auto;
  }
  .container {
    padding: var(--sp-5) var(--sp-4) var(--sp-6);
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
