body {
    font-family: sans-serif;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #1a1a1a;
    color: #fff;
}

body::before {
    content: "";
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('assets/Grey_worldmap.png');
    background-size: contain;
    background-position: center center;
    background-repeat: no-repeat;
    background-color: #121212;
    opacity: 0.2;
    z-index: -1;
    pointer-events: none;
}

@media (orientation: portrait) {
    body::before {
        background-position: center center;
    }
}

#controls {
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 10;
    background: rgba(0, 0, 0, 0.7);
    padding: 10px;
    border-radius: 8px;
}

#graph {
    width: 100vw;
    height: 100vh;
    display: none;
    opacity: 0;
    transition: opacity 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

#graph.visible {
    opacity: 1;
}

/* Loading Overlay & Animated Dots */
#loading {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: radial-gradient(circle at center, rgba(30, 41, 59, 0.95) 0%, rgba(15, 23, 42, 0.98) 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    opacity: 1;
    visibility: visible;
    transition: opacity 0.5s cubic-bezier(0.4, 0, 0.2, 1), visibility 0.5s ease;
}

#loading.fade-out {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.loading-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 36px 52px;
    background: rgba(15, 23, 42, 0.75);
    border: 1px solid rgba(56, 189, 248, 0.25);
    border-radius: 20px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6), 0 0 30px rgba(56, 189, 248, 0.15);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    gap: 16px;
    user-select: none;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(56, 189, 248, 0.15);
    border-top-color: #38bdf8;
    border-right-color: #818cf8;
    border-radius: 50%;
    animation: loadingSpin 0.9s linear infinite;
}

@keyframes loadingSpin {
    to { transform: rotate(360deg); }
}

.loading-text {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 26px;
    font-weight: 600;
    letter-spacing: 0.5px;
    color: #f8fafc;
    display: inline-flex;
    align-items: baseline;
}

.loading-dots {
    display: inline-flex;
    margin-left: 3px;
    letter-spacing: 2px;
}

.loading-dots .dot {
    display: inline-block;
    color: #38bdf8;
    font-size: 28px;
    line-height: 1;
    animation: dotWave 1.4s infinite ease-in-out both;
}

.loading-dots .dot:nth-child(1) {
    animation-delay: -0.32s;
}

.loading-dots .dot:nth-child(2) {
    animation-delay: -0.16s;
}

.loading-dots .dot:nth-child(3) {
    animation-delay: 0s;
}

@keyframes dotWave {
    0%, 80%, 100% {
        opacity: 0.25;
        transform: translateY(0) scale(0.9);
    }
    40% {
        opacity: 1;
        transform: translateY(-8px) scale(1.25);
        text-shadow: 0 0 12px rgba(56, 189, 248, 0.9);
    }
}

button {
    cursor: pointer;
    padding: 8px 12px;
    background: #007bff;
    border: none;
    color: white;
    border-radius: 4px;
    font-weight: bold;
}

button:hover {
    background: #0056b3;
}

.tooltip {
    position: absolute;
    background: rgba(0, 0, 0, 0.8);
    color: #fff;
    padding: 5px 10px;
    border-radius: 4px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
    font-size: 12px;
}

.date-bubble {
    position: absolute;
    top: 10px;
    right: 10px;
    background: #fff;
    color: #000;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: bold;
    z-index: 10;
}

.date-bubble.fallback {
    background: #fffacd;
    color: #333;
}

line {
    stroke-opacity: 0.4;
}

line:hover {
    stroke-opacity: 0.8;
}

/* ==========================================================
   Modal Dialog (#nodeModal & .modal-content)
   Responsive, glassmorphic executive design
   ========================================================== */

#nodeModal,
#nodeModal * {
    box-sizing: border-box;
}

#nodeModal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background: rgba(4, 7, 14, 0.78);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    justify-content: center;
    align-items: center;
    padding: 16px;
    overflow-y: auto;
    overscroll-behavior: contain;
    animation: modalBackdropFade 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

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

