:root {
  --bg: #f6f2ee;
  --panel: #ffffff;
  --ink: #2b2622;
  --muted: #8a817a;
  --line: #e8e1da;
  --accent: #d2557a;
  --accent-ink: #ffffff;
  --stage-bg: #f7e4dc;
  --handle: 22px;
  --radius: 14px;
}

@media (pointer: coarse) { :root { --handle: 30px; } }

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- header ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
}
.topbar h1 { margin: 0; font-size: 20px; font-weight: 650; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.actions { display: flex; gap: 8px; align-items: center; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover:not(:disabled) { border-color: #cfc5bb; }
.btn:disabled { opacity: .35; cursor: default; }
.btn.icon { width: 38px; padding: 0; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger:hover:not(:disabled) { color: #c0392b; border-color: #e7b3ab; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn svg .fill { fill: currentColor; }

/* ---------- layout ---------- */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  padding: 0 16px 16px;
}

.stage-col { display: flex; flex-direction: column; gap: 12px; min-height: 0; }

.stage-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  container-type: size;
}

#stage {
  --ratio: 0.75;
  position: relative;
  width: min(100cqw, calc(100cqh * var(--ratio)));
  aspect-ratio: var(--ratio);
  background: var(--stage-bg);
  border-radius: var(--radius);
  overflow: hidden;
  touch-action: none;
  box-shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 30px rgba(60,30,20,.08);
}
#stage.drop-target { outline: 3px dashed var(--accent); outline-offset: -3px; }

#base, #items img {
  position: absolute;
  pointer-events: none;
  -webkit-user-drag: none;
}
#base { inset: 0; width: 100%; height: 100%; object-fit: contain; }
#items { position: absolute; inset: 0; }
#items img { transform-origin: 50% 50%; will-change: transform; }

/* ---------- selection ---------- */
#selection {
  position: absolute;
  border: 1.5px solid var(--accent);
  border-radius: 4px;
  pointer-events: none;
  transform-origin: 50% 50%;
}
#selection[hidden] { display: none; }
.handle {
  position: absolute;
  width: var(--handle);
  height: var(--handle);
  margin: calc(var(--handle) / -2);
  padding: 0;
  border: 2px solid var(--accent);
  border-radius: 50%;
  background: #fff;
  pointer-events: auto;
  touch-action: none;
  cursor: nwse-resize;
}
.handle.nw { left: 0; top: 0; }
.handle.ne { left: 100%; top: 0; cursor: nesw-resize; }
.handle.sw { left: 0; top: 100%; cursor: nesw-resize; }
.handle.se { left: 100%; top: 100%; }
.handle.rot {
  left: 50%;
  top: calc(var(--handle) * -1.6);
  background: var(--accent);
  cursor: grab;
}
.rot-stem {
  position: absolute;
  left: 50%;
  top: calc(var(--handle) * -1.6);
  height: calc(var(--handle) * 1.6);
  border-left: 1.5px solid var(--accent);
}

/* ---------- item toolbar ---------- */
.toolbar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.toolbar .sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }

/* ---------- panel ---------- */
.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.tabs {
  display: flex;
  gap: 2px;
  padding: 8px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.tab {
  flex: 0 0 auto;
  padding: 7px 6px;
  font-size: 14px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.tab[aria-selected="true"] { background: #f7e4dc; color: var(--ink); }

.tray {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 10px;
  padding: 12px;
  align-content: start;
}
.thumb {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fbf8f5;
  cursor: grab;
  touch-action: pan-y;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color .15s, transform .15s;
}
.thumb:hover { border-color: var(--accent); }
.thumb:active { cursor: grabbing; }
.thumb img { max-width: 100%; max-height: 100%; pointer-events: none; -webkit-user-drag: none; }
.thumb .label {
  position: absolute;
  inset: auto 0 4px;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
}
.thumb:has(.label) { padding-bottom: 20px; }

.hint { margin: 0; padding: 8px 12px 12px; font-size: 12px; color: var(--muted); text-align: center; }

.ghost {
  position: fixed;
  left: 0;
  top: 0;
  pointer-events: none;
  z-index: 50;
  opacity: .85;
  filter: drop-shadow(0 6px 12px rgba(0,0,0,.18));
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--ink);
  color: #fff;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  max-width: calc(100vw - 32px);
  z-index: 60;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- phones / portrait tablets ---------- */
@media (max-width: 820px) {
  .topbar { padding: 10px 12px; }
  .topbar h1 { font-size: 17px; }
  .btn { height: 36px; padding: 0 10px; }
  .btn.icon { width: 36px; }
  #save span { display: none; }
  #save { width: 36px; padding: 0; }

  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: 10px;
    padding: 0 12px 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  .stage-col { gap: 8px; }
  .toolbar { gap: 4px; }
  .toolbar .sep { margin: 0 2px; }

  .tray {
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 10px;
    gap: 8px;
  }
  .thumb { flex: 0 0 78px; height: 78px; touch-action: pan-x; }
  .thumb .label { display: none; }
  .thumb:has(.label) { padding-bottom: 10px; }
  .hint { display: none; }
  .tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
}
