:root {
  --bg: #020617;
  --bg-elevated: #020617;
  --card-bg: #020617;
  --accent: #ec4899;
  --accent-soft: rgba(236, 72, 153, 0.16);
  --accent-strong: #db2777;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --border: rgba(148, 163, 184, 0.35);
  --danger: #f97373;
  --warn: #fbbf24;
  --radius-lg: 16px;
  --radius-xl: 22px;
  --shadow-soft: 0 18px 45px rgba(15, 23, 42, 0.8);
  --shadow-card: 0 14px 30px rgba(15, 23, 42, 0.9);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body.wb-body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(236,72,153,0.18), transparent 55%),
    radial-gradient(circle at bottom right, rgba(59,130,246,0.12), transparent 55%),
    #020617;
}

/* Login */
.wb-login-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
}

.wb-login-card {
  width: 100%;
  max-width: 420px;
  background: radial-gradient(circle at top, rgba(236,72,153,0.16), transparent 55%), #020617;
  border-radius: 26px;
  box-shadow: var(--shadow-soft);
  padding: 28px 26px 24px;
  border: 1px solid rgba(148,163,184,0.4);
  backdrop-filter: blur(18px);
}

.wb-title {
  margin: 0 0 4px;
  font-size: 2.2rem;
  font-weight: 900;
  text-align: center;
  letter-spacing: 3px;
  background: linear-gradient(135deg, rgba(236,72,153,0.9), rgba(147,51,234,0.9));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.wb-subtitle {
  margin: 0 0 20px;
  font-size: 1.1rem;
  font-weight: 700;
  text-align: center;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #ec4899 0%, #f472b6 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 2px 2px 4px rgba(236, 72, 153, 0.3);
}

.wb-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.wb-form label {
  font-size: .8rem;
  color: var(--muted);
}

.wb-form input {
  background: rgba(15,23,42,0.9);
  border-radius: 12px;
  border: 1px solid rgba(51,65,85,0.9);
  color: var(--text);
  padding: 9px 11px;
  font-size: .9rem;
  outline: none;
}

.wb-form input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(236,72,153,0.5);
}

.wb-btn-primary {
  margin-top: 10px;
  padding: 9px 12px;
  border-radius: 999px;
  border: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: white;
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  box-shadow: 0 14px 35px rgba(236,72,153,0.5);
}

.wb-btn-primary:hover {
  filter: brightness(1.05);
}

.wb-error {
  margin-top: 6px;
  font-size: .8rem;
  color: var(--danger);
}

.wb-footnote {
  margin-top: 14px;
  font-size: .75rem;
  color: var(--muted);
}

/* Password field with eye toggle */
.passwordField {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
}

.passwordField input {
  flex: 1;
  padding-right: 45px;
}

