/* ======================================================================
   RESET & TOKENS
   ====================================================================== */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

:root {
  --azul-escuro: #0c2340;
  --azul-medio: #1e4d8b;
  --azul-claro: #4a7bc4;
  --azul-suave: #eaf1fb;
  --branco: #ffffff;
  --cinza-fundo: #f4f6f9;
  --cinza-borda: #dbe2ec;
  --cinza-texto: #4a5568;
  --vermelho: #b3261e;
  --sombra: 0 4px 14px rgba(12, 35, 64, 0.12);
  --radius: 10px;
}

body {
  font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
  background: var(--cinza-fundo);
  color: var(--azul-escuro);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

/* ======================================================================
   HEADER
   ====================================================================== */
.app-header {
  background: var(--azul-escuro);
  color: var(--branco);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  gap: 16px;
  flex-wrap: wrap;
  box-shadow: var(--sombra);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  background: var(--azul-medio);
  color: var(--branco);
  font-weight: 700;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 1.1rem; letter-spacing: 0.5px; }
.brand-text span { font-size: 0.8rem; color: #b9c9e0; }

.header-actions { display: flex; align-items: center; gap: 12px; }

.zoom-controls {
  display: flex;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  overflow: hidden;
}
.zoom-controls button {
  background: transparent;
  border: none;
  color: var(--branco);
  padding: 8px 12px;
  font-size: 0.9rem;
  border-right: 1px solid rgba(255, 255, 255, 0.12);
}
.zoom-controls button:last-child { border-right: none; }
.zoom-controls button:hover { background: rgba(255, 255, 255, 0.15); }

.icon-btn {
  background: rgba(255, 255, 255, 0.08);
  border: none;
  color: var(--branco);
  width: 38px;
  height: 38px;
  border-radius: 8px;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.18); }

/* ======================================================================
   ORGANOGRAMA — VIEWPORT E ÁRVORE
   ====================================================================== */
.chart-viewport {
  flex: 1;
  overflow: auto;
  padding: 60px 40px;
  cursor: grab;
}
.chart-viewport.panning { cursor: grabbing; user-select: none; }

.chart {
  display: inline-flex;
  justify-content: center;
  min-width: 100%;
  transform-origin: top center;
  transition: transform 0.15s ease-out;
}

.branch {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  position: relative;
  padding: 0 14px;
}

.children {
  display: flex;
  padding-top: 40px;
  position: relative;
}

.children::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 0;
  height: 20px;
  border-left: 2px solid var(--azul-claro);
}

.children .branch {
  position: relative;
}

/* linha vertical descendo do card pai até a linha horizontal dos filhos */
.children .branch::before {
  content: '';
  position: absolute;
  top: -20px;
  left: 50%;
  width: 0;
  height: 20px;
  border-left: 2px solid var(--azul-claro);
}

/* linha horizontal conectando irmãos */
.children .branch::after {
  content: '';
  position: absolute;
  top: -20px;
  left: 0;
  right: 0;
  height: 0;
  border-top: 2px solid var(--azul-claro);
}
.children .branch:first-child::after { left: 50%; }
.children .branch:last-child::after { right: 50%; }
.children .branch:only-child::after { display: none; }

.node-card {
  background: var(--branco);
  border: 1px solid var(--cinza-borda);
  border-radius: var(--radius);
  padding: 12px 18px;
  min-width: 160px;
  max-width: 220px;
  box-shadow: var(--sombra);
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
  position: relative;
  z-index: 1;
}
.node-card:hover,
.node-card:focus-visible {
  transform: translateY(-3px);
  border-color: var(--azul-medio);
  box-shadow: 0 8px 22px rgba(12, 35, 64, 0.2);
  outline: none;
}

.node-title {
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--azul-escuro);
  line-height: 1.3;
}
.node-subtitle {
  font-size: 0.76rem;
  color: var(--azul-claro);
  font-weight: 600;
}

