/* ==========================================================
   계약서 템플릿 편집기 화면 — template-edit.html
   · 기본 화면만(디자인은 나중): 시스템 글꼴 · 회색 선 · 파랑 버튼
   · 칸 색: 서명자 1(대표님) = 회색, 서명자 2(사장님) = 파랑 · 날짜 칸 안에는 형식별 보기 글자
   · 필수 칸 = 실선 + 위에 '필수' 표 · 선택 칸 = 점선 + '선택' 표 · 보기 글자는 칸의 정렬대로(가로 · 세로)
   · 고른 칸 = 그림자(여러 개 고를 수 있음) · 모서리 손잡이는 하나만 골랐을 때(.solo)만
   · 몸통은 화면 높이에 맞추고, 가운데 쪽 그림과 오른쪽 칸 · 단계 · 기본 서류가 각자 스크롤
   · 파일을 끌고 들어오면: 쪽이 없을 땐 빈 칸(#empty), 쪽이 있을 땐 쪽 영역(#pages)에 drop-on
   ========================================================== */

:root {
  --ink: #171717;
  --sub: #5c636d;
  --muted: #8a9099;
  --line: #e2e5e9;
  --line-strong: #c8cdd4;
  --bg: #f3f4f6;
  --white: #ffffff;
  --blue: #3d7efe;
  --blue-ink: #1c5ad6;
  --blue-soft: #eaf1ff;
  --gray-field: #7b828c;
  --gray-ink: #4a5059;
  --gray-soft: #eef0f2;
  --danger: #d71900;
  --ok: #1d7a46;
  --radius: 8px;
}

* { box-sizing: border-box; }

html,
body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  font-family: system-ui, -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
}

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

[hidden] { display: none !important; }

/* 키보드로 옮겨 다닐 때 보이는 테두리 (브라우저 기본 검정 대신 파랑) */
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

.muted { color: var(--muted); font-size: 13px; }

/* ---------- 버튼 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--white);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--sub); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.primary { background: var(--blue); border-color: var(--blue); color: var(--white); font-weight: 600; }
.btn.primary:hover { background: var(--blue-ink); border-color: var(--blue-ink); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }

/* ---------- 맨 위 막대 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--white);
  border-bottom: 1px solid var(--line);
}
.back { color: var(--sub); text-decoration: none; white-space: nowrap; }
.back::before { content: "\2190\00a0"; }
.back:hover { color: var(--ink); }
.name-input {
  width: min(420px, 40vw);
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
}
.name-input:focus,
.fld input:focus,
.fld select:focus,
.fld textarea:focus,
.stage-name:focus { outline: 2px solid var(--blue-soft); border-color: var(--blue); }
.version { color: var(--sub); font-size: 13px; white-space: nowrap; }
.grow { flex: 1; }

/* ---------- 잠긴 버전 안내 · 바꾼 PDF 안내 (같은 모양 한 줄) ---------- */
.locked {
  margin: 0;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--gray-soft);
  color: var(--gray-ink);
  font-size: 13px;
}

/* ---------- 위 도구 (알림 글이 바뀌어도 높이가 그대로 — 쪽 그림이 밀리지 않게 한 줄) ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--white);
  border-bottom: 1px solid var(--line);
}
.seg { flex: none; }
.message {
  flex: 1;
  min-width: 0;
  margin: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--muted);
  font-size: 13px;
}
.message.busy { color: var(--sub); }
.message.error { color: var(--danger); font-weight: 600; }
.message.ok { color: var(--ok); font-weight: 600; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius); overflow: hidden; }
.seg button {
  height: 34px;
  padding: 0 12px;
  border: 0;
  border-left: 1px solid var(--line);
  background: var(--white);
  cursor: pointer;
}
.seg button:first-child { border-left: 0; }
.seg button:disabled { color: var(--muted); cursor: default; }
.tool.on { background: var(--blue-soft); color: var(--blue-ink); font-weight: 600; }
.signer.s1.on { background: var(--gray-soft); color: var(--gray-ink); font-weight: 600; box-shadow: inset 0 -2px 0 var(--gray-field); }
.signer.s2.on { background: var(--blue-soft); color: var(--blue-ink); font-weight: 600; box-shadow: inset 0 -2px 0 var(--blue); }

/* ---------- 작업 영역 ----------
   칸 자리를 정해 둔다: 쪽(#pages)과 끌어다 놓기 덮개(.drop-cover)는 같은 왼쪽 칸에 겹치고, 오른쪽은 .side */
.work {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  grid-template-rows: minmax(0, 1fr);
}

