/* Padrão visual do sistema — dark mode é o padrão e único tema.
   Tokens no estilo shadcn/ui (mesmos nomes de variável, mesma escala de raio e espaçamento),
   escritos em CSS puro: shadcn/ui é React+Tailwind e o projeto é PHP puro com JS vanilla,
   então adotamos a linguagem visual, não a biblioteca. */

:root {
  color-scheme: dark;

  --background: #0a0a0b;
  --foreground: #fafafa;
  --card: #131316;
  --card-foreground: #fafafa;
  --muted: #1c1c20;
  --muted-foreground: #a1a1aa;
  --border: #27272a;
  --input: #27272a;
  --ring: #682a2c;

  --primary: #682a2c;
  --primary-hover: #7d3336;
  --primary-foreground: #ffffff;

  --destructive: #b91c1c;
  --destructive-foreground: #ffffff;

  /* faixas de utilização de uplink (nível 1 e 2 da topologia) */
  --util-ok: #22c55e;
  --util-atencao: #eab308;
  --util-critico: #ef4444;
  --pipe-verde: #22c55e;
  --pipe-amarelo: #eab308;
  --pipe-laranja: #f97316;
  --pipe-vermelho: #ef4444;
  --pipe-desconhecido: #71717a;

  --radius: 0.5rem;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 2rem;
  background: var(--background);
  color: var(--foreground);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 0.95rem;
  line-height: 1.5;
}

h1, h2, h3 { font-weight: 600; letter-spacing: -0.01em; margin: 0 0 1rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.25rem; }
a { color: var(--foreground); text-decoration: underline; text-underline-offset: 3px; }
a:hover { color: var(--muted-foreground); }

.container { max-width: 72rem; margin: 0 auto; }
.muted { color: var(--muted-foreground); }
.stack > * + * { margin-top: 1rem; }
.row { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.nav-bar { margin-bottom: 0.5rem; padding-bottom: 0.75rem; border-bottom: 1px solid var(--border); }
.menu-app {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: 1rem;
}
a.menu-card {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s, background 0.15s;
}
a.menu-card:hover {
  border-color: var(--ring);
  background: var(--muted);
  color: inherit;
}

.card {
  background: var(--card);
  color: var(--card-foreground);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  height: 2.25rem;
  padding: 0 1rem;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: var(--primary);
  color: var(--primary-foreground);
  font: inherit;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s;
}
.btn:hover { background: var(--primary-hover); color: var(--primary-foreground); }
.btn:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.btn-outline {
  background: transparent;
  color: var(--foreground);
  border-color: var(--border);
}
.btn-outline:hover { background: var(--muted); color: var(--foreground); }

.btn-destructive { background: var(--destructive); color: var(--destructive-foreground); }
.btn-destructive:hover { background: #a01818; color: var(--destructive-foreground); }

label { display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 0.375rem; }

input[type=text], input[type=file], input[type=number], select, textarea {
  width: 100%;
  height: 2.25rem;
  padding: 0 0.75rem;
  background: var(--background);
  color: var(--foreground);
  border: 1px solid var(--input);
  border-radius: var(--radius);
  font: inherit;
}
input[type=file] { padding: 0.35rem 0.75rem; height: auto; }
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 1px;
  border-color: var(--ring);
}
.field + .field { margin-top: 1rem; }

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 0.75rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted-foreground); font-weight: 500; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }
tr:last-child td { border-bottom: none; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 9999px;
  font-size: 0.75rem;
  color: var(--muted-foreground);
}

.alert {
  border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  border-radius: var(--radius);
  background: var(--muted);
  padding: 0.75rem 1rem;
}
.alert-erro { border-left-color: var(--destructive); }

/* ---- editor de posicionamento (fase 6) ---- */

.editor { display: grid; grid-template-columns: 16rem 1fr; gap: 1rem; align-items: start; }
@media (max-width: 48rem) { .editor { grid-template-columns: 1fr; } }

.editor-lista h3 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted-foreground); font-weight: 500; margin: 1rem 0 0.5rem; }
.editor-lista h3:first-child { margin-top: 0; }

.switch-item {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.375rem;
  font-size: 0.875rem;
}
/* sem isto o Chrome inicia o arraste NATIVO do texto, dispara pointercancel e o gesto
   é perdido no meio — o switch nunca chega à planta */
.switch-item.arrastavel, .switch-marcador, .switch-nome {
  user-select: none;
  -webkit-user-drag: none;
  touch-action: none;
}
.switch-item.arrastavel { cursor: grab; background: var(--muted); }
.switch-item.arrastavel:hover { border-color: var(--ring); }

.editor-canvas { position: relative; touch-action: none; }
.editor-canvas img { display: block; width: 100%; height: auto; border-radius: var(--radius); }
.editor-canvas svg { position: absolute; inset: 0; pointer-events: none; }
.editor-canvas svg .enlace { stroke: var(--muted-foreground); stroke-width: 2; opacity: 0.8; }

.switch-marcador {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  gap: 0.375rem;
  cursor: grab;
  white-space: nowrap;
}
.switch-ponto {
  width: 0.875rem; height: 0.875rem;
  background: var(--primary);
  border: 2px solid var(--foreground);
  border-radius: 50%;
  flex: none;
}
/* switch com enlace saindo deste andar/prédio: anel externo (indicador de saída) */
.switch-marcador.tem-saida .switch-ponto { box-shadow: 0 0 0 3px var(--util-atencao); }
.switch-nome {
  font-size: 0.75rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.05rem 0.35rem;
}

body.arrastando { cursor: grabbing; user-select: none; }
#status-salvamento { margin-top: 0.75rem; font-size: 0.875rem; }
#status-salvamento.erro { color: var(--destructive); }

