:root {
    --ink: #26215C;
    --ink-soft: #4A4770;
    --cream: #F1EFE8;
    --cream-deep: #E7E3D8;
    --paper: #FBFAF6;
    --line: #B9B6A9;
    --teal: #0F6E56;
    --teal-soft: #E1F5EE;
    --rust: #A85C32;
    --text: #2B2A28;
    --muted: #726F66;
  }

  * { box-sizing: border-box; }

  #root {
    font-family: Georgia, 'Iowan Old Style', 'Palatino Linotype', serif;
    color: var(--text);
    background: var(--paper);
    min-height: 100vh;
    padding: 0;
  }

  .app-header {
    background: var(--ink);
    color: var(--cream);
    padding: 22px 28px;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    border-bottom: 4px solid var(--teal);
  }

  .app-header h1 {
    margin: 0;
    font-size: 26px;
    font-weight: 400;
    letter-spacing: 0.02em;
  }

  .app-header .sub {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 12.5px;
    color: #C9C6E8;
    margin-top: 4px;
  }

  .toolbar {
    display: flex;
    gap: 10px;
    align-items: center;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  }

  .toolbar input[type="text"] {
    background: rgba(255,255,255,0.08);
    border: 1px solid rgba(255,255,255,0.25);
    color: var(--cream);
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 13px;
    width: 200px;
    font-family: inherit;
  }
  .toolbar input[type="text"]::placeholder { color: #B3AFD9; }

  .btn {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 13px;
    font-weight: 600;
    border: none;
    border-radius: 6px;
    padding: 9px 16px;
    cursor: pointer;
    transition: transform 0.08s ease, box-shadow 0.15s ease;
  }
  .btn:active { transform: translateY(1px); }

  .btn-primary { background: var(--teal); color: white; }
  .btn-primary:hover { box-shadow: 0 2px 10px rgba(15,110,86,0.4); }

  .btn-ghost {
    background: transparent;
    color: var(--cream);
    border: 1px solid rgba(255,255,255,0.3);
  }
  .btn-ghost:hover { background: rgba(255,255,255,0.08); }

  .status-bar {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 12px;
    color: var(--muted);
    padding: 8px 28px;
    background: var(--cream-deep);
    border-bottom: 1px solid var(--line);
  }

  .canvas-wrap {
    overflow: auto;
    padding: 40px 60px 100px 60px;
  }

  .tree-scene {
    position: relative;
    width: max-content;
    margin: 0 auto;
  }

  .root-note {
    max-width: 640px;
    margin: 0 auto 28px auto;
    text-align: center;
    font-style: italic;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.5;
  }

  /* ---- Root pair: Saen's card beside Kambhu's own box ---- */
  .root-pair {
    display: flex;
    gap: 50px;
    align-items: stretch;
    position: relative;
    margin-top: 0;
  }
  .root-pair::before {
    /* marriage line connecting Saen and Kambhu */
    content: '';
    position: absolute;
    top: 50%;
    left: 190px;
    width: 50px;
    height: 2px;
    background: var(--line);
    transform: translateY(-1px);
  }
  .root-pair::after {
    /* single line from the couple down to the children's line */
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2px;
    height: calc(50% + 26px);
    background: var(--line);
    transform: translateX(-1px);
  }
  .root-card {
    width: 190px;
    min-width: 190px;
    max-width: 190px;
  }
  .kambhu-box {
    width: 190px;
    background: var(--cream);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 14px;
    text-align: center;
    position: relative;
  }
  .kambhu-box .name {
    font-weight: 700;
    color: var(--ink);
    font-size: 14px;
  }
  .kambhu-box .name-thai {
    font-weight: 400;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.3;
    margin-top: 1px;
  }
  .kambhu-box .dates {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 11px;
    color: var(--muted);
    margin-top: 3px;
  }

  /* ---- Tree structure (classic pure-CSS org chart) ---- */
  .tree, .tree ul, .tree li {
    list-style: none;
    margin: 0;
    padding: 0;
    position: relative;
  }
  .tree {
    display: flex;
    justify-content: flex-start;
    padding-top: 8px;
    width: max-content;
    margin: 0 auto;
  }
  .tree ul {
    display: flex;
    padding-top: 26px;
  }
  .tree li {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 26px 10px 0 10px;
  }
  .tree li::before, .tree li::after {
    content: '';
    position: absolute;
    top: 0;
    right: 50%;
    border-top: 2px solid var(--line);
    width: 50%;
    height: 26px;
  }
  .tree li::after {
    right: auto;
    left: 50%;
    border-left: 2px solid var(--line);
  }
  .tree li:only-child::after, .tree li:only-child::before { display: none; }
  .tree li:only-child { padding-top: 0; }
  .tree li:first-child::before, .tree li:last-child::after { border: 0 none; }
  .tree li:last-child::before { border-right: 2px solid var(--line); border-radius: 0 6px 0 0; }
  .tree li:first-child::after { border-radius: 6px 0 0 0; }
  .tree ul ul::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    border-left: 2px solid var(--line);
    width: 0;
    height: 26px;
  }

  /* ---- Person card ---- */
  #root.read-only .card {
    cursor: default;
  }
  #root.read-only .card:hover {
    box-shadow: 0 1px 3px rgba(38,33,92,0.08);
    border-color: var(--line);
  }
  .card {
    background: var(--cream);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 14px;
    width: 190px;
    word-wrap: break-word;
    text-align: center;
    position: relative;
    box-shadow: 0 1px 3px rgba(38,33,92,0.08);
    cursor: pointer;
    transition: box-shadow 0.15s ease, border-color 0.15s ease;
  }
  .card:hover {
    box-shadow: 0 4px 14px rgba(38,33,92,0.18);
    border-color: var(--teal);
  }
  .card .cross-links { position: absolute; top: -9px; left: -9px; display: flex; gap: 3px; z-index: 2; }
  .card .cross-link {
    background: var(--teal);
    color: #fff;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 9px;
    font-weight: 600;
    letter-spacing: 0.02em;
    padding: 3px 7px;
    border-radius: 10px;
    text-decoration: none;
    box-shadow: 0 1px 3px rgba(38,33,92,0.25);
    white-space: nowrap;
  }
  .card .cross-link:hover { background: var(--ink); }
  .card.highlight {
    border-color: var(--rust);
    box-shadow: 0 0 0 3px rgba(168,92,50,0.18);
  }
  .card.accented { border-color: var(--teal); background: var(--teal-soft); }
  .card .name {
    font-weight: 700;
    color: var(--ink);
    font-size: 14px;
    line-height: 1.25;
  }
  .card .name-thai {
    font-weight: 400;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.3;
    margin-top: 1px;
  }
  .card .dates {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 11px;
    color: var(--muted);
    margin-top: 3px;
  }
  .card .spouse {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 11px;
    font-style: italic;
    color: var(--teal);
    margin-top: 4px;
    border-top: 1px solid var(--line);
    padding-top: 4px;
  }
  .card .spouse-thai {
    font-size: 11px;
    font-style: italic;
    color: var(--teal);
    opacity: 0.8;
    margin-top: 1px;
  }
  .card .note-line {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 10px;
    color: var(--rust);
    margin-top: 3px;
    font-style: italic;
  }
  .card .add-child-btn {
    position: absolute;
    bottom: -11px;
    left: 50%;
    transform: translateX(-50%);
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--teal);
    color: white;
    border: 2px solid var(--paper);
    font-size: 14px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s ease;
  }
  .card:hover .add-child-btn { opacity: 1; }

  /* ---- Modal ---- */
  .overlay {
    position: fixed;
    inset: 0;
    background: rgba(38,33,92,0.35);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 20px;
  }
  .modal {
    background: var(--paper);
    border-radius: 10px;
    padding: 26px 28px 22px 28px;
    width: 100%;
    max-width: 460px;
    max-height: 88vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  }
  .modal h2 {
    font-family: Georgia, serif;
    color: var(--ink);
    font-size: 20px;
    margin: 0 0 4px 0;
  }
  .modal .modal-sub {
    font-size: 12.5px;
    color: var(--muted);
    margin-bottom: 18px;
  }
  .field { margin-bottom: 13px; }
  .field label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--ink-soft);
    margin-bottom: 4px;
    letter-spacing: 0.01em;
  }
  .field input, .field select, .field textarea {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 13.5px;
    font-family: inherit;
    background: white;
    color: var(--text);
  }
  .field input:focus, .field select:focus, .field textarea:focus {
    outline: none;
    border-color: var(--teal);
    box-shadow: 0 0 0 3px rgba(15,110,86,0.12);
  }
  .field-row { display: flex; gap: 10px; }
  .field-row .field { flex: 1; }
  .field textarea { resize: vertical; min-height: 50px; }

  .modal-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--line);
  }
  .modal-actions .right { display: flex; gap: 8px; }

  .btn-danger { background: transparent; color: #A83232; border: 1px solid #A83232; }
  .btn-danger:hover { background: #FBEBEB; }
  .btn-secondary { background: var(--cream-deep); color: var(--ink); }
  .btn-secondary:hover { background: var(--cream); }

  .empty-state {
    text-align: center;
    color: var(--muted);
    font-size: 14px;
    padding: 60px 20px;
    font-style: italic;
  }

  .toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--ink);
    color: var(--cream);
    padding: 10px 18px;
    border-radius: 8px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 13px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.25);
    z-index: 100;
    animation: fadeInOut 2.2s ease forwards;
  }
  @keyframes fadeInOut {
    0% { opacity: 0; transform: translate(-50%, 8px); }
    12% { opacity: 1; transform: translate(-50%, 0); }
    85% { opacity: 1; }
    100% { opacity: 0; }
  }
