:root {
  --bg: #f4f1ec;
  --paper: #fffdf9;
  --paper-border: #e6e1d8;
  --ink: #1a1816;
  --ink-soft: #4a463f;
  --muted: #7d776c;
  --rule: #d8d2c5;
  --accent: #7a1f1f;
  --accent-soft: #f4e7e3;
  --accent-ink: #fff;
  --warn: #8a5a00;
  --warn-soft: #fbf1d8;
  --erro: #8a1a16;
  --erro-soft: #fbe7e4;
  --ok: #1d5a3e;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
  --radius: 6px;
  --serif: "Iowan Old Style","Georgia","Charter","Cambria",ui-serif,serif;
  --sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", "Menlo", monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
a { color: var(--accent); text-decoration: none; border-bottom: 1px solid transparent; }
a:hover { border-bottom-color: var(--accent); }
button { font: inherit; }

/* ========== Topbar ========== */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: .55rem 1.25rem;
  background: var(--paper);
  border-bottom: 1px solid var(--paper-border);
  position: sticky; top: 0; z-index: 50;
}
.topbar .brand {
  display: flex; align-items: center; gap: .55rem;
  font-family: var(--serif);
  font-weight: 600; font-size: 1.05rem;
  color: var(--ink); border-bottom: none;
}
.topbar .brand::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
}
.topbar nav { display: flex; gap: 1rem; align-items: center; }
.topbar nav a, .topbar nav button {
  color: var(--muted); font-size: .88rem;
  background: none; border: none; cursor: pointer; padding: 0;
  border-bottom: 1px solid transparent;
}
.topbar nav a:hover, .topbar nav button:hover { color: var(--ink); border-bottom-color: var(--ink); }

.content { max-width: 1100px; margin: 0 auto; padding: 1.25rem 1rem 6rem; }
.content.narrow { max-width: 720px; }
.content.wide { max-width: 1280px; padding: 1rem .5rem 6rem; }

/* ========== Tipografia ========== */
h1, h2, h3, h4 { font-family: var(--serif); font-weight: 600; letter-spacing: -.005em; line-height: 1.25; }
h1 { font-size: 2rem; margin: 0 0 .25rem; }
h2 { font-size: 1.4rem; margin: 1.4em 0 .55em; }
h3 { font-size: 1.1rem; margin: 1.1em 0 .4em; }
.eyebrow { text-transform: uppercase; font-size: .72rem; letter-spacing: .12em; color: var(--muted); font-weight: 600; }
.lead { font-family: var(--serif); font-size: 1.15rem; color: var(--ink-soft); }
.meta { color: var(--muted); font-size: .85rem; margin: 0; }
.empty { color: var(--muted); font-style: italic; }
.dica { color: var(--muted); font-size: .85rem; margin: 0 0 .75rem; }

/* ========== Botões / forms ========== */
.btn, button[type="submit"], .btn-primary {
  display: inline-flex; align-items: center; gap: .4rem;
  background: var(--ink); color: #fff; border: 1px solid var(--ink);
  padding: .5rem .95rem; border-radius: var(--radius); cursor: pointer;
  font-size: .9rem; font-weight: 500; text-decoration: none;
  transition: transform .05s ease, background .15s ease;
}
.btn:hover, button[type="submit"]:hover, .btn-primary:hover { background: #000; color: #fff; }
.btn:active { transform: translateY(1px); }
.btn-sm { padding: .35rem .7rem; font-size: .82rem; }
.btn-ghost { background: transparent; color: var(--ink); border: 1px solid var(--rule); }
.btn-ghost:hover { background: var(--paper); border-color: var(--ink); color: var(--ink); }
.btn-link { background: none; border: none; color: var(--muted); cursor: pointer; padding: 0; }
.btn-link:hover { color: var(--ink); }

input, textarea, select {
  font-family: inherit; font-size: 1rem;
  width: 100%; padding: .55rem .75rem;
  border: 1px solid var(--rule); border-radius: var(--radius);
  background: var(--paper); color: var(--ink);
  transition: border-color .15s ease, box-shadow .15s ease;
}
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(122,31,31,.12);
}
label { display: block; font-size: .85rem; color: var(--ink-soft); margin-bottom: .35rem; font-weight: 500; }
.form-stack { display: flex; flex-direction: column; gap: 1rem; }
.form-stack > div { display: flex; flex-direction: column; }
.form-inline { display: flex; gap: .5rem; flex-wrap: wrap; align-items: flex-end; }
.form-inline > div { flex: 1; min-width: 180px; }

