:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e1e4ea;
  --border-strong: #c7ccd6;
  --text: #1d2330;
  --muted: #6b7384;
  --accent: #2563eb;
  --accent-hover: #1d4fd4;
  --danger: #c0392b;
  --danger-hover: #9c2c20;
  --green: #1aa75c;
  --yellow: #d29806;
  --gray: #8a8f9a;
  --row-hover: #f0f3f8;
  --row-selected: #e6efff;
  --shadow: 0 1px 2px rgba(15, 22, 36, 0.05), 0 4px 12px rgba(15, 22, 36, 0.06);
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
}
.brand { display: flex; align-items: baseline; gap: 12px; }
.logo {
  font-weight: 800;
  font-size: 16px;
  color: var(--accent);
  background: #e6efff;
  padding: 4px 8px;
  border-radius: 4px;
  letter-spacing: 0.5px;
}
.title { font-weight: 600; font-size: 16px; }
.subtitle { color: var(--muted); font-size: 12px; }
.health {
  font-size: 12px;
  color: var(--muted);
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
}
.health.ok { color: var(--green); border-color: #c9efd9; background: #f0faf4; }
.health.bad { color: var(--danger); border-color: #f1c7c2; background: #fbeeec; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 3fr 7fr;
  gap: 16px;
  padding: 16px;
  overflow: hidden;
}

.tree-pane, .detail-pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.pane-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.pane-header h2 { margin: 0; font-size: 14px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }

.tree {
  flex: 1;
  overflow: auto;
  padding: 6px 0;
}

.tree-node { user-select: none; }
.tree-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  cursor: pointer;
  border-left: 3px solid transparent;
}
.tree-row:hover { background: var(--row-hover); }
.tree-row.selected { background: var(--row-selected); border-left-color: var(--accent); }

.toggle {
  width: 16px;
  display: inline-flex;
  justify-content: center;
  color: var(--muted);
  font-size: 10px;
  flex-shrink: 0;
}
.toggle.placeholder { visibility: hidden; }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-dot.active { background: var(--green); }
.status-dot.under_review { background: var(--yellow); }
.status-dot.deprecated { background: var(--gray); }

.row-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  color: var(--muted);
  background: #f3f5f9;
  padding: 1px 5px;
  border-radius: 3px;
  flex-shrink: 0;
}
.row-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-add {
  opacity: 0;
  font-size: 12px;
  color: var(--accent);
  background: transparent;
  border: 0;
  padding: 2px 6px;
  cursor: pointer;
  border-radius: 3px;
}
.tree-row:hover .row-add { opacity: 1; }
.row-add:hover { background: #e6efff; }

.children { padding-left: 16px; }
.children.collapsed { display: none; }

.detail-pane { padding: 0; }
.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

.detail-form { display: flex; flex-direction: column; height: 100%; }
.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}
.level-chip, .code-chip {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 4px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.level-chip { background: #e6efff; color: var(--accent); margin-right: 6px; }
.code-chip {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: #f3f5f9;
  color: var(--text);
}
.detail-actions { display: flex; gap: 8px; }

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
  padding: 16px;
  overflow: auto;
  flex: 1;
}
.grid label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
}
.grid label.span-2 { grid-column: span 2; }
.grid input,
.grid select,
.grid textarea {
  font: inherit;
  color: var(--text);
  padding: 7px 9px;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  background: #fff;
  outline: none;
}
.grid input:focus,
.grid select:focus,
.grid textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15); }
.grid input:disabled { background: #f3f5f9; color: var(--muted); }
.grid textarea { resize: vertical; font-family: inherit; }

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: #fafbfd;
}
.muted { color: var(--muted); font-size: 12px; }

.btn-primary, .btn-secondary, .btn-danger {
  font: inherit;
  font-size: 13px;
  padding: 7px 14px;
  border-radius: 5px;
  cursor: pointer;
  border: 1px solid transparent;
  font-weight: 500;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: #fff; color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover { background: var(--row-hover); }
.btn-danger { background: #fff; color: var(--danger); border-color: #f1c7c2; }
.btn-danger:hover { background: var(--danger); color: #fff; }

dialog {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  box-shadow: 0 10px 40px rgba(15, 22, 36, 0.2);
  width: min(520px, 92vw);
}
dialog::backdrop { background: rgba(15, 22, 36, 0.4); }
dialog form { display: flex; flex-direction: column; }
dialog h3 { margin: 0; padding: 14px 16px; border-bottom: 1px solid var(--border); font-size: 14px; }

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--text);
  color: #fff;
  padding: 10px 16px;
  border-radius: 5px;
  font-size: 13px;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--danger); }


/* ============================================================
   Tile pane — focused branch view.
   ROOT MODE: all L1 tiles in a responsive grid.
   BRANCH MODE: highlighted parent header + vertical stack of
   full-width child tiles with an org-chart connector line.
   ============================================================ */

