/* =============================================
   Cây Gia Phả - Main Stylesheet (Mobile-first)
   Obsidian-inspired dark theme
   ============================================= */

@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,600;0,700;1,400&family=Inter:wght@300;400;500;600&display=swap');

/* ---- CSS Variables ---- */
:root {
  --bg-primary: #0d0d14;
  --bg-secondary: #12121f;
  --bg-panel: rgba(22, 22, 38, 0.96);
  --bg-card: rgba(30, 30, 50, 0.85);
  --accent: #7c6af7;
  --accent-glow: rgba(124, 106, 247, 0.4);
  --accent-light: #a89bff;
  --gold: #e2b96f;
  --gold-glow: rgba(226, 185, 111, 0.3);
  --female-color: #f472b6;
  --female-glow: rgba(244, 114, 182, 0.35);
  --male-color: #60a5fa;
  --male-glow: rgba(96, 165, 250, 0.35);
  --text-primary: #e8e6f0;
  --text-secondary: #8b89a0;
  --text-muted: #5a5870;
  --border: rgba(124, 106, 247, 0.2);
  --border-hover: rgba(124, 106, 247, 0.5);
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  --shadow-glow: 0 0 30px rgba(124, 106, 247, 0.2);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --header-h: 52px;
  --bottom-bar-h: 60px;
}

/* ---- Reset & Base ---- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  /* Prevent overscroll bounce on iOS */
  overscroll-behavior: none;
}

/* ---- Animated Background ---- */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 50% at 20% 20%, rgba(124, 106, 247, 0.08) 0%, transparent 60%),
    radial-gradient(ellipse 60% 40% at 80% 80%, rgba(226, 185, 111, 0.05) 0%, transparent 60%),
    radial-gradient(ellipse 40% 40% at 50% 50%, rgba(96, 165, 250, 0.04) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

/* =============================================
   HEADER
   ============================================= */
#header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-h);
  background: rgba(13, 13, 20, 0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  z-index: 100;
  box-shadow: 0 1px 0 rgba(124, 106, 247, 0.1), var(--shadow);
}

#header .logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

#header .logo-icon {
  width: 30px;
  height: 30px;
  background: linear-gradient(135deg, var(--accent), var(--gold));
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  box-shadow: 0 0 14px var(--accent-glow);
  flex-shrink: 0;
}

#header .logo-text {
  font-family: 'Playfair Display', serif;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #e8e6f0 0%, var(--accent-light) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  white-space: nowrap;
}

#header .header-subtitle {
  display: none;
  /* hidden on mobile */
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-family: 'Inter', sans-serif;
  font-size: 13px;
  cursor: pointer;
  transition: var(--transition);
  text-decoration: none;
  white-space: nowrap;
  /* Good tap target size */
  min-height: 38px;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover,
.btn:active {
  background: rgba(124, 106, 247, 0.15);
  border-color: var(--border-hover);
  color: var(--text-primary);
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #5b4de0);
  border-color: transparent;
  color: #fff;
  font-weight: 500;
}

.btn-primary:hover,
.btn-primary:active {
  background: linear-gradient(135deg, var(--accent-light), var(--accent));
  box-shadow: 0 4px 20px var(--accent-glow);
  color: #fff;
}

/* ---- Icon-only btn on mobile ---- */
.btn-icon-only .btn-label {
  display: none;
}

/* =============================================
   TREE CANVAS
   ============================================= */
#tree-container {
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: 0;
  bottom: var(--bottom-bar-h);
  overflow: hidden;
  cursor: grab;
  z-index: 1;
  /* Smooth momentum on iOS */
  -webkit-overflow-scrolling: touch;
}

#tree-container.panning {
  cursor: grabbing;
}

#tree-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  /* prevent browser scroll interference */
}

/* =============================================
   SVG NODE STYLES
   ============================================= */
.node-group {
  cursor: pointer;
}

.node-ring {
  fill: none;
  stroke-width: 2;
  transition: all 0.3s ease;
  opacity: 0;
}