/* 가운데: 쪽 그림 */
.pages {
  grid-column: 1;
  grid-row: 1;
  overflow: auto;
  padding: 20px 24px 40px;
  user-select: none;
}
.empty {
  max-width: 420px;
  margin: 12vh auto 0;
  padding: 36px 24px;
  text-align: center;
  background: var(--white);
  border: 1px dashed var(--line-strong);
  border-radius: 12px;
}
.empty-title { margin: 0 0 16px; font-size: 16px; font-weight: 600; }
.empty-sub { margin: 14px 0 0; color: var(--muted); font-size: 13px; }
.empty.drop-on { border-color: var(--blue); background: var(--blue-soft); }

/* 쪽이 있을 때 파일을 끌고 들어오면 쪽 영역 위에 덮개 (누르기 · 끌기는 아래 #pages 가 그대로 받음) */
.drop-cover {
  grid-column: 1;
  grid-row: 1;
  z-index: 4;
  display: none;
  align-items: center;
  justify-content: center;
  margin: 10px;
  border: 2px dashed var(--blue);
  border-radius: 12px;
  background: rgba(234, 241, 255, 0.88);
  color: var(--blue-ink);
  font-size: 16px;
  font-weight: 600;
  pointer-events: none;
}
.pages.drop-on + .drop-cover { display: flex; }

.sheet { width: min(100%, 880px); margin: 0 auto 28px; }
.sheet-head { margin: 0 0 6px; color: var(--sub); font-size: 12px; }
.page {
  position: relative;
  width: 100%;
  background: var(--white);
  box-shadow: 0 0 0 1px var(--line), 0 2px 8px rgba(23, 23, 23, 0.06);
}
.page-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.layer { position: absolute; inset: 0; touch-action: none; }
body.placing .layer { cursor: crosshair; }

/* 칸 (위치 · 크기는 editor.js 가 % 로 넣는다) */
.field {
  position: absolute;
  border: 1.5px solid;
  border-radius: 2px;
  cursor: move;
  touch-action: none;
}
.field.s1 { border-color: var(--gray-field); background: rgba(123, 130, 140, 0.16); color: var(--gray-ink); }
.field.s2 { border-color: var(--blue); background: rgba(61, 126, 254, 0.13); color: var(--blue-ink); }
.field.opt { border-style: dashed; }   /* 선택 칸(필수 아님) — 필수 칸은 실선 */
.field.selected { z-index: 2; }
.field.s1.selected { box-shadow: 0 0 0 3px rgba(123, 130, 140, 0.35); }
.field.s2.selected { box-shadow: 0 0 0 3px rgba(61, 126, 254, 0.3); }
.field.ghost { border-style: dashed; pointer-events: none; }

/* 필수 · 선택 표 — 칸 위 왼쪽에 붙는 작은 이름표(누르기는 아래 칸이 받음) */
.field-tag {
  position: absolute;
  left: -1.5px;
  bottom: 100%;
  padding: 0 4px;
  border: 1px solid;
  border-bottom: 0;
  border-radius: 3px 3px 0 0;
  font-size: 10px;
  line-height: 13px;
  white-space: nowrap;
  pointer-events: none;
}
.field.s1 .field-tag { border-color: var(--gray-field); background: var(--gray-field); color: var(--white); }
.field.s2 .field-tag { border-color: var(--blue); background: var(--blue); color: var(--white); }
.field.opt .field-tag { border-style: dashed; background: var(--white); }
.field.s1.opt .field-tag { color: var(--gray-ink); }
.field.s2.opt .field-tag { color: var(--blue-ink); }
.field.ghost .field-tag { display: none; }
.field-label {
  position: absolute;
  left: 4px;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 11px;
  line-height: 1.2;
  pointer-events: none;
}
.field.t-check .field-label { display: none; }
/* 글자 정렬: 보기 글자를 칸의 정렬대로 (고객 화면 · 완성 PDF 와 같게 — 년 · 월 · 일 날짜 칸은 기본 가운데) */
.field.f-center .field-label { text-align: center; }
.field.f-right .field-label { text-align: right; }
.field.v-top .field-label { top: 2px; transform: none; }
.field.v-bottom .field-label { top: auto; bottom: 2px; transform: none; }

/* 모서리 손잡이 (하나만 골랐을 때만 보임 — 여러 개 골랐을 때는 옮기기 · 정렬만) */
.handle {
  position: absolute;
  display: none;
  width: 10px;
  height: 10px;
  background: var(--white);
  border: 1.5px solid currentColor;
  border-radius: 2px;
}
.field.solo .handle { display: block; }
.handle.nw { left: -6px; top: -6px; cursor: nwse-resize; }
.handle.ne { right: -6px; top: -6px; cursor: nesw-resize; }
.handle.sw { left: -6px; bottom: -6px; cursor: nesw-resize; }
.handle.se { right: -6px; bottom: -6px; cursor: nwse-resize; }

