:root {
  --stage: #20242b;
  --stage-deep: #191c22;
  --panel: #1a1d23;
  --panel-2: #22262e;
  --line: #333944;
  --line-soft: #2a2f38;
  --text: #e8ebf0;
  --muted: #8f98a8;
  --accent: #4d8dff;
  --accent-ink: #0b1a33;
  --accent-soft: rgba(77, 141, 255, 0.16);
  --warn: #f0b429;
  --danger: #ff6b6b;
  --ok: #4cd08a;
  --radius: 6px;
  --font: "Segoe UI Variable Text", "Segoe UI", "SF Pro Text", -apple-system, Inter, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 13px/1.45 var(--font);
  color: var(--text);
  background: var(--stage);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }

.app { display: flex; flex-direction: column; height: 100vh; }
.app > main, .app > .upscale-app { flex: 1; min-height: 0; }

/* ------------------------------------------------------------- topbar --- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--accent); min-width: 0; }
.brand > span:first-of-type { color: var(--text); font-weight: 600; font-size: 14px; letter-spacing: 0.01em; }
.file-name {
  color: var(--muted); font-weight: 400; margin-left: 6px; padding-left: 14px;
  border-left: 1px solid var(--line); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 46vw;
}
.file-name:empty { display: none; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

/* seletor de idioma (bandeiras) */
.lang { display: flex; align-items: center; gap: 4px; margin-right: 6px; padding-right: 12px; border-right: 1px solid var(--line); }
.lang button {
  padding: 3px; border: 0; border-radius: 6px; background: transparent; cursor: pointer;
  line-height: 0; opacity: 0.5; transition: opacity 0.12s, background 0.12s, box-shadow 0.12s;
}
.lang button:hover { opacity: 0.9; background: var(--panel-2); }
.lang button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.lang button[aria-pressed="true"] { opacity: 1; background: var(--accent-soft); box-shadow: inset 0 0 0 1.5px var(--accent); }
.lang svg { display: block; border-radius: 2px; box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12); }

