body {
  background-color: #f8fafc;
}

.custom-scrollbar::-webkit-scrollbar { width: 6px; height: 6px; }
.custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
.custom-scrollbar::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
.custom-scrollbar::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

.pattern-bg {
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%238c4b38' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.tree-grid-bg {
  background-image: radial-gradient(#cbd5e1 1px, transparent 1px);
  background-size: 18px 18px;
}

/* =========================================================
   1. TỐI ƯU KHUNG NHẬP LIỆU (MODAL) TRÊN ĐIỆN THOẠI & MÁY TÍNH
   ========================================================= */
.modal-backdrop-scroll {
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.modal-card-mobile {
  max-height: 86vh;
  max-height: 86dvh;
  display: flex;
  flex-direction: column;
  margin-top: auto;
  margin-bottom: auto;
}

.modal-body-scroll {
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  flex: 1 1 auto;
}

@media (max-width: 767px) {
  input, select, textarea {
    font-size: 16px !important;
  }
  .modal-backdrop-scroll {
    align-items: flex-start !important;
    padding: 0.5rem !important;
  }
  .modal-card-mobile {
    max-height: 92vh;
    max-height: 92dvh;
    margin-top: 0.25rem;
    margin-bottom: 1.5rem;
  }
}

/* =========================================================
   2. CỐ ĐỊNH TIÊU ĐỀ DỌC & CỘT NGANG BẢNG QUẢN LÝ (#view-admin)
   ========================================================= */
#view-admin table {
  border-collapse: separate !important;
  border-spacing: 0 !important;
  width: 100% !important;
}

#view-admin table th {
  position: -webkit-sticky !important;
  position: sticky !important;
  top: 0 !important;
  background-color: #f1f5f9 !important;
  z-index: 40 !important;
  border-top: 1px solid #e2e8f0 !important;
  border-bottom: 2px solid #cbd5e1 !important;
}

#view-admin table th:first-child {
  position: -webkit-sticky !important;
  position: sticky !important;
  top: 0 !important;
  left: 0 !important;
  z-index: 50 !important;
  background-color: #f1f5f9 !important;
  border-right: 1px solid #cbd5e1 !important;
  min-width: 145px !important;
  max-width: 155px !important;
  box-shadow: 4px 0 6px -2px rgba(0, 0, 0, 0.1) !important;
}

#view-admin table td:first-child {
  position: -webkit-sticky !important;
  position: sticky !important;
  left: 0 !important;
  background-color: #ffffff !important;
  z-index: 20 !important;
  border-right: 1px solid #e2e8f0 !important;
  min-width: 145px !important;
  max-width: 155px !important;
  box-shadow: 4px 0 6px -2px rgba(0, 0, 0, 0.1) !important;
}

#view-admin table td {
  border-bottom: 1px solid #f1f5f9 !important;
}

#view-admin table tr:hover td,
#view-admin table tr:hover td:first-child {
  background-color: #f8fafc !important;
}

#view-admin table tr[class*="bg-amber"] td,
#view-admin table tr[class*="bg-amber"] td:first-child {
  background-color: #fffbeb !important;
}

#view-admin th:not(:first-child),
#view-admin td:not(:first-child) {
  min-width: 135px !important;
  white-space: normal !important;
}

#view-admin th:last-child,
#view-admin td:last-child {
  min-width: 110px !important;
}
/* Cố định bề rộng cột 1 vừa vặn trên điện thoại (khoảng 165px) để nhìn rõ các cột phía sau */
@media (max-width: 767px) {
  #view-admin table th:first-child,
  #view-admin table td:first-child {
    width: 165px !important;
    min-width: 165px !important;
    max-width: 165px !important;
    white-space: normal !important;
    word-break: break-word !important;
    box-shadow: 2px 0 5px -1px rgba(0, 0, 0, 0.08) !important;
  }
}

/* =========================================================
   3. MENU ĐIỀU HƯỚNG & THANH HEADER
   ========================================================= */
