/* Wood & leaves (pastel): birch and oak surfaces, walnut text, sage leaves, terracotta actions.
   Same palette as home.html and guide.html; always light. */
:root {
  --bg: #f3f1e4;
  --surface: #fbf7ee;
  --surface-2: #fffdf7;
  --ink: #4e321c;
  --muted: #7a5b42;
  --line: #c4a272;
  --border: #dcc6a3;
  --accent: #c46a3c;
  --accent-2: #b25a2c;
  --accent-ink: #ffffff;
  --leaf: #7fa877; --leaf-ink: #4f7a48; --leaf-soft: #e4eedc;
  --m-bg: #e3edf0; --m-bd: #8aaab5;
  --f-bg: #f7e3dc; --f-bd: #d49a88;
  --u-bg: #efede6; --u-bd: #b3a58f;
  --dec-bg: #ebe5d8;
  --sel: #e08a2e;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgba(110, 70, 30, .08), 0 3px 10px rgba(110, 70, 30, .08);
  --serif: Georgia, "Palatino Linotype", "Book Antiqua", Palatino, "Noto Serif", serif;
  --wood: repeating-linear-gradient(96deg, rgba(150, 105, 60, .07) 0 2px, transparent 2px 11px), linear-gradient(180deg, #efe3cc, #e7d7b9);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(60rem 36rem at 0% 0%, rgba(178, 207, 160, .50), transparent 62%), radial-gradient(50rem 32rem at 100% 0%, rgba(214, 232, 196, .55), transparent 60%), radial-gradient(70rem 40rem at 50% 110%, rgba(226, 204, 170, .60), transparent 65%), linear-gradient(175deg, #eef3e6 0%, #f3f1e4 45%, #f5efe2 100%);
  color: var(--ink);
  /* Fallbacks so names in Arabic, Urdu, Gujarati, Hindi… render on every PC and phone */
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", "Noto Nastaliq Urdu", "Noto Sans Gujarati", "Noto Sans Devanagari", sans-serif;
  display: flex; flex-direction: column;
}
button, input, select, textarea { font: inherit; color: inherit; }

/* ───── Top bar ───── */
.bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px;
  padding: 10px 16px;
  background: var(--wood);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 6px rgba(110, 70, 30, .08);
  position: relative; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; }
.brand .logo { display: grid; }
.brand h1 { font: 700 19px var(--serif); margin: 0; letter-spacing: .2px; color: #4e321c; }
.brand small { color: var(--muted); }
.field { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.field select { max-width: 230px; }
select, input[type=search], input[type=text], textarea {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 9px;
}
textarea { width: 100%; min-height: 70px; resize: vertical; }
.group { display: flex; gap: 4px; flex-wrap: wrap; }
.tools { display: flex; flex-wrap: wrap; gap: 10px 14px; }
.menu-btn { display: none; margin-left: auto; font-size: 18px; width: 44px; height: 40px; padding: 0; }
.zoombar {
  position: absolute; right: 12px; top: 12px; z-index: 3;
  display: flex; flex-direction: column; gap: 6px;
}
.zoombar button { width: 40px; height: 40px; padding: 0; font-size: 16px; box-shadow: var(--shadow); background: var(--surface); }
.zoombar button[data-act=fit] { font-size: 12px; }
.legend-btn { display: none; }
[hidden] { display: none !important; }
.user-box { align-items: center; }
.user-name { color: var(--muted); font-size: 12.5px; padding: 0 4px; }
.users-wrap { max-height: 40vh; overflow: auto; margin-bottom: 8px; border: 1px solid var(--border); border-radius: 8px; }
table.users { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.users td { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.users tr:last-child td { border-bottom: 0; }
table.users small { color: var(--muted); }
table.users .f-row { margin: 0; }
.users-h { margin: 12px 0 8px; font-size: 13px; }
.boot-error { color: var(--danger); background: var(--surface); padding: 12px 16px; border-radius: 10px; border: 1px solid var(--border); }
button {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: linear-gradient(180deg, var(--accent), var(--accent-2)); color: var(--accent-ink); border-color: var(--accent-2); }
button.danger { color: var(--danger); }
button:focus-visible, .card:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--sel); outline-offset: 1px; }

.search { position: relative; }
.search input { width: 220px; }
.dropdown {
  position: absolute; top: calc(100% + 4px); left: 0; width: 320px; max-height: 360px; overflow: auto;
  margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow);
}
.dropdown li { padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.dropdown li:hover, .dropdown li.active { background: var(--surface-2); }
.dropdown small { display: block; color: var(--muted); }

/* ───── Layout ───── */
main { flex: 1; display: grid; grid-template-columns: 1fr 340px; min-height: 0; }
#viewport {
  position: relative; overflow: hidden; cursor: grab; touch-action: none;
  background-image: radial-gradient(rgba(160, 125, 80, .28) 1px, transparent 1px);
  background-size: 22px 22px;
}
#viewport.dragging { cursor: grabbing; }
#panel {
  background: var(--surface); border-left: 1px solid var(--border);
  overflow: auto; padding: 16px;
}
#panel .empty { color: var(--muted); text-align: center; margin-top: 40px; }

