/* ==========================================================================
   Create Your Family Tree — styles.css
   Design direction: "the archival registry" — a genealogical record chart.
   Parchment ground, green record-ink, oxblood accents, index-card people,
   wedding-ring joints on couple lines, roman-numeral generation rail.
   Type: Fraunces (display) + Archivo (interface).
   ========================================================================== */

:root {
  --paper:       #F3EDDC;
  --paper-deep:  #EAE1C8;
  --card:        #FFFDF4;
  --ink:         #22312B;
  --ink-soft:    #5F6D60;
  --rule:        #C9BC9B;
  --rule-soft:   #DCD2B4;
  --oxblood:     #8C2F39;
  --pine:        #2E5944;
  --ochre:       #A8772E;
  --gold:        #C2A24B;
  --danger:      #9B3030;

  --f-display: "Fraunces", "Iowan Old Style", Georgia, serif;
  --f-ui: "Archivo", "Helvetica Neue", Arial, sans-serif;

  --shadow-card: 0 1px 2px rgba(34, 49, 43, .10), 0 8px 20px -10px rgba(34, 49, 43, .25);
  --shadow-pop:  0 2px 6px rgba(34, 49, 43, .12), 0 18px 44px -18px rgba(34, 49, 43, .38);

  --topbar-h: 66px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--f-ui);
  font-size: 15px;
  color: var(--ink);
  background: var(--paper);
  overflow: hidden;
}

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

:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ==========================================================================
   Top bar
   ========================================================================== */

.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 18px;
  background: var(--paper-deep);
  border-bottom: 1px solid var(--rule);
  box-shadow: inset 0 -4px 0 var(--paper), inset 0 -5px 0 var(--rule-soft);
  position: relative;
  z-index: 30;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }

.brand-mark {
  width: 34px; height: 34px; flex: none;
  fill: var(--pine);
}
.brand-mark .stroke { fill: none; stroke: var(--pine); stroke-width: 2; }

.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }

.brand-name {
  font-family: var(--f-display);
  font-weight: 700;
  font-size: 19px;
  letter-spacing: .01em;
  white-space: nowrap;
}

.brand-tag {
  font-family: var(--f-display);
  font-style: italic;
  font-weight: 500;
  font-size: 12.5px;
  color: var(--oxblood);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Search ---- */

.search {
  position: relative;
  display: flex;
  align-items: center;
  margin-left: auto;
  width: clamp(150px, 22vw, 270px);
}

.search > svg {
  position: absolute; left: 10px;
  width: 15px; height: 15px;
  fill: none; stroke: var(--ink-soft); stroke-width: 1.8;
  pointer-events: none;
}

.search input {
  width: 100%;
  padding: 8px 10px 8px 32px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: var(--card);
  font-size: 14px;
}
.search input::placeholder { color: var(--ink-soft); }

#searchResults {
  position: absolute;
  top: calc(100% + 6px);
  left: 0; right: 0;
  margin: 0; padding: 5px;
  list-style: none;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow-pop);
  max-height: 290px;
  overflow: auto;
  z-index: 60;
}

#searchResults li {
  padding: 7px 10px;
  border-radius: 7px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: baseline;
}
#searchResults li:hover, #searchResults li.active { background: var(--paper-deep); }
#searchResults li .sr-name { font-weight: 600; }
#searchResults li .sr-dates { font-size: 12px; color: var(--ink-soft); white-space: nowrap; }
#searchResults li.none { color: var(--ink-soft); cursor: default; }
#searchResults li.none:hover { background: none; }

/* ---- Toolbar buttons ---- */

.toolbar { display: flex; align-items: center; gap: 7px; }

.sep { width: 1px; height: 24px; background: var(--rule); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 13px;
  border-radius: 9px;
  border: 1px solid transparent;
  font-weight: 600;
  font-size: 13.5px;
  letter-spacing: .01em;
  background: none;
  transition: background-color .15s, border-color .15s, transform .08s;
}
.btn:active { transform: translateY(1px); }

.btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.btn.primary {
  background: var(--pine);
  color: var(--paper);
  border-color: var(--pine);
}
.btn.primary:hover { background: #254837; }

.btn.ghost {
  border-color: var(--rule);
  background: var(--card);
}
.btn.ghost:hover { background: var(--paper); border-color: var(--ink-soft); }

.btn.danger { background: var(--danger); border-color: var(--danger); color: #FFF7EE; }
.btn.danger:hover { background: #7E2424; }

.btn.link {
  background: none; border: none;
  color: var(--oxblood);
  text-decoration: underline;
  text-underline-offset: 3px;
  font-weight: 600;
}

.btn.big { padding: 12px 22px; font-size: 15px; }
.btn.small { padding: 6px 10px; font-size: 12.5px; }

.danger-text { color: var(--danger); }

.iconbtn {
  width: 30px; height: 30px;
  display: inline-flex; align-items: center; justify-content: center;
  border: none; background: none; border-radius: 7px;
}
.iconbtn:hover { background: var(--paper-deep); }
.iconbtn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

/* ==========================================================================
   Chart viewport — the registry sheet
   ========================================================================== */

main {
  position: relative;
  height: calc(100vh - var(--topbar-h));
  height: calc(100dvh - var(--topbar-h));
}

#viewport {
  position: absolute;
  inset: 14px;
  overflow: hidden;
  background:
    radial-gradient(rgba(95, 109, 96, .16) 1px, transparent 1.5px) 0 0 / 30px 30px,
    var(--paper);
  border: 1px solid var(--rule);
  outline: 1px solid var(--rule);
  outline-offset: 4px;             /* certificate double rule */
  border-radius: 3px;
  cursor: grab;
  touch-action: none;
}
#viewport.dragging { cursor: grabbing; }

#canvas {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

#canvas > svg { position: absolute; top: 0; left: 0; overflow: visible; }

/* ---- Connector lines ---- */

.l-couple { stroke: var(--ink); stroke-width: 1.7; opacity: .8; }
.l-couple.elbow { stroke-dasharray: 4 4; opacity: .55; fill: none; }
.l-desc { stroke: var(--ink-soft); stroke-width: 1.5; opacity: .85; }
.ring { fill: var(--paper); stroke: var(--oxblood); stroke-width: 1.7; }

/* ---- Generation rail ---- */

.gen-num {
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 21px;
  fill: var(--oxblood);
  opacity: .85;
}
.gen-tick { stroke: var(--oxblood); stroke-width: 1.4; opacity: .5; }
.gen-word {
  font-family: var(--f-ui);
  font-weight: 600;
  font-size: 10.5px;
  letter-spacing: .34em;
  fill: var(--ink-soft);
  opacity: .8;
}

/* ---- Person cards (index cards) ---- */

.card {
  position: absolute;
  width: 172px;
  height: 74px;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px 8px 12px;
  text-align: left;
  background:
    linear-gradient(to bottom, rgba(34,49,43,.035) 1px, transparent 1px) 0 18px / 100% 14px repeat-y,
    var(--card);
  border: 1px solid #D8CCA9;
  border-left-width: 4px;
  border-radius: 8px;
  box-shadow: var(--shadow-card);
  transition: box-shadow .15s, transform .15s, border-color .15s;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 3px 5px rgba(34,49,43,.12), 0 14px 26px -12px rgba(34,49,43,.32);
}
.card.selected {
  border-color: var(--gold);
  border-left-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(194, 162, 75, .35), var(--shadow-pop);
}

.card.g-f { border-left-color: var(--oxblood); }
.card.g-m { border-left-color: var(--pine); }
.card.g-o { border-left-color: var(--ochre); }

.avatar {
  flex: none;
  width: 44px; height: 44px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 16px;
  border: 1px solid rgba(34,49,43,.18);
}
.card.g-f .avatar { background: #F2DFDC; color: var(--oxblood); }
.card.g-m .avatar { background: #DCE8DC; color: var(--pine); }
.card.g-o .avatar { background: #F0E4CC; color: var(--ochre); }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.card .info { min-width: 0; display: flex; flex-direction: column; gap: 2px; }

.card .name {
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 14.5px;
  line-height: 1.22;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card .dates {
  font-size: 11.5px;
  color: var(--ink-soft);
  letter-spacing: .02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Empty state ---- */

#emptyState {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}

.empty-card {
  max-width: 460px;
  text-align: center;
  background: var(--card);
  border: 1px solid var(--rule);
  outline: 1px solid var(--rule-soft);
  outline-offset: 5px;
  border-radius: 6px;
  padding: 38px 36px 30px;
  box-shadow: var(--shadow-pop);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}

.empty-mark { width: 64px; height: 64px; fill: var(--pine); opacity: .9; margin-bottom: 4px; }
.empty-mark .stroke { fill: none; stroke: var(--pine); stroke-width: 3; }

.empty-card h2 {
  font-family: var(--f-display);
  font-weight: 700;
  font-size: 25px;
  line-height: 1.2;
  margin: 0;
}

.empty-card p { margin: 4px 0 14px; color: var(--ink-soft); line-height: 1.5; }

.empty-privacy { font-size: 12px; margin: 14px 0 0 !important; opacity: .8; }

/* ---- Floating chrome ---- */

.stats-chip {
  position: absolute;
  left: 14px; bottom: 14px;
  padding: 6px 12px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
  box-shadow: var(--shadow-card);
  pointer-events: none;
}

.zoomctrl {
  position: absolute;
  right: 14px; bottom: 14px;
  display: flex;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow-card);
  overflow: hidden;
}
.zoomctrl button {
  border: none; background: none;
  min-width: 36px; height: 36px;
  padding: 0 9px;
  font-weight: 600;
  font-size: 15px;
  display: inline-flex; align-items: center; justify-content: center;
}
.zoomctrl button + button { border-left: 1px solid var(--rule-soft); }
.zoomctrl button:hover { background: var(--paper); }
.zoomctrl svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
#zoomPct { font-size: 12px; min-width: 52px; color: var(--ink-soft); }

.hint {
  position: absolute;
  bottom: 14px; left: 50%;
  transform: translateX(-50%);
  margin: 0;
  font-size: 12px;
  color: var(--ink-soft);
  opacity: .85;
  pointer-events: none;
  white-space: nowrap;
}

/* ==========================================================================
   Person editor panel
   ========================================================================== */

#panel {
  position: absolute;
  top: 14px; right: 14px; bottom: 14px;
  width: 320px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 6px;
  box-shadow: var(--shadow-pop);
  display: flex;
  flex-direction: column;
  transform: translateX(calc(100% + 24px));
  transition: transform .22s ease;
  z-index: 20;
}
#panel.open { transform: translateX(0); }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 14px 11px 18px;
  border-bottom: 1px solid var(--rule-soft);
}
.panel-head h2 {
  margin: 0;
  font-family: var(--f-display);
  font-size: 17px;
  font-weight: 700;
}

.panel-scroll {
  overflow-y: auto;
  padding: 16px 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-photo { display: flex; align-items: center; gap: 14px; }

.photo-preview {
  width: 66px; height: 66px;
  flex: none;
  border-radius: 50%;
  overflow: hidden;
  border: 1px solid var(--rule);
  background: var(--paper-deep);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 22px;
  color: var(--ink-soft);
}
.photo-preview img { width: 100%; height: 100%; object-fit: cover; }

.photo-actions { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }

.kin {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--ink-soft);
  border-left: 3px solid var(--rule-soft);
  padding-left: 10px;
}
.kin:empty { display: none; }

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
  letter-spacing: .02em;
}
.field .optional { font-weight: 400; opacity: .75; }

.field input, .field select, .field textarea {
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--paper);
  padding: 8px 10px;
  font-size: 14px;
  font-weight: 400;
  color: var(--ink);
  width: 100%;
}
.field textarea { resize: vertical; min-height: 60px; }
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--gold);
  outline-offset: 0;
  border-color: var(--gold);
}

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.panel-sub {
  margin: 8px 0 0;
  font-family: var(--f-display);
  font-size: 15px;
  font-weight: 700;
}

