/* ======================================================= */
/* ==          STYLES FOR PUBLIC FACING CHATBOT         == */
/* ======================================================= */

/* --- Base Styles (Desktop) --- */
#chatbox {
    position: fixed;
    bottom: -800px; /* Initially hidden */
    width: 400px;
    height: var(--chatbot-window-height, 600px);
    background-color: var(--chatbot-chatbox-bg-color, #fff);
    border: none;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    transition: bottom 0.3s ease;
    display: flex;
    flex-direction: column;
    z-index: 9999;
    border-radius: 10px;
    box-sizing: border-box;
}

#chatbox.chatbox-closed {
    bottom: -800px;
}

#chatbox-header {
    background-color: var(--chatbot-header-bg-color, #038AF9);
    color: var(--chatbot-header-text-color, #fff);
    padding: 10px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;    
    font-size: 18px;
    text-align: center;
    position: relative;
    min-height: 40px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px 10px 0 0;
    overflow: hidden;
}

#chatbox-header-actions {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 8px;
}

#chatbox-close {
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    opacity: 0.8;
    transition: opacity 0.2s;
}

#chatbox-close:hover {
    opacity: 1;
}

#chatbox-new {
    background: none;
    border: none;
    color: inherit;
    font-size: 18px;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    opacity: 0.8;
    transition: opacity 0.2s;
}

#chatbox-new:hover {
    opacity: 1;
}

#chatbox-content {
    flex: 1;
    padding: 10px;
    overflow-y: auto;
    box-sizing: border-box;
}

/* =================================== */
/* == RESTORED FUNCTIONAL HACK      == */
/* =================================== */

.chat-message {
    margin-bottom: 10px;
    display: flex;
    align-items: flex-start; /* Allinea in alto */
    max-width: 100%;
}

.chat-message.user {
    flex-direction: row-reverse;
    margin-right: -10px; /* L'hack per tirare a destra */
}

.chat-message.bot {
    justify-content: flex-start;
}

.chat-message img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    flex-shrink: 0;
}

.chat-message .chat-bubble {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 15px;
    padding: 10px;
    border-radius: 10px;
    max-width: 90%;
    word-wrap: break-word;
    box-sizing: border-box;
    line-height: 1.5;
    white-space: normal;
}

.chat-message.user .chat-bubble {
    background-color: var(--chatbot-user-message-bg-color, #add8e6);
    color: var(--chatbot-user-message-text-color, #000);
    margin-right: 10px; /* L'hack per creare lo spazio */
}

.chat-message.bot .chat-bubble {
    background-color: var(--chatbot-bot-message-bg-color, #ffc0cb);
    color: var(--chatbot-bot-message-text-color, #000);
    margin-left: 10px;
}

#chatbox .chat-message .chat-bubble p {
    margin: 0 0 8px;
    padding: 0;
    font-size: inherit;
    line-height: 1.5;
}

.chat-message.loading img {
    width: 40px;
    height: 20px;
}

#chatbox-controls {
    display: flex;
    align-items: flex-end;
    padding: 10px;
    border-top: 1px solid #ccc;
    box-sizing: border-box;
    min-height: 60px;
}

#chatbox-input {
    flex: 1;
    padding: 10px;
    border: 1px solid #ccc;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 15px !important;
    color: #4a4a4a;
    border-radius: 5px;
    margin-right: 10px;
    box-sizing: border-box;
    height: 40px;
    min-height: 40px;
    max-height: 120px;
    resize: none;
    overflow-y: auto;
    line-height: normal !important;
}

#chatbox-stop {
    padding: 10px 14px;
    border: none;
    background-color: var(--chatbot-stop-button-bg-color, #777777);
    color: var(--chatbot-stop-button-text-color, #fff);
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 14px;
    border-radius: 5px;
    cursor: pointer;
    box-sizing: border-box;
    height: 40px;
    margin-right: 10px;
    white-space: nowrap;
}

#chatbox-stop:hover {
    opacity: 0.85;
}

#chatbox-send {
    padding: 10px 20px;
    border: none;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 16px;
    border-radius: 5px;
    cursor: pointer;
    box-sizing: border-box;
    height: 40px !important;
    min-height: 40px !important;
    line-height: 20px !important;
    background-color: var(--chatbot-send-button-bg-color, #dc3232);
    color: var(--chatbot-send-button-text-color, #fff);
}

#chatbox-send:hover {
    background-color: #b91919;
}

#chatbot-container {
    position: fixed;
    bottom: 20px;
    width: 350px;
    height: 80px;
    z-index: 2147483647;
    pointer-events: none;
    display: flex;
    align-items: flex-end;
}

