/* ============================================
   MOBILE-OPTIMIZED DASHBOARD STYLES
   ============================================
   Dashboard-specific mobile optimizations
   Charts, KPIs, filters, and data visualizations
   ============================================ */

/* ============================================
   DASHBOARD LAYOUT
   ============================================ */

@media (max-width: 640px) {
  .dashboard-header {
    padding: 1rem;
    margin-bottom: 1rem;
  }
  
  .dashboard-header h1 {
    font-size: 24px;
    margin: 0 0 0.5rem 0;
  }
  
  .dashboard-header .hint {
    font-size: 14px;
  }
}

/* ============================================
   KPI CARDS - VERTICAL STACKING
   ============================================ */

@media (max-width: 640px) {
  .kpi-grid,
  .stats-grid,
  .metrics-grid {
    display: flex !important;
    flex-direction: column;
    gap: 0.75rem;
  }
  
  .kpi-card,
  .stat-card,
  .metric-card {
    padding: 1rem;
    min-height: auto;
  }
  
  .kpi-value {
    font-size: 32px;
  }
  
  .kpi-label {
    font-size: 14px;
    margin-top: 0.5rem;
  }
  
  .kpi-change {
    font-size: 13px;
    margin-top: 0.375rem;
  }
}

/* ============================================
   CHART CONTAINERS
   ============================================ */

@media (max-width: 640px) {
  .chart-container {
    position: relative;
    width: 100%;
    margin-bottom: 1.5rem;
  }
  
  .chart-wrapper {
    position: relative;
    height: 280px;
    width: 100%;
    padding: 1rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
  }
  
  .chart-wrapper canvas {
    max-width: 100%;
    height: auto !important;
  }
  
  .chart-title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--color-text);
  }
  
  .chart-subtitle {
    font-size: 13px;
    color: var(--color-muted);
    margin-bottom: 0.75rem;
  }
}

/* ============================================
   COLLAPSIBLE FILTER PANELS
   ============================================ */

@media (max-width: 640px) {
  .dashboard-filters {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    margin-bottom: 1rem;
    overflow: hidden;
  }
  
  .filters-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background: var(--color-surface-alt);
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    min-height: 44px;
  }
  
  .filters-header:active {
    background: var(--color-border);
  }
  
  .filters-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--color-text);
    margin: 0;
  }
  
  .filters-toggle {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease;
  }
  
  .filters-toggle::before {
    content: '▼';
    font-size: 12px;
    color: var(--color-muted);
  }
  
  .dashboard-filters.collapsed .filters-toggle {
    transform: rotate(-90deg);
  }
  
  .filters-body {
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }
  
  .dashboard-filters.collapsed .filters-body {
    display: none;
  }
  
  .filter-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }
  
  .filter-group label {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text);
  }
  
  .filter-group select,
  .filter-group input {
    width: 100%;
    padding: 0.875rem 1rem;
    min-height: 44px;
    font-size: 16px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-field-bg);
  }
  
  .filters-actions {
    display: flex;
    gap: 0.75rem;
    margin-top: 0.5rem;
  }
  
  .filters-actions .btn {
    flex: 1;
    min-height: 44px;
  }
}

/* ============================================
   DATA TABLES - HORIZONTAL SCROLL
   ============================================ */

@media (max-width: 640px) {
  .dashboard-table-wrapper {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    margin-bottom: 1rem;
    position: relative;
  }
  
  .dashboard-table-wrapper::after {
    content: '→ Swipe';
    position: sticky;
    right: 0;
    bottom: 0;
    background: linear-gradient(to left, var(--color-surface) 50%, transparent);
    padding: 0.5rem 1rem;
    font-size: 11px;
    color: var(--color-muted);
    font-weight: 600;
    text-transform: uppercase;
    pointer-events: none;
    opacity: 0.8;
  }
  
  .dashboard-table {
    width: 100%;
    min-width: 600px;
    border-collapse: collapse;
  }
  
  .dashboard-table th {
    position: sticky;
    top: 0;
    background: var(--color-surface-alt);
    padding: 0.75rem;
    text-align: left;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--color-muted);
    border-bottom: 2px solid var(--color-border);
    z-index: 10;
  }
  
  .dashboard-table td {
    padding: 0.875rem 0.75rem;
    font-size: 14px;
    border-bottom: 1px solid var(--color-divider);
  }
  
  .dashboard-table tr:last-child td {
    border-bottom: none;
  }
  
  /* Sticky first column */
  .dashboard-table th:first-child,
  .dashboard-table td:first-child {
    position: sticky;
    left: 0;
    background: var(--color-surface);
    z-index: 11;
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.05);
  }
  
  .dashboard-table th:first-child {
    background: var(--color-surface-alt);
    z-index: 12;
  }
}

/* ============================================
   PULL-TO-REFRESH
   ============================================ */

@media (max-width: 640px) {
  .pull-to-refresh-indicator {
    position: fixed;
    top: 64px; /* Below topbar */
    left: 50%;
    transform: translateX(-50%) translateY(-100%);
    background: var(--color-accent);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    box-shadow: var(--shadow-soft);
    z-index: 9999;
    transition: transform 0.3s ease;
    pointer-events: none;
  }
  
  .pull-to-refresh-indicator.active {
    transform: translateX(-50%) translateY(0.5rem);
  }
  
  .pull-to-refresh-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    margin-right: 0.5rem;
    vertical-align: middle;
  }
}

