:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --line: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-h: #2563eb;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  padding: 18px 28px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

header h1 { margin: 0 0 12px; font-size: 22px; }

nav { display: flex; gap: 8px; }

.tab {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

main { padding: 24px 28px; max-width: 1200px; margin: 0 auto; }

.panel { display: none; }
.panel.active { display: block; }

.grid {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 20px;
  align-items: start;
}
@media (max-width: 820px) { .grid { grid-template-columns: 1fr; } }
/* Orçamentos: coluna única (largura total) para caber a tabela de itens. */
.quotes-stack { grid-template-columns: 1fr; }
.quotes-stack .form-card, .quotes-stack .list-card { max-width: none; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px;
}
.card h2 { margin: 0 0 14px; font-size: 16px; }

form label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
}
form input, form select, form textarea {
  width: 100%;
  margin-top: 4px;
  padding: 9px 10px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
}
form textarea { resize: vertical; }

.actions { display: flex; gap: 8px; margin-top: 4px; }

button.primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 9px 18px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
button.primary:hover { background: var(--primary-h); }

button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
button.ghost:hover { color: var(--text); }

.hidden { display: none !important; }

.msg { font-size: 13px; margin: 10px 0 0; min-height: 18px; }
.msg.error { color: var(--red); }
.msg.success { color: var(--green); }

.list-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.list-head h2 { margin: 0; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; }
tbody tr:hover { background: var(--panel-2); }

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.b-green { background: rgba(34,197,94,.15); color: var(--green); }
.b-amber { background: rgba(245,158,11,.15); color: var(--amber); }
.b-red { background: rgba(239,68,68,.15); color: var(--red); }
.b-blue { background: rgba(59,130,246,.15); color: var(--primary); }
.b-gray { background: rgba(148,163,184,.15); color: var(--muted); }

.row-actions { display: flex; gap: 6px; }
.icon-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 12px;
}
.icon-btn:hover { color: var(--text); }
.icon-btn.danger:hover { color: var(--red); border-color: var(--red); }

.empty { color: var(--muted); font-style: italic; padding: 16px 8px; }

/* ===== Dashboard ===== */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
}
.stat-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px 18px;
}
.stat-card.danger { border-color: var(--red); }
.stat-value { font-size: 30px; font-weight: 700; line-height: 1; }
.stat-card.danger .stat-value { color: var(--red); }
.stat-label { color: var(--text); margin-top: 6px; font-size: 13px; }
.stat-hint { color: var(--muted); font-size: 11px; margin-top: 2px; }

/* ===== Grid das Configurações (cards lado a lado) ===== */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 20px;
  align-items: start;
}
.settings-grid .card { max-width: none; }
.settings-wide { grid-column: 1 / -1; }

.chart-title { margin: 0 0 12px; font-size: 14px; color: var(--muted); }
.bar-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; font-size: 13px; }
.bar-label { width: 120px; color: var(--muted); flex-shrink: 0; }
.bar-track { flex: 1; background: var(--panel-2); border-radius: 6px; height: 14px; overflow: hidden; }
.bar-fill { display: block; height: 100%; border-radius: 6px; min-width: 2px; }
.bar-value { width: 36px; text-align: right; }
.bar-blue { background: var(--primary); }
.bar-green { background: var(--green); }
.bar-amber { background: var(--amber); }
.bar-red { background: var(--red); }
.bar-gray { background: var(--muted); }

/* ===== Login ===== */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 0%, #1e293b, var(--bg));
  z-index: 50;
}
.overlay.hidden { display: none; }
.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 32px;
  width: 340px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.login-card h1 { margin: 0; font-size: 24px; text-align: center; }
.login-sub { color: var(--muted); text-align: center; margin: 6px 0 22px; font-size: 14px; }
.login-hint { color: var(--muted); font-size: 12px; text-align: center; margin: 12px 0 0; }

