/* Knowledge AI Bot - Frontend Styles */

/* Toggle Button */
.kaib-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    z-index: 9998;
}

.kaib-toggle:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

.kaib-toggle.active {
    transform: rotate(90deg);
}

.kaib-toggle svg {
    color: white;
    width: 24px;
    height: 24px;
}

/* Chatbot Container */
.kaib-chatbot {
    position: fixed;
    bottom: 90px;
    right: 20px;
    width: 500px;
    height: 500px;
    background: #ffffff;
    border-radius: 16px;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.15);
    display: none;
    flex-direction: column;
    z-index: 9999;
    overflow: hidden;
    animation: slideUp 0.3s ease;
}

.kaib-chatbot.active {
    display: flex;
}

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

/* Header */
.kaib-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 500;
}

.kaib-title {
    font-size: 16px;
}

.kaib-close {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background-color 0.2s;
}

.kaib-close:hover {
    background-color: rgba(255, 255, 255, 0.2);
}

/* Messages Container */
.kaib-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #f8f9fa;
}

.kaib-messages::-webkit-scrollbar {
    width: 6px;
}

.kaib-messages::-webkit-scrollbar-track {
    background: #f1f3f5;
}

.kaib-messages::-webkit-scrollbar-thumb {
    background: #dee2e6;
    border-radius: 3px;
}

.kaib-messages::-webkit-scrollbar-thumb:hover {
    background: #ced4da;
}

/* Message Styles */
.kaib-message {
    margin-bottom: 16px;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.kaib-user-message {
    text-align: right;
}

.kaib-user-message p {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    display: inline-block;
    text-align: left;
}

/* Bot message content container */
.kaib-message-content {
    margin: 0;
    background: transparent;
    color: inherit;
    display: block;
}

/* Bot message paragraphs no longer carry the bubble; container does. */
.kaib-bot-message p {
    margin: 8px 0;
    background: transparent;
    color: inherit;
    display: block;
    box-shadow: none;
}

/* Bot bubble wraps multiple blocks (p, ul, ol, pre, tables, etc.) */
.kaib-bot-message {
    display: inline-block;
    background: #fff;
    color: #333;
    padding: 12px 16px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    max-width: min(68ch, calc(100% - 40px));
    word-break: break-word;
    hyphens: auto;
    text-align: left; /* ensure left alignment inside the bubble */
}

/* Proper spacing for content elements */
.kaib-message-content > p { margin: 8px 0; }
.kaib-message-content > p:first-child { margin-top: 0; }
.kaib-message-content > p:last-child { margin-bottom: 0; }
.kaib-message-content > ul,
.kaib-message-content > ol { margin: 8px 0; padding-left: 22px; }

/* Streaming cursor animation - show caret after the last block inside the message content */
.kaib-bot-message.streaming .kaib-message-content > *:last-child::after,
.kaib-bot-message.streaming .kaib-message-content:not(:has(> *))::after {
    content: '▊';
    display: inline-block;
    animation: blink 1s infinite;
    color: #667eea;
    font-weight: normal;
}

/* Typing Indicator */
.kaib-typing {
    display: inline-block;
    padding: 12px 16px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.kaib-typing span {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #999;
    margin: 0 2px;
    animation: typing 1.4s infinite;
}

.kaib-typing span:nth-child(2) {
    animation-delay: 0.2s;
}

.kaib-typing span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.7;
    }
    30% {
        transform: translateY(-10px);
        opacity: 1;
    }
}

/* Input Container */
.kaib-input-container {
    display: flex;
    padding: 16px;
    background: white;
    border-top: 1px solid #e9ecef;
    gap: 8px;
}

.kaib-input {
    flex: 1;
    border: 1px solid #e9ecef;
    border-radius: 24px;
    padding: 10px 16px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
}

.kaib-input:focus {
    border-color: #667eea;
}

