/* Apply global box-sizing for more predictable dimension management */
* {
    box-sizing: border-box;
}

/* New background styles, inspired by main.css */
body {
    font-family: sans-serif;
    margin: 0;
    display: flex;
    flex-direction: column; /* Stack elements vertically */
    min-height: 100vh; /* Ensure the body occupies the entire viewport height */
    overflow-x: hidden; /* Prevent horizontal scrolling */
    background-color: #2e3141; /* Solid background color */
    /* Path to the background image. Make sure 'static/images/bg.jpg' is the correct path. */
    background-image: linear-gradient(to top, rgba(46, 49, 65, 0.8), rgba(46, 49, 65, 0.8)), url('static/images/bg.jpg'); /* Corrected path */
    background-size: cover; /* Cover the entire area */
    background-position: center center; /* Center the image */
    background-attachment: fixed; /* Fix the background image on scroll */
    color: #ffffff; /* Default text color for readability */
}

/* Media queries for responsiveness */
@media screen and (max-width: 1280px) {
    body {
        background-size: cover; /* Ensure the image continues to cover */
    }
}

@media screen and (max-width: 980px) {
    body {
        background-size: cover;
    }
}

/* Styles for page-wrapper to manage fixed header space */
#page-wrapper {
    display: flex;
    flex-direction: column;
    min-height: 100vh; /* Ensure page-wrapper fills the viewport height */
    /* Add padding-top to make room for the fixed header. Adjust value if header has different height. */
    padding-top: 60px; /* Estimated header height */
}

/* NEW: Main container for graph and chat */
#main-content {
    flex-grow: 1; /* Allow this div to occupy available space */
    position: relative; /* Positioning context for its absolutely positioned children */
    width: 100%;
    /* Its height will be viewport height minus header and footer */
    height: calc(100vh - 60px - 10vh); /* 60px for header, 10vh for footer (estimated) */
    overflow: hidden; /* Ensure nothing overflows from this container */
}

/* Styles for graph-container (now child of #main-content) */
#graph-container {
    position: absolute; /* Absolutely positioned relative to #main-content */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%; /* Occupy entire height of #main-content */
    z-index: 1; /* Z-index for graph - will be underneath (or on the same layer as) chat */
    overflow: hidden;
    pointer-events: auto; /* Allows interaction with #graph-container and its contents */
}

svg {
    display: block;
    width: 100%;
    height: 100%;
    cursor: grab;
    pointer-events: auto; /* VERY IMPORTANT: Ensure the SVG itself is interactive (for node dragging/clicking) */
}

svg:active {
    cursor: grabbing;
}

.links path {
    stroke-opacity: 0.6;
    fill: none;
}

.nodes circle {
    stroke: none;
    cursor: pointer;
}

.nodes text {
    font-size: 15px;
    fill: #ffffff;
    pointer-events: none; /* Node text should not block clicks on nodes */
    text-anchor: middle;
    dominant-baseline: central;
    paint-order: stroke;
    stroke: #333;
    stroke-width: 2.5px;
    stroke-linejoin: round;
}

.tooltip {
    position: absolute;
    text-align: center;
    padding: 6px;
    border: 0px;
    border-radius: 8px;
    pointer-events: none;
    opacity: 0;
    transition-property: opacity;
    z-index: 10;
    max-width: 200px; /* Makes tooltip narrower */
    word-wrap: break-word; /* Ensures text wraps */
}

/* Styles for modal window */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000; /* Very high z-index for overlay */
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

.node-modal {
    position: relative;
    background-color: white; /* Color will be set dynamically */
    border-radius: 20px; /* Increased border-radius for a softer look */
    width: 400px; /* Fixed width, adjustable */
    height: auto; /* Auto height for content */
    max-height: 80vh; /* Max height to prevent overflow on smaller screens */
    display: flex;
    flex-direction: column;
    justify-content: flex-start; /* Align content to the top */
    align-items: center;
    padding: 30px; /* Increased padding */
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5); /* Stronger, softer shadow */
    transform: scale(0.8);
    opacity: 0;
    transition: transform 0.3s ease, opacity 0.3s ease;
    color: black; /* Text color, will be adjusted for contrast */
    text-align: center;
    overflow: hidden; /* Hide overflow for content */
}

