:root {
  --bg: #0f1115;
  --bg2: #171a21;
  --borda: #2a2f3a;
  --txt: #e6e9ef;
  --txt-dim: #9aa3b2;
  --acc: #4c8dff;
  --ok: #2ecc71;
  --off: #6b7280;
  --danger: #e5484d;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--txt);
  font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Diálogo de conexão ---- */
dialog {
  border: 1px solid var(--borda);
  background: var(--bg2);
  color: var(--txt);
  border-radius: 12px;
  padding: 24px;
  width: min(92vw, 380px);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog h1 { margin: 0; font-size: 20px; }
dialog .sub { margin: 2px 0 18px; color: var(--txt-dim); }
dialog label {
  display: block;
  margin: 12px 0;
  color: var(--txt-dim);
  font-size: 12px;
}
dialog label.check {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--txt-dim);
}
dialog input[type="text"],
dialog input[type="password"] {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 9px 10px;
  border: 1px solid var(--borda);
  border-radius: 8px;
  background: var(--bg);
  color: var(--txt);
  font-size: 14px;
}
dialog .acoes { margin-top: 16px; }
dialog .erro {
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 13px;
}

/* ---- Barra superior ---- */
#barra {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--borda);
  flex-wrap: wrap;
}
.spacer { flex: 1; }
.info { color: var(--txt-dim); font-size: 12px; }

.pill {
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
}
.pill-off { background: var(--off); }
.pill-con { background: var(--acc); }
.pill-live { background: var(--ok); }

button {
  padding: 7px 12px;
  border: 1px solid var(--borda);
  border-radius: 8px;
  background: #222733;
  color: var(--txt);
  font-size: 13px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--acc); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.danger { background: #3a1e20; border-color: #5a2a2d; }
button.danger:hover:not(:disabled) { border-color: var(--danger); }

.sel {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--txt-dim);
  font-size: 12px;
}
.sel select,
.sel input[type="range"] {
  accent-color: var(--acc);
}
.sel select {
  background: #222733;
  color: var(--txt);
  border: 1px solid var(--borda);
  border-radius: 8px;
  padding: 5px 8px;
}

/* ---- Palco ---- */
#palco {
  flex: 1;
  overflow: auto;
  position: relative;
  background:
    repeating-conic-gradient(#14171d 0% 25%, #11141a 0% 50%) 50% / 24px 24px;
}
.aviso {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  color: var(--txt-dim);
}
.aviso p { max-width: 460px; }

/* O atributo [hidden] precisa vencer o display acima; senao a camada de aviso
   fica invisivel mas por cima do canvas, engolindo o mouse. */
.aviso[hidden],
#viewport[hidden] { display: none !important; }

#viewport {
  display: inline-block;
  padding: 0;
  line-height: 0;
}
#tela {
  display: block;
  background: #000;
  outline: none;
  image-rendering: auto;
  box-shadow: 0 0 0 1px var(--borda);
  cursor: crosshair;
}
#tela.pausado { cursor: not-allowed; }

/* Campo captador de texto fica fora da tela, mas focavel. */
#captadorTexto {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
}

/* Varios tecnicos: quem nao esta com o controle so assiste. */
#tela.observando { cursor: default; }
button.destaque { background: #1d3157; border-color: var(--acc); }

/* Aviso de area de transferencia (canto inferior). */
.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: min(420px, calc(100vw - 32px));
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--bg2);
  border: 1px solid var(--borda);
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  font-size: 13px;
  z-index: 10;
}
.toast span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Mesmo cuidado do .aviso: [hidden] precisa vencer o display:flex. */
.toast[hidden] { display: none !important; }

/* ---- Celular / tablet ---- */

/* Gestos (toque, pinca, dois dedos) sao tratados no JS. */
#tela { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

#btnMais { display: none; }

@media (max-width: 760px) {
  #barra { gap: 6px; padding: 6px 8px; }
  #barra button { padding: 7px 10px; }
  #btnMais { display: inline-block; }
  #barra:not(.aberta) .extra { display: none; }
  #barra.aberta .extra { flex-basis: auto; }
  #clienteLbl { display: none; }
}

