:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --panel-2: #eeeef1;
  --border: #dcdce2;
  --text: #17171a;
  --muted: #6b6b74;
  --accent: #6440e0;
  --accent-text: #ffffff;
  --danger: #c0342b;
  --checker-a: #e4e4e9;
  --checker-b: #f7f7f8;
  --shimmer: rgba(255, 255, 255, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101014;
    --panel: #18181d;
    --panel-2: #202027;
    --border: #2d2d36;
    --text: #eeeef2;
    --muted: #93939e;
    --accent: #9c7bff;
    --accent-text: #101014;
    --danger: #ff6b60;
    --checker-a: #26262f;
    --checker-b: #1a1a20;
    --shimmer: rgba(255, 255, 255, 0.08);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

a {
  color: var(--accent);
}

.topbar {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  text-decoration: none;
}

.topbar-logo {
  display: block;
  height: 22px;
  width: auto;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 20px;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
}

/* ---------- trạng thái chung ---------- */

[hidden] {
  display: none !important;
}

.state-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
}

/* ---------- idle: dropzone ---------- */

.dropzone {
  flex: 1;
  min-height: 60vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: 16px;
  padding: 32px;
  margin: 12px 0;
  cursor: pointer;
  transition: border-color 0.15s ease, background-color 0.15s ease;
  background: var(--panel);
}

.dropzone:hover,
.dropzone:focus-visible {
  border-color: var(--accent);
}

.dropzone.drag-active {
  border-color: var(--accent);
  background: var(--panel-2);
}

.dropzone-title {
  font-size: 1.4rem;
  font-weight: 600;
  margin: 0;
}

.dropzone-subtitle {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 0;
}

.dropzone-reassurance {
  color: var(--muted);
  font-size: 0.82rem;
  margin: 20px 0 0;
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
}

/* ---------- processing ---------- */

.processing-stage {
  position: relative;
  border-radius: 16px;
  overflow: hidden;
  margin: 12px 0;
  background: var(--panel-2);
  display: flex;
  align-items: center;
  justify-content: center;
  max-height: 70vh;
}

.processing-stage img {
  display: block;
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
}

.processing-shimmer {
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 35%, var(--shimmer) 50%, transparent 65%);
  background-size: 200% 100%;
  animation: shimmer 1.6s ease-in-out infinite;
}

@keyframes shimmer {
  from {
    background-position: 150% 0;
  }
  to {
    background-position: -50% 0;
  }
}

.processing-status {
  text-align: center;
  color: var(--muted);
  margin: 14px 0 0;
  font-size: 0.95rem;
}

.processing-status[aria-busy="true"]::before {
  content: "";
}

/* ---------- result ---------- */

.result-slider {
  position: relative;
  border-radius: 16px;
  overflow: hidden;
  margin: 12px 0;
  touch-action: none;
  user-select: none;
  max-height: 70vh;
  background-image: linear-gradient(45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker-a) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker-a) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  background-color: var(--checker-b);
}

.result-slider.bg-white {
  background-image: none;
  background-color: #ffffff;
}

.result-slider.bg-black {
  background-image: none;
  background-color: #000000;
}

.result-slider img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 70vh;
  object-fit: contain;
  pointer-events: none;
}

.result-slider #result-before {
  /* Clip riêng cả ảnh "before" (chỉ lộ phần bên phải, từ slider-pos trở đi)
     -- clip-path chỉ ảnh hưởng paint, không ảnh hưởng layout, nên ảnh này
     vẫn quyết định chiều cao của .result-slider như trước. Không clip nó
     thì nó vẫn là ảnh gốc KHÔNG TRONG SUỐT nằm full-width bên dưới, khiến
     phần trong suốt của ảnh "after" lộ ra ảnh gốc thay vì ô caro/màu nền đã
     chọn -- nhìn như "không có gì thay đổi" dù dữ liệu alpha đã đúng. */
  clip-path: inset(0 0 0 var(--slider-pos, 50%));
}

