/* ---------- Tokens ---------- */
:root {
  --bg:        #f5f2ec;
  --bg-alt:    #ece8df;
  --paper:     #ffffff;
  --ink:       #1a1815;
  --ink-soft:  #5a564f;
  --ink-mute:  #8e8a82;
  --line:      #d9d3c5;
  --line-soft: #ece7da;
  --accent:    #4f6b54;        /* muted sage */
  --accent-ink:#2f4435;
  --danger:    #a3503b;
  --shadow-sm: 0 1px 2px rgba(40, 35, 25, 0.04), 0 1px 1px rgba(40, 35, 25, 0.03);
  --shadow-md: 0 2px 6px rgba(40, 35, 25, 0.05), 0 8px 24px rgba(40, 35, 25, 0.06);
  --shadow-lg: 0 12px 40px rgba(40, 35, 25, 0.12);
  --radius-sm: 6px;
  --radius:    10px;
  --radius-lg: 18px;
  --font-sans: 'Geist', 'Inter', system-ui, -apple-system, sans-serif;
  --font-serif:'Newsreader', 'Source Serif 4', Georgia, serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--ink);
  font-feature-settings: "ss01" on, "cv01" on;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; color: inherit; }

/* ---------- Gate ---------- */
.gate {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background:
    radial-gradient(1200px 600px at 70% 110%, rgba(79, 107, 84, 0.12), transparent 60%),
    radial-gradient(900px 500px at 10% -10%, rgba(201, 184, 154, 0.30), transparent 60%),
    var(--bg);
}
.gate-card {
  width: min(460px, calc(100vw - 32px));
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 38px 40px 32px;
  box-shadow: var(--shadow-md);
}
.gate-mark {
  width: 48px; height: 48px;
  margin: 0 auto 18px;
  display: grid; place-items: center;
  color: var(--accent);
}
.gate-mark svg { width: 100%; height: 100%; }
.gate-eyebrow {
  text-align: center;
  font-size: 11px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--ink-mute);
  margin: 0 0 8px;
}
.gate-title {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 38px;
  letter-spacing: -0.015em;
  text-align: center;
  margin: 0 0 16px;
  line-height: 1.05;
}
.gate-pitch {
  font-family: var(--font-serif);
  font-size: 15.5px;
  line-height: 1.55;
  color: var(--ink-soft);
  text-align: left;
  margin: 0 0 24px;
  text-wrap: pretty;
}
.gate-sub {
  text-align: center;
  color: var(--ink-soft);
  font-size: 14px;
  margin: 0 0 28px;
}
.gate-field { margin-bottom: 14px; }
.gate-label {
  display: block;
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-mute);
  margin-bottom: 6px;
}
.field-input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--line);
  background: #fafaf7;
  border-radius: var(--radius);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
.field-input:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(79, 107, 84, 0.12);
}
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 8px;
  padding: 11px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: #fafaf7; }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
.btn-block { width: 100%; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--line-soft); }
.btn-danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn-danger:hover { background: rgba(163, 80, 59, 0.08); }
.btn-icon {
  width: 32px; height: 32px; padding: 0;
  display: grid; place-items: center;
}
.gate-err {
  color: var(--danger);
  font-size: 13px;
  margin-top: 6px;
  min-height: 18px;
}
.gate-hint {
  font-size: 12.5px;
  color: var(--ink-mute);
  text-align: center;
  margin-top: 22px;
  font-style: italic;
  line-height: 1.5;
  text-wrap: pretty;
}

