/* ============================================================
   Centinell — Design System
   Single-source, no overrides, no layers.
   ============================================================ */

/* ── Tokens ─────────────────────────────────────────────────── */
:root {
  /* Brand */
  --brand:        #7c3aed;
  --brand-hover:  #6d28d9;
  --brand-soft:   #ede9fe;
  --brand-border: #c4b5fd;

  /* Neutrals */
  --canvas:    #f3f4f8;
  --surface:   #ffffff;
  --surface-2: #f8f9fc;
  --border:    #e2e6f0;
  --border-2:  #d0d6e8;

  /* Text */
  --text-1: #111827;
  --text-2: #3d4a66;
  --text-3: #7080a0;

  /* Sidebar (light) */
  --sidebar-bg:                #ffffff;
  --sidebar-nav-text:          #3d4a66;
  --sidebar-nav-active-bg:     var(--brand-soft);
  --sidebar-nav-active-border: var(--brand-border);
  --sidebar-nav-active-text:   var(--brand);
  --sidebar-border:            #e2e6f0;
  --sidebar-icon:              #7080a0;

  /* Semantic */
  --ok:          #0a7c50;
  --ok-soft:     #e8f7f0;
  --ok-border:   #b2dfc7;
  --warn:        #c0313b;
  --warn-soft:   #fdf0ef;
  --warn-border: #f0bdc0;
  --info:        #1d55cc;
  --info-soft:   #edf2ff;
  --info-border: #b8ccf8;

  /* Elevation */
  --shadow-xs: 0 1px 2px rgba(10, 20, 50, 0.06);
  --shadow-sm: 0 2px 8px rgba(10, 20, 50, 0.07);
  --shadow-md: 0 8px 24px rgba(10, 20, 50, 0.10);
  --shadow-lg: 0 20px 48px rgba(10, 20, 50, 0.14);

  /* Radius */
  --r-sm: 6px;
  --r:    8px;
  --r-lg: 12px;
  --r-xl: 16px;

  /* Typography */
  --font: -apple-system, BlinkMacSystemFont, "Aptos", "Segoe UI Variable", "Segoe UI", Roboto, sans-serif;
  --font-mono: "IBM Plex Mono", "Cascadia Code", "Consolas", monospace;
}

/* ── Dark mode ───────────────────────────────────────────────── */
body.dark {
  --canvas:    #0f1117;
  --surface:   #1a1d27;
  --surface-2: #22263a;
  --border:    #2d3348;
  --border-2:  #353b55;

  --text-1: #f0f2f8;
  --text-2: #b8c0d8;
  --text-3: #6070a0;

  --sidebar-bg:                #1a1d27;
  --sidebar-nav-text:          #b8c0d8;
  --sidebar-nav-active-bg:     rgba(124, 58, 237, 0.18);
  --sidebar-nav-active-border: rgba(124, 58, 237, 0.45);
  --sidebar-nav-active-text:   #a78bfa;
  --sidebar-border:            #2d3348;
  --sidebar-icon:              #6070a0;

  --brand-soft:   rgba(124, 58, 237, 0.18);
  --brand-border: rgba(124, 58, 237, 0.45);

  --ok-soft:     rgba(10, 124, 80, 0.15);
  --ok-border:   rgba(10, 124, 80, 0.35);
  --warn-soft:   rgba(192, 49, 59, 0.15);
  --warn-border: rgba(192, 49, 59, 0.35);
  --info-soft:   rgba(29, 85, 204, 0.15);
  --info-border: rgba(29, 85, 204, 0.35);

  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 20px 48px rgba(0, 0, 0, 0.55);
}

body.dark { background: var(--canvas); color: var(--text-1); }

/* Oculta ambas vistas mientras se verifica la sesión con el backend */
body.auth-loading #authGate,
body.auth-loading #appShell { visibility: hidden; pointer-events: none; }

body.dark .auth-gate {
  background:
    radial-gradient(ellipse at 20% 0%, rgba(91, 69, 224, 0.12) 0%, transparent 55%),
    radial-gradient(ellipse at 80% 100%, rgba(0, 140, 120, 0.07) 0%, transparent 50%),
    #0f1117;
}

body.dark tbody tr:hover { background: #242840; }

body.dark .bu-switcher select { color-scheme: dark; }

body.dark input, body.dark select, body.dark textarea { color-scheme: dark; }

body.dark option {
  background-color: #1e2035;
  color: #e2e4f0;
}

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

body {
  margin: 0;
  font-family: var(--font);
  font-size: 0.875rem;
  line-height: 1.55;
  color: var(--text-1);
  background: var(--canvas);
  -webkit-font-smoothing: antialiased;
}

/* ── Auth Gate ───────────────────────────────────────────────── */
.bg-shape { display: none; }

.auth-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(91, 69, 224, 0.10) 0%, transparent 55%),
    radial-gradient(ellipse at 80% 100%, rgba(0, 140, 120, 0.06) 0%, transparent 50%),
    #f3f4f8;
}

.auth-panel {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  padding: 2.5rem 2rem 2rem;
}

.auth-brand {
  text-align: center;
  font-size: 1.7rem;
  font-weight: 800;
  letter-spacing: 0.2em;
  color: var(--brand);
  margin-bottom: 0.3rem;
}

.auth-subtitle {
  display: block;
  text-align: center;
  color: var(--text-3);
  font-size: 0.82rem;
  margin: 0 0 1.8rem;
}

.auth-form {
  display: grid;
  gap: 0.9rem;
}

.auth-form label { margin: 0; }

.auth-form input {
  border-radius: var(--r);
  padding: 0.65rem 0.75rem;
  font-size: 0.875rem;
}

.auth-form button {
  margin-top: 0.2rem;
  padding: 0.7rem;
  font-size: 0.9rem;
  letter-spacing: 0.02em;
  border-radius: var(--r);
  width: 100%;
}

.auth-message {
  min-height: 1rem;
  margin: 0.75rem 0 0;
  text-align: center;
  font-size: 0.82rem;
  color: var(--text-3);
}

.auth-forgot {
  margin: 0.5rem 0 0;
  text-align: center;
}
.auth-forgot button {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.8rem;
  color: var(--brand);
  cursor: pointer;
  text-decoration: underline;
}
.auth-forgot button:hover { background: none; color: var(--brand-hover); transform: none; }

.auth-form-desc {
  margin: 0 0 0.75rem;
  font-size: 0.82rem;
  color: var(--text-2);
  line-height: 1.5;
}

.auth-alert {
  display: none;
  align-items: flex-start;
  gap: 0.6rem;
  margin-top: 1rem;
  padding: 0.8rem 0.9rem;
  background: var(--warn-soft);
  border: 1px solid var(--warn-border);
  border-radius: var(--r);
  font-size: 0.82rem;
  color: var(--warn);
  font-weight: 600;
  text-align: left;
  line-height: 1.45;
}

