.python-widget {
    font-family: 'Inter', sans-serif;
    background-color: #ddd; /* <-- CHANGED */
    padding: 0.75rem;
    border-radius: 0.5rem;
    /* box-shadow: 0 4px 12px rgba(0,0,0,0.05), 0 1px 3px rgba(0,0,0,0.08); */
    margin-bottom: 1.5rem;
    border: 1px solid #888; /* <-- CHANGED */
    position: relative; 
    font-size: 14px; /* <-- FIX: Set base font size for widget */
}

/* Base CodeMirror overrides */
.python-widget .CodeMirror {
    line-height: 1.4;
    font-family: 'Fira Code', monospace;
    font-size: 1em; /* <-- FIX: Relative to widget base */
}
.python-widget .CodeMirror-gutters {
    border-right: none !important;
    background-color: inherit !important; /* Ensure gutter matches theme */
}
.python-widget .CodeMirror-line {
    overflow-wrap: anywhere !important;
}

/* Default Grid: 1 column, rows for editor, buttons, output */
.python-widget-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.75rem;
    align-items: start; 
}
.python-widget-grid > div:not(.python-widget-h-separator):not(.python-widget-readonly-buttons-horizontal):not(.python-widget-button-container) {
    display: flex;
    flex-direction: column;
    min-height: 0; 
}