.modal-overlay.active .node-modal {
    transform: scale(1);
    opacity: 1;
}

.node-modal-content {
    flex-grow: 1;
    overflow-y: auto;
    padding: 10px;
    word-wrap: break-word;
    font-size: 16px;
    line-height: 1.5;
    max-height: 100%; /* Use 100% for internal scrolling */
}

.node-modal-close {
    position: absolute;
    top: 15px; /* Adjusted position */
    right: 15px; /* Adjusted position */
    background: none;
    border: none;
    font-size: 28px; /* Slightly larger close icon */
    cursor: pointer;
    color: inherit; /* Inherit color from modal for contrast */
    padding: 5px;
    line-height: 1;
    transition: transform 0.2s ease, color 0.2s ease; /* Smooth transition */
}
.node-modal-close:hover {
    transform: rotate(90deg); /* Rotate on hover for effect */
    color: #ff0000; /* Red color on hover */
}


/* Chatbot CSS styles */
:root {
    --body-background: #1a1d21;
    --container-background: rgba(33, 37, 43, 0.8);
    --chat-area-background: transparent;
    --input-area-background: transparent;
    --input-box-background: rgba(42, 47, 56, 0.8);
    --input-text: #e0e0e0;
    --general-text-color: #e0e0e0;
    --user-message-background: rgba(0, 123, 255, 0.8);
    --user-message-text: #ffffff;
    --ai-message-background: rgba(68, 74, 84, 0.8);
    --ai-message-text: #e0e0e0;
    --error-message-background: rgba(120, 42, 42, 0.9);
    --error-message-text: #ffdddd;
    --send-button-background: rgba(106, 27, 154, 0.9);
    --send-button-background-hover: rgba(123, 31, 162, 1);
    --send-button-text: #ffffff;
    --border-color: rgba(85, 85, 85, 0.7);
    --scrollbar-thumb: #555962;
    --scrollbar-track: rgba(0, 0, 0, 0.1);
    --scrollbar-thumb-hover: #666;
}

/* Styles for #chat (now sibling of #graph-container) */
#chat {
    position: absolute; /* Absolutely positioned relative to #main-content */
    top: 25%; /* Chat starts at 25% of parent container height */
    left: 0;
    right: 0;
    width: 100%;
    bottom: 70px; /* Space for input area */
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    overflow-y: auto; /* Enables vertical scrolling */
    z-index: 10 !important; /* Z-index for #chat: above the graph (#graph-container has z-index: 1) */
    padding: 20px 50px;
    padding-bottom: 10px;
    /* THIS IS THE KEY LINE FOR "PASSTHROUGH": */
    pointer-events: none !important; /* Makes #chat transparent to events.
                                       Events pass through to the graph underneath,
                                       EXCEPT for CHILD elements within #chat that have 'pointer-events: auto;'. */
}

/* Webkit scrollbar styles */
#chat::-webkit-scrollbar {
    width: 8px;
}

#chat::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

#chat::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 4px;
}

#chat::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
}

