/**
 * AI Question Block - Styles
 *
 * Frontend and editor styles for the AI Question block.
 *
 * @package AIWorkflowBlocks
 * @author Mika Wenell
 * @copyright 2025 uniwaves Oy
 */

/* ==========================================================================
   Block Wrapper
   ========================================================================== */

.ai-question-block-wrapper {
    position: relative;
    width: 100%;
    margin: 20px 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
    background: #ffffff;
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    overflow: visible;
}

/* ==========================================================================
   Message Templates (Hidden)
   ========================================================================== */

.message-templates {
    display: none !important;
}

/* ==========================================================================
   Question Input Frame
   ========================================================================== */

.ai-question-frame {
    margin-top: 20px;
}

.ai-question-label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    font-weight: 600;
    color: #333;
    font-size: 16px;
}

.ai-question-icon {
    font-size: 20px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.ai-question-icon img {
    width: 24px;
    height: 24px;
    display: block;
}

/* Input Row: Textarea + Send Button */
.ai-question-input-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
}

.ai-question-textarea {
    flex: 1;
    padding: 12px;
    border: 2px solid #ddd;
    border-radius: 6px;
    font-size: 15px;
    font-family: inherit;
    line-height: 1.6;
    resize: vertical;
    transition: border-color 0.2s ease;
    min-height: 60px;
}

.ai-question-textarea:focus {
    outline: none;
    border-color: #0073aa;
    box-shadow: 0 0 0 2px rgba(0, 115, 170, 0.1);
}

.ai-question-textarea::placeholder {
    color: #999;
    font-style: italic;
}

/* Send Button with Icon */
.ai-question-submit-btn {
    flex-shrink: 0;
    width: 48px;
    height: 48px;
    padding: 0;
    background: #0073aa;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ai-question-submit-btn:hover:not(:disabled) {
    background: #005a87;
    transform: scale(1.05);
    box-shadow: 0 4px 8px rgba(0, 115, 170, 0.3);
}

.ai-question-submit-btn:active:not(:disabled) {
    transform: scale(0.98);
}

.ai-question-submit-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
    opacity: 0.5;
}

.ai-submit-icon {
    width: 24px;
    height: 24px;
    filter: brightness(0) invert(1); /* Make SVG white */
}

/* ==========================================================================
   Single Answer Mode Frame
   ========================================================================== */

.ai-single-answer-frame {
    margin-bottom: 20px;
}

/* Answer Section */
.ai-answer-wrapper {
    margin-bottom: 24px;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 8px;
    border-left: 4px solid #00a0d2;
}

.ai-answer-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
    font-weight: 600;
    color: #333;
    font-size: 16px;
}

.ai-answer-icon {
    font-size: 24px;
    line-height: 1;
}

.ai-answer-icon img {
    width: 24px;
    height: 24px;
    display: block;
    vertical-align: middle;
}

.ai-answer-content {
    color: #333;
    line-height: 1.8;
    font-size: 15px;
}

.ai-answer-content p {
    margin: 0 0 12px 0;
}

.ai-answer-content p:last-child {
    margin-bottom: 0;
}

/* Answer Waiting State */
.ai-answer-waiting {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 30px 20px;
    text-align: center;
}

.ai-answer-waiting-icon {
    width: 80px;
    height: 80px;
    margin-bottom: 15px;
    opacity: 0.6;
}

.ai-answer-waiting-text {
    margin: 0;
    color: #999;
    font-size: 14px;
    font-style: italic;
}

/* Answer Error State */
.ai-answer-content .ai-error {
    padding: 15px;
    background: #ffebee;
    border-left: 4px solid #d32f2f;
    border-radius: 4px;
    color: #c62828;
    font-size: 14px;
}

.ai-answer-content .ai-error strong {
    font-weight: 600;
}

/* Loading State */
.ai-loading-wrapper {
    margin-bottom: 24px;
    padding: 30px;
    text-align: center;
    background: #f8f9fa;
    border-radius: 8px;
}

