/* ==========================================================================
   Widget and Container Setup
   ========================================================================== */
.widget-container {
    width: 100%;
    max-width: 80rem;
    background-color: #ffffff;
    border-radius: 1rem;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    position: relative;
    margin: 0 auto; /* Ensures it stays centered */
}

@media (max-width: 640px) {
    body {
        padding: 0.5rem;
    }
    .widget-container {
        padding: 0.75rem;
    }
}

/* ==========================================================================
   Chart Main Area
   ========================================================================== */
#chart-main {
    width: 100%;
    flex-grow: 1;
    min-height: 400px;
}

@media (max-height: 500px) and (orientation: landscape) {
    #chart-main {
        height: 85vh;
    }
}

#chart-container:fullscreen #chart-main,
.pseudo-fullscreen #chart-main {
    height: 80vh;
}

@media (max-width: 640px) and (orientation: portrait) {
    #chart-container:fullscreen #chart-main,
    .pseudo-fullscreen #chart-main {
        height: 65vh;
    }
}

/* ==========================================================================
   Header and Title Text
   ========================================================================== */
.header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.title-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-grow: 1;
    min-width: 0;
    position: relative;
}

#chart-title {
    font-family: inherit; 
    font-size: 1.5rem !important; /* Overrides default span sizing */
    font-weight: bold !important;
    text-align: center !important;
    background-color: transparent !important;
    padding: 0.25rem 0.5rem;
    display: inline-block;
    max-width: 100%; 
    min-width: 2rem;
    white-space: pre-wrap;
    word-wrap: break-word;
    outline: none;
    border: none !important;
    border-radius: 0.375rem;
}

#chart-title:focus {
    box-shadow: 0 0 0 2px #3b82f6;
}

#chart-title[contenteditable="false"] {
    color: #111827 !important;
    background-color: transparent;
    opacity: 1;
    -webkit-text-fill-color: #111827 !important;
}

/* Restores the placeholder on empty contenteditable spans */
#chart-title:empty::before,
#description:empty::before {
    content: attr(placeholder);
    color: #9ca3af;
    font-style: italic;
    pointer-events: none;
    display: inline-block;
}

/* ==========================================================================
   Edit Buttons
   ========================================================================== */
#edit-title-btn, 
#edit-description-btn {
    border: none !important;
    background: transparent !important; /* Strips gray background from global button styles */
    background-color: transparent !important;
    box-shadow: none !important;
    cursor: pointer;
    opacity: 0.5;
    transition: opacity 0.2s;
    flex-shrink: 0;
    padding: 0 !important;
}

#edit-title-btn {
    font-size: 1.2rem;
    margin-left: 8px;
    width: 24px;
    /* Negative margin negates the width so the title remains perfectly centered! */
    margin-right: -32px; 
}

#edit-description-btn {
    font-size: 1rem;
    margin-left: 8px;
    margin-top: 4px;
}

#edit-title-btn:hover, 
#edit-description-btn:hover { 
    opacity: 1; 
}

.chart-subtitle {
    text-align: center;
    font-size: 0.9rem;
    color: #6b7280;
    margin-top: 0.25rem;
    margin-bottom: 0.5rem;
}

/* ==========================================================================
   Description Text
   ========================================================================== */
.description-container {
    display: flex;
    align-items: flex-start;
    justify-content: flex-start;
    width: 100%;
    margin-top: 0.5rem;
    margin-bottom: 0.5rem;
    padding: 0;
    box-sizing: border-box;
}

#description {
    font-family: inherit;
    font-size: 0.85rem;
    color: #6b7280;
    text-align: left !important;
    background-color: transparent !important;
    padding: 0.25rem 0.5rem;
    display: inline-block;
    max-width: 100%;
    min-width: 4rem;
    min-height: 1.4rem;
    white-space: pre-wrap;
    word-wrap: break-word;
    outline: none;
    border: none !important;
    border-radius: 0.375rem;
    line-height: 1.4;
}

#description:focus {
    box-shadow: 0 0 0 2px #3b82f6;
}

#description[contenteditable="false"] {
    color: #4b5563;
    background-color: transparent;
    opacity: 1;
    -webkit-text-fill-color: #4b5563;
}

/* ==========================================================================
   Controls Section
   ========================================================================== */
