/* Interface do Cortex (EAP/WBS e Boas Práticas). Sem estilo inline (a CSP só aceita 'self'). */

:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #17202e;
  --muted: #5b6678;
  --border: #dde3ec;
  --accent: #2557d6;
  --accent-text: #ffffff;
  --danger: #b42318;
  --warn-bg: #fff6e0;
  --warn-border: #f0c45a;
  --error-bg: #fdecea;
  --error-border: #f2a59d;
  --ai-bg: #eef3ff;
  --ai-border: #bfd0fb;
  --focus: #2557d6;

  /* Cores por tipo de elemento: faixa (forte) e fundo (claro). */
  --t-fase: #1d4ed8;
  --t-fase-bg: #e8efff;
  --t-entrega: #0f766e;
  --t-entrega-bg: #e3f6f3;
  --t-pacote: #b45309;
  --t-pacote-bg: #fff3e2;
  --t-atividade: #7c3aed;
  --t-atividade-bg: #f2ecff;
  --t-raiz: #334155;
  --t-raiz-bg: #e9edf3;

  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b;
    --surface: #171e28;
    --surface-2: #1d2531;
    --text: #e6ebf2;
    --muted: #9aa6b8;
    --border: #2c3644;
    --accent: #6d95ff;
    --accent-text: #0b1220;
    --danger: #ff8b80;
    --warn-bg: #3a2e12;
    --warn-border: #8a6a1f;
    --error-bg: #3b1a17;
    --error-border: #8c3a33;
    --ai-bg: #172340;
    --ai-border: #33508f;
    --focus: #8fb0ff;
    --t-fase: #7ea2ff;
    --t-fase-bg: #1b2747;
    --t-entrega: #4fd1c0;
    --t-entrega-bg: #133430;
    --t-pacote: #f5b25c;
    --t-pacote-bg: #3a2a14;
    --t-atividade: #b79bff;
    --t-atividade-bg: #2a1f45;
    --t-raiz: #c3cddb;
    --t-raiz-bg: #263040;
    --shadow: none;
  }
}

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

html, body { margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 .5rem; }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.05rem; }
h4 { font-size: .9rem; margin-top: 1rem; }
p { margin: 0 0 .5rem; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); }

/* ---------------------------------------------------------------- botões */
button, .primary {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
}
.primary {
  display: inline-block;
  background: var(--accent);
  color: var(--accent-text);
  border: 1px solid var(--accent);
  padding: .55rem 1.1rem;
  font-weight: 600;
  text-decoration: none;
  text-align: center;
}
.primary:disabled { opacity: .6; cursor: progress; }
.primary.block { display: block; }
.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: .45rem .9rem;
}
.ghost:hover { background: var(--surface-2); }
.ghost.danger { color: var(--danger); }
.small { padding: .25rem .6rem; font-size: .85rem; }
.link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  text-align: left;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ---------------------------------------------------------------- estrutura */
.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .6rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-weight: 700; color: var(--text); text-decoration: none; display: flex; align-items: center; gap: .5rem; }
.brand-mark { width: 1.1rem; height: 1.1rem; border-radius: 4px; background: linear-gradient(135deg, var(--t-fase), var(--t-entrega)); }
.brand-sub { font-weight: 500; color: var(--muted); }
.agents { display: flex; gap: .25rem; margin-left: 1rem; }
.agent-tab {
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
  font-size: .92rem;
  padding: .4rem .8rem;
  border-radius: 8px;
}
.agent-tab:hover { background: var(--surface-2); color: var(--text); }
.agent-tab[aria-current="page"] { background: var(--ai-bg); color: var(--accent); }
.session { margin-left: auto; display: flex; align-items: center; gap: .75rem; }
.user { color: var(--muted); font-size: .9rem; }

