:root {
  --bg: #f5f2e9;
  --panel: #ffffff;
  --ink: #26221a;
  --muted: #7a7466;
  --line: #d8d2c2;
  --primary: #1f4a3d;
  --primary-ink: #ffffff;
  --accent: #a67c33;
  --male: #3a6ea5;
  --female: #b0575f;
  --male-bg: #eef4fb;
  --female-bg: #fdf0f1;
  --danger: #b03a2e;
  --row-a: rgba(31, 74, 61, 0.035);
  --shadow: 0 2px 8px rgba(40, 32, 12, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
  color: var(--ink);
  background: var(--bg);
}

.hidden { display: none !important; }

button {
  font: inherit;
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-ink);
}
button.primary:hover { filter: brightness(1.15); }
button.danger { color: var(--danger); border-color: var(--danger); }

input, select, textarea {
  font: inherit;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(31, 74, 61, 0.35);
  outline-offset: 0;
}

/* ---------- 로그인 ---------- */
.auth-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
}
.auth-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 36px 32px;
  width: min(400px, 92vw);
  text-align: center;
}
.auth-card h1 { font-size: 22px; margin: 0 0 6px; color: var(--primary); }
.auth-sub { color: var(--muted); font-size: 13px; margin: 0 0 24px; }
.auth-card input { margin-bottom: 10px; }
.auth-buttons { display: flex; gap: 8px; }
.auth-buttons button { flex: 1; }
.auth-message { font-size: 13px; color: var(--danger); min-height: 1em; margin: 12px 0 0; }
.auth-message.ok { color: var(--primary); }
.auth-help { font-size: 12.5px; color: var(--muted); margin: 14px 0 0; }

/* ---------- 헤더 ---------- */
#app { display: flex; flex-direction: column; height: 100vh; }

#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 18px;
  background: var(--primary);
  color: #fff;
  flex-wrap: wrap;
}
/* 좌우 영역이 같은 비율로 늘어나 검색창이 정중앙에 온다 */
.title-block { display: flex; align-items: center; gap: 10px; flex: 1 1 0; min-width: 0; }
#app-header .search-wrap { flex: 0 0 auto; }
#app-header .toolbar { flex: 1 1 0; justify-content: flex-end; }
#site-subtitle { align-self: center; }
.title-block button {
  padding: 5px 11px;
  font-size: 13px;
  background: rgba(255, 255, 255, 0.92);
  margin-left: 4px;
}
/* 종중 홈페이지 링크 — 버튼과 같은 모양으로 */
.header-link {
  padding: 5px 11px;
  font-size: 13px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
}
.header-link:hover { border-color: var(--accent); }
#site-title { font-size: 19px; margin: 0; font-weight: 700; }
#site-subtitle { font-size: 12px; opacity: 0.75; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar button { padding: 6px 12px; font-size: 14px; }
.toolbar button:not(.primary) { background: rgba(255,255,255,0.92); }
.toolbar .primary { background: var(--accent); border-color: var(--accent); }

.search-wrap { position: relative; }
#search-input { width: 190px; }
.search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  min-width: 240px;
  max-height: 300px;
  overflow-y: auto;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 50;
}
.search-results .item {
  padding: 8px 12px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.search-results .item:hover { background: var(--row-a); }
.search-results .item .gen { color: var(--muted); font-size: 12px; }
.search-results .empty { padding: 10px 12px; color: var(--muted); font-size: 13px; }

/* ---------- 배너 ---------- */
.banner {
  padding: 8px 18px;
  background: #fdf3d7;
  border-bottom: 1px solid #e8d59a;
  font-size: 13px;
  color: #6b5518;
}

/* ---------- 메인 ---------- */
main { flex: 1; display: flex; min-height: 0; }

#tree-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--bg);
}
/* touch-action: none — 모바일 브라우저가 터치를 스크롤로 가로채지 않고
   pointer 이벤트로 넘겨줘야 드래그 이동·핀치 확대가 동작한다 */
#tree-svg { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#tree-svg.dragging { cursor: grabbing; }

#tree-hint {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  padding: 7px 16px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  font-size: 13px;
  color: var(--muted);
  pointer-events: none;
  white-space: nowrap;
}
#tree-hint b { color: var(--primary); }

#empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--muted);
}