.tile-pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Pane header now hosts breadcrumb + back button instead of a static title */
.tile-pane .pane-header {
  gap: 12px;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  font-size: 13px;
  min-height: 28px;
}
.crumb {
  cursor: pointer;
  color: var(--accent);
  padding: 3px 6px;
  border-radius: 4px;
}
.crumb:hover { background: var(--row-selected); }
.crumb.current {
  cursor: default;
  color: var(--text);
  font-weight: 600;
}
.crumb.current:hover { background: transparent; }
.crumb-sep { color: var(--muted); font-size: 12px; }

.back-btn {
  background: #fff;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  padding: 4px 9px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  line-height: 1;
}
.back-btn:hover { background: var(--row-hover); border-color: var(--accent); color: var(--accent); }
.back-btn[disabled] { opacity: 0.35; cursor: not-allowed; }

.tile-grid-root {
  flex: 1;
  overflow: auto;
  padding: 16px;
}

/* Root mode: L1 tiles in a responsive grid */
.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}

/* Branch mode: vertical org-chart view */
.branch-view {
  display: flex;
  flex-direction: column;
}

/* Header tile (focused parent) */
.branch-header-tile {
  border: 2px solid var(--accent);
  background: linear-gradient(180deg, #f4f8ff 0%, #ffffff 100%);
  box-shadow: 0 1px 2px rgba(15,22,36,0.05), 0 6px 16px rgba(37,99,235,0.08);
}

/* Container that holds the children and renders the connector line */
.branch-children-list {
  position: relative;
  margin-top: 24px;
  padding-left: 40px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.branch-children-list::before {
  /* vertical trunk drops down from below the header */
  content: "";
  position: absolute;
  left: 20px;
  top: -24px;
  bottom: 50%;
  width: 2px;
  background: var(--border-strong);
}
.branch-children-list.has-children::before {
  bottom: 28px; /* extend trunk to last child's connector */
}
.branch-child {
  position: relative;
}
.branch-child::before {
  /* horizontal stub connecting child to the vertical trunk */
  content: "";
  position: absolute;
  left: -20px;
  top: 28px;
  width: 20px;
  height: 2px;
  background: var(--border-strong);
}

.branch-empty {
  margin-top: 16px;
  padding: 14px 16px;
  border: 1px dashed var(--border-strong);
  border-radius: 6px;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
}

/* Doc-count badge shown on tiles */
.doc-count {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
  background: #f3f5f9;
  border-radius: 3px;
  padding: 2px 6px;
}
.doc-count strong { color: var(--text); font-weight: 600; }

.tile {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.tile:hover { border-color: var(--border-strong); }
.tile.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
}

.tile-top {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.tile-top .row-code { font-size: 11px; }
.tile-name {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.25;
}
.tile-meta { color: var(--muted); font-size: 11px; }
.tile-kpi {
  background: #f3f5f9;
  border-radius: 5px;
  padding: 7px 9px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.tile-kpi .kpi-name { font-weight: 600; color: var(--text); }
.tile-kpi .kpi-vals { color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 11px; }

.tile-actions {
  display: flex;
  gap: 6px;
  margin-left: auto;
}
.tile-btn {
  background: #fff;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  line-height: 1;
}
.tile-btn:hover { background: var(--row-hover); }
.tile-btn.is-expanded { background: var(--row-selected); border-color: var(--accent); color: var(--accent); }
.tile-btn[disabled] { opacity: 0.35; cursor: not-allowed; }
.tile-btn .icon { font-size: 13px; }

/* Docs section appears below the tile body when expanded */
.tile-docs {
  margin-top: 4px;
  border-top: 1px dashed var(--border);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.tile-docs-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
}
.tile-docs-list { display: flex; flex-direction: column; gap: 6px; }
.tile-docs-empty {
  color: var(--muted);
  font-size: 12px;
  padding: 6px 0;
}

.doc-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fafbfd;
  align-items: center;
}
.doc-icon {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  background: var(--row-selected);
  color: var(--accent);
  flex-shrink: 0;
}
.doc-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.doc-filename {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.doc-sub {
  font-size: 11px;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.doc-tag {
  background: var(--row-selected);
  color: var(--accent);
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 500;
}
.doc-actions { display: flex; gap: 4px; }
.doc-actions button {
  border: 0;
  background: transparent;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  color: var(--muted);
  font-size: 12px;
}
.doc-actions button:hover { background: var(--row-hover); color: var(--text); }
.doc-actions button.danger:hover { background: #fbeeec; color: var(--danger); }

/* Big dialog for forms with many fields */
dialog.big-dialog { width: min(720px, 94vw); }

/* Required-field indicator inside the doc dialog */
.req-mark { color: var(--danger); margin-left: 2px; }

/* Predefined-tag chips */
.tag-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: -4px;
}
.tag-chip {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: #fff;
  cursor: pointer;
  user-select: none;
}
.tag-chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.tag-chip:hover { border-color: var(--accent); }