.legend {
  position: absolute; left: 12px; bottom: 10px; display: flex; flex-wrap: wrap; gap: 6px 14px;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px; font-size: 12px; color: var(--muted);
  max-width: calc(100% - 24px);
}
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend .hint { opacity: .85; }
.sw { width: 14px; height: 10px; border-radius: 3px; border: 1.5px solid; display: inline-block; }
.sw.g-M { background: var(--m-bg); border-color: var(--m-bd); }
.sw.g-F { background: var(--f-bg); border-color: var(--f-bd); }
.sw.dec { background: var(--dec-bg); border-color: var(--u-bd); }
.sw.inf { border-style: dashed; border-color: var(--muted); }

/* ───── Tree (CSS connectors) ───── */
#tree { position: absolute; top: 0; left: 0; transform-origin: 0 0; padding: 30px; }
.tree ul { display: flex; justify-content: center; position: relative; margin: 0; padding: 22px 0 0; }
#tree > ul { padding-top: 0; }
.tree li { list-style: none; position: relative; padding: 22px 6px 0; display: flex; flex-direction: column; align-items: center; }
.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 9px 0 0; }
.tree li:first-child::after { border-radius: 9px 0 0 0; }
.tree ul ul::before {
  content: ''; position: absolute; top: 0; left: 50%; width: 0; height: 22px;
  border-left: 2px solid var(--line);
}
/* Vertical stack for childless siblings: spine on the left, tick into each card */
.tree ul.stack { flex-direction: column; align-items: flex-start; }
.tree ul.stack::before { height: 11px; }
.tree ul.stack::after {
  content: ''; position: absolute; top: 11px; left: 6px; width: calc(50% - 6px);
  border-top: 2px solid var(--line);
}
.tree ul.stack > li { padding: 4px 0 4px 20px; align-items: flex-start; }
.tree ul.stack > li::before, .tree ul.stack > li:first-child::before, .tree ul.stack > li:last-child::before {
  display: block; top: 0; bottom: 0; left: 6px; right: auto; width: 0; height: auto;
  border: 0; border-left: 2px solid var(--line); border-radius: 0;
}
.tree ul.stack > li:first-child::before { top: -11px; }
.tree ul.stack > li:last-child::before { bottom: 50%; }
.tree ul.stack > li::after, .tree ul.stack > li:first-child::after, .tree ul.stack > li:last-child::after {
  display: block; top: 50%; left: 6px; right: auto; width: 14px; height: 0;
  border: 0; border-top: 2px solid var(--line); border-radius: 0;
}
#tree > ul > li { padding-top: 0; }
#tree > ul > li::before, #tree > ul > li::after { display: none; }

/* Horizontal tree (#tree.horiz): generations run left to right, children listed one under another.
   Each child's ::before = upper half of the spine + tick into the card, ::after = lower half of the spine. */
