:root {
  color-scheme: dark;   /* controles nativos (calendario, scrollbars, selects) en tema oscuro */
  --body-bg-color: #020617;
  --body-fg-color: #f8fafc;
  --card-bg-color: #0f172a;
  --enfasis-color: #38bdf8;
  --enfasis-color-dark: #0a7bb0;
  --muted-color: #7c8b9d;
  --border-color: rgba(255, 255, 255, 0.08);
  --border-color-btn: #0a2c4e;

  --border-color-strong: rgba(255, 255, 255, 0.14);
  --surface-hover: rgba(255, 255, 255, 0.05);
  /* superficie con degradado sutil: paneles, tarjetas, tablas, filtros */
  --grad-surface: linear-gradient(180deg, color-mix(in srgb, var(--card-bg-color) 97%, #fff), var(--card-bg-color));

  /* Estados: apagados para no competir con el contenido; legibles sobre fondo oscuro */
  --color-success: #3fbf7f;
  --color-warning: #e3b341;
  --color-danger: #e5636b;
  --color-info: #5b9bef;
}

html, body {
  height: 100%;
  margin: 0px;
}
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

/* Foco visible en cualquier elemento interactivo (los componentes pueden afinarlo) */
:where(a, button, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--enfasis-color);
  outline-offset: 2px;
}

/* Preferencia de movimiento reducido: único uso justificado de !important (anular animaciones de cualquier hoja) */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }   /* iOS no infla el texto al girar el móvil */
a, button, input, select, textarea, label, summary { touch-action: manipulation; }   /* sin zoom por doble toque ni retardo */

body {
  margin: 0;
  padding: 0;
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont,"Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  letter-spacing: -0.01em;
  background-color: #020617;
  color: var(--body-fg-color);
  margin: 0;
}

h1, h2, h3, h4, h5, p {
  color: var(--body-fg-color);
}
h2 { font-size: clamp(1rem, 0.95rem + 0.3vw, 1.15rem); }
h3 { font-size: clamp(0.95rem, 0.9rem + 0.2vw, 1.05rem); }

/* ---------- Layout con sidebar ---------- */
:root { --sidebar-w: 232px; --sidebar-w-collapsed: 68px; }
@media (max-width: 1024px) { :root { --sidebar-w: 208px; } }

.app-shell { display: flex; min-height: 100vh; min-height: 100dvh; }
.app-content { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.sidebar {
  position: sticky;
  top: 0;
  align-self: flex-start;
  box-sizing: border-box;   /* el padding no debe sumarse a los 100vh (provocaba scroll) */
  height: 100vh;
  height: 100dvh;
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding: 0.9rem 0.65rem;
  background: var(--body-bg-color);
  border-right: 1px solid var(--border-color);
  transition: width 0.2s ease;
  z-index: 40;
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
  color: var(--body-fg-color);
  text-decoration: none;
  font-weight: 600;
}
.sidebar-brand img, .brand-logo { width: 36px; height: 36px; border-radius: 8px; flex-shrink: 0; object-fit: contain; background: var(--card-bg-color); }
.brand-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; font-size: 0.95rem; }
.topbar-brand { display: inline-flex; align-items: center; gap: 0.6rem; min-width: 0; }
.topbar-brand strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.95rem; }

.sidebar-collapse {
  display: inline-flex;
  padding: 0.4rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--muted-color);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.2s;
}
.sidebar-collapse:hover { background: var(--surface-hover); color: var(--body-fg-color); }

.sidebar-nav { display: flex; flex-direction: column; gap: 0.2rem; flex: 1; min-height: 0; overflow-y: auto; }

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  color: var(--muted-color);
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.sidebar-nav a i { width: 1.25rem; text-align: center; flex-shrink: 0; }
.sidebar-nav a:hover { background: var(--surface-hover); color: var(--body-fg-color); }
.sidebar-nav a.active {
  color: var(--enfasis-color);
  background: transparent;   /* el activo se distingue solo por el color */
}
.nav-group { margin: 0.9rem 0 0.2rem; padding: 0 0.7rem; font-size: 0.68rem; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted-color); opacity: 0.8; }
.nav-badge { margin-left: auto; min-width: 1.35rem; padding: 0.05rem 0.4rem; border-radius: 999px; text-align: center; font-size: 0.7rem; font-weight: 600; line-height: 1.3; color: var(--enfasis-color); background: color-mix(in srgb, var(--enfasis-color) 14%, transparent); }
.sidebar-upload { display: flex; flex-shrink: 0; justify-content: flex-start; gap: 0.75rem; margin-top: 0.75rem; padding-inline: 0.7rem; }
.sidebar-upload i { width: 1.25rem; text-align: center; flex-shrink: 0; }
.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

