:root {
  --bg: #0f1320;
  --panel: #1a2031;
  --panel2: #232b40;
  --text: #e8ecf5;
  --muted: #8d97ad;
  --accent: #4f8cff;
  --accent2: #28d39f;
  --line: #313a52;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo",
               "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

header { padding: 24px 20px 6px; text-align: center; }
header h1 { margin: 0; font-size: 28px; }
header p { margin: 6px 0 0; color: var(--muted); }

/* 상단 툴바 */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 12px 16px;
  background: rgba(15, 19, 32, 0.92);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}
.toolbar .slider {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--muted); font-size: 14px;
}
.toolbar input[type=range] { accent-color: var(--accent); }

.filebtn {
  display: inline-block;
  background: var(--panel2);
  border: 1px dashed var(--line);
  padding: 10px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
}
.filebtn:hover { border-color: var(--accent); }
.filebtn input { display: none; }

button {
  background: var(--accent); color: #fff; border: 0;
  padding: 10px 20px; border-radius: 10px;
  font-size: 15px; font-weight: 700; cursor: pointer;
}
button:disabled { background: #3a4258; opacity: .45; cursor: not-allowed; }
button:not(:disabled):hover { filter: brightness(1.1); }

.muted { color: var(--muted); font-size: 14px; }
.small { font-size: 12px; }
.error { color: #ff7b7b; }
.badge {
  background: var(--panel2); padding: 2px 8px; border-radius: 6px;
  font-variant-numeric: tabular-nums; min-width: 44px; text-align: center;
}

main { max-width: 1100px; margin: 0 auto; padding: 16px 16px 60px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  margin-top: 18px;
}
h2 { font-size: 17px; margin: 0 0 12px; }
h2 small { font-weight: 400; }

.empty {
  color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  height: 100%;
}
.empty.small { height: auto; padding: 18px; }

/* 1단: 유사도 지도 */
.map {
  position: relative;
  width: 100%;
  height: 560px;
  background: radial-gradient(circle at 50% 50%, #19233b 0%, #121726 70%);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  touch-action: none;
}
.map-stage {
  position: absolute; inset: 0;
  transform-origin: 0 0;
  will-change: transform;
}
.map svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.map .empty { position: absolute; inset: 0; pointer-events: none; }

.map-controls {
  position: absolute; top: 10px; right: 10px; z-index: 60;
  display: flex; flex-direction: column; gap: 6px;
}
.mapbtn {
  width: 34px; height: 34px; padding: 0;
  background: rgba(15, 19, 32, 0.78);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  font-size: 16px; font-weight: 700;
  backdrop-filter: blur(4px);
  cursor: pointer;
}
.mapbtn:hover { border-color: var(--accent); background: rgba(79, 140, 255, 0.18); }

.map:fullscreen { height: 100vh; border-radius: 0; border: 0; }
.map:-webkit-full-screen { height: 100vh; }

.node {
  position: absolute;
  transform: translate(-50%, -50%);
  text-align: center;
  cursor: grab;
  user-select: none;
  transition: filter .15s;
}
.node.dragging { cursor: grabbing; z-index: 50; filter: brightness(1.08); }
.node img {
  width: 84px; height: 84px; object-fit: cover;
  border-radius: 10px; border: 3px solid var(--accent);
  box-shadow: 0 4px 14px rgba(0,0,0,.45); display: block;
  -webkit-user-drag: none;
  user-drag: none;
  pointer-events: none;  /* 클릭/드래그는 부모(.node)가 받게 — 이미지 고스트 차단 */
}
.node .cap { margin-top: 5px; font-size: 12px; white-space: nowrap;
  text-shadow: 0 1px 3px rgba(0,0,0,.7); }
.node .cap small { color: var(--muted); }
.edge-label { fill: var(--muted); font-size: 11px; paint-order: stroke;
  stroke: rgba(15,19,32,.7); stroke-width: 3px; }
/* 엣지는 기본 숨김. 호버한 노드 중심으로만 노출됨(.show 클래스) */
#edges line, #edges text { opacity: 0; transition: opacity .15s; pointer-events: none; }
#edges line.show, #edges text.show { opacity: 1; }

/* 2·3단 공통: 가로 스트립 */
.strip {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 6px;
}
.card {
  flex: 0 0 auto;
  width: 168px;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-top: 3px solid var(--accent);
  border-radius: 10px;
  padding: 12px;
  transition: opacity .15s, filter .15s;
}
.card.off { opacity: .42; filter: grayscale(0.8); }

.tag {
  display: inline-block;
  margin-top: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  background: rgba(79, 140, 255, 0.16);
  color: #c9dbff;
  font-size: 12px;
}

.card-head { display: flex; gap: 10px; align-items: center; }
.card-title { font-weight: 700; font-size: 14px; }
.name-edit {
  display: inline-flex; align-items: center; gap: 4px;
}
.name-edit input {
  background: rgba(255,255,255,.06);
  border: 1px solid var(--line);
  color: var(--text);
  font-weight: 700; font-size: 14px;
  padding: 2px 6px; border-radius: 6px;
  width: 110px;
  font-family: inherit;
}
.name-edit input:focus { outline: 0; border-color: var(--accent); }
.name-pencil {
  cursor: pointer; color: var(--muted); font-size: 12px;
  opacity: .55; transition: opacity .12s;
}
.card:hover .name-pencil { opacity: 1; }
.avatar {
  width: 48px; height: 48px; object-fit: cover;
  border-radius: 8px; border: 1px solid var(--line);
}
.avatar.lg { width: 100%; height: 110px; }
.avatar.heic {
  display: flex; align-items: center; justify-content: center;
  background: #2c344c; color: var(--muted); font-weight: 700; font-size: 13px;
}

.mini-row { display: flex; gap: 5px; flex-wrap: wrap; margin: 10px 0; }
.mini {
  width: 34px; height: 34px; object-fit: cover;
  border-radius: 6px; border: 1px solid var(--line);
  cursor: pointer; transition: transform .1s, box-shadow .1s, border-color .1s;
}
.mini:hover { border-color: var(--accent); transform: scale(1.06); }
.mini.picked {
  border-color: #ff9f43;
  box-shadow: 0 0 0 2px rgba(255, 159, 67, 0.45);
  transform: scale(1.06);
}

/* 합치기 바(섹션 헤더 아래) */
.merge-bar {
  display: flex; align-items: center; gap: 8px;
  margin: 0 0 12px;
  padding: 8px 12px;
  background: rgba(40, 211, 159, 0.10);
  border: 1px solid rgba(40, 211, 159, 0.38);
  border-radius: 10px;
}
.merge-bar .merge-count { font-size: 13px; color: #a7f3d0; flex: 1; font-weight: 600; }
.merge-bar #mergeBtn {
  background: var(--accent2); color: #04241a;
  padding: 6px 14px; border-radius: 8px;
  font-size: 13px; font-weight: 700;
}
.merge-bar #mergeBtn:disabled { background: #4a695e; opacity: .6; }
.merge-bar #mergeClear {
  background: transparent; border: 1px solid rgba(40, 211, 159, 0.45);
  color: #a7f3d0;
  width: 26px; height: 26px; padding: 0; border-radius: 6px;
  font-size: 12px; line-height: 1;
}
.merge-bar #mergeClear:hover { background: rgba(40, 211, 159, 0.2); }

/* 인물 카드의 합치기 선택 박스(좌상단) */
.merge-pick {
  position: absolute; top: 6px; left: 6px; z-index: 3;
  width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.45);
  border-radius: 6px;
  cursor: pointer;
  transition: background .15s;
}
.merge-pick:hover { background: rgba(40, 211, 159, 0.3); }
.merge-pick input {
  width: 16px; height: 16px; margin: 0;
  accent-color: var(--accent2);
  cursor: pointer;
}
.card.merge-picked {
  box-shadow: 0 0 0 2px var(--accent2), 0 4px 14px rgba(40, 211, 159, .25);
}
.card.merge-picked .merge-pick { background: var(--accent2); }

/* 분리 바 */
.split-bar {
  display: flex; align-items: center; gap: 6px;
  margin: 4px 0 8px;
  padding: 6px 8px;
  background: rgba(255, 159, 67, 0.10);
  border: 1px solid rgba(255, 159, 67, 0.35);
  border-radius: 8px;
}
.split-bar .split-count { font-size: 12px; color: #ffce8a; flex: 1; }
.split-bar .split-btn {
  background: #ff9f43; color: #1a1300;
  padding: 4px 10px; border-radius: 6px;
  font-size: 12px; font-weight: 700;
}
.split-bar .split-btn:disabled { background: #6a594a; color: #aaa; opacity: .6; }
.split-bar .split-clear {
  background: transparent; border: 1px solid rgba(255, 159, 67, 0.45);
  color: #ffce8a;
  width: 24px; height: 24px; padding: 0; border-radius: 6px;
  font-size: 12px; line-height: 1;
}
.split-bar .split-clear:hover { background: rgba(255, 159, 67, 0.2); }

/* 토글 스위치 */
.switch {
  display: flex; align-items: center; gap: 8px;
  margin-top: 6px; font-size: 13px; color: var(--muted); cursor: pointer;
}
.switch input {
  appearance: none; width: 38px; height: 22px; border-radius: 12px;
  background: #44506e; position: relative; cursor: pointer; transition: background .15s;
  flex: 0 0 auto;
}
.switch input:checked { background: var(--accent2); }
.switch input::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: left .15s;
}
.switch input:checked::after { left: 18px; }

.card { position: relative; }
.card.upload { text-align: center; }
.card.upload .card-title { margin-top: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card.upload .switch { justify-content: center; }
.card.upload .switch.target {
  margin-top: 10px; padding: 6px 8px;
  background: rgba(40, 211, 159, 0.08);
  border: 1px solid rgba(40, 211, 159, 0.28);
  border-radius: 8px;
  color: var(--text); font-weight: 600;
}
.card.upload.off .switch.target {
  background: rgba(141, 151, 173, 0.08);
  border-color: rgba(141, 151, 173, 0.28);
  color: var(--muted);
}
.rm {
  position: absolute; top: 6px; right: 6px;
  width: 24px; height: 24px; padding: 0; border-radius: 50%;
  background: rgba(0,0,0,.45); font-size: 12px; line-height: 1;
}
.rm:hover { background: #ee5253; }

/* 인물 카드의 토글+삭제 줄 */
.card-foot { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.card-foot .switch { margin-top: 0; flex: 1; }
.card-foot .rm-inline {
  position: static;
  flex: 0 0 auto;
}

footer { text-align: center; padding: 18px; }

/* 드래그 앤 드롭 오버레이 */
.drop-overlay {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(15, 19, 32, 0.82);
  backdrop-filter: blur(3px);
}
.drop-overlay[hidden] { display: none; }
.drop-box {
  pointer-events: none;
  padding: 48px 64px;
  border: 3px dashed var(--accent);
  border-radius: 18px;
  font-size: 22px; font-weight: 800; color: var(--text);
  background: rgba(79, 140, 255, 0.08);
}
