/* 
 * This CSS approach isn't ideal - fighting with Bootstrap, ScriptBlocks global styling,
 * and overly broad global styles from the header. I think the overall CSS setup should be refactored
 * in the future to use better encapsulation (stricter scoping, remove old bootstrap, maybe use some styling framework).
 */

/* CSS variables scoped to turtle helper */
#starlogo-turtle-helper {
    --th-primary-color: #99CC67;
    --th-background-color: #FCFCFC;
    --th-container-bg-color: #F0EBCE;
    --th-text-color: #222233;
    --th-border-color: #E0D8B0;
    --th-shadow-color: rgba(34, 34, 51, 0.15);
    --th-font-family: "Geist Sans", -apple-system, BlinkMacSystemFont, "Segoe UI",
        Roboto, Helvetica, Arial, sans-serif;
    --th-muted-color: #649633;
    --th-gray-100: #F0EBCE;
    --th-gray-800: #222233;
    --th-code-bg: #f6f8fa;
    --th-code-border: #d1d9e0;
}

/* Utility class for hiding turtle helper elements */
.turtle-helper-hidden {
    display: none !important;
}

/* Turtle helper icon */
.turtle-helper-icon {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 1000;
    width: 48px;
    height: 48px;
    background-color: var(--th-primary-color);
    border-radius: 50%;
    box-shadow: 0 4px 12px var(--th-shadow-color);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease-in-out;
}

.turtle-helper-icon:hover {
    transform: scale(1.1);
}

.turtle-helper-icon img {
    width: 48px;
    height: 48px;
}

#starlogo-turtle-helper {
    background-color: var(--th-background-color);
    width: 600px;
    height: calc(100vh - 200px);
    position: fixed;
    bottom: 80px;
    right: 20px;
    border-radius: 12px;
    box-shadow: 0 5px 20px var(--th-shadow-color);
    font-family: var(--th-font-family);
    color: var(--th-text-color);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    transition: opacity 0.3s ease, transform 0.3s ease;
    overflow: hidden;
}

#starlogo-turtle-helper.hidden {
    opacity: 0;
    transform: translateY(20px);
    pointer-events: none;
}

/* Chat header */
.chat-header {
    padding: 12px 15px;
    background: #F8FCFF;
    border-bottom: 1px solid #E5E5E5;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    border-radius: 12px 12px 0 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-logo {
    width: 32px !important;
    height: 32px !important;
    max-width: none !important;
}

.header-text {
    font-size: 18px;
    font-weight: 600;
    color: var(--th-text-color);
}

.header-right {
    display: flex;
    gap: 10px;
}

/* Override Bootstrap button styles */
#starlogo-turtle-helper .chat-header .header-btn {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    padding: 10px 18px !important;
    margin: 0 !important;
    border: none !important;
    border-radius: 8px !important;
    background: #cbd5e1 !important;
    color: #475569 !important;
    font-family: var(--th-font-family) !important;
    font-size: 13px !important;
    font-weight: 600 !important;
    line-height: 1 !important;
    text-align: center !important;
    text-decoration: none;
    white-space: nowrap !important;
    cursor: pointer !important;
    user-select: none !important;
    transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1) !important;
    outline: none !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 
                0 1px 2px rgba(0, 0, 0, 0.06),
                inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
    position: relative !important;
    overflow: hidden !important;
    min-width: 80px !important;
}

#starlogo-turtle-helper .chat-header .header-btn:hover {
    background: #94a3b8 !important;
    color: #334155 !important;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 
                0 2px 4px rgba(0, 0, 0, 0.06),
                inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
}

#starlogo-turtle-helper .chat-header .header-btn:active {
    color: #1e293b !important;
    transform: translateY(0) !important;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1),
                0 1px 2px rgba(0, 0, 0, 0.05) !important;
}

#starlogo-turtle-helper .chat-header .new-chat-btn {
    background: #0f90d1 !important;
    color: white !important;
}

#starlogo-turtle-helper .chat-header .new-chat-btn:hover {
    background: #0f90d1 !important;
    color: white !important;
}

#starlogo-turtle-helper .chat-header .new-chat-btn:active {
    background: #50b1e2 !important;
    color: white !important;
}

/* Message container */
#past-messages {
    flex-grow: 1;
    padding: 15px;
    overflow-y: auto;
    overflow-x: hidden;
    background: #FFFFFF;
    border-bottom: 1px solid var(--th-border-color);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Message bubbles */