.tree.horiz ul { flex-direction: column; align-items: flex-start; justify-content: center; padding: 0 0 0 30px; }
#tree.horiz > ul { padding-left: 0; }
.tree.horiz li { flex-direction: row; align-items: center; padding: 4px 0 4px 22px; }
.tree.horiz li::before, .tree.horiz li::after,
.tree.horiz li:first-child::before, .tree.horiz li:first-child::after,
.tree.horiz li:last-child::before, .tree.horiz li:last-child::after {
  display: block; top: 0; left: 0; right: auto; width: 22px; height: 50%;
  border: 0; border-left: 2px solid var(--line); border-radius: 0;
}
.tree.horiz li::before, .tree.horiz li:first-child::before, .tree.horiz li:last-child::before { border-bottom: 2px solid var(--line); }
.tree.horiz li::after, .tree.horiz li:first-child::after, .tree.horiz li:last-child::after { top: 50%; }
.tree.horiz li:first-child::before { border: 0; }
.tree.horiz li:first-child::after { border-top: 2px solid var(--line); border-radius: 9px 0 0 0; top: calc(50% - 1px); }
.tree.horiz li:last-child::before { border-radius: 0 0 0 9px; }
.tree.horiz li:last-child::after { border: 0; }
.tree.horiz li:only-child { padding-left: 0; }
.tree.horiz li:only-child::before, .tree.horiz li:only-child::after { display: none; }
.tree.horiz ul ul::before { top: 50%; left: 0; width: 30px; height: 0; border-left: 0; border-top: 2px solid var(--line); }
#tree.horiz > ul > li { padding: 4px 0; }
#tree.horiz > ul > li::before, #tree.horiz > ul > li::after { display: none; }
/* A couple sits one above the other so each generation stays narrow; the +/− button moves to the right edge. */
.tree.horiz .couple { flex-direction: column; gap: 2px; padding: 0 2px 0 0; }
.tree.horiz .tg { left: auto; right: -14px; bottom: auto; top: 50%; transform: translateY(-50%); }

.couple { display: inline-flex; align-items: center; gap: 4px; position: relative; padding-bottom: 2px; }
.couple.sub { flex-direction: column; gap: 2px; }
.ring { color: var(--muted); font-size: 15px; line-height: 1; }
.ring.div { color: var(--danger); }
.ord { font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.ord.div { color: var(--danger); }

.card {
  min-width: 92px; max-width: 150px; padding: 6px 10px;
  border: 1.5px solid var(--u-bd); background: var(--u-bg);
  border-radius: 9px; box-shadow: var(--shadow);
  text-align: center; cursor: pointer; user-select: none;
  transition: transform .12s ease, box-shadow .12s ease;
}
.card:hover { transform: translateY(-1px); }
.card .nm { display: block; font-weight: 600; font-size: 12.5px; line-height: 1.25; word-wrap: break-word; }
.card .mk { display: block; font-size: 10.5px; color: var(--muted); margin-top: 2px; }
.card.g-M { background: var(--m-bg); border-color: var(--m-bd); }
.card.g-F { background: var(--f-bg); border-color: var(--f-bd); }
.card.dec { background: var(--dec-bg); }
.card.dec .nm { color: var(--muted); }
.card.inf { border-style: dashed; }
.card.unknown { cursor: default; color: var(--muted); font-style: italic; box-shadow: none; border-style: dotted; }
.card.unknown:hover { transform: none; }
.card.sel { outline: 3px solid var(--sel); outline-offset: 2px; }
.card.flash { animation: flash 1.2s ease 2; }
@keyframes flash { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--sel) 45%, transparent); } }

.tg {
  position: absolute; left: 50%; bottom: -13px; transform: translateX(-50%);
  min-width: 22px; height: 20px; padding: 0 5px; border-radius: 10px;
  font-size: 11px; line-height: 18px; z-index: 2;
}
.seefam { font-size: 11.5px; padding: 4px 8px; border-style: dashed; color: var(--muted); }

