/**
 * Cheshire Cat Chatbot - Frontend Styles
 *
 * Main stylesheet for the chat interface.
 */

/* ----------------------------------------
   1. Variables
---------------------------------------- */
/* 
 * The chat is styled to look like a speech bubble (balloon) when an avatar is enabled.
 * This is achieved through rounded corners, a tail pointing to the avatar,
 * and subtle shadows for depth.
 */
:root {
    --chat-primary-color: #0078d7;
    --chat-primary-hover: #005bb5;
    --chat-primary-active: #004494;
    --chat-user-msg-bg: #4caf50;
    --chat-user-msg-color: #ffffff;
    --chat-bot-msg-bg: #ffffff;
    --chat-bot-msg-color: #333333;
    --chat-error-msg-bg: #ffcccc;
    --chat-error-msg-border: #ffaaaa;
    --chat-error-msg-color: #991111;
    --chat-border-color: #ddd;
    --chat-bg-color: #fff;
    --chat-messages-bg: #f9f9f9;
    --chat-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    --chat-input-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
    --chat-input-focus-shadow: inset 0 2px 6px rgba(0, 120, 215, 0.2);
    --chat-border-radius: 10px;
    --chat-balloon-border-radius: 20px;
    --chat-msg-border-radius: 15px;
    --chat-transition-speed: 0.3s;
    --chat-avatar-size: 50px;
    --chat-avatar-bottom-margin: 20px;
    --chat-container-bottom-margin: 10px;
    --chat-balloon-tail-size: 20px;
}

/* ----------------------------------------
   2. Chat Container
---------------------------------------- */
#cheshire-chat-container {
    width: 100%;
    max-width: 400px;
    font-family: 'Arial', sans-serif;
    position: fixed;
    bottom: calc(var(--chat-avatar-size) + var(--chat-avatar-bottom-margin) + var(--chat-container-bottom-margin));
    right: 20px;
    z-index: 1000;
    transition: all var(--chat-transition-speed) ease;
    /* Default styles for closed state */
    display: flex;
    flex-direction: column;
    height: 0;
    min-height: 0;
    opacity: 0;
    visibility: hidden;
    border: none;
    border-radius: var(--chat-border-radius);
    background-color: transparent;
    box-shadow: none;
    overflow: hidden;
}

/* Chat container when explicitly open (mostly for JS to toggle) */
#cheshire-chat-container.cheshire-chat-open {
    display: flex; /* Ensure it's flex if it was changed */
    height: auto; /* Or a specific height if you prefer, e.g., 450px */
    min-height: 150px;
    opacity: 1;
    visibility: visible;
    border: 1px solid var(--chat-border-color);
    background-color: var(--chat-bg-color);
    box-shadow: var(--chat-shadow), 0 0 15px rgba(0, 0, 0, 0.05); /* Enhanced shadow for balloon effect */
    overflow: visible;
    border-radius: var(--chat-balloon-border-radius); /* More rounded corners for balloon effect */
    transition: height var(--chat-transition-speed) ease,
                min-height var(--chat-transition-speed) ease,
                opacity var(--chat-transition-speed) ease,
                visibility 0s linear 0s;
}

/* Chat container when closed and avatar is NOT enabled */
#cheshire-chat-container.cheshire-chat-closed:not(.with-avatar) {
    display: none;
    opacity: 0;
    visibility: hidden;
    height: 0;
    min-height: 0;
}

/* Chat container when closed BUT avatar IS enabled */
#cheshire-chat-container.cheshire-chat-closed.with-avatar {
    height: 0;
    min-height: 0;
    opacity: 0;
    visibility: hidden;
    border: none !important; /* Remove border */
    background: transparent !important; /* Make container background transparent */
    box-shadow: none !important; /* Remove shadow */
    overflow: hidden;
    transition: height var(--chat-transition-speed) ease,
                min-height var(--chat-transition-speed) ease,
                opacity var(--chat-transition-speed) ease,
                visibility 0s linear var(--chat-transition-speed);
}

/* Ensure smooth transition when opening */
#cheshire-chat-container.cheshire-chat-open.with-avatar {
    transition: height var(--chat-transition-speed) ease,
                min-height var(--chat-transition-speed) ease,
                opacity var(--chat-transition-speed) ease,
                visibility 0s linear 0s;
}

