:root {
  --font: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  --bg: #f4f7f1;
  --card: #ffffff;
  --ink: #1d2a1d;
  --muted: #637263;
  --line: #dfe7da;
  --green: #2e7d32;
  --green-2: #5fa83a;
  --green-soft: #e8f3e1;
  --blue: #1f5cc2;
  --brown: #7a4b25;
  --gold: #f0c440;
  --danger: #c0392b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 40, 20, .06), 0 4px 16px rgba(20, 40, 20, .06);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--blue); }
.ic { flex: none; vertical-align: middle; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- الهيكل ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, .92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; color: var(--green); text-decoration: none; }
.brand .ic { color: var(--green-2); }
.nav { display: flex; gap: 4px; margin-inline-start: auto; }
.nav a, .nav button {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: 10px;
  color: var(--muted); text-decoration: none; font-weight: 500; font-size: 15px;
  background: none; border: 0; cursor: pointer;
}
.nav a:hover, .nav button:hover { background: var(--green-soft); color: var(--green); }
.nav a.active { background: var(--green-soft); color: var(--green); }
.who { font-size: 13px; color: var(--muted); white-space: nowrap; }
.demo-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; justify-content: center;
  padding: 6px 16px; font-size: 13px;
  background: #fff7dc; color: #6b5310; border-bottom: 1px solid #f1e2a8;
}
.demo-bar button { background: none; border: 0; color: #6b5310; text-decoration: underline; cursor: pointer; padding: 0; font-size: 13px; }
main { max-width: 1240px; margin: 0 auto; padding: 20px 16px 100px; }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; justify-content: space-between; margin-bottom: 16px; }
.page-head h1 { margin: 0; font-size: 24px; }
.page-head p { margin: 2px 0 0; color: var(--muted); }

/* شريط سفلي للجوال */
.bottomnav { display: none; }
@media (max-width: 760px) {
  .nav, .who { display: none; }
  .topbar { padding: 10px 16px; }
  .bottomnav {
    display: flex; position: fixed; bottom: 0; inset-inline: 0; z-index: 30;
    background: #fff; border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .bottomnav a, .bottomnav button {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    font-size: 11px; color: var(--muted); text-decoration: none; padding: 4px 2px;
    background: none; border: 0;
  }
  .bottomnav a.active { color: var(--green); }
  .page-head h1 { font-size: 21px; }
}

