/* ============================================================
   Gia Phả Việt — giao diện
   Màu chủ đạo: đỏ son & vàng đồng trên nền giấy dó
   ============================================================ */
:root {
  --scale: 1;
  --bg: #f6efe0;
  --paper: #fffaf0;
  --surface: #ffffff;
  --surface-2: #f4ebd8;
  --text: #2b2118;
  --muted: #6b5e4f;
  --line: #e2d4b7;
  --primary: #8f1d1d;
  --primary-2: #a82828;
  --primary-soft: #f6e1dc;
  --on-primary: #ffffff;
  --gold: #a87a0c;
  --gold-soft: #f6e9c2;
  --male: #2f5d8a;
  --male-soft: #e4eef9;
  --female: #a83c63;
  --female-soft: #f9e5ed;
  --ok: #2e7d32;
  --warn: #a35d00;
  --danger: #b3261e;
  --shadow: 0 1px 3px rgba(60, 40, 10, .08), 0 8px 24px rgba(60, 40, 10, .07);
  --radius: 16px;
  --topbar-h: 62px;
  --font: 'Be Vietnam Pro', 'Segoe UI', Roboto, Arial, sans-serif;
  --serif: 'Noto Serif', Georgia, serif;
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #15110e;
  --paper: #1d1814;
  --surface: #241e19;
  --surface-2: #2e2620;
  --text: #f3eadb;
  --muted: #b9aa92;
  --line: #3d342b;
  --primary: #d9534f;
  --primary-2: #e8655f;
  --primary-soft: #3a2220;
  --on-primary: #ffffff;
  --gold: #e3b54a;
  --gold-soft: #3a3020;
  --male: #86b6e6;
  --male-soft: #1d2b3a;
  --female: #eb93b4;
  --female-soft: #3a2230;
  --ok: #7bc67e;
  --warn: #f0b04a;
  --danger: #ff7a70;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { font-size: calc(17px * var(--scale)); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.55;
  min-height: 100vh;
  background-image: radial-gradient(circle at 20% 0%, rgba(168, 122, 12, .06), transparent 40%),
                    radial-gradient(circle at 100% 100%, rgba(143, 29, 29, .05), transparent 40%);
  background-attachment: fixed;
}
a { color: var(--primary); }
img { max-width: 100%; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-family: var(--serif); font-size: 1.7rem; }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.1rem; }
p { margin: 0 0 .8em; }
.muted { color: var(--muted); }
.hidden, [hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; border-radius: 6px; }

/* ---------------- Thanh trên cùng */
.topbar {
  position: sticky; top: 0; z-index: 50;
  height: var(--topbar-h);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 0 14px;
  padding-top: env(safe-area-inset-top);
  background: linear-gradient(135deg, #7a1515 0%, #9c2121 55%, #7a1515 100%);
  color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .18);
}
.topbar::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 3px;
  background: repeating-linear-gradient(90deg, #e3b54a 0 18px, #b8860b 18px 22px);
  opacity: .85;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; min-width: 0; }
