:root {
  --primary-color: #4ecdc4;
  --secondary-color: #ff6b6b;
  --success-color: #4CAF50;
  --error-color: #ff4444;
  --button-radius: 8px;
  --text-primary: #222;
  --text-secondary: #555;
  --border-color: #ccc;
  --border-light: #eee;
  --bg-light: #f5f5f5;
  --bg-white: #fff;
  --box-shadow-light: 0 1px 5px rgba(0, 0, 0, 0.1);
  --box-shadow-hover: 0 4px 15px rgba(0, 0, 0, 0.2);
  --pulse-color: rgba(78, 205, 196, 0.7);
  --spinner-color: #4ecdc4;
  --progress-bg: #ddd;
  --info-bg: rgba(0, 0, 0, 0.6);
  --icon-hover-bg: rgba(78, 205, 196, 0.15);
  --icon-remove-hover-bg: rgba(255, 68, 68, 0.15);
}

body {
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100vh;
  margin: 0;
  font-family: 'Segoe UI', sans-serif;
  background-color: var(--bg-light);
  color: var(--text-primary);
}

.container {
  width: 100%;
  max-width: 1200px;
  padding: 15px;
  display: flex;
  flex-wrap: wrap;
  /* Initial wrap for desktop layout */
  gap: 20px;
}

header#mainHeader {
  /* Target specific header */
  width: 100%;
  text-align: center;
  margin-bottom: 10px;
}

/* Hide main header when preview is shown */
header#mainHeader.hidden {
  display: none;
}


h1 {
  font-size: clamp(1rem, 3vw, 1.5rem);
  font-weight: 700;
  margin-bottom: 10px;
  color: var(--text-primary);
  position: relative;
  display: inline-block;
}

h1::after {
  content: '';
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 100%;
  height: 3px;
  background-color: var(--primary-color);
}

.upload-section {
  border: 2px dashed var(--primary-color);
  padding: 15px;
  border-radius: var(--button-radius);
  transition: all 0.3s ease;
  width: 100%;
  max-width: 500px;
  min-height: 70px;
  background-color: var(--primary-color);
  color: var(--bg-white);
  text-align: center;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--box-shadow-light);
  margin: 0 auto 20px;
}

.upload-section:hover {
  background-color: #3abead;
  border-color: #3abead;
}

.upload-section.dragover {
  border-color: var(--secondary-color);
  transform: scale(1.02);
  background-color: #3abead;
}

.upload-section label {
  font-weight: bold;
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 1.1rem;
}

.upload-section label i {
  font-size: 1.3rem;
}

/* --- Desktop Layout --- */
.left-panel {
  flex: 1;
  /* Takes remaining space */
  min-width: 300px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  order: 1;
  /* Show preview first */
}

.settings {
  flex: 0 0 320px;
  /* Fixed width for settings */
  order: 2;
  /* Show settings second */
  width: 320px;
  /* Ensure width is set */
}

/* Hide settings until first image uploaded */
.settings.hidden {
  display: none;
}

.preview-box,
.resized-box {
  width: 100%;
  padding: 20px;
  background: var(--bg-white);
  border-radius: var(--button-radius);
  box-shadow: var(--box-shadow-light);
  border: 1px solid var(--border-light);
  box-sizing: border-box;
}

.preview-box h2,
.resized-box h2 {
  font-size: 1.1rem;
  margin-top: 0;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-light);
  color: var(--text-primary);
}

/* Style for resized header area */
.resized-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-light);
  padding-bottom: 10px;
  margin-bottom: 15px;
  flex-wrap: wrap;
  /* Allow wrapping on small screens */
  gap: 10px;
  /* Add gap for wrapping */
}

.resized-header h2 {
  margin: 0;
  padding: 0;
  border: none;
  flex-shrink: 0;
  /* Prevent title from shrinking too much */
}

.resized-header label {
  font-size: 0.9rem;
  cursor: pointer;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
}

.resized-header label input[type="checkbox"] {
  vertical-align: middle;
  margin-right: 5px;
}

.resized-header label:hover {
  color: var(--text-primary);
}


#previewContainer,
#resizedContainer {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 15px;
  min-height: 150px;
  position: relative;
}

#previewContainer.hidden,
#resizedContainer.hidden {
  display: none;
}