/* ------------------------------------------------------------ buttons --- */
.btn, .text-btn, .icon-btn, .chip, .seg button, .select {
  font: inherit; color: inherit; cursor: pointer;
}
.btn {
  height: 32px; padding: 0 14px; border-radius: var(--radius);
  background: var(--panel-2); border: 1px solid var(--line);
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover:not(:disabled) { background: #2b303a; border-color: #444b58; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: #6ba0ff; border-color: #6ba0ff; }
.btn-primary.dirty { box-shadow: 0 0 0 2px var(--stage), 0 0 0 4px var(--warn); }
.btn-wide { width: 100%; height: 34px; }
.text-btn { background: none; border: 0; padding: 4px 8px; border-radius: 4px; color: var(--muted); }
.text-btn:hover { color: var(--text); background: var(--panel-2); }
.icon-btn {
  width: 28px; height: 28px; border-radius: 4px; background: none; border: 0; font-size: 18px; line-height: 1; color: var(--text);
}
.icon-btn:hover { background: var(--panel-2); }
.chip {
  margin-left: auto; height: 22px; padding: 0 10px; border-radius: 11px;
  background: transparent; border: 1px solid var(--line); color: var(--muted); font-size: 12px;
}
.chip:hover:not(:disabled) { color: var(--text); border-color: #444b58; }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: #b9d2ff; }
.chip:disabled { opacity: 0.35; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------- workspace --- */
.workspace { display: grid; grid-template-columns: 1fr 330px; min-height: 0; }

.viewer { position: relative; min-width: 0; min-height: 0; background: var(--stage-deep); }
.panes { position: absolute; inset: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); }
.pane-wrap { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--stage); }
.pane-head {
  height: 32px; flex: none; display: flex; align-items: center; gap: 10px; padding: 0 12px;
  background: var(--panel); border-bottom: 1px solid var(--line-soft); font-weight: 600;
}
.pane-head .muted { font-weight: 400; }
.pane {
  flex: 1; min-height: 0; overflow: auto; display: flex; cursor: grab; position: relative; padding: 24px;
  scrollbar-width: thin; scrollbar-color: #4a5160 transparent;
}
.pane.grabbing { cursor: grabbing; }
.pane::-webkit-scrollbar { width: 10px; height: 10px; }
.pane::-webkit-scrollbar-thumb { background: #4a5160; border-radius: 5px; border: 2px solid var(--stage); }
.content {
  margin: auto; flex: none; position: relative;
  /* xadrez: mostra transparência real */
  background-color: #fff;
  background-image:
    conic-gradient(#e4e6ea 25%, transparent 0 50%, #e4e6ea 0 75%, transparent 0);
  background-size: 16px 16px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5), 0 10px 40px rgba(0, 0, 0, 0.45);
}
.content img:not([src]) { visibility: hidden; }
.content img { display: block; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; }
.content.pixelated img { image-rendering: pixelated; }
#content-vec.pending img { opacity: 0.25; }

.viewer:not(.has-image) .panes { visibility: hidden; }

/* ---------------------------------------------------------- empty state --- */
.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 32px; }
.empty-card {
  max-width: 420px; text-align: center; padding: 40px 36px; border: 1.5px dashed #464e5c; border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.empty-card h1 { font-size: 19px; font-weight: 600; margin: 14px 0 0; }
.empty-card p { margin: 0 0 16px; color: var(--muted); max-width: 32ch; }
.viewer.has-image .empty { display: none; }

.dropveil {
  position: absolute; inset: 10px; display: grid; place-items: center;
  border: 2px dashed var(--accent); border-radius: 12px; background: rgba(32, 36, 43, 0.86);
  font-size: 18px; font-weight: 600; color: #b9d2ff; z-index: 20; pointer-events: none;
}

/* ---------------------------------------------------------- zoom toolbar --- */
.viewer-toolbar {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 10;
  display: flex; align-items: center; gap: 2px; padding: 4px 6px;
  background: rgba(26, 29, 35, 0.94); border: 1px solid var(--line); border-radius: 10px;
  backdrop-filter: blur(6px);
}
.z-label { min-width: 52px; text-align: center; }
.sep { width: 1px; height: 18px; background: var(--line); margin: 0 6px; }

/* ------------------------------------------------------------------ busy --- */
.busy { position: absolute; inset: 32px 0 0 0; display: grid; place-items: start center; padding-top: 24px; z-index: 15; pointer-events: none; }
.busy-card {
  pointer-events: auto; min-width: 300px; padding: 12px 14px; border-radius: 10px;
  background: rgba(26, 29, 35, 0.96); border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0,0,0,.4);
  display: grid; gap: 10px;
}
.busy-row { display: flex; align-items: center; gap: 10px; }
.spinner {
  width: 14px; height: 14px; border-radius: 50%; border: 2px solid #3a4150; border-top-color: var(--accent);
  animation: spin 0.8s linear infinite; flex: none;
}
.bar { height: 3px; background: #2f3540; border-radius: 2px; overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); transition: width 0.25s ease; }
.busy-card .text-btn { justify-self: end; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } .bar > div { transition: none; } }

/* --------------------------------------------------------------- sidebar --- */
.sidebar {
  background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 4px 0 18px;
  scrollbar-width: thin; scrollbar-color: #4a5160 transparent;
}
.group { padding: 14px 16px; border-bottom: 1px solid var(--line-soft); display: grid; gap: 12px; }
.group:last-child { border-bottom: 0; }
h2 { margin: 0; font-size: 13px; font-weight: 600; }
.field-label { font-weight: 600; }

.row { display: grid; gap: 7px; }
.row-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; color: var(--muted); }
.row-head output { color: var(--text); }

.select {
  width: 100%; height: 32px; padding: 0 10px; border-radius: var(--radius);
  background: var(--panel-2); border: 1px solid var(--line);
}

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { height: 28px; padding: 0 6px; background: var(--panel-2); border: 0; border-right: 1px solid var(--line); color: var(--muted); }
.seg button:last-child { border-right: 0; }
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--accent-soft); color: #cfe0ff; box-shadow: inset 0 -2px 0 var(--accent); }
.seg-wide button { height: 30px; }

input[type="range"] { -webkit-appearance: none; appearance: none; width: 100%; height: 18px; background: transparent; margin: 0; cursor: pointer; }
input[type="range"]::-webkit-slider-runnable-track { height: 3px; background: #3a4150; border-radius: 2px; }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 12px; height: 12px; margin-top: -4.5px;
  background: var(--panel); border: 2px solid var(--accent); border-radius: 2px; /* âncora quadrada, como nos editores vetoriais */
}
input[type="range"]:disabled { opacity: 0.4; cursor: default; }

.check { display: flex; align-items: center; gap: 9px; cursor: pointer; }
.check input { width: 15px; height: 15px; accent-color: var(--accent); margin: 0; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; margin: 0; padding-top: 12px; border-top: 1px solid var(--line-soft); }
.stats div { display: grid; gap: 1px; }
.stats dt { color: var(--muted); font-size: 12px; }
.stats dd { margin: 0; font-weight: 600; }

