/**
 * Mobile Chart Optimizations
 *
 * Optimizes ApexCharts and other chart libraries for mobile viewing
 * Based on patterns from Power BI Mobile, Looker, Tableau
 *
 * Key optimizations:
 * 1. Reduced chart heights
 * 2. Simplified legends (bottom, smaller text)
 * 3. Reduced axis label density
 * 4. Smaller fonts throughout
 * 5. Touch-friendly tooltips
 */

/* ===========================================
   GLOBAL TOOLTIP REFINEMENT
   =========================================== */

.apexcharts-tooltip {
    border-radius: 8px !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08), 0 8px 24px rgba(0, 0, 0, 0.06) !important;
    border: 1px solid #e2e8f0 !important;
    padding: 6px 10px !important;
    font-family: 'Inter', 'Roboto', sans-serif !important;
}

.apexcharts-tooltip-title {
    border-bottom: 1px solid #f1f5f9 !important;
    padding: 6px 10px !important;
    font-weight: 600 !important;
}

/* ===========================================
   GLOBAL CHART CONTAINER
   =========================================== */

.apex-chart-container {
    width: 100%;
    height: 100%;
    min-height: 200px;
}

/* Desktop/Tablet: Default sizing */
@media (min-width: 769px) {
    .apex-chart-container {
        min-height: 280px;
    }
}

/* ===========================================
   MOBILE CHART OPTIMIZATIONS (≤768px)
   =========================================== */

@media (max-width: 768px) {
    /* Container sizing */
    .apex-chart-container {
        min-height: 140px !important;
    }

    /* Chart title - compact */
    .apex-chart-container .mud-typography-h6 {
        font-size: var(--chart-title-size, 11px) !important;
        margin-bottom: 2px !important;
        line-height: 1.2 !important;
    }

    /* ApexCharts specific overrides */
    .apexcharts-canvas {
        max-height: 180px !important;
    }

    /* Title styling */
    .apexcharts-title-text {
        font-size: 10px !important;
    }

    /* Legend - compact bottom position */
    .apexcharts-legend {
        padding: 2px 0 !important;
    }

    .apexcharts-legend-text {
        font-size: var(--chart-legend-size, 8px) !important;
    }

    .apexcharts-legend-marker {
        width: 8px !important;
        height: 8px !important;
        margin-right: 2px !important;
    }

    .apexcharts-legend-series {
        margin: 1px 4px !important;
    }

    /* X-Axis labels - rotated and smaller */
    .apexcharts-xaxis-label {
        font-size: var(--chart-label-size, 8px) !important;
    }

    .apexcharts-xaxis text {
        font-size: 8px !important;
    }

    /* Y-Axis labels - smaller */
    .apexcharts-yaxis-label {
        font-size: var(--chart-label-size, 8px) !important;
    }

    .apexcharts-yaxis text {
        font-size: 8px !important;
    }

    /* Axis titles - hide on mobile for space */
    .apexcharts-xaxis-title,
    .apexcharts-yaxis-title {
        display: none !important;
    }

    /* Grid lines - lighter, fewer */
    .apexcharts-gridlines-horizontal line,
    .apexcharts-gridlines-vertical line {
        stroke: #f0f0f0 !important;
        stroke-width: 0.5px !important;
    }

    /* Data labels - smaller */
    .apexcharts-datalabel,
    .apexcharts-data-labels text {
        font-size: 7px !important;
    }

    /* Pie/Donut labels */
    .apexcharts-pie-label {
        font-size: 8px !important;
    }

    .apexcharts-datalabel-label {
        font-size: 8px !important;
    }

    .apexcharts-datalabel-value {
        font-size: 10px !important;
    }

    /* Tooltips - touch friendly */
    .apexcharts-tooltip {
        font-size: 10px !important;
        padding: 4px 8px !important;
        border-radius: 4px !important;
    }

    .apexcharts-tooltip-title {
        font-size: 9px !important;
        padding: 3px 6px !important;
    }

    .apexcharts-tooltip-series-group {
        padding: 2px 6px !important;
    }

    .apexcharts-tooltip-text {
        font-size: 10px !important;
    }

    /* Toolbar - hide on mobile (not touch-friendly) */
    .apexcharts-toolbar {
        display: none !important;
    }

    /* Zoom/pan controls - disable */
    .apexcharts-zoom-icon,
    .apexcharts-pan-icon,
    .apexcharts-reset-icon {
        display: none !important;
    }
}

/* ===========================================
   SMALL MOBILE (≤480px) - Even more compact
   =========================================== */

@media (max-width: 480px) {
    .apex-chart-container {
        min-height: 120px !important;
    }

    .apexcharts-canvas {
        max-height: 150px !important;
    }

    .apex-chart-container .mud-typography-h6 {
        font-size: 10px !important;
    }

    .apexcharts-legend-text {
        font-size: 7px !important;
    }

    .apexcharts-xaxis-label,
    .apexcharts-yaxis-label {
        font-size: 7px !important;
    }

    /* Only show every other tick label */
    .apexcharts-xaxis-label:nth-child(odd) {
        opacity: 0.3;
    }

    .apexcharts-datalabel {
        font-size: 6px !important;
    }

    .apexcharts-tooltip {
        font-size: 9px !important;
    }
}