#tree-search-dropdown, .search-dropdown-container {
  z-index: 99999 !important;
  position: absolute;
  width: 100%;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.2);
}

@media (max-width: 1023px) {
  div:has(> [id^="m-nav-"]) {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    gap: 0.375rem !important;
    padding-bottom: 4px !important;
    scrollbar-width: none !important;
  }
  div:has(> [id^="m-nav-"])::-webkit-scrollbar {
    display: none !important;
  }
}

@media (min-width: 1024px) {
  div:has(> [id^="m-nav-"]) {
    display: none !important;
  }
}

[id^="m-nav-"], [id^="nav-"] {
  flex-shrink: 0 !important;
  white-space: nowrap !important;
}

#user-scope-badge {
  max-width: none !important;
  white-space: nowrap !important;
  display: inline-flex !important;
  align-items: center !important;
}

@media (min-width: 1024px) and (max-width: 1366px) {
  #user-scope-badge {
    max-width: 180px !important;
  }
}

header.bg-white > div:first-child {
  margin-right: 1.5rem !important;
  flex-shrink: 0 !important;
}

/* =========================================================
   4. SƠ ĐỒ TRUYỀN THỐNG PHÂN NHÁNH (.trad-tree)
   ========================================================= */
.trad-tree, .trad-tree ul {
  display: flex;
  justify-content: center;
  padding-top: 18px;
  position: relative;
  margin: 0;
  padding-left: 0;
  list-style: none;
}

.trad-tree li {
  float: left;
  text-align: center;
  list-style-type: none;
  position: relative;
  padding: 18px 6px 0 6px;
}

.trad-tree li::before, .trad-tree li::after {
  content: '';
  position: absolute;
  top: 0;
  right: 50%;
  border-top: 1.5px solid #78350f;
  width: 50%;
  height: 18px;
}

.trad-tree li::after {
  right: auto;
  left: 50%;
  border-left: 1.5px solid #78350f;
}

.trad-tree li:only-child::after, .trad-tree li:only-child::before {
  display: none;
}

.trad-tree li:only-child {
  padding-top: 0;
}

.trad-tree li:first-child::before, .trad-tree li:last-child::after {
  border: 0 none;
}

.trad-tree li:last-child::before {
  border-right: 1.5px solid #78350f;
  border-radius: 0 5px 0 0;
}

.trad-tree li:first-child::after {
  border-radius: 5px 0 0 0;
}

.trad-tree ul::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  border-left: 1.5px solid #78350f;
  width: 0;
  height: 18px;
}

