/* ============================================================
   Painel do Servidor — Design System v2
   Tokens, componentes e utilitários padronizados.
   Documentação: frontend/COMPONENTS.md
   ============================================================ */

/* ---------- 1. Design tokens ---------- */
:root {
  /* cores */
  --bg: #0b0f14;
  --bg2: #10151c;
  --card: #161d26;
  --card-hover: #1a222e;
  --border: #232c38;
  --border-soft: rgba(45, 51, 59, 0.5);
  --text: #e8eef5;
  --muted: #8b98a9;
  --accent: #3fb950;
  --accent2: #58a6ff;
  --warn: #d29922;
  --danger: #f85149;

  /* espaçamento (escala 4px) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  /* raios */
  --radius: 8px;
  --radius-lg: 12px;
  --radius-full: 20px;

  /* tipografia */
  --font-xs: 12px;
  --font-sm: 13px;
  --font-md: 14px;
  --font-lg: 20px;
  --font-xl: 28px;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;

  /* sombras / transições */
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  --shadow-sm: 0 1px 4px rgba(0, 0, 0, 0.3);
  --transition: 0.15s ease;

  /* layout */
  --sidebar-w: 250px;
  --topbar-h: 56px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { scrollbar-color: var(--border) transparent; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background:
    radial-gradient(1100px 500px at 85% -10%, rgba(88, 166, 255, 0.05), transparent 60%),
    var(--bg);
  color: var(--text);
  font-size: var(--font-md);
  line-height: 1.5;
}

.hidden { display: none !important; }

/* ---------- 2. Utilitários ---------- */
.mt-1 { margin-top: var(--space-1); }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: var(--space-1); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }

.w-xs { width: 90px; }
.w-sm { width: 120px; }
.w-md { width: 160px; }
.w-full { width: 100%; }

.flex { display: flex; align-items: center; gap: var(--space-2); }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.grow { flex: 1; }

.text-ok { color: var(--accent); }
.text-err { color: var(--danger); }
.text-warn { color: var(--warn); }
.text-muted { color: var(--muted); }
.mono { font-family: var(--mono); }

/* ---------- 3. Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(900px 450px at 20% -10%, rgba(88, 166, 255, 0.14), transparent 55%),
    radial-gradient(800px 500px at 90% 110%, rgba(63, 185, 80, 0.10), transparent 55%),
    var(--bg);
}
.login-card {
  width: 380px;
  background: linear-gradient(180deg, var(--card-hover), var(--card));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-8);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  box-shadow: var(--shadow);
}
.login-logo {
  width: 64px; height: 64px;
  margin: 0 auto;
  display: flex; align-items: center; justify-content: center;
  font-size: 30px;
  background: rgba(88, 166, 255, 0.12);
  border: 1px solid rgba(88, 166, 255, 0.35);
  border-radius: var(--radius-lg);
}
.login-card h1 { font-size: var(--font-lg); text-align: center; }
.login-sub { color: var(--muted); text-align: center; font-size: var(--font-sm); }
.login-card input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 11px var(--space-3);
  font-size: var(--font-md);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.login-card input:focus {
  outline: none;
  border-color: var(--accent2);
  box-shadow: 0 0 0 3px rgba(88, 166, 255, 0.18);
}
.login-card button {
  background: linear-gradient(180deg, #6cb4ff, var(--accent2));
  color: #04141f;
  border: 0;
  border-radius: var(--radius);
  padding: 12px;
  font-weight: 700;
  font-size: var(--font-md);
  cursor: pointer;
  transition: filter var(--transition), transform var(--transition);
}
.login-card button:hover { filter: brightness(1.12); }
.login-card button:active { transform: scale(0.99); }
.error-msg { color: var(--danger); font-size: var(--font-sm); text-align: center; }
.error-msg.ok { color: var(--accent); }

/* ---------- 4. Layout ---------- */
.app { display: flex; min-height: 100vh; }