.image-item {
  position: relative;
  aspect-ratio: 1/1;
  border-radius: var(--button-radius);
  box-shadow: var(--box-shadow-light);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  overflow: hidden;
  border: 1px solid var(--border-light);
  display: flex;
  /* Ensure img is block inside */
  align-items: center;
  justify-content: center;
  background-color: #fafafa;
  /* Light bg for image container */
}

.image-item:hover {
  transform: scale(1.05);
  box-shadow: var(--box-shadow-hover);
}

.image-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--button-radius);
  cursor: pointer;
  display: block;
}

.image-item.selected {
  outline: 3px solid var(--primary-color);
  outline-offset: -3px;
  box-shadow: 0 0 10px var(--primary-color);
}

.image-item .spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: none;
  border: 4px solid var(--bg-light);
  border-top: 4px solid var(--spinner-color);
  border-radius: 50%;
  width: 24px;
  height: 24px;
  animation: spin 1s linear infinite;
  z-index: 2;
}

.image-item.loading .spinner {
  display: block;
}

.image-item .file-size {
  position: absolute;
  bottom: 5px;
  left: 5px;
  background: var(--info-bg);
  color: var(--bg-white);
  font-size: 0.7rem;
  padding: 2px 5px;
  border-radius: 3px;
  z-index: 1;
}

.remove-btn {
  position: absolute;
  top: 5px;
  right: 5px;
  background: transparent;
  border: none;
  color: var(--secondary-color);
  font-size: 1.3rem;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.2s, color 0.2s, background-color 0.2s;
  border-radius: 50%;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  z-index: 3;
}

.remove-btn:hover {
  opacity: 1;
  color: var(--error-color);
  background-color: var(--icon-remove-hover-bg);
}

/* Styles for Resized Item & Comparison */
.resized-item {
  position: relative;
  aspect-ratio: 1/1;
  border-radius: var(--button-radius);
  box-shadow: var(--box-shadow-light);
  transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.2s;
  overflow: hidden;
  border: 1px solid var(--border-light);
  display: flex;
  /* Changed to flex for comparison mode */
  align-items: stretch;
  /* Make children fill height */
  justify-content: center;
  background-color: #fafafa;
  /* Background for container */
}

.resized-item:hover {
  transform: scale(1.03);
  box-shadow: var(--box-shadow-hover);
}

.resized-item img {
  /* General style for both images */
  height: 100%;
  display: block;
}

.resized-item .original-preview {
  display: none;
  /* Hidden by default */
  width: 50%;
  object-fit: contain;
  /* Show full original image */
  border-right: 1px solid var(--border-light);
  background-color: var(--bg-white);
  /* White bg for original */
}

.resized-item .resized-preview {
  display: block;
  /* Visible by default */
  width: 100%;
  /* Takes full width initially */
  object-fit: cover;
  /* Cover space for resized */
  background-color: var(--bg-white);
  /* White bg for resized */
}

/* Styles when comparison mode is active */
#resizedBox.comparison-mode .resized-item {
  /* Flex already set, just adjust children */
  gap: 0;
  /* No gap needed due to border */
}

#resizedBox.comparison-mode .resized-item .original-preview {
  display: block;
  /* Show original */
  width: 50%;
}

#resizedBox.comparison-mode .resized-item .resized-preview {
  width: 50%;
  /* Make resized take half */
}

#resizedBox.comparison-mode .resized-item .resized-info {
  /* Adjust info position */
  position: absolute;
  bottom: 0;
  left: 50%;
  /* Start from middle */
  right: 0;
  width: 50%;
  /* Occupy right half */
  border-bottom-left-radius: 0;
  /* Remove left radius */
  border-bottom-right-radius: var(--button-radius);
  /* Keep right */
  padding: 3px 5px;
  /* Slightly less padding */
  font-size: 0.65rem;
  /* Slightly smaller font */
}

#resizedBox.comparison-mode .resized-item .download-btn-small {
  /* Adjust download button position if needed */
  right: 5px;
  /* Keep on far right */
  top: 5px;
}

/* END Comparison Styles */

.resized-info {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--info-bg);
  color: var(--bg-white);
  font-size: 0.7rem;
  padding: 4px 6px;
  border-bottom-left-radius: var(--button-radius);
  border-bottom-right-radius: var(--button-radius);
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px;
  z-index: 1;
}

.resized-dimensions,
.resized-filesize {
  white-space: nowrap;
}

