/**
 * Componentes de interfaz: botones, tarjetas, tablas, formularios, modales y toasts.
 */

/* Botones */
button {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.9rem;
  min-height: 2.5rem;
  font-weight: 500;
  font-size: 0.84rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  transition: all 0.12s ease;
  user-select: none;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

button:focus:not(:focus-visible) {
  outline: none;
  box-shadow: none;
}

.btn-primary {
  background: var(--primary);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-primary:focus-visible {
  outline: 2px solid transparent;
  outline-offset: 2px;
  box-shadow: 0 0 0 2px #ffffff, 0 0 0 5px var(--focus-ring);
}

.btn-secondary {
  background: var(--primary-soft);
  color: var(--primary);
  border: 1px solid var(--primary-border);
}

.btn-secondary:hover:not(:disabled) {
  background: #dbeafe;
  border-color: var(--primary);
  color: var(--primary-hover);
}

.btn-secondary:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn-success {
  background: var(--success-strong);
  color: #ffffff;
}

.btn-success:hover:not(:disabled) {
  background: #065f46;
}

.btn-success:focus-visible {
  outline: 2px solid transparent;
  outline-offset: 2px;
  box-shadow: 0 0 0 2px #ffffff, 0 0 0 5px var(--focus-ring);
}

.btn-danger {
  background: var(--danger);
  color: #ffffff;
}

.btn-danger:hover:not(:disabled) {
  background: #b91c1c;
}

.btn-danger:focus-visible {
  outline: 2px solid transparent;
  outline-offset: 2px;
  box-shadow: 0 0 0 2px #ffffff, 0 0 0 5px var(--focus-ring);
}

.btn-outline {
  background: var(--bg-surface);
  border-color: var(--border-strong);
  color: var(--text-main);
}

.btn-outline:hover:not(:disabled) {
  background: var(--bg-subtle);
  border-color: #9ca3af;
}

.btn-outline:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn-outline-danger {
  background: #ffffff;
  border: 1px solid var(--danger-border);
  color: var(--danger);
}

.btn-outline-danger:hover:not(:disabled) {
  background: var(--danger-soft);
  border-color: #f87171;
  color: #b91c1c;
}

.btn-outline-danger:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn-ghost-danger {
  background: transparent;
  color: var(--text-muted);
  padding: 0.25rem 0.45rem;
  border: 1px solid transparent;
  min-height: 2.125rem;
}

.btn-ghost-danger:hover:not(:disabled) {
  background: var(--danger-soft);
  color: var(--danger);
}

.btn-ghost-danger:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.76rem;
  min-height: 2.125rem;
}

.btn-full {
  width: 100%;
}

.btn-lg {
  padding: 0.75rem;
  font-size: 0.92rem;
  font-weight: 600;
  min-height: 2.75rem;
}

/* CTA Primaria de cierre: elemento visualmente mas fuerte del mostrador */
.btn-success.btn-lg {
  min-height: 3rem;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.col-span-2 {
  grid-column: span 2;
}

.mb-0 {
  margin-bottom: 0 !important;
}

.subtitle-hint {
  font-size: 0.75rem;
}

.card-subtitle-section {
  margin-bottom: 0.75rem;
  font-weight: 600;
  font-size: 0.88rem;
  color: var(--text-main);
}

/* Tarjetas */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  margin-bottom: 1.25rem;
  overflow: hidden;
}

.card-header {
  padding: 0.95rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  background: var(--bg-surface);
}

.card-title {
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text-main);
}

.card-body {
  padding: 1.25rem;
}

/* Formularios */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
  gap: 0.85rem;
  margin-bottom: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

label {
  font-size: 0.76rem;
  color: var(--text-secondary);
  font-weight: 600;
}

input, select, textarea {
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  color: var(--text-main);
  padding: 0.5rem 0.65rem;
  min-height: 2.5rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-family: inherit;
  transition: border-color 0.12s, box-shadow 0.12s;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid transparent;
  outline-offset: 1px;
  border-color: var(--focus-ring);
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.28);
}

input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid transparent;
  outline-offset: 1px;
  border-color: var(--focus-ring);
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.35);
}

/* Tablas */
.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

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

th {
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 0.04em;
  background: var(--bg-subtle);
}

tbody tr:hover {
  background: #f8fafc;
}

tbody tr:last-child td {
  border-bottom: none;
}

/* Etiquetas y estados */
.badge {
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  white-space: nowrap;
}

.badge-ok {
  background: var(--success-soft);
  color: var(--success-strong);
  border: 1px solid var(--success-border);
}

.badge-low {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid var(--danger-border);
}

.badge-warn {
  background: var(--warning-soft);
  color: var(--warning-strong);
  border: 1px solid var(--warning-border);
}