.togglePasswordBtn {
  background: rgba(236, 72, 153, 0.1);
  border: 1px solid rgba(236, 72, 153, 0.3);
  color: #ec4899;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1.2rem;
  transition: all 0.2s ease;
  min-width: 42px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.togglePasswordBtn:hover {
  background: rgba(236, 72, 153, 0.2);
  border-color: #ec4899;
  transform: scale(1.05);
}

.togglePasswordBtn:active {
  transform: scale(0.95);
}

/* Server Actions Menu */
.wb-server-menu {
  position: fixed;
  background: #1e293b;
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
  padding: 6px;
  z-index: 9999;
  min-width: 220px;
  max-width: 250px;
  animation: menuFadeIn 0.15s ease-out;
}

@keyframes menuFadeIn {
  from {
    opacity: 0;
    transform: translateY(-5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.wb-menu-item {
  display: block;
  width: 100%;
  padding: 10px 14px;
  background: transparent;
  border: none;
  color: #e5e7eb;
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s ease;
  margin-bottom: 2px;
}

.wb-menu-item:last-child {
  margin-bottom: 0;
}

.wb-menu-item:hover {
  background: rgba(236, 72, 153, 0.15);
  color: #ec4899;
  transform: translateX(3px);
}

.wb-menu-item:active {
  transform: translateX(3px) scale(0.98);
}

/* Layout principal */
.wb-app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.wb-topbar {
  padding: 10px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border);
  background: radial-gradient(circle at top left, rgba(236,72,153,0.25), transparent 55%), rgba(15,23,42,0.96);
  backdrop-filter: blur(16px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.wb-topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.wb-logo {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top, rgba(236,72,153,0.4), rgba(15,23,42,1));
  font-size: 1rem;
}

.wb-topbar-title {
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  background: linear-gradient(135deg, rgba(236,72,153,0.9), rgba(147,51,234,0.9));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.wb-topbar-sub {
  font-size: .85rem;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: #ffffff;
  text-shadow: 
    0 1px 0 #e2e8f0,
    0 2px 0 #cbd5e1,
    0 3px 0 #94a3b8,
    0 4px 6px rgba(0,0,0,0.5);
  filter: brightness(1.1);
}

.wb-topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.wb-userinfo {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-size: .8rem;
}

.wb-userrole {
  color: var(--muted);
  font-size: .72rem;
}

.wb-btn-secondary {
  padding: 7px 11px;
  border-radius: 999px;
  background: transparent;
  border: 1px solid rgba(148,163,184,0.6);
  color: var(--text);
  font-size: .8rem;
  cursor: pointer;
}

.wb-btn-secondary:hover {
  border-color: var(--accent);
}

.wb-main {
  flex: 1;
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
  gap: 0;
  overflow: hidden;
  max-height: calc(100vh - 60px);
}

/* Sidebar */
.wb-sidebar {
  border-right: 1px solid var(--border);
  padding: 14px 10px;
  background: radial-gradient(circle at top, rgba(15,23,42,0.6), rgba(15,23,42,1));
  overflow-y: auto;
}

.wb-sidebar::-webkit-scrollbar {
  width: 8px;
}

.wb-sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.wb-sidebar::-webkit-scrollbar-thumb {
  background: rgba(236, 72, 153, 0.4);
  border-radius: 4px;
}

.wb-sidebar::-webkit-scrollbar-thumb:hover {
  background: rgba(236, 72, 153, 0.6);
}

.wb-nav-btn {
  width: 100%;
  text-align: center;
  padding: 9px 8px;
  margin-bottom: 6px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: .82rem;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  white-space: nowrap;
  overflow: visible;
  min-height: 34px;
  line-height: 1.2;
}

.wb-nav-btn:hover {
  background: rgba(15,23,42,0.8);
}

.wb-nav-active {
  color: var(--text);
  background: var(--accent-soft);
  border-color: rgba(236,72,153,0.5);
}

/* Badge de alerta en menú */
.wb-badge-alert {
  position: absolute;
  top: 6px;
  right: 8px;
  background: #ef4444;
  color: white;
  font-size: 0.65rem;
  font-weight: bold;
  padding: 2px 5px;
  border-radius: 10px;
  min-width: 16px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
  animation: pulse-badge 2s infinite;
  z-index: 10;
}
  box-shadow: 0 2px 4px rgba(239, 68, 68, 0.4);
  animation: pulse-badge 2s ease-in-out infinite;
}

@keyframes pulse-badge {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* Content */
.wb-content {
  padding: 18px 18px 26px;
  overflow-y: auto;
  max-height: calc(100vh - 60px);
}

.wb-content::-webkit-scrollbar {
  width: 10px;
}

.wb-content::-webkit-scrollbar-track {
  background: rgba(15, 23, 42, 0.5);
  border-radius: 5px;
}

.wb-content::-webkit-scrollbar-thumb {
  background: rgba(236, 72, 153, 0.5);
  border-radius: 5px;
}

.wb-content::-webkit-scrollbar-thumb:hover {
  background: rgba(236, 72, 153, 0.7);
}

.wb-section-title {
  font-size: 1.1rem;
  margin: 0 0 12px;
}

.wb-subsection-title {
  margin-top: 26px;
  margin-bottom: 8px;
  font-size: .95rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .12em;
}

.wb-view-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.wb-filters {
  display: flex;
  gap: 6px;
  align-items: center;
}

.wb-filters input,
.wb-filters select {
  background: rgba(15,23,42,0.9);
  border-radius: 999px;
  border: 1px solid rgba(51,65,85,0.9);
  color: var(--text);
  padding: 6px 10px;
  font-size: .8rem;
  outline: none;
}

.wb-filters input:focus,
.wb-filters select:focus {
  border-color: var(--accent);
}

/* Cards */
.wb-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 10px;
}

.wb-card {
  background: radial-gradient(circle at top, rgba(15,23,42,0.7), rgba(15,23,42,1));
  border-radius: var(--radius-xl);
  padding: 12px 14px 10px;
  border: 1px solid rgba(148,163,184,0.35);
  box-shadow: var(--shadow-card);
}

.wb-card-clickable {
  cursor: pointer;
  transition: all 0.2s ease;
}

.wb-card-clickable:hover {
  border-color: rgba(236, 72, 153, 0.6);
  box-shadow: 0 0 20px rgba(236, 72, 153, 0.3);
  transform: translateY(-2px);
}

.wb-card-clickable:active {
  transform: translateY(0);
}

.wb-card-warning {
  border-color: rgba(251, 191, 36, 0.4);
}

.wb-card-warning:hover {
  border-color: rgba(251, 191, 36, 0.7);
  box-shadow: 0 0 20px rgba(251, 191, 36, 0.3);
}

.wb-card-wide {
  grid-column: span 2;
}

.wb-card-label {
  font-size: .8rem;
  color: var(--muted);
}

.wb-card-value {
  margin-top: 4px;
  font-size: 1.4rem;
  font-weight: 600;
}

.wb-card-foot {
  margin-top: 4px;
  font-size: .75rem;
  color: var(--muted);
}

.wb-warn {
  color: var(--warn);
}

.wb-danger {
  color: var(--danger);
}

/* Tablas */
.wb-table-wrapper {
  margin-top: 8px;
  border-radius: 18px;
  border: 1px solid rgba(51,65,85,0.9);
  overflow-x: auto;
  overflow-y: visible;
  background: rgba(15,23,42,0.96);
  box-shadow: var(--shadow-soft);
  position: relative;
}

.wb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .8rem;
}

.wb-table thead {
  background: rgba(15,23,42,0.98);
}

.wb-table th,
.wb-table td {
  padding: 7px 9px;
  border-bottom: 1px solid rgba(31,41,55,0.9);
}

.wb-table th {
  text-align: left;
  font-weight: 500;
  color: var(--muted);
  font-size: .75rem;
}

.wb-table tbody tr:nth-child(even) {
  background: rgba(15,23,42,0.85);
}

.wb-table tbody tr:hover {
  background: rgba(30,64,175,0.25);
}

.wb-table-empty {
  text-align: center;
  color: var(--muted);
  padding: 14px 9px;
}

/* Modal */
.wb-modal {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.85);
}

/* Cuando el modal está visible, usar flex para centrar */
.wb-modal[style*="display: flex"],
.wb-modal[style*="display:flex"] {
  display: flex !important;
  align-items: center;
  justify-content: center;
}

.wb-modal-content {
  background: radial-gradient(circle at top, rgba(236,72,153,0.16), transparent 55%), #020617;
  border-radius: 22px;
  padding: 20px;
  max-width: 500px;
  width: 90%;
  border: 1px solid rgba(148,163,184,0.4);
  box-shadow: var(--shadow-soft);
}

/* Animación de luz girando en el borde tipo víbora */
.wb-modal-edit-animated {
  position: relative;
  border: 3px solid transparent;
  background: radial-gradient(circle at top, rgba(236,72,153,0.16), transparent 55%), #020617;
  background-clip: padding-box;
  overflow: hidden;
}

.wb-modal-edit-animated::before {
  content: '';
  position: absolute;
  inset: -50%;
  background: conic-gradient(
    from 0deg,
    transparent 0deg,
    transparent 340deg,
    #ec4899 350deg,
    #8b5cf6 355deg,
    #3b82f6 360deg,
    #06b6d4 365deg,
    #10b981 370deg,
    transparent 380deg,
    transparent 360deg
  );
  animation: spinBorder 3s linear infinite;
}

.wb-modal-edit-animated::after {
  content: '';
  position: absolute;
  inset: 3px;
  border-radius: 19px;
  background: radial-gradient(circle at top, rgba(236,72,153,0.16), transparent 55%), #020617;
  z-index: 0;
}

.wb-modal-edit-animated > * {
  position: relative;
  z-index: 2;
}

@keyframes spinBorder {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.wb-modal-terminal {
  max-width: 1200px;
  width: 95%;
  max-height: 95vh;
}

.wb-modal-content h3 {
  margin: 0 0 16px;
  font-size: 1.2rem;
}

/* Filter buttons for renewed modal */
.wb-filter-btn {
  padding: 8px 16px;
  border: 1px solid rgba(148,163,184,0.3);
  background: rgba(15,23,42,0.6);
  color: #e2e8f0;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.wb-filter-btn:hover {
  border-color: rgba(236,72,153,0.5);
  background: rgba(236,72,153,0.15);
}

.wb-filter-btn.active {
  background: linear-gradient(135deg, rgba(236,72,153,0.3), rgba(219,39,119,0.3));
  border-color: rgba(236,72,153,0.7);
  color: #fff;
  font-weight: 600;
}

/* Compact progress bars */
.wb-compact-bar {
  display: inline-block;
  width: 60px;
  height: 18px;
  background: rgba(148,163,184,0.2);
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  vertical-align: middle;
}

.wb-compact-bar-fill {
  height: 100%;
  transition: width 0.3s ease;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.3);
}

.wb-compact-bar-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 0.7rem;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.9);
}

