:root {
  --font-poppins: 'Poppins';
  --font-sans: var(--font-poppins), 'Poppins', ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

  --r-color-brand-950: #0F157A;
  --r-color-brand-900: #18208C;
  --r-color-brand-800: #1E27AD;
  --r-color-brand-700: #3340C7;
  --r-color-brand-600: #4C5BE0;
  --r-color-brand-500: #6F7CEF;
  --r-color-brand-100: #E8EBFF;
  --r-color-brand-50: #F4F6FF;

  --r-color-mint-900: #046C54;
  --r-color-mint-800: #047857;
  --r-color-mint-700: #058A69;
  --r-color-mint-600: #07A97E;
  --r-color-mint-500: #07F49E;
  --r-color-mint-100: #D9FFF0;
  --r-color-mint-50: #EEFFF7;

  --r-color-ink-950: #0B1220;
  --r-color-ink-900: #0F172A;
  --r-color-ink-800: #1E293B;
  --r-color-ink-700: #334155;
  --r-color-ink-600: #475569;
  --r-color-ink-500: #64748B;

  --r-color-border-300: #CBD5E1;
  --r-color-border-200: #E2E8F0;
  --r-color-surface-100: #F1F5F9;
  --r-color-surface-50: #F8FAFC;
  --r-color-white: #FFFFFF;

  --r-color-success-700: #047857;
  --r-color-success-100: #D1FAE5;
  --r-color-success-50: #ECFDF5;

  --r-color-warning-700: #B45309;
  --r-color-warning-100: #FDE68A;
  --r-color-warning-50: #FFFBEB;

  --r-color-danger-700: #B91C1C;
  --r-color-danger-100: #FECACA;
  --r-color-danger-50: #FEF2F2;

  --r-color-info-700: #1D4ED8;
  --r-color-info-100: #DBEAFE;
  --r-color-info-50: #EFF6FF;

  --r-space-1: 4px;
  --r-space-2: 8px;
  --r-space-3: 12px;
  --r-space-4: 16px;
  --r-space-5: 20px;
  --r-space-6: 24px;
  --r-space-8: 32px;
  --r-space-10: 40px;
  --r-space-12: 48px;
  --r-space-16: 64px;

  --r-radius-sm: 8px;
  --r-radius-md: 12px;
  --r-radius-lg: 16px;
  --r-radius-xl: 20px;
  --r-radius-2xl: 24px;

  --r-shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04), 0 2px 6px rgba(15, 23, 42, 0.04);
  --r-shadow-md: 0 6px 16px rgba(15, 23, 42, 0.07), 0 2px 6px rgba(15, 23, 42, 0.05);
  --r-shadow-lg: 0 16px 38px rgba(15, 23, 42, 0.10), 0 6px 18px rgba(15, 23, 42, 0.06);

  --r-focus-ring: 0 0 0 3px rgba(111, 124, 239, 0.35);

  --r-chart-primary: #1E27AD;
  --r-chart-positive: #07A97E;
  --r-chart-neutral: #64748B;
  --r-chart-comparison: #A5B4FC;
  --r-chart-warning: #D97706;
  --r-chart-risk: #DC2626;

  --r-text-primary: var(--r-color-ink-900);
  --r-text-secondary: var(--r-color-ink-700);
  --r-text-muted: var(--r-color-ink-500);
  --r-text-disabled: #94A3B8;

  --r-bg-app: var(--r-color-surface-50);
  --r-bg-card: var(--r-color-white);
  --r-bg-subpanel: var(--r-color-surface-100);

  /* Legacy aliases to avoid breaking existing templates */
  --app-bg: var(--r-bg-app);
  --app-surface: var(--r-bg-card);
  --app-surface-soft: var(--r-bg-subpanel);
  --app-sidebar: #f2f5fb;
  --app-header: #f8fbff;
  --app-border: var(--r-color-border-200);
  --app-text: var(--r-text-primary);
  --app-muted: var(--r-text-muted);
  --app-muted-soft: var(--r-text-disabled);
  --nav-hover: #edf2fb;
  --nav-hover-text: var(--r-color-ink-900);
  --nav-active-bg: var(--r-color-brand-700);
  --nav-active-text: #ffffff;
  --input-bg: #ffffff;
  --input-border: var(--r-color-border-300);
  --shadow-soft: var(--r-shadow-md);
}

