/**
 * Company Analytics Mobile Optimization
 * Mobile-first responsive design för analytics & statistik
 * Fokus: Responsiva charts, dropdown selectors, kompakta layouts
 */

/* ========== MOBILE LAYOUT ========== */

@media (max-width: 768px) {
  /* Container */
  .max-w-7xl {
    padding: 1rem !important;
    padding-top: 80px !important;
    padding-bottom: calc(80px + env(safe-area-inset-bottom)) !important;
  }

  .analytics-hero h1 {
    font-size: 1.4rem !important;
  }

  .analytics-hero p {
    font-size: 0.875rem !important;
  }

  /* TIME SELECTOR — dropdown finns i HTML; bas-CSS döljer knappar */

  /* ========== STATS OVERVIEW - 2x2 GRID ========== */
  .stats-overview {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.75rem !important;
    margin-bottom: 1.5rem !important;
  }

  .stat-card {
    padding: 1rem !important;
  }

  .stat-header {
    flex-direction: row !important;
    justify-content: space-between !important;
    align-items: center !important;
    margin-bottom: 0.75rem !important;
  }

  .stat-icon {
    width: 40px !important;
    height: 40px !important;
    font-size: 1.25rem !important;
  }

  .stat-trend {
    font-size: 0.75rem !important;
    padding: 0.25rem 0.5rem !important;
  }

  .stat-number {
    font-size: 1.5rem !important;
    margin-bottom: 0.25rem !important;
  }

  .stat-label {
    font-size: 0.75rem !important;
  }

  .stat-change {
    font-size: 0.688rem !important;
    margin-top: 0.25rem !important;
  }

  /* ========== CHARTS SECTION - STACK VERTICALLY ========== */
  .charts-section {
    display: block !important;
  }

  .chart-card {
    margin-bottom: 1.5rem !important;
    padding: 1rem !important;
  }

  .chart-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0.75rem !important;
    margin-bottom: 1rem !important;
  }

  .chart-title {
    font-size: 1.125rem !important;
  }

  .chart-title i {
    font-size: 1rem !important;
  }

  .chart-actions {
    display: flex !important;
    gap: 0.5rem !important;
  }

  .chart-btn {
    width: 36px !important;
    height: 36px !important;
    font-size: 0.938rem !important;
  }

  /* Chart container - Responsive height */
  .chart-container {
    position: relative !important;
    width: 100% !important;
    height: 250px !important;
    margin-bottom: 1rem !important;
  }

  .chart-container canvas {
    max-height: 250px !important;
    width: 100% !important;
    height: auto !important;
  }

  /* Revenue chart */
  #revenueChart {
    max-height: 250px !important;
  }

  /* Categories chart - Smaller for mobile */
  #categoriesChart {
    max-height: 200px !important;
  }

  /* ========== PERFORMANCE METRICS - STACK VERTICALLY ========== */
  .performance-section {
    display: block !important;
  }

  .performance-card {
    margin-bottom: 1rem !important;
    padding: 1rem !important;
  }

  .performance-header {
    flex-direction: row !important;
    justify-content: space-between !important;
    align-items: center !important;
    margin-bottom: 0.75rem !important;
  }

  .performance-title {
    font-size: 1rem !important;
  }

  .performance-title i {
    font-size: 1rem !important;
    margin-right: 0.5rem !important;
  }

  .performance-value {
    font-size: 1.5rem !important;
  }

  .performance-bar {
    height: 8px !important;
    margin: 0.75rem 0 !important;
  }

  .performance-fill {
    height: 8px !important;
  }

  .performance-details {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0.25rem !important;
  }

  .performance-details span {
    font-size: 0.813rem !important;
  }

  /* ========== INSIGHTS SECTION ========== */
  .insights-section {
    margin-bottom: 1.5rem !important;
    padding: 1rem !important;
  }

  .insights-header {
    margin-bottom: 1.5rem !important;
  }

  .insights-title {
    font-size: 1.25rem !important;
  }

  .insights-title i {
    font-size: 1.125rem !important;
  }

  .insights-grid {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  .insight-card {
    padding: 1rem !important;
  }

  .insight-header {
    margin-bottom: 0.75rem !important;
  }

  .insight-icon {
    width: 40px !important;
    height: 40px !important;
    font-size: 1.25rem !important;
  }

  .insight-title {
    font-size: 1rem !important;
  }

  .insight-description {
    font-size: 0.875rem !important;
    line-height: 1.5 !important;
    margin-bottom: 1rem !important;
  }

  .insight-action {
    width: 100% !important;
    padding: 0.75rem !important;
    font-size: 0.938rem !important;
  }

  /* ========== EXPORT SECTION ========== */
  .export-section {
    padding: 1rem !important;
    margin-bottom: 1.5rem !important;
  }

  .export-title {
    font-size: 1.25rem !important;
    margin-bottom: 0.75rem !important;
  }

  .export-title i {
    font-size: 1.125rem !important;
  }

  .export-description {
    font-size: 0.875rem !important;
    margin-bottom: 1.5rem !important;
  }

  .export-buttons {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 0.75rem !important;
  }

  .export-btn {
    width: 100% !important;
    padding: 0.875rem 1rem !important;
    font-size: 0.938rem !important;
    justify-content: center !important;
  }

  .export-btn i {
    margin-right: 0.5rem !important;
    font-size: 1.125rem !important;
  }

  .export-btn-primary {
    order: 1 !important;
  }

  .export-btn-secondary {
    order: 2 !important;
  }
}