.auth-alert.is-visible { display: flex; }

.auth-alert svg {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  fill: var(--warn);
  margin-top: 1px;
}

/* ── App Shell ───────────────────────────────────────────────── */
#appShell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ── Topbar ──────────────────────────────────────────────────── */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  padding: 0 1.25rem;
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 100;
}

.topbar-brand { justify-self: start; }

.topbar h1 {
  margin: 0;
  font-size: 0.92rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--text-1);
}

.topbar p {
  display: none;
}

.bu-switcher {
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--surface-2);
  border: 1px solid var(--border-2);
  border-radius: 999px;
  padding: 0.2rem 0.5rem 0.2rem 0.75rem;
  box-shadow: var(--shadow-xs);
}

.bu-switcher label {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  white-space: nowrap;
}

.bu-switcher-controls { display: flex; align-items: center; }

.bu-switcher select {
  margin: 0;
  border: none;
  background: transparent;
  padding: 0.06rem 1.2rem 0.06rem 0.1rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-1);
  box-shadow: none;
  min-width: 0;
  width: auto;
  max-width: 200px;
  /* override global width:100% for this special select */
  flex: 1;
}

.bu-switcher select:focus {
  outline: none;
  box-shadow: none;
  text-decoration: underline;
}

.topbar-actions {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.auth-user-box {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.22rem 0.28rem 0.22rem 0.7rem;
  background: var(--surface);
  box-shadow: var(--shadow-xs);
}

.auth-user-box span {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-2);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.auth-user-box button {
  border-radius: 999px;
  padding: 0.28rem 0.7rem;
  font-size: 0.76rem;
  min-height: auto;
}

/* ── Account menu dropdown ───────────────────────────────────── */
.account-menu {
  position: relative;
}

.account-menu-trigger {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.account-chevron {
  width: 16px;
  height: 16px;
  fill: currentColor;
  transition: transform 0.15s ease;
}

.account-menu-trigger[aria-expanded="true"] .account-chevron {
  transform: rotate(180deg);
}

.account-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 140px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: var(--shadow-md, 0 4px 16px rgba(0,0,0,0.12));
  list-style: none;
  padding: 0.25rem 0;
  margin: 0;
  z-index: 200;
}

.account-dropdown.is-open {
  display: block;
}

.account-dropdown li button {
  display: block;
  width: 100%;
  padding: 0.45rem 0.9rem;
  text-align: left;
  background: none;
  border: none;
  font-size: 0.84rem;
  color: var(--text-1);
  cursor: pointer;
  min-height: auto;
  border-radius: 0;
}

.account-dropdown li button:hover {
  background: var(--surface-2);
}

/* ── Account modals ──────────────────────────────────────────── */
.account-modal {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  z-index: 900;
}

.account-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 10, 25, 0.5);
  backdrop-filter: blur(3px);
}

.account-modal-panel {
  position: relative;
  width: min(520px, calc(100% - 2rem));
  max-height: calc(100vh - 4rem);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg, calc(var(--r) * 1.5));
  box-shadow: var(--shadow-lg, 0 8px 32px rgba(0,0,0,0.18));
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.account-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.account-modal-header h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-1);
  letter-spacing: normal;
}

.account-modal-close {
  flex-shrink: 0;
}

.account-modal-close svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
}

.account-section {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.account-section:first-of-type {
  padding-top: 0;
  border-top: none;
}

.account-section h4 {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-2);
}

.account-section .form-row {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.account-section .form-row label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-2);
}

.account-section .form-row input {
  width: 100%;
  box-sizing: border-box;
}

.account-form-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 0.75rem;
  flex-wrap: wrap;
}

.account-msg {
  font-size: 0.82rem;
  flex: 1;
}

.account-msg--error  { color: var(--error); }
.account-msg--success { color: var(--success); }
.account-msg--loading { color: var(--text-2); }

.account-danger-zone {
  border-top-color: var(--error, #e53e3e);
}


.account-danger-zone p {
  font-size: 0.84rem;
  color: var(--text-2);
  margin: 0;
}

/* ── Organizations list ──────────────────────────────────────── */
.account-orgs-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding-top: 0.25rem;
}

.account-orgs-empty {
  font-size: 0.88rem;
  color: var(--text-2);
  margin: 0;
  text-align: center;
  padding: 1rem 0;
}

.account-org-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
}

.account-org-info {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.account-org-name {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.account-org-code {
  font-size: 0.76rem;
  color: var(--text-3, var(--text-2));
  font-family: monospace;
}

.account-org-role {
  font-size: 0.76rem;
  color: var(--text-2);
  font-style: italic;
}

.account-org-access-btn {
  flex-shrink: 0;
  font-size: 0.8rem;
  padding: 0.3rem 0.75rem;
  min-height: auto;
}

.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.22rem 0.65rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}

.status-chip.is-online  { color: var(--ok);   background: var(--ok-soft);   border-color: var(--ok-border); }
.status-chip.is-offline { color: var(--warn);  background: var(--warn-soft); border-color: var(--warn-border); }
.status-chip.is-loading { color: var(--info);  background: var(--info-soft); border-color: var(--info-border); }

/* ── Layout ──────────────────────────────────────────────────── */
.layout {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────────── */
.sidebar {
  width: 224px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-shrink: 0;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0.4rem 0.5rem;
}

.sidebar-toggle {
  width: 28px;
  height: 28px;
  min-height: 28px;
  min-width: 28px;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--sidebar-icon);
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.sidebar-toggle svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

.sidebar-toggle:hover {
  background: var(--border);
  color: var(--text-2);
  transform: none;
  box-shadow: none;
}

.nav-btn {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.55rem;
  width: 100%;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--sidebar-nav-text);
  padding: 0.52rem 0.6rem;
  font-size: 0.82rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: background 100ms ease, color 100ms ease;
}

.nav-btn .nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--sidebar-icon);
}

.nav-btn .nav-icon svg {
  width: 15px;
  height: 15px;
  fill: currentColor;
}

.nav-btn .nav-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-btn:hover {
  background: var(--surface-2);
  color: var(--text-1);
  box-shadow: none;
  transform: none;
}

.nav-btn.active {
  background: var(--sidebar-nav-active-bg);
  border-color: var(--sidebar-nav-active-border);
  color: var(--sidebar-nav-active-text);
  font-weight: 600;
}

.nav-btn.active .nav-icon { color: var(--brand); }

.sidebar-footer {
  flex-shrink: 0;
  padding: 0.4rem 0.5rem 0.6rem;
  border-top: 1px solid var(--sidebar-border);
  display: grid;
  gap: 2px;
}

