/* ========== Design tokens ========== */
:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #f1ece5;
  --text: #1f1a16;
  --muted: #6f655c;
  --accent: #7c4a1e;
  --accent-soft: #f3e6d8;
  --line: #e4dbd0;
  --male: #e3eefb;  --male-bd: #9dbbe0;
  --female: #fbe6ee; --female-bd: #e3a4ba;
  --danger: #b3261e;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(31,26,22,.06), 0 4px 16px rgba(31,26,22,.06);
  --shadow-lg: 0 10px 40px rgba(31,26,22,.18);
  --tabbar-h: 64px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-serif: Georgia, "Times New Roman", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15120f; --surface: #1f1b17; --surface-2: #2a251f; --text: #f1ebe4; --muted: #a89d92;
    --accent: #d9a46c; --accent-soft: #3a2c1e; --line: #3a332c;
    --male: #1d2a3a; --male-bd: #3f6290; --female: #3a1f2a; --female-bd: #8f4d66;
    --danger: #f2867e; --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 10px 40px rgba(0,0,0,.6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #15120f; --surface: #1f1b17; --surface-2: #2a251f; --text: #f1ebe4; --muted: #a89d92;
    --accent: #d9a46c; --accent-soft: #3a2c1e; --line: #3a332c;
    --male: #1d2a3a; --male-bd: #3f6290; --female: #3a1f2a; --female-bd: #8f4d66;
    --danger: #f2867e; --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 10px 40px rgba(0,0,0,.6);
    color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { scroll-behavior: smooth; }
body {
  margin: 0; font-family: var(--font); font-size: 16px; line-height: 1.5;
  background: var(--bg); color: var(--text);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}
h1, h2, h3 { line-height: 1.25; }
h2, h3 { font-family: var(--font-serif); font-weight: 600; }

/* ========== App bar & navigation ========== */
.appbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 1.1rem; font-family: var(--font-serif); }
.logo {
  width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center;
  background: var(--accent); color: var(--bg); font-weight: 700; font-size: .85rem;
}
.tabbar { display: flex; gap: 4px; }
.tabbar button {
  display: flex; align-items: center; gap: 8px; border: 0; background: transparent;
  color: var(--muted); padding: 8px 14px; border-radius: 999px; font-weight: 500;
}
.tabbar button:hover { background: var(--surface-2); color: var(--text); }
.tabbar button.active { background: var(--accent-soft); color: var(--accent); }
.tabbar svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Mobile: bottom tab bar */
@media (max-width: 720px) {
  .appbar { justify-content: center; }
  .tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    padding: 6px 8px env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--line);
    justify-content: space-around; gap: 0;
  }
  .tabbar button { flex: 1; flex-direction: column; gap: 2px; padding: 6px 0; font-size: .72rem; border-radius: 12px; }
  .tabbar button.active { background: transparent; }
  .tabbar button.active svg { background: var(--accent-soft); border-radius: 999px; padding: 3px 14px; width: 52px; height: 28px; }
}

/* ========== Layout ========== */
main { max-width: 1080px; margin: 0 auto; padding: 8px 16px; }
.tab { display: none; animation: fade .2s ease; }
.tab.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.page-head { margin: 16px 0 8px; }
.page-head h2 { margin: 0; font-size: 1.6rem; }
.page-head p { margin: 2px 0 0; color: var(--muted); }