/* Barra de teclas especiais (fixa embaixo, rola na horizontal). */
.teclas {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  gap: 6px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  overflow-x: auto;
  background: var(--bg2);
  border-top: 1px solid var(--borda);
  z-index: 9;
  -webkit-overflow-scrolling: touch;
}
.teclas[hidden] { display: none !important; }
.teclas button {
  flex: 0 0 auto;
  min-width: 44px;
  height: 40px;
  padding: 0 10px;
  font-size: 14px;
}
.teclas button.ativo { background: var(--acc); border-color: var(--acc); color: #fff; }
body.com-teclas #palco { padding-bottom: 56px; }
body.com-teclas .toast { bottom: 68px; }

/* No toque, o captador fica dentro da tela (alguns teclados virtuais nao abrem
   para campo fora da tela). Continua invisivel. */
body.toque #captadorTexto { left: 0; top: auto; bottom: 0; }
/* F1..F12 so aparecem depois de tocar em "Fn". */
.teclas:not(.com-fn) .fn { display: none; }

/* ---- Lista de clientes (depois do login) ---- */
#telaClientes[hidden], #formLogin[hidden] { display: none !important; }
dialog:has(#telaClientes:not([hidden])) { width: min(94vw, 520px); }
.topo-clientes { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.lista-clientes {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: min(60vh, 480px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.lista-clientes button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  text-align: left;
  border-radius: 10px;
}
.lista-clientes button:disabled { opacity: 0.55; }
.lista-clientes .nome-cliente { font-weight: 600; font-size: 15px; }
.lista-clientes .detalhe { color: var(--txt-dim); font-size: 12px; margin-top: 2px; }
.lista-clientes .textos { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.lista-clientes .pill { flex: 0 0 auto; }
.pill-espera { background: #b7791f; }

/* Voltar para a lista de clientes: sempre visivel, no inicio da barra. */
button.voltar { font-weight: 600; }

/* A pagina em si nunca rola (so a imagem dentro do #palco). Sem isso, no
   celular o teclado virtual empurra a pagina e a barra de cima some. */
html { overflow: hidden; overscroll-behavior: none; }
body {
  position: fixed;
  inset: 0;
  height: auto;
  overscroll-behavior: none;
}
#barra { flex: 0 0 auto; position: relative; z-index: 5; }
#palco { min-height: 0; }

/* Anel do cursor (modo touchpad no celular). */
#viewport { position: relative; }
#cursorLocal {
  position: absolute;
  width: 22px;
  height: 22px;
  margin: -11px 0 0 -11px;
  border: 2px solid var(--acc);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.8), 0 0 6px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  z-index: 2;
}
#cursorLocal[hidden] { display: none !important; }

.lista-clientes .detalhe.desatualizado { color: #e0a33a; }

/* ---- Arquivos (estilo VNC) ---- */
dialog.arquivos {
  width: min(96vw, 1100px);
  height: min(92vh, 760px);
  padding: 14px;
}
dialog.arquivos[open] { display: flex; flex-direction: column; gap: 10px; }
.arq-topo { display: flex; align-items: center; gap: 10px; }
.arq-topo h2 { margin: 0; font-size: 17px; }
.arq-corpo { flex: 1; min-height: 0; display: flex; gap: 10px; }
.arq-lado {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  border: 1px solid var(--borda);
  border-radius: 10px;
  padding: 8px;
  background: var(--bg);
}
.arq-cab { display: flex; align-items: center; gap: 6px; }
.arq-cab button { padding: 4px 10px; }
.arq-caminho {
  font-size: 12px;
  color: var(--txt-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  direction: rtl;           /* corta o inicio do caminho, mostra a pasta atual */
  text-align: left;
  min-height: 1.4em;
}
.arq-lista { flex: 1; min-height: 0; overflow: auto; border-radius: 6px; }
.arq-lista[hidden], .arq-alt[hidden] { display: none !important; }
.arq-lista.alvo { outline: 2px dashed var(--acc); outline-offset: -4px; }
.arq-lista table { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
.arq-lista th:nth-child(2) { width: 92px; }
.arq-lista th:nth-child(3) { width: 128px; }
.arq-lista th {
  position: sticky;
  top: 0;
  background: var(--bg2);
  text-align: left;
  font-weight: 600;
  color: var(--txt-dim);
  font-size: 12px;
  padding: 5px 8px;
}
.arq-lista td { padding: 4px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: default; user-select: none; }
.arq-lista td:not(:first-child) { color: var(--txt-dim); font-size: 12px; }
.arq-lista tr:hover td { background: #1b2030; }
/* "marcada" e nao "sel": .sel ja e dos rotulos da barra (display: flex). */
.arq-lista tr.marcada td { background: #1d3157; color: var(--txt); }
.arq-vazio { padding: 12px 8px; margin: 0; }
.arq-lista .erro { color: var(--danger); }
button.arq-grande { margin: 24px auto; display: block; padding: 10px 16px; }
.arq-alt { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 8px; padding: 16px; }
.arq-alt p { margin: 0; max-width: 320px; }
.arq-meio { display: flex; flex-direction: column; justify-content: center; gap: 10px; }
.arq-meio button { white-space: nowrap; }
.arq-acoes { display: flex; gap: 6px; flex-wrap: wrap; }
.arq-acoes button { padding: 5px 10px; }
.arq-progresso { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.arq-progresso span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.arq-progresso progress { width: min(280px, 30vw); accent-color: var(--acc); }
.arq-historico {
  list-style: none;
  margin: 0;
  padding: 6px 8px;
  height: 84px;
  overflow-y: auto;
  font-size: 12px;
  color: var(--txt-dim);
  background: var(--bg);
  border: 1px solid var(--borda);
  border-radius: 8px;
}
.arq-historico li.erro { color: var(--danger); }

/* Tela estreita: um lado em cima do outro, botoes do meio na horizontal. */
@media (max-width: 760px) {
  dialog.arquivos { width: 100vw; height: 100dvh; max-width: none; max-height: none; border-radius: 0; padding: 10px; }
  .arq-corpo { flex-direction: column; }
  .arq-meio { flex-direction: row; justify-content: center; }
  .arq-lista td:nth-child(3), .arq-lista th:nth-child(3) { display: none; }
}