/* ---------- Preview banner ---------- */
.preview-banner {
  background: #fdf3d8;
  border-bottom: 1px solid #ead9a8;
  color: #6b5a2d;
  padding: 8px 18px;
  font-size: 12.5px;
  text-align: center;
  line-height: 1.45;
}
.preview-banner strong { color: #4a3e1f; font-weight: 600; }

/* ---------- App chrome ---------- */
.app { position: fixed; inset: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 14px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(8px);
  z-index: 10;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  color: var(--ink);
}
.brand-mark {
  width: 24px; height: 24px;
  color: var(--accent);
}
.brand-name {
  font-family: var(--font-serif);
  font-size: 20px;
  letter-spacing: -0.005em;
}
.brand-dot {
  color: var(--ink-mute);
  margin: 0 2px;
}
.brand-tag {
  font-size: 12.5px;
  color: var(--ink-mute);
  font-style: italic;
}
.brand-sep {
  width: 1px; height: 22px;
  background: var(--line);
  margin: 0 4px;
}
.brand-fam {
  font-size: 14px;
  color: var(--ink-soft);
  font-weight: 500;
}
.topbar-spacer { flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }

/* ---------- Save indicator ---------- */
.save-indicator {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--ink-mute);
  padding: 6px 10px;
  margin-right: 4px;
}
.save-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--ink-mute);
}
.save-saving .save-dot { background: #c9a14a; animation: pulse 1.2s ease-in-out infinite; }
.save-saved  .save-dot { background: var(--accent); }
.save-error  .save-dot { background: var(--danger); }
.save-error  { color: var(--danger); }
@keyframes pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

.search-wrap {
  position: relative;
  width: 240px;
}
.search-wrap svg {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--ink-mute); width: 14px; height: 14px;
}
.search-wrap input {
  width: 100%;
  padding: 8px 12px 8px 32px;
  border: 1px solid var(--line);
  background: #fafaf7;
  border-radius: 999px;
  font-size: 13.5px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search-wrap input:focus {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(79, 107, 84, 0.10);
}
.search-results {
  position: absolute; top: calc(100% + 6px); left: 0;
  width: 320px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  max-height: 360px;
  overflow: auto;
  z-index: 20;
}
.search-result {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
}
.search-result:hover { background: var(--line-soft); }
.search-result + .search-result { border-top: 1px solid var(--line-soft); }
.search-empty {
  padding: 14px;
  color: var(--ink-mute);
  font-size: 13px;
  text-align: center;
}

/* ---------- Canvas ---------- */
.canvas-wrap {
  position: relative;
  flex: 1;
  overflow: hidden;
  background:
    radial-gradient(circle at 1px 1px, rgba(40, 35, 25, 0.06) 1px, transparent 0) 0 0 / 28px 28px,
    var(--bg);
  cursor: grab;
}
.canvas-wrap.grabbing { cursor: grabbing; }
.canvas {
  position: absolute; top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
}
.connector-layer {
  position: absolute; top: 0; left: 0;
  pointer-events: none;
  overflow: visible;
}
.connector {
  fill: none;
  stroke: #a89f88;
  stroke-width: 1.5;
  /* Keep stroke width visually constant when the canvas is zoomed out — */
  /* otherwise sub-pixel scaling makes lines fade or look broken.        */
  vector-effect: non-scaling-stroke;
}
.connector.couple {
  stroke: #8c8472;
  stroke-width: 2;
}
.connector.partial {
  stroke: #a89f88;
  stroke-dasharray: 5 4;
}
.divorce-x line {
  stroke: #8c8472;
  stroke-width: 2;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}

/* ---------- Card ---------- */
.card {
  position: absolute;
  width: var(--card-w, 168px);
  /* Match the layout-reserved slot height so connectors attach flush to */
  /* the card bottom instead of leaving a vertical gap underneath.       */
  min-height: var(--card-h, 200px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 14px 14px 12px;
  text-align: center;
  transition: border-color 0.12s, box-shadow 0.12s, transform 0.12s;
  cursor: pointer;
  user-select: none;
}
.card:hover {
  border-color: #b8af9a;
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}
.card.focused {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 107, 84, 0.14), var(--shadow-md);
}
.card.deceased { background: #faf8f3; }

.avatar {
  width: 76px; height: 76px;
  border-radius: 50%;
  background: var(--line-soft);
  display: grid; place-items: center;
  font-family: var(--font-serif);
  font-size: 28px;
  color: rgba(255,255,255,0.95);
  letter-spacing: 0.02em;
  overflow: hidden;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.04);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.deceased { filter: saturate(0.55); }

.card-name {
  font-family: var(--font-serif);
  font-size: 17px;
  line-height: 1.15;
  margin: 12px 0 0;
  letter-spacing: -0.005em;
  text-wrap: balance;
}
.card-nick {
  font-size: 12.5px;
  color: var(--ink-soft);
  font-style: italic;
  margin: 2px 0 0;
}
.card-dates {
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--ink-mute);
  margin: 8px 0 0;
  text-transform: uppercase;
}

/* density variants */
.card.compact { padding: 10px 10px 10px; --card-w: 144px; }
.card.compact .avatar { width: 56px; height: 56px; font-size: 22px; }
.card.compact .card-name { font-size: 15px; margin-top: 8px; }
.card.compact .card-nick { font-size: 11.5px; }
.card.compact .card-dates { font-size: 10.5px; margin-top: 4px; }

.card.cozy { padding: 16px 14px 14px; }

/* photo-forward variant */
.card.photo-forward { padding: 0; overflow: hidden; }
.card.photo-forward .avatar {
  width: 100%; height: 168px;
  border-radius: 0;
  border: none;
  font-size: 48px;
}
.card.photo-forward .card-body {
  padding: 12px 14px 14px;
  width: 100%;
  text-align: center;
}
.card.photo-forward .card-name { margin-top: 0; }

/* ---------- Zoom controls ---------- */
.zoom-controls {
  position: absolute;
  right: 18px; bottom: 18px;
  display: flex; flex-direction: column;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  z-index: 5;
}
.zoom-controls button {
  width: 36px; height: 36px;
  border: none; background: var(--paper);
  display: grid; place-items: center;
  color: var(--ink-soft);
}
.zoom-controls button:hover { background: var(--line-soft); color: var(--ink); }
.zoom-controls button + button { border-top: 1px solid var(--line); }

.add-fab {
  position: absolute;
  right: 18px; bottom: 154px;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  display: grid; place-items: center;
  box-shadow: var(--shadow-md);
  z-index: 5;
  transition: background 0.15s, transform 0.05s;
}
.add-fab:hover { background: var(--accent-ink); }
.add-fab:active { transform: translateY(1px); }

/* ---------- Side panel ---------- */
.panel-backdrop {
  position: absolute; inset: 0;
  background: rgba(20, 18, 14, 0.18);
  z-index: 30;
  animation: fade-in 0.15s ease both;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.panel {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: min(420px, 100vw);
  background: var(--paper);
  border-left: 1px solid var(--line);
  z-index: 31;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg);
  animation: slide-in 0.22s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}
@keyframes slide-in {
  from { transform: translateX(40px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
.panel-header {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}
.panel-title {
  font-family: var(--font-serif);
  font-size: 19px;
  margin: 0;
  flex: 1;
}
.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 22px 24px 28px;
}
.panel-footer {
  padding: 14px 16px;
  border-top: 1px solid var(--line);
  display: flex; gap: 8px; justify-content: flex-end;
}

.detail-hero {
  display: flex; flex-direction: column; align-items: center;
  padding: 4px 0 18px;
  border-bottom: 1px solid var(--line-soft);
  margin-bottom: 20px;
  gap: 4px;
}
.detail-hero .avatar { width: 120px; height: 120px; font-size: 42px; margin-bottom: 10px; }
.detail-hero h2 {
  font-family: var(--font-serif);
  font-size: 28px;
  line-height: 1.15;
  margin: 0;
  letter-spacing: -0.01em;
  text-align: center;
  text-wrap: balance;
  max-width: 100%;
  word-break: break-word;
}
.detail-hero .nick {
  font-style: italic;
  color: var(--ink-soft);
  font-size: 15px;
  line-height: 1.4;
  margin: 0;
  text-align: center;
}
.detail-hero .dates {
  font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--ink-mute);
  line-height: 1.4;
  margin: 8px 0 0;
}

.detail-row {
  display: flex; align-items: baseline; gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line-soft);
}
.detail-row:last-child { border-bottom: none; }
.detail-label {
  flex-shrink: 0;
  width: 90px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-mute);
}
.detail-value {
  flex: 1;
  font-size: 14.5px;
  color: var(--ink);
  word-break: break-word;
}
.detail-value.muted { color: var(--ink-mute); }
.relations-list {
  display: flex; flex-wrap: wrap; gap: 6px;
}
.relation-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px 4px 4px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
}
.relation-chip:hover { background: var(--line-soft); }
.relation-chip .avatar { width: 22px; height: 22px; font-size: 10px; }