.assistant-message {
    padding: 10px 15px;
    border-radius: 18px;
    line-height: 1.5;
    max-width: 80%;
    word-wrap: break-word;
    align-self: flex-start;
    background: transparent;
}

.user-message {
    padding: 10px 15px;
    line-height: 1.5;
    max-width: 80%;
    word-wrap: break-word;
    align-self: flex-end;
    background-color: #E9EEF6;
    color: black;
    display: inline-block;
    border-radius: 18px 4px 18px 18px;
}

/* Input area */
#starlogo-turtle-helper #turtle-helper {
    padding: 10px !important;
    background-color: var(--th-background-color) !important;
    flex-shrink: 0 !important;
    width: 100% !important;
    box-sizing: border-box !important;
    display: flex !important;
    flex-direction: column !important;
}

/* Example prompts in messages area */
#past-messages .example-prompts {
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    margin-top: auto;
}

#past-messages .example-label {
    font-size: 1.4rem;
    color: var(--th-text-color);
    margin: 0;
    font-weight: 400;
}

#past-messages .example-btn {
    display: inline-block;
    padding: 12px 16px;
    border: 1px solid var(--th-border-color);
    border-radius: 8px;
    background: linear-gradient(135deg, #F0EBCE 0%, #E8E0B8 100%);
    text-align: left;
    cursor: pointer;
    font-family: var(--th-font-family);
    color: var(--th-text-color);
    font-size: 1.4rem;
    transition: all 0.2s ease;
    white-space: nowrap;
    box-shadow: 0 1px 3px rgba(34, 34, 51, 0.1);
}

#past-messages .example-btn:hover {
    background: linear-gradient(135deg, #E8E0B8 0%, #D8CF98 100%);
    transform: translateY(-1px);
    box-shadow: 0 2px 6px rgba(34, 34, 51, 0.15);
}

#starlogo-turtle-helper .chat-input-container {
    display: flex !important;
    align-items: center !important;
    gap: 0 !important;
    position: relative !important;
    width: 100% !important;
    box-sizing: border-box !important;
}

#starlogo-turtle-helper .chat-input-container textarea {
    flex: 1 1 auto !important;
}

/* Chat input textarea styling */
#starlogo-turtle-helper textarea.chat-input {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    padding: 11px 50px 11px 12px !important;
    border-radius: 12px !important;
    border: 1px solid var(--th-border-color) !important;
    resize: none !important;
    font-family: var(--th-font-family) !important;
    font-size: 14px !important;
    line-height: 22px !important;
    min-height: 44px !important;
    height: 44px !important;
    max-height: 180px !important;
    overflow-y: auto !important;
    outline: none !important;
    background: white !important;
    color: var(--th-text-color) !important;
    margin: 0;
    display: block;
}

#starlogo-turtle-helper #turtle-helper .chat-input::placeholder {
    color: #999 !important;
    font-style: italic !important;
    line-height: 22px !important;
}

#send-button {
    position: absolute;
    right: 6px;
    bottom: 6px;
    top: 6px;
    margin: auto 0;
    width: 32px;
    height: 32px;
    background-color: var(--th-primary-color);
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease;
}

#send-button:hover {
    background-color: var(--th-muted-color);
}

/* Dynamic element styles */
#starlogo-turtle-helper .text-gray-500,
#starlogo-turtle-helper [data-status-message] .text-gray-500 {
    color: #6b7280;
}


#starlogo-turtle-helper .flex,
#starlogo-turtle-helper [data-status-message].flex {
    display: flex;
}

#starlogo-turtle-helper .items-center,
#starlogo-turtle-helper [data-status-message].items-center {
    align-items: center;
}

#starlogo-turtle-helper .justify-end {
    justify-content: flex-end;
}

#starlogo-turtle-helper .justify-start {
    justify-content: flex-start;
}

#starlogo-turtle-helper .whitespace-pre-wrap {
    white-space: pre-wrap;
}

#starlogo-turtle-helper .font-sans {
    font-family: var(--th-font-family);
}

#starlogo-turtle-helper .text-neutral-900 {
    color: #171717;
}

#starlogo-turtle-helper .space-y-6 {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

#starlogo-turtle-helper pre {
    overflow-x: hidden !important;
    word-break: break-word;
    white-space: pre-wrap;
}

.code-block-wrapper {
    display: flex;
    flex-direction: column;
}

