:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #f0f2f6;
  --border: #e1e4ea;
  --text: #171a21;
  --muted: #646b78;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eceafd;
  --on-accent: #ffffff;
  --danger: #c62828;
  --danger-soft: #fdecec;
  --warn: #9a6700;
  --checker-a: #ffffff;
  --checker-b: #e6e8ee;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 14px;
  --radius-sm: 9px;
  --stage-h: min(64vh, 680px);
  --pos: 50%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --border: #2b303b;
    --text: #e8eaf0;
    --muted: #9aa1ae;
    --accent: #8b85ff;
    --accent-hover: #a29dff;
    --accent-soft: #26244a;
    --on-accent: #0f1115;
    --danger: #ff7b7b;
    --danger-soft: #3a1d1f;
    --warn: #e3b341;
    --checker-a: #2a2e37;
    --checker-b: #22252d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex;
  flex-direction: column;
}

[hidden] { display: none !important; }

button, input, select { font: inherit; color: inherit; }

.topbar {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 6px 16px;
  padding: 18px max(16px, env(safe-area-inset-left)) 8px;
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
}

.brand { display: flex; align-items: center; gap: 10px; }

.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background:
    linear-gradient(135deg, transparent 48%, var(--on-accent) 48% 56%, transparent 56%),
    linear-gradient(135deg, var(--accent), #ec4899);
  align-self: center;
}

.brand-name { font-weight: 700; font-size: 19px; letter-spacing: -.01em; }

.brand-tag { margin: 0; color: var(--muted); font-size: 14px; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 16px;
  padding: 8px max(16px, env(safe-area-inset-right)) 16px max(16px, env(safe-area-inset-left));
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
  align-items: start;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.settings {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: sticky;
  top: 12px;
}

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 22px 14px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background-color .15s;
  outline: none;
}

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

.dropzone.is-over { border-color: var(--accent); background: var(--accent-soft); }

.dropzone-icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: var(--accent-soft);
  position: relative;
  margin-bottom: 6px;
}

.dropzone-icon::before, .dropzone-icon::after {
  content: "";
  position: absolute;
  left: 50%;
  background: var(--accent);
  transform: translateX(-50%);
}

.dropzone-icon::before { top: 10px; width: 3px; height: 18px; border-radius: 2px; }

.dropzone-icon::after {
  top: 9px;
  width: 12px;
  height: 12px;
  background: none;
  border-left: 3px solid var(--accent);
  border-top: 3px solid var(--accent);
  transform: translateX(-50%) rotate(45deg);
}

.dropzone-title { font-weight: 600; }

.dropzone-hint { color: var(--muted); font-size: 13px; }

.file-info {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.file-thumb {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 7px;
  background: var(--checker-b);
  flex: none;
}

.file-meta { display: flex; flex-direction: column; min-width: 0; flex: 1; }

.file-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.file-details { color: var(--muted); font-size: 13px; }

.settings-form { display: flex; flex-direction: column; gap: 14px; }

.field { border: 0; margin: 0; padding: 0; min-width: 0; }

.field legend, .field-head label {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 6px;
  padding: 0;
}

.field-head { display: flex; justify-content: space-between; align-items: baseline; }

.field-head output {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--accent);
  min-width: 2ch;
  text-align: right;
}

.segmented {
  display: flex;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}

.segmented label { flex: 1; min-width: 0; }

.segmented input { position: absolute; opacity: 0; pointer-events: none; }

.segmented span {
  display: block;
  text-align: center;
  padding: 7px 8px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  color: var(--muted);
  transition: background-color .15s, color .15s;
  user-select: none;
}

.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

.segmented-sm span { padding: 5px 10px; white-space: nowrap; }

input[type="range"] { width: 100%; accent-color: var(--accent); margin: 2px 0; }

.range-labels { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
  transition: background-color .15s, border-color .15s, opacity .15s;
  white-space: nowrap;
}

.btn:hover:not(:disabled) { border-color: var(--accent); }

.btn:disabled { opacity: .55; cursor: not-allowed; }

.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

#convert-btn { width: 100%; padding: 12px 16px; font-size: 15px; }

.btn-spinner {
  display: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-right-color: transparent;
  animation: spin .7s linear infinite;
}

.is-busy .btn-spinner { display: inline-block; }

.stale-note { margin: -6px 0 0; color: var(--warn); font-size: 13px; text-align: center; }

.icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  display: inline-grid;
  place-items: center;
  flex: none;
}

.icon-btn:disabled { opacity: .45; cursor: not-allowed; }

.link-btn {
  border: 0;
  background: none;
  color: var(--accent);
  cursor: pointer;
  padding: 4px 6px;
  font-size: 13px;
  font-weight: 600;
}

.link-btn:disabled { color: var(--muted); cursor: not-allowed; }

.workspace { padding: 14px; display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 14px;
}

.banner-error { background: var(--danger-soft); color: var(--danger); }

.banner .icon-btn { background: transparent; border-color: transparent; color: inherit; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; }

.zoom { display: flex; align-items: center; gap: 6px; }

.zoom-value { min-width: 48px; text-align: center; font-variant-numeric: tabular-nums; font-size: 13px; }

.bg-toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); margin-left: auto; cursor: pointer; }

.bg-toggle input { accent-color: var(--accent); }

.empty-state {
  min-height: var(--stage-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  border-radius: var(--radius);
  background: var(--surface-2);
}

.empty-state p { margin: 4px 0; max-width: 420px; }

.empty-icon {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  margin-bottom: 12px;
  background:
    radial-gradient(circle at 30% 35%, var(--accent) 0 9px, transparent 10px),
    linear-gradient(135deg, transparent 55%, #ec4899 55%),
    var(--accent-soft);
}

.muted { color: var(--muted); font-size: 14px; }

.viewer { position: relative; display: grid; gap: 12px; }

.view-side { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.view-side #pane-compare, .view-slider #pane-original, .view-slider #pane-result,
.view-result #pane-original, .view-result #pane-compare { display: none; }

.pane { margin: 0; min-width: 0; display: flex; flex-direction: column; gap: 6px; }

.pane figcaption { font-size: 13px; font-weight: 600; color: var(--muted); }

.stage {
  height: var(--stage-h);
  overflow: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  overscroll-behavior: contain;
}

.stage.checker, .file-thumb {
  background-color: var(--checker-a);
  background-image: conic-gradient(var(--checker-b) 25%, transparent 0 50%, var(--checker-b) 0 75%, transparent 0);
  background-size: 18px 18px;
}

.viewer.no-checker .stage { background-image: none; background-color: var(--surface); }

.stage-inner {
  position: relative;
  width: calc(var(--zoom, 1) * 100%);
  height: calc(var(--zoom, 1) * 100%);
  padding: 10px;
}

.stage-inner img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
}

.stage-inner img:not([src]), .stage-inner img[src=""] { visibility: hidden; }

.result-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 14px;
}

.compare-inner img { position: absolute; inset: 10px; width: calc(100% - 20px); height: calc(100% - 20px); }

.cmp-top { clip-path: inset(0 0 0 var(--pos)); }

.cmp-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(10px + (100% - 20px) * var(--pos-num, .5));
  width: 2px;
  margin-left: -1px;
  background: var(--accent);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .6);
  pointer-events: none;
}

.cmp-range { margin-top: 4px; }

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  backdrop-filter: blur(2px);
  border-radius: var(--radius-sm);
  font-weight: 600;
  z-index: 2;
}

.loader {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.result-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 20px;
  padding-top: 4px;
}

.stats { display: flex; flex-wrap: wrap; gap: 8px 22px; margin: 0; }

.stats div { display: flex; flex-direction: column; }

.stats dt { font-size: 12px; color: var(--muted); }

.stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }

.downloads { display: flex; flex-wrap: wrap; gap: 8px; }

.png-group { display: flex; }

.png-group select {
  border: 1px solid var(--border);
  border-right: 0;
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  background: var(--surface-2);
  padding: 0 8px;
  cursor: pointer;
}

.png-group .btn { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }

.footer {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  padding: 8px 16px max(16px, env(safe-area-inset-bottom));
}

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .settings { position: static; }
  :root { --stage-h: min(56vh, 520px); }
}

@media (max-width: 640px) {
  .view-side { grid-template-columns: minmax(0, 1fr); }
  :root { --stage-h: min(46vh, 420px); }
  .bg-toggle { margin-left: 0; }
  .result-bar, .downloads { flex-direction: column; align-items: stretch; }
  .downloads .btn, .png-group { width: 100%; }
  .png-group .btn { flex: 1; }
  .brand-tag { font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0s !important; transition: none !important; }
}
