@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap");

/* FontSiteFT font - will use theme's font definitions */

:root {
  --brown: #7b3f00;
  --red: #b22222;
  --gold: #c8a25a;
  --ivory: #f7f2e9;
  --line: #a86b3a;
  --shadow: rgba(0, 0, 0, 0.15);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  background: radial-gradient(circle at 20% 20%, #f8f0e3, #f1e2c8);
  color: #2c1c0f;
}

.page-wrap {
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 16px 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 12px;
  background: linear-gradient(90deg, #f3e1c7, #f8ebd6);
  border: 1px solid #e3cba0;
  border-radius: 12px;
  box-shadow: 0 6px 18px var(--shadow);
}

.topbar h1 {
  margin: 0;
  color: var(--brown);
  letter-spacing: 0.5px;
  font-family: "FontSite", "FontSiteFT", "Inter", system-ui, -apple-system, sans-serif;
}

.subtitle {
  margin: 4px 0 0;
  color: #6e4c28;
  font-size: 14px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.controls button {
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid #d2b07b;
  background: #fff4e6;
  color: var(--brown);
  cursor: pointer;
  font-weight: 600;
  box-shadow: 0 2px 8px var(--shadow);
  transition: transform 0.1s ease, box-shadow 0.2s ease;
}

.controls button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px var(--shadow);
}

.back-btn {
  display: inline-flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid #d2b07b;
  background: #fff4e6;
  color: var(--brown);
  text-decoration: none;
  font-weight: 600;
  font-family: "FontSite", "FontSiteFT", "Inter", system-ui, -apple-system, sans-serif;
  box-shadow: 0 2px 8px var(--shadow);
  margin-right: 8px;
}

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-top: 12px;
}

.layout.single-column {
  grid-template-columns: 1fr;
}

.tree-panel {
  background: #fffdf9;
  border: 1px solid #e5d2b0;
  border-radius: 10px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6), 0 10px 30px var(--shadow);
  padding: 6px;
}

#tree-container {
  position: relative;
  width: 100%;
  height: 85vh;
  overflow: hidden;
  border-radius: 10px;
  background: url("../assets/images/bg-pattern.jpg"), radial-gradient(circle at 30% 30%, #fff7e8, #f4e6cd);
  background-size: cover;
  border: 1px dashed #d9b887;
  transition: transform 0.3s ease, width 0.3s ease, height 0.3s ease;
}

/* Landscape mode styles */
body.tree-landscape-mode {
  overflow: hidden;
}

body.tree-landscape-mode:fullscreen,
body.tree-landscape-mode:-webkit-full-screen,
body.tree-landscape-mode:-moz-full-screen,
body.tree-landscape-mode:-ms-fullscreen {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

body.tree-landscape-mode .page-wrap {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  z-index: 9999;
}

body.tree-landscape-mode:fullscreen .page-wrap,
body.tree-landscape-mode:-webkit-full-screen .page-wrap,
body.tree-landscape-mode:-moz-full-screen .page-wrap,
body.tree-landscape-mode:-ms-fullscreen .page-wrap {
  position: fixed;
  width: 100vw;
  height: 100vh;
}

body.tree-landscape-mode .topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10001;
  border-radius: 0;
  padding: 6px 10px;
  background: linear-gradient(90deg, #f3e1c7, #f8ebd6);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

body.tree-landscape-mode .tree-panel {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0;
  border-radius: 0;
  padding: 0;
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  z-index: 9998;
}

body.tree-landscape-mode:fullscreen .tree-panel,
body.tree-landscape-mode:-webkit-full-screen .tree-panel,
body.tree-landscape-mode:-moz-full-screen .tree-panel,
body.tree-landscape-mode:-ms-fullscreen .tree-panel {
  position: fixed;
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
  z-index: 9998;
}

body.tree-landscape-mode:fullscreen .topbar,
body.tree-landscape-mode:-webkit-full-screen .topbar,
body.tree-landscape-mode:-moz-full-screen .topbar,
body.tree-landscape-mode:-ms-fullscreen .topbar {
  position: fixed;
  z-index: 10001;
}

#tree-container.landscape-rotated {
  transform: rotate(90deg);
  transform-origin: center center;
  position: absolute;
  top: 50%;
  left: 50%;
  /* Prevent touch events from being inverted */
  touch-action: pan-x pan-y pinch-zoom;
  /* Ensure pointer events work correctly */
  pointer-events: auto;
  /* Ensure smooth transitions */
  transition: transform 0.3s ease, width 0.3s ease, height 0.3s ease;
  /* Prevent text selection issues */
  user-select: none;
  -webkit-user-select: none;
}

/* Center rotated container in fullscreen */
body.tree-landscape-mode:fullscreen #tree-container.landscape-rotated,
body.tree-landscape-mode:-webkit-full-screen #tree-container.landscape-rotated,
body.tree-landscape-mode:-moz-full-screen #tree-container.landscape-rotated,
body.tree-landscape-mode:-ms-fullscreen #tree-container.landscape-rotated {
  position: absolute;
  top: 50%;
  left: 50%;
  /* When rotated 90deg, swap viewport dimensions */
  margin-left: calc(-50vh);
  margin-top: calc(-50vw);
  width: 100vh;
  height: 100vw;
}