.rel-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.relbtn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 11px;
  border: 1px solid var(--rule);
  border-radius: 9px;
  background: var(--paper);
  font-weight: 600;
  font-size: 13px;
  transition: background-color .15s, border-color .15s;
}
.relbtn:hover:not(:disabled) { background: var(--paper-deep); border-color: var(--ink-soft); }
.relbtn:disabled { opacity: .45; cursor: not-allowed; }
.relbtn svg { width: 16px; height: 16px; fill: none; stroke: var(--pine); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }

#btnDelete { margin-top: 6px; justify-content: center; }

/* ==========================================================================
   Dialogs & toast
   ========================================================================== */

dialog {
  border: 1px solid var(--rule);
  outline: 1px solid var(--rule-soft);
  outline-offset: 4px;
  border-radius: 6px;
  background: var(--card);
  color: var(--ink);
  padding: 24px 26px;
  width: min(92vw, 380px);
  box-shadow: var(--shadow-pop);
}
dialog::backdrop { background: rgba(34, 49, 43, .42); }

dialog h3 {
  margin: 0 0 4px;
  font-family: var(--f-display);
  font-size: 20px;
  font-weight: 700;
}

.dlg-sub { margin: 0 0 14px; font-size: 13px; color: var(--ink-soft); line-height: 1.45; }
.dlg-sub:empty { display: none; }

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

