/* ===================== CSS VARIABLES ===================== */
:root {
    /* Panel dimensions */
    --right-panel-width: 0px;
    --sidebar-width-desktop: 400px;
    --sidebar-width-mobile: 100vw;
    --sidebar-min-width: 400px;
    --sidebar-max-width: 700px;

    /* Main content dimensions */
    --main-content-width: 50vw;
    --main-content-offset: 200px;
    --mobile-content-width: 95vw;

    /* Thread list styling */
    --thread-button-min-height: 50px;
    --thread-button-max-height: 60px;
    --thread-timestamp-size: 0.75rem;
    --thread-name-line-clamp: 2;

    /* Border radius */
    --border-radius-default: 15px;
    --border-radius-message: 20px;
    --border-radius-message-tail: 4px;
    --border-radius-scrollbar: 6px;

    /* Icon sizes */
    --icon-size-default: 24px;
    --icon-size-small: 22px;
    --icon-size-large: 26px;

    /* Scrollbar */
    --scrollbar-width: 12px;

    /* Spacing */
    --message-composer-margin-top: 10px;

    /* Transitions */
    --transition-default: 0.3s ease-in-out;
    --transition-highlight: 2s ease-out;

    /* Watermark */
    --watermark-width: 200px;
    --watermark-height: 60px;
    --watermark-opacity: 0.9;
}

/* ===================== HIDE ELEMENTS ===================== */
/* Hide Readme button */
#readme-button {
    display: none !important;
}

/* Hide assistant avatar image */
img.aspect-square.h-full.w-full.bg-transparent {
    display: none !important;
}

/* Style chat profiles button with smaller, non-bold font */
#chat-profiles {
    font-weight: normal !important;
    font-size: 1rem !important;
}

/* Make logo bigger */
img.logo {
    width: 400px !important;
}

/* Hide logo container on login page */
.flex.justify-center.gap-2.md\:justify-start:has(img.logo) {
    display: none !important;
}

/* Increase height of menu buttons and allow 2 lines for thread name */
[data-sidebar="menu-button"] {
    height: auto !important;
    min-height: var(--thread-button-min-height) !important;
    max-height: var(--thread-button-max-height) !important;
    white-space: normal !important;
    line-height: 1.3 !important;
    overflow: hidden !important;
    display: flex !important;
    align-items: center !important;
    padding: 0.5rem !important;
    border-bottom: 1px solid rgba(150, 150, 150, 0.2) !important;
    border-radius: 0 !important;
    margin-bottom: 0 !important;
    color: transparent !important;
    background-color: transparent !important;
}

/* Once formatted by JS, the color will be set inline */
[data-sidebar="menu-button"][data-timestamp-formatted] {
    color: inherit !important;
}



/* Add slight hover effect to make threads more interactive */
[data-sidebar="menu-button"]:hover {
    background-color: hsl(var(--primary) / 0.05) !important;
    border-radius: 0 !important;
}

/* Style for thread timestamp (will be added by JavaScript) */
.thread-timestamp {
    font-weight: 700 !important;
    font-size: var(--thread-timestamp-size) !important;
    color: hsl(var(--primary)) !important;
    opacity: 1 !important;
    min-width: fit-content !important;
    max-width: fit-content !important;
    flex-shrink: 0 !important;
    text-align: center !important;
    line-height: 1.1 !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    padding: 0 !important;
    margin: 0 !important;
    gap: 0.1rem !important;
}

/* White shadow for light theme */
[data-theme="light"] .thread-timestamp,
.light .thread-timestamp,
:root:not(.dark) .thread-timestamp {
    text-shadow: 0 1px 3px rgba(255, 255, 255, 0.8), 0 1px 2px rgba(255, 255, 255, 0.6) !important;
}

/* Dark shadow for dark theme */
[data-theme="dark"] .thread-timestamp,
.dark .thread-timestamp {
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2) !important;
}