/* Non-fullscreen landscape mode */
body.tree-landscape-mode:not(:fullscreen):not(:-webkit-full-screen):not(:-moz-full-screen):not(:-ms-fullscreen)
  #tree-container.landscape-rotated {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: calc(-50vh);
  margin-top: calc(-50vw);
  width: 100vh;
  height: 100vw;
}

.controls button#rotate-landscape.active {
  background: linear-gradient(135deg, var(--red), #d24d2c);
  color: #fff;
  border-color: var(--red);
}

/* Chỉ hiển thị button xoay ngang trên mobile */
.controls button#rotate-landscape {
  display: none;
}

@media (max-width: 768px) {
  .controls button#rotate-landscape {
    display: inline-flex;
  }
}

#tree-canvas {
  width: 100%;
  height: 100%;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #8a5a2b;
  font-weight: 600;
  background: linear-gradient(180deg, rgba(255, 250, 243, 0.8), rgba(244, 230, 205, 0.8));
  pointer-events: none;
}

/* Ẩn panel nhập liệu cũ (không dùng) */
.form-panel {
  display: none;
}

.form-panel h3 {
  margin-top: 0;
  color: var(--brown);
}

form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

label {
  font-weight: 600;
  color: #4a3119;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
}

input,
textarea,
select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid #d9c19a;
  background: #fffaf2;
  font-size: 14px;
}

textarea {
  resize: vertical;
}

.inline {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
}

.inline label {
  font-weight: 500;
}

.primary {
  background: linear-gradient(135deg, var(--red), #d24d2c);
  color: #fff;
  border: none;
  padding: 10px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
  box-shadow: 0 6px 18px rgba(178, 34, 34, 0.25);
  transition: transform 0.1s ease, box-shadow 0.2s ease;
}

.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(178, 34, 34, 0.3);
}

.hint {
  margin: 0;
  font-size: 12px;
  color: #6b4a28;
}

.guide {
  margin-top: 18px;
  padding: 12px;
  border-radius: 10px;
  background: #f6ebd9;
  border: 1px solid #e2c69a;
}

.guide h4 {
  margin: 0 0 6px;
  color: var(--brown);
}

.guide ul {
  margin: 0;
  padding-left: 18px;
  color: #54361f;
}

.guide li {
  margin-bottom: 6px;
}

.data-guide {
  padding-left: 18px;
  color: #54361f;
  margin: 0 0 12px;
}

.data-guide li {
  margin-bottom: 6px;
}

.import-box {
  padding: 12px;
  border: 1px dashed #d9b887;
  border-radius: 10px;
  background: #fff4e6;
}

/* Tree styling */
.link {
  fill: none;
  stroke: var(--line);
  stroke-width: 2.5px;
}

.spouse-link {
  fill: none;
  stroke: var(--red);
  stroke-width: 2px;
  stroke-dasharray: 6 4;
}

