:root {
    --font-ui: 'Manrope', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    --radius-lg: 20px;
    --radius-md: 14px;
    --radius-sm: 8px;

    /* DC Colors (Refined) */
    --dc1: #FF453A;
    --dc2: #0A84FF;
    --dc3: #32D74B;
    --dc4: #FFD60A;
    --dc5: #BF5AF2;
}

[data-theme="light"] {
    --bg-app: #F5F5F7;
    --bg-card: #FFFFFF;
    --bg-sidebar: #FFFFFF;
    --text-primary: #1D1D1F;
    --text-secondary: #86868B;
    --border: #E5E5EA;
    --accent: #0071E3;
    --accent-hover: #005BB5;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
    --shadow-hover: 0 12px 24px rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
    --bg-app: #000000;
    --bg-card: #1C1C1E;
    --bg-sidebar: #121212;
    --text-primary: #F5F5F7;
    --text-secondary: #98989D;
    --border: #2C2C2E;
    --accent: #0A84FF;
    --accent-hover: #409CFF;
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    --shadow-hover: 0 12px 24px rgba(0, 0, 0, 0.5);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: var(--font-ui);
    background-color: var(--bg-app);
    color: var(--text-primary);
    transition: background 0.3s ease, color 0.3s ease;
    height: 100vh;
    overflow: hidden;
    /* Prevent body scroll, use container scroll */
}

.app-container {
    display: flex;
    height: 100vh;
    width: 100%;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex-shrink: 0;
    z-index: 10;
    overflow-y: auto;
    transition: width 0.3s ease;
}

.sidebar-header {
    margin-bottom: 2rem;
}

.logo-box {
    width: 44px;
    height: 44px;
    background: var(--text-primary);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1rem;
    box-shadow: var(--shadow);
}

.pulse-dot {
    width: 12px;
    height: 12px;
    background: var(--bg-card);
    border-radius: 50%;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        opacity: 0.5;
        transform: scale(0.8);
    }

    50% {
        opacity: 1;
        transform: scale(1.1);
    }

    100% {
        opacity: 0.5;
        transform: scale(0.8);
    }
}

.sidebar-header h1 {
    font-size: 1.35rem;
    font-weight: 800;
    line-height: 1.1;
    margin: 0;
    letter-spacing: -0.03em;
}

.control-box {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.status-badge-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    background: var(--bg-app);
    border-radius: var(--radius-sm);
}

.mono-label {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--text-secondary);
    font-weight: 600;
}

.status-pill {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--accent);
}

.info-grid {
    display: grid;
    gap: 1rem;
}

.info-item {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.mono-val {
    font-family: var(--font-mono);
    font-size: 0.95rem;
    font-weight: 700;
}

.primary-btn {
    width: 100%;
    background: var(--accent);
    color: white;
    border: none;
    padding: 1rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-family: var(--font-ui);
    font-weight: 700;
    font-size: 0.95rem;
    transition: all 0.2s cubic-bezier(0.25, 1, 0.5, 1);
    box-shadow: 0 4px 12px rgba(0, 113, 227, 0.3);
}

.primary-btn:hover {
    background: var(--accent-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 113, 227, 0.4);
}

.primary-btn:active {
    transform: translateY(0);
}

.primary-btn:disabled {
    opacity: 0.6;
    cursor: wait;
    transform: none;
    box-shadow: none;
    filter: grayscale(1);
}

.btn-content {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}


.theme-switcher {
    display: flex;
    gap: 0.5rem;
    margin-top: 2rem;
    padding: 0.25rem;
    background: var(--bg-app);
    border-radius: var(--radius-lg);
    width: fit-content;
}

.theme-btn {
    background: none;
    border: none;
    padding: 0.5rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    color: var(--text-secondary);
    display: flex;
}

.theme-btn.active {
    background: var(--bg-card);
    color: var(--text-primary);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}


.side-footer {
    margin-top: 2rem;
    border-top: 1px solid var(--border);
    padding-top: 1rem;
}

.side-footer p {
    margin: 0;
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
}

.mono-xs {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    opacity: 0.7;
}

/* Main Content */
.main-view {
    flex: 1;
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
    overflow-y: auto;
    /* Enable scroll */
    height: 100%;
}

/* DC Grid */
.dc-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    flex-shrink: 0;
}