.code-block-header {
    background-color: #e0dbba;
    padding: 0.5rem 1rem;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-bottom: none;
    border-radius: 0.5rem 0.5rem 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.code-block-title {
    margin: 0;
    font-size: 1.4rem;
    font-weight: bold;
    color: #333;
}

.code-block-content {
    background-color: #f0ebce;
    padding: 1rem;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 0 0 0.5rem 0.5rem;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: flex-start;
}

/* Copy button styling */
.copy-button {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid #d4c8a0;
    border-radius: 0.375rem;
    background-color: #f6f2e1;
    color: #5a5a3d;
    font-family: var(--th-font-family);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease-in-out;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.copy-button:hover {
    background-color: #ede7d0;
    color: #4a4a2d;
    border-color: #c8bc94;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.copy-button.copied {
    background-color: #e8e0b8;
    color: #4a5d23;
    border-color: #d8cf98;
}

.copy-button-icon {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

/* Image preview area above chat input */
.chat-image-preview {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
    padding: 8px;
    background: #f9f9f9;
    border-radius: 8px;
    border: 1px solid #e0e0e0;
    width: 100%;
    box-sizing: border-box;
}

.chat-image-wrapper {
    position: relative;
    display: inline-block;
}

.chat-image-thumbnail {
    width: 120px;
    height: 120px;
    object-fit: contain;
    border: 1px solid #d0d0d0;
    border-radius: 6px;
    background: white;
    padding: 4px;
    cursor: pointer;
    transition: border-color 0.2s ease;
}

.chat-image-thumbnail:hover {
    border-color: #007acc;
}

.chat-image-remove {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 20px;
    height: 20px;
    background: #ff4444;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 12px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    transition: background-color 0.2s ease;
}

.chat-image-remove:hover {
    background: #cc0000;
}

.chat-image-remove:focus {
    outline: none;
    box-shadow: 0 0 0 2px rgba(255, 68, 68, 0.3);
}

/* Markdown styles */

.markdown-content {
    font-family: var(--th-font-family);
    color: var(--th-text-color);
    line-height: 1.6;
    word-wrap: break-word;
}

#starlogo-turtle-helper .markdown-content .markdown-paragraph {
    margin: 0 0 1em 0 !important;
    padding: 0 !important;
    line-height: 1.6 !important;
}

#starlogo-turtle-helper .markdown-content .markdown-paragraph:last-child {
    margin-bottom: 0 !important;
}

#starlogo-turtle-helper .markdown-content .markdown-bold,
#starlogo-turtle-helper .markdown-content strong {
    font-weight: 600 !important;
    color: inherit !important;
    letter-spacing: normal !important;
}

#starlogo-turtle-helper .markdown-content .markdown-italic,
#starlogo-turtle-helper .markdown-content em {
    font-style: italic !important;
    color: inherit !important;
}

#starlogo-turtle-helper .markdown-content .markdown-inline-code,
#starlogo-turtle-helper .markdown-content code:not(pre code) {
    background-color: var(--th-code-bg) !important;
    color: #e01e5a !important;
    padding: 2px 6px !important;
    border-radius: 3px !important;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
    font-size: 0.9em !important;
    border: 1px solid var(--th-code-border) !important;
    margin: 0 2px !important;
    display: inline !important;
    line-height: 1.4 !important;
    white-space: pre-wrap !important;
}

.markdown-content .markdown-code-block,
.markdown-content pre {
    background-color: var(--th-code-bg) !important;
    border: 1px solid var(--th-code-border) !important;
    border-radius: 6px !important;
    padding: 12px !important;
    margin: 1em 0 !important;
    overflow-x: auto !important;
    max-width: 100% !important;
}

.markdown-content .markdown-code-block code,
.markdown-content pre code {
    background: transparent !important;
    color: var(--th-text-color) !important;
    padding: 0 !important;
    border: none !important;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
    font-size: 0.9em !important;
    line-height: 1.5 !important;
    display: block !important;
    white-space: pre !important;
    margin: 0 !important;
}

.markdown-content .markdown-list {
    margin: 0.5em 0 1em 0 !important;
    padding-left: 1.5em !important;
    list-style-position: outside !important;
}

.markdown-content .markdown-ul {
    list-style-type: disc !important;
}

.markdown-content .markdown-ol {
    list-style-type: decimal !important;
}

.markdown-content .markdown-list-item {
    margin: 0.25em 0 !important;
    padding: 0 !important;
    line-height: 1.6 !important;
    display: list-item !important;
}

