:root {
  --paper: #f6f1e7;
  --paper-2: #fbf7ef;
  --ink: #2b2622;
  --ink-soft: #5b5249;
  --muted: #8a8073;
  --line: #e0d6c4;
  --line-strong: #c9bda6;
  --accent: #b0863f;
  --accent-soft: #f0e2c6;
  --rose: #c9756a;
  --danger: #a8443a;
  --card: #fffdf8;
  --shadow-sm: 0 1px 2px rgba(43, 38, 34, 0.06), 0 2px 8px rgba(43, 38, 34, 0.05);
  --shadow-md: 0 8px 30px rgba(43, 38, 34, 0.12);
  --radius: 12px;
  --font: "Noto Sans TC", "PingFang TC", "Hiragino Sans CNS", "Microsoft JhengHei", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

/* hidden 屬性一律優先（否則會被 display:flex 之類的規則蓋掉） */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 20% -10%, #fffdf7 0%, rgba(255, 253, 247, 0) 60%),
    linear-gradient(180deg, var(--paper) 0%, #f1ead8 100%),
    var(--paper);
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3,
h4 {
  margin: 0;
  font-weight: 700;
}

button {
  font-family: inherit;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 100vh;
}

/* ---- 頂欄 ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 18px;
  background: rgba(255, 253, 248, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(6px);
  z-index: 5;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.seal {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 9px;
  background: linear-gradient(160deg, #c0584c, #8f3a30);
  color: #fff6ee;
  font-size: 20px;
  font-weight: 700;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.35), var(--shadow-sm);
  flex: none;
}

.brand-text {
  min-width: 0;
}

.brand-text h1 {
  font-size: 17px;
  letter-spacing: 0.04em;
  cursor: text;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-text h1:hover::after {
  content: " ✎";
  font-size: 12px;
  color: var(--muted);
}

.status {
  display: block;
  font-size: 11.5px;
  color: var(--muted);
  margin-top: 1px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* ---- 按鈕 ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 13px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink-soft);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.16s ease, background 0.16s ease, border-color 0.16s ease;
  white-space: nowrap;
}

.btn:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow-sm);
  background: #fff;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: linear-gradient(180deg, #bf9147, #a87b33);
  border-color: #9a7029;
  color: #fffaf1;
}

.btn-primary:hover {
  background: linear-gradient(180deg, #c99a4e, #b0833a);
  border-color: #8d6524;
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--ink-soft);
}

.btn-ghost:hover {
  background: rgba(43, 38, 34, 0.06);
  border-color: transparent;
  box-shadow: none;
}

.btn-danger {
  color: var(--danger);
  border-color: #e6cdc8;
}

.btn-danger:hover {
  background: #fdf3f1;
}

.btn-small {
  padding: 6px 10px;
  font-size: 12.5px;
}

.icon-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 7px;
  cursor: pointer;
}

.icon-btn:hover {
  background: rgba(43, 38, 34, 0.07);
  color: var(--ink);
}

/* ---- 版面 ---- */
.layout {
  display: grid;
  grid-template-columns: 268px minmax(0, 1fr) 330px;
  flex: 1;
  min-height: 0;
}

.sidebar,
.details {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  overflow-y: auto;
  background: rgba(252, 249, 242, 0.72);
}

.sidebar {
  border-right: 1px solid var(--line);
}

.details {
  border-left: 1px solid var(--line);
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 13px;
  box-shadow: var(--shadow-sm);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.panel-head h2 {
  font-size: 14px;
  letter-spacing: 0.05em;
}

.pill {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 2px 9px;
}

.panel-tips h2 {
  font-size: 13px;
  margin-bottom: 8px;
  color: var(--ink-soft);
}

.tips {
  margin: 0;
  padding-left: 18px;
  font-size: 12.5px;
  line-height: 1.75;
  color: var(--ink-soft);
}

.tips li + li {
  margin-top: 3px;
}

/* ---- 輸入 ---- */
.input {
  width: 100%;
  padding: 9px 11px;
  font-family: inherit;
  font-size: 13.5px;
  color: var(--ink);
  background: #fffefb;
  border: 1px solid var(--line);
  border-radius: 9px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(176, 134, 63, 0.15);
}

.input::placeholder {
  color: #b3a996;
}

textarea.input {
  resize: vertical;
  line-height: 1.6;
}

/* ---- 成員清單 ---- */
.member-list {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-height: 52vh;
  overflow-y: auto;
}

.member-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 9px;
  border-radius: 9px;
  cursor: pointer;
  border: 1px solid transparent;
}

.member-item:hover {
  background: #faf5ea;
  border-color: var(--line);
}

.member-item.is-selected {
  background: var(--accent-soft);
  border-color: var(--line-strong);
}

.member-rank {
  flex: none;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  font-size: 11.5px;
  font-weight: 700;
  color: var(--accent);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 50%;
}

.member-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.member-name {
  font-size: 13.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.member-name.is-placeholder {
  color: var(--muted);
  font-style: italic;
  font-weight: 500;
}

.member-sub {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.member-empty {
  padding: 14px 8px;
  font-size: 12.5px;
  color: var(--muted);
  text-align: center;
}

/* ---- 畫布區 ---- */
.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.stage-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 14px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.7);
  flex-wrap: wrap;
}

.legend {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 11.5px;
  color: var(--muted);
  flex-wrap: wrap;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  display: inline-block;
}

.dot-focus {
  background: #fdf3dd;
  border: 2px solid var(--accent);
}

.dot-placeholder {
  background: #fff;
  border: 1px dashed var(--muted);
}

.line-sample {
  display: inline-block;
  width: 20px;
  height: 0;
  border-top-width: 2px;
  border-top-style: solid;
}

.line-blood {
  border-color: var(--accent);
}

.line-marriage {
  border-color: var(--rose);
}

.stage-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--ink-soft);
  cursor: pointer;
  user-select: none;
}

