/* ==========================================================================
   DataViz — Interactive Data Visualization Dashboard
   Design System & Styles
   ========================================================================== */

/* ---------- Custom Properties ---------- */
:root {
  --bg-body: #f1f5f9;
  --bg-card: #ffffff;
  --bg-input: #f8fafc;
  --bg-hover: #e0e7ff;
  --bg-overlay: rgba(255, 255, 255, 0.92);

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --text-inverse: #ffffff;

  --border: #e2e8f0;
  --border-hover: #cbd5e1;

  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-light: #e0e7ff;
  --success: #22c55e;
  --success-light: #dcfce7;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --info: #3b82f6;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.06);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.08);
  --shadow-card: 0 1px 3px rgba(0,0,0,0.04), 0 4px 12px rgba(0,0,0,0.04);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 20px;

  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', monospace;
  --transition: 150ms ease;

  /* Chart palette */
  --c1: #6366f1; --c2: #8b5cf6; --c3: #ec4899; --c4: #f43f5e;
  --c5: #f97316; --c6: #eab308; --c7: #22c55e; --c8: #14b8a6;
  --c9: #06b6d4; --c10: #3b82f6;
}

[data-theme="dark"] {
  --bg-body: #0f172a;
  --bg-card: #1e293b;
  --bg-input: #1e293b;
  --bg-hover: #312e81;
  --bg-overlay: rgba(15, 23, 42, 0.92);

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  --border: #334155;
  --border-hover: #475569;

  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-light: #1e1b4b;
  --success-light: #14532d;
  --danger-light: #7f1d1d;
  --warning-light: #78350f;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.35);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.4);
  --shadow-card: 0 1px 3px rgba(0,0,0,0.2), 0 4px 12px rgba(0,0,0,0.15);
}

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }

body {
  font-family: var(--font);
  background: var(--bg-body);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100dvh;
  transition: background var(--transition), color var(--transition);
  -webkit-font-smoothing: antialiased;
}

/* ---------- App Layout ---------- */
.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- Header ---------- */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.logo-icon { font-size: 1.6rem; }

.badge {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--accent-light);
  color: var(--accent);
}

.icon-btn {
  width: 40px; height: 40px;
  border: none;
  background: var(--bg-card);
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
  transition: all var(--transition);
  box-shadow: var(--shadow-sm);
}
.icon-btn:hover { background: var(--accent-light); color: var(--accent); }

.icon { width: 20px; height: 20px; }
[data-theme="light"] .moon-icon { display: none; }
[data-theme="dark"] .sun-icon { display: none; }

/* ---------- KPI Strip ---------- */
.kpi-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.kpi-card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  box-shadow: var(--shadow-card);
  transition: transform var(--transition), box-shadow var(--transition);
}
.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.kpi-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.kpi-value {
  font-size: 1.35rem;
  font-weight: 800;
  font-family: var(--font-mono);
  letter-spacing: -0.02em;
}

.kpi-delta {
  font-size: 0.72rem;
  font-weight: 600;
  border-radius: 999px;
  padding: 1px 6px;
  width: fit-content;
}
.kpi-delta.positive { background: var(--success-light); color: var(--success); }
.kpi-delta.negative { background: var(--danger-light); color: var(--danger); }
.kpi-delta.neutral { background: var(--bg-input); color: var(--text-muted); }