.python-widget-subtitle {
    font-size: 0.928em; /* 13px / 14px */
    font-weight: 600;
    color: #4b5563;
    margin-top: 0px;
    margin-bottom: 0.5rem; 
    flex-shrink: 0;
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

/* Theme Selector */
.python-widget-theme-selector-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.python-widget-theme-selector-container label {
    display: inline-flex;
    align-items: center;
    padding: 6px; /* 6px */
    border-radius: 0.25rem;
    color: #4b5563;
    cursor: pointer;
    transition: background-color 0.2s;
}
.python-widget-theme-selector-container label:hover {
    background-color: #f3f4f6;
}
.python-widget-theme-selector {
    padding: 0.375rem 2rem 0.375rem 0.75rem; /* <-- CHANGED (T, R, B, L) */
    border-radius: 0.375rem;
    border: 1px solid #d1d5db;
    font-family: 'Inter', sans-serif;
    font-size: 1em; /* <-- FIX: Relative to widget base */
    background-color: #ffffff;

    /* --- The Real Solution --- */
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    
    /* Custom arrow */
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23374151' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 0.6rem center; /* Position the arrow */
    background-size: 1em;
    /* --- End Real Solution --- */
}

/* Button container layout - Default for vertical/narrow */
.python-widget-button-container {
    display: flex;
    flex-wrap: wrap; /* Allow wrapping on small screens */
    gap: 0.75rem;
    justify-content: space-between;
    align-items: center; 
    padding: 0.25rem;
}

.python-widget-action-buttons {
    display: flex;
    flex-wrap: wrap; /* Allow buttons themselves to wrap if needed */
    gap: 0.5rem;
}

.python-widget-btn {
    font-family: 'Inter', sans-serif;
    font-weight: 600;
    font-size: 0.928em; /* 13px / 14px */
    padding: 0.5rem; 
    width: 34px; 
    height: 34px; 
    border-radius: 0.375rem;
    border: none;
    color: white;
    cursor: pointer;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px; 
}
.python-widget-btn svg {
    display: block;
    width: 16px; 
    height: 16px; 
}
.python-widget-theme-selector-container label svg {
    width: 16px;
    height: 16px;
}
.python-widget-btn:not(:disabled):hover {
    filter: brightness(1.1);
}
.python-widget-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.python-widget-btn-run { background-color: #2563eb; }
.python-widget-btn-stop { background-color: #dc2626; }
.python-widget-btn-reset { background-color: #f97316; }
.python-widget-btn-clear { background-color: #6b7280; }
.python-widget-btn-print { background-color: #16a34a; }

.python-widget .python-widget-error-text { 
    color: #f87171; 
    font-weight: 500;
}
.python-widget .python-widget-interrupt-text { 
    color: #facc15; /* yellow-400 */
    font-weight: 500;
}

.python-widget-h-separator {
    display: none; 
    cursor: col-resize;
    width: 0.75rem; 
    height: 100%;
    align-self: stretch;
    position: relative; 
    background-color: transparent; 
}
.python-widget-h-separator::before {
    content: '';
    position: absolute;
    top: 50%;
    left: calc(50% - 2px); 
    transform: translateY(-50%); 
    width: 4px; 
    height: 48px; 
    background-color: #b0b0b0; /* <-- CHANGED */
    border-radius: 2px;
    transition: background-color 0.2s;
}
.python-widget-h-separator:hover::before {
    background-color: #999; /* <-- CHANGED */
}

/* --- READ-ONLY STYLES --- */
.python-widget-readonly-buttons-vertical {
    display: flex; 
    flex-wrap: wrap;
    gap: 0.75rem;
    justify-content: center; 
    margin-top: 0.75rem; 
}
.python-widget-readonly-buttons-horizontal {
    display: none; 
}

/* Read-only styles for CodeMirror */
.python-widget-readonly .CodeMirror,
.python-widget .CodeMirror-readonly {
    cursor: default;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/*
 * DELETED the broken CSS rules that were here.
 * The JS now handles forcing a light theme.
*/

.python-widget-readonly .CodeMirror-gutters {
    background-color: inherit !important; /* Force gutter to match theme */
}
.python-widget-readonly .CodeMirror-cursors,
.python-widget .CodeMirror-readonly .CodeMirror-cursors {
     display: none !important;
}


/* --- MEDIA QUERY for layout (desktop) --- */
@media (min-width: 768px) {

    /* 1. NON-Readonly Horizontal Layout */
    .python-widget-orientation-horizontal:not(.python-widget-readonly) .python-widget-grid {
        grid-template-columns: 1fr 0.75rem 1fr; 
        grid-template-rows: 1fr auto; 
        gap: 0; 
        row-gap: 0.75rem; 
        align-items: stretch; 
    }
    .python-widget-orientation-horizontal:not(.python-widget-readonly) .python-widget-grid > div:nth-child(1) { /* Editor Container */
        grid-column: 1 / 2;
        grid-row: 1 / 2;
    }
    .python-widget-orientation-horizontal:not(.python-widget-readonly) .python-widget-h-separator { /* Separator */
        display: block;
        grid-column: 2 / 3;
        grid-row: 1 / 2;
    }
    .python-widget-orientation-horizontal:not(.python-widget-readonly) .python-widget-grid > div:nth-child(4) { /* Output Container */
        grid-column: 3 / 4;
        grid-row: 1 / 2;
    }
    .python-widget-orientation-horizontal:not(.python-widget-readonly) .python-widget-grid > .python-widget-button-container { /* Button Container */
       grid-column: 1 / 4;
       grid-row: 2 / 3;
       flex-wrap: nowrap; /* No wrapping on desktop */
       padding: 0 0.25rem;
    }

    /* 2. READONLY Horizontal Layout */
    .python-widget-orientation-horizontal.python-widget-readonly .python-widget-grid {
        grid-template-columns: 1fr auto 1fr; 
        grid-template-rows: 1fr; 
        gap: 0.75rem;
        align-items: center; 
    }
    .python-widget-orientation-horizontal.python-widget-readonly .python-widget-readonly-buttons-horizontal {
        display: flex; 
        flex-direction: column; 
        gap: 0.5rem;
    }
    .python-widget-orientation-horizontal.python-widget-readonly .python-widget-readonly-buttons-vertical {
        display: none; 
    }
}

/* Editor and Output Panel Wrapper Styles */
.python-widget-editor-wrapper {
    position: relative;
    flex-grow: 1; 
    min-height: 0; 
    border-radius: 0.375rem;
    overflow: hidden; /* Ensures CodeMirror respects border radius */
    border: 1px solid #d1d5db; /* <-- FIX: Border moved here */
}
.python-widget .CodeMirror {
    /* border: 1px solid #d1d5db; */ /* <-- FIX: Border removed here */
    border: none; /* <-- FIX: Explicitly no border */
    font-size: 1em; /* <-- FIX: Relative to widget base */
    height: 100%; 
}
.python-widget .CodeMirror-scroll {
    padding: 8px 4px 8px 0; /* <-- FIX: Reduced padding */
    overflow-y: scroll !important; 
}

.python-widget-output-wrapper {
    position: relative;
    flex-grow: 1; 
    min-height: 0;
}
.python-widget .python-widget-output-panel {
    padding: 8px 12px; /* <-- FIX: Reduced padding */
    overflow-y: auto; 
    white-space: pre-wrap;
    font-family: 'Fira Code', monospace;
    font-size: 1em; /* <-- FIX: Relative to widget base */
    border: 1px solid #d1d5db;
    height: 100%; 
    flex-grow: 1;
    border-radius: 0.375rem;
    box-sizing: border-box;
    line-height: 1.4;
}

/* Mobile Enter Button */
.python-widget-enter-btn {
    position: absolute;
    bottom: 8px;
    right: 8px;
    z-index: 10;
    background-color: #2563eb;
    color: white;
    border: none;
    border-radius: 0.375rem;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-family: 'Inter', sans-serif;
    font-weight: 600;
    font-size: 0.928em; /* 13px / 14px */
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    
    /* --- FADE --- */
    opacity: 0;
    transform: scale(0.95);
    transition: opacity 0.2s ease, transform 0.2s ease;
}
.python-widget-enter-btn.visible {
    opacity: 1;
    transform: scale(1);
}
/* --- END FADE --- */

.python-widget-enter-btn:hover {
    filter: brightness(1.1);
}

/* Flashing animation for Enter button */
@keyframes python-widget-flash {
    0%, 100% { filter: brightness(1); }
    50% { filter: brightness(1.6); }
}
.python-widget-enter-btn-flash {
    animation: python-widget-flash 1s infinite ease-in-out;
    box-shadow: 0 0 12px 2px #2563eb;
}

.python-widget-output-panel input[type="text"] {
    border: none;
    background: transparent;
    color: inherit;
    font: inherit;
    outline: none;
    padding: 0;
    margin: 0;
    /* margin-left: 4px; */ /* <-- FIX: Removed this line */
    line-height: 1.4;
}

.python-widget-copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 10;
    background-color: rgba(75, 85, 99, 0.7);
    color: white;
    border: 1px solid rgba(107, 114, 128, 0.7);
    padding: 4px;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    cursor: pointer;
    opacity: 0.3;
    transition: all 0.2s ease-in-out;
}
.python-widget-editor-wrapper:hover .python-widget-copy-btn {
    opacity: 0.7;
}
.python-widget-copy-btn:hover {
    opacity: 1;
    background-color: rgba(55, 65, 81, 0.8);
}
.python-widget-copy-btn.copied {
    background-color: #16a34a;
    border-color: #15803d;
    opacity: 1;
}
.python-widget-copy-btn svg {
    width: 14px;
    height: 14px;
}

/* Theme-specific styles for output panel */
.python-widget .output-panel-cobalt { background-color: #002240; color: #f8f8f8; border-color: #002240; }
.python-widget .output-panel-lucario { background-color: #2B3E50; color: #F8F8F2; border-color: #2B3E50;}
.python-widget .output-panel-mbo { background-color: #2c2c2c; color: #d0d0d0; border-color: #2c2c2c;}
.python-widget .output-panel-zenburn { background-color: #3f3f3f; color: #dcdccc; border-color: #3f3f3f;}
.python-widget .output-panel-default { background-color: #ffffff; color: #000000; border-color: #d1d5db; }
.python-widget .output-panel-eclipse { background-color: #ffffff; color: #333333; border-color: #d1d5db; }

/* Dark theme output input */
.python-widget .output-panel-cobalt input,
.python-widget .output-panel-lucario input,
.python-widget .output-panel-mbo input,
.python-widget .output-panel-zenburn input {
    caret-color: #f8f8f8;
}
/* Light theme output input */
.python-widget .output-panel-default input,
.python-widget .output-panel-eclipse input {
    caret-color: #333333;
}


/* Modal Styles */
.python-widget-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    font-family: 'Inter', sans-serif;
}
.python-widget-modal {
    background: white;
    padding: 1.5rem 2rem 2rem 2rem;
    border-radius: 0.5rem;
    width: 90%;
    max-width: 400px;
    color: #333;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
}
.python-widget-modal-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    color: #1f2937;
}
.python-widget-modal label {
    font-size: 1em; /* <-- FIX: Relative to widget base */
    font-weight: 500;
    color: #374151;
}
.python-widget-modal-input {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid #ccc;
    border-radius: 0.375rem;
    margin-top: 0.25rem;
    margin-bottom: 1rem;
    font-family: 'Inter', sans-serif;
    font-size: 1em; /* <-- FIX: Relative to widget base */
    box-sizing: border-box; /* Important */
}
.python-widget-modal-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    margin-top: 1.5rem;
}
.python-widget-modal-btn-submit {
    background-color: #2563eb;
}
.python-widget-modal-btn-cancel {
    background-color: #6b7280;
}

/* Resize Handle */
.python-widget-resize-handle {
    position: absolute;
    bottom: 0px;
    right: 0px;
    width: 20px;
    height: 20px;
    cursor: se-resize;
    z-index: 10;
    opacity: 0.6;
    background-image: url('data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M11 1L1 11" stroke="%23888" stroke-width="1.5" stroke-linecap="round"/><path d="M11 5L5 11" stroke="%23888" stroke-width="1.5" stroke-linecap="round"/><path d="M11 9L9 11" stroke="%23888" stroke-width="1.5" stroke-linecap="round"/></svg>');
    background-repeat: no-repeat;
    background-position: bottom 4px right 4px;
}
.python-widget-resize-handle:hover {
    opacity: 1;
}