/* Theme and page layout */
:root {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-text-size-adjust: 100%;

  /* Backgrounds and surfaces */
  --color-bg: #172126;
  --color-bg-top: #34474c;
  --color-main-bg-top: #848d96;
  --color-main-bg-bottom: #33363b;
  --color-surface: #22343b;

  /* Text and borders */
  --color-text: #e8f0f1;
  --color-muted: #a8c3c7;
  --color-border: #55757b;

  /* Accent and status colors */
  --color-accent: #29b9c5;
  --color-danger: #d95761;
  --color-success: #72dc7a;

  /* Image preview and utility colors */
  --color-image-bg: #ddd;
  --color-shadow: rgb(0 0 0 / 30%);
  --color-transparent: transparent;

  /* Reusable component values */
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 10px;
  --radius-xl: 30px;
  --radius-pill: 999px;
  --background-button: var(--color-bg-top);
  --shadow-card: 0 4px 8px var(--color-shadow);
  --main-padding: clamp(20px, 3vw, 40px);
}

* {
  box-sizing: border-box;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
  min-width: 320px;
  width: 100%;
  margin: 0 auto;
  color: var(--color-text);
  text-align: center;

  /* background: linear-gradient(to bottom,
      var(--color-main-bg-top) 0% 85%,
      var(--color-main-bg-bottom) 85% 100%); */
  background: url("../images/body-background.png") left center / auto 100% repeat-x fixed;
}

h1 {
  margin: 0;
  color: var(--color-text);
  font-size: clamp(1.25rem, 2vw, 2rem);
  line-height: 1.25;
}

h2 {
  margin: 0;
  color: var(--color-text);
  font-size: clamp(0.9rem, 1.75vw, 1.5rem);
  line-height: 1.25;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

#workspace>.brand {
  display: none;
}

.brand h1,
.brand h2 {
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand h1,
.brand h2 {
  gap: 5px;
}

.brand img {
  display: block;
  width: 32px;
  height: 32px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

main {
  display: flex;
  flex: 1 1 0;
  flex-direction: row;
  gap: 14px;
  min-width: 0;
  min-height: 0;

  max-height: 95%;
  max-width: 95%;
  aspect-ratio: 16 / 9;
  margin: auto;

  padding: var(--main-padding);

  border-radius: var(--radius-xl);
  border-image-source: url("../images/frame-9slice.png");
  border-image-slice: 100 fill;
  border-image-width: 40px;
  border-image-repeat: stretch;

  background: radial-gradient(ellipse at 50% 0%, var(--color-bg-top), var(--color-bg) 72%);
  box-shadow: var(--shadow-card);
}

/* Sidebar: image list, thumbnails, and scrollbar */
#sidebarColumn {
  display: flex;
  flex: 1 1 0;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  min-width: 170px;
}

#imageSidebar {
  display: flex;
  flex: 1 1 0;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  box-shadow: var(--shadow-card);
  text-align: left;
}

#imageSidebar h2 {
  margin: 0;
  padding: 0;
  color: var(--color-text);
  font-size: 1rem;
}

.imageListHeading {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 14px;
  border-bottom: 1px solid var(--color-border);
}

#imageCount {
  color: var(--color-muted);
  font-size: 0.72rem;
  white-space: nowrap;
}

#imageListStatus {
  flex: 0 0 auto;
  margin: 8px;
  border: 1px solid var(--color-danger);
  border-radius: var(--radius-sm);
  padding: 4px;
  color: var(--color-text);
  background: rgb(217 87 97 / 18%);
  font-size: 0.8rem;
  line-height: 1;
  text-align: center;
}

#imageList {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow: auto;
  margin: 0;
  padding: 6px 6px;
  list-style: none;
  scrollbar-color: var(--color-accent) var(--color-bg);
  scrollbar-width: thin;
}

#imageList::-webkit-scrollbar {
  width: 9px;
  height: 9px;
}

#imageList::-webkit-scrollbar-track {
  border-radius: var(--radius-md);
  background: var(--color-bg);
}

#imageList::-webkit-scrollbar-thumb {
  border: 1px solid var(--color-bg);
  border-radius: var(--radius-md);
  background: var(--color-accent);
}