.ai-loading-spinner {
    width: 40px;
    height: 40px;
    margin: 0 auto 15px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #0073aa;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.ai-loading-text {
    margin: 0;
    color: #666;
    font-size: 15px;
    font-style: italic;
}

/* ==========================================================================
   Chat History Frame
   ========================================================================== */

.ai-chat-history-frame {
    min-height: 200px;
    margin-bottom: 20px;
    padding: 20px 20px 20px 20px;
    background: #f8f9fa;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow: visible;
}

/* Date Separator */
.ai-chat-date-separator {
    text-align: center;
    margin: 16px 0;
    position: relative;
    display: flex;
    align-items: center;
}

.ai-chat-date-separator::before,
.ai-chat-date-separator::after {
    content: '';
    flex: 1;
    height: 1px;
    background: #ddd;
}

.ai-chat-date-separator span {
    display: inline-block;
    padding: 0 12px;
    font-size: 12px;
    color: #444;
    font-weight: normal;
    margin: 0 12px;
    position: relative;
    z-index: 1;
}

/* Load More History Button */
.ai-load-more-history {
    display: block;
    margin: 0px auto 0px;
    padding: 0;
    background: none!important;
    border: none;
    border-radius: 0;
    width: auto;
    height: auto;
    cursor: pointer;
    transition: opacity 0.2s ease;
}

.ai-load-more-history:hover {
    opacity: 0.7;
}

.ai-load-more-history img {
    width: 50px;
    height: 50px;
    display: block;
}

/* Clear History Button */
.ai-clear-history-btn {
    position: absolute;
    top: 0px;
    right: 12px;
    width: auto;
    height: auto;
    padding: 0;
    background: none!important;
    border: none;
    cursor: pointer;
    transition: opacity 0.2s ease;
    z-index: 10;
}

.ai-clear-history-btn:hover {
    opacity: 0.7;
}

.ai-clear-history-btn img {
    width: 36px;
    height: 36px;
    display: block;
}

/* Message Wrapper - Base Styles */
.user-message-wrapper,
.ai-message-wrapper {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    animation: messageSlideIn 0.3s ease;
    overflow: visible;
    position: relative;
}

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

/* User Message Wrapper */
.user-message-wrapper {
    flex-direction: row-reverse;
}

.user-message-icon {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    max-width: 36px;
    max-height: 36px;
    display: block;
    align-self: flex-start;
    position: relative;
    margin: 2px 0 0 0;
    padding: 0;
}

.user-message-icon svg {
    width: 100%;
    height: 100%;
    display: block;
    vertical-align: top;
}

.user-message-content {
    flex: 1;
    padding: 25px 16px 12px 16px;
    background: #0073aa;
    color: #ffffff;
    border-radius: 18px 18px 4px 18px;
    line-height: 1.6;
    font-size: 15px;
    max-width: 75%;
    word-wrap: break-word;
    position: relative;
}

.ai-message-timestamp {
    position: absolute;
    top: 6px;
    left: 12px;
    font-size: 11px;
    opacity: 0.7;
    white-space: nowrap;
    pointer-events: none;
    z-index: 1;
}

.user-message-content .ai-message-timestamp {
    color: rgba(255, 255, 255, 0.8);
}

.ai-message-content .ai-message-timestamp {
    color: rgba(0, 0, 0, 0.5);
}

/* AI Message Wrapper */
.ai-message-wrapper {
    flex-direction: row;
}

.ai-message-icon {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    max-width: 36px;
    max-height: 36px;
    display: block;
    align-self: flex-start;
    position: relative;
    margin: 2px 0 0 0;
    padding: 0;
}

.ai-message-icon svg {
    width: 100%;
    height: 100%;
    display: block;
    vertical-align: top;
}

.ai-message-content {
    flex: 1;
    padding: 12px 16px;
    background: #ffffff;
    border-radius: 18px 18px 18px 4px;
    line-height: 1.2;
    font-size: 15px;
    max-width: 75%;
    border: 1px solid #e0e0e0;
    word-wrap: break-word;
    position: relative;
}