#chatbox-button {
    position: relative;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    overflow: hidden;
    cursor: pointer;
    pointer-events: all;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
    flex-shrink: 0;
}

#chatbox-button img {
    display: block;
    width: 100%;
    height: 110%;
    object-fit: cover;
    transform: translate(-0%, -2.5%);
    border-radius: 50%;
}

#chatbox-footer {
    padding: 5px 10px;
    text-align: center;
    font-size: 0.8em;
    color: #808080;
    border-top: 1px solid #ccc;
    box-sizing: border-box;
}

#chatbox-powered {
  text-align: center;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;  
  font-size: 14px !important;
  color: #808080 !important;
}

#chatbox-powered a {
    color: #808080 !important;
    text-decoration: none;
}

#chatbox-powered a:hover {
    text-decoration: underline;
}

/* --- Positioning Rules --- */
#chatbox.chatbot-position-right,
#chatbot-container.chatbot-position-right {
    right: 0;
    left: auto;
    justify-content: flex-end;
}

#chatbox.chatbot-position-left,
#chatbot-container.chatbot-position-left {
    left: 20px;
    right: auto;
    justify-content: flex-start;
}

/* --- Pre-Chat Form --- */
#prechat-form {
    padding: 20px;
    text-align: center;
}
#prechat-form h3 {
    margin-top: 0;
    font-size: 1.2em;
}
#prechat-form .form-field {
    margin-bottom: 15px;
    text-align: left;
}
#prechat-form label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
    font-size: 0.9em;
}
#prechat-form input[type="text"],
#prechat-form input[type="email"] {
    width: 100%;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}
#prechat-start-btn {
    background-color: var(--chatbot-header-bg-color, #038AF9);
    color: var(--chatbot-header-text-color, #fff);
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1em;
    width: 100%;
}

/* --- Proactive Message Bubble --- */
#chatbot-proactive-bubble {
    position: fixed;
    bottom: 25px;
    z-index: 9998;
    background-color: var(--chatbot-header-bg-color, #038AF9);
    color: var(--chatbot-header-text-color, #fff);
    padding: 12px 18px;
    padding-right: 30px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    opacity: 0;
    transform: translateY(10px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    pointer-events: none;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 14px;
    max-width: 300px;
    line-height: 1.4;
    text-align: left;
    word-wrap: break-word;
}

#chatbot-proactive-bubble.visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: all;
}

.chatbot-proactive-text {
    white-space: pre-line;
    word-wrap: break-word;
}

#chatbot-proactive-bubble::after {
    content: '';
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 0; 
    height: 0; 
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
}

#chatbot-proactive-bubble.chatbot-position-right {
    right: 95px;
    left: auto;
}
#chatbot-proactive-bubble.chatbot-position-right::after {
    right: -8px;
    left: auto;
    border-left: 8px solid var(--chatbot-header-bg-color, #038AF9);
}

#chatbot-proactive-bubble.chatbot-position-left {
    left: 95px;
    right: auto;
}
#chatbot-proactive-bubble.chatbot-position-left::after {
    left: -8px;
    right: auto;
    border-right: 8px solid var(--chatbot-header-bg-color, #038AF9);
}

.chatbot-proactive-close {
    position: absolute;
    top: 5px;
    right: 8px;
    font-size: 22px;
    cursor: pointer;
    font-weight: bold;
    line-height: 1;
    color: var(--chatbot-header-text-color, #fff);
    opacity: 0.7;
    transition: opacity 0.2s;
}

.chatbot-proactive-close:hover {
    opacity: 1;
}

/* --- Embedded Shortcode Styles --- */
.chatbot-shortcode-wrapper {
    position: relative;
    height: 600px;
    width: 100%; 
    max-width: 1100px; 
    margin: 40px auto;
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    overflow: hidden;
}

.chatbot-shortcode-wrapper #chatbox {
    border-radius: 0;
    box-shadow: none;
}