.sidebar-footer-link {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.55rem;
  text-decoration: none;
  color: var(--text-3);
  border-radius: var(--r-sm);
  border: 1px solid transparent;
  padding: 0.46rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 500;
}

.sidebar-footer-link .nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.sidebar-footer-link .nav-icon svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

.sidebar-footer-link:hover {
  background: var(--surface-2);
  color: var(--text-1);
}

/* Collapsed sidebar */
body.sidebar-collapsed .sidebar {
  width: 56px;
}

body.sidebar-collapsed .sidebar-header {
  justify-content: center;
}

body.sidebar-collapsed .sidebar-nav {
  padding-left: 0.25rem;
  padding-right: 0.25rem;
}

body.sidebar-collapsed .nav-btn {
  justify-content: center;
  padding: 0.52rem 0;
}

body.sidebar-collapsed .nav-btn .nav-label,
body.sidebar-collapsed .sidebar-footer-link .nav-label {
  display: none;
}

body.sidebar-collapsed .sidebar-footer {
  padding-left: 0.25rem;
  padding-right: 0.25rem;
}

body.sidebar-collapsed .sidebar-footer-link {
  justify-content: center;
  padding: 0.46rem 0;
}

/* ── Content area ────────────────────────────────────────────── */
.content {
  flex: 1;
  min-width: 0;
  padding: 1rem;
  overflow-y: auto;
}

/* ── Cards / Views ───────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 1.25rem 1.35rem 1.1rem;
}

.view { display: none; }
.view.active { display: block; }

/* ── Typography ──────────────────────────────────────────────── */
h2 {
  margin: 0 0 0.2rem;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-1);
  letter-spacing: -0.01em;
}

h2 + p,
h2 + .section-lead {
  margin-top: 0.2rem;
}

h3 {
  margin: 1.2rem 0 0.55rem;
  font-size: 0.84rem;
  font-weight: 700;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

h4 {
  margin: 0 0 0.5rem;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.section-lead {
  margin: 0 0 1rem;
  color: var(--text-3);
  font-size: 0.84rem;
  line-height: 1.5;
}

/* ── Form controls ───────────────────────────────────────────── */
label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-2);
}

input, select, textarea, button { font: inherit; }

input, select, textarea {
  border: 1px solid var(--border-2);
  border-radius: var(--r);
  padding: 0.52rem 0.65rem;
  background: var(--surface);
  color: var(--text-1);
  font-size: 0.84rem;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

input::placeholder, textarea::placeholder {
  color: var(--text-3);
  font-weight: 400;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.12);
}

textarea { resize: vertical; }

.field-hint {
  margin-top: 0.1rem;
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--text-3);
}

/* ── Buttons ─────────────────────────────────────────────────── */
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: none;
  border-radius: var(--r);
  background: var(--brand);
  color: #fff;
  padding: 0.52rem 0.9rem;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  min-height: 34px;
  transition: background 120ms ease, transform 100ms ease, box-shadow 120ms ease;
  white-space: nowrap;
}

button:hover {
  background: var(--brand-hover);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(124, 58, 237, 0.28);
}

button:active { transform: translateY(0); box-shadow: none; }

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

button.secondary {
  background: var(--surface);
  color: var(--text-2);
  border: 1px solid var(--border-2);
  box-shadow: var(--shadow-xs);
}

button.secondary:hover {
  background: var(--surface-2);
  color: var(--text-1);
  box-shadow: var(--shadow-sm);
}

button.danger {
  background: var(--warn);
  color: #fff;
}

button.danger:hover {
  background: #a8282f;
  box-shadow: 0 4px 12px rgba(192, 49, 59, 0.28);
}

.icon-btn {
  width: 32px;
  min-width: 32px;
  height: 32px;
  min-height: 32px;
  padding: 0;
  border-radius: var(--r);
}

.icon-btn svg {
  width: 15px;
  height: 15px;
  fill: currentColor;
}

/* ── Layout helpers ──────────────────────────────────────────── */
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.5rem 0 0.9rem;
}

.grid-2, .grid-3 {
  display: grid;
  gap: 0.75rem;
  align-items: end;
}

.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: 1fr 1fr 1fr; }

.grid-2 > label, .grid-3 > label,
.run-form-col > label { margin-bottom: 0; }

/* Evitar desbordamiento en items de grid/flex */
.grid-2 > *, .grid-3 > *,
.run-form-col > *,
.collections-setup > *,
.assess-config-add-row > * { min-width: 0; }

.run-form-col {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* ── Status messages ─────────────────────────────────────────── */
.message {
  font-size: 0.8rem;
  border-radius: var(--r);
  padding: 0;
  margin-bottom: 0.9rem;
  border: 1px solid transparent;
  min-height: 1rem;
}

.message.is-idle    { padding: 0; border: none; background: none; }
.message.is-loading { padding: 0.52rem 0.7rem; color: var(--info);  background: var(--info-soft);  border-color: var(--info-border); }
.message.is-success { padding: 0.52rem 0.7rem; color: var(--ok);    background: var(--ok-soft);    border-color: var(--ok-border); }
.message.is-error   { padding: 0.52rem 0.7rem; color: var(--warn);  background: var(--warn-soft);  border-color: var(--warn-border); }

/* ── Tables ──────────────────────────────────────────────────── */
table {
  width: 100%;
  border-collapse: collapse;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
  background: var(--surface);
  margin-bottom: 1rem;
  font-size: 0.8rem;
}

th, td {
  text-align: left;
  padding: 0.48rem 0.6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

th {
  background: var(--surface-2);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-3);
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8f9fd; }

.empty-row {
  text-align: center;
  color: var(--text-3);
  font-style: italic;
  padding: 1.2rem;
}

/* ── Status badges ───────────────────────────────────────────── */
.status-badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.14rem 0.5rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border: 1px solid transparent;
}

.status-success, .status-validated { color: var(--ok);   background: var(--ok-soft);   border-color: var(--ok-border); }
.status-failed                      { color: var(--warn); background: var(--warn-soft); border-color: var(--warn-border); }
.status-pending, .status-running,
.status-processing,
.status-queued                      { color: var(--info); background: var(--info-soft); border-color: var(--info-border); }

.badge-assess {
  display: inline-block;
  font-size: 0.6rem;
  font-weight: 700;
  background: var(--brand-soft);
  color: var(--brand);
  border: 1px solid var(--brand-border);
  border-radius: 3px;
  padding: 1px 4px;
  vertical-align: middle;
  margin-right: 3px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  line-height: 1.4;
}

/* ── Run view ────────────────────────────────────────────────── */
.file-upload-row { margin-bottom: 0.75rem; }

.file-upload-row > label {
  display: block;
  margin: 0 0 0.35rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-2);
}

.file-upload-controls {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.6rem;
  align-items: center;
}