.brand-logo { width: 40px; height: 40px; border-radius: 10px; box-shadow: 0 0 0 2px rgba(255, 220, 140, .5); }
.brand-text { font-family: var(--serif); font-weight: 700; font-size: 1.3rem; color: #fbe3a6; letter-spacing: .3px; white-space: nowrap; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.font-ctl { display: flex; gap: 2px; background: rgba(255, 255, 255, .12); border-radius: 12px; padding: 2px; }
.icon-btn {
  min-width: 44px; height: 44px; padding: 0 10px;
  display: inline-flex; align-items: center; justify-content: center; gap: 2px;
  border: 0; border-radius: 12px; cursor: pointer;
  background: transparent; color: inherit; font: inherit; font-weight: 700; font-size: 1.05rem;
}
.icon-btn small { font-size: .8em; }
.icon-btn:hover { background: rgba(255, 255, 255, .16); }
.topbar .icon-btn { color: #fff; }
.user-btn {
  display: flex; align-items: center; gap: 8px; height: 44px; padding: 0 10px 0 4px;
  background: rgba(255, 255, 255, .14); color: #fff; border: 0; border-radius: 22px; cursor: pointer; font: inherit; font-weight: 600;
}
.user-btn:hover { background: rgba(255, 255, 255, .24); }
.uname { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------------- Nút & trường nhập */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 10px 18px;
  font: inherit; font-weight: 600; font-size: 1rem; line-height: 1.2;
  color: var(--text); background: var(--surface); border: 2px solid var(--line); border-radius: 14px;
  cursor: pointer; text-decoration: none; transition: transform .08s, background .15s, border-color .15s;
}
.btn:hover { border-color: var(--gold); }
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-2); border-color: var(--primary-2); }
.btn.gold { background: var(--gold-soft); border-color: var(--gold); color: var(--text); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn.danger.solid { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.big { min-height: 56px; font-size: 1.08rem; padding: 12px 22px; }
.btn.block { width: 100%; }
.btn.sm { min-height: 40px; padding: 6px 12px; font-size: .92rem; border-radius: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }

.fld { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.fld > span { font-weight: 600; }
.fld small, .hint { color: var(--muted); font-size: .9rem; }
input, select, textarea {
  font: inherit; font-size: 1.05rem; color: var(--text);
  min-height: 52px; padding: 10px 14px; width: 100%;
  background: var(--paper); border: 2px solid var(--line); border-radius: 12px;
}
textarea { min-height: 120px; resize: vertical; line-height: 1.5; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--gold); box-shadow: 0 0 0 4px color-mix(in srgb, var(--gold) 22%, transparent); }
input[type=checkbox], input[type=radio] { width: 24px; height: 24px; min-height: 0; accent-color: var(--primary); margin: 0; flex: none; }
.chk { display: flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; font-weight: 500; }
.pw { position: relative; }
.pw input { padding-right: 56px; }
.pw-eye { position: absolute; right: 4px; top: 4px; bottom: 4px; width: 48px; border: 0; background: transparent; cursor: pointer; font-size: 1.2rem; border-radius: 10px; color: var(--muted); }

.seg { display: flex; gap: 8px; flex-wrap: wrap; }
.seg label {
  flex: 1; min-width: 120px; display: flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 54px; border: 2px solid var(--line); border-radius: 14px; cursor: pointer; font-weight: 600; background: var(--paper);
}
.seg label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }

.switch { display: flex; align-items: center; gap: 12px; min-height: 56px; padding: 8px 14px; border: 2px solid var(--line); border-radius: 14px; cursor: pointer; font-weight: 600; margin-bottom: 14px; background: var(--paper); }
.switch:has(input:checked) { border-color: var(--muted); background: var(--surface-2); }

fieldset { border: 2px solid var(--line); border-radius: 16px; padding: 12px 14px 4px; margin: 0 0 16px; }
legend { font-weight: 700; padding: 0 8px; color: var(--primary); }
.date-row { display: grid; grid-template-columns: 1fr 1fr 1.4fr; gap: 10px; }
.date-row label { display: flex; flex-direction: column; gap: 4px; font-size: .92rem; font-weight: 600; color: var(--muted); }
.conv { min-height: 1.4em; margin: 4px 0 10px; color: var(--gold); font-weight: 600; font-size: .95rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
details.more { border: 2px dashed var(--line); border-radius: 16px; padding: 4px 14px; margin-bottom: 16px; }
details.more summary { cursor: pointer; font-weight: 700; min-height: 48px; display: flex; align-items: center; color: var(--primary); }
details.more[open] { padding-bottom: 6px; }

/* ---------------- Thẻ, trang */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.page { max-width: 1100px; margin: 0 auto; padding: 24px 16px 60px; }
.page.narrow { max-width: 820px; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.page-head h1 { margin: 0; }
.loading { padding: 40px 16px; text-align: center; color: var(--muted); font-size: 1.1rem; }
.loading::before { content: ""; display: block; width: 36px; height: 36px; margin: 0 auto 12px; border: 4px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.err { color: var(--danger); font-weight: 600; margin-top: 10px; }
.section-title { display: flex; align-items: center; gap: 10px; margin: 28px 0 12px; font-size: 1.2rem; }
.section-title::after { content: ""; flex: 1; height: 2px; background: linear-gradient(90deg, var(--line), transparent); }
.empty { text-align: center; padding: 40px 20px; }
.empty .big-icon { font-size: 3.4rem; margin-bottom: 8px; }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; font-size: .82rem; font-weight: 700; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.owner { background: var(--gold-soft); color: var(--gold); }
.badge.editor { background: var(--male-soft); color: var(--male); }
.badge.red { background: var(--primary-soft); color: var(--primary); }

/* ---------------- Đăng nhập */
.auth { min-height: calc(100vh - var(--topbar-h)); display: grid; grid-template-columns: 1.1fr 1fr; gap: 32px; align-items: center; max-width: 1100px; margin: 0 auto; padding: 32px 18px; }
.auth-hero h1 { font-size: 2.6rem; color: var(--primary); margin-bottom: .2em; }
.auth-hero .quote { font-family: var(--serif); font-size: 1.35rem; color: var(--gold); font-style: italic; margin-bottom: 1.2em; }
.feat { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.feat li { display: flex; gap: 14px; align-items: flex-start; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.feat .fi { font-size: 1.6rem; line-height: 1; flex: none; }
.feat b { display: block; }
.feat span { color: var(--muted); font-size: .95rem; }
.auth-card { padding: 26px; }
.tabs2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; background: var(--surface-2); border-radius: 14px; padding: 5px; margin-bottom: 20px; }
.tabs2 a { text-align: center; padding: 12px; border-radius: 10px; text-decoration: none; color: var(--muted); font-weight: 700; font-size: 1.05rem; }
.tabs2 a.on { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }

/* ---------------- Danh sách gia phả */
.tree-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.tree-card {
  position: relative; display: flex; flex-direction: column; gap: 6px; padding: 20px; min-height: 170px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  text-decoration: none; color: var(--text); overflow: hidden; transition: transform .15s, border-color .15s;
}
.tree-card::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 6px; background: linear-gradient(90deg, var(--primary), var(--gold)); }
.tree-card:hover { transform: translateY(-3px); border-color: var(--gold); }
.tree-card h2 { font-family: var(--serif); margin: 6px 0 0; color: var(--primary); }
.tree-card .meta { color: var(--muted); font-size: .95rem; }
.tree-card .foot { margin-top: auto; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tree-card .tc-icon { font-size: 2rem; }
.tree-card.add { align-items: center; justify-content: center; text-align: center; border: 2px dashed var(--line); background: transparent; box-shadow: none; cursor: pointer; font: inherit; color: var(--muted); }
.tree-card.add::before { display: none; }

/* ---------------- Không gian gia phả */
.ws { display: flex; flex-direction: column; height: calc(100vh - var(--topbar-h)); height: calc(100dvh - var(--topbar-h)); }
.ws-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--paper); border-bottom: 1px solid var(--line); }
.back-btn { width: 46px; height: 46px; flex: none; display: flex; align-items: center; justify-content: center; border-radius: 12px; background: var(--surface-2); text-decoration: none; color: var(--text); font-size: 1.8rem; font-weight: 700; }
.ws-title { min-width: 0; flex: 1; }
.ws-title h1 { font-size: 1.35rem; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--primary); }
.ws-title p { margin: 0; color: var(--muted); font-size: .92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tabs { display: flex; gap: 4px; padding: 6px 12px 0; background: var(--paper); border-bottom: 2px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  position: relative; display: flex; align-items: center; gap: 8px; padding: 10px 16px; min-height: 50px;
  text-decoration: none; color: var(--muted); font-weight: 600; white-space: nowrap; border-radius: 12px 12px 0 0;
}
.tab .ti { font-size: 1.25rem; line-height: 1; }
.tab:hover { color: var(--text); background: var(--surface-2); }
.tab.on { color: var(--primary); background: var(--bg); box-shadow: inset 0 -3px 0 var(--primary); }
.tab .dot { min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px; background: var(--primary); color: #fff; font-size: .75rem; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; }
.ws-body { flex: 1; min-height: 0; overflow: auto; position: relative; }

/* ---------------- Phả đồ */
.tree-wrap { position: absolute; inset: 0; overflow: hidden; }
.tree-canvas { position: absolute; inset: 0; }
.tree-svg { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
.tree-svg:active { cursor: grabbing; }
.tree-svg .card, .tree-svg .tg { cursor: pointer; }
.tree-svg .card:hover > rect:first-of-type { stroke-width: 3.5; }
.tree-wrap::before {
  content: ""; position: absolute; inset: 0; pointer-events: none; opacity: .5;
  background-image: radial-gradient(var(--line) 1px, transparent 1px); background-size: 26px 26px;
}
.tree-top { position: absolute; top: 12px; left: 12px; right: 12px; display: flex; gap: 10px; align-items: flex-start; pointer-events: none; z-index: 3; }
.tree-top > * { pointer-events: auto; }
.tsearch { position: relative; width: min(420px, 100%); }
.tsearch input { background: var(--surface); box-shadow: var(--shadow); padding-left: 16px; }
.sugg { position: absolute; top: calc(100% + 6px); left: 0; right: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; z-index: 5; }
.sugg:empty { display: none; }
.sugg button { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 14px; min-height: 56px; border: 0; background: transparent; text-align: left; font: inherit; color: var(--text); cursor: pointer; border-bottom: 1px solid var(--line); }
.sugg button:last-child { border-bottom: 0; }
.sugg button:hover, .sugg button:focus { background: var(--surface-2); }
.focus-bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; background: var(--gold-soft); border: 2px solid var(--gold); border-radius: 14px; padding: 6px 8px 6px 14px; font-weight: 600; box-shadow: var(--shadow); }
.tree-tools { position: absolute; right: 12px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 3; }
.tool {
  display: flex; align-items: center; gap: 10px; min-width: 54px; height: 54px; padding: 0 16px;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
  font: inherit; font-weight: 600; cursor: pointer; justify-content: flex-start;
}
.tool .tic { font-size: 1.45rem; width: 24px; text-align: center; line-height: 1; }
.tool:hover { border-color: var(--gold); }
.tree-legend { position: absolute; left: 12px; bottom: 16px; display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; padding: 8px 14px; background: color-mix(in srgb, var(--surface) 88%, transparent); border: 1px solid var(--line); border-radius: 14px; font-size: .9rem; color: var(--muted); z-index: 2; max-width: calc(100% - 110px); }
.tree-legend span { display: inline-flex; align-items: center; gap: 6px; }
.lg { width: 16px; height: 16px; border-radius: 5px; display: inline-block; border: 2px solid; }
.lg.m { background: var(--male-soft); border-color: var(--male); }
.lg.f { background: var(--female-soft); border-color: var(--female); }
.lg.d { background: var(--surface-2); border-color: var(--muted); }
.lg.l { width: 22px; height: 0; border-width: 3px 0 0; border-color: #d39200; border-radius: 0; }

/* ---------------- Ảnh đại diện */
.avatar {
  --sz: 52px;
  width: var(--sz); height: var(--sz); flex: none; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center; overflow: hidden;
  font-weight: 700; font-size: calc(var(--sz) * .42);
  background: var(--male-soft); color: var(--male); border: 2px solid var(--male);
}
.avatar.F { background: var(--female-soft); color: var(--female); border-color: var(--female); }
.avatar.dead { filter: grayscale(.5); border-style: dashed; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.sm { --sz: 34px; border-width: 0; background: #fbe3a6; color: #7a1515; }
.avatar.xl { --sz: 110px; border-width: 3px; }

/* ---------------- Danh sách thành viên */
.list-tools { display: grid; gap: 10px; margin-bottom: 12px; }
.filters { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.list-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.plist { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.prow {
  display: flex; align-items: center; gap: 14px; width: 100%; padding: 12px 14px; min-height: 76px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; cursor: pointer; text-align: left; font: inherit; color: var(--text);
}
.prow:hover { border-color: var(--gold); }
.prow .pmain { flex: 1; min-width: 0; }
.prow b { font-size: 1.08rem; }
.prow .alias { color: var(--muted); font-style: italic; }
.prow small { display: block; color: var(--muted); font-size: .92rem; }
.prow .chev { font-size: 1.6rem; color: var(--muted); }
.gen-head { margin: 18px 0 6px; font-family: var(--serif); color: var(--primary); font-size: 1.15rem; }

/* ---------------- Ngăn chi tiết (drawer) */
.drawer {
  position: fixed; z-index: 60; top: var(--topbar-h); right: 0; bottom: 0; width: min(440px, 100%);
  background: var(--paper); border-left: 1px solid var(--line); box-shadow: -10px 0 30px rgba(0, 0, 0, .15);
  transform: translateX(105%); transition: transform .25s ease; display: flex; flex-direction: column;
}
.drawer.open { transform: none; }
.drawer-back { display: none; }
.dr-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.dr-head .icon-btn { color: var(--text); background: var(--surface-2); }
.dr-body { flex: 1; overflow: auto; padding: 18px 18px 40px; }
.dr-hero { text-align: center; padding: 6px 0 14px; }
.dr-hero h2 { font-family: var(--serif); font-size: 1.5rem; margin: 12px 0 2px; }
.dr-hero .alias { color: var(--muted); font-style: italic; margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 14px 0; padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; }
.facts dt { color: var(--muted); font-weight: 600; }
.facts dd { margin: 0; font-weight: 500; overflow-wrap: anywhere; }
.facts dd .gold { color: var(--gold); font-weight: 700; }
.bio { white-space: pre-wrap; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 14px; }
.act-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.act-grid .btn { min-height: 54px; font-size: .98rem; }
.rel { margin: 6px 0 12px; }
.rel h4 { margin: 0 0 6px; color: var(--muted); font-size: .95rem; }
.rel-list { display: flex; flex-direction: column; gap: 6px; }
.rel-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; min-height: 58px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; cursor: pointer; text-align: left; font: inherit; color: var(--text); width: 100%; }
.rel-item:hover { border-color: var(--gold); }
.rel-item .avatar { --sz: 40px; }
.rel-item .grow { flex: 1; min-width: 0; }
.rel-item small { display: block; color: var(--muted); }
.rel-row { display: flex; gap: 6px; align-items: stretch; }
.rel-row .rel-item { flex: 1; }
.rel-row .btn { min-height: 58px; }

/* ---------------- Hộp thoại */
.modal-wrap { position: fixed; inset: 0; z-index: 100; background: rgba(20, 12, 6, .55); display: flex; align-items: center; justify-content: center; padding: 16px; animation: fade .15s; }
@keyframes fade { from { opacity: 0; } }
.modal { width: min(560px, 100%); max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; background: var(--paper); border-radius: 20px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35); overflow: hidden; }
.modal.wide { width: min(760px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 12px 12px 20px; border-bottom: 1px solid var(--line); background: var(--surface); }
.modal-head h2 { margin: 0; font-size: 1.25rem; }
.modal-head .icon-btn { color: var(--text); background: var(--surface-2); }
.modal-body { padding: 18px 20px 22px; overflow: auto; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; position: sticky; bottom: -22px; background: var(--paper); padding: 12px 0 4px; margin-top: 6px; border-top: 1px solid var(--line); }
.form-actions .btn { flex: 1; max-width: 240px; }
.photo-pick { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.photo-pick .btn-col { display: flex; flex-direction: column; gap: 8px; }

/* ---------------- Toast */
#toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; width: min(92vw, 520px); pointer-events: none; }
.toast { background: #2b2118; color: #fff; padding: 14px 20px; border-radius: 14px; font-weight: 600; box-shadow: 0 8px 24px rgba(0, 0, 0, .3); animation: up .2s; text-align: center; }
.toast.ok { background: #256b2a; }
.toast.bad { background: #a52019; }
@keyframes up { from { transform: translateY(10px); opacity: 0; } }

/* ---------------- Ngày giỗ */
.today-card { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; background: linear-gradient(135deg, #8f1d1d, #a8341f); color: #fff; border: 0; }
.today-card .big-day { font-family: var(--serif); font-size: 3.4rem; line-height: 1; color: #fbe3a6; min-width: 84px; text-align: center; }
.today-card .t1 { font-size: 1.15rem; font-weight: 700; }
.today-card .t2 { opacity: .9; }
.gio-list { display: grid; gap: 10px; }
.gio-item { display: flex; align-items: center; gap: 14px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; cursor: pointer; width: 100%; text-align: left; font: inherit; color: var(--text); }
.gio-item:hover { border-color: var(--gold); }
.gio-item.soon { border: 2px solid var(--primary); background: var(--primary-soft); }
.gio-date { flex: none; width: 78px; text-align: center; padding: 6px 4px; background: var(--gold-soft); border-radius: 12px; line-height: 1.15; }
.gio-date b { display: block; font-size: 1.8rem; font-family: var(--serif); color: var(--primary); }
.gio-date span { font-size: .85rem; font-weight: 700; }
.gio-date small { display: block; font-size: .75rem; color: var(--muted); }
.gio-info { flex: 1; min-width: 0; }
.gio-info h3 { margin: 0 0 2px; font-size: 1.08rem; }
.gio-info p { margin: 0; color: var(--muted); font-size: .94rem; }
.gio-left { flex: none; text-align: center; font-weight: 700; color: var(--primary); min-width: 70px; }
.gio-left b { display: block; font-size: 1.5rem; line-height: 1.1; }

/* ---------------- Xưng hô */
.kin-swap { display: flex; justify-content: center; margin: -4px 0 10px; }
.kin-res { margin-top: 18px; display: grid; gap: 12px; }
.kin-line { padding: 16px; border-radius: 16px; background: var(--surface-2); font-size: 1.08rem; }
.kin-term { display: block; margin-top: 4px; font-family: var(--serif); font-size: 1.8rem; color: var(--primary); font-weight: 700; }
.kin-path { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: .95rem; }
.kin-path .pchip { padding: 4px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; cursor: pointer; font: inherit; color: var(--text); }
.kin-path .arr { color: var(--gold); font-weight: 700; }

/* ---------------- Thống kê */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 16px; }
.stat b { display: block; font-family: var(--serif); font-size: 2.1rem; color: var(--primary); line-height: 1.1; }
.stat span { color: var(--muted); font-weight: 600; }
.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 76px 1fr 54px; align-items: center; gap: 10px; }
.bar-row .lbl { font-weight: 700; }
.bar { height: 26px; background: var(--surface-2); border-radius: 8px; overflow: hidden; display: flex; }
.bar i { display: block; height: 100%; }
.bar i.m { background: var(--male); }
.bar i.f { background: var(--female); }
.bar-row .num { text-align: right; font-weight: 700; }
.legend-inline { display: flex; gap: 16px; margin: 6px 0 12px; color: var(--muted); font-size: .92rem; }

/* ---------------- Cài đặt */
.set-sec { margin-bottom: 18px; }
.set-sec h2 { display: flex; align-items: center; gap: 10px; }
.code-box { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 12px 14px; background: var(--surface-2); border-radius: 14px; margin: 8px 0 14px; }
.code-box code { font-size: 1.5rem; font-weight: 700; letter-spacing: 3px; color: var(--primary); font-family: ui-monospace, Consolas, monospace; flex: 1; min-width: 160px; overflow-wrap: anywhere; }
.code-box code.url { font-size: .95rem; letter-spacing: 0; }
.mem-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.mem-row .grow { flex: 1; min-width: 160px; }
.mem-row select { width: auto; min-height: 44px; }
.danger-zone { border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }

/* ---------------- Hướng dẫn */
.help-steps { counter-reset: s; list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.help-steps li { counter-increment: s; position: relative; padding: 12px 14px 12px 60px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; }
.help-steps li::before { content: counter(s); position: absolute; left: 12px; top: 12px; width: 36px; height: 36px; border-radius: 50%; background: var(--primary); color: #fff; font-weight: 700; display: flex; align-items: center; justify-content: center; }

/* ================= Điện thoại / máy tính bảng ================= */
@media (max-width: 860px) {
  .auth { grid-template-columns: 1fr; gap: 20px; padding-top: 20px; }
  .auth-hero h1 { font-size: 2rem; }
  .auth-hero .feat { display: none; }
  .auth-hero { text-align: center; }
}
@media (max-width: 760px) {
  :root { --topbar-h: 58px; }
  html { font-size: calc(17px * var(--scale)); }
  .brand-text { font-size: 1.1rem; }
  .uname { display: none; }
  .user-btn { padding: 0 4px; }
  .font-ctl .icon-btn { min-width: 40px; padding: 0 6px; }
  .topbar { padding: 0 8px; }
  .topbar .icon-btn[data-act="help"] { display: none; }

  .ws { height: calc(100dvh - var(--topbar-h)); }
  .ws-head { padding: 8px 10px; }
  .ws-title h1 { font-size: 1.15rem; }
  /* Thanh tab chuyển xuống dưới cùng như ứng dụng điện thoại */
  .tabs {
    order: 3; padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); border-bottom: 0; border-top: 2px solid var(--line);
    justify-content: space-around; gap: 0; overflow-x: auto;
  }
  .tab { flex-direction: column; gap: 2px; padding: 6px 4px; min-width: 64px; flex: 1; font-size: .74rem; border-radius: 12px; min-height: 58px; justify-content: center; }
  .tab .ti { font-size: 1.45rem; }
  .tab.on { box-shadow: none; background: var(--primary-soft); }
  .tab .dot { position: absolute; top: 2px; right: 8px; min-width: 18px; height: 18px; font-size: .68rem; }

  .tree-tools { right: 8px; bottom: 10px; }
  .tool { min-width: 52px; height: 52px; padding: 0; justify-content: center; }
  .tool .tlabel { display: none; }
  .tree-legend { left: 8px; bottom: 10px; font-size: .8rem; padding: 6px 10px; max-width: calc(100% - 80px); }
  .tree-legend .hint-drag { display: none; }
  .tree-top { top: 8px; left: 8px; right: 8px; flex-direction: column; }
  .tsearch { width: 100%; }

  .drawer { top: auto; left: 0; width: 100%; height: 88dvh; border-radius: 22px 22px 0 0; border-left: 0; transform: translateY(105%); }
  .drawer.open { transform: none; }
  .drawer-back { position: fixed; inset: 0; z-index: 55; background: rgba(0, 0, 0, .4); }
  .drawer.open + .drawer-back { display: block; }
  .dr-head::before { content: ""; width: 48px; height: 5px; background: var(--line); border-radius: 3px; position: absolute; left: 50%; top: 6px; transform: translateX(-50%); }
  .dr-head { position: relative; padding-top: 14px; }

  .modal-wrap { padding: 0; align-items: flex-end; }
  .modal, .modal.wide { width: 100%; max-height: 94dvh; border-radius: 22px 22px 0 0; }
  .modal-body { padding: 16px 16px 22px; }
  .grid2 { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters select:last-child { grid-column: span 2; }
  .page { padding: 16px 12px 40px; }
  h1 { font-size: 1.45rem; }
  .gio-left { min-width: 56px; font-size: .85rem; }
  .gio-date { width: 66px; }
  .act-grid { grid-template-columns: 1fr 1fr; }
  .hide-sm { display: none !important; }
}
@media (max-width: 380px) {
  .brand-text { display: none; }
}

/* ---------------- In ấn */
@media print {
  .topbar, .tabs, .tree-tools, .tree-top, .drawer, .drawer-back, .ws-head .back-btn, .no-print, #toasts { display: none !important; }
  body { background: #fff; color: #000; }
  .ws { height: auto; }
  .ws-body { overflow: visible; }
  .card, .gio-item, .prow { box-shadow: none; break-inside: avoid; }
}
