/* ---------- tokens ---------- */
:root {
  --ui: 1;
  --bg: #f6f3ee;
  --bg-dot: #e4ded4;
  --surface: #ffffff;
  --surface-2: #f1ede6;
  --text: #2a2723;
  --muted: #7a736a;
  --line: #a59d92;
  --border: #e2dcd2;
  --accent: #c2602f;
  --accent-soft: #f7e3d6;
  --danger: #c0392b;
  --m: #3f78b5;  --m-bg: #eaf2fb;
  --f: #c4547a;  --f-bg: #fbecf1;
  --o: #7d7a74;  --o-bg: #f0eeea;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 14px rgb(0 0 0 / .07);
  --radius: 12px;
  color-scheme: light;
}
/* Dark colors: device is dark (unless the user forced light), or the user forced dark. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1d21; --bg-dot: #2a2d33;
    --surface: #26292f; --surface-2: #30343b;
    --text: #ece8e2; --muted: #a29b91; --line: #6f6a63; --border: #3a3e46;
    --accent: #e48452; --accent-soft: #4a3226; --danger: #ef6b5b;
    --m: #7fb0e6; --m-bg: #24323f;
    --f: #ea8fae; --f-bg: #3c2730;
    --o: #b0aca5; --o-bg: #33353a;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 4px 14px rgb(0 0 0 / .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1d21; --bg-dot: #2a2d33;
  --surface: #26292f; --surface-2: #30343b;
  --text: #ece8e2; --muted: #a29b91; --line: #6f6a63; --border: #3a3e46;
  --accent: #e48452; --accent-soft: #4a3226; --danger: #ef6b5b;
  --m: #7fb0e6; --m-bg: #24323f;
  --f: #ea8fae; --f-bg: #3c2730;
  --o: #b0aca5; --o-bg: #33353a;
  --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 4px 14px rgb(0 0 0 / .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: calc(15px * var(--ui)) / 1.4 system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Noto Sans JP", sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .45; }
.ic svg, .icon-btn svg, .couple svg {
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- stage ---------- */
#stage {
  position: fixed; inset: 0;
  touch-action: none;
  user-select: none; -webkit-user-select: none;
  background-image: radial-gradient(var(--bg-dot) 1px, transparent 1px);
  background-size: 22px 22px;
  overflow: hidden;
}
.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.world.anim { transition: transform .35s cubic-bezier(.2, .7, .2, 1); }
.lines { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.ln { fill: none; stroke: var(--line); stroke-width: 1.6; stroke-linejoin: round; }
.ln.marriage { stroke-width: 2.2; }
.ln.divorced { stroke-dasharray: 5 4; }
.ln.adopt { stroke-dasharray: 6 5; }

/* ---------- person card ---------- */
.card {
  --k: 1;
  position: absolute;
  display: flex; align-items: center; gap: calc(8px * var(--k));
  padding: calc(6px * var(--k)) calc(9px * var(--k));
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-left: calc(5px * var(--k)) solid var(--o);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: calc(13px * var(--k));
  line-height: 1.3;
  cursor: pointer;
  transition: box-shadow .15s, transform .15s;
}
.world .card { --k: inherit; }
.card.g-M { border-left-color: var(--m); }
.card.g-F { border-left-color: var(--f); }
.card.focus {
  border-color: var(--accent);
  border-width: 2.5px;
  box-shadow: 0 0 0 4px var(--accent-soft), var(--shadow);
}
.card.g-M.focus { border-left-color: var(--m); }
.card.g-F.focus { border-left-color: var(--f); }
.card.ghost { opacity: .6; border-style: dashed; box-shadow: none; }
.card.dead .ph { filter: grayscale(1); }
.card.dragging { opacity: .85; z-index: 10; box-shadow: 0 10px 30px rgb(0 0 0 / .25); pointer-events: none; transition: none; }
.card.drop { outline: 3px dashed var(--accent); outline-offset: 3px; }

.ph {
  flex: none;
  width: calc(40px * var(--k)); height: calc(40px * var(--k));
  border-radius: 50%; overflow: hidden;
  background: var(--o-bg); color: var(--o);
  display: grid; place-items: center;
}
.g-M .ph { background: var(--m-bg); color: var(--m); }
.g-F .ph { background: var(--f-bg); color: var(--f); }
.ph img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ph svg { width: 80%; height: 80%; fill: currentColor; margin-top: 15%; }

.tx { min-width: 0; flex: 1; }
.nm span { display: block; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub, .note { color: var(--muted); font-size: .82em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub .cross {            /* deceased mark: drawn, so it looks the same on every device */
  height: .8em; width: .48em; vertical-align: 0; margin: 0 .06em 0 .16em;   /* a touch taller than the digits */
  fill: currentColor;
}
.note { font-style: italic; }

/* vertical text */
.card.vert { flex-direction: column; align-items: center; padding: calc(8px * var(--k)) calc(4px * var(--k)); border-left-width: 1.5px; border-top: calc(5px * var(--k)) solid var(--o); }
.card.vert.g-M { border-top-color: var(--m); border-left-color: var(--border); }
.card.vert.g-F { border-top-color: var(--f); border-left-color: var(--border); }
.card.vert.focus { border-left-color: var(--accent); }
.card.vert .tx { writing-mode: vertical-rl; text-orientation: mixed; flex: 1; min-height: 0; overflow: hidden; }
.card.vert .nm span, .card.vert .sub, .card.vert .note { white-space: nowrap; }

/* hints for hidden relatives */
.more { position: absolute; width: 0; height: 0; border: 5px solid transparent; pointer-events: none; }
.more.up { top: -12px; left: 50%; margin-left: -5px; border-bottom: 6px solid var(--line); }
.more.down { bottom: -12px; left: 50%; margin-left: -5px; border-top: 6px solid var(--line); }
.more.side { right: -12px; top: 50%; margin-top: -5px; border-left: 6px solid var(--line); }
.ghost-ic { position: absolute; top: 2px; right: 6px; font-style: normal; color: var(--muted); font-size: 12px; }

/* couple icon */
.couple {
  position: absolute; padding: 2px;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--accent);
  display: grid; place-items: center;
  touch-action: none;
}
.couple svg { width: 100%; height: 100%; stroke-width: 2.2; }
.couple.div { color: var(--muted); border-style: dashed; }
.couple.dragging { z-index: 10; pointer-events: none; box-shadow: 0 6px 16px rgb(0 0 0 / .25); }
.couple.drop { outline: 3px dashed var(--accent); outline-offset: 2px; }

/* ---------- toolbars ---------- */
.bar {
  position: fixed; left: 0; right: 0;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
  pointer-events: none;
  z-index: 5;
}
.bar > * { pointer-events: auto; }
.bar.top { top: 0; padding-top: max(10px, env(safe-area-inset-top)); }
.bar.bottom { bottom: 0; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.grp { display: flex; align-items: center; gap: 6px; }
.grp.end { margin-left: auto; }
.title-wrap {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  max-width: 34vw; min-width: 0; margin-left: auto;
}
.title-pill {
  border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
  border-radius: 999px; padding: 6px 14px; font-weight: 600;
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* People count under the tree name: total · male · female · deceased */
.tree-stats {
  font-size: .78em; color: var(--muted); line-height: 1.3; text-align: center;
  background: var(--bg); border-radius: 8px; padding: 1px 8px;
}
.tree-stats:empty { display: none; }
.tree-stats b { font-weight: 600; }
.tree-stats .st-M { color: var(--m); }
.tree-stats .st-F { color: var(--f); }
.tree-stats .st-sep { opacity: .6; margin: 0 .3em; }
.tree-stats .cross { height: .85em; width: .51em; vertical-align: -.05em; margin-right: .2em; fill: currentColor; }
.icon-btn {
  width: calc(40px * var(--ui)); height: calc(40px * var(--ui));
  display: grid; place-items: center;
  border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); box-shadow: var(--shadow);
  padding: 0;
}
.icon-btn svg { width: 55%; height: 55%; }
.icon-btn.locked { background: var(--accent); color: #fff; border-color: var(--accent); }
.zoom { gap: 8px; }

.seg {
  display: inline-flex; background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px; box-shadow: var(--shadow);
}
.seg button {
  border: 0; background: none; border-radius: 999px;
  padding: 5px 10px; font-size: .85em; white-space: nowrap; color: var(--muted);
}
.seg button.on { background: var(--accent); color: #fff; font-weight: 600; }
.seg.small { box-shadow: none; background: var(--surface-2); }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 6px 11px; font-size: .85em; white-space: nowrap; box-shadow: var(--shadow);
}
.chip.side { color: var(--accent); font-weight: 600; }

@media (max-width: 620px) {
  /* Narrow screens: row 1 = generation controls, row 2 = tree name (left) + buttons (right). */
  #anc { flex-basis: 100%; pointer-events: none; }
  #anc > * { pointer-events: auto; }
  .title-wrap { order: 10; flex: 0 1 auto; max-width: none; margin: 0; align-items: flex-start; }
  .title-pill { font-size: .9em; padding: 4px 12px; }
  .tree-stats { text-align: left; }
  .bar.top .grp.end { order: 11; }
  .seg button { padding: 5px 8px; }
  .bar.bottom { align-items: flex-end; }
}
@media (max-width: 380px) {
  .seg button { padding: 4px 6px; font-size: .78em; }
  .chip { padding: 5px 8px; font-size: .78em; }
}

/* ---------- sheets ---------- */
#modals { position: relative; z-index: 20; }
.backdrop {
  position: fixed; inset: 0; background: rgb(0 0 0 / .35);
  display: flex; align-items: flex-end; justify-content: center;
  opacity: 0; transition: opacity .16s;
}
.backdrop.in { opacity: 1; }
.backdrop.out { opacity: 0; }
.sheet {
  width: 100%; max-width: 440px; max-height: 88vh;
  display: flex; flex-direction: column;
  background: var(--surface); color: var(--text);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -6px 30px rgb(0 0 0 / .2);
  transform: translateY(20px); transition: transform .16s;
  padding-bottom: env(safe-area-inset-bottom);
}
.backdrop.in .sheet { transform: none; }
.sheet.wide { max-width: 560px; }
@media (min-width: 700px) {
  .backdrop { align-items: center; }
  .sheet { border-radius: 18px; }
}
.sh-head { display: flex; align-items: center; gap: 8px; padding: 12px 12px 4px 18px; }
.sh-head h2 { flex: 1; margin: 0; font-size: 1.1em; }
.sh-head .icon-btn { box-shadow: none; width: 36px; height: 36px; border: 0; background: var(--surface-2); }
.sh-body { padding: 6px 18px 16px; overflow-y: auto; }
.sh-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 18px 16px; border-top: 1px solid var(--border); }
.sh-foot:has(> [hidden]:only-child) { display: none; }
.sheet.small .sh-head { padding-bottom: 0; }
.msg { margin: 4px 0 6px; font-size: 1.02em; }

.btn {
  border: 1px solid var(--border); background: var(--surface-2);
  border-radius: 10px; padding: 9px 16px; font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.small { padding: 6px 12px; font-size: .88em; }

.input {
  width: 100%; padding: 9px 11px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2);
}
.input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea.input { resize: vertical; }
.input:disabled { opacity: .8; }

/* menu */
.menu-head { display: flex; align-items: center; gap: 12px; margin: -4px 0 8px; font-size: 1.08em; }
.menu-sheet .sh-head h2 { display: none; }
.menu-sheet .sh-head { justify-content: flex-end; }
.menu { display: flex; flex-direction: column; }
.menu-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%; padding: 11px 6px; border: 0; background: none;
  border-bottom: 1px solid var(--border); text-align: left; color: inherit; text-decoration: none;
}
.menu-item:last-child { border-bottom: 0; }
.menu-item .ic { width: 22px; height: 22px; color: var(--accent); flex: none; }
.menu-item .ic svg { width: 100%; height: 100%; }
.mi-label { flex: 1; }
.menu-item small { color: var(--muted); font-size: .82em; }
.menu-item.danger, .menu-item.danger .ic { color: var(--danger); }
.menu-item.pair { cursor: default; flex-wrap: wrap; }
.pair-btns { display: flex; gap: 6px; }
.pair-btns .chip { box-shadow: none; background: var(--surface-2); }

/* avatar */
.avatar {
  flex: none; width: 42px; height: 42px; border-radius: 50%; overflow: hidden;
  display: grid; place-items: center; background: var(--o-bg); color: var(--o);
}
.avatar.g-M { background: var(--m-bg); color: var(--m); }
.avatar.g-F { background: var(--f-bg); color: var(--f); }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar svg { width: 80%; height: 80%; fill: currentColor; margin-top: 15%; }

/* edit form */
.form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > label { font-size: .85em; color: var(--muted); font-weight: 600; }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { color: var(--muted); font-size: .8em; }
.photo-edit { display: flex; align-items: center; gap: 14px; }
.photo-edit .avatar { width: 72px; height: 72px; }
.photo-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.date-row { display: grid; grid-template-columns: 1fr 1.3fr 1.4fr; gap: 6px; }
.date-row .y { order: 0; }
.date-row input::-webkit-inner-spin-button { display: none; }
.date-row input { -moz-appearance: textfield; }

.switch { position: relative; display: inline-block; width: 46px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span {
  position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: background .15s; cursor: pointer;
}
.switch span::after {
  content: ""; position: absolute; left: 3px; top: 3px; width: 20px; height: 20px;
  background: #fff; border-radius: 50%; transition: transform .15s; box-shadow: 0 1px 3px rgb(0 0 0 / .3);
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* choose list */
.search { margin-bottom: 8px; }
.choose-list { display: flex; flex-direction: column; gap: 4px; }
.choose-row {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 7px 8px; border: 1.5px solid transparent; border-radius: 10px; background: none; text-align: left;
}
.choose-row.on { border-color: var(--accent); background: var(--accent-soft); }
.choose-row .avatar { width: 36px; height: 36px; }
.cr-text { display: flex; flex-direction: column; min-width: 0; }
.cr-text small { color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 16px 0; }

/* data manager */
.dm-tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 10px; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 4px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); font-size: .82em;
}
.tool .ic { width: 22px; height: 22px; color: var(--accent); }
.tool .ic svg { width: 100%; height: 100%; }
.tool.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.tool.on .ic { color: #fff; }
.dm-hint { margin: 0 0 8px; color: var(--accent); font-weight: 600; }
.dm-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.dm-row {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 12px;
  border: 1.5px solid var(--border); border-radius: 12px; background: none; text-align: left;
}
.dm-row .ic { width: 22px; height: 22px; color: var(--muted); flex: none; }
.dm-row .ic svg { width: 100%; height: 100%; }
.dm-row.cur { border-color: var(--accent); }
.dm-row.on { background: var(--accent-soft); border-color: var(--danger); }
.dm-text { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.dm-text b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dm-text small { color: var(--muted); font-size: .8em; }
.badge { font-size: .75em; background: var(--accent); color: #fff; border-radius: 999px; padding: 2px 8px; flex: none; }
.check { width: 20px; height: 20px; border-radius: 6px; border: 2px solid var(--line); flex: none; }
.dm-row.on .check { background: var(--danger); border-color: var(--danger); box-shadow: inset 0 0 0 3px var(--surface); }

/* settings */
.settings h3 { font-size: .85em; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 4px; }
.set-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.set-row select.input { width: auto; min-width: 160px; }
.fine { color: var(--muted); font-size: .82em; margin: 8px 0; }

/* toast & drop */
#toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #2a2723; color: #fff; padding: 10px 16px; border-radius: 12px;
  max-width: min(90vw, 460px); text-align: center; font-size: .92em;
  transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#dropzone {
  position: fixed; inset: 12px; z-index: 40; border: 3px dashed var(--accent); border-radius: 20px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  display: grid; place-items: center; font-size: 1.3em; font-weight: 600; color: var(--accent); pointer-events: none;
}
#dropzone[hidden] { display: none; }

@media (prefers-reduced-motion: reduce) {
  .world.anim, .backdrop, .sheet, #toast { transition: none; }
}

/* ---------- server connection ---------- */
.net-badge {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 25;
  top: calc(max(10px, env(safe-area-inset-top)) + 72px);
  background: var(--danger); color: #fff; padding: 6px 14px; border-radius: 999px;
  font-size: .85em; font-weight: 600; box-shadow: var(--shadow); white-space: nowrap;
  max-width: calc(100vw - 32px); overflow: hidden; text-overflow: ellipsis;
}
@media (max-width: 620px) { .net-badge { top: calc(max(10px, env(safe-area-inset-top)) + 116px); } }
.fatal {
  position: fixed; inset: 0; z-index: 50; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  padding: 24px 16px; text-align: center;
}
.fatal p { max-width: 420px; margin: 0; color: var(--text); }

/* ---------- sign-in & users ---------- */
body.login-page { overflow: auto; display: flex; align-items: center; justify-content: center; min-height: 100%; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; box-shadow: var(--shadow); padding: 28px 22px 24px; text-align: center;
}
.login-logo { width: 56px; height: 56px; }
.login-card h1 { font-size: 1.3em; margin: 10px 0 4px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; text-align: left; }
.login-card .btn { width: 100%; padding: 11px; font-size: 1em; margin-top: 4px; }
.login-err { color: var(--danger); font-size: .88em; margin: 0; }
.muted { color: var(--muted); font-size: .9em; }

.role-chip { font-size: .78em; font-weight: 600; border-radius: 999px; padding: 3px 10px; flex: none; background: var(--surface-2); color: var(--muted); }
.role-chip.r-admin { background: var(--accent); color: #fff; }
.role-chip.r-editor { background: var(--m-bg); color: var(--m); }
.role-chip.r-viewer { background: var(--o-bg); color: var(--o); }

.user-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.user-row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 10px; background: var(--surface);
}
.user-name { flex: 1; min-width: 90px; display: flex; flex-direction: column; overflow: hidden; }
.user-name b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-name small { color: var(--muted); font-size: .8em; }
.icon-btn.sm { width: 34px; height: 34px; box-shadow: none; }
.icon-btn.sm.danger { color: var(--danger); }
.user-add { display: flex; flex-direction: column; gap: 10px; border-top: 1px solid var(--border); padding-top: 4px; }
.user-add .btn { align-self: flex-end; }
.icon-btn.view-only { background: var(--surface-2); color: var(--muted); border-color: var(--border); }
