/* ===== Design Tokens ===== */
:root {
  /* Colors — Slate + Indigo palette */
  --bg: #f8fafc;
  --card: #ffffff;
  --primary: #6366f1;
  --primary-light: #818cf8;
  --emerald: #10b981;
  --rose: #f43f5e;
  --amber: #f59e0b;
  --slate-900: #0f172a;
  --slate-800: #1e293b;
  --slate-700: #334155;
  --slate-500: #64748b;
  --slate-400: #94a3b8;
  --slate-200: #e2e8f0;
  --slate-100: #f1f5f9;
  --slate-50: #f8fafc;

  /* Typography */
  --font: 'Inter', -apple-system, 'Segoe UI', system-ui, sans-serif;

  /* Spacing (8px grid) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  /* Elevation */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-full: 9999px;

  /* Transitions */
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
}

/* ===== Reset ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* ===== Base ===== */
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--slate-800);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== Loading Overlay ===== */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: var(--space-4);
  transition: opacity 0.3s var(--ease);
}

.loading-overlay.fade-out {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--slate-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-text {
  font-size: 14px;
  color: var(--slate-500);
  font-weight: 500;
}

.error-message {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

.error-message h3 {
  font-size: 18px;
  color: var(--slate-800);
}

.error-message p {
  font-size: 14px;
  color: var(--slate-500);
  max-width: 360px;
}

.retry-btn {
  margin-top: var(--space-2);
  padding: 10px 24px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s var(--ease);
}

.retry-btn:hover {
  background: var(--primary-light);
}

/* ===== Layout ===== */
.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: var(--space-8) var(--space-6);
}

/* ===== Header ===== */
.header {
  background: linear-gradient(135deg, var(--slate-900) 0%, var(--slate-800) 100%);
  border-radius: var(--radius);
  padding: var(--space-8) var(--space-8);
  margin-bottom: var(--space-6);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
}

.header-title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.header-subtitle {
  font-size: 13px;
  opacity: 0.6;
  margin-top: var(--space-1);
  font-weight: 400;
}

.header-tag {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 8px 20px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.2px;
  white-space: nowrap;
}

/* ===== Filter Bar ===== */
.filter-bar {
  background: var(--card);
  border-radius: var(--radius);
  padding: var(--space-4) var(--space-6);
  margin-bottom: var(--space-5);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--slate-200);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.filter-bar::-webkit-scrollbar {
  display: none;
}

.filter-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--slate-500);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  white-space: nowrap;
  flex-shrink: 0;
}

.year-btn {
  padding: 8px 20px;
  border-radius: var(--radius-full);
  border: 1.5px solid var(--slate-200);
  background: var(--card);
  font-size: 13px;
  font-weight: 500;
  color: var(--slate-500);
  cursor: pointer;
  transition: all 0.2s var(--ease);
  white-space: nowrap;
  flex-shrink: 0;
  min-height: 38px;
}

.year-btn:hover {
  border-color: var(--primary-light);
  color: var(--primary);
}

.year-btn.active {
  background: var(--slate-800);
  border-color: var(--slate-800);
  color: #fff;
}

.filter-divider {
  width: 1px;
  height: 24px;
  background: var(--slate-200);
  flex-shrink: 0;
}

.preset-btn {
  padding: 7px 16px;
  border-radius: var(--radius-full);
  border: 1px solid var(--slate-200);
  background: transparent;
  font-size: 11px;
  font-weight: 600;
  color: var(--slate-400);
  cursor: pointer;
  transition: all 0.2s var(--ease);
  white-space: nowrap;
  flex-shrink: 0;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  min-height: 34px;
}

.preset-btn:hover {
  border-color: var(--primary-light);
  color: var(--primary);
  background: rgba(99, 102, 241, 0.04);
}

/* ===== KPI Cards ===== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-4);
  margin-bottom: var(--space-6);
}

.kpi-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: var(--space-6);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--slate-200);
  transition: all 0.25s var(--ease);
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--primary);
  border-radius: var(--radius) var(--radius) 0 0;
}

.kpi-card:nth-child(2)::before { background: var(--emerald); }
.kpi-card:nth-child(3)::before { background: var(--amber); }
.kpi-card:nth-child(4)::before { background: var(--slate-700); }

.kpi-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.kpi-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--slate-400);
  font-weight: 600;
}

.kpi-value {
  font-size: 28px;
  font-weight: 700;
  margin: 8px 0 4px;
  color: var(--slate-900);
  letter-spacing: -0.5px;
}

.kpi-sub {
  font-size: 12px;
  color: var(--slate-400);
}

.kpi-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: var(--radius-full);
  margin-top: 8px;
}

.badge-up {
  background: rgba(16, 185, 129, 0.1);
  color: #059669;
}

.badge-down {
  background: rgba(244, 63, 94, 0.1);
  color: #e11d48;
}

/* ===== Section ===== */
.section {
  margin-bottom: var(--space-8);
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--slate-800);
  margin-bottom: var(--space-4);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--slate-200);
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.section-number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--slate-100);
  color: var(--slate-500);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 700;
}