#zoom-controls {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#zoom-controls button {
  width: 36px; height: 36px;
  padding: 0;
  font-size: 17px;
  box-shadow: var(--shadow);
}

/* SVG 노드 */
.person-node { cursor: pointer; }
.person-node > rect {
  fill: #fff;
  stroke: var(--line);
  stroke-width: 1.2;
  rx: 8;
}
.person-node.male > rect { stroke: var(--male); fill: var(--male-bg); }
.person-node.female > rect { stroke: var(--female); fill: var(--female-bg); }
.person-node.selected > rect {
  stroke: var(--accent);
  stroke-width: 2.5;
  fill: #fdf8ec;
}
.person-node text { fill: var(--ink); text-anchor: middle; }
.person-node .p-name { font-size: 15px; font-weight: 700; }
.person-node .p-years { font-size: 10.5px; fill: var(--muted); }
.person-node.deceased .p-name { fill: #555; }

/* 로그인한 본인과 호칭 배지 */
.person-node.me > rect { stroke: var(--accent); stroke-width: 2; }
.kin-badge rect { fill: var(--primary); }
.kin-badge text { fill: #fff; font-size: 9.5px; font-weight: 700; }
.kin-badge.kin-me rect { fill: var(--accent); }
#me-chip { background: var(--accent) !important; border-color: var(--accent); color: #fff; }

/* 작고하신 분 표시 국화 */
.chrysanth .petal {
  fill: #fdfcfa;
  stroke: #b9b2a2;
  stroke-width: 0.5;
}
.chrysanth .petal-inner { fill: #f3efe6; }
.chrysanth .core { fill: var(--accent); }
.person-node.selected .chrysanth .petal { stroke: #a99c7d; }

/* 전화번호 등록 표시 휴대전화 */
.phone-icon .ph-body {
  fill: none;
  stroke: var(--primary);
  stroke-width: 1.7;
}
.phone-icon .ph-dot { fill: var(--primary); }

.edge { stroke: #9a927d; stroke-width: 1.4; fill: none; }
.edge.spouse { stroke-dasharray: 2 3; }

.gen-label .g-main { font-size: 13px; font-weight: 700; fill: var(--primary); }

/* ---------- 상세 팝업 ---------- */
#detail-dialog {
  width: min(380px, 92vw);
  padding: 20px 22px;
}
#detail-dialog h2 {
  margin: 0 0 2px;
  font-size: 21px;
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--ink);
}
#detail-dialog h2 .chrysanth { flex: none; }
#detail-dialog h2 .d-hanja {
  color: var(--muted);
  font-size: 15px;
  font-weight: 500;
}
#detail-dialog .detail-head { margin-bottom: 14px; }
#detail-dialog .d-photo {
  width: 100%;
  max-height: 260px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line);
  margin-bottom: 14px;
  display: block;
}
#detail-dialog dl {
  margin: 0;
  font-size: 14px;
  display: grid;
  grid-template-columns: 60px 1fr;
  gap: 7px 12px;
  align-items: baseline;
}
#detail-dialog dt { color: var(--muted); font-size: 12.5px; }
#detail-dialog dd { margin: 0; }
#detail-dialog .d-link {
  color: var(--male);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}
#detail-dialog .d-link:hover { color: var(--accent); }
#detail-dialog .d-age { color: var(--muted); font-size: 13px; }
/* 비고는 입력한 줄바꿈을 그대로 보여준다 */
#detail-dialog .d-note { white-space: pre-line; }
#detail-dialog .d-ganji { color: var(--accent); font-size: 13px; }
#detail-dialog .d-solar {
  display: block;
  color: var(--muted);
  font-size: 12.5px;
  margin-top: 1px;
}

#detail-dialog .d-grave-map {
  font-size: 12.5px;
  color: var(--male);
  text-underline-offset: 2px;
  white-space: nowrap;
  text-decoration: none;
}
#detail-dialog .d-grave-map { margin-left: 2px; }
#detail-dialog .d-grave-map img {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  vertical-align: -4px;
  border: 1px solid rgba(0, 0, 0, 0.08);
  background: #fff;
  object-fit: contain;
}
#detail-dialog .d-grave-map:hover img { filter: brightness(1.06); }