/* Hide internal elements when chat is closed */
#cheshire-chat-container.cheshire-chat-closed #cheshire-chat-header,
#cheshire-chat-container.cheshire-chat-closed #cheshire-chat-messages,
#cheshire-chat-container.cheshire-chat-closed #cheshire-chat-input-container {
    display: none;
}

/* ----------------------------------------
   3. Chat Header
---------------------------------------- */
#cheshire-chat-header {
    display: flex;
    justify-content: space-between;
    padding: 5px 10px;
    border-bottom: 1px solid var(--chat-border-color);
    background-color: var(--chat-bg-color); /* Ensure header has background */
    border-top-left-radius: var(--chat-balloon-border-radius); /* Match container's rounded corners */
    border-top-right-radius: var(--chat-balloon-border-radius); /* Match container's rounded corners */
    flex-shrink: 0; /* Prevent header from shrinking */
}

/* Common styles for header buttons */
#cheshire-chat-close,
#cheshire-chat-new {
    background: transparent;
    border: none;
    color: #999;
    font-size: 16px;
    cursor: pointer;
    padding: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    transition: all var(--chat-transition-speed) ease;
}

#cheshire-chat-close:hover,
#cheshire-chat-new:hover {
    background-color: rgba(0, 0, 0, 0.05);
    color: #666;
}

#cheshire-chat-close:focus,
#cheshire-chat-new:focus {
    outline: 2px solid var(--chat-primary-color);
    outline-offset: 2px;
}

/* Position the new conversation button to the left */
#cheshire-chat-new {
    margin-right: auto;
}

/* Position the close button to the right */
#cheshire-chat-close {
    margin-left: auto;
}

/* ----------------------------------------
   4. Chat Messages Area
---------------------------------------- */
#cheshire-chat-messages {
    height: 300px; /* Default height, can be overridden by playground styles */
    overflow-y: auto;
    padding: 10px 15px;
    background-color: var(--chat-messages-bg);
    border-bottom: 1px solid var(--chat-border-color);
    scroll-behavior: smooth;
    flex-grow: 1; /* Allow messages area to take available space */
}

/* ----------------------------------------
   5. Chat Input Area
---------------------------------------- */
#cheshire-chat-input-container {
    display: flex;
    align-items: center;
    padding: 10px;
    background-color: var(--chat-bg-color); /* Ensure input container has background */
    flex-shrink: 0; /* Prevent input area from shrinking */
}

#cheshire-chat-input {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--chat-border-color);
    border-radius: 20px;
    font-size: 14px;
    box-shadow: var(--chat-input-shadow);
    outline: none;
    margin: 0;
    background-color: var(--chat-bg-color); /* Ensure input has background */
}

#cheshire-chat-input:focus {
    border-color: var(--chat-primary-color);
    box-shadow: var(--chat-input-focus-shadow);
}

/* ----------------------------------------
   6. Send Button
---------------------------------------- */
#cheshire-chat-send {
    padding: 0;
    border: none;
    background-color: transparent;
    color: var(--chat-primary-color);
    font-size: 32px;
    cursor: pointer;
    width: 40px;
    height: 40px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    transition: color var(--chat-transition-speed) ease, background-color var(--chat-transition-speed) ease;
    margin-left: 10px;
    outline: none;
    border-radius: 50%;
}

#cheshire-chat-send:hover {
    color: var(--chat-primary-hover);
    background-color: rgba(0, 0, 0, 0.05);
}

#cheshire-chat-send:active {
    color: var(--chat-primary-active);
}

#cheshire-chat-send:focus {
    box-shadow: 0 0 0 2px rgba(0, 120, 215, 0.3);
}

#cheshire-chat-send i {
    pointer-events: none;
}

/* ----------------------------------------
   7. Message Styling
---------------------------------------- */
.user-message,
.bot-message,
.error-message {
    margin-bottom: 15px;
    padding: 10px 15px;
    border-radius: var(--chat-msg-border-radius);
    font-size: 14px;
    max-width: 75%;
    display: inline-block; /* Changed from inline-block to block for proper float clearing */
    word-wrap: break-word;
    clear: both;
    position: relative;
    line-height: 1.5;
}

.user-message {
    background-color: var(--chat-user-msg-bg);
    color: var(--chat-user-msg-color);
    text-align: right;
    float: right;
    animation: fadeInRight var(--chat-transition-speed) ease-out;
}

.bot-message {
    background-color: var(--chat-bot-msg-bg);
    color: var(--chat-bot-msg-color);
    text-align: left;
    float: left;
    border: 1px solid var(--chat-border-color);
    animation: fadeInLeft var(--chat-transition-speed) ease-out;
}