/* ---- dashboard (fase 8) ---- */

/* canvas de topologia: mesmo modelo do editor, mas somente leitura e com enlaces clicáveis */
.topo-card { padding: 1rem; }
.topo-canvas { position: relative; touch-action: pan-x pan-y pinch-zoom; }
.topo-canvas img { display: block; width: 100%; height: auto; border-radius: var(--radius); }
.topo-canvas svg { position: absolute; inset: 0; pointer-events: none; }
.topo-canvas svg a { pointer-events: stroke; cursor: pointer; }
.topo-legenda { margin: 0.75rem 0 0; font-size: 0.8rem; }

.topo-canvas .enlace-hit { stroke: transparent; stroke-width: 44; }
.topo-canvas svg a:focus-visible .enlace-hit { stroke: var(--ring); stroke-opacity: 0.25; }
.topo-canvas .pipe-track { stroke: var(--muted-foreground); stroke-width: 5; opacity: 0.45; pointer-events: none; }
.topo-canvas .pipe-fill { fill: none; stroke-width: 5; pointer-events: none; }
.topo-canvas .pipe-verde { stroke: var(--pipe-verde); fill: var(--pipe-verde); }
.topo-canvas .pipe-amarelo { stroke: var(--pipe-amarelo); fill: var(--pipe-amarelo); }
.topo-canvas .pipe-laranja { stroke: var(--pipe-laranja); fill: var(--pipe-laranja); }
.topo-canvas .pipe-vermelho { stroke: var(--pipe-vermelho); fill: var(--pipe-vermelho); }
.topo-canvas .pipe-desconhecido { stroke: transparent; fill: var(--pipe-desconhecido); }
.topo-canvas .pipe-label {
  font-size: 0.7rem;
  font-weight: 700;
  pointer-events: none;
  paint-order: stroke;
  stroke: var(--card);
  stroke-width: 3px;
  stroke-linejoin: round;
}
.topo-canvas .enlace-alerta-taxa {
  stroke: var(--util-critico);
  stroke-width: 2;
  stroke-dasharray: 6 5;
  pointer-events: none;
}
.topo-canvas .enlace-alerta-contato {
  fill: var(--util-critico);
  font-size: 1.1rem;
  font-weight: 700;
  text-anchor: middle;
  pointer-events: none;
  paint-order: stroke;
  stroke: var(--card);
  stroke-width: 3px;
}

/* marcadores de prédio/switch viram links nas telas de leitura */
a.switch-marcador { text-decoration: none; }
a.switch-marcador:hover .switch-nome { border-color: var(--ring); }
a.switch-marcador:focus-visible { outline: none; }
a.switch-marcador:focus-visible .switch-nome {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
  border-color: var(--ring);
}

/* contagem de saídas anônimas no marcador do prédio (nível 1) */
.badge-saida { color: var(--util-atencao); font-size: 0.7rem; }

/* marcador de saída: para onde vai a ponta que não cabe nesta tela */
.marcador-saida {
  position: absolute;
  transform: translateY(-50%);
  font-size: 0.75rem;
  background: var(--muted);
  color: var(--muted-foreground);
  border: 1px solid var(--border);
  border-radius: 9999px;
  padding: 0.1rem 0.6rem;
  white-space: nowrap;
  text-decoration: none;
}
a.marcador-saida:hover { border-color: var(--ring); color: var(--foreground); }
a.marcador-saida:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }
.marcador-saida.alerta-taxa-erro { border-style: dashed; border-color: var(--util-critico); }
.marcador-saida.alerta-sem-contato { color: var(--util-critico); }

/* cores de utilização também em badges e textos (tabelas) */
.badge.util-ok { color: var(--util-ok); border-color: var(--util-ok); }
.badge.util-atencao { color: var(--util-atencao); border-color: var(--util-atencao); }
.badge.util-critico { color: var(--util-critico); border-color: var(--util-critico); }

/* ---- alertas visuais (fase 10) ---- */

.badge.alerta-sem_contato,
.badge.alerta-utilizacao_critica { color: var(--util-critico); border-color: var(--util-critico); }
.badge.alerta-taxa_erro { color: var(--util-critico); border-color: var(--util-critico); border-style: dashed; }

.alerta-lista { display: grid; gap: 0.75rem; }
.alerta-item {
  display: grid;
  grid-template-columns: auto minmax(12rem, 1fr) auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
}
.alerta-item:hover,
.alerta-item:focus-visible { border-color: var(--ring); background: var(--muted); color: var(--foreground); }
.alerta-item:focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }

@media (max-width: 48rem) {
  .alerta-item { grid-template-columns: 1fr; }
  .alerta-item .badge { justify-self: start; }
}

/* tabelas ordenáveis (tabela.js) */
.ordenavel-th { cursor: pointer; user-select: none; }
.ordenavel-th:hover { color: var(--foreground); }
.ordenavel-th[data-dir="asc"]::after { content: " ↑"; }
.ordenavel-th[data-dir="desc"]::after { content: " ↓"; }

/* gráfico de histórico (historico.js) */
.grafico-svg { width: 100%; height: auto; display: block; }
.grafico-texto { fill: var(--muted-foreground); font-size: 12px; }
.grafico-grade { stroke: var(--border); stroke-width: 1; }
.grafico-linha { fill: none; stroke-width: 2; }
.grafico-linha.in { stroke: var(--util-ok); }
.grafico-linha.out { stroke: #60a5fa; }
.grafico-erros { fill: var(--destructive); opacity: 0.85; }
.grafico-amostra { display: inline-block; width: 1rem; height: 3px; vertical-align: middle; margin: 0 0.25rem 0 0.75rem; }
.grafico-amostra.in { background: var(--util-ok); }
.grafico-amostra.out { background: #60a5fa; }