.run-results-layout {
  display: grid;
  grid-template-columns: minmax(0, 0.85fr) minmax(380px, 1.35fr);
  gap: 0.8rem;
  align-items: start;
}

.run-results-main { min-width: 0; }

.run-results-main th,
.run-results-main td { padding: 0.32rem 0.4rem; font-size: 0.76rem; }

.run-results-main th:first-child,
.run-results-main td:first-child { width: 30%; }

.run-results-main td input[data-answer-title] {
  font-size: 0.76rem;
  padding: 0.26rem 0.36rem;
  width: 100%;
}

.run-document-panel {
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  padding: 0.65rem 0.75rem;
}

.document-preview-live {
  margin: 0;
  min-height: 360px;
  max-height: 540px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.6;
  color: var(--text-2);
}

.detail-box {
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 0.8rem;
  background: var(--surface-2);
  min-height: 180px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: 1.55;
  color: var(--text-2);
}

.doc-highlight-match {
  background: #ffe87a;
  color: #2d2d2d;
  border-radius: 3px;
  padding: 0 2px;
}

/* Result row details */
.result-row-details {
  margin-top: 0.2rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  align-items: center;
}

.result-find-btn {
  width: 18px;
  min-width: 18px;
  height: 18px;
  min-height: 18px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--text-3);
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: none;
}

.result-find-btn svg { width: 11px; height: 11px; fill: currentColor; }

.result-find-btn:hover {
  background: var(--brand-soft);
  border-color: var(--brand-border);
  color: var(--brand);
  box-shadow: none;
  transform: none;
}

.result-desc-toggle {
  background: transparent;
  border: none;
  color: var(--text-3);
  font-size: 0.68rem;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 2px;
  min-height: auto;
  height: auto;
  padding: 0;
  line-height: 1;
  box-shadow: none;
}

.result-desc-toggle:hover {
  color: var(--brand);
  background: transparent;
  box-shadow: none;
  transform: none;
}

.result-detail-text {
  font-size: 0.72rem;
  color: var(--text-3);
  flex-basis: 100%;
  line-height: 1.45;
}

/* ── Export buttons ──────────────────────────────────────────── */
.export-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.6rem;
  flex-wrap: wrap;
}

.export-label {
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--text-3);
  white-space: nowrap;
}

.export-btn {
  padding: 0.28rem 0.7rem;
  font-size: 0.74rem;
  font-weight: 700;
  border-radius: var(--r-sm);
  cursor: pointer;
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text-2);
  min-width: auto;
  width: auto;
  min-height: auto;
  box-shadow: var(--shadow-xs);
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
}

.export-btn:hover {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  transform: none;
  box-shadow: none;
}

/* ── Collections ─────────────────────────────────────────────── */
.collections-upload { margin-top: 1.25rem; margin-bottom: 0.75rem; }

.docs-upload-zone.is-disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.col-summary {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  flex-wrap: wrap;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 0.75rem 1rem;
  margin-top: 0.75rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-2);
}

.col-status      { font-weight: 700; }
.col-status--pendiente  { color: var(--text-3); }
.col-status--extrayendo { color: var(--info); }
.col-status--procesando { color: #b06a00; }
.col-status--ok         { color: var(--ok); }
.col-status--error      { color: var(--warn); }
.col-error { font-size: 0.74rem; color: var(--warn); max-width: 200px; }

.section-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.25rem 0;
}

/* ── Configs view ────────────────────────────────────────────── */
.response-format-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin: 0.75rem 0 0.35rem;
}

.response-format-header h3 { margin: 0; }

.response-format-controls {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.compact-label {
  margin: 0;
  font-size: 0.76rem;
  color: var(--text-3);
  font-weight: 600;
  white-space: nowrap;
}

.response-format-controls select {
  width: auto;
  min-width: 110px;
  margin: 0;
  font-size: 0.8rem;
  padding: 0.32rem 0.5rem;
}

.response-format-panel { margin-bottom: 0.6rem; }
.response-format-panel textarea { width: 100%; }

.preview-selector-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

.preview-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.preview-chip {
  min-width: 28px;
  height: 26px;
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text-2);
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 0.4rem;
  min-height: auto;
  box-shadow: var(--shadow-xs);
}

.preview-chip.active {
  background: var(--brand-soft);
  color: var(--brand);
  border-color: var(--brand-border);
}

/* ── Users view ──────────────────────────────────────────────── */
.users-create-box {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: var(--r-lg);
  padding: 1rem 1.1rem;
  margin-bottom: 1rem;
}

.users-create-box h3 { margin-top: 0; }
.users-create-grid { margin-top: 0.25rem; }
.users-create-actions { margin-top: 0.2rem; }

.users-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }

.users-actions button {
  min-height: 28px;
  padding: 0.3rem 0.55rem;
  font-size: 0.72rem;
}

.users-role-select { min-width: 110px; font-size: 0.8rem; }

/* ── Audit ───────────────────────────────────────────────────── */
.audit-details-cell {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  color: var(--text-3);
  max-width: 280px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Confirm modal ───────────────────────────────────────────── */
.confirm-modal {
  position: fixed;
  inset: 0;
  display: none;
  z-index: 1000;
}

.confirm-modal.is-open {
  display: grid;
  place-items: center;
}

.confirm-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 10, 25, 0.5);
  backdrop-filter: blur(3px);
}

.confirm-modal-panel {
  position: relative;
  width: min(400px, calc(100% - 2rem));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  padding: 1.25rem 1.35rem;
}

.confirm-modal-panel h3 {
  margin: 0 0 0.4rem;
  font-size: 0.95rem;
  color: var(--text-1);
  text-transform: none;
  letter-spacing: normal;
}

.confirm-modal-panel p {
  margin: 0;
  font-size: 0.84rem;
  color: var(--text-2);
}

.confirm-modal-actions {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* ── Documents View ─────────────────────────────────────────── */

.docs-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.docs-header h2 { margin: 0; }

@keyframes spin {
  to { transform: rotate(360deg); }
}

.spinner-mini {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid var(--brand-border);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
  flex-shrink: 0;
}

.docs-polling-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 12px 3px 8px;
  background: var(--brand-soft);
  border: 1px solid var(--brand-border);
  border-radius: 999px;
  font-size: 0.73rem;
  font-weight: 600;
  color: var(--brand);
  letter-spacing: 0.01em;
}

/* Upload zone */
.docs-upload-zone {
  border: 2px dashed var(--border-2);
  border-radius: var(--r-lg);
  background: var(--surface-2);
  cursor: pointer;
  transition: border-color 0.18s, background 0.18s;
  margin-bottom: 20px;
}

.docs-upload-zone:hover,
.docs-upload-zone.drag-over {
  border-color: var(--brand);
  background: var(--brand-soft);
}