.hint { margin: -4px 0 0; color: var(--muted); font-size: 12px; min-height: 2.9em; }

/* --------------------------------------------------------------- tabs/upscale --- */
.tab-switcher { display: flex; gap: 4px; margin-left: auto; }
.tab-btn { border: 1px solid transparent; border-radius: 7px; padding: 7px 11px; background: transparent; color: var(--muted); cursor: pointer; font: inherit; }
.tab-btn:hover, .tab-btn.active { color: var(--text); background: var(--accent-soft); border-color: var(--line); }
.upscale-app { min-height: 0; flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) 340px; background: var(--bg); }
.upscale-preview { position: relative; min-height: 0; display: grid; place-items: center; padding: 28px; overflow: auto; }
.upscale-preview-wrap { display: grid; gap: 10px; place-items: center; max-width: 100%; max-height: 100%; }
.upscale-preview-wrap img { max-width: 100%; max-height: calc(100vh - 150px); object-fit: contain; border: 1px solid var(--line); box-shadow: 0 14px 42px rgba(0,0,0,.25); }
.compare { width: min(100%, 1000px); display: grid; gap: 12px; }
.compare-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.compare-modes { width: 260px; }
.compare-range { display: flex; align-items: center; gap: 8px; color: var(--muted); white-space: nowrap; }
.compare-range input { width: 150px; }
.compare-stage { min-height: 300px; max-height: calc(100vh - 180px); display: grid; overflow: hidden; border: 1px solid var(--line); background-color: #d9d9d9; background-image: linear-gradient(45deg, #c2c2c2 25%, transparent 25%), linear-gradient(-45deg, #c2c2c2 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #c2c2c2 75%), linear-gradient(-45deg, transparent 75%, #c2c2c2 75%); background-size: 24px 24px; background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}
.compare-side { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); min-height: 0; }
.compare-side figure { margin: 0; min-width: 0; display: grid; grid-template-rows: 1fr auto; background: rgba(25,28,34,.82); padding: 12px; gap: 8px; }
.compare-side img { width: 100%; height: 100%; min-height: 0; object-fit: contain; }
.compare-side figcaption { text-align: center; color: var(--muted); font-size: 12px; }
.compare-overlay { display: none; position: relative; overflow: hidden; }
.compare-overlay > .compare-original { width: 100%; height: 100%; object-fit: contain; }
.compare-result-clip { position: absolute; inset: 0 auto 0 0; width: 50%; overflow: hidden; border-right: 1px solid var(--accent); }
.compare-result-clip img { width: 100%; height: 100%; object-fit: contain; }
.compare-divider { position: absolute; top: 0; bottom: 0; left: 50%; width: 2px; background: var(--accent); box-shadow: 0 0 0 1px rgba(0,0,0,.3); pointer-events: none; }
.compare-overlay-label { position: absolute; top: 10px; padding: 3px 7px; border-radius: 4px; background: rgba(20,23,28,.8); color: var(--text); font-size: 11px; pointer-events: none; }
.original-label { left: 10px; } .result-label { right: 10px; }
.compare-stage.overlay-mode .compare-side { display: none; }
.compare-stage.overlay-mode .compare-overlay { display: block; }
.upscale-sidebar { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; }
.upscale-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.upscale-grid label { display: grid; gap: 6px; color: var(--muted); font-size: 12px; }
.upscale-grid input[type="number"] { width: 100%; box-sizing: border-box; }
input[type="number"] { height: 32px; padding: 0 8px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel-2); color: var(--text); }
@media (max-width: 820px) { .upscale-app { grid-template-columns: 1fr; grid-template-rows: minmax(300px, 1fr) auto; } .upscale-sidebar { border-left: 0; border-top: 1px solid var(--line); } .tab-switcher { margin-left: 8px; } }

/* ----------------------------------------------------------------- toast --- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  max-width: min(640px, 90vw); padding: 10px 16px; border-radius: 8px;
  background: #2b303a; border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0,0,0,.45);
  display: flex; gap: 12px; align-items: center;
}
.toast.error { border-color: var(--danger); }
.toast.ok { border-color: var(--ok); }
.toast button { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; padding: 0; white-space: nowrap; }

@media (max-width: 1100px) { .workspace { grid-template-columns: 1fr 290px; } }