/* ───── Families, users, Super Admin ───── */
/* Until sign-in finishes, show only the title (the toolbar would act on nothing). */
body.signed-out .tools, body.signed-out .field, body.signed-out .search, body.signed-out .menu-btn, body.signed-out #panel .empty { visibility: hidden; }
input[type=tel], input[type=password], input[type=date] {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 9px;
}
.f-row input[type=tel], .f-row input[type=password], .f-row input[type=date] { width: 100%; }
dialog.wide { width: min(920px, calc(100vw - 32px)); }
.added-by { font-size: 11.5px; color: var(--muted); margin: -4px 0 10px; }
ul.history { list-style: none; margin: 0 0 10px; padding: 0; border: 1px solid var(--border); border-radius: 8px; max-height: 48vh; overflow: auto; }
ul.history li { display: grid; grid-template-columns: 150px 1fr; gap: 10px; padding: 7px 10px; border-bottom: 1px solid var(--border); font-size: 13px; }
ul.history li:last-child { border-bottom: 0; }
ul.history .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
@media (max-width: 600px) { ul.history li { grid-template-columns: 1fr; gap: 2px; } }
button.linkish { background: none; border: 0; padding: 0; color: var(--accent); font-weight: 600; text-align: left; }
table.users tr.current td { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.phone-row { display: flex; gap: 6px; }
.phone-row select { flex: 0 0 auto; max-width: 46%; }
.phone-row input { flex: 1 1 auto; min-width: 0; }
.users-wrap.tall { max-height: 48vh; }
table.users th { text-align: left; font-size: 11.5px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
table.users .num, table.users th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.owe, .owe b { color: var(--danger); }
.icon-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.badge.danger-badge { color: var(--danger); border-color: var(--danger); }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin-bottom: 6px; }
.stat-row > div { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; }
.stat-row b { display: block; font-size: 16px; font-variant-numeric: tabular-nums; }
.stat-row span { font-size: 11.5px; color: var(--muted); }
.notice {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-bottom: 12px; padding: 10px 12px;
  background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border: 1px solid var(--accent); border-radius: 10px;
}
.notice span { flex: 1 1 220px; }
a.btn {
  display: inline-block; padding: 6px 12px; border-radius: 8px; text-decoration: none; font-weight: 600;
  border: 1px solid var(--border); color: inherit; background: var(--surface-2);
}
a.btn.wa { background: #1f8f4e; border-color: #1f8f4e; color: #fff; }
.hp { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }
.fam-list { display: grid; gap: 8px; }
.fam-pick { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 10px 12px; text-align: left; }
.fam-pick small { color: var(--muted); }
.fam-pick.current { border-color: var(--accent); }
.empty-tree { max-width: 360px; text-align: center; padding: 40px 24px; margin: 60px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); }
.empty-tree h2 { margin: 0 0 6px; font-size: 18px; }
.empty-tree p { color: var(--muted); margin: 0 0 16px; }

/* ───── Globe view ───── */
#tree.globe-mode { padding: 0; }
svg.globe { display: block; overflow: visible; }
.g-ring { fill: none; stroke: var(--border); stroke-width: 1; stroke-dasharray: 2 5; }
.glink { fill: none; stroke: var(--line); stroke-width: 1.4; opacity: .85; transition: opacity .15s; }
.glink.spouse { stroke: var(--f-bd); stroke-dasharray: 4 3; }
.glink.child2 { opacity: .28; }
svg.globe.hovering .glink { opacity: .18; }
svg.globe .glink.hot { opacity: 1; stroke: var(--accent); stroke-width: 2.4; }
.gnode { cursor: grab; outline: none; }
.gnode:active { cursor: grabbing; }
.gnode circle { fill: var(--u-bg); stroke: var(--u-bd); stroke-width: 1.8; transition: stroke-width .12s; }
.gnode.g-M circle { fill: var(--m-bg); stroke: var(--m-bd); }
.gnode.g-F circle { fill: var(--f-bg); stroke: var(--f-bd); }
.gnode.dec circle { fill: var(--dec-bg); }
.gnode.inf circle { stroke-dasharray: 2 2; }
.gnode:hover circle, .gnode:focus-visible circle { stroke-width: 3.2; }
.gnode text {
  font-size: 11px; fill: var(--ink); user-select: none;
  paint-order: stroke; stroke: var(--bg); stroke-width: 3px; stroke-linejoin: round;
}
.gnode.spouse text { font-size: 10px; fill: var(--muted); }
.gnode.dec text { fill: var(--muted); }
.gnode.root text { font-size: 13px; font-weight: 700; }
.gnode.sel circle { stroke: var(--sel); stroke-width: 4; }
.gnode.sel text { font-weight: 700; }
.gnode.flash circle { animation: gflash 1.2s ease 2; }
@keyframes gflash { 50% { stroke: var(--sel); stroke-width: 9; } }

/* ───── Details panel ───── */
#panel h2 { margin: 0 0 4px; font-size: 19px; }
.badges { display: flex; flex-wrap: wrap; gap: 5px; margin: 6px 0 10px; }
.badge { font-size: 11.5px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.badge.g-M { border-color: var(--m-bd); }
.badge.g-F { border-color: var(--f-bd); }
.notes { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; white-space: pre-wrap; margin: 0 0 12px; }
dl.props {
  display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px;
  margin: 0 0 12px; padding: 8px 10px; font-size: 13px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
}
dl.props dt { color: var(--muted); }
dl.props dd { margin: 0; overflow-wrap: anywhere; }
.f-sec { margin: 16px 0 8px; padding-top: 12px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; }
.nowrap { white-space: nowrap; text-align: right; }
.icon-btn:disabled { opacity: .35; cursor: default; }
dialog { max-height: calc(100dvh - 32px); overflow: auto; }
.rel h4 { margin: 14px 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip { padding: 3px 9px; border-radius: 99px; font-size: 12.5px; border: 1.5px solid var(--u-bd); background: var(--u-bg); }
.chip.g-M { border-color: var(--m-bd); background: var(--m-bg); }
.chip.g-F { border-color: var(--f-bd); background: var(--f-bg); }
.chip.dec { opacity: .75; }
.spouse-row { display: flex; align-items: center; gap: 6px; margin-bottom: 5px; flex-wrap: wrap; }
.spouse-row small { color: var(--muted); }
.icon-btn { padding: 1px 7px; font-size: 12px; }
.none { color: var(--muted); font-size: 12.5px; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 18px; }
.actions .wide { grid-column: 1 / -1; }
.close-panel { display: none; float: right; }

/* ───── Dialog ───── */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 18px 20px; width: min(460px, calc(100vw - 32px));
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h3 { margin: 0 0 12px; }
.f-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.f-row > span { font-size: 12px; color: var(--muted); }
.f-row input[type=text], .f-row select { width: 100%; }
.f-row.check { flex-direction: row; align-items: center; gap: 8px; }
.f-row .hint { font-size: 11.5px; color: var(--muted); }
.dlg-actions { display: flex; align-items: center; gap: 8px; justify-content: flex-end; margin-top: 14px; }
#dlgExtra { margin-right: auto; order: 1; }
#dlgCancel { order: 2; }
#dlgOk { order: 3; }
.error { color: var(--danger); margin: 6px 0 0; }

#toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 10px; z-index: 50; box-shadow: var(--shadow);
}

/* ───── Phone / tablet ───── */
@media (max-width: 820px) {
  .bar { padding: 8px 16px; gap: 8px; }
  .brand { width: 100%; margin-right: 0; }
  .menu-btn { display: inline-block; }
  .field, .search { flex: 1 1 100%; min-width: 0; }
  .field select { max-width: none; flex: 1; min-width: 0; width: 100%; }
  .search input { width: 100%; }
  .dropdown { width: 100%; max-height: 55vh; }
  .tools { display: none; flex: 1 1 100%; }
  .bar.menu-open .tools { display: flex; }
  .tools .group { flex: 1 1 100%; }
  .tools .group button { flex: 1 1 auto; min-height: 40px; }
  /* 16px stops iOS Safari from zooming the page when a field is focused */
  select, input, textarea { font-size: 16px; }
  main { grid-template-columns: 1fr; }
  .legend-btn { display: inline-block; }
  .legend { display: none; right: 64px; }
  .legend.show { display: flex; }
  .card { min-height: 36px; }
  .tg { min-width: 28px; height: 24px; line-height: 22px; bottom: -15px; }
  .actions button, .dlg-actions button { min-height: 42px; }
  dialog { padding: 16px; max-height: calc(100dvh - 32px); overflow: auto; }
  #panel {
    position: fixed; left: 0; right: 0; bottom: 0; max-height: 62vh; z-index: 20;
    border-left: 0; border-top: 1px solid var(--border); border-radius: 16px 16px 0 0;
    box-shadow: 0 -10px 30px rgba(0,0,0,.2); transform: translateY(100%); transition: transform .2s ease;
  }
  #panel.open { transform: none; }
  .close-panel { display: inline-block; }
  .legend .hint { display: none; }
}

@media print {
  .bar, #panel, .legend, .zoombar, .tg { display: none !important; }
  main { display: block; }
  #viewport { overflow: visible; background: none; }
  #tree { position: static; transform: none !important; }
  body { background: #fff; }
}

/* ───── Plan: banner, meter, prices ───── */
.plan-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 8px 16px; font-size: 13px;
  background: color-mix(in srgb, var(--accent) 10%, var(--surface)); border-bottom: 1px solid var(--border);
}
.plan-bar[hidden] { display: none; }
.plan-bar.warn { background: color-mix(in srgb, var(--danger) 10%, var(--surface)); color: var(--danger); }
.plan-bar.warn button { color: var(--ink); }
.meter { height: 12px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.meter.full i { background: var(--danger); }
.meter-label { margin: 6px 0 12px; font-size: 13px; }
.price-list { list-style: none; padding: 0; margin: 12px 0 8px; border: 1px solid var(--border); border-radius: 10px; }
.price-list li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--border); font-size: 13px; }
.price-list li:last-child { border-bottom: 0; }
.plan-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 4px; }
.plan-actions > * { flex: 1 1 160px; text-align: center; }
#dlgExtra a.btn { padding: 6px 12px; }