.sidebar-bottom {
  flex-shrink: 0;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.sidebar-user {
  flex: 1; min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  color: var(--body-fg-color);
  text-decoration: none;
  font-size: 0.9rem;
  transition: background 0.15s;
}
.sidebar-user:hover { background: var(--surface-hover); }
.sidebar-user i { width: 1.25rem; text-align: center; flex-shrink: 0; color: var(--muted-color); }
.sidebar-user-text { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
.sidebar-user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.sidebar-role {
  flex-shrink: 0;
  padding: 0.05rem 0.5rem;
  border-radius: 999px;
  font-size: 0.7rem;
  color: var(--enfasis-color);
  background: color-mix(in srgb, var(--enfasis-color) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--enfasis-color) 28%, transparent);
}
.sidebar-bottom form { margin: 0; }
.sidebar-logout { box-sizing: border-box; flex-shrink: 0; width: 2.5rem; border: none; margin: 0; justify-content: center; padding: 0.55rem 0; border-radius: 8px; font-size: 0.92rem; }
.sidebar-logout i { width: 1.25rem; text-align: center; }
.sidebar-logout:hover { color: var(--color-danger); }

/* Contraído (escritorio): solo iconos */
.sidebar-collapsed .sidebar { width: var(--sidebar-w-collapsed); }
.sidebar-collapsed .nav-label { display: none; }
.sidebar-collapsed .sidebar-top { flex-direction: column; }
.sidebar-collapsed .sidebar-collapse { transform: rotate(180deg); }
.sidebar-collapsed .sidebar-nav a,
.sidebar-collapsed .sidebar-upload { justify-content: center; padding-inline: 0; }
.sidebar-collapsed .nav-group { height: 1px; margin: 0.7rem 0.6rem; padding: 0; background: var(--border-color); overflow: hidden; }
.sidebar-collapsed .nav-badge { display: none; }
.sidebar-collapsed .sidebar-bottom { flex-direction: column; }
.sidebar-collapsed .sidebar-user { justify-content: center; padding-inline: 0; }

/* Barra superior y panel deslizante (móvil) */
.topbar { display: none; }
.sidebar-overlay { display: none; }

@media (max-width: 768px) {
  .topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* env(): zona segura (notch/isla) cuando la página usa viewport-fit=cover; 0 en caso contrario */
    padding: calc(0.5rem + env(safe-area-inset-top)) max(0.75rem, env(safe-area-inset-right)) 0.5rem max(0.75rem, env(safe-area-inset-left));
    background: var(--body-bg-color);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 30;
  }
  .topbar .brand-logo { width: 32px; height: 32px; border-radius: 8px; }
  .topbar #sidebar-open { min-width: 44px; min-height: 44px; padding: 0; font-size: 1.05rem; }
  .sidebar-nav a, .sidebar-user, .sidebar-logout { min-height: 44px; box-sizing: border-box; }
  .sidebar-collapsed .sidebar-bottom { flex-direction: row; }
  .sidebar-collapsed .nav-group { height: auto; margin: 0.9rem 0 0.2rem; padding: 0 0.7rem; background: none; overflow: visible; }
  .sidebar-collapsed .nav-badge { display: inline-block; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(80vw, 280px);
    padding-top: calc(0.9rem + env(safe-area-inset-top));
    padding-bottom: calc(0.9rem + env(safe-area-inset-bottom));
    padding-left: calc(0.65rem + env(safe-area-inset-left));
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 60;                       /* por encima de las barras fijas de las páginas (p. ej. acciones del detalle) */
  }
  .sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-open .sidebar-overlay { display: block; position: fixed; inset: 0; background: rgba(2, 6, 23, 0.6); z-index: 55; }
  body:has(.sidebar-open) { overflow: hidden; }   /* el fondo no se desplaza con el panel abierto */
  .sidebar-nav { overscroll-behavior: contain; }
  .sidebar-collapse { display: none; }

  /* En móvil el estado "contraído" no aplica */
  .sidebar-collapsed .sidebar { width: min(80vw, 280px); }
  .sidebar-collapsed .nav-label { display: inline; }
  .sidebar-collapsed .sidebar-user-text { display: flex; }
  .sidebar-collapsed .sidebar-nav a, .sidebar-collapsed .sidebar-user, .sidebar-collapsed .sidebar-upload { justify-content: flex-start; padding-inline: 0.7rem; }
}

.app-main {
  padding: 1.5rem 1.25rem 2.5rem;
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
  flex: 1;
}
@media (max-width: 640px) {
  .app-main { padding: 1.25rem max(1rem, env(safe-area-inset-right)) 1.5rem max(1rem, env(safe-area-inset-left)); }
}

/* Táctil: objetivos de al menos 44 px en la navegación */
@media (pointer: coarse) {
  .sidebar-nav a, .sidebar-user, .sidebar-logout, .sidebar-upload { min-height: 2.75rem; box-sizing: border-box; }
  .topbar .btn { min-width: 2.75rem; min-height: 2.75rem; }
}

@media (min-width: 1025px) { .app-main { padding: 2rem 1.5rem 3rem; } }
@media (max-width: 640px) { .app-main { padding: 1rem 1rem 2rem; } }

.messages {
    list-style: none;
    padding: 0;
    margin: 1em 0;
}

.messages .message {
    padding: 0.65rem 1rem;
    margin-bottom: 0.6rem;
    border-radius: 8px;
    font-weight: 500;
    font-size: 0.9rem;
    overflow-wrap: anywhere;
    color: var(--tone, var(--body-fg-color));
    background: color-mix(in srgb, var(--tone, #fff) 12%, transparent);
    border: 1px solid color-mix(in srgb, var(--tone, #fff) 28%, transparent);
}

.messages .success { --tone: var(--color-success); }
.messages .error   { --tone: var(--color-danger); }
.messages .warning { --tone: var(--color-warning); }
.messages .info    { --tone: var(--color-info); }

/* Página de error */
.error-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 70vh;
  min-height: 70dvh;
}

.error-card {
  box-sizing: border-box;
  text-align: center;
  padding: clamp(1.5rem, 6vw, 3rem);
  background: var(--card-bg-color);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  max-width: 500px;
}
@media (max-width: 480px) { .error-card { padding: 2rem 1.25rem; } }

.error-code {
  font-size: clamp(3.5rem, 18vw, 5rem);
  font-weight: 700;
  margin-bottom: 1rem;
  color: var(--enfasis-color);
}
@media (max-width: 480px) { .error-code { font-size: 4rem; } }

.error-actions {
  margin-top: 1.5rem;
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
}