.docs-upload-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 28px 24px 22px;
  gap: 6px;
  text-align: center;
  pointer-events: none;
}

.docs-upload-icon {
  width: 44px;
  height: 44px;
  fill: var(--text-3);
  transition: fill 0.18s;
}

.docs-upload-zone:hover .docs-upload-icon,
.docs-upload-zone.drag-over .docs-upload-icon {
  fill: var(--brand);
}

.docs-upload-label {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-2);
}

.docs-upload-link {
  color: var(--brand);
  font-weight: 600;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
  pointer-events: auto;
}

.docs-upload-hint {
  margin: 0;
  font-size: 0.75rem;
  color: var(--text-3);
}

/* Upload progress */
.docs-upload-progress {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}

.docs-upload-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  font-size: 0.82rem;
}

.docs-upload-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-2);
}

.docs-upload-item-status {
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}

.docs-upload-item-status.uploading { color: var(--brand); }
.docs-upload-item-status.done      { color: var(--ok); }
.docs-upload-item-status.error     { color: var(--warn); }

/* List header */
.docs-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.docs-count {
  font-size: 0.78rem;
  color: var(--text-3);
  font-weight: 500;
}

/* Document table */
.docs-table { width: 100%; }

.docs-table td { vertical-align: middle; }

.docs-filename-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.docs-file-icon {
  width: 20px;
  height: 20px;
  fill: var(--text-3);
  flex-shrink: 0;
}

.docs-filename {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 260px;
  color: var(--text-1);
  font-weight: 500;
}

.docs-size { color: var(--text-3); white-space: nowrap; }
.docs-date  { color: var(--text-3); white-space: nowrap; font-size: 0.8rem; }

/* Status badges */
.doc-status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.73rem;
  font-weight: 700;
  white-space: nowrap;
  letter-spacing: 0.01em;
}

.doc-status svg {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
  fill: currentColor;
}

.doc-status.pending {
  background: var(--info-soft);
  color: var(--info);
  border: 1px solid var(--info-border);
}

.doc-status.processing {
  background: var(--brand-soft);
  color: var(--brand);
  border: 1px solid var(--brand-border);
}

.doc-status.processing .spinner-mini {
  width: 11px;
  height: 11px;
  border-width: 1.5px;
}

.doc-status.processed {
  background: var(--ok-soft);
  color: var(--ok);
  border: 1px solid var(--ok-border);
}

.doc-status.failed {
  background: var(--warn-soft);
  color: var(--warn);
  border: 1px solid var(--warn-border);
}

.docs-row-actions {
  display: flex;
  gap: 5px;
  align-items: center;
}

.docs-load-more {
  margin-top: 16px;
  text-align: center;
}

/* Docs table row - clickable when processed */
.docs-table tbody tr[data-doc-status="processed"] {
  cursor: default;
}

/* ── Toggle switch ──────────────────────────────────────────── */

.toggle-switch {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
}
.toggle-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-track {
  width: 36px;
  height: 20px;
  background: var(--border-2);
  border-radius: 10px;
  position: relative;
  transition: background 0.18s;
  flex-shrink: 0;
}
.toggle-track::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 50%;
  top: 3px;
  left: 3px;
  transition: transform 0.18s;
  box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.toggle-switch input:checked + .toggle-track { background: var(--brand); }
.toggle-switch input:checked + .toggle-track::after { transform: translateX(16px); }

/* ── Variable form row ──────────────────────────────────────── */

.var-form-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}
.var-form-row > label { flex: 1; min-width: 120px; margin: 0; }
.var-desc-label { flex: 2 !important; }
.var-type-label { flex: 0 0 110px !important; min-width: 0 !important; }
.var-required-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding-bottom: 4px;
  flex-shrink: 0;
}
.var-required-label {
  font-size: 0.72rem;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* ── Temperatura ────────────────────────────────────────────── */

.temp-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.3rem;
}
.temp-row input[type=range] {
  flex: 1;
  accent-color: var(--brand);
  cursor: pointer;
}
.temp-value {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--brand);
  min-width: 2.5rem;
  text-align: right;
}

/* ── Assessments View ───────────────────────────────────────── */

.assess-layout {
  display: grid;
  grid-template-columns: minmax(300px, 420px) 1fr;
  gap: 24px;
  align-items: start;
}

/* Detail view */

.assess-detail-layout {
  display: grid;
  grid-template-columns: 55% 1fr;
  gap: 24px;
  align-items: start;
}

.card-inner {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 1.1rem 1.2rem;
  margin-bottom: 20px;
}

.assess-form h3,
.assess-run h2 { margin-top: 0; }

.assess-config-add-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-bottom: 8px;
}

.assess-config-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-height: 32px;
}

.assess-config-empty {
  font-size: 0.8rem;
  color: var(--text-3);
  padding: 6px 0;
}

.assess-config-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  font-size: 0.83rem;
}

.assess-config-item-name {
  flex: 1;
  font-weight: 500;
  color: var(--text-1);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.assess-config-pos {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-3);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 7px;
}

.assess-config-remove {
  color: var(--warn);
  background: transparent;
  border: none;
  padding: 0 2px;
  min-height: auto;
  height: 20px;
  width: 20px;
  min-width: auto;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.assess-config-remove:hover { background: var(--warn-soft); border-radius: var(--r-sm); box-shadow: none; transform: none; }

.assess-list-title { margin-bottom: 8px; }

.assess-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.assess-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 12px 14px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.assess-card { cursor: pointer; }
.assess-card:hover { border-color: var(--brand); box-shadow: var(--shadow-xs); }

.assess-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 4px;
}

.assess-card-name {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--text-1);
}

.assess-card-desc {
  font-size: 0.8rem;
  color: var(--text-3);
  margin-bottom: 8px;
}

.assess-card-configs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 10px;
}

.assess-config-tag {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--brand-soft);
  color: var(--brand);
  border: 1px solid var(--brand-border);
}

.assess-card-actions { display: flex; gap: 6px; }

/* Run panel */
.assess-run-form { margin-bottom: 20px; }

.assess-run-results { margin-top: 4px; }

.assess-results-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.assess-results-header h3 { margin: 0; }

.assess-run-meta {
  font-size: 0.78rem;
  color: var(--text-3);
}

.assess-section {
  margin-bottom: 16px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
}

.assess-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.assess-section-name {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--text-1);
}

.assess-section-meta {
  font-size: 0.75rem;
  color: var(--text-3);
}

.assess-section-error {
  padding: 10px 14px;
  font-size: 0.82rem;
  color: var(--warn);
}

.assess-section table {
  width: 100%;
  margin: 0;
  border-radius: 0;
}