html[data-theme='dark'] {
  color-scheme: dark;
  --r-bg-app: #0d1422;
  --r-bg-card: #111b2b;
  --r-bg-subpanel: #162133;
  --r-text-primary: #eef3fa;
  --r-text-secondary: #d1d9e7;
  --r-text-muted: #9bacc3;
  --r-text-disabled: #7f90a7;
  --r-color-border-200: #2a3749;
  --r-color-border-300: #324258;
  --app-sidebar: #0f1726;
  --app-header: #0f1726;
  --nav-hover: #1a2638;
  --nav-hover-text: #eef3fa;
  --nav-active-bg: #4C5BE0;
  --input-bg: #0f1a2a;
  --input-border: #304158;
}

*, *::before, *::after {
  box-sizing: border-box;
  font-family: var(--font-sans);
}

html,
body {
  background: var(--r-bg-app);
  color: var(--r-text-primary);
}

body {
  font-size: 14px;
  line-height: 22px;
  font-weight: 400;
}

/* Typographic roles */
.ds-display-xl { font-size: 32px; line-height: 38px; font-weight: 700; letter-spacing: -0.02em; }
.ds-h1 { font-size: 28px; line-height: 36px; font-weight: 700; letter-spacing: -0.02em; }
.ds-h2 { font-size: 22px; line-height: 30px; font-weight: 600; letter-spacing: -0.01em; }
.ds-h3 { font-size: 18px; line-height: 26px; font-weight: 600; }
.ds-section { font-size: 16px; line-height: 24px; font-weight: 600; }
.ds-body { font-size: 14px; line-height: 22px; font-weight: 400; }
.ds-body-sm { font-size: 13px; line-height: 20px; font-weight: 400; }
.ds-label { font-size: 13px; line-height: 18px; font-weight: 500; }
.ds-button-text { font-size: 14px; line-height: 20px; font-weight: 600; }
.ds-caption { font-size: 12px; line-height: 18px; font-weight: 400; }
.ds-table-head { font-size: 12px; line-height: 18px; font-weight: 600; }
.ds-table-cell { font-size: 13px; line-height: 20px; font-weight: 400; }

h1, .h1 {
  font-size: 28px;
  line-height: 36px;
  letter-spacing: -0.02em;
  font-weight: 700;
  color: var(--r-text-primary);
}

h2, .h2 {
  font-size: 22px;
  line-height: 30px;
  letter-spacing: -0.01em;
  font-weight: 600;
  color: var(--r-text-primary);
}

h3, .h3 {
  font-size: 18px;
  line-height: 26px;
  font-weight: 600;
  color: var(--r-text-primary);
}

.text-muted,
.text-secondary {
  color: var(--r-text-muted);
}

.tabular-nums,
.kpi-number,
.fin-number,
[data-financial],
table,
input[type='number'] {
  font-variant-numeric: tabular-nums lining-nums;
}

/* Surfaces */
.ds-surface,
.card,
.panel,
.surface-card {
  background: var(--r-bg-card);
  border: 1px solid var(--r-color-border-200);
  border-radius: var(--r-radius-lg);
  box-shadow: var(--r-shadow-sm);
}

.ds-subpanel {
  background: var(--r-bg-subpanel);
  border: 1px solid var(--r-color-border-200);
  border-radius: var(--r-radius-md);
}

/* Buttons */
.btn,
button.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--r-space-2);
  border-radius: var(--r-radius-md);
  border: 1px solid transparent;
  font-size: 14px;
  line-height: 20px;
  font-weight: 600;
  transition: all 0.18s ease;
}

.btn:focus-visible,
button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: none;
  box-shadow: var(--r-focus-ring);
}

.btn-sm { min-height: 36px; padding: 0 12px; }
.btn-md { min-height: 40px; padding: 0 14px; }
.btn-lg { min-height: 44px; padding: 0 18px; }