.badge-neutral {
  background: var(--bg-subtle);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

/* Modales accesibles */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.55);
  backdrop-filter: blur(2px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.modal-backdrop.open {
  display: flex;
}

.modal {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  max-width: 480px;
  width: 100%;
  padding: 1.5rem;
  box-shadow: var(--shadow-md);
  position: relative;
}

.modal-header {
  margin-bottom: 1rem;
}

.modal-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 0.35rem;
}

.modal-info {
  font-size: 0.84rem;
  color: var(--text-muted);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.25rem;
}

/* Notificaciones Toast */
#toast-container {
  position: fixed;
  top: 1.25rem;
  right: 1.25rem;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 380px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  background: #111827;
  color: #ffffff;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  font-size: 0.84rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  gap: 0.6rem;
  border-left: 4px solid var(--success-strong);
  animation: slideIn 0.2s ease-out;
  cursor: pointer;
}

.toast.error {
  border-left-color: var(--danger);
}

.toast.advertencia,
.toast.warn {
  border-left-color: var(--warning-strong);
}

.toast.info {
  border-left-color: var(--primary);
}

.toast-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.toast-icon svg {
  width: 18px;
  height: 18px;
}

.toast-message {
  flex: 1;
  word-break: break-word;
}

@keyframes slideIn {
  from {
    transform: translateY(12px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* Campos de formulario con toggle de contraseña y validacion */
.password-field-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.password-field-wrap input {
  width: 100%;
  padding-right: 2.5rem;
}

.btn-toggle-password {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  padding: 0.25rem;
  min-height: auto;
  width: 2rem;
  height: 2rem;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: color 0.15s, background 0.15s;
}

.btn-toggle-password:hover {
  color: var(--text-main);
  background: var(--bg-subtle);
}

.btn-toggle-password:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn-toggle-password svg {
  width: 16px;
  height: 16px;
}

/* Indicador visual de error en inputs y mensajes de formulario */
.form-group input.input-error,
.form-group input[aria-invalid="true"],
.form-group select[aria-invalid="true"],
input.input-error,
input[aria-invalid="true"],
select[aria-invalid="true"] {
  border: 2px solid var(--danger) !important;
  padding: calc(0.5rem - 1px) calc(0.65rem - 1px);
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.15) !important;
}

.form-group input.input-error:focus,
.form-group input[aria-invalid="true"]:focus,
.form-group select[aria-invalid="true"]:focus,
input.input-error:focus,
input[aria-invalid="true"]:focus,
select[aria-invalid="true"]:focus {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.3) !important;
}

.form-error {
  color: var(--danger);
  font-size: 0.82rem;
  font-weight: 500;
  margin-top: 0.3rem;
  display: flex;
  gap: 0.35rem;
  align-items: flex-start;
  background: #ffffff;
}

.form-error svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  margin-top: 0.15rem;
}

.form-error[hidden] {
  display: none !important;
}

.form-error-block {
  display: flex;
  gap: 0.45rem;
  align-items: flex-start;
  padding: 0.65rem 0.85rem;
  margin-bottom: 0.85rem;
  border-left: 3px solid var(--danger);
  background: #ffffff;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  box-shadow: var(--shadow-sm);
}

.form-error-block[hidden] {
  display: none !important;
}

/* Boton sutil con borde discontinuo (demo/auditoria) */
.btn-demo {
  background: transparent;
  border: 1px dashed var(--border-strong);
  color: var(--text-secondary);
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.55rem 0.85rem;
  min-height: 2.5rem;
  border-radius: var(--radius-sm);
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  transition: all 0.15s;
}

.btn-demo:hover:not(:disabled) {
  background: var(--bg-subtle);
  border-color: var(--text-muted);
  color: var(--text-main);
}

.btn-demo:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Perfil y control de operador en barra superior (Topbar) */
.topbar-operator {
  display: inline-flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.3rem 0.65rem;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
}

.topbar-operator[hidden] {
  display: none !important;
}

.topbar-operator-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.topbar-operator-avatar svg {
  width: 14px;
  height: 14px;
}

.topbar-operator-info {
  display: flex;
  align-items: center;
  gap: 0.45rem;
}

.topbar-operator-nombre {
  font-weight: 600;
  color: var(--text-main);
}

.badge-admin {
  background: #eef2ff;
  color: #3730a3;
  border: 1px solid #c7d2fe;
}

.badge-operador {
  background: #f3f4f6;
  color: #374151;
  border: 1px solid #e5e7eb;
}

.btn-logout {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  padding: 0.25rem 0.45rem;
  min-height: 2.125rem;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.76rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-logout:hover {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: var(--danger-border);
}

.btn-logout:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.btn-logout svg {
  width: 14px;
  height: 14px;
}

/* Soporte de alto contraste para botones rellenos en modo forced-colors */
@media (forced-colors: active) {
  .btn-primary:focus-visible,
  .btn-success:focus-visible,
  .btn-danger:focus-visible {
    outline: 3px solid Highlight;
    outline-offset: 2px;
    box-shadow: none;
  }
}
