:root {
  --bg: #0e1116; --panel: #161b22; --panel2: #1c232d; --line: #2a3340;
  --text: #e6edf3; --muted: #8b98a8; --accent: #3fb68b; --accent-2: #2b8a67;
  --danger: #e5534b; --warn: #d29922; --radius: 12px;
  color-scheme: dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
a { color: var(--accent); }
.boot, .center { display: grid; place-items: center; min-height: 100vh; color: var(--muted); }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel2); border-radius: 8px; padding: .55rem .9rem; }
button:hover { border-color: #3a4656; }
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #06140e; font-weight: 600; }
button.primary:hover { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
button.ghost { background: transparent; }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.danger-ghost { background: transparent; color: var(--danger); border-color: #5a2a28; }
button.small { padding: .3rem .6rem; font-size: .85rem; }
button.icon { background: transparent; border: none; padding: .35rem .45rem; font-size: 1rem; line-height: 1; border-radius: 6px; }
button.icon:hover { background: var(--panel2); }
button.link { background: none; border: none; padding: 0; color: var(--accent); }

input, select, textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: .6rem .7rem;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; }

.field { display: grid; gap: .35rem; margin-bottom: .9rem; }
.field > span { font-size: .85rem; color: var(--muted); }
.field small, .small { font-size: .8rem; color: var(--muted); }
.check { display: flex; gap: .6rem; align-items: flex-start; font-size: .88rem; margin: .8rem 0; }
.check input { width: auto; margin-top: .2rem; }
.pw-wrap { display: flex; gap: .3rem; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: .3rem 0; font-size: .9rem; }
.notice { color: var(--warn); font-size: .9rem; }
meter { width: 100%; height: 8px; }

/* ---- Autenticación ---- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.6rem; }
.auth-card h1 { font-size: 1.3rem; margin: 1rem 0 .4rem; }
.auth-card .primary { width: 100%; margin-top: .4rem; padding: .75rem; }
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; letter-spacing: .3px; }
.steps { padding-left: 1.2rem; line-height: 1.55; }
.secret-key { background: var(--bg); border: 1px solid var(--line); padding: .15rem .45rem; border-radius: 6px; font-size: 1rem; letter-spacing: 1px; word-break: break-all; }

/* ---- Layout principal ---- */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; gap: .6rem; align-items: center;
  padding: .6rem 1rem; padding-top: max(.6rem, env(safe-area-inset-top));
  background: rgba(14, 17, 22, .92); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
}
.topbar input[type=search] { flex: 1; min-width: 0; max-width: 560px; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: 110%; min-width: 280px; display: grid; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; padding: .35rem; box-shadow: 0 12px 30px rgba(0,0,0,.45);
}
.menu button { text-align: left; border: none; background: transparent; }
.menu button:hover { background: var(--panel2); }

.layout { display: grid; grid-template-columns: 270px 1fr; min-height: calc(100vh - 58px); }
.sidebar { border-right: 1px solid var(--line); background: var(--panel); padding: .8rem .5rem; overflow-y: auto; max-height: calc(100vh - 58px); position: sticky; top: 58px; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 0 .4rem .6rem; color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .6px; }
.tree ul { list-style: none; margin: 0; padding-left: .9rem; }
.tree > ul { padding-left: 0; }
.tree-row { display: flex; align-items: center; border-radius: 7px; }
.tree-row.active { background: var(--panel2); }
.tree-row.active .tree-name { color: var(--accent); font-weight: 600; }
.caret { width: 1.6rem; flex: none; border: none; background: none; padding: .3rem 0; color: var(--muted); text-align: center; }
.tree-name { flex: 1; text-align: left; border: none; background: none; padding: .38rem .3rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.content { padding: 1.1rem 1.3rem 4rem; min-width: 0; }
.content-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: .6rem; align-items: center; margin-bottom: 1rem; }
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; font-size: 1.05rem; }
.crumbs .sep { color: var(--muted); }
.folder-actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.chips { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1.1rem; }
.chip { display: flex; gap: .5rem; align-items: center; border-radius: 999px; }
.chip small { color: var(--muted); }

