:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --line: #dfe2e7;
  --text: #1c2026;
  --muted: #69707c;
  --accent: #3b6cf6;
  --accent-text: #ffffff;
  --danger: #d33a3a;
  --check-a: #e9ebef;
  --check-b: #ffffff;
  --radius: 9px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317;
    --surface: #1a1d22;
    --surface-2: #23272e;
    --line: #2e333b;
    --text: #e7e9ed;
    --muted: #9aa1ac;
    --accent: #5b86ff;
    --check-a: #2a2e35;
    --check-b: #33383f;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; }
.brand svg { width: 26px; height: 26px; }
.brand rect { fill: var(--accent); }
.brand path, .brand circle { fill: #fff; }

.actions { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.repo { color: var(--muted); text-decoration: none; }
.repo:hover { color: var(--text); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 32px;
  padding: 5px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.wide { width: 100%; }
.btn.active { outline: 2px solid var(--accent); outline-offset: -1px; }

.layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  min-height: 0;
}

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.drop {
  position: absolute;
  inset: 16px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--line);
  border-radius: 16px;
  text-align: center;
}
.drop.over, .stage.over .view { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 6%, transparent); }
.drop-inner { max-width: 420px; padding: 24px; }
.drop svg { width: 48px; height: 48px; color: var(--muted); }
.drop h1 { margin: 8px 0 4px; font-size: 22px; font-weight: 650; }
.drop p { margin: 6px 0; }
.link { color: var(--accent); cursor: pointer; text-decoration: underline; }
.muted { color: var(--muted); }

.view {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: auto;
  padding: 16px;
}
.view.actual { place-items: start; }

#canvas {
  max-width: 100%;
  max-height: 100%;
  background-color: var(--check-b);
  background-image:
    linear-gradient(45deg, var(--check-a) 25%, transparent 25%, transparent 75%, var(--check-a) 75%),
    linear-gradient(45deg, var(--check-a) 25%, transparent 25%, transparent 75%, var(--check-a) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 8px 8px;
  box-shadow: var(--shadow);
  image-rendering: auto;
}
.view.actual #canvas { max-width: none; max-height: none; }

.crop { position: absolute; inset: 0; pointer-events: none; }
.crop-box {
  position: absolute;
  border: 1px solid #fff;
  outline: 1px solid rgb(0 0 0 / 50%);
  box-shadow: 0 0 0 100vmax rgb(0 0 0 / 45%);
  pointer-events: auto;
  cursor: move;
  touch-action: none;
}
.crop-box i {
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border: 1px solid rgb(0 0 0 / 50%);
  border-radius: 3px;
}
.crop-box i[data-h="nw"] { left: -7px; top: -7px; cursor: nwse-resize; }
.crop-box i[data-h="ne"] { right: -7px; top: -7px; cursor: nesw-resize; }
.crop-box i[data-h="sw"] { left: -7px; bottom: -7px; cursor: nesw-resize; }
.crop-box i[data-h="se"] { right: -7px; bottom: -7px; cursor: nwse-resize; }

.busy {
  position: absolute;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.busy .bar { flex-basis: 100%; height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden; }
.busy .bar div { height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }
.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.panel {
  overflow-y: auto;
  background: var(--surface);
  border-left: 1px solid var(--line);
}
.panel details { border-bottom: 1px solid var(--line); }
.panel summary {
  padding: 12px 16px;
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  display: flex;
  justify-content: space-between;
}
.panel summary::after { content: "▸"; color: var(--muted); transition: transform 0.15s; }
.panel details[open] summary::after { transform: rotate(90deg); }
.panel summary::-webkit-details-marker { display: none; }

.group { display: flex; flex-direction: column; gap: 8px; padding: 0 16px 14px; }
.group + .group { padding-top: 4px; }
.buttons { display: flex; gap: 6px; flex-wrap: wrap; }
.buttons .btn { flex: 1 1 auto; }

.row { display: grid; grid-template-columns: 92px 1fr auto; align-items: center; gap: 8px; }
.row > span { color: var(--muted); }
.row output { min-width: 38px; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }
.row select, .row input[type="text"], .row input[type="number"] {
  grid-column: 2 / 4;
  min-height: 30px;
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
}
.row input[type="color"] { grid-column: 2 / 4; width: 100%; height: 30px; border: 1px solid var(--line); border-radius: 7px; padding: 2px; background: var(--surface); }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check input { accent-color: var(--accent); }
.hint { margin: 0; color: var(--muted); font-size: 12px; }

/* Tool buttons with a small revert (↶) half on the right. */
.tools { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.split { display: flex; min-width: 0; }
.split > .btn:first-child { flex: 1; min-width: 0; border-top-right-radius: 0; border-bottom-right-radius: 0; }
.split > .btn.rev { border-top-left-radius: 0; border-bottom-left-radius: 0; margin-left: -1px; }
.btn.rev { flex: 0 0 auto; min-width: 34px; padding: 5px 8px; font-size: 15px; line-height: 1; }
.buttons .btn.rev { flex: 0 0 auto; }

.history { list-style: none; margin: 0; padding: 0; max-height: 210px; overflow-y: auto; }
.history li { display: flex; align-items: center; gap: 8px; min-height: 34px; padding: 2px 0; border-bottom: 1px solid var(--line); }
.history li:last-child { border-bottom: 0; }
.history li span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history li:first-child span { color: var(--muted); }
.history li:last-child span { font-weight: 600; }

.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 3px; background: var(--surface-2); border-radius: var(--radius); }
.seg button {
  padding: 6px 4px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  cursor: pointer;
  font-size: 13px;
}
.seg button[aria-checked="true"] { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }
.seg button:disabled { opacity: 0.4; cursor: default; }

.crop-bar { display: flex; gap: 6px; }
.crop-bar select { flex: 1; min-height: 32px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); }

.resize { display: flex; align-items: center; gap: 6px; }
.resize input[type="number"] { flex: 1; min-width: 0; width: 70px; min-height: 30px; padding: 4px 6px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); }
.resize .check { white-space: nowrap; color: var(--muted); }

.status {
  display: flex;
  gap: 18px;
  padding: 6px 16px;
  background: var(--surface);
  border-top: 1px solid var(--line);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
}
.status .muted { margin-left: auto; overflow: hidden; text-overflow: ellipsis; }

.toast {
  position: fixed;
  left: 50%;
  top: 64px;
  transform: translateX(-50%);
  max-width: min(520px, calc(100% - 32px));
  padding: 10px 16px;
  background: var(--text);
  color: var(--bg);
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 10;
}
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 820px) {
  body { grid-template-rows: auto 1fr auto; }
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 16px; }
  .actions { order: 3; flex-basis: 100%; }
  .layout { grid-template-columns: 1fr; grid-template-rows: minmax(260px, 1fr) minmax(0, 45vh); }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
  .status .muted { display: none; }
}