/* ══════════════════════════════════════════════════════════════
   RESPONSIVE
   1100px  tablet landscape  — nav se vuelve barra horizontal
    900px  tablet portrait   — layouts de 2 col se apilan
    800px  tablet pequeño    — grids 1 col, tablas con scroll
    640px  móvil landscape   — nav scrollable + topbar compacto
    480px  móvil portrait    — máxima compactación
   ══════════════════════════════════════════════════════════════ */

/* ── 1100px: tablet landscape ────────────────────────────────── */
@media (max-width: 1100px) {
  #appShell { height: auto; overflow: visible; }

  .layout { flex-direction: column; overflow: visible; }
  .content { overflow-y: visible; }

  /* Sidebar → barra horizontal */
  .sidebar {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    overflow: visible;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .sidebar-header { border: none; padding: 0.4rem 0.5rem; flex-shrink: 0; }

  .sidebar-nav {
    flex-direction: row;
    flex-wrap: wrap;
    flex: none;
    overflow: visible;
    padding: 0.3rem 0.25rem;
    gap: 4px;
  }

  .sidebar-footer {
    flex-direction: row;
    border-top: none;
    padding: 0.3rem 0.25rem;
  }

  /* Sidebar colapsado en tablet: resetear a barra horizontal */
  body.sidebar-collapsed .sidebar                     { width: 100%; }
  body.sidebar-collapsed .sidebar-nav                 { flex-direction: row; padding-left: 0.25rem; padding-right: 0.25rem; }
  body.sidebar-collapsed .sidebar-footer              { padding-left: 0.25rem; padding-right: 0.25rem; }
  body.sidebar-collapsed .nav-btn .nav-label,
  body.sidebar-collapsed .sidebar-footer-link .nav-label { display: inline; }
  body.sidebar-collapsed .nav-btn                     { justify-content: flex-start; padding: 0.52rem 0.6rem; }
  body.sidebar-collapsed .sidebar-footer-link         { justify-content: flex-start; padding: 0.46rem 0.6rem; }

  /* Topbar */
  .topbar { grid-template-columns: 1fr; height: auto; padding: 0.6rem 1rem; gap: 0.5rem; }
  .topbar-brand, .bu-switcher, .topbar-actions { justify-self: stretch; }
  .bu-switcher { justify-content: flex-start; border-radius: var(--r); }
  .bu-switcher select { max-width: none; flex: 1; }
}

/* ── 900px: tablet portrait ──────────────────────────────────── */
@media (max-width: 900px) {
  /* Assessments: de 2 cols a 1 */
  .assess-layout { grid-template-columns: 1fr; }
  .assess-detail-layout { grid-template-columns: 1fr; }

  /* Run y doc detail: apilan en tablet */
  .run-detail-layout:not(.preview-collapsed) { grid-template-columns: 1fr; }
  .doc-detail-layout {
    grid-template-columns: 1fr;
    grid-template-areas: "preview" "form";
  }

  /* En tablet las columnas no se anclan al viewport — scroll de página normal */
  .doc-detail-left,
  .doc-detail-right,
  .run-detail-results,
  .run-detail-doc { max-height: none; overflow-y: visible; }

  /* Preview con altura proporcional en tablet */
  .doc-preview-wrap,
  .run-detail-doc .doc-preview-wrap { height: 55vw; min-height: 260px; max-height: 60vh; }
  .doc-detail-right .document-preview-live,
  .run-detail-doc .document-preview-live { max-height: 55vw; }
}

/* ── 800px: tablet pequeño / móvil landscape ─────────────────── */
@media (max-width: 800px) {
  /* Grids */
  .grid-2, .grid-3        { grid-template-columns: 1fr; }
  .file-upload-controls   { grid-template-columns: 1fr; }
  .run-results-layout     { grid-template-columns: 1fr; }
  .document-preview-live  { min-height: 220px; max-height: 320px; }
  .card                   { padding: 1rem; }

  /* Tablas: scroll horizontal en lugar de desbordarse */
  .table-scroll-wrap      { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
  /* Excepciones: tablas pequeñas que no necesitan scroll */
  .run-results-main table,
  .assess-section table {
    display: table;
    overflow-x: visible;
    white-space: normal;
  }
  th, td { white-space: nowrap; }
  .run-results-main th,
  .run-results-main td,
  .assess-section th,
  .assess-section td { white-space: normal; }

  /* Topbar: BU selector y acciones más compactos */
  .topbar { padding: 0.5rem 0.75rem; }
  .auth-user-box span { max-width: 120px; }
}

/* ── 640px: móvil landscape / smartphones ────────────────────── */
@media (max-width: 640px) {
  /* Topbar muy compacto */
  .topbar { padding: 0.45rem 0.6rem; gap: 0.35rem; }
  .topbar h1 { font-size: 0.88rem; }
  .auth-user-box span { max-width: 90px; font-size: 0.72rem; }
  .auth-user-box button { padding: 0.25rem 0.55rem; font-size: 0.72rem; }

  /* Sidebar → nav scrollable horizontal tipo app */
  .sidebar {
    flex-wrap: nowrap;
    border-bottom: 1px solid var(--border);
    overflow: hidden;
  }

  /* Ocultar botón colapsar en móvil (no tiene sentido) */
  .sidebar-header { display: none; }

  /* Nav: scroll horizontal sin mostrar scrollbar */
  .sidebar-nav {
    flex-direction: row;
    flex-wrap: nowrap;
    flex: 1;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0.25rem 0.35rem;
    gap: 2px;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
  .sidebar-nav::-webkit-scrollbar { display: none; }

  /* Botones de nav: icono arriba, label pequeño abajo */
  .nav-btn {
    flex-direction: column;
    align-items: center;
    gap: 0.1rem;
    padding: 0.4rem 0.45rem;
    min-width: 52px;
    max-width: 68px;
    flex-shrink: 0;
    min-height: 50px;
  }
  .nav-btn .nav-label {
    font-size: 0.58rem;
    text-align: center;
    white-space: normal;
    line-height: 1.2;
    overflow: visible;
    text-overflow: clip;
  }
  .nav-btn .nav-icon         { width: 20px; height: 20px; }
  .nav-btn .nav-icon svg     { width: 17px; height: 17px; }

  /* Ocultar footer de sidebar en móvil */
  .sidebar-footer { display: none; }

  /* Resetear sidebar colapsado en móvil */
  body.sidebar-collapsed .sidebar     { width: 100%; }
  body.sidebar-collapsed .sidebar-nav { flex-direction: row; flex-wrap: nowrap; }
  body.sidebar-collapsed .nav-btn .nav-label { display: inline; }
  body.sidebar-collapsed .nav-btn     { flex-direction: column; align-items: center; padding: 0.4rem 0.45rem; justify-content: center; }

  /* Contenido: menos padding */
  .content { padding: 0.6rem 0.5rem; }
  .card    { padding: 0.75rem 0.65rem; border-radius: var(--r); }

  /* Targets táctiles: mínimo 44 px (Apple HIG) */
  button          { min-height: 40px; }
  .icon-btn       { width: 40px; min-width: 40px; height: 40px; min-height: 40px; }
  .export-btn,
  .auth-user-box button,
  .sidebar-toggle { min-height: 36px; }

  /* Formularios: más espacio entre campos */
  label         { gap: 0.4rem; margin-bottom: 0.8rem; }
  input, select, textarea { padding: 0.6rem 0.7rem; font-size: 0.875rem; }

  /* Upload zone: más compacta */
  .docs-upload-inner  { padding: 18px 14px 14px; gap: 4px; }
  .docs-upload-icon   { width: 36px; height: 36px; }
  .docs-upload-label  { font-size: 0.82rem; }

  /* Historial detalle */
  .detail-box { min-height: 120px; font-size: 0.72rem; }

  /* Col summary */
  .col-summary { flex-direction: column; align-items: flex-start; gap: 0.6rem; }

  /* Assess: layouts internos */
  .assess-config-add-row { grid-template-columns: 1fr auto; }
  .assess-card-actions   { flex-wrap: wrap; }

  /* Users create box: sin grid-3 en móvil (ya apilado por 800px) */
  .users-create-box { padding: 0.75rem; }

  /* Confirm modal */
  .confirm-modal-panel { padding: 1rem; }

  /* Copy-to-BU: stack */
  .copy-to-bu-row { flex-direction: column; align-items: stretch; }
  .copy-to-bu-row select { width: 100%; }

  /* API Keys: stack código de clave */
  .apikey-copy-row { flex-direction: column; align-items: stretch; }
  .apikey-value    { width: 100%; }
  .apikey-copy-btn { width: 100%; }

  /* Doc detail topbar */
  .doc-detail-topbar { gap: 0.5rem; margin-bottom: 0.75rem; }

  /* ── Tabla de documentos: layout fijo para que rellene el ancho ── */
  /* Cancelar el display:block heredado del breakpoint 800px */
  .docs-table {
    display: table;
    table-layout: fixed;
    width: 100%;
    white-space: normal;
  }
  .docs-table th,
  .docs-table td { white-space: normal; overflow: hidden; text-overflow: ellipsis; }

  /* Columnas visibles en móvil (640px): Nombre · Subido · Estado · Acciones */
  .docs-table th:nth-child(1), .docs-table td:nth-child(1) { width: 38%; } /* Nombre */
  .docs-table th:nth-child(4), .docs-table td:nth-child(4) { width: 22%; } /* Subido */
  .docs-table th:nth-child(5), .docs-table td:nth-child(5) { width: 22%; } /* Estado */
  .docs-table th:nth-child(6), .docs-table td:nth-child(6) { width: 18%; } /* Acciones */

  /* Doc preview en móvil: altura más compacta */
  .doc-preview-wrap {
    height: 50vw;
    min-height: 200px;
    max-height: 50vh;
  }
}

/* ── 480px: móvil portrait ───────────────────────────────────── */
@media (max-width: 480px) {
  .topbar   { padding: 0.4rem 0.5rem; }
  .content  { padding: 0.5rem 0.4rem; }
  .card     { padding: 0.6rem 0.5rem; }

  /* Nav: iconos aún más pequeños */
  .nav-btn          { min-width: 46px; max-width: 60px; padding: 0.35rem 0.3rem; min-height: 46px; }
  .nav-btn .nav-icon svg { width: 15px; height: 15px; }
  .nav-btn .nav-label    { font-size: 0.55rem; }

  /* Docs table en portrait: solo Nombre · Estado · Acciones */
  .docs-table th:nth-child(2), .docs-table td:nth-child(2) { display: none; } /* Subido por */
  .docs-table th:nth-child(3), .docs-table td:nth-child(3) { display: none; } /* Tamaño */
  .docs-table th:nth-child(4), .docs-table td:nth-child(4) { display: none; } /* Fecha subida */
  /* Rebalancear 3 columnas */
  .docs-table th:nth-child(1), .docs-table td:nth-child(1) { width: 55%; }
  .docs-table th:nth-child(5), .docs-table td:nth-child(5) { width: 25%; }
  .docs-table th:nth-child(6), .docs-table td:nth-child(6) { width: 20%; }

  /* Historial: ocultar columnas menos críticas */
  #historyTableBody tr td:nth-child(2),  /* ID */
  table thead tr th:nth-child(2):has(~ th:nth-child(8)) { display: none; }

  h2 { font-size: 0.95rem; }
  h3 { font-size: 0.78rem; }
}

/* ── Copy to BU ──────────────────────────────────────────────── */
.copy-to-bu-section { margin-top: 0.5rem; }

.copy-to-bu-row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-top: 0.75rem;
}
.copy-to-bu-row select { flex: 1; }

/* ── Doc detail / Run detail ─────────────────────────────── */
.doc-detail-topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.doc-detail-title-area {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
}

.doc-detail-title-area h2 { margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  white-space: nowrap;
  flex-shrink: 0;
}

.back-btn svg { width: 16px; height: 16px; fill: currentColor; flex-shrink: 0; }

/* ── Variable compartida para altura de paneles de detalle ───── */
:root { --panel-h: calc(100vh - 220px); }

.doc-detail-layout {
  display: grid;
  grid-template-columns: 2fr 3fr;
  grid-template-areas: "form preview";
  gap: 1.5rem;
  align-items: start;
}

/* Asignamos área a cada columna por nombre, independiente del orden en el DOM */
.doc-detail-left  { grid-area: form; min-width: 0; }
.doc-detail-right { grid-area: preview; min-width: 0; }

/* Columna izquierda (form + runs): scroll propio */
.doc-detail-left {
  max-height: var(--panel-h);
  overflow-y: auto;
}

/* Columna derecha (preview): contiene sin scroll — el doc-preview-wrap scrollea */
.doc-detail-right {
  max-height: var(--panel-h);
  overflow-y: hidden;
}

/* El preview llena la columna y scrollea internamente — sin doble barra */
.doc-detail-right .document-preview-live,
.run-detail-doc .document-preview-live {
  max-height: none;
  overflow-y: visible;
}

.doc-preview-wrap {
  width: 100%;
  height: var(--panel-h);
  min-height: 400px;
  overflow-y: auto;
}

.doc-preview-frame {
  width: 100%;
  height: 100%;
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface-2);
}

.doc-preview-img {
  max-width: 100%;
  border-radius: var(--r);
  border: 1px solid var(--border);
}

.doc-preview-wrap .detail-box {
  max-height: 100%;
  overflow-y: auto;
  margin: 0;
}

.run-detail-layout {
  display: grid;
  grid-template-columns: 2fr 3fr;
  gap: 1.5rem;
  align-items: start;
}
.run-detail-layout > * { min-width: 0; }

/* Preview colapsado: resultados ocupan todo el ancho */
.run-detail-layout.preview-collapsed {
  grid-template-columns: 1fr;
}
.run-detail-layout.preview-collapsed .run-detail-doc .run-detail-doc-inner {
  display: none;
}
.run-detail-layout.preview-collapsed .run-detail-doc-header {
  justify-content: flex-start;
  gap: 0.5rem;
}
.run-detail-layout.preview-collapsed #runDetailCollapseBtn svg {
  transform: rotate(180deg);
}