.ai-message-content h2, .ai-message-content h3 {
    font-size: 1.5em;
    margin-top: 0.5em;
}

.ai-message-content-text {
    color: #333;
}

.ai-message-content-text p {
    margin: 12px 0 6px 0;
}

.ai-message-content-text p:last-child {
    margin-bottom: 0;
}

/* Lists - Reduce left padding for more space */
.ai-message-content-text ul,
.ai-message-content-text ol,
.ai-answer-content ul,
.ai-answer-content ol {
    padding-left: 20px;
    margin: 8px 0;
}

.ai-message-content-text ul ul,
.ai-message-content-text ul ol,
.ai-message-content-text ol ul,
.ai-message-content-text ol ol,
.ai-answer-content ul ul,
.ai-answer-content ul ol,
.ai-answer-content ol ul,
.ai-answer-content ol ol {
    padding-left: 20px;
    margin: 4px 0;
}

.ai-message-content-text li,
.ai-answer-content li {
    margin: 4px 0;
}

/* Message Items (Sections) */
.ai-message-item {
    margin-bottom: 0;
}

.ai-message-content-text {
    /* Content area */
}

.ai-message-content-text:empty {
    display: none;
}

/* Per-Message Section Controls (inside each AI message) */
.ai-message-section-controls {
    margin-top: 16px;
    padding: 12px 16px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
}

/* Streaming Indicator (below show more button) */
.ai-message-streaming-indicator {
    width: 100%;
    padding: 8px 0;
    display: flex;
    justify-content: center;
    align-items: center;
}

.ai-streaming-dots {
    display: flex;
    gap: 4px;
    align-items: center;
}

.ai-streaming-dots span {
    width: 6px;
    height: 6px;
    background: #0073aa;
    border-radius: 50%;
    animation: streamingBounce 1.4s infinite ease-in-out both;
}

