:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f2f8;
  --text: #1a1d2e;
  --muted: #646a85;
  --border: #e3e5ef;
  --primary: #6d5dfc;
  --primary-2: #22c1c3;
  --primary-ink: #ffffff;
  --danger: #e5484d;
  --checker-a: #ffffff;
  --checker-b: #e6e8f0;
  --shadow: 0 10px 30px rgba(28, 31, 60, 0.08);
  --radius: 18px;
}

[data-theme="dark"] {
  --bg: #0f1120;
  --surface: #171a2e;
  --surface-2: #1f2340;
  --text: #eef0fa;
  --muted: #9aa0bf;
  --border: #2a2f52;
  --checker-a: #2a2e4a;
  --checker-b: #22253d;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  transition: background-color .2s ease, color .2s ease;
}

img { max-width: 100%; display: block; }

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

.container { width: min(1120px, 100% - 32px); margin-inline: auto; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }

kbd {
  font: 600 .75rem/1 "Inter", sans-serif;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface);
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar__inner { display: flex; align-items: center; justify-content: space-between; height: 64px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; color: var(--text); text-decoration: none; letter-spacing: -.01em; }
.brand img { border-radius: 8px; }
.topbar__actions { display: flex; align-items: center; gap: 14px; }
.link { color: var(--primary); text-decoration: none; font-weight: 600; cursor: pointer; }
.link:hover { text-decoration: underline; }

.icon-btn {
  display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--primary); }
.icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }

/* ---------- Hero ---------- */
.hero { text-align: center; padding: 48px 0 28px; }
.hero h1 { font-size: clamp(2rem, 4.5vw, 3.2rem); line-height: 1.1; margin: 0 0 14px; letter-spacing: -.03em; }
.hero p { color: var(--muted); max-width: 620px; margin: 0 auto; font-size: 1.08rem; }
.hero__stats { margin-top: 14px !important; font-size: .9rem !important; }
.grad { background: linear-gradient(90deg, var(--primary), var(--primary-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  margin-bottom: 22px;
}
.section-title { margin: 0 0 14px; font-size: 1.15rem; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.section-head .section-title { margin: 0; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: 600 .95rem/1 "Inter", sans-serif;
  padding: 13px 20px; border-radius: 12px; border: 1px solid transparent;
  cursor: pointer; transition: transform .08s ease, filter .15s ease, background-color .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn--primary { background: linear-gradient(135deg, var(--primary), color-mix(in srgb, var(--primary) 70%, var(--primary-2))); color: var(--primary-ink); }
.btn--primary:not(:disabled):hover { filter: brightness(1.08); }
.btn--ghost { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn--ghost:hover { border-color: var(--primary); }
.btn--block { width: 100%; }
.btn--sm { padding: 8px 12px; font-size: .85rem; border-radius: 10px; }
.btn--danger { color: var(--danger); }

/* ---------- Dropzone ---------- */
.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  min-height: 340px; padding: 32px; text-align: center;
  border: 2px dashed var(--border); border-radius: 14px;
  background:
    radial-gradient(circle at 20% 10%, color-mix(in srgb, var(--primary) 9%, transparent), transparent 50%),
    radial-gradient(circle at 80% 90%, color-mix(in srgb, var(--primary-2) 10%, transparent), transparent 50%),
    var(--surface-2);
  cursor: pointer; transition: border-color .15s ease, background-color .15s ease;
  outline: none;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.is-dragover { border-color: var(--primary); }
.dropzone.is-dragover { background-color: color-mix(in srgb, var(--primary) 8%, var(--surface-2)); }
.dropzone__icon { width: 76px; height: 76px; display: grid; place-items: center; border-radius: 20px; background: var(--surface); color: var(--primary); box-shadow: var(--shadow); margin-bottom: 10px; }
.dropzone__title { font-size: 1.2rem; font-weight: 700; margin: 0; }
.dropzone__hint { color: var(--muted); margin: 0 0 14px; font-size: .92rem; }

/* ---------- Editor ---------- */
.editor { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 22px; }

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

.stage {
  position: relative; border-radius: 14px; overflow: hidden;
  min-height: 360px; display: grid; place-items: center;
  background: var(--surface-2); border: 1px solid var(--border);
}
.stage__canvas { position: relative; display: inline-block; max-width: 100%; line-height: 0; user-select: none; }
.stage__canvas.checker, .stage__canvas[style*="background"] { border-radius: 4px; }
.stage__img { max-height: 70vh; width: auto; height: auto; object-fit: contain; }
.stage__img--original {
  position: absolute; inset: 0; width: 100%; height: 100%;
  clip-path: inset(0 calc(100% - var(--pos, 50%)) 0 0);
}
.stage.is-processing .stage__img--original { clip-path: none; }
.stage.is-processing .stage__img--result,
.stage.is-processing .compare-handle,
.stage.is-processing .compare-range,
.stage.is-processing .badge { visibility: hidden; }
.stage:not(.has-result) .compare-handle,
.stage:not(.has-result) .compare-range,
.stage:not(.has-result) .badge { display: none; }

.compare-handle {
  position: absolute; top: 0; bottom: 0; left: var(--pos, 50%);
  width: 2px; margin-left: -1px; background: #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.15);
  pointer-events: none;
}
.compare-handle span {
  position: absolute; top: 50%; left: 50%; width: 36px; height: 36px; transform: translate(-50%, -50%);
  border-radius: 50%; background: #fff; box-shadow: 0 4px 14px rgba(0,0,0,.25);
}
.compare-handle span::before, .compare-handle span::after {
  content: ""; position: absolute; top: 50%; width: 0; height: 0; border: 6px solid transparent; transform: translateY(-50%);
}
.compare-handle span::before { left: 3px; border-right-color: #6d5dfc; }
.compare-handle span::after { right: 3px; border-left-color: #6d5dfc; }
.compare-range {
  position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: ew-resize;
  -webkit-appearance: none; appearance: none;
}
.badge {
  position: absolute; top: 12px; padding: 4px 10px; border-radius: 999px;
  font: 600 .75rem/1.4 "Inter", sans-serif; color: #fff; background: rgba(17, 20, 40, .65);
  pointer-events: none; backdrop-filter: blur(4px);
}
.badge--left { left: 12px; }
.badge--right { right: 12px; }

.overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  background: color-mix(in srgb, var(--surface) 78%, transparent); backdrop-filter: blur(3px);
  padding: 24px; text-align: center;
}
.overlay__title { margin: 0; font-weight: 700; font-size: 1.05rem; }
.overlay__hint { margin: 0; color: var(--muted); font-size: .85rem; max-width: 340px; }
.spinner {
  width: 46px; height: 46px; border-radius: 50%;
  border: 4px solid color-mix(in srgb, var(--primary) 20%, transparent); border-top-color: var(--primary);
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress { width: min(320px, 80%); height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; border: 1px solid var(--border); }
.progress__bar { height: 100%; width: 0%; background: linear-gradient(90deg, var(--primary), var(--primary-2)); transition: width .2s ease; }
.progress.is-indeterminate .progress__bar { width: 35% !important; animation: indet 1.2s ease-in-out infinite; }
@keyframes indet { 0% { transform: translateX(-100%); } 100% { transform: translateX(300%); } }

.panel { display: flex; flex-direction: column; gap: 22px; }
.panel__title { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 0 0 10px; }
.panel__actions { display: flex; flex-direction: column; gap: 10px; margin-top: auto; }
.meta { margin: 0; display: grid; gap: 8px; }
.meta div { display: flex; justify-content: space-between; gap: 12px; font-size: .92rem; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; font-weight: 600; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 190px; }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.swatch {
  width: 34px; height: 34px; border-radius: 10px; cursor: pointer;
  border: 2px solid var(--border); background: var(--c);
}
.swatch--checker {
  background-color: #fff;
  background-image: linear-gradient(45deg, #d9dce8 25%, transparent 25%), linear-gradient(-45deg, #d9dce8 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #d9dce8 75%), linear-gradient(-45deg, transparent 75%, #d9dce8 75%);
  background-size: 10px 10px; background-position: 0 0, 0 5px, 5px -5px, -5px 0;
}
.swatch.is-active { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 25%, transparent); }

.error {
  margin: 16px 0 0; padding: 12px 14px; border-radius: 12px;
  background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); font-weight: 500;
}

/* ---------- Settings ---------- */
.settings__grid { display: flex; flex-wrap: wrap; align-items: center; gap: 18px 32px; margin-bottom: 10px; }
.field { display: flex; align-items: center; gap: 12px; }
.field__label { font-weight: 600; }
select {
  font: 500 .95rem "Inter", sans-serif; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
.toggle { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 500; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle__track { width: 44px; height: 26px; border-radius: 999px; background: var(--border); position: relative; transition: background .15s ease; flex: none; }
.toggle__thumb { position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .15s ease; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.toggle input:checked + .toggle__track { background: var(--primary); }
.toggle input:checked + .toggle__track .toggle__thumb { transform: translateX(18px); }
.toggle input:focus-visible + .toggle__track { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- History ---------- */
.history { list-style: none; padding: 0; margin: 12px 0 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
.history__item { border: 1px solid var(--border); border-radius: 14px; overflow: hidden; background: var(--surface-2); display: flex; flex-direction: column; }
.history__thumb { aspect-ratio: 1 / 1; display: grid; place-items: center; padding: 8px; }
.history__thumb img { max-height: 100%; object-fit: contain; }
.history__body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.history__name { font-weight: 600; font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history__date { font-size: .78rem; color: var(--muted); }
.history__actions { display: flex; gap: 6px; }
.history__actions .btn { flex: 1; }

/* ---------- Features ---------- */
.features { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; margin: 8px 0 40px; }
.feature { padding: 18px 20px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); }
.feature h3 { margin: 0 0 6px; font-size: 1rem; }
.feature p { margin: 0; color: var(--muted); font-size: .92rem; }

.footer { border-top: 1px solid var(--border); padding: 22px 0; color: var(--muted); font-size: .88rem; text-align: center; }
.footer p { margin: 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #1a1d2e; color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 500;
  box-shadow: 0 10px 30px rgba(0,0,0,.25); z-index: 50; max-width: calc(100% - 32px);
}

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .editor { grid-template-columns: 1fr; }
  .features { grid-template-columns: 1fr; }
  .meta dd { max-width: 60vw; }
}
@media (max-width: 520px) {
  .card { padding: 16px; }
  .dropzone { min-height: 280px; padding: 22px; }
  .hero { padding-top: 32px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}
