:root {
  /* Native UI -- form fields, carets, scrollbars -- is painted by the browser
     from color-scheme, not from these tokens. Without it the toggle recolours
     the page and leaves a light scrollbar on a dark one. */
  color-scheme: light;
  /* Segmented footer pill -- branding/chrome.css "the footer's controls". */
  --chip-bg: #f3f3ed;
  --chip-bg-hover: #eaeae2;
  --chip-line: #d5d1c7;
  /* Corner radius, Fibonacci, same scale as spacing so that
     inner = outer - padding lands on another step.
     See branding/tokens.css. */
  --radius-xs: 5px;
  --radius-sm: 8px;
  --radius-md: 13px;
  --radius-lg: 21px;
  --radius-pill: 999px;
  --radius-circle: 50%;

  --bg: #fbfbf9;
  --panel: #fcfcfb;
  --panel-2: #eeeef1;
  --border: #dfdbd3;
  --text: #1c1917;
  /* Dấu mark, KHÔNG thuộc bậc chữ: chỉnh màu chữ thì logo phải đứng yên.
     Cùng giá trị ở mọi site d4t0 (branding/tokens.css --logo-color). */
  --logo-color: #293133;
  --muted: #6f6b64;
  --accent: #6440e0;
  --on-accent: #ffffff;
  --danger: #c0342b;
  --checker-a: #e4e4e9;
  --checker-b: #f7f7f8;
  --shimmer: rgba(255, 255, 255, 0.35);
}

/* Dark tokens exist TWICE on purpose -- once for the OS preference, once for an
   explicit choice from the footer toggle. CSS cannot share a declaration block
   between a media query and a plain selector, and the two must not drift, so
   branding/check-contrast.py compares them and fails on any token that differs.
   Edit both.

   :not([data-theme="light"]) is what lets the toggle win over the OS: without
   it, choosing light on a machine set to dark leaves the media query in force
   and the button appears to do nothing. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --chip-bg: #16191f;
    --chip-bg-hover: #1c202a;
    --chip-line: #2e3036;

    --bg: #101014;
    --panel: #18181d;
    --panel-2: #202027;
    --border: #34363c;
    --text: #eeeef2;
    --logo-color: #eef0f4; /* branding --logo-color, khối dark */
    --muted: #a2a3a8;
    --accent: #9c7bff;
    --on-accent: #101014;
    --danger: #ff6b60;
    --checker-a: #26262f;
    --checker-b: #1a1a20;
    --shimmer: rgba(255, 255, 255, 0.08);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --chip-bg: #16191f;
  --chip-bg-hover: #1c202a;
  --chip-line: #2e3036;

  --bg: #101014;
  --panel: #18181d;
  --panel-2: #202027;
  --border: #34363c;
  --text: #eeeef2;
  --logo-color: #eef0f4; /* branding --logo-color, khối dark */
  --muted: #a2a3a8;
  --accent: #9c7bff;
  --on-accent: #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;
}

/* The mark is a masked shape, not an <img>, so it takes var(--text) and
   follows light/dark without shipping two files. */
/* 36×36 is the repo-wide size (branding/chrome.css, 22/08/2026). */
.topbar-logo {
  display: block;
  width: 36px;
  height: 36px;
  background-color: var(--logo-color);
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: center;
  mask-position: center;
  -webkit-mask-size: contain;
  mask-size: contain;
  -webkit-mask-image: url("/logo.svg");
  mask-image: url("/logo.svg");
  /* Không xoay ở đây: logo.svg sinh ra đã xoay 45° sẵn
     (branding/build-icons.py, LOGO_ROTATE). Xoay thêm lần nữa là ra 90°. */
}

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: var(--radius-lg);
  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: var(--radius-lg);
  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: var(--radius-lg);
  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(--on-accent, #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: var(--radius-circle);
  background: var(--accent);
  color: var(--on-accent);
  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: var(--radius-pill);
  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: var(--radius-circle);
  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: var(--radius-sm);
  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(--on-accent);
  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: var(--radius-sm);
  border: 2px solid var(--border);
  background-size: cover;
  background-position: center;
  cursor: pointer;
  padding: 0;
}

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

/* Centred (22/08/2026, repo-wide — branding/chrome.css), replacing the
   left-aligned rule this comment used to explain. The horizontal padding still
   matches .topbar: it keeps the line off the viewport edge on a narrow screen. */
.footer {
  margin-top: auto;
  /* flex: none + width: 100% are not decoration -- they are in
     branding/chrome.css for this exact reason. The footer is a flex item in a
     column that centres its children, so without them it shrinks to its own
     content (measured 40px) and then squeezes the pill to zero width. */
  flex: none;
  width: 100%;
  /* Flex, because the footer now carries a control beside the credit line.
     text-align: center centres inline text but leaves a button stretched to the
     block's height -- the pill measured 44px tall instead of 27px until this
     rule existed. */
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 13px;
  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;
  }
}


/* ---------------------------------------------- footer controls (branding)
   One segmented pill for controls that change how the page behaves. The group
   draws the fill, rounding and dividers; segments inside are bare, or you get
   a pill inside a pill. See branding/chrome.css. */
.pill-group {
  display: inline-flex;
  align-items: center;
  border: 0;
  border-radius: var(--radius-pill);
  background: var(--chip-bg);
  overflow: hidden;
}

.pill-group > * + * { border-left: 1px solid var(--chip-line); }

/* Scoped to .pill-group so it outranks whatever the project already says about
   `button`. removebg has a global `button { min-height: 44px; padding: 12px 18px }`
   for touch targets, and min-height beats height no matter the specificity --
   the segment measured 44px tall until this rule killed it. Any project with a
   global button rule will need the same. flex: none stops the segment being
   shrunk by the group. */
.pill-group .theme-switch {
  min-height: 0;
  flex: none;
}

.theme-switch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 27px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease, background-color 0.15s ease;
}

.theme-switch:hover { color: var(--text); background-color: var(--chip-bg-hover); }

.theme-switch svg {
  width: 15px;
  height: 15px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Which glyph shows follows the RESOLVED theme on <html>, which theme.js always
   writes explicitly -- CSS cannot ask what the media query decided. The button
   shows where a click goes: a moon on a light page, a sun on a dark one.
   display, not [hidden]: a class setting display silently beats the UA's
   [hidden] rule. */
.theme-switch .icon-moon { display: block; }
.theme-switch .icon-sun { display: none; }
:root[data-theme="dark"] .theme-switch .icon-moon { display: none; }
:root[data-theme="dark"] .theme-switch .icon-sun { display: block; }