#imageList::-webkit-scrollbar-thumb:hover {
  background: var(--color-accent);
}

#imageList li {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.imageThumbnail {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  width: 0;
  min-width: 0;
  border: 1px solid var(--color-transparent);
  border-radius: var(--radius-md);
  padding: 6px;
  color: var(--color-text);
  background: rgb(255 255 255 / 3%);
  text-align: left;
  cursor: pointer;
}

.imageThumbnail:hover:not([aria-pressed="true"]) {
  border-color: var(--color-border);
  background: var(--color-bg-top);
}

.imageThumbnail[aria-pressed="true"] {
  border-color: var(--color-accent);
  background: rgb(41 185 197 / 22%);
  box-shadow: inset 3px 0 var(--color-accent);
}

.imageThumbnail img {
  flex: 0 0 48px;
  width: 48px;
  height: 48px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--color-image-bg);
}

.imageThumbnailInfo {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-width: 0;
}

.imageThumbnailName {
  overflow: hidden;
  font-size: 0.8rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.imageThumbnailDimensions {
  color: var(--color-muted);
  font-size: 0.68rem;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.deleteImageButton {
  display: grid;
  flex: 0 0 32px;
  width: 32px;
  height: 32px;
  place-items: center;
  border: 1px solid var(--color-transparent);
  border-radius: var(--radius-sm);
  padding: 5px;
  color: var(--color-muted);
  background: var(--color-transparent);
  cursor: pointer;
}

.deleteImageButton:hover {
  border-color: var(--color-danger);
  color: var(--color-text);
  background: var(--color-danger);
}

.deleteImageButton:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 1px;
}

.deleteImageButton svg {
  display: block;
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.7;
}

/* Main image preview */
#workspace {
  display: flex;
  flex: 3 1 0;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  gap: 8px;
}

#imageComparison {
  --position: 50%;
  --zoom: 1;
  position: relative;
  flex: 1 1 0;
  width: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--color-border);
  border-radius: 12px;
  background: var(--color-bg);
  box-shadow: var(--shadow-card);
}

#imageComparison::after {
  position: absolute;
  z-index: 1;
  inset: 0;
  border-radius: inherit;
  box-shadow:
    inset 0 3px 6px rgb(0 0 0 / 65%),
    inset 0 -3px 5px rgb(255 255 255 / 22%),
    inset 3px 0 5px rgb(0 0 0 / 35%),
    inset -3px 0 5px rgb(255 255 255 / 14%);
  content: "";
  pointer-events: none;
}

.comparisonImage {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  transform: scale(var(--zoom));
}

.comparisonAfter {
  position: absolute;
  inset: 0;
  clip-path: inset(0 0 0 var(--position));
}

.comparisonImageConverted:not([src]) {
  display: none;
}

.comparisonLabel {
  position: absolute;
  z-index: 3;
  top: 12px;
  padding: 6px 8px;

  color: var(--color-text);
  background: rgb(23 33 38 / 88%);
  font-size: 0.9rem;
  font-weight: 600;
  pointer-events: none;

  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

#sampleImageNotice {
  left: 50%;
  transform: translateX(-50%)
}

.comparisonLabelBefore {
  left: 12px;
}

.comparisonLabelAfter {
  right: 12px;
}

.comparisonDivider {
  position: absolute;
  z-index: 2;
  top: 0;
  bottom: 0;
  left: var(--position);
  width: 3px;
  background: var(--color-accent);
  transform: translateX(-50%);
  pointer-events: none;
}

.comparisonDivider span {
  position: absolute;
  top: 50%;
  left: 50%;
  display: grid;
  width: 42px;
  height: 42px;
  border: 1px solid var(--color-accent);
  border-radius: 50%;
  background: var(--color-surface);
  box-shadow: var(--shadow-card);
  transform: translate(-50%, -50%);
}

.comparisonDivider span::before,
.comparisonDivider span::after {
  position: absolute;
  top: 50%;
  width: 7px;
  height: 7px;
  margin-top: -4px;
  border-color: var(--color-text);
  border-style: solid;
  border-width: 0;
  content: "";
}

.comparisonDivider span::before {
  left: 11px;
  border-bottom-width: 2px;
  border-left-width: 2px;
  transform: rotate(45deg);
}

