:root {
  --primary-color: #214b82;
  --line: #7ea2e8;
  --text: #111827;
  --muted: #5d6673;
  --border: #252a31;
  --shadow: 0 6px 16px rgba(0,0,0,.14);
  --node-bg: #ffffff;
  --node-soft: #eef4ff;
  --node-border: #214b82;
}

.flex-org-widget {
  width: 100%;
  min-height: 100vh;
  background: #fff;
  padding: 18px 12px 50px;
  overflow-x: hidden;
  direction: rtl;
  font-family: Tahoma, Arial, sans-serif;
  color: var(--text);
}

.flex-header { text-align: center; margin-bottom: 14px; }
.flex-logo { width: min(210px,70vw); height: auto; display: block; margin: 0 auto 8px; background: #fff; padding: 8px 12px; border-radius: 10px; border: 1px solid #e6e9ee; }
.flex-title { margin: 0; color: var(--primary-color); font-size: clamp(23px,2.6vw,36px); font-weight: 900; }
.flex-subtitle { margin: 8px 0 0; color: var(--muted); font-size: 13px; line-height: 1.8; }

.tree-toolbar { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin: 16px auto 20px; }
.tree-btn { border: 1px solid var(--primary-color); border-radius: 8px; padding: 9px 14px; font-family: inherit; font-weight: 900; cursor: pointer; color: var(--primary-color); background: #fff; transition: .2s; }
.tree-btn:hover { background: var(--primary-color); color: #fff; transform: translateY(-2px); }
.print-btn { border-color: #c56a22; color: #a84f0d; }
.print-btn:hover { background: #c56a22; color: #fff; }
.pdf-btn { border-color: #214b82; color: #fff; background: #214b82; }
.pdf-btn:hover { background: #153258; transform: translateY(-2px); color: #fff; }

.tree-shell { width: min(100%,1100px); margin: 0 auto; direction: rtl; }
.flex-tree { list-style: none; margin: 0; padding: 0; }

.node-wrap { display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative; z-index: 2; gap: 8px; }
.org-box {
  width: min(100%,330px); min-height: 60px; padding: 10px 14px; border: 1.8px solid var(--node-border); border-radius: 10px;
  background: linear-gradient(135deg,var(--node-bg),color-mix(in srgb,var(--node-bg) 74%,#fff));
  box-shadow: var(--shadow); display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  font-size: 14px; font-weight: 900; line-height: 1.55; cursor: pointer;
  transition: transform .22s ease,box-shadow .22s ease,background .22s ease,border-color .22s ease; color: #111;
}
.org-box .org-manager { display: block; font-size: 12px; color: var(--muted); font-weight: 600; margin-top: 4px; }
.org-box:hover {
  transform: translateY(-3px); box-shadow: 0 10px 20px color-mix(in srgb,var(--line) 28%,rgba(0,0,0,.12));
  background: linear-gradient(135deg,var(--node-soft),#fff);
}
.org-box.active { outline: 3px solid color-mix(in srgb,var(--line) 45%,transparent); transform: translateY(-3px); border-color: var(--line); }

/* Node Types */
.org-box.top { background: #f4b27d; border-color: var(--border); }
.org-box.exec { background: #d5d5d5; border-color: var(--border); }
.org-box.cream { background: #fde8d7; border-color: var(--border); }
.sub .org-box { background: linear-gradient(135deg,color-mix(in srgb,var(--node-bg) 68%,#fff),#fff); border-color: var(--node-border); width: min(100%,300px); min-height: 50px; font-size: 13px; }

.mini-btn { border: 1px solid #9aa8b9; background: #fff; border-radius: 7px; padding: 7px 10px; font-family: inherit; font-size: 12px; font-weight: 900; cursor: pointer; color: var(--primary-color); transition: .18s; white-space: nowrap; width: 100%; max-width: 120px; }
.mini-btn:hover { background: #fde8d7; color: #a84f0d; border-color: #d89563; }

/* Custom Department Colors */
.finance { --line: #1f6feb; --node-bg: #dbeafe; --node-border: #1f6feb; --node-soft: #eff6ff; }
.governance { --line: #7c3aed; --node-bg: #ede9fe; --node-border: #7c3aed; --node-soft: #f5f3ff; }
.hr { --line: #059669; --node-bg: #dcfce7; --node-border: #059669; --node-soft: #f0fdf4; }
.credit { --line: #dc2626; --node-bg: #fee2e2; --node-border: #dc2626; --node-soft: #fff1f2; }
.factories { --line: #ea580c; --node-bg: #ffedd5; --node-border: #ea580c; --node-soft: #fff7ed; }
.sales { --line: #0891b2; --node-bg: #cffafe; --node-border: #0891b2; --node-soft: #ecfeff; }
.procurement { --line: #9333ea; --node-bg: #f3e8ff; --node-border: #9333ea; --node-soft: #faf5ff; }
.it { --line: #2563eb; --node-bg: #dbeafe; --node-border: #2563eb; --node-soft: #eff6ff; }
.legal { --line: #475569; --node-bg: #e2e8f0; --node-border: #475569; --node-soft: #f8fafc; }
.research { --line: #16a34a; --node-bg: #dcfce7; --node-border: #16a34a; --node-soft: #f0fdf4; }
.custom-dept-color { --line: var(--dynamic-line); --node-bg: var(--dynamic-bg); --node-border: var(--dynamic-border); --node-soft: var(--dynamic-soft); }

/* DETAILS PANE */
.details-inline { display: none; width: min(920px,100%); margin: 22px auto 0; background: #fff; border: 1px solid #b9c4d2; border-radius: 14px; box-shadow: var(--shadow); padding: 16px; animation: fade .22s ease both; position: relative; z-index: 20; }
.details-inline.open { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }
.details-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; border-bottom: 1px solid #e1e6ee; padding-bottom: 10px; margin-bottom: 12px; }
.details-head h3 { margin: 0; color: var(--primary-color); font-size: 20px; font-weight: 900; }
.close-details { border: 1px solid #ddb093; background: #fff7ef; color: #a84f0d; border-radius: 7px; padding: 7px 12px; font-family: inherit; font-weight: 900; cursor: pointer; }
.details-meta { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.meta-pill { background: #eef4ff; color: var(--primary-color); border: 1px solid #cdd9ee; border-radius: 7px; padding: 8px 12px; font-weight: 900; font-size: 13px; }
.details-grid { display: grid; grid-template-columns: 1.1fr .9fr; gap: 12px; }
.details-box { background: #fbfdff; border: 1px solid #d7dee8; border-radius: 10px; padding: 13px; }
.details-box h4 { margin: 0 0 9px; color: #a84f0d; font-size: 15px; }
.details-box ul { margin: 0; padding: 0 20px 0 0; }
.details-box li { margin: 7px 0; line-height: 1.7; font-weight: 700; font-size: 13.5px; }


/* =========================================
   VERTICAL LAYOUT
   ========================================= */
.layout-vertical .flex-tree ul { list-style: none; margin: 0; padding: 0 34px 0 0; display: none; position: relative; }
.layout-vertical .flex-tree ul.open { display: block; animation: branchOpen .32s ease both; }
.layout-vertical .flex-tree ul::before {
  content: ""; position: absolute; top: 0; bottom: 20px; right: 16px; width: 3px; border-radius: 99px;
  background: var(--line); transform-origin: top; animation: growY .38s ease both;
  box-shadow: 0 0 0 3px color-mix(in srgb,var(--line) 12%,transparent);
}
.layout-vertical .flex-tree li { position: relative; margin: 12px 0 0; padding-right: 28px; }
.layout-vertical .flex-tree li::before {
  content: ""; position: absolute; top: 29px; right: 0; width: 28px; height: 3px; border-radius: 99px;
  background: var(--line); transform-origin: right; animation: growX .32s ease both;
  box-shadow: 0 0 0 3px color-mix(in srgb,var(--line) 12%,transparent);
}
.layout-vertical .flex-tree > li { padding-right: 0; margin-top: 0; }
.layout-vertical .flex-tree > li::before { display: none; }
.layout-vertical .node-wrap { flex-direction: row; align-items: center; justify-content: flex-start; }
.layout-vertical .mini-btn { margin-top: 0; width: auto; }

@keyframes branchOpen { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes growY { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes growX { from { transform: scaleX(0); } to { transform: scaleX(1); } }


/* =========================================
   HORIZONTAL LAYOUT
   ========================================= */
.layout-horizontal .tree-shell { width: 100%; overflow-x: auto; text-align: center; padding-bottom: 20px; }
.layout-horizontal .flex-tree { display: inline-flex; justify-content: center; padding: 20px 20px 0 20px; }
.layout-horizontal .flex-tree ul {
    padding: 20px 0 0 0; position: relative;
    display: none; justify-content: center;
}
.layout-horizontal .flex-tree ul.open { display: flex; animation: branchOpen .3s ease both; }
.layout-horizontal .flex-tree ul::before {
    content: ''; position: absolute; top: 0; left: 50%; transform: translateX(-50%);
    border-left: 2px solid var(--line);
    width: 0; height: 20px;
}
.layout-horizontal .flex-tree li {
    position: relative; padding: 20px 5px 0 5px; margin: 0; text-align: center; display: flex; flex-direction: column; align-items: center;
}
.layout-horizontal .flex-tree li::before,
.layout-horizontal .flex-tree li::after {
    content: ''; position: absolute; top: 0; width: 50%; height: 20px;
    border-top: 2px solid var(--line);
}
.layout-horizontal .flex-tree li::before { right: 50%; left: auto; }
.layout-horizontal .flex-tree li::after { left: 50%; right: auto; border-left: 2px solid var(--line); }
.layout-horizontal .flex-tree li:first-child::after { border-top: none; }
.layout-horizontal .flex-tree li:last-child::before { border-top: none; }
.layout-horizontal .flex-tree li:only-child::after,
.layout-horizontal .flex-tree li:only-child::before { display: none; }
.layout-horizontal .flex-tree li:only-child { padding-top: 0; }
.layout-horizontal .flex-tree > li { padding-top: 0; }
.layout-horizontal .flex-tree > li::before,
.layout-horizontal .flex-tree > li::after { display: none; }
.layout-horizontal .org-box { width: 220px; min-height: 65px; font-size: 13px; }


/* =========================================
   PRINT / PDF STYLES
   ========================================= */
@media print {
  @page { size: A4 landscape; margin: 10mm; }
  .layout-vertical @page { size: A4 portrait; }
  
  html, body { background: #fff!important; overflow: visible!important; }
  body * { visibility: hidden!important; }
  .flex-org-widget, .flex-org-widget * { visibility: visible!important; }
  .flex-org-widget { position: absolute!important; inset: 0!important; padding: 0!important; background: #fff!important; overflow: visible!important; }
  
  .tree-toolbar, .mini-btn, .close-details { display: none!important; }
  .flex-header { margin: 0 0 8mm!important; padding: 0 0 4mm!important; border-bottom: 1px solid #ccd6e2!important; }
  .flex-logo { width: 45mm!important; box-shadow: none!important; }
  .flex-title { font-size: 22pt!important; }
  .flex-subtitle { display: none!important; }
  
  .tree-shell { width: 100%!important; }
  
  /* Show all trees during print mode */
  .print-all-mode .flex-tree ul { display: flex!important; }
  .layout-vertical.print-all-mode .flex-tree ul { display: block!important; }
  
  .flex-tree ul:not(.open) { display: none!important; }
  
  .org-box { box-shadow: none!important; break-inside: avoid!important; }
  .details-inline.open { display: block!important; box-shadow: none!important; page-break-inside: avoid!important; }
  .details-inline:not(.open) { display: none!important; }
  
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
}

@media(max-width:700px) {
  .layout-vertical .flex-tree ul { padding-right: 22px; }
  .layout-vertical .flex-tree ul::before { right: 8px; }
  .layout-vertical .flex-tree li { padding-right: 20px; }
  .layout-vertical .flex-tree li::before { right: 0; width: 20px; }
  .org-box { width: 100%; font-size: 13px; }
  .details-grid { grid-template-columns: 1fr; }
  .details-meta { flex-direction: column; }
  .tree-btn { font-size: 12px; padding: 8px 10px; }
}
