/* Глобальні CSS-змінні для узгоджених відступів/розмірів */
:root {
    --header-h: 64px;
    --sidebar-w: 320px;
    --toolbar-gap: 0.75rem;

    --brand-color: #362460;  
    --brand-color-75: #362460BF;  
    --brand-color-50: #36246080;  
    --brand-color-25: #36246040; 

    --brand-color-hover: #4a2c6c;  
    --main-bg-color: #f5f5f5;
    --secondary-bg-color: #ebebeb;

    --border-color: #36246050;
    --main-text-color: #111;
    --secondary-text-color: #6c6c6c;

    --main-btn-bg-color: #362460;
    --main-btn-text-color: #ffffff;
    --secondary-btn-text-color: #d8d8d8;
}


/* Кнопка чату в правому нижньому куті зони 3D */
.chat-fab-button {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 60px;
    height: 60px;
    z-index: 1350;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: all 0.3s ease;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--brand-color);
    color: var(--main-btn-text-color);
    border: none;
    border-radius: 50%;
}

/* Приховування кнопки чату коли чат відкритий */
.chat-fab-button.hidden {
    opacity: 0;
    visibility: hidden;
    transform: scale(0.8);
}

.chat-fab-button:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
    background: var(--brand-color-hover);
}

.chat-fab-button .material-symbols-rounded {
    font-size: 28px;
    position: relative;
    z-index: 1;
}

/* Анімація хвиль для кнопки чату */
.chat-fab-button::before,
.chat-fab-button::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: var(--brand-color);
    animation: wave 2.5s infinite ease-out;
    z-index: 0;
}

.chat-fab-button::after {
    animation-delay: 1.25s;
}

@keyframes wave {
    0% {
        transform: scale(1);
        opacity: 0.7;
    }
    80% {
        transform: scale(1.5);
        opacity: 0;
    }
    100% {
        transform: scale(1.5);
        opacity: 0;
    }
}

/* Адаптивність для мобільних пристроїв */
@media (max-width: 768px) {
    .chat-fab-button {
        bottom: 10px;
        right: 10px;
        width: 56px;
        height: 56px;
    }
    
    .chat-fab-button .material-symbols-rounded {
        font-size: 24px;
    }
}

/* Додаткові стилі для забезпечення видимості кнопки на всіх розмірах екрану */
@media (min-width: 769px) {
    .chat-fab-button {
        bottom: 20px;
        right: 20px;
    }
}

/* Забезпечення що кнопка завжди залишається в межах viewport */
.chat-fab-button {
    /* Максимальні значення для запобігання виходу за межі екрану */
    bottom: max(10px, min(calc(100vh - 80px), 20px));
    right: max(10px, min(calc(100vw - 80px), 20px));
}

/* Адаптація позиції кнопки при відкритих сайдбарах на великих екранах */
@media (min-width: 992px) {
    .app-shell.pad-right .chat-fab-button {
        right: calc(var(--sidebar-w) + max(20px, env(safe-area-inset-right, 0) + 20px));
    }
}

/* Контейнер чату в правому нижньому кутку */
.chat-bot-container {
    position: fixed;
    bottom: 90px;
    right: 20px;
    width: 380px;
    height: 500px;
    background: var(--main-bg-color);
    border: 1px solid var(--border-color);
    border-radius: 1rem;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    z-index: 1340;
    display: flex;
    flex-direction: column;
    transition: all 0.3s ease;
    /* Забезпечуємо що контейнер не виходить за межі viewport */
        max-height: calc(100vh - 110px); /* This line remains unchanged */
    max-width: calc(100vw - 40px);
}

/* Адаптація позиції контейнера чату при відкритих сайдбарах на великих екранах */
@media (min-width: 992px) {
    .app-shell.pad-right .chat-bot-container {
        right: calc(var(--sidebar-w) + 20px);
        max-width: calc(100vw - var(--sidebar-w) - 40px);
    }
}

/* Запобігаємо прокручуванню body коли чат відкритий на мобільному */
@media (max-width: 768px) {
    body.chat-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }
}

.dark-theme .chat-bot-container {
    background: var(--secondary-bg-color);
    border-color: var(--border-color);
    box-shadow: 0 8px 24px rgba(255, 255, 255, 0.1);
}

/* Заголовок чату */
.chat-bot-header {
    background: var(--secondary-bg-color);
    border-bottom: 1px solid var(--border-color);
    border-radius: 1rem 1rem 0 0;
    padding: 1rem;
    flex-shrink: 0;
}

.dark-theme .chat-bot-header {
    background: var(--secondary-bg-color);
    border-color: var(--border-color);
}

