:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #1c2230;
  --muted: #67708a;
  --line: #c8cee0;
  --accent: #2f6df4;
  --accent-ink: #ffffff;
  --danger: #c0392b;
  --shadow: 0 1px 2px rgba(20, 30, 60, 0.06),
            0 4px 12px rgba(20, 30, 60, 0.06);
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  min-height: 100%;
}

/* ---------------- Topbar ---------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand {
  font-weight: 600;
  font-size: 16px;
  letter-spacing: 0.2px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.control {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.control select,
.control input {
  font-size: 14px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  min-width: 180px;
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 6px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
}

button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
}

button.ghost { background: #fff; }
button.danger { color: var(--danger); border-color: #e3b8b3; }
button.icon {
  border: 0;
  background: transparent;
  font-size: 18px;
  padding: 4px 8px;
}

button:disabled { opacity: 0.6; cursor: not-allowed; }

/* ---------------- Status ---------------- */

.status {
  padding: 10px 20px;
  color: var(--muted);
  font-size: 13px;
}
.status.error { color: var(--danger); }

/* ---------------- Chart ---------------- */

.chart {
  padding: 0;
  overflow: hidden;
  /* Fill the remaining viewport below the topbar */
  height: calc(100vh - 57px);
  cursor: grab;
  user-select: none;
}
.chart.grabbing { cursor: grabbing; }

.chart-inner {
  padding: 40px 40px 80px;
  /* width/height expand to fit the tree — don't constrain it */
  width: max-content;
  min-width: 100%;
}

/* The org tree: each .node renders itself plus a row of children below.
   Connectors between parent and children are pure CSS via ::before /
   ::after on .children and its <li>. */

.tree, .tree ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tree {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
  align-items: flex-start;
}

.tree > li {
  /* Each top-level subtree */
  display: inline-block;
  vertical-align: top;
  margin: 0 6px;
}

.tree li {
  position: relative;
  padding: 16px 8px 0;
  text-align: center;
}

/* Vertical line from parent card down to the children's horizontal bar */
.tree li.has-children > .children::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  width: 0;
  height: 16px;
  border-left: 1px solid var(--line);
}

/* Horizontal line above each child (drawn on the child) */
.tree li:not(.root)::before,
.tree li:not(.root)::after {
  content: "";
  position: absolute;
  top: 0;
  height: 16px;
  border-top: 1px solid var(--line);
}
.tree li:not(.root)::before {
  left: 0;
  right: 50%;
}
.tree li:not(.root)::after {
  left: 50%;
  right: 0;
}
/* First / last child: trim the horizontal line so only the inner half shows */
.tree li:not(.root):first-child::before { border-top: 0; }
.tree li:not(.root):last-child::after { border-top: 0; }

/* Vertical drop from the horizontal line down to the card */
.tree li:not(.root)::before {
  border-right: 1px solid var(--line);
  /* draw the small vertical down-stub on the right edge of ::before,
     positioned at the card's center */
}

/* Children container — a flex row */
.children {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  position: relative;
  padding-top: 16px;
}

/* team-cluster: a li inside ul.children that holds a dept team box.
   Gets standard connector lines like any other child node. */
.team-cluster {
  vertical-align: top;
}

/* Leaf grid inside a team box — up to 3 cards per row, fewer if less items */
.leaf-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 200px));
  max-width: calc(3 * 200px + 2 * 8px);
  gap: 8px;
}
/* Individual leaf cards inside the grid — no tree connectors */
.leaf-grid > li {
  padding: 0;
}
.leaf-grid > li::before,
.leaf-grid > li::after {
  display: none !important;
}

/* Coloured team box wrapping a leaf grid */
.team-box {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  background: var(--team-bg);
  border: 2px solid var(--team-border);
  border-radius: 14px;
  padding: 8px 12px 12px;
}

.team-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--team-border);
  margin-bottom: 8px;
  align-self: flex-start;
}

/* The card itself */
.card {
  display: inline-block;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px 10px;
  width: 180px;
  height: 90px;
  overflow: hidden;
  text-align: left;
  box-sizing: border-box;
}

@media (max-width: 900px) {
  .tree { gap: 12px; }
  .card { width: 150px; height: 80px; padding: 6px 8px; }
}

.card .name {
  font-weight: 600;
  font-size: 14px;
  line-height: 1.25;
}
.card .title {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  line-height: 1.3;
}
.card .dept {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}
.card.orphan { border-style: dashed; }

/* ---------------- Modal ---------------- */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 60, 0.4);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 60px 20px;
  z-index: 50;
}
.modal.hidden { display: none; }

.modal-box {
  background: var(--panel);
  border-radius: var(--radius);
  width: 100%;
  max-width: 560px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-box header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.modal-box h2 { margin: 0; font-size: 16px; }
.modal-box section {
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.modal-box section:last-child { border-bottom: 0; }
.modal-box h3 { font-size: 13px; margin: 0 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.modal-box label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 10px;
}
.modal-box label small { display: block; margin-top: 4px; }
.modal-box input[type="text"],
.modal-box input[type="password"],
.modal-box select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 6px 8px;
  font: inherit;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}
.modal-box .row {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.view-list { list-style: none; margin: 0; padding: 0; }
.view-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.view-list li:last-child { border-bottom: 0; }
.view-list .vname { font-weight: 600; }
.view-list .vcomp { color: var(--muted); font-size: 12px; }