/* Names in any script: right-to-left names keep their own direction inside cards, chips and lists */
.card .nm, .chip, #searchResults li span { unicode-bidi: isolate; }

/* ───── Phones: dialogs become full-screen sheets, tables become cards ───── */
@media (max-width: 600px) {
  dialog, dialog.wide {
    width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0;
    border-radius: 0; border: 0; padding: 14px 16px 0;
    display: flex; flex-direction: column;
  }
  dialog:not([open]) { display: none; }
  #dlgForm { display: flex; flex-direction: column; flex: 1; min-height: 0; }
  #dlgBody { flex: 1; overflow: auto; min-height: 0; -webkit-overflow-scrolling: touch; }
  dialog h3 { font-size: 18px; padding-right: 8px; }
  .dlg-actions {
    position: sticky; bottom: 0; margin: 0 -16px; padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--border); flex-wrap: wrap;
  }
  .dlg-actions > button { flex: 1 1 0; }
  #dlgExtra { flex: 1 1 100%; display: flex; flex-wrap: wrap; gap: 6px; }
  #dlgExtra:empty { display: none; }
  #dlgExtra > * { flex: 1 1 auto; min-height: 42px; display: inline-flex; align-items: center; justify-content: center; }
  .f-row input, .f-row select, .f-row textarea { min-height: 44px; }
  .phone-row select { max-width: 45%; }
  .users-wrap, .users-wrap.tall { max-height: none; border: 0; overflow: visible; }
  .stat-row { grid-template-columns: 1fr 1fr; }
  .icon-btn { min-height: 36px; padding: 4px 10px; }

  /* Super Admin / billing tables: one card per row, each value labelled */
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr:has(th) { display: none; }
  table.cards tr { border: 1px solid var(--border); border-radius: 12px; padding: 8px 10px; margin-bottom: 8px; background: var(--surface-2); }
  table.cards td { border: 0; padding: 3px 0; text-align: left; }
  table.cards td.num { display: flex; justify-content: space-between; gap: 10px; text-align: right; }
  table.cards td[data-label]::before { content: attr(data-label); color: var(--muted); font-size: 12px; text-align: left; }
  table.cards td[data-label]:not(.num)::before { display: block; }
  table.cards td:first-child[data-label]::before { content: none; }
  table.cards td.nowrap { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 8px; }
  table.cards td.nowrap button { flex: 1 1 auto; }

  /* Users table (name · role · buttons, no header row): stack the cells. Tables with headers (history) stay tables. */
  table.users:not(.cards):not(:has(th)) tr { display: grid; grid-template-columns: 1fr auto; border-bottom: 1px solid var(--border); padding: 6px 0; }
  table.users:not(.cards):not(:has(th)) td { border: 0; }
  table.users:not(.cards):not(:has(th)) td.nowrap { grid-column: 1 / -1; display: flex; gap: 6px; justify-content: flex-start; }

  .plan-bar { padding: 8px 12px; }
  .fam-pick { min-height: 52px; }
}