/* General message styles */
.message {
    position: relative; /* Remains relative to allow z-index */
    z-index: 12 !important; /* Messages will appear above nodes,
                               even if #chat has pointer-events: none;.
                               A high z-index on a child works within the parent's context. */
    max-width: calc(100% - 25%);
    padding: 10px 15px;
    border-radius: 18px;
    line-height: 1.45;
    margin-bottom: 12px;
    word-wrap: break-word;
    font-size: 15px;
    opacity: 1;
    transform: translateY(0);
    animation: fadeInUp 0.4s ease forwards;
    align-self: auto;
    background-color: var(--ai-message-background);
    color: var(--ai-message-text);
    font-family: inherit;
    /* VERY IMPORTANT: Messages capture events and allow scrolling. */
    pointer-events: all !important; /* Ensures interaction with messages */
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

/* Message fade-in animation */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* User message styles */
.user-message {
    background: var(--user-message-background);
    color: var(--user-message-text);
    align-self: flex-end; /* User messages to the right */
    margin-left: auto; /* Push to the right */
    border-bottom-right-radius: 5px;
    border-top-right-radius: 18px;
    border-top-left-radius: 18px;
    border-bottom-left-radius: 18px;
}

/* AI message styles */
.ai-message {
    background-color: var(--ai-message-background);
    color: var(--ai-message-text);
    align-self: flex-start; /* AI messages to the left */
    margin-right: auto; /* Push to the left */
    border-bottom-left-radius: 5px;
    border-top-right-radius: 18px;
    border-top-left-radius: 18px;
    border-bottom-right-radius: 18px;
}

/* AI error message styles */
.ai-message.error-message {
    background-color: var(--error-message-background);
    color: var(--error-message-text);
}

/* Input area (text box + button), now sibling of #graph-container and #chat */
.input-area {
    position: absolute; /* Absolutely positioned relative to #main-content */
    bottom: 1%; /* Adjusted from the bottom of the main container */
    left: 50%;
    transform: translateX(-50%);
    width: 50vw;
    padding: 12px 15px;
    background-color: var(--input-area-background);
    flex-shrink: 0;
    border-top: 1px solid var(--border-color);
    z-index: 15 !important; /* Z-index for input area - above chat and graph */
    box-sizing: border-box;
    pointer-events: auto !important; /* VERY IMPORTANT: Allows interaction with the input */
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -5px 10px rgba(0, 0, 0, 0.3);
}

/* Input focus and autofill styles */
input:focus-visible {
    outline: none;
}

input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
    -webkit-background-clip: text;
    -webkit-text-fill-color: var(--input-text);
    transition: background-color 5000s ease-in-out 0s;
    box-shadow: inset 0 0 20px 20px var(--input-box-background);
    font-family: inherit;
}

/* Input row */
.input-row {
    display: flex;
    align-items: center;
}

/* Input box wrapper */
.inputBoxdiv {
    flex-grow: 1;
    padding: 0;
    border-radius: 25px;
    margin-right: 10px;
    border: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    background-color: var(--input-box-background);
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Text input box */
#message {
    color: var(--input-text);
    font-size: 15px;
    border: none;
    width: 100%;
    background-color: transparent;
    padding: 10px 15px;
    flex-grow: 1;
    font-family: inherit;
    pointer-events: auto;
}

/* Send button */
#send {
    text-align: center;
    width: 42px;
    height: 42px;
    background: var(--send-button-background);
    border-radius: 50%;
    border: none;
    cursor: pointer;
    transition: background-color 0.3s ease, transform 0.1s ease, box-shadow 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    position: relative;
    pointer-events: auto;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}

#send:hover {
    background-color: var(--send-button-background-hover);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
    transform: scale(1.05);
}

#send:active {
    transform: scale(0.95);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

#send:disabled {
    background-color: var(--send-button-background);
    cursor: not-allowed;
    transform: scale(1);
    opacity: 0.6;
    box-shadow: none;
}

/* Icons within the button - common positioning */
#send i {
    font-size: 18px;
    color: var(--send-button-text);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* Send icon (paper plane) */
#send-icon {
    display: inline-block;
}

/* Waiting indicator (spinner) */
#waiting-indicator {
    animation: spin 1s linear infinite;
    display: none;
}

/* Spinner animation */
@keyframes spin {
    0% {
        transform: translate(-50%, -50%) rotate(0deg);
    }

    100% {
        transform: translate(-50%, -50%) rotate(360deg);
    }
}

/* Media queries for responsiveness */
@media (max-width: 768px) {
    #main-content {
        height: calc(100vh - 60px - 10vh); /* Maintain height calculation on mobile */
    }

    #chat {
        top: 25%;
        bottom: 70px;
        padding-left: 20px;
        padding-right: 20px;
        padding-top: 10px;
        padding-bottom: 10px;
        pointer-events: none !important; /* Keep #chat transparent to events on mobile */
    }

    /* Specific styles for .input-area on small screens (max-width: 768px) */
    .input-area {
        width: 90vw;
        left: 50%;
        transform: translateX(-50%);
        bottom: 10%;
        padding: 8px 10px;
        pointer-events: auto !important; /* Input interactive on mobile */
        border-radius: 15px 15px 0 0;
    }

    #graph-container {
        height: 100%; /* Occupy 100% of #main-content */
        pointer-events: auto !important; /* Graph interactive on mobile */
    }

    .message {
        max-width: calc(100% - 40px);
        pointer-events: all !important; /* Messages interactive on mobile */
    }

    .node-modal {
        width: 90%;
        height: auto;
        max-height: 80%;
        border-radius: 15px;
        padding: 20px;
    }
}

