/* ==================== Keyframe Animations ==================== */

@keyframes pulse-dot {
    0%, 100% {
        opacity: 0.4;
        transform: scale(1);
    }
    50% {
        opacity: 0.8;
        transform: scale(1.2);
    }
}

@keyframes chartSpin {
    to { transform: translate(-50%, -50%) rotate(360deg); }
    from { transform: translate(-50%, -50%) rotate(0deg); }
}

@keyframes chartLoadingFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes blink-dot {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.4;
    }
}

/* ==================== Custom background overrides for modal ==================== */

body.has-custom-background .modal {
    background: rgba(var(--bg-card-rgb), 0.92);
    border-color: rgba(var(--border-color-rgb), 0.5);
}

body.has-custom-background .modal-header {
    background: rgba(var(--bg-card-rgb), 0.95);
}

body.has-custom-background .modal-tabs {
    background: rgba(var(--bg-card-rgb), 0.85);
}

body.has-custom-background .modal-info-item {
    background: rgba(var(--bg-card-rgb), 0.6);
}

body.has-custom-background .metric-bar {
    background: rgba(var(--border-color-rgb), 0.3);
}

body.has-custom-background .modal-close {
    background: rgba(var(--bg-card-rgb), 0.6);
}

body.has-custom-background .modal-close:hover {
    background: rgba(var(--danger-light), 0.8);
}

body.has-custom-background .modal-tab::before {
    background: rgba(var(--bg-card-rgb), 0.7);
}

body.has-custom-background .latency-task-card {
    background: rgba(var(--bg-card-rgb), 0.7);
}

body.has-custom-background .latency-stat {
    background: rgba(var(--bg-card-rgb), 0.65);
}

body.has-custom-background .latency-chart-container {
    background: rgba(var(--bg-card-rgb), 0.65);
}

body.has-custom-background .chart-section canvas {
    background: rgba(var(--bg-card-rgb), 0.5);
}

body.has-custom-background .latency-task-loss {
    background: rgba(var(--bg-card-rgb), 0.5);
}

body.has-custom-background .latency-legend-item {
    background: rgba(var(--bg-card-rgb), 0.55);
}

/* ==================== Media Queries ==================== */

@media (hover: none) and (pointer: coarse) {
    .modal-body {
        touch-action: pan-y;
    }
}

@media (prefers-reduced-motion: reduce) {
    .chart-section.loading::before,
    .latency-chart-container.loading::before {
        animation: none;
        border-top-color: var(--accent);
        border-right-color: var(--accent);
    }

    .chart-section.loading::after,
    .latency-chart-container.loading::after {
        animation: none;
    }
}

/* ==================== Responsive ==================== */