.run-detail-results {
  overflow-y: auto;
  max-height: var(--panel-h);
}

.run-detail-doc {
  max-height: var(--panel-h);
  overflow-y: hidden;
}

.run-detail-doc .doc-preview-wrap {
  height: calc(var(--panel-h) - 52px); /* minus doc header height */
}

.run-detail-doc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}
.run-detail-doc-title {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.run-detail-doc-title h3 { margin: 0; }
.run-export-btns { display: flex; gap: 0.4rem; }
.run-export-btns button { font-size: 0.78rem; padding: 0.3rem 0.7rem; }

/* Doc run cards */
.doc-run-card {
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 0.7rem 0.9rem;
  margin-bottom: 0.5rem;
  background: var(--surface-2);
  transition: background 0.15s, border-color 0.15s;
}

.doc-run-card.clickable { cursor: pointer; }

.doc-run-card.clickable:hover {
  background: var(--surface-3, var(--surface-2));
  border-color: var(--accent);
}

.doc-run-card-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.doc-run-card-name { font-weight: 500; flex: 1; font-size: 0.9rem; }
.doc-run-card-meta { font-size: 0.76rem; color: var(--text-3); }

/* Clickable filename in docs list */
.docs-filename-clickable { cursor: pointer; }
.docs-filename-clickable:hover .docs-filename {
  text-decoration: underline;
  color: var(--accent);
}


/* ── PDF.js viewer ───────────────────────────────────────────── */
.doc-preview-wrap {
  overflow-y: auto;
}

.pdfViewerWrap {
  background: #606060;
  padding: 8px;
  min-height: 100%;
}

.pdfPage {
  position: relative;
  margin: 0 auto 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.4);
  line-height: 0;
}