.toggle input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
}

.canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

#graph {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

.zoom-controls {
  position: absolute;
  right: 14px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: rgba(255, 253, 248, 0.9);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 4px;
  box-shadow: var(--shadow-sm);
}

.zoom-controls .btn {
  padding: 4px 9px;
  font-size: 16px;
  line-height: 1.2;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  background: rgba(250, 246, 238, 0.86);
  backdrop-filter: blur(2px);
}

.empty-state h3 {
  font-size: 18px;
}

.empty-state p {
  margin: 0;
  color: var(--muted);
  font-size: 13.5px;
}

/* ---- 詳情 ---- */
.details-body {
  font-size: 13px;
}

.muted {
  color: var(--muted);
}

.facts {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}

.fact {
  font-size: 11.5px;
  color: var(--ink-soft);
  background: #f7f2e7;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 9px;
}

.fact b {
  color: var(--muted);
  font-weight: 600;
  margin-right: 4px;
}

.notice {
  margin: 0 0 10px;
  padding: 8px 10px;
  font-size: 12px;
  color: #7a5a1f;
  background: #fdf6e4;
  border: 1px dashed #e0c98f;
  border-radius: 9px;
}

.notes-block {
  margin: 0 0 14px;
  padding: 10px 11px;
  background: #fbf8f1;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.notes-block h4 {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.06em;
  margin-bottom: 7px;
}

.notes-count {
  font-size: 10.5px;
  background: #f0e9db;
  border-radius: 999px;
  padding: 0 6px;
}

.note-text {
  margin: 0 0 8px;
  padding-left: 9px;
  border-left: 3px solid var(--accent-soft);
  font-size: 12.5px;
  line-height: 1.7;
  color: var(--ink-soft);
  white-space: pre-wrap;
}

.note-empty {
  margin: 0 0 8px;
  font-size: 12px;
  line-height: 1.6;
  color: #a99f8d;
}

.note-input {
  margin-bottom: 8px;
}

.notes-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.details-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.edit-form {
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 11px;
  margin-bottom: 12px;
  background: #f9f5ec;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.rel-groups {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.rel-group h4 {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}

.rel-count {
  font-size: 10.5px;
  background: #f0e9db;
  border-radius: 999px;
  padding: 0 6px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 5px 10px;
  background: #fffefb;
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.14s ease, box-shadow 0.14s ease;
}

.chip:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.chip-label {
  font-size: 10.5px;
  color: var(--accent);
  font-weight: 600;
}

.chip-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
}

.chip-name.is-placeholder {
  color: var(--muted);
  font-style: italic;
  font-weight: 500;
}

/* ---- 對話框 ---- */
.dialog {
  border: none;
  padding: 0;
  width: min(560px, calc(100vw - 32px));
  border-radius: 16px;
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow-md);
}

.dialog::backdrop {
  background: rgba(43, 38, 34, 0.35);
  backdrop-filter: blur(2px);
}

.dialog-small {
  width: min(440px, calc(100vw - 32px));
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 18px 10px;
}

.dialog-head h2 {
  font-size: 16px;
  letter-spacing: 0.04em;
}

.dialog-body {
  padding: 4px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: min(62vh, 520px);
  overflow-y: auto;
}

.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 18px 16px;
  border-top: 1px solid var(--line);
  margin-top: 6px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field > span {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
}

.field small {
  font-size: 11.5px;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.relation-hint {
  font-size: 12.5px;
  line-height: 1.6;
  padding: 8px 11px;
  border-radius: 9px;
  background: #f8f3e8;
  border: 1px solid var(--line);
  color: var(--ink-soft);
}

.relation-hint b {
  color: var(--accent);
}

.match-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 10px;
  background: #fbf7ee;
  border: 1px dashed var(--line-strong);
  border-radius: 10px;
}

.match-title {
  margin: 0 0 2px;
  font-size: 11.5px;
  color: var(--muted);
}

