:root {
    --background: #f9fafb;
    --text: #111827;
    --border: #e5e7eb;
    --shadow: rgba(0, 0, 0, 0.1);
    --primary: #f47621;
    --primary-hover: #e56b1f;
    --secondary: #6b7280;
    --accent: #3b82f6;
    --success: #10b981;
    --error: #ef4444;
    --panel-bg: #ffffff;
    --panel-border: #e5e7eb;
    --tooltip-bg: #ffffff;
    --dns-country: #f47621;
    --default-country: #e0e0e0;
    --default-country-hover: #c0c0c0;
}

.dark-mode {
    --background: #0f172a;
    --text: #ffffff;
    --border: #374151;
    --shadow: rgba(0, 0, 0, 0.5);
    --panel-bg: #1e293b;
    --panel-border: #374151;
    --tooltip-bg: #1e293b;
    --default-country: #1e293b;
    --default-country-hover: #334155;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--background);
    color: var(--text);
    transition: background-color 0.3s ease, color 0.3s ease;
    overflow: hidden;
}

#app {
    width: 100vw;
    height: 100vh;
    position: relative;
}

/* Loading and Error Overlays */
.loading-overlay,
.error-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    background-color: var(--background);
}

.loading-content,
.error-content {
    text-align: center;
    max-width: 400px;
    padding: 2rem;
}

