/* ==========================================================================
   ANALYTICS DASHBOARD - Business Intelligence
   Executive dashboard with KPIs, charts, and AI insights
   ========================================================================== */

/* CSS Variables - Analytics Palette */
:root {
  --ana-bg: #f1f5f9;
  --ana-bg-secondary: #e2e8f0;
  --ana-card: #ffffff;
  --ana-card-hover: #fafafa;
  --ana-border: #e2e8f0;
  --ana-text: #1e293b;
  --ana-text-secondary: #475569;
  --ana-muted: #64748b;

  /* Brand Colors */
  --ana-primary: #3b82f6;
  --ana-success: #22c55e;
  --ana-warning: #eab308;
  --ana-danger: #ef4444;

  /* Chart Colors */
  --ana-chart-1: #8b5cf6;
  --ana-chart-2: #06b6d4;
  --ana-chart-3: #f97316;
  --ana-chart-4: #ec4899;
  --ana-chart-5: #14b8a6;

  /* KPI Colors */
  --kpi-revenue: #10b981;
  --kpi-orders: #3b82f6;
  --kpi-ticket: #8b5cf6;
  --kpi-conversion: #f59e0b;

  /* Delivery/Pickup */
  --delivery-color: #3b82f6;
  --pickup-color: #10b981;

  --ana-shadow: rgba(0, 0, 0, 0.08);
}

/* Dark Mode */
@media (prefers-color-scheme: dark) {
  :root {
    --ana-bg: #0f172a;
    --ana-bg-secondary: #1e293b;
    --ana-card: #1e293b;
    --ana-card-hover: #334155;
    --ana-border: #334155;
    --ana-text: #f1f5f9;
    --ana-text-secondary: #cbd5e1;
    --ana-muted: #94a3b8;
  }
}

/* ==========================================================================
   Base Container
   ========================================================================== */

.analytics-container {
  min-height: 100vh;
  background: var(--ana-bg);
  padding: 1.5rem;
  color: var(--ana-text);
}

/* ==========================================================================
   Header
   ========================================================================== */

.analytics-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.page-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.page-title .icon {
  font-size: 1.75rem;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

/* Period Selector */
.period-selector {
  display: flex;
  background: var(--ana-card);
  border: 1px solid var(--ana-border);
  border-radius: 10px;
  padding: 4px;
  gap: 4px;
}

.period-btn {
  padding: 0.5rem 1rem;
  background: transparent;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--ana-muted);
  cursor: pointer;
  transition: all 0.2s ease;
}

.period-btn:hover {
  color: var(--ana-text);
}

.period-btn.active {
  background: var(--ana-primary);
  color: white;
}

/* Button */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 1.25rem;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary {
  background: var(--ana-card);
  color: var(--ana-text);
  border: 1px solid var(--ana-border);
}

.btn-secondary:hover {
  background: var(--ana-bg-secondary);
}

/* ==========================================================================
   KPI Cards
   ========================================================================== */

.kpi-section {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.kpi-card {
  background: var(--ana-card);
  border-radius: 16px;
  padding: 1.25rem;
  border: 1px solid var(--ana-border);
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
}

.kpi-card.revenue::before { background: var(--kpi-revenue); }
.kpi-card.orders::before { background: var(--kpi-orders); }
.kpi-card.ticket::before { background: var(--kpi-ticket); }
.kpi-card.conversion::before { background: var(--kpi-conversion); }

.kpi-icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--ana-bg-secondary);
  border-radius: 12px;
  font-size: 1.5rem;
  flex-shrink: 0;
}

.kpi-card.revenue .kpi-icon { background: rgba(16, 185, 129, 0.15); }
.kpi-card.orders .kpi-icon { background: rgba(59, 130, 246, 0.15); }
.kpi-card.ticket .kpi-icon { background: rgba(139, 92, 246, 0.15); }
.kpi-card.conversion .kpi-icon { background: rgba(245, 158, 11, 0.15); }

.kpi-content {
  flex: 1;
  min-width: 0;
}

.kpi-value {
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.2;
  margin-bottom: 0.25rem;
}

.kpi-label {
  font-size: 0.85rem;
  color: var(--ana-muted);
  margin-bottom: 0.5rem;
}

.kpi-trend {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.8rem;
}

.kpi-trend.positive {
  color: var(--ana-success);
}

.kpi-trend.negative {
  color: var(--ana-danger);
}

.kpi-trend.negative .trend-arrow {
  transform: rotate(180deg);
}

.trend-label {
  color: var(--ana-muted);
}

/* Sparkline */
.kpi-sparkline {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100px;
  height: 40px;
  opacity: 0.3;
}

.kpi-sparkline svg {
  width: 100%;
  height: 100%;
}

.sparkline-path {
  fill: none;
  stroke-width: 2;
}

