:root {
  --ink: #18212b;
  --ink-strong: #0d151d;
  --muted: #64707d;
  --faint: #8b96a3;
  --line: #d8dde3;
  --line-strong: #bbc4ce;
  --surface: #ffffff;
  --soft: #f6f8fa;
  --soft-2: #eef5f3;
  --header: #202b37;
  --header-2: #17222e;
  --teal: #009879;
  --teal-dark: #007961;
  --teal-soft: #e3f4ef;
  --brass: #c9973f;
  --danger: #b84444;
  --warning: #b1780d;
  --radius: 8px;
  --shadow: 0 18px 46px rgba(24, 33, 43, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: #f3f6f8;
  font-family: Inter, "Avenir Next", "Segoe UI", Arial, sans-serif;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: 56px minmax(0, 1fr) auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 0 18px;
  background: linear-gradient(180deg, var(--header), var(--header-2));
  color: #f8fafc;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.brand {
  display: flex;
  align-items: center;
  gap: 16px;
  min-width: 0;
}

.brand-mark {
  color: var(--brass);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 26px;
  font-weight: 700;
  line-height: 1;
}

.brand-title {
  font-size: 22px;
  font-weight: 750;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-meta {
  display: flex;
  align-items: center;
  gap: 16px;
  color: rgba(248, 250, 252, 0.88);
  font-size: 13px;
  white-space: nowrap;
}

.top-meta span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.top-meta svg {
  width: 18px;
  height: 18px;
  stroke-width: 2;
}

.save-state.error {
  color: #ffb7b7;
}

.divider {
  width: 1px;
  height: 24px;
  background: rgba(255, 255, 255, 0.28);
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: 310px minmax(500px, 1fr) 480px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.sidebar,
.editor,
.tree-panel {
  min-width: 0;
  min-height: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: #fbfcfd;
}

.sidebar-body {
  flex: 1;
  overflow: auto;
  padding: 20px 18px;
}

.sidebar-title,
.tree-title {
  margin: 0;
  color: var(--ink-strong);
  font-size: 18px;
  line-height: 1.25;
}

.progress-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 28px 0 8px;
  color: var(--muted);
  font-size: 13px;
}

.progress-track {
  height: 7px;
  overflow: hidden;
  border-radius: 999px;
  background: #e5e9ed;
}

.progress-fill {
  height: 100%;
  border-radius: inherit;
  background: var(--teal);
  transition: width 160ms ease;
}

.department-list {
  display: grid;
  gap: 10px;
  margin-top: 24px;
}

.department-button {
  width: 100%;
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) auto 24px;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
}

.department-button:hover .department-index,
.department-button.active .department-index {
  border-color: var(--teal);
  background: var(--teal);
  color: #fff;
}

.department-index {
  width: 23px;
  height: 23px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  color: var(--muted);
  font-size: 12px;
  transition: 160ms ease;
}

.department-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.department-count {
  color: var(--muted);
  font-size: 12px;
}

.department-check {
  color: var(--teal);
}

.ghost-button,
.secondary-button,
.primary-button,
.danger-button,
.icon-button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--ink);
  font-weight: 650;
  transition: border-color 140ms ease, background 140ms ease, color 140ms ease, transform 140ms ease;
}

.ghost-button,
.secondary-button,
.primary-button,
.danger-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
}

.icon-button {
  width: 38px;
  display: grid;
  place-items: center;
  padding: 0;
}

.ghost-button:hover,
.secondary-button:hover,
.icon-button:hover {
  border-color: var(--teal);
  color: var(--teal-dark);
}

.primary-button {
  border-color: var(--teal);
  background: var(--teal);
  color: #fff;
}

.primary-button:hover {
  background: var(--teal-dark);
}

.danger-button {
  border-color: rgba(184, 68, 68, 0.25);
  color: var(--danger);
}

.danger-button:hover {
  border-color: var(--danger);
  background: #fff4f4;
}

.editor {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: #fff;
}