.dlg-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 8px 0 0;
  padding: 0;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 34px;
  transform: translate(-50%, 16px);
  background: var(--ink);
  color: var(--paper);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: var(--shadow-pop);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s, transform .25s;
  max-width: min(86vw, 540px);
  text-align: center;
  z-index: 90;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ==========================================================================
   Print — the keepsake chart
   ========================================================================== */

.print-header { display: none; }

@page { size: landscape; margin: 10mm; }

@media print {
  body { overflow: visible; background: #fff; }

  .topbar, #panel, .zoomctrl, .stats-chip, .hint, #emptyState, #toast, dialog { display: none !important; }

  .print-header {
    display: block;
    text-align: center;
    padding: 2mm 0 4mm;
    border-bottom: 1px solid var(--rule);
    margin-bottom: 5mm;
  }
  .print-header h2 {
    margin: 0;
    font-family: var(--f-display);
    font-weight: 700;
    font-size: 22pt;
    color: var(--ink);
  }
  .print-header p {
    margin: 2mm 0 0;
    font-size: 9pt;
    color: var(--ink-soft);
    font-family: var(--f-ui);
    letter-spacing: .06em;
  }

  main { height: auto; }

  #viewport {
    position: static;
    inset: auto;
    overflow: visible;
    border: none;
    outline: none;
    background: #fff;
    cursor: default;
  }

  .card {
    box-shadow: none;
    background: #fff;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

/* ==========================================================================
   Responsive & accessibility
   ========================================================================== */

@media (max-width: 1060px) {
  .brand-tag { display: none; }
  .toolbar .btn span { display: none; }
  .toolbar .btn { padding: 8px 10px; }
  .toolbar .btn svg { width: 17px; height: 17px; }
}

@media (max-width: 700px) {
  .topbar { gap: 10px; padding: 0 10px; }
  .brand-name { font-size: 16px; }
  .search { width: clamp(110px, 30vw, 200px); }
  .hint { display: none; }

  #viewport { inset: 8px; }

  #panel {
    top: auto; left: 8px; right: 8px; bottom: 8px;
    width: auto;
    max-height: 68%;
    transform: translateY(calc(100% + 20px));
  }
  #panel.open { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