/* ========== Controls ========== */
button, .btn {
  font: inherit; font-size: .95rem; min-height: 44px; padding: 10px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: background .15s, box-shadow .15s, transform .05s;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:active { transform: scale(.98); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-within {
  outline: 3px solid color-mix(in srgb, var(--accent) 40%, transparent); outline-offset: 2px;
}
button.primary { background: var(--accent); color: var(--bg); border-color: var(--accent); font-weight: 600; }
button.primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
button.danger { color: var(--danger); border-color: transparent; background: transparent; }
button.danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.plus { font-size: 1.3rem; line-height: 1; }

.toolbar { display: flex; gap: 10px; margin: 12px 0 16px; align-items: center; }
.toolbar input { flex: 1; }
input, textarea, select {
  font: inherit; font-size: 16px; /* cegah auto-zoom iOS */
  min-height: 44px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px;
  width: 100%; background: var(--surface); color: var(--text);
}
input[type="search"] {
  padding-left: 40px;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23998' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M20 20l-3.5-3.5'/%3E%3C/svg%3E") no-repeat 12px center / 18px;
}
input[type="file"] { padding: 8px; }
textarea { min-height: 180px; resize: vertical; line-height: 1.6; }

/* Mobile: tombol tambah jadi FAB */
@media (max-width: 720px) {
  .fab {
    position: fixed; right: 16px; z-index: 25;
    bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
    width: 56px; height: 56px; border-radius: 18px; padding: 0; box-shadow: var(--shadow-lg);
  }
  .fab .lbl { display: none; }
  .fab .plus { font-size: 1.8rem; }
}

/* ========== Cards & text ========== */
.card { background: var(--surface); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 14px; border: 1px solid var(--line); }
.card h2 { margin: 0 0 6px; font-size: 1.2rem; }
.card > button, .card > .btn { margin: 8px 8px 0 0; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.hint { color: var(--muted); font-size: .88rem; }
.empty {
  color: var(--muted); text-align: center; padding: 56px 20px; border: 2px dashed var(--line);
  border-radius: var(--radius); list-style: none;
}
.story { white-space: pre-wrap; line-height: 1.75; font-size: 1.02rem; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; }
.dot { width: 12px; height: 12px; border-radius: 4px; display: inline-block; margin-right: -8px; border: 1px solid; }
.dot.L { background: var(--male); border-color: var(--male-bd); }
.dot.P { background: var(--female); border-color: var(--female-bd); }

/* ========== Timeline ========== */
.timeline { list-style: none; margin: 0; padding: 0 0 0 22px; position: relative; }
.timeline::before { content: ""; position: absolute; left: 6px; top: 8px; bottom: 8px; width: 2px; background: var(--line); }
.timeline li { position: relative; margin-bottom: 14px; cursor: pointer; }
.timeline li::before {
  content: ""; position: absolute; left: -22px; top: 22px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--surface); border: 3px solid var(--accent);
}
.timeline .card { display: flex; gap: 16px; margin: 0; padding: 14px; transition: box-shadow .15s, transform .15s; }
.timeline li:hover .card { box-shadow: var(--shadow-lg); transform: translateY(-1px); }
.timeline img { width: 128px; height: 96px; object-fit: cover; border-radius: 12px; flex-shrink: 0; }
.timeline .date {
  display: inline-block; color: var(--accent); background: var(--accent-soft); font-size: .78rem;
  font-weight: 600; padding: 2px 10px; border-radius: 999px;
}
.timeline h3 { margin: 6px 0 4px; font-size: 1.1rem; }
.timeline p { margin: 0; color: var(--muted); font-size: .92rem;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
@media (max-width: 560px) {
  .timeline .card { flex-direction: column; gap: 10px; }
  .timeline img { width: 100%; height: 170px; }
}

/* ========== Member grid ========== */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.member-card { text-align: center; cursor: pointer; margin: 0; padding: 20px 12px; transition: box-shadow .15s, transform .15s; }
.member-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }
.member-card h3 { font-size: 1rem; }
.avatar {
  width: 80px; height: 80px; border-radius: 50%; object-fit: cover; background: var(--line);
  display: inline-flex; align-items: center; justify-content: center; font-size: 1.6rem; color: #fff; font-weight: 600;
  box-shadow: 0 0 0 3px var(--surface), 0 0 0 4px var(--line);
}
.avatar.big { width: 128px; height: 128px; font-size: 2.6rem; }
@media (max-width: 560px) {
  .grid { grid-template-columns: 1fr; gap: 8px; }
  .member-card { display: flex; align-items: center; gap: 14px; text-align: left; padding: 12px 14px; }
  .member-card .avatar { width: 52px; height: 52px; font-size: 1.1rem; flex-shrink: 0; }
  .member-card h3 { margin: 0 !important; }
}

/* ========== Tree ========== */
.scroll-hint { display: none; }
@media (max-width: 720px) { .scroll-hint { display: block; margin: 0 0 4px; } }
.tree-wrap {
  overflow: auto; padding: 20px 8px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  background-image: radial-gradient(var(--line) 1px, transparent 1px); background-size: 18px 18px;
  -webkit-overflow-scrolling: touch;
}
.tree, .tree ul { padding-top: 22px; position: relative; display: flex; justify-content: center; margin: 0; padding-left: 0; }
.tree { width: max-content; min-width: 100%; }
.tree li { list-style: none; text-align: center; position: relative; padding: 22px 6px 0; }
.tree li::before, .tree li::after { content: ""; position: absolute; top: 0; right: 50%; width: 50%; height: 22px; border-top: 2px solid var(--line); }
.tree li::after { right: auto; left: 50%; border-left: 2px solid var(--line); }
.tree li:only-child::before, .tree li:only-child::after { 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 8px 0 0; }
.tree li:first-child::after { border-radius: 8px 0 0 0; }
.tree ul::before { content: ""; position: absolute; top: 0; left: 50%; height: 22px; border-left: 2px solid var(--line); }
.tree > li { padding-top: 0; } .tree > li::before, .tree > li::after { display: none; }
.tree-root + .tree-root { margin-top: 32px; padding-top: 32px; border-top: 1px dashed var(--line); }
.couple { display: inline-flex; align-items: center; gap: 4px; padding: 4px; border-radius: 16px; }
.couple .heart { color: var(--danger); font-size: .9rem; }
.node {
  display: inline-flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 10px 8px; border-radius: 14px; border: 1.5px solid var(--line); background: var(--surface);
  cursor: pointer; width: 112px; font-size: .82rem; box-shadow: var(--shadow); transition: transform .15s, box-shadow .15s;
}
.node:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.node.L { background: var(--male); border-color: var(--male-bd); }
.node.P { background: var(--female); border-color: var(--female-bd); }
.node .avatar { width: 44px; height: 44px; font-size: 1rem; }
.node b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 92px; }
.node small { color: var(--muted); display: block; font-size: .72rem; }

/* ========== Modal / bottom sheet ========== */
dialog {
  border: 0; border-radius: 20px; width: min(680px, 94vw); max-height: 88vh; padding: 28px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
dialog[open] { animation: pop .2s ease; }
@keyframes pop { from { opacity: 0; transform: scale(.97); } to { opacity: 1; transform: none; } }
dialog::backdrop { background: rgba(20,15,10,.5); backdrop-filter: blur(2px); }
dialog h2 { margin-top: 0; padding-right: 36px; }
.close {
  position: sticky; float: right; top: 0; margin: -12px -12px 0 0; z-index: 2;
  width: 40px; height: 40px; min-height: 40px; padding: 0; border-radius: 50%;
  border: 0; font-size: 1.4rem; background: var(--surface-2);
}
@media (max-width: 720px) {
  dialog {
    width: 100vw; max-width: 100vw; max-height: 92vh; margin: auto 0 0; border-radius: 22px 22px 0 0;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  }
  dialog::before {
    content: ""; display: block; width: 40px; height: 4px; border-radius: 4px; background: var(--line); margin: -8px auto 12px;
  }
  dialog[open] { animation: sheet .25s cubic-bezier(.2,.8,.2,1); }
  @keyframes sheet { from { transform: translateY(100%); } to { transform: none; } }
}

/* Forms */
.form label { display: block; margin: 14px 0 6px; font-size: .85rem; font-weight: 600; color: var(--muted); }
.form .actions {
  margin-top: 22px; display: flex; gap: 8px; justify-content: flex-end;
  position: sticky; bottom: -28px; background: var(--surface); padding: 12px 0; border-top: 1px solid var(--line);
}
.form .actions .danger { margin-right: auto; }
@media (max-width: 720px) {
  .form .actions { bottom: calc(-20px - env(safe-area-inset-bottom)); }
  .form .actions button { flex: 1; }
  .form .actions .danger { flex: 0 0 auto; }
}
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
@media (max-width: 560px) { .two-col { grid-template-columns: 1fr; } }
.preview { max-width: 180px; max-height: 130px; margin-top: 10px; border-radius: 12px; display: block; object-fit: cover; }
#fImgRm { margin-top: 8px; }

/* Detail */
.detail-img { width: calc(100% + 56px); margin: -28px -28px 16px; max-height: 340px; object-fit: cover; border-radius: 20px 20px 0 0; display: block; }
@media (max-width: 720px) { .detail-img { width: calc(100% + 32px); margin: -8px -16px 16px; border-radius: 0; } }
.facts {
  display: grid; grid-template-columns: max-content 1fr; gap: 0; margin: 18px 0;
  background: var(--surface-2); border-radius: 14px; overflow: hidden;
}
.facts dt, .facts dd { padding: 10px 14px; border-bottom: 1px solid var(--line); margin: 0; }
.facts dt { color: var(--muted); font-size: .88rem; }
.facts dt:last-of-type, .facts dd:last-of-type { border-bottom: 0; }
.link { color: var(--accent); cursor: pointer; font-weight: 500; text-decoration: underline; text-underline-offset: 3px; }

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

/* ========== Filter & tag chips ========== */
.filters { display: flex; gap: 8px; margin: -6px 0 10px; flex-wrap: wrap; }
.filters select { width: auto; min-width: 140px; flex: 0 0 auto; }
.filter-btn.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.badge { background: var(--accent); color: var(--bg); border-radius: 999px; font-size: .72rem; padding: 1px 7px; font-weight: 700; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.chips:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 3px 12px 3px 3px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: .85rem; cursor: pointer;
}
.chip .avatar { width: 24px; height: 24px; font-size: .6rem; box-shadow: none; }
.chip.sel { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chip .x { margin-left: 2px; opacity: .7; }
.picker { display: flex; flex-wrap: wrap; gap: 6px; max-height: 50vh; overflow: auto; padding: 4px 0; }
.picker-search { margin-bottom: 8px; }
.tags-mini { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.tags-mini .chip { min-height: 26px; font-size: .75rem; padding-right: 8px; cursor: default; }
.tags-mini .chip .avatar { width: 20px; height: 20px; }
.kat { display: inline-block; font-size: .75rem; font-weight: 600; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); margin-left: 4px; }
.theme-btn { order: 3; width: 40px; height: 40px; min-height: 40px; padding: 0; border-radius: 50%; font-size: 1.1rem; flex-shrink: 0; }
@media (max-width: 720px) { .appbar { justify-content: space-between; } }
