:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #dfe2e7;
  --text: #16181d;
  --muted: #5d6470;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --ok: #2b8a3e;
  --warn: #c77700;
  --error: #d6336c;
  --unknown: #868e96;
  --ok-bg: #ebfbee;
  --warn-bg: #fff4e0;
  --error-bg: #fff0f6;
  --code-bg: #0f1115;
  --code-text: #d8dee9;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.04);
  --nav-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1013;
    --surface: #171a1f;
    --surface-2: #20242b;
    --border: #2b3038;
    --text: #e9ecef;
    --muted: #9aa3ae;
    --accent: #748ffc;
    --accent-text: #0e1013;
    --ok: #51cf66;
    --warn: #fcc419;
    --error: #ff6b9a;
    --ok-bg: #12261a;
    --warn-bg: #2b2410;
    --error-bg: #2d1420;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0e1013;
  --surface: #171a1f;
  --surface-2: #20242b;
  --border: #2b3038;
  --text: #e9ecef;
  --muted: #9aa3ae;
  --accent: #748ffc;
  --accent-text: #0e1013;
  --ok: #51cf66;
  --warn: #fcc419;
  --error: #ff6b9a;
  --ok-bg: #12261a;
  --warn-bg: #2b2410;
  --error-bg: #2d1420;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.95rem; }
p { margin: 0 0 0.75rem; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.hidden { display: none !important; }

/* ---------- topo ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; display: flex; align-items: center; gap: 8px; color: var(--text); }
.topbar .spacer { flex: 1; }
.admin-pill {
  font-size: 0.75rem; font-weight: 600; padding: 4px 10px; border-radius: 999px;
  background: var(--warn-bg); color: var(--warn); border: 1px solid currentColor; cursor: pointer;
}
.admin-pill.off { background: transparent; color: var(--muted); border-color: var(--border); }

main { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* ---------- navegação inferior (celular) / lateral (desktop) ---------- */
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.bottomnav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 0.72rem; font-weight: 500;
}
.bottomnav a .ico { font-size: 1.3rem; line-height: 1; }
.bottomnav a.active { color: var(--accent); }

/* ---------- cards e grade ---------- */
.page-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .grow { flex: 1; min-width: 200px; }
.category { margin: 22px 0 10px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr)); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
a.card { display: block; color: inherit; transition: border-color 0.15s; }
a.card:hover { border-color: var(--accent); }
.card-head { display: flex; align-items: center; gap: 10px; }
.card-head .icon { font-size: 1.5rem; }
.card-head .title { flex: 1; min-width: 0; }
.card-head .title b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .summary { margin-top: 6px; font-size: 0.85rem; color: var(--muted); overflow-wrap: anywhere; }

.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--unknown); flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-bg); }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 3px var(--warn-bg); }
.dot.error { background: var(--error); box-shadow: 0 0 0 3px var(--error-bg); }
.dot.loading { animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: 0.3; } }