.rec-box {
    background: var(--bg-card);
    border: 1px solid var(--accent);
    border-radius: var(--radius-md);
    padding: 1rem;
    margin-bottom: 1rem;
    box-shadow: 0 4px 12px rgba(var(--accent-rgb), 0.1);
    animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.rec-label {
    display: block;
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
    font-weight: 700;
}

.rec-val {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

#best-dc-id {
    font-size: 1.25rem;
    font-weight: 800;
    color: var(--accent);
}

#best-dc-ms {
    font-family: var(--font-mono);
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
}

.rank-badge {
    font-size: 0.8rem;
    font-weight: 800;
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    background: var(--bg-app);
    color: var(--text-secondary);
    font-family: var(--font-mono);
}

.rank-badge.rank-1 {
    background: #FFD700;
    color: #000;
    box-shadow: 0 0 10px rgba(255, 215, 0, 0.4);
    border: 1px solid #FFD700;
}

.rank-badge.rank-2 {
    background: #C0C0C0;
    color: #000;
    border: 1px solid #C0C0C0;
}

.rank-badge.rank-3 {
    background: #CD7F32;
    color: #fff;
    border: 1px solid #CD7F32;
}

.dc-card {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 180px;
    transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1), box-shadow 0.3s ease;
    border: 1px solid var(--border);
}

.dc-card:hover {
    transform: translateY(-6px);
    box-shadow: var(--shadow-hover);
    border-color: var(--accent);
}

.card-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.dc-badge {
    font-size: 0.75rem;
    font-weight: 800;
    color: #fff;
    padding: 0.3rem 0.6rem;
    border-radius: 99px;
    font-family: var(--font-mono);
}

.badge-dc1 {
    background: var(--dc1);
}

.badge-dc2 {
    background: var(--dc2);
}

.badge-dc3 {
    background: var(--dc3);
}

.badge-dc4 {
    background: var(--dc4);
}

.badge-dc5 {
    background: var(--dc5);
}

.dc-region {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.card-main {
    margin-bottom: auto;
}

.latency-value {
    font-family: var(--font-ui);
    font-size: 2.5rem;
    font-weight: 800;
    letter-spacing: -0.05em;
    display: block;
    line-height: 1;
}

.latency-unit {
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-weight: 600;
}

.card-bottom {
    margin-top: 1.5rem;
}

.status-text {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-secondary);
    display: block;
    margin-bottom: 0.5rem;
}

.mini-progress-track {
    height: 6px;
    background: var(--bg-app);
    border-radius: 99px;
    overflow: hidden;
}

.mini-progress-bar {
    height: 100%;
    width: 0%;
    transition: width 0.3s ease;
}

.badge-dc1-bg {
    background-color: var(--dc1);
}

.badge-dc2-bg {
    background-color: var(--dc2);
}

.badge-dc3-bg {
    background-color: var(--dc3);
}

.badge-dc4-bg {
    background-color: var(--dc4);
}

.badge-dc5-bg {
    background-color: var(--dc5);
}


/* Graph Section */
.visualizer-section {
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 2rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    height: 400px;
    /* Fixed robust height */
    min-height: 400px;
    margin-bottom: 2rem;
    /* Add margin at bottom for scroll space */
    position: relative;
    flex-shrink: 0;
    border: 1px solid var(--border);
}

.vis-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.vis-header h3 {
    margin: 0;
    font-size: 1rem;
    color: var(--text-secondary);
}

.vis-legend {
    display: flex;
    gap: 1.5rem;
}

.v-tag {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    font-weight: 600;
}

.dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.chart-container {
    flex: 1;
    width: 100%;
    height: 100%;
    /* Fill the section */
    position: relative;
    overflow: hidden;
}

canvas {
    width: 100%;
    height: 100%;
}