/* sidebar */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  flex-shrink: 0;
  z-index: 50;
}
.brand {
  display: flex; gap: 10px; align-items: center;
  padding: 18px var(--space-4);
  border-bottom: 1px solid var(--border);
}
.brand-icon {
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  background: rgba(88, 166, 255, 0.12);
  border: 1px solid rgba(88, 166, 255, 0.3);
  border-radius: 10px;
}
.brand-text strong { display: block; font-size: 15px; line-height: 1.2; }
.brand-text small { display: block; color: var(--muted); font-size: 11px; margin-top: 2px; }

nav { flex: 1; padding: var(--space-3) var(--space-2); overflow-y: auto; }
.nav-group {
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  padding: var(--space-3) var(--space-3) var(--space-1);
}
.nav-item {
  position: relative;
  width: 100%;
  background: none; border: 0; color: var(--muted);
  text-align: left; padding: 9px var(--space-3);
  border-radius: var(--radius);
  font-size: var(--font-md); cursor: pointer;
  display: flex; align-items: center; gap: 10px;
  transition: background var(--transition), color var(--transition);
}
.nav-item:hover { background: var(--card); color: var(--text); }
.nav-item.active {
  background: linear-gradient(90deg, rgba(88, 166, 255, 0.16), rgba(88, 166, 255, 0.05));
  color: var(--accent2); font-weight: 600;
}
.nav-item.active::before {
  content: "";
  position: absolute; left: 0; top: 20%; bottom: 20%;
  width: 3px; border-radius: 3px;
  background: var(--accent2);
}
.nav-item span { flex: 1; }

.sidebar-foot {
  padding: var(--space-3);
  border-top: 1px solid var(--border);
  display: flex; flex-direction: column; gap: var(--space-2);
}
.foot-status { color: var(--muted); font-size: var(--font-xs); text-align: center; }
.foot-status.ok { color: var(--accent); }
.foot-status.bad { color: var(--danger); }

/* área principal */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 40;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: var(--space-3);
  padding: 0 var(--space-6);
  background: rgba(11, 15, 20, 0.82);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 17px; font-weight: 700; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; gap: var(--space-2); }
.status-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: var(--font-xs);
  color: var(--muted);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 4px 12px;
  white-space: nowrap;
}
.status-chip .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status-chip.ok { color: var(--accent); border-color: rgba(63, 185, 80, 0.35); }
.status-chip.ok .dot { background: var(--accent); box-shadow: 0 0 6px rgba(63, 185, 80, 0.8); }
.status-chip.bad { color: var(--danger); border-color: rgba(248, 81, 73, 0.35); }
.status-chip.bad .dot { background: var(--danger); }
.user-chip { color: var(--text); }

.icon-btn {
  background: none; border: 1px solid transparent; color: var(--muted);
  border-radius: var(--radius); padding: 6px 10px;
  font-size: 18px; cursor: pointer; line-height: 1;
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--card); color: var(--text); }
@media (min-width: 901px) { #menu-toggle { display: none; } }

.content { flex: 1; padding: var(--space-6) 28px; max-width: 1200px; width: 100%; }
.view { display: none; animation: fadeIn 0.18s ease; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.view-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 18px; gap: var(--space-3); flex-wrap: wrap;
}
.view-head h2 { font-size: var(--font-lg); }
.updated { color: var(--muted); font-size: var(--font-xs); }

/* mobile: sidebar off-canvas */
@media (max-width: 900px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
  }
  .sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-open::after {
    content: ""; position: fixed; inset: 0; z-index: 45;
    background: rgba(0, 0, 0, 0.55);
  }
  .main { width: 100%; }
  .content { padding: var(--space-4) var(--space-4); }
  .topbar { padding: 0 var(--space-4); }
}