#detail-dialog .d-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
#detail-dialog .d-actions button { font-size: 13px; padding: 6px 11px; }
/* 삭제는 되돌릴 수 없으므로 다른 버튼과 떼어 오른쪽에 둔다 */
#detail-dialog .d-actions #d-delete { margin-left: auto; }
#detail-dialog #d-close {
  border: none;
  background: none;
  font-size: 16px;
  color: var(--muted);
  padding: 2px 6px;
  line-height: 1;
}
#detail-dialog #d-close:hover { color: var(--ink); }
#detail-body:focus { outline: none; }
.detail-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

/* ---------- 다이얼로그 ---------- */
dialog {
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 22px 24px;
  width: min(520px, 94vw);
  max-height: 90vh;
  overflow-y: auto;
}
dialog::backdrop { background: rgba(30, 26, 16, 0.45); }
dialog h2 { margin: 0 0 14px; font-size: 18px; color: var(--primary); }
dialog .dialog-sub { margin: -8px 0 12px; font-size: 12px; color: var(--muted); }
dialog menu {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 16px 0 0;
  padding: 0;
}

/* 항목 이름과 값을 같은 줄에 둔다 */
.form-grid {
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.form-grid label,
.form-grid .field {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
}
.form-grid .fl {
  flex: none;
  width: 66px;
  text-align: right;
}
.form-grid label > input,
.form-grid label > select,
.form-grid label > textarea,
.form-grid label > .date-field,
.form-grid label > .photo-cell {
  flex: 1;
  min-width: 0;
}
.form-grid .field > .radio-set { flex: none; }
.form-grid .field > .radio-set.right { margin-left: auto; }
.form-grid label.align-top { align-items: flex-start; }
.form-grid label.align-top .fl { padding-top: 8px; }
.form-grid .photo-cell { display: flex; flex-direction: column; gap: 5px; }
/* 라디오 입력 (성별) */
.radio-set { display: flex; gap: 16px; }
.form-grid .radio-option {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin: 0;
  font-size: 14px;
  color: var(--ink);
  cursor: pointer;
}
.form-grid .radio-option input[type="radio"] {
  width: auto;
  margin: 0;
  accent-color: var(--primary);
}

/* 날짜 입력 + 달력 버튼 */
.date-field {
  display: flex;
  gap: 6px;
  align-items: stretch;
  position: relative;
}
.date-field > input { flex: 1; min-width: 0; }
.date-btn {
  flex: none;
  width: 38px;
  padding: 0;
  font-size: 15px;
  line-height: 1;
}
.lunar-toggle {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
.lunar-toggle input[type="checkbox"] {
  width: auto;
  margin: 0;
  accent-color: var(--primary);
}
/* 달력 팝업의 기준 위치만 잡아두고 화면에는 보이지 않게 한다 */
.date-hidden {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 38px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

.pf-context {
  grid-column: 1 / -1;
  font-size: 13px;
  color: var(--muted);
  background: var(--row-a);
  border-radius: 6px;
  padding: 8px 12px;
}
.pf-context b { color: var(--ink); }
.form-error { color: var(--danger); font-size: 13px; min-height: 1em; margin: 8px 0 0; }
#pf-photo-current { font-size: 12px; color: var(--muted); }

/* 항렬표 다이얼로그 */
#hangnyeol-table-wrap { overflow-x: auto; }
#hangnyeol-table-wrap table { border-collapse: collapse; width: 100%; font-size: 14px; }
#hangnyeol-table-wrap th, #hangnyeol-table-wrap td {
  border: 1px solid var(--line);
  padding: 6px 10px;
  text-align: center;
}
#hangnyeol-table-wrap th { background: var(--row-a); color: var(--primary); }
#hangnyeol-table-wrap .cur { background: #fdf8ec; }

/* 모바일: 헤더를 한 줄로 압축 — 항렬표·종중 홈페이지·검색은 표시하지 않는다 */
@media (max-width: 720px) {
  #app-header { padding: 6px 10px; gap: 4px 6px; }
  #site-title { font-size: 15px; white-space: nowrap; }
  #site-subtitle,
  #btn-hangnyeol,
  .header-link,
  #btn-export,
  #app-header .search-wrap { display: none; }
  .title-block { flex: 0 1 auto; min-width: 0; }
  #app-header .toolbar { flex: 1 1 auto; gap: 4px; }
  .toolbar button { padding: 4px 8px; font-size: 12px; }
}