.controls-container {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.control-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

.control-group.disabled {
    color: #9ca3af;
    cursor: not-allowed;
}

@media (min-width: 640px) {
    .control-group {
        margin-top: 0;
        margin-left: 1rem;
    }
}

/* ==========================================================================
   Buttons and Inputs
   ========================================================================== */
.control-btn, .settings-input {
    padding: 0.25rem 0.75rem;
    font-size: 0.875rem;
    font-weight: 500;
    border-radius: 0.375rem;
    border: 1px solid #d1d5db;
    background-color: #f9fafb;
    color: #1f2937;
    transition: background-color 0.2s, color 0.2s;
    cursor: pointer;
}

.control-btn:hover { 
    background-color: #f3f4f6; 
}

.control-btn.active {
    background-color: #3b82f6;
    color: #ffffff;
    border-color: #3b82f6;
}

.control-btn.btn-dirty {
    background-color: #3b82f6;
    color: #ffffff;
    border-color: #3b82f6;
}

.control-btn:disabled { 
    opacity: 0.5; 
    cursor: not-allowed; 
}

.settings-input:focus {
    outline: 2px solid transparent;
    outline-offset: 2px;
    box-shadow: 0 0 0 2px #3b82f6;
}

/* ==========================================================================
   Custom Legend
   ========================================================================== */
#custom-legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: flex-start;
    gap: 0.5rem 1rem;
    padding: 0.5rem;
    padding-right: 3.5rem;
    box-sizing: border-box;
}

.legend-item-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: background-color 0.2s;
    border-radius: 0.5rem;
}

.legend-item {
    display: flex;
    align-items: center;
    padding: 0.25rem;
    border-radius: 0.375rem;
    user-select: none;
    -webkit-user-select: none;
}

.legend-color-swatch {
    width: 12px;
    height: 12px;
    margin-right: 8px;
    border-radius: 2px;
    flex-shrink: 0;
}

.legend-name-toggle { 
    font-weight: 500; 
    cursor: pointer; 
}

.legend-name-toggle.deselected {
    color: #9ca3af;
    text-decoration: line-through;
}

.legend-name-toggle.disabled-toggle {
    cursor: default;
    pointer-events: none;
}

/* ==========================================================================
   Legend Settings Button
   ========================================================================== */
.settings-btn, .close-settings-btn {
    border: none;
    background: none;
    padding: 0 0 0 8px;
    font-size: 1.5em;
    line-height: 1;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.2s;
    vertical-align: middle;
}

.settings-btn { 
    color: #6b7280; 
}

.close-settings-btn { 
    color: #9ca3af; 
    font-size: 1.2em; 
    padding: 0 0 0 12px; 
}

.settings-btn:hover, 
.close-settings-btn:hover { 
    opacity: 1; 
}

/* ==========================================================================
   Series Settings Panel
   ========================================================================== */
.series-settings-panel {
    display: none;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.5rem;
    margin-top: 0.25rem;
    background-color: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    position: relative;
}

.series-settings-panel.active {
    display: flex;
}

.settings-group {
    display: flex;
    flex-direction: column;
}

.line-specific-settings {
    display: contents;
}

.settings-group label {
    font-size: 0.75rem;
    color: #6b7280;
    margin-bottom: 0.125rem;
}

input[type="color"] {
    -webkit-appearance: none;
    width: 32px;
    height: 32px;
    border: none;
    padding: 0;
    background: none;
}

input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

input[type="color"]::-webkit-color-swatch {
    border: 1px solid #d1d5db;
    border-radius: 0.375rem;
}

/* ==========================================================================
   Drag and Drop Styles
   ========================================================================== */
.legend-item[draggable="true"] {
    cursor: grab;
    touch-action: none;
}

.legend-item-wrapper.dragging {
    opacity: 0.4;
}

.legend-item-wrapper.drag-over {
    background-color: #eef2ff;
    border-radius: 0.5rem;
}

.legend-ghost {
    position: absolute;
    pointer-events: none;
    z-index: 1001;
    opacity: 0.8;
    background-color: #e0e7ff;
    border-radius: 0.5rem;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

/* ==========================================================================
   Zoom Bar Label
   ========================================================================== */
.zoom-bar-label {
    text-align: center;
    font-size: 0.8rem;
    color: #6b7280;
    margin-top: 0.5rem;
}

/* ==========================================================================
   Fullscreen Controls
   ========================================================================== */
#fullscreen-btn, 
#exit-fullscreen-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    z-index: 50;
    padding: 0.5rem;
    line-height: 1;
}

.pseudo-fullscreen {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100% !important;
    z-index: 9999 !important;
    background: #ffffff !important;
    padding: 1rem !important;
    box-sizing: border-box !important;
    margin: 0 !important;
    border-radius: 0 !important;
    display: flex !important;
    flex-direction: column !important;
}

#chart-container:fullscreen { 
    background-color: #ffffff; 
    padding: 1.5rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

/* ==========================================================================
   Loading Overlay
   ========================================================================== */
#loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.8);
    z-index: 1000;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 1rem;
}

.spinner {
    border: 5px solid #f3f3f3;
    border-top: 5px solid #3b82f6;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}