/* Style for date/time lines within timestamp */
.thread-timestamp-line {
    white-space: nowrap !important;
    font-size: var(--thread-timestamp-size) !important;
}

/* Style for thread separator */
.thread-separator {
    color: #9ca3af !important;
    font-weight: 300 !important;
    padding: 0 0.25rem !important;
    margin: 0 !important;
    opacity: 0.8 !important;
    flex-shrink: 0 !important;
    align-self: stretch !important;
    display: flex !important;
    align-items: center !important;
}

/* Style for thread item wrapper */
.thread-item-wrapper {
    display: flex !important;
    align-items: center !important;
    width: 100% !important;
    gap: 0 !important;
    padding-right: 1.2rem !important;
}

/* Style for formatted thread buttons */
.thread-button-formatted {
    position: relative !important;
    color: inherit !important;
}

/* Style for thread name with 2 lines support */
.thread-name {
    font-weight: 400 !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    flex: 1 !important;
    min-width: 0 !important;
    display: -webkit-box !important;
    -webkit-line-clamp: var(--thread-name-line-clamp) !important;
    -webkit-box-orient: vertical !important;
    line-height: 1.4 !important;
    white-space: normal !important;
}

/* Center the thread options button vertically */
#thread-options {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    margin: auto 0 !important;
}

/* Make the left sidebar (thread history) wider - 40% of screen width */
[data-test="thread-history-sidebar"],
aside[class*="sidebar"],
nav[class*="sidebar"],
div[class*="Sidebar"],
[class*="MuiDrawer"] > div,
aside {
    width: 40vw !important;
    min-width: var(--sidebar-min-width) !important;
    max-width: var(--sidebar-max-width) !important;
}

/* Adjust for smaller screens */
@media (max-width: 1200px) {
    [data-test="thread-history-sidebar"],
    aside[class*="sidebar"],
    nav[class*="sidebar"],
    div[class*="Sidebar"],
    [class*="MuiDrawer"] > div,
    aside {
        width: 45vw !important;
        min-width: 350px !important;
    }
}

@media (max-width: 768px) {
    [data-test="thread-history-sidebar"],
    aside[class*="sidebar"],
    nav[class*="sidebar"],
    div[class*="Sidebar"],
    [class*="MuiDrawer"] > div,
    aside {
        width: var(--sidebar-width-mobile) !important;
        min-width: auto !important;
        max-width: var(--sidebar-width-mobile) !important;
    }
}

/* Fix the content margin so it doesn't get hidden behind the wider sidebar */
#root > div > div {
    margin-left: 0px;
}

/* Add right margin to main content container to make space for right panel */
#root {
    margin-right: var(--right-panel-width) !important;
    transition: var(--transition-default) !important;
}

/* Override the message composer parent container width to adapt to right panel */
.flex.flex-col.mx-auto.w-full.p-4.pt-0 {
    max-width: min(var(--main-content-width), calc(100vw - var(--right-panel-width) - var(--main-content-offset))) !important;
    transition: var(--transition-default) !important;
}

/* Override the messages/steps container width to adapt to right panel */
.flex.flex-col.mx-auto.w-full.flex-grow.p-4 {
    max-width: min(var(--main-content-width), calc(100vw - var(--right-panel-width) - var(--main-content-offset))) !important;
    transition: var(--transition-default) !important;
}

/* Override individual step containers width to adapt to right panel */
.step .flex.flex-col {
    max-width: min(var(--main-content-width), calc(100vw - var(--right-panel-width) - var(--main-content-offset))) !important;
    transition: var(--transition-default) !important;
}

/* Make message composer container follow parent width */
#message-composer {
    max-width: 100% !important;
    /*border-radius: var(--border-radius-default) !important;*/
    margin-top: var(--message-composer-margin-top) !important;
}

/* Also apply border-radius to the textarea and input elements inside */

/* Hide scrollbar in chat and right panel while keeping scroll functionality */
.flex.flex-col.mx-auto.w-full.flex-grow.p-4,
.overflow-y-auto,
#root {
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE and Edge */
}

