:root {
    --primary-color: #007bff;
    --secondary-color: #6c757d;
    --success-color: #28a745;
    --info-color: #17a2b8;
    --danger-color: #dc3545;
    --warning-color: #ffc107; /* Added warning color */
    --light-color: #f8f9fa;
    --dark-color: #343a40;
    --border-color: #dee2e6;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 15px;
    background-color: var(--light-color);
    margin: 0;
    min-height: 100vh;
    box-sizing: border-box;
    /* Prevent pull-to-refresh and other touch gestures interfering with drawing */
    overscroll-behavior-y: contain;
}

h1 {
    color: var(--dark-color);
    margin-bottom: 15px;
    text-align: center;
}

#editor-container {
    display: flex;
    gap: 20px;
    width: 100%;
    max-width: 1400px;
    background-color: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    flex-grow: 1;
    box-sizing: border-box;
    /* Ensure container adapts height on smaller screens */
    min-height: 0; /* Override default min-height if needed */
    flex-direction: column; /* Stack toolbar/canvas on smaller screens */
}

@media (min-width: 768px) { /* Apply side-by-side layout on larger screens */
    #editor-container {
        flex-direction: row;
    }
}

#toolbar {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-right: 0; /* Remove padding on vertical layout */
    border-right: none; /* Remove border on vertical layout */
    border-bottom: 1px solid var(--border-color); /* Add border below on vertical */
    padding-bottom: 15px; /* Add padding below on vertical */
    min-width: unset; /* Allow toolbar to shrink */
    width: 100%; /* Take full width on vertical */
    overflow-y: visible; /* Don't scroll toolbar vertically by default */
    flex-shrink: 0; /* Prevent toolbar from shrinking too much */
}

@media (min-width: 768px) {
    #toolbar {
        padding-right: 20px;
        border-right: 1px solid var(--border-color);
        border-bottom: none; /* Remove bottom border on horizontal */
        padding-bottom: 0; /* Remove bottom padding on horizontal */
        min-width: 240px; /* Wider toolbar on larger screens */
        width: auto; /* Allow natural width based on content */
        overflow-y: auto; /* Allow scrolling if too tall */
        max-height: calc(100vh - 100px); /* Limit height, adjust as needed */
    }
}


#toolbar h3 {
    margin-top: 5px;
    margin-bottom: 10px;
    font-size: 1.1em;
    color: var(--dark-color);
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
}
#toolbar h4 { /* Style for options sections */
    margin-top: 0;
    margin-bottom: 10px;
    font-size: 1em;
    color: var(--secondary-color);
}

/* General Button Styling */
#toolbar button, #toolbar select {
    padding: 8px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 0.95em;
    cursor: pointer;
    background-color: #fff;
    transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
    display: flex;
    align-items: center;
    gap: 8px; /* Increased gap */
    width: 100%;
    box-sizing: border-box;
    text-align: left;
    line-height: 1.2; /* Prevent icons pushing text down */
}
#toolbar select { padding: 8px 6px;} /* Adjust select padding */

#toolbar input[type="file"] { display: none; }

#toolbar button:hover:not(:disabled) {
    background-color: #e9ecef;
    border-color: #adb5bd;
}

#toolbar button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    background-color: #e9ecef !important; /* Ensure disabled style overrides active */
    color: #6c757d !important;
    border-color: #ced4da !important;
}

/* Active Tool Button Style */
#toolbar button.active {
    background-color: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}
#toolbar button.active:hover:not(:disabled) {
    background-color: #0056b3;
}

/* Tool Grid for compact layout */
.tool-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); /* Adjust minmax for button size */
    gap: 5px;
}
.tool-button {
    justify-content: center; /* Center icon in grid button */
    padding: 8px; /* Adjust padding for grid buttons */
    min-height: 40px; /* Ensure consistent button height */
}
.tool-button span { /* Ensure icon is centered */
   margin: auto;
}


#toolbar label {
    display: block;
    margin-bottom: 5px;
    font-size: 0.9em;
    color: var(--secondary-color);
    font-weight: 500;
}