.btn-primary {
  background: var(--r-color-brand-800);
  color: #fff;
}
.btn-primary:hover { background: var(--r-color-brand-900); }

.btn-secondary {
  background: #fff;
  color: var(--r-color-brand-800);
  border-color: var(--r-color-border-300);
}
.btn-secondary:hover { background: var(--r-color-surface-100); }

.btn-accent {
  background: var(--r-color-mint-500);
  color: var(--r-color-ink-900);
}
.btn-accent:hover { background: var(--r-color-mint-600); }

.btn-ghost {
  background: transparent;
  color: var(--r-color-ink-700);
  border-color: transparent;
}
.btn-ghost:hover { background: var(--r-color-surface-100); }

.btn-danger {
  background: var(--r-color-danger-700);
  color: #fff;
}
.btn-danger:hover { filter: brightness(0.9); }

/* Form controls */
label {
  font-size: 13px;
  line-height: 18px;
  font-weight: 500;
  color: var(--r-color-ink-700);
}

input:not([type='checkbox']):not([type='radio']),
select,
textarea {
  min-height: 40px;
  width: 100%;
  border-radius: var(--r-radius-md);
  border: 1px solid var(--r-color-border-300);
  background: var(--input-bg);
  color: var(--r-text-primary);
  padding: 8px 12px;
  font-size: 14px;
  line-height: 20px;
  transition: all 0.18s ease;
}

input:not([type='checkbox']):not([type='radio'])::placeholder,
textarea::placeholder {
  color: var(--r-text-muted);
}

input:not([type='checkbox']):not([type='radio']):hover,
select:hover,
textarea:hover {
  border-color: var(--r-color-brand-500);
}

input:not([type='checkbox']):not([type='radio']):focus,
select:focus,
textarea:focus {
  border-color: var(--r-color-brand-600);
  box-shadow: var(--r-focus-ring);
  outline: none;
}

input:disabled,
select:disabled,
textarea:disabled,
button:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.field-help,
.helper-text {
  font-size: 12px;
  line-height: 18px;
  color: var(--r-text-muted);
}

.field-error,
.error-text {
  font-size: 12px;
  line-height: 18px;
  font-weight: 500;
  color: var(--r-color-danger-700);
}

/* Tables */
table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--r-bg-card);
}

table thead th {
  font-size: 12px;
  line-height: 18px;
  font-weight: 600;
  color: var(--r-color-ink-600);
  background: var(--r-bg-subpanel);
  border-bottom: 1px solid var(--r-color-border-200);
  text-transform: none;
  letter-spacing: 0.01em;
  padding: 10px 12px;
}

table tbody td {
  font-size: 13px;
  line-height: 20px;
  font-weight: 400;
  color: var(--r-color-ink-700);
  border-bottom: 1px solid var(--r-color-border-200);
  padding: 11px 12px;
}

table tbody tr:hover {
  background: rgba(111, 124, 239, 0.07);
}

.table-zebra tbody tr:nth-child(2n) {
  background: rgba(148, 163, 184, 0.04);
}

/* Badges / chips */
.badge,
.chip,
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  line-height: 18px;
  font-weight: 500;
  border: 1px solid transparent;
}

.badge-success { background: var(--r-color-success-50); color: var(--r-color-success-700); border-color: var(--r-color-success-100); }
.badge-warning { background: var(--r-color-warning-50); color: var(--r-color-warning-700); border-color: var(--r-color-warning-100); }
.badge-danger  { background: var(--r-color-danger-50); color: var(--r-color-danger-700); border-color: var(--r-color-danger-100); }
.badge-info    { background: var(--r-color-info-50); color: var(--r-color-info-700); border-color: var(--r-color-info-100); }
.badge-brand   { background: var(--r-color-brand-50); color: var(--r-color-brand-800); border-color: var(--r-color-brand-100); }
.badge-neutral { background: var(--r-color-surface-100); color: var(--r-color-ink-700); border-color: var(--r-color-border-200); }