.node-card {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  background: linear-gradient(145deg, #fff9f1, #f4e4c8);
  border: 1px solid #d8bb8a;
  border-radius: 12px;
  box-shadow: 0 8px 18px var(--shadow);
  padding: 12px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #2c1c0f;
}

.node-card.single {
  width: 148px;
  height: 150px;
  min-height: 150px;
  padding: 8px 6px;
}

.node-card.couple-wrapper {
  width: 300px;
  height: 150px;
  padding: 0;
  background: transparent;
  border: none;
  box-shadow: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.root-node .node-card.single {
  width: 180px;
  height: 160px;
  min-height: 160px;
}

.root-node .node-card.couple-wrapper {
  width: 300px;
  height: 165px;
}

.root-node .node-card.spouse-card {
  width: 145px;
  min-height: 165px;
}

.couple-cards {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  padding: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  gap: 0;
}

.node-card.spouse-card {
  width: 148px;
  min-height: 150px;
  padding: 8px 6px;
}

.couple-cards .node-card.spouse-card:first-child {
  border-radius: 12px 0 0 12px;
}

.couple-cards .node-card.spouse-card:last-child {
  border-radius: 0 12px 12px 0;
}

.node-card .title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.person {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  height: 100%;
  justify-content: center;
}

.node-card.single .person {
  gap: 10px;
}

.avatar {
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid var(--gold);
  background: #f2dcb8;
  box-shadow: 0 2px 6px var(--shadow);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  /* Default PC size */
  width: 78px;
  height: 78px;
  min-width: 78px;
  min-height: 78px;
  max-width: 78px;
  max-height: 78px;
  flex: 0 0 78px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
  display: block;
}

/* Tablet: 769px - 1024px */
@media (min-width: 769px) and (max-width: 1024px) {
  .avatar {
    width: 70px;
    height: 70px;
    min-width: 70px;
    min-height: 70px;
    max-width: 70px;
    max-height: 70px;
    flex: 0 0 70px;
  }
}

/* Mobile: max-width 768px */
@media (max-width: 768px) {
  .avatar {
    width: 62px;
    height: 62px;
    min-width: 62px;
    min-height: 62px;
    max-width: 62px;
    max-height: 62px;
    flex: 0 0 62px;
  }
}

.meta h4 {
  line-height: 1.1;
  margin: 0;
  font-size: 13px;
  color: var(--brown);
}

.meta p {
  line-height: 1.1;
  margin: 2px 0 0;
  font-size: 11px;
  color: #5b3a1f;
}

.node-card.single .meta h4 {
  font-size: 14px;
}

.node-card.single .meta p {
  font-size: 10px;
}

.meta {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.spouse-sep {
  width: 100%;
  height: 1px;
  background: #d9b887;
  margin: 8px 0;
}

.couple-gap {
  width: 4px;
  height: 100%;
  background: transparent;
  border-radius: 0;
  margin: 0;
}

.tag {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 10px;
  background: #f2d9ae;
  color: #5a3b1e;
  margin-top: 3px;
}

.tag-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 4px;
}

.node-card.single .tag {
  padding: 2px 12px;
  font-size: 10px;
  margin-top: 6px;
}

.tag-death {
  background: #8b1e1e;
  color: #fff7f0;
}

.node-card.single .spouse-sep {
  display: none;
}

.node-card:hover {
  cursor: pointer;
  box-shadow: 0 10px 22px var(--shadow);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.hidden {
  display: none;
}

.modal-content {
  background: #f5ead5;
  background-image: 
    repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(139, 110, 99, 0.03) 2px, rgba(139, 110, 99, 0.03) 4px),
    linear-gradient(180deg, #f8f0e0 0%, #f0e4d0 100%);
  border-radius: 8px;
  padding: 28px 24px;
  width: min(580px, 92vw);
  box-shadow: 
    0 20px 50px rgba(0, 0, 0, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.4),
    inset 0 -1px 0 rgba(139, 110, 99, 0.2);
  border: 2px solid #8b6f47;
  border-top: 3px solid #a0825f;
  position: relative;
}

.close-modal {
  position: absolute;
  top: 12px;
  right: 12px;
  background: #d4c4a8;
  border: 2px solid #8b6f47;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 20px;
  cursor: pointer;
  color: #5a4228;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.close-modal:hover {
  background: #c4b08f;
  border-color: #6b5637;
}

.modal-body {
  display: grid;
  grid-template-columns: 140px 1fr;
  column-gap: 22px;
  row-gap: 14px;
  align-items: flex-start;
}

.modal-avatar img {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  object-fit: cover;
  border: 5px solid #8b6f47;
  box-shadow: 
    inset 0 0 0 2px #d4c4a8,
    0 4px 12px rgba(91, 66, 40, 0.3);
}
.modal-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: center;
  align-items: center;
}

.modal-info h3 {
  margin: 0 0 10px;
  color: #5a4228;
  font-family: "Times New Roman", "Georgia", "Noto Serif", "Noto Serif Display", serif;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-shadow: 1px 1px 2px rgba(139, 110, 99, 0.2);
  border-bottom: 2px solid #8b6f47;
  padding-bottom: 8px;
  width: 100%;
}

.modal-info p {
  margin: 4px 0;
  color: #4a3119;
  line-height: 1.7;
  word-break: break-word;
  font-size: 16px;
  font-family: "Times New Roman", "Georgia", "Noto Serif", serif;
  letter-spacing: 0.3px;
}

.modal-info p.empty-field {
  display: none;
}

#modal-relation {
  white-space: pre-line;
  font-style: italic;
  color: #6b5637;
}

@media (max-width: 1024px) {
  .layout {
    grid-template-columns: 1fr;
  }

  #tree-container {
    height: 65vh;
  }

  .modal-body {
    grid-template-columns: 1fr;
    text-align: center;
    row-gap: 10px;
  }
}

@media (max-width: 768px) {
  .topbar h1 {
    font-size: 22px;
  }
  .subtitle {
    font-size: 12px;
  }
  .controls button,
  .back-btn {
    padding: 7px 10px;
    font-size: 13px;
  }
  .page-wrap {
    padding: 10px 5px 10px;
  }
  #tree-container {
    height: 78vh;
  }

  .modal-content {
    padding: 24px 20px;
    width: min(540px, 94vw);
  }

  .modal-avatar img {
    width: 120px;
    height: 120px;
  }

  .modal-info h3 {
    font-size: 22px;
  }

  .modal-info p {
    font-size: 15px;
  }
}