#terminal {
  border: 2px solid rgba(236, 72, 153, 0.3);
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  height: 500px;
}

#terminal .xterm {
  height: 100% !important;
  padding: 10px;
}

#terminal .xterm-viewport {
  overflow-y: scroll !important;
  scrollbar-width: thin;
  scrollbar-color: rgba(236, 72, 153, 0.6) rgba(15, 23, 42, 0.8);
}

#terminal .xterm-viewport::-webkit-scrollbar {
  width: 12px;
}

#terminal .xterm-viewport::-webkit-scrollbar-track {
  background: rgba(15, 23, 42, 0.8);
  border-radius: 6px;
}

#terminal .xterm-viewport::-webkit-scrollbar-thumb {
  background: rgba(236, 72, 153, 0.6);
  border-radius: 6px;
  border: 2px solid rgba(15, 23, 42, 0.8);
}

#terminal .xterm-viewport::-webkit-scrollbar-thumb:hover {
  background: rgba(236, 72, 153, 0.9);
}

#terminal .xterm-screen {
  height: auto !important;
}

/* Responsive */
@media (max-width: 900px) {
  .wb-main {
    grid-template-columns: 1fr;
  }
  .wb-sidebar {
    display: flex;
    gap: 6px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
  }
}

/* Small devices */
@media (max-width: 600px) {
  .wb-topbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  .wb-topbar-right {
    width: 100%;
    justify-content: space-between;
  }
}
EOF
}