.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 24px 20px 18px;
  border-bottom: 1px solid var(--line);
}

.crumbs {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  font-size: 18px;
  font-weight: 750;
}

.crumbs span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.crumb-muted {
  color: var(--muted);
  font-weight: 600;
}

.editor-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.form-wrap {
  overflow: auto;
  padding: 22px 20px 92px;
}

.role-actions-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--soft-2);
}

.role-actions-panel strong,
.role-actions-panel span {
  display: block;
}

.role-actions-panel strong {
  color: var(--ink-strong);
  font-size: 14px;
}

.role-actions-panel span {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.role-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 22px 16px;
}

.field {
  display: grid;
  gap: 7px;
}

.field.double {
  grid-column: span 2;
}

.field.full {
  grid-column: 1 / -1;
}

label {
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
}

.required::after {
  content: " *";
  color: var(--danger);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid #cbd2da;
  border-radius: 5px;
  background: #fff;
  color: var(--ink);
  outline: none;
  transition: border-color 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

input,
select {
  height: 36px;
  padding: 0 11px;
}

textarea {
  min-height: 126px;
  resize: vertical;
  padding: 11px;
  line-height: 1.45;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(0, 152, 121, 0.12);
}

.soft-missing input,
.soft-missing select,
.soft-missing textarea {
  border-color: rgba(177, 120, 13, 0.52);
  background: #fffdf6;
}

.char-count {
  justify-self: end;
  color: var(--faint);
  font-size: 11px;
}

.checkbox-row,
.messenger-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
}

.check-label {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 500;
}

.check-label input {
  width: 15px;
  height: 15px;
}

.tree-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border-left: 1px solid var(--line);
  background: #fbfcfd;
}

.tree-head {
  padding: 20px 18px 14px;
  border-bottom: 1px solid var(--line);
}

.tree-tools {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 38px;
  gap: 10px;
  margin-top: 16px;
}

.tree-body {
  overflow: auto;
  padding: 14px 18px 90px;
}

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

.tree-children {
  margin-left: 18px;
  padding-left: 15px;
  border-left: 1px solid #c9d1d9;
}

.tree-node {
  margin: 6px 0;
}

.tree-item {
  display: grid;
  grid-template-columns: 19px minmax(0, 1fr);
  gap: 8px;
  align-items: start;
  padding: 5px 7px;
  border-radius: 6px;
  color: var(--ink);
}

.tree-item.direct-report {
  background: #fff6df;
  box-shadow: inset 3px 0 0 var(--brass);
}

.tree-item.indirect-report {
  background: #f0f7f5;
  box-shadow: inset 2px 0 0 rgba(0, 152, 121, 0.45);
}

.tree-item:hover {
  background: #eaf4f1;
}

.tree-item.active {
  background: var(--teal-soft);
  box-shadow: inset 3px 0 0 var(--teal);
}

.tree-icon {
  margin-top: 1px;
  color: var(--muted);
}

.tree-label {
  min-width: 0;
  font-size: 13px;
  line-height: 1.35;
}

.tree-label strong {
  font-weight: 750;
}

.tree-label span {
  color: var(--muted);
}

.tree-empty {
  margin-top: 24px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.45;
}

.bottom-bar {
  min-height: 72px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 20px;
  background: #fff;
  border-top: 1px solid var(--line);
  box-shadow: 0 -12px 30px rgba(24, 33, 43, 0.06);
}

.bottom-spacer {
  flex: 1;
}

.metric-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 18px;
}