.layout {
  display: grid;
  grid-template-columns: 17rem minmax(0, 1fr);
  min-height: calc(100vh - 3.2rem);
}
.layout.no-sidebar { grid-template-columns: minmax(0, 1fr); }
.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 1rem;
  overflow-y: auto;
}
.sidebar-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1.25rem 0 .5rem; }
.view { padding: 1.25rem 1.5rem 3rem; min-width: 0; display: flex; flex-direction: column; gap: 1rem; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.25rem;
}

/* ---------------------------------------------------------------- histórico */
.history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .25rem; }
.history-link {
  display: block;
  padding: .5rem .6rem;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}
.history-link:hover { background: var(--surface-2); }
.history li.active .history-link { background: var(--ai-bg); }
.history-title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: .9rem;
}
.history-meta { display: flex; gap: .5rem; align-items: center; font-size: .78rem; color: var(--muted); margin-top: .2rem; }
.empty { padding: .5rem .6rem; font-size: .9rem; }

/* ---------------------------------------------------------------- formulário */
.new-form { max-width: 60rem; display: flex; flex-direction: column; gap: .6rem; }
label { font-weight: 600; font-size: .9rem; }
textarea, select, input[type="password"] {
  font: inherit;
  color: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .6rem .7rem;
  width: 100%;
}
textarea { resize: vertical; min-height: 14rem; line-height: 1.5; }
select { width: auto; }
.form-row { display: flex; align-items: center; gap: .9rem; flex-wrap: wrap; }
.spacer { flex: 1; }
.counter { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
.check { display: inline-flex; align-items: center; gap: .4rem; font-weight: 500; }

/* ---------------------------------------------------------------- avisos */
.note { border-radius: 8px; padding: .65rem .9rem; border: 1px solid var(--border); background: var(--surface-2); margin: 0; }
.note.ai { background: var(--ai-bg); border-color: var(--ai-border); }
.note.warn { background: var(--warn-bg); border-color: var(--warn-border); }
.note.error { background: var(--error-bg); border-color: var(--error-border); }

.status { display: inline-block; font-size: .75rem; font-weight: 600; padding: .05rem .5rem; border-radius: 999px; border: 1px solid var(--border); white-space: nowrap; }
.status-running { color: var(--accent); border-color: var(--ai-border); background: var(--ai-bg); }
.status-ok { color: var(--t-entrega); border-color: var(--t-entrega); }
.status-recuo_few_shot { color: var(--t-pacote); border-color: var(--warn-border); background: var(--warn-bg); }
.status-indisponivel, .status-failed { color: var(--danger); border-color: var(--error-border); background: var(--error-bg); }

/* ---------------------------------------------------------------- execução */
.run-head { display: flex; align-items: flex-start; gap: 1rem; }
.run-head > div { flex: 1; min-width: 0; }
.run-head h2 { overflow-wrap: anywhere; }
.scope-sent summary { cursor: pointer; color: var(--muted); }
.scope-sent pre {
  white-space: pre-wrap;
  font: inherit;
  font-size: .9rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .75rem 1rem;
  max-height: 20rem;
  overflow: auto;
}

.progress { display: flex; align-items: center; gap: 1rem; }
.progress .step { font-weight: 600; font-size: 1.05rem; }
.spinner {
  width: 2rem; height: 2rem; flex: none;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

.eap { display: flex; flex-direction: column; gap: .6rem; }
.eap-bar { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.tabs { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.tabs button { border: 0; background: transparent; color: var(--text); padding: .4rem 1rem; border-radius: 0; }
.tabs button[aria-selected="true"] { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.legend { list-style: none; display: flex; flex-wrap: wrap; gap: .9rem; margin: 0; padding: 0; font-size: .85rem; color: var(--muted); }
.legend li { display: inline-flex; align-items: center; gap: .35rem; }
.swatch { width: .8rem; height: .8rem; border-radius: 3px; display: inline-block; }

.eap-body { display: grid; grid-template-columns: minmax(0, 1fr) 22rem; gap: 1rem; align-items: start; }
.eap-view { padding: 0; overflow: hidden; }
.card { position: sticky; top: 4.2rem; max-height: calc(100vh - 5.5rem); overflow-y: auto; }

/* ---------------------------------------------------------------- organograma */
.orgchart-wrap { position: relative; }
.chart-toolbar {
  position: absolute; top: .6rem; left: .6rem; z-index: 1;
  display: flex; gap: .35rem; flex-wrap: wrap;
}
.chart-toolbar button { background: var(--surface); }
.orgchart {
  display: block;
  width: 100%;
  height: 72vh;
  min-height: 26rem;
  cursor: grab;
  touch-action: none;
  user-select: none;
  background:
    radial-gradient(circle, var(--border) 1px, transparent 1px) 0 0 / 22px 22px,
    var(--surface-2);
}
.orgchart.dragging { cursor: grabbing; }
.orgchart .edge { fill: none; stroke: var(--muted); stroke-opacity: .55; stroke-width: 1.4; }
.orgchart .node { cursor: pointer; }
.orgchart .node .box { fill: var(--surface); stroke: var(--border); stroke-width: 1.2; }
.orgchart .node:hover .box { stroke: var(--muted); }
.orgchart .node.selected .box { stroke: var(--accent); stroke-width: 2.5; }
.orgchart .node:focus { outline: none; }
.orgchart .node:focus-visible .box { stroke: var(--focus); stroke-width: 3; }
.orgchart .code { font-size: 12px; font-weight: 700; fill: var(--text); }
.orgchart .kind { font-size: 10.5px; fill: var(--muted); }
.orgchart .name { font-size: 12.5px; fill: var(--text); }
.orgchart .toggle { cursor: pointer; }
.orgchart .toggle circle { fill: var(--surface); stroke: var(--muted); stroke-width: 1.2; }
.orgchart .toggle:hover circle, .orgchart .toggle:focus-visible circle { stroke: var(--accent); stroke-width: 2; }
.orgchart .toggle:focus { outline: none; }
.orgchart .toggle text { font-size: 14px; font-weight: 700; fill: var(--text); }

.orgchart .type-raiz .band { fill: var(--t-raiz); }
.orgchart .type-raiz .box { fill: var(--t-raiz-bg); }
.orgchart .type-raiz { cursor: default; }
.orgchart .type-fase_area .band { fill: var(--t-fase); }
.orgchart .type-fase_area .box { fill: var(--t-fase-bg); }
.orgchart .type-entrega .band { fill: var(--t-entrega); }
.orgchart .type-entrega .box { fill: var(--t-entrega-bg); }
.orgchart .type-pacote_trabalho .band { fill: var(--t-pacote); }
.orgchart .type-pacote_trabalho .box { fill: var(--t-pacote-bg); }
.orgchart .type-atividade .band { fill: var(--t-atividade); }
.orgchart .type-atividade .box { fill: var(--t-atividade-bg); }

.swatch.type-fase_area, .chip.type-fase_area { background: var(--t-fase); }
.swatch.type-entrega, .chip.type-entrega { background: var(--t-entrega); }
.swatch.type-pacote_trabalho, .chip.type-pacote_trabalho { background: var(--t-pacote); }
.swatch.type-atividade, .chip.type-atividade { background: var(--t-atividade); }

/* ---------------------------------------------------------------- lista */
.outline, .outline ul { list-style: none; margin: 0; padding: 0; }
.outline { padding: .6rem; max-height: 72vh; overflow: auto; }
.outline .line { display: flex; align-items: center; gap: .15rem; }
.twisty, .twisty-space { width: 1.5rem; flex: none; text-align: center; }
.twisty { background: none; border: 0; color: var(--muted); padding: .2rem 0; }
.outline .row {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: .6rem;
  background: none; border: 0; border-radius: 6px;
  padding: .3rem .5rem; text-align: left; color: var(--text);
}
.outline .row:hover { background: var(--surface-2); }
.outline .row.selected { background: var(--ai-bg); box-shadow: inset 3px 0 0 var(--accent); }
.outline .code { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 3.2rem; }
.outline .name { flex: 1; min-width: 0; }
.outline .row.type-fase_area .name { font-weight: 600; }

.chip {
  display: inline-block; flex: none;
  font-size: .72rem; font-weight: 600;
  padding: .05rem .45rem; border-radius: 999px;
  color: #fff; background: var(--muted);
}
@media (prefers-color-scheme: dark) { .chip { color: #0b1220; } }
.chip.kind-adicionar { background: var(--t-entrega); }
.chip.kind-fundir, .chip.kind-mover, .chip.kind-absorver { background: var(--t-fase); }
.chip.severity-info { background: var(--muted); }
.chip.severity-aviso { background: var(--t-pacote); }
.chip.severity-erro { background: var(--danger); }

/* ---------------------------------------------------------------- ficha e listas */
.eyebrow { font-size: .8rem; color: var(--muted); font-weight: 600; margin-bottom: .15rem; }
.card h3 { font-size: 1.1rem; overflow-wrap: anywhere; }
.card .note { margin: .5rem 0; font-size: .88rem; }
.fields { display: grid; grid-template-columns: 1fr; gap: .1rem; margin: .5rem 0 0; }
.fields dt { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-top: .55rem; }
.fields dd { margin: 0; overflow-wrap: anywhere; }
.bullets { margin: .1rem 0 0; padding-left: 1.1rem; }
blockquote { margin: .1rem 0 0; padding: .3rem .7rem; border-left: 3px solid var(--border); color: var(--muted); font-style: italic; }
.links { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .2rem; font-size: .9rem; }

.cards { list-style: none; margin: .5rem 0 0; padding: 0; display: flex; flex-direction: column; gap: .6rem; }
.item { border: 1px solid var(--border); border-radius: 8px; padding: .65rem .85rem; background: var(--surface-2); }
.item-head { display: flex; align-items: baseline; gap: .5rem; flex-wrap: wrap; }
.item .fields { margin-top: .2rem; }
.item .fields dt { margin-top: .35rem; }

/* ---------------------------------------------------------------- Boas Práticas */
textarea.short { min-height: 6rem; }
.bp-result { display: flex; flex-direction: column; gap: 1rem; max-width: 60rem; }
.bp-answer { display: flex; flex-direction: column; gap: .35rem; }
.bp-answer h3 { margin-top: .75rem; }
.bp-question { font-weight: 600; overflow-wrap: anywhere; }
.bp-text p, .bp-text li { line-height: 1.6; overflow-wrap: anywhere; }
.bp-text h4 { margin: .9rem 0 .3rem; font-size: 1rem; }
.bp-text ul, .bp-text ol { margin: .2rem 0 .6rem; padding-left: 1.3rem; }
.cite {
  border: 0;
  background: none;
  padding: 0 .1rem;
  color: var(--accent);
  font-weight: 600;
  font-size: .85em;
  vertical-align: super;
  line-height: 1;
  cursor: pointer;
}
.cite:hover { text-decoration: underline; }
.refs { margin: 0; padding-left: 1.6rem; display: flex; flex-direction: column; gap: .5rem; }
.refs li { padding: .35rem .5rem; border-radius: 6px; scroll-margin-top: 5rem; }
.refs li.highlight { background: var(--ai-bg); outline: 1px solid var(--ai-border); }
.ref-label { display: block; font-weight: 600; overflow-wrap: anywhere; }
.ref-meta { display: block; font-size: .85rem; color: var(--muted); }
.ref-source { display: block; font-size: .85rem; color: var(--muted); font-style: italic; overflow-wrap: anywhere; }
.small-text { font-size: .85rem; }

/* ---------------------------------------------------------------- telas estreitas */
@media (max-width: 1100px) {
  .eap-body { grid-template-columns: 1fr; }
  .card { position: static; max-height: none; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); }
  .view { padding: 1rem; }
}