/* Тіло чату з повідомленнями */
.chat-bot-body {
    flex: 1;
    padding: 1rem;
    overflow-y: auto;
    max-height: 350px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    background: var(--main-bg-color);
}

.chat-bot-body::-webkit-scrollbar {
    width: 6px;
}

.chat-bot-body::-webkit-scrollbar-track {
    background: var(--secondary-bg-color);
    border-radius: 0.5rem;
}

.chat-bot-body::-webkit-scrollbar-thumb {
    background: var(--brand-color-50);
    border-radius: 0.5rem;
}

.dark-theme .chat-bot-body::-webkit-scrollbar-track {
    background: var(--secondary-bg-color);
}

.dark-theme .chat-bot-body::-webkit-scrollbar-thumb {
    background: var(--brand-color-75);
}

/* Повідомлення в чаті */
.chat-message {
    display: inline-block;
    margin-bottom: 12px;
    padding: 0.5rem 0.75rem;
    border-radius: 0.5rem;
    max-width: 90%;
    min-width: 40px;
    word-wrap: break-word;
    line-height: 1.4;
}

/* Стилі для Markdown елементів у повідомленнях */
.chat-message strong {
    font-weight: 600;
}

.chat-message em {
    font-style: italic;
}

.chat-message code {
    background-color: rgba(54, 36, 96, 0.1);
    border: 1px solid rgba(54, 36, 96, 0.2);
    border-radius: 0.25rem;
    padding: 0.1rem 0.3rem;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.9em;
}

.dark-theme .chat-message code {
    background-color: rgba(54, 36, 96, 0.2);
    border-color: rgba(54, 36, 96, 0.3);
}

.chat-message pre {
    background-color: rgba(54, 36, 96, 0.05);
    border: 1px solid rgba(54, 36, 96, 0.1);
    border-radius: 0.5rem;
    padding: 0.75rem;
    margin: 0.5rem 0;
    overflow-x: auto;
    font-family: 'Courier New', Consolas, monospace;
    font-size: 0.9em;
    line-height: 1.3;
}

.dark-theme .chat-message pre {
    background-color: rgba(54, 36, 96, 0.15);
    border-color: rgba(54, 36, 96, 0.2);
}

.chat-message pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: inherit;
}

.chat-message a {
    color: var(--brand-color);
    text-decoration: none;
}

.chat-message a:hover {
    color: var(--brand-color-hover);
    text-decoration: underline;
}

.chat-message ul,
.chat-message ol {
    margin: 0.5rem 0;
    padding-left: 1.2rem;
}

.chat-message li {
    margin: 0.2rem 0;
    line-height: 1.3;
}

.chat-message ul {
    list-style-type: disc;
}

.chat-message ol {
    list-style-type: decimal;
}

.chat-message.user-message {
    background: var(--brand-color-25);
    color: var(--main-text-color);
    margin-left: auto;
    text-align: left;
    align-self: flex-end;
    font-size: 14px;
}

.chat-message.ai-message {
    font-size: 14px;
    background: var(--secondary-bg-color);
    color: var(--main-text-color);
    margin-right: auto;
    align-self: flex-start;
}

.dark-theme .chat-message.ai-message {
    font-size: 14px;
    background: var(--secondary-bg-color);
    color: var(--main-text-color);
}

.chat-message.system-message {
    background: transparent;
    text-align: center;
    margin: 0 auto;
    max-width: 100%;
}

/* Індикатор друкування */
.typing-indicator {
    position: relative;
}

.typing-dots {
    animation: typingAnimation 1.4s infinite;
}

@keyframes typingAnimation {
    0% { opacity: 0.3; }
    50% { opacity: 1; }
    100% { opacity: 0.3; }
}

/* Підвал чату з полем вводу */
.chat-bot-footer {
    border-top: 1px solid var(--border-color);
    padding: 1rem;
    flex-shrink: 0;
    border-radius: 0 0 1rem 1rem;
    background: var(--main-bg-color);
}

.dark-theme .chat-bot-footer {
    border-color: var(--border-color);
}

.chat-bot-footer .input-group textarea {
    resize: none;
    border-radius: 0.5rem;
}

.chat-bot-footer .input-group button {
    border-radius: 0.5rem;
    min-width: 45px;
}

/* Кнопка-переключувач чату - тепер просто стандартна кнопка в UI */
.chat-toggle-btn {
    /* Зберігаємо стилі для кнопки в UI, але без fixed позиціонування */
    min-width: 45px;
    height: 32px;
    border-radius:  0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: all 0.3s ease;
    background: var(--brand-color);
    color: var(--main-btn-text-color);
    border: 1px solid var(--brand-color);
}