@media (max-width: 768px) {
    .modal-overlay {
        padding: 0;
        align-items: flex-end;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .modal {
        border-radius: 20px 20px 0 0;
        max-height: 92vh;
        max-width: 100%;
        width: 100%;
    }

    .modal-scroll-indicator {
        height: 3px;
    }

    .modal-drag-handle {
        display: block;
        width: 36px;
        height: 5px;
        background: var(--border-color);
        border-radius: 3px;
        margin: 10px auto 0;
    }

    .modal-header {
        padding: 14px 16px;
        gap: 12px;
    }

    .modal-header h2 {
        font-size: 1.1rem;
        line-height: 1.3;
    }

    .modal-close {
        width: 44px;
        height: 44px;
        font-size: 1.4rem;
        border-radius: var(--radius-md);
    }

    .modal-tabs {
        padding: 10px 12px 0;
        gap: 4px;
    }

    .modal-tab {
        padding: 12px 16px;
        font-size: 0.9rem;
        min-height: 48px;
        border-radius: 12px 12px 0 0;
    }

    .modal-tab::after {
        height: 3px;
        border-radius: 3px 3px 0 0;
    }

    .modal-body {
        padding: 16px;
        padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }

    .modal-info {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .modal-info-item {
        padding: 12px;
        border-radius: var(--radius-md);
    }

    .modal-info-label {
        font-size: 0.72rem;
        margin-bottom: 6px;
    }

    .modal-info-value {
        font-size: 0.88rem;
        line-height: 1.4;
    }

    .modal-charts {
        gap: 16px;
    }

    .modal-charts .chart-section {
        flex: 1 1 100%;
    }

    .chart-header {
        flex-wrap: wrap;
    }

    .chart-header h3,
    .chart-section > h3 {
        font-size: 0.88rem;
        margin-bottom: 0;
    }

    .chart-header h3::before,
    .chart-section > h3::before,
    .latency-chart-header h3::before {
        height: 14px;
    }

    .chart-header h3::after,
    .chart-section > h3::after,
    .latency-chart-header h3::after {
        width: 5px;
        height: 5px;
    }

    .chart-legend {
        font-size: 0.72rem;
        gap: 12px;
    }

    .chart-section canvas {
        width: 100% !important;
        height: 140px !important;
        border-radius: var(--radius-md);
    }

    .latency-summary {
        grid-template-columns: repeat(3, 1fr);
        gap: 10px;
        margin-bottom: 16px;
    }

    .latency-stat {
        padding: 12px 8px;
        border-radius: var(--radius-md);
    }

    .latency-stat-value {
        font-size: 1.2rem;
        margin-bottom: 4px;
    }

    .latency-stat-label {
        font-size: 0.7rem;
    }

    .latency-tasks {
        margin-bottom: 16px;
        grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
        gap: 8px;
    }

    .latency-tasks-title {
        font-size: 0.88rem;
        margin-bottom: 4px;
    }

    .latency-tasks-title::before {
        height: 14px;
    }

    .latency-tasks-title::after {
        width: 5px;
        height: 5px;
    }

    .latency-task-card {
        padding: 8px 10px;
        border-radius: var(--radius-md);
        border-left-width: 4px;
        gap: 4px;
    }

    .latency-task-name {
        font-size: 0.8rem;
        gap: 5px;
    }

    .latency-task-name::before {
        width: 5px;
        height: 5px;
    }

    .latency-task-info {
        gap: 5px;
        white-space: nowrap;
    }

    .latency-task-ping {
        font-size: 0.75rem;
        padding: 2px 5px;
    }

    .latency-task-loss {
        font-size: 0.65rem;
        padding: 1px 4px;
    }

    .latency-chart-container {
        padding: 14px;
        border-radius: var(--radius-md);
    }

    .latency-chart-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        margin-bottom: 12px;
    }

    .latency-chart-header h3 {
        font-size: 0.88rem;
    }

    .latency-chart-header h3::before {
        height: 14px;
    }

    .latency-chart-header h3::after {
        width: 5px;
        height: 5px;
    }

    .latency-legend {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
        width: 100%;
    }

    .latency-legend-item {
        font-size: 0.7rem;
        padding: 4px 8px;
        border-radius: var(--radius-sm);
    }

    .latency-legend-color {
        width: 12px;
        height: 3px;
    }

    .latency-chart-container canvas {
        width: 100% !important;
        height: 200px !important;
    }

    .modal-drag-handle {
        display: block;
    }
}

@media (max-width: 480px) {
    .modal-overlay {
        padding: 0;
    }

    .modal {
        border-radius: 16px 16px 0 0;
        max-height: 95vh;
    }

    .modal-header {
        padding: 12px 14px;
    }

    .modal-header h2 {
        font-size: 1rem;
    }

    .modal-close {
        width: 40px;
        height: 40px;
        font-size: 1.2rem;
    }

    .modal-tabs {
        padding: 8px 10px 0;
        gap: 2px;
    }

    .modal-tab {
        padding: 10px 12px;
        font-size: 0.85rem;
        min-height: 44px;
        border-radius: 10px 10px 0 0;
    }

    .modal-tab::after {
        height: 2px;
    }

    .modal-body {
        padding: 14px;
        padding-bottom: calc(14px + env(safe-area-inset-bottom));
    }

    .modal-info {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .modal-info-item {
        padding: 10px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 4px;
    }

    .modal-info-label {
        font-size: 0.7rem;
        margin-bottom: 0;
    }

    .modal-info-value {
        font-size: 0.82rem;
        text-align: right;
    }

    .chart-header h3,
    .chart-section > h3,
    .latency-chart-header h3 {
        font-size: 0.82rem;
    }

    .latency-chart-header h3::before {
        height: 12px;
        width: 2px;
    }

    .latency-chart-header h3::after {
        width: 4px;
        height: 4px;
    }

    .chart-header h3::before,
    .chart-section > h3::before,
    .latency-chart-header h3::before {
        height: 12px;
        width: 2px;
    }

    .chart-header h3::after,
    .chart-section > h3::after,
    .latency-chart-header h3::after {
        width: 4px;
        height: 4px;
    }

    .chart-legend {
        font-size: 0.68rem;
        gap: 8px;
    }

    .chart-section canvas {
        height: 120px !important;
    }

    .latency-summary {
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
        margin-bottom: 14px;
    }

    .latency-stat {
        padding: 10px 6px;
    }

    .latency-stat-value {
        font-size: 1.1rem;
    }

    .latency-stat-label {
        font-size: 0.65rem;
    }

    .latency-tasks {
        margin-bottom: 14px;
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    .latency-tasks-title {
        font-size: 0.82rem;
        margin-bottom: 4px;
    }

    .latency-tasks-title::before {
        height: 12px;
        width: 2px;
    }

    .latency-tasks-title::after {
        width: 4px;
        height: 4px;
    }

    .latency-task-card {
        padding: 8px 10px;
        grid-template-columns: 1fr;
        gap: 6px;
    }

    .latency-task-name {
        font-size: 0.75rem;
        white-space: normal;
        overflow-wrap: break-word;
        overflow: visible;
        text-overflow: unset;
        gap: 4px;
        padding-left: 10px;
    }

    .latency-task-name::before {
        width: 4px;
        height: 4px;
        left: 2px;
        position: absolute;
    }

    .latency-task-info {
        justify-content: space-between;
        gap: 4px;
        padding-left: 10px;
    }

    .latency-task-ping {
        font-size: 0.72rem;
        padding: 1px 4px;
    }

    .latency-task-loss {
        font-size: 0.62rem;
        padding: 1px 3px;
    }

    .latency-chart-container {
        padding: 12px;
    }

    .latency-chart-header h3 {
        font-size: 0.82rem;
    }

    .latency-legend {
        gap: 4px;
    }

    .latency-legend-item {
        font-size: 0.65rem;
        padding: 3px 6px;
    }

    .latency-chart-container canvas {
        height: 180px !important;
    }

    .chart-section canvas {
        height: 120px !important;
    }
}

@media (orientation: landscape) and (max-height: 500px) {
    .modal {
        max-height: 98vh;
    }

    .modal-overlay {
        padding: 8px;
    }
}

@media (max-width: 768px) and (orientation: landscape) {
    .modal {
        max-height: 95vh;
        border-radius: var(--radius-md);
        margin: auto;
    }

    .modal-overlay {
        padding: 10px;
        align-items: center;
    }

    .modal-body {
        max-height: calc(95vh - 120px);
        overflow-y: auto;
    }

    .chart-section canvas {
        height: 100px !important;
    }

    .latency-chart-container canvas {
        height: 150px !important;
    }
}

@supports (padding: max(0px)) {
    .modal-body {
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }

    .modal-header {
        padding-top: max(14px, env(safe-area-inset-top));
    }

    @media (max-width: 480px) {
        .modal-body {
            padding-bottom: max(14px, env(safe-area-inset-bottom));
        }
    }
}

/* ==================== Hover: hover ==================== */

@media (hover: hover) and (pointer: fine) {
    .modal-info-item:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }

    .latency-stat:hover {
        transform: translateY(-3px);
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
    }

    .latency-task-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1), 0 2px 8px rgba(var(--accent-rgb), 0.1);
    }
}

/* ==================== Hover: none (touch) ==================== */

@media (hover: none) {
    .modal-info-item,
    .latency-stat,
    .latency-task-card,
    .modal-close:hover {
        transition: none;
        transform: none;
    }

    .modal-info-item:active,
    .latency-stat:active,
    .latency-task-card:active {
        background: var(--bg-secondary);
        transform: scale(0.98);
    }

    .latency-task-card:active::before {
        opacity: 1;
    }

    .modal-close:active {
        background: var(--danger-light);
        color: var(--danger);
        transform: scale(0.95);
    }

    .modal-tab:active {
        background: var(--bg-secondary);
        transform: scale(0.98);
    }

    .modal-tab:active::before {
        opacity: 1;
    }

    .chart-tooltip {
        max-width: 200px;
        font-size: 0.75rem;
        padding: 8px 10px;
    }
}