/* raiz recebe destaque visual */
.chart > .branch > .node-card {
  background: var(--azul-escuro);
  border-color: var(--azul-escuro);
}
.chart > .branch > .node-card .node-title { color: var(--branco); }
.chart > .branch > .node-card .node-subtitle { color: #9fc0ec; }

.hint {
  text-align: center;
  font-size: 0.8rem;
  color: var(--cinza-texto);
  padding: 8px 0 16px;
  margin: 0;
}

/* ======================================================================
   FOOTER
   ====================================================================== */
.app-footer {
  text-align: center;
  padding: 14px 20px;
  border-top: 1px solid var(--cinza-borda);
  background: var(--branco);
}
.app-footer p {
  margin: 0;
  font-size: 0.78rem;
  color: var(--cinza-texto);
}
.app-footer a {
  color: var(--azul-medio);
  font-weight: 600;
  text-decoration: none;
  transition: color 0.15s ease;
}
.app-footer a:hover,
.app-footer a:focus-visible {
  color: var(--azul-escuro);
  text-decoration: underline;
}

/* ======================================================================
   DRAWER LATERAL
   ====================================================================== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(12, 35, 64, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 30;
}
.overlay.visible { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: 380px;
  max-width: 90vw;
  background: var(--branco);
  box-shadow: -6px 0 24px rgba(12, 35, 64, 0.25);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 31;
  padding: 28px 24px;
  overflow-y: auto;
}
.drawer.open { transform: translateX(0); }

.drawer-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: var(--cinza-fundo);
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 1.1rem;
  color: var(--azul-escuro);
}
.drawer-close:hover { background: var(--cinza-borda); }

.drawer h2 { margin: 4px 0 2px; color: var(--azul-escuro); padding-right: 30px; }
.drawer-subtitle { color: var(--azul-medio); font-weight: 600; margin: 0 0 16px; }
.drawer-meta { display: flex; flex-direction: column; gap: 4px; font-size: 0.88rem; color: var(--cinza-texto); margin-bottom: 12px; }
.drawer hr { border: none; border-top: 1px solid var(--cinza-borda); margin: 16px 0; }
.drawer-description { font-size: 0.92rem; line-height: 1.6; color: #2d3748; }

/* ======================================================================
   MODAL PIN ADMIN
   ====================================================================== */
.pin-modal {
  position: fixed;
  inset: 0;
  background: rgba(12, 35, 64, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 40;
  padding: 16px;
}
.pin-modal.visible { opacity: 1; pointer-events: auto; }

.pin-modal-box {
  background: var(--branco);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--sombra);
}
.pin-modal-box h2 { margin-top: 0; color: var(--azul-escuro); font-size: 1.1rem; }
.pin-modal-box p { color: var(--cinza-texto); font-size: 0.88rem; margin-top: 4px; }
.pin-modal-box input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--cinza-borda);
  border-radius: 8px;
  font-size: 0.95rem;
  margin-top: 8px;
}
.pin-modal-box input[type="password"]:focus { outline: 2px solid var(--azul-claro); border-color: var(--azul-claro); }
.pin-error { color: var(--vermelho); font-size: 0.82rem; margin-top: 8px; }
.pin-modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; flex-wrap: wrap; }

#delete-modal .pin-modal-box { max-width: 420px; }
.delete-modal-actions button { white-space: nowrap; }

/* ======================================================================
   BOTÕES GENÉRICOS
   ====================================================================== */
