:root {
  --red: #9e1c1f;
  --red-dark: #6f1013;
  --gold: #d4a73c;
  --gold-soft: #f3dfa6;
  --paper: #fbf6ea;
  --paper-2: #f3ead4;
  --ink: #2b1d14;
  --muted: #7a6656;
  --line: #e3d5b8;
  --ok: #2f7a45;
  --warn: #b26b00;
  --radius: 10px;
  --serif: 'Times New Roman', 'Noto Serif', Georgia, serif;
  --sans: 'Segoe UI', system-ui, -apple-system, Roboto, 'Helvetica Neue', Arial, sans-serif;
  --topbar-h: 60px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--sans); color: var(--ink); background: var(--paper); font-size: 15px; line-height: 1.5; }
a { color: var(--red); }
h1, h2, h3 { font-family: var(--serif); color: var(--red-dark); margin: 0 0 .5em; line-height: 1.25; }
h1 { font-size: 1.7rem; } h2 { font-size: 1.35rem; } h3 { font-size: 1.1rem; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- Thanh trên cùng ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--topbar-h);
  display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: linear-gradient(180deg, var(--red) 0%, var(--red-dark) 100%);
  color: #fff; border-bottom: 3px solid var(--gold);
  box-shadow: 0 2px 8px rgba(60, 10, 10, .25);
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; min-width: 0; }
.seal { width: 38px; height: 38px; border-radius: 8px; background: var(--gold); color: var(--red-dark); display: grid; place-items: center; flex: none; }
.seal svg { width: 26px; height: 26px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-text b { font-family: var(--serif); font-size: 1.2rem; letter-spacing: .5px; color: var(--gold-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text small { font-size: .72rem; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
.mainnav { display: flex; gap: 2px; margin-left: 8px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.mainnav a { color: #fbe9c4; text-decoration: none; padding: 8px 12px; border-radius: 8px; white-space: nowrap; font-size: .93rem; }
.mainnav a:hover { background: rgba(255,255,255,.1); }
.mainnav a.active { background: rgba(0,0,0,.22); color: #fff; box-shadow: inset 0 -2px 0 var(--gold); }
.mainnav .badge { background: var(--gold); color: var(--red-dark); border-radius: 9px; padding: 0 6px; font-size: .72rem; font-weight: 700; margin-left: 4px; }
.userbox { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.userbox .who { font-size: .85rem; text-align: right; line-height: 1.2; }
.userbox .who small { display: block; opacity: .75; font-size: .72rem; }
.menu-btn { display: none; background: none; border: 1px solid rgba(255,255,255,.35); color: #fff; border-radius: 8px; padding: 4px 10px; font-size: 1.1rem; cursor: pointer; margin-left: auto; }

/* ---------- Nút & ô nhập ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 7px 14px; border-radius: 8px; cursor: pointer; font-size: .92rem; text-decoration: none; line-height: 1.2; }
.btn:hover { border-color: var(--gold); background: #fffaf0; }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.primary:hover { background: var(--red-dark); }
.btn.gold { background: var(--gold); border-color: var(--gold); color: var(--red-dark); font-weight: 600; }
.btn.danger { color: #b3261e; border-color: #e7b4b0; }
.btn.danger:hover { background: #fdecea; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 4px 10px; font-size: .84rem; }
.btn.light { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.35); color: #fff; }
.btn.light:hover { background: rgba(255,255,255,.22); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.input, select.input, textarea.input { width: 100%; padding: 8px 10px; border: 1px solid #d8c9a8; border-radius: 8px; background: #fff; }
.input:focus { outline: 2px solid var(--gold); outline-offset: 0; border-color: var(--gold); }
textarea.input { min-height: 90px; resize: vertical; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: .86rem; color: var(--muted); }
label.field > span b { color: var(--red); }
.check { display: flex; align-items: center; gap: 8px; font-size: .9rem; cursor: pointer; }
.hint { font-size: .8rem; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid .full { grid-column: 1 / -1; }
.form-section { grid-column: 1 / -1; font-family: var(--serif); color: var(--red-dark); font-weight: bold; border-bottom: 1px solid var(--line); padding-bottom: 4px; margin-top: 6px; }

/* ---------- Bố cục trang ---------- */
.view { min-height: calc(100vh - var(--topbar-h)); }
.page { max-width: 1180px; margin: 0 auto; padding: 24px 20px 60px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head p { margin: 0; color: var(--muted); }
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: 0 1px 2px rgba(90, 60, 20, .05); }
.card + .card { margin-top: 16px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: .76rem; background: var(--paper-2); color: var(--muted); border: 1px solid var(--line); }
.tag.red { background: #fbe9e7; color: var(--red); border-color: #f0c4bf; }
.tag.green { background: #e7f4ea; color: var(--ok); border-color: #bfe0c8; }
.tag.gold { background: #fbf0d0; color: #8a5a00; border-color: #ecd391; }

/* ---------- Phả đồ ---------- */
.tree-page { display: flex; flex-direction: column; height: calc(100vh - var(--topbar-h)); }
.tree-toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 8px 12px; background: #fffaf0; border-bottom: 1px solid var(--line); }
.tree-toolbar .input { width: auto; padding: 6px 8px; font-size: .88rem; }
.tree-toolbar .search { position: relative; }
.tree-toolbar .search .input { width: 220px; padding-left: 30px; }
.tree-toolbar .search::before { content: '⌕'; position: absolute; left: 10px; top: 50%; transform: translateY(-52%); color: var(--muted); font-size: 1.1rem; }
.tree-toolbar .sep { width: 1px; height: 26px; background: var(--line); margin: 0 2px; }
.tree-toolbar .grow { flex: 1; }
.tree-wrap { position: relative; flex: 1; overflow: hidden; background: #e9dfc8; touch-action: none; }
.tree-canvas { position: absolute; inset: 0; cursor: grab; }
.tree-canvas.dragging { cursor: grabbing; }
.gp-svg { width: 100%; height: 100%; display: block; user-select: none; }
.gp-hit { cursor: pointer; }
.gp-hit:hover rect { fill: rgba(212, 167, 60, .16); }
.gp-toggle { cursor: pointer; }
.zoombar { position: absolute; right: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 6px; z-index: 2; }
.zoombar button { width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: #fff; font-size: 1.15rem; cursor: pointer; box-shadow: 0 1px 4px rgba(0,0,0,.12); }
.zoombar button:hover { background: #fffaf0; border-color: var(--gold); }
.tree-banner { position: absolute; left: 14px; top: 12px; z-index: 2; background: rgba(255,255,255,.95); border: 1px solid var(--gold); border-radius: 10px; padding: 6px 10px; font-size: .86rem; display: flex; gap: 8px; align-items: center; box-shadow: 0 2px 8px rgba(0,0,0,.1); }
.dropdown { position: relative; }
.dropdown-menu { position: absolute; right: 0; top: calc(100% + 4px); background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 24px rgba(40,20,0,.18); min-width: 220px; padding: 6px; z-index: 20; }
.dropdown-menu button, .dropdown-menu a { display: block; width: 100%; text-align: left; padding: 8px 10px; border: 0; background: none; border-radius: 6px; cursor: pointer; color: var(--ink); text-decoration: none; font-size: .9rem; }
.dropdown-menu button:hover, .dropdown-menu a:hover { background: var(--paper-2); }
.dropdown-menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

/* ---------- Ngăn kéo hồ sơ ---------- */
.drawer { position: fixed; top: var(--topbar-h); right: 0; bottom: 0; width: 420px; max-width: 100%; background: #fff; border-left: 3px solid var(--gold); box-shadow: -8px 0 24px rgba(40,20,0,.18); transform: translateX(105%); transition: transform .22s ease; z-index: 25; display: flex; flex-direction: column; }
.drawer.open { transform: none; }
.drawer-head { padding: 18px 18px 14px; background: linear-gradient(180deg, #fff7e3, #fff); border-bottom: 1px solid var(--line); position: relative; }
.drawer-head .close { position: absolute; right: 10px; top: 10px; }
.drawer-body { overflow-y: auto; padding: 14px 18px 30px; flex: 1; }
.profile-top { display: flex; gap: 14px; align-items: center; }
.avatar { width: 76px; height: 76px; border-radius: 50%; flex: none; background: var(--paper-2); border: 3px solid var(--gold); display: grid; place-items: center; font-family: var(--serif); font-size: 1.6rem; color: var(--red-dark); overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.profile-top h2 { margin: 0; font-size: 1.3rem; }
.profile-top .meta { color: var(--muted); font-size: .86rem; }
.profile-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.info-list { display: grid; grid-template-columns: 120px 1fr; gap: 6px 10px; font-size: .9rem; margin: 0; }
.info-list dt { color: var(--muted); }
.info-list dd { margin: 0; word-break: break-word; }
.section-title { font-family: var(--serif); color: var(--red-dark); font-weight: bold; margin: 18px 0 8px; padding-bottom: 4px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.rel-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; }
.rel-list li { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.rel-list a { cursor: pointer; text-decoration: none; }
.rel-list a:hover { text-decoration: underline; }
.rel-list .role { color: var(--muted); font-size: .8rem; min-width: 70px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.bio { white-space: pre-wrap; font-size: .92rem; background: var(--paper); padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line); }
.kin-box { background: #fffaf0; border: 1px dashed var(--gold); border-radius: 8px; padding: 10px; font-size: .9rem; }
.kin-result { margin-top: 8px; font-weight: 600; color: var(--red-dark); }

/* ---------- Bảng ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
table.data { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.data th { text-align: left; background: var(--paper-2); color: var(--red-dark); font-weight: 600; padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; position: sticky; top: 0; }
table.data td { padding: 8px 10px; border-bottom: 1px solid #f1e8d6; vertical-align: top; }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: #fffaf0; }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
.filters .input { width: auto; }
.filters .input.search { min-width: 240px; flex: 1; max-width: 360px; }

/* ---------- Lịch ---------- */
.today { display: flex; gap: 18px; align-items: center; background: linear-gradient(135deg, var(--red), var(--red-dark)); color: #fff; border-radius: var(--radius); padding: 18px 22px; border: 2px solid var(--gold); }
.today .big { font-family: var(--serif); font-size: 3rem; line-height: 1; color: var(--gold-soft); }
.today .lunar { color: var(--gold-soft); }
.month-group h3 { margin-top: 22px; font-size: 1rem; color: var(--muted); font-family: var(--sans); text-transform: uppercase; letter-spacing: .5px; }
.ev { display: grid; grid-template-columns: 72px 1fr auto; gap: 14px; align-items: center; padding: 12px 14px; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 8px; }
.ev .date { text-align: center; border-right: 1px solid var(--line); padding-right: 12px; }
.ev .date b { display: block; font-family: var(--serif); font-size: 1.6rem; color: var(--red); line-height: 1; }
.ev .date small { color: var(--muted); font-size: .76rem; }
.ev .title { font-weight: 600; }
.ev .sub { color: var(--muted); font-size: .84rem; }
.ev .left { font-size: .84rem; color: var(--muted); text-align: right; white-space: nowrap; }
.ev .left b { color: var(--red); font-size: 1rem; }
.ev.soon { border-color: var(--gold); background: #fffaf0; }

/* ---------- Tin tức ---------- */
.post { padding: 18px 20px; }
.post .post-meta { color: var(--muted); font-size: .82rem; margin-bottom: 8px; }
.post .post-body { white-space: pre-wrap; }
.prose { white-space: pre-wrap; }

/* ---------- Thống kê ---------- */
.stat { text-align: center; padding: 16px 10px; }
.stat b { display: block; font-family: var(--serif); font-size: 2rem; color: var(--red); line-height: 1.1; }
.stat span { color: var(--muted); font-size: .86rem; }
.bars { display: flex; flex-direction: column; gap: 6px; }
.bar-row { display: grid; grid-template-columns: 64px 1fr 170px; gap: 10px; align-items: center; font-size: .88rem; }
.bar-track { height: 18px; background: var(--paper-2); border-radius: 4px; overflow: hidden; display: flex; }
.bar-m { background: #2f5d7c; }
.bar-f { background: #c2457a; }
.legend { display: flex; gap: 14px; font-size: .82rem; color: var(--muted); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }

/* ---------- Hộp thoại ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(30, 15, 5, .45); z-index: 50; display: grid; place-items: center; padding: 16px; animation: fade .15s; }
.modal { background: #fff; border-radius: 14px; width: 560px; max-width: 100%; max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(30, 10, 0, .35); border-top: 4px solid var(--red); }
.modal.wide { width: 820px; }
.modal-head { padding: 16px 20px 10px; display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; font-size: 1.25rem; }
.modal-body { padding: 6px 20px 16px; overflow-y: auto; }
.modal-foot { padding: 12px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; background: #fffcf5; border-radius: 0 0 14px 14px; }
.error-box { background: #fdecea; color: #8c1d18; border: 1px solid #f3c1bc; border-radius: 8px; padding: 8px 12px; font-size: .88rem; margin-bottom: 10px; white-space: pre-wrap; }
@keyframes fade { from { opacity: 0; } }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: #2b1d14; color: #fff; padding: 10px 18px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; z-index: 60; max-width: 90vw; }
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { background: #8c1d18; }
.toast.ok { background: #245c35; }

/* ---------- Màn hình khởi tạo / cổng vào ---------- */
.gate { min-height: calc(100vh - var(--topbar-h)); display: grid; place-items: center; padding: 30px 16px; background:
  radial-gradient(circle at 50% 30%, rgba(212,167,60,.18), transparent 60%), var(--paper); }
.gate .card { width: 480px; max-width: 100%; border-top: 4px solid var(--red); }
.gate h1 { text-align: center; }
.gate .lead { text-align: center; color: var(--muted); margin-top: -4px; }

.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--line); margin-bottom: 18px; flex-wrap: wrap; }
.tabs a { padding: 8px 14px; text-decoration: none; color: var(--muted); border-bottom: 3px solid transparent; margin-bottom: -2px; }
.tabs a.active { color: var(--red); border-color: var(--red); font-weight: 600; }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; }
.swatch { border: 2px solid var(--line); border-radius: 10px; padding: 6px; cursor: pointer; text-align: center; font-size: .8rem; width: 120px; background: #fff; }
.swatch.active { border-color: var(--red); box-shadow: 0 0 0 2px var(--gold-soft); }
.swatch .chip { height: 40px; border-radius: 6px; display: flex; align-items: center; justify-content: center; margin-bottom: 4px; }
.swatch .chip span { padding: 2px 8px; border-radius: 3px; font-family: var(--serif); font-weight: bold; font-size: .78rem; }

@media (max-width: 1280px) {
  .userbox .who { display: none; }
}
@media (max-width: 1100px) {
  .menu-btn { display: block; }
  .mainnav { display: none; position: absolute; top: var(--topbar-h); left: 0; right: 0; flex-direction: column; background: var(--red-dark); padding: 8px; margin: 0; border-bottom: 3px solid var(--gold); }
  .mainnav.open { display: flex; }
  .userbox .who { display: none; }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .form-grid, .grid-2 { grid-template-columns: 1fr; }
  .grid-3 { grid-template-columns: 1fr 1fr; }
  .drawer { width: 100%; }
  .tree-toolbar .search .input { width: 150px; }
  .tree-toolbar .opt { display: none; }
  .brand-text small { display: none; }
  .ev { grid-template-columns: 60px 1fr; }
  .ev .left { grid-column: 2; text-align: left; }
  .info-list { grid-template-columns: 100px 1fr; }
}
@media print {
  .topbar, .tree-toolbar, .zoombar, .drawer { display: none !important; }
}