#toolbar input[type="color"] {
    padding: 0;
    height: 28px; /* Slightly smaller */
    width: 35px;
    border: 1px solid #ccc;
    vertical-align: middle;
    cursor: pointer;
    margin-left: 5px;
}
 #toolbar input[type="range"] { width: calc(100% - 24px); cursor: pointer;}
 #toolbar input[type="number"] { width: 60px; padding: 5px; margin-left: 5px;}

 .toolbar-section {
     margin-bottom: 15px;
     padding-bottom: 10px;
     border-bottom: 1px solid #f1f1f1;
 }
 .toolbar-section:last-child { border-bottom: none; }

 #toolbar .tool-options {
     padding-left: 0; /* Remove indent */
     border-left: none; /* Remove border */
     margin-top: 10px;
     display: none; /* Hidden by default */
     background-color: #f8f9fa;
     padding: 10px;
     border-radius: 4px;
     border: 1px solid var(--border-color); /* Add border for visual grouping */
 }
 #toolbar .tool-options.active {
     display: block;
     animation: fadeIn 0.3s ease;
 }

 .color-palette {
    display: flex;
    gap: 5px;
    margin-top: 5px;
    margin-bottom: 10px;
 }
 .palette-color {
    width: 20px;
    height: 20px;
    border-radius: 3px;
    cursor: pointer;
    display: inline-block;
    border: 1px solid rgba(0,0,0,0.2);
    transition: transform 0.1s ease;
 }
  .palette-color:hover {
     transform: scale(1.1);
  }


@keyframes fadeIn { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: translateY(0); } }

#canvas-container {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overflow: auto;
    background-color: #e0e0e0; /* Slightly darker background */
    border: 1px solid var(--border-color);
    border-radius: 4px;
    position: relative;
    min-height: 300px; /* Ensure minimum height */
    width: 100%; /* Take remaining width */
}

#editorCanvas {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
    cursor: default;
    background-color: white;
    image-rendering: pixelated; /* Optional: sharper pixels for pixel art */
    /* Prevent selection during drawing */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    /* Improve touch interaction */
    touch-action: none; /* Disable browser default touch actions like pan/zoom on the canvas */

}

 #text-input-overlay {
     position: absolute;
     padding: 5px;
     background: rgba(255, 255, 255, 0.95); /* More opaque */
     border: 1px solid var(--primary-color);
     border-radius: 3px;
     box-shadow: 0 2px 5px rgba(0,0,0,0.2);
     z-index: 10;
     display: none;
 }
  #text-input-overlay input {
      border: none;
      outline: none;
      background: transparent;
      font-size: 16px; /* Default size */
      padding: 2px;
  }

 #brush-preview {
    position: absolute;
    border: 1px solid black;
    border-radius: 50%;
    pointer-events: none;
    display: none;
    z-index: 15;
    background-color: rgba(0,0,0, 0.3);
 }

.file-upload-label {
    display: inline-block;
    padding: 8px 12px;
    background-color: var(--success-color);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    text-align: center;
    width: 100%;
    box-sizing: border-box;
    transition: background-color 0.2s ease;
    font-size: 0.95em;
}
.file-upload-label:hover {
    background-color: #218838;
}
#fileName { font-size: 0.85em; color: #666; margin-top: 5px; word-break: break-all; }

 #status-bar {
     margin-top: 10px;
     padding: 8px 12px; /* More padding */
     font-size: 0.9em;
     color: var(--secondary-color);
     min-height: 1.2em;
     text-align: center;
     width: 100%;
     max-width: 1400px;
     background-color: #fff;
     border-radius: 4px;
     box-shadow: 0 2px 4px rgba(0,0,0,0.05);
     box-sizing: border-box; /* Include padding in width */
 }
  #status-bar.error { color: var(--danger-color); font-weight: bold; }
  #status-bar.success { color: var(--success-color); }
  #status-bar.warning { color: var(--dark-color); background-color: var(--warning-color); border: 1px solid #e6ac00; } /* Warning style */

 .hidden { display: none !important; }