/* ───── Interface language ───── */
.lang-select { max-width: 112px; padding: 4px 6px; font-size: 13px; }
#dlgExtra .lang-select { max-width: none; }
@media (max-width: 820px) { .lang-select { font-size: 16px; min-height: 40px; } }
html[lang=hi] body { font-family: "Nirmala UI", "Noto Sans Devanagari", "Mangal", system-ui, sans-serif; }
html[lang=ar] body { font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, sans-serif; }

/* Right-to-left (Arabic): the page mirrors, but the tree drawing keeps its left-to-right connector layout */
html[dir=rtl] #viewport { direction: ltr; }
html[dir=rtl] #viewport .legend { direction: rtl; }
html[dir=rtl] .brand { margin-right: 0; margin-left: auto; }
html[dir=rtl] #panel { border-left: 0; border-right: 1px solid var(--border); }
html[dir=rtl] .close-panel { float: left; }
html[dir=rtl] #panel h2 { text-align: right; padding-left: 44px; }
html[dir=rtl] #dlgExtra { margin-right: 0; margin-left: auto; }
html[dir=rtl] table.users th, html[dir=rtl] .fam-pick { text-align: right; }
html[dir=rtl] table.users .num, html[dir=rtl] table.users th.num, html[dir=rtl] .nowrap { text-align: left; }
html[dir=rtl] input[type=tel], html[dir=rtl] input[type=password], html[dir=rtl] input[inputmode=numeric], html[dir=rtl] input[inputmode=decimal] { direction: ltr; text-align: right; }
html[dir=rtl] .phone-row { direction: ltr; }