/* ===========================================
   VERY SMALL MOBILE (≤360px) - Maximum compact
   =========================================== */

@media (max-width: 360px) {
    .apex-chart-container {
        min-height: 100px !important;
    }

    .apexcharts-canvas {
        max-height: 120px !important;
    }

    /* Hide legend on very small screens */
    .apexcharts-legend {
        display: none !important;
    }

    .apexcharts-xaxis-label,
    .apexcharts-yaxis-label {
        font-size: 6px !important;
    }

    /* Show only every 3rd label */
    .apexcharts-xaxis-label:not(:nth-child(3n)) {
        opacity: 0;
    }
}

/* ===========================================
   CHART TYPE SPECIFIC OPTIMIZATIONS
   =========================================== */

/* Bar/Column charts - adjust bar width for touch */
@media (max-width: 768px) {
    .apexcharts-bar-area rect {
        /* Ensure bars are touch-friendly */
        min-width: 20px;
    }
}

/* Line charts - thinner lines on mobile */
@media (max-width: 768px) {
    .apexcharts-line {
        stroke-width: 1.5px !important;
    }

    .apexcharts-area {
        stroke-width: 1px !important;
    }

    /* Smaller data point markers */
    .apexcharts-marker {
        width: 4px !important;
        height: 4px !important;
    }
}

/* Pie/Donut - adjust for mobile */
@media (max-width: 768px) {
    .apexcharts-pie-series path {
        stroke-width: 1px !important;
    }

    /* Smaller center total for donut */
    .apexcharts-datalabel-value {
        font-size: 12px !important;
    }

    .apexcharts-pie-label {
        font-size: 7px !important;
    }
}

/* Radar charts */
@media (max-width: 768px) {
    .apexcharts-radar-series polygon {
        stroke-width: 1px !important;
    }
}

/* ===========================================
   KPI WIDGET MOBILE OPTIMIZATIONS
   =========================================== */

@media (max-width: 768px) {
    /* KPI value - primary metric */
    .kpi-value,
    .kpi-widget .kpi-value {
        font-size: 18px !important;
        line-height: 1.1 !important;
    }

    /* KPI title */
    .kpi-title,
    .kpi-widget .kpi-title {
        font-size: 10px !important;
        line-height: 1.2 !important;
    }

    /* KPI change indicator */
    .kpi-change,
    .kpi-widget .kpi-change {
        font-size: 9px !important;
    }

    /* KPI icon */
    .kpi-icon {
        font-size: 16px !important;
    }

    /* KPI card padding */
    .kpi-widget,
    .kpi-card {
        padding: 6px 8px !important;
    }
}

@media (max-width: 480px) {
    .kpi-value,
    .kpi-widget .kpi-value {
        font-size: 16px !important;
    }

    .kpi-title,
    .kpi-widget .kpi-title {
        font-size: 9px !important;
    }

    .kpi-widget,
    .kpi-card {
        padding: 4px 6px !important;
    }
}

/* ===========================================
   TABLE WIDGET MOBILE OPTIMIZATIONS
   =========================================== */

@media (max-width: 768px) {
    .table-widget,
    .pivot-table-widget {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .table-widget table,
    .pivot-table-widget table {
        font-size: 9px !important;
    }

    .table-widget th,
    .pivot-table-widget th {
        font-size: 8px !important;
        padding: 4px 6px !important;
        white-space: nowrap !important;
    }

    .table-widget td,
    .pivot-table-widget td {
        font-size: 9px !important;
        padding: 3px 6px !important;
    }

    /* MudTable overrides */
    .mud-table .mud-table-head th {
        font-size: 8px !important;
        padding: 4px 6px !important;
    }

    .mud-table .mud-table-body td {
        font-size: 9px !important;
        padding: 3px 6px !important;
    }
}

@media (max-width: 480px) {
    .table-widget table,
    .pivot-table-widget table {
        font-size: 8px !important;
    }

    .table-widget th,
    .pivot-table-widget th {
        font-size: 7px !important;
        padding: 2px 4px !important;
    }

    .table-widget td,
    .pivot-table-widget td {
        font-size: 8px !important;
        padding: 2px 4px !important;
    }
}

/* ===========================================
   DARK MODE CHART ADJUSTMENTS
   =========================================== */

/* Dark mode — responds to app toggle via data-theme attribute */
[data-theme="dark"] .apexcharts-gridlines-horizontal line,
[data-theme="dark"] .apexcharts-gridlines-vertical line {
    stroke: #334155 !important;
}

[data-theme="dark"] .apexcharts-xaxis-label,
[data-theme="dark"] .apexcharts-yaxis-label {
    fill: #94a3b8 !important;
}

[data-theme="dark"] .apexcharts-tooltip {
    background: #1e293b !important;
    border-color: #334155 !important;
    color: #e2e8f0 !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.2) !important;
}

[data-theme="dark"] .apexcharts-tooltip-title {
    background: #334155 !important;
    border-color: #475569 !important;
}