# ====== Nginx para panel ======

# vhost temporal solo HTTP para emitir el certificado (webroot)
write_nginx_http_challenge() {
  log "Escribiendo Nginx HTTP temporal para challenge…"
  local F="/etc/nginx/sites-available/wimi-billing-http"
  mkdir -p /var/www/html
  cat > "$F" <<EOF
server {
  listen 80;
  listen [::]:80;
  server_name ${DOMAIN};

  root /var/www/html;

  location /.well-known/acme-challenge/ {
    root /var/www/html;
  }

  location / {
    return 503;
  }
}
EOF

  ln -sf "$F" /etc/nginx/sites-enabled/wimi-billing-http
}

# vhost final HTTP + HTTPS (IPv4/IPv6) apuntando al backend
write_nginx_panel() {
  log "Escribiendo Nginx final (HTTP + HTTPS) para panel billing…"
  local F="/etc/nginx/sites-available/wimi-billing-panel"
  local CERT_DIR="/etc/letsencrypt/live/${DOMAIN}"

  cat > "$F" <<EOF
# WIMI-BILLING SSL Configuration — FINAL
server {
    listen 80;
    listen [::]:80;
    server_name ${DOMAIN};

    # Redirección a HTTPS
    location /.well-known/acme-challenge/ {
        root /var/www/html;
        try_files \$uri \$uri/ =404;
    }

    location / {
        return 301 https://\$server_name\$request_uri;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name ${DOMAIN};

    access_log /var/log/nginx/billing_access.log;
    error_log  /var/log/nginx/billing_error.log;

    # Configuración SSL
    ssl_certificate     ${CERT_DIR}/fullchain.pem;
    ssl_certificate_key ${CERT_DIR}/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # Headers de seguridad
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Frame-Options DENY always;
    add_header X-Content-Type-Options nosniff always;

    # Proxy al backend Node
    location / {
        proxy_pass http://127.0.0.1:${APP_PORT};
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host \$host;
        proxy_cache_bypass \$http_upgrade;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
        proxy_set_header X-Forwarded-Host \$host;

        # Timeout extendido para conexiones lentas
        proxy_connect_timeout 60s;
        proxy_send_timeout    60s;
        proxy_read_timeout    60s;
    }
}
EOF

  # Desactivo configs viejas que puedan chocar
  rm -f /etc/nginx/sites-enabled/wimi-billing-http
  rm -f /etc/nginx/sites-enabled/wimi-billing-ssl-final || true
  ln -sf "$F" /etc/nginx/sites-enabled/wimi-billing-panel
  
  # Forzar recarga de Nginx para aplicar redirección HTTP -> HTTPS
  log "Aplicando configuración de Nginx..."
  nginx -t && systemctl reload nginx
  log "✅ Redirección HTTP -> HTTPS activada"
}

issue_ssl() {
  log "Emitiendo/renovando certificados para panel billing…"
  systemctl enable nginx --now >/dev/null 2>&1 || true

  local CERT_DIR="/etc/letsencrypt/live/${DOMAIN}"
  mkdir -p /var/www/html

  if [[ ! -f "${CERT_DIR}/fullchain.pem" ]]; then
    log "No existe certificado previo, se generará uno nuevo."
    write_nginx_http_challenge
    nginx -t && systemctl reload nginx || true

    read -rp "Email para Let's Encrypt: " LE_MAIL
    if [[ -z "$LE_MAIL" ]]; then
      err "Email vacío, salto emisión SSL."
      return
    fi

    certbot certonly --webroot -w /var/www/html \
      -d "${DOMAIN}" \
      -m "${LE_MAIL}" --agree-tos --no-eff-email --non-interactive || {
        err "Certbot falló. Revisa los logs."
        return
      }
  else
    log "Certificado ya existe en ${CERT_DIR}."
  fi

  # Escribo config final y recargo
  write_nginx_panel
  nginx -t && systemctl reload nginx || true
}

start_pm2() {
  log "Arrancando backend con PM2…"
  cd "${BASE_DIR}"
  if ! command -v pm2 >/dev/null 2>&1; then
    npm install -g pm2
  fi
  pm2 delete "${APP_NAME}" >/dev/null 2>&1 || true
  pm2 start index.js --name "${APP_NAME}"
  pm2 save
  pm2 status "${APP_NAME}"
}

show_logs() {
  if ! command -v pm2 >/dev/null 2>&1; then
    err "PM2 no está instalado."
    return
  fi
  pm2 logs "${APP_NAME}" --lines 80
}

npm_install() {
  log "Instalando dependencias npm…"
  cd "${BASE_DIR}"
  npm install --no-audit --no-fund
}

write_all_files() {
  ensure_dirs
  write_env
  write_package_json

/* Modal overlays inline (para modalResult, modalSyncProgress, etc) */
.wb-modal-overlay[style*="display: flex"],
.wb-modal-overlay[style*="display:flex"] {
  display: flex !important;
  align-items: center;
  justify-content: center;
}

/* Modal Terminal SSH - Asegurar que se vea correctamente */
#modalTerminal {
  z-index: 10000 !important;
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.85);
}

