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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #232733;
  --border: #2d3242;
  --text: #e4e7ef;
  --text-muted: #8b90a0;
  --accent: #6366f1;
  --accent-light: #818cf8;
  --green: #22c55e;
  --green-bg: rgba(34, 197, 94, 0.12);
  --red: #ef4444;
  --red-bg: rgba(239, 68, 68, 0.12);
  --orange: #f59e0b;
  --orange-bg: rgba(245, 158, 11, 0.12);
  --blue: #3b82f6;
  --blue-bg: rgba(59, 130, 246, 0.12);
  --purple: #a855f7;
  --purple-bg: rgba(168, 85, 247, 0.12);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

/* ── Layout ───────────────────────────────────────────── */
.app {
  max-width: 1440px;
  margin: 0 auto;
  padding: 24px;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
  flex-wrap: wrap;
  gap: 16px;
}

header h1 {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

header h1 span {
  color: var(--accent-light);
}

/* ── Setup Panel ──────────────────────────────────────── */
.setup-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
}

.setup-panel h2 {
  font-size: 16px;
  margin-bottom: 16px;
  color: var(--text-muted);
  font-weight: 500;
}

.setup-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}

.setup-grid label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.setup-grid select,
.setup-grid input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

.setup-grid select:focus,
.setup-grid input:focus {
  border-color: var(--accent);
}

/* ── Filters Bar ──────────────────────────────────────── */
.filters {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.filter-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.filter-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.filter-custom {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-left: 8px;
}

.filter-custom input[type="date"] {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--text);
  font-size: 13px;
}

.btn-load {
  padding: 8px 20px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-load:hover { background: var(--accent-light); }
.btn-load:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Section Headers ──────────────────────────────────── */
.section {
  margin-bottom: 32px;
}

.section-title {
  font-size: 14px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-title .badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 500;
}

.badge-zephir { background: var(--purple-bg); color: var(--purple); }
.badge-main { background: var(--blue-bg); color: var(--blue); }

/* ── KPI Cards Grid ───────────────────────────────────── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  transition: border-color 0.2s;
}

.kpi-card:hover {
  border-color: var(--accent);
}

.kpi-card .label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.kpi-card .value {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -1px;
  line-height: 1;
}

.kpi-card .subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
}

.kpi-card .diagnostic {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
  font-style: italic;
}

/* ── Color variants ───────────────────────────────────── */
.value-green { color: var(--green); }
.value-red { color: var(--red); }
.value-orange { color: var(--orange); }
.value-blue { color: var(--blue); }
.value-purple { color: var(--purple); }
.value-default { color: var(--text); }

/* ── Funnel visual ────────────────────────────────────── */
.funnel {
  display: flex;
  align-items: stretch;
  gap: 2px;
  margin: 16px 0;
  height: 120px;
}

.funnel-step {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  position: relative;
  transition: all 0.2s;
}

.funnel-step:hover {
  border-color: var(--accent);
  background: var(--surface2);
}

.funnel-step .step-name {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.funnel-step .step-count {
  font-size: 24px;
  font-weight: 700;
}

.funnel-step .step-value {
  font-size: 12px;
  color: var(--accent-light);
  margin-top: 2px;
}

.funnel-arrow {
  display: flex;
  align-items: center;
  color: var(--text-muted);
  font-size: 20px;
  padding: 0 4px;
}

/* ── Tables ───────────────────────────────────────────── */
.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}

.data-table th,
.data-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.data-table th {
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.5px;
  font-weight: 600;
}

.data-table tr:hover td {
  background: var(--surface2);
}

/* ── Alert badges ─────────────────────────────────────── */
.alert-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
}

.alert-danger { background: var(--red-bg); color: var(--red); }
.alert-warning { background: var(--orange-bg); color: var(--orange); }
.alert-ok { background: var(--green-bg); color: var(--green); }

/* ── Coverage meter ───────────────────────────────────── */
.coverage-meter {
  margin-top: 12px;
}

.meter-bar {
  width: 100%;
  height: 12px;
  background: var(--surface2);
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 8px;
}