.ai-streaming-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.ai-streaming-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes streamingBounce {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Show More/Less Buttons (per message) */
.ai-message-section-controls .ai-show-more-btn,
.ai-message-section-controls .ai-show-less-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: #f0f0f0;
    border: 1px solid #ddd;
    border-radius: 6px;
    color: #0073aa;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.ai-message-section-controls .ai-show-more-btn:hover,
.ai-message-section-controls .ai-show-less-btn:hover {
    background: #0073aa;
    color: #ffffff;
    border-color: #0073aa;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 115, 170, 0.3);
}

.ai-message-section-controls .ai-show-more-btn:active,
.ai-message-section-controls .ai-show-less-btn:active {
    transform: translateY(0);
    box-shadow: none;
}

.show-more-icon,
.show-less-icon {
    font-size: 12px;
    line-height: 1;
}

/* Typing Indicator */
.ai-message-loading {
    display: flex;
    align-items: center;
    padding: 8px 0;
}

.ai-typing-indicator {
    display: flex;
    gap: 4px;
}

.ai-typing-indicator span {
    width: 8px;
    height: 8px;
    background: #999;
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out both;
}

.ai-typing-indicator span:nth-child(1) {
    animation-delay: -0.32s;
}

.ai-typing-indicator span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes typingBounce {
    0%, 80%, 100% {
        transform: scale(0);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Error in Chat */
.ai-message-content .ai-error {
    padding: 12px;
    background: #ffebee;
    border-left: 3px solid #d32f2f;
    border-radius: 4px;
    color: #c62828;
    font-size: 14px;
}

.ai-message-content .ai-error strong {
    font-weight: 600;
}

/* No scrollbar needed - frame grows with content */

/* ==========================================================================
   Empty State
   ========================================================================== */

.ai-question-block-wrapper.is-empty .ai-question-block-empty {
    display: block;
    padding: 40px 20px;
    text-align: center;
    background: #f9f9f9;
    border: 2px dashed #ddd;
    border-radius: 8px;
}

.ai-question-instructions-content h3 {
    margin: 0 0 15px 0;
    color: #333;
    font-size: 20px;
}

.ai-question-instructions-content p {
    margin: 0 0 15px 0;
    color: #666;
    line-height: 1.6;
}

.ai-question-instructions-list {
    list-style: none;
    padding: 0;
    margin: 0 0 15px 0;
    text-align: left;
    display: inline-block;
}

.ai-question-instructions-list li {
    margin: 8px 0;
    padding-left: 25px;
    position: relative;
    color: #666;
}

.ai-question-instructions-list li:before {
    content: "→";
    position: absolute;
    left: 0;
    color: #0073aa;
}

.ai-question-instructions-tip {
    font-style: italic;
    color: #999;
    font-size: 14px;
}

/* ==========================================================================
   State Modifiers
   ========================================================================== */

.ai-question-block-wrapper.hide-question .ai-question-question {
    display: none;
}

.ai-question-block-wrapper.hide-answer .ai-question-answer {
    display: none;
}

/* ==========================================================================
   Responsive Design
   ========================================================================== */

@media (max-width: 768px) {
    .ai-question-block-wrapper {
        padding: 15px;
    }
    
    .ai-answer-wrapper {
        padding: 15px;
    }
    
    .ai-question-textarea {
        font-size: 14px;
    }
    
    .ai-question-submit-btn {
        width: 44px;
        height: 44px;
    }
    
    .ai-submit-icon {
        width: 20px;
        height: 20px;
    }
    
    /* Chat History Mobile Adjustments */
    .ai-chat-history-frame {
        padding: 15px;
        gap: 12px;
    }
    
    /* Mobile: Stack icons on top of message boxes (column layout) */
    .user-message-wrapper,
    .ai-message-wrapper {
        flex-direction: column;
        gap: 8px;
    }
    
    /* User messages: Align icon and content to the right */
    .user-message-wrapper {
        align-items: flex-end;
        align-self: flex-end;
    }
    
    /* AI messages: Align icon and content to the left */
    .ai-message-wrapper {
        align-items: flex-start;
        align-self: flex-start;
    }
    
    .user-message-content,
    .ai-message-content {
        max-width: 100%;
        font-size: 14px;
        padding: 10px 14px;
    }
    
    .user-message-icon,
    .ai-message-icon {
        width: 32px;
        height: 32px;
        min-width: 32px;
        min-height: 32px;
        max-width: 32px;
        max-height: 32px;
        margin: 0;
    }
    
    .user-message-icon img,
    .user-message-icon svg,
    .ai-message-icon img,
    .ai-message-icon svg {
        width: 100%;
        height: 100%;
    }
    
    /* Per-Message Section Controls Mobile */
    .ai-message-section-controls {
        padding: 10px 12px;
        gap: 8px;
    }
    
    .ai-message-section-controls .ai-show-more-btn,
    .ai-message-section-controls .ai-show-less-btn {
        padding: 6px 12px;
        font-size: 12px;
    }
    
    /* Date Separator Mobile */
    .ai-chat-date-separator {
        margin: 12px 0;
    }
    
    .ai-chat-date-separator span {
        font-size: 11px;
        padding: 3px 12px;
    }
    
    /* Load More Button Mobile */
    .ai-load-more-history {
        width: auto;
        height: auto;
        padding: 0;
    }
    
    .ai-load-more-history img {
        width: 28px;
        height: 28px;
    }
    
    /* Clear History Button Mobile */
    .ai-clear-history-btn {
        top: 8px;
        right: 8px;
        width: auto;
        height: auto;
        padding: 0;
    }
    
    .ai-clear-history-btn img {
        width: 20px;
        height: 20px;
    }
}

/* ==========================================================================
   Editor Styles
   ========================================================================== */

.ai-question-block-editor-preview {
    min-height: 150px;
}

.ai-question-block-editor-preview h3 {
    font-size: 18px;
    color: #333;
}