/* --- Mobile Responsive Styles --- */
@media (max-width: 768px) {
    #chatbox {
        width: 100%;
        height: calc(100% - 60px);
        bottom: -100vh;
        left: 0;
        right: 0;
        border-radius: 0;
    }
    
    #chatbox-header {
        height: 55px;
        border-radius: 0;
    }
    
    #chatbot-container {
        bottom: 5px;
    }
    
    #chatbot-container.chatbot-position-right { right: 5px; }
    #chatbot-container.chatbot-position-left { left: 5px; }
    
    #chatbox-button {
        width: 50px;
        height: 50px;
    }

    #chatbot-proactive-bubble {
        bottom: 20px;
        max-width: calc(100vw - 80px);
    }
    #chatbot-proactive-bubble.chatbot-position-right { right: 65px; }
    #chatbot-proactive-bubble.chatbot-position-left { left: 65px; }    
}

/* ============================================= */
/* STILI SPECIFICI PER LO SHORTCODE EMBEDDED     */
/* ============================================= */

body .chatbot-shortcode-wrapper {
    position: relative !important;
    height: 600px !important;
    width: 100% !important; 
    max-width: 100% !important;
    margin: 40px 0 !important;
    border: 1px solid #e0e0e0 !important;
    overflow: hidden !important;
    border-radius: 8px !important; /* Ripristiniamo gli angoli arrotondati desiderati */
    box-shadow: 0 1px 3px rgba(0,0,0,0.04) !important;
}

body .chatbot-shortcode-wrapper #chatbox {
    border-radius: 0 !important;
    box-shadow: none !important;
}

body .chatbot-shortcode-wrapper #chatbox-header {
    border-radius: 0 !important;
    min-height: 50px !important;
    font-size: 20px !important;
}

body .chatbot-shortcode-wrapper #chatbox-controls {
    height: 80px !important;
    padding: 20px !important;
    align-items: center !important;
}

body .chatbot-shortcode-wrapper #chatbox-input {
    height: 50px !important;
    font-size: 16px !important;
}

body .chatbot-shortcode-wrapper #chatbox-send {
    height: 50px !important;
    padding: 10px 25px !important;
}

/* --- Streaming Bot Message Styles --- */
.chat-message.bot.streaming .chat-bubble.streaming::after {
    content: '';
    display: inline-block;
    width: 8px;
    height: 16px;
    background-color: #333;
    margin-left: 4px;
    animation: chatbot-blink 1s step-end infinite;
    vertical-align: text-bottom;
}

@keyframes chatbot-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

.chat-message.bot.streaming .chat-bubble.streaming {
    min-height: 24px;
}

.typing-indicator {
    color: #999;
    font-style: italic;
    font-size: 0.9em;
}

/* --- Header actions (new conversation + close) --- */
#chatbox-header #chatbox-header-actions {
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 8px;
}

#chatbox-header #chatbox-new,
#chatbox-header #chatbox-close {
    background: none;
    border: none;
    color: var(--chatbot-header-text-color, #fff);
    font-size: 20px;
    cursor: pointer;
    line-height: 1;
    padding: 0 4px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

#chatbox-header #chatbox-new {
    transform: translateY(2px);
}

#chatbox-header #chatbox-new:hover {
    opacity: 0.8;
}

/* --- Unread badge on the floating button --- */
#chatbox-unread-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    border-radius: 9px;
    background-color: #e53935;
    color: #fff;
    font-size: 11px;
    font-weight: bold;
    line-height: 18px;
    text-align: center;
    box-sizing: border-box;
    z-index: 10;
}

/* --- Error message + Retry button --- */
.chat-message.bot .chat-bubble.error {
    background-color: #fdecea;
    color: #8b0000;
}

.chat-message.bot .chat-bubble.error .error-text {
    display: block;
}

.chat-retry-button {
    display: block;
    margin-top: 8px;
    padding: 6px 14px;
    border: 1px solid var(--chatbot-header-bg-color, #038AF9);
    background-color: var(--chatbot-header-bg-color, #038AF9);
    color: var(--chatbot-header-text-color, #fff);
    border-radius: 5px;
    cursor: pointer;
    font-size: 13px;
    font-family: inherit;
}

.chat-retry-button:hover {
    opacity: 0.85;
}

/* --- Shortcode mode overrides for new controls --- */
body .chatbot-shortcode-wrapper #chatbox-controls {
    height: auto !important;
    min-height: 80px !important;
    padding: 20px !important;
    align-items: flex-end !important;
}

body .chatbot-shortcode-wrapper #chatbox-input {
    min-height: 50px !important;
    max-height: 120px !important;
    font-size: 16px !important;
}

body .chatbot-shortcode-wrapper #chatbox-stop {
    height: 50px !important;
}