/* ---------- 5. Componente: Card ---------- */
.grid { display: grid; gap: var(--space-3); margin-bottom: 18px; }
.cards-4 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.cols-2 { grid-template-columns: 1fr 1fr; }
@media (max-width: 900px) { .cols-2 { grid-template-columns: 1fr; } }
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: var(--space-4);
  margin-bottom: var(--space-3);
  box-shadow: var(--shadow-sm);
  transition: border-color var(--transition), background var(--transition);
}
.card:hover { border-color: var(--border); background: var(--card); }
.card-title {
  color: var(--muted);
  font-size: var(--font-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
  font-weight: 700;
}
.big { font-size: var(--font-xl); font-weight: 700; font-variant-numeric: tabular-nums; }
.sub { color: var(--muted); font-size: var(--font-xs); }
.spark { width: 100%; height: 44px; margin-top: var(--space-2); display: block; }

/* cartões de estatística (visão geral) */
.stat-card { border-top: 2px solid var(--acc, var(--accent2)); position: relative; }
.stat-card[data-acc="accent2"] { --acc: var(--accent2); }
.stat-card[data-acc="accent"] { --acc: var(--accent); }
.stat-card[data-acc="warn"] { --acc: var(--warn); }
.stat-card[data-acc="danger"] { --acc: var(--danger); }
.stat-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.stat-head .card-title { margin-bottom: 0; }
.stat-icon { font-size: 16px; }
.stat-card .big { color: var(--acc); }

/* ---------- 6. Componente: Tabela ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th {
  text-align: left; color: var(--muted); font-size: var(--font-xs);
  text-transform: uppercase; letter-spacing: 0.04em;
  padding: 8px var(--space-2); border-bottom: 1px solid var(--border);
  font-weight: 700;
}
.table td { padding: 8px var(--space-2); border-bottom: 1px solid var(--border-soft); font-variant-numeric: tabular-nums; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr { transition: background var(--transition); }
.table tbody tr:hover { background: rgba(255, 255, 255, 0.03); }

/* ---------- 7. Componente: Badge ---------- */
.badge {
  display: inline-block; padding: 2px 10px; border-radius: var(--radius-full);
  font-size: var(--font-xs); font-weight: 600;
}
.badge.active, .badge.running { background: rgba(63, 185, 80, 0.15); color: var(--accent); }
.badge.failed, .badge.inactive { background: rgba(248, 81, 73, 0.15); color: var(--danger); }
.badge.activating, .badge.deactivating, .badge.reloading { background: rgba(210, 153, 34, 0.15); color: var(--warn); }

/* ---------- 8. Componente: Barra de progresso ---------- */
.bar {
  height: 8px; background: var(--bg); border-radius: var(--radius-full); overflow: hidden; flex: 1;
}
.bar > i {
  display: block; height: 100%;
  background: linear-gradient(90deg, var(--accent2), var(--accent));
  border-radius: var(--radius-full); transition: width 0.4s ease;
}
.bar.warn > i { background: linear-gradient(90deg, var(--warn), var(--danger)); }
.bar-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: var(--space-3); }
.bar-row { display: flex; align-items: center; gap: 10px; font-size: var(--font-xs); }
.bar-row .label { width: 120px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row .pct { width: 52px; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- 9. Componente: KV grid ---------- */
.kv-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.kv-grid > div { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px var(--space-3); }
.kv-grid span { display: block; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
.kv-grid b { font-size: 15px; }

/* ---------- 10. Componente: Botões / inputs ---------- */
.btn, .btn-ghost {
  background: linear-gradient(180deg, #6cb4ff, var(--accent2)); color: #04141f; border: 0;
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-3); font-size: var(--font-sm); font-weight: 600; cursor: pointer;
  transition: filter var(--transition), transform var(--transition);
  box-shadow: var(--shadow-sm);
}
.btn:hover { filter: brightness(1.1); }
.btn:active { transform: scale(0.98); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); font-weight: 500; box-shadow: none; }
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }
.btn.danger { background: linear-gradient(180deg, #ff7b72, var(--danger)); color: #fff; }
.btn.btn-sm, .btn-ghost.btn-sm { padding: 4px 10px; font-size: var(--font-xs); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.input, .select {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: var(--space-2) 10px; font-size: var(--font-sm);
  transition: border-color var(--transition);
}
textarea.input {
  resize: vertical; min-height: 56px;
  font-family: var(--mono);
}
.input:focus, .select:focus { outline: none; border-color: var(--accent2); }
.head-actions { display: flex; gap: var(--space-2); align-items: center; }
.hint { color: var(--muted); font-size: var(--font-xs); margin-top: 10px; }
.hint code { background: var(--bg); padding: 1px 6px; border-radius: 4px; font-family: var(--mono); }

/* ---------- 11. Componente: Log view ---------- */
.log-view {
  font-family: var(--mono); font-size: var(--font-xs); line-height: 1.55;
  background: #010409; border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--space-3); max-height: 560px; overflow: auto; white-space: pre-wrap;
  word-break: break-all;
}
.log-view::-webkit-scrollbar { width: 10px; }
.log-view::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
.log-actions { display: flex; gap: var(--space-2); align-items: center; margin-top: var(--space-2); }
.log-actions .hint { margin: 0; flex: 1; }

/* ---------- 12. Componente: Tabs ---------- */
.tabs { display: flex; gap: 6px; margin-bottom: var(--space-3); flex-wrap: wrap; }
.tab {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  padding: 7px var(--space-3); border-radius: var(--radius); cursor: pointer; font-size: var(--font-sm);
  transition: all var(--transition);
}
.tab:hover { color: var(--text); }
.tab.active { background: rgba(88, 166, 255, 0.12); color: var(--accent2); border-color: var(--accent2); }

/* ---------- 13. Componente: Modal (formulários adicionar/editar) ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0, 0, 0, 0.62);
  display: flex; align-items: center; justify-content: center;
  padding: var(--space-4);
  animation: fadeIn 0.15s ease;
}
.modal {
  width: 100%; max-width: 480px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  animation: modalIn 0.18s ease;
}
@keyframes modalIn {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-4);
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 16px; }
.modal-body {
  padding: var(--space-4);
  display: flex; flex-direction: column; gap: var(--space-3);
}
.modal-error { padding: 0 var(--space-4); margin: 0; }
.modal-foot {
  display: flex; justify-content: flex-end; gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border);
}
.modal-field { display: flex; flex-direction: column; gap: 6px; }
.modal-field span {
  color: var(--muted); font-size: 11px; text-transform: uppercase;
  letter-spacing: 0.04em; font-weight: 700;
}

/* ---------- 14. Barra de offline (PWA) ---------- */
.offline-bar {
  position: fixed; top: 0; left: 0; right: 0;
  z-index: 1000;
  background: rgba(210, 153, 34, 0.92);
  color: #1a1203;
  font-size: var(--font-sm);
  font-weight: 600;
  text-align: center;
  padding: var(--space-2) var(--space-3);
}

/* ---------- 15. Gateway / tags ---------- */
.gateway-box {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--space-3); font-size: var(--font-sm);
}
.gateway-row { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; }
.tag-list { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.tag {
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-full);
  padding: 3px var(--space-3); font-family: var(--mono); font-size: var(--font-xs); color: var(--accent2);
}
a { color: var(--accent2); }

/* helper invisível para fallback de copiar (textarea offscreen) */
.copy-helper {
  position: fixed; top: 0; left: 0;
  opacity: 0; pointer-events: none;
}

/* ---------- 16. Templates de stack (docker compose) ---------- */
.tpl-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: var(--space-3);
}
.tpl-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-3);
  display: flex; flex-direction: column; gap: var(--space-2);
}
.tpl-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); }
.tpl-head strong { font-size: 15px; }
.tpl-card details summary { cursor: pointer; color: var(--accent2); font-size: var(--font-xs); }
.tpl-card .log-view { max-height: 200px; margin: 0; }
.tag.warn { color: var(--warn); border-color: rgba(210, 153, 34, 0.4); }
