:root {
  --bg: #f6f1e8;
  --surface: #fffdf8;
  --surface-2: #f1eadf;
  --fg: #271e17;
  --muted: #7b6c5e;
  --line: #e2d7c6;
  --line-strong: #cbbba5;
  --accent: #9a3324;
  --accent-soft: #f4e2da;
  --accent-ink: #fff;
  --warn: #b45f06;
  --shadow: 0 1px 2px rgb(80 50 20 / 0.06), 0 6px 18px -8px rgb(80 50 20 / 0.18);
  --radius: 14px;
  --serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15110e;
    --surface: #1e1915;
    --surface-2: #26201b;
    --fg: #f0e7da;
    --muted: #a8998a;
    --line: #362d26;
    --line-strong: #4d4037;
    --accent: #e48b74;
    --accent-soft: #3b241d;
    --accent-ink: #1a110d;
    --warn: #f0a24b;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 22px -10px rgb(0 0 0 / 0.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 17px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
p a, .link { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; letter-spacing: -0.01em; line-height: 1.2; text-wrap: balance; margin: 0; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }
.skip { position: absolute; left: -999px; }
.skip:focus { left: 1rem; top: 1rem; background: var(--surface); padding: .5rem 1rem; z-index: 10; }

/* ---------- Thanh trên ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 1rem;
  padding: .6rem clamp(1rem, 3vw, 2rem);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .55rem; font-family: var(--serif); font-weight: 600; font-size: 1.1rem; white-space: nowrap; }
.brand-mark { width: 1.9rem; height: 1.9rem; border-radius: 9px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 1rem; }
.nav { display: flex; gap: .2rem; margin-left: auto; overflow-x: auto; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a, .nav button {
  padding: .4rem .75rem; border-radius: 999px; color: var(--muted); font: inherit; font-size: .95rem;
  white-space: nowrap; background: none; border: 0; cursor: pointer; transition: background .2s, color .2s;
}
.nav a:hover, .nav button:hover { background: var(--surface-2); color: var(--fg); }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav form { margin: 0; }

main { max-width: 1240px; margin: 0 auto; padding: clamp(1rem, 3vw, 2rem); }

/* ---------- Nút, ô nhập ---------- */
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .55rem 1.05rem; border-radius: 10px; border: 1px solid var(--accent);
  background: var(--accent); color: var(--accent-ink); font: inherit; font-weight: 600; font-size: .95rem;
  cursor: pointer; transition: transform .15s, filter .2s, background .2s;
}
.btn:hover { filter: brightness(1.07); }
.btn:active { transform: translateY(1px) scale(.99); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.quiet { background: var(--surface-2); color: var(--fg); border-color: var(--line); font-weight: 500; }
.btn.small { padding: .3rem .7rem; font-size: .85rem; }
label { display: block; margin: .8rem 0 .3rem; font-weight: 600; font-size: .92rem; }
input[type=text], input[type=password], input[type=search], input[type=number], select, textarea {
  width: 100%; padding: .6rem .75rem; font: inherit; color: var(--fg);
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 10px;
  transition: border-color .2s, box-shadow .2s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type=checkbox] { width: 1.15rem; height: 1.15rem; accent-color: var(--accent); vertical-align: -3px; margin-right: .35rem; }
.check { font-weight: 500; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: 0 1rem; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: clamp(1rem, 2.5vw, 1.5rem); box-shadow: var(--shadow); }
.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; margin: 0 0 1rem; }
.section-title h2 { font-size: 1.3rem; }
.chip { display: inline-flex; align-items: center; gap: .3rem; padding: .15rem .6rem; border-radius: 999px; font-size: .82rem; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.chip.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); background: color-mix(in srgb, var(--warn) 10%, transparent); }
.chip.accent { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.notice { padding: .75rem 1rem; border-radius: 10px; background: var(--accent-soft); color: var(--accent); margin: 1rem 0; }
.notice.warn { background: color-mix(in srgb, var(--warn) 12%, transparent); color: var(--warn); }

/* ---------- Trang chủ ---------- */
.home-top { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr); gap: 1.25rem; margin-bottom: 1.75rem; }
@media (max-width: 820px) { .home-top { grid-template-columns: 1fr; } }
.hero {
  position: relative; overflow: hidden;
  padding: clamp(1.25rem, 4vw, 2.25rem);
  border-radius: calc(var(--radius) + 4px);
  background:
    radial-gradient(120% 90% at 100% 0%, var(--accent-soft) 0%, transparent 60%),
    var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.hero .eyebrow { font-size: .85rem; letter-spacing: .08em; color: var(--muted); margin: 0 0 .4rem; font-variant: small-caps; }
.hero h1 { font-size: clamp(2rem, 5vw, 3.1rem); line-height: 1.05; letter-spacing: -0.02em; }
.stats { display: flex; flex-wrap: wrap; gap: 1.6rem; margin: 1.4rem 0 1.5rem; padding: 0; list-style: none; }
.stats li { display: flex; flex-direction: column; }
.stats b { font-family: var(--serif); font-size: 1.9rem; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.stats span { color: var(--muted); font-size: .88rem; margin-top: .3rem; }
.search { position: relative; max-width: 30rem; }
.search input { padding-left: 2.4rem; background: var(--bg); }
.search svg { position: absolute; left: .8rem; top: 50%; transform: translateY(-50%); color: var(--muted); }

.next { display: flex; flex-direction: column; }
.next-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: .75rem; }
.next-head h2 { font-size: 1.15rem; }
.next-feature { display: grid; grid-template-columns: auto 1fr; gap: 1rem; align-items: center; padding: 1rem; border-radius: 12px; background: var(--accent-soft); margin-bottom: .5rem; transition: transform .2s; }
.next-feature:hover { transform: translateY(-1px); }
.countdown { font-family: var(--serif); color: var(--accent); text-align: center; min-width: 4.2rem; }
.countdown b { display: block; font-size: 2.4rem; line-height: 1; font-variant-numeric: tabular-nums; }
.countdown span { font-size: .8rem; }
.next-feature strong { display: block; font-size: 1.1rem; }

/* Danh sách giỗ */
.gio-list { list-style: none; margin: 0; padding: 0; }
.gio { display: grid; grid-template-columns: 3.4rem 1fr auto; gap: .9rem; align-items: center; padding: .7rem .25rem; border-bottom: 1px solid var(--line); }
.gio:last-child { border-bottom: 0; }
.gio-date { text-align: center; border-radius: 10px; background: var(--surface-2); padding: .3rem 0; line-height: 1.1; }
.gio-date b { display: block; font-family: var(--serif); font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.gio-date span { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.gio-name { font-weight: 600; }
.gio-name:hover { color: var(--accent); }
.gio-sub { display: block; font-size: .85rem; color: var(--muted); }
.gio-count { font-size: .85rem; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.gio.today .gio-date { background: var(--accent); color: var(--accent-ink); }
.gio.today .gio-date span { color: inherit; }
.month-label { font-family: var(--serif); font-size: 1.05rem; margin: 1.5rem 0 .25rem; color: var(--muted); }

/* ---------- Tab + công cụ sơ đồ ---------- */
.tree-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: .75rem; }
.tree-head h2 { font-size: 1.45rem; }
.tabs { display: inline-flex; padding: 3px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); }
.tabs a { padding: .35rem .9rem; border-radius: 8px; font-size: .92rem; color: var(--muted); transition: background .2s, color .2s; }
.tabs a[aria-current="page"] { background: var(--surface); color: var(--fg); font-weight: 600; box-shadow: var(--shadow); }
.tools { display: inline-flex; gap: .3rem; }
.tools button { width: 2.2rem; height: 2.2rem; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); color: var(--fg); font: inherit; cursor: pointer; transition: background .2s; }
.tools button.wide { width: auto; padding: 0 .75rem; font-size: .88rem; }
.tools button:hover { background: var(--surface-2); }
.legend { display: flex; flex-wrap: wrap; gap: 1rem; font-size: .85rem; color: var(--muted); margin: .25rem 0 .75rem; }
.legend i { display: inline-block; width: .85rem; height: .85rem; border-radius: 4px; vertical-align: -1px; margin-right: .35rem; }
.legend .l-blood { background: var(--surface); border: 1.5px solid color-mix(in srgb, var(--accent) 55%, var(--line)); }
.legend .l-spouse { background: var(--surface-2); border: 1.5px dashed var(--line-strong); }
.legend .l-dead { background: var(--muted); opacity: .5; }

/* ---------- Sơ đồ cây ---------- */
.chart-wrap {
  position: relative; overflow: auto; cursor: grab;
  height: min(72vh, 760px); min-height: 360px;
  border-radius: var(--radius); border: 1px solid var(--line);
  background:
    radial-gradient(circle, var(--line) 1px, transparent 1.2px) 0 0 / 22px 22px,
    var(--surface);
  overscroll-behavior: contain;
}
.chart-wrap.dragging { cursor: grabbing; user-select: none; }
.chart { width: max-content; min-width: 100%; padding: 2rem 2rem 3rem; }
.chart ul { display: flex; justify-content: center; padding: 22px 0 0; margin: 0; position: relative; list-style: none; }
.chart > ul { padding-top: 0; }
.chart li { display: flex; flex-direction: column; align-items: center; position: relative; padding: 22px 10px 0; }
.chart > ul > li { padding-top: 0; }
/* đường nối anh chị em */
.chart li::before, .chart li::after { content: ""; position: absolute; top: 0; right: 50%; width: 50%; height: 22px; border-top: 2px solid var(--line-strong); }
.chart li::after { right: auto; left: 50%; border-left: 2px solid var(--line-strong); }
.chart li:only-child::before, .chart li:only-child::after { display: none; }
.chart li:only-child { padding-top: 0; }
.chart li:first-child::before, .chart li:last-child::after { border: 0; }
.chart li:last-child::before { border-right: 2px solid var(--line-strong); border-top-right-radius: 10px; }
.chart li:first-child::after { border-top-left-radius: 10px; }
.chart > ul > li::before, .chart > ul > li::after { display: none; }
/* đường từ cha mẹ xuống */
.chart li > ul::before { content: ""; position: absolute; top: 0; left: 50%; height: 22px; border-left: 2px solid var(--line-strong); }
.chart li > ul > li:only-child { padding-top: 22px; }
.chart li > ul > li:only-child::after { display: block; border-top: 0; height: 22px; left: 50%; }

.unit { display: flex; align-items: center; gap: 0; position: relative; }
.unit .pcard + .pcard { margin-left: 18px; }
.unit .pcard + .pcard::before { content: ""; position: absolute; left: -19px; top: 50%; width: 18px; border-top: 2px solid var(--line-strong); }

.pcard {
  position: relative; display: flex; align-items: center; gap: .65rem;
  width: 218px; height: 74px; padding: .55rem .7rem;
  border-radius: 12px; background: var(--surface);
  border: 1.5px solid color-mix(in srgb, var(--accent) 55%, var(--line));
  box-shadow: var(--shadow); text-align: left;
  transition: transform .18s, box-shadow .2s, border-color .2s;
}
.pcard:hover { transform: translateY(-2px); border-color: var(--accent); }
.pcard.spouse { background: var(--surface-2); border: 1.5px dashed var(--line-strong); box-shadow: none; }
.pcard.dead .av { filter: grayscale(1); opacity: .75; }
.pcard.dead .pc-name { color: color-mix(in srgb, var(--fg) 80%, var(--muted)); }
.av {
  flex: none; width: 2.3rem; height: 2.3rem; border-radius: 10px;
  display: grid; place-items: center; font-family: var(--serif); font-weight: 600; font-size: 1.1rem;
  background: var(--accent-soft); color: var(--accent);
}
.av.nu { border-radius: 50%; }
.pc { min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.pc-name { font-weight: 600; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pc-meta, .pc-note { font-size: .78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.pc-note { color: var(--accent); }
.pcard .dot { position: absolute; top: 7px; right: 7px; width: 8px; height: 8px; border-radius: 50%; background: var(--warn); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn) 25%, transparent); }

.fold {
  position: relative; margin-top: 12px; padding: .1rem .6rem; border-radius: 999px;
  font: inherit; font-size: .75rem; color: var(--muted); background: var(--surface); border: 1px solid var(--line-strong); cursor: pointer;
  transition: background .2s, color .2s;
}
.fold:hover { color: var(--accent); border-color: var(--accent); }
.fold::before { content: ""; position: absolute; bottom: 100%; left: 50%; height: 12px; border-left: 2px solid var(--line-strong); }
li.collapsed > ul { display: none; }

/* ---------- Dạng danh sách ---------- */
.outline, .outline ul { list-style: none; margin: 0; padding: 0; }
.outline ul { margin-left: 1.1rem; padding-left: 1rem; border-left: 2px solid var(--line); }
.outline li { margin: .3rem 0; }
.outline summary { list-style: none; cursor: pointer; }
.outline summary::-webkit-details-marker { display: none; }
.row-person { display: flex; flex-wrap: wrap; align-items: baseline; gap: .2rem .6rem; padding: .35rem .5rem; border-radius: 8px; }
.row-person:hover { background: var(--surface-2); }
.row-person .name { font-weight: 600; }
.row-person .name:hover { color: var(--accent); }
.row-person .sub { font-size: .85rem; color: var(--muted); }
.caret { display: inline-block; width: 1rem; color: var(--muted); transition: transform .2s; }
details[open] > summary .caret { transform: rotate(90deg); }

/* ---------- Trang một người ---------- */
.profile { display: grid; grid-template-columns: auto 1fr; gap: 1.25rem; align-items: center; }
.profile .av { width: 4.5rem; height: 4.5rem; font-size: 2rem; border-radius: 18px; }
.profile .av.nu { border-radius: 50%; }
.profile h1 { font-size: clamp(1.7rem, 4vw, 2.4rem); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .6rem; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr)); gap: 1px; background: var(--line); border-radius: 12px; overflow: hidden; margin-top: 1.5rem; border: 1px solid var(--line); }
.fact { background: var(--surface); padding: .9rem 1rem; }
.fact dt { font-size: .8rem; color: var(--muted); }
.fact dd { margin: .2rem 0 0; font-family: var(--serif); font-size: 1.2rem; font-variant-numeric: tabular-nums; }
.fact dd small { display: block; font-family: var(--sans); font-size: .82rem; color: var(--muted); }
.note-text { white-space: pre-wrap; margin: 1.25rem 0 0; max-width: 65ch; }
.family { display: grid; gap: 1.25rem; margin-top: 1.75rem; }
.family h2 { font-size: 1.1rem; margin-bottom: .6rem; }
.cards { display: flex; flex-wrap: wrap; gap: .75rem; }
.empty-line { color: var(--muted); font-size: .92rem; }
.editor { margin-top: 2rem; }
.editor > summary { cursor: pointer; font-weight: 600; padding: .8rem 1rem; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--line); list-style: none; }
.editor > summary::-webkit-details-marker { display: none; }
.editor[open] > summary { margin-bottom: 1rem; }
.editor-grid { display: grid; gap: 1rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 1rem; }
.rel-list { list-style: none; padding: 0; margin: 0; }
.rel-list li { display: flex; justify-content: space-between; align-items: center; gap: .5rem; padding: .45rem 0; border-bottom: 1px solid var(--line); }
.danger { color: var(--warn); border-color: var(--warn); background: transparent; }
.danger:hover { background: color-mix(in srgb, var(--warn) 10%, transparent); }