.metrics { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); margin-top: 12px; }
.metric { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; border-left: 3px solid transparent; min-width: 0; }
.metric.ok { border-left-color: var(--ok); }
.metric.warn { border-left-color: var(--warn); }
.metric.error { border-left-color: var(--error); }
.metric .label { font-size: 0.72rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metric .value { font-weight: 700; font-size: 1.05rem; overflow-wrap: anywhere; }
.metric .hint { font-size: 0.72rem; color: var(--muted); overflow-wrap: anywhere; }

.section { margin-top: 14px; }
.section > h2 { margin-bottom: 4px; }
.section > .help { font-size: 0.83rem; color: var(--muted); margin-bottom: 8px; }

/* ---------- tabelas: viram cartões no celular ---------- */
.table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.table th { text-align: left; font-weight: 600; color: var(--muted); font-size: 0.75rem; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.table td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; overflow-wrap: anywhere; }
.table tr:last-child td { border-bottom: 0; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
@media (max-width: 640px) {
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { padding: 10px 0; border-bottom: 1px solid var(--border); }
  .table td { border: 0; padding: 2px 0; }
  .table td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: 0.78rem; }
  .table td.actions-cell { padding-top: 8px; }
}

pre.mono, .mono {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 0.8rem;
}
pre.mono {
  background: var(--code-bg); color: var(--code-text); padding: 12px; border-radius: 10px;
  overflow: auto; max-height: 60vh; white-space: pre; margin: 0;
}

/* ---------- botões e formulários ---------- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 10px; padding: 9px 14px; font: inherit; font-weight: 600; font-size: 0.9rem;
  cursor: pointer; display: inline-flex; align-items: center; gap: 6px; min-height: 40px;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--error); border-color: color-mix(in srgb, var(--error) 50%, var(--border)); }
.btn.small { padding: 5px 10px; min-height: 32px; font-size: 0.8rem; }
.btn.block { width: 100%; justify-content: center; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.form { display: grid; gap: 14px; }
.field label { display: block; font-weight: 600; font-size: 0.88rem; margin-bottom: 4px; }
.field .help { font-size: 0.78rem; color: var(--muted); margin-top: 4px; }
input[type="text"], input[type="password"], input[type="number"], input[type="url"], textarea, select {
  width: 100%; font: inherit; font-size: 16px; /* 16px evita zoom no iPhone */
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
textarea { min-height: 96px; resize: vertical; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 14px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 0.88rem; }
.check input { width: 20px; height: 20px; }
.code-input { font-size: 1.6rem !important; letter-spacing: 0.4em; text-align: center; font-family: ui-monospace, Menlo, monospace !important; }

.guide {
  background: var(--surface-2); border-radius: 12px; padding: 12px 14px; font-size: 0.86rem;
  white-space: pre-wrap; border: 1px dashed var(--border);
}
details.guide-box summary { cursor: pointer; font-weight: 600; margin-bottom: 8px; }

.alert { border-radius: 10px; padding: 10px 12px; font-size: 0.88rem; }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.error { background: var(--error-bg); color: var(--error); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }

/* ---------- login ---------- */
.auth-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.auth { width: 100%; max-width: 380px; }
.auth h1 { margin-bottom: 4px; }
.auth .logo { font-size: 2.4rem; }
.qr { display: block; margin: 8px auto; border-radius: 12px; background: #fff; padding: 8px; width: 220px; height: 220px; }
.secret { font-family: ui-monospace, Menlo, monospace; word-break: break-all; background: var(--surface-2); padding: 8px; border-radius: 8px; font-size: 0.85rem; user-select: all; }
body.auth-mode { padding-bottom: 0; }

/* ---------- modal (bottom sheet no celular) ---------- */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgb(0 0 0 / 0.45); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--surface); width: 100%; max-width: 720px; max-height: 92dvh; overflow: auto;
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
}
@media (min-width: 720px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 18px; }
}
.sheet-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.sheet-head h2 { flex: 1; }

/* ---------- plugins (escolha) ---------- */
.plugin-card { display: flex; gap: 12px; align-items: flex-start; }
.plugin-card .icon { font-size: 1.8rem; }

/* ---------- auditoria ---------- */
.log-item { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 0.86rem; }
.log-item:last-child { border-bottom: 0; }

/* ---------- terminal ---------- */
.term-page { position: fixed; inset: 0; z-index: 40; background: #0f1115; display: flex; flex-direction: column; }
.term-bar { display: flex; align-items: center; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 10px 8px; color: #d8dee9; background: #171a1f; }
.term-bar b { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.term-host { flex: 1; min-height: 0; padding: 6px; }
.term-keys { display: flex; gap: 6px; padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); background: #171a1f; overflow-x: auto; }
.term-keys button {
  flex: none; background: #2b3038; color: #e9ecef; border: 0; border-radius: 8px;
  padding: 8px 12px; font: 600 0.85rem ui-monospace, Menlo, monospace; min-width: 44px; min-height: 38px;
}

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  z-index: 60; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 0.88rem;
  max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
}

.empty { text-align: center; padding: 40px 16px; }
.empty .big { font-size: 2.6rem; }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 900px) {
  body { padding-bottom: 0; padding-left: 88px; }
  .bottomnav { top: 0; right: auto; width: 88px; height: 100dvh; flex-direction: column; justify-content: flex-start; padding-top: 72px; border-top: 0; border-right: 1px solid var(--border); }
  .bottomnav a { flex: none; height: 72px; }
  .toast { bottom: 24px; }
}