.download-btn-small {
  position: absolute;
  top: 5px;
  right: 5px;
  background: transparent;
  border: none;
  border-radius: 50%;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--primary-color);
  font-size: 1.1rem;
  opacity: 0.7;
  transition: opacity 0.2s, color 0.2s, background-color 0.2s, transform 0.2s;
  z-index: 2;
}

.download-btn-small:hover {
  opacity: 1;
  color: #3abead;
  transform: scale(1.1);
  background-color: var(--icon-hover-bg);
}

.add-image-btn {
  background: transparent;
  border: 1px dashed var(--primary-color);
  border-radius: var(--button-radius);
  width: 100%;
  height: 100%;
  aspect-ratio: 1/1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  cursor: pointer;
  color: var(--primary-color);
  font-size: 0.8rem;
  box-shadow: none;
  transition: background-color 0.2s, color 0.2s, border-color 0.2s;
  text-align: center;
  padding: 10px;
  box-sizing: border-box;
}

.add-image-btn i {
  font-size: 1.8rem;
  margin-bottom: 5px;
}

.add-image-btn:hover {
  background-color: var(--icon-hover-bg);
  color: #3abead;
  border-color: #3abead;
}

.settings-panel {
  display: block;
  background: var(--bg-white);
  padding: 20px;
  border-radius: var(--button-radius);
  box-shadow: var(--box-shadow-light);
  width: 100%;
  /* Changed from fixed width to allow flexibility */
  border: 1px solid var(--border-color);
  box-sizing: border-box;
}

.settings-panel h2 {
  margin-top: 0;
  margin-bottom: 15px;
  font-size: 1.2rem;
  border-bottom: 1px solid var(--border-light);
  padding-bottom: 10px;
}

.settings-section {
  margin-bottom: 15px;
  border-bottom: 1px solid var(--border-light);
  padding-bottom: 15px;
}

.settings-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.settings-section summary {
  cursor: pointer;
  font-weight: bold;
  margin-bottom: 10px;
  list-style: none;
  position: relative;
  padding-left: 20px;
}

.settings-section summary::-webkit-details-marker {
  display: none;
}

.settings-section summary::before {
  content: '\f078';
  font-family: 'Font Awesome 6 Free';
  font-weight: 900;
  position: absolute;
  left: 0;
  top: 2px;
  transition: transform 0.2s ease;
  color: var(--primary-color);
}

.settings-section[open] summary::before {
  transform: rotate(-180deg);
}

.input-group {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
  flex-wrap: wrap;
}

.input-field {
  flex: 1;
  min-width: 100px;
  position: relative;
  margin-bottom: 10px;
  /* Added margin for spacing when wrapped */
}

/* Ensure last field in group has no bottom margin only if not wrapped column */
.input-group:not(.flex-col) .input-field:last-child {
  margin-bottom: 0;
}

.input-field label {
  display: block;
  margin-bottom: 5px;
  font-weight: 600;
  color: var(--text-primary);
  font-size: 0.9rem;
}

.input-field label input[type="checkbox"] {
  margin-right: 8px;
  vertical-align: middle;
  border: 1px solid var(--border-color);
}

.quality-input-group {
  display: flex;
  align-items: center;
  gap: 10px;
  /* Spacing between slider and number input */
}

/* Quality Slider width increased */
.quality-input-group input[type="range"] {
  flex-grow: 1;
  /* Slider takes available space */
}

/* Quality Number Input width reduced */
.quality-input-group input[type="number"] {
  width: 60px;
  /* Reduced width */
  flex-shrink: 0;
  /* Prevent shrinking */
  text-align: center;
}

.input-field input[type="number"],
.input-field select,
.input-field input[type="range"] {
  width: 100%;
  /* Inputs take full width of their container */
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: calc(var(--button-radius) / 2);
  box-sizing: border-box;
  font-size: 0.9rem;
  background-color: var(--bg-white);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.input-field input[type="number"]:focus,
.input-field select:focus,
.input-field input[type="range"]:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(78, 205, 196, 0.3);
  outline: none;
}

.input-field input:invalid {
  border-color: var(--error-color) !important;
  background-color: #fffafa;
}

.input-field .error-message {
  color: var(--error-color);
  font-size: 0.8rem;
  display: none;
  margin-top: 5px;
}

.input-field input:invalid+.error-message {
  display: block;
}

.input-field input[type="number"]:disabled,
.input-field select:disabled,
.input-field input[type="range"]:disabled {
  background-color: #eee;
  opacity: 0.7;
  cursor: not-allowed;
}