#nodeModal .modal-content {
    background: linear-gradient(160deg, rgba(30, 41, 59, 0.95) 0%, rgba(15, 23, 42, 0.98) 100%);
    border: 1px solid rgba(255, 255, 255, 0.12);
    padding: 24px 24px 18px 24px;
    border-radius: 20px;
    max-width: 520px;
    width: 100%;
    margin: auto;
    max-height: min(88vh, 720px);
    max-height: min(88dvh, 720px);
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 25px 60px -12px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(255, 255, 255, 0.05);
    animation: modalPopIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalPopIn {
    from {
        opacity: 0;
        transform: scale(0.92) translateY(12px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

/* Close & External action buttons */
#nodeModal .close-btn {
    position: absolute;
    top: 14px;
    right: 14px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: #94a3b8;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    transition: all 0.2s ease;
    z-index: 10;
    user-select: none;
}

#nodeModal .close-btn:hover {
    background: rgba(239, 68, 68, 0.2);
    color: #f87171;
    border-color: rgba(239, 68, 68, 0.4);
    transform: scale(1.08);
}

#chatgptBtn {
    position: absolute;
    top: 14px;
    right: 52px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.08);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: all 0.2s ease;
    z-index: 10;
}

#chatgptBtn img {
    width: 18px;
    height: 18px;
    border-radius: 4px;
    object-fit: cover;
    margin: 0;
}

#chatgptBtn:hover {
    background: rgba(16, 163, 127, 0.2);
    border-color: rgba(16, 163, 127, 0.5);
    transform: scale(1.08);
    box-shadow: 0 0 12px rgba(16, 163, 127, 0.35);
}

/* Reduced size and refined styling for node image */
#nodeModal #modalImg {
    width: 72px;
    height: 72px;
    min-width: 72px;
    min-height: 72px;
    border-radius: 50%;
    object-fit: cover;
    margin: 4px auto 12px auto;
    display: block;
    border: 2px solid rgba(56, 189, 248, 0.5);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.5), 0 0 16px rgba(56, 189, 248, 0.2);
    background: #0f172a;
    flex-shrink: 0;
    transition: transform 0.25s ease, border-color 0.25s ease;
}

#nodeModal #modalImg:hover {
    transform: scale(1.05);
    border-color: rgba(56, 189, 248, 0.9);
}

/* Entity name title */
#nodeModal h2 {
    margin: 0 0 14px 0;
    padding: 0 36px;
    font-size: clamp(1.2rem, 3.2vw, 1.55rem);
    font-weight: 700;
    color: #f8fafc;
    text-align: center;
    line-height: 1.25;
    word-break: break-word;
    letter-spacing: -0.01em;
    flex-shrink: 0;
}

/* Description area (Markdown content) */
#nodeModal #modalDesc {
    font-size: 14px;
    color: #cbd5e1;
    margin-bottom: 12px;
    word-break: break-word;
    text-align: left;
    flex: 1 1 auto;
    min-height: 0;
    max-height: min(48vh, 380px);
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 12px 14px;
    line-height: 1.6;
    background: rgba(15, 23, 42, 0.45);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 12px;
}

/* Custom scrollbar for modal description */
#nodeModal #modalDesc::-webkit-scrollbar {
    width: 6px;
}
#nodeModal #modalDesc::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.04);
    border-radius: 4px;
}
#nodeModal #modalDesc::-webkit-scrollbar-thumb {
    background: rgba(56, 189, 248, 0.35);
    border-radius: 4px;
}
#nodeModal #modalDesc::-webkit-scrollbar-thumb:hover {
    background: rgba(56, 189, 248, 0.65);
}

/* Markdown elements inside #modalDesc */
#nodeModal #modalDesc h1,
#nodeModal #modalDesc h2,
#nodeModal #modalDesc h3,
#nodeModal #modalDesc h4 {
    color: #f1f5f9;
    margin: 16px 0 8px 0;
    font-weight: 600;
}
#nodeModal #modalDesc h1:first-child,
#nodeModal #modalDesc h2:first-child,
#nodeModal #modalDesc h3:first-child {
    margin-top: 0;
}
#nodeModal #modalDesc h1 { font-size: 19px; }
#nodeModal #modalDesc h2 { font-size: 17px; }
#nodeModal #modalDesc h3 { 
    font-size: 15.5px; 
    color: #38bdf8;
    border-bottom: 1px solid rgba(56, 189, 248, 0.2);
    padding-bottom: 4px;
}
#nodeModal #modalDesc h4 { font-size: 14px; }

