:root {
  --bg: #060b18;
  --bg-elev: #0d1527;
  --bg-card: rgba(15, 23, 42, 0.86);
  --bg-hover: rgba(30, 41, 59, 0.72);
  --border: rgba(148, 163, 184, 0.18);
  --text: #f8fafc;
  --muted: #94a3b8;
  --primary: #0ea5e9;
  --primary-2: #38bdf8;
  --indigo: #6366f1;
  --ok: #10b981;
  --ok-bg: rgba(16, 185, 129, 0.14);
  --warn: #f59e0b;
  --warn-bg: rgba(245, 158, 11, 0.16);
  --danger: #ef4444;
  --danger-bg: rgba(239, 68, 68, 0.16);
  --info: #38bdf8;
  --info-bg: rgba(56, 189, 248, 0.14);
  --shadow: 0 12px 32px rgba(2, 6, 23, 0.45);
  --radius: 14px;
}

html[data-theme="light"] {
  --bg: #f1f5f9;
  --bg-elev: #ffffff;
  --bg-card: #ffffff;
  --bg-hover: #f8fafc;
  --border: #cbd5e1;
  --text: #0f172a;
  --muted: #64748b;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif; background: var(--bg); color: var(--text); min-height: 100%; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* Login */
.login-body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1.25rem;
  background: radial-gradient(circle at 15% 15%, rgba(14,165,233,0.18), transparent 45%),
              radial-gradient(circle at 85% 80%, rgba(99,102,241,0.18), transparent 45%),
              var(--bg);
}
.login-wrapper { width: 100%; max-width: 420px; }
.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 2rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}
.login-brand { display: flex; align-items: center; gap: 0.9rem; margin-bottom: 1.5rem; }
.login-logo { width: 52px; height: 52px; border-radius: 12px; }
.login-brand h1 { margin: 0; font-size: 1.35rem; letter-spacing: -0.02em; }
.login-brand p { margin: 0.2rem 0 0; font-size: 0.82rem; color: var(--muted); }
.login-form { display: flex; flex-direction: column; gap: 1rem; }

/* App Shell */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem 0.85rem;
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 40;
}
.sidebar-brand { display: flex; align-items: center; gap: 0.75rem; padding: 0.35rem 0.5rem 1rem; border-bottom: 1px solid var(--border); margin-bottom: 0.75rem; }
.brand-icon { width: 40px; height: 40px; }
.brand-title { font-weight: 800; font-size: 1.05rem; letter-spacing: -0.02em; }
.brand-sub { font-size: 0.74rem; color: var(--muted); }

.sidebar-nav { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 0.22rem; }
.nav-section { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); padding: 0.75rem 0.65rem 0.25rem; font-weight: 700; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  background: transparent;
  border: 1px solid transparent;
  padding: 0.6rem 0.75rem;
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--muted);
  transition: 0.16s ease;
}
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active { background: rgba(14, 165, 233, 0.14); border-color: rgba(56, 189, 248, 0.32); color: var(--primary-2); font-weight: 600; }
.nav-ico { width: 20px; text-align: center; }
.nav-badge { margin-left: auto; font-size: 0.75rem; padding: 0.12rem 0.5rem; border-radius: 999px; background: rgba(148,163,184,0.15); color: var(--text); }

.sidebar-footer { border-top: 1px solid var(--border); padding-top: 0.85rem; margin-top: 0.5rem; }
.user-pill { display: flex; align-items: center; gap: 0.65rem; margin-bottom: 0.6rem; }
.user-avatar {
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--indigo));
  display: grid; place-items: center; font-weight: 800; color: #fff;
}
.user-name { font-size: 0.86rem; font-weight: 600; line-height: 1.2; }
.user-role { font-size: 0.74rem; color: var(--muted); }
.sidebar-actions { display: flex; gap: 0.45rem; }

.main-wrap { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 25;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: 0.85rem 1.35rem;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: 0.75rem; }
.topbar-left h2 { margin: 0; font-size: 1.18rem; }
.topbar-sub { font-size: 0.8rem; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }

.main-content { padding: 1.25rem 1.35rem 5rem; max-width: 1440px; width: 100%; margin: 0 auto; }