.node-group:hover .node-ring,
.node-group.active .node-ring {
  opacity: 1;
  animation: ring-pulse 2s infinite;
}

@keyframes ring-pulse {

  0%,
  100% {
    opacity: 0.5;
  }

  50% {
    opacity: 1;
  }
}

.node-circle {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  filter: drop-shadow(0 0 8px rgba(124, 106, 247, 0.3));
}

.node-group:hover .node-circle,
.node-group.active .node-circle {
  filter: drop-shadow(0 0 20px var(--accent-glow));
}

.node-group.male:hover .node-circle,
.node-group.male.active .node-circle {
  filter: drop-shadow(0 0 20px var(--male-glow));
}

.node-group.female:hover .node-circle,
.node-group.female.active .node-circle {
  filter: drop-shadow(0 0 20px var(--female-glow));
}

.node-name {
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  font-weight: 600;
  fill: var(--text-primary);
  text-anchor: middle;
  pointer-events: none;
  dominant-baseline: middle;
}

.node-year {
  font-family: 'Inter', sans-serif;
  font-size: 9px;
  fill: var(--text-muted);
  text-anchor: middle;
  pointer-events: none;
}

/* SVG edge lines */
.edge-line {
  fill: none;
  stroke-width: 1.5;
  stroke: rgba(124, 106, 247, 0.25);
  stroke-dasharray: 5, 5;
  animation: dash-flow 20s linear infinite;
}

.edge-line.spouse {
  stroke: rgba(226, 185, 111, 0.3);
  stroke-dasharray: 3, 6;
}

@keyframes dash-flow {
  to {
    stroke-dashoffset: -200;
  }
}

/* =============================================
   BOTTOM CONTROL BAR (mobile-first)
   ============================================= */
#controls {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--bottom-bar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  background: rgba(13, 13, 20, 0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  z-index: 100;
  padding: 0 8px;
  padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -1px 0 rgba(124, 106, 247, 0.1);
}

.ctrl-btn {
  flex: 1;
  max-width: 64px;
  height: 44px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.ctrl-btn:hover,
.ctrl-btn:active {
  background: rgba(124, 106, 247, 0.15);
  color: var(--accent-light);
}

.ctrl-divider {
  width: 1px;
  height: 22px;
  background: var(--border);
  flex-shrink: 0;
  margin: 0 4px;
}

#zoom-level {
  font-size: 11px;
  color: var(--text-muted);
  min-width: 38px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* =============================================
   DETAIL PANEL — Bottom sheet on mobile
   ============================================= */
#detail-panel {
  position: fixed;
  /* Mobile: slides up from bottom */
  left: 0;
  right: 0;
  bottom: var(--bottom-bar-h);
  height: 65vh;
  max-height: calc(100vh - var(--header-h) - var(--bottom-bar-h));
  background: var(--bg-panel);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  z-index: 90;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

#detail-panel.open {
  transform: translateY(0);
}

/* Drag handle on mobile */
#detail-panel::before {
  content: '';
  display: block;
  width: 40px;
  height: 4px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 4px;
  margin: 10px auto 0;
  flex-shrink: 0;
}

.panel-header {
  padding: 12px 16px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.panel-avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 700;
  color: #fff;
  border: 2.5px solid rgba(255, 255, 255, 0.15);
  overflow: hidden;
  background: linear-gradient(135deg, var(--accent), #5b4de0);
  box-shadow: 0 0 0 3px var(--accent-glow), 0 4px 20px rgba(0, 0, 0, 0.4);
  position: relative;
}

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

.panel-avatar span {
  font-family: 'Playfair Display', serif;
  font-size: 28px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.95);
  line-height: 1;
  user-select: none;
}