.flex.flex-col.mx-auto.w-full.flex-grow.p-4::-webkit-scrollbar,
.overflow-y-auto::-webkit-scrollbar,
#root::-webkit-scrollbar {
    display: none; /* Chrome, Safari, Opera */
}

/* Mobile styles - Right panel takes full screen on mobile */
@media (max-width: 768px) {
    /* No margin adjustment on mobile - panel overlays content */
    #root {
        margin-right: 0 !important;
    }

    /* Full width for containers on mobile */
    .flex.flex-col.mx-auto.w-full.p-4.pt-0 {
        max-width: var(--mobile-content-width) !important;
    }

    .flex.flex-col.mx-auto.w-full.flex-grow.p-4 {
        max-width: var(--mobile-content-width) !important;
    }

    .step .flex.flex-col {
        max-width: var(--mobile-content-width) !important;
    }
}

/* Make citation references clickable */
[data-citation] {
    cursor: pointer !important;
    color: hsl(var(--primary)) !important;
    font-weight: 600 !important;
    transition: all 0.2s ease;
    text-decoration: none;
    border-bottom: 1px solid transparent;
}

.message-content [data-citation]:hover {
    border-bottom-color: hsl(var(--primary));
    opacity: 0.8;
}

.message-content [data-citation]:active {
    transform: scale(0.95);
}

/* Highlighted quote with primary color gradient transition */
.quote-highlighted {
    position: relative;
    overflow: hidden;
    animation: fadeHighlight var(--transition-highlight) ease-out forwards;
}

.quote-highlighted::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
        135deg,
        hsl(var(--primary) / 0.4) 0%,
        hsl(var(--primary) / 0.35) 25%,
        hsl(var(--primary) / 0.3) 50%,
        hsl(var(--primary) / 0.25) 75%,
        hsl(var(--primary) / 0.15) 100%
    );
    background-size: 200% 200%;
    border-radius: inherit;
    pointer-events: none;
    z-index: 0;
    animation: fadeAndShift var(--transition-highlight) ease-out forwards;
}

.quote-highlighted > * {
    position: relative;
    z-index: 1;
}

/* Ring border that fades */
.quote-highlighted {
    box-shadow:
        0 0 0 2px hsl(var(--primary) / 0.6),
        0 0 20px hsl(var(--primary) / 0.4);
    animation: fadeHighlight var(--transition-highlight) ease-out forwards;
}

@keyframes fadeAndShift {
    0% {
        background-position: 0% 50%;
        opacity: 1;
    }
    50% {
        background-position: 100% 50%;
        opacity: 0.8;
    }
    100% {
        background-position: 200% 50%;
        opacity: 0;
    }
}

@keyframes fadeHighlight {
    0% {
        box-shadow:
            0 0 0 2px hsl(var(--primary) / 0.8),
            0 0 25px hsl(var(--primary) / 0.5);
    }
    100% {
        box-shadow:
            0 0 0 0 hsl(var(--primary) / 0),
            0 0 0 hsl(var(--primary) / 0);
    }
}

/* Style for Fuentes button - rounded only on left corners */
button[aria-label="Mostrar fuentes"],
button[aria-label="Show sources"],
button[aria-label="Ocultar fuentes"],
button[aria-label="Hide sources"] {
    border-top-left-radius: var(--radius, 15px) !important;
    border-bottom-left-radius: var(--radius, 15px) !important;
    border-top-right-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
}

/* Replace Chainlit watermark text with logo based on theme */
.watermark {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: var(--watermark-width) !important;
    height: var(--watermark-height) !important;
    background-repeat: no-repeat !important;
    background-position: center !important;
    background-size: contain !important;
    opacity: var(--watermark-opacity) !important;
}

/* Light theme - use logo_dark.png for better contrast */
[data-theme="light"] .watermark,
.light .watermark,
:root:not(.dark) .watermark {
    background-image: url('/public/logo_dark_chat.png') !important;
}