.pdfCanvas { display: block; }

.pdfTextLayer {
  position: absolute;
  left: 0; top: 0;
  overflow: hidden;
  line-height: 1;
  pointer-events: auto;
}

.pdfTextLayer span {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
}

.pdfTextLayer span::selection {
  background: rgba(0, 0, 255, 0.25);
  color: transparent;
}

.pdfTextLayer span.pdfHighlight {
  background: rgba(255, 215, 0, 0.55);
  border-radius: 2px;
}

.pdfTextLayer span.pdfHighlightActive {
  background: rgba(255, 130, 0, 0.75);
}

.preview-loading {
  padding: 16px;
  color: var(--text-2);
  font-style: italic;
  text-align: center;
}

/* ── Reasoning / lupa per field ──────────────────────────────── */
.assess-section table th:last-child,
.assess-section table td:last-child {
  width: 52px;
  padding-right: 8px;
}

.field-actions {
  display: flex;
  gap: 4px;
  align-items: center;
  justify-content: flex-end;
}

.btn-reasoning, .btn-lupa {
  background: none;
  border: none;
  cursor: pointer;
  padding: 3px 5px;
  border-radius: 4px;
  color: var(--text-3);
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  transition: background 0.12s, color 0.12s;
}

.btn-reasoning:hover, .btn-lupa:hover {
  background: var(--surface-3, var(--surface-2));
  color: var(--accent);
}

.btn-reasoning.active, .btn-lupa.active { color: var(--accent); }

.btn-lupa svg { width: 15px; height: 15px; vertical-align: middle; }

.reasoning-row td { padding: 0 !important; }

.reasoning-box {
  display: none;
  margin: 0 8px 6px;
  padding: 6px 10px;
  font-size: 0.78rem;
  color: var(--text-2);
  font-style: italic;
  background: var(--bg-2, var(--surface));
  border-left: 3px solid var(--accent);
  border-radius: 0 4px 4px 0;
  line-height: 1.5;
}

.reasoning-box.open { display: block; }

/* ── BU code badge (documents table) ─────────── */
.docs-bu-code { white-space: nowrap; }
.bu-code-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  background: var(--accent-alpha, rgba(99,102,241,0.12));
  color: var(--accent);
  text-transform: uppercase;
}

/* ── API Keys ─────────────────────────────────── */
.apikeys-create-box {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.5rem;
}
.apikey-new-key-box {
  margin-top: 1rem;
  padding: 1rem;
  background: var(--success-bg, #d4edda);
  border: 1px solid var(--success-border, #28a745);
  border-radius: var(--radius);
}
.apikey-warning {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--success-text, #155724);
  margin: 0 0 0.5rem;
}
.apikey-copy-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.apikey-value {
  font-family: var(--font-mono, monospace);
  font-size: 0.85rem;
  word-break: break-all;
  flex: 1;
  padding: 0.4rem 0.6rem;
  background: var(--surface-0);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm, 4px);
}
.apikey-copy-btn { flex-shrink: 0; }
.apikey-revoke-btn {
  font-size: 0.8rem;
  padding: 0.25rem 0.6rem;
  color: var(--danger, #dc3545);
  border-color: var(--danger, #dc3545);
}
.apikey-revoke-btn:hover { background: var(--danger, #dc3545); color: #fff; }
.apikeys-usage-box {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}
.apikeys-usage-box .detail-box {
  font-size: 0.82rem;
  line-height: 1.6;
}

/* ══════════════ DASHBOARD ═══════════════════════════════════════════ */
.dashboard-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 1rem 1.2rem;
  text-align: center;
}
.stat-card--alert { border-color: var(--warn-border); background: var(--warn-soft); }
.stat-value { font-size: 1.8rem; font-weight: 700; color: var(--brand); line-height: 1.1; }
.stat-card--alert .stat-value { color: var(--warn); }
.stat-label { font-size: 0.75rem; color: var(--text-3); margin-top: 0.3rem; }
.dash-chart-wrap { position: relative; width: 100%; }
.dash-chart-wrap canvas { width: 100% !important; }

/* ══════════════ WEBHOOKS ════════════════════════════════════════════ */
.webhook-events-check {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.2rem;
  margin-top: 0.4rem;
}
.webhook-events-check label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.83rem;
  font-weight: 400;
  cursor: pointer;
}

/* ══════════════ NOTIFICACIONES ══════════════════════════════════════ */
.notify-pref-box {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.5rem;
  padding: 0.9rem 1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r);
}
.toggle-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-weight: 400;
}