.input-field.disabled-field {
  opacity: 0.5;
  pointer-events: none;
}

.tooltip {
  position: relative;
  display: inline-block;
  margin-left: 5px;
  cursor: help;
  color: var(--primary-color);
}

.tooltip .tooltip-text {
  visibility: hidden;
  width: 160px;
  background-color: var(--text-primary);
  color: var(--bg-white);
  text-align: center;
  border-radius: 6px;
  padding: 8px;
  position: absolute;
  z-index: 10;
  bottom: 135%;
  left: 50%;
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity 0.3s;
  font-size: 0.8rem;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  line-height: 1.3;
}

.tooltip .tooltip-text::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: var(--text-primary) transparent transparent transparent;
}

.tooltip:hover .tooltip-text {
  visibility: visible;
  opacity: 1;
}

.quality-section label {
  margin-bottom: 8px;
}

#qualityValue {
  font-weight: bold;
  color: var(--primary-color);
}

.panel-buttons {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  flex-wrap: wrap;
  justify-content: center;
  border-top: 1px solid var(--border-light);
  padding-top: 15px;
}

.panel-btn {
  padding: 10px 16px;
  border: 1px solid var(--primary-color);
  border-radius: var(--button-radius);
  background-color: var(--bg-white);
  color: var(--primary-color);
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: all 0.2s ease;
  box-shadow: none;
  position: relative;
  overflow: hidden;
  flex: 1 1 auto;
  min-width: 110px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.panel-btn i {
  background: none !important;
}

.panel-btn:hover:not(:disabled) {
  /* Prevent hover effect when disabled */
  background-color: var(--icon-hover-bg);
  border-color: #3abead;
  color: #3abead;
}

.panel-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background-color: #eee;
  border-color: var(--border-color);
  color: var(--text-secondary);
}

#downloadZipButton {
  border-color: var(--secondary-color);
  color: var(--secondary-color);
}

#downloadZipButton:hover:not(:disabled) {
  border-color: #e65c5c;
  color: #e65c5c;
  background-color: rgba(255, 107, 107, 0.1);
}

/* Spinner styles specifically for buttons */
.panel-btn .fa-spinner {
  margin-right: 5px;
}


.resize-action-btn {
  padding: 14px 24px;
  border: 1px solid transparent;
  border-radius: var(--button-radius);
  background-color: var(--primary-color);
  color: var(--bg-white);
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: bold;
  transition: all 0.3s ease;
  box-shadow: var(--box-shadow-light);
  position: relative;
  overflow: hidden;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 20px;
}

.resize-action-btn i {
  background: none !important;
}

.resize-action-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: var(--box-shadow-hover);
  background-color: #3abead;
  border-color: #2a8a81;
}

.resize-action-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background-color: #aaa;
  transform: none;
  box-shadow: var(--box-shadow-light);
  border-color: #999;
}

.resize-action-btn.pulse:not(:disabled) {
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 var(--pulse-color);
  }

  70% {
    box-shadow: 0 0 0 15px rgba(78, 205, 196, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(78, 205, 196, 0);
  }
}

#loading {
  display: none;
  margin: 20px 0;
  text-align: center;
  width: 100%;
}

.progress-bar {
  width: 90%;
  max-width: 400px;
  height: 12px;
  background: var(--progress-bg);
  border-radius: 6px;
  overflow: hidden;
  margin: 0 auto 10px auto;
}

.progress-bar #progress {
  height: 100%;
  background: var(--primary-color);
  transition: width 0.3s ease;
  width: 0%;
  border-radius: 6px;
}

#loading span {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
  color: var(--text-secondary);
}

