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

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

body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  margin: 0 0 var(--space-3);
  font-weight: 600;
  line-height: 1.25;
}

h1 { font-size: var(--text-2xl); }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }

p { margin: 0 0 var(--space-3); }

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

button, input, select {
  font: inherit;
  color: inherit;
}

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: var(--space-5);
}

.muted {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

/* Forms */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--space-3);
  align-items: end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.field label {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.field input,
.field select,
.field textarea {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-bg);
  color: var(--color-text);
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: 2px solid var(--color-accent);
  outline-offset: -1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-2) var(--space-4);
  border: none;
  border-radius: var(--radius);
  background: var(--color-accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: var(--color-accent-hover); }

.btn-ghost {
  background: none;
  border: none;
  color: var(--color-text-muted);
  padding: var(--space-1) var(--space-2);
  font-weight: 500;
  border-radius: var(--radius);
  cursor: pointer;
}
.btn-ghost:hover { background: var(--color-bg); color: var(--color-text); }

.btn-ghost-danger { color: var(--color-danger); }
.btn-ghost-danger:hover { background: var(--color-bg); color: var(--color-danger); }

.btn-ghost-accent { color: var(--color-accent); }
.btn-ghost-accent:hover { background: var(--color-bg); color: var(--color-accent); }

.btn-ghost-success { color: var(--color-success); }
.btn-ghost-success:hover { background: var(--color-bg); color: var(--color-success); }

/* Tables */
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.table th,
.table td {
  text-align: left;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

.table th {
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

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

.badge {
  display: inline-block;
  padding: 2px var(--space-2);
  border-radius: 999px;
  font-size: var(--text-xs);
  background: var(--color-bg);
  color: var(--color-text-muted);
}

.badge-review {
  background: rgba(161, 92, 0, 0.12);
  color: var(--color-warning);
}

.badge-success {
  background: rgba(30, 122, 76, 0.12);
  color: var(--color-success);
}

.badge-danger {
  background: rgba(179, 38, 30, 0.12);
  color: var(--color-danger);
}

.badge-accent {
  background: rgba(31, 95, 139, 0.12);
  color: var(--color-accent);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

/* Dashboard / charts */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: var(--space-5);
}

.stat-tile {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.stat-tile-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
}

.stat-tile-label {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  font-weight: 500;
}

.stat-tile-value {
  font-size: var(--text-3xl);
  font-weight: 700;
  letter-spacing: -0.01em;
}

.stat-tile-trend {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--text-sm);
  font-weight: 600;
}

.stat-tile-trend.is-up { color: var(--color-success); }
.stat-tile-trend.is-down { color: var(--color-danger); }
.stat-tile-trend.is-flat { color: var(--color-text-muted); }

.stat-tile-trend-label {
  color: var(--color-text-muted);
  font-weight: 400;
}

/* Icon badges — colored circular anchors used on stat tiles and cards */
.icon-badge {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: var(--text-base);
  color: #fff;
}

.icon-badge-sm {
  width: 32px;
  height: 32px;
  font-size: var(--text-sm);
  border-radius: 10px;
}

.icon-badge svg {
  width: 20px;
  height: 20px;
}

.icon-badge-sm svg {
  width: 16px;
  height: 16px;
}

.badge-slot-1 { background: var(--series-self); }
.badge-slot-2 { background: var(--series-spouse); }
.badge-slot-3 { background: #eda100; }
.badge-slot-4 { background: #4a3aa7; }
.badge-slot-5 { background: #e34948; }
.badge-slot-6 { background: #eb6834; }
.badge-slot-neutral { background: var(--color-text-muted); }

/* Clickable cards — clear affordance for "this responds to a click" */
.card-clickable {
  cursor: pointer;
  transition: box-shadow 0.15s ease, transform 0.15s ease, border-color 0.15s ease;
}

.card-clickable:hover,
.card-clickable:focus-visible {
  box-shadow: var(--shadow-card-hover);
  transform: translateY(-2px);
  border-color: var(--color-accent);
  outline: none;
}

.card-clickable.is-active {
  border-color: var(--color-accent);
  box-shadow: var(--shadow-card-hover);
}

/* Institution / account cards */
.account-group-title {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: var(--space-5) 0 var(--space-3);
}

.account-group-title:first-child {
  margin-top: 0;
}

.account-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: var(--space-4);
}

.account-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.account-card-head {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.account-card-name {
  font-weight: 600;
}

.account-card-meta {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.account-card-metric-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.account-card-metric-value {
  font-size: var(--text-xl);
  font-weight: 700;
}

.account-card-actions {
  margin-top: auto;
  padding-top: var(--space-2);
  border-top: 1px solid var(--color-border);
  display: flex;
  justify-content: flex-end;
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}

.chart-legend {
  display: flex;
  gap: var(--space-4);
}

.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.chart-legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  display: inline-block;
}

.chart-mark {
  cursor: pointer;
}

.chart-mark:hover,
.chart-mark:focus {
  opacity: 0.85;
  outline: none;
}

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

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

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

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

.chart-value-label {
  fill: var(--color-text);
  font-size: 11px;
  dominant-baseline: middle;
}

.chart-value-label-small {
  fill: var(--color-text-muted);
  font-size: 10px;
}

.chart-tooltip {
  position: fixed;
  z-index: 50;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-sm);
  pointer-events: none;
  max-width: 240px;
}

.chart-tooltip-title {
  font-weight: 600;
  margin-bottom: var(--space-1);
}

.chart-tooltip-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.chart-tooltip-key {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text-muted);
}

.chart-tooltip-key::before {
  content: "";
  width: 10px;
  height: 2px;
  background: var(--key-color, currentColor);
  display: inline-block;
}

details.chart-table-toggle > summary {
  cursor: pointer;
  color: var(--color-accent);
  font-size: var(--text-sm);
  margin-top: var(--space-3);
}

details.chart-table-toggle table {
  margin-top: var(--space-3);
}

details.drilldown {
  padding: var(--space-2) 0;
  border-top: 1px solid var(--color-border);
}

details.drilldown:first-child {
  border-top: none;
}

details.drilldown > summary {
  margin-top: 0;
  color: var(--color-text);
  font-weight: 500;
}

details.drilldown table {
  margin-left: var(--space-5);
  width: calc(100% - var(--space-5));
}

/* Toasts */
.toast-container {
  position: fixed;
  bottom: var(--space-5);
  right: var(--space-5);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: min(360px, calc(100vw - var(--space-6)));
}

.toast {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-left: 3px solid var(--color-text-muted);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-sm);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast-visible {
  opacity: 1;
  transform: translateY(0);
}

.toast-success { border-left-color: var(--color-success); }
.toast-error { border-left-color: var(--color-danger); }
