:root {
  --blue: #1a73e8;
  --blue-dark: #1557b0;
  --green: #34a853;
  --red: #ea4335;
  --yellow: #fbbc04;
  --ink: #202124;
  --ink-soft: #5f6368;
  --line: #dadce0;
  --bg: #f6f8fc;
  --surface: #ffffff;
  --shadow: 0 1px 2px rgba(60,64,67,.12), 0 2px 6px rgba(60,64,67,.08);
  --shadow-lg: 0 4px 12px rgba(60,64,67,.18);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Roboto", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

/* ---------- Top bar ---------- */
.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-title {
  font-family: "Google Sans", "Roboto", sans-serif;
  font-size: 20px;
  font-weight: 500;
  color: var(--ink);
}
.toolbar { display: flex; align-items: center; gap: 8px; }

/* ---------- Buttons ---------- */
.btn {
  font-family: "Google Sans", "Roboto", sans-serif;
  font-size: 14px;
  font-weight: 500;
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  cursor: pointer;
  transition: background .15s, box-shadow .15s;
}
.btn.primary { background: var(--blue); color: #fff; }
.btn.primary:hover { background: var(--blue-dark); box-shadow: var(--shadow); }
.btn.ghost { background: transparent; color: var(--blue); }
.btn.ghost:hover { background: rgba(26,115,232,.08); }
.btn.wide { width: 100%; margin-top: 8px; padding: 12px; }
.btn.gsi { display: inline-flex; align-items: center; justify-content: center; gap: 10px; margin-top: 20px; font-size: 15px; }
.help.adv { margin-top: 16px; }
.help.adv summary { color: var(--ink-soft); font-weight: 400; }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- Search ---------- */
.search-wrap { position: relative; }
#search {
  width: 260px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 22px;
  font-size: 14px;
  background: var(--bg);
  outline: none;
  transition: box-shadow .15s, background .15s;
}
#search:focus { background: #fff; box-shadow: var(--shadow); border-color: transparent; }
.suggest {
  position: absolute;
  top: 44px; left: 0; right: 0;
  margin: 0; padding: 6px 0;
  list-style: none;
  background: #fff;
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  max-height: 320px;
  overflow-y: auto;
  z-index: 30;
}
.suggest li {
  padding: 8px 14px;
  cursor: pointer;
  font-size: 14px;
  display: flex; flex-direction: column;
}
.suggest li:hover, .suggest li.active { background: var(--bg); }
.suggest .s-name { font-weight: 500; }
.suggest .s-meta { font-size: 12px; color: var(--ink-soft); }

.user-pill {
  font-size: 13px;
  color: var(--ink-soft);
  background: var(--bg);
  padding: 6px 12px;
  border-radius: 16px;
  max-width: 220px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- Stage ---------- */
.stage {
  position: relative; height: calc(100% - 64px); overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(var(--line) 1px, transparent 1px);
  background-size: 22px 22px;
}
.stage:fullscreen { background-color: var(--bg); }

/* ---------- Zoom controls ---------- */
.zoom-ctl {
  position: absolute; right: 20px; bottom: 20px; z-index: 16;
  display: flex; align-items: center; gap: 2px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); padding: 4px;
}
.zc-btn {
  border: none; background: transparent; cursor: pointer;
  width: 34px; height: 34px; border-radius: 8px;
  font-size: 18px; color: var(--ink-soft);
  display: inline-flex; align-items: center; justify-content: center;
}
.zc-btn:hover { background: var(--bg); color: var(--ink); }
.zc-pct { font-size: 13px; color: var(--ink-soft); min-width: 46px; text-align: center; user-select: none; }
.zc-sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }

/* ---------- Legend ---------- */
.legend {
  position: absolute; left: 20px; bottom: 20px; z-index: 16;
  width: 230px; max-height: 46%;
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); overflow: hidden;
}
.legend-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--line);
}
.legend-stat { font-size: 13px; font-weight: 500; color: var(--ink); font-family: "Google Sans", sans-serif; }
.legend-toggle { border: none; background: none; cursor: pointer; color: var(--ink-soft); font-size: 14px; }
.legend-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; }
.legend.collapsed .legend-list { display: none; }
.legend.collapsed .legend-toggle { transform: rotate(-90deg); }
.legend-list li {
  display: flex; align-items: center; gap: 9px;
  padding: 6px 8px; border-radius: 8px; cursor: pointer;
  font-size: 13px; color: var(--ink);
}
.legend-list li:hover { background: var(--bg); }
.legend-list li.active { background: #e8f0fe; font-weight: 500; }
.legend-list .sw { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }
.legend-list .ln { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-list .lc { margin-left: auto; color: var(--ink-soft); font-size: 12px; padding-left: 8px; }
.legend-roletoggle {
  display: flex; align-items: center; gap: 7px;
  padding: 8px 14px; border-bottom: 1px solid var(--line);
  font-size: 12px; color: var(--ink-soft); cursor: pointer;
}
.legend.collapsed .legend-roletoggle { display: none; }

/* ---------- Modal ---------- */
.modal {
  position: absolute; inset: 0; z-index: 40;
  display: flex; align-items: center; justify-content: center;
  background: rgba(32,33,36,.32);
}
.modal-card {
  background: var(--surface); border-radius: 16px; box-shadow: var(--shadow-lg);
  width: 380px; max-width: calc(100% - 40px); padding: 24px;
}
.modal-card h3 { font-family: "Google Sans", sans-serif; font-size: 19px; font-weight: 500; margin: 0 0 16px; }
.m-field { display: block; margin-bottom: 14px; }
.m-field > span { display: block; font-size: 13px; font-weight: 500; color: var(--ink-soft); margin-bottom: 5px; }
.m-field input, .m-field select {
  width: 100%; padding: 10px 12px; font-size: 14px;
  border: 1px solid var(--line); border-radius: 8px; outline: none; background: #fff;
}
.m-field input:focus, .m-field select:focus { border-color: var(--blue); box-shadow: 0 0 0 1px var(--blue); }
.m-actions { display: flex; align-items: center; gap: 8px; margin-top: 22px; }
.m-spacer { flex: 1; }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: rgba(234,67,53,.08); }

/* role avatar in drawer */
.d-avatar.role {
  background: #fff; border: 2px dashed var(--line);
  font-size: 32px; font-weight: 400;
}

/* ---------- Setup card ---------- */
.setup {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 36px;
  max-width: 480px;
  width: 100%;
}
.card h1 {
  font-family: "Google Sans", "Roboto", sans-serif;
  font-size: 24px; font-weight: 500; margin: 0 0 10px;
}
.muted { color: var(--ink-soft); font-size: 14px; line-height: 1.5; }
.field { display: block; margin: 24px 0 4px; }
.field > span { display: block; font-weight: 500; font-size: 14px; margin-bottom: 6px; }
.field input {
  width: 100%; padding: 11px 14px;
  border: 1px solid var(--line); border-radius: 8px;
  font-size: 14px; outline: none;
}
.field input:focus { border-color: var(--blue); box-shadow: 0 0 0 1px var(--blue); }
.field small { display: block; margin-top: 6px; }
.help { margin-top: 20px; font-size: 13px; }
.help summary { cursor: pointer; color: var(--blue); font-weight: 500; }
.help ul { margin: 10px 0 0; padding-left: 18px; color: var(--ink-soft); line-height: 1.7; }
code {
  background: var(--bg); padding: 1px 5px; border-radius: 4px;
  font-family: "Roboto Mono", monospace; font-size: 12px;
}

/* ---------- Chart ---------- */
.chart { width: 100%; height: 100%; display: block; cursor: grab; }
.chart:active { cursor: grabbing; }
.link { fill: none; stroke: var(--line); stroke-width: 1.5px; }
.link.dotted { stroke-dasharray: 4 4; stroke: #b3b7bd; }

/* vacant / open-role cards */
.node.role .node-card { fill: #fbfbfd; stroke-dasharray: 6 4; stroke: #b3b7bd; }
.node.role .node-name { fill: var(--ink-soft); }
.avatar-icon { font-family: "Google Sans", sans-serif; font-size: 22px; font-weight: 500; text-anchor: middle; }

.node { cursor: pointer; }
.node-card {
  fill: #fff;
  stroke: var(--line);
  stroke-width: 1px;
  rx: 12; ry: 12;
  filter: drop-shadow(0 1px 2px rgba(60,64,67,.15));
  transition: filter .15s;
}
.node:hover .node-card { filter: drop-shadow(0 4px 10px rgba(60,64,67,.28)); }
.node.match .node-card { stroke: var(--blue); stroke-width: 2px; }
.node.selected .node-card { stroke: var(--blue); stroke-width: 2.5px; }
.node.dim { opacity: .22; transition: opacity .2s; }
.node.pulse .node-card { stroke: var(--blue); stroke-width: 3px; animation: pulse 1.4s ease-out; }
@keyframes pulse {
  0% { stroke-width: 3px; }
  50% { stroke-width: 6px; }
  100% { stroke-width: 2.5px; }
}
.node-accent { rx: 12; ry: 12; }
.node-name { font-family: "Google Sans", sans-serif; font-size: 14px; font-weight: 500; fill: var(--ink); }
.node-title { font-size: 12px; fill: var(--ink-soft); }
.node-dept { font-size: 11px; fill: var(--ink-soft); }
.avatar-text { font-family: "Google Sans", sans-serif; font-size: 15px; font-weight: 500; fill: #fff; text-anchor: middle; }
.badge {
  fill: var(--blue);
}
.badge-text { font-size: 11px; font-weight: 700; fill: #fff; text-anchor: middle; }

/* ---------- Status ---------- */
.status {
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px 28px;
  font-size: 14px; color: var(--ink-soft);
  display: flex; align-items: center; gap: 14px;
  z-index: 15;
}
.status.error { color: var(--red); border-color: var(--red); max-width: 460px; }
.spinner {
  width: 20px; height: 20px;
  border: 3px solid var(--line);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Drawer ---------- */
.drawer {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: 340px;
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow-lg);
  padding: 24px;
  overflow-y: auto;
  z-index: 18;
  animation: slideIn .2s ease;
}
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }
.drawer-close {
  position: absolute; top: 12px; right: 14px;
  border: none; background: none; font-size: 26px; line-height: 1;
  color: var(--ink-soft); cursor: pointer;
}
.drawer-close:hover { color: var(--ink); }
.d-avatar {
  position: relative; overflow: hidden;
  width: 72px; height: 72px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-family: "Google Sans", sans-serif; font-size: 28px; font-weight: 500;
  color: #fff; margin-bottom: 14px;
}
.d-avatar-img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.d-name { font-family: "Google Sans", sans-serif; font-size: 20px; font-weight: 500; margin: 0; }
.d-title { color: var(--ink-soft); font-size: 14px; margin: 2px 0 16px; }
.d-row { display: flex; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); font-size: 14px; }
.d-row .k { color: var(--ink-soft); width: 92px; flex-shrink: 0; }
.d-row .v { color: var(--ink); word-break: break-word; }
.d-row .v a { color: var(--blue); text-decoration: none; }
.d-actions {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line);
}
.d-actions .btn { border: 1px solid var(--line); padding: 7px 12px; }
.d-report-chip.is-role { font-style: italic; color: var(--ink-soft); }

.d-reports {
  margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line);
}
.d-reports h4 { margin: 0 0 8px; font-size: 13px; color: var(--ink-soft); font-weight: 500; }
.d-report-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--bg); border-radius: 16px; padding: 4px 12px 4px 4px;
  margin: 0 6px 6px 0; font-size: 13px; cursor: pointer;
}
.d-report-chip:hover { background: #eef3fc; }
.chip-dot {
  position: relative; overflow: hidden;
  width: 24px; height: 24px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-size: 11px; font-weight: 500;
}
.chip-img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

/* ============ Premium UI ============ */
.filter-sel {
  font-family: "Google Sans", sans-serif; font-size: 14px; color: var(--ink-soft);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; cursor: pointer; outline: none; max-width: 170px;
}
.filter-sel:focus { border-color: var(--blue); }

.export-wrap { position: relative; }
.export-menu {
  position: absolute; right: 0; top: 44px; z-index: 30; min-width: 190px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow-lg); padding: 6px;
  display: flex; flex-direction: column;
}
.export-menu button {
  border: none; background: none; text-align: left; padding: 9px 12px;
  border-radius: 8px; cursor: pointer; font-size: 14px; color: var(--ink);
  font-family: "Roboto", sans-serif;
}
.export-menu button:hover { background: var(--bg); }

