/*
 * iERP — componentes Twig compartidos (fase F1.5 hacia F2).
 *
 * Fuente de verdad: docs/10-architecture/design-system.md.
 * Pulido A (visual pass) — sigue §3 (tipografía), §5 (Card / Field /
 * Button / Badge / Dialog), §8 (auth) y §10 (focus + transiciones).
 *
 * Reglas:
 *   - Sin colores hardcoded: todo via `var(--*)` de tokens.css.
 *   - Sin reset propio: tokens.css ya configura body/headings.
 *   - Tamaños canónicos: h-9 default, h-10 auth, h-8 sm.
 */

/* ---------- Reset cooperativo mínimo ---------- */

.app, .app-wrap, .wrap, main { box-sizing: border-box; }
.app * { box-sizing: border-box; }

/* ---------- Topbar reutilizable ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 24px;
  background: var(--background);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--foreground);
}
.topbar .back, .topbar .logout, .topbar .nav-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--primary);
  font-size: .875rem;
  padding: 6px 10px;
  border-radius: calc(var(--radius) - 0.25rem);
  transition: background-color .15s ease, color .15s ease;
}
.topbar .back:hover, .topbar .logout:hover, .topbar .nav-link:hover {
  background: var(--muted);
}

/* ---------- Auth nav (tabs cluster perfil · usuarios · roles) ---------- */
/*
 * Se renderiza justo debajo del .topbar via `components/auth-nav.twig`.
 * Visualmente es una barra fina con tabs tipo pill; el item activo se
 * marca con fondo `--muted` + borde inferior `--primary` (analogía con
 * la tab list del DS §5 antes de que el shell real aterrice en F2).
 */
.auth-nav {
  background: var(--background);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 9;
}
.auth-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.auth-nav-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--foreground-muted-soft);
  font-size: .875rem;
  font-weight: 500;
  padding: 10px 12px;
  border-bottom: 2px solid transparent;
  transition: color .15s ease, border-color .15s ease, background-color .15s ease;
}
.auth-nav-link:hover {
  color: var(--foreground);
  background: var(--muted);
}
.auth-nav-link.is-active {
  color: var(--foreground);
  border-bottom-color: var(--primary);
}
.auth-nav-link:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px color-mix(in oklab, var(--ring) 60%, transparent);
  border-radius: calc(var(--radius) - 0.45rem);
}

/* ---------- Card (DS §5) ---------- */

.card {
  background: var(--background);
  color: var(--foreground-body);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 1px 2px color-mix(in oklab, var(--foreground) 4%, transparent),
              0 4px 12px color-mix(in oklab, var(--foreground) 4%, transparent);
}
.card h2 {
  margin: 0 0 4px;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--foreground);
}
.card .muted {
  margin: 0 0 16px;
  color: var(--foreground-muted-soft);
  font-size: .8125rem;  /* DS meta = text-xs+ */
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.card-header h2 { margin: 0; }

/* ---------- Field (DS §5 input) ---------- */

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  font-size: .75rem;
  font-weight: 500;
  color: var(--foreground-muted-soft);
  margin-bottom: 6px;
  letter-spacing: 0.005em;
}
.field input,
.field select,
.field textarea {
  width: 100%;
  height: 36px; /* DS h-9 default */
  padding: 0 12px;
  border: 1px solid var(--input);
  border-radius: calc(var(--radius) - 0.375rem); /* rounded-md ≈ 6px */
  font-size: .875rem;
  font-family: inherit;
  color: var(--foreground);
  background: var(--background);
  outline: none;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.field textarea {
  height: auto;
  padding: 8px 12px;
  min-height: 80px;
  resize: vertical;
}
.field input:focus-visible,
.field select:focus-visible,
.field textarea:focus-visible {
  border-color: var(--ring);
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--ring) 50%, transparent);
}
.field-error input,
.field-error select,
.field-error textarea {
  border-color: var(--destructive);
}
.field-error input:focus-visible {
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--destructive) 50%, transparent);
}

/* ---------- Buttons (DS §5) ---------- */