/* ---------- Controls ---------- */
.controls-bar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: flex-end;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.control-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}
.select:hover { border-color: var(--accent); }
.select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.chart-type-selector {
  display: flex;
  gap: 2px;
  background: var(--bg-card);
  padding: 3px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.chart-type-btn {
  padding: 6px 10px;
  border: none;
  background: transparent;
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all var(--transition);
  display: flex; align-items: center; justify-content: center;
}
.chart-type-btn:hover { color: var(--accent); background: var(--bg-hover); }
.chart-type-btn.active { background: var(--accent); color: var(--text-inverse); }

/* ---------- Actions Row ---------- */
.actions-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: var(--text-inverse);
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover { background: var(--accent-hover); transform: translateY(-1px); box-shadow: var(--shadow-md); }

.btn-secondary {
  background: var(--bg-card);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

.btn-sm {
  padding: 4px 10px;
  font-size: 0.72rem;
  background: var(--bg-input);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.btn-sm:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- Chart Cards ---------- */
.chart-section { width: 100%; }

.chart-card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  overflow: hidden;
  transition: box-shadow var(--transition);
}
.chart-card:hover { box-shadow: var(--shadow-md); }

.chart-header {
  padding: 16px 20px 0;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.chart-title {
  font-size: 1rem;
  font-weight: 700;
}

.chart-subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.chart-body {
  padding: 12px 16px 16px;
  position: relative;
  height: 380px;
}

.chart-body-sm {
  height: 260px;
}

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* ---------- Detail Panel ---------- */
.detail-panel {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  overflow: hidden;
  transition: all 0.3s ease;
  max-height: 0;
  opacity: 0;
}
.detail-panel.open {
  max-height: 300px;
  opacity: 1;
  padding-bottom: 16px;
}

.detail-header {
  padding: 14px 20px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail-header h3 {
  font-size: 0.95rem;
  font-weight: 700;
}

.icon-btn-sm {
  width: 28px; height: 28px;
  border: none;
  background: var(--bg-input);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  transition: all var(--transition);
}
.icon-btn-sm:hover { background: var(--danger-light); color: var(--danger); }

.detail-body {
  padding: 10px 20px 0;
}

.detail-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.detail-stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.detail-stat-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.detail-stat-value {
  font-size: 1.15rem;
  font-weight: 700;
  font-family: var(--font-mono);
}

.detail-bar-container {
  margin-top: 8px;
  height: 8px;
  background: var(--bg-input);
  border-radius: 999px;
  overflow: hidden;
}

.detail-bar {
  height: 100%;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.5s ease;
}

/* ---------- Data Table ---------- */
.table-card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

.table-wrapper {
  overflow-x: auto;
  transition: max-height 0.3s ease;
}
.table-wrapper.collapsed {
  max-height: 0;
  overflow: hidden;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

.data-table th {
  text-align: left;
  padding: 10px 16px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-card);
}

.data-table td {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.data-table tbody tr {
  transition: background var(--transition);
}
.data-table tbody tr:hover {
  background: var(--bg-hover);
}

.table-bar {
  height: 6px;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.5s ease;
  min-width: 2px;
}

.table-bar-bg {
  width: 100%;
  max-width: 120px;
  height: 6px;
  background: var(--bg-input);
  border-radius: 999px;
  overflow: hidden;
}

/* ---------- Footer ---------- */
.footer {
  text-align: center;
  padding: 12px 0 4px;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.footer a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}
.footer a:hover { text-decoration: underline; }

/* ---------- Animations ---------- */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes countUp {
  from { opacity: 0; }
  to { opacity: 1; }
}

.kpi-card { animation: fadeIn 0.4s ease both; }
.kpi-card:nth-child(2) { animation-delay: 0.05s; }
.kpi-card:nth-child(3) { animation-delay: 0.1s; }
.kpi-card:nth-child(4) { animation-delay: 0.15s; }

/* ---------- Responsive ---------- */
@media (max-width: 768px) {
  .kpi-strip {
    grid-template-columns: repeat(2, 1fr);
  }
  .charts-grid {
    grid-template-columns: 1fr;
  }
  .controls-bar {
    flex-direction: column;
    gap: 10px;
  }
  .control-group {
    width: 100%;
  }
  .select { width: 100%; }
  .chart-type-selector { width: 100%; justify-content: center; }
}

@media (max-width: 480px) {
  .app { padding: 10px 12px 24px; gap: 12px; }
  .chart-body { height: 280px; }
  .chart-body-sm { height: 200px; }
  .actions-row { flex-direction: column; }
  .btn { width: 100%; justify-content: center; }
}

/* ---------- Accessibility ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

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