:root {
  --navy-900: #0F1B2E;
  --navy-800: #172942;
  --navy-700: #223A5C;
  --navy-100: #E6EBF2;

  --bg: #F8F7F4;
  --surface: #FFFFFF;
  --surface-hover: #FCFBF8;
  --border: #E8E4DC;
  --border-strong: #D6D0C4;

  --text: #0F1419;
  --text-muted: #5B6472;
  --text-subtle: #8A9099;

  /* verde agua (accent) */
  --accent: #27b1bc;
  --accent-hover: #1f8e97;
  --accent-soft: #51b5bd;

  /* canales SIM (semánticos) */
  --verde: #22A06B;
  --verde-soft: #E1F3EA;
  --naranja: #E8862E;
  --naranja-soft: #FBEBD8;
  --rojo: #D53F3F;
  --rojo-soft: #FADFDF;
  --morado: #6E4EBD;
  --morado-soft: #ECE5F8;

  --radius: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(15, 27, 46, 0.04);
  --shadow-md: 0 4px 12px rgba(15, 27, 46, 0.06), 0 1px 3px rgba(15, 27, 46, 0.04);

  --sidebar-w: 232px;
  --sidebar-w-collapsed: 60px;
  --topbar-h: 56px;
  --transition: 180ms cubic-bezier(0.4, 0, 0.2, 1);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
  overflow-x: hidden;
}
code, .mono { font-family: 'IBM Plex Mono', monospace; letter-spacing: -0.01em; }
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
button:focus-visible, input:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
a { color: inherit; text-decoration: none; }
a.underline, .content a:not(.btn-primary):not(.btn-secondary):not(.btn-icon):not(.nav-item) { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }

/* ============ SIDEBAR ============ */
.sidebar {
  position: fixed;
  top: 0; left: 0;
  width: var(--sidebar-w);
  height: 100vh;
  background: var(--navy-900);
  color: #C9D2E0;
  display: flex;
  flex-direction: column;
  z-index: 20;
  transition: width var(--transition);
  border-right: 1px solid rgba(255,255,255,0.05);
}
.sidebar.collapsed { width: var(--sidebar-w-collapsed); }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  height: var(--topbar-h);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  white-space: nowrap;
  overflow: hidden;
}
.brand-mark {
  width: 26px; height: 26px;
  border-radius: 6px;
  background: var(--accent);
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
  letter-spacing: -0.03em;
}
.brand-name { font-weight: 600; font-size: 14px; color: var(--navy-900); letter-spacing: -0.01em; }
.brand-name.insideapp {
    color: #fff;
}
.brand-name span { color: var(--accent); }
.sidebar.collapsed .brand-name { opacity: 0; }

.nav { flex: 1; overflow-y: auto; padding: 12px 8px; }
.nav::-webkit-scrollbar { width: 4px; }
.nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; }

.nav-section-label {
  padding: 14px 12px 6px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #6B7688;
  font-weight: 600;
}
.sidebar.collapsed .nav-section-label { opacity: 0; height: 8px; padding: 4px 0; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  margin: 1px 0;
  border-radius: 6px;
  font-size: 13px;
  color: #B8C1D1;
  font-weight: 500;
  transition: background var(--transition), color var(--transition);
  position: relative;
  white-space: nowrap;
  overflow: hidden;
}
.nav-item:hover { background: rgba(255,255,255,0.05); color: #fff; }
.nav-item.active { background: rgba(39, 177, 188, 0.14); color: #fff; }
.nav-item.active::before {
  content: '';
  position: absolute;
  left: -8px;
  top: 6px; bottom: 6px;
  width: 3px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}
.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; }
.sidebar.collapsed .nav-item { justify-content: center; padding: 8px; }
.sidebar.collapsed .nav-item .label { display: none; }

.sidebar-user {
  padding: 10px 12px;
  border-top: 1px solid rgba(255,255,255,0.06);
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;
}
.avatar {
  width: 30px; height: 30px;
  border-radius: 6px;
  background: var(--accent);
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 11.5px;
  font-weight: 600;
  flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; }
.user-name { font-size: 12.5px; color: #fff; font-weight: 500; }
.user-org { font-size: 11px; color: #7A8598; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar.collapsed .user-meta, .sidebar.collapsed form { display: none; }

.btn-icon {
  width: 26px; height: 26px;
  border-radius: 6px;
  color: #7A8598;
  display: grid;
  place-items: center;
  transition: background var(--transition);
}
.btn-icon:hover { background: rgba(255,255,255,0.08); color: #fff; }

/* ============ MAIN ============ */
.main {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
  transition: margin-left var(--transition);
}
.main.expanded { margin-left: var(--sidebar-w-collapsed); }

.topbar {
  position: sticky;
  top: 0;
  background: rgba(248, 247, 244, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 16px;
  z-index: 15;
}

.toggle-btn {
  width: 30px; height: 30px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  color: var(--text-muted);
  transition: background var(--transition);
}
.toggle-btn:hover { background: var(--navy-100); color: var(--text); }
.toggle-btn svg { width: 16px; height: 16px; }

.breadcrumb { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-muted); }
.breadcrumb .current { color: var(--text); font-weight: 500; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.env-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--verde-soft);
  color: #0F6E44;
  border-radius: 6px;
  font-size: 11.5px;
  font-weight: 600;
  transition: background var(--transition), color var(--transition);
}
.env-badge.state-off { background: var(--border); color: var(--text-muted); }
.env-badge.state-warn { background: var(--naranja-soft); color: #A85A15; }
.env-badge.state-on .live-dot { background: var(--accent); }
.env-badge .live-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--verde);
  animation: pulse 2s infinite;
}
.env-badge.state-off .live-dot { background: var(--text-subtle); animation: none; }
.env-badge.state-warn .live-dot { background: var(--naranja); }
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(1.2); }
}