.error-message {
    background-color: var(--chat-error-msg-bg);
    border: 1px solid var(--chat-error-msg-border);
    color: var(--chat-error-msg-color);
    text-align: left;
    float: left;
    animation: shake var(--chat-transition-speed) ease-out;
}

/* Clearfix for message containers if needed, though float on messages should handle it */
#cheshire-chat-messages::after {
    content: "";
    clear: both;
    display: table;
}

/* ----------------------------------------
   8. Loader
---------------------------------------- */
.loader {
    border: 3px solid #f3f3f3;
    border-top: 3px solid var(--chat-primary-color);
    border-radius: 50%;
    width: 25px;
    height: 25px;
    animation: spin 1s linear infinite;
    margin: 10px auto;
    clear: both;
}

/* ----------------------------------------
   9. Animations
---------------------------------------- */
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

@keyframes fadeInLeft {
    from { opacity: 0; transform: translateX(-20px); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes fadeInRight {
    from { opacity: 0; transform: translateX(20px); }
    to { opacity: 1; transform: translateX(0); }
}

@keyframes shake {
    0% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    50% { transform: translateX(5px); }
    75% { transform: translateX(-5px); }
    100% { transform: translateX(0); }
}

/* ----------------------------------------
   10. Accessibility Improvements
---------------------------------------- */
#cheshire-chat-input:focus,
#cheshire-chat-send:focus,
#cheshire-chat-close:focus { /* Added close button */
    outline: 2px solid var(--chat-primary-color);
    outline-offset: 2px;
}

/* ----------------------------------------
   11. Avatar Styles
---------------------------------------- */
#cheshire-chat-avatar {
    position: fixed;
    right: 20px;
    bottom: var(--chat-avatar-bottom-margin);
    width: var(--chat-avatar-size);
    height: var(--chat-avatar-size);
    z-index: 1001; /* Ensure avatar is above the transparent closed container */
    display: block; /* Default, will be overridden if avatar not enabled */
    cursor: pointer;
}

#cheshire-chat-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

/* Chat bubble tail - only show when chat is explicitly open and avatar is enabled */
#cheshire-chat-container.cheshire-chat-open.with-avatar:after {
    content: '';
    position: absolute;
    bottom: calc(-1 * var(--chat-balloon-tail-size) / 2); /* Position tail to connect with avatar */
    right: 30px;   /* Align with avatar position */
    width: var(--chat-balloon-tail-size);
    height: calc(var(--chat-balloon-tail-size) / 2);
    background-color: var(--chat-bg-color); /* Match chat container background */
    clip-path: polygon(0 0, 100% 0, 50% 100%);
    border-right: 1px solid var(--chat-border-color);
    border-left: 1px solid var(--chat-border-color);
    border-bottom: 1px solid var(--chat-border-color);
    z-index: 999; /* Below main container content but above page */
    display: block;
    filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.1)); /* Add shadow to the tail for depth */
}


/* ----------------------------------------
   12. Responsive Design
---------------------------------------- */
@media screen and (max-width: 480px) {
    #cheshire-chat-container {
        max-width: 100%; /* Use full width available */
        width: calc(100% - 20px); /* Full width minus some padding */
        right: 10px;
        left: 10px;
        bottom: calc(var(--chat-avatar-size) + var(--chat-avatar-bottom-margin) + var(--chat-container-bottom-margin));
    }

    #cheshire-chat-avatar {
        right: 10px;
        bottom: 10px; /* Smaller bottom margin on mobile */
    }

    #cheshire-chat-container.cheshire-chat-open.with-avatar:after {
        right: 20px; /* Adjust tail position for mobile */
        width: var(--chat-balloon-tail-size); /* Maintain tail size on mobile */
    }

    .user-message,
    .bot-message,
    .error-message {
        max-width: 85%;
    }
}

/* ----------------------------------------
   13. HTML Content Styling in Bot Messages
---------------------------------------- */
.bot-message p { /* Ensure paragraphs inside bot messages have no extra margin if not desired */
    margin: 0 0 0.5em 0; /* Add some bottom margin to paragraphs */
}
.bot-message p:last-child {
    margin-bottom: 0;
}

.bot-message strong,
.bot-message b {
    font-weight: bold;
}

.bot-message em,
.bot-message i {
    font-style: italic;
}

