:root {
    /* Light mode (Tomorrow) */
    --background: #FFFFFF;
    --text-color: #4D4D4C;
    --toolbar-bg: #EFEFEF;
    --toolbar-border: #D6D6D6;
    --gutter-bg: #EFEFEF;
    --gutter-text: #4D4D4C;
    --scrollbar-track: #F5F5F5;
    --scrollbar-thumb: #B0B0B0;
}

@media (prefers-color-scheme: dark) {
    :root {
        /* Dark mode (Twilight) */
        --background: #141414;
        --text-color: #F8F8F8;
        --toolbar-bg: #323232;
        --toolbar-border: #5F5A60;
        --gutter-bg: #323232;
        --gutter-text: #F8F8F8;
        --scrollbar-track: #2F2F2F;
        --scrollbar-thumb: #4A4A4A;
    }
}

[data-theme="light"] {
    --background: #FFFFFF;
    --text-color: #4D4D4C;
    --toolbar-bg: #EFEFEF;
    --toolbar-border: #D6D6D6;
    --gutter-bg: #EFEFEF;
    --gutter-text: #4D4D4C;
    --scrollbar-track: #F5F5F5;
    --scrollbar-thumb: #B0B0B0;
}

[data-theme="dark"] {
    --background: #141414;
    --text-color: #F8F8F8;
    --toolbar-bg: #323232;
    --toolbar-border: #5F5A60;
    --gutter-bg: #323232;
    --gutter-text: #F8F8F8;
    --scrollbar-track: #2F2F2F;
    --scrollbar-thumb: #4A4A4A;
}

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background: var(--background);
    color: var(--text-color);
}

.container-fluid {
    height: 100vh;
    margin: 0;
    padding: 0;
    position: relative;
}

.split-container {
    height: 100%;
    margin: 0;
    padding: 0;
    display: flex;
}

/* Custom scrollbar styling */
html, body, .editor-wrapper, .iframe-container, #editor, #iframe, .ai-chat-content {
    scrollbar-width: thin; /* Firefox */
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track); /* Firefox */
}
html::-webkit-scrollbar, body::-webkit-scrollbar, .editor-wrapper::-webkit-scrollbar, .iframe-container::-webkit-scrollbar, #editor::-webkit-scrollbar, #iframe::-webkit-scrollbar, .ai-chat-content::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}
html::-webkit-scrollbar-track, body::-webkit-scrollbar-track, .editor-wrapper::-webkit-scrollbar-track, .iframe-container::-webkit-scrollbar-track, #editor::-webkit-scrollbar-track, #iframe::-webkit-scrollbar-track, .ai-chat-content::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
    border-radius: 4px;
}
html::-webkit-scrollbar-thumb, body::-webkit-scrollbar-thumb, .editor-wrapper::-webkit-scrollbar-thumb, .iframe-container::-webkit-scrollbar-thumb, #editor::-webkit-scrollbar-thumb, #iframe::-webkit-scrollbar-thumb, .ai-chat-content::-webkit-scrollbar-thumb {
    background-color: var(--scrollbar-thumb);
    border-radius: 4px;
    border: 2px solid var(--scrollbar-track); /* Ensures a clean edge */
}
html::-webkit-scrollbar-thumb:hover, body::-webkit-scrollbar-thumb:hover, .editor-wrapper::-webkit-scrollbar-thumb:hover, .iframe-container::-webkit-scrollbar-thumb:hover, #editor::-webkit-scrollbar-thumb:hover, #iframe::-webkit-scrollbar-thumb:hover, .ai-chat-content::-webkit-scrollbar-thumb:hover {
    background-color: color-mix(in srgb, var(--scrollbar-thumb), #FFFFFF 20%);
}

.editor-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    width: 25%; /* Set initial width explicitly */
    min-width: 50px; /* Minimum width when visible */
    position: relative; /* Contain children when visible */
}

.editor-container.hidden {
    width: 0 !important; /* Force width to 0 when hidden */
    min-width: 0 !important;
    overflow: hidden;
}

.editor-buttons {
    background-color: var(--toolbar-bg);
    border-bottom: 1px solid var(--toolbar-border);
    flex-shrink: 0; /* Prevent buttons from shrinking */
    display: flex;
    align-items: center;
    z-index: 1000; /* Stay above other content */
    padding: 2px 8px; /* Consistent padding, right padding for buffer */
    box-sizing: border-box; /* Ensure padding is included in width */
}

/* When editor is visible, .editor-buttons is in flow */
.editor-container:not(.hidden) .editor-buttons {
    position: static; /* Part of the DOM flow */
    width: 100%; /* Full width of the pane */
}

/* When editor-buttons is absolute (handled by JS), it’s an overlay fitting content */
.editor-buttons.absolute {
    position: absolute;
    top: 0;
    left: 0;
    width: auto; /* Fit content naturally */
    min-width: 150px; /* Minimum to prevent collapse, adjust as needed */
    background-color: var(--toolbar-bg); /* Ensure background is applied */
}

/* Hide and collapse Save, Font Size, and AI Chat Toggle when editor is hidden */
.editor-container.hidden #toggleAiChatButton,
.editor-buttons.absolute #saveButton,
.editor-buttons.absolute .font-size-control {
    display: none !important;
}

/* Ensure all buttons are visible by default when editor is visible */
.editor-buttons #toggleEditorButton,
.editor-buttons #reloadButton,
.editor-buttons #loadButton,
.editor-buttons #saveButton,
.editor-buttons #exportPngButton,
.editor-buttons #exportCommentsToClipboardButton,
.editor-buttons #exportCommentsToMdButton,
.editor-buttons #themeToggleButton,
.editor-buttons #infoButton,
.editor-buttons .font-size-control,
.editor-container:not(.hidden) .editor-buttons #toggleAiChatButton {
    display: inline-block; /* Visible unless overridden */
}