/* ---------- 오른쪽 ---------- */
.side {
  grid-column: 2;
  grid-row: 1;
  overflow: auto;
  background: var(--white);
  border-left: 1px solid var(--line);
}
.tabs {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  background: var(--white);
  border-bottom: 1px solid var(--line);
}
.tab {
  flex: 1;
  height: 42px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--sub);
  cursor: pointer;
}
.tab.on { color: var(--ink); font-weight: 600; border-bottom-color: var(--blue); }
.panel { padding: 16px; }
.panel > .muted:first-child { margin-top: 0; }
.panel-foot { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.sub { margin: 22px 0 8px; font-size: 14px; }

/* 입력 칸 묶음 */
.fld { display: flex; flex-direction: column; gap: 4px; margin: 0 0 12px; font-size: 13px; color: var(--sub); }
.fld input,
.fld select,
.fld textarea {
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--white);
  color: var(--ink);
  font-size: 14px;
}
.fld textarea { resize: vertical; line-height: 1.5; }
.fld.small { margin-bottom: 8px; }
.chk { display: flex; align-items: center; gap: 8px; margin: 0 0 12px; cursor: pointer; }
.chk input { width: 16px; height: 16px; margin: 0; }
.chk.small { margin: 8px 0 4px; font-size: 13px; color: var(--sub); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* 고른 칸의 속성 */
.props { padding: 14px; border: 1px solid var(--line); border-radius: 10px; background: #fafbfc; }
.props-head { margin: 0 0 12px; font-weight: 600; }

/* 여러 개 골랐을 때 정렬 도구 (가로 셋 · 세로 셋) */
.fld-title { margin: 0 0 6px; color: var(--sub); font-size: 13px; }
.align-tools { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 0 0 12px; }
.align-tools .btn { height: 34px; padding: 0 6px; font-size: 13px; }

/* 칸 목록 */
.field-list { list-style: none; margin: 0; padding: 0; }
.field-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: none;
  text-align: left;
  cursor: pointer;
}
.field-item:hover { background: var(--bg); }
.field-item.on { border-color: var(--blue); background: var(--blue-soft); }
.item-name { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.item-meta { color: var(--muted); font-size: 12px; white-space: nowrap; }
.list-empty { padding: 8px 2px; color: var(--muted); font-size: 13px; }

/* 서명자 표시 (글자가 들어간 작은 표 — 색 점 아님) */
.badge {
  flex: none;
  padding: 1px 6px;
  border: 1px solid;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.5;
}
.badge.s1 { border-color: var(--gray-field); color: var(--gray-ink); background: var(--gray-soft); }
.badge.s2 { border-color: var(--blue); color: var(--blue-ink); background: var(--blue-soft); }

/* 단계 */
.stage-list { list-style: none; margin: 0; padding: 0; }
.stage { padding: 10px; margin: 0 0 8px; border: 1px solid var(--line); border-radius: 10px; }
.stage.is-auto { background: #fafbfc; }
.stage-row { display: flex; align-items: center; gap: 6px; }
.stage-no { width: 18px; color: var(--muted); font-size: 12px; text-align: right; }
.stage-name {
  flex: 1;
  min-width: 0;
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
}
.tag {
  flex: none;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--gray-soft);
  color: var(--gray-ink);
  font-size: 12px;
}
.icon,
.text-btn {
  flex: none;
  height: 30px;
  min-width: 30px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--white);
  cursor: pointer;
}
.icon:disabled { color: var(--line-strong); cursor: default; }
.text-btn { font-size: 12px; color: var(--danger); }
.stage-note { margin: 6px 0 0 24px; color: var(--muted); font-size: 12px; }
.stage .chk.small,
.stage .checklist { margin-left: 24px; }
.checklist { margin-top: 4px; }
.checklist summary { color: var(--sub); font-size: 13px; cursor: pointer; padding: 2px 0; }
.checklist[open] summary { margin-bottom: 8px; }

/* 기본 서류 — 서류 템플릿 체크 목록 (서류명 · 작게 서류 설명 · 작게 받기 · 장수) */
.pick-list { list-style: none; margin: 12px 0 0; padding: 0; }
.pick {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 0 0 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}
.pick:hover { background: #fafbfc; }
.pick input { flex: none; width: 16px; height: 16px; margin: 2px 0 0; }
.pick-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.pick-name { font-weight: 600; overflow-wrap: anywhere; }
.pick-note,
.pick-meta { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.no-docs { margin-top: 12px; }
.no-docs .list-empty { margin: 0 0 8px; padding: 0; }
.link { color: var(--blue-ink); }
.error-line { margin: 12px 0 0; color: var(--danger); font-size: 13px; font-weight: 600; }