.spinner {
  /* General spinner */
  display: inline-block;
  /* Change display for use within text */
  margin-right: 8px;
  width: 18px;
  height: 18px;
  border: 3px solid var(--bg-light);
  /* Adjusted border for visibility */
  border-top-color: var(--spinner-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  vertical-align: middle;
  /* Align better with text */
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.notification {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 15px 25px;
  border-radius: var(--button-radius);
  color: var(--bg-white);
  display: flex;
  align-items: center;
  z-index: 1050;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  opacity: 0;
  transform: translateX(100%);
  transition: opacity 0.5s ease, transform 0.5s ease;
  min-width: 250px;
}

.notification.show {
  opacity: 1;
  transform: translateX(0);
}

.notification.success {
  background-color: var(--success-color);
}

.notification.error {
  background-color: var(--error-color);
}

.notification .close-btn {
  background: none;
  border: none;
  color: inherit;
  font-size: 1.4rem;
  cursor: pointer;
  margin-left: auto;
  padding: 0 5px;
  line-height: 1;
  opacity: 0.7;
  transition: opacity 0.2s;
}

.notification .close-btn:hover {
  opacity: 1;
}

.cropper-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s ease;
  padding: 15px;
  box-sizing: border-box;
}

.cropper-modal.active {
  display: flex;
  opacity: 1;
}

.cropper-container {
  width: 100%;
  height: 100%;
  max-width: 800px;
  max-height: 600px;
  background: var(--bg-white);
  padding: 20px;
  border-radius: var(--button-radius);
  display: flex;
  flex-direction: column;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
}

.cropper-image-wrapper {
  flex-grow: 1;
  margin-bottom: 15px;
  overflow: hidden;
  background: #f0f0f0;
  border-radius: calc(var(--button-radius) / 2);
  border: 1px solid var(--border-color);
}

#cropperImage {
  display: block;
  max-width: 100%;
  max-height: 100%;
}

.cropper-container .panel-buttons {
  flex-shrink: 0;
  margin-top: 0;
  padding-top: 15px;
  border-top: 1px solid var(--border-light);
}

/* Screen Reader Only text */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* Tablet Layout (Side-by-Side) */
@media (max-width: 992px) {
  .container {
    flex-direction: row;
    align-items: flex-start;
    flex-wrap: nowrap;
  }

  .settings {
    flex: 0 0 300px;
    width: 300px;
    order: 2;
    margin-bottom: 0;
    max-width: 300px;
  }

  .left-panel {
    order: 1;
    flex: 1;
    min-width: 0;
    width: auto;
    max-width: none;
  }

  #previewContainer,
  #resizedContainer {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 15px;
  }

  .preview-box,
  .resized-box {
    padding: 15px;
  }

  .preview-box h2,
  .resized-box h2 {
    font-size: 1rem;
    margin-bottom: 10px;
  }

  /* Adjust comparison mode padding for tablet */
  #resizedBox.comparison-mode .resized-item .resized-info {
    font-size: 0.6rem;
    padding: 2px 4px;
  }
}

/* Mobile Layout (Stacked, Settings Last) */
@media (max-width: 768px) {
  .container {
    flex-direction: column;
    /* Stack elements */
    align-items: center;
    /* Center items */
    flex-wrap: wrap;
    /* Allow wrapping if needed */
  }

  .settings {
    order: 2;
    /* Settings second/last */
    width: 100%;
    /* Full width */
    max-width: 100%;
    /* Override previous max-width */
    flex-basis: auto;
    /* Reset flex basis */
    margin-bottom: 20px;
    /* Add margin back */
  }

  .left-panel {
    order: 1;
    /* Preview first */
    width: 100%;
    /* Full width */
    max-width: 100%;
    /* Ensure full width */
  }

  h1 {
    font-size: clamp(1rem, 5vw, 1.3rem);
    text-align: center;
    display: block;
    margin-left: 0;
    margin-right: 0;
  }

  h1::after {
    width: 60%;
    left: 50%;
    transform: translateX(-50%);
  }

  .upload-section {
    max-width: 95%;
    min-height: 70px;
  }

  #previewContainer,
  #resizedContainer {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 10px;
  }

  .preview-box,
  .resized-box {
    padding: 10px;
  }

  .notification {
    top: 10px;
    left: 10px;
    right: 10px;
    width: auto;
    transform: translateY(-150%);
  }

  .notification.show {
    transform: translateY(0);
  }

  .panel-buttons {
    flex-direction: row;
  }

  .panel-btn {
    min-width: auto;
    flex-grow: 1;
  }

  /* Let most input-groups wrap naturally */
  .input-group {
    flex-direction: row;
    /* Keep row for potential side-by-side */
    flex-wrap: wrap;
    /* Allow wrapping on small screens */
    gap: 10px;
    /* Adjust gap */
  }

  /* Ensure fields take good width when wrapped */
  .input-group .input-field {
    min-width: calc(50% - 5px);
    /* Aim for 2 per row if space */
    margin-bottom: 10px;
    /* Ensure spacing when wrapped */
  }

  /* Prevent margin on last item ONLY if not wrapped */
  .input-group:not(.flex-col) .input-field:last-child {
    margin-bottom: 10px;
    /* Re-add margin if potentially wrapped */
  }

  .settings-section:last-child .input-group .input-field:last-child {
    margin-bottom: 0;
    /* Keep no margin if last section's last item */
  }

  /* Keep Quality Input Group Inline */
  .quality-input-group {
    flex-direction: row !important;
    /* Ensure row */
    align-items: center;
  }

  .quality-input-group input[type="number"] {
    width: 60px;
  }

  /* Mobile comparison layout tweaks */
  #resizedBox.comparison-mode .resized-item .resized-info {
    font-size: 0.6rem;
    /* Smaller text */
    padding: 2px 4px;
    gap: 2px;
    left: 50%;
    width: 50%;
  }

  #resizedBox.comparison-mode .resized-item .download-btn-small {
    width: 36px;
    height: 36px;
    font-size: 0.9rem;
  }

  .remove-btn {
    width: 40px;
    height: 40px;
    font-size: 1.1rem;
  }

  .cropper-container {
    max-width: 95vw;
    max-height: 85vh;
    padding: 15px;
  }

  .cropper-container .panel-buttons {
    flex-direction: column;
    gap: 8px;
  }

  .resize-action-btn {
    font-size: 1rem;
    padding: 12px 15px;
  }
}