/* Dark theme - use logo_light.png for better contrast */
[data-theme="dark"] .watermark,
.dark .watermark {
    background-image: url('/public/logo_dark_chat.png') !important;
}


/* Icon replacing */
/* Hide the original "new thread" icon */
/* Remove default button backgrounds and focus states */
#new-chat-button svg,
#thread-options {
    background-color: transparent !important;
}

#new-chat-button svg,
#search-chats-button svg,
#sidebar-trigger-button svg,
#theme-toggle svg,
#chat-submit svg,
#upload-button svg,
#thread-options svg{
    display: none !important;
}

/* Match dark/light theme */
.dark #search-chats-button::before,
.dark #sidebar-trigger-button::before,
.dark #theme-toggle::before,
.dark #chat-submit::before,
.dark #upload-button::before,
.dark #thread-options::before{
    filter: brightness(0) saturate(100%) invert(100%);
}

.dark #new-chat-button::before {
    filter : invert(10%);
}

/* Add custom icon*/
#new-chat-button::before {
    content: '';
    display: inline-block;
    width: var(--icon-size-default);
    height: var(--icon-size-default);
    mask-image: url('/public/icons/add-circle-rounded.svg');
    mask-size: contain;
    mask-position: center;
    mask-repeat: no-repeat;
    background-color: hsl(var(--primary));  /* Uses Chainlit's primary/accent color */

}

#search-chats-button::before {
    content: '';
    display: inline-block;
    width: var(--icon-size-default);
    height: var(--icon-size-small);
    background-image: url('/public/icons/search-rounded.svg');
    background-size: contain;
    background-position: center;
    filter: brightness(0) saturate(100%) invert(48%) sepia(8%) saturate(430%) hue-rotate(182deg);
}

#sidebar-trigger-button::before {
    content: '';
    display: inline-block;
    width: var(--icon-size-default);
    height: var(--icon-size-small);
    background-image: url('/public/icons/side-navigation-sharp.svg');
    background-size: contain;
    background-position: center;
    filter: brightness(0) saturate(100%) invert(48%) sepia(8%) saturate(430%) hue-rotate(182deg);
}

#theme-toggle::before {
    content: '';
    display: inline-block;
    width: var(--icon-size-default);
    height: var(--icon-size-small);
    background-image: url('/public/icons/moon-stars-outline.svg');
    background-size: contain;
    background-position: center;
    filter: brightness(0) saturate(100%) invert(15%) sepia(8%) saturate(430%) hue-rotate(182deg);

}

#chat-submit::before {
    content: '';
    display: inline-block;
    width: var(--icon-size-large);
    height: var(--icon-size-large);
    background-image: url('/public/icons/send-rounded.svg');
    background-size: contain;
    background-position: center;
    filter: brightness(0) saturate(100%) invert(100%) sepia(8%) saturate(430%) hue-rotate(182deg);
}

#upload-button::before {
    content: '';
    display: inline-block;
    width: var(--icon-size-large);
    height: var(--icon-size-default);
    background-image: url('/public/icons/drive-folder-upload-rounded.svg');
    background-size: contain;
    background-position: center;
    filter: brightness(0) saturate(100%) invert(48%) sepia(8%) saturate(430%) hue-rotate(182deg);
}

#thread-options::before {
    content: '';
    display: inline-block;
    width: var(--icon-size-small);
    height: var(--icon-size-small);
    background-image: url('/public/icons/arrow-circle-left.svg');
    background-size: contain;
    background-position: center;
    filter: brightness(0) saturate(100%) invert(48%) sepia(8%) saturate(430%) hue-rotate(182deg);
}




/*SET LEFT PANEL WIDTH*/

/* Set custom width variable for desktop */
[data-sidebar="sidebar"] {
    --sidebar-width: var(--sidebar-width-desktop) !important;
}

/* Apply ONLY when expanded */
.group.peer[data-state="expanded"] > div {
    width: var(--sidebar-width-desktop) !important;
}