.match-item {
  text-align: left;
  padding: 7px 10px;
  font-size: 13px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.match-item:hover {
  border-color: var(--accent);
}

.match-item b {
  color: var(--ink);
}

.more {
  font-size: 12.5px;
  color: var(--ink-soft);
  border-top: 1px dashed var(--line);
  padding-top: 8px;
}

.more summary {
  cursor: pointer;
  font-weight: 600;
  margin-bottom: 8px;
}

.more .field-row,
.more .field {
  margin-bottom: 9px;
}

.dialog-error,
.dialog-note {
  font-size: 12.5px;
  padding: 9px 11px;
  border-radius: 9px;
}

.dialog-error {
  color: #8f342a;
  background: #fdefec;
  border: 1px solid #eccdc6;
}

.dialog-note {
  color: #7a5a1f;
  background: #fdf6e4;
  border: 1px solid #e8d4a4;
}

.choice-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}

.choice-list .btn {
  justify-content: center;
}

/* ---- 匯出選單 ---- */
.menu {
  position: relative;
}

.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 300px;
  max-width: 340px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 11px;
  box-shadow: var(--shadow-md);
  padding: 6px;
  z-index: 20;
  display: flex;
  flex-direction: column;
}

.menu-title {
  margin: 4px 8px 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.menu-panel button {
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: left;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  color: var(--ink-soft);
  background: transparent;
  border: none;
  border-radius: 8px;
  cursor: pointer;
}

.menu-panel button b {
  font-size: 13px;
  color: var(--ink);
}

.menu-panel button span {
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--muted);
}

.menu-panel button:hover {
  background: #f7f2e7;
}

.menu-panel button:hover b {
  color: var(--accent);
}

/* ---- Toast ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  max-width: min(560px, calc(100vw - 40px));
  padding: 11px 16px;
  font-size: 13.5px;
  color: #fffaf1;
  background: rgba(43, 38, 34, 0.94);
  border-radius: 11px;
  box-shadow: var(--shadow-md);
  z-index: 50;
  animation: toast-in 0.18s ease;
}

.toast[data-tone='error'] {
  background: rgba(143, 52, 42, 0.95);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

/* ---- 響應式 ---- */
@media (max-width: 1180px) {
  .layout {
    grid-template-columns: 232px minmax(0, 1fr) 300px;
  }
}

@media (max-width: 960px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(320px, 1fr) auto;
  }

  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--line);
    flex-direction: row;
    align-items: flex-start;
    overflow-x: auto;
    max-height: 46vh;
  }

  .sidebar .panel {
    min-width: 250px;
  }

  .details {
    border-left: none;
    border-top: 1px solid var(--line);
  }

  .member-list {
    max-height: 30vh;
  }

  .field-row {
    grid-template-columns: 1fr;
  }
}

@media print {
  .topbar,
  .sidebar,
  .details,
  .stage-toolbar,
  .zoom-controls {
    display: none;
  }

  .layout {
    display: block;
  }

  .canvas-wrap {
    height: 100vh;
  }
}

/* ---- 執行期錯誤（方便回報問題） ---- */
.error-log {
  position: fixed;
  left: 12px;
  bottom: 12px;
  max-width: min(680px, calc(100vw - 24px));
  max-height: 40vh;
  overflow: auto;
  margin: 0;
  padding: 10px 12px;
  font-size: 11.5px;
  line-height: 1.5;
  color: #8f342a;
  background: #fdefec;
  border: 1px solid #eccdc6;
  border-radius: 10px;
  white-space: pre-wrap;
  z-index: 60;
}

/* ---- 另一位家長對話框的子女清單 ---- */
.check-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.check-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  font-size: 13px;
  background: #fffefb;
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
}

.check-item:hover {
  border-color: var(--line-strong);
}

.check-item input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
}

.check-item .check-hint {
  margin-left: auto;
  font-size: 11.5px;
  color: var(--muted);
}

.panel-head-right {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* Toast 裡的下載連結 */
.toast a {
  color: #ffe6a8;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.toast a:hover {
  color: #fff3cf;
}

/* ---- 登入畫面 ---- */
.login-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 20px;
  background: linear-gradient(180deg, #f8f4ea, #efe7d8);
}

.login-card {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 28px 26px 24px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  text-align: center;
}

.login-seal {
  width: 52px;
  height: 52px;
  margin: 0 auto;
  display: grid;
  place-items: center;
  font-size: 26px;
  font-weight: 700;
  color: #fff6ee;
  background: linear-gradient(160deg, #c0584c, #8f3a30);
  border-radius: 12px;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.35);
}

.login-card h2 {
  font-size: 19px;
  letter-spacing: 0.06em;
}

.login-card .field {
  text-align: left;
}

.login-card .btn {
  justify-content: center;
  padding: 10px;
  font-size: 14.5px;
}