/* ───── Wood & leaves touches ───── */
#panel h2, dialog h3, .empty-tree h2 { font-family: var(--serif); color: #4e321c; }
#panel { background: rgba(251, 247, 238, .94); }
.plan-bar { background: var(--leaf-soft); }
.theme-btn { display: inline-grid; place-items: center; min-width: 40px; min-height: 40px; padding: 0; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); color: var(--ink); font-size: 17px; line-height: 1; cursor: pointer; }

/* ───── Night forest (dark theme, chosen with the 🌙 button) ───── */
html[data-theme=dark] {
  --bg: #1c1914; --ink: #efe3cf; --muted: #c3ab8f; --border: #4b3f31; --line: #8c7353; --accent: #d97a45; --accent-2: #bd6533; --accent-text: #f2a678; --accent-ink: #ffffff; --leaf: #8fb886; --leaf-ink: #a9d29f; --leaf-soft: #2e3a2a; --m-bg: #22323a; --m-bd: #7fa3b2; --f-bg: #3a2926; --f-bd: #c98f82; --danger: #f2817a; --ok: #a9d29f; color-scheme: dark;
  --surface: #2a241d; --surface-2: #322b22; --u-bg: #2c2a26; --u-bd: #8c8273; --dec-bg: #2a2622; --sel: #f0a04b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .45), 0 3px 10px rgba(0, 0, 0, .35);
  --wood: repeating-linear-gradient(96deg, rgba(255, 220, 170, .035) 0 2px, transparent 2px 11px), linear-gradient(180deg, #3a2c20, #2c2219);
}
html[data-theme=dark] body { background: radial-gradient(60rem 36rem at 0% 0%, rgba(80, 110, 70, .30), transparent 62%), radial-gradient(50rem 32rem at 100% 0%, rgba(70, 95, 60, .25), transparent 60%), radial-gradient(70rem 40rem at 50% 110%, rgba(110, 75, 40, .30), transparent 65%), linear-gradient(175deg, #1d211a 0%, #1f1c16 50%, #1b1712 100%); }
html[data-theme=dark] .brand h1, html[data-theme=dark] #panel h2, html[data-theme=dark] dialog h3, html[data-theme=dark] .empty-tree h2 { color: #f4e2c4; }
html[data-theme=dark] #panel { background: rgba(42, 36, 29, .95); }
html[data-theme=dark] #viewport { background-image: radial-gradient(rgba(220, 190, 150, .12) 1px, transparent 1px); }
html[data-theme=dark] dialog::backdrop { background: rgba(0, 0, 0, .55); }
@media print { html[data-theme=dark] body { background: #fff; color: #000; } }
