:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #9097a6;
  --accent: #4f8cff;
  --accent-2: #3a74e6;
  --danger: #ff5d5d;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
[hidden] { display: none !important; }

/* ------------------------------------------------------------ top bar */
.topbar {
  display: flex; align-items: center; gap: 24px;
  height: 52px; padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex: none;
}
.brand { font-weight: 700; font-size: 16px; letter-spacing: .2px; display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; }
.app-name { font-weight: 600; color: var(--muted); border-left: 1px solid var(--line); padding-left: 14px; white-space: nowrap; }
.logo {
  display: inline-grid; place-items: center; width: 26px; height: 26px; border-radius: 7px;
  background: linear-gradient(135deg, #4f8cff, #8b5cf6); font-size: 14px;
}
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 3px; border-radius: 9px; }
.tab {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600;
  padding: 6px 14px; border-radius: 7px; cursor: pointer;
}
.tab.active { background: var(--panel-2); color: var(--text); }
.topbar-note { margin-left: auto; color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------ layout */
.workspace { flex: 1; display: flex; min-height: 0; }
.stage { position: relative; flex: 1; min-width: 0; overflow: hidden; }
.panel {
  width: 300px; flex: none; overflow-y: auto;
  background: var(--panel); border-left: 1px solid var(--line);
  padding: 8px 16px 24px;
}
.group { padding: 14px 0; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.group:last-child { border-bottom: 0; }
.group h2 { margin: 0; font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); font-weight: 700; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.row.center { justify-content: center; }
.row.center > * { flex: none; }
.meta { margin: 0; color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------ controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  font: inherit; font-weight: 600; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover:not(:disabled) { border-color: var(--line-strong); background: var(--hover); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-2); border-color: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12px; font-weight: 500; }
.btn.wide { width: 100%; }

select {
  width: 100%; font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
.slider { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.slider span { display: flex; justify-content: space-between; color: var(--muted); }
.slider output { color: var(--text); font-variant-numeric: tabular-nums; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.swatch {
  position: relative; aspect-ratio: 1; border-radius: 8px; cursor: pointer;
  border: 2px solid var(--line); background: var(--c, var(--panel-2));
  color: var(--text); font-family: inherit; font-size: 11px; font-weight: 600; line-height: 1; display: grid; place-items: center; padding: 0;
}
.swatch.blur, .swatch.img { grid-column: span 2; aspect-ratio: auto; height: 100%; min-height: 38px; }
.swatch.blur { background: linear-gradient(135deg, var(--line-strong), #6b7280); filter: saturate(1.2); }
.swatch.img { background: linear-gradient(135deg, #1e3a5f, #5b3a78); }
.swatch.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(79, 140, 255, .35); }
.swatch.picker { background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); overflow: hidden; }
.swatch.picker input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }

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

/* ------------------------------------------------------------ photo stage */
.dropzone {
  position: absolute; inset: 20px; border: 2px dashed var(--line-strong); border-radius: 16px;
  display: grid; place-items: center; text-align: center; transition: border-color .15s, background .15s;
}
.dropzone.over, body.dragging .dropzone { border-color: var(--accent); background: rgba(79, 140, 255, .06); }
.drop-inner { max-width: 440px; padding: 20px; }
.drop-inner h1 { margin: 10px 0 6px; font-size: 22px; }
.drop-inner p { color: var(--muted); margin: 6px 0 16px; }
.drop-inner .hint { font-size: 12px; margin-top: 16px; }
.drop-icon {
  margin: 0 auto; width: 56px; height: 56px; border-radius: 16px; display: grid; place-items: center;
  font-size: 24px; background: var(--panel-2); color: var(--accent);
}

.viewport { position: absolute; inset: 0; overflow: hidden; touch-action: none; }
#photo .viewport { background-color: var(--line-strong); background-image: none; }
.canvas-wrap {
  position: absolute; left: 0; top: 0; transform-origin: 0 0;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .45);
}
.canvas-wrap canvas { display: block; }
#outCanvas { position: relative; }
.canvas-wrap.checker-bg #outCanvas {
  background-color: #cfd3da;
  background-image:
    linear-gradient(45deg, #eef0f3 25%, transparent 25%, transparent 75%, #eef0f3 75%),
    linear-gradient(45deg, #eef0f3 25%, transparent 25%, transparent 75%, #eef0f3 75%);
  background-size: 24px 24px;
  background-position: 0 0, 12px 12px;
}
#origCanvas { position: absolute; inset: 0; }
.canvas-wrap.pixelated canvas { image-rendering: pixelated; }
.viewport[data-tool=hand] { cursor: grab; }
.viewport[data-tool=hand].panning { cursor: grabbing; }
.viewport[data-tool=erase], .viewport[data-tool=restore] { cursor: none; }
.brush-cursor {
  position: absolute; pointer-events: none; border-radius: 50%;
  border: 1.5px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .6), inset 0 0 0 1px rgba(0, 0, 0, .6);
  transform: translate(-50%, -50%);
}
.brush-cursor.restore { border-color: #7dffb0; }
.brush-cursor.erase { border-color: var(--danger-text); }

.toolbar {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 4px; padding: 6px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 12px;
  backdrop-filter: blur(8px); box-shadow: 0 8px 30px rgba(0, 0, 0, .4);
}
.tool {
  border: 0; background: transparent; color: var(--text); font: inherit; font-size: 16px;
  padding: 6px 10px; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 6px;
}
.tool span { font-size: 12px; font-weight: 600; }
.tool:hover { background: var(--panel-2); }
.tool.active { background: var(--accent); color: #fff; }
.tool:disabled { opacity: .4; }
.brush-size { width: 110px; padding: 0 6px; display: flex; }
.sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }

.busy {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: rgba(15, 17, 21, .6); backdrop-filter: blur(2px); z-index: 5;
}
.busy-text { font-weight: 600; text-align: center; max-width: 360px; }
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .15); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress { width: 260px; height: 6px; border-radius: 3px; background: rgba(255, 255, 255, .12); overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width .3s; }

.toast {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%); z-index: 6;
  background: var(--panel-2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .4); max-width: 80%;
}
.toast.error { border-color: var(--danger); color: var(--danger-text); }

/* ------------------------------------------------------------ AI select, scan, studio */
.tool.ai { background: linear-gradient(135deg, rgba(79, 140, 255, .18), rgba(139, 92, 246, .18)); }
.tool.ai.active { background: linear-gradient(135deg, #4f8cff, #8b5cf6); box-shadow: 0 0 16px rgba(139, 92, 246, .55); }
.tool-hint {
  position: absolute; left: 50%; bottom: 74px; transform: translateX(-50%); z-index: 3;
  font-size: 12px; color: #c9d4ff; background: var(--glass); border: 1px solid var(--line);
  padding: 5px 12px; border-radius: 999px; white-space: nowrap; pointer-events: none;
}
.tool-hint:empty, body:has(#toolbar[hidden]) .tool-hint, body:has(#busy:not([hidden])) .tool-hint { display: none; }
.tool-hint.working { color: #9fe3ff; border-color: rgba(120, 200, 255, .5); animation: gridFade 1.2s ease-in-out infinite; }
#selCanvas {
  position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none;
  animation: selPulse 1.3s ease-in-out infinite;
}
#selCanvas[data-mode=add] { filter: drop-shadow(0 0 6px rgba(0, 220, 255, .9)); }
#selCanvas[data-mode=remove] { filter: drop-shadow(0 0 6px rgba(255, 70, 120, .9)); }
@keyframes selPulse { 50% { opacity: .65; } }

.scan { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.scan::before {
  content: ""; position: absolute; inset: 0;
  background-image:
    linear-gradient(rgba(120, 200, 255, .10) 1px, transparent 1px),
    linear-gradient(90deg, rgba(120, 200, 255, .10) 1px, transparent 1px);
  background-size: 28px 28px;
  animation: gridFade 2s ease-in-out infinite;
}
.scan::after {
  content: ""; position: absolute; left: 0; right: 0; height: 18%; top: -18%;
  background: linear-gradient(to bottom, transparent, rgba(80, 190, 255, .10) 60%, rgba(140, 220, 255, .85) 98%, #fff);
  box-shadow: 0 6px 22px rgba(120, 210, 255, .8);
  animation: scanMove 1.8s cubic-bezier(.5, 0, .5, 1) infinite;
}
@keyframes scanMove { to { top: 100%; } }
@keyframes gridFade { 50% { opacity: .35; } }
.busy.soft { background: rgba(15, 17, 21, .12); backdrop-filter: none; justify-content: flex-end; padding-bottom: 110px; }
.busy.soft .spinner { display: none; }
.busy.soft .busy-text {
  background: rgba(15, 17, 21, .82); border: 1px solid rgba(120, 200, 255, .35); padding: 8px 16px;
  border-radius: 999px; color: #d6ecff; box-shadow: 0 0 24px rgba(79, 140, 255, .35);
}

.badge {
  margin-left: 6px; font-size: 10px; letter-spacing: .2px; text-transform: none; font-weight: 600;
  color: #c9b8ff; background: rgba(139, 92, 246, .16); border: 1px solid rgba(139, 92, 246, .35);
  padding: 1px 7px; border-radius: 999px; vertical-align: 1px;
}
.presets, .effects { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.effects { grid-template-columns: repeat(5, 1fr); }
.preset, .effect {
  display: flex; flex-direction: column; align-items: center; gap: 3px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); border-radius: 9px;
  padding: 7px 2px; font: inherit; font-size: 17px; line-height: 1;
}
.preset span, .effect span { font-size: 10.5px; font-weight: 600; color: var(--muted); }
.preset:hover, .effect:hover { border-color: var(--line-strong); }
.preset.active, .effect.active {
  border-color: #8b5cf6; background: linear-gradient(135deg, rgba(79, 140, 255, .2), rgba(139, 92, 246, .25));
  box-shadow: 0 0 14px rgba(139, 92, 246, .35);
}
.preset.active span, .effect.active span { color: var(--text); }
details { border: 1px solid var(--line); border-radius: 9px; padding: 0 10px; }
details[open] { padding-bottom: 10px; }
details > summary { cursor: pointer; padding: 8px 0; font-size: 13px; font-weight: 600; color: var(--text-2); }
details > .slider + .slider { margin-top: 8px; }
.group.disabled { opacity: .45; pointer-events: none; }
.gesture-list { margin: 0; padding-left: 2px; list-style: none; font-size: 12px; color: var(--muted); display: grid; gap: 4px; }
.gesture-list b { color: var(--text); font-weight: 600; }

/* ------------------------------------------------------------ live */
.live-view { display: block; }
#live .viewport { background-color: var(--line-strong); background-image: none; }
.live-stage { position: absolute; }
#liveCanvas, #handCanvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#handCanvas { pointer-events: none; }
.countdown {
  position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none;
  font: 800 min(28vh, 220px)/1 system-ui, sans-serif; color: #fff;
  text-shadow: 0 0 30px rgba(79, 140, 255, .9), 0 0 60px rgba(139, 92, 246, .8);
}
.countdown.tick { animation: tick .9s ease-out; }
@keyframes tick { from { transform: scale(1.6); opacity: 0; } 30% { opacity: 1; } to { transform: scale(.9); opacity: .2; } }
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.go { animation: flash .6s ease-out; }
@keyframes flash { from { opacity: .9; } to { opacity: 0; } }
.gesture-toast {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%); z-index: 4;
  background: rgba(15, 17, 21, .85); border: 1px solid rgba(120, 200, 255, .45); color: #e6f4ff;
  padding: 8px 18px; border-radius: 999px; font-weight: 600; box-shadow: 0 0 26px rgba(79, 140, 255, .45);
  animation: pop .25s ease-out;
}
@keyframes pop { from { transform: translateX(-50%) scale(.85); opacity: 0; } }
#gestureState { color: #9fe3ff; }
#liveCanvas.checker-bg {
  background-color: #cfd3da;
  background-image:
    linear-gradient(45deg, #eef0f3 25%, transparent 25%, transparent 75%, #eef0f3 75%),
    linear-gradient(45deg, #eef0f3 25%, transparent 25%, transparent 75%, #eef0f3 75%);
  background-size: 24px 24px;
  background-position: 0 0, 12px 12px;
}
.live-start { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; background: var(--bg); }
.hud {
  position: absolute; top: 12px; left: 12px; display: flex; gap: 10px;
  font: 600 12px/1 ui-monospace, Consolas, monospace;
}
.hud span { background: rgba(0, 0, 0, .6); padding: 6px 8px; border-radius: 6px; }
#rec { color: #ff6b6b; }

@media (max-width: 800px) {
  .workspace { flex-direction: column; }
  .panel { width: auto; border-left: 0; border-top: 1px solid var(--line); max-height: 45vh; }
  .topbar-note { display: none; }
}