.foot { max-width: 1240px; margin: 1rem auto 2.5rem; padding: 0 clamp(1rem, 3vw, 2rem); font-size: .88rem; }
.foot a { color: var(--muted); }
.foot a:hover { color: var(--accent); }
.share { margin-top: .35rem; }

/* ---------- Khác ---------- */
.narrow { max-width: 760px; margin: 0 auto; }
.login { min-height: 80dvh; display: grid; place-items: center; }
.login .panel { width: min(100%, 26rem); }
.login h1 { font-size: 2rem; margin: .3rem 0 1rem; }
.empty { text-align: center; padding: 3rem 1rem; }
.empty h2 { font-size: 1.5rem; margin-bottom: .5rem; }
table.log { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.log td { padding: .55rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
pre { white-space: pre-wrap; word-break: break-word; font-size: .78rem; background: var(--surface-2); padding: .6rem; border-radius: 8px; }
code { background: var(--surface-2); padding: .05rem .3rem; border-radius: 5px; font-size: .88em; }

@media (max-width: 560px) {
  body { font-size: 16px; }
  .brand-text { display: none; }
  .gio { grid-template-columns: 3rem 1fr; }
  .gio-count { grid-column: 2; }
  .profile .av { width: 3.5rem; height: 3.5rem; font-size: 1.5rem; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; scroll-behavior: auto !important; } }
@media print {
  .topbar, .no-print, form, button, .tools, .tabs, .editor { display: none !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .chart-wrap { height: auto; overflow: visible; border: 0; background: none; }
  .panel, .hero { box-shadow: none; border: 0; background: none; }
  li.collapsed > ul { display: flex; }
}