.section-head {
  font-size: 11px;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  color: var(--ink-mute);
  margin: 24px 0 10px;
}
.section-head:first-child { margin-top: 0; }

.notes {
  font-family: var(--font-serif);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink);
  white-space: pre-wrap;
}

/* ---------- Edit form ---------- */
.form-grid { display: grid; gap: 14px; }
.form-row { display: grid; gap: 4px; }
.form-row label {
  font-size: 12px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-mute);
}
.form-row .field-input { padding: 9px 12px; font-size: 14px; }
textarea.field-input { min-height: 84px; resize: vertical; font-family: var(--font-serif); font-size: 15px; line-height: 1.5; }

.photo-edit {
  display: flex; align-items: center; gap: 14px;
  margin-bottom: 8px;
}
.photo-edit .avatar { width: 72px; height: 72px; font-size: 26px; }
.photo-edit-buttons { display: flex; flex-direction: column; gap: 6px; }
.photo-edit-buttons .btn { padding: 7px 12px; font-size: 13px; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.relation-picker {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.relation-tabs {
  display: flex;
  background: #fafaf7;
  border-bottom: 1px solid var(--line);
}
.relation-tab {
  flex: 1;
  padding: 9px 8px;
  background: none;
  border: none;
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  font-weight: 500;
  letter-spacing: 0.01em;
}
.relation-tab.active {
  background: var(--paper);
  color: var(--accent-ink);
  box-shadow: inset 0 -2px 0 var(--accent);
}
.relation-tab + .relation-tab { border-left: 1px solid var(--line); }
.relation-content { padding: 14px; }

.person-pick-list {
  max-height: 200px;
  overflow-y: auto;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}
.person-pick-row {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  font-size: 13.5px;
}
.person-pick-row:hover { background: var(--line-soft); }
.person-pick-row + .person-pick-row { border-top: 1px solid var(--line-soft); }
.person-pick-row .avatar { width: 26px; height: 26px; font-size: 12px; }
.person-pick-row.selected { background: rgba(79, 107, 84, 0.10); }

/* ---------- Tweaks panel ---------- */
.tweaks {
  position: absolute;
  right: 18px; top: 70px;
  width: 280px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  z-index: 8;
  overflow: hidden;
}
.tweaks-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line-soft);
  display: flex; align-items: center; gap: 8px;
}
.tweaks-title {
  font-size: 12px;
  letter-spacing: 0.10em;
  text-transform: uppercase;
  color: var(--ink-mute);
  flex: 1;
}
.tweaks-body { padding: 12px 14px 14px; }
.tweak-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0;
}
.tweak-row + .tweak-row { border-top: 1px solid var(--line-soft); }
.tweak-row label {
  flex: 1;
  font-size: 13px;
  color: var(--ink);
}
.tweak-segment {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #fafaf7;
}
.tweak-segment button {
  border: none; background: none;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--ink-soft);
  cursor: pointer;
}
.tweak-segment button.active {
  background: var(--paper);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--accent);
}
.tweak-segment button + button { border-left: 1px solid var(--line); }
.tweak-toggle {
  width: 32px; height: 18px;
  border-radius: 999px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  border: none;
  flex-shrink: 0;
}
.tweak-toggle::after {
  content: '';
  position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}
.tweak-toggle.on { background: var(--accent); }
.tweak-toggle.on::after { transform: translateX(14px); }

/* ---------- Menus ---------- */
.menu {
  position: absolute;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 6px;
  z-index: 40;
  min-width: 200px;
}
.menu-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: none; border: none;
  width: 100%;
  text-align: left;
  font-size: 13.5px;
  color: var(--ink);
  cursor: pointer;
}
.menu-item:hover { background: var(--line-soft); }
.menu-item.danger { color: var(--danger); }
.menu-item.danger:hover { background: rgba(163, 80, 59, 0.08); }
.menu-sep { height: 1px; background: var(--line-soft); margin: 4px 6px; }

/* ---------- Misc ---------- */
.empty-state {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  text-align: center;
  pointer-events: none;
}
.empty-state-inner { pointer-events: auto; max-width: 360px; padding: 32px; }
.empty-state h2 {
  font-family: var(--font-serif);
  font-size: 26px;
  margin: 0 0 8px;
}
.empty-state p {
  color: var(--ink-soft);
  margin: 0 0 18px;
  line-height: 1.5;
}

/* hide scrollbars subtly */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: #c5bdab; }
::-webkit-scrollbar-track { background: transparent; }