.markdown-content .markdown-list .markdown-list {
    margin-top: 0.25em !important;
    margin-bottom: 0.25em !important;
}

.markdown-content .markdown-link,
.markdown-content a {
    color: #0066cc !important;
    text-decoration: underline !important;
    cursor: pointer !important;
}

.markdown-content .markdown-link:hover,
.markdown-content a:hover {
    color: #0052a3 !important;
    text-decoration: underline !important;
}

.markdown-content br {
    display: block !important;
    margin: 0 !important;
    padding: 0 !important;
    content: "" !important;
}

#starlogo-turtle-helper .assistant-message {
    white-space: pre-wrap !important;
    line-height: 1.6 !important;
    word-break: break-word !important;
}

#starlogo-turtle-helper .markdown-content > *:not(:first-child) {
    margin-top: 0.8em !important;
}

#starlogo-turtle-helper .markdown-content > *:not(:last-child) {
    margin-bottom: 0.8em !important;
}

.assistant-message p,
.assistant-message ul,
.assistant-message ol,
.assistant-message li,
.assistant-message code,
.assistant-message pre,
.assistant-message strong,
.assistant-message em,
.assistant-message a {
    all: revert !important;
}

.assistant-message .markdown-content * {
    max-width: 100% !important;
    word-wrap: break-word !important;
}

/* Retry button styles */
.message-retry-container {
    margin-top: 8px;
    display: flex;
    justify-content: flex-start;
}

.message-retry-button {
    display: inline-flex !important;
    align-items: center !important;
    gap: 6px !important;
    padding: 6px 12px !important;
    font-size: 0.85em !important;
    color: #6b7280 !important;
    background: transparent !important;
    border: 1px solid #e5e7eb !important;
    border-radius: 6px !important;
    cursor: pointer !important;
    transition: all 0.15s ease !important;
    font-family: var(--th-font-family) !important;
    font-weight: 500 !important;
    line-height: 1 !important;
    min-height: unset;
    margin: 0;
    outline: none !important;
    text-decoration: none;
    white-space: nowrap !important;
    user-select: none !important;
    box-shadow: none;
}

.message-retry-button:hover {
    color: #374151 !important;
    background: #f9fafb !important;
    border-color: #d1d5db !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1) !important;
}

.message-retry-button:active {
    transform: translateY(0) !important;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1) !important;
}

.retry-icon {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

/* Reference links styles */
.message-references {
    margin-top: 8px;
    padding: 6px 10px;
    background: var(--th-gray-100);
    border-radius: 6px;
    border: 1px solid var(--th-border-color);
    font-size: 0.85em;
    color: var(--th-muted-color);
    display: inline-block;
    max-width: fit-content;
    line-height: 1.4;
}

.message-references a.reference-link {
    color: var(--th-text-color) !important;
    text-decoration: none !important;
    border: none !important;
    background: none !important;
    transition: all 0.2s ease;
    margin-left: 8px;
    padding: 0 !important;
    line-height: inherit !important;
}

.message-references a.reference-link:hover {
    color: var(--primary-color) !important;
    text-decoration: underline !important;
    border: none !important;
    background: none !important;
}

.message-references a.reference-link:before,
.message-references a.reference-link:after {
    content: none !important;
    display: none !important;
}

.reference-number {
    font-weight: 600;
    color: var(--primary-color);
    text-decoration: none !important;
}

.reference-arrow {
    font-size: 0.8em;
    opacity: 0.6;
    margin-left: 2px;
}

/* ScriptBlocks protection */
#starlogo-turtle-helper .block-preview,
#starlogo-turtle-helper .code-block-content {
    isolation: isolate;
    contain: layout style paint;
}

#starlogo-turtle-helper .sb_block,
#starlogo-turtle-helper .sb_block *,
#starlogo-turtle-helper [class*="sb_"],
#starlogo-turtle-helper [class*="sb_"] *,
#starlogo-turtle-helper .blockView,
#starlogo-turtle-helper .blockView * {
    background: unset !important;
    background-color: unset !important;
}

#starlogo-turtle-helper .block-preview .blockView,
#starlogo-turtle-helper .code-block-content .blockView {
    position: relative !important;
    display: inline-block !important;
}

#starlogo-turtle-helper .block-preview canvas,
#starlogo-turtle-helper .block-preview svg,
#starlogo-turtle-helper .code-block-content canvas,
#starlogo-turtle-helper .code-block-content svg {
    background-color: transparent !important;
    opacity: 1 !important;
    visibility: visible !important;
}