#modalTerminal[style*="flex"] {
  display: flex !important;
  align-items: center;
  justify-content: center;
}

#modalTerminal .wb-modal-content {
  position: relative;
  z-index: 10001;
}
/* ============================================
   RESPONSIVE DESIGN - MOBILE & TABLET
   ============================================ */

/* Botón hamburger (oculto en desktop) */
.wb-hamburger {
  display: none;
  position: fixed;
  top: 20px;
  left: 20px;
  z-index: 1002;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
  box-shadow: var(--shadow-card);
}

.wb-hamburger span {
  display: block;
  width: 24px;
  height: 2px;
  background: var(--text);
  margin: 5px 0;
  transition: 0.3s;
}

/* Tablet - 1024px y menos */
@media (max-width: 1024px) {
  .wb-hamburger {
    display: block;
  }
  
  .wb-sidebar {
    position: fixed;
    left: -250px;
    top: 0;
    height: 100vh;
    z-index: 1001;
    transition: left 0.3s ease;
    overflow-y: auto;
  }
  
  .wb-sidebar.show {
    left: 0;
  }
  
  .wb-main {
    margin-left: 0 !important;
    width: 100%;
  }
  
  .wb-dashboard-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    padding: 20px;
  }
  
  .wb-clients-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
}

/* Mobile - 768px y menos */
@media (max-width: 768px) {
  .wb-hamburger {
    top: 15px;
    left: 15px;
    padding: 10px;
  }
  
  .wb-sidebar {
    width: 250px;
    left: -250px;
  }
  
  .wb-main {
    padding: 15px;
  }
  
  .wb-dashboard-grid {
    grid-template-columns: 1fr;
    gap: 12px;
    padding: 15px;
  }
  
  .wb-clients-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .wb-card {
    padding: 16px;
  }
  
  .wb-topbar {
    flex-direction: column;
    gap: 12px;
    padding: 15px;
    align-items: flex-start;
  }
  
  .wb-topbar-actions {
    width: 100%;
    flex-wrap: wrap;
  }
  
  .wb-search-box {
    width: 100%;
    max-width: none;
  }
  
  .wb-btn {
    padding: 8px 14px;
    font-size: 0.85rem;
  }
  
  .wb-title {
    font-size: 1.8rem;
  }
  
  .wb-subtitle {
    font-size: 0.95rem;
  }
  
  /* Tabla responsive - scroll horizontal */
  .wb-table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .wb-table {
    min-width: 600px;
  }
  
  /* Modales en mobile */
  .wb-modal-content {
    width: 95%;
    max-width: 95%;
    margin: 10px;
    max-height: 90vh;
    overflow-y: auto;
  }
  
  /* Formularios en mobile */
  .wb-form-row {
    flex-direction: column;
  }
  
  .wb-form-group {
    width: 100% !important;
  }
}

/* Mobile pequeño - 480px y menos */
@media (max-width: 480px) {
  .wb-hamburger {
    top: 10px;
    left: 10px;
    padding: 8px;
  }
  
  .wb-sidebar {
    width: 220px;
    left: -220px;
  }
  
  .wb-main {
    padding: 10px;
  }
  
  .wb-dashboard-grid {
    padding: 10px;
    gap: 10px;
  }
  
  .wb-card {
    padding: 12px;
  }
  
  .wb-title {
    font-size: 1.5rem;
  }
  
  .wb-btn {
    padding: 7px 12px;
    font-size: 0.8rem;
  }
  
  .wb-topbar {
    padding: 12px;
  }
  
  /* Ajustar tamaño de fuente en tablas */
  .wb-table {
    font-size: 0.85rem;
  }
  
  .wb-table th,
  .wb-table td {
    padding: 8px;
  }
}