/* Smallest Mobile Devices */
@media (max-width: 480px) {
  body {
    padding: 5px;
  }

  .container {
    padding: 10px;
    gap: 15px;
  }

  h1 {
    font-size: 1.1rem;
  }

  .upload-section {
    padding: 10px;
    min-height: 60px;
  }

  .settings-panel {
    padding: 15px;
  }

  #previewContainer,
  #resizedContainer {
    grid-template-columns: repeat(auto-fill, minmax(95px, 1fr));
    gap: 8px;
  }

  .image-item,
  .resized-item {
    border-radius: calc(var(--button-radius) / 1.5);
  }

  .remove-btn {
    width: 38px;
    height: 38px;
    font-size: 1rem;
  }

  /* Slightly smaller */
  .download-btn-small {
    width: 38px;
    height: 38px;
    font-size: 0.9rem;
  }

  /* Slightly smaller */
  .add-image-btn {
    font-size: 0.7rem;
  }

  .add-image-btn i {
    font-size: 1.5rem;
  }

  .resized-info {
    font-size: 0.65rem;
    padding: 3px 5px;
  }

  .panel-buttons {
    flex-direction: column;
  }

  .panel-btn {
    width: 100%;
    padding: 10px;
    font-size: 0.9rem;
  }

  .resize-action-btn {
    padding: 12px;
    font-size: 1rem;
  }

  .notification {
    padding: 12px 20px;
    font-size: 0.9rem;
  }

  .notification .close-btn {
    font-size: 1.2rem;
  }

  /* ENHANCEMENT: Stack specific input groups (Presets/Format) vertically */
  .settings-section .input-group.stack-on-mobile {
    flex-direction: column;
    gap: 10px;
  }

  .settings-section .input-group.stack-on-mobile .input-field {
    min-width: 100%;
    margin-bottom: 10px;
  }

  .settings-section .input-group.stack-on-mobile .input-field:last-child {
    margin-bottom: 0;
  }

  /* Keep quality group row */
  .quality-input-group {
    flex-direction: row !important;
  }


  /* ENHANCEMENT: Stack comparison images vertically */
  #resizedBox.comparison-mode .resized-item {
    flex-direction: column;
    /* Stack images vertically */
    /* Adjust aspect-ratio or set a fixed height if needed */
    aspect-ratio: auto;
    /* Let height be determined by content */
    /* Or set a specific height: e.g., height: 200px; */
  }

  #resizedBox.comparison-mode .resized-item .original-preview,
  #resizedBox.comparison-mode .resized-item .resized-preview {
    width: 100%;
    /* Take full width */
    height: 50%;
    /* Each takes half the height */
    object-fit: contain;
    /* Ensure image fits without cropping */
    border-right: none;
    /* Remove side border */
  }

  #resizedBox.comparison-mode .resized-item .original-preview {
    border-bottom: 1px solid var(--border-light);
    /* Add border between them */
  }

  #resizedBox.comparison-mode .resized-item .resized-info {
    /* Adjust info position for vertical layout */
    position: absolute;
    /* Keep absolute positioning */
    left: 0;
    /* Align to left */
    bottom: 0;
    /* Stick to bottom */
    width: 100%;
    /* Full width */
    border-bottom-left-radius: calc(var(--button-radius) / 1.5);
    /* Match item radius */
    border-bottom-right-radius: calc(var(--button-radius) / 1.5);
    border-bottom-left-radius: var(--button-radius);
    /* Adjust radius */
    border-bottom-right-radius: var(--button-radius);
    /* Use styles from base .resized-info and adjust */
    font-size: 0.55rem;
    padding: 2px 3px;
    gap: 1px;
  }

  #resizedBox.comparison-mode .resized-item .download-btn-small {
    /* Position download button top-right relative to the whole item */
    width: 34px;
    height: 34px;
    font-size: 0.8rem;
    top: 3px;
    right: 3px;
  }
}