.trad-node {
  display: inline-block;
  border: 1px solid #92400e;
  background: #fffbeb;
  padding: 4px 8px;
  border-radius: 6px;
  min-width: 105px;
  max-width: 135px;
  text-align: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.trad-name {
  font-weight: 700;
  font-size: 11px;
  color: #451a03;
  line-height: 1.2;
}

.trad-meta {
  font-size: 9px;
  color: #78350f;
  margin-top: 1px;
}

.trad-spouse {
  font-size: 8.5px;
  color: #b45309;
  font-style: italic;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Kiểu 4: Thẻ Bài Vị Cổ Điển (.trad-plaque-node) */
.trad-plaque-node {
  border: 2px solid #991b1b !important;
  outline: 1px solid #d97706;
  outline-offset: -4px;
  background: linear-gradient(180deg, #fffbeb 0%, #fef3c7 100%) !important;
  padding: 6px 10px 8px 10px !important;
  border-radius: 10px 10px 4px 4px !important;
  min-width: 118px !important;
  max-width: 148px !important;
  box-shadow: 0 3px 8px rgba(120, 53, 15, 0.14) !important;
}
.trad-plaque-head {
  background: #991b1b;
  color: #fde68a;
  font-size: 8.5px;
  font-weight: 800;
  letter-spacing: 0.5px;
  padding: 2px 6px;
  border-radius: 4px;
  margin-bottom: 4px;
  display: inline-block;
}
.trad-nick {
  font-size: 9px;
  color: #92400e;
  font-style: italic;
}

/* Kiểu 5: Bảng Tông Đồ Thế Thứ (.lineage-table-wrap) */
.lineage-table-wrap {
  border: 2px solid #78350f;
  border-radius: 14px;
  overflow: hidden;
  background: #fffbeb;
}
.lineage-row {
  display: flex;
  border-bottom: 1px solid #d97706;
}
.lineage-row:last-child {
  border-bottom: none;
}
.lineage-gen-col {
  width: 110px;
  flex-shrink: 0;
  background: #451a03;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 6px;
  border-right: 2px solid #92400e;
  text-align: center;
}
.lineage-members-col {
  flex: 1;
  padding: 10px 12px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  align-items: center;
}
.lineage-box {
  background: #ffffff;
  border: 1px solid #b45309;
  border-radius: 8px;
  padding: 5px 10px;
  min-width: 145px;
  max-width: 190px;
  text-align: center;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

/* =========================================================
   5. ĐỊNH DẠNG CHUYÊN DỤNG KHI IN GIA PHẢ & SỔ QUỸ (KHUNG TRANH A4)
   ========================================================= */
@media print {
  @page {
    size: A4 landscape;
    /* Lề trang tạo khoảng đệm tự động ở đầu và cuối MỌI tờ giấy khi ngắt trang */
    margin: 13mm 12mm 13mm 12mm;
  }

  #landing-page, header, nav, .flex.lg\:hidden, #user-scope-badge,
  button, select, input, #tree-search-dropdown, .search-dropdown-container,
  #tree-smart-search, #tree-search-clear, #toast, [id$="-modal"],
  #btn-floating-chat, #chat-widget-box, #view-tree > div:first-child,
  .no-print-duplicate {
    display: none !important;
    visibility: hidden !important;
  }

  body * {
    visibility: hidden;
  }

  #tree-print-frame,
  #view-tree:not(.hidden), #view-tree:not(.hidden) *,
  #view-fund:not(.hidden), #view-fund:not(.hidden) * {
    visibility: visible !important;
  }

    /* Khung viền kép nằm ngoài vùng nội dung 6px để chữ và thẻ không bao giờ chạm viền */
  #tree-print-frame {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    border: 3px double #78350f !important;
    outline: 1px solid #b45309 !important;
    outline-offset: -5px !important;
    border-radius: 6px !important;
    pointer-events: none !important;
    z-index: 9999 !important;
  }

  body, html, #app-portal, main, #view-tree, #view-fund {
    height: auto !important;
    overflow: visible !important;
    background: #fffdf7 !important;
    position: static !important;
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  #tree-canvas {
    width: 100% !important;
    min-height: 175mm !important;
    margin: 0 auto !important;
    /* Khoảng cách lùi sâu vào trong lòng khung viền ở cả 4 phía */
    padding: 16px 24px 18px 24px !important;
    background: transparent !important;
    box-sizing: border-box !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: space-between !important;
  }

  #tree-scroll-container, .lineage-table-wrap, #tree-canvas > .max-w-4xl {
    overflow: visible !important;
    margin: auto !important;
  }

  /* Tạo khoảng cách đệm trên đầu các thẻ khi sang trang 2, 3 để không dính viền trên */
  #tree-canvas > .max-w-6xl > div {
    margin-top: 14px !important;
    margin-bottom: 14px !important;
    padding-top: 14px !important;
    break-inside: avoid !important;
    page-break-inside: avoid !important;
  }

  .tree-grid-bg {
    background-image: none !important;
  }

  /* Khóa chặt khối Chữ ký, các dòng Tông Đồ và thẻ thành viên không bao giờ bị cắt đôi qua viền */
  #tree-print-banner, #tree-print-footer,
  .trad-node, .lineage-row, #tree-canvas .w-64, tr {
    break-inside: avoid !important;
    page-break-inside: avoid !important;
  }

  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
}

