:root {
  --bg: #f6f7f9; --panel: #ffffff; --line: #e3e6eb; --text: #1b1f27; --muted: #667085;
  --accent: #5b5bf0; --accent-ink: #fff; --ok: #12a150; --err: #d92d20; --stage: #eceef2;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1115; --panel: #171a21; --line: #2a2f3a; --text: #e8eaef; --muted: #98a2b3;
          --accent: #7b7bff; --stage: #0b0d11; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
       display: flex; flex-direction: column; min-height: 100vh; }
a { color: var(--accent); }
[hidden] { display: none !important; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px;
          border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.logo { color: var(--accent); font-size: 20px; }
.tag { font-weight: 500; font-size: 12px; color: var(--muted); }
.model-status { font-size: 12px; color: var(--muted); }
.model-status.ready { color: var(--ok); }
.model-status.error { color: var(--err); }

.btn { border: 1px solid var(--line); background: var(--panel); color: var(--text); padding: 8px 14px; border-radius: 8px;
       font: inherit; cursor: pointer; white-space: nowrap; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.small { padding: 5px 10px; font-size: 13px; }

/* start */
.start { max-width: 760px; margin: 0 auto; padding: 40px 16px; text-align: center; flex: 1; }
.start h1 { font-size: clamp(24px, 4vw, 34px); margin: 0 0 10px; }
.sub { color: var(--muted); margin: 0 auto 26px; max-width: 560px; }
.dropzone { border: 2px dashed var(--line); border-radius: 16px; padding: 36px 16px; background: var(--panel); transition: .15s; }
.dropzone.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--panel)); }
.dz-title { font-size: 18px; font-weight: 600; margin: 0; }
.dz-or { color: var(--muted); margin: 6px 0 12px; }
.dz-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.dz-hint { color: var(--muted); font-size: 12px; margin: 14px 0 0; }
.terms { display: flex; gap: 8px; align-items: flex-start; justify-content: center; text-align: left; margin: 18px auto 0; max-width: 560px; font-size: 13px; }
.terms.shake { animation: shake .35s; color: var(--err); }
@keyframes shake { 25% { transform: translateX(-6px) } 75% { transform: translateX(6px) } }
.how { display: flex; gap: 12px; justify-content: center; padding: 0; margin: 28px 0 0; list-style: none; flex-wrap: wrap; counter-reset: s; }
.how li { counter-increment: s; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; font-size: 13px; color: var(--muted); }
.how li::before { content: counter(s); display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%;
                  background: var(--accent); color: #fff; font-size: 11px; margin-right: 6px; font-weight: 700; }

/* workspace */
.workspace { flex: 1; display: grid; grid-template-columns: 220px 1fr; grid-template-rows: minmax(0, 1fr); min-height: 0; }
.sidebar { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; max-height: calc(100vh - 90px); }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 10px; border-bottom: 1px solid var(--line); font-weight: 600; }
.thumbs { list-style: none; margin: 0; padding: 8px; overflow-y: auto; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; align-content: start; }
.thumbs li { position: relative; border: 2px solid transparent; border-radius: 8px; overflow: hidden; cursor: pointer; aspect-ratio: 1; background: var(--stage); }
.thumbs li.active { border-color: var(--accent); }
.thumbs img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumbs .badge { position: absolute; right: 4px; top: 4px; font-size: 10px; padding: 1px 5px; border-radius: 10px; background: rgba(0,0,0,.6); color: #fff; }
.thumbs .badge.done { background: var(--ok); }
.thumbs .badge.error { background: var(--err); }
.thumbs .badge.busy { background: var(--accent); }
.thumbs .own { position: absolute; left: 4px; top: 4px; font-size: 10px; padding: 1px 5px; border-radius: 10px; background: #f79009; color: #fff; }

.editor { display: flex; flex-direction: column; min-width: 0; min-height: 0; padding: 10px 14px; gap: 10px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: center; }
.group { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.tool, .seg { border: 1px solid var(--line); background: var(--panel); color: var(--text); padding: 6px 10px; border-radius: 8px; cursor: pointer; font: inherit; font-size: 13px; }
.tool.active, .seg.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.size { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.size span { min-width: 26px; }
.scope span { font-size: 13px; color: var(--muted); }

.stage-wrap { position: relative; flex: 1 1 0; min-height: 220px; background: var(--stage); border-radius: var(--radius);
              display: grid; place-items: center; overflow: hidden;
              background-image: linear-gradient(45deg, rgba(128,128,128,.08) 25%, transparent 25%, transparent 75%, rgba(128,128,128,.08) 75%),
                                linear-gradient(45deg, rgba(128,128,128,.08) 25%, transparent 25%, transparent 75%, rgba(128,128,128,.08) 75%);
              background-size: 20px 20px; background-position: 0 0, 10px 10px; }
.stage { position: relative; line-height: 0; touch-action: none; }
.stage canvas { display: block; }
.stage canvas.result, .stage canvas.mask { position: absolute; inset: 0; width: 100%; height: 100%; }
.stage canvas.mask { opacity: .5; cursor: none; }
.stage.compare canvas.mask { display: none; }
.stage:not(.compare) canvas.result { display: none; }
.compare-handle { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; box-shadow: 0 0 4px rgba(0,0,0,.5); cursor: ew-resize; }
.compare-handle span { position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); background: #fff; color: #333;
                       border-radius: 50%; width: 30px; height: 30px; display: grid; place-items: center; font-size: 14px; line-height: 1; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.stage.compare { cursor: ew-resize; }
.cursor { position: absolute; pointer-events: none; border: 1.5px solid #fff; outline: 1px solid rgba(0,0,0,.6); border-radius: 50%; transform: translate(-50%,-50%); display: none; }
.stage-msg { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,.7); color: #fff;
             padding: 5px 12px; border-radius: 20px; font-size: 12px; line-height: 1.4; pointer-events: none; white-space: nowrap; }
.stage-msg:empty { display: none; }

.dock { display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.dock-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.view-toggle { display: flex; gap: 4px; }
.export { border-top: 1px solid var(--line); padding-top: 8px; color: var(--muted); }
.export .btn.ready { background: var(--ok); border-color: var(--ok); color: #fff; font-weight: 600; }
.tool.auto { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.tool.auto:disabled { opacity: .5; }
.progress { position: relative; height: 22px; background: var(--line); border-radius: 11px; overflow: hidden; }
.progress .bar { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.progress span { position: absolute; inset: 0; display: block; text-align: center; line-height: 22px; padding: 0 10px; font-size: 12px; font-weight: 600;
                 white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.foot { text-align: center; color: var(--muted); font-size: 12px; padding: 10px 16px; border-top: 1px solid var(--line); }

@media (max-width: 760px) {
  html, body { height: auto; }
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto auto; }
  .stage-wrap { flex: none; min-height: 0; }
  .editor { padding: 10px; }
  .dock { position: sticky; bottom: 0; z-index: 5; padding: 8px 0; margin: 0 -10px; padding: 8px 10px;
          border-top: 1px solid var(--line); box-shadow: 0 -4px 12px rgba(0,0,0,.15); }
  .dock { gap: 6px; }
  .dock-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .dock-row .view-toggle { grid-column: 1 / -1; }
  .dock .btn { padding: 7px 6px; font-size: 12px; white-space: normal; min-width: 0; line-height: 1.25; }
  .toolbar { gap: 6px 10px; }
  .tool, .seg { padding: 5px 8px; font-size: 12px; }
  .scope span { display: none; }
  .size input { width: 90px; }
  .topbar .tag { display: none; }
  .side-head { padding: 6px 10px; }
  .thumbs { grid-auto-columns: 60px; padding: 6px; }
  .dock .export { padding-top: 6px; }
  #saveFolder { display: none; }
  .dock .spacer { display: none; }
  #doneLabel { grid-column: 1 / -1; font-size: 12px; }
  .sidebar { max-height: none; border-right: 0; border-bottom: 1px solid var(--line); }
  .thumbs { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: 72px; overflow-x: auto; overflow-y: hidden; }
  .stage-wrap { min-height: 260px; }
}

/* delete button on thumbnails */
.thumbs .del { position: absolute; left: 4px; bottom: 4px; width: 24px; height: 24px; border-radius: 50%; border: 0;
               background: rgba(0,0,0,.65); color: #fff; font-size: 12px; line-height: 24px; padding: 0; cursor: pointer; opacity: .85; }
.thumbs .del:hover { background: var(--err); opacity: 1; }
@media (hover: hover) { .thumbs li .del { opacity: 0; } .thumbs li:hover .del, .thumbs li.active .del { opacity: .85; } }
.terms-msg { color: var(--err); font-weight: 600; margin: 18px 0 -10px; }