/* Alerts */
.alert {
  border-radius: var(--r-radius-md);
  padding: 12px 14px;
  font-size: 13px;
  line-height: 20px;
  font-weight: 500;
  border: 1px solid transparent;
}
.alert-success { background: var(--r-color-success-50); color: var(--r-color-success-700); border-color: var(--r-color-success-100); }
.alert-info    { background: var(--r-color-info-50); color: var(--r-color-info-700); border-color: var(--r-color-info-100); }
.alert-warning { background: var(--r-color-warning-50); color: var(--r-color-warning-700); border-color: var(--r-color-warning-100); }
.alert-danger,
.alert-error   { background: var(--r-color-danger-50); color: var(--r-color-danger-700); border-color: var(--r-color-danger-100); }

/* Tabs */
.tabs {
  display: inline-flex;
  align-items: center;
  border-radius: var(--r-radius-md);
  background: var(--r-bg-subpanel);
  border: 1px solid var(--r-color-border-200);
  padding: 4px;
  gap: 4px;
}

.tab-item {
  border-radius: 10px;
  min-height: 36px;
  padding: 0 12px;
  font-size: 13px;
  line-height: 18px;
  font-weight: 500;
  color: var(--r-color-ink-600);
  background: transparent;
  border: 1px solid transparent;
}

.tab-item:hover { background: rgba(111, 124, 239, 0.12); color: var(--r-color-ink-800); }
.tab-item.is-active,
.tab-item[aria-selected='true'] {
  background: var(--r-color-brand-50);
  color: var(--r-color-brand-800);
  border-color: var(--r-color-brand-100);
}

/* Sidebar visual language */
.sidebar {
  background: var(--app-sidebar);
  border-right: 1px solid var(--r-color-border-200);
}

.nav-item {
  color: var(--r-color-ink-700);
  border-radius: var(--r-radius-md);
}

.nav-item:hover {
  background: var(--nav-hover) !important;
  color: var(--nav-hover-text) !important;
}

.active-nav {
  background: var(--r-color-brand-50) !important;
  color: var(--r-color-brand-800) !important;
  border: 1px solid var(--r-color-brand-100);
}

.active-nav svg,
.active-nav .sidebar-label {
  color: var(--r-color-brand-800) !important;
}

/* Header and shell buttons */
.app-header {
  background: var(--app-header);
  border-bottom: 1px solid var(--r-color-border-200);
}

.app-shell-button {
  border: 1px solid var(--r-color-border-200);
  background: var(--r-bg-card);
  color: var(--r-color-ink-700);
  border-radius: var(--r-radius-md);
}

.app-shell-button:hover {
  background: var(--r-bg-subpanel);
}

/* Generic utility mapping for existing Tailwind-heavy templates */
.bg-white { background-color: var(--r-bg-card) !important; }
.bg-gray-50, .bg-slate-50, .bg-gray-100 { background-color: var(--r-bg-subpanel) !important; }
.text-gray-900, .text-slate-900 { color: var(--r-text-primary) !important; }
.text-gray-700, .text-slate-700, .text-gray-600, .text-slate-600 { color: var(--r-text-secondary) !important; }
.text-gray-500, .text-slate-500 { color: var(--r-text-muted) !important; }
.border-gray-200, .border-slate-200, .border-gray-300 { border-color: var(--r-color-border-200) !important; }

/* Financial hierarchy */
.amount-lg,
.kpi-amount,
.metric-amount {
  font-size: 28px;
  line-height: 36px;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums lining-nums;
}

.amount-md {
  font-size: 22px;
  line-height: 30px;
  font-weight: 600;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums lining-nums;
}

/* Modal/Drawer base */
.modal-panel,
.drawer-panel {
  border-radius: var(--r-radius-2xl);
  background: var(--r-bg-card);
  border: 1px solid var(--r-color-border-200);
  box-shadow: var(--r-shadow-lg);
}

/* Responsive fine tune */
@media (max-width: 1024px) {
  h1, .h1 { font-size: 24px; line-height: 32px; }
  h2, .h2 { font-size: 20px; line-height: 28px; }
}