/* ===== Grid Layouts ===== */
.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-4);
  margin-bottom: var(--space-4);
}

.grid-4 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-4);
  margin-bottom: var(--space-4);
}

.full-width {
  margin-bottom: var(--space-4);
}

/* ===== Card ===== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: var(--space-6);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--slate-200);
}

.card-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--slate-800);
  margin-bottom: 2px;
}

.card-subtitle {
  font-size: 11px;
  color: var(--slate-400);
  margin-bottom: var(--space-4);
}

/* ===== Chart Container ===== */
.chart-container {
  position: relative;
  min-height: 260px;
  height: 280px;
}

.chart-container.chart-tall {
  height: 320px;
}

.chart-container.chart-short {
  height: 240px;
}

.chart-container canvas {
  width: 100% !important;
}

.chart-container.chart-error::after {
  content: attr(data-error);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--slate-50);
  color: var(--slate-400);
  font-size: 13px;
  border-radius: var(--radius-sm);
}

/* ===== Table ===== */
.table-wrapper {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--slate-200);
  overflow: hidden;
  margin-bottom: var(--space-4);
}

.table-wrapper .table-header {
  padding: var(--space-5) var(--space-6) 0;
}

.table-wrapper h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--slate-800);
}

.table-wrapper .table-sub {
  font-size: 11px;
  color: var(--slate-400);
  padding: var(--space-1) 0 var(--space-3);
}

.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  min-width: 580px;
}

th {
  background: var(--slate-800);
  color: #fff;
  text-align: left;
  padding: 12px var(--space-4);
  font-weight: 500;
  font-size: 12px;
  letter-spacing: 0.3px;
}

td {
  padding: 12px var(--space-4);
  border-bottom: 1px solid var(--slate-100);
  color: var(--slate-700);
}

tr:last-child td {
  border-bottom: none;
}

tr:hover td {
  background: var(--slate-50);
}

.text-right {
  text-align: right;
}

.pos { color: var(--emerald); font-weight: 600; }
.neg { color: var(--rose); font-weight: 600; }

.row-highlight td {
  background: rgba(99, 102, 241, 0.04);
  font-weight: 600;
}

/* ===== Ratio Cards ===== */
.ratio-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--slate-200);
  text-align: center;
}

.ratio-label {
  font-size: 11px;
  color: var(--slate-400);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
}

.ratio-value {
  font-size: 30px;
  font-weight: 700;
  margin: 8px 0;
  letter-spacing: -0.5px;
}

.ratio-sub {
  font-size: 11px;
  color: var(--slate-400);
}

.ratio-bar {
  height: 4px;
  background: var(--slate-100);
  border-radius: 2px;
  margin-top: 12px;
  overflow: hidden;
}

.ratio-bar-fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.6s var(--ease);
}

/* ===== Callout ===== */
.callout {
  background: linear-gradient(135deg, rgba(16, 185, 129, 0.06), rgba(99, 102, 241, 0.04));
  border-left: 3px solid var(--emerald);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: var(--space-5) var(--space-6);
  margin-bottom: var(--space-4);
}

.callout h4 {
  font-size: 14px;
  font-weight: 600;
  color: var(--slate-800);
  margin-bottom: var(--space-1);
}

.callout p {
  font-size: 13px;
  color: var(--slate-700);
  line-height: 1.7;
}

/* ===== Footer ===== */
.footer {
  text-align: center;
  color: var(--slate-400);
  font-size: 11px;
  padding: var(--space-6) 0 var(--space-3);
  border-top: 1px solid var(--slate-200);
  margin-top: var(--space-2);
}

/* ===== Responsive ===== */
@media (max-width: 1024px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .container {
    padding: var(--space-4) var(--space-4);
  }

  .header {
    flex-direction: column;
    text-align: center;
    gap: var(--space-4);
    padding: var(--space-6);
  }

  .header-title { font-size: 18px; }

  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: var(--space-3); }
  .kpi-value { font-size: 24px; }
  .kpi-card { padding: var(--space-5); }

  .grid-2 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: 1fr 1fr; }

  .card { padding: var(--space-5); }
  .chart-container { min-height: 220px; height: 250px; }
  .chart-container.chart-tall { height: 280px; }

  .section-title { font-size: 15px; }
}

@media (max-width: 480px) {
  .container { padding: var(--space-3); }

  .kpi-grid { grid-template-columns: 1fr; gap: var(--space-3); }
  .grid-4 { grid-template-columns: 1fr; }

  .kpi-card {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-4) var(--space-5);
  }

  .kpi-label { width: 100%; }
  .kpi-value { font-size: 22px; margin: 4px 0 0; }
  .kpi-sub { margin-left: auto; }
  .kpi-badge { margin-top: 0; }

  .header-tag { font-size: 11px; padding: 6px 14px; }
}