.kpi-card.revenue .sparkline-path { stroke: var(--kpi-revenue); }
.kpi-card.orders .sparkline-path { stroke: var(--kpi-orders); }
.kpi-card.ticket .sparkline-path { stroke: var(--kpi-ticket); }

/* Mini Chart for Conversion */
.kpi-mini-chart {
  display: flex;
  align-items: center;
  gap: 3px;
  margin-top: 0.5rem;
}

.mini-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ana-bg-secondary);
}

.mini-dot.filled {
  background: var(--kpi-conversion);
}

/* ==========================================================================
   Charts Row
   ========================================================================== */

.charts-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.chart-card {
  background: var(--ana-card);
  border-radius: 16px;
  border: 1px solid var(--ana-border);
  overflow: hidden;
}

.chart-card.large {
  min-height: 320px;
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--ana-border);
}

.chart-title {
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.chart-title .icon {
  font-size: 1.25rem;
}

.chart-badge {
  padding: 0.25rem 0.75rem;
  background: var(--ana-bg-secondary);
  border-radius: 20px;
  font-size: 0.8rem;
  color: var(--ana-muted);
}

.chart-body {
  padding: 1.25rem;
}

/* ==========================================================================
   Top Dishes Chart
   ========================================================================== */

.top-items {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.top-item {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.top-rank {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--ana-bg-secondary);
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--ana-muted);
  flex-shrink: 0;
}

.top-rank.gold { background: #fef3c7; color: #b45309; }
.top-rank.silver { background: #f1f5f9; color: #64748b; }
.top-rank.bronze { background: #fed7aa; color: #c2410c; }

.top-item-info {
  flex: 1;
  min-width: 0;
}

.top-item-name {
  font-weight: 600;
  margin-bottom: 0.25rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.top-item-name .emoji {
  font-size: 1.25rem;
}

.top-item-bar {
  height: 8px;
  background: var(--ana-bg-secondary);
  border-radius: 4px;
  overflow: hidden;
}

.top-item-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.5s ease;
}

.top-item:nth-child(1) .top-item-bar-fill { background: var(--ana-chart-1); }
.top-item:nth-child(2) .top-item-bar-fill { background: var(--ana-chart-2); }
.top-item:nth-child(3) .top-item-bar-fill { background: var(--ana-chart-3); }
.top-item:nth-child(4) .top-item-bar-fill { background: var(--ana-chart-4); }
.top-item:nth-child(5) .top-item-bar-fill { background: var(--ana-chart-5); }

.top-item-stats {
  text-align: right;
  flex-shrink: 0;
}

.top-item-count {
  font-size: 1.1rem;
  font-weight: 700;
}

.top-item-revenue {
  font-size: 0.8rem;
  color: var(--ana-success);
}

/* ==========================================================================
   Hourly Bar Chart
   ========================================================================== */

.bar-chart {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  height: 180px;
  gap: 6px;
  padding-bottom: 0.5rem;
}

.bar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
}

.bar {
  width: 100%;
  max-width: 30px;
  background: var(--ana-primary);
  border-radius: 4px 4px 0 0;
  transition: height 0.5s ease, background 0.3s ease;
  cursor: pointer;
  position: relative;
}

.bar:hover {
  background: var(--ana-chart-1);
}

.bar.peak {
  background: var(--ana-chart-3);
}

.bar-value {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.25rem 0.5rem;
  background: var(--ana-text);
  color: var(--ana-card);
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.2s ease;
  white-space: nowrap;
}

.bar:hover .bar-value {
  opacity: 1;
}

.chart-x-axis {
  display: flex;
  justify-content: space-between;
  padding-top: 0.5rem;
  border-top: 1px solid var(--ana-border);
}

.x-label {
  font-size: 0.75rem;
  color: var(--ana-muted);
  text-align: center;
  flex: 1;
}

/* ==========================================================================
   Donut Chart
   ========================================================================== */

.donut-chart-container {
  display: flex;
  align-items: center;
  gap: 2rem;
}

.donut-chart {
  width: 160px;
  height: 160px;
  position: relative;
}

.donut-chart svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.donut-segment {
  fill: none;
  stroke-width: 20;
  transition: stroke-dasharray 0.8s ease;
}

.donut-segment.delivery {
  stroke: var(--delivery-color);
}

.donut-segment.pickup {
  stroke: var(--pickup-color);
}

.donut-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.donut-value {
  display: block;
  font-size: 1.75rem;
  font-weight: 700;
}

.donut-label {
  font-size: 0.8rem;
  color: var(--ana-muted);
}

.donut-legend {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.legend-color {
  width: 12px;
  height: 12px;
  border-radius: 3px;
}

.legend-color.delivery { background: var(--delivery-color); }
.legend-color.pickup { background: var(--pickup-color); }

.legend-label {
  font-weight: 500;
  flex: 1;
}

.legend-value {
  font-size: 0.9rem;
  color: var(--ana-muted);
}

/* ==========================================================================
   AI Metrics
   ========================================================================== */

.ai-metrics {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1rem;
}

.ai-metric {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 1rem;
  background: var(--ana-bg);
  border-radius: 10px;
}

.metric-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--ana-card);
  border-radius: 8px;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.metric-content {
  flex: 1;
}

.metric-label {
  font-size: 0.8rem;
  color: var(--ana-muted);
  margin-bottom: 0.25rem;
}

.metric-value {
  font-size: 1.25rem;
  font-weight: 700;
}

.metric-value.negative {
  color: var(--ana-danger);
}

.metric-sub {
  font-size: 0.75rem;
  color: var(--ana-muted);
}

/* ==========================================================================
   Insight Section
   ========================================================================== */

.insight-section {
  margin-bottom: 1.5rem;
}

.insight-card {
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
  border-radius: 16px;
  padding: 1.5rem;
  border: 2px solid #fbbf24;
}

@media (prefers-color-scheme: dark) {
  .insight-card {
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.2) 0%, rgba(245, 158, 11, 0.2) 100%);
    border-color: #f59e0b;
  }
}

.insight-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.insight-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(251, 191, 36, 0.3);
  border-radius: 10px;
  font-size: 1.5rem;
}