/* Tailwind bridge: keep existing templates but with DS tokens */
.bg-indigo-50,
.bg-blue-50,
.bg-purple-50 {
  background-color: var(--r-color-brand-50) !important;
}

.bg-indigo-100,
.bg-blue-100,
.bg-purple-100 {
  background-color: var(--r-color-brand-100) !important;
}

.bg-indigo-600,
.bg-blue-600,
.bg-purple-600 {
  background-color: var(--r-color-brand-700) !important;
}

.bg-indigo-700,
.bg-blue-700,
.bg-purple-700 {
  background-color: var(--r-color-brand-800) !important;
}

.hover\:bg-indigo-700:hover,
.hover\:bg-blue-700:hover,
.hover\:bg-purple-700:hover {
  background-color: var(--r-color-brand-900) !important;
}

.text-indigo-600,
.text-blue-600,
.text-purple-600 {
  color: var(--r-color-brand-700) !important;
}

.text-indigo-700,
.text-blue-700,
.text-purple-700 {
  color: var(--r-color-brand-800) !important;
}

.text-indigo-800,
.text-blue-800,
.text-purple-800,
.text-indigo-900,
.text-blue-900,
.text-purple-900 {
  color: var(--r-color-brand-900) !important;
}

.border-indigo-100,
.border-blue-100,
.border-purple-100 {
  border-color: var(--r-color-brand-100) !important;
}

.border-indigo-200,
.border-blue-200,
.border-purple-200 {
  border-color: var(--r-color-brand-100) !important;
}

.border-indigo-300,
.border-blue-300,
.border-purple-300 {
  border-color: var(--r-color-brand-500) !important;
}

.bg-emerald-50,
.bg-green-50 {
  background-color: var(--r-color-success-50) !important;
}

.bg-emerald-100,
.bg-green-100 {
  background-color: var(--r-color-success-100) !important;
}

.bg-emerald-600,
.bg-green-600 {
  background-color: var(--r-color-success-700) !important;
}

.bg-emerald-700,
.bg-green-700 {
  background-color: var(--r-color-success-700) !important;
  filter: brightness(0.95);
}

.hover\:bg-emerald-700:hover,
.hover\:bg-green-700:hover {
  background-color: var(--r-color-success-700) !important;
  filter: brightness(0.9);
}

.text-emerald-600,
.text-green-600,
.text-emerald-700,
.text-green-700,
.text-emerald-800,
.text-green-800,
.text-emerald-900,
.text-green-900 {
  color: var(--r-color-success-700) !important;
}

.border-emerald-100,
.border-green-100 {
  border-color: var(--r-color-success-100) !important;
}

.border-emerald-200,
.border-green-200 {
  border-color: var(--r-color-success-100) !important;
}

.border-emerald-300,
.border-green-300 {
  border-color: #86efac !important;
}

.bg-amber-50,
.bg-yellow-50 {
  background-color: var(--r-color-warning-50) !important;
}

.bg-amber-100,
.bg-yellow-100 {
  background-color: var(--r-color-warning-100) !important;
}

.text-amber-600,
.text-yellow-600,
.text-amber-700,
.text-yellow-700,
.text-amber-800,
.text-yellow-800 {
  color: var(--r-color-warning-700) !important;
}

.border-amber-200,
.border-yellow-200 {
  border-color: var(--r-color-warning-100) !important;
}

.bg-red-50,
.bg-rose-50 {
  background-color: var(--r-color-danger-50) !important;
}

.bg-red-100,
.bg-rose-100 {
  background-color: var(--r-color-danger-100) !important;
}

.bg-red-600,
.bg-rose-600 {
  background-color: var(--r-color-danger-700) !important;
}

.bg-red-700,
.bg-rose-700 {
  background-color: var(--r-color-danger-700) !important;
  filter: brightness(0.9);
}

.text-red-600,
.text-rose-600,
.text-red-700,
.text-rose-700,
.text-red-800,
.text-rose-800 {
  color: var(--r-color-danger-700) !important;
}

