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;
}

/* ==========================================================
   Edge & Link Interactive Styles
   ========================================================== */

.link-item {
    cursor: pointer;
}

.link-hitarea {
    stroke: transparent;
    stroke-width: 16px;
    cursor: pointer;
    pointer-events: stroke;
}

.link-visual {
    transition: stroke 0.2s ease, stroke-width 0.2s ease, stroke-opacity 0.2s ease;
    pointer-events: none;
}

.link-item:hover .link-visual {
    stroke: #38bdf8 !important;
    stroke-opacity: 1 !important;
    stroke-width: 3.5px !important;
    filter: drop-shadow(0 0 8px rgba(56, 189, 248, 0.75));
}

.link-item.active .link-visual {
    stroke: #38bdf8 !important;
    stroke-opacity: 1 !important;
    stroke-width: 4px !important;
}

/* ==========================================================
   Modal Dialogs (#nodeModal & #edgeModal)
   Responsive, glassmorphic executive design
   ========================================================== */

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

#nodeModal,
#edgeModal {
    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,
#edgeModal .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,
#edgeModal .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,
#edgeModal .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,
#edgeChatgptBtn {
    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,
#edgeChatgptBtn img {
    width: 18px;
    height: 18px;
    border-radius: 4px;
    object-fit: cover;
    margin: 0;
}

#chatgptBtn:hover,
#edgeChatgptBtn: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 10px 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 12px 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;
}

/* ==========================================================
   Neighbor Selection Section inside #nodeModal
   ========================================================== */

.modal-neighbors-section {
    margin-bottom: 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-shrink: 0;
}

.modal-section-title {
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #94a3b8;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 4px;
}

#modalNeighborsCount {
    color: #38bdf8;
}

.modal-neighbors-list {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 2px 2px 6px 2px;
    scrollbar-width: thin;
    overscroll-behavior-x: contain;
}

.modal-neighbors-list::-webkit-scrollbar {
    height: 4px;
}

.modal-neighbors-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 4px;
}

.neighbor-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px 4px 6px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: #e2e8f0;
    font-size: 12px;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    user-select: none;
}

.neighbor-chip:hover {
    background: rgba(56, 189, 248, 0.15);
    border-color: rgba(56, 189, 248, 0.4);
    color: #38bdf8;
    transform: translateY(-1px);
}

.neighbor-chip.active {
    background: rgba(56, 189, 248, 0.25);
    border-color: #38bdf8;
    color: #fff;
    box-shadow: 0 0 10px rgba(56, 189, 248, 0.35);
}

.neighbor-chip-img {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    object-fit: cover;
    background: #1e293b;
    border: 1px solid rgba(255, 255, 255, 0.15);
}

.neighbor-chip-name {
    font-weight: 500;
}

.neighbor-chip-count {
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 8px;
    background: rgba(0, 0, 0, 0.35);
    color: #94a3b8;
    font-weight: 600;
}

.neighbor-chip.active .neighbor-chip-count {
    background: rgba(56, 189, 248, 0.45);
    color: #fff;
}

/* Relation Banner when viewing a neighbor relationship inside nodeModal */
.modal-relation-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 12px;
    margin-bottom: 10px;
    border-radius: 10px;
    background: linear-gradient(90deg, rgba(56, 189, 248, 0.15), rgba(99, 102, 241, 0.15));
    border: 1px solid rgba(56, 189, 248, 0.3);
    animation: modalPopIn 0.2s ease-out;
    flex-shrink: 0;
}

.modal-relation-text {
    font-size: 12px;
    font-weight: 600;
    color: #7dd3fc;
    display: flex;
    align-items: center;
    gap: 6px;
}

.modal-relation-back-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #e2e8f0;
    border-radius: 6px;
    font-size: 11px;
    padding: 3px 8px;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.modal-relation-back-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

/* ==========================================================
   Edge Modal Header & Elements (#edgeModal)
   ========================================================== */

.edge-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 8px 0 14px 0;
    padding: 12px 14px;
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 14px;
    flex-shrink: 0;
}

.edge-entity-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    flex: 1;
    min-width: 0;
}

.edge-entity-img {
    width: 58px;
    height: 58px;
    min-width: 58px;
    min-height: 58px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(56, 189, 248, 0.6);
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
    margin-bottom: 6px;
    background: #1e293b;
    transition: transform 0.2s ease;
}

.edge-entity-img:hover {
    transform: scale(1.05);
}