.spinner {
    width: 4rem;
    height: 4rem;
    border: 4px solid var(--border);
    border-top: 4px solid var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.loading-text {
    font-size: 1.125rem;
    margin-bottom: 0.5rem;
}

.loading-subtext {
    font-size: 0.875rem;
    color: var(--secondary);
}

.error-content h2 {
    color: var(--error);
    font-size: 1.5rem;
    margin-bottom: 1rem;
}

.refresh-btn {
    padding: 0.5rem 1rem;
    background-color: var(--accent);
    color: white;
    border: none;
    border-radius: 0.375rem;
    cursor: pointer;
    transition: background-color 0.2s;
}

.refresh-btn:hover {
    background-color: #2563eb;
}

.hidden {
    display: none !important;
}

/* Ensure hidden panel content does not interfere with touch events */
.panel-content.hidden {
    pointer-events: none;
}

/* Map Container */
.map-container {
    width: 100%;
    height: 100%;
    cursor: move;
    background-color: var(--background);
}

.dark-mode .map-container {
    background-color: #0f172a;
}

/* Controls */
.controls {
    position: absolute;
    top: 1rem;
    left: 1rem;
    z-index: 30;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.control-btn {
    padding: 0.75rem;
    border-radius: 50%;
    border: none;
    background-color: var(--panel-bg);
    color: var(--text);
    box-shadow: 0 4px 6px var(--shadow);
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(8px);
    border: 1px solid var(--panel-border);
}

.control-btn:hover {
    transform: scale(1.1);
}

.zoom-controls {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.zoom-level {
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
    background-color: var(--panel-bg);
    color: var(--text);
    font-size: 0.75rem;
    font-family: monospace;
    text-align: center;
    backdrop-filter: blur(8px);
    border: 1px solid var(--panel-border);
}

/* Title Container */
.title-container {
    position: absolute;
    top: 1rem;
    left: 50%;
    transform: translateX(-50%);
    z-index: 20;
    padding: 0 1rem;
}

.title-content {
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    background-color: var(--panel-bg);
    backdrop-filter: blur(8px);
    border: 1px solid var(--panel-border);
    box-shadow: 0 4px 6px var(--shadow);
    max-width: 100%;
}

.title-content {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.logo {
    width: 140px;
    height: 45px;
    object-fit: contain;
}

.title-text h1 {
    font-size: 1.5rem;
    font-weight: bold;
    margin-bottom: 0.25rem;
}

.title-text p {
    font-size: 0.875rem;
    color: var(--secondary);
}

.dark-mode .title-text h1 {
    color: #f9fafb;
}

/* Tooltip */
.tooltip {
    position: absolute;
    pointer-events: none;
    background-color: var(--tooltip-bg);
    color: var(--text);
    padding: 0.75rem;
    border-radius: 0.5rem;
    box-shadow: 0 4px 6px var(--shadow);
    font-size: 0.875rem;
    max-width: 300px;
    z-index: 30;
    transition: opacity 0.2s;
    backdrop-filter: blur(8px);
    border: 1px solid var(--panel-border);
}

/* Panels - Fixed positioning for mobile */
.panel {
    position: absolute;
    z-index: 25;
    transition: all 0.3s ease;
}

/* Ensure panel container does not interfere when content is hidden */
.panel .panel-content.hidden + .panel-toggle {
    z-index: 45; /* Ensure toggle is above hidden content and other elements */
}

.node-types-panel {
    bottom: 1rem;
    right: 1rem;
}

.stats-panel {
    bottom: 1rem;
    left: 1rem;
}

/* Ensure buttons are clickable on mobile */
.panel-toggle {
    padding: 0.75rem;
    border-radius: 50%;
    border: none;
    background-color: var(--panel-bg);
    color: var(--text);
    box-shadow: 0 4px 6px var(--shadow);
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(8px);
    border: 1px solid var(--panel-border);
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    z-index: 30;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.panel-toggle:hover {
    transform: scale(1.1);
}

.panel-toggle svg {
    pointer-events: none; /* Ensure clicks/touches pass through to the button */
}

.panel-toggle:active {
    transform: scale(0.95);
}

.panel-content {
    width: 18rem;
    border-radius: 0.5rem;
    background-color: var(--panel-bg);
    backdrop-filter: blur(8px);
    border: 1px solid var(--panel-border);
    box-shadow: 0 4px 6px var(--shadow);
    padding: 1rem;
    margin-top: 0.5rem;
    position: absolute;
    bottom: 100%;
    right: 0;
    margin-bottom: 0.5rem;
}

.stats-panel .panel-content {
    left: 0;
    right: auto;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

.panel-header h3 {
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.dark-mode .panel-header h3 {
    color: #f9fafb;
}

.panel-close {
    padding: 0.25rem;
    border: none;
    background: none;
    color: var(--text);
    cursor: pointer;
    border-radius: 0.25rem;
    transition: background-color 0.2s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.panel-close:hover {
    background-color: var(--border);
}

.panel-body {
    font-size: 0.875rem;
}

.node-type {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.node-indicator {
    width: 0.75rem;
    height: 0.75rem;
    border-radius: 50%;
    flex-shrink: 0;
}

.node-indicator.cip {
    background-color: #3b82f6;
}

.node-indicator.ns2 {
    background-color: #8b5cf6;
}

.node-label {
    font-weight: 500;
}

.node-description {
    font-size: 0.75rem;
    color: var(--secondary);
}

.dark-mode .node-label,
.dark-mode .node-description {
    color: #f9fafb;
}

.panel-footer {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border);
    font-size: 0.75rem;
    color: var(--secondary);
}

.stat-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.stat-value {
    font-weight: 600;
}

.dark-mode .stat-item {
    color: #f9fafb;
}

/* Modal - Centered for both mobile and desktop */
.modal {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    overflow: auto;
}

.modal:not(.hidden) {
    display: flex;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1;
}

.modal-content {
    position: relative;
    z-index: 2;
    width: 100%;
    max-width: 64rem;
    max-height: 80vh;
    overflow-y: auto;
    border-radius: 0.5rem;
    background-color: var(--background);
    color: var(--text);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
    margin: auto;
}

.modal-header {
    position: sticky;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background-color: var(--background);
}

.modal-title {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.modal-title h2 {
    font-size: 1.5rem;
    font-weight: bold;
}

.modal-title p {
    color: var(--secondary);
}

.modal-close {
    padding: 0.5rem;
    border: none;
    background: none;
    color: var(--text);
    cursor: pointer;
    border-radius: 50%;
    transition: background-color 0.2s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.modal-close:hover {
    background-color: var(--border);
}

.modal-body {
    padding: 1.5rem;
/* Country Overview Styles */
.country-overview {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1.5rem;
    background-color: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    margin-bottom: 1.5rem;
}

.country-flag-large {
    font-size: 3rem;
    line-height: 1;
}

.country-details h3 {
    margin: 0 0 0.5rem 0;
    font-size: 1.25rem;
    font-weight: 600;
}

.country-details p {
    margin: 0.25rem 0;
    color: var(--secondary);
    font-size: 0.9rem;
}

.cities-section {
    margin-top: 1rem;
}

.city-card {
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 1rem;
    margin-bottom: 1rem;
    background-color: var(--panel-bg);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.city-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.city-card:last-child {
    margin-bottom: 0;
}

.city-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.city-name {
    font-size: 1.125rem;
    font-weight: 600;
}

.city-stats {
    color: var(--secondary);
}
}

.city-card {
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 1rem;
    margin-bottom: 1rem;
    background-color: var(--panel-bg);
}

.city-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.city-name {
    font-size: 1.125rem;
    font-weight: 600;
}

.city-stats {
    color: var(--secondary);
}

.host-grid {
    display: grid;
    gap: 1rem;
}

.host-section {
    margin-bottom: 1rem;
}

.host-section h4 {
    font-weight: 500;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.host-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.host-item {
    font-family: monospace;
    font-size: 0.875rem;
    padding: 0.5rem;
    border-radius: 0.25rem;
    background-color: var(--panel-bg);
    border: 1px solid var(--border);
}

/* Responsive Design - Ensure panels are visible and clickable on mobile */
@media (max-width: 768px) {
    .title-content {
        flex-direction: column;
        text-align: center;
        gap: 0.5rem;
    }

    .logo {
        width: 120px;
        height: 38px;
    }

    .title-text h1 {
        font-size: 1.25rem;
    }

    .title-text p {
        font-size: 0.75rem;
    }

    .panel-content {
        width: 16rem;
    }

    .modal-content {
        max-height: 90vh;
        margin: 1rem;
    }

    .modal-header {
        padding: 0.75rem 1rem;
    }

    .modal-body {
        padding: 1rem;
    }

    .host-grid {
        grid-template-columns: 1fr;
    }

    /* Ensure panels are visible and clickable on mobile */
    .node-types-panel,
    .stats-panel {
        bottom: 0.5rem;
        z-index: 25;
    }

    .node-types-panel {
        right: 0.5rem;
    }

    .stats-panel {
        left: 0.5rem;
    }

    .panel-content {
        position: absolute;
        bottom: 100%;
        margin-bottom: 0.5rem;
    }
}

@media (max-width: 480px) {
    .controls {
        top: 0.5rem;
        left: 0.5rem;
    }

    .title-container {
        top: 0.5rem;
    }

    .title-content {
        padding: 0.5rem 1rem;
    }

    .logo {
        width: 100px;
        height: 32px;
    }

    .stats-panel {
        bottom: 0.5rem;
        left: 0.5rem;
        right: auto; /* Ensure it stays on the left */
    }

    .node-types-panel {
        bottom: 0.5rem;
        right: 0.5rem;
        left: auto; /* Ensure it stays on the right */
        z-index: 35; /* Ensure this panel's stacking context is above others */
    }

    .panel-toggle {
        /* Reverted mobile-specific z-index and touch properties */
    }

    .panel-content {
        width: auto;
        max-width: none;
        position: relative;
        bottom: auto;
        margin-top: 0.5rem;
    }

    .modal-content {
        margin: 0.5rem;
        max-height: 95vh;
    }

    /* Ensure panels don't overlap on very small screens */
    .node-types-panel {
        right: 0.5rem;
        left: auto;
    }
}

@media (max-width: 320px) {
    /* Ensure visibility on very small screens */
    .node-types-panel,
    .stats-panel {
        bottom: 0.25rem;
    }

    .panel-toggle {
        padding: 0.5rem;
        min-width: 40px;
        min-height: 40px;
    }

    .panel-content {
        width: calc(100vw - 1rem);
        max-width: 280px;
    }
}

/* SVG Styles */
.country {
    stroke: var(--border);
    stroke-width: 0.5;
    cursor: pointer;
    transition: fill 0.3s ease;
}

.country.dns-country {
    fill: var(--dns-country);
}

.country.default-country {
    fill: var(--default-country);
}

.country:hover {
    stroke-width: 1;
}

.country.dns-country:hover {
    fill: var(--primary-hover);
}

.country.default-country:hover {
    fill: var(--default-country-hover);
}

/* Theme-specific overrides */
.dark-mode .country {
    stroke: var(--border);
}

.light-mode .country {
    stroke: #ffffff;
}

/* Ensure buttons are always clickable */
.panel-toggle,
.panel-close {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* Fix for mobile Safari */
@supports (-webkit-touch-callout: none) {
    .panel-toggle,
    .panel-close {
        cursor: pointer;
    }
}