body .chatbot-shortcode-wrapper #chatbox-send {
    height: 50px !important;
    padding: 10px 25px !important;
}

body .chatbot-shortcode-wrapper #chatbox-header {
    position: relative;
}

/* --- Reasoning / thinking box --- */
.chat-thinking {
    margin: 0 0 10px;
    padding: 8px 10px;
    background-color: rgba(0, 0, 0, 0.04);
    border-radius: 8px;
    font-size: 0.85em;
}

.chat-thinking-header {
    display: flex;
    align-items: center;
    gap: 6px;
    color: #888;
    cursor: pointer;
    font-style: italic;
    user-select: none;
}

.chat-thinking-spinner {
    width: 12px;
    height: 12px;
    border: 2px solid rgba(0, 0, 0, 0.15);
    border-top-color: #888;
    border-radius: 50%;
    display: inline-block;
    flex-shrink: 0;
    animation: chat-thinking-spin 0.8s linear infinite;
}

@keyframes chat-thinking-spin {
    to { transform: rotate(360deg); }
}

.chat-thinking-body {
    margin-top: 4px;
    color: #666;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 140px;
    overflow-y: auto;
}

.chat-thinking.collapsed .chat-thinking-body {
    display: none;
}

.chat-thinking.collapsed .chat-thinking-spinner {
    display: none;
}

.chat-thinking.collapsed .chat-thinking-label::before {
    content: '▸ ';
}

/* --- Markdown content inside bot bubbles --- */
.chat-message.bot .chat-bubble {
    white-space: normal;
}

#chatbox .chat-message .chat-bubble p:last-child,
#chatbox .chat-message .chat-bubble > *:last-child {
    margin-bottom: 0;
}

#chatbox .chat-message .chat-bubble h1,
#chatbox .chat-message .chat-bubble h2,
#chatbox .chat-message .chat-bubble h3,
#chatbox .chat-message .chat-bubble h4,
#chatbox .chat-message .chat-bubble h5,
#chatbox .chat-message .chat-bubble h6 {
    color: inherit;
    margin: 14px 0 6px;
    padding: 0;
    line-height: 1.3;
    font-weight: bold;
    font-size: 1em;
}

#chatbox .chat-message .chat-bubble h1 { font-size: 1.3em; }
#chatbox .chat-message .chat-bubble h2 { font-size: 1.2em; }
#chatbox .chat-message .chat-bubble h3 { font-size: 1.1em; }

#chatbox .chat-message .chat-bubble ul,
#chatbox .chat-message .chat-bubble ol {
    margin: 6px 0 8px;
    padding-left: 22px;
}

#chatbox .chat-message .chat-bubble li {
    margin-bottom: 4px;
    line-height: 1.5;
}

#chatbox .chat-message .chat-bubble blockquote {
    margin: 8px 0;
    padding: 6px 14px;
    border-left: 3px solid var(--chatbot-header-bg-color, #038AF9);
    background-color: rgba(0, 0, 0, 0.05);
    border-radius: 0 6px 6px 0;
}

#chatbox .chat-message .chat-bubble blockquote p {
    margin-bottom: 4px;
}

#chatbox .chat-message .chat-bubble a {
    color: var(--chatbot-header-bg-color, #038AF9);
    text-decoration: underline;
    word-break: break-all;
}

#chatbox .chat-message .chat-bubble code {
    font-family: Consolas, Monaco, "Courier New", monospace;
    font-size: 0.88em;
    background-color: rgba(0, 0, 0, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    word-break: break-word;
}

#chatbox .chat-message .chat-bubble pre {
    margin: 8px 0;
    padding: 12px 14px;
    background-color: #1e1e2e;
    color: #e0e0e0;
    border-radius: 8px;
    overflow-x: auto;
    font-family: Consolas, Monaco, "Courier New", monospace;
    font-size: 0.85em;
    line-height: 1.5;
    white-space: pre;
    word-wrap: normal;
    max-width: 100%;
    box-sizing: border-box;
}