.edge-entity-name {
    font-size: 13px;
    font-weight: 600;
    color: #f1f5f9;
    max-width: 140px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.edge-connector-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.edge-connector-arrow {
    font-size: 20px;
    color: #38bdf8;
    letter-spacing: -2px;
    font-weight: bold;
    text-shadow: 0 0 10px rgba(56, 189, 248, 0.5);
}

.edge-weight-badge {
    font-size: 11px;
    padding: 2px 8px;
    background: rgba(56, 189, 248, 0.15);
    border: 1px solid rgba(56, 189, 248, 0.3);
    color: #7dd3fc;
    border-radius: 999px;
    font-weight: 600;
    white-space: nowrap;
}

/* ==========================================================
   Description Area (Markdown Content for Node & Edge Modals)
   ========================================================== */

#nodeModal #modalDesc,
#edgeModal #edgeModalDesc {
    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,
#edgeModal #edgeModalDesc::-webkit-scrollbar {
    width: 6px;
}

#nodeModal #modalDesc::-webkit-scrollbar-track,
#edgeModal #edgeModalDesc::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.04);
    border-radius: 4px;
}

#nodeModal #modalDesc::-webkit-scrollbar-thumb,
#edgeModal #edgeModalDesc::-webkit-scrollbar-thumb {
    background: rgba(56, 189, 248, 0.35);
    border-radius: 4px;
}

#nodeModal #modalDesc::-webkit-scrollbar-thumb:hover,
#edgeModal #edgeModalDesc::-webkit-scrollbar-thumb:hover {
    background: rgba(56, 189, 248, 0.65);
}

/* Markdown elements inside #modalDesc & #edgeModalDesc */
#nodeModal #modalDesc h1,
#nodeModal #modalDesc h2,
#nodeModal #modalDesc h3,
#nodeModal #modalDesc h4,
#edgeModal #edgeModalDesc h1,
#edgeModal #edgeModalDesc h2,
#edgeModal #edgeModalDesc h3,
#edgeModal #edgeModalDesc 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,
#edgeModal #edgeModalDesc h1:first-child,
#edgeModal #edgeModalDesc h2:first-child,
#edgeModal #edgeModalDesc h3:first-child {
    margin-top: 0;
}

#nodeModal #modalDesc h1,
#edgeModal #edgeModalDesc h1 {
    font-size: 19px;
}

#nodeModal #modalDesc h2,
#edgeModal #edgeModalDesc h2 {
    font-size: 17px;
}

#nodeModal #modalDesc h3,
#edgeModal #edgeModalDesc h3 {
    font-size: 15.5px;
    color: #38bdf8;
    border-bottom: 1px solid rgba(56, 189, 248, 0.2);
    padding-bottom: 4px;
}

#nodeModal #modalDesc h4,
#edgeModal #edgeModalDesc h4 {
    font-size: 14px;
}

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

#nodeModal #modalDesc p:last-child,
#edgeModal #edgeModalDesc p:last-child {
    margin-bottom: 0;
}

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

#nodeModal #modalDesc em,
#edgeModal #edgeModalDesc em {
    color: #94a3b8;
}

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

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

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

#nodeModal #modalDesc blockquote,
#edgeModal #edgeModalDesc 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,
#edgeModal #edgeModalDesc a {
    color: #38bdf8;
    text-decoration: underline;
    text-underline-offset: 3px;
}

#nodeModal #modalDesc a:hover,
#edgeModal #edgeModalDesc a:hover {
    color: #7dd3fc;
}

#nodeModal #modalDesc code,
#edgeModal #edgeModalDesc 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,
    #edgeModal .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,
    #edgeModal {
        padding: 12px;
    }

    #nodeModal .modal-content,
    #edgeModal .modal-content {
        width: 100%;
        padding: 18px 14px 14px 14px;
        border-radius: 16px;
        max-height: calc(100dvh - 24px);
    }

    #nodeModal .close-btn,
    #edgeModal .close-btn {
        top: 12px;
        right: 12px;
        width: 30px;
        height: 30px;
        font-size: 18px;
    }

    #chatgptBtn,
    #edgeChatgptBtn {
        top: 12px;
        right: 48px;
        width: 30px;
        height: 30px;
    }

    #chatgptBtn img,
    #edgeChatgptBtn 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,
    #edgeModal #edgeModalDesc {
        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,
    #edgeModal {
        padding: 8px;
    }

    #nodeModal .modal-content,
    #edgeModal .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,
    #edgeModal #edgeModalDesc {
        max-height: 44dvh;
        padding: 8px 12px;
        font-size: 13px;
        margin-bottom: 6px;
    }

    .modal-footer {
        margin-top: 6px;
        padding-top: 6px;
        font-size: 10.5px;
    }
}