.group.peer[data-state="expanded"] > div > div {
    width: var(--sidebar-width-desktop) !important;
}

/* Leave collapsed state alone */
.group.peer[data-state="collapsed"] > div,
.group[data-collapsible="icon"] > div {
    width: var(--sidebar-width-icon) !important;
}

/* Mobile: full width */
@media (max-width: 768px) {
    [data-sidebar="sidebar"] {
        --sidebar-width: var(--sidebar-width-mobile) !important;
    }

    .group.peer[data-state="expanded"] > div {
        width: var(--sidebar-width-mobile) !important;
    }

    .group.peer[data-state="expanded"] > div > div {
        width: var(--sidebar-width-mobile) !important;
    }
}




/* Change scrollbar color*/
.dark ::-webkit-scrollbar {
    width: var(--scrollbar-width);
    height: var(--scrollbar-width);
}

.dark ::-webkit-scrollbar-track {
    background: hsl(var(--background));
}

.dark ::-webkit-scrollbar-thumb {
    background: hsl(var(--muted-foreground) / 0.3);
    border-radius: var(--border-radius-scrollbar);
}

.dark ::-webkit-scrollbar-thumb:hover {
    background: hsl(var(--muted-foreground) / 0.5);
}

/* Firefox */
.dark * {
    scrollbar-width: thin;
    scrollbar-color: hsl(var(--muted-foreground) / 0.3) hsl(var(--background));
}

/* User message bubble */
/* Modern speech bubble */
[data-step-type="user_message"] .bg-accent.rounded-3xl {
    border-radius: var(--border-radius-message) var(--border-radius-message) var(--border-radius-message-tail) var(--border-radius-message) !important;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

[data-step-type="assistant_message"] .bg-muted.rounded-3xl {
    border-radius: var(--border-radius-message) var(--border-radius-message) var(--border-radius-message) var(--border-radius-message-tail) !important;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}




/* ------------- */



/* CHAINLIT STEPS */

/*Hide avatar*/
.animate-pulse.bg-muted.h-full.w-full.rounded-full {
    display: none;
}

/* =================== QUOTA BAR =================== */

/* Ensure the quota bar and send button share a flex row */
*:has(> #quota-bar-container) {
    display: flex !important;
    align-items: center !important;
    flex-wrap: nowrap !important;
}

#quota-bar-container {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 8px;
    flex-shrink: 0;
    height: 36px;
}

.quota-track {
    width: 70px;
    height: 5px;
    border-radius: 3px;
    background: hsl(var(--muted));
    overflow: hidden;
    flex-shrink: 0;
}

.quota-fill {
    height: 100%;
    background: hsl(var(--primary));
    border-radius: 3px;
    transition: width 0.4s ease;
}

.quota-fill--full {
    background: hsl(var(--destructive));
}

.quota-count {
    font-size: 0.68rem;
    color: hsl(var(--muted-foreground));
    white-space: nowrap;
    flex-shrink: 0;
    line-height: 1;
}

.quota-tooltip-wrap {
    position: relative;
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;
}

.quota-tooltip-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 13px;
    height: 13px;
    border-radius: 50%;
    border: 1px solid hsl(var(--muted-foreground) / 0.5);
    color: hsl(var(--muted-foreground));
    font-size: 0.6rem;
    font-weight: bold;
    cursor: help;
    user-select: none;
    line-height: 1;
}

.quota-tooltip-popup {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    background: hsl(var(--popover));
    color: hsl(var(--popover-foreground));
    border: 1px solid hsl(var(--border));
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 0.75rem;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s ease;
    z-index: 9999;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
}

.quota-tooltip-wrap:hover .quota-tooltip-popup {
    opacity: 1;
}

/* =================== HIDE ELEMENT SIDEBAR =================== */
/* Target the exact Chainlit ElementSidebar using its known IDs */
#side-view-title,
#side-view-content,
aside:has(#side-view-title),
div[data-panel]:has(#side-view-title) {
    display: none !important;
}