.btn-primary,
.btn-ghost,
.btn-destructive {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px; /* h-9 default */
  padding: 0 14px;
  border-radius: calc(var(--radius) - 0.375rem);
  font-family: inherit;
  font-size: .875rem;
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  text-decoration: none;
  user-select: none;
  white-space: nowrap;
  transition: background-color .15s ease, border-color .15s ease,
              color .15s ease, box-shadow .15s ease, opacity .15s ease;
}
.btn-primary {
  background: var(--primary);
  color: var(--primary-foreground);
  border: 1px solid var(--primary);
  font-weight: 600;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-active); border-color: var(--primary-active); }
.btn-primary:disabled { opacity: .55; cursor: wait; }
.btn-primary:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px color-mix(in oklab, var(--ring) 60%, transparent);
}
/* Botón large (DS §8 auth submit). Aplicar con .btn-primary.btn-lg */
.btn-lg { height: 40px; padding: 0 16px; font-size: .9rem; }
.btn-sm { height: 32px; padding: 0 10px; font-size: .8125rem; }

.btn-ghost {
  background: transparent;
  color: var(--foreground);
  border: 1px solid var(--border);
}
.btn-ghost:hover:not(:disabled) { background: var(--muted); }
.btn-ghost:disabled { opacity: .5; cursor: not-allowed; }
.btn-ghost:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px color-mix(in oklab, var(--ring) 60%, transparent);
}

.btn-destructive {
  background: var(--destructive);
  color: var(--destructive-foreground);
  border: 1px solid var(--destructive);
  font-weight: 600;
}
.btn-destructive:hover:not(:disabled) { opacity: .92; }
.btn-destructive:disabled { opacity: .5; cursor: not-allowed; }
.btn-destructive:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px color-mix(in oklab, var(--destructive) 60%, transparent);
}

/* Botón icono — para "×" en chips de roles, close de dialog, etc. */
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid transparent;
  background: transparent;
  color: var(--foreground-muted-soft);
  border-radius: calc(var(--radius) - 0.45rem);
  cursor: pointer;
  transition: color .15s ease, background-color .15s ease;
}
.btn-icon:hover:not(:disabled) { background: var(--muted); color: var(--foreground); }
.btn-icon:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Status / feedback ---------- */

.status { font-size: .8125rem; }
.status.ok    { color: var(--success); }
.status.error { color: var(--destructive); }

/* Error box estilo DS §8 (auth). */
.alert-error {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 14px;
  padding: 10px 12px;
  border: 1px solid color-mix(in oklab, var(--destructive) 30%, transparent);
  background: color-mix(in oklab, var(--destructive) 8%, var(--background));
  color: var(--destructive);
  border-radius: calc(var(--radius) - 0.375rem);
  font-size: .75rem;
}
.alert-error .icon { flex-shrink: 0; margin-top: 1px; }

/* ---------- Badge (DS §5) ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 20px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: .6875rem;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--muted);
  color: var(--foreground);
  white-space: nowrap;
}
.badge.badge-success {
  background: color-mix(in oklab, var(--success) 12%, var(--background));
  border-color: color-mix(in oklab, var(--success) 28%, var(--border));
  color: var(--success);
}
.badge.badge-warning {
  background: color-mix(in oklab, var(--warning) 12%, var(--background));
  border-color: color-mix(in oklab, var(--warning) 28%, var(--border));
  color: var(--warning);
}
.badge.badge-destructive {
  background: color-mix(in oklab, var(--destructive) 10%, var(--background));
  border-color: color-mix(in oklab, var(--destructive) 28%, var(--border));
  color: var(--destructive);
}
.badge.badge-muted {
  background: var(--muted);
  border-color: var(--border-soft);
  color: var(--foreground-muted-soft);
}

/* ---------- Inline icons (Lucide via Twig macro) ---------- */

.icon {
  display: inline-block;
  width: 16px;
  height: 16px;
  stroke-width: 2;
  vertical-align: -3px;
  flex-shrink: 0;
}
.icon-sm { width: 14px; height: 14px; vertical-align: -2px; }
.icon-xs { width: 12px; height: 12px; vertical-align: -1px; }

/* ---------- Page shell wrap ---------- */