.comparisonDivider span::after {
  right: 11px;
  border-right-width: 2px;
  border-top-width: 2px;
  transform: rotate(45deg);
}

.comparisonSlider {
  position: absolute;
  z-index: 4;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  appearance: none;
  border: 0;
  background: var(--color-transparent);
  cursor: ew-resize;
  touch-action: pan-y;
}

.comparisonSlider::-webkit-slider-runnable-track {
  height: 100%;
  background: var(--color-transparent);
}

.comparisonSlider::-moz-range-track {
  height: 100%;
  background: var(--color-transparent);
}

.comparisonSlider::-webkit-slider-thumb {
  width: 36px;
  height: 100%;
  appearance: none;
  background: var(--color-transparent);
}

.comparisonSlider::-moz-range-thumb {
  width: 36px;
  height: 100%;
  border: 0;
  border-radius: 0;
  background: var(--color-transparent);
}

.comparisonSlider:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: -3px;
}

/* Quality and zoom controls */
#converterControls {
  display: flex;
  flex-direction: row;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-width: 0;
  margin: 0;
  border: 0;
  padding: 0;
}

#scaleControl,
#qualityControl {
  min-width: 0;
  margin: 0;
  border: 1px solid var(--color-border);
  padding: 8px 10px;
  border-radius: var(--radius-md);
  color: var(--color-text);
  background: var(--color-surface);
  box-shadow:
    0 2px 0 var(--color-bg),
    0 8px 14px rgb(0 0 0 / 42%),
    inset 0 1px 0 rgb(255 255 255 / 22%),
    inset 0 -1px 0 rgb(0 0 0 / 28%);
}

#qualityControl {
  flex: 2 1 0;
}

#scaleControl {
  flex: 1 1 0;
}

#comparisonScale,
#qualitySlider {
  display: block;
  width: 100%;
  height: 12px;
  margin: 4px 0;
  appearance: none;
  background: var(--color-transparent);
  cursor: pointer;
  touch-action: pan-y;
}

#comparisonScale::-webkit-slider-runnable-track,
#qualitySlider::-webkit-slider-runnable-track {
  height: 6px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  background: linear-gradient(90deg,
      var(--color-accent) 0%,
      var(--color-accent) var(--range-progress, 0%),
      var(--color-bg) var(--range-progress, 0%),
      var(--color-bg) 100%);
  box-shadow: none;
}

#comparisonScale::-moz-range-track,
#qualitySlider::-moz-range-track {
  height: 6px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-pill);
  background: linear-gradient(90deg,
      var(--color-accent) 0%,
      var(--color-accent) var(--range-progress, 0%),
      var(--color-bg) var(--range-progress, 0%),
      var(--color-bg) 100%);
  box-shadow: none;
}

#comparisonScale::-webkit-slider-thumb,
#qualitySlider::-webkit-slider-thumb {
  width: 18px;
  height: 18px;
  margin-top: -7px;
  appearance: none;
  border: 1px solid var(--color-text);
  border-radius: 50%;
  background: var(--color-accent);
}

#comparisonScale::-moz-range-thumb,
#qualitySlider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 1px solid var(--color-text);
  border-radius: 50%;
  background: var(--color-accent);
}

#comparisonScale:focus-visible,
#qualitySlider:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
  border-radius: 4px;
}

#scaleControl:hover,
#qualityControl:hover {
  background: var(--color-bg-top);
}

/* Keep the native file input accessible while hiding its default appearance. */
.visuallyHidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  clip-path: inset(50%);
}

/* Shared styles for the main action buttons */
#uploadButton,
#clearImagesButton,
#downloadButton {
  display: inline-block;
  max-width: 420px;
  min-width: 170px;
  margin: 0;
  /* border: 1px solid var(--color-border); */
  border: none;
  padding: 18px 10px;
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.2;
  color: var(--color-text);
  background: var(--background-button);
  text-decoration: none;
  cursor: pointer;
  flex: 0 0 calc((100% - 10px) / 4);
}

#downloadButton[aria-disabled="true"] {
  opacity: 0.45;
  cursor: not-allowed;
}

