:root {
  --ink: #0b1d2c;
  --muted: #4a5b6f;
  --accent: #0b4f8a;
  --accent-2: #1ea7c6;
  --bg: #eef2f7;
  --surface: #ffffff;
  --border: #d6dde6;
  --shadow: 0 18px 40px rgba(11, 31, 52, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: "Manrope", "Sora", sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at top, rgba(0, 161, 199, 0.08), transparent 55%), var(--bg);
  overflow: hidden;
}

.page {
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: linear-gradient(120deg, #002a52 0%, #00498c 60%, #00a1c7 100%);
  color: #ffffff;
  box-shadow: var(--shadow);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 12px 10px;
  width: 100%;
  max-width: none;
  margin: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand img {
  height: 42px;
}

.brand-copy {
  display: flex;
  flex-direction: column;
}

.brand-title {
  font-weight: 700;
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.brand-subtitle {
  color: rgba(255, 255, 255, 0.82);
  font-size: 0.82rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

.topbar .ghost {
  border: 1px solid rgba(255, 255, 255, 0.4);
  background: rgba(255, 255, 255, 0.16);
  color: #ffffff;
}

.topbar-reset {
  padding: 5px 10px;
  font-size: 0.76rem;
  border-radius: 10px;
}

.topbar .grid-metric-switch {
  border-color: rgba(255, 255, 255, 0.38);
  background: rgba(255, 255, 255, 0.12);
}

.topbar .metric-btn {
  color: rgba(255, 255, 255, 0.9);
}

.topbar .metric-btn + .metric-btn {
  border-left-color: rgba(255, 255, 255, 0.22);
}

.topbar .scale-mode-switch {
  border-color: rgba(255, 255, 255, 0.38);
  background: rgba(255, 255, 255, 0.12);
}

.topbar .mode-btn {
  color: rgba(255, 255, 255, 0.9);
}

.topbar .mode-btn + .mode-btn {
  border-left-color: rgba(255, 255, 255, 0.22);
}

.topbar .cta {
  background: #ffffff;
  color: #003a70;
}

.app {
  display: grid;
  grid-template-columns: 378px 1fr;
  flex: 1;
  min-height: 0;
}

.panel {
  background: var(--surface);
  padding: 24px 22px;
  border-right: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 22px;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -ms-overflow-style: none;
  scrollbar-width: none;
}

.panel::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}

.panel-header h1 {
  margin: 6px 0 4px 0;
  font-size: 1.8rem;
}

.eyebrow {
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  font-size: 0.7rem;
  color: var(--muted);
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.panel-actions {
  margin-top: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.grid-metric-switch {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: #f7f9fc;
}

.metric-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 8px 10px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  min-width: 90px;
  white-space: nowrap;
}

.metric-btn + .metric-btn {
  border-left: 1px solid var(--border);
}

.metric-btn.is-active {
  color: #ffffff;
}

.metric-btn[data-grid-metric="homes"].is-active {
  background: #1662a8;
}

.metric-btn[data-grid-metric="schools"].is-active {
  background: #ba1f3d;
}

.metric-btn[data-grid-metric="workplaces"].is-active {
  background: #187a3f;
}

.metric-btn[data-grid-metric="holiday_houses"].is-active {
  background: #6b3bb6;
}

.metric-btn[data-grid-metric="none"].is-active {
  background: #3f556e;
}

.scale-mode-switch {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: #f7f9fc;
}

.mode-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 8px 12px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  min-width: 68px;
  white-space: nowrap;
}

.mode-btn + .mode-btn {
  border-left: 1px solid var(--border);
}

.mode-btn.is-active {
  color: #ffffff;
  background: #0f5f9f;
}

.panel-section h2 {
  margin: 0 0 10px 0;
  font-size: 1.05rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--muted);
}

.field input {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 0.92rem;
}

.cta,
.ghost {
  border: none;
  padding: 10px 14px;
  border-radius: 999px;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.cta {
  background: var(--accent);
  color: #ffffff;
}

.ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}

.summary {
  background: #f7f9fc;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  color: var(--muted);
  font-size: 0.9rem;
  min-height: 80px;
}

.detail-loading {
  margin-top: 10px;
  display: none;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.83rem;
}

.detail-loading.is-visible {
  display: inline-flex;
}

.spinner-sm {
  width: 14px;
  height: 14px;
  border-width: 2px;
}

.list {
  display: grid;
  gap: 10px;
  max-height: none;
  overflow: visible;
  padding-right: 6px;
}

.list-item {
  border: 1px solid var(--border);
  background: #ffffff;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.88rem;
  position: relative;
}

.list-item button {
  margin-top: 8px;
  border: none;
  background: var(--accent-2);
  color: #ffffff;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  cursor: pointer;
}

.has-table-action {
  padding-right: 44px;
}

.has-dual-action {
  padding-right: 76px;
}

.list-item .item-table-action {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  margin: 0;
  padding: 0;
  border-radius: 6px;
  border: 1px solid #b7c4d4;
  background: #f5f9ff;
  color: #36597d;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  font-size: 0.72rem;
  cursor: pointer;
}

.list-item .item-table-action:hover {
  background: #e8f1ff;
  border-color: #8fa8c3;
}

.list-item .item-calendar-action {
  position: absolute;
  top: 8px;
  right: 40px;
  width: 24px;
  height: 24px;
  margin: 0;
  padding: 0;
  border-radius: 6px;
  border: 1px solid #b7c4d4;
  background: #f5f9ff;
  color: #36597d;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  cursor: pointer;
}

.list-item .item-calendar-action:hover {
  background: #e8f1ff;
  border-color: #8fa8c3;
}

.calendar-chip {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: linear-gradient(180deg, #d8e8fb 0%, #f5f9ff 45%);
  border: 1px solid #9ab2cc;
  color: #274967;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.57rem;
  font-weight: 800;
}

.list-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.list-actions .ghost {
  border: 1px solid var(--border);
  color: var(--accent);
  background: transparent;
}

.tree-building {
  background: #f3f8ff;
  cursor: pointer;
}

.tree-grid {
  background: #f5f9ff;
  cursor: pointer;
}

.grid-summary-list {
  margin: 8px 0 0 18px;
  padding: 0;
  display: grid;
  gap: 2px;
  color: #2f4358;
  font-size: 0.84rem;
}

.tree-household {
  margin-bottom: 0;
  background: #ffffff;
  padding: 8px 10px;
  cursor: pointer;
}

.tree-household-title {
  font-size: 0.85rem;
  margin-bottom: 8px;
  font-weight: 600;
}

.tree-household-line-primary {
  font-weight: 700;
  color: #1e2f40;
}

.tree-household-line-secondary {
  margin-top: 2px;
  font-weight: 500;
  color: #4b5f75;
}

.tree-people {
  margin: 0;
  padding-left: 18px;
  display: grid;
  gap: 4px;
  font-size: 0.82rem;
}

.tree-person {
  border-radius: 8px;
  padding: 2px 2px;
  list-style: none;
}

.tree-person:hover {
  background: transparent;
}

.tree-person-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  padding: 6px 8px;
  cursor: pointer;
}

.tree-person-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.tree-person-summary {
  font-size: 0.82rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.tree-person-card .person-table-action {
  position: static;
  width: 22px;
  height: 22px;
  margin: 0;
  padding: 0;
  border-radius: 6px;
  border: 1px solid #b7c4d4;
  background: #f5f9ff;
  color: #36597d;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  font-size: 0.7rem;
  flex: 0 0 22px;
}

.tree-person-card .person-table-action:hover {
  background: #e8f1ff;
  border-color: #8fa8c3;
}

.tree-person-actions {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.tree-person-card .person-calendar-action {
  position: static;
  width: 22px;
  height: 22px;
  margin: 0;
  padding: 0;
  border-radius: 6px;
  border: 1px solid #b7c4d4;
  background: #f5f9ff;
  color: #36597d;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  font-size: 0.7rem;
  flex: 0 0 22px;
}

.tree-person-card .person-calendar-action:hover {
  background: #e8f1ff;
  border-color: #8fa8c3;
}

.tree-person-meta {
  margin: 4px 0 6px;
  color: var(--muted);
  font-size: 0.78rem;
}

.person-sex-icon {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
}

.person-sex-icon.is-man {
  color: #1f5f96;
  background: #deecfa;
}

.person-sex-icon.is-woman {
  color: #9b3f53;
  background: #f8e2e8;
}

.person-sex-icon.is-unknown {
  color: #5e6b79;
  background: #e8edf3;
}

.person-age {
  color: var(--text);
  font-weight: 700;
}

.person-id {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 500;
}

.person-link-label {
  --line-color: #6b7280;
  --label-bg: rgba(107, 114, 128, 0.2);
  color: var(--line-color);
  background: var(--label-bg);
  border: 1.5px solid var(--line-color);
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 0.76rem;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(11, 31, 52, 0.12);
  pointer-events: none;
  z-index: 3;
}

.tree-person-table {
  max-height: 220px;
  overflow: auto;
}

.tree-empty {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.panel-footer {
  margin-top: auto;
  font-size: 0.85rem;
  color: var(--muted);
}

.panel-footer ul {
  margin: 6px 0 10px 18px;
  padding: 0;
}

.tiny {
  font-size: 0.75rem;
}

.map-wrap {
  position: relative;
  height: 100%;
}

#map {
  width: 100%;
  height: 100%;
}

.tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(11, 31, 52, 0.92);
  color: #ffffff;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 0.78rem;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.tooltip.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.legend {
  position: absolute;
  right: 20px;
  bottom: 20px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  font-size: 0.8rem;
}

.legend-title {
  font-weight: 700;
  margin-bottom: 8px;
}

.legend-scale {
  display: flex;
  align-items: center;
  gap: 8px;
}

.scale-bar {
  flex: 1;
  height: 8px;
  border-radius: 6px;
  background: linear-gradient(90deg, #dbe9f7, #1ea7c6, #0b4f8a);
}

.legend-note {
  margin-top: 8px;
  color: var(--muted);
}

.loading-indicator {
  position: absolute;
  top: 20px;
  left: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 12px;
  box-shadow: var(--shadow);
  font-size: 0.85rem;
  color: var(--muted);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.loading-indicator.is-visible {
  opacity: 1;
}

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

.loading-text {
  max-width: 260px;
}

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

body.modal-open {
  overflow: hidden;
}

body.modal-open .person-link-label {
  display: none !important;
}

.summary-actions {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.detail-modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 6000;
}

.detail-modal.is-visible {
  display: flex;
}

.detail-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(11, 31, 52, 0.6);
}

.detail-content {
  position: relative;
  background: var(--surface);
  width: min(980px, 94vw);
  max-height: calc(100vh - 28px);
  overflow: auto;
  overscroll-behavior: contain;
  padding: 24px;
  border-radius: 18px;
  box-shadow: var(--shadow);
  margin: 14px;
}

.detail-close {
  position: absolute;
  top: 16px;
  right: 18px;
  border: none;
  background: #edf3fb;
  color: var(--ink);
  font-size: 1.4rem;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  cursor: pointer;
}

.detail-section {
  margin-bottom: 22px;
}

.detail-section h3 {
  margin: 0 0 12px 0;
  font-size: 1.1rem;
}

.detail-section h4 {
  margin: 12px 0 8px 0;
  font-size: 0.95rem;
  color: var(--muted);
}

.detail-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.detail-table th,
.detail-table td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.detail-table th {
  width: 34%;
  color: var(--muted);
  font-weight: 600;
}

.detail-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 12px;
  background: #f7f9fc;
}

.detail-card summary {
  cursor: pointer;
  font-weight: 600;
  margin-bottom: 10px;
  list-style: none;
}

.detail-card summary::-webkit-details-marker {
  display: none;
}

.detail-subsection {
  margin-top: 12px;
}

.person-week-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
  table-layout: fixed;
}

.person-week-table th,
.person-week-table td {
  text-align: left;
  padding: 6px 7px;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid #e5ebf2;
  vertical-align: top;
}

.person-week-table th:last-child,
.person-week-table td:last-child {
  border-right: none;
}

.person-week-table thead th {
  background: #f5f8fc;
  font-weight: 700;
  color: #324b64;
}

.person-week-table th:first-child,
.person-week-table td:first-child {
  width: 145px;
}

.person-week-day-cell {
  min-width: 130px;
}

.person-week-day-name {
  font-weight: 700;
  margin-bottom: 4px;
}

.person-week-flags {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.week-flag {
  width: 18px;
  height: 18px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.64rem;
  font-weight: 800;
  border: 1px solid;
}

.week-flag.is-active {
  color: #9e1d2f;
  background: #fbe4e8;
  border-color: #de9ca9;
}

.week-flag.is-inactive {
  color: #5f6d7b;
  background: #eef2f6;
  border-color: #c7d1db;
}

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

  .topbar-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .topbar-actions {
    flex-wrap: wrap;
  }

  .panel {
    position: relative;
    height: auto;
    overflow: visible;
  }

  #map {
    height: 70vh;
  }
}