.kaib-send {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.kaib-send:hover {
    transform: scale(1.05);
}

.kaib-send:active {
    transform: scale(0.95);
}

.kaib-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Markdown Element Styles */
.kaib-bot-message h1,
.kaib-bot-message h2,
.kaib-bot-message h3,
.kaib-bot-message h4,
.kaib-bot-message h5,
.kaib-bot-message h6 {
    margin: 16px 0 8px 0;
    font-weight: 600;
    color: #333;
    line-height: 1.3;
}

.kaib-bot-message h1 {
    font-size: 18px;
    border-bottom: 1px solid #e9ecef;
    padding-bottom: 8px;
}

.kaib-bot-message h2 {
    font-size: 16px;
}

.kaib-bot-message h3 {
    font-size: 15px;
}

.kaib-bot-message h4,
.kaib-bot-message h5,
.kaib-bot-message h6 {
    font-size: 14px;
}

.kaib-bot-message p {
    margin: 8px 0;
}

.kaib-bot-message p:first-child {
    margin-top: 0;
}

.kaib-bot-message p:last-child {
    margin-bottom: 0;
}

.kaib-bot-message strong,
.kaib-bot-message b {
    font-weight: 600;
    color: #333;
}

.kaib-bot-message em,
.kaib-bot-message i {
    font-style: italic;
    color: #555;
}

.kaib-bot-message code {
    background: #f1f3f5;
    border: 1px solid #e9ecef;
    border-radius: 3px;
    padding: 2px 4px;
    font-family: 'Monaco', 'Consolas', 'Courier New', monospace;
    font-size: 13px;
    color: #e83e8c;
}

.kaib-bot-message pre {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 6px;
    padding: 12px;
    margin: 12px 0;
    overflow-x: auto;
    font-family: 'Monaco', 'Consolas', 'Courier New', monospace;
    font-size: 13px;
    line-height: 1.4;
}

.kaib-bot-message pre code {
    background: none;
    border: none;
    padding: 0;
    color: #333;
}

.kaib-bot-message blockquote {
    margin: 12px 0;
    padding: 8px 16px;
    border-left: 4px solid #667eea;
    background: #f8f9fa;
    color: #555;
    font-style: italic;
}

.kaib-bot-message blockquote p {
    margin: 0;
}

.kaib-bot-message ul,
.kaib-bot-message ol {
    margin: 8px 0;
    padding-left: 22px; /* keep bullets inside the bubble */
}

.kaib-bot-message li {
    margin: 4px 0;
    line-height: 1.5;
}

.kaib-bot-message ul li {
    list-style-type: disc;
}

.kaib-bot-message ol li {
    list-style-type: decimal;
}

.kaib-bot-message a {
    color: #667eea;
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s;
}

.kaib-bot-message a:hover {
    border-bottom-color: #667eea;
}

.kaib-bot-message img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
    margin: 8px 0;
}

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

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

.kaib-bot-message table th {
    background: #f8f9fa;
    font-weight: 600;
}

.kaib-bot-message hr {
    border: none;
    border-top: 1px solid #e9ecef;
    margin: 16px 0;
}

/* Bolla: crea un contesto che evita il "margin-collapsing" dei <p> iniziali/finali */
.kaib-chat__bubble .kaib-chat__content {
  display: flow-root;           /* oppure overflow:hidden; */
  max-width: 68ch;              /* leggibilità */
  word-break: break-word;
  hyphens: auto;
}

/* Paragrafi: spaziatura coerente */
.kaib-chat__bubble .kaib-chat__content p {
  margin: 0 0 0.75rem;          /* spazio sotto ogni paragrafo */
  line-height: 1.55;
}

/* Niente spazio extra sull’ultimo paragrafo */
.kaib-chat__bubble .kaib-chat__content p:last-child {
  margin-bottom: 0;
}

/* Evita paragrafi vuoti residui */
.kaib-chat__bubble .kaib-chat__content p:empty {
  display: none;
}

/* Se qualche parser inserisce <br><br> per i paragrafi, nascondi i doppi break */
.kaib-chat__bubble .kaib-chat__content br+br {
  display: none;
}

/* Optional: immagini “standalone” dentro p */
.kaib-chat__bubble .kaib-chat__content p > img:only-child {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0.25rem auto 0.5rem;
  border-radius: 6px;
}

/* Dark mode (se hai temi scuri) */
@media (prefers-color-scheme: dark) {
  .kaib-chat__bubble .kaib-chat__content {
    color: rgba(255,255,255,.92);
  }
}

/* Mobile Responsive */
@media (max-width: 480px) {
    .kaib-chatbot {
        width: calc(100vw - 40px);
        height: calc(100vh - 120px);
        bottom: 80px;
        right: 20px;
    }
    
    .kaib-toggle {
        bottom: 16px;
        right: 16px;
    }
    
    .kaib-bot-message pre {
        font-size: 12px;
        padding: 8px;
    }
    
    .kaib-bot-message table {
        font-size: 12px;
    }
    
    .kaib-bot-message table th,
    .kaib-bot-message table td {
        padding: 4px 6px;
    }
}