/* Vista de lista: una conexión por bloque, separada por líneas (sin recuadros). */
.cards { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.card { background: transparent; border: none; border-bottom: 2px solid #2a3440; border-radius: 0; padding: .8rem 1rem; }
.card:last-child { border-bottom: none; }
.card:hover { background: #151c25; }
/* Lista de conexiones (filas) y panel de detalle */
.erows { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.erow { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .2rem .6rem; align-items: center; width: 100%; text-align: left; background: transparent; border: none; border-bottom: 1px solid #2a3440; padding: .7rem .9rem; color: inherit; font: inherit; cursor: pointer; }
.erow:hover { background: #151c25; }
.erow.active { background: #1a2a24; }
.erow .ename { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.erow .esub { grid-column: 1 / -1; color: var(--muted); font-size: .85rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.split { display: grid; grid-template-columns: minmax(260px, 2fr) minmax(0, 3fr); gap: 1rem; align-items: start; }
.list-col, .detail-col { min-width: 0; }
.detail-col { position: sticky; top: .5rem; }
.detail-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; }
.detail-card .card { background: transparent; border: none; padding: 0; }
.back { display: none; margin-bottom: .5rem; }
.mnav { display: none; }

/* Dashboard de Inicio */
.dash { display: grid; gap: 1rem; }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem; display: grid; gap: .2rem; }
.kpi strong { font-size: 1.5rem; }
.kpi span { color: var(--muted); font-size: .8rem; }
.dash-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; align-items: start; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; min-width: 0; }
.panel h3 { margin: 0 0 .6rem; font-size: .95rem; color: var(--muted); font-weight: 600; }
.bar-row { display: grid; grid-template-columns: minmax(80px, 140px) minmax(0, 1fr) 2.2rem; gap: .5rem; align-items: center; margin: .4rem 0; font-size: .85rem; }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { display: block; background: #1f2731; border-radius: 4px; height: .7rem; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: linear-gradient(90deg, #2f8f6b, #7ee2b8); border-radius: 4px; }
.bar-row b { text-align: right; }
.dash-link { display: grid; width: 100%; text-align: left; background: transparent; border: none; border-bottom: 1px solid #2a3440; color: inherit; font: inherit; padding: .5rem 0; cursor: pointer; }
.dash-link .ename { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-link small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel ul { margin: 0; padding-left: 1.1rem; font-size: .88rem; }
@media (max-width: 760px) {
  .split { grid-template-columns: 1fr; }
  .split.has-detail .list-col { display: none; }
  .mnav { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 8; gap: .4rem; overflow-x: auto; scroll-snap-type: x proximity; background: var(--panel); border-top: 1px solid var(--line); padding: .5rem .6rem calc(.5rem + env(safe-area-inset-bottom)); }
  .mitem { flex: none; scroll-snap-align: start; background: #1a2430; border: 1px solid var(--line); color: var(--text); border-radius: 999px; padding: .45rem .95rem; font: inherit; white-space: nowrap; cursor: pointer; }
  .mitem.active { background: #1d3a2f; border-color: #2f6b52; color: #7ee2b8; }
  .back { display: inline-block; }
  .detail-col { position: static; }
}
.org-list { display: flex; flex-direction: column; max-height: 60vh; overflow-y: auto; border-top: 1px solid #2a3440; }
.org-row { display: flex; align-items: center; gap: .6rem; padding: .55rem 0; border-bottom: 1px solid #2a3440; }
.org-row.dragging { position: relative; z-index: 2; background: #1a2430; box-shadow: 0 6px 18px rgba(0, 0, 0, .45); }
.org-row.drop-before { box-shadow: inset 0 3px 0 #7ee2b8; }
.org-row.drop-after { box-shadow: inset 0 -3px 0 #7ee2b8; }
.handle { cursor: grab; touch-action: none; user-select: none; border: none; background: transparent; color: var(--muted); font-size: 1.3rem; padding: 0 .4rem; }
.org-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-head { display: flex; justify-content: space-between; gap: .5rem; margin-bottom: .5rem; }
.card h3 { margin: 0; font-size: 1.02rem; word-break: break-word; }
.meta { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .3rem; align-items: center; }
.badge { font-size: .72rem; background: #1d3a2f; color: #7ee2b8; padding: .1rem .5rem; border-radius: 999px; }
.path { font-size: .78rem; color: var(--muted); }
.card-actions { display: flex; flex: none; }
.frow { display: grid; grid-template-columns: 110px 1fr auto; gap: .5rem; align-items: center; padding: .35rem 0; border-top: 1px solid #1f2731; }
.flabel { font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.val { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: .9rem; overflow-wrap: anywhere; }
.val.masked { letter-spacing: 2px; color: var(--muted); }
/* Todos los valores: una sola línea; lo que no cabe se corta con "…". Copiar sigue copiando el valor completo. */
.frow > .val { display: block; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; overflow-wrap: normal; }
.fbtns { display: flex; }
.notes { white-space: pre-wrap; font-size: .85rem; color: var(--muted); border-top: 1px solid #1f2731; padding-top: .5rem; margin: .3rem 0 0; }
.empty { color: var(--muted); padding: 2rem 0; display: grid; gap: .8rem; justify-items: start; }

/* ---- Modales ---- */
dialog.modal { border: none; padding: 0; background: transparent; color: var(--text); width: min(520px, 94vw); max-height: 92vh; }
dialog.modal.wide { width: min(760px, 96vw); }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: grid; max-height: 92vh; grid-template-rows: auto 1fr auto; }
.modal-box header { display: flex; justify-content: space-between; align-items: center; padding: .9rem 1.1rem; border-bottom: 1px solid var(--line); }
.modal-box h2 { margin: 0; font-size: 1.1rem; }
.modal-body { padding: 1rem 1.1rem; overflow-y: auto; }
.modal-box footer { display: flex; justify-content: flex-end; gap: .5rem; padding: .8rem 1.1rem; border-top: 1px solid var(--line); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.fields-title { display: flex; justify-content: space-between; align-items: center; margin: .3rem 0 .5rem; color: var(--muted); font-size: .85rem; }
.edit-fields { display: grid; gap: .45rem; margin-bottom: .9rem; }
.edit-row { display: grid; grid-template-columns: 150px 1fr 110px 34px 68px 34px; gap: .35rem; align-items: center; }
.fmove { display: flex; gap: 2px; }
.edit-row input:not(.lbl) { font-family: ui-monospace, "SF Mono", Consolas, monospace; }
table.audit { width: 100%; border-collapse: collapse; font-size: .85rem; }
.audit th, .audit td { text-align: left; padding: .4rem; border-bottom: 1px solid var(--line); }

#toast {
  position: fixed; left: 50%; bottom: calc(1.2rem + env(safe-area-inset-bottom)); transform: translate(-50%, 150%);
  background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: .7rem 1rem;
  transition: transform .2s ease, opacity .2s ease; z-index: 100; max-width: 92vw; font-size: .9rem;
  opacity: 0; pointer-events: none;
}
#toast.show { transform: translate(-50%, 0); opacity: 1; }
#toast.err { border-color: var(--danger); }

.only-mobile { display: none; }
@media (max-width: 760px) {
  .only-mobile { display: inline-block; }
  .hide-mobile { display: none; }
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: none; position: fixed; inset: 58px 0 0 0; z-index: 9; max-height: none; border-right: none; }
  .layout.show-side .sidebar { display: block; }
  .content { padding: .9rem .8rem 4rem; }
  .frow { grid-template-columns: 90px 1fr auto; }
  .two { grid-template-columns: 1fr; }
  .edit-row { grid-template-columns: 1fr 1fr; }
  .edit-row .lbl { grid-column: 1 / -1; }
  .edit-row input:not(.lbl) { grid-column: 1 / -1; }
  .topbar .primary { padding: .55rem .7rem; }
}