.border-red-200,
.border-rose-200 {
  border-color: var(--r-color-danger-100) !important;
}

.focus\:ring-indigo-500:focus,
.focus\:ring-blue-500:focus,
.focus\:ring-purple-500:focus {
  --tw-ring-color: rgba(111, 124, 239, 0.38) !important;
  box-shadow: var(--r-focus-ring) !important;
}

.focus\:border-transparent:focus {
  border-color: var(--r-color-brand-600) !important;
}

.hover\:text-indigo-600:hover,
.hover\:text-blue-600:hover,
.hover\:text-purple-600:hover,
.hover\:text-indigo-700:hover,
.hover\:text-blue-700:hover,
.hover\:text-purple-700:hover {
  color: var(--r-color-brand-800) !important;
}

html[data-theme='dark'] .text-indigo-500,
html[data-theme='dark'] .text-blue-500,
html[data-theme='dark'] .text-purple-500,
html[data-theme='dark'] .text-indigo-600,
html[data-theme='dark'] .text-blue-600,
html[data-theme='dark'] .text-purple-600,
html[data-theme='dark'] .text-indigo-700,
html[data-theme='dark'] .text-blue-700,
html[data-theme='dark'] .text-purple-700 {
  color: #b8c6ff !important;
}

html[data-theme='dark'] .text-indigo-800,
html[data-theme='dark'] .text-blue-800,
html[data-theme='dark'] .text-purple-800,
html[data-theme='dark'] .text-indigo-900,
html[data-theme='dark'] .text-blue-900,
html[data-theme='dark'] .text-purple-900 {
  color: #d6dfff !important;
}

html[data-theme='dark'] .hover\:text-indigo-600:hover,
html[data-theme='dark'] .hover\:text-blue-600:hover,
html[data-theme='dark'] .hover\:text-purple-600:hover,
html[data-theme='dark'] .hover\:text-indigo-700:hover,
html[data-theme='dark'] .hover\:text-blue-700:hover,
html[data-theme='dark'] .hover\:text-purple-700:hover,
html[data-theme='dark'] .hover\:text-indigo-800:hover,
html[data-theme='dark'] .hover\:text-blue-800:hover,
html[data-theme='dark'] .hover\:text-purple-800:hover,
html[data-theme='dark'] .hover\:text-indigo-900:hover,
html[data-theme='dark'] .hover\:text-blue-900:hover,
html[data-theme='dark'] .hover\:text-purple-900:hover {
  color: #d6dfff !important;
}

.bg-gradient-to-r.from-indigo-600.to-blue-600,
.bg-gradient-to-r.from-indigo-600.to-purple-600,
.bg-gradient-to-br.from-indigo-600.to-blue-600 {
  background-image: linear-gradient(120deg, var(--r-color-brand-800), var(--r-color-brand-600)) !important;
}

.bg-gradient-to-br.from-emerald-600.to-teal-700,
.bg-gradient-to-r.from-emerald-600.to-teal-700 {
  background-image: linear-gradient(120deg, var(--r-color-success-700), var(--r-color-mint-700)) !important;
}

html[data-theme='dark'] .bg-indigo-50,
html[data-theme='dark'] .bg-blue-50,
html[data-theme='dark'] .bg-purple-50 {
  background-color: rgba(111, 124, 239, 0.15) !important;
}

html[data-theme='dark'] .bg-indigo-100,
html[data-theme='dark'] .bg-blue-100,
html[data-theme='dark'] .bg-purple-100 {
  background-color: rgba(111, 124, 239, 0.24) !important;
}

html[data-theme='dark'] .bg-emerald-50,
html[data-theme='dark'] .bg-green-50 {
  background-color: rgba(7, 169, 126, 0.16) !important;
}

html[data-theme='dark'] .bg-amber-50,
html[data-theme='dark'] .bg-yellow-50 {
  background-color: rgba(180, 83, 9, 0.18) !important;
}

html[data-theme='dark'] .bg-red-50,
html[data-theme='dark'] .bg-rose-50 {
  background-color: rgba(185, 28, 28, 0.16) !important;
}
