:root {
  --fundo: #f3f5f7;
  --superficie: #ffffff;
  --texto: #1d2329;
  --suave: #5f6b76;
  --linha: #d9dee3;
  --marca: #1f5f8b;
  --marca-texto: #ffffff;
  --selecao: #1f5f8b;
  --cabecalho: #eef1f4;
  --word: #2b579a;
  --excel: #217346;
}
@media (prefers-color-scheme: dark) {
  :root {
    --fundo: #15191d;
    --superficie: #1f252b;
    --texto: #e7ebee;
    --suave: #9aa6b1;
    --linha: #343c44;
    --marca: #5aa6dc;
    --marca-texto: #0b1a26;
    --selecao: #5aa6dc;
    --cabecalho: #262d34;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--fundo);
  color: var(--texto);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }

.tela { min-height: 100%; }

/* ---------- início ---------- */
#inicio { max-width: 560px; margin: 0 auto; padding: 24px 16px calc(24px + env(safe-area-inset-bottom)); }
.marca { display: flex; align-items: center; gap: 12px; margin: 8px 0 28px; }
.marca h1 { font-size: 26px; margin: 0; }
#inicio h2 { font-size: 15px; color: var(--suave); text-transform: uppercase; letter-spacing: .04em; margin: 32px 0 8px; }
.dica { color: var(--suave); font-size: 14px; margin: 8px 0; }

.botao {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 16px; border-radius: 10px;
  border: 1px solid var(--linha); background: var(--superficie); color: var(--texto);
  font: inherit; font-weight: 600; cursor: pointer;
}
.botao.cheio, .botao.grande { background: var(--marca); color: var(--marca-texto); border-color: var(--marca); }
.botao.grande { width: 100%; min-height: 60px; font-size: 18px; }
.botao.pequeno { min-height: 36px; padding: 0 12px; font-size: 14px; }
.novos { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }

#recentes { list-style: none; margin: 0; padding: 0; }
#recentes li {
  display: flex; align-items: center; gap: 12px;
  background: var(--superficie); border: 1px solid var(--linha); border-radius: 10px;
  padding: 10px 12px; margin-bottom: 8px; cursor: pointer;
}
.tipo { flex: none; width: 36px; height: 36px; border-radius: 8px; color: #fff; font-weight: 700; font-size: 13px; display: grid; place-items: center; }
.tipo.docx { background: var(--word); }
.tipo.xlsx, .tipo.csv { background: var(--excel); }
.recente-nome { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recente-data { font-size: 13px; color: var(--suave); }
.tirar { flex: none; border: 0; background: none; color: var(--suave); font-size: 22px; width: 36px; height: 36px; cursor: pointer; }

/* ---------- editor ---------- */
#editor { display: flex; flex-direction: column; height: 100dvh; }
.barra {
  flex: none; display: flex; align-items: center; gap: 8px;
  padding: 8px 8px 8px 4px; padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--superficie); border-bottom: 1px solid var(--linha);
}
.icone { border: 0; background: none; color: var(--texto); font-size: 32px; line-height: 1; width: 40px; height: 40px; cursor: pointer; }
#nome {
  flex: 1; min-width: 0; font: inherit; font-weight: 600; color: var(--texto);
  background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 6px 8px;
}
#nome:focus { border-color: var(--linha); outline: none; }

.ferramentas {
  flex: none; display: flex; gap: 4px; overflow-x: auto; padding: 6px 8px;
  background: var(--superficie); border-bottom: 1px solid var(--linha);
}
.ferramentas button {
  flex: none; min-width: 44px; height: 40px; border: 1px solid var(--linha); border-radius: 8px;
  background: var(--fundo); color: var(--texto); font: inherit; font-size: 15px; padding: 0 10px; cursor: pointer;
}

.barra-formula {
  flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 8px;
  background: var(--superficie); border-bottom: 1px solid var(--linha);
}
#ref { flex: none; min-width: 48px; font-weight: 700; color: var(--suave); font-size: 14px; text-align: center; }
#formula {
  flex: 1; min-width: 0; height: 38px; font: inherit; color: var(--texto);
  background: var(--fundo); border: 1px solid var(--linha); border-radius: 8px; padding: 0 10px;
}
#formula:read-only { color: var(--suave); }

#conteudo { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }

/* documento: a página fica clara nos dois temas, como papel */
.pagina-word {
  max-width: calc(var(--largura, 640px) + 64px); margin: 16px auto; padding: 32px;
  background: #fff; color: #000; box-shadow: 0 1px 4px rgba(0,0,0,.12);
  font-family: Calibri, Carlito, "Segoe UI", Arial, sans-serif; font-size: 10pt; line-height: 1.15;
}
@media (max-width: 600px) { .pagina-word { margin: 0; padding: 20px 16px; box-shadow: none; min-height: 100%; } }
.folha { min-height: 50vh; outline: none; white-space: pre-wrap; overflow-wrap: break-word; tab-size: 4; }
.folha[contenteditable="true"] { caret-color: var(--word); }
.folha p { margin: 0; }
.folha p[data-marca]::before {
  content: attr(data-marca); display: inline-block; min-width: var(--pendurado, 18pt);
  padding-right: 4px; text-indent: 0; white-space: pre; font-style: normal; text-decoration: none;
}
.pagina-word img { max-width: 100%; height: auto; vertical-align: bottom; }
.atomo { white-space: pre-wrap; }
.quebra-pagina { display: block; height: 0; margin: 14px 0; border-top: 1px dashed #b0b0b0; }
.quebra-antes { border-top: 1px dashed #b0b0b0; padding-top: 10px; }
.caixa-texto { display: inline-block; max-width: 100%; border: 1px dashed #b0b0b0; padding: 4px 6px; }
.objeto { color: #888; font-size: 12px; }
.atomo-bloco { display: block; margin: 6px 0; padding: 6px 8px; border: 1px dashed #c8c8c8; color: #777; font-size: 12px; }
.tabela-word { border-collapse: collapse; width: 100%; margin: 4pt 0; white-space: normal; }
.tabela-word td { border: 1px solid #000; padding: 2pt 5pt; vertical-align: top; white-space: pre-wrap; }
.tabela-word.sem-borda td { border: 1px dotted #d0d0d0; }
.cabecalho-word, .rodape-word { opacity: .7; white-space: pre-wrap; outline: none; min-height: 1.2em; }
.cabecalho-word { border-bottom: 1px dashed #d0d0d0; padding-bottom: 8px; margin-bottom: 16px; }
.rodape-word { border-top: 1px dashed #d0d0d0; padding-top: 8px; margin-top: 16px; }
/* editando: o cabeçalho e o rodapé viram campos que se tocam */
.cabecalho-word[contenteditable="true"], .rodape-word[contenteditable="true"] {
  opacity: 1; background: #f6f8fa; border: 1px dashed #b4c0cc; border-radius: 6px; padding: 6px 8px; caret-color: var(--word);
}
.area-vazia:not([contenteditable="true"]) { display: none; }
.area-vazia:empty::before { content: attr(data-rotulo); color: #9aa5b0; }
.cabecalho-word p, .rodape-word p { margin: 0; }

/* planilha */
table.grade { table-layout: fixed; border-collapse: separate; border-spacing: 0; font-size: 14px; background: var(--superficie); }
.grade th, .grade td {
  border-right: 1px solid var(--linha); border-bottom: 1px solid var(--linha);
  height: 30px; padding: 0 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.grade td { color: #1a1a1a; background: #fff; cursor: cell; }
.grade thead th { position: sticky; top: 0; z-index: 2; background: var(--cabecalho); color: var(--suave); font-weight: 600; text-align: center; }
.grade tbody th { position: sticky; left: 0; z-index: 1; background: var(--cabecalho); color: var(--suave); font-weight: 600; min-width: 40px; text-align: center; }
.grade thead th:first-child { left: 0; z-index: 3; }
.grade td.num { text-align: right; }
.grade td.selecionada { outline: 2px solid var(--selecao); outline-offset: -2px; }
.mais-linhas { margin: 12px; }

.abas {
  flex: none; display: flex; gap: 2px; overflow-x: auto; padding: 0 8px calc(0px + env(safe-area-inset-bottom));
  background: var(--cabecalho); border-top: 1px solid var(--linha);
}
.abas button {
  flex: none; border: 0; background: none; color: var(--suave); font: inherit; font-size: 14px;
  padding: 10px 14px; border-bottom: 3px solid transparent; cursor: pointer;
}
.abas button.atual { color: var(--excel); border-bottom-color: var(--excel); font-weight: 700; background: var(--superficie); }
@media (prefers-color-scheme: dark) { .abas button.atual { color: #6fd39a; border-bottom-color: #6fd39a; } }

/* avisos */
.aviso {
  position: fixed; left: 16px; right: 16px; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 20;
  max-width: 520px; margin: 0 auto; padding: 12px 16px; border-radius: 10px;
  background: #222b33; color: #fff; box-shadow: 0 4px 16px rgba(0,0,0,.25); font-size: 15px;
}
.pergunta {
  position: fixed; left: 16px; right: 16px; top: 30%; z-index: 30;
  max-width: 420px; margin: 0 auto; padding: 20px; border-radius: 14px;
  background: var(--superficie); border: 1px solid var(--linha); box-shadow: 0 8px 32px rgba(0,0,0,.3);
}
.pergunta p { margin: 0 0 16px; }
.pergunta div { display: flex; gap: 10px; justify-content: flex-end; }
.carregando { padding: 40px 16px; text-align: center; color: var(--suave); }