.wrap {
  max-width: 1100px;
  margin: 24px auto;
  padding: 0 24px;
  display: grid;
  gap: 16px;
}
.wrap.wrap-narrow { max-width: 800px; }

/* ---------- Dialog (DS §5) ---------- */

.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: color-mix(in oklab, var(--foreground) 30%, transparent);
  backdrop-filter: blur(1px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: dialog-fade-in .12s ease-out;
}
.dialog {
  width: min(92vw, 420px);
  background: var(--background);
  color: var(--foreground-body);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 50px color-mix(in oklab, var(--foreground) 24%, transparent);
  overflow: hidden;
  animation: dialog-slide-up .15s ease-out;
}
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-soft);
}
.dialog-header h2 {
  margin: 0;
  font-size: .9375rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.dialog-body {
  padding: 16px;
  font-size: .875rem;
  color: var(--foreground-body);
}
.dialog-body p { margin: 0 0 8px; }
.dialog-body p:last-child { margin-bottom: 0; }
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border-soft);
  background: var(--surface-soft);
}

@keyframes dialog-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes dialog-slide-up {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .dialog, .dialog-backdrop { animation: none; }
}

/* ---------- App shell topbar (sub-card `cross.ui-shell-bridge.topbar`) ---------- */
/*
 * Topbar pensada para envolver pantallas migradas. Convive con el
 * `.topbar` minimal por pantalla (back/title/logout) que usan las
 * pantallas auth tempranas — esa se queda viva para no romper migradas
 * existentes; las nuevas se montan sobre `.app-shell-topbar`.
 */
.app-shell-topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  height: 56px;
  padding: 0 20px;
  background: var(--background);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
}
.app-shell-topbar__brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--foreground);
  text-decoration: none;
  font-weight: 600;
  font-size: .95rem;
  letter-spacing: -0.02em;
}
.app-shell-topbar__brand:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px color-mix(in oklab, var(--ring) 60%, transparent);
  border-radius: calc(var(--radius) - 0.5rem);
}
.app-shell-topbar__brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: calc(var(--radius) - 0.4rem);
  background: var(--primary);
  color: var(--primary-foreground);
  font-weight: 700;
  font-size: .8125rem;
  letter-spacing: -0.02em;
}
.app-shell-topbar__nav {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex: 1 1 auto;
  min-width: 0;
}
.app-shell-topbar__actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}
.app-shell-topbar__user {
  position: relative;
}
.app-shell-topbar__user-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--foreground);
  padding: 4px 8px 4px 4px;
  border-radius: 999px;
  cursor: pointer;
  font: inherit;
  font-size: .8125rem;
  transition: background-color .15s ease, border-color .15s ease;
}
.app-shell-topbar__user-trigger:hover {
  background: var(--muted);
}
.app-shell-topbar__user-trigger:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px color-mix(in oklab, var(--ring) 60%, transparent);
}
.app-shell-topbar__avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--muted);
  color: var(--foreground-muted-soft);
  object-fit: cover;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: .75rem;
  font-weight: 600;
  border: 1px solid var(--border-soft);
}
.app-shell-topbar__user-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 200px;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--popover);
  color: var(--popover-foreground);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 0.25rem);
  box-shadow: 0 8px 24px color-mix(in oklab, var(--foreground) 18%, transparent);
  z-index: var(--z-popover);
}
.app-shell-topbar__user-menu[hidden] { display: none; }
.app-shell-topbar__user-menu li { margin: 0; }
.app-shell-topbar__user-menu a,
.app-shell-topbar__user-menu button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  background: transparent;
  border: none;
  border-radius: calc(var(--radius) - 0.45rem);
  color: inherit;
  text-decoration: none;
  font: inherit;
  font-size: .8125rem;
  text-align: left;
  cursor: pointer;
}
.app-shell-topbar__user-menu a:hover,
.app-shell-topbar__user-menu button:hover {
  background: var(--muted);
}
.app-shell-topbar__user-menu .divider {
  height: 1px;
  background: var(--border-soft);
  margin: 4px 2px;
}

/* ---------- Theme toggle (DS §F1 — UI del runtime cerrado en #268) ---------- */