/* ============ CONTENT ============ */
.content { padding: 24px; max-width: 1600px; }

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 24px;
  flex-wrap: wrap;
}
.page-title { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; color: var(--text); }
.page-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
.page-subtitle .mono { color: var(--text); }

.btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: var(--navy-900);
  color: #fff;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  transition: background var(--transition), transform var(--transition);
}
.btn-primary:hover { background: var(--navy-800); transform: translateY(-1px); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary.btn-block { width: 100%; justify-content: center; padding: 10px; }
.btn-primary svg { width: 14px; height: 14px; }

.btn-secondary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  transition: background var(--transition);
}
.btn-secondary:hover { background: var(--surface-hover); border-color: var(--border-strong); }

.btn-danger {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px;
  color: var(--rojo);
  border-radius: 4px;
  transition: background var(--transition);
  font-size: 16px; font-weight: 700; line-height: 1;
}
.btn-danger:hover { background: var(--rojo-soft); }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  gap: 16px;
}
.card-title { font-size: 14px; font-weight: 600; letter-spacing: -0.01em; }
.card-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* Tables */
.table-card { padding: 0; overflow: hidden; }
.table-scroll { overflow-x: auto; overflow-y: visible; max-width: 100%; }
.table-scroll::-webkit-scrollbar { height: 10px; }
.table-scroll::-webkit-scrollbar-track { background: var(--bg); }
.table-scroll::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 5px; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
thead th {
  text-align: left;
  padding: 10px 14px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-subtle);
  font-weight: 600;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
thead th.pk-col { color: var(--accent); }
tbody tr { border-bottom: 1px solid var(--border); transition: background var(--transition); }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--surface-hover); }
tbody td { padding: 8px 14px; vertical-align: middle; white-space: nowrap; max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
td.pk-col { background: rgba(39, 177, 188, 0.04); }

/* Sticky columns: primera (PK) a la izquierda, última (acciones) a la derecha */
#grid thead th:first-child,
#grid tbody td:first-child {
  position: sticky; left: 0; z-index: 2;
  background: var(--surface);
  box-shadow: 1px 0 0 var(--border);
}
#grid thead th:first-child { background: var(--bg); }
#grid tbody tr:hover td:first-child { background: var(--surface-hover); }

#grid thead th:last-child,
#grid tbody td:last-child {
  position: sticky; right: 0; z-index: 2;
  background: var(--surface);
  box-shadow: -1px 0 0 var(--border);
  width: 44px; padding-left: 0; padding-right: 8px;
}
#grid thead th:last-child { background: var(--bg); }
#grid tbody tr:hover td:last-child { background: var(--surface-hover); }

.amount { font-family: 'IBM Plex Mono', monospace; font-weight: 500; text-align: right; }
.pk-mark {
  font-size: 9px;
  padding: 1px 4px;
  margin-left: 4px;
  border-radius: 3px;
  background: rgba(39, 177, 188, 0.15);
  color: var(--accent);
  vertical-align: middle;
}