.insight-title {
  font-size: 1.1rem;
  font-weight: 700;
  margin: 0;
  color: #92400e;
}

@media (prefers-color-scheme: dark) {
  .insight-title {
    color: #fbbf24;
  }
}

.insight-body {
  margin-bottom: 1.25rem;
}

.insight-text {
  font-size: 1rem;
  line-height: 1.6;
  color: #78350f;
  margin: 0;
}

@media (prefers-color-scheme: dark) {
  .insight-text {
    color: var(--ana-text);
  }
}

.insight-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.insight-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 1.25rem;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(251, 191, 36, 0.5);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #78350f;
}

@media (prefers-color-scheme: dark) {
  .insight-btn {
    background: var(--ana-card);
    color: var(--ana-text);
    border-color: var(--ana-border);
  }
}

.insight-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.insight-btn.primary {
  background: #f59e0b;
  color: white;
  border-color: #f59e0b;
}

.insight-btn.primary:hover {
  background: #d97706;
}

/* ==========================================================================
   Stats Row
   ========================================================================== */

.stats-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 1rem;
}

.stat-mini-card {
  background: var(--ana-card);
  border-radius: 12px;
  padding: 1rem;
  border: 1px solid var(--ana-border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.stat-mini-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--ana-bg-secondary);
  border-radius: 10px;
  font-size: 1.25rem;
  flex-shrink: 0;
}

.stat-mini-content {
  flex: 1;
  min-width: 0;
}

.stat-mini-value {
  font-size: 1.25rem;
  font-weight: 700;
}

.stat-mini-label {
  font-size: 0.75rem;
  color: var(--ana-muted);
}

/* ==========================================================================
   Toast
   ========================================================================== */

.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 9999;
}

.toast {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  background: var(--ana-card);
  border-radius: 10px;
  border: 1px solid var(--ana-border);
  box-shadow: 0 4px 12px var(--ana-shadow);
  animation: toastSlideIn 0.3s ease;
}

@keyframes toastSlideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.toast.success { border-color: var(--ana-success); }
.toast.error { border-color: var(--ana-danger); }

.toast-icon { font-size: 1.25rem; }
.toast-message { font-size: 0.9rem; font-weight: 500; }

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 1280px) {
  .kpi-section {
    grid-template-columns: repeat(2, 1fr);
  }

  .stats-row {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 1024px) {
  .charts-row {
    grid-template-columns: 1fr;
  }

  .donut-chart-container {
    flex-direction: column;
  }

  .ai-metrics {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  .analytics-container {
    padding: 1rem;
  }

  .analytics-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .header-right {
    width: 100%;
    flex-direction: column;
  }

  .period-selector {
    width: 100%;
    justify-content: center;
  }

  .kpi-section {
    grid-template-columns: 1fr;
  }

  .stats-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .insight-actions {
    flex-direction: column;
  }

  .insight-btn {
    width: 100%;
    justify-content: center;
  }

  .bar-chart {
    height: 140px;
  }
}

/* ==========================================================================
   Loading & Empty States
   ========================================================================== */

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--ana-bg-secondary);
  border-top-color: var(--ana-primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-text {
  color: var(--ana-muted);
  font-size: 0.9rem;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
}

.empty-state-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  opacity: 0.5;
}

.empty-state-text {
  color: var(--ana-muted);
  font-size: 0.9rem;
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
  .analytics-container {
    background: white;
  }

  .btn,
  .period-selector,
  .insight-actions {
    display: none !important;
  }

  .kpi-section,
  .charts-row,
  .stats-row {
    break-inside: avoid;
  }
}