.chat-toggle-btn:hover {
    transform: scale(1.05);
    background: var(--brand-color-hover);
}

/* Анімації */
.chat-bot-container.show {
    animation: slideInUp 0.3s ease-out;
}

.chat-bot-container.hide {
    animation: slideOutDown 0.3s ease-in;
}

@keyframes slideInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideOutDown {
    from {
        opacity: 1;
        transform: translateY(0);
    }
    to {
        opacity: 0;
        transform: translateY(20px);
    }
}

/* Стани завантаження */
.chat-message.loading {
    position: relative;
}

/* Вимкнено анімацію трьох крапок для чату книги */
.chat-message.loading::after {
    content: none !important;
    animation: none !important;
}

@keyframes dots {
    0%, 20% {
        color: transparent;
        text-shadow: .25em 0 0 transparent, .5em 0 0 transparent;
    }
    40% {
        color: var(--main-text-color);
        text-shadow: .25em 0 0 transparent, .5em 0 0 transparent;
    }
    60% {
        text-shadow: .25em 0 0 var(--main-text-color), .5em 0 0 transparent;
    }
    80%, 100% {
        text-shadow: .25em 0 0 var(--main-text-color), .5em 0 0 var(--main-text-color);
    }
}

/* Респонсивність */
@media (max-width: 768px) {
    .chat-bot-container {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        height: 100vh;
        max-width: none;
        max-height: none;
        border-radius: 0;
        border: none;
        z-index: 1370;
        display: flex;
        flex-direction: column;
        margin: 0;
        padding: 0;
    }
    

/* -------------------------------------------------- */
/* Mobile keyboard-safe fullscreen chat adjustments   */
/* -------------------------------------------------- */

/* JS will set --vh based on VisualViewport; fallback to 1vh */
:root { --vh: 1vh; }

/* When chat is open on mobile, make it fullscreen and stick to the bottom
     so there is no gap when the on-screen keyboard appears */
@media (max-width: 768px) {
    body.chat-open .chat-bot-container {
        bottom: 0 !important;
        right: 0;
        left: 0;
        width: 100vw;
        max-width: 100vw;
        height: calc(var(--vh, 1vh) * 100);
        max-height: calc(var(--vh, 1vh) * 100);
        border-radius: 0;
    }
}

/* Prefer modern dynamic viewport units when supported */
@supports (height: 100dvh) {
    @media (max-width: 768px) {
        body.chat-open .chat-bot-container {
            height: 100dvh;
            max-height: 100dvh;
        }
    }
}
    .chat-bot-header {
        border-radius: 0;
        flex-shrink: 0;
        position: sticky;
        top: 0;
        z-index: 1371;
        margin: 0;
    }
    
    .chat-bot-footer {
        border-radius: 0;
        flex-shrink: 0;
        position: sticky;
        bottom: 0;
        z-index: 1371;
        margin: 0;
    }
    
    .chat-bot-body {
        flex: 1;
        overflow-y: auto;
        min-height: 0;
        max-height: none;
        margin: 0;
    }
}

/* Приховування елементів */
.d-none {
    display: none !important;
}

/* Основний колір заповненої частини */
.progress-bar {
    background-color: var(--brand-color) !important; /* Ваш колір */
}

/* Колір фону незаповненої частини */
.progress {
    background-color: var(--secondary-bg-color) !important; /* Ваш колір */
}

/* ---------------------------------------------- */
/* Mobile overrides to pin header and safe-areas  */
/* ---------------------------------------------- */
:root { --vh: 1vh; }

@media (max-width: 768px) {
    /* Ensure the chat takes full visual height and is pinned to viewport on mobile */
    body.chat-open .chat-bot-container {
        bottom: 0 !important;
        right: 0;
        left: 0;
        width: 100vw;
        max-width: 100vw;
        height: calc(var(--vh, 1vh) * 100);
        max-height: calc(var(--vh, 1vh) * 100);
        border-radius: 0;
        padding-top: env(safe-area-inset-top, 0);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    /* Keep the header pinned to the very top within the chat container */
    .chat-bot-header {
        position: sticky;
        top: env(safe-area-inset-top, 0);
        z-index: 2;
        border-radius: 0;
    }

    /* Only the message area should scroll; allow it to shrink when keyboard opens */
    .chat-bot-body {
        -webkit-overflow-scrolling: touch;
        flex: 1 1 auto;
        min-height: 0;
        max-height: none;
    }
}

@supports (height: 100dvh) {
    @media (max-width: 768px) {
        body.chat-open .chat-bot-container {
            height: 100dvh;
            max-height: 100dvh;
        }
    }
}