#chatbox .chat-message .chat-bubble pre code {
    background: none;
    padding: 0;
    border-radius: 0;
    font-size: inherit;
    color: inherit;
    word-break: normal;
    white-space: pre;
}

#chatbox .chat-message .chat-bubble table {
    margin: 10px 0;
    border-collapse: collapse;
    width: 100%;
    font-size: 0.9em;
    overflow-x: auto;
    display: block;
}

#chatbox .chat-message .chat-bubble thead,
#chatbox .chat-message .chat-bubble tbody {
    display: table-row-group;
}

#chatbox .chat-message .chat-bubble tr {
    display: table-row;
}

#chatbox .chat-message .chat-bubble th,
#chatbox .chat-message .chat-bubble td {
    border: 1px solid rgba(0, 0, 0, 0.2);
    padding: 7px 10px;
    text-align: left;
    display: table-cell;
}

#chatbox .chat-message .chat-bubble th {
    background-color: rgba(0, 0, 0, 0.08);
    font-weight: bold;
    white-space: nowrap;
}

#chatbox .chat-message .chat-bubble hr {
    border: none;
    border-top: 1px solid rgba(0, 0, 0, 0.15);
    margin: 12px 0;
}

#chatbox .chat-message .chat-bubble img {
    max-width: 100%;
    border-radius: 6px;
}

#chatbox .chat-message .chat-bubble strong {
    font-weight: bold;
}

/* --- Code block with copy button --- */
.chat-code-block {
    position: relative;
    margin: 8px 0;
}

.chat-code-block pre {
    margin: 0;
}

.chat-code-copy {
    position: absolute;
    top: 6px;
    right: 6px;
    padding: 2px 8px;
    font-size: 11px;
    line-height: 1.6;
    color: #ccc;
    background-color: rgba(255, 255, 255, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    z-index: 1;
}

.chat-code-copy:hover {
    background-color: rgba(255, 255, 255, 0.25);
    color: #fff;
}

/* ======================================================= */
/* ==        RESPONSIVE: LAPTOP / ≤1366px               == */
/* ======================================================= */
@media (max-width: 1366px) {
    #chatbox {
        width: 360px !important;
        height: 500px !important;
        bottom: 0 !important;
    }

    .chat-message .chat-bubble {
        font-size: 14px;
    }

    #chatbox-header {
        font-size: 16px;
        display: flex !important;
        visibility: visible !important;
    }

    .chat-code-copy {
        font-size: 10px;
    }
}

/* ======================================================= */
/* ==        RESPONSIVE: TABLET & MOBILE / ≤768px       == */
/* ======================================================= */
@media (max-width: 768px) {
    #chatbox {
        width: 100vw !important;
        height: 100vh !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        min-width: 100vw !important;
        min-height: 100vh !important;
        border-radius: 0 !important;
        bottom: 100vh !important;
        left: 0 !important;
        right: 0 !important;
        top: 0 !important;
        position: fixed !important;
        transition: bottom 0.25s ease-in-out;
    }

    #chatbox:not(.chatbox-closed) {
        bottom: 0 !important;
    }

    #chatbox.chatbox-closed {
        bottom: 100vh !important;
    }

    #chatbox-header {
        border-radius: 0 !important;
        font-size: 16px;
        min-height: 48px;
        padding: 10px 40px 10px 10px;
        display: flex !important;
        visibility: visible !important;
    }

    #chatbox-content {
        padding: 10px;
        flex: 1;
        overflow-y: auto;
    }

    .chat-message .chat-bubble {
        font-size: 14px;
        max-width: 88%;
    }

    #chatbox-controls {
        display: flex !important;
    }

    #chatbot-container {
        bottom: 15px;
        width: auto;
        height: auto;
    }

    #chatbot-container.chatbot-position-right {
        right: 0;
    }

    #chatbot-container.chatbot-position-left {
        left: 0;
    }

    #chatbox-button {
        width: 54px;
        height: 54px;
    }

    #chatbot-proactive-bubble {
        max-width: 260px;
        font-size: 13px;
    }

    .chatbot-shortcode-wrapper {
        height: 100vh !important;
        min-height: 100vh !important;
        max-height: 100vh !important;
    }

    .chatbot-shortcode-wrapper #chatbox {
        position: absolute !important;
    }
}