/* Mobile - Polished "Perfect" Layout */
@media (max-width: 900px) {
    .app-container {
        flex-direction: column;
        height: 100vh;
        overflow: hidden;
        background: var(--bg-app);
    }

    /* Header Bar */
    .sidebar {
        width: 100%;
        height: 60px;
        flex-shrink: 0;
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding: 0 1rem;
        background: var(--bg-card);
        /* Remove backdrop-filter to prevent containing block trap */
        transform: none !important;
        will-change: auto;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        z-index: 900;
        /* Lower than modal/control-box */
        position: relative;
    }

    .sidebar-header {
        margin: 0;
        display: flex;
        align-items: center;
        width: auto;
        flex: 1;
    }

    .logo-box {
        width: 28px;
        height: 28px;
        margin-right: 0.75rem;
        margin-bottom: 0;
        box-shadow: none;
        background: transparent;
    }

    .logo-box img {
        filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
    }

    .sidebar-header h1 {
        font-size: 1rem;
        line-height: 1;
        text-align: left;
        margin: 0;
    }

    .sidebar-header h1 br {
        display: none;
    }

    /* Header Actions (Theme) */
    .theme-switcher {
        margin: 0;
        padding: 0;
        background: transparent;
        position: static;
        transform: none;
        display: flex;
        gap: 0.5rem;
    }

    .theme-btn {
        padding: 0.4rem;
        background: var(--bg-app);
        border-radius: 50%;
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .theme-btn.active {
        background: var(--accent);
        color: white;
        box-shadow: 0 2px 8px rgba(0, 113, 227, 0.3);
    }

    /* Scrollable Content */
    .main-view {
        padding: 1rem;
        height: 100%;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        display: flex;
        flex-direction: column;
        gap: 1rem;
        padding-bottom: 100px;
        /* Space for floating bottom bar */
    }

    /* Cards - Refined Grid */
    .dc-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0.75rem;
    }

    .dc-card {
        min-height: auto;
        padding: 1rem;
        border-radius: var(--radius-lg);
        gap: 0.25rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
        border: 1px solid var(--border);
    }

    .card-top {
        margin-bottom: 0.5rem;
    }

    .dc-badge {
        font-size: 0.6rem;
        padding: 0.15rem 0.5rem;
    }

    .dc-region {
        font-size: 0.6rem;
        font-weight: 700;
        opacity: 0.6;
    }

    .card-main {
        margin: 0.25rem 0 0.5rem 0;
    }

    .latency-value {
        font-size: 1.8rem;
        font-weight: 800;
        letter-spacing: -0.03em;
    }

    .latency-unit {
        font-size: 0.75rem;
        font-weight: 600;
        opacity: 0.5;
    }

    .card-bottom {
        margin-top: 0;
    }

    .status-text {
        font-size: 0.6rem;
        margin-bottom: 0.35rem;
        font-weight: 700;
        opacity: 0.7;
    }

    .mini-progress-track {
        height: 4px;
        background: rgba(0, 0, 0, 0.05);
    }

    /* Graph */
    .visualizer-section {
        height: 200px;
        min-height: 200px;
        padding: 1rem;
        width: 100%;
        order: 99;
        margin-bottom: 1rem;
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow);
    }

    .vis-header {
        margin-bottom: 0.5rem;
    }

    .vis-header h3 {
        font-size: 0.8rem;
        font-weight: 700;
        letter-spacing: 0.05em;
        opacity: 0.6;
    }

    /* Sticky Bottom Action Bar */
    .sidebar .control-box {
        position: fixed;
        bottom: 1.5rem !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        width: 90% !important;
        /* Center it nicely */
        max-width: 400px;
        background: rgba(var(--bg-card-rgb, 255, 255, 255), 0.95);
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        padding: 0.75rem;
        border: 1px solid var(--border);
        border-radius: 20px;
        z-index: 9999;
        display: flex !important;
        flex-direction: row;
        align-items: center;
        margin: 0;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
        gap: 0;
    }

    .sidebar .action-wrapper,
    .sidebar .rec-box,
    .sidebar .info-grid,
    .sidebar .status-badge-row,
    .sidebar .side-footer {
        display: contents;
    }

    /* Hide non-critical elements in floating bar */
    .sidebar .rec-box,
    .sidebar .info-grid,
    .sidebar .status-badge-row,
    .sidebar .side-footer,
    .header-left,
    .mono-label {
        display: none !important;
    }

    .sidebar .action-wrapper {
        display: flex;
        width: 100%;
    }

    .primary-btn {
        height: 52px;
        font-size: 1rem;
        border-radius: 14px;
        box-shadow: 0 4px 12px rgba(0, 113, 227, 0.3);
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .btn-content svg {
        margin-left: 0.5rem;
    }
}