.panel-avatar.female {
  background: linear-gradient(135deg, var(--female-color), #c026d3);
  box-shadow: 0 0 0 3px var(--female-glow), 0 4px 20px rgba(0, 0, 0, 0.4);
}

.panel-avatar.male {
  box-shadow: 0 0 0 3px var(--male-glow), 0 4px 20px rgba(0, 0, 0, 0.4);
  background: linear-gradient(135deg, var(--male-color), #3b82f6);
}

.panel-info {
  flex: 1;
  min-width: 0;
}

.panel-name {
  font-family: 'Playfair Display', serif;
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 2px;
  line-height: 1.3;
}

.panel-dates {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 5px;
}

.panel-gender-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 20px;
  font-weight: 500;
}

.panel-gender-badge.male {
  background: rgba(96, 165, 250, 0.15);
  color: var(--male-color);
  border: 1px solid rgba(96, 165, 250, 0.3);
}

.panel-gender-badge.female {
  background: rgba(244, 114, 182, 0.15);
  color: var(--female-color);
  border: 1px solid rgba(244, 114, 182, 0.3);
}

.panel-close {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text-muted);
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
  flex-shrink: 0;
}

.panel-close:hover,
.panel-close:active {
  background: rgba(124, 106, 247, 0.15);
  color: var(--text-primary);
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  -webkit-overflow-scrolling: touch;
}

.panel-body::-webkit-scrollbar {
  width: 4px;
}

.panel-body::-webkit-scrollbar-track {
  background: transparent;
}

.panel-body::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

.info-section {
  margin-bottom: 18px;
}

.info-section-title {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--accent-light);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.info-section-title::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.bio-text {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.7;
  font-style: italic;
}

.relation-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.relation-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: var(--transition);
  /* Large tap target */
  min-height: 48px;
}

.relation-item:active {
  background: rgba(124, 106, 247, 0.08);
  border-color: var(--border-hover);
}

.relation-avatar-mini {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.relation-avatar-mini.male {
  background: linear-gradient(135deg, var(--male-color), #3b82f6);
}

.relation-avatar-mini.female {
  background: linear-gradient(135deg, var(--female-color), #c026d3);
}

.relation-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
}

.relation-label {
  font-size: 11px;
  color: var(--text-muted);
}

/* =============================================
   LEGEND — hide on mobile, show on tablet+
   ============================================= */
#legend {
  display: none;
  /* hidden on mobile */
}

/* =============================================
   SEARCH BAR — full width on mobile
   ============================================= */
#search-bar {
  position: fixed;
  top: var(--header-h);
  left: 0;
  right: 0;
  z-index: 90;
  padding: 8px 12px;
  display: none;
  background: rgba(13, 13, 20, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

#search-bar.open {
  display: block;
}

#search-input {
  width: 100%;
  padding: 11px 16px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  border-radius: 50px;
  color: var(--text-primary);
  font-family: 'Inter', sans-serif;
  font-size: 15px;
  /* larger for mobile readability */
  outline: none;
  transition: var(--transition);
}

#search-input:focus {
  border-color: var(--accent);
  background: rgba(124, 106, 247, 0.06);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

#search-input::placeholder {
  color: var(--text-muted);
}

/* =============================================
   LOADING SCREEN
   ============================================= */
#loading {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  transition: opacity 0.6s ease;
}

#loading.hidden {
  opacity: 0;
  pointer-events: none;
}

.load-icon {
  font-size: 52px;
  margin-bottom: 16px;
  animation: float 3s ease-in-out infinite;
}

.load-text {
  font-family: 'Playfair Display', serif;
  font-size: 24px;
  font-weight: 600;
  background: linear-gradient(135deg, var(--text-primary), var(--accent-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
  text-align: center;
  padding: 0 20px;
}

.load-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 28px;
  text-align: center;
}

.load-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes float {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-10px);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* =============================================
   TOAST
   ============================================= */
#toast {
  position: fixed;
  bottom: calc(var(--bottom-bar-h) + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 50px;
  padding: 10px 20px;
  font-size: 13px;
  color: var(--text-primary);
  z-index: 300;
  opacity: 0;
  transition: all 0.3s ease;
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow);
  pointer-events: none;
  white-space: nowrap;
  max-width: 90vw;
  text-overflow: ellipsis;
  overflow: hidden;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* =============================================
   STATS BADGE — shown inline in header on mobile
   ============================================= */
#stats-badge {
  display: none;
}