/* ============================================
   SPARKLINES & MINI CHARTS
   ============================================ */

@media (max-width: 640px) {
  .sparkline-container {
    height: 40px;
    width: 100%;
    position: relative;
  }
  
  .sparkline-container canvas {
    width: 100% !important;
    height: 40px !important;
  }
  
  /* Enlarge sparklines on tap */
  .sparkline-enlarged {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    height: 300px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    z-index: 10000;
    padding: 1rem;
  }
  
  .sparkline-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9999;
  }
}

/* ============================================
   DASHBOARD REFRESH BUTTON
   ============================================ */

@media (max-width: 640px) {
  .dashboard-refresh {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    width: 56px;
    height: 56px;
    background: var(--color-accent);
    color: white;
    border: none;
    border-radius: 50%;
    box-shadow: 0 4px 12px rgba(2, 129, 82, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    cursor: pointer;
    z-index: 100;
    transition: all 0.2s ease;
    -webkit-tap-highlight-color: transparent;
  }
  
  .dashboard-refresh:active {
    transform: scale(0.9);
    box-shadow: 0 2px 8px rgba(2, 129, 82, 0.3);
  }
  
  .dashboard-refresh.refreshing {
    animation: spin 1s linear infinite;
  }
  
  /* Account for safe area */
  @supports (padding: max(0px)) {
    .dashboard-refresh {
      bottom: max(1rem, env(safe-area-inset-bottom));
      right: max(1rem, env(safe-area-inset-right));
    }
  }
}

/* ============================================
   CHART LEGENDS - MOBILE LAYOUT
   ============================================ */

@media (max-width: 640px) {
  .chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--color-divider);
  }
  
  .chart-legend-item {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    font-size: 12px;
    color: var(--color-muted);
    padding: 0.375rem 0.625rem;
    background: var(--color-surface-alt);
    border-radius: var(--radius-xs);
  }
  
  .chart-legend-color {
    width: 12px;
    height: 12px;
    border-radius: 2px;
  }
}

/* ============================================
   TIME RANGE SELECTOR
   ============================================ */

@media (max-width: 640px) {
  .time-range-selector {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.5rem;
  }
  
  .time-range-selector::-webkit-scrollbar {
    height: 4px;
  }
  
  .time-range-selector::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 2px;
  }
  
  .time-range-option {
    flex-shrink: 0;
    padding: 0.625rem 1rem;
    min-height: 40px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text);
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    -webkit-tap-highlight-color: transparent;
  }
  
  .time-range-option:active,
  .time-range-option.active {
    background: var(--color-accent);
    border-color: var(--color-accent);
    color: white;
  }
}

/* ============================================
   DASHBOARD EMPTY STATES
   ============================================ */

@media (max-width: 640px) {
  .dashboard-empty {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--color-muted);
  }
  
  .dashboard-empty-icon {
    font-size: 48px;
    margin-bottom: 1rem;
    opacity: 0.5;
  }
  
  .dashboard-empty h3 {
    font-size: 18px;
    color: var(--color-text);
    margin: 0 0 0.5rem 0;
  }
  
  .dashboard-empty p {
    font-size: 14px;
    margin: 0 0 1.5rem 0;
  }
  
  .dashboard-empty .btn {
    width: auto;
    display: inline-flex;
  }
}

/* ============================================
   DASHBOARD LOADING STATES
   ============================================ */

@media (max-width: 640px) {
  .dashboard-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1rem;
    min-height: 300px;
  }
  
  .dashboard-loading-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--color-border);
    border-top-color: var(--color-accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-bottom: 1rem;
  }
  
  .dashboard-loading-text {
    font-size: 14px;
    color: var(--color-muted);
  }
}

/* ============================================
   DASHBOARD CARDS
   ============================================ */

@media (max-width: 640px) {
  .dashboard-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    padding: 1rem;
    margin-bottom: 1rem;
  }
  
  .dashboard-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--color-divider);
  }
  
  .dashboard-card-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text);
    margin: 0;
  }
  
  .dashboard-card-action {
    font-size: 13px;
    color: var(--color-accent);
    text-decoration: none;
    font-weight: 600;
  }
  
  .dashboard-card-action:active {
    color: var(--color-accent-strong);
  }
  
  .dashboard-card-body {
    font-size: 14px;
  }
}

/* ============================================
   REALTIME INDICATORS
   ============================================ */

@media (max-width: 640px) {
  .realtime-indicator {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.625rem;
    background: rgba(34, 197, 94, 0.1);
    border-radius: var(--radius-xs);
    font-size: 12px;
    font-weight: 600;
    color: #22c55e;
  }
  
  .realtime-indicator::before {
    content: '';
    width: 6px;
    height: 6px;
    background: currentColor;
    border-radius: 50%;
    animation: pulse 2s ease-in-out infinite;
  }
  
  @keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
  }
}