.metric {
  min-width: 0;
  padding: 9px 7px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.metric span {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.metric strong {
  display: block;
  margin-top: 3px;
  font-size: 20px;
}

.role-list {
  display: grid;
  gap: 10px;
  margin-top: 18px;
}

.role-list-heading {
  display: grid;
  gap: 3px;
  padding: 0 2px;
}

.role-list-heading strong {
  color: var(--ink-strong);
  font-size: 13px;
}

.role-list-heading span {
  color: var(--muted);
  font-size: 12px;
}

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

.role-tree {
  display: grid;
  gap: 8px;
}

.role-subtree {
  margin-left: 17px;
  padding-left: 12px;
  border-left: 1px solid var(--line);
}

.role-branch {
  display: grid;
  gap: 7px;
}

.role-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  padding: 10px 10px 10px calc(10px + (var(--level, 0) * 5px));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  text-align: left;
}

.role-row.active {
  border-color: var(--teal);
  background: var(--teal-soft);
}

.role-row strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.role-row span {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
}

.role-row em {
  min-width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--soft-2);
  color: var(--teal-dark);
  font-size: 11px;
  font-style: normal;
  font-weight: 750;
}

.subordinate-panel {
  display: grid;
  gap: 12px;
  margin-bottom: 18px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.subordinate-panel > div:first-child {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.subordinate-panel strong,
.subordinate-panel span {
  display: block;
}

.subordinate-panel strong {
  color: var(--ink-strong);
  font-size: 14px;
}

.subordinate-panel span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.subordinate-list {
  display: grid;
  gap: 8px;
}

.subordinate-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--soft);
  text-align: left;
}

.subordinate-row:hover,
.subordinate-row.active {
  border-color: var(--teal);
  background: var(--teal-soft);
}

.subordinate-row strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.subordinate-empty {
  padding: 12px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--teal);
}

.status-dot.vacant {
  background: var(--warning);
}

.status-dot.missing {
  background: var(--line-strong);
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 88px;
  z-index: 20;
  max-width: 360px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--ink-strong);
  color: #fff;
  box-shadow: var(--shadow);
  font-size: 13px;
  transform: translateY(12px);
  opacity: 0;
  pointer-events: none;
  transition: 160ms ease;
}

.toast.show {
  transform: translateY(0);
  opacity: 1;
}

.print-report {
  display: none;
}

svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 260px minmax(420px, 1fr);
  }

  .tree-panel {
    display: none;
  }
}

@media (max-width: 820px) {
  .app {
    grid-template-rows: auto auto auto;
  }

  .topbar,
  .bottom-bar {
    flex-wrap: wrap;
  }

  .topbar {
    padding: 12px;
  }

  .brand-title {
    font-size: 18px;
    white-space: normal;
  }

  .top-meta {
    width: 100%;
    justify-content: space-between;
  }

  .workspace {
    display: block;
  }

  .sidebar,
  .editor,
  .tree-panel {
    display: block;
    border: 0;
  }

  .sidebar-body,
  .tree-body,
  .form-wrap {
    overflow: visible;
  }

  .form-grid,
  .role-actions-panel,
  .subordinate-panel > div:first-child,
  .metric-row {
    grid-template-columns: 1fr;
  }

  .role-actions-panel,
  .subordinate-panel > div:first-child {
    display: grid;
  }

  .role-actions {
    justify-content: stretch;
  }

  .role-actions .primary-button,
  .role-actions .secondary-button,
  .role-actions .danger-button {
    width: 100%;
  }

  .field.double {
    grid-column: auto;
  }

  .bottom-bar {
    position: sticky;
    bottom: 0;
    z-index: 10;
  }

  .bottom-spacer {
    display: none;
  }
}

@media print {
  body {
    background: #fff;
  }

  .app {
    display: none;
  }

  .print-report {
    display: block;
    padding: 24px;
    color: #111;
    font-family: Arial, sans-serif;
  }

  .print-report h1 {
    margin: 0 0 8px;
    font-size: 24px;
  }

  .print-report h2 {
    margin: 24px 0 8px;
    font-size: 16px;
  }

  .print-report table {
    width: 100%;
    border-collapse: collapse;
    font-size: 10px;
  }

  .print-report th,
  .print-report td {
    padding: 6px;
    border: 1px solid #bbb;
    vertical-align: top;
    text-align: left;
  }

  .print-report ul {
    margin: 8px 0 0;
    padding-left: 20px;
    font-size: 12px;
  }
}