.meter-fill {
  height: 100%;
  border-radius: 6px;
  transition: width 0.5s ease;
}

.meter-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-muted);
}

/* ── Forecast cards ───────────────────────────────────── */
.forecast-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.forecast-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  text-align: center;
}

.forecast-card .period {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.forecast-card .amount {
  font-size: 28px;
  font-weight: 700;
  color: var(--accent-light);
  margin: 8px 0;
}

/* ── Lost reasons chart ───────────────────────────────── */
.bar-chart {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

.bar-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.bar-label {
  min-width: 140px;
  font-size: 13px;
  color: var(--text-muted);
  text-align: right;
  flex-shrink: 0;
}

.bar-track {
  flex: 1;
  height: 24px;
  background: var(--surface2);
  border-radius: 4px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 4px;
  background: var(--red);
  display: flex;
  align-items: center;
  padding: 0 8px;
  font-size: 12px;
  font-weight: 600;
  color: white;
  transition: width 0.5s ease;
  min-width: 28px;
}

/* ── Info Button & Modal ──────────────────────────────── */
.kpi-card {
  position: relative;
}

.info-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  line-height: 1;
  font-family: 'Inter', sans-serif;
}

.info-btn:hover {
  border-color: var(--accent);
  color: var(--accent-light);
  background: rgba(99, 102, 241, 0.1);
}

.info-btn-inline {
  position: static;
  width: 18px;
  height: 18px;
  font-size: 10px;
  display: inline-flex;
  vertical-align: middle;
  margin-left: 4px;
  flex-shrink: 0;
}

.setup-label-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 23, 0.8);
  backdrop-filter: blur(4px);
  z-index: 1000;
  justify-content: center;
  align-items: center;
  padding: 24px;
}

.modal-overlay.visible {
  display: flex;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  max-width: 520px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  padding: 28px;
  position: relative;
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(12px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.modal-close:hover {
  border-color: var(--red);
  color: var(--red);
}

.modal h3 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 16px;
  padding-right: 36px;
}

.modal-section {
  margin-bottom: 16px;
}

.modal-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 600;
}

.modal-section p {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
}

.modal-interpret {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}

.modal-interpret .level {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.level-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.level-dot.green { background: var(--green); }
.level-dot.orange { background: var(--orange); }
.level-dot.red { background: var(--red); }

/* ── Status Bar ───────────────────────────────────────── */
.status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background 0.3s;
}

.status-dot.active {
  background: var(--green);
  box-shadow: 0 0 6px rgba(34, 197, 94, 0.5);
}

.status-dot.inactive {
  background: var(--text-muted);
}

.status-dot.loading {
  background: var(--orange);
  animation: pulse 1s ease infinite;
}

.status-dot.snapshot {
  background: var(--blue);
  box-shadow: 0 0 6px rgba(59, 130, 246, 0.5);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

.status-text {
  color: var(--text-muted);
  white-space: nowrap;
}

.btn-refresh {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  padding: 0;
}

.btn-refresh:hover {
  border-color: var(--accent);
  color: var(--accent-light);
}

/* ── Notice link ──────────────────────────────────────── */
.notice-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 13px;
  text-decoration: none;
  transition: all 0.2s;
}

.notice-link:hover {
  border-color: var(--accent);
  color: var(--accent-light);
}

/* ── Loading ──────────────────────────────────────────── */
.loading-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 23, 0.85);
  z-index: 999;
  justify-content: center;
  align-items: center;
}

.loading-overlay.visible {
  display: flex;
}

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

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

.loading-text {
  color: var(--text-muted);
  margin-top: 16px;
  font-size: 14px;
  text-align: center;
}

/* ── Empty state ──────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state p {
  font-size: 14px;
  margin-top: 8px;
}

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 768px) {
  .app { padding: 16px; }
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .forecast-grid { grid-template-columns: 1fr; }
  header { flex-direction: column; align-items: flex-start; }
  .funnel { flex-direction: column; height: auto; }
  .funnel-arrow { transform: rotate(90deg); justify-content: center; }
}

@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: 1fr; }
}