.bot-message pre {
    background-color: #f5f5f5;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 10px;
    margin: 10px 0;
    overflow-x: auto;
    font-family: monospace, 'Courier New', Courier;
    font-size: 13px;
    line-height: 1.4;
    white-space: pre-wrap; /* Allow wrapping for long lines */
    word-wrap: break-word; /* Break words if necessary */
}

.bot-message code {
    background-color: #f5f5f5;
    border-radius: 3px;
    font-family: monospace, 'Courier New', Courier;
    padding: 2px 4px;
    font-size: 90%;
}

.bot-message pre code {
    background-color: transparent;
    padding: 0;
    border-radius: 0;
    font-size: inherit; /* Inherit font size from pre */
    white-space: pre-wrap; /* Ensure code inside pre also wraps */
}

.bot-message ul,
.bot-message ol {
    margin: 10px 0 10px 20px; /* Adjusted left margin for padding */
    padding-left: 20px;
}

.bot-message li {
    margin-bottom: 5px;
    line-height: 1.4;
}

.bot-message a {
    color: var(--chat-primary-color);
    text-decoration: underline;
}

.bot-message a:hover {
    color: var(--chat-primary-hover);
}

.cheshire-related-links{
   background: white;
}

.cheshire-related-links-tag a {
    display: inline-block;
    background-color: var(--chat-primary-color);
    color: white;
    padding: 5px 10px;
    border-radius: 15px;
    font-size: 12px;
    cursor: pointer;
    transition: background-color 0.2s;
    text-decoration: none;
}

.cheshire-related-links-tag a:hover {
    background-color: var(--chat-primary-hover);
    color: white;
}


/* Predefined responses in content */
.cheshire-related-links {
    margin-bottom: 20px;
    padding: 15px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    background-color: #f9f9f9;
    border-radius: 8px;
    border: 1px solid #eee;
}

.cheshire-related-links:before {
    content: attr(data-title);
    display: block;
    width: 100%;
    margin-bottom: 10px;
    font-weight: bold;
    font-size: 16px;
    color: #333;
}

.bot-message table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
    font-size: 13px;
}

.bot-message th,
.bot-message td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
}

.bot-message th {
    background-color: #f5f5f5;
    font-weight: bold;
}

.bot-message blockquote {
    border-left: 3px solid var(--chat-primary-color);
    margin: 10px 0 10px 5px; /* Adjusted left margin */
    padding: 5px 10px; /* Added some padding */
    color: #555; /* Slightly darker for better readability */
    font-style: italic;
}

.bot-message h1,
.bot-message h2,
.bot-message h3,
.bot-message h4,
.bot-message h5,
.bot-message h6 {
    margin: 15px 0 8px;
    font-weight: bold;
    line-height: 1.3;
}
.bot-message h1:first-child, /* Remove top margin if heading is the first element */
.bot-message h2:first-child,
.bot-message h3:first-child,
.bot-message h4:first-child,
.bot-message h5:first-child,
.bot-message h6:first-child {
    margin-top: 0;
}

.bot-message h1 { font-size: 1.6em; }
.bot-message h2 { font-size: 1.4em; }
.bot-message h3 { font-size: 1.2em; }
.bot-message h4 { font-size: 1.1em; }
.bot-message h5 { font-size: 1em; }
.bot-message h6 { font-size: 0.9em; }

.bot-message img {
    max-width: 100%;
    height: auto;
    margin: 10px 0;
    border-radius: 4px;
    display: block; /* Ensure image is block for margin auto if needed */
}

/* Predefined responses styles */
#cheshire-predefined-responses {
    padding: 8px 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    background-color: #f5f5f5;
    border-top: 1px solid #ddd;
}

.predefined-response-tag {
    display: inline-block;
    background-color: var(--chat-primary-color);
    color: white;
    padding: 5px 10px;
    border-radius: 15px;
    font-size: 12px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.predefined-response-tag:hover {
    background-color: var(--chat-primary-hover);
}

/* Predefined responses in content */
.cheshire-predefined-responses-content {
    margin-top: 20px;
    margin-bottom: 20px;
    padding: 15px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    background-color: #f9f9f9;
    border-radius: 8px;
    border: 1px solid #eee;
}

.cheshire-predefined-responses-content:before {
    content: attr(data-title);
    display: block;
    width: 100%;
    margin-bottom: 10px;
    font-weight: bold;
    font-size: 16px;
    color: #333;
}

.content-response-tag {
    cursor: pointer;
    transition: transform 0.2s;
}

.content-response-tag:hover {
    transform: scale(1.05);
}