/* ===== Seletor segmentado (login local/dominio) ===== */
.seg {
  display: flex;
  gap: 4px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px;
  margin-bottom: 16px;
}
.seg-btn {
  flex: 1;
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}
.seg-btn.active { background: var(--primary); color: #fff; }

.field-hint { display: block; font-size: 11px; color: var(--muted); margin-top: 4px; }

/* ===== Switch row (config AD) ===== */
.switch-row {
  display: flex !important;
  align-items: center;
  gap: 8px;
  color: var(--text) !important;
  margin-bottom: 16px;
}
.switch-row input { width: auto !important; margin: 0 !important; }

.tls-box {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px 4px;
  margin-bottom: 16px;
}
.tls-box legend { color: var(--muted); font-size: 12px; padding: 0 6px; }
.tls-box .field-hint { margin: -4px 0 12px; }

/* ===== Modal de detalhe ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 60;
  overflow-y: auto;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  width: 100%;
  max-width: 760px;
}
.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.modal-body { padding: 16px 20px 24px; }
.modal-body h3 { font-size: 14px; color: var(--muted); margin: 18px 0 8px; }
.modal-body h3:first-child { margin-top: 0; }
table.kv th {
  text-align: left;
  color: var(--muted);
  font-weight: 500;
  width: 200px;
  vertical-align: top;
}
table.kv td { word-break: break-word; }

.comment {
  border-left: 2px solid var(--primary);
  padding: 6px 10px;
  margin-bottom: 8px;
  background: var(--panel-2);
  border-radius: 0 6px 6px 0;
  font-size: 13px;
}
.comment-head { color: var(--muted); font-size: 11px; margin-bottom: 2px; }

.attach-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 4px 0; font-size: 13px; }
.attach-row a { color: var(--primary); }

.assist-box {
  background: rgba(59,130,246,.08);
  border: 1px solid var(--primary);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 13px;
  line-height: 1.5;
}
.assist-box.hidden { display: none; }
.assist-head { color: var(--primary); font-weight: 600; margin-bottom: 4px; }

.quote-items { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
.quote-items th { text-align: left; font-size: 11px; color: var(--muted); padding: 2px 4px; }
.quote-items td { padding: 2px 4px; overflow: hidden; }
.quote-items input, .quote-items select { width: 100%; box-sizing: border-box; padding: 4px 6px; min-width: 0; }
.quote-items th:nth-child(1), .quote-items td:nth-child(1) { width: 28%; }
.quote-items th:nth-child(2), .quote-items td:nth-child(2) { width: 15%; }
.quote-items th:nth-child(3), .quote-items td:nth-child(3) { width: 9%; }
.quote-items th:nth-child(4), .quote-items td:nth-child(4) { width: 16%; }
.quote-items th:nth-child(5), .quote-items td:nth-child(5) { width: 18%; }
.quote-items th:nth-child(6), .quote-items td:nth-child(6) { width: 14%; text-align: right; }
.quote-items .icon-btn { padding: 2px 5px; }

.online-wrap { position: relative; display: inline-block; }
.online-panel {
  position: absolute; right: 0; top: 110%; z-index: 50;
  min-width: 220px; max-height: 320px; overflow-y: auto;
  background: var(--card, #fff); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.15); padding: 8px;
}
.online-panel .online-row { display: flex; justify-content: space-between; gap: 10px; padding: 5px 6px; border-radius: 6px; font-size: 13px; }
.online-panel .online-role { color: var(--muted); font-size: 11px; }

.kb-item { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; }
.kb-item > summary { cursor: pointer; font-weight: 600; }
.kb-body { margin-top: 8px; font-size: 13px; line-height: 1.5; color: var(--text); white-space: pre-wrap; }

/* ===== Inventario completo (arvore navegavel) ===== */
.inv-full { margin-top: 18px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; }
.inv-full > summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--text); }
.inv-section { margin: 6px 0 6px 8px; }
.inv-section > summary { cursor: pointer; color: var(--primary); font-size: 13px; padding: 2px 0; }
.inv-section .kv th { width: 200px; color: var(--muted); font-weight: 500; vertical-align: top; }
.inv-table { width: 100%; border-collapse: collapse; font-size: 12px; margin: 4px 0; }
.inv-table th, .inv-table td { border: 1px solid var(--line); padding: 4px 6px; text-align: left; word-break: break-word; }
.inv-table th { color: var(--muted); background: var(--panel-2); }

/* ===== Revelacao de token de agente ===== */
.token-reveal {
  background: rgba(34,197,94,.1);
  border: 1px solid var(--green);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 12px;
  font-size: 13px;
}
.token-reveal.hidden { display: none; }
.token-value {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.token-value code {
  background: var(--panel-2);
  padding: 6px 10px;
  border-radius: 6px;
  word-break: break-all;
  flex: 1;
}

/* ===== Header / sessao ===== */
.header-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.header-top h1 { margin: 0; }
.session { display: flex; align-items: center; gap: 12px; font-size: 14px; }
#session-user { color: var(--muted); }
#session-user strong { color: var(--text); }