/* Media query for desktop (screens wider than 769px) */
@media screen and (min-width: 769px) {
    #chat {
        pointer-events: none !important; /* Keep #chat transparent to events on desktop */
    }

    .input-area {
        bottom: 1%;
        width: 50vw;
        max-width: 800px;
        padding: 12px 15px;
        border-radius: 20px;
        box-shadow: 10px 10px 10px 10px rgba(0, 0, 0, 0.3);
        z-index: 15 !important; /* Maintain high z-index for input area on desktop */
        pointer-events: auto !important; /* Input interactive on desktop */
    }

    #graph-container {
        pointer-events: auto !important; /* Graph interactive on desktop */
    }

    .message {
        pointer-events: all !important; /* Messages interactive on desktop */
    }

    /* Ensure #chat has enough space from the bottom */
    #chat {
        bottom: calc(15% + 70px); /* Adjust bottom space for chat on desktop */
    }
}

/* --- Custom Footer Styles for chatbot.css (Override main.css) --- */

#footer {
    /* Reset negative margin-top and ensure correct positioning */
    margin-top: 0 !important;
    position: relative !important; /* Ensure it's positioned correctly and can be controlled */
    width: 100% !important; /* Occupy full available width */

    /* Retain background styles from main.css (these should be fine) */
    background-color: #2e3141;
    background-image: linear-gradient(to top, rgba(46, 49, 65, 0.8), rgba(46, 49, 65, 0.8)), url('static/images/bg.jpg');
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
    color: rgba(255, 255, 255, 0.5);
    text-align: center;

    /* AGGRESSIVE RESET AND HEIGHT CONTROL */
    padding: 0.5em 0 !important; /* Reduce general vertical padding, with !important */
    margin: 0 !important; /* Eliminate margin from #footer completely */

    min-height: 5vh !important; /* Minimum height based on viewport */
    max-height: 10vh !important; /* Set a maximum height of 10% of viewport height */

    /* Use flexbox to center content vertically and horizontally */
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    overflow: hidden !important; /* Ensure content does not overflow max-height */
}

#footer .inner {
    /* Reset padding and margin of the .inner element within the footer */
    padding: 0.25em 0.5em !important; /* Minimal padding, with !important */
    margin: 0 !important; /* Eliminate margin completely */

    max-width: 75em; /* Retain max-width */
    width: 100%; /* Occupy full available width within the footer */
    display: block; /* Retain block behavior */
}

#footer .inner .copyright {
    /* Reset margin and padding of the copyright list */
    margin: 0 !important; /* Eliminate margins completely */
    padding: 0 !important; /* Eliminate padding completely */

    font-size: 0.7em !important; /* Reduce font size, with !important */
    opacity: 0.75 !important; /* Retain opacity */
    text-align: center !important; /* Retain text alignment */
    list-style: none !important; /* Retain no list bullets */
}

/* --- Footer styles for mobile (Aggressively override main.css) --- */
@media screen and (max-width: 980px) {
    #footer {
        /* Reapply resets and height limits for mobile */
        max-height: 10vh !important; /* Ensure max-height of 10% on mobile */
        min-height: auto !important; /* Let height adapt to content on mobile (if less than 10vh) */
        padding: 0.1em 0 !important; /* Reduce vertical padding to a minimum, with !important */
    }

    #footer .inner {
        padding: 0.05em 0.1em !important; /* Further reduce inner padding on mobile, with !important */
    }

    #footer .inner .copyright {
        margin: 0 !important; /* Eliminate margins completely on mobile */
        font-size: 0.6em !important; /* Further reduce font size on mobile, with !important */
    }
}