.theme-toggle {
  position: relative;
}
.theme-toggle__button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: calc(var(--radius) - 0.45rem);
  color: var(--foreground-muted-soft);
  cursor: pointer;
  transition: background-color .15s ease, color .15s ease, border-color .15s ease;
}
.theme-toggle__button:hover {
  background: var(--muted);
  color: var(--foreground);
}
.theme-toggle__button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px color-mix(in oklab, var(--ring) 60%, transparent);
}
/*
 * Sólo el icono que corresponde al estado actual es visible. El estado
 * se materializa en `data-theme-toggle-state` (set por theme-toggle.js)
 * para evitar depender de `html[data-theme=…]` y permitir múltiples
 * toggles desincronizados en transición.
 */
.theme-toggle__icon { display: none; }
.theme-toggle[data-theme-toggle-state="light"]  .theme-toggle__icon--light  { display: inline-flex; }
.theme-toggle[data-theme-toggle-state="dark"]   .theme-toggle__icon--dark   { display: inline-flex; }
.theme-toggle[data-theme-toggle-state="system"] .theme-toggle__icon--system { display: inline-flex; }
/* Fallback: si nadie aplicó el estado todavía, muestra "monitor" como neutro. */
.theme-toggle:not([data-theme-toggle-state]) .theme-toggle__icon--system { display: inline-flex; }

.theme-toggle__menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 160px;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--popover);
  color: var(--popover-foreground);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 0.25rem);
  box-shadow: 0 8px 24px color-mix(in oklab, var(--foreground) 18%, transparent);
  z-index: var(--z-popover);
}
.theme-toggle__menu[hidden] { display: none; }
.theme-toggle__menu li { margin: 0; }
.theme-toggle__menu button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  background: transparent;
  border: none;
  border-radius: calc(var(--radius) - 0.45rem);
  color: inherit;
  font: inherit;
  font-size: .8125rem;
  text-align: left;
  cursor: pointer;
}
.theme-toggle__menu button:hover { background: var(--muted); }
.theme-toggle__menu button[aria-checked="true"] {
  color: var(--foreground);
  font-weight: 600;
  background: color-mix(in oklab, var(--primary) 8%, transparent);
}

/* ============================================================
 * Dark mode — override de la paleta neutral.
 *
 * Por qué vive aquí y no en `tokens.css`:
 *   El bundle de Preline Pro (`main.min.css`) y el bundle Tailwind
 *   compilado (`tailwind.compiled.css`) declaran `.dark { --background:
 *   var(--color-neutral-800); ... }` con la misma specificity que
 *   tokens.css pero se cargan DESPUÉS (ver base.twig L52 vs L69-73), así
 *   que sus mappings ganan en el cascade. La forma robusta de oscurecer
 *   el shell sin tocar bundles compilados ni light mode es overridear
 *   `--color-neutral-*` aquí, en components.css (último en el order),
 *   bajo el selector `.dark`. Como los `.dark` de los bundles resuelven
 *   sus tokens a `var(--color-neutral-XXX)` en tiempo de uso, la
 *   redefinición se propaga a `--background`, `--background-1`,
 *   `--background-2`, `--card`, `--muted`, `--layer`, `--sidebar`, etc.
 *
 * Tailwind default vs. iERP dark:
 *   --color-neutral-600: 0.439 → 0.30
 *   --color-neutral-700: 0.371 → 0.23
 *   --color-neutral-800: 0.269 → 0.17   (background, card, layer, muted)
 *   --color-neutral-900: 0.205 → 0.13   (background-1, sidebar)
 *   --color-neutral-950: 0.145 → 0.10   (background-2, inverse)
 *
 * Sólo afecta a `.dark` (`<html class="dark">`); light mode queda intacto.
 * ============================================================ */
.dark {
  --color-neutral-600: oklch(0.30 0 0);
  --color-neutral-700: oklch(0.23 0 0);
  --color-neutral-800: oklch(0.17 0 0);
  --color-neutral-900: oklch(0.13 0 0);
  --color-neutral-950: oklch(0.10 0 0);
}