/* ---------- عناصر عامة ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: #fff; cursor: pointer; font-weight: 600; font-size: 15px;
  text-decoration: none; color: var(--ink);
}
.btn:hover { border-color: var(--green-2); }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: #276b2a; }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 14px; }
.btn.ghost { background: none; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--green); background: var(--green-soft); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 600; }
.input, select.input, textarea.input {
  width: 100%; min-height: 42px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.input:focus, .btn:focus-visible, .node:focus-visible { outline: 3px solid rgba(95, 168, 58, .45); outline-offset: 1px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.muted { color: var(--muted); }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px; background: var(--green-soft); color: var(--green); font-size: 12px; font-weight: 600; }
.chip.gray { background: #eef1ec; color: var(--muted); }
.score-pill { display: inline-block; min-width: 46px; text-align: center; padding: 2px 8px; border-radius: 999px; font-size: 13px; font-weight: 700; }
.bar { height: 8px; border-radius: 99px; background: #eef1ec; overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 99px; }
#toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #1d2a1d; color: #fff; padding: 10px 18px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- صفحة الشجرة ---------- */
.tree-layout { display: grid; grid-template-columns: 1fr 360px; gap: 16px; align-items: start; }
.tree-wrap { border-radius: 18px; overflow: hidden; border: 1px solid var(--line); box-shadow: var(--shadow); background: #cdeafb; }
.tree-svg { display: block; width: 100%; height: auto; }
.tree-svg .node { cursor: pointer; }
.tree-svg .node:hover path, .tree-svg .node:hover rect { filter: brightness(1.06); }
.panel { position: sticky; top: 76px; max-height: calc(100vh - 96px); overflow: auto; }
.panel h2 { margin: 0 0 4px; font-size: 20px; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 12px; color: var(--muted); margin-top: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend i { width: 14px; height: 14px; border-radius: 4px 50%; display: inline-block; }
.view-toggle { display: inline-flex; background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.view-toggle button { border: 0; background: none; padding: 6px 12px; border-radius: 8px; cursor: pointer; display: flex; gap: 6px; align-items: center; color: var(--muted); font-weight: 600; }
.view-toggle button.on { background: var(--green-soft); color: var(--green); }
.leaf-row { padding: 10px 0; border-top: 1px solid var(--line); }
.leaf-row:first-child { border-top: 0; }
.leaf-row .top { display: flex; align-items: center; gap: 8px; }
.leaf-row .title { flex: 1; font-weight: 600; }
.leaf-dot { width: 16px; height: 16px; border-radius: 2px 70% 2px 70%; flex: none; border: 1px solid rgba(0, 0, 0, .15); }
.days { display: grid; grid-template-columns: repeat(15, 1fr); gap: 3px; margin-top: 8px; direction: ltr; }
.days i { aspect-ratio: 1; border-radius: 3px; background: #eef1ec; }
.days i.on { background: var(--green-2); }
.days i.part { background: #c7df9f; }
.sub-title { font-weight: 700; margin: 14px 0 4px; display: flex; align-items: center; gap: 8px; }
.sub-title:first-child { margin-top: 0; }
@media (max-width: 1000px) {
  .tree-layout { grid-template-columns: 1fr; }
  .panel { position: static; max-height: none; }
}

/* عرض القائمة */
.tree-list details { border: 1px solid var(--line); border-radius: 12px; background: #fff; margin-bottom: 8px; }
.tree-list summary { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 10px; padding: 12px 14px; font-weight: 700; }
.tree-list summary::-webkit-details-marker { display: none; }
.tree-list .body { padding: 0 14px 12px; }
.trunk-ic { width: 34px; height: 34px; border-radius: 50%; background: #f6d27a; color: #5a3b0c; display: grid; place-items: center; flex: none; }

/* ---------- صفحة اليوم ---------- */
.day-nav { display: flex; align-items: center; gap: 8px; }
.day-nav .date { text-align: center; min-width: 170px; }
.day-nav .date b { display: block; }
.day-nav .date small { color: var(--muted); }
.flip { transform: scaleX(-1); }
.progress-card { display: flex; align-items: center; gap: 18px; margin-bottom: 16px; }
.ring { --p: 0; width: 76px; height: 76px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: conic-gradient(var(--green-2) calc(var(--p) * 1%), #e6ece2 0); }
.ring span { width: 60px; height: 60px; border-radius: 50%; background: #fff; display: grid; place-items: center; font-weight: 700; }
.group { margin-bottom: 16px; }
.group h3 { display: flex; align-items: center; gap: 10px; margin: 0 0 8px; font-size: 17px; }
.task {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 14px; background: #fff; border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px;
}
.task.done { background: #f1f8ec; border-color: #c9e3b6; }
.task .t { flex: 1 1 180px; min-width: 0; }
.task .t b { display: block; font-weight: 600; }
.task .t small { color: var(--muted); }
.tick {
  width: 40px; height: 40px; border-radius: 12px; border: 2px solid var(--line); background: #fff;
  display: grid; place-items: center; cursor: pointer; color: transparent; flex: none;
}
.tick.on { background: var(--green); border-color: var(--green); color: #fff; }
.counter { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.counter .val { min-width: 74px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.add-amount { display: flex; gap: 6px; align-items: center; }
.add-amount .input { width: 90px; min-height: 36px; }

/* ---------- صفحة التعديل ---------- */
.edit-trunk { margin-bottom: 14px; }
.edit-trunk > h3 { display: flex; align-items: center; gap: 10px; margin: 0 0 10px; }
.edit-branch { border-top: 1px dashed var(--line); padding: 10px 0; }
.edit-branch .bh { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.edit-branch .bh .btn { margin-inline-start: auto; }
.edit-leaf { display: flex; align-items: center; gap: 8px; padding: 6px 10px; margin: 6px 0; background: #f8faf6; border-radius: 10px; }
.edit-leaf .name { flex: 1; }
.edit-branch .edit-branch { margin-inline-start: 18px; }
.leaf-form { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr; gap: 8px; padding: 10px; margin: 8px 0; background: var(--green-soft); border-radius: 12px; }
.leaf-form .full { grid-column: 1 / -1; }
.leaf-form .actions { grid-column: 1 / -1; display: flex; gap: 8px; }
@media (max-width: 640px) { .leaf-form { grid-template-columns: 1fr 1fr; } .leaf-form .wide { grid-column: 1 / -1; } }

/* ---------- المشرف ---------- */
.members { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 12px; }
.member { display: block; text-decoration: none; color: inherit; }
.member:hover { border-color: var(--green-2); }
.member .head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.avatar { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; flex: none; }
.member .name { font-weight: 700; font-size: 17px; }
.mini { display: grid; grid-template-columns: 70px 1fr 40px; gap: 4px 8px; align-items: center; font-size: 12px; }
.mini .n { color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat b { display: block; font-size: 26px; }
.stat span { color: var(--muted); font-size: 13px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters .input { width: auto; min-width: 140px; }

/* ---------- الدخول ---------- */
.auth { max-width: 940px; margin: 30px auto; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.auth h2 { margin-top: 0; }
.hero { text-align: center; margin: 20px auto 0; max-width: 620px; }
.hero h1 { font-size: 34px; margin: 8px 0; color: var(--green); }
.hero p { color: var(--muted); font-size: 17px; margin: 0; }
.user-pick { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: #fff; cursor: pointer; margin-bottom: 8px; text-align: start; }
.user-pick:hover { border-color: var(--green-2); background: #fafdf8; }
.error { color: var(--danger); font-size: 14px; min-height: 1.4em; }
@media (max-width: 760px) { .auth { grid-template-columns: 1fr; } .hero h1 { font-size: 28px; } }

/* ---------- القالب ---------- */
.tpl-row { display: flex; gap: 8px; align-items: center; margin: 6px 0; }
.tpl-row .input { min-height: 36px; }
.tpl-children { margin-inline-start: 22px; border-inline-start: 2px solid var(--line); padding-inline-start: 12px; }

/* ---------- حركة نمو الشجرة ---------- */
.tree-svg .lf-in { transition: filter .15s; }
.tree-svg .pill:hover .lf-in { filter: brightness(1.08) saturate(1.1); }
.tree-svg .pop { transform-box: fill-box; transform-origin: center; }
.tree-svg .node:hover .pop rect, .tree-svg .trunk-label:hover rect { filter: brightness(1.07) drop-shadow(0 3px 4px rgba(0,0,0,.25)); }
.tree-svg .trunk-label.empty { opacity: .72; }
.tree-svg .trunk-label.empty:hover { opacity: 1; }
@keyframes leaf-grow { from { transform: scale(0) rotate(-25deg); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes pop-in { from { transform: scale(.4); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes sway { 0%, 100% { transform: rotate(0); } 50% { transform: rotate(4deg); } }
.tree-svg.grow .lf-in { animation: leaf-grow .55s cubic-bezier(.3, 1.4, .5, 1) both; }
.tree-svg.grow .pop { animation: pop-in .4s ease-out both; }
.tree-svg.grow .br { animation: fade-in .5s ease-out both; }
.tree-svg:not(.grow) .lf-in, .tree-svg.grow .lf-in { transform-origin: 0 0; }
.tree-svg .pill .lf-in { animation-fill-mode: both; }
.tree-svg .pill:hover .lf-in { animation: sway 1.2s ease-in-out infinite; animation-delay: 0s !important; }
@media (prefers-reduced-motion: reduce) {
  .tree-svg .lf-in, .tree-svg .pop, .tree-svg .br { animation: none !important; }
}
.empty-branches { margin-top: 12px; padding: 10px 12px; background: #f6f8f3; border-radius: 10px; font-size: 13px; color: var(--muted); }