.result-slider .after-layer {
  position: absolute;
  inset: 0;
  /* z-index tường minh: chỉ dựa vào position:absolute để stack trên ảnh
     "before" (position:static) không đáng tin cậy trên mọi engine -- từng
     thấy after-layer bị ảnh before đè lên dù đúng thứ tự DOM/spec, có lẽ do
     tương tác với overflow:hidden + border-radius của .result-slider ép
     compositing layer theo thứ tự khác. z-index rõ ràng loại bỏ hẳn rủi ro
     này thay vì phụ thuộc default stacking. */
  z-index: 1;
  /* Lớp "after" luôn full-size khớp với ảnh "before" bên dưới (cùng box,
     cùng object-fit:contain) -- chỉ clip-path để lộ phần bên trái tới vị trí
     slider, không resize width (tránh ảnh bị scale méo khi kéo). */
  clip-path: inset(0 calc(100% - var(--slider-pos, 50%)) 0 0);
}

.result-slider .after-layer img {
  width: 100%;
  height: 100%;
  max-height: none;
}

.slider-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--slider-pos, 50%);
  width: 40px;
  margin-left: -20px;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: ew-resize;
  touch-action: none;
}

.slider-handle::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  background: var(--accent-text, #fff);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
  transform: translateX(-50%);
}

.slider-handle-grip {
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}

.cache-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 2;
  background: var(--panel);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.75rem;
}

.bg-picker {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 0 16px;
  flex-wrap: wrap;
}

.bg-swatch {
  /* width/height/padding/min-height ghi đè rõ ràng: .bg-swatch là <button>
     hoặc <label>, và rule "button" chung ở trên đặt min-height:44px +
     padding:12px 18px cho toàn site (touch target) -- min-height đó thắng
     height:Npx nếu không override, kéo ô tròn thành hình bầu dục. */
  width: 28px;
  height: 28px;
  min-height: 28px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
  padding: 0;
  flex: none;
}

.bg-swatch[aria-pressed="true"] {
  border-color: var(--accent);
}

.bg-swatch.transparent {
  background-image: linear-gradient(45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(-45deg, var(--checker-a) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--checker-a) 75%),
    linear-gradient(-45deg, transparent 75%, var(--checker-a) 75%);
  background-size: 10px 10px;
  background-position: 0 0, 0 5px, 5px -5px, -5px 0px;
  background-color: var(--checker-b);
}

.bg-swatch.white {
  background-color: #ffffff;
}

.bg-swatch.black {
  background-color: #000000;
}

.bg-swatch.custom {
  padding: 0;
  overflow: hidden;
}

.bg-swatch.custom input[type="color"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 100%;
  border: none;
  padding: 0;
  cursor: pointer;
}

/* Chrome/Safari vẽ input[type=color] bằng 2 pseudo-element nội bộ có
   padding/border riêng, bỏ qua overflow:hidden + border-radius của label
   cha -- phải reset thẳng chúng thì ô mới lấp đầy đúng hình tròn đã clip. */
.bg-swatch.custom input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

.bg-swatch.custom input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 0;
}

.bg-swatch.custom input[type="color"]::-moz-color-swatch {
  border: none;
}

.action-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}

button {
  font-family: inherit;
  font-size: 0.95rem;
  border-radius: 10px;
  padding: 12px 18px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  min-height: 44px;
}

button:hover {
  border-color: var(--accent);
}

button:focus-visible,
.dropzone:focus-visible,
.bg-swatch:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

/* ---------- error ---------- */

.error-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
  min-height: 60vh;
}

.error-message {
  color: var(--danger);
  font-size: 1.05rem;
  max-width: 480px;
}

/* ---------- hàng đợi nhiều ảnh ---------- */

.queue-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 0 16px;
}

.queue-thumb {
  flex: none;
  width: 56px;
  height: 56px;
  border-radius: 8px;
  border: 2px solid var(--border);
  background-size: cover;
  background-position: center;
  cursor: pointer;
  padding: 0;
}

.queue-thumb[aria-current="true"] {
  border-color: var(--accent);
}

.footer {
  margin-top: auto;
  padding: 13px 20px;
  background: var(--bg);
  text-align: center;
}

.footer p {
  color: var(--muted);
  font-size: 12.5px;
  margin: 0;
}

.footer a {
  color: var(--accent);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

@media (max-width: 640px) {
  main {
    padding: 0 12px;
  }

  .action-row {
    flex-direction: column;
  }

  .action-row button {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .processing-shimmer {
    animation: none;
  }

  .dropzone,
  .slider-handle,
  .after-layer {
    transition: none !important;
  }
}