#uploadButton,
#downloadButton,
#clearImagesButton {
  box-shadow:
    0 4px 0 var(--color-bg),
    0 7px 12px rgb(0 0 0 / 36%),
    inset 0 1px 0 rgb(255 255 255 / 22%),
    inset 0 -1px 0 rgb(0 0 0 / 28%);
  transition: transform 120ms ease, box-shadow 120ms ease, filter 120ms ease;
}

#uploadButton:hover {
  filter: brightness(1.08);
  box-shadow:
    0 2px 0 var(--color-bg),
    0 3px 6px rgb(0 0 0 / 28%),
    inset 0 1px 0 rgb(255 255 255 / 18%);
  transform: translateY(1px);
}

#downloadButton:not([aria-disabled="true"]):hover,
#clearImagesButton:not(:disabled):hover {
  filter: brightness(1.08);
  box-shadow:
    0 2px 0 var(--color-bg),
    0 3px 6px rgb(0 0 0 / 28%),
    inset 0 1px 0 rgb(255 255 255 / 18%);
  transform: translateY(1px);
}

#uploadButton:active,
#downloadButton:not([aria-disabled="true"]):active,
#clearImagesButton:not(:disabled):active {
  box-shadow:
    0 0 0 var(--color-bg),
    0 1px 2px rgb(0 0 0 / 24%),
    inset 0 1px 2px rgb(0 0 0 / 30%);
  transform: translateY(3px);
}

#imageInput:focus-visible+#uploadControl #uploadButton {
  outline: 2px solid var(--color-accent);
  outline-offset: 3px;
}

#sidebarColumn>#downloadButton[aria-disabled="true"][aria-busy="true"] {
  opacity: 1;
  cursor: progress;
  background: var(--color-bg-top);
}

#downloadProgressContainer {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: 100%;
}

#downloadProgressContainer[hidden] {
  display: none;
}

#downloadProgress {
  width: min(100%, 260px);
  height: 12px;
  overflow: hidden;
  appearance: none;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  background: var(--background-button);
}

#downloadProgress::-webkit-progress-bar {
  border-radius: 999px;
  background: var(--background-button);
}

#downloadProgress::-webkit-progress-value {
  border-radius: 999px;
  background: var(--color-accent);
  transition: width 160ms ease;
}

#downloadProgress::-moz-progress-bar {
  border-radius: 999px;
  background: var(--color-accent);
}

#downloadProgressValue {
  min-width: 5ch;
  color: var(--color-text);
  font-size: 1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

#uploadButton:hover,
#clearImagesButton:hover:not(:disabled) {
  background: var(--color-bg-top);
}

#uploadControl {
  position: relative;
  flex: 0 0 auto;
}

#sidebarColumn>#uploadControl>#uploadButton,
#sidebarColumn>#downloadButton,
#sidebarColumn>#clearImagesButton {
  width: 100%;
  max-width: none;
  flex: 0 0 auto;
}

#uploadTooltip {
  position: absolute;
  z-index: 5;
  top: calc(100% + 2px);
  bottom: auto;
  left: 50%;
  width: max-content;
  max-width: 100%;
  border: 1px solid var(--color-success);
  border-radius: var(--radius-md);
  padding: 7px 12px;
  color: var(--color-success);
  background: var(--color-bg-top);
  box-shadow: 0 8px 18px rgb(0 0 0 / 55%);
  font-size: 0.9rem;
  font-weight: bold;
  line-height: 1.2;
  text-align: center;
  transform: translateX(-50%);
  pointer-events: none;
}

#uploadTooltip::after {
  position: absolute;
  top: -9px;
  bottom: auto;
  left: 50%;
  width: 8px;
  height: 8px;
  border-left: none;
  border-bottom: none;
  border-right: 1px solid var(--color-success);
  border-top: 1px solid var(--color-success);
  background: var(--color-bg-top);
  content: "";
  transform: translate(-50%, 50%) rotate(-45deg);
}

#clearImagesButton:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#clearImagesButton:not(:disabled) {
  border-color: var(--color-danger);
  color: var(--color-text);
  background: var(--color-danger);
}

#clearImagesButton:not(:disabled):hover {
  background: var(--color-danger);
}

#downloadButton:not([aria-disabled="true"]) {
  border-color: var(--color-accent);
  color: var(--color-bg);
  background: var(--color-accent);
}