ImageResizerApp {
  constructor() {
    this.images=[]; // Stores { id, file, originalURL, selected, originalFileName, originalWidth, originalHeight, aspectRatio, originalType }

    Stores {
      [originalId]: {
        blob,
        url,
        width,
        height,
        size,
        originalFileName,
        format
      }

      else {
        this.updateDimensions();
      }

      try {
        const settings= {
          format: this.elements.formatSelect.value,
            quality: this.elements.qualityInput.value,
            maintainAspectRatio: this.elements.maintainAspectRatio.checked
        }

        try {
          const settings=JSON.parse(savedSettings);

          // Set format first as it affects quality control state
          this.elements.formatSelect.value=settings.format || 'image/jpeg';

          // Set quality (use || 80 for fallback)
          this.elements.qualityInput.value=settings.quality || '80';

          // Trigger event to update slider and text visually
          this.elements.qualityInput.dispatchEvent(new Event('input', {
              bubbles: true
            }

            else {
              // Initialize quality state if no settings saved
              this.updateQualityControlState();
            }

            else {
              // Restore percentage display using the current input value
              const currentValue=parseInt(this.elements.qualityInput.value) || 80;
              this.elements.qualityInput.value=currentValue; // Ensure value is set
              this.elements.qualitySlider.value=currentValue;

              this.elements.qualityValue.textContent=`$ {
                currentValue
              }

              try {
                const dimensions=await this.getImageDimensions(url);

                this.images.push({
                  id, file, originalURL: url, selected: false,
                  originalFileName: file.name, originalWidth: dimensions.width, originalHeight: dimensions.height,
                  aspectRatio: dimensions.width / dimensions.height, originalType: file.type
                }

                else {
                  this.elements.previewContainer.appendChild(item);
                }

                else {
                  const button=this.elements.previewContainer.querySelector('.add-image-btn');
                  if (button) button.remove();
                  // Don't show main header again if all images are removed, user might just be clearing
                  this.elements.resizedBox.style.display='none'; // Hide results if no source images
                  this.clearResizedResults(); // Also clear data and revoke URLs
                }

                else {
                  this.elements.buttonText.textContent='Resize';
                  this.elements.resizeActionButton.disabled=true;
                }

                else {
                  URL.revokeObjectURL(newUrl); // Revoke if image somehow not found
                }

                else {
                  quality=100; // Use conceptually max quality for PNG/Source
                }

                return {
                  width, height, maintainRatio: this.elements.maintainAspectRatio.checked, quality: quality / 100, format
                }

                try {
                  let finalWidth=settings.width || img.naturalWidth;
                  let finalHeight=settings.height || img.naturalHeight;

                  if (settings.maintainRatio && image.aspectRatio > 0) {
                    const originalAspect=image.aspectRatio;

                    if (settings.width && !settings.height) {
                      finalHeight=Math.max(1, Math.round(finalWidth / originalAspect));
                    }

                    else {
                      finalWidth=Math.max(1, Math.round(finalHeight * originalAspect));
                    }

                    try {
                      Object.values(this.resizedImages).forEach(data=> {
                          if (data.blob) {
                            const extension=data.format.split('/')[1] || (data.format==='image/jpeg' ? 'jpg' : data.format.split('/')[1]);
                            const baseName=data.originalFileName.substring(0, data.originalFileName.lastIndexOf('.')) || data.originalFileName;

                            const fileName=`$ {
                              baseName
                            }

                            finally {
                              // Re-enable button and restore text after process finishes (success or error)
                              zipBtn.disabled=false;
                              zipBtn.innerHTML=originalBtnText;
                            }