.alert { padding: .65rem .85rem; border-radius: var(--radius); font-size: .9rem; margin: .5rem 0; }
.alert-erro { background: var(--erro-soft); color: var(--erro); border: 1px solid #f0c8c4; }
.alert-ok { background: #e2f0e7; color: var(--ok); border: 1px solid #b9d6c5; }
.alert-info { background: var(--warn-soft); color: var(--warn); border: 1px solid #ecd9a8; }
.flash-stack { display: flex; flex-direction: column; gap: .35rem; margin-bottom: 1rem; }

/* ========== Login ========== */
.login-shell { min-height: calc(100vh - 56px); display: flex; align-items: center; justify-content: center; padding: 2rem; }
.login-card { width: 360px; max-width: 100%; background: var(--paper); border: 1px solid var(--paper-border); border-radius: 10px; padding: 2.25rem 2rem; box-shadow: var(--shadow); }
.login-card h1 { font-size: 1.4rem; margin-bottom: .25rem; }
.login-card .meta { margin-bottom: 1.5rem; }
.login-card input[type="password"] { text-align: center; letter-spacing: .25em; font-size: 1.1rem; }
.login-card button { width: 100%; justify-content: center; padding: .7rem; }

/* ========== Index (cards de protocolos) ========== */
.page-head {
  display: flex; justify-content: space-between; align-items: flex-end;
  gap: 1rem; margin-bottom: 1.25rem;
  border-bottom: 1px solid var(--rule); padding-bottom: .75rem;
}
.page-head .right { display: flex; gap: .5rem; align-items: center; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.card {
  background: var(--paper); border: 1px solid var(--paper-border); border-radius: 8px;
  padding: 1rem 1.1rem; transition: transform .1s, border-color .15s, box-shadow .15s;
  display: flex; flex-direction: column; min-height: 110px;
  text-decoration: none; color: var(--ink);
}
.card:hover { transform: translateY(-2px); border-color: var(--ink); box-shadow: var(--shadow); }
.card .title { font-family: var(--serif); font-weight: 600; font-size: 1.1rem; margin-bottom: .25rem; }
.card .badges { margin-top: auto; display: flex; gap: .35rem; flex-wrap: wrap; padding-top: .5rem; }

.badge {
  display: inline-block; font-size: .68rem; padding: .12rem .5rem;
  border-radius: 999px; background: var(--bg); color: var(--ink-soft);
  border: 1px solid var(--rule); font-weight: 500; letter-spacing: .03em;
  text-transform: uppercase;
}
.badge.rascunho { background: var(--warn-soft); color: var(--warn); border-color: #ecd9a8; }
.badge.aprovado { background: #e2f0e7; color: var(--ok); border-color: #b9d6c5; }
.badge.revisao  { background: var(--accent-soft); color: var(--accent); border-color: #ebcbc6; }

/* ========== Documento (protocolo) ========== */
.doc-paper {
  background: var(--paper);
  border: 1px solid var(--paper-border);
  border-radius: 6px;
  padding: 1.5rem 2rem 2.5rem;
  box-shadow: var(--shadow);
}
@media (max-width: 700px) { .doc-paper { padding: 1rem; } .content.wide { padding: .5rem .25rem 5rem; } }

.crumbs { display: flex; gap: .4rem; align-items: center; font-size: .82rem; color: var(--muted); margin: 0 0 .75rem; }
.crumbs a { color: var(--muted); border-bottom: none; }
.crumbs a:hover { color: var(--ink); }

.pop-head-toggle { margin-bottom: 1rem; }
.pop-head-toggle > summary { cursor: pointer; color: var(--muted); font-size: .82rem; user-select: none; }
.pop-head-toggle > summary:hover { color: var(--ink); }
.pop-head-toggle[open] > summary { margin-bottom: .65rem; }

.pop-head {
  border: 1px solid var(--ink);
  margin-bottom: 1rem;
  font-size: .82rem;
}
.pop-head table { width: 100%; border-collapse: collapse; }
.pop-head th, .pop-head td {
  border: 1px solid var(--ink); padding: .35rem .55rem;
  vertical-align: middle; text-align: left;
}
.pop-head th { font-weight: 600; background: #f3eee5; font-size: .78rem; }
.pop-head .titulo { font-family: var(--serif); font-weight: 700; font-size: .95rem; text-align: center; }
.pop-head .logo-cell { width: 110px; text-align: center; }
.pop-head .logo-cell img { max-width: 90px; max-height: 60px; object-fit: contain; }
.pop-head td.preencher:empty::before { content: "—"; color: var(--muted); }

.doc-titulo { margin: .5rem 0 1.25rem; }
.doc-titulo h1 { font-size: 1.85rem; }

.toolbar-doc {
  display: flex; justify-content: space-between; align-items: center;
  padding: .5rem .75rem; margin: 0 0 1rem;
  background: var(--bg); border: 1px solid var(--rule); border-radius: 8px;
  font-size: .85rem; flex-wrap: wrap; gap: .5rem;
}
.toolbar-doc .right { display: flex; gap: .35rem; }
.hint-handle { font-family: var(--mono); color: var(--muted); padding: 0 .3rem; }

/* ========== Cards de seção (accordion) ========== */
.secoes-grid { display: flex; flex-direction: column; gap: .35rem; }

.secao-card {
  background: var(--paper); border: 1px solid var(--paper-border);
  border-radius: 6px; overflow: hidden;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.secao-card.aberta { box-shadow: var(--shadow); border-color: var(--ink); }
.secao-card.dragging { opacity: .35; }
.secao-card.drop-target { border-color: var(--accent); border-style: dashed; }

.secao-card-head {
  display: flex; align-items: center; gap: .65rem;
  padding: .65rem .85rem; cursor: pointer;
  border-bottom: 1px solid transparent;
  font-family: var(--serif);
  user-select: none;
}
.secao-card.aberta .secao-card-head { border-bottom-color: var(--paper-border); background: #faf7f1; }
.secao-card-head:hover { background: #faf7f1; }
.secao-card-head:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

.secao-handle {
  cursor: grab; color: var(--muted); font-size: 1rem;
  user-select: none; padding: .15rem .25rem;
  border-radius: 3px; line-height: 1;
}
.secao-handle:hover { color: var(--ink); background: var(--bg); }
.secao-handle:active { cursor: grabbing; }

.secao-card-head .num { font-family: var(--mono); color: var(--muted); font-size: .8rem; min-width: 1.75rem; }
.secao-card-head .titulo-secao { flex: 1; font-size: 1.05rem; font-weight: 600; }

.secao-acoes { display: flex; gap: .25rem; align-items: center; opacity: .55; transition: opacity .15s; }
.secao-card-head:hover .secao-acoes, .secao-card.aberta .secao-acoes { opacity: 1; }
.btn-icon {
  background: none; border: none; cursor: pointer;
  padding: .25rem .4rem; border-radius: 4px; color: var(--muted);
  font-size: .9rem; line-height: 1;
}
.btn-icon:hover { background: var(--bg); color: var(--ink); }
.caret { color: var(--muted); font-size: .9rem; transition: transform .15s; margin-left: .25rem; }
.secao-card.aberta .caret { transform: rotate(180deg); }

.secao-body { padding: .35rem 1rem 1rem; }
.secao-body[hidden] { display: none; }

/* ========== Bloco principal ========== */
.bloco-conteudo { position: relative; }
.bloco-conteudo .render {
  padding: .9rem 1rem 1rem;
  font-family: var(--serif); font-size: 1.05rem; line-height: 1.7;
  border-radius: 6px;
  cursor: text; transition: background .12s ease, box-shadow .12s ease;
  min-height: 4rem;
  border: 1px solid transparent;
}
.bloco-conteudo .render:hover { background: #faf7f1; }
.bloco-conteudo .render.editando { background: var(--paper); border-color: var(--rule); box-shadow: 0 0 0 3px rgba(122,31,31,.08); cursor: text; outline: none; }
.bloco-conteudo .render p:first-child, .bloco-conteudo .render h1:first-child, .bloco-conteudo .render h2:first-child, .bloco-conteudo .render h3:first-child { margin-top: 0; }
.bloco-conteudo .render p:last-child { margin-bottom: 0; }
.bloco-conteudo .render p, .bloco-conteudo .render li { font-family: var(--serif); }
.bloco-conteudo .render img { max-width: 100%; height: auto; border-radius: 4px; }
.bloco-conteudo .render blockquote { border-left: 3px solid var(--rule); padding: 0 0 0 1rem; color: var(--ink-soft); margin: 1rem 0; font-style: italic; }
.bloco-conteudo .render code { background: #efe9dd; padding: 1px 4px; border-radius: 3px; font-size: .9em; font-family: var(--mono); }
.bloco-conteudo .render pre { background: #1a1816; color: #f8f3e7; padding: .9rem; border-radius: 4px; overflow-x: auto; }
.bloco-conteudo .render hr { border: none; border-top: 1px solid var(--rule); margin: 1.25rem 0; }
.bloco-conteudo .render table { border-collapse: collapse; margin: 1rem 0; }
.bloco-conteudo .render th, .bloco-conteudo .render td { border: 1px solid var(--rule); padding: .35rem .55rem; }

/* Texto-guia (fantasma) — só aparece quando vazio */
.guia {
  color: #b3a98f; font-style: italic; font-family: var(--serif);
  border-left: 2px dashed #d7cdb1; padding-left: .85rem;
  margin: 0;
}
.guia p:first-child { margin-top: 0; }
.guia p:last-child { margin-bottom: 0; }
.bloco-conteudo .render.editando .guia { display: none; }

/* Sub-accordion (IA + refs) */
.sub-accordion { margin-top: .6rem; display: flex; flex-direction: column; gap: .25rem; }
.sub-bloco {
  border: 1px solid var(--paper-border); border-radius: 6px;
  background: #fbfaf6;
  font-size: .92rem;
}
.sub-bloco > summary {
  list-style: none; cursor: pointer; padding: .5rem .85rem;
  display: flex; align-items: center; gap: .5rem;
  font-family: var(--sans); color: var(--ink-soft); user-select: none;
}
.sub-bloco > summary::-webkit-details-marker { display: none; }
.sub-bloco > summary::before {
  content: "▸"; color: var(--muted); font-size: .8rem; transition: transform .12s;
}
.sub-bloco[open] > summary::before { transform: rotate(90deg); }
.sub-bloco > summary:hover { color: var(--ink); }
.sub-bloco > summary .sub-label { flex: 1; font-weight: 600; }
.sub-bloco .dot { width: 6px; height: 6px; background: var(--accent); border-radius: 50%; }
.sub-bloco .bloco-conteudo .render { font-size: .95rem; padding: .65rem .85rem; }
.sub-bloco .render.simples { padding: .65rem .85rem; }
.sub-bloco.tipo-anotacoes-ia { border-left: 3px solid #6a7a4e; }

/* ========== Toolbar do editor ========== */
.md-toolbar {
  display: flex; flex-wrap: wrap; gap: .15rem; padding: .25rem .35rem;
  background: var(--bg); border: 1px solid var(--rule);
  border-radius: 6px;
  margin: -2px 0 .35rem;
  font-size: .85rem;
  position: sticky; top: 56px; z-index: 10;
}
.md-toolbar button {
  background: transparent; border: 1px solid transparent;
  color: var(--ink-soft); cursor: pointer;
  padding: .25rem .55rem; border-radius: 4px;
  font-family: var(--sans); font-weight: 500; min-width: 1.6rem;
  font-size: .82rem; line-height: 1;
}
.md-toolbar button:hover { background: var(--paper); border-color: var(--rule); color: var(--ink); }
.md-toolbar button:active { background: var(--ink); color: #fff; border-color: var(--ink); }
.md-toolbar .sep { width: 1px; background: var(--rule); margin: .15rem .25rem; }
.md-toolbar .editor-status { margin-left: auto; color: var(--muted); font-size: .75rem; padding: .25rem .35rem; align-self: center; }

/* ========== Comentários inline {{...}} ========== */
span.coment {
  background: linear-gradient(180deg, transparent 60%, #fdf6dd 60%);
  color: #6a5a1c; font-size: .92em;
  padding: 0 .15em; border-radius: 2px;
  border-bottom: 1px dotted #c2a44a;
}
span.coment::before { content: "❡ "; color: #b89645; font-weight: 700; }

/* ========== Refs ========== */
.refs { padding-left: 1.2rem; font-family: var(--serif); margin: 0; }
.refs li { margin-bottom: .5rem; }
.refs .meta-ref { display: block; font-family: var(--sans); font-size: .8rem; color: var(--muted); }

/* ========== Nova seção ========== */
.nova-secao { margin: 1rem 0; padding: .75rem 1rem; border: 1px dashed var(--rule); border-radius: var(--radius); }
.nova-secao summary { cursor: pointer; color: var(--ink-soft); font-weight: 500; font-size: .9rem; }
.nova-secao summary:hover { color: var(--ink); }

/* ========== Histórico modal ========== */
.hist-list { list-style: none; padding: 0; margin: 0; font-family: var(--sans); }
.hist-list li {
  padding: .5rem 0; border-bottom: 1px solid var(--paper-border);
  display: grid; grid-template-columns: 110px 1fr; gap: .75rem; font-size: .88rem;
}
.hist-list li:last-child { border-bottom: none; }
.hist-list .ts { color: var(--muted); font-family: var(--mono); font-size: .8rem; }
.hist-list .acao { font-weight: 600; color: var(--ink); text-transform: lowercase; }
.hist-list .det { color: var(--ink-soft); }

/* ========== Modelo cards ========== */
.modelo-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: .65rem; }
.modelo-card {
  display: block; cursor: pointer;
  border: 1px solid var(--rule); border-radius: 8px;
  padding: .85rem; background: var(--paper);
  transition: border-color .15s, box-shadow .15s;
}
.modelo-card:hover { border-color: var(--ink); }
.modelo-card input { display: none; }
.modelo-card-body { display: flex; flex-direction: column; gap: .35rem; }
.modelo-card-body strong { font-family: var(--serif); font-size: 1.05rem; }
.modelo-card input:checked + .modelo-card-body { color: var(--accent); }
.modelo-card:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ========== FAB ========== */
.fab { position: fixed; right: 1.25rem; bottom: 1.25rem; z-index: 100; }
.fab-trigger {
  width: 56px; height: 56px; border-radius: 50%; border: none;
  background: var(--ink); color: #fff; font-size: 1.4rem;
  box-shadow: 0 4px 14px rgba(0,0,0,.18); cursor: pointer;
  transition: transform .15s, background .15s;
}
.fab-trigger:hover { background: var(--accent); transform: scale(1.05); }
.fab-trigger.aberto { background: var(--accent); transform: rotate(45deg); }
.fab-menu {
  position: absolute; right: 0; bottom: 64px; min-width: 240px;
  background: var(--paper); border: 1px solid var(--paper-border);
  border-radius: 10px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; padding: .35rem;
  animation: fab-in .12s ease-out;
}
@keyframes fab-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.fab-menu[hidden] { display: none; }
.fab-action, .fab-upload {
  background: none; border: none; padding: .65rem .8rem; text-align: left;
  cursor: pointer; border-radius: 6px; font-size: .92rem; color: var(--ink);
  display: flex; align-items: center; gap: .5rem; text-decoration: none;
}
.fab-action:hover, .fab-upload:hover { background: var(--bg); }
.fab-upload { display: block; }
.fab-divider { height: 1px; background: var(--rule); margin: .25rem .5rem; }
.fab-hint { color: var(--muted); padding: .65rem .8rem; margin: 0; font-size: .85rem; }

/* ========== Dialog ========== */
.dialog {
  border: 1px solid var(--paper-border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 0; width: min(720px, 92vw);
  background: var(--paper); color: var(--ink);
}
.dialog::backdrop { background: rgba(20, 18, 14, .35); backdrop-filter: blur(2px); }
.dialog header { display: flex; justify-content: space-between; align-items: center; padding: .85rem 1.1rem; border-bottom: 1px solid var(--paper-border); }
.dialog header h3 { margin: 0; font-size: 1rem; font-family: var(--serif); }
.dialog header button { background: none; border: none; font-size: 1.4rem; cursor: pointer; line-height: 1; color: var(--muted); }
.dialog header button:hover { color: var(--ink); }
.dialog-body { padding: 1rem 1.1rem; max-height: 65vh; overflow: auto; }
.dialog-body table { width: 100%; border-collapse: collapse; }
.dialog-body td, .dialog-body th { padding: .55rem .4rem; border-bottom: 1px solid var(--paper-border); text-align: left; font-size: .9rem; }
.dialog-body th { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 600; }
.dialog-body .acoes { white-space: nowrap; text-align: right; }
.dialog-body .acoes a, .dialog-body .acoes button { margin-left: .5rem; font-size: .82rem; }

/* ========== HTML view (leitura final) ========== */
.html-view { max-width: 760px; margin: 2rem auto; padding: 0 1rem; font-family: var(--serif); font-size: 1.08rem; line-height: 1.7; }
.html-view h1 { font-size: 2.2rem; margin-bottom: .25rem; }
.html-view h2 { font-size: 1.55rem; margin: 2rem 0 .55rem; padding-bottom: .25rem; border-bottom: 1px solid var(--rule); }
.html-view h3 { font-size: 1.2rem; margin: 1.5rem 0 .4rem; }
.html-view img { max-width: 100%; border-radius: 6px; }
.html-view .secao-html { margin-bottom: 2rem; }
.html-view .anot-ia { background: #f6f8ee; border-left: 3px solid #6a7a4e; padding: .65rem 1rem; margin: 1rem 0; font-size: .96rem; }
.html-view .anot-ia h4 { margin: 0 0 .3rem; font-family: var(--sans); font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-soft); }

/* ========== Print (PDF / impressão) ========== */
@media print {
  body { background: #fff; color: #000; font-size: 11pt; }
  .topbar, .fab, .toolbar-doc, .nova-secao, .md-toolbar, .secao-acoes, .secao-handle { display: none !important; }
  .doc-paper { box-shadow: none; border: none; padding: 0; background: #fff; }
  .secao-card { border: none; background: #fff; page-break-inside: avoid; }
  .secao-card-head { background: none; border: none; padding: 0; }
  .secao-body { display: block !important; padding: 0; }
  .pop-head { border-color: #000; }
  .pop-head th, .pop-head td { border-color: #000; }
  a { color: #000; text-decoration: underline; }
}