#downloadButton:not([aria-disabled="true"]):hover {
  background: var(--color-accent);
}

/* Conversion results */
#converterInfo {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0 8px;
  width: 100%;
  min-height: 56px;
  line-height: 1;
  margin: 0;
  padding: 0 4px;

  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  color: var(--color-text);
  background: var(--color-surface);
  box-shadow:
    0 2px 0 var(--color-bg),
    0 8px 14px rgb(0 0 0 / 42%),
    inset 0 1px 0 rgb(255 255 255 / 22%),
    inset 0 -1px 0 rgb(0 0 0 / 28%);
}

#converterInfo p {
  flex: 0 1 auto;
  width: max-content;
  max-width: 100%;
  min-width: 0;
  margin: 0;
  color: var(--color-text);
  font-size: 0.8rem;
  font-weight: 600;
  text-align: center;
}

#converterInfo p:empty {
  display: none;
}

#converterInfo .sizeReduction {
  color: var(--color-success);
}

#sizeChange.sizeIncreased {
  color: var(--color-danger);
  font-weight: 650;
}

@media (max-width: 720px) {

  main {
    aspect-ratio: auto;
    max-height: 98%;
    max-width: 98%;

    border-width: 40px;
    box-shadow: none;
  }

  body {
    background: linear-gradient(to bottom,
        var(--color-main-bg-top) 0% 85%,
        var(--color-main-bg-bottom) 85% 100%);
  }

}

@media (max-width: 600px) {
  body {
    padding-top: 0;
    padding-bottom: 0;
  }

  main {
    max-width: none;
    max-height: none;
    width: 100%;
    box-shadow: none;

    border-radius: 0;
    border: none;
  }

  #qualityControl {
    flex: 3 1 0;
  }

  #scaleControl {
    flex: 2 1 0;
  }
}

@media (max-width: 480px) {

  body {
    height: auto;
    min-height: 0;
  }

  main {
    border-radius: var(--radius-xl);
    border-image-source: url("../images/frame-9slice.png");
    border-image-slice: 100 fill;
    border-image-width: 40px;
    border-image-repeat: stretch;
  }


  #scaleControl,
  #qualityControl {
    padding: 5px 10px;
  }

  #sidebarColumn>.brand {
    display: none;
  }

  #workspace>.brand {
    display: flex;
  }

  main {
    flex: 0 0 auto;
    flex-direction: column-reverse;
    gap: 8px;
    min-height: auto;
  }

  #sidebarColumn {
    flex: 0 0 auto;
    min-height: auto;
  }

  #workspace {
    flex: 0 0 auto;
    min-height: auto;
  }

  #imageComparison {
    flex: 0 0 auto;
    height: auto;
    min-height: 0;
    aspect-ratio: 4 / 3;
  }

  #imageSidebar {
    min-height: 225px;
  }

  #clearImagesButton {
    min-width: 0;
  }

  .imageThumbnail {
    gap: 4px;
    padding: 2px;
  }

  .imageThumbnail img {
    flex: 0 0 80px;
    width: 80px;
    height: 80px;
  }

  .imageThumbnailName {
    font-size: 0.65rem;
  }

  .deleteImageButton {
    flex-basis: 26px;
    width: 26px;
    height: 28px;
    padding: 4px;
  }

  #uploadTooltip {
    top: auto;
    bottom: calc(100% + 2px);
  }

  #uploadTooltip::after {
    top: calc(100% - 8px);
    bottom: auto;
    border-right: none;
    border-top: none;
    border-left: 1px solid var(--color-success);
    border-bottom: 1px solid var(--color-success);
  }
}

@media (max-width: 360px) {
  body {
    background:  var(--color-bg)
  }

  main {
    border-radius: 0;
    border: none;
  }
}

@media (max-height: 600px) {
  body {
    height: auto;
    min-height: 0;
    max-height: none;

    /* padding-top: 20px;
    padding-bottom: 20px; */
  }

  main {
    min-height: 560px;
    max-height: auto;
    aspect-ratio: auto;
    flex: 0 0 auto;
  }
}

@media (max-height: 600px) and (min-width: 601px) {
  body {
    padding-top: 10px;
    padding-bottom: 10px;
  }

}