#nodeModal #modalDesc p {
    margin: 0 0 10px 0;
    font-size: 14px;
    color: #cbd5e1;
    line-height: 1.6;
    white-space: normal;
}
#nodeModal #modalDesc p:last-child {
    margin-bottom: 0;
}

#nodeModal #modalDesc strong {
    color: #f8fafc;
    font-weight: 600;
}

#nodeModal #modalDesc em {
    color: #94a3b8;
}

#nodeModal #modalDesc ul,
#nodeModal #modalDesc ol {
    margin: 6px 0 12px 0;
    padding-left: 20px;
}

#nodeModal #modalDesc li {
    margin-bottom: 4px;
    line-height: 1.5;
}

#nodeModal #modalDesc hr {
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    margin: 14px 0;
}

#nodeModal #modalDesc blockquote {
    margin: 8px 0;
    padding: 6px 12px;
    border-left: 3px solid #38bdf8;
    background: rgba(56, 189, 248, 0.08);
    border-radius: 0 6px 6px 0;
    color: #94a3b8;
}

#nodeModal #modalDesc a {
    color: #38bdf8;
    text-decoration: underline;
    text-underline-offset: 3px;
}
#nodeModal #modalDesc a:hover {
    color: #7dd3fc;
}

#nodeModal #modalDesc code {
    background: rgba(15, 23, 42, 0.6);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 12.5px;
    color: #e2e8f0;
}

.modal-footer {
    margin-top: 10px;
    font-size: 12px;
    color: #64748b;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    padding-top: 10px;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex-shrink: 0;
    letter-spacing: 0.2px;
}

/* ==========================================================
   Media Queries for Device Responsiveness
   ========================================================== */

/* Tablets and medium screens */
@media (max-width: 768px) {
    #nodeModal .modal-content {
        max-width: 480px;
        padding: 22px 20px 16px 20px;
        border-radius: 18px;
    }
    #nodeModal #modalImg {
        width: 66px;
        height: 66px;
        min-width: 66px;
        min-height: 66px;
    }
}

/* Mobile phones (portrait) */
@media (max-width: 480px) {
    #nodeModal {
        padding: 12px;
    }
    #nodeModal .modal-content {
        width: 100%;
        padding: 18px 14px 14px 14px;
        border-radius: 16px;
        max-height: calc(100dvh - 24px);
    }
    #nodeModal .close-btn {
        top: 12px;
        right: 12px;
        width: 30px;
        height: 30px;
        font-size: 18px;
    }
    #chatgptBtn {
        top: 12px;
        right: 48px;
        width: 30px;
        height: 30px;
    }
    #chatgptBtn img {
        width: 16px;
        height: 16px;
    }
    #nodeModal #modalImg {
        width: 58px;
        height: 58px;
        min-width: 58px;
        min-height: 58px;
        margin: 2px auto 10px auto;
        border-width: 2px;
    }
    #nodeModal h2 {
        font-size: 1.2rem;
        margin-bottom: 10px;
        padding: 0 40px;
    }
    #nodeModal #modalDesc {
        font-size: 13.5px;
        padding: 10px 12px;
        max-height: min(46dvh, 300px);
        margin-bottom: 10px;
    }
    .modal-footer {
        font-size: 11px;
        margin-top: 8px;
        padding-top: 8px;
    }
}

/* Landscape on mobile phones (narrow vertical viewport) */
@media (max-height: 500px) and (orientation: landscape) {
    #nodeModal {
        padding: 8px;
    }
    #nodeModal .modal-content {
        max-height: 96dvh;
        max-width: 580px;
        padding: 12px 18px 10px 18px;
    }
    #nodeModal #modalImg {
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        margin: 0 auto 6px auto;
    }
    #nodeModal h2 {
        font-size: 1.1rem;
        margin-bottom: 6px;
    }
    #nodeModal #modalDesc {
        max-height: 44dvh;
        padding: 8px 12px;
        font-size: 13px;
        margin-bottom: 6px;
    }
    .modal-footer {
        margin-top: 6px;
        padding-top: 6px;
        font-size: 10.5px;
    }
}