.btn-primary, .btn-secondary, .btn-danger, .btn-ghost {
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font-size: 0.88rem;
  font-weight: 600;
}
.btn-primary { background: var(--azul-medio); color: var(--branco); }
.btn-primary:hover { background: var(--azul-escuro); }
.btn-secondary { background: var(--azul-suave); color: var(--azul-escuro); }
.btn-secondary:hover { background: #dbe8fa; }
.btn-danger { background: #fbe4e2; color: var(--vermelho); }
.btn-danger:hover { background: #f6cfcb; }
.btn-ghost { background: transparent; color: var(--cinza-texto); }
.btn-ghost:hover { color: var(--azul-escuro); }
.file-label { display: inline-flex; align-items: center; justify-content: center; }

/* ======================================================================
   PAINEL ADMIN
   ====================================================================== */
.admin-panel {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: 640px;
  max-width: 100vw;
  background: var(--branco);
  box-shadow: -6px 0 24px rgba(12, 35, 64, 0.25);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 35;
  display: flex;
  flex-direction: column;
}
.admin-panel.open { transform: translateX(0); }

.admin-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  background: var(--azul-escuro);
  color: var(--branco);
}
.admin-panel-header h2 { margin: 0; font-size: 1.05rem; }
.admin-panel-header .icon-btn { background: rgba(255,255,255,0.1); }

.admin-panel-body {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.admin-node-list {
  width: 220px;
  flex-shrink: 0;
  overflow-y: auto;
  border-right: 1px solid var(--cinza-borda);
  background: var(--cinza-fundo);
  padding: 8px 0;
}
.admin-list-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 8px 12px;
  font-size: 0.82rem;
  color: var(--azul-escuro);
  border-radius: 0;
}
.admin-list-item:hover { background: var(--azul-suave); }
.admin-list-item.active { background: var(--azul-medio); color: var(--branco); font-weight: 600; }
.admin-list-item.dragging { opacity: 0.4; }
.admin-list-item.drag-over { outline: 2px dashed var(--azul-medio); outline-offset: -2px; background: var(--azul-suave); }

.admin-form-area { flex: 1; overflow-y: auto; padding: 20px 24px; }
.admin-empty { color: var(--cinza-texto); font-size: 0.9rem; }

#admin-form { display: flex; flex-direction: column; gap: 14px; }
#admin-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--azul-escuro);
}
#admin-form input, #admin-form textarea {
  padding: 9px 11px;
  border: 1px solid var(--cinza-borda);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 400;
  color: #2d3748;
  resize: vertical;
}
#admin-form input:focus, #admin-form textarea:focus {
  outline: 2px solid var(--azul-claro);
  border-color: var(--azul-claro);
}
#admin-form button { align-self: flex-start; margin-top: 4px; }

/* ---- Ações Estruturais (Etapa 2) ---- */
.admin-structural-actions {
  border: 1px solid var(--cinza-borda);
  border-radius: var(--radius);
  padding: 14px 16px 16px;
  margin-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.admin-structural-actions legend {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--azul-medio);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  padding: 0 4px;
}
.admin-structural-actions label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--azul-escuro);
}
.admin-structural-actions select {
  padding: 9px 11px;
  border: 1px solid var(--cinza-borda);
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 400;
  color: #2d3748;
}
#admin-form .admin-structural-actions button { align-self: stretch; margin-top: 0; }
.admin-structural-actions .reorder-row { display: flex; gap: 10px; }
.admin-structural-actions .reorder-row button { flex: 1; }
.admin-structural-actions button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.admin-panel-footer {
  display: flex;
  gap: 10px;
  padding: 16px 22px;
  border-top: 1px solid var(--cinza-borda);
  flex-wrap: wrap;
}

/* ======================================================================
   TOAST
   ====================================================================== */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--azul-escuro);
  color: var(--branco);
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 50;
  box-shadow: var(--sombra);
}
.toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ======================================================================
   ACESSIBILIDADE
   ====================================================================== */
:focus-visible { outline: 2px solid var(--azul-claro); outline-offset: 2px; }

/* ======================================================================
   RESPONSIVO
   ====================================================================== */
@media (max-width: 720px) {
  .app-header { padding: 10px 14px; }
  .brand-text span { display: none; }
  .chart-viewport { padding: 40px 16px; }
  .node-card { min-width: 130px; max-width: 170px; padding: 10px 12px; }
  .node-title { font-size: 0.82rem; }
  .node-subtitle { font-size: 0.7rem; }

  .drawer, .admin-panel { width: 100%; max-width: 100vw; }

  .admin-panel-body { flex-direction: column; }
  .admin-node-list { width: 100%; max-height: 160px; border-right: none; border-bottom: 1px solid var(--cinza-borda); }
  .admin-panel-footer { justify-content: center; }
}