/* Inline cell editor */
td.editable { cursor: cell; }
td.editable:hover { background: rgba(39, 177, 188, 0.06); }
td .cell-editor {
  width: 100%;
  padding: 4px 6px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  font-family: inherit; font-size: inherit;
  background: #fff;
  outline: none;
}
td.cell-updated { background: rgba(34, 160, 107, 0.15); transition: background 700ms ease-out; }

/* Alerts */
.alert {
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 16px;
}
.alert-error { background: var(--rojo-soft); color: #7C1F1F; border: 1px solid rgba(213,63,63,0.25); }
.alert-info { background: rgba(39, 177, 188, 0.08); color: #145864; border: 1px solid rgba(39, 177, 188, 0.25); }
.alert pre.token-box {
  margin: 10px 0 8px;
  padding: 10px 12px;
  background: #0F1B2E;
  color: #E8FBFD;
  border-radius: 6px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  overflow-x: auto;
  user-select: all;
}

/* Empty state */
.empty { padding: 32px; text-align: center; color: var(--text-muted); }

/* Inputs */
.input, input[type=text], input[type=email], input[type=password] {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: var(--surface);
}
input:focus { border-color: var(--accent); }

/* Status dot */
.dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  vertical-align: middle;
  margin-right: 6px;
}
.dot.on { background: var(--verde); box-shadow: 0 0 0 3px var(--verde-soft); }
.dot.done { background: var(--text-subtle); }
.dot.wait { background: var(--naranja); box-shadow: 0 0 0 3px var(--naranja-soft); }

.text-error { color: var(--rojo); }

/* ============ AUTH ============ */
.auth-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: linear-gradient(180deg, var(--bg) 0%, #EEEBE4 100%);
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 32px 28px;
  box-shadow: var(--shadow-md);
}
.auth-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}
.auth-title { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; margin-bottom: 4px; }
.auth-sub { color: var(--text-muted); font-size: 13px; margin-bottom: 20px; }
.auth-form { display: flex; flex-direction: column; gap: 14px; margin-bottom: 16px; }
.auth-form label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--text-muted); font-weight: 500; }
.auth-form label input { font-size: 14px; padding: 10px 12px; }
.auth-form label small { color: var(--text-subtle); font-size: 11.5px; }
.auth-alt { text-align: center; font-size: 12.5px; color: var(--text-muted); margin-top: 8px; }
.auth-alt a { color: var(--accent); font-weight: 600; }

/* Modal */
.modal {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  max-width: 640px;
  width: 90%;
  box-shadow: var(--shadow-md);
}
.modal::backdrop { background: rgba(15, 27, 46, 0.35); backdrop-filter: blur(2px); }
.modal h3 { margin-bottom: 16px; font-size: 16px; }
.new-row-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  max-height: 60vh;
  overflow-y: auto;
}
.new-row-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; }
.new-row-grid label em { color: var(--accent); font-style: normal; font-weight: 500; }
.modal menu { list-style: none; display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; padding: 0; }
.modal menu button:not(.btn-primary) {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  font-size: 13px;
}

/* ============ INSTALL WIZARD ============ */
.install-intro p { color: var(--text-muted); font-size: 13.5px; }
.install-h2 { font-size: 15px; font-weight: 600; margin-bottom: 8px; }
.install-steps { list-style: none; padding: 0; margin: 24px 0 0; display: flex; flex-direction: column; gap: 14px; }
.install-step {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
  display: grid;
  grid-template-columns: 44px 1fr;
  gap: 14px;
  align-items: start;
}
.install-num {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 700; font-size: 14px;
}
.install-body h3 { font-size: 14.5px; font-weight: 600; margin-bottom: 6px; }
.install-body p { font-size: 13px; color: var(--text-muted); margin-bottom: 10px; }
.install-body ul { padding-left: 20px; font-size: 13px; color: var(--text-muted); }
.install-body ul li { margin-bottom: 4px; }
.install-warn ul { margin-top: 6px; }
.install-warn { margin-top: 12px; }

/* Responsive */
@media (max-width: 1200px) { }
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.mobile-open { transform: translateX(0); }
  .main { margin-left: 0; }
  .content { padding: 16px; }
  .topbar { padding: 0 12px; }
}