/* moved to header on mobile */

/* =============================================
   SCROLLBAR
   ============================================= */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* =============================================
   OVERLAY (when panel open on mobile)
   ============================================= */
#panel-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 85;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

#panel-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

/* =============================================
   TABLET (≥ 640px)
   ============================================= */
@media (min-width: 640px) {
  :root {
    --header-h: 56px;
    --bottom-bar-h: 0px;
  }

  #header {
    padding: 0 20px;
  }

  #header .logo-text {
    font-size: 18px;
  }

  #header .header-subtitle {
    display: block;
  }

  .btn {
    padding: 7px 14px;
  }

  .btn-icon-only .btn-label {
    display: inline;
  }

  /* Tree takes full height below header */
  #tree-container {
    bottom: 60px;
    /* space for floating controls */
  }

  /* Controls: floating pill in center */
  #controls {
    bottom: 8px;
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    width: auto;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 50px;
    padding: 6px 10px;
    box-shadow: var(--shadow), var(--shadow-glow);
    height: auto;
    gap: 4px;
  }

  .ctrl-btn {
    flex: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    font-size: 16px;
    max-width: none;
  }

  #zoom-level {
    min-width: 45px;
  }

  /* Side panel on tablet+ */
  #detail-panel {
    top: var(--header-h);
    right: 0;
    left: auto;
    bottom: 0;
    width: 310px;
    height: auto;
    max-height: none;
    border-top: none;
    border-left: 1px solid var(--border);
    border-radius: 0;
    transform: translateX(100%);
  }

  #detail-panel.open {
    transform: translateX(0);
  }

  #detail-panel::before {
    display: none;
  }

  .panel-header {
    padding: 18px 18px 14px;
  }

  .panel-avatar {
    width: 60px;
    height: 60px;
    font-size: 26px;
  }

  .panel-name {
    font-size: 17px;
  }

  .panel-body {
    padding: 16px 18px;
  }

  /* Legend visible on tablet+ */
  #legend {
    display: block;
    position: fixed;
    top: calc(var(--header-h) + 16px);
    left: 16px;
    background: var(--bg-panel);
    backdrop-filter: blur(16px);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 16px;
    z-index: 90;
    min-width: 140px;
    box-shadow: var(--shadow);
  }

  #legend h4 {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 10px;
  }

  .legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
    font-size: 12px;
    color: var(--text-secondary);
  }

  .legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
  }

  /* Search bar: centered pill */
  #search-bar {
    left: auto;
    right: auto;
    width: 300px;
    left: 50%;
    transform: translateX(-50%);
    padding: 0;
    background: transparent;
    backdrop-filter: none;
    border-bottom: none;
  }

  #search-input {
    padding: 10px 16px;
    font-size: 13px;
    background: var(--bg-panel);
    backdrop-filter: blur(16px);
    box-shadow: var(--shadow);
  }

  /* Stats badge */
  #stats-badge {
    display: flex;
    position: fixed;
    bottom: 74px;
    right: 16px;
    background: var(--bg-panel);
    backdrop-filter: blur(16px);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px 12px;
    font-size: 12px;
    color: var(--text-muted);
    z-index: 90;
    gap: 10px;
  }

  #stats-badge span {
    display: flex;
    align-items: center;
    gap: 4px;
  }

  /* Overlay not needed on tablet */
  #panel-overlay {
    display: none !important;
  }

  /* Toast above controls */
  #toast {
    bottom: 80px;
  }
}

/* =============================================
   DESKTOP (≥ 1024px)
   ============================================= */
@media (min-width: 1024px) {
  #detail-panel {
    width: 320px;
  }

  .ctrl-btn:hover {
    background: rgba(124, 106, 247, 0.2);
    color: var(--accent-light);
    transform: scale(1.1);
  }

  .btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px var(--accent-glow);
  }

  .relation-item:hover {
    background: rgba(124, 106, 247, 0.08);
    border-color: var(--border-hover);
  }
}