.node.chain .node-card { stroke: var(--blue); stroke-width: 2px; }
path.link.chain { stroke: var(--blue); stroke-width: 2.5px; }

.ins-card { width: 560px; max-width: calc(100% - 40px); position: relative; max-height: 86vh; overflow-y: auto; }
.ins-card h3 { margin: 0 0 18px; }
.ins-card h4 {
  font-family: "Google Sans", sans-serif; font-size: 12px; text-transform: uppercase;
  letter-spacing: .6px; color: var(--ink-soft); margin: 26px 0 10px;
}
.ins-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.ins-stat { background: var(--bg); border-radius: 12px; padding: 16px; }
.ins-n { font-family: "Google Sans", sans-serif; font-size: 26px; font-weight: 500; color: var(--ink); }
.ins-l { font-size: 12.5px; color: var(--ink-soft); margin-top: 2px; line-height: 1.3; }
.ins-flags { list-style: none; margin: 0; padding: 0; }
.ins-flags li { padding: 9px 12px; border-radius: 8px; font-size: 14px; margin-bottom: 6px; background: var(--bg); color: var(--ink); }
.ins-flags li.warn { background: #fef7e0; color: #7a5a00; }
.ins-flags li b { font-family: "Google Sans", sans-serif; }
.ins-depts { list-style: none; margin: 0; padding: 0; }
.ins-depts li { display: flex; align-items: center; gap: 10px; padding: 5px 0; font-size: 13.5px; }
.ins-dl { width: 130px; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink); }
.ins-track { flex: 1; height: 8px; background: var(--bg); border-radius: 5px; overflow: hidden; }
.ins-bar { display: block; height: 100%; border-radius: 5px; }
.ins-dc { width: 38px; text-align: right; color: var(--ink-soft); }