/* Font size control styling */
.font-size-control {
    background-color: var(--toolbar-bg);
    color: var(--text-color);
    margin-right: 8px;
}

.font-size-control label {
    color: var(--text-color);
    font-size: 0.75rem; /* Matches .small from Bootstrap */
    margin-bottom: 0; /* Remove default margin for inline alignment */
}

.font-size-control .form-select {
    background-color: var(--toolbar-bg);
    color: var(--text-color);
    border: 1px solid var(--toolbar-border);
    padding: 0.25rem 1.5rem 0.25rem 0.5rem; /* Adjust padding for small size */
    font-size: 0.75rem; /* Matches .form-select-sm */
    line-height: 1.5;
    border-radius: 0.2rem;
}

.font-size-control .form-select:focus {
    border-color: color-mix(in srgb, var(--toolbar-border), #FFFFFF 20%);
    box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--toolbar-border), #FFFFFF 10%);
    outline: none;
}

.font-size-control .form-select:hover {
    background-color: color-mix(in srgb, var(--toolbar-bg), #FFFFFF 10%);
}

.editor-split {
    flex: 1; /* Take up remaining space */
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden; /* Prevent content from overflowing */
}

.editor-upper {
    flex: 1; /* Take available space */
    position: relative;
    overflow: hidden;
}

.ai-chat-pane {
    height: 0; /* Hidden by default */
    overflow: hidden;
    position: relative;
    background: var(--background);
    border-top: 1px solid var(--toolbar-border);
}

.ai-chat-pane.expanded {
    height: 30%; /* Default height when expanded */
}

.ai-chat-content {
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    margin-bottom: 8px;
    padding: 8px;
    background: color-mix(in srgb, var(--background), #000000 10%);
    border-radius: 4px;
}

.ai-chat-messages .message {
    margin-bottom: 12px;
}

.ai-chat-messages .message.user {
    text-align: right;
}

.ai-chat-messages .message.assistant {
    text-align: left;
}

.ai-chat-messages .message-content {
    display: inline-block;
    padding: 8px 12px;
    border-radius: 8px;
    max-width: 80%;
}

.ai-chat-messages .message.user .message-content {
    background: var(--toolbar-bg);
    color: var(--text-color);
}

.ai-chat-messages .message.user .message-content pre,
.ai-chat-messages .message.user .message-content code {
    background: color-mix(in srgb, var(--background), #EFEFEF 20%);
}

.ai-chat-messages .message.assistant .message-content {
    max-width: 100%;
    background: color-mix(in srgb, var(--background), #FFFFFF 20%);
    color: var(--text-color);
}

.ai-chat-messages .message.assistant .message-content pre,
.ai-chat-messages .message.assistant .message-content code {
    background: color-mix(in srgb, var(--toolbar-bg), #EFEFEF 20%);
}

.ai-chat-messages .message.user .delete-button-container {
    display: flex;
    justify-content: flex-end;
    margin-top: 4px;
}

.ai-chat-messages .message.user .delete-message-btn {
    font-size: 0.35rem;
    padding: 0.1rem 0.2rem;
    line-height: 1;
    min-width: 16px;
    min-height: 16px;
}

.ai-chat-input {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.ai-chat-input textarea {
    resize: none;
    height: 60px;
    margin-bottom: 8px;
}

.ai-chat-buttons {
    display: flex;
    justify-content: flex-end;
}

.ai-settings-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    visibility: hidden;
    opacity: 0;
    transition: visibility 0s linear 0.3s, opacity 0.3s ease;
}

.ai-settings-overlay:not(.hidden) {
    visibility: visible;
    opacity: 1;
    transition: visibility 0s linear, opacity 0.3s ease;
}

.ai-settings-content {
    background: var(--background);
    color: var(--text-color);
    width: 400px;
    max-height: 80vh;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    position: relative;
    overflow-y: auto;
}

.ai-settings-content h3 {
    margin-top: 0;
}

.ai-settings-content .form-control {
    background-color: color-mix(in srgb, var(--background), #FFFFFF 10%);
    color: var(--text-color);
    border: 1px solid var(--toolbar-border);
}

.ai-settings-content textarea.form-control {
    resize: vertical;
    min-height: 80px;
}

#ai-settings-close-btn {
    position: absolute;
    top: 10px;
    right: 10px;
}

/* Adjust editor-wrapper to sit below editor-buttons */
.editor-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
}

.editor-content {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

.iframe-container {
    height: 100%;
    flex: 1; /* Take up remaining space */
    width: 75%; /* Set initial width explicitly */
    padding: 0;
}

#iframe {
    width: 100%;
    height: 100%;
    border: none;
    box-sizing: border-box;
}

#editor {
    width: 100%;
    height: 100%;
    border: none;
    box-sizing: border-box;
}

.ace_gutter {
    margin: 0 !important;
    padding: 0 !important;
    background: var(--gutter-bg) !important;
    color: var(--gutter-text) !important;
}

.ace_content {
    margin: 0 !important;
    padding: 0 !important;
}

/* Split.js gutter styles */
.gutter {
    background-color: var(--toolbar-border);
    background-repeat: no-repeat;
    background-position: 50%;
    margin: 0; /* Remove any default margins */
    padding: 0; /* Remove any default padding */
}

.gutter.gutter-horizontal {
    cursor: col-resize;
    background-color: var(--toolbar-border);
    width: 3px;
}

.gutter.gutter-vertical {
    cursor: row-resize;
    background-color: var(--toolbar-border);
    height: 3px;
}

.split, .gutter.gutter-horizontal, .gutter.gutter-vertical {
    box-sizing: border-box;
}