/* ========== SMALL SCREENS (< 360px) ========== */
@media (max-width: 360px) {
  .max-w-7xl {
    padding: 0.75rem !important;
  }

  .bg-gradient-to-r {
    padding: 1rem 0.75rem !important;
  }

  .bg-gradient-to-r h1 {
    font-size: 1.25rem !important;
  }

  .stats-overview {
    gap: 0.5rem !important;
  }

  .stat-card {
    padding: 0.75rem !important;
  }

  .stat-number {
    font-size: 1.25rem !important;
  }

  .chart-card {
    padding: 0.875rem !important;
  }

  .chart-container {
    height: 200px !important;
  }

  .chart-container canvas {
    max-height: 200px !important;
  }

  .performance-card {
    padding: 0.875rem !important;
  }

  .insight-card {
    padding: 0.875rem !important;
  }
}

/* ========== LANDSCAPE MODE ========== */
@media (max-width: 768px) and (orientation: landscape) {
  .max-w-7xl {
    padding-top: 70px !important;
  }

  .bg-gradient-to-r {
    padding: 1rem !important;
  }

  .bg-gradient-to-r h1 {
    font-size: 1.25rem !important;
  }

  .stats-overview {
    grid-template-columns: repeat(4, 1fr) !important;
  }

  .chart-container {
    height: 200px !important;
  }

  .chart-container canvas {
    max-height: 200px !important;
  }

  .insights-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  .export-buttons {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}

/* ========== TOUCH OPTIMIZATIONS ========== */
@media (max-width: 768px) {
  /* Större touch targets */
  .time-selector-mobile,
  .chart-btn,
  .insight-action,
  .export-btn,
  button {
    min-height: 44px !important;
  }

  /* Tap highlight */
  * {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.05) !important;
  }

  /* Prevent double-tap zoom */
  button,
  .export-btn,
  .chart-btn {
    touch-action: manipulation !important;
  }

  /* Smooth scrolling */
  html {
    scroll-behavior: smooth !important;
  }
}

/* ========== CHART RESPONSIVENESS ========== */
@media (max-width: 768px) {
  /* Chart.js responsive options (applied via JS) */
  /* These are CSS fallbacks */
  
  canvas {
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  /* Chart legend - Compact */
  .chartjs-legend {
    font-size: 0.75rem !important;
  }

  .chartjs-legend-item {
    margin: 0.25rem !important;
    padding: 0.25rem !important;
  }

  /* Chart tooltips - Larger text */
  .chartjs-tooltip {
    font-size: 0.875rem !important;
    padding: 0.5rem !important;
  }
}

/* ========== ANIMATIONS ========== */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in-up {
  animation: fadeInUp 0.3s ease !important;
}

.stat-card,
.chart-card,
.performance-card,
.insight-card {
  animation: fadeInUp 0.3s ease !important;
}

/* Stagger animation delays */
.stat-card:nth-child(1) { animation-delay: 0s !important; }
.stat-card:nth-child(2) { animation-delay: 0.05s !important; }
.stat-card:nth-child(3) { animation-delay: 0.1s !important; }
.stat-card:nth-child(4) { animation-delay: 0.15s !important; }

/* ========== ACCESSIBILITY ========== */
@media (max-width: 768px) {
  /* Focus states */
  *:focus-visible {
    outline: 3px solid #0ea5e9 !important;
    outline-offset: 2px !important;
  }

  /* Screen reader only text */
  .sr-only {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border-width: 0 !important;
  }

  /* Skip to content */
  .skip-to-analytics {
    position: absolute;
    top: -100px;
    left: 0;
    background: #0f172a;
    color: white;
    padding: 1rem;
    z-index: 9999;
  }

  .skip-to-analytics:focus {
    top: 0;
  }

  /* ARIA live regions for chart updates */
  .chart-status {
    position: absolute;
    left: -10000px;
    width: 1px;
    height: 1px;
    overflow: hidden;
  }
}

/* ========== LOADING STATES ========== */
@media (max-width: 768px) {
  .chart-loading {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    min-height: 250px !important;
    color: #64748b !important;
  }

  .chart-loading i {
    font-size: 2rem !important;
    margin-right: 0.5rem !important;
    animation: spin 1s linear infinite !important;
  }

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

  /* Skeleton loading for stats */
  .stat-skeleton {
    background: linear-gradient(
      90deg,
      #f0f0f0 25%,
      #e0e0e0 50%,
      #f0f0f0 75%
    );
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
    border-radius: 0.5rem;
    height: 20px;
  }

  @keyframes loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
  }
}

/* ========== ERROR STATES ========== */
@media (max-width: 768px) {
  .chart-error {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    min-height: 250px !important;
    padding: 1rem !important;
    text-align: center !important;
  }

  .chart-error i {
    font-size: 3rem !important;
    color: #ef4444 !important;
    margin-bottom: 1rem !important;
  }

  .chart-error-message {
    font-size: 0.938rem !important;
    color: #64748b !important;
    margin-bottom: 1rem !important;
  }

  .chart-error-retry {
    padding: 0.75rem 1.5rem !important;
    font-size: 0.938rem !important;
  }
}

/* ========== PRINT MEDIA ========== */
@media print {
  .time-selector,
  .chart-actions,
  .export-section,
  .company-mobile-bottom-nav,
  .company-standalone-fab {
    display: none !important;
  }

  .max-w-7xl {
    padding: 0 !important;
  }

  .stats-overview {
    grid-template-columns: repeat(4, 1fr) !important;
  }

  .chart-container {
    height: auto !important;
    page-break-inside: avoid !important;
  }

  .chart-card,
  .performance-card,
  .insight-card {
    page-break-inside: avoid !important;
  }
}

/*
 * Ingen prefers-color-scheme: dark här.
 * Analytics är ljus (som övriga företagssidor). Tidigare dark-override
 * satte mörka kortbakgrunder men lämnade mörk text → oläsbart.
 */