/* ============ Dark theme ============ */
:root[data-theme="dark"] {
  --ink: #e8eaed; --ink-soft: #9aa0a6; --line: #3c4043; --bg: #202124; --surface: #2a2b2e;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 2px 6px rgba(0,0,0,.3);
  --shadow-lg: 0 4px 16px rgba(0,0,0,.55);
}
:root[data-theme="dark"] body { background: var(--bg); }
:root[data-theme="dark"] .node-card { fill: #2a2b2e; stroke: #3c4043; }
:root[data-theme="dark"] .node.role .node-card { fill: #26282c; }
:root[data-theme="dark"] #search,
:root[data-theme="dark"] .filter-sel { background: #202124; color: var(--ink); }
:root[data-theme="dark"] .stage { background-image: radial-gradient(#3c4043 1px, transparent 1px); }
:root[data-theme="dark"] .ins-flags li.warn { background: #3a3320; color: #f5d27a; }
:root[data-theme="dark"] code { background: #3c4043; color: #e8eaed; }

/* ============ Embed (read-only) mode ============ */
body.embed .d-actions { display: none; }
body.embed #connectBtn,
body.embed #meBtn,
body.embed #addRoleBtn,
body.embed #syncBtn,
body.embed #publishBtn { display: none !important; }

/* ============ Drag-and-drop reassignment ============ */
.node.dragging { cursor: grabbing; }
.node.dragging .node-card { stroke: var(--blue); stroke-width: 2px; filter: drop-shadow(0 10px 22px rgba(60,64,67,.4)); }
.node.dragging { opacity: .85; }
.node.drop-ok .node-card { stroke: var(--green, #188038); stroke-width: 3px; }
.node.drop-ok::after { content: ""; }

/* ============ Toast (undo) ============ */
.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 14px; z-index: 90;
  background: #202124; color: #e8eaed; padding: 12px 18px; border-radius: 8px;
  font-size: 14px; box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
.toast-undo {
  background: none; border: none; color: #8ab4f8; font: inherit; font-weight: 600;
  cursor: pointer; padding: 4px 6px; border-radius: 4px;
}
.toast-undo:hover { background: rgba(138,180,248,.12); }
:root[data-theme="dark"] .toast { background: #e8eaed; color: #202124; }
:root[data-theme="dark"] .toast-undo { color: #1a73e8; }

/* ============ Department tiles ============ */
.node .dept-card { fill: #f8f9fb; stroke: var(--line); }
.node .node-dept { font-size: 11px; fill: var(--ink-soft); }
:root[data-theme="dark"] .node .dept-card { fill: #303136; stroke: #3c4043; }

/* active toolbar toggle (Departments view on) */
.btn.ghost.active { background: rgba(26,115,232,.14); color: var(--blue); font-weight: 500; }

/* ============ Publish modal ============ */
.pub-loading { display: flex; align-items: center; gap: 10px; color: var(--ink-soft); padding: 18px 0; }
.pub-note { color: var(--ink-soft); font-size: 13.5px; line-height: 1.5; margin: 0 0 14px; }
.pub-note.small { font-size: 12.5px; margin-top: 14px; }
.pub-label { display: block; font-size: 13px; font-weight: 500; color: var(--ink-soft); margin: 0 0 6px; }
.pub-row { display: flex; gap: 8px; align-items: flex-start; margin-bottom: 16px; }
.pub-input {
  flex: 1; font: inherit; font-size: 13px; padding: 9px 11px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--surface); color: var(--ink); resize: none; font-family: ui-monospace, Menlo, monospace;
}
.pub-area { line-height: 1.45; }
.pub-actions { display: flex; gap: 10px; align-items: center; margin-top: 4px; flex-wrap: wrap; }
.pub-err { color: var(--red, #d93025); font-size: 14px; margin: 6px 0; }
:root[data-theme="dark"] .pub-input { background: #202124; }
