/* Organizational chart (الهيكل التنظيمي.jpg).
   A trunk line runs down the inline-start edge of the chart from the minister; the rows of
   affiliated bodies and offices hang from it, and each department gets an arrow from it. */

.org-chart {
  --line: #CBD8E0;
  --trunk-pad: 55px;      /* room between the trunk and the cards */
  --stub: 26px;           /* bar → card top */
  max-width: 1440px; margin-inline: auto; padding: 71px 32px 176px;
}
.org-chart__visual { position: relative; }
.org-chart__note { margin-top: 48px; color: var(--text-2); text-align: center; font-size: var(--fs-small); }

/* ---- legend */
.org-legend {
  position: absolute; top: -14px; inset-inline-end: 25px; z-index: 1; display: grid; gap: 6px; width: max-content; min-width: 130px;
  padding: 13px 30px 15px 10px; list-style: none; background: #fff; border: 2px solid #C9D2DB; border-radius: var(--r-md);
  color: var(--gold-strong); font-size: var(--fs-ui); font-weight: 700; line-height: 1.45;
}
.org-legend__item { display: flex; align-items: center; gap: 8px; }
.org-legend__dot { width: 18px; height: 18px; border-radius: 50%; flex: none; background: var(--card); }
.org-legend__dot--white { background: #fff; border: 1px solid #4867A7; }

/* ---- cards */
.org-node {
  position: relative; display: flex; flex-direction: column; align-items: center; width: 220px; min-height: 174px; padding: 9px 10px 12px;
  border-radius: var(--r-md); background: var(--card); color: #fff; text-align: center;
}
.org-node__avatar {
  display: block; width: 86px; aspect-ratio: 1; overflow: hidden; border-radius: 50%; flex: none;
  background: var(--avatar); color: var(--figure, #fff);
}
.org-node__avatar img { width: 100%; height: 100%; object-fit: cover; }
.org-node__placeholder { width: 100%; height: 100%; fill: currentColor; }
.org-node__name { flex: 1; display: flex; align-items: center; padding-block: 6px; font-size: var(--fs-small); font-weight: 700; line-height: 1.45; }
.org-node__label { font-size: var(--fs-xs); line-height: 1.5; }

.org-node--gold, .org-legend__dot--gold { --card: var(--gold); --avatar: var(--selected-bg); --figure: var(--gold-strong); }
/* White text on gold (2.6:1) and orange (3.4:1) failed AA: these two fills carry the text colour. */
.org-node--gold, .org-node--orange { color: var(--text); }
.org-node--maroon, .org-legend__dot--maroon { --card: #9C1E1F; --avatar: #6B0405; }
.org-node--orange, .org-legend__dot--orange { --card: #D17208; --avatar: #7A4710; }
.org-node--teal, .org-legend__dot--teal { --card: #01677E; --avatar: #0692B5; }
.org-node--turquoise, .org-legend__dot--turquoise { --card: #00796B; --avatar: #26A69A; } /* «أمانة سر مدير المكتب» (478:30554); white text 5.3:1 */
.org-node--green, .org-legend__dot--green { --card: #39770A; --avatar: #7BA901; }
.org-node--olive, .org-legend__dot--olive { --card: #8A5B00; --avatar: #B09252; }
.org-node--brown, .org-legend__dot--brown { --card: #A15000; --avatar: #D79875; }
.org-node--navy, .org-legend__dot--navy { --card: #14286E; --avatar: #0B1A4A; }
.org-node--blue, .org-legend__dot--blue { --card: #0563A0; --avatar: #054873; }
.org-node--white { --card: #fff; --avatar: #E8EFF5; --figure: #B1C3CF; color: var(--text); border: 1px solid #2D519B; }
/* In a four-section department with units, the sections without a full row of units are drawn narrower, with a lighter, thicker edge. */
@media (min-width: 641px) { .org-dept:has(.org-dept__col:nth-child(4)):has(.org-dept__units) .org-dept__col:not(:has(.org-dept__units > :nth-child(4))) > .org-node--white { width: 198px; border: 2px solid #94A6CA; } } /* desktop only, so the phone tree needs no !important */
.org-node--white .org-node__label { color: var(--text-2); }

.org-node--root { width: 280px; min-height: 289px; padding: 17px 16px 22px; }
.org-node--root .org-node__avatar { width: 148px; }
.org-node--root .org-node__name { font-size: var(--fs-lead); }
.org-node--root .org-node__label { font-size: var(--fs-xs); }
.org-node--leader { width: 222px; min-height: 233px; padding: 17px 12px 20px; }
.org-node--leader .org-node__avatar, .org-node--dept .org-node__avatar { width: 94px; }
.org-node--leader-kid { width: 194px; min-height: 180px; border: 1px solid var(--line); }
.org-node--dept { width: 248px; min-height: 204px; }
.org-node--dept .org-node__name { padding-block: 10px; }
.org-node--section { width: 226px; max-width: 100%; min-height: 191px; padding-top: 10px; }
.org-node--section .org-node__name { font-size: var(--fs-small); }
.org-node--unit { width: 196px; min-width: 0; min-height: 174px; flex: 0 1 auto; }
.org-node--unit .org-node__avatar { width: 86px; }
.org-node--unit .org-node__name { font-size: var(--fs-small); }
@media (min-width: 1240px) {
  /* A department with four sections has narrower units, as drawn (they keep their size instead of shrinking). */
  .org-dept__units > .org-node--unit { flex-shrink: 0; }
  /* Departments with fewer sections are drawn with 30 px between units and between section groups. */
  .org-dept:not(:has(.org-dept__col:nth-child(4))) :is(.org-dept__units, .org-dept__sections) { gap: 30px; --half-gap: 15px; }
  .org-dept:has(.org-dept__col:nth-child(4)) .org-node--unit { width: 156px; }
  /* As drawn, a row of four or more units reaches 117 px under a neighbouring section that has no units. */
  .org-dept__col--leaf + .org-dept__col:not(.org-dept__col--leaf):has(.org-dept__units > :nth-child(4)) { margin-inline-start: -117px; }
}
.org-dept:has(.org-dept__col:nth-child(4)) .org-node--unit .org-node__avatar { width: 78px; }
.org-dept:has(.org-dept__col:nth-child(4)) .org-node--unit .org-node__name { font-size: var(--fs-xs); }

/* ---- connectors (desktop) */
@media (min-width: 1240px) {
  /* a stub from the bar above, with the arrowhead touching the card */
  .org-node--arrow::after {
    content: ""; position: absolute; bottom: 100%; left: 50%; width: 24px; height: var(--stub); margin-left: -12px;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='26'%3E%3Cpath d='M12 0v25M1.5 15.5 12 25l10.5-9.5' fill='none' stroke='%23CBD8E0' stroke-width='3'/%3E%3C/svg%3E") no-repeat bottom center / 24px 26px;
  }
  .org-row--dashed .org-node--arrow::after {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='26'%3E%3Cpath d='M12 0v25' fill='none' stroke='%23CBD8E0' stroke-width='3' stroke-dasharray='4.5 4.5'/%3E%3Cpath d='M1.5 15.5 12 25l10.5-9.5' fill='none' stroke='%23CBD8E0' stroke-width='3'/%3E%3C/svg%3E");
  }

  /* the trunk: one segment per level, each reaching up through the gap above it */
  .org-top::before, .org-row::before, .org-dept::before, .org-dept:last-child .org-dept__head::before {
    content: ""; position: absolute; inset-inline-start: -2px; width: 3px; top: calc(-1 * var(--gap-above, 0px)); bottom: 0; background: var(--line);
  }
  .org-top::before { top: 131px; }
  .org-dept:last-child::before { display: none; }
  .org-dept:last-child .org-dept__head::before { inset-inline-start: calc(-1 * var(--trunk-pad) - 2px); bottom: 50%; }

  /* minister → trunk */
  .org-top__root::after {
    content: ""; position: absolute; top: 131px; height: 3px; inset-inline-start: -2px; inset-inline-end: calc(50% + 140px); background: var(--line);
  }

  /* minister → leaders: stem, bar, one stub per leader */
  .org-leaders::before { content: ""; position: absolute; top: 0; left: 50%; width: 3px; height: 34px; margin-left: -2px; background: var(--line); }
  .org-leaders__branch::before {
    content: ""; position: absolute; top: -34px; height: 3px; inset-inline: -98px; background: var(--line);
  }
  .org-leaders__branch:first-child::before { inset-inline-start: calc(50% - 2px); }
  .org-leaders__branch:last-child::before { inset-inline-end: calc(50% - 2px); }
  .org-leaders__branch:only-child::before { display: none; }

  /* trunk → a row of five: the bar ends over the last card's centre */
  .org-row::after {
    content: ""; position: absolute; top: 0; height: 3px; inset-inline-start: -2px; background: var(--line);
    inset-inline-end: calc(114px + var(--empty, 0) * (220px + (100% - 1160px) / 4));
  }
  .org-row--4 { --empty: 1; } .org-row--3 { --empty: 2; } .org-row--2 { --empty: 3; } .org-row--1 { --empty: 4; }
  .org-row--dashed::after { background: repeating-linear-gradient(to left, var(--line) 0 4px, transparent 4px 9px); }
  [dir="ltr"] .org-row--dashed::after { background: repeating-linear-gradient(to right, var(--line) 0 4px, transparent 4px 9px); }

  /* trunk → department, arrowhead at the card */
  .org-dept__head::after {
    content: ""; position: absolute; top: 50%; height: 24px; margin-top: -12px;
    inset-inline-start: calc(-1 * var(--trunk-pad) - 2px); inset-inline-end: calc(50% + 124px);
    background:
      url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='24'%3E%3Cpath d='M11 1.5 1.5 12l9.5 10.5' fill='none' stroke='%23CBD8E0' stroke-width='3'/%3E%3C/svg%3E") no-repeat left center / 12px 24px,
      linear-gradient(var(--line), var(--line)) no-repeat right center / calc(100% - 1px) 3px;
  }
  [dir="ltr"] .org-dept__head::after { transform: scaleX(-1); }

  /* department → sections and section → units: stem, bar from the first to the last centre, stubs */
  .org-dept__sections::before, .org-dept__units::before {
    content: ""; position: absolute; top: 0; left: 50%; width: 3px; height: var(--stem); margin-left: -2px; background: var(--line);
  }
  .org-dept__col::before, .org-dept__units > .org-node::before {
    content: ""; position: absolute; top: calc(-1 * var(--stub)); height: 3px; inset-inline: calc(-1 * var(--half-gap)); background: var(--line);
  }
  .org-dept__col:first-child::before, .org-dept__units > .org-node:first-child::before { inset-inline-start: calc(50% - 2px); }
  .org-dept__col:last-child::before, .org-dept__units > .org-node:last-child::before { inset-inline-end: calc(50% - 2px); }
  .org-dept__col:only-child::before, .org-dept__units > .org-node:only-child::before { display: none; }
}

/* ---- minister and leadership */
.org-top, .org-row, .org-dept { position: relative; }
.org-top__root { position: relative; display: flex; justify-content: center; }
.org-leaders { position: relative; display: flex; justify-content: center; align-items: flex-start; gap: 192px; padding-top: 68px; }
.org-leaders__branch { position: relative; display: grid; justify-items: center; gap: 32px; --stub: 34px; }
.org-leaders__branch > .org-node--leader-kid { --stub: 32px; }

/* ---- rows of affiliated bodies and offices: five per row */
.org-row {
  display: grid; grid-template-columns: repeat(5, 220px); justify-content: space-between;
  margin-top: var(--gap-above); padding: var(--stub) 5px 0 0; padding-inline: var(--trunk-pad) 5px;
  --gap-above: 38px;
}
.org-top + .org-row { --gap-above: 30px; }
.org-row--dashed + .org-row { --gap-above: 44px; }
.org-row--dashed .org-node { min-height: 182px; }

/* ---- departments */
.org-dept { margin-top: var(--gap-above); padding-inline: var(--trunk-pad) 5px; --gap-above: 92px; }
.org-row + .org-dept { --gap-above: 102px; }
.org-dept__head { position: relative; display: flex; justify-content: center; }
.org-dept__sections { position: relative; display: flex; gap: 12px; padding-top: calc(var(--stem) + var(--stub)); --stem: 38px; --half-gap: 6px; }
.org-dept__col.org-dept__col--leaf { min-width: 196px; }
.org-dept__col { position: relative; flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; align-items: center; }
.org-dept__units { position: relative; display: flex; justify-content: center; gap: 12px; width: 100%; padding-top: calc(var(--stem) + var(--stub)); --stem: 40px; --half-gap: 6px; }

/* ---- smaller screens: a stacked chart without connectors */
@media (max-width: 1239px) {
  .org-chart { padding: 48px var(--gutter) 80px; }
  .org-legend { position: static; width: auto; display: flex; flex-wrap: wrap; gap: 8px 20px; margin-bottom: 32px; padding: 12px 16px; }
  .org-leaders { flex-wrap: wrap; gap: 24px; padding-top: 32px; }
  .org-row { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 24px 16px; padding: 0; --gap-above: 48px; }
  .org-top + .org-row, .org-row--dashed + .org-row { --gap-above: 48px; }
  .org-row .org-node { width: auto; }
  .org-dept { padding-inline: 0; --gap-above: 64px; }
  .org-row + .org-dept { --gap-above: 64px; }
  .org-dept__sections { flex-wrap: wrap; justify-content: center; gap: 32px 16px; padding-top: 32px; }
  .org-dept__col { flex: 1 1 220px; }
  .org-dept__units { flex-wrap: wrap; gap: 12px; padding-top: 16px; }
  .org-dept__units .org-node { flex: 0 1 160px; min-height: 150px; }
}
@media (max-width: 640px) {
  .org-node--root { width: 240px; min-height: 0; }
  .org-node--root .org-node__avatar { width: 112px; }
  .org-node--leader, .org-node--leader-kid { min-height: 0; }
  .org-row { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px 12px; }
  .org-node { min-height: 0; padding-inline: 8px; }
  .org-node--section { width: 100%; }
  .org-dept__units .org-node { flex-basis: calc(50% - 6px); }
}

/* Phones: an indented tree. Each level hangs from a line at the inline start,
   and every card is a full-width row (avatar, then name and role), so the order and depth read top to bottom. */
@media (max-width: 640px) {
  .org-chart { padding-inline: var(--gutter); }
  .org-chart :is(.org-top__root, .org-leaders, .org-leaders__branch, .org-row, .org-dept__head, .org-dept__sections, .org-dept__col, .org-dept__units) {
    display: flex; flex-direction: column; flex-wrap: nowrap; align-items: stretch; gap: var(--sp-2); width: auto; height: auto; padding: 0; margin: 0;
  }
  .org-chart :is(.org-leaders, .org-row, .org-dept__sections, .org-dept__units) {
    margin-top: var(--sp-2); padding-inline-start: var(--sp-4); border-inline-start: 2px solid var(--line);
  }
  .org-chart .org-dept { margin-top: var(--sp-5); padding: 0; }
  .org-chart .org-dept__sections { gap: var(--sp-3); }
  .org-chart :is(.org-top, .org-row, .org-dept, .org-dept__head, .org-dept__sections, .org-dept__units, .org-top__root, .org-dept__col)::before,
  .org-chart :is(.org-top, .org-row, .org-dept, .org-dept__head, .org-dept__sections, .org-dept__units, .org-top__root, .org-dept__col)::after,
  .org-chart .org-node::before, .org-chart .org-node::after { display: none; }
  .org-chart .org-node {
    display: grid; grid-template-columns: 48px 1fr; grid-template-rows: auto auto; column-gap: var(--sp-3); align-items: center;
    width: 100%; max-width: none; min-height: 0; margin: 0; padding: var(--sp-2) var(--sp-3); text-align: start;
  }
  .org-chart .org-node__avatar { grid-row: span 2; width: 48px; }
  .org-chart .org-node__name { padding: 0; font-size: var(--fs-ui); line-height: 1.4; }
  .org-chart .org-node__label { font-size: var(--fs-xs); }
  .org-chart .org-node:not(:has(.org-node__label)) .org-node__avatar { grid-row: auto; }
  .org-chart .org-node--text { grid-template-columns: minmax(0, 1fr); } /* no portrait: the name takes the row (R-79) */
  .org-chart .org-dept .org-node { width: 100%; border-width: 1px; }
  .org-chart :is(.org-node, .org-dept__col, .org-dept__units .org-node) { flex: none; min-height: 0; } /* the tablet rules size these as flex items in a row */
  .org-chart .org-node .org-node__avatar { width: 48px; height: 48px; } /* the desktop rule that narrows some section cards */
}

/* «أمانة سر مدير المكتب» (478:30554): its ten sections in rows of three under it, joined by one stem, not a bar per box */
.org-dept--secretariat .org-dept__sections { display: grid; grid-template-columns: repeat(3, minmax(0, 240px)); justify-content: center; gap: 50px clamp(24px, 8vw, 128px); }  /* 478:30554 */
.org-dept--secretariat .org-dept__sections::before, .org-dept--secretariat .org-dept__col::before { display: none; }
.org-dept--secretariat .org-dept__sections::after { content: ""; position: absolute; top: 0; inset-inline-start: calc(50% - 1px); width: 2px; height: calc(var(--stem) + var(--stub)); background: var(--line); }
.org-dept--secretariat .org-dept__col.org-dept__col--leaf { min-width: 0; }
/* wide screens: a bar over each row of three, fed by a line on the start side, as drawn */
@media (min-width: 1240px) {
  .org-dept--secretariat .org-dept__sections { --g: clamp(24px, 8vw, 128px); gap: 50px var(--g); }
  .org-dept--secretariat .org-dept__col::before { display: block; content: ""; position: absolute; top: calc(-1 * var(--stub)); height: 3px; background: var(--line); inset-inline: calc(-.5 * var(--g) - 2px) calc(-.5 * var(--g)); }
  .org-dept--secretariat .org-dept__col:is(:nth-child(3n), :last-child)::before { inset-inline-end: calc(50% - 2px); }
  .org-dept--secretariat .org-dept__col:nth-child(3n+1):not(:first-child)::after { content: ""; position: absolute; inset-inline-start: calc(-.5 * var(--g) - 2px); width: 3px; top: calc(-100% - 50px - var(--stub)); height: calc(100% + 50px + 3px); background: var(--line); }
}
@media (max-width: 700px) {
  .org-dept--secretariat .org-dept__sections { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .org-dept--secretariat .org-dept__col > .org-node { width: 100%; }
  .org-dept--secretariat :is(.org-node__name, .org-node__label) { width: auto; max-width: 100%; overflow-wrap: anywhere; }
}

/* Units without a portrait are text cards (R-79): the name takes the whole card, never under 14 px. */
.org-node--text .org-node__name { justify-content: center; text-align: center; }
.org-node--text { min-height: 72px; justify-content: center; }
.org-node .org-node__name, .org-dept:has(.org-dept__col:nth-child(4)) .org-node--unit .org-node__name { font-size: max(var(--fs-small), 14px); }
