/* Editor de R — cores do tema cosmo do Quarto */

:root {
  --azul: #2780e3;
  --azul-escuro: #1a5fb4;
  --texto: #1f2328;
  --texto-suave: #57606a;
  --fundo: #f6f8fa;
  --painel: #ffffff;
  --borda: #d0d7de;
  --entrada: #1d4ed8;
  --erro: #b42318;
  --erro-fundo: #fef3f2;
  --aviso: #9a6700;
  --dica-fundo: #fff8c5;
  --mono: ui-monospace, "SFMono-Regular", "DejaVu Sans Mono", Menlo, Consolas, monospace;
  --raio: 6px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--texto);
  background: var(--fundo);
  display: flex;
  flex-direction: column;
}

/* ---------- Topo ---------- */

.topo {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.5rem 1rem;
  background: var(--azul);
  color: #fff;
}

.topo h1 { font-size: 1.1rem; margin: 0; font-weight: 600; }

.status {
  margin-left: auto;
  font-size: 0.85rem;
  padding: 0.2rem 0.7rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60vw;
}
.status::before { content: "● "; }
.status.carregando::before, .status.executando::before { color: #ffd33d; }
.status.pronto::before { color: #7ee2a8; }
.status.falha::before { color: #ffb4a8; }

/* ---------- Layout (como no RStudio) ----------
   Duas colunas; em cada uma, dois painéis. Os tamanhos ficam em variáveis
   (--col-esq, --lin-esq, --lin-dir) que as divisórias alteram ao arrastar. */

.area {
  flex: 1;
  min-height: 0;
  display: flex;
  padding: 0.5rem;
  --col-esq: 50%;
  --lin-esq: 55%;
  --lin-dir: 45%;
}
.coluna { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#coluna-esq { flex: 0 0 var(--col-esq); }
#coluna-dir { flex: 1 1 0; }
#painel-editor { flex: 0 0 var(--lin-esq); }
#painel-console { flex: 1 1 0; }
#painel-ambiente { flex: 0 0 var(--lin-dir); }
#painel-abas { flex: 1 1 0; }

.painel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--painel);
  border: 1px solid var(--borda);
  border-radius: var(--raio);
  overflow: hidden;
}

.divisoria { flex: 0 0 0.5rem; }
.divisoria.vertical { cursor: col-resize; }
.divisoria.horizontal { cursor: row-resize; }
.divisoria:hover, .divisoria.arrastando { background: #ddf4ff; border-radius: 3px; }
body.arrastando { user-select: none; }
body.arrastando iframe { pointer-events: none; }

@media (max-width: 900px) {
  html, body { height: auto; }
  .area { flex: none; flex-direction: column; padding-inline: 1rem; }
  .coluna { display: contents; }
  .divisoria { display: none; }
  .painel { flex: none !important; margin-bottom: 0.5rem; }
  #painel-editor { height: 55vh; }
  #painel-console { height: 45vh; }
  #painel-ambiente { height: 40vh; }
  #painel-abas { height: 70vh; }
  .status { max-width: 45vw; }
}

/* ---------- Barras e botões ---------- */

.barra {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--borda);
  background: var(--fundo);
}
.barra.fina { padding: 0.3rem 0.5rem; font-size: 0.85rem; color: var(--texto-suave); }
.espaco { flex: 1; }

select { max-width: 11rem; }

button, select {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--borda);
  border-radius: var(--raio);
  background: #fff;
  color: var(--texto);
  cursor: pointer;
}
button:hover:not(:disabled), select:hover { border-color: var(--azul); }
button:disabled { opacity: 0.45; cursor: default; }
button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--azul);
  outline-offset: 1px;
}
button.primario { background: var(--azul); border-color: var(--azul); color: #fff; font-weight: 600; }
button.primario:hover:not(:disabled) { background: var(--azul-escuro); }
button.pequeno { padding: 0.1rem 0.45rem; font-size: 0.8rem; }

/* ---------- Editor (CodeMirror) ---------- */

.editor { flex: 1; min-height: 0; }
.editor .CodeMirror {
  height: 100%;
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.5;
}
.CodeMirror-gutters { background: var(--fundo); border-right: 1px solid var(--borda); }
.CodeMirror-linenumber { color: #8c959f; }
.CodeMirror .linha-executada { background: #ddf4ff; transition: background 0.6s; }

/* ---------- Console ---------- */

.console {
  flex: 1;
  min-height: 0;
  overflow: auto;
  cursor: text;
  padding: 0.5rem 0.75rem;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.45;
  white-space: pre;
  tab-size: 4;
}
#console > div { min-height: 1.45em; }
.console .entrada { color: var(--entrada); }
.console .saida { color: var(--texto); }
.console .mensagem { color: var(--texto-suave); white-space: pre-wrap; }
.console .aviso { color: var(--aviso); white-space: pre-wrap; }
.console .erro {
  color: var(--erro);
  background: var(--erro-fundo);
  white-space: pre-wrap;
  padding-inline: 0.3rem;
  border-radius: 3px;
}
.console .dica {
  font-family: system-ui, sans-serif;
  white-space: normal;
  background: var(--dica-fundo);
  border-left: 3px solid #d4a72c;
  padding: 0.3rem 0.6rem;
  margin: 0.25rem 0 0.5rem;
  border-radius: 3px;
}
.console .sistema {
  font-family: system-ui, sans-serif;
  white-space: normal;
  color: var(--texto-suave);
  font-style: italic;
}

/* Linha de comando no fim do console: ">" na 1ª linha, "+" nas seguintes */
.linha-comando { display: flex; align-items: flex-start; gap: 0.5ch; }
.linha-comando .prompt {
  margin: 0;
  font: inherit;
  color: var(--entrada);
  font-weight: 700;
  user-select: none;
}
.linha-comando textarea {
  flex: 1;
  min-width: 0;
  border: none;
  resize: none;
  overflow: hidden;
  padding: 0;
  margin: 0;
  font: inherit;
  line-height: inherit;
  color: var(--entrada);
  background: transparent;
  white-space: pre;
}
.linha-comando textarea:focus-visible { outline: none; }
.console.ocupado .linha-comando { opacity: 0.45; }
.console .opcoes { color: var(--texto-suave); white-space: pre-wrap; }

/* ---------- Abas ---------- */

.abas {
  display: flex;
  gap: 0.2rem;
  padding: 0.3rem 0.5rem 0;
  border-bottom: 1px solid var(--borda);
  background: var(--fundo);
  overflow-x: auto;
}
.abas button {
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: var(--raio) var(--raio) 0 0;
  background: transparent;
  color: var(--texto-suave);
  margin-bottom: -1px;
}
.abas button[aria-selected="true"] {
  background: var(--painel);
  border-color: var(--borda);
  color: var(--texto);
  font-weight: 600;
}

.aba { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.rolagem { flex: 1; min-height: 0; overflow: auto; }

.graficos {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  overflow: hidden;
}
.graficos canvas {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.vazio { color: var(--texto-suave); text-align: center; padding: 1rem; }

.tabela { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.tabela th, .tabela td {
  text-align: left;
  padding: 0.3rem 0.6rem;
  border-bottom: 1px solid var(--fundo);
  vertical-align: top;
}
.tabela th { position: sticky; top: 0; background: var(--painel); color: var(--texto-suave); font-weight: 600; }
.tabela td:first-child { font-family: var(--mono); white-space: nowrap; }
.tabela td.valor { font-family: var(--mono); color: var(--texto-suave); word-break: break-all; }
.tabela .num { text-align: right; white-space: nowrap; }
.tabela td.acoes { text-align: right; white-space: nowrap; }

.ajuda { flex: 1; min-height: 0; overflow: auto; padding: 0.25rem 1rem 1rem; line-height: 1.55; }
.ajuda h2 { font-size: 1rem; margin: 1rem 0 0.3rem; }
.ajuda ul { padding-left: 1.2rem; margin: 0.3rem 0; }
.ajuda li { margin-bottom: 0.3rem; }
kbd {
  font-family: var(--mono);
  font-size: 0.8em;
  padding: 0.05rem 0.35rem;
  border: 1px solid var(--borda);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--fundo);
}
code { font-family: var(--mono); font-size: 0.9em; }

/* ---------- Aviso flutuante ---------- */

.aviso {
  position: fixed;
  left: 50%;
  bottom: calc(1.5rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  max-width: min(36rem, calc(100vw - 2rem));
  background: #24292f;
  color: #fff;
  padding: 0.6rem 1rem;
  border-radius: var(--raio);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  z-index: 10;
}
.aviso input {
  width: 100%;
  margin-top: 0.4rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  padding: 0.2rem 0.4rem;
}

/* ---------- History, Packages e Help ---------- */

.historico { list-style: none; margin: 0; padding: 0; font-family: var(--mono); font-size: 0.82rem; }
.historico li {
  padding: 0.2rem 0.6rem;
  border-bottom: 1px solid var(--fundo);
  white-space: pre-wrap;
  word-break: break-all;
  cursor: pointer;
}
.historico li:hover { background: #ddf4ff; }

.tabela input[type="checkbox"] { margin: 0; cursor: pointer; }
.tabela tr.carregado td:nth-child(2) { font-weight: 600; }

.ajuda-titulo { font-weight: 600; color: var(--texto); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ajuda-doc { flex: 1; min-height: 0; width: 100%; border: none; background: #fff; }