/* Controls & Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.55rem 0.95rem; border-radius: 10px; border: 1px solid transparent;
  font-weight: 600; font-size: 0.86rem; cursor: pointer; transition: 0.15s ease;
}
.btn-sm { padding: 0.38rem 0.7rem; font-size: 0.8rem; border-radius: 8px; }
.btn-xs { padding: 0.24rem 0.52rem; font-size: 0.74rem; border-radius: 7px; }
.btn-block { width: 100%; }
.btn-primary { background: linear-gradient(135deg, #0ea5e9, #2563eb); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-success { background: #10b981; color: #fff; }
.btn-warning { background: #f59e0b; color: #111827; }
.btn-danger { background: rgba(239, 68, 68, 0.15); border-color: rgba(239, 68, 68, 0.35); color: #f87171; }
.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover { border-color: var(--primary-2); color: var(--primary-2); }
.btn-ghost { background: rgba(148,163,184,0.1); color: var(--text); }
.btn-ghost:hover { background: rgba(148,163,184,0.2); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg-card); cursor: pointer; display: grid; place-items: center;
}

.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field label { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.field input, .field select, .field textarea, .select-compact, .search-input {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 0.56rem 0.75rem; font-size: 0.9rem; outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus, .select-compact:focus, .search-input:focus {
  border-color: var(--primary-2);
}
.select-compact { padding: 0.42rem 0.65rem; font-size: 0.84rem; max-width: 240px; }

/* Cards & Grids */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0.9rem; margin-bottom: 1.2rem; }
.kpi-card {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem 1.1rem; display: flex; flex-direction: column; gap: 0.3rem; cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease;
}
.kpi-card:hover { transform: translateY(-2px); border-color: var(--primary-2); }
.kpi-label { font-size: 0.78rem; color: var(--muted); font-weight: 600; }
.kpi-value { font-size: 1.75rem; font-weight: 800; line-height: 1.1; }
.kpi-sub { font-size: 0.76rem; color: var(--muted); }

.panel {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.1rem; margin-bottom: 1.1rem;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 0.8rem; flex-wrap: wrap; margin-bottom: 0.95rem; }
.panel-head h3 { margin: 0; font-size: 1.02rem; }

.toolbar { display: flex; align-items: center; gap: 0.65rem; flex-wrap: wrap; margin-bottom: 1rem; }
.search-input { flex: 1; min-width: 210px; }

.cards-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(310px, 1fr)); gap: 0.95rem; }
.entity-card {
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem;
  transition: border-color 0.15s ease;
}
.entity-card:hover { border-color: rgba(56, 189, 248, 0.45); }
.entity-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 0.6rem; }
.entity-title { font-weight: 700; font-size: 1rem; line-height: 1.25; }
.entity-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.8rem; color: var(--primary-2); margin-top: 0.18rem; }
.entity-meta { display: flex; flex-wrap: wrap; gap: 0.4rem 0.9rem; font-size: 0.8rem; color: var(--muted); }
.entity-stats-row { display: flex; gap: 0.45rem; flex-wrap: wrap; font-size: 0.76rem; }
.entity-actions { display: flex; gap: 0.45rem; flex-wrap: wrap; margin-top: auto; padding-top: 0.45rem; border-top: 1px solid var(--border); }

/* Badges */
.badge {
  display: inline-flex; align-items: center; gap: 0.25rem;
  padding: 0.2rem 0.58rem; border-radius: 999px; font-size: 0.74rem; font-weight: 700; white-space: nowrap;
}
.badge-ok { background: var(--ok-bg); color: #34d399; }
.badge-warn { background: var(--warn-bg); color: #fbbf24; }
.badge-danger { background: var(--danger-bg); color: #f87171; }
.badge-info { background: var(--info-bg); color: #38bdf8; }
.badge-muted { background: rgba(148, 163, 184, 0.16); color: var(--muted); }
html[data-theme="light"] .badge-ok { color: #047857; }
html[data-theme="light"] .badge-warn { color: #b45309; }
html[data-theme="light"] .badge-danger { color: #b91c1c; }
html[data-theme="light"] .badge-info { color: #0369a1; }

/* Tables */
.table-wrap { overflow-x: auto; }
table.pg-table { width: 100%; border-collapse: collapse; font-size: 0.86rem; }
table.pg-table th { text-align: left; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); padding: 0.65rem 0.7rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.pg-table td { padding: 0.72rem 0.7rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.pg-table tr:hover td { background: var(--bg-hover); }

/* Progress bar */
.vigencia-bar { height: 6px; border-radius: 999px; background: rgba(148,163,184,0.2); overflow: hidden; margin-top: 0.35rem; width: 120px; }
.vigencia-bar > span { display: block; height: 100%; border-radius: 999px; }

/* Modals */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(2, 6, 23, 0.72); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; padding: 1rem; z-index: 100;
}
.modal-card {
  width: 100%; max-width: 720px; max-height: 92vh; overflow-y: auto;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 16px;
  padding: 1.25rem; box-shadow: var(--shadow);
}
.modal-card.modal-lg { max-width: 960px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 0.8rem; padding-bottom: 0.8rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.modal-head h3 { margin: 0; font-size: 1.08rem; }
.modal-foot { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.15rem; padding-top: 0.85rem; border-top: 1px solid var(--border); }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.85rem; }
.form-grid .full { grid-column: 1 / -1; }

.chips-presets { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.25rem; }
.chip-btn {
  padding: 0.24rem 0.55rem; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); font-size: 0.76rem; cursor: pointer;
}
.chip-btn.active, .chip-btn:hover { border-color: var(--primary-2); color: var(--primary-2); background: rgba(56,189,248,0.12); }

.preview-box {
  background: var(--bg); border: 1px dashed var(--border); border-radius: 12px;
  padding: 0.85rem; font-size: 0.84rem;
}
.viewer-frame { width: 100%; height: 68vh; border: 1px solid var(--border); border-radius: 10px; background: #0b1120; }
.viewer-img { max-width: 100%; max-height: 68vh; display: block; margin: 0 auto; border-radius: 10px; }

.alert { padding: 0.75rem 0.9rem; border-radius: 10px; font-size: 0.85rem; margin-bottom: 0.85rem; }
.alert-danger { background: var(--danger-bg); border: 1px solid rgba(239,68,68,0.35); color: #fca5a5; }
.alert-info { background: var(--info-bg); border: 1px solid rgba(56,189,248,0.32); color: var(--primary-2); }
.alert-warn { background: var(--warn-bg); border: 1px solid rgba(245,158,11,0.35); color: #fbbf24; }
html[data-theme="light"] .alert-danger { color: #991b1b; }
html[data-theme="light"] .alert-warn { color: #92400e; }

.toast-root { position: fixed; right: 1rem; bottom: 4.5rem; display: flex; flex-direction: column; gap: 0.5rem; z-index: 150; }
.toast {
  background: var(--bg-elev); border: 1px solid var(--border); border-left: 4px solid var(--primary);
  padding: 0.7rem 1rem; border-radius: 10px; box-shadow: var(--shadow); font-size: 0.86rem; max-width: 340px;
}
.toast.ok { border-left-color: var(--ok); }
.toast.err { border-left-color: var(--danger); }

.empty-state { text-align: center; padding: 2.2rem 1rem; color: var(--muted); }
.mobile-only { display: none; }
.mobile-bottom-nav { display: none; }
.sidebar-backdrop { display: none; }

/* Responsive Mobile / Tablet */
@media (max-width: 920px) {
  .mobile-only { display: inline-grid; }
  .sidebar {
    position: fixed; left: -280px; top: 0; bottom: 0; transition: left 0.22s ease;
  }
  .sidebar.open { left: 0; }
  .sidebar-backdrop.open { display: block; position: fixed; inset: 0; background: rgba(2,6,23,0.6); z-index: 35; }
  .mobile-bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: var(--bg-elev); border-top: 1px solid var(--border);
    justify-content: space-around; padding: 0.35rem 0.2rem;
  }
  .mob-nav-btn {
    background: transparent; border: none; color: var(--muted);
    display: flex; flex-direction: column; align-items: center; gap: 0.1rem;
    font-size: 1rem; padding: 0.25rem 0.45rem; cursor: pointer;
  }
  .mob-nav-btn small { font-size: 0.66rem; }
  .mob-nav-btn.active { color: var(--primary-2); font-weight: 700; }
  .form-grid { grid-template-columns: 1fr; }
  .topbar { padding: 0.75rem 0.9rem; }
  .main-content { padding: 0.9rem 0.85rem 5.4rem; }
}

/* Impresión de Ficha de Operatividad */
@media print {
  .sidebar, .topbar, .mobile-bottom-nav, .no-print, .toast-root { display: none !important; }
  body, html { background: #ffffff !important; color: #000000 !important; }
  .panel, .entity-card { border: 1px solid #cbd5e1 !important; background: #ffffff !important; box-shadow: none !important; }
  .main-content { padding: 0 !important; max-width